1 /*****************************************************************************
2 * a52.c: parse A/52 audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2001-2002 VideoLAN
5 * $Id: a52.c,v 1.30 2003/11/16 22:54:12 gbazin Exp $
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <string.h> /* memcpy() */
34 #include <vlc/decoder.h>
35 #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;
77 /****************************************************************************
79 ****************************************************************************/
80 static int OpenDecoder ( vlc_object_t * );
81 static int OpenPacketizer( vlc_object_t * );
82 static void CloseDecoder ( vlc_object_t * );
83 static void *DecodeBlock ( decoder_t *, block_t ** );
85 static int SyncInfo ( const byte_t *, int *, int *, int *,int * );
87 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
88 static aout_buffer_t *GetAoutBuffer( decoder_t * );
89 static block_t *GetSoutBuffer( decoder_t * );
91 /*****************************************************************************
93 *****************************************************************************/
95 set_description( _("A/52 parser") );
96 set_capability( "decoder", 100 );
97 set_callbacks( OpenDecoder, CloseDecoder );
100 set_description( _("A/52 audio packetizer") );
101 set_capability( "packetizer", 10 );
102 set_callbacks( OpenPacketizer, CloseDecoder );
105 /*****************************************************************************
106 * OpenDecoder: probe the decoder and return score
107 *****************************************************************************/
108 static int OpenDecoder( vlc_object_t *p_this )
110 decoder_t *p_dec = (decoder_t*)p_this;
111 decoder_sys_t *p_sys;
113 if( p_dec->fmt_in.i_codec != VLC_FOURCC('a','5','2',' ')
114 && p_dec->fmt_in.i_codec != VLC_FOURCC('a','5','2','b') )
119 /* Allocate the memory needed to store the decoder's structure */
120 if( ( p_dec->p_sys = p_sys =
121 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
123 msg_Err( p_dec, "out of memory" );
128 p_sys->b_packetizer = VLC_FALSE;
129 p_sys->i_state = STATE_NOSYNC;
130 aout_DateSet( &p_sys->end_date, 0 );
132 p_sys->bytestream = block_BytestreamInit( p_dec );
134 /* Set output properties */
135 p_dec->fmt_out.i_cat = AUDIO_ES;
136 p_dec->fmt_out.i_codec = VLC_FOURCC('a','5','2',' ');
139 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
141 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
147 static int OpenPacketizer( vlc_object_t *p_this )
149 decoder_t *p_dec = (decoder_t*)p_this;
151 int i_ret = OpenDecoder( p_this );
153 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
158 /****************************************************************************
159 * DecodeBlock: the whole thing
160 ****************************************************************************
161 * This function is called just after the thread is launched.
162 ****************************************************************************/
163 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
165 decoder_sys_t *p_sys = p_dec->p_sys;
166 uint8_t p_header[A52_HEADER_SIZE];
170 if( !pp_block || !*pp_block ) return NULL;
172 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
174 /* We've just started the stream, wait for the first PTS. */
175 block_Release( *pp_block );
179 if( (*pp_block)->b_discontinuity )
181 p_sys->i_state = STATE_NOSYNC;
184 block_BytestreamPush( &p_sys->bytestream, *pp_block );
188 switch( p_sys->i_state )
191 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
194 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
196 p_sys->i_state = STATE_SYNC;
199 block_SkipByte( &p_sys->bytestream );
201 if( p_sys->i_state != STATE_SYNC )
203 block_BytestreamFlush( &p_sys->bytestream );
210 /* New frame, set the Presentation Time Stamp */
211 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
212 if( p_sys->i_pts != 0 &&
213 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
215 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
217 p_sys->i_state = STATE_HEADER;
220 /* Get A/52 frame header (A52_HEADER_SIZE bytes) */
221 if( block_PeekBytes( &p_sys->bytestream, p_header,
222 A52_HEADER_SIZE ) != VLC_SUCCESS )
228 /* Check if frame is valid and get frame info */
229 p_sys->i_frame_size = SyncInfo( p_header,
231 &p_sys->i_channels_conf,
233 &p_sys->i_bit_rate );
234 if( !p_sys->i_frame_size )
236 msg_Dbg( p_dec, "emulated sync word" );
237 block_SkipByte( &p_sys->bytestream );
238 p_sys->i_state = STATE_NOSYNC;
241 p_sys->i_state = STATE_NEXT_SYNC;
243 case STATE_NEXT_SYNC:
244 /* TODO: If pp_block == NULL, flush the buffer without checking the
247 /* Check if next expected frame contains the sync word */
248 if( block_PeekOffsetBytes( &p_sys->bytestream,
249 p_sys->i_frame_size, p_header, 2 )
256 if( p_header[0] != 0x0b || p_header[1] != 0x77 )
258 msg_Dbg( p_dec, "emulated sync word "
259 "(no sync on following frame)" );
260 p_sys->i_state = STATE_NOSYNC;
261 block_SkipByte( &p_sys->bytestream );
264 p_sys->i_state = STATE_SEND_DATA;
268 /* Make sure we have enough data.
269 * (Not useful if we went through NEXT_SYNC) */
270 if( block_WaitBytes( &p_sys->bytestream,
271 p_sys->i_frame_size ) != VLC_SUCCESS )
276 p_sys->i_state = STATE_SEND_DATA;
278 case STATE_SEND_DATA:
279 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
281 //p_dec->b_error = VLC_TRUE;
285 /* Copy the whole frame into the buffer. When we reach this point
286 * we already know we have enough data available. */
287 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
289 /* Make sure we don't reuse the same pts twice */
290 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
291 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
293 /* So p_block doesn't get re-added several times */
294 *pp_block = block_BytestreamPop( &p_sys->bytestream );
296 p_sys->i_state = STATE_NOSYNC;
305 /*****************************************************************************
306 * CloseDecoder: clean up the decoder
307 *****************************************************************************/
308 static void CloseDecoder( vlc_object_t *p_this )
310 decoder_t *p_dec = (decoder_t*)p_this;
311 decoder_sys_t *p_sys = p_dec->p_sys;
313 block_BytestreamRelease( &p_sys->bytestream );
318 /*****************************************************************************
320 *****************************************************************************/
321 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
323 decoder_sys_t *p_sys = p_dec->p_sys;
326 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
328 msg_Info( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
329 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
331 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
332 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
335 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
336 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
337 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
338 p_dec->fmt_out.audio.i_frame_length = A52_FRAME_NB;
340 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
341 p_dec->fmt_out.audio.i_physical_channels =
342 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
344 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
346 if( p_sys->b_packetizer )
348 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
349 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
350 *pp_out_buffer = p_sout_buffer;
354 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
355 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
356 *pp_out_buffer = p_aout_buffer;
362 /*****************************************************************************
364 *****************************************************************************/
365 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
367 decoder_sys_t *p_sys = p_dec->p_sys;
368 aout_buffer_t *p_buf;
370 p_buf = p_dec->pf_aout_buffer_new( p_dec, A52_FRAME_NB );
371 if( p_buf == NULL ) return NULL;
373 p_buf->start_date = aout_DateGet( &p_sys->end_date );
374 p_buf->end_date = aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB );
379 /*****************************************************************************
381 *****************************************************************************/
382 static block_t *GetSoutBuffer( decoder_t *p_dec )
384 decoder_sys_t *p_sys = p_dec->p_sys;
387 p_block = block_New( p_dec, p_sys->i_frame_size );
388 if( p_block == NULL ) return NULL;
390 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
392 p_block->i_length = aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB ) -
398 /*****************************************************************************
399 * SyncInfo: parse A/52 sync info
400 *****************************************************************************
401 * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
402 * since we don't want to oblige S/PDIF people to use liba52 just to get
404 *****************************************************************************/
405 static int SyncInfo( const byte_t * p_buf,
406 int * pi_channels, int * pi_channels_conf,
407 int * pi_sample_rate, int * pi_bit_rate )
409 static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
410 static const int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
411 128, 160, 192, 224, 256, 320, 384, 448,
413 static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
414 0x04, 0x01, 0x04, 0x01 };
420 if ((p_buf[0] != 0x0b) || (p_buf[1] != 0x77)) /* syncword */
423 if (p_buf[5] >= 0x60) /* bsid >= 12 */
425 half = halfrate[p_buf[5] >> 3];
427 /* acmod, dsurmod and lfeon */
428 acmod = p_buf[6] >> 5;
429 if ( (p_buf[6] & 0xf8) == 0x50 )
431 /* Dolby surround = stereo + Dolby */
433 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
434 | AOUT_CHAN_DOLBYSTEREO;
436 else switch ( acmod )
439 /* Dual-mono = stereo + dual-mono */
441 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
442 | AOUT_CHAN_DUALMONO;
447 *pi_channels_conf = AOUT_CHAN_CENTER;
452 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
457 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
463 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
464 | AOUT_CHAN_REARCENTER;
469 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
470 | AOUT_CHAN_REARCENTER;
475 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
476 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
481 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
482 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
488 if ( p_buf[6] & lfeon[acmod] )
491 *pi_channels_conf |= AOUT_CHAN_LFE;
494 frmsizecod = p_buf[4] & 63;
495 if (frmsizecod >= 38)
497 bitrate = rate [frmsizecod >> 1];
498 *pi_bit_rate = (bitrate * 1000) >> half;
500 switch (p_buf[4] & 0xc0) {
502 *pi_sample_rate = 48000 >> half;
505 *pi_sample_rate = 44100 >> half;
506 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
508 *pi_sample_rate = 32000 >> half;