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