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_input.h>
38 #include <vlc_block_helper.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int OpenDecoder ( vlc_object_t * );
45 static int OpenPacketizer( vlc_object_t * );
46 static void CloseCommon ( vlc_object_t * );
49 set_description( N_("A/52 parser") );
50 set_capability( "decoder", 100 );
51 set_callbacks( OpenDecoder, CloseCommon );
52 set_category( CAT_INPUT );
53 set_subcategory( SUBCAT_INPUT_ACODEC );
56 set_description( N_("A/52 audio packetizer") );
57 set_capability( "packetizer", 10 );
58 set_callbacks( OpenPacketizer, CloseCommon );
61 /*****************************************************************************
62 * decoder_sys_t : decoder descriptor
63 *****************************************************************************/
65 #define A52_HEADER_SIZE 7
77 block_bytestream_t bytestream;
82 audio_date_t end_date;
85 int i_frame_size, i_bit_rate;
86 unsigned int i_rate, i_channels, i_channels_conf;
101 /****************************************************************************
103 ****************************************************************************/
104 static void *DecodeBlock ( decoder_t *, block_t ** );
106 static int SyncInfo ( const uint8_t *, unsigned int *, unsigned int *,
107 unsigned int *, int * );
109 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
110 static aout_buffer_t *GetAoutBuffer( decoder_t * );
111 static block_t *GetSoutBuffer( decoder_t * );
113 /*****************************************************************************
114 * OpenCommon: probe the decoder/packetizer and return score
115 *****************************************************************************/
116 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
118 decoder_t *p_dec = (decoder_t*)p_this;
119 decoder_sys_t *p_sys;
120 vlc_fourcc_t i_codec;
122 switch( p_dec->fmt_in.i_codec )
124 case VLC_FOURCC('a','5','2',' '):
125 case VLC_FOURCC('a','5','2','b'):
126 i_codec = VLC_FOURCC('a','5','2',' ');
128 case VLC_FOURCC('e','a','c','3'):
129 /* XXX ugly hack, a52 does not support eac3 so no eac3 pass-through
133 i_codec = VLC_FOURCC('e','a','c','3');
139 /* Allocate the memory needed to store the decoder's structure */
140 if( ( p_dec->p_sys = p_sys =
141 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
145 p_sys->b_packetizer = b_packetizer;
146 p_sys->i_state = STATE_NOSYNC;
147 aout_DateSet( &p_sys->end_date, 0 );
149 p_sys->bytestream = block_BytestreamInit();
150 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
152 /* Set output properties */
153 p_dec->fmt_out.i_cat = AUDIO_ES;
154 p_dec->fmt_out.i_codec = i_codec;
155 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
159 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
162 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
167 static int OpenDecoder( vlc_object_t *p_this )
169 return OpenCommon( p_this, false );
172 static int OpenPacketizer( vlc_object_t *p_this )
174 return OpenCommon( p_this, true );
177 /****************************************************************************
178 * DecodeBlock: the whole thing
179 ****************************************************************************
180 * This function is called just after the thread is launched.
181 ****************************************************************************/
182 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
184 decoder_sys_t *p_sys = p_dec->p_sys;
185 uint8_t p_header[A52_HEADER_SIZE];
189 if( !pp_block || !*pp_block ) return NULL;
191 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
193 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
195 p_sys->i_state = STATE_NOSYNC;
196 block_BytestreamFlush( &p_sys->bytestream );
198 // aout_DateSet( &p_sys->end_date, 0 );
199 block_Release( *pp_block );
203 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
205 /* We've just started the stream, wait for the first PTS. */
206 block_Release( *pp_block );
210 if( (*pp_block)->i_rate > 0 )
211 p_sys->i_input_rate = (*pp_block)->i_rate;
213 block_BytestreamPush( &p_sys->bytestream, *pp_block );
217 switch( p_sys->i_state )
220 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
223 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
225 p_sys->i_state = STATE_SYNC;
228 block_SkipByte( &p_sys->bytestream );
230 if( p_sys->i_state != STATE_SYNC )
232 block_BytestreamFlush( &p_sys->bytestream );
239 /* New frame, set the Presentation Time Stamp */
240 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
241 if( p_sys->i_pts != 0 &&
242 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
244 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
246 p_sys->i_state = STATE_HEADER;
249 /* Get A/52 frame header (A52_HEADER_SIZE bytes) */
250 if( block_PeekBytes( &p_sys->bytestream, p_header,
251 A52_HEADER_SIZE ) != VLC_SUCCESS )
257 /* Check if frame is valid and get frame info */
258 p_sys->i_frame_size = SyncInfo( p_header,
260 &p_sys->i_channels_conf,
262 &p_sys->i_bit_rate );
263 if( !p_sys->i_frame_size )
265 msg_Dbg( p_dec, "emulated sync word" );
266 block_SkipByte( &p_sys->bytestream );
267 p_sys->i_state = STATE_NOSYNC;
270 p_sys->i_state = STATE_NEXT_SYNC;
272 case STATE_NEXT_SYNC:
273 /* TODO: If pp_block == NULL, flush the buffer without checking the
276 /* Check if next expected frame contains the sync word */
277 if( block_PeekOffsetBytes( &p_sys->bytestream,
278 p_sys->i_frame_size, p_header, 2 )
285 if( p_sys->b_packetizer &&
286 p_header[0] == 0 && p_header[1] == 0 )
288 /* A52 wav files and audio CD's use stuffing */
289 p_sys->i_state = STATE_GET_DATA;
293 if( p_header[0] != 0x0b || p_header[1] != 0x77 )
295 msg_Dbg( p_dec, "emulated sync word "
296 "(no sync on following frame)" );
297 p_sys->i_state = STATE_NOSYNC;
298 block_SkipByte( &p_sys->bytestream );
301 p_sys->i_state = STATE_SEND_DATA;
305 /* Make sure we have enough data.
306 * (Not useful if we went through NEXT_SYNC) */
307 if( block_WaitBytes( &p_sys->bytestream,
308 p_sys->i_frame_size ) != VLC_SUCCESS )
313 p_sys->i_state = STATE_SEND_DATA;
315 case STATE_SEND_DATA:
316 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
318 //p_dec->b_error = true;
322 /* Copy the whole frame into the buffer. When we reach this point
323 * we already know we have enough data available. */
324 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
326 /* Make sure we don't reuse the same pts twice */
327 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
328 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
330 /* So p_block doesn't get re-added several times */
331 *pp_block = block_BytestreamPop( &p_sys->bytestream );
333 p_sys->i_state = STATE_NOSYNC;
342 /*****************************************************************************
343 * CloseCommon: clean up the decoder
344 *****************************************************************************/
345 static void CloseCommon( vlc_object_t *p_this )
347 decoder_t *p_dec = (decoder_t*)p_this;
348 decoder_sys_t *p_sys = p_dec->p_sys;
350 block_BytestreamRelease( &p_sys->bytestream );
355 /*****************************************************************************
357 *****************************************************************************/
358 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
360 decoder_sys_t *p_sys = p_dec->p_sys;
363 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
365 msg_Info( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
366 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
368 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
369 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
372 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
373 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
374 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
375 p_dec->fmt_out.audio.i_frame_length = A52_FRAME_NB;
377 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
378 p_dec->fmt_out.audio.i_physical_channels =
379 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
381 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
383 if( p_sys->b_packetizer )
385 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
386 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
387 *pp_out_buffer = p_sout_buffer;
391 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
392 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
393 *pp_out_buffer = p_aout_buffer;
399 /*****************************************************************************
401 *****************************************************************************/
402 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
404 decoder_sys_t *p_sys = p_dec->p_sys;
405 aout_buffer_t *p_buf;
407 p_buf = p_dec->pf_aout_buffer_new( p_dec, A52_FRAME_NB );
408 if( p_buf == NULL ) return NULL;
410 p_buf->start_date = aout_DateGet( &p_sys->end_date );
411 p_buf->end_date = aout_DateIncrement( &p_sys->end_date,
412 A52_FRAME_NB * p_sys->i_input_rate / INPUT_RATE_DEFAULT );
417 /*****************************************************************************
419 *****************************************************************************/
420 static block_t *GetSoutBuffer( decoder_t *p_dec )
422 decoder_sys_t *p_sys = p_dec->p_sys;
425 p_block = block_New( p_dec, p_sys->i_frame_size );
426 if( p_block == NULL ) return NULL;
428 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
431 aout_DateIncrement( &p_sys->end_date,
432 A52_FRAME_NB * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
441 unsigned int i_count;
442 unsigned int i_configuration;
444 { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DUALMONO }, /* Dual-channel 1+1 */
445 { 1, AOUT_CHAN_CENTER }, /* Mono 1/0 */
446 { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT }, /* Stereo 2/0 */
447 { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER }, /* 3F 3/0 */
448 { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER }, /* 2F1R 2/1 */
449 { 4, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
450 AOUT_CHAN_REARCENTER }, /* 3F1R 3/1 */
451 { 5, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
452 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT }, /* 2F2R 2/2 */
453 { 6, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
454 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT }, /* 3F2R 3/2 */
458 * It parse AC3 sync info.
460 * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
461 * since we don't want to oblige S/PDIF people to use liba52 just to get
464 static int SyncInfoAC3( const uint8_t *p_buf,
465 unsigned int *pi_channels,
466 unsigned int *pi_channels_conf,
467 unsigned int *pi_sample_rate, int *pi_bit_rate )
469 static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
470 static const int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
471 128, 160, 192, 224, 256, 320, 384, 448,
473 static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
474 0x04, 0x01, 0x04, 0x01 };
481 half = halfrate[p_buf[5] >> 3];
483 /* acmod, dsurmod and lfeon */
484 acmod = p_buf[6] >> 5;
485 if ( (p_buf[6] & 0xf8) == 0x50 )
487 /* Dolby surround = stereo + Dolby */
489 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
490 | AOUT_CHAN_DOLBYSTEREO;
494 *pi_channels = p_acmod[acmod].i_count;
495 *pi_channels_conf = p_acmod[acmod].i_configuration;
498 if ( p_buf[6] & lfeon[acmod] )
501 *pi_channels_conf |= AOUT_CHAN_LFE;
504 frmsizecod = p_buf[4] & 63;
505 if (frmsizecod >= 38)
507 bitrate = rate [frmsizecod >> 1];
508 *pi_bit_rate = (bitrate * 1000) >> half;
510 switch (p_buf[4] & 0xc0) {
512 *pi_sample_rate = 48000 >> half;
515 *pi_sample_rate = 44100 >> half;
516 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
518 *pi_sample_rate = 32000 >> half;
526 * It parse E-AC3 sync info
528 static int SyncInfoEAC3( const uint8_t *p_buf,
529 unsigned int *pi_channels,
530 unsigned int *pi_channels_conf,
531 unsigned int *pi_sample_rate, int *pi_bit_rate )
533 static const int pi_samplerate[3] = { 48000, 44100, 32000 };
536 int i_fscod, i_fscod2;
538 int i_acmod, i_lfeon;
542 bs_init( &s, (void*)p_buf, A52_HEADER_SIZE );
543 bs_skip( &s, 16 + /* start code */
544 2 + /* stream type */
545 3 ); /* substream id */
546 i_frame_size = bs_read( &s, 11 );
547 if( i_frame_size < 2 )
549 i_bytes = 2 * ( i_frame_size + 1 );
551 i_fscod = bs_read( &s, 2 );
552 if( i_fscod == 0x03 )
554 i_fscod2 = bs_read( &s, 2 );
555 if( i_fscod2 == 0X03 )
557 *pi_sample_rate = pi_samplerate[i_fscod2] / 2;
562 static const int pi_blocks[4] = { 1, 2, 3, 6 };
564 *pi_sample_rate = pi_samplerate[i_fscod];
565 i_numblkscod = pi_blocks[bs_read( &s, 2 )];
568 i_acmod = bs_read( &s, 3 );
569 i_lfeon = bs_read1( &s );
571 *pi_channels = p_acmod[i_acmod].i_count + i_lfeon;
572 *pi_channels_conf = p_acmod[i_acmod].i_configuration | ( i_lfeon ? AOUT_CHAN_LFE : 0);
573 *pi_bit_rate = 8 * i_bytes * (*pi_sample_rate) / (i_numblkscod * 256);
578 static int SyncInfo( const uint8_t *p_buf,
579 unsigned int *pi_channels,
580 unsigned int *pi_channels_conf,
581 unsigned int *pi_sample_rate, int *pi_bit_rate )
586 if( p_buf[0] != 0x0b || p_buf[1] != 0x77 )
590 bsid = p_buf[5] >> 3;
595 return SyncInfoAC3( p_buf, pi_channels, pi_channels_conf,
596 pi_sample_rate, pi_bit_rate );
598 return SyncInfoEAC3( p_buf, pi_channels, pi_channels_conf,
599 pi_sample_rate, pi_bit_rate );