1 /*****************************************************************************
2 * decoder.c: AAC decoder using libfaad2
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: decoder.c,v 1.13 2002/11/15 01:17:08 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 decoder module (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 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 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
137 #define GetDWLE( p ) \
138 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
139 ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
141 static void faac_GetWaveFormatEx( waveformatex_t *p_wh,
145 p_wh->i_formattag = GetWLE( p_data );
146 p_wh->i_nb_channels = GetWLE( p_data + 2 );
147 p_wh->i_samplespersec = GetDWLE( p_data + 4 );
148 p_wh->i_avgbytespersec= GetDWLE( p_data + 8 );
149 p_wh->i_blockalign = GetWLE( p_data + 12 );
150 p_wh->i_bitspersample = GetWLE( p_data + 14 );
151 p_wh->i_size = GetWLE( p_data + 16 );
155 p_wh->p_data = malloc( p_wh->i_size );
156 memcpy( p_wh->p_data, p_data + 18, p_wh->i_size );
160 static void GetPESData( u8 *p_buf, int i_max, pes_packet_t *p_pes )
165 data_packet_t *p_data;
168 p_data = p_pes->p_first;
169 while( p_data != NULL && i_count < i_max )
172 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
178 p_data->p_payload_start,
182 p_data = p_data->p_next;
187 if( i_count < i_max )
189 memset( p_buf, 0, i_max - i_count );
193 /*****************************************************************************
194 * InitThread: initialize data before entering main loop
195 *****************************************************************************/
196 static int InitThread( adec_thread_t * p_adec )
199 unsigned long i_rate;
200 unsigned char i_nb_channels;
202 faacDecConfiguration *p_faad_config;
204 if( !p_adec->p_fifo->p_demux_data )
206 msg_Warn( p_adec->p_fifo,
207 "cannot load stream informations" );
211 faac_GetWaveFormatEx( &p_adec->format,
212 (u8*)p_adec->p_fifo->p_demux_data );
215 p_adec->p_buffer = NULL;
216 p_adec->i_buffer = 0;
219 if( !( p_adec->p_handle = faacDecOpen() ) )
221 msg_Err( p_adec->p_fifo,
222 "cannot initialize faad" );
223 FREE( p_adec->format.p_data );
227 if( p_adec->format.p_data == NULL )
232 msg_Warn( p_adec->p_fifo,
233 "DecoderSpecificInfo missing, trying with first frame" );
234 // gather first frame
237 input_ExtractPES( p_adec->p_fifo, &p_pes );
242 i_frame_size = p_pes->i_pes_size;
244 if( i_frame_size > 0 )
246 if( p_adec->i_buffer < i_frame_size + 16 )
248 FREE( p_adec->p_buffer );
249 p_adec->p_buffer = malloc( i_frame_size + 16 );
250 p_adec->i_buffer = i_frame_size + 16;
253 GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
257 input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
259 } while( i_frame_size <= 0 );
262 i_status = faacDecInit( p_adec->p_handle,
270 i_status = faacDecInit2( p_adec->p_handle,
271 p_adec->format.p_data,
272 p_adec->format.i_size,
279 msg_Err( p_adec->p_fifo,
280 "failed to initialize faad" );
281 faacDecClose( p_adec->p_handle );
284 msg_Dbg( p_adec->p_fifo,
285 "faad intitialized, samplerate:%dHz channels:%d",
290 /* set default configuration */
291 p_faad_config = faacDecGetCurrentConfiguration( p_adec->p_handle );
292 p_faad_config->outputFormat = FAAD_FMT_FLOAT;
293 faacDecSetConfiguration( p_adec->p_handle, p_faad_config );
296 /* Initialize the thread properties */
297 p_adec->output_format.i_format = VLC_FOURCC('f','l','3','2');
298 p_adec->output_format.i_rate = i_rate;
299 p_adec->output_format.i_physical_channels =
300 p_adec->output_format.i_original_channels =
301 pi_channels_maps[i_nb_channels];
302 p_adec->p_aout = NULL;
303 p_adec->p_aout_input = NULL;
310 /*****************************************************************************
311 * DecodeThread: decodes a frame
312 *****************************************************************************
313 * XXX it will work only for frame based streams like from mp4 file
314 * but it's the only way to use libfaad2 without having segfault
315 * after 1 or 2 frames
316 *****************************************************************************/
317 static void DecodeThread( adec_thread_t *p_adec )
319 aout_buffer_t *p_aout_buffer;
322 faacDecFrameInfo faad_frame;
326 /* **** Get a new frames from streams **** */
329 input_ExtractPES( p_adec->p_fifo, &p_pes );
332 p_adec->p_fifo->b_error = 1;
335 if( p_pes->i_pts != 0 )
337 p_adec->pts = p_pes->i_pts;
339 i_frame_size = p_pes->i_pes_size;
341 if( i_frame_size > 0 )
343 if( p_adec->i_buffer < i_frame_size + 16 )
345 FREE( p_adec->p_buffer );
346 p_adec->p_buffer = malloc( i_frame_size + 16 );
347 p_adec->i_buffer = i_frame_size + 16;
350 GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
352 input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
353 } while( i_frame_size <= 0 );
355 /* **** decode this frame **** */
356 p_faad_buffer = faacDecDecode( p_adec->p_handle,
361 /* **** some sanity checks to see if we have samples to out **** */
362 if( faad_frame.error > 0 )
364 msg_Warn( p_adec->p_fifo, "%s",
365 faacDecGetErrorMessage(faad_frame.error) );
368 if( ( faad_frame.channels <= 0 )||
369 ( faad_frame.channels > AAC_MAXCHANNELS) ||
370 ( faad_frame.channels > 5 ) )
372 msg_Warn( p_adec->p_fifo,
373 "invalid channels count(%d)", faad_frame.channels );
376 if( faad_frame.samples <= 0 )
378 msg_Warn( p_adec->p_fifo, "decoded zero sample !" );
383 msg_Dbg( p_adec->p_fifo,
384 "decoded frame samples:%d, channels:%d, consumed:%d",
387 faad_frame.bytesconsumed );
390 /* **** Now we can output these samples **** */
392 /* **** First check if we have a valid output **** */
393 if( ( !p_adec->p_aout_input )||
394 ( p_adec->output_format.i_original_channels !=
395 pi_channels_maps[faad_frame.channels] ) )
397 if( p_adec->p_aout_input )
399 /* **** Delete the old **** */
400 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
403 /* **** Create a new audio output **** */
404 p_adec->output_format.i_physical_channels =
405 p_adec->output_format.i_original_channels =
406 pi_channels_maps[faad_frame.channels];
408 aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
409 p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
411 &p_adec->output_format );
414 if( !p_adec->p_aout_input )
416 msg_Err( p_adec->p_fifo, "cannot create aout" );
420 if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
422 aout_DateSet( &p_adec->date, p_adec->pts );
424 else if( !aout_DateGet( &p_adec->date ) )
429 p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
430 p_adec->p_aout_input,
431 faad_frame.samples / faad_frame.channels );
434 msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
435 p_adec->p_fifo->b_error = 1;
438 p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
439 p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
441 faad_frame.channels );
442 memcpy( p_aout_buffer->p_buffer,
444 p_aout_buffer->i_nb_bytes );
446 aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
450 /*****************************************************************************
451 * EndThread : faad decoder thread destruction
452 *****************************************************************************/
453 static void EndThread (adec_thread_t *p_adec)
455 if( p_adec->p_aout_input )
457 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
460 if( p_adec->p_handle )
462 faacDecClose( p_adec->p_handle );
465 FREE( p_adec->format.p_data );
466 FREE( p_adec->p_buffer );
468 msg_Dbg( p_adec->p_fifo, "faad decoder closed" );