1 /*****************************************************************************
2 * decoder.c: AAC decoder using libfaad2
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: decoder.c,v 1.22 2003/03/30 18:14:36 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>
32 #include <stdlib.h> /* malloc(), free() */
33 #include <string.h> /* strdup() */
40 /*****************************************************************************
42 *****************************************************************************/
43 static int OpenDecoder ( vlc_object_t * );
45 static int RunDecoder ( decoder_fifo_t * );
46 static int InitThread ( adec_thread_t * );
47 static void DecodeThread ( adec_thread_t * );
48 static void EndThread ( adec_thread_t * );
50 /*****************************************************************************
52 *****************************************************************************/
55 set_description( _("AAC audio decoder (using libfaad2)") );
56 set_capability( "decoder", 60 );
57 set_callbacks( OpenDecoder, NULL );
60 /*****************************************************************************
61 * OpenDecoder: probe the decoder and return score
62 *****************************************************************************
63 * Tries to launch a decoder and return score so that the interface is able
65 *****************************************************************************/
66 static int OpenDecoder( vlc_object_t *p_this )
68 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
70 if( p_fifo->i_fourcc != VLC_FOURCC('m','p','4','a') )
75 p_fifo->pf_run = RunDecoder;
79 /*****************************************************************************
80 * RunDecoder: this function is called just after the thread is created
81 *****************************************************************************/
82 static int RunDecoder( decoder_fifo_t *p_fifo )
84 adec_thread_t *p_adec;
87 if( !( p_adec = malloc( sizeof( adec_thread_t ) ) ) )
89 msg_Err( p_fifo, "out of memory" );
90 DecoderError( p_fifo );
93 memset( p_adec, 0, sizeof( adec_thread_t ) );
95 p_adec->p_fifo = p_fifo;
97 if( InitThread( p_adec ) != 0 )
99 DecoderError( p_fifo );
103 while( ( !p_adec->p_fifo->b_die )&&( !p_adec->p_fifo->b_error ) )
105 DecodeThread( p_adec );
109 if( ( b_error = p_adec->p_fifo->b_error ) )
111 DecoderError( p_adec->p_fifo );
123 static unsigned int pi_channels_maps[6] =
126 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
127 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
128 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
129 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
130 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
133 #define FREE( p ) if( p != NULL ) free( p ); p = NULL
134 #define GetWLE( p ) \
135 ( *(uint8_t*)(p) + ( *((uint8_t*)(p)+1) << 8 ) )
137 #define GetDWLE( p ) \
138 ( *(uint8_t*)(p) + ( *((uint8_t*)(p)+1) << 8 ) + \
139 ( *((uint8_t*)(p)+2) << 16 ) + ( *((uint8_t*)(p)+3) << 24 ) )
142 static void GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
147 data_packet_t *p_data;
150 p_data = p_pes->p_first;
151 while( p_data != NULL && i_count < i_max )
154 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
160 p_data->p_payload_start,
164 p_data = p_data->p_next;
169 if( i_count < i_max )
171 memset( p_buf, 0, i_max - i_count );
175 /*****************************************************************************
176 * InitThread: initialize data before entering main loop
177 *****************************************************************************/
178 static int InitThread( adec_thread_t * p_adec )
180 WAVEFORMATEX wf, *p_wf;
182 unsigned long i_rate;
183 unsigned char i_nb_channels;
185 faacDecConfiguration *p_faad_config;
187 if( ( p_wf = (WAVEFORMATEX*)p_adec->p_fifo->p_waveformatex ) == NULL )
189 msg_Warn( p_adec->p_fifo,
190 "cannot load stream informations" );
192 memset( p_wf, 0, sizeof( WAVEFORMATEX ) );
195 p_adec->p_buffer = NULL;
196 p_adec->i_buffer = 0;
199 if( !( p_adec->p_handle = faacDecOpen() ) )
201 msg_Err( p_adec->p_fifo,
202 "cannot initialize faad" );
206 if( p_wf->cbSize <= 0 )
211 msg_Warn( p_adec->p_fifo,
212 "DecoderSpecificInfo missing, trying with first frame" );
213 // gather first frame
216 input_ExtractPES( p_adec->p_fifo, &p_pes );
221 i_frame_size = p_pes->i_pes_size;
223 if( i_frame_size > 0 )
225 if( p_adec->i_buffer < i_frame_size + 16 )
227 FREE( p_adec->p_buffer );
228 p_adec->p_buffer = malloc( i_frame_size + 16 );
229 p_adec->i_buffer = i_frame_size + 16;
232 GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
236 input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
238 } while( i_frame_size <= 0 );
241 i_status = faacDecInit( p_adec->p_handle,
249 i_status = faacDecInit2( p_adec->p_handle,
258 msg_Err( p_adec->p_fifo,
259 "failed to initialize faad" );
260 //faacDecClose( p_adec->p_handle );
263 msg_Dbg( p_adec->p_fifo,
264 "faad intitialized, samplerate: %ldHz channels: %d",
269 /* set default configuration */
270 p_faad_config = faacDecGetCurrentConfiguration( p_adec->p_handle );
271 p_faad_config->outputFormat = FAAD_FMT_FLOAT;
272 faacDecSetConfiguration( p_adec->p_handle, p_faad_config );
275 /* Initialize the thread properties */
276 p_adec->output_format.i_format = VLC_FOURCC('f','l','3','2');
277 p_adec->output_format.i_rate = i_rate;
278 p_adec->output_format.i_physical_channels =
279 p_adec->output_format.i_original_channels =
280 pi_channels_maps[i_nb_channels];
281 p_adec->p_aout = NULL;
282 p_adec->p_aout_input = NULL;
289 /*****************************************************************************
290 * DecodeThread: decodes a frame
291 *****************************************************************************
292 * XXX it will work only for frame based streams like from mp4 file
293 * but it's the only way to use libfaad2 without having segfault
294 * after 1 or 2 frames
295 *****************************************************************************/
296 static void DecodeThread( adec_thread_t *p_adec )
298 aout_buffer_t *p_aout_buffer;
301 faacDecFrameInfo faad_frame;
305 /* **** Get a new frames from streams **** */
308 input_ExtractPES( p_adec->p_fifo, &p_pes );
311 p_adec->p_fifo->b_error = 1;
314 if( p_pes->i_pts != 0 )
316 p_adec->pts = p_pes->i_pts;
318 i_frame_size = p_pes->i_pes_size;
320 if( i_frame_size > 0 )
322 if( p_adec->i_buffer < i_frame_size + 16 )
324 FREE( p_adec->p_buffer );
325 p_adec->p_buffer = malloc( i_frame_size + 16 );
326 p_adec->i_buffer = i_frame_size + 16;
329 GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
331 input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
332 } while( i_frame_size <= 0 );
334 /* **** decode this frame **** */
335 p_faad_buffer = faacDecDecode( p_adec->p_handle,
340 /* **** some sanity checks to see if we have samples to out **** */
341 if( faad_frame.error > 0 )
343 msg_Warn( p_adec->p_fifo, "%s",
344 faacDecGetErrorMessage(faad_frame.error) );
347 if( ( faad_frame.channels <= 0 )||
348 ( faad_frame.channels > AAC_MAXCHANNELS) ||
349 ( faad_frame.channels > 5 ) )
351 msg_Warn( p_adec->p_fifo,
352 "invalid channels count(%d)", faad_frame.channels );
355 if( faad_frame.samples <= 0 )
357 msg_Warn( p_adec->p_fifo, "decoded zero sample !" );
362 msg_Dbg( p_adec->p_fifo,
363 "decoded frame samples:%d, channels:%d, consumed:%d",
366 faad_frame.bytesconsumed );
369 /* **** Now we can output these samples **** */
371 /* **** First check if we have a valid output **** */
372 if( ( !p_adec->p_aout_input )||
373 ( p_adec->output_format.i_original_channels !=
374 pi_channels_maps[faad_frame.channels] ) )
376 if( p_adec->p_aout_input )
378 /* **** Delete the old **** */
379 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
382 /* **** Create a new audio output **** */
383 p_adec->output_format.i_physical_channels =
384 p_adec->output_format.i_original_channels =
385 pi_channels_maps[faad_frame.channels];
387 aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
388 p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
390 &p_adec->output_format );
393 if( !p_adec->p_aout_input )
395 msg_Err( p_adec->p_fifo, "cannot create aout" );
399 if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
401 aout_DateSet( &p_adec->date, p_adec->pts );
403 else if( !aout_DateGet( &p_adec->date ) )
408 p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
409 p_adec->p_aout_input,
410 faad_frame.samples / faad_frame.channels );
413 msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
414 p_adec->p_fifo->b_error = 1;
417 p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
418 p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
420 faad_frame.channels );
421 memcpy( p_aout_buffer->p_buffer,
423 p_aout_buffer->i_nb_bytes );
425 aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
429 /*****************************************************************************
430 * EndThread : faad decoder thread destruction
431 *****************************************************************************/
432 static void EndThread (adec_thread_t *p_adec)
434 if( p_adec->p_aout_input )
436 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
439 if( p_adec->p_handle )
441 faacDecClose( p_adec->p_handle );
444 FREE( p_adec->p_buffer );
446 msg_Dbg( p_adec->p_fifo, "faad decoder closed" );