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>
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 *****************************************************************************/
75 block_bytestream_t bytestream;
80 audio_date_t end_date;
84 vlc_a52_header_t frame;
97 /****************************************************************************
99 ****************************************************************************/
100 static void *DecodeBlock ( decoder_t *, block_t ** );
102 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
103 static aout_buffer_t *GetAoutBuffer( decoder_t * );
104 static block_t *GetSoutBuffer( decoder_t * );
106 /*****************************************************************************
107 * OpenCommon: probe the decoder/packetizer and return score
108 *****************************************************************************/
109 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
111 decoder_t *p_dec = (decoder_t*)p_this;
112 decoder_sys_t *p_sys;
113 vlc_fourcc_t i_codec;
115 switch( p_dec->fmt_in.i_codec )
117 case VLC_FOURCC('a','5','2',' '):
118 case VLC_FOURCC('a','5','2','b'):
119 i_codec = VLC_FOURCC('a','5','2',' ');
121 case VLC_FOURCC('e','a','c','3'):
122 /* XXX ugly hack, a52 does not support eac3 so no eac3 pass-through
126 i_codec = VLC_FOURCC('e','a','c','3');
132 /* Allocate the memory needed to store the decoder's structure */
133 if( ( p_dec->p_sys = p_sys =
134 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
138 p_sys->b_packetizer = b_packetizer;
139 p_sys->i_state = STATE_NOSYNC;
140 aout_DateSet( &p_sys->end_date, 0 );
142 p_sys->bytestream = block_BytestreamInit();
144 /* Set output properties */
145 p_dec->fmt_out.i_cat = AUDIO_ES;
146 p_dec->fmt_out.i_codec = i_codec;
147 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
148 p_dec->fmt_out.audio.i_bytes_per_frame = 0;
152 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
155 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
160 static int OpenDecoder( vlc_object_t *p_this )
162 return OpenCommon( p_this, false );
165 static int OpenPacketizer( vlc_object_t *p_this )
167 return OpenCommon( p_this, true );
170 /****************************************************************************
171 * DecodeBlock: the whole thing
172 ****************************************************************************
173 * This function is called just after the thread is launched.
174 ****************************************************************************/
175 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
177 decoder_sys_t *p_sys = p_dec->p_sys;
178 uint8_t p_header[VLC_A52_HEADER_SIZE];
182 if( !pp_block || !*pp_block ) return NULL;
184 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
186 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
188 p_sys->i_state = STATE_NOSYNC;
189 block_BytestreamEmpty( &p_sys->bytestream );
191 aout_DateSet( &p_sys->end_date, 0 );
192 block_Release( *pp_block );
196 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
198 /* We've just started the stream, wait for the first PTS. */
199 block_Release( *pp_block );
203 block_BytestreamPush( &p_sys->bytestream, *pp_block );
207 switch( p_sys->i_state )
210 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
213 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
215 p_sys->i_state = STATE_SYNC;
218 block_SkipByte( &p_sys->bytestream );
220 if( p_sys->i_state != STATE_SYNC )
222 block_BytestreamFlush( &p_sys->bytestream );
229 /* New frame, set the Presentation Time Stamp */
230 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
231 if( p_sys->i_pts != 0 &&
232 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
234 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
236 p_sys->i_state = STATE_HEADER;
239 /* Get A/52 frame header (VLC_A52_HEADER_SIZE bytes) */
240 if( block_PeekBytes( &p_sys->bytestream, p_header,
241 VLC_A52_HEADER_SIZE ) != VLC_SUCCESS )
247 /* Check if frame is valid and get frame info */
248 if( vlc_a52_header_Parse( &p_sys->frame, p_header, VLC_A52_HEADER_SIZE ) )
250 msg_Dbg( p_dec, "emulated sync word" );
251 block_SkipByte( &p_sys->bytestream );
252 p_sys->i_state = STATE_NOSYNC;
256 p_sys->i_state = STATE_NEXT_SYNC;
258 case STATE_NEXT_SYNC:
259 /* TODO: If pp_block == NULL, flush the buffer without checking the
262 /* Check if next expected frame contains the sync word */
263 if( block_PeekOffsetBytes( &p_sys->bytestream,
264 p_sys->frame.i_size, p_header, 2 )
271 if( p_sys->b_packetizer &&
272 p_header[0] == 0 && p_header[1] == 0 )
274 /* A52 wav files and audio CD's use stuffing */
275 p_sys->i_state = STATE_GET_DATA;
279 if( p_header[0] != 0x0b || p_header[1] != 0x77 )
281 msg_Dbg( p_dec, "emulated sync word "
282 "(no sync on following frame)" );
283 p_sys->i_state = STATE_NOSYNC;
284 block_SkipByte( &p_sys->bytestream );
287 p_sys->i_state = STATE_SEND_DATA;
291 /* Make sure we have enough data.
292 * (Not useful if we went through NEXT_SYNC) */
293 if( block_WaitBytes( &p_sys->bytestream,
294 p_sys->frame.i_size ) != VLC_SUCCESS )
299 p_sys->i_state = STATE_SEND_DATA;
301 case STATE_SEND_DATA:
302 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
304 //p_dec->b_error = true;
308 /* Copy the whole frame into the buffer. When we reach this point
309 * we already know we have enough data available. */
310 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->frame.i_size );
312 /* Make sure we don't reuse the same pts twice */
313 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
314 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
316 /* So p_block doesn't get re-added several times */
317 *pp_block = block_BytestreamPop( &p_sys->bytestream );
319 p_sys->i_state = STATE_NOSYNC;
328 /*****************************************************************************
329 * CloseCommon: clean up the decoder
330 *****************************************************************************/
331 static void CloseCommon( vlc_object_t *p_this )
333 decoder_t *p_dec = (decoder_t*)p_this;
334 decoder_sys_t *p_sys = p_dec->p_sys;
336 block_BytestreamRelease( &p_sys->bytestream );
341 /*****************************************************************************
343 *****************************************************************************/
344 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
346 decoder_sys_t *p_sys = p_dec->p_sys;
349 if( p_dec->fmt_out.audio.i_rate != p_sys->frame.i_rate )
351 msg_Info( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
352 p_sys->frame.i_channels, p_sys->frame.i_rate, p_sys->frame.i_bitrate );
354 aout_DateInit( &p_sys->end_date, p_sys->frame.i_rate );
355 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
358 p_dec->fmt_out.audio.i_rate = p_sys->frame.i_rate;
359 p_dec->fmt_out.audio.i_channels = p_sys->frame.i_channels;
360 if( p_dec->fmt_out.audio.i_bytes_per_frame < p_sys->frame.i_size )
361 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->frame.i_size;
362 p_dec->fmt_out.audio.i_frame_length = p_sys->frame.i_samples;
364 p_dec->fmt_out.audio.i_original_channels = p_sys->frame.i_channels_conf;
365 p_dec->fmt_out.audio.i_physical_channels =
366 p_sys->frame.i_channels_conf & AOUT_CHAN_PHYSMASK;
368 p_dec->fmt_out.i_bitrate = p_sys->frame.i_bitrate;
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 = decoder_NewAudioBuffer( p_dec, p_sys->frame.i_samples );
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, p_sys->frame.i_samples );
403 /*****************************************************************************
405 *****************************************************************************/
406 static block_t *GetSoutBuffer( decoder_t *p_dec )
408 decoder_sys_t *p_sys = p_dec->p_sys;
411 p_block = block_New( p_dec, p_sys->frame.i_size );
412 if( p_block == NULL ) return NULL;
414 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
417 aout_DateIncrement( &p_sys->end_date, p_sys->frame.i_samples ) - p_block->i_pts;