1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: mpegaudio.c,v 1.6 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;
51 uint32_t i_samplespersecond;
56 static int Open ( vlc_object_t * );
57 static int Run ( decoder_fifo_t * );
59 static int InitThread ( packetizer_t * );
60 static void PacketizeThread ( packetizer_t * );
61 static void EndThread ( packetizer_t * );
63 #define FREE( p ) if( p ) free( p ); p = NULL
65 /*****************************************************************************
67 *****************************************************************************/
70 set_description( _("MPEG-I/II audio packetizer") );
71 set_capability( "packetizer", 50 );
72 set_callbacks( Open, NULL );
76 static int mpegaudio_bitrate[2][3][16] =
80 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
82 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
84 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}
89 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
91 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
93 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}
98 static int mpegaudio_samplerate[2][4] = /* version 1 then 2 */
100 { 44100, 48000, 32000, 0 },
101 { 22050, 24000, 16000, 0 }
105 /*****************************************************************************
106 * OpenDecoder: probe the packetizer and return score
107 *****************************************************************************
108 * Tries to launch a decoder and return score so that the interface is able
110 *****************************************************************************/
111 static int Open( vlc_object_t *p_this )
113 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
115 if( p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', 'a') )
120 p_fifo->pf_run = Run;
124 /*****************************************************************************
125 * RunDecoder: this function is called just after the thread is created
126 *****************************************************************************/
127 static int Run( decoder_fifo_t *p_fifo )
129 packetizer_t *p_pack;
132 msg_Info( p_fifo, "Running mpegaudio packetizer" );
133 if( !( p_pack = malloc( sizeof( packetizer_t ) ) ) )
135 msg_Err( p_fifo, "out of memory" );
136 DecoderError( p_fifo );
139 memset( p_pack, 0, sizeof( packetizer_t ) );
141 p_pack->p_fifo = p_fifo;
143 if( InitThread( p_pack ) != 0 )
145 DecoderError( p_fifo );
149 while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
151 PacketizeThread( p_pack );
155 if( ( b_error = p_pack->p_fifo->b_error ) )
157 DecoderError( p_pack->p_fifo );
174 /*****************************************************************************
175 * InitThread: initialize data before entering main loop
176 *****************************************************************************/
178 static int InitThread( packetizer_t *p_pack )
181 p_pack->output_format.i_cat = AUDIO_ES;
182 p_pack->output_format.i_fourcc = p_pack->p_fifo->i_fourcc;
183 p_pack->output_format.i_sample_rate = 0;
184 p_pack->output_format.i_channels = 0;
185 p_pack->output_format.i_block_align = 0;
186 p_pack->output_format.i_bitrate = 0;
187 p_pack->output_format.i_extra_data = 0;
188 p_pack->output_format.p_extra_data = NULL;
191 p_pack->p_sout_input = NULL;
193 p_pack->i_samplescount = 0;
194 p_pack->i_samplespersecond = 0;
195 p_pack->i_last_pts = 0;
197 if( InitBitstream( &p_pack->bit_stream, p_pack->p_fifo,
198 NULL, NULL ) != VLC_SUCCESS )
200 msg_Err( p_pack->p_fifo, "cannot initialize bitstream" );
207 /*****************************************************************************
208 * PacketizeThread: packetize an unit (here copy a complete pes)
209 *****************************************************************************/
210 static void PacketizeThread( packetizer_t *p_pack )
212 sout_buffer_t *p_sout_buffer;
216 uint32_t i_sync, i_header;
217 int i_version, i_layer;
218 int i_channels, i_samplerate, i_bitrate;
219 int i_samplesperframe, i_framelength;
221 /* search a valid start code */
224 int i_crc, i_bitrate_index, i_samplerate_index;
225 int i_padding, i_extention, i_mode, i_modeext, i_copyright;
226 int i_original, i_emphasis;
228 RealignBits( &p_pack->bit_stream );
231 while( ShowBits( &p_pack->bit_stream, 12 ) != 0x0fff &&
232 !p_pack->p_fifo->b_die && !p_pack->p_fifo->b_error )
234 //msg_Warn( p_pack->p_fifo, "trash..." );
235 RemoveBits( &p_pack->bit_stream, 8 );
239 if( p_pack->p_fifo->b_die || p_pack->p_fifo->b_error )
244 /* Set the Presentation Time Stamp */
245 NextPTS( &p_pack->bit_stream, &i_pts, NULL );
247 i_sync = GetBits( &p_pack->bit_stream, 12 );
248 i_header = ShowBits( &p_pack->bit_stream, 20 );
250 i_version = 1 - GetBits( &p_pack->bit_stream, 1 );
251 i_layer = 3 - GetBits( &p_pack->bit_stream, 2 );
252 i_crc = 1 - GetBits( &p_pack->bit_stream, 1 );
253 i_bitrate_index = GetBits( &p_pack->bit_stream, 4 );
254 i_samplerate_index = GetBits( &p_pack->bit_stream, 2 );
255 i_padding = GetBits( &p_pack->bit_stream, 1 );
256 i_extention = GetBits( &p_pack->bit_stream, 1 );
257 i_mode = GetBits( &p_pack->bit_stream, 2 );
258 i_modeext = GetBits( &p_pack->bit_stream, 2 );
259 i_copyright = GetBits( &p_pack->bit_stream, 1 );
260 i_original = GetBits( &p_pack->bit_stream, 1 );
261 i_emphasis = GetBits( &p_pack->bit_stream, 2 );
264 i_bitrate_index > 0x00 && i_bitrate_index < 0x0f &&
265 i_samplerate_index != 0x03 &&
268 i_channels = ( i_mode == 3 ) ? 1 : 2;
269 i_bitrate = mpegaudio_bitrate[i_version][i_layer][i_bitrate_index];
270 i_samplerate = mpegaudio_samplerate[i_version][i_samplerate_index];
274 i_framelength = ( 12000 * i_bitrate /
275 i_samplerate + i_padding ) * 4;
278 i_framelength = 144000 * i_bitrate /
279 i_samplerate + i_padding;
282 i_framelength = ( i_version ? 72000 : 144000 ) *
283 i_bitrate / i_samplerate + i_padding;
291 i_samplesperframe = 384;
294 i_samplesperframe = 1152;
297 i_samplesperframe = i_version ? 576 : 1152;
300 i_samplesperframe = 0;
306 if( !p_pack->p_sout_input )
308 /* add a input for the stream ouput */
309 p_pack->output_format.i_sample_rate = i_samplerate;
310 p_pack->output_format.i_channels = i_channels;
311 p_pack->output_format.i_block_align = 1;
312 p_pack->output_format.i_bitrate = 0;
314 p_pack->p_sout_input =
315 sout_InputNew( p_pack->p_fifo,
316 &p_pack->output_format );
318 if( !p_pack->p_sout_input )
320 msg_Err( p_pack->p_fifo,
321 "cannot add a new stream" );
322 p_pack->p_fifo->b_error = 1;
325 msg_Dbg( p_pack->p_fifo,
326 "v:%d l:%d channels:%d samplerate:%d bitrate:%d size:%d",
327 i_version, i_layer, i_channels, i_samplerate,
328 i_bitrate, i_framelength );
331 if( i_pts <= 0 && p_pack->i_last_pts <= 0 )
333 msg_Dbg( p_pack->p_fifo, "need a starting pts" );
338 i_pts = p_pack->i_last_pts +
340 (uint64_t)i_samplesperframe /
341 (uint64_t)i_samplerate;
343 p_pack->i_last_pts = i_pts;
345 i_size = __MAX( i_framelength, 4 );
346 // msg_Dbg( p_pack->p_fifo, "frame length %d b", i_size );
348 sout_BufferNew( p_pack->p_sout_input->p_sout, i_size );
351 p_pack->p_fifo->b_error = 1;
354 p_sout_buffer->p_buffer[0] = ( i_sync >> 4 )&0xff;
355 p_sout_buffer->p_buffer[1] =
356 ( ( i_sync << 4 )&0xf0 ) | ( ( i_header >> 16 )&0x0f );
357 p_sout_buffer->p_buffer[2] = ( i_header >> 8 )&0xff;
358 p_sout_buffer->p_buffer[3] = ( i_header )&0xff;
359 p_sout_buffer->i_bitrate = i_bitrate;
361 p_sout_buffer->i_dts = i_pts;
362 p_sout_buffer->i_pts = i_pts;
364 p_sout_buffer->i_length =
366 (uint64_t)i_samplesperframe /
367 (uint64_t)i_samplerate;
369 p_pack->i_samplescount += i_samplesperframe;
371 /* we are already aligned */
372 GetChunk( &p_pack->bit_stream,
373 p_sout_buffer->p_buffer + 4,
376 sout_InputSendBuffer( p_pack->p_sout_input,
381 /*****************************************************************************
382 * EndThread : packetizer thread destruction
383 *****************************************************************************/
384 static void EndThread ( packetizer_t *p_pack)
386 if( p_pack->p_sout_input )
388 sout_InputDelete( p_pack->p_sout_input );