1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: mpeg4audio.c,v 1.7 2003/09/02 20:19:26 gbazin Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc/decoder.h>
30 #include <vlc/input.h>
33 #include <stdlib.h> /* malloc(), free() */
34 #include <string.h> /* strdup() */
36 /*****************************************************************************
38 *****************************************************************************/
42 * AudioObjectType 5 bits
43 * samplingFrequencyIndex 4 bits
44 * if (samplingFrequencyIndex == 0xF)
45 * samplingFrequency 24 bits
46 * channelConfiguration 4 bits
48 * FrameLengthFlag 1 bit 1024 or 960
49 * DependsOnCoreCoder 1 bit (always 0)
50 * ExtensionFlag 1 bit (always 0)
53 typedef struct packetizer_thread_s
55 /* Input properties */
58 decoder_fifo_t *p_fifo;
59 bit_stream_t bit_stream;
61 /* Output properties */
62 sout_packetizer_input_t *p_sout_input;
63 sout_format_t output_format;
65 // mtime_t i_pts_start;
70 /* Extracted from AAC config */
72 int i_frame_size; // 1024 or 960
74 } packetizer_thread_t;
76 static int Open ( vlc_object_t * );
77 static int Run ( decoder_fifo_t * );
79 static int InitThread ( packetizer_thread_t * );
80 static void PacketizeThreadMPEG4 ( packetizer_thread_t * );
81 static void PacketizeThreadADTS ( packetizer_thread_t * );
82 static void EndThread ( packetizer_thread_t * );
84 /*****************************************************************************
86 *****************************************************************************/
89 set_description( _("MPEG4 Audio packetizer") );
90 set_capability( "packetizer", 50 );
91 set_callbacks( Open, NULL );
95 /*****************************************************************************
96 * OpenDecoder: probe the packetizer and return score
97 *****************************************************************************
98 * Tries to launch a decoder and return score so that the interface is able
100 *****************************************************************************/
101 static int Open( vlc_object_t *p_this )
103 decoder_t *p_dec = (decoder_t*)p_this;
107 if( p_dec->p_fifo->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'a') )
109 return( VLC_SUCCESS );
113 return( VLC_EGENERIC );
117 /*****************************************************************************
118 * RunDecoder: this function is called just after the thread is created
119 *****************************************************************************/
120 static int Run( decoder_fifo_t *p_fifo )
122 packetizer_thread_t *p_pack;
125 msg_Info( p_fifo, "Running MPEG4 audio packetizer" );
126 if( !( p_pack = malloc( sizeof( packetizer_thread_t ) ) ) )
128 msg_Err( p_fifo, "out of memory" );
129 DecoderError( p_fifo );
132 memset( p_pack, 0, sizeof( packetizer_thread_t ) );
134 p_pack->p_fifo = p_fifo;
136 if( InitThread( p_pack ) != 0 )
138 DecoderError( p_fifo );
142 while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
146 PacketizeThreadADTS( p_pack );
150 PacketizeThreadMPEG4( p_pack );
155 if( ( b_error = p_pack->p_fifo->b_error ) )
157 DecoderError( p_pack->p_fifo );
170 #define FREE( p ) if( p ) free( p ); p = NULL
172 /*****************************************************************************
173 * InitThread: initialize data before entering main loop
174 *****************************************************************************/
175 static int i_sample_rates[] =
177 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
178 16000, 12000, 11025, 8000, 7350, 0, 0, 0
181 static int InitThread( packetizer_thread_t *p_pack )
185 p_wf = (WAVEFORMATEX*)p_pack->p_fifo->p_waveformatex;
187 if( p_wf && p_wf->cbSize > 0)
189 uint8_t *p_config = (uint8_t*)&p_wf[1];
194 i_index = ( ( p_config[0] << 1 ) | ( p_config[1] >> 7 ) )&0x0f;
195 if( i_index != 0x0f )
197 p_pack->i_sample_rate = i_sample_rates[i_index];
198 p_pack->i_frame_size = ( ( p_config[1] >> 2 )&0x01 ) ? 960 : 1024;
202 p_pack->i_sample_rate = ( ( p_config[1]&0x7f ) << 17 ) | ( p_config[2] << 9 )|
203 ( p_config[3] << 1 ) | ( p_config[4] >> 7 );
204 p_pack->i_frame_size = ( ( p_config[4] >> 2 )&0x01 ) ? 960 : 1024;
206 msg_Dbg( p_pack->p_fifo,
207 "aac %dHz %d samples/frame",
208 p_pack->i_sample_rate,
209 p_pack->i_frame_size );
211 p_pack->output_format.i_cat = AUDIO_ES;
212 p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
213 p_pack->output_format.i_sample_rate = p_pack->i_sample_rate;
214 p_pack->output_format.i_channels = p_wf->nChannels;
215 p_pack->output_format.i_block_align = 0;
216 p_pack->output_format.i_bitrate = 0;
218 p_pack->output_format.i_extra_data = p_wf->cbSize;
219 p_pack->output_format.p_extra_data = malloc( p_wf->cbSize );
220 memcpy( p_pack->output_format.p_extra_data,
226 /* we will try to create a AAC Config from adts */
227 p_pack->output_format.i_cat = UNKNOWN_ES;
228 p_pack->output_format.i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
231 if( InitBitstream( &p_pack->bit_stream, p_pack->p_fifo,
232 NULL, NULL ) != VLC_SUCCESS )
234 msg_Err( p_pack->p_fifo, "cannot initialize bitstream" );
240 p_pack->p_sout_input =
241 sout_InputNew( p_pack->p_fifo,
242 &p_pack->output_format );
244 if( !p_pack->p_sout_input )
246 msg_Err( p_pack->p_fifo,
247 "cannot add a new stream" );
251 p_pack->i_last_pts = 0;
255 /*****************************************************************************
256 * PacketizeThread: packetize an unit (here copy a complete pes)
257 *****************************************************************************/
258 static void PacketizeThreadMPEG4( packetizer_thread_t *p_pack )
260 sout_buffer_t *p_sout_buffer;
265 /* **** get samples count **** */
266 input_ExtractPES( p_pack->p_fifo, &p_pes );
269 p_pack->p_fifo->b_error = 1;
273 if( p_pack->i_pts_start < 0 && p_pes->i_pts > 0 )
275 p_pack->i_pts_start = p_pes->i_pts;
277 p_pack->i_pts = p_pes->i_pts - p_pack->i_pts_start;
280 i_pts = p_pes->i_pts;
282 if( i_pts <= 0 && p_pack->i_last_pts <= 0 )
284 msg_Dbg( p_pack->p_fifo, "need a starting pts" );
285 input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
288 i_size = p_pes->i_pes_size;
292 data_packet_t *p_data;
296 sout_BufferNew( p_pack->p_sout_input->p_sout, i_size );
299 p_pack->p_fifo->b_error = 1;
300 input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
303 /* TODO: memcpy of the pes packet */
304 for( i_buffer = 0, p_data = p_pes->p_first;
305 p_data != NULL && i_buffer < i_size;
306 p_data = p_data->p_next)
310 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
314 p_pack->p_fifo->p_vlc->pf_memcpy( p_sout_buffer->p_buffer + i_buffer,
315 p_data->p_payload_start,
323 i_pts = p_pack->i_last_pts +
325 (mtime_t)p_pack->i_frame_size /
326 (mtime_t)p_pack->i_sample_rate;
328 p_pack->i_last_pts = i_pts;
330 p_sout_buffer->i_length = (mtime_t)1000000 *
331 (mtime_t)p_pack->i_frame_size /
332 (mtime_t)p_pack->i_sample_rate;
333 p_sout_buffer->i_bitrate = 0;
334 p_sout_buffer->i_dts = i_pts;
335 p_sout_buffer->i_pts = i_pts;
337 sout_InputSendBuffer( p_pack->p_sout_input,
341 input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
345 static void PacketizeThreadADTS( packetizer_thread_t *p_pack )
347 msg_Err( p_pack->p_fifo, "adts stream unsupported" );
348 p_pack->p_fifo->b_error = 1;
351 /*****************************************************************************
352 * EndThread : packetizer thread destruction
353 *****************************************************************************/
354 static void EndThread ( packetizer_thread_t *p_pack)
356 if( p_pack->p_sout_input )
358 sout_InputDelete( p_pack->p_sout_input );
362 free( p_pack->p_wf );