ac3_decoder_thread.c 13.9 KB
Newer Older
Michel Lespinasse's avatar
 
Michel Lespinasse committed
1
/*****************************************************************************
Michel Lespinasse's avatar
 
Michel Lespinasse committed
2
 * ac3_decoder_thread.c: ac3 decoder thread
3 4
 *****************************************************************************
 * Copyright (C) 1999, 2000 VideoLAN
5
 * $Id: ac3_decoder_thread.c,v 1.19 2000/12/21 13:25:50 massiot Exp $
6 7 8 9 10 11 12
 *
 * Authors:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
13
 * 
14 15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
18
 *
19 20 21
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
Michel Lespinasse's avatar
 
Michel Lespinasse committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *****************************************************************************/

/*
 * TODO :
 *
 * - vrifier l'tat de la fifo de sortie avant d'y stocker les samples
 *   dcods ;
 * - vlc_cond_signal() / vlc_cond_wait()
 *
 */

/*****************************************************************************
 * Preamble
 *****************************************************************************/
36 37
#include "defs.h"

Michel Lespinasse's avatar
 
Michel Lespinasse committed
38 39 40 41
#include <unistd.h>                                              /* getpid() */

#include <stdio.h>                                           /* "intf_msg.h" */
#include <stdlib.h>                                      /* malloc(), free() */
42
#include <sys/types.h>                        /* on BSD, uio.h needs types.h */
Michel Lespinasse's avatar
 
Michel Lespinasse committed
43 44 45
#include <sys/uio.h>                                            /* "input.h" */

#include "config.h"
46 47
#include "common.h"
#include "threads.h"
Michel Lespinasse's avatar
 
Michel Lespinasse committed
48
#include "mtime.h"
49
#include "plugins.h"
Michel Lespinasse's avatar
 
Michel Lespinasse committed
50 51 52 53
#include "debug.h"                                      /* "input_netlist.h" */

#include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */

54 55
#include "stream_control.h"
#include "input_ext-dec.h"
Michel Lespinasse's avatar
 
Michel Lespinasse committed
56 57 58 59 60

#include "audio_output.h"

#include "ac3_decoder.h"
#include "ac3_decoder_thread.h"
Michel Lespinasse's avatar
 
Michel Lespinasse committed
61 62

#define AC3DEC_FRAME_SIZE (2*1536) 
Michel Lespinasse's avatar
 
Michel Lespinasse committed
63 64 65 66

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Michel Lespinasse's avatar
 
Michel Lespinasse committed
67 68 69 70
static int      InitThread              (ac3dec_thread_t * p_adec);
static void     RunThread               (ac3dec_thread_t * p_adec);
static void     ErrorThread             (ac3dec_thread_t * p_adec);
static void     EndThread               (ac3dec_thread_t * p_adec);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
71 72 73 74

/*****************************************************************************
 * ac3dec_CreateThread: creates an ac3 decoder thread
 *****************************************************************************/
75
vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
Michel Lespinasse's avatar
 
Michel Lespinasse committed
76 77 78
{
    ac3dec_thread_t *   p_ac3dec;

79
    intf_DbgMsg( "ac3dec debug: creating ac3 decoder thread\n" );
Michel Lespinasse's avatar
 
Michel Lespinasse committed
80 81

    /* Allocate the memory needed to store the thread's structure */
82 83
    if ((p_ac3dec = (ac3dec_thread_t *)malloc (sizeof(ac3dec_thread_t))) == NULL)
    {
84 85
        intf_ErrMsg ( "ac3dec error: not enough memory "
                      "for ac3dec_CreateThread() to create the new thread\n");
86
        return 0;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
87 88 89 90 91
    }

    /*
     * Initialize the thread properties
     */
92 93
    p_ac3dec->p_config = p_config;
    p_ac3dec->p_fifo = p_config->decoder_config.p_decoder_fifo;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
94 95 96 97

    /* Initialize the ac3 decoder structures */
    ac3_init (&p_ac3dec->ac3_decoder);

Michel Lespinasse's avatar
 
Michel Lespinasse committed
98 99 100
    /*
     * Initialize the output properties
     */
101
    p_ac3dec->p_aout = p_config->p_aout;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
102 103 104
    p_ac3dec->p_aout_fifo = NULL;

    /* Spawn the ac3 decoder thread */
105 106
    if (vlc_thread_create(&p_ac3dec->thread_id, "ac3 decoder", (vlc_thread_func_t)RunThread, (void *)p_ac3dec))
    {
107
        intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread\n" );
Michel Lespinasse's avatar
 
Michel Lespinasse committed
108
        free (p_ac3dec);
109
        return 0;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
110 111
    }

Michel Lespinasse's avatar
 
Michel Lespinasse committed
112
    intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created\n", p_ac3dec);
113
    return p_ac3dec->thread_id;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
114 115 116 117 118 119 120
}

/* Following functions are local */

/*****************************************************************************
 * InitThread : initialize an ac3 decoder thread
 *****************************************************************************/
Michel Lespinasse's avatar
 
Michel Lespinasse committed
121
static int InitThread (ac3dec_thread_t * p_ac3dec)
Michel Lespinasse's avatar
 
Michel Lespinasse committed
122 123
{
    aout_fifo_t         aout_fifo;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
124
    ac3_byte_stream_t * byte_stream;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
125

Michel Lespinasse's avatar
 
Michel Lespinasse committed
126
    intf_DbgMsg ("ac3dec debug: initializing ac3 decoder thread %p\n", p_ac3dec);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
127

128 129 130 131 132 133 134 135 136 137 138
    /* Get the first data packet. */
    vlc_mutex_lock( &p_ac3dec->p_fifo->data_lock );
    while ( DECODER_FIFO_ISEMPTY( *p_ac3dec->p_fifo ) )
    {
        if ( p_ac3dec->p_fifo->b_die )
        {
            vlc_mutex_unlock( &p_ac3dec->p_fifo->data_lock );
            return -1;
        }
        vlc_cond_wait( &p_ac3dec->p_fifo->data_wait, &p_ac3dec->p_fifo->data_lock );
    }
139
    p_ac3dec->p_data = DECODER_FIFO_START(*p_ac3dec->p_fifo)->p_first;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
140
    byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
141 142
    byte_stream->p_byte = p_ac3dec->p_data->p_payload_start;
    byte_stream->p_end = p_ac3dec->p_data->p_payload_end;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
143
    byte_stream->info = p_ac3dec;
144
    vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
145 146 147 148 149 150 151 152

    aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
    aout_fifo.i_channels = 2;
    aout_fifo.b_stereo = 1;

    aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;

    /* Creating the audio output fifo */
153 154
    if ((p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL)
    {
Michel Lespinasse's avatar
 
Michel Lespinasse committed
155
        return -1;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
156 157
    }

Michel Lespinasse's avatar
 
Michel Lespinasse committed
158 159
    intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p initialized\n", p_ac3dec);
    return 0;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
160 161 162 163 164
}

/*****************************************************************************
 * RunThread : ac3 decoder thread
 *****************************************************************************/
Michel Lespinasse's avatar
 
Michel Lespinasse committed
165
static void RunThread (ac3dec_thread_t * p_ac3dec)
Michel Lespinasse's avatar
 
Michel Lespinasse committed
166
{
Michel Lespinasse's avatar
 
Michel Lespinasse committed
167 168
    int sync;

Michel Lespinasse's avatar
 
Michel Lespinasse committed
169
    intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)\n", p_ac3dec, getpid());
Michel Lespinasse's avatar
 
Michel Lespinasse committed
170

171 172
    /* FIXME ! Qu'est-ce que c'est que ce bordel !?!?!?!? --Meuuh */
    //msleep (INPUT_PTS_DELAY);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
173 174

    /* Initializing the ac3 decoder thread */
175 176
    if (InitThread (p_ac3dec)) /* XXX?? */
    {
177
        p_ac3dec->p_fifo->b_error = 1;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
178 179
    }

Michel Lespinasse's avatar
 
Michel Lespinasse committed
180 181 182
    sync = 0;
    p_ac3dec->sync_ptr = 0;

Michel Lespinasse's avatar
 
Michel Lespinasse committed
183
    /* ac3 decoder thread's main loop */
184
    /* FIXME : do we have enough room to store the decoded frames ?? */
185
    while ((!p_ac3dec->p_fifo->b_die) && (!p_ac3dec->p_fifo->b_error))
186
    {
187 188
        s16 * buffer;
        ac3_sync_info_t sync_info;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
189

190 191 192
        if (!sync) { /* have to find a synchro point */
            int ptr;
            ac3_byte_stream_t * p_byte_stream;
Michel Lespinasse's avatar
 
Michel Lespinasse committed
193

194
            intf_DbgMsg ("ac3dec: sync\n");
195

196
            p_byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
197

198
            /* first read till next ac3 magic header */
199 200
            do
            {
201 202
                ac3_byte_stream_next (p_byte_stream);
            } while ((!p_ac3dec->sync_ptr) &&
203 204
                    (!p_ac3dec->p_fifo->b_die) &&
                    (!p_ac3dec->p_fifo->b_error));
205
            /* skip the specified number of bytes */
Michel Lespinasse's avatar
 
Michel Lespinasse committed
206

207
            if( p_ac3dec->p_fifo->b_die || p_ac3dec->p_fifo->b_error )
208 209 210 211
            {
                goto bad_frame;
            }

212
            ptr = p_ac3dec->sync_ptr;
213
            while (--ptr && (!p_ac3dec->p_fifo->b_die) && (!p_ac3dec->p_fifo->b_error))
214 215 216
            {
                if (p_byte_stream->p_byte >= p_byte_stream->p_end)
                {
217 218 219 220
                    ac3_byte_stream_next (p_byte_stream);                    
                }
                p_byte_stream->p_byte++;
            }
Michel Lespinasse's avatar
 
Michel Lespinasse committed
221

222
            if( p_ac3dec->p_fifo->b_die || p_ac3dec->p_fifo->b_error )
223 224 225
            {
                goto bad_frame;
            }
Michel Lespinasse's avatar
 
Michel Lespinasse committed
226

227
            /* we are in sync now */
228 229 230
            sync = 1;
            p_ac3dec->sync_ptr = 0;
        }
231

232
        if (DECODER_FIFO_START(*p_ac3dec->p_fifo)->b_has_pts)
233
        {
234 235
            p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(*p_ac3dec->p_fifo)->i_pts;
            DECODER_FIFO_START(*p_ac3dec->p_fifo)->b_has_pts = 0;
236 237 238
        }
        else
        {
239
            p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
240 241
        }

242 243
        if (ac3_sync_frame (&p_ac3dec->ac3_decoder, &sync_info))
        {
244 245 246
            sync = 0;
            goto bad_frame;
        }
247

248
        p_ac3dec->p_aout_fifo->l_rate = sync_info.sample_rate;
249

250
        buffer = ((s16 *)p_ac3dec->p_aout_fifo->buffer) + (p_ac3dec->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
251

252 253
        if (ac3_decode_frame (&p_ac3dec->ac3_decoder, buffer))
        {
254 255 256
            sync = 0;
            goto bad_frame;
        }
257

258 259 260 261
        vlc_mutex_lock (&p_ac3dec->p_aout_fifo->data_lock);
        p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
        vlc_cond_signal (&p_ac3dec->p_aout_fifo->data_wait);
        vlc_mutex_unlock (&p_ac3dec->p_aout_fifo->data_lock);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
262

Michel Lespinasse's avatar
 
Michel Lespinasse committed
263
    bad_frame:
Michel Lespinasse's avatar
 
Michel Lespinasse committed
264 265 266
    }

    /* If b_error is set, the ac3 decoder thread enters the error loop */
267
    if (p_ac3dec->p_fifo->b_error)
268
    {
Michel Lespinasse's avatar
 
Michel Lespinasse committed
269
        ErrorThread (p_ac3dec);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
270 271 272
    }

    /* End of the ac3 decoder thread */
Michel Lespinasse's avatar
 
Michel Lespinasse committed
273
    EndThread (p_ac3dec);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
274 275 276 277 278
}

/*****************************************************************************
 * ErrorThread : ac3 decoder's RunThread() error loop
 *****************************************************************************/
Michel Lespinasse's avatar
 
Michel Lespinasse committed
279
static void ErrorThread (ac3dec_thread_t * p_ac3dec)
Michel Lespinasse's avatar
 
Michel Lespinasse committed
280 281 282
{
    /* We take the lock, because we are going to read/write the start/end
     * indexes of the decoder fifo */
283
    vlc_mutex_lock (&p_ac3dec->p_fifo->data_lock);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
284 285

    /* Wait until a `die' order is sent */
286
    while (!p_ac3dec->p_fifo->b_die)
287
    {
Michel Lespinasse's avatar
 
Michel Lespinasse committed
288
        /* Trash all received PES packets */
289
        while (!DECODER_FIFO_ISEMPTY(*p_ac3dec->p_fifo))
290
        {
291 292 293
            p_ac3dec->p_fifo->pf_delete_pes(p_ac3dec->p_fifo->p_packets_mgt,
                    DECODER_FIFO_START(*p_ac3dec->p_fifo));
            DECODER_FIFO_INCSTART (*p_ac3dec->p_fifo);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
294 295 296
        }

        /* Waiting for the input thread to put new PES packets in the fifo */
297 298
        vlc_cond_wait (&p_ac3dec->p_fifo->data_wait,
                       &p_ac3dec->p_fifo->data_lock);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
299 300 301
    }

    /* We can release the lock before leaving */
302
    vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
303 304 305 306 307
}

/*****************************************************************************
 * EndThread : ac3 decoder thread destruction
 *****************************************************************************/
Michel Lespinasse's avatar
 
Michel Lespinasse committed
308
static void EndThread (ac3dec_thread_t * p_ac3dec)
Michel Lespinasse's avatar
 
Michel Lespinasse committed
309
{
Michel Lespinasse's avatar
 
Michel Lespinasse committed
310
    intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p\n", p_ac3dec);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
311 312

    /* If the audio output fifo was created, we destroy it */
313 314
    if (p_ac3dec->p_aout_fifo != NULL)
    {
Michel Lespinasse's avatar
 
Michel Lespinasse committed
315
        aout_DestroyFifo (p_ac3dec->p_aout_fifo);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
316 317

        /* Make sure the output thread leaves the NextFrame() function */
Michel Lespinasse's avatar
 
Michel Lespinasse committed
318 319 320
        vlc_mutex_lock (&(p_ac3dec->p_aout_fifo->data_lock));
        vlc_cond_signal (&(p_ac3dec->p_aout_fifo->data_wait));
        vlc_mutex_unlock (&(p_ac3dec->p_aout_fifo->data_lock));
Michel Lespinasse's avatar
 
Michel Lespinasse committed
321 322 323
    }

    /* Destroy descriptor */
Michel Lespinasse's avatar
 
Michel Lespinasse committed
324
    free (p_ac3dec);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
325

Michel Lespinasse's avatar
 
Michel Lespinasse committed
326
    intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed\n", p_ac3dec);
Michel Lespinasse's avatar
 
Michel Lespinasse committed
327
}
Michel Lespinasse's avatar
 
Michel Lespinasse committed
328 329 330 331 332 333 334

void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream)
{
    ac3dec_thread_t * p_ac3dec = p_byte_stream->info;

    /* We are looking for the next TS packet that contains real data,
     * and not just a PES header */
335 336
    do
    {
337 338
        /* We were reading the last TS packet of this PES packet... It's
         * time to jump to the next PES packet */
339
        if (p_ac3dec->p_data->p_next == NULL)
340
        {
341 342 343 344 345
            int ptr;

            /* We are going to read/write the start and end indexes of the 
             * decoder fifo and to use the fifo's conditional variable, 
             * that's why we need to take the lock before */ 
346
            vlc_mutex_lock (&p_ac3dec->p_fifo->data_lock);
347 348

            /* Is the input thread dying ? */
349
            if (p_ac3dec->p_fifo->b_die)
350
            {
351
                vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
352 353 354 355
                return;
            }

            /* We should increase the start index of the decoder fifo, but
Michel Lespinasse's avatar
 
Michel Lespinasse committed
356 357 358 359
             * if we do this now, the input thread could overwrite the
             * pointer to the current PES packet, and we weren't able to
             * give it back to the netlist. That's why we free the PES
             * packet first. */
360 361
            p_ac3dec->p_fifo->pf_delete_pes(p_ac3dec->p_fifo->p_packets_mgt,
                    DECODER_FIFO_START(*p_ac3dec->p_fifo));
362

363
            DECODER_FIFO_INCSTART (*p_ac3dec->p_fifo);
364

365
            while (DECODER_FIFO_ISEMPTY(*p_ac3dec->p_fifo))
366
            {
367
                vlc_cond_wait(&p_ac3dec->p_fifo->data_wait, &p_ac3dec->p_fifo->data_lock);
368

369
                if (p_ac3dec->p_fifo->b_die)
370
                {
371
                    vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
372 373 374 375 376
                    return;
                }
            }

            /* The next byte could be found in the next PES packet */
377
            p_ac3dec->p_data = DECODER_FIFO_START(*p_ac3dec->p_fifo)->p_first;
378 379

            /* parse ac3 magic header */
380
            ptr = *(p_ac3dec->p_data->p_payload_start + 1);
381
            ptr <<= 8;
382
            ptr |= *(p_ac3dec->p_data->p_payload_start + 2);
383
            p_ac3dec->sync_ptr = ptr;
384
            p_ac3dec->p_data->p_payload_start += 3;
385 386

            /* We can release the fifo's data lock */
387
            vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
388 389 390
        }
        /* Perhaps the next TS packet of the current PES packet contains 
         * real data (ie its payload's size is greater than 0) */
391 392
        else
        {
393
            p_ac3dec->p_data = p_ac3dec->p_data->p_next;
394
        }
395 396 397
    } while (p_ac3dec->p_data->p_payload_start == p_ac3dec->p_data->p_payload_end);
    p_byte_stream->p_byte = p_ac3dec->p_data->p_payload_start; 
    p_byte_stream->p_end = p_ac3dec->p_data->p_payload_end; 
Michel Lespinasse's avatar
 
Michel Lespinasse committed
398
}