1 /*****************************************************************************
2 * a52.c: parse A/52 audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2001-2002 the VideoLAN team
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
37 #include <vlc_block_helper.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int OpenDecoder ( vlc_object_t * );
44 static int OpenPacketizer( vlc_object_t * );
45 static void CloseCommon ( vlc_object_t * );
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 )
55 set_description( N_("A/52 audio packetizer") )
56 set_capability( "packetizer", 10 )
57 set_callbacks( OpenPacketizer, CloseCommon )
60 /*****************************************************************************
61 * decoder_sys_t : decoder descriptor
62 *****************************************************************************/
64 #define A52_HEADER_SIZE 7
76 block_bytestream_t bytestream;
81 audio_date_t end_date;
84 int i_frame_size, i_bit_rate;
85 unsigned int i_rate, i_channels, i_channels_conf;
98 /****************************************************************************
100 ****************************************************************************/
101 static void *DecodeBlock ( decoder_t *, block_t ** );
103 static int SyncInfo ( const uint8_t *, unsigned int *, unsigned int *,
104 unsigned int *, int * );
106 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
107 static aout_buffer_t *GetAoutBuffer( decoder_t * );
108 static block_t *GetSoutBuffer( decoder_t * );
110 /*****************************************************************************
111 * OpenCommon: probe the decoder/packetizer and return score
112 *****************************************************************************/
113 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
115 decoder_t *p_dec = (decoder_t*)p_this;
116 decoder_sys_t *p_sys;
117 vlc_fourcc_t i_codec;
119 switch( p_dec->fmt_in.i_codec )
121 case VLC_FOURCC('a','5','2',' '):
122 case VLC_FOURCC('a','5','2','b'):
123 i_codec = VLC_FOURCC('a','5','2',' ');
125 case VLC_FOURCC('e','a','c','3'):
126 /* XXX ugly hack, a52 does not support eac3 so no eac3 pass-through
130 i_codec = VLC_FOURCC('e','a','c','3');
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 )
142 p_sys->b_packetizer = b_packetizer;
143 p_sys->i_state = STATE_NOSYNC;
144 aout_DateSet( &p_sys->end_date, 0 );
146 p_sys->bytestream = block_BytestreamInit();
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;
156 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
159 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
164 static int OpenDecoder( vlc_object_t *p_this )
166 return OpenCommon( p_this, false );
169 static int OpenPacketizer( vlc_object_t *p_this )
171 return OpenCommon( p_this, true );
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 )
181 decoder_sys_t *p_sys = p_dec->p_sys;
182 uint8_t p_header[A52_HEADER_SIZE];
186 if( !pp_block || !*pp_block ) return NULL;
188 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
190 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
192 p_sys->i_state = STATE_NOSYNC;
193 block_BytestreamEmpty( &p_sys->bytestream );
195 aout_DateSet( &p_sys->end_date, 0 );
196 block_Release( *pp_block );
200 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
202 /* We've just started the stream, wait for the first PTS. */
203 block_Release( *pp_block );
207 block_BytestreamPush( &p_sys->bytestream, *pp_block );
211 switch( p_sys->i_state )
214 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
217 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
219 p_sys->i_state = STATE_SYNC;
222 block_SkipByte( &p_sys->bytestream );
224 if( p_sys->i_state != STATE_SYNC )
226 block_BytestreamFlush( &p_sys->bytestream );
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 ) )
238 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
240 p_sys->i_state = 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 )
251 /* Check if frame is valid and get frame info */
252 p_sys->i_frame_size = SyncInfo( p_header,
254 &p_sys->i_channels_conf,
256 &p_sys->i_bit_rate );
257 if( !p_sys->i_frame_size )
259 msg_Dbg( p_dec, "emulated sync word" );
260 block_SkipByte( &p_sys->bytestream );
261 p_sys->i_state = STATE_NOSYNC;
264 p_sys->i_state = STATE_NEXT_SYNC;
266 case STATE_NEXT_SYNC:
267 /* TODO: If pp_block == NULL, flush the buffer without checking the
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 )
279 if( p_sys->b_packetizer &&
280 p_header[0] == 0 && p_header[1] == 0 )
282 /* A52 wav files and audio CD's use stuffing */
283 p_sys->i_state = STATE_GET_DATA;
287 if( p_header[0] != 0x0b || p_header[1] != 0x77 )
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 );
295 p_sys->i_state = STATE_SEND_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 )
307 p_sys->i_state = STATE_SEND_DATA;
309 case STATE_SEND_DATA:
310 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
312 //p_dec->b_error = true;
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 );
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;
324 /* So p_block doesn't get re-added several times */
325 *pp_block = block_BytestreamPop( &p_sys->bytestream );
327 p_sys->i_state = STATE_NOSYNC;
336 /*****************************************************************************
337 * CloseCommon: clean up the decoder
338 *****************************************************************************/
339 static void CloseCommon( vlc_object_t *p_this )
341 decoder_t *p_dec = (decoder_t*)p_this;
342 decoder_sys_t *p_sys = p_dec->p_sys;
344 block_BytestreamRelease( &p_sys->bytestream );
349 /*****************************************************************************
351 *****************************************************************************/
352 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
354 decoder_sys_t *p_sys = p_dec->p_sys;
357 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
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 );
362 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
363 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
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;
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;
376 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
378 if( p_sys->b_packetizer )
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;
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;
394 /*****************************************************************************
396 *****************************************************************************/
397 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
399 decoder_sys_t *p_sys = p_dec->p_sys;
400 aout_buffer_t *p_buf;
402 p_buf = decoder_NewAudioBuffer( p_dec, A52_FRAME_NB );
403 if( p_buf == NULL ) return NULL;
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 );
411 /*****************************************************************************
413 *****************************************************************************/
414 static block_t *GetSoutBuffer( decoder_t *p_dec )
416 decoder_sys_t *p_sys = p_dec->p_sys;
419 p_block = block_New( p_dec, p_sys->i_frame_size );
420 if( p_block == NULL ) return NULL;
422 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
425 aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB ) - p_block->i_pts;
433 unsigned int i_count;
434 unsigned int i_configuration;
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 */
450 * It parse AC3 sync info.
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
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 )
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,
465 static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
466 0x04, 0x01, 0x04, 0x01 };
473 half = halfrate[p_buf[5] >> 3];
475 /* acmod, dsurmod and lfeon */
476 acmod = p_buf[6] >> 5;
477 if ( (p_buf[6] & 0xf8) == 0x50 )
479 /* Dolby surround = stereo + Dolby */
481 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
482 | AOUT_CHAN_DOLBYSTEREO;
486 *pi_channels = p_acmod[acmod].i_count;
487 *pi_channels_conf = p_acmod[acmod].i_configuration;
490 if ( p_buf[6] & lfeon[acmod] )
493 *pi_channels_conf |= AOUT_CHAN_LFE;
496 frmsizecod = p_buf[4] & 63;
497 if (frmsizecod >= 38)
499 bitrate = rate [frmsizecod >> 1];
500 *pi_bit_rate = (bitrate * 1000) >> half;
502 switch (p_buf[4] & 0xc0) {
504 *pi_sample_rate = 48000 >> half;
507 *pi_sample_rate = 44100 >> half;
508 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
510 *pi_sample_rate = 32000 >> half;
518 * It parse E-AC3 sync info
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 )
525 static const int pi_samplerate[3] = { 48000, 44100, 32000 };
528 int i_fscod, i_fscod2;
530 int i_acmod, i_lfeon;
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 )
541 i_bytes = 2 * ( i_frame_size + 1 );
543 i_fscod = bs_read( &s, 2 );
544 if( i_fscod == 0x03 )
546 i_fscod2 = bs_read( &s, 2 );
547 if( i_fscod2 == 0X03 )
549 *pi_sample_rate = pi_samplerate[i_fscod2] / 2;
554 static const int pi_blocks[4] = { 1, 2, 3, 6 };
556 *pi_sample_rate = pi_samplerate[i_fscod];
557 i_numblkscod = pi_blocks[bs_read( &s, 2 )];
560 i_acmod = bs_read( &s, 3 );
561 i_lfeon = bs_read1( &s );
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);
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 )
578 if( p_buf[0] != 0x0b || p_buf[1] != 0x77 )
582 bsid = p_buf[5] >> 3;
587 return SyncInfoAC3( p_buf, pi_channels, pi_channels_conf,
588 pi_sample_rate, pi_bit_rate );
590 return SyncInfoEAC3( p_buf, pi_channels, pi_channels_conf,
591 pi_sample_rate, pi_bit_rate );