1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: a52.c,v 1.4 2003/04/13 20:00:21 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <vlc/decoder.h>
31 #include <vlc/input.h>
34 #include <stdlib.h> /* malloc(), free() */
35 #include <string.h> /* strdup() */
36 #include "codecs.h" /* WAVEFORMATEX BITMAPINFOHEADER */
37 /*****************************************************************************
39 *****************************************************************************/
40 typedef struct packetizer_s
42 /* Input properties */
43 decoder_fifo_t *p_fifo;
44 bit_stream_t bit_stream;
46 /* Output properties */
47 sout_packetizer_input_t *p_sout_input;
48 sout_format_t output_format;
50 uint64_t i_samplescount;
55 static int Open ( vlc_object_t * );
56 static int Run ( decoder_fifo_t * );
58 static int InitThread ( packetizer_t * );
59 static void PacketizeThread ( packetizer_t * );
60 static void EndThread ( packetizer_t * );
62 static int SyncInfo( const byte_t *, int *, int *, int * );
64 #define FREE( p ) if( p ) free( p ); p = NULL
66 /*****************************************************************************
68 *****************************************************************************/
71 set_description( _("A/52 audio packetizer") );
72 set_capability( "packetizer", 10 );
73 set_callbacks( Open, NULL );
78 /*****************************************************************************
79 * OpenDecoder: probe the packetizer and return score
80 *****************************************************************************
81 * Tries to launch a decoder and return score so that the interface is able
83 *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
86 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
88 if( p_fifo->i_fourcc != VLC_FOURCC( 'a', '5', '2', ' ') &&
89 p_fifo->i_fourcc != VLC_FOURCC( 'a', '5', '2', 'b') )
98 /*****************************************************************************
99 * RunDecoder: this function is called just after the thread is created
100 *****************************************************************************/
101 static int Run( decoder_fifo_t *p_fifo )
103 packetizer_t *p_pack;
106 msg_Info( p_fifo, "Running A/52 packetizer" );
107 if( !( p_pack = malloc( sizeof( packetizer_t ) ) ) )
109 msg_Err( p_fifo, "out of memory" );
110 DecoderError( p_fifo );
113 memset( p_pack, 0, sizeof( packetizer_t ) );
115 p_pack->p_fifo = p_fifo;
117 if( InitThread( p_pack ) != 0 )
119 DecoderError( p_fifo );
123 while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
125 PacketizeThread( p_pack );
129 if( ( b_error = p_pack->p_fifo->b_error ) )
131 DecoderError( p_pack->p_fifo );
148 /*****************************************************************************
149 * InitThread: initialize data before entering main loop
150 *****************************************************************************/
152 static int InitThread( packetizer_t *p_pack )
155 p_pack->output_format.i_cat = AUDIO_ES;
156 p_pack->output_format.i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
158 p_pack->p_sout_input = NULL;
160 p_pack->i_samplescount = 0;
161 p_pack->i_last_pts = 0;
163 if( InitBitstream( &p_pack->bit_stream, p_pack->p_fifo,
164 NULL, NULL ) != VLC_SUCCESS )
166 msg_Err( p_pack->p_fifo, "cannot initialize bitstream" );
173 /*****************************************************************************
174 * PacketizeThread: packetize an unit (here copy a complete pes)
175 *****************************************************************************/
176 static void PacketizeThread( packetizer_t *p_pack )
178 sout_buffer_t *p_sout_buffer;
182 int i_channels, i_samplerate, i_bitrate;
186 /* search a valid start code */
191 RealignBits( &p_pack->bit_stream );
192 while( ShowBits( &p_pack->bit_stream, 16 ) != 0x0b77 &&
193 !p_pack->p_fifo->b_die && !p_pack->p_fifo->b_error )
195 RemoveBits( &p_pack->bit_stream, 8 );
200 msg_Warn( p_pack->p_fifo, "trashing %d bytes", i_skip );
202 if( p_pack->p_fifo->b_die || p_pack->p_fifo->b_error )
207 /* Set the Presentation Time Stamp */
208 NextPTS( &p_pack->bit_stream, &i_pts, NULL );
210 GetChunk( &p_pack->bit_stream, p_header, 7 );
211 if( p_pack->p_fifo->b_die ) return;
213 /* Check if frame is valid and get frame info */
214 i_framelength = SyncInfo( p_header, &i_channels, &i_samplerate,
219 msg_Warn( p_pack->p_fifo, "invalid header found" );
224 // msg_Dbg( p_pack->p_fifo, "frame length %d b", i_framelength );
229 if( !p_pack->p_sout_input )
231 p_pack->output_format.i_sample_rate = i_samplerate;
232 p_pack->output_format.i_channels = i_channels;
233 p_pack->output_format.i_block_align = 0;
234 p_pack->output_format.i_bitrate = 0;
235 p_pack->output_format.i_extra_data = 0;
236 p_pack->output_format.p_extra_data = NULL;
238 p_pack->p_sout_input =
239 sout_InputNew( p_pack->p_fifo,
240 &p_pack->output_format );
242 if( !p_pack->p_sout_input )
244 msg_Err( p_pack->p_fifo,
245 "cannot add a new stream" );
246 p_pack->p_fifo->b_error = 1;
249 msg_Info( p_pack->p_fifo,
250 "A/52 channels:%d samplerate:%d bitrate:%d",
251 i_channels, i_samplerate, i_bitrate );
254 if( i_pts <= 0 && p_pack->i_last_pts <= 0 )
256 msg_Dbg( p_pack->p_fifo, "need a starting pts" );
263 i_pts = p_pack->i_last_pts +
265 (uint64_t)A52_FRAME_NB /
266 (uint64_t)i_samplerate;
268 p_pack->i_last_pts = i_pts;
271 sout_BufferNew( p_pack->p_sout_input->p_sout, i_framelength );
274 p_pack->p_fifo->b_error = 1;
277 memcpy( p_sout_buffer->p_buffer, p_header, 7 );
278 p_sout_buffer->i_bitrate = i_bitrate;
280 p_sout_buffer->i_pts =
281 p_sout_buffer->i_dts = i_pts;
283 p_sout_buffer->i_length =
285 (uint64_t)A52_FRAME_NB /
286 (uint64_t)i_samplerate;
288 p_pack->i_samplescount += A52_FRAME_NB;
290 /* we are already aligned */
291 GetChunk( &p_pack->bit_stream,
292 p_sout_buffer->p_buffer + 7,
295 sout_InputSendBuffer( p_pack->p_sout_input,
300 /*****************************************************************************
301 * EndThread : packetizer thread destruction
302 *****************************************************************************/
303 static void EndThread ( packetizer_t *p_pack)
305 if( p_pack->p_sout_input )
307 sout_InputDelete( p_pack->p_sout_input );
311 /*****************************************************************************
312 * SyncInfo: parse A52 sync info
313 *****************************************************************************
314 * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
315 * since we don't want to oblige S/PDIF people to use liba52 just to get
317 * fenrir: I've change it to report channels count
318 *****************************************************************************/
319 static int SyncInfo( const byte_t * p_buf, int * pi_channels,
320 int * pi_sample_rate, int * pi_bit_rate )
322 static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
323 static const int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
324 128, 160, 192, 224, 256, 320, 384, 448,
326 static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
327 0x04, 0x01, 0x04, 0x01 };
333 if ((p_buf[0] != 0x0b) || (p_buf[1] != 0x77)) /* syncword */
336 if (p_buf[5] >= 0x60) /* bsid >= 12 */
338 half = halfrate[p_buf[5] >> 3];
340 /* acmod, dsurmod and lfeon */
341 acmod = p_buf[6] >> 5;
342 if ( (p_buf[6] & 0xf8) == 0x50 )
344 /* Dolby surround = stereo + Dolby */
345 /* *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
346 | AOUT_CHAN_DOLBYSTEREO;*/
347 *pi_channels = 2; /* FIXME ??? */
349 else switch ( acmod )
352 /* Dual-mono = stereo + dual-mono */
353 // *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
354 // | AOUT_CHAN_DUALMONO;
355 *pi_channels = 2; /* FIXME ??? */
381 if ( p_buf[6] & lfeon[acmod] ) *pi_channels += 1;//|= AOUT_CHAN_LFEA;
383 frmsizecod = p_buf[4] & 63;
384 if (frmsizecod >= 38)
386 bitrate = rate [frmsizecod >> 1];
387 *pi_bit_rate = (bitrate * 1000) >> half;
389 switch (p_buf[4] & 0xc0) {
391 *pi_sample_rate = 48000 >> half;
394 *pi_sample_rate = 44100 >> half;
395 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
397 *pi_sample_rate = 32000 >> half;