]> git.sesse.net Git - vlc/blob - modules/codec/a52.c
* ./configure.ac.in: removed -W in favour of -Wtraditional.
[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.19 2002/12/06 16:34:05 sam 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 : A52 pass-through 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
157         /* Set the Presentation Time Stamp */
158         NextPTS( &p_dec->bit_stream, &pts, NULL );
159         if ( pts != 0 && pts != aout_DateGet( &end_date ) )
160         {
161             aout_DateSet( &end_date, pts );
162         }
163
164         /* Get A/52 frame header */
165         GetChunk( &p_dec->bit_stream, p_header, 7 );
166         if( p_dec->p_fifo->b_die ) break;
167
168         /* Check if frame is valid and get frame info */
169         i_frame_size = SyncInfo( p_header, &i_original_channels, &i_rate,
170                                  &i_bit_rate );
171
172         if( !i_frame_size )
173         {
174             msg_Warn( p_dec->p_fifo, "a52_syncinfo failed" );
175             continue;
176         }
177
178         if( (p_dec->p_aout_input != NULL) &&
179             ( (p_dec->output_format.i_rate != i_rate)
180                 || (p_dec->output_format.i_original_channels
181                       != i_original_channels)
182                 || (p_dec->output_format.i_bytes_per_frame != i_frame_size) ) )
183         {
184             /* Parameters changed - this should not happen. */
185             aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
186             p_dec->p_aout_input = NULL;
187         }
188
189         /* Creating the audio input if not created yet. */
190         if( p_dec->p_aout_input == NULL )
191         {
192             p_dec->output_format.i_rate = i_rate;
193             p_dec->output_format.i_original_channels = i_original_channels;
194             p_dec->output_format.i_physical_channels
195                        = i_original_channels & AOUT_CHAN_PHYSMASK;
196             p_dec->output_format.i_bytes_per_frame = i_frame_size;
197             p_dec->output_format.i_frame_length = A52_FRAME_NB;
198             aout_DateInit( &end_date, i_rate );
199             p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
200                                                &p_dec->p_aout,
201                                                &p_dec->output_format );
202
203             if ( p_dec->p_aout_input == NULL )
204             {
205                 p_dec->p_fifo->b_error = 1;
206                 break;
207             }
208         }
209
210         if ( !aout_DateGet( &end_date ) )
211         {
212             byte_t p_junk[3840];
213
214             /* We've just started the stream, wait for the first PTS. */
215             GetChunk( &p_dec->bit_stream, p_junk, i_frame_size - 7 );
216             continue;
217         }
218
219         p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
220                                       A52_FRAME_NB );
221         if ( p_buffer == NULL ) return -1;
222         p_buffer->start_date = aout_DateGet( &end_date );
223         p_buffer->end_date = aout_DateIncrement( &end_date,
224                                                  A52_FRAME_NB );
225
226         /* Get the whole frame. */
227         memcpy( p_buffer->p_buffer, p_header, 7 );
228         GetChunk( &p_dec->bit_stream, p_buffer->p_buffer + 7,
229                   i_frame_size - 7 );
230         if( p_dec->p_fifo->b_die )
231         {
232             aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
233                                   p_buffer );
234             break;
235         }
236
237         /* Send the buffer to the mixer. */
238         aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
239     }
240
241     /* If b_error is set, the spdif thread enters the error loop */
242     if( p_dec->p_fifo->b_error )
243     {
244         DecoderError( p_dec->p_fifo );
245     }
246
247     /* End of the spdif decoder thread */
248     EndThread( p_dec );
249
250     return 0;
251 }
252
253 /*****************************************************************************
254  * EndThread : spdif thread destruction
255  *****************************************************************************/
256 static void EndThread( dec_thread_t * p_dec )
257 {
258     if ( p_dec->p_aout_input != NULL )
259     {
260         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
261     }
262
263     CloseBitstream( &p_dec->bit_stream );
264     free( p_dec );
265 }
266
267 /*****************************************************************************
268  * SyncInfo: parse A52 sync info
269  *****************************************************************************
270  * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
271  * since we don't want to oblige S/PDIF people to use liba52 just to get
272  * their SyncInfo...
273  *****************************************************************************/
274 static int SyncInfo( const byte_t * p_buf, int * pi_channels,
275                      int * pi_sample_rate, int * pi_bit_rate )
276 {
277     static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
278     static const int rate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
279                                 128, 160, 192, 224, 256, 320, 384, 448,
280                                 512, 576, 640 };
281     static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
282                                       0x04, 0x01, 0x04, 0x01 };
283     int frmsizecod;
284     int bitrate;
285     int half;
286     int acmod;
287
288     if ((p_buf[0] != 0x0b) || (p_buf[1] != 0x77))        /* syncword */
289         return 0;
290
291     if (p_buf[5] >= 0x60)                /* bsid >= 12 */
292         return 0;
293     half = halfrate[p_buf[5] >> 3];
294
295     /* acmod, dsurmod and lfeon */
296     acmod = p_buf[6] >> 5;
297     if ( (p_buf[6] & 0xf8) == 0x50 )
298     {
299         /* Dolby surround = stereo + Dolby */
300         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
301                         | AOUT_CHAN_DOLBYSTEREO;
302     }
303     else switch ( acmod )
304     {
305     case 0x0:
306         /* Dual-mono = stereo + dual-mono */
307         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
308                         | AOUT_CHAN_DUALMONO;
309         break;
310     case 0x1:
311         /* Mono */
312         *pi_channels = AOUT_CHAN_CENTER;
313         break;
314     case 0x2:
315         /* Stereo */
316         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
317         break;
318     case 0x3:
319         /* 3F */
320         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER;
321         break;
322     case 0x4:
323         /* 2F1R */
324         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
325         break;
326     case 0x5:
327         /* 3F1R */
328         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
329                         | AOUT_CHAN_REARCENTER;
330         break;
331     case 0x6:
332         /* 2F2R */
333         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
334                         | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
335         break;
336     case 0x7:
337         /* 3F2R */
338         *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
339                         | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
340         break;
341     default:
342         return 0;
343     }
344
345     if ( p_buf[6] & lfeon[acmod] ) *pi_channels |= AOUT_CHAN_LFE;
346
347     frmsizecod = p_buf[4] & 63;
348     if (frmsizecod >= 38)
349         return 0;
350     bitrate = rate [frmsizecod >> 1];
351     *pi_bit_rate = (bitrate * 1000) >> half;
352
353     switch (p_buf[4] & 0xc0) {
354     case 0:
355         *pi_sample_rate = 48000 >> half;
356         return 4 * bitrate;
357     case 0x40:
358         *pi_sample_rate = 44100 >> half;
359         return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
360     case 0x80:
361         *pi_sample_rate = 32000 >> half;
362         return 6 * bitrate;
363     default:
364         return 0;
365     }
366 }
367