]> git.sesse.net Git - vlc/blob - modules/codec/a52.c
* modules/codec/a52.c: fixed segfault
[vlc] / modules / codec / a52.c
1 /*****************************************************************************
2  * a52.c: parse A/52 audio sync info and packetize the stream
3  *****************************************************************************
4  * Copyright (C) 2001-2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Stéphane Borel <stef@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc/decoder.h>
31
32 #include "vlc_block_helper.h"
33
34 #define A52_HEADER_SIZE 7
35
36 /*****************************************************************************
37  * decoder_sys_t : decoder descriptor
38  *****************************************************************************/
39 struct decoder_sys_t
40 {
41     /* Module mode */
42     vlc_bool_t b_packetizer;
43
44     /*
45      * Input properties
46      */
47     int i_state;
48
49     block_bytestream_t bytestream;
50
51     /*
52      * Common properties
53      */
54     audio_date_t   end_date;
55
56     mtime_t i_pts;
57     int i_frame_size, i_bit_rate;
58     unsigned int i_rate, i_channels, i_channels_conf;
59
60 };
61
62 enum {
63
64     STATE_NOSYNC,
65     STATE_SYNC,
66     STATE_HEADER,
67     STATE_NEXT_SYNC,
68     STATE_GET_DATA,
69     STATE_SEND_DATA
70 };
71
72 /****************************************************************************
73  * Local prototypes
74  ****************************************************************************/
75 static int  OpenDecoder   ( vlc_object_t * );
76 static int  OpenPacketizer( vlc_object_t * );
77 static void CloseDecoder  ( vlc_object_t * );
78 static void *DecodeBlock  ( decoder_t *, block_t ** );
79
80 static int  SyncInfo      ( const byte_t *, int *, int *, int *,int * );
81
82 static uint8_t       *GetOutBuffer ( decoder_t *, void ** );
83 static aout_buffer_t *GetAoutBuffer( decoder_t * );
84 static block_t       *GetSoutBuffer( decoder_t * );
85
86 /*****************************************************************************
87  * Module descriptor
88  *****************************************************************************/
89 vlc_module_begin();
90     set_description( _("A/52 parser") );
91     set_capability( "decoder", 100 );
92     set_callbacks( OpenDecoder, CloseDecoder );
93
94     add_submodule();
95     set_description( _("A/52 audio packetizer") );
96     set_capability( "packetizer", 10 );
97     set_callbacks( OpenPacketizer, CloseDecoder );
98 vlc_module_end();
99
100 /*****************************************************************************
101  * OpenDecoder: probe the decoder and return score
102  *****************************************************************************/
103 static int OpenDecoder( vlc_object_t *p_this )
104 {
105     decoder_t *p_dec = (decoder_t*)p_this;
106     decoder_sys_t *p_sys;
107
108     if( p_dec->fmt_in.i_codec != VLC_FOURCC('a','5','2',' ')
109           && p_dec->fmt_in.i_codec != VLC_FOURCC('a','5','2','b') )
110     {
111         return VLC_EGENERIC;
112     }
113
114     /* Allocate the memory needed to store the decoder's structure */
115     if( ( p_dec->p_sys = p_sys =
116           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
117     {
118         msg_Err( p_dec, "out of memory" );
119         return VLC_EGENERIC;
120     }
121
122     /* Misc init */
123     p_sys->b_packetizer = VLC_FALSE;
124     p_sys->i_state = STATE_NOSYNC;
125     aout_DateSet( &p_sys->end_date, 0 );
126
127     p_sys->bytestream = block_BytestreamInit( p_dec );
128
129     /* Set output properties */
130     p_dec->fmt_out.i_cat = AUDIO_ES;
131     p_dec->fmt_out.i_codec = VLC_FOURCC('a','5','2',' ');
132     p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
133
134     /* Set callback */
135     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
136         DecodeBlock;
137     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
138         DecodeBlock;
139
140     return VLC_SUCCESS;
141 }
142
143 static int OpenPacketizer( vlc_object_t *p_this )
144 {
145     decoder_t *p_dec = (decoder_t*)p_this;
146
147     int i_ret = OpenDecoder( p_this );
148
149     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
150
151     return i_ret;
152 }
153
154 /****************************************************************************
155  * DecodeBlock: the whole thing
156  ****************************************************************************
157  * This function is called just after the thread is launched.
158  ****************************************************************************/
159 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
160 {
161     decoder_sys_t *p_sys = p_dec->p_sys;
162     uint8_t p_header[A52_HEADER_SIZE];
163     uint8_t *p_buf;
164     void *p_out_buffer;
165
166     if( !pp_block || !*pp_block ) return NULL;
167
168     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
169     {
170         /* We've just started the stream, wait for the first PTS. */
171         block_Release( *pp_block );
172         return NULL;
173     }
174
175     if( (*pp_block)->i_flags&BLOCK_FLAG_DISCONTINUITY )
176     {
177         p_sys->i_state = STATE_NOSYNC;
178     }
179
180     block_BytestreamPush( &p_sys->bytestream, *pp_block );
181
182     while( 1 )
183     {
184         switch( p_sys->i_state )
185         {
186         case STATE_NOSYNC:
187             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
188                    == VLC_SUCCESS )
189             {
190                 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
191                 {
192                     p_sys->i_state = STATE_SYNC;
193                     break;
194                 }
195                 block_SkipByte( &p_sys->bytestream );
196             }
197             if( p_sys->i_state != STATE_SYNC )
198             {
199                 block_BytestreamFlush( &p_sys->bytestream );
200
201                 /* Need more data */
202                 return NULL;
203             }
204
205         case STATE_SYNC:
206             /* New frame, set the Presentation Time Stamp */
207             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
208             if( p_sys->i_pts != 0 &&
209                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
210             {
211                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
212             }
213             p_sys->i_state = STATE_HEADER;
214
215         case STATE_HEADER:
216             /* Get A/52 frame header (A52_HEADER_SIZE bytes) */
217             if( block_PeekBytes( &p_sys->bytestream, p_header,
218                                  A52_HEADER_SIZE ) != VLC_SUCCESS )
219             {
220                 /* Need more data */
221                 return NULL;
222             }
223
224             /* Check if frame is valid and get frame info */
225             p_sys->i_frame_size = SyncInfo( p_header,
226                                             &p_sys->i_channels,
227                                             &p_sys->i_channels_conf,
228                                             &p_sys->i_rate,
229                                             &p_sys->i_bit_rate );
230             if( !p_sys->i_frame_size )
231             {
232                 msg_Dbg( p_dec, "emulated sync word" );
233                 block_SkipByte( &p_sys->bytestream );
234                 p_sys->i_state = STATE_NOSYNC;
235                 break;
236             }
237             p_sys->i_state = STATE_NEXT_SYNC;
238
239         case STATE_NEXT_SYNC:
240             /* TODO: If pp_block == NULL, flush the buffer without checking the
241              * next sync word */
242
243             /* Check if next expected frame contains the sync word */
244             if( block_PeekOffsetBytes( &p_sys->bytestream,
245                                        p_sys->i_frame_size, p_header, 2 )
246                 != VLC_SUCCESS )
247             {
248                 /* Need more data */
249                 return NULL;
250             }
251
252             if( p_sys->b_packetizer &&
253                 p_header[0] == 0 && p_header[1] == 0 )
254             {
255                 /* A52 wav files and audio CD's use stuffing */
256                 p_sys->i_state = STATE_GET_DATA;
257                 break;
258             }
259
260             if( p_header[0] != 0x0b || p_header[1] != 0x77 )
261             {
262                 msg_Dbg( p_dec, "emulated sync word "
263                          "(no sync on following frame)" );
264                 p_sys->i_state = STATE_NOSYNC;
265                 block_SkipByte( &p_sys->bytestream );
266                 break;
267             }
268             p_sys->i_state = STATE_SEND_DATA;
269             break;
270
271         case STATE_GET_DATA:
272             /* Make sure we have enough data.
273              * (Not useful if we went through NEXT_SYNC) */
274             if( block_WaitBytes( &p_sys->bytestream,
275                                  p_sys->i_frame_size ) != VLC_SUCCESS )
276             {
277                 /* Need more data */
278                 return NULL;
279             }
280             p_sys->i_state = STATE_SEND_DATA;
281
282         case STATE_SEND_DATA:
283             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
284             {
285                 //p_dec->b_error = VLC_TRUE;
286                 return NULL;
287             }
288
289             /* Copy the whole frame into the buffer. When we reach this point
290              * we already know we have enough data available. */
291             block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
292
293             /* Make sure we don't reuse the same pts twice */
294             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
295                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
296
297             /* So p_block doesn't get re-added several times */
298             *pp_block = block_BytestreamPop( &p_sys->bytestream );
299
300             p_sys->i_state = STATE_NOSYNC;
301
302             return p_out_buffer;
303         }
304     }
305
306     return NULL;
307 }
308
309 /*****************************************************************************
310  * CloseDecoder: clean up the decoder
311  *****************************************************************************/
312 static void CloseDecoder( vlc_object_t *p_this )
313 {
314     decoder_t *p_dec = (decoder_t*)p_this;
315     decoder_sys_t *p_sys = p_dec->p_sys;
316
317     block_BytestreamRelease( &p_sys->bytestream );
318
319     free( p_sys );
320 }
321
322 /*****************************************************************************
323  * GetOutBuffer:
324  *****************************************************************************/
325 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
326 {
327     decoder_sys_t *p_sys = p_dec->p_sys;
328     uint8_t *p_buf;
329
330     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
331     {
332         msg_Info( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
333                   p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
334
335         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
336         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
337     }
338
339     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
340     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
341     p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
342     p_dec->fmt_out.audio.i_frame_length = A52_FRAME_NB;
343
344     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
345     p_dec->fmt_out.audio.i_physical_channels =
346         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
347
348     p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
349
350     if( p_sys->b_packetizer )
351     {
352         block_t *p_sout_buffer = GetSoutBuffer( p_dec );
353         p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
354         *pp_out_buffer = p_sout_buffer;
355     }
356     else
357     {
358         aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
359         p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
360         *pp_out_buffer = p_aout_buffer;
361     }
362
363     return p_buf;
364 }
365
366 /*****************************************************************************
367  * GetAoutBuffer:
368  *****************************************************************************/
369 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
370 {
371     decoder_sys_t *p_sys = p_dec->p_sys;
372     aout_buffer_t *p_buf;
373
374     p_buf = p_dec->pf_aout_buffer_new( p_dec, A52_FRAME_NB  );
375     if( p_buf == NULL ) return NULL;
376
377     p_buf->start_date = aout_DateGet( &p_sys->end_date );
378     p_buf->end_date = aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB );
379
380     return p_buf;
381 }
382
383 /*****************************************************************************
384  * GetSoutBuffer:
385  *****************************************************************************/
386 static block_t *GetSoutBuffer( decoder_t *p_dec )
387 {
388     decoder_sys_t *p_sys = p_dec->p_sys;
389     block_t *p_block;
390
391     p_block = block_New( p_dec, p_sys->i_frame_size );
392     if( p_block == NULL ) return NULL;
393
394     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
395
396     p_block->i_length = aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB ) -
397         p_block->i_pts;
398
399     return p_block;
400 }
401
402 /*****************************************************************************
403  * SyncInfo: parse A/52 sync info
404  *****************************************************************************
405  * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
406  * since we don't want to oblige S/PDIF people to use liba52 just to get
407  * their SyncInfo...
408  *****************************************************************************/
409 static int SyncInfo( const byte_t * p_buf,
410                      int * pi_channels, int * pi_channels_conf,
411                      int * pi_sample_rate, int * pi_bit_rate )
412 {
413     static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
414     static const int rate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
415                                 128, 160, 192, 224, 256, 320, 384, 448,
416                                 512, 576, 640 };
417     static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
418                                       0x04, 0x01, 0x04, 0x01 };
419     int frmsizecod;
420     int bitrate;
421     int half;
422     int acmod;
423
424     if ((p_buf[0] != 0x0b) || (p_buf[1] != 0x77))        /* syncword */
425         return 0;
426
427     if (p_buf[5] >= 0x60)                /* bsid >= 12 */
428         return 0;
429     half = halfrate[p_buf[5] >> 3];
430
431     /* acmod, dsurmod and lfeon */
432     acmod = p_buf[6] >> 5;
433     if ( (p_buf[6] & 0xf8) == 0x50 )
434     {
435         /* Dolby surround = stereo + Dolby */
436         *pi_channels = 2;
437         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
438                             | AOUT_CHAN_DOLBYSTEREO;
439     }
440     else switch ( acmod )
441     {
442     case 0x0:
443         /* Dual-mono = stereo + dual-mono */
444         *pi_channels = 2;
445         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
446                             | AOUT_CHAN_DUALMONO;
447         break;
448     case 0x1:
449         /* Mono */
450         *pi_channels = 1;
451         *pi_channels_conf = AOUT_CHAN_CENTER;
452         break;
453     case 0x2:
454         /* Stereo */
455         *pi_channels = 2;
456         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
457         break;
458     case 0x3:
459         /* 3F */
460         *pi_channels = 3;
461         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
462                             | AOUT_CHAN_CENTER;
463         break;
464     case 0x4:
465         /* 2F1R */
466         *pi_channels = 3;
467         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
468                             | AOUT_CHAN_REARCENTER;
469         break;
470     case 0x5:
471         /* 3F1R */
472         *pi_channels = 4;
473         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
474                             | AOUT_CHAN_REARCENTER;
475         break;
476     case 0x6:
477         /* 2F2R */
478         *pi_channels = 4;
479         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
480                             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
481         break;
482     case 0x7:
483         /* 3F2R */
484         *pi_channels = 5;
485         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
486                             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
487         break;
488     default:
489         return 0;
490     }
491
492     if ( p_buf[6] & lfeon[acmod] )
493     {
494         (*pi_channels)++;
495         *pi_channels_conf |= AOUT_CHAN_LFE;
496     }
497
498     frmsizecod = p_buf[4] & 63;
499     if (frmsizecod >= 38)
500         return 0;
501     bitrate = rate [frmsizecod >> 1];
502     *pi_bit_rate = (bitrate * 1000) >> half;
503
504     switch (p_buf[4] & 0xc0) {
505     case 0:
506         *pi_sample_rate = 48000 >> half;
507         return 4 * bitrate;
508     case 0x40:
509         *pi_sample_rate = 44100 >> half;
510         return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
511     case 0x80:
512         *pi_sample_rate = 32000 >> half;
513         return 6 * bitrate;
514     default:
515         return 0;
516     }
517 }