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