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