]> git.sesse.net Git - vlc/blob - modules/codec/a52.c
Fixed some AC3 file playback with a52 audio filter.
[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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_aout.h>
37 #include <vlc_block_helper.h>
38 #include <vlc_bits.h>
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  OpenDecoder   ( vlc_object_t * );
44 static int  OpenPacketizer( vlc_object_t * );
45 static void CloseCommon   ( vlc_object_t * );
46
47 vlc_module_begin ()
48     set_description( N_("A/52 parser") )
49     set_capability( "decoder", 100 )
50     set_callbacks( OpenDecoder, CloseCommon )
51     set_category( CAT_INPUT )
52     set_subcategory( SUBCAT_INPUT_ACODEC )
53
54     add_submodule ()
55     set_description( N_("A/52 audio packetizer") )
56     set_capability( "packetizer", 10 )
57     set_callbacks( OpenPacketizer, CloseCommon )
58 vlc_module_end ()
59
60 /*****************************************************************************
61  * decoder_sys_t : decoder descriptor
62  *****************************************************************************/
63
64 #define A52_HEADER_SIZE 7
65
66 struct decoder_sys_t
67 {
68     /* Module mode */
69     bool b_packetizer;
70
71     /*
72      * Input properties
73      */
74     int i_state;
75
76     block_bytestream_t bytestream;
77
78     /*
79      * Common properties
80      */
81     audio_date_t   end_date;
82
83     mtime_t i_pts;
84     int i_frame_size, i_bit_rate;
85     unsigned int i_rate, i_channels, i_channels_conf;
86 };
87
88 enum {
89
90     STATE_NOSYNC,
91     STATE_SYNC,
92     STATE_HEADER,
93     STATE_NEXT_SYNC,
94     STATE_GET_DATA,
95     STATE_SEND_DATA
96 };
97
98 /****************************************************************************
99  * Local prototypes
100  ****************************************************************************/
101 static void *DecodeBlock  ( decoder_t *, block_t ** );
102
103 static int  SyncInfo      ( const uint8_t *, unsigned int *, unsigned int *,
104                             unsigned int *, int * );
105
106 static uint8_t       *GetOutBuffer ( decoder_t *, void ** );
107 static aout_buffer_t *GetAoutBuffer( decoder_t * );
108 static block_t       *GetSoutBuffer( decoder_t * );
109
110 /*****************************************************************************
111  * OpenCommon: probe the decoder/packetizer and return score
112  *****************************************************************************/
113 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
114 {
115     decoder_t *p_dec = (decoder_t*)p_this;
116     decoder_sys_t *p_sys;
117     vlc_fourcc_t i_codec;
118
119     switch( p_dec->fmt_in.i_codec )
120     {
121     case VLC_FOURCC('a','5','2',' '):
122     case VLC_FOURCC('a','5','2','b'):
123         i_codec = VLC_FOURCC('a','5','2',' ');
124         break;
125     case VLC_FOURCC('e','a','c','3'):
126         /* XXX ugly hack, a52 does not support eac3 so no eac3 pass-through
127          * support */
128         if( !b_packetizer )
129             return VLC_EGENERIC;
130         i_codec = VLC_FOURCC('e','a','c','3');
131         break;
132     default:
133         return VLC_EGENERIC;
134     }
135
136     /* Allocate the memory needed to store the decoder's structure */
137     if( ( p_dec->p_sys = p_sys =
138           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
139         return VLC_ENOMEM;
140
141     /* Misc init */
142     p_sys->b_packetizer = b_packetizer;
143     p_sys->i_state = STATE_NOSYNC;
144     aout_DateSet( &p_sys->end_date, 0 );
145
146     p_sys->bytestream = block_BytestreamInit();
147
148     /* Set output properties */
149     p_dec->fmt_out.i_cat = AUDIO_ES;
150     p_dec->fmt_out.i_codec = i_codec;
151     p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
152     p_dec->fmt_out.audio.i_bytes_per_frame = 0;
153
154     /* Set callback */
155     if( b_packetizer )
156         p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
157             DecodeBlock;
158     else
159         p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
160             DecodeBlock;
161     return VLC_SUCCESS;
162 }
163
164 static int OpenDecoder( vlc_object_t *p_this )
165 {
166     return OpenCommon( p_this, false );
167 }
168
169 static int OpenPacketizer( vlc_object_t *p_this )
170 {
171     return OpenCommon( p_this, true );
172 }
173
174 /****************************************************************************
175  * DecodeBlock: the whole thing
176  ****************************************************************************
177  * This function is called just after the thread is launched.
178  ****************************************************************************/
179 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
180 {
181     decoder_sys_t *p_sys = p_dec->p_sys;
182     uint8_t p_header[A52_HEADER_SIZE];
183     uint8_t *p_buf;
184     void *p_out_buffer;
185
186     if( !pp_block || !*pp_block ) return NULL;
187
188     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
189     {
190         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
191         {
192             p_sys->i_state = STATE_NOSYNC;
193             block_BytestreamEmpty( &p_sys->bytestream );
194         }
195         aout_DateSet( &p_sys->end_date, 0 );
196         block_Release( *pp_block );
197         return NULL;
198     }
199
200     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
201     {
202         /* We've just started the stream, wait for the first PTS. */
203         block_Release( *pp_block );
204         return NULL;
205     }
206
207     block_BytestreamPush( &p_sys->bytestream, *pp_block );
208
209     while( 1 )
210     {
211         switch( p_sys->i_state )
212         {
213         case STATE_NOSYNC:
214             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
215                    == VLC_SUCCESS )
216             {
217                 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
218                 {
219                     p_sys->i_state = STATE_SYNC;
220                     break;
221                 }
222                 block_SkipByte( &p_sys->bytestream );
223             }
224             if( p_sys->i_state != STATE_SYNC )
225             {
226                 block_BytestreamFlush( &p_sys->bytestream );
227
228                 /* Need more data */
229                 return NULL;
230             }
231
232         case STATE_SYNC:
233             /* New frame, set the Presentation Time Stamp */
234             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
235             if( p_sys->i_pts != 0 &&
236                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
237             {
238                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
239             }
240             p_sys->i_state = STATE_HEADER;
241
242         case STATE_HEADER:
243             /* Get A/52 frame header (A52_HEADER_SIZE bytes) */
244             if( block_PeekBytes( &p_sys->bytestream, p_header,
245                                  A52_HEADER_SIZE ) != VLC_SUCCESS )
246             {
247                 /* Need more data */
248                 return NULL;
249             }
250
251             /* Check if frame is valid and get frame info */
252             p_sys->i_frame_size = SyncInfo( p_header,
253                                             &p_sys->i_channels,
254                                             &p_sys->i_channels_conf,
255                                             &p_sys->i_rate,
256                                             &p_sys->i_bit_rate );
257             if( !p_sys->i_frame_size )
258             {
259                 msg_Dbg( p_dec, "emulated sync word" );
260                 block_SkipByte( &p_sys->bytestream );
261                 p_sys->i_state = STATE_NOSYNC;
262                 break;
263             }
264             p_sys->i_state = STATE_NEXT_SYNC;
265
266         case STATE_NEXT_SYNC:
267             /* TODO: If pp_block == NULL, flush the buffer without checking the
268              * next sync word */
269
270             /* Check if next expected frame contains the sync word */
271             if( block_PeekOffsetBytes( &p_sys->bytestream,
272                                        p_sys->i_frame_size, p_header, 2 )
273                 != VLC_SUCCESS )
274             {
275                 /* Need more data */
276                 return NULL;
277             }
278
279             if( p_sys->b_packetizer &&
280                 p_header[0] == 0 && p_header[1] == 0 )
281             {
282                 /* A52 wav files and audio CD's use stuffing */
283                 p_sys->i_state = STATE_GET_DATA;
284                 break;
285             }
286
287             if( p_header[0] != 0x0b || p_header[1] != 0x77 )
288             {
289                 msg_Dbg( p_dec, "emulated sync word "
290                          "(no sync on following frame)" );
291                 p_sys->i_state = STATE_NOSYNC;
292                 block_SkipByte( &p_sys->bytestream );
293                 break;
294             }
295             p_sys->i_state = STATE_SEND_DATA;
296             break;
297
298         case STATE_GET_DATA:
299             /* Make sure we have enough data.
300              * (Not useful if we went through NEXT_SYNC) */
301             if( block_WaitBytes( &p_sys->bytestream,
302                                  p_sys->i_frame_size ) != VLC_SUCCESS )
303             {
304                 /* Need more data */
305                 return NULL;
306             }
307             p_sys->i_state = STATE_SEND_DATA;
308
309         case STATE_SEND_DATA:
310             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
311             {
312                 //p_dec->b_error = true;
313                 return NULL;
314             }
315
316             /* Copy the whole frame into the buffer. When we reach this point
317              * we already know we have enough data available. */
318             block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
319
320             /* Make sure we don't reuse the same pts twice */
321             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
322                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
323
324             /* So p_block doesn't get re-added several times */
325             *pp_block = block_BytestreamPop( &p_sys->bytestream );
326
327             p_sys->i_state = STATE_NOSYNC;
328
329             return p_out_buffer;
330         }
331     }
332
333     return NULL;
334 }
335
336 /*****************************************************************************
337  * CloseCommon: clean up the decoder
338  *****************************************************************************/
339 static void CloseCommon( vlc_object_t *p_this )
340 {
341     decoder_t *p_dec = (decoder_t*)p_this;
342     decoder_sys_t *p_sys = p_dec->p_sys;
343
344     block_BytestreamRelease( &p_sys->bytestream );
345
346     free( p_sys );
347 }
348
349 /*****************************************************************************
350  * GetOutBuffer:
351  *****************************************************************************/
352 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
353 {
354     decoder_sys_t *p_sys = p_dec->p_sys;
355     uint8_t *p_buf;
356
357     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
358     {
359         msg_Info( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
360                   p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
361
362         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
363         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
364     }
365
366     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
367     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
368     if( p_dec->fmt_out.audio.i_bytes_per_frame < p_sys->i_frame_size )
369         p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
370     p_dec->fmt_out.audio.i_frame_length = A52_FRAME_NB;
371
372     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
373     p_dec->fmt_out.audio.i_physical_channels =
374         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
375
376     p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
377
378     if( p_sys->b_packetizer )
379     {
380         block_t *p_sout_buffer = GetSoutBuffer( p_dec );
381         p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
382         *pp_out_buffer = p_sout_buffer;
383     }
384     else
385     {
386         aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
387         p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
388         *pp_out_buffer = p_aout_buffer;
389     }
390
391     return p_buf;
392 }
393
394 /*****************************************************************************
395  * GetAoutBuffer:
396  *****************************************************************************/
397 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
398 {
399     decoder_sys_t *p_sys = p_dec->p_sys;
400     aout_buffer_t *p_buf;
401
402     p_buf = decoder_NewAudioBuffer( p_dec, A52_FRAME_NB  );
403     if( p_buf == NULL ) return NULL;
404
405     p_buf->start_date = aout_DateGet( &p_sys->end_date );
406     p_buf->end_date = aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB );
407
408     return p_buf;
409 }
410
411 /*****************************************************************************
412  * GetSoutBuffer:
413  *****************************************************************************/
414 static block_t *GetSoutBuffer( decoder_t *p_dec )
415 {
416     decoder_sys_t *p_sys = p_dec->p_sys;
417     block_t *p_block;
418
419     p_block = block_New( p_dec, p_sys->i_frame_size );
420     if( p_block == NULL ) return NULL;
421
422     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
423
424     p_block->i_length =
425         aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB ) - p_block->i_pts;
426
427     return p_block;
428 }
429
430 /* Tables */
431 static const struct
432 {
433     unsigned int i_count;
434     unsigned int i_configuration;
435 } p_acmod[8] = {
436     { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DUALMONO },   /* Dual-channel 1+1 */
437     { 1, AOUT_CHAN_CENTER },                                        /* Mono 1/0 */
438     { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT },                        /* Stereo 2/0 */
439     { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER },     /* 3F 3/0 */
440     { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER }, /* 2F1R 2/1 */
441     { 4, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
442          AOUT_CHAN_REARCENTER },                                    /* 3F1R 3/1 */
443     { 5, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
444          AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT },                /* 2F2R 2/2 */
445     { 6, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
446          AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT },                /* 3F2R 3/2 */
447 };
448
449 /**
450  * It parse AC3 sync info.
451  *
452  * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
453  * since we don't want to oblige S/PDIF people to use liba52 just to get
454  * their SyncInfo...
455  */
456 static int SyncInfoAC3( const uint8_t *p_buf,
457                         unsigned int *pi_channels,
458                         unsigned int *pi_channels_conf,
459                         unsigned int *pi_sample_rate, int *pi_bit_rate )
460 {
461     static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
462     static const int rate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
463                                 128, 160, 192, 224, 256, 320, 384, 448,
464                                 512, 576, 640 };
465     static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
466                                       0x04, 0x01, 0x04, 0x01 };
467     int frmsizecod;
468     int bitrate;
469     int half;
470     int acmod;
471
472     /* */
473     half = halfrate[p_buf[5] >> 3];
474
475     /* acmod, dsurmod and lfeon */
476     acmod = p_buf[6] >> 5;
477     if ( (p_buf[6] & 0xf8) == 0x50 )
478     {
479         /* Dolby surround = stereo + Dolby */
480         *pi_channels = 2;
481         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
482                             | AOUT_CHAN_DOLBYSTEREO;
483     }
484     else
485     {
486         *pi_channels      = p_acmod[acmod].i_count;
487         *pi_channels_conf = p_acmod[acmod].i_configuration;
488     }
489
490     if ( p_buf[6] & lfeon[acmod] )
491     {
492         (*pi_channels)++;
493         *pi_channels_conf |= AOUT_CHAN_LFE;
494     }
495
496     frmsizecod = p_buf[4] & 63;
497     if (frmsizecod >= 38)
498         return 0;
499     bitrate = rate [frmsizecod >> 1];
500     *pi_bit_rate = (bitrate * 1000) >> half;
501
502     switch (p_buf[4] & 0xc0) {
503     case 0:
504         *pi_sample_rate = 48000 >> half;
505         return 4 * bitrate;
506     case 0x40:
507         *pi_sample_rate = 44100 >> half;
508         return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
509     case 0x80:
510         *pi_sample_rate = 32000 >> half;
511         return 6 * bitrate;
512     default:
513         return 0;
514     }
515 }
516
517 /**
518  * It parse E-AC3 sync info
519  */
520 static int SyncInfoEAC3( const uint8_t *p_buf,
521                          unsigned int *pi_channels,
522                          unsigned int *pi_channels_conf,
523                          unsigned int *pi_sample_rate, int *pi_bit_rate )
524 {
525     static const int pi_samplerate[3] = { 48000, 44100, 32000 };
526     bs_t s;
527     int i_frame_size;
528     int i_fscod, i_fscod2;
529     int i_numblkscod;
530     int i_acmod, i_lfeon;
531     int i_bytes;
532
533
534     bs_init( &s, (void*)p_buf, A52_HEADER_SIZE );
535     bs_skip( &s, 16 +   /* start code */
536                  2 +    /* stream type */
537                  3 );   /* substream id */
538     i_frame_size = bs_read( &s, 11 );
539     if( i_frame_size < 2 )
540         return 0;
541     i_bytes = 2 * ( i_frame_size + 1 );
542
543     i_fscod = bs_read( &s, 2 );
544     if( i_fscod == 0x03 )
545     {
546         i_fscod2 = bs_read( &s, 2 );
547         if( i_fscod2 == 0X03 )
548             return 0;
549         *pi_sample_rate = pi_samplerate[i_fscod2] / 2;
550         i_numblkscod = 6;
551     }
552     else
553     {
554         static const int pi_blocks[4] = { 1, 2, 3, 6 };
555
556         *pi_sample_rate = pi_samplerate[i_fscod];
557         i_numblkscod = pi_blocks[bs_read( &s, 2 )];
558     }
559
560     i_acmod = bs_read( &s, 3 );
561     i_lfeon = bs_read1( &s );
562
563     *pi_channels      = p_acmod[i_acmod].i_count + i_lfeon;
564     *pi_channels_conf = p_acmod[i_acmod].i_configuration | ( i_lfeon ? AOUT_CHAN_LFE : 0);
565     *pi_bit_rate = 8 * i_bytes * (*pi_sample_rate) / (i_numblkscod * 256);
566
567     return i_bytes;
568 }
569
570 static int SyncInfo( const uint8_t *p_buf,
571                      unsigned int *pi_channels,
572                      unsigned int *pi_channels_conf,
573                      unsigned int *pi_sample_rate, int *pi_bit_rate )
574 {
575     int bsid;
576
577     /* Check synword */
578     if( p_buf[0] != 0x0b || p_buf[1] != 0x77 )
579         return 0;
580
581     /* Check bsid */
582     bsid = p_buf[5] >> 3;
583     if( bsid > 16 )
584         return 0;
585
586     if( bsid <= 10 )
587         return SyncInfoAC3( p_buf, pi_channels, pi_channels_conf,
588                             pi_sample_rate, pi_bit_rate );
589     else
590         return SyncInfoEAC3( p_buf, pi_channels, pi_channels_conf,
591                              pi_sample_rate, pi_bit_rate );
592 }
593