1 /*****************************************************************************
2 * a52.c: parse A/52 audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2001-2002 VLC authors and VideoLAN
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 it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * 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>
36 #include <vlc_block_helper.h>
37 #include <vlc_modules.h>
41 #include "../packetizer/packetizer_helper.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int OpenDecoder ( vlc_object_t * );
47 static int OpenPacketizer( vlc_object_t * );
48 static void CloseCommon ( vlc_object_t * );
51 set_description( N_("A/52 parser") )
52 set_capability( "decoder", 100 )
53 set_callbacks( OpenDecoder, CloseCommon )
54 set_category( CAT_INPUT )
55 set_subcategory( SUBCAT_INPUT_ACODEC )
58 set_description( N_("A/52 audio packetizer") )
59 set_capability( "packetizer", 10 )
60 set_callbacks( OpenPacketizer, CloseCommon )
63 /*****************************************************************************
64 * decoder_sys_t : decoder descriptor
65 *****************************************************************************/
77 block_bytestream_t bytestream;
86 vlc_a52_header_t frame;
89 /****************************************************************************
91 ****************************************************************************/
92 static block_t *DecodeBlock ( decoder_t *, block_t ** );
94 static uint8_t *GetOutBuffer ( decoder_t *, block_t ** );
95 static block_t *GetAoutBuffer( decoder_t * );
96 static block_t *GetSoutBuffer( decoder_t * );
98 /*****************************************************************************
99 * OpenCommon: probe the decoder/packetizer and return score
100 *****************************************************************************/
101 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
103 decoder_t *p_dec = (decoder_t*)p_this;
104 decoder_sys_t *p_sys;
105 vlc_fourcc_t i_codec;
107 switch( p_dec->fmt_in.i_codec )
110 i_codec = VLC_CODEC_A52;
113 /* XXX ugly hack, a52 does not support eac3 so no eac3 pass-through
117 i_codec = VLC_CODEC_EAC3;
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 )
129 p_sys->b_packetizer = b_packetizer;
130 p_sys->i_state = STATE_NOSYNC;
131 date_Set( &p_sys->end_date, 0 );
132 p_sys->i_pts = VLC_TS_INVALID;
134 block_BytestreamInit( &p_sys->bytestream );
136 /* Set output properties */
137 p_dec->fmt_out.i_cat = AUDIO_ES;
138 p_dec->fmt_out.i_codec = i_codec;
139 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
140 p_dec->fmt_out.audio.i_bytes_per_frame = 0;
144 p_dec->pf_packetize = DecodeBlock;
146 p_dec->pf_decode_audio = DecodeBlock;
150 static int OpenDecoder( vlc_object_t *p_this )
152 /* HACK: Don't use this codec if we don't have an a52 audio filter */
153 if( !module_exists( "a52tofloat32" ) )
155 return OpenCommon( p_this, false );
158 static int OpenPacketizer( vlc_object_t *p_this )
160 return OpenCommon( p_this, true );
163 /****************************************************************************
164 * DecodeBlock: the whole thing
165 ****************************************************************************
166 * This function is called just after the thread is launched.
167 ****************************************************************************/
168 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
170 decoder_sys_t *p_sys = p_dec->p_sys;
171 uint8_t p_header[VLC_A52_HEADER_SIZE];
173 block_t *p_out_buffer;
175 if( !pp_block || !*pp_block ) return NULL;
177 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
179 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
181 p_sys->i_state = STATE_NOSYNC;
182 block_BytestreamEmpty( &p_sys->bytestream );
184 date_Set( &p_sys->end_date, 0 );
185 block_Release( *pp_block );
189 if( !date_Get( &p_sys->end_date ) && (*pp_block)->i_pts <= VLC_TS_INVALID)
191 /* We've just started the stream, wait for the first PTS. */
192 block_Release( *pp_block );
196 block_BytestreamPush( &p_sys->bytestream, *pp_block );
200 switch( p_sys->i_state )
203 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
206 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
208 p_sys->i_state = STATE_SYNC;
211 block_SkipByte( &p_sys->bytestream );
213 if( p_sys->i_state != STATE_SYNC )
215 block_BytestreamFlush( &p_sys->bytestream );
222 /* New frame, set the Presentation Time Stamp */
223 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
224 if( p_sys->i_pts > VLC_TS_INVALID &&
225 p_sys->i_pts != date_Get( &p_sys->end_date ) )
227 date_Set( &p_sys->end_date, p_sys->i_pts );
229 p_sys->i_state = STATE_HEADER;
232 /* Get A/52 frame header (VLC_A52_HEADER_SIZE bytes) */
233 if( block_PeekBytes( &p_sys->bytestream, p_header,
234 VLC_A52_HEADER_SIZE ) != VLC_SUCCESS )
240 /* Check if frame is valid and get frame info */
241 if( vlc_a52_header_Parse( &p_sys->frame, p_header, VLC_A52_HEADER_SIZE ) )
243 msg_Dbg( p_dec, "emulated sync word" );
244 block_SkipByte( &p_sys->bytestream );
245 p_sys->i_state = STATE_NOSYNC;
249 p_sys->i_state = STATE_NEXT_SYNC;
251 case STATE_NEXT_SYNC:
252 /* TODO: If pp_block == NULL, flush the buffer without checking the
255 /* Check if next expected frame contains the sync word */
256 if( block_PeekOffsetBytes( &p_sys->bytestream,
257 p_sys->frame.i_size, p_header, 2 )
264 if( p_sys->b_packetizer &&
265 p_header[0] == 0 && p_header[1] == 0 )
267 /* A52 wav files and audio CD's use stuffing */
268 p_sys->i_state = STATE_GET_DATA;
272 if( p_header[0] != 0x0b || p_header[1] != 0x77 )
274 msg_Dbg( p_dec, "emulated sync word "
275 "(no sync on following frame)" );
276 p_sys->i_state = STATE_NOSYNC;
277 block_SkipByte( &p_sys->bytestream );
280 p_sys->i_state = STATE_SEND_DATA;
284 /* Make sure we have enough data.
285 * (Not useful if we went through NEXT_SYNC) */
286 if( block_WaitBytes( &p_sys->bytestream,
287 p_sys->frame.i_size ) != VLC_SUCCESS )
292 p_sys->i_state = STATE_SEND_DATA;
294 case STATE_SEND_DATA:
295 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
297 //p_dec->b_error = true;
301 /* Copy the whole frame into the buffer. When we reach this point
302 * we already know we have enough data available. */
303 block_GetBytes( &p_sys->bytestream,
304 p_buf, __MIN( p_sys->frame.i_size, p_out_buffer->i_buffer ) );
306 /* Make sure we don't reuse the same pts twice */
307 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
308 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
310 /* So p_block doesn't get re-added several times */
311 *pp_block = block_BytestreamPop( &p_sys->bytestream );
313 p_sys->i_state = STATE_NOSYNC;
320 /*****************************************************************************
321 * CloseCommon: clean up the decoder
322 *****************************************************************************/
323 static void CloseCommon( vlc_object_t *p_this )
325 decoder_t *p_dec = (decoder_t*)p_this;
326 decoder_sys_t *p_sys = p_dec->p_sys;
328 block_BytestreamRelease( &p_sys->bytestream );
333 /*****************************************************************************
335 *****************************************************************************/
336 static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
338 decoder_sys_t *p_sys = p_dec->p_sys;
341 if( p_dec->fmt_out.audio.i_rate != p_sys->frame.i_rate )
343 msg_Dbg( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
344 p_sys->frame.i_channels, p_sys->frame.i_rate, p_sys->frame.i_bitrate );
346 date_Init( &p_sys->end_date, p_sys->frame.i_rate, 1 );
347 date_Set( &p_sys->end_date, p_sys->i_pts );
350 p_dec->fmt_out.audio.i_rate = p_sys->frame.i_rate;
351 p_dec->fmt_out.audio.i_channels = p_sys->frame.i_channels;
352 if( p_dec->fmt_out.audio.i_bytes_per_frame < p_sys->frame.i_size )
353 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->frame.i_size;
354 p_dec->fmt_out.audio.i_frame_length = p_sys->frame.i_samples;
356 p_dec->fmt_out.audio.i_original_channels = p_sys->frame.i_channels_conf;
357 p_dec->fmt_out.audio.i_physical_channels =
358 p_sys->frame.i_channels_conf & AOUT_CHAN_PHYSMASK;
360 p_dec->fmt_out.i_bitrate = p_sys->frame.i_bitrate;
362 if( p_sys->b_packetizer )
364 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
365 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
366 *pp_out_buffer = p_sout_buffer;
370 block_t *p_aout_buffer = GetAoutBuffer( p_dec );
371 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
372 *pp_out_buffer = p_aout_buffer;
378 /*****************************************************************************
380 *****************************************************************************/
381 static block_t *GetAoutBuffer( decoder_t *p_dec )
383 decoder_sys_t *p_sys = p_dec->p_sys;
385 block_t *p_buf = decoder_NewAudioBuffer( p_dec, p_sys->frame.i_samples );
388 p_buf->i_pts = date_Get( &p_sys->end_date );
389 p_buf->i_length = date_Increment( &p_sys->end_date,
390 p_sys->frame.i_samples ) - p_buf->i_pts;
396 /*****************************************************************************
398 *****************************************************************************/
399 static block_t *GetSoutBuffer( decoder_t *p_dec )
401 decoder_sys_t *p_sys = p_dec->p_sys;
403 block_t *p_block = block_Alloc( p_sys->frame.i_size );
406 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
408 date_Increment( &p_sys->end_date, p_sys->frame.i_samples ) - p_block->i_pts;