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 *****************************************************************************/
34 #include <vlc_codec.h>
36 #include <vlc_input.h>
37 #include <vlc_block_helper.h>
39 #define A52_HEADER_SIZE 7
41 /*****************************************************************************
42 * decoder_sys_t : decoder descriptor
43 *****************************************************************************/
47 vlc_bool_t b_packetizer;
54 block_bytestream_t bytestream;
59 audio_date_t end_date;
62 int i_frame_size, i_bit_rate;
63 unsigned int i_rate, i_channels, i_channels_conf;
78 /****************************************************************************
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 ** );
86 static int SyncInfo ( const byte_t *, unsigned int *, unsigned int *,
87 unsigned int *, int * );
89 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
90 static aout_buffer_t *GetAoutBuffer( decoder_t * );
91 static block_t *GetSoutBuffer( decoder_t * );
93 /*****************************************************************************
95 *****************************************************************************/
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 );
104 set_description( _("A/52 audio packetizer") );
105 set_capability( "packetizer", 10 );
106 set_callbacks( OpenPacketizer, CloseDecoder );
109 /*****************************************************************************
110 * OpenDecoder: probe the decoder and return score
111 *****************************************************************************/
112 static int OpenDecoder( vlc_object_t *p_this )
114 decoder_t *p_dec = (decoder_t*)p_this;
115 decoder_sys_t *p_sys;
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') )
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 )
127 msg_Err( p_dec, "out of memory" );
132 p_sys->b_packetizer = VLC_FALSE;
133 p_sys->i_state = STATE_NOSYNC;
134 aout_DateSet( &p_sys->end_date, 0 );
136 p_sys->bytestream = block_BytestreamInit( p_dec );
137 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
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 */
145 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
147 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
153 static int OpenPacketizer( vlc_object_t *p_this )
155 decoder_t *p_dec = (decoder_t*)p_this;
157 int i_ret = OpenDecoder( p_this );
159 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
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 )
171 decoder_sys_t *p_sys = p_dec->p_sys;
172 uint8_t p_header[A52_HEADER_SIZE];
176 if( !pp_block || !*pp_block ) return NULL;
178 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
180 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
182 p_sys->i_state = STATE_NOSYNC;
183 block_BytestreamFlush( &p_sys->bytestream );
185 // aout_DateSet( &p_sys->end_date, 0 );
186 block_Release( *pp_block );
190 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
192 /* We've just started the stream, wait for the first PTS. */
193 block_Release( *pp_block );
197 if( (*pp_block)->i_rate > 0 )
198 p_sys->i_input_rate = (*pp_block)->i_rate;
200 block_BytestreamPush( &p_sys->bytestream, *pp_block );
204 switch( p_sys->i_state )
207 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
210 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
212 p_sys->i_state = STATE_SYNC;
215 block_SkipByte( &p_sys->bytestream );
217 if( p_sys->i_state != STATE_SYNC )
219 block_BytestreamFlush( &p_sys->bytestream );
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 ) )
231 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
233 p_sys->i_state = 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 )
244 /* Check if frame is valid and get frame info */
245 p_sys->i_frame_size = SyncInfo( p_header,
247 &p_sys->i_channels_conf,
249 &p_sys->i_bit_rate );
250 if( !p_sys->i_frame_size )
252 msg_Dbg( p_dec, "emulated sync word" );
253 block_SkipByte( &p_sys->bytestream );
254 p_sys->i_state = STATE_NOSYNC;
257 p_sys->i_state = STATE_NEXT_SYNC;
259 case STATE_NEXT_SYNC:
260 /* TODO: If pp_block == NULL, flush the buffer without checking the
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 )
272 if( p_sys->b_packetizer &&
273 p_header[0] == 0 && p_header[1] == 0 )
275 /* A52 wav files and audio CD's use stuffing */
276 p_sys->i_state = STATE_GET_DATA;
280 if( p_header[0] != 0x0b || p_header[1] != 0x77 )
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 );
288 p_sys->i_state = STATE_SEND_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 )
300 p_sys->i_state = STATE_SEND_DATA;
302 case STATE_SEND_DATA:
303 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
305 //p_dec->b_error = VLC_TRUE;
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 );
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;
317 /* So p_block doesn't get re-added several times */
318 *pp_block = block_BytestreamPop( &p_sys->bytestream );
320 p_sys->i_state = STATE_NOSYNC;
329 /*****************************************************************************
330 * CloseDecoder: clean up the decoder
331 *****************************************************************************/
332 static void CloseDecoder( vlc_object_t *p_this )
334 decoder_t *p_dec = (decoder_t*)p_this;
335 decoder_sys_t *p_sys = p_dec->p_sys;
337 block_BytestreamRelease( &p_sys->bytestream );
342 /*****************************************************************************
344 *****************************************************************************/
345 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
347 decoder_sys_t *p_sys = p_dec->p_sys;
350 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
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 );
355 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
356 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
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;
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;
368 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
370 if( p_sys->b_packetizer )
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;
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;
386 /*****************************************************************************
388 *****************************************************************************/
389 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
391 decoder_sys_t *p_sys = p_dec->p_sys;
392 aout_buffer_t *p_buf;
394 p_buf = p_dec->pf_aout_buffer_new( p_dec, A52_FRAME_NB );
395 if( p_buf == NULL ) return NULL;
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 );
404 /*****************************************************************************
406 *****************************************************************************/
407 static block_t *GetSoutBuffer( decoder_t *p_dec )
409 decoder_sys_t *p_sys = p_dec->p_sys;
412 p_block = block_New( p_dec, p_sys->i_frame_size );
413 if( p_block == NULL ) return NULL;
415 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
418 aout_DateIncrement( &p_sys->end_date,
419 A52_FRAME_NB * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
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
431 *****************************************************************************/
432 static int SyncInfo( const byte_t * p_buf,
433 unsigned int * pi_channels,
434 unsigned int * pi_channels_conf,
435 unsigned int * pi_sample_rate, int * pi_bit_rate )
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,
441 static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
442 0x04, 0x01, 0x04, 0x01 };
448 if ((p_buf[0] != 0x0b) || (p_buf[1] != 0x77)) /* syncword */
451 if (p_buf[5] >= 0x60) /* bsid >= 12 */
453 half = halfrate[p_buf[5] >> 3];
455 /* acmod, dsurmod and lfeon */
456 acmod = p_buf[6] >> 5;
457 if ( (p_buf[6] & 0xf8) == 0x50 )
459 /* Dolby surround = stereo + Dolby */
461 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
462 | AOUT_CHAN_DOLBYSTEREO;
464 else switch ( acmod )
467 /* Dual-mono = stereo + dual-mono */
469 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
470 | AOUT_CHAN_DUALMONO;
475 *pi_channels_conf = AOUT_CHAN_CENTER;
480 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
485 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
491 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
492 | AOUT_CHAN_REARCENTER;
497 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
498 | AOUT_CHAN_REARCENTER;
503 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
504 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
509 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
510 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
516 if ( p_buf[6] & lfeon[acmod] )
519 *pi_channels_conf |= AOUT_CHAN_LFE;
522 frmsizecod = p_buf[4] & 63;
523 if (frmsizecod >= 38)
525 bitrate = rate [frmsizecod >> 1];
526 *pi_bit_rate = (bitrate * 1000) >> half;
528 switch (p_buf[4] & 0xc0) {
530 *pi_sample_rate = 48000 >> half;
533 *pi_sample_rate = 44100 >> half;
534 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
536 *pi_sample_rate = 32000 >> half;