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 *****************************************************************************/
30 #include <vlc_codec.h>
32 #include <vlc_block_helper.h>
34 #define A52_HEADER_SIZE 7
36 /*****************************************************************************
37 * decoder_sys_t : decoder descriptor
38 *****************************************************************************/
42 vlc_bool_t b_packetizer;
49 block_bytestream_t bytestream;
54 audio_date_t end_date;
57 int i_frame_size, i_bit_rate;
58 unsigned int i_rate, i_channels, i_channels_conf;
72 /****************************************************************************
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 ** );
80 static int SyncInfo ( const byte_t *, unsigned int *, unsigned int *,
81 unsigned int *, int * );
83 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
84 static aout_buffer_t *GetAoutBuffer( decoder_t * );
85 static block_t *GetSoutBuffer( decoder_t * );
87 /*****************************************************************************
89 *****************************************************************************/
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 );
98 set_description( _("A/52 audio packetizer") );
99 set_capability( "packetizer", 10 );
100 set_callbacks( OpenPacketizer, CloseDecoder );
103 /*****************************************************************************
104 * OpenDecoder: probe the decoder and return score
105 *****************************************************************************/
106 static int OpenDecoder( vlc_object_t *p_this )
108 decoder_t *p_dec = (decoder_t*)p_this;
109 decoder_sys_t *p_sys;
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') )
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 )
121 msg_Err( p_dec, "out of memory" );
126 p_sys->b_packetizer = VLC_FALSE;
127 p_sys->i_state = STATE_NOSYNC;
128 aout_DateSet( &p_sys->end_date, 0 );
130 p_sys->bytestream = block_BytestreamInit( p_dec );
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 */
138 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
140 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
146 static int OpenPacketizer( vlc_object_t *p_this )
148 decoder_t *p_dec = (decoder_t*)p_this;
150 int i_ret = OpenDecoder( p_this );
152 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
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 )
164 decoder_sys_t *p_sys = p_dec->p_sys;
165 uint8_t p_header[A52_HEADER_SIZE];
169 if( !pp_block || !*pp_block ) return NULL;
171 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
173 /* We've just started the stream, wait for the first PTS. */
174 block_Release( *pp_block );
178 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
180 p_sys->i_state = STATE_NOSYNC;
183 block_BytestreamPush( &p_sys->bytestream, *pp_block );
187 switch( p_sys->i_state )
190 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
193 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
195 p_sys->i_state = STATE_SYNC;
198 block_SkipByte( &p_sys->bytestream );
200 if( p_sys->i_state != STATE_SYNC )
202 block_BytestreamFlush( &p_sys->bytestream );
209 /* New frame, set the Presentation Time Stamp */
210 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
211 if( p_sys->i_pts != 0 &&
212 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
214 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
216 p_sys->i_state = STATE_HEADER;
219 /* Get A/52 frame header (A52_HEADER_SIZE bytes) */
220 if( block_PeekBytes( &p_sys->bytestream, p_header,
221 A52_HEADER_SIZE ) != VLC_SUCCESS )
227 /* Check if frame is valid and get frame info */
228 p_sys->i_frame_size = SyncInfo( p_header,
230 &p_sys->i_channels_conf,
232 &p_sys->i_bit_rate );
233 if( !p_sys->i_frame_size )
235 msg_Dbg( p_dec, "emulated sync word" );
236 block_SkipByte( &p_sys->bytestream );
237 p_sys->i_state = STATE_NOSYNC;
240 p_sys->i_state = STATE_NEXT_SYNC;
242 case STATE_NEXT_SYNC:
243 /* TODO: If pp_block == NULL, flush the buffer without checking the
246 /* Check if next expected frame contains the sync word */
247 if( block_PeekOffsetBytes( &p_sys->bytestream,
248 p_sys->i_frame_size, p_header, 2 )
255 if( p_sys->b_packetizer &&
256 p_header[0] == 0 && p_header[1] == 0 )
258 /* A52 wav files and audio CD's use stuffing */
259 p_sys->i_state = STATE_GET_DATA;
263 if( p_header[0] != 0x0b || p_header[1] != 0x77 )
265 msg_Dbg( p_dec, "emulated sync word "
266 "(no sync on following frame)" );
267 p_sys->i_state = STATE_NOSYNC;
268 block_SkipByte( &p_sys->bytestream );
271 p_sys->i_state = STATE_SEND_DATA;
275 /* Make sure we have enough data.
276 * (Not useful if we went through NEXT_SYNC) */
277 if( block_WaitBytes( &p_sys->bytestream,
278 p_sys->i_frame_size ) != VLC_SUCCESS )
283 p_sys->i_state = STATE_SEND_DATA;
285 case STATE_SEND_DATA:
286 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
288 //p_dec->b_error = VLC_TRUE;
292 /* Copy the whole frame into the buffer. When we reach this point
293 * we already know we have enough data available. */
294 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
296 /* Make sure we don't reuse the same pts twice */
297 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
298 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
300 /* So p_block doesn't get re-added several times */
301 *pp_block = block_BytestreamPop( &p_sys->bytestream );
303 p_sys->i_state = STATE_NOSYNC;
312 /*****************************************************************************
313 * CloseDecoder: clean up the decoder
314 *****************************************************************************/
315 static void CloseDecoder( vlc_object_t *p_this )
317 decoder_t *p_dec = (decoder_t*)p_this;
318 decoder_sys_t *p_sys = p_dec->p_sys;
320 block_BytestreamRelease( &p_sys->bytestream );
325 /*****************************************************************************
327 *****************************************************************************/
328 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
330 decoder_sys_t *p_sys = p_dec->p_sys;
333 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
335 msg_Info( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
336 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
338 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
339 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
342 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
343 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
344 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
345 p_dec->fmt_out.audio.i_frame_length = A52_FRAME_NB;
347 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
348 p_dec->fmt_out.audio.i_physical_channels =
349 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
351 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
353 if( p_sys->b_packetizer )
355 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
356 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
357 *pp_out_buffer = p_sout_buffer;
361 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
362 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
363 *pp_out_buffer = p_aout_buffer;
369 /*****************************************************************************
371 *****************************************************************************/
372 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
374 decoder_sys_t *p_sys = p_dec->p_sys;
375 aout_buffer_t *p_buf;
377 p_buf = p_dec->pf_aout_buffer_new( p_dec, A52_FRAME_NB );
378 if( p_buf == NULL ) return NULL;
380 p_buf->start_date = aout_DateGet( &p_sys->end_date );
381 p_buf->end_date = aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB );
386 /*****************************************************************************
388 *****************************************************************************/
389 static block_t *GetSoutBuffer( decoder_t *p_dec )
391 decoder_sys_t *p_sys = p_dec->p_sys;
394 p_block = block_New( p_dec, p_sys->i_frame_size );
395 if( p_block == NULL ) return NULL;
397 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
399 p_block->i_length = aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB ) -
405 /*****************************************************************************
406 * SyncInfo: parse A/52 sync info
407 *****************************************************************************
408 * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
409 * since we don't want to oblige S/PDIF people to use liba52 just to get
411 *****************************************************************************/
412 static int SyncInfo( const byte_t * p_buf,
413 unsigned int * pi_channels,
414 unsigned int * pi_channels_conf,
415 unsigned int * pi_sample_rate, int * pi_bit_rate )
417 static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
418 static const int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
419 128, 160, 192, 224, 256, 320, 384, 448,
421 static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
422 0x04, 0x01, 0x04, 0x01 };
428 if ((p_buf[0] != 0x0b) || (p_buf[1] != 0x77)) /* syncword */
431 if (p_buf[5] >= 0x60) /* bsid >= 12 */
433 half = halfrate[p_buf[5] >> 3];
435 /* acmod, dsurmod and lfeon */
436 acmod = p_buf[6] >> 5;
437 if ( (p_buf[6] & 0xf8) == 0x50 )
439 /* Dolby surround = stereo + Dolby */
441 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
442 | AOUT_CHAN_DOLBYSTEREO;
444 else switch ( acmod )
447 /* Dual-mono = stereo + dual-mono */
449 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
450 | AOUT_CHAN_DUALMONO;
455 *pi_channels_conf = AOUT_CHAN_CENTER;
460 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
465 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
471 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
472 | AOUT_CHAN_REARCENTER;
477 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
478 | AOUT_CHAN_REARCENTER;
483 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
484 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
489 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
490 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
496 if ( p_buf[6] & lfeon[acmod] )
499 *pi_channels_conf |= AOUT_CHAN_LFE;
502 frmsizecod = p_buf[4] & 63;
503 if (frmsizecod >= 38)
505 bitrate = rate [frmsizecod >> 1];
506 *pi_bit_rate = (bitrate * 1000) >> half;
508 switch (p_buf[4] & 0xc0) {
510 *pi_sample_rate = 48000 >> half;
513 *pi_sample_rate = 44100 >> half;
514 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
516 *pi_sample_rate = 32000 >> half;