1 /*****************************************************************************
2 * decoder.c: AAC decoder using libfaad2
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: decoder.c,v 1.27 2003/08/23 16:15:47 fenrir 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[7] =
127 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
128 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
129 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
130 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
132 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_REARCENTER,
136 #define FREE( p ) if( p != NULL ) free( p ); p = NULL
138 static void GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
143 data_packet_t *p_data;
146 p_data = p_pes->p_first;
147 while( p_data != NULL && i_count < i_max )
150 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
156 p_data->p_payload_start,
160 p_data = p_data->p_next;
165 if( i_count < i_max )
167 memset( p_buf, 0, i_max - i_count );
171 /*****************************************************************************
172 * InitThread: initialize data before entering main loop
173 *****************************************************************************/
174 static int InitThread( adec_thread_t * p_adec )
176 WAVEFORMATEX wf, *p_wf;
178 unsigned long i_rate;
179 unsigned char i_nb_channels;
181 faacDecConfiguration *p_faad_config;
183 if( ( p_wf = (WAVEFORMATEX*)p_adec->p_fifo->p_waveformatex ) == NULL )
185 msg_Warn( p_adec->p_fifo,
186 "cannot load stream informations" );
188 memset( p_wf, 0, sizeof( WAVEFORMATEX ) );
191 p_adec->p_buffer = NULL;
192 p_adec->i_buffer = 0;
195 if( !( p_adec->p_handle = faacDecOpen() ) )
197 msg_Err( p_adec->p_fifo,
198 "cannot initialize faad" );
202 if( p_wf->cbSize <= 0 )
207 faacDecConfigurationPtr cfg;
209 cfg = faacDecGetCurrentConfiguration( p_adec->p_handle );
210 if( p_wf->nSamplesPerSec > 0 )
212 cfg->defSampleRate = p_wf->nSamplesPerSec;
214 faacDecSetConfiguration( p_adec->p_handle, cfg );
215 msg_Warn( p_adec->p_fifo,
216 "DecoderSpecificInfo missing, trying with first frame" );
217 // gather first frame
220 input_ExtractPES( p_adec->p_fifo, &p_pes );
225 i_frame_size = p_pes->i_pes_size;
227 if( i_frame_size > 0 )
229 if( p_adec->i_buffer < i_frame_size + 16 )
231 FREE( p_adec->p_buffer );
232 p_adec->p_buffer = malloc( i_frame_size + 16 );
233 p_adec->i_buffer = i_frame_size + 16;
236 GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
240 input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
242 } while( i_frame_size <= 0 );
244 #ifdef HAVE_OLD_FAAD2
245 i_status = faacDecInit( p_adec->p_handle,
250 i_status = faacDecInit( p_adec->p_handle,
260 i_status = faacDecInit2( p_adec->p_handle,
269 msg_Err( p_adec->p_fifo,
270 "failed to initialize faad" );
271 //faacDecClose( p_adec->p_handle );
274 msg_Dbg( p_adec->p_fifo,
275 "faad intitialized, samplerate: %ldHz channels: %d",
280 /* set default configuration */
281 p_faad_config = faacDecGetCurrentConfiguration( p_adec->p_handle );
282 p_faad_config->outputFormat = FAAD_FMT_FLOAT;
283 faacDecSetConfiguration( p_adec->p_handle, p_faad_config );
286 /* Initialize the thread properties */
287 p_adec->output_format.i_format = VLC_FOURCC('f','l','3','2');
288 p_adec->output_format.i_rate = i_rate;
289 p_adec->output_format.i_physical_channels =
290 p_adec->output_format.i_original_channels =
291 pi_channels_maps[i_nb_channels];
292 p_adec->p_aout = NULL;
293 p_adec->p_aout_input = NULL;
300 /*****************************************************************************
301 * DecodeThread: decodes a frame
302 *****************************************************************************
303 * XXX it will work only for frame based streams like from mp4 file
304 * but it's the only way to use libfaad2 without having segfault
305 * after 1 or 2 frames
306 *****************************************************************************/
307 static void DecodeThread( adec_thread_t *p_adec )
309 aout_buffer_t *p_aout_buffer;
312 faacDecFrameInfo faad_frame;
317 /* **** Get a new frames from streams **** */
320 input_ExtractPES( p_adec->p_fifo, &p_pes );
323 p_adec->p_fifo->b_error = 1;
326 if( p_pes->i_pts != 0 )
328 p_adec->pts = p_pes->i_pts;
330 i_frame_size = p_pes->i_pes_size;
332 if( i_frame_size > 0 )
334 if( p_adec->i_buffer < i_frame_size + 16 )
336 FREE( p_adec->p_buffer );
337 p_adec->p_buffer = malloc( i_frame_size + 16 );
338 p_adec->i_buffer = i_frame_size + 16;
341 GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
343 input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
344 } while( i_frame_size <= 0 );
348 while( i_used < i_frame_size )
350 /* **** decode this frame **** */
351 #ifdef HAVE_OLD_FAAD2
352 p_faad_buffer = faacDecDecode( p_adec->p_handle,
354 &p_adec->p_buffer[i_used] );
356 p_faad_buffer = faacDecDecode( p_adec->p_handle,
358 &p_adec->p_buffer[i_used],
359 i_frame_size - i_used );
362 /* **** some sanity checks to see if we have samples to out **** */
363 if( faad_frame.error > 0 )
365 msg_Warn( p_adec->p_fifo, "%s",
366 faacDecGetErrorMessage(faad_frame.error) );
369 if( ( faad_frame.channels <= 0 )||
370 ( faad_frame.channels > AAC_MAXCHANNELS) ||
371 ( faad_frame.channels > 6 ) )
373 msg_Warn( p_adec->p_fifo,
374 "invalid channels count(%d)", faad_frame.channels );
377 if( faad_frame.samples <= 0 )
379 msg_Warn( p_adec->p_fifo, "decoded zero sample !" );
384 msg_Dbg( p_adec->p_fifo,
385 "decoded frame samples:%d, channels:%d, consumed:%d",
388 faad_frame.bytesconsumed );
390 i_used += faad_frame.bytesconsumed;
392 /* **** Now we can output these samples **** */
394 /* **** First check if we have a valid output **** */
395 if( ( !p_adec->p_aout_input )||
396 ( p_adec->output_format.i_original_channels != pi_channels_maps[faad_frame.channels] ) ||
397 ( p_adec->output_format.i_rate != faad_frame.samplerate ) )
399 if( p_adec->p_aout_input )
401 /* **** Delete the old **** */
402 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
405 /* **** Create a new audio output **** */
406 p_adec->output_format.i_physical_channels =
407 p_adec->output_format.i_original_channels =
408 pi_channels_maps[faad_frame.channels];
409 p_adec->output_format.i_rate = faad_frame.samplerate;
411 aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
412 p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
414 &p_adec->output_format );
417 if( !p_adec->p_aout_input )
419 msg_Err( p_adec->p_fifo, "cannot create aout" );
423 if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
425 aout_DateSet( &p_adec->date, p_adec->pts );
427 else if( !aout_DateGet( &p_adec->date ) )
432 p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
433 p_adec->p_aout_input,
434 faad_frame.samples / faad_frame.channels );
437 msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
438 p_adec->p_fifo->b_error = 1;
441 p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
442 p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
444 faad_frame.channels );
445 memcpy( p_aout_buffer->p_buffer,
447 p_aout_buffer->i_nb_bytes );
449 aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
454 /*****************************************************************************
455 * EndThread : faad decoder thread destruction
456 *****************************************************************************/
457 static void EndThread (adec_thread_t *p_adec)
459 if( p_adec->p_aout_input )
461 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
464 if( p_adec->p_handle )
466 faacDecClose( p_adec->p_handle );
469 FREE( p_adec->p_buffer );
471 msg_Dbg( p_adec->p_fifo, "faad decoder closed" );