]> git.sesse.net Git - vlc/blob - modules/codec/a52.c
* modules/codec/*: Fixed a PTS bug at the initialization of some codecs
[vlc] / modules / codec / a52.c
1 /*****************************************************************************
2  * a52.c: A/52 basic parser
3  *****************************************************************************
4  * Copyright (C) 2001-2002 VideoLAN
5  * $Id: a52.c,v 1.22 2003/03/31 22:39:27 massiot Exp $
6  *
7  * Authors: Stéphane Borel <stef@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Michel Lespinasse <walken@zoy.org>
10  *          Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>                                              /* memcpy() */
33 #include <fcntl.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc/decoder.h>
37 #include <vlc/aout.h>
38
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #endif
42
43 /*****************************************************************************
44  * dec_thread_t : decoder thread descriptor
45  *****************************************************************************/
46 typedef struct dec_thread_t
47 {
48     /*
49      * Thread properties
50      */
51     vlc_thread_t        thread_id;                /* id for thread functions */
52
53     /*
54      * Input properties
55      */
56     decoder_fifo_t *    p_fifo;                /* stores the PES stream data */
57     bit_stream_t        bit_stream;
58
59     /*
60      * Output properties
61      */
62     aout_instance_t *   p_aout; /* opaque */
63     aout_input_t *      p_aout_input; /* opaque */
64     audio_sample_format_t output_format;
65 } dec_thread_t;
66
67 /****************************************************************************
68  * Local prototypes
69  ****************************************************************************/
70 static int  OpenDecoder    ( vlc_object_t * );
71 static int  RunDecoder     ( decoder_fifo_t * );
72
73 static void EndThread      ( dec_thread_t * );
74
75 static int  SyncInfo       ( const byte_t *, int *, int *, int * );
76
77 /*****************************************************************************
78  * Module descriptor
79  *****************************************************************************/
80 vlc_module_begin();
81     set_description( _("A/52 parser") );
82     set_capability( "decoder", 100 );
83     set_callbacks( OpenDecoder, NULL );
84 vlc_module_end();
85
86 /*****************************************************************************
87  * OpenDecoder: probe the decoder and return score
88  *****************************************************************************/
89 static int OpenDecoder( vlc_object_t *p_this )
90 {
91     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
92
93     if( p_fifo->i_fourcc != VLC_FOURCC('a','5','2',' ')
94          && p_fifo->i_fourcc != VLC_FOURCC('a','5','2','b') )
95     {
96         return VLC_EGENERIC;
97     }
98
99     p_fifo->pf_run = RunDecoder;
100     return VLC_SUCCESS;
101 }
102
103 /****************************************************************************
104  * RunDecoder: the whole thing
105  ****************************************************************************
106  * This function is called just after the thread is launched.
107  ****************************************************************************/
108 static int RunDecoder( decoder_fifo_t *p_fifo )
109 {
110     dec_thread_t * p_dec;
111     audio_date_t end_date;
112
113     /* Allocate the memory needed to store the thread's structure */
114     p_dec = malloc( sizeof(dec_thread_t) );
115     if( p_dec == NULL )
116     {
117         msg_Err( p_fifo, "out of memory" );
118         DecoderError( p_fifo );
119         return -1;
120     }
121
122     /* Initialize the thread properties */
123     p_dec->p_aout = NULL;
124     p_dec->p_aout_input = NULL;
125     p_dec->p_fifo = p_fifo;
126     p_dec->output_format.i_format = VLC_FOURCC('a','5','2',' ');
127
128     aout_DateSet( &end_date, 0 );
129
130     /* Init the bitstream */
131     if( InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
132                        NULL, NULL ) != VLC_SUCCESS )
133     {
134         msg_Err( p_fifo, "cannot initialize bitstream" );
135         DecoderError( p_fifo );
136         free( p_dec );
137         return -1;
138     }
139
140     /* Decoder thread's main loop */
141     while ( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
142     {
143         int i_bit_rate;
144         unsigned int i_rate, i_original_channels, i_frame_size;
145         mtime_t pts;
146         byte_t p_header[7];
147         aout_buffer_t * p_buffer;
148
149         /* Look for sync word - should be 0x0b77 */
150         RealignBits( &p_dec->bit_stream );
151         while ( (ShowBits( &p_dec->bit_stream, 16 ) ) != 0x0b77 &&
152                 (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
153         {
154             RemoveBits( &p_dec->bit_stream, 8 );
155         }
156         if ( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error ) break;
157
158         /* Set the Presentation Time Stamp */
159         NextPTS( &p_dec->bit_stream, &pts, NULL );
160         if ( pts != 0 && pts != aout_DateGet( &end_date ) )
161         {
162             aout_DateSet( &end_date, pts );
163         }
164
165         /* Get A/52 frame header */
166         GetChunk( &p_dec->bit_stream, p_header, 7 );
167         if ( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error ) break;
168
169         /* Check if frame is valid and get frame info */
170         i_frame_size = SyncInfo( p_header, &i_original_channels, &i_rate,
171                                  &i_bit_rate );
172
173         if( !i_frame_size )
174         {
175             msg_Warn( p_dec->p_fifo, "a52_syncinfo failed" );
176             continue;
177         }
178
179         if( (p_dec->p_aout_input != NULL) &&
180             ( (p_dec->output_format.i_rate != i_rate)
181                 || (p_dec->output_format.i_original_channels
182                       != i_original_channels)
183                 || (p_dec->output_format.i_bytes_per_frame != i_frame_size) ) )
184         {
185             /* Parameters changed - this should not happen. */
186             aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
187             p_dec->p_aout_input = NULL;
188         }
189
190         /* Creating the audio input if not created yet. */
191         if( p_dec->p_aout_input == NULL )
192         {
193             p_dec->output_format.i_rate = i_rate;
194             p_dec->output_format.i_original_channels = i_original_channels;
195             p_dec->output_format.i_physical_channels
196                        = i_original_channels & AOUT_CHAN_PHYSMASK;
197             p_dec->output_format.i_bytes_per_frame = i_frame_size;
198             p_dec->output_format.i_frame_length = A52_FRAME_NB;
199             aout_DateInit( &end_date, i_rate );
200             aout_DateSet( &end_date, pts );
201             p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
202                                                &p_dec->p_aout,
203                                                &p_dec->output_format );
204
205             if ( p_dec->p_aout_input == NULL )
206             {
207                 p_dec->p_fifo->b_error = 1;
208                 break;
209             }
210         }
211
212         if ( !aout_DateGet( &end_date ) )
213         {
214             byte_t p_junk[3840];
215
216             /* We've just started the stream, wait for the first PTS. */
217             GetChunk( &p_dec->bit_stream, p_junk, i_frame_size - 7 );
218             continue;
219         }
220
221         p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
222                                       A52_FRAME_NB );
223         if ( p_buffer == NULL )
224         {
225             p_dec->p_fifo->b_error = 1;
226             break;
227         }
228         p_buffer->start_date = aout_DateGet( &end_date );
229         p_buffer->end_date = aout_DateIncrement( &end_date,
230                                                  A52_FRAME_NB );
231
232         /* Get the whole frame. */
233         memcpy( p_buffer->p_buffer, p_header, 7 );
234         GetChunk( &p_dec->bit_stream, p_buffer->p_buffer + 7,
235                   i_frame_size - 7 );
236         if( p_dec->p_fifo->b_die )
237         {
238             aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
239                                   p_buffer );
240             break;
241         }
242
243         /* Send the buffer to the aout core. */
244         aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
245     }
246
247     if( p_dec->p_fifo->b_error )
248     {
249         DecoderError( p_dec->p_fifo );
250     }
251
252     EndThread( p_dec );
253
254     return 0;
255 }
256
257 /*****************************************************************************
258  * EndThread : thread destruction
259  *****************************************************************************/
260 static void EndThread( dec_thread_t * p_dec )
261 {
262     if ( p_dec->p_aout_input != NULL )
263     {
264         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
265     }
266
267     CloseBitstream( &p_dec->bit_stream );
268     free( p_dec );
269 }
270
271 /*****************************************************************************
272  * SyncInfo: parse A/52 sync info
273  *****************************************************************************
274  * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
275  * since we don't want to oblige S/PDIF people to use liba52 just to get
276  * their SyncInfo...
277  *****************************************************************************/
278 static int SyncInfo( const byte_t * p_buf, int * pi_channels,
279                      int * pi_sample_rate, int * pi_bit_rate )
280 {
281     static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
282     static const int rate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
283                                 128, 160, 192, 224, 256, 320, 384, 448,
284                                 512, 576, 640 };
285     static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
286                                       0x04, 0x01, 0x04, 0x01 };
287     int frmsizecod;
288     int bitrate;
289     int half;
290     int acmod;
291
292     if ((p_buf[0] != 0x0b) || (p_buf[1] != 0x77))        /* syncword */
293         return 0;
294
295     if (p_buf[5] >= 0x60)                /* bsid >= 12 */
296         return 0;
297     half = halfrate[p_buf[5] >> 3];
298
299     /* acmod, dsurmod and lfeon */
300     acmod = p_buf[6] >> 5;
301     if ( (p_buf[6] & 0xf8) == 0x50 )
302     {
303         /* Dolby surround = stereo + Dolby */
304         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
305                         | AOUT_CHAN_DOLBYSTEREO;
306     }
307     else switch ( acmod )
308     {
309     case 0x0:
310         /* Dual-mono = stereo + dual-mono */
311         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
312                         | AOUT_CHAN_DUALMONO;
313         break;
314     case 0x1:
315         /* Mono */
316         *pi_channels = AOUT_CHAN_CENTER;
317         break;
318     case 0x2:
319         /* Stereo */
320         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
321         break;
322     case 0x3:
323         /* 3F */
324         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER;
325         break;
326     case 0x4:
327         /* 2F1R */
328         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
329         break;
330     case 0x5:
331         /* 3F1R */
332         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
333                         | AOUT_CHAN_REARCENTER;
334         break;
335     case 0x6:
336         /* 2F2R */
337         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
338                         | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
339         break;
340     case 0x7:
341         /* 3F2R */
342         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
343                         | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
344         break;
345     default:
346         return 0;
347     }
348
349     if ( p_buf[6] & lfeon[acmod] ) *pi_channels |= AOUT_CHAN_LFE;
350
351     frmsizecod = p_buf[4] & 63;
352     if (frmsizecod >= 38)
353         return 0;
354     bitrate = rate [frmsizecod >> 1];
355     *pi_bit_rate = (bitrate * 1000) >> half;
356
357     switch (p_buf[4] & 0xc0) {
358     case 0:
359         *pi_sample_rate = 48000 >> half;
360         return 4 * bitrate;
361     case 0x40:
362         *pi_sample_rate = 44100 >> half;
363         return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
364     case 0x80:
365         *pi_sample_rate = 32000 >> half;
366         return 6 * bitrate;
367     default:
368         return 0;
369     }
370 }
371