configure: Improve detection of ibtool
[vlc.git] / modules / codec / faad.c
blobfa82b0ccc89f79c8d842ec3146f39193ae567186
1 /*****************************************************************************
2 * faad.c: AAC decoder using libfaad2
3 *****************************************************************************
4 * Copyright (C) 2001, 2003 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * NOTA BENE: this module requires the linking against a library which is
27 * known to require licensing under the GNU General Public License version 2
28 * (or later). Therefore, the result of compiling this module will normally
29 * be subject to the terms of that later license.
30 *****************************************************************************/
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_input.h>
40 #include <vlc_codec.h>
41 #include <vlc_cpu.h>
42 #include <vlc_aout.h>
44 #include <neaacdec.h>
45 #include "../packetizer/mpeg4audio.h"
47 /*****************************************************************************
48 * Module descriptor
49 *****************************************************************************/
50 static int Open( vlc_object_t * );
51 static void Close( vlc_object_t * );
53 vlc_module_begin ()
54 set_description( N_("AAC audio decoder (using libfaad2)") )
55 set_capability( "decoder", 100 )
56 set_category( CAT_INPUT )
57 set_subcategory( SUBCAT_INPUT_ACODEC )
58 set_callbacks( Open, Close )
59 vlc_module_end ()
61 /****************************************************************************
62 * Local prototypes
63 ****************************************************************************/
64 static int DecodeBlock( decoder_t *, block_t * );
65 static void Flush( decoder_t * );
66 static void DoReordering( uint32_t *, uint32_t *, int, int, uint8_t * );
68 struct decoder_sys_t
70 /* faad handler */
71 NeAACDecHandle *hfaad;
73 /* samples */
74 date_t date;
76 /* temporary buffer */
77 block_t *p_block;
79 /* Channel positions of the current stream (for re-ordering) */
80 uint32_t pi_channel_positions[MPEG4_ASC_MAX_INDEXEDPOS];
82 bool b_sbr, b_ps, b_discontinuity;
85 #if MPEG4_ASC_MAX_INDEXEDPOS != LFE_CHANNEL
86 #error MPEG4_ASC_MAX_INDEXEDPOS != LFE_CHANNEL
87 #endif
89 #define FAAD_CHANNEL_ID_COUNT (LFE_CHANNEL + 1)
90 static const uint32_t pi_tovlcmapping[FAAD_CHANNEL_ID_COUNT] =
92 [UNKNOWN_CHANNEL] = 0,
93 [FRONT_CHANNEL_CENTER] = AOUT_CHAN_CENTER,
94 [FRONT_CHANNEL_LEFT] = AOUT_CHAN_LEFT,
95 [FRONT_CHANNEL_RIGHT] = AOUT_CHAN_RIGHT,
96 [SIDE_CHANNEL_LEFT] = AOUT_CHAN_MIDDLELEFT,
97 [SIDE_CHANNEL_RIGHT] = AOUT_CHAN_MIDDLERIGHT,
98 [BACK_CHANNEL_LEFT] = AOUT_CHAN_REARLEFT,
99 [BACK_CHANNEL_RIGHT] = AOUT_CHAN_REARRIGHT,
100 [BACK_CHANNEL_CENTER] = AOUT_CHAN_REARCENTER,
101 [LFE_CHANNEL] = AOUT_CHAN_LFE
104 /*****************************************************************************
105 * OpenDecoder: probe the decoder and return score
106 *****************************************************************************/
107 static int Open( vlc_object_t *p_this )
109 decoder_t *p_dec = (decoder_t*)p_this;
110 decoder_sys_t *p_sys;
111 NeAACDecConfiguration *cfg;
113 if( p_dec->fmt_in.i_codec != VLC_CODEC_MP4A )
115 return VLC_EGENERIC;
118 /* Allocate the memory needed to store the decoder's structure */
119 if( ( p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) ) ) == NULL )
120 return VLC_ENOMEM;
122 /* Open a faad context */
123 if( ( p_sys->hfaad = NeAACDecOpen() ) == NULL )
125 msg_Err( p_dec, "cannot initialize faad" );
126 free( p_sys );
127 return VLC_EGENERIC;
130 /* Misc init */
131 date_Set( &p_sys->date, 0 );
132 p_dec->fmt_out.i_cat = AUDIO_ES;
134 p_dec->fmt_out.i_codec = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
136 p_dec->fmt_out.audio.i_physical_channels =
137 p_dec->fmt_out.audio.i_original_channels = 0;
139 if( p_dec->fmt_in.i_extra > 0 )
141 /* We have a decoder config so init the handle */
142 unsigned long i_rate;
143 unsigned char i_channels;
145 if( NeAACDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
146 p_dec->fmt_in.i_extra,
147 &i_rate, &i_channels ) < 0 )
149 msg_Err( p_dec, "Failed to initialize faad using extra data" );
150 NeAACDecClose( p_sys->hfaad );
151 free( p_sys );
152 return VLC_EGENERIC;
155 p_dec->fmt_out.audio.i_rate = i_rate;
156 p_dec->fmt_out.audio.i_channels = i_channels;
157 p_dec->fmt_out.audio.i_physical_channels
158 = p_dec->fmt_out.audio.i_original_channels
159 = mpeg4_asc_channelsbyindex[i_channels];
160 date_Init( &p_sys->date, i_rate, 1 );
162 else
164 /* Will be initalised from first frame */
165 p_dec->fmt_out.audio.i_rate = 0;
166 p_dec->fmt_out.audio.i_channels = 0;
169 /* Set the faad config */
170 cfg = NeAACDecGetCurrentConfiguration( p_sys->hfaad );
171 if( p_dec->fmt_in.audio.i_rate )
172 cfg->defSampleRate = p_dec->fmt_in.audio.i_rate;
173 cfg->outputFormat = HAVE_FPU ? FAAD_FMT_FLOAT : FAAD_FMT_16BIT;
174 NeAACDecSetConfiguration( p_sys->hfaad, cfg );
176 /* buffer */
177 p_sys->p_block = NULL;
179 p_sys->b_discontinuity =
180 p_sys->b_sbr = p_sys->b_ps = false;
182 p_dec->pf_decode = DecodeBlock;
183 p_dec->pf_flush = Flush;
184 return VLC_SUCCESS;
187 /*****************************************************************************
188 * FlushBuffer:
189 *****************************************************************************/
190 static void FlushBuffer( decoder_sys_t *p_sys, size_t i_used )
192 block_t *p_block = p_sys->p_block;
193 if( p_block )
195 if( i_used < p_block->i_buffer )
197 /* Drop padding */
198 for( ; i_used < p_block->i_buffer; i_used++ )
199 if( p_block->p_buffer[i_used] != 0x00 )
200 break;
202 p_block->i_buffer -= i_used;
203 p_block->p_buffer += i_used;
205 else p_block->i_buffer = 0;
206 if( p_block->i_buffer == 0 )
208 block_Release( p_block );
209 p_sys->p_block = NULL;
214 /*****************************************************************************
215 * Flush:
216 *****************************************************************************/
217 static void Flush( decoder_t *p_dec )
219 decoder_sys_t *p_sys = p_dec->p_sys;
221 date_Set( &p_sys->date, VLC_TS_INVALID );
222 FlushBuffer( p_sys, SIZE_MAX );
225 /*****************************************************************************
226 * DecodeBlock:
227 *****************************************************************************/
228 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
230 decoder_sys_t *p_sys = p_dec->p_sys;
232 if( !p_block ) /* No Drain */
233 return VLCDEC_SUCCESS;
235 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
237 Flush( p_dec );
238 if( p_block->i_flags & (BLOCK_FLAG_CORRUPTED) )
240 block_Release( p_block );
241 return VLCDEC_SUCCESS;
245 /* Remove ADTS header if we have decoder specific config */
246 if( p_dec->fmt_in.i_extra && p_block->i_buffer > 7 )
248 if( p_block->p_buffer[0] == 0xff &&
249 ( p_block->p_buffer[1] & 0xf0 ) == 0xf0 ) /* syncword */
250 { /* ADTS header present */
251 size_t i_header_size; /* 7 bytes (+ 2 bytes for crc) */
252 i_header_size = 7 + ( ( p_block->p_buffer[1] & 0x01 ) ? 0 : 2 );
253 /* FIXME: multiple blocks per frame */
254 if( p_block->i_buffer > i_header_size )
256 p_block->p_buffer += i_header_size;
257 p_block->i_buffer -= i_header_size;
262 const mtime_t i_pts = p_block->i_pts;
264 /* Append block as temporary buffer */
265 if( p_sys->p_block == NULL )
267 p_sys->p_block = p_block;
269 else
271 p_sys->p_block->p_next = p_block;
272 block_t *p_prev = p_sys->p_block;
273 p_sys->p_block = block_ChainGather( p_sys->p_block );
274 if( p_sys->p_block == NULL )
275 block_ChainRelease( p_prev );
278 /* !Warn: do not use p_block beyond this point */
280 if( p_dec->fmt_out.audio.i_rate == 0 && p_dec->fmt_in.i_extra > 0 )
282 /* We have a decoder config so init the handle */
283 unsigned long i_rate;
284 unsigned char i_channels;
286 if( NeAACDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
287 p_dec->fmt_in.i_extra,
288 &i_rate, &i_channels ) >= 0 )
290 p_dec->fmt_out.audio.i_rate = i_rate;
291 p_dec->fmt_out.audio.i_channels = i_channels;
292 p_dec->fmt_out.audio.i_physical_channels
293 = p_dec->fmt_out.audio.i_original_channels
294 = mpeg4_asc_channelsbyindex[i_channels];
296 date_Init( &p_sys->date, i_rate, 1 );
300 if( p_dec->fmt_out.audio.i_rate == 0 && p_sys->p_block && p_sys->p_block->i_buffer )
302 unsigned long i_rate;
303 unsigned char i_channels;
305 /* Init faad with the first frame */
306 if( NeAACDecInit( p_sys->hfaad,
307 p_sys->p_block->p_buffer, p_sys->p_block->i_buffer,
308 &i_rate, &i_channels ) < 0 )
310 return VLCDEC_SUCCESS;
313 p_dec->fmt_out.audio.i_rate = i_rate;
314 p_dec->fmt_out.audio.i_channels = i_channels;
315 p_dec->fmt_out.audio.i_physical_channels
316 = p_dec->fmt_out.audio.i_original_channels
317 = mpeg4_asc_channelsbyindex[i_channels];
318 date_Init( &p_sys->date, i_rate, 1 );
321 if( i_pts > VLC_TS_INVALID && i_pts != date_Get( &p_sys->date ) )
323 date_Set( &p_sys->date, i_pts );
325 else if( !date_Get( &p_sys->date ) )
327 /* We've just started the stream, wait for the first PTS. */
328 FlushBuffer( p_sys, SIZE_MAX );
329 return VLCDEC_SUCCESS;
332 /* Decode all data */
333 if( p_sys->p_block && p_sys->p_block->i_buffer > 1 )
335 void *samples;
336 NeAACDecFrameInfo frame;
337 block_t *p_out = NULL;
339 samples = NeAACDecDecode( p_sys->hfaad, &frame,
340 p_sys->p_block->p_buffer,
341 p_sys->p_block->i_buffer );
343 if( frame.error > 0 )
345 msg_Warn( p_dec, "%s", NeAACDecGetErrorMessage( frame.error ) );
347 if( frame.error == 21 || frame.error == 12 )
350 * Once an "Unexpected channel configuration change"
351 * or a "Invalid number of channels" error
352 * occurs, it will occurs afterwards, and we got no sound.
353 * Reinitialization of the decoder is required.
355 unsigned long i_rate;
356 unsigned char i_channels;
357 NeAACDecHandle *hfaad;
358 NeAACDecConfiguration *cfg,*oldcfg;
360 oldcfg = NeAACDecGetCurrentConfiguration( p_sys->hfaad );
361 hfaad = NeAACDecOpen();
362 cfg = NeAACDecGetCurrentConfiguration( hfaad );
363 if( oldcfg->defSampleRate )
364 cfg->defSampleRate = oldcfg->defSampleRate;
365 cfg->defObjectType = oldcfg->defObjectType;
366 cfg->outputFormat = oldcfg->outputFormat;
367 NeAACDecSetConfiguration( hfaad, cfg );
369 if( NeAACDecInit( hfaad,
370 p_sys->p_block->p_buffer,
371 p_sys->p_block->i_buffer,
372 &i_rate,&i_channels ) < 0 )
374 /* reinitialization failed */
375 NeAACDecClose( hfaad );
376 NeAACDecSetConfiguration( p_sys->hfaad, oldcfg );
378 else
380 NeAACDecClose( p_sys->hfaad );
381 p_sys->hfaad = hfaad;
382 p_dec->fmt_out.audio.i_rate = i_rate;
383 p_dec->fmt_out.audio.i_channels = i_channels;
384 p_dec->fmt_out.audio.i_physical_channels
385 = p_dec->fmt_out.audio.i_original_channels
386 = mpeg4_asc_channelsbyindex[i_channels];
387 date_Init( &p_sys->date, i_rate, 1 );
391 Flush( p_dec );
392 p_sys->b_discontinuity = true;
394 return VLCDEC_SUCCESS;
397 if( frame.channels == 0 || frame.channels >= 64 )
399 msg_Warn( p_dec, "invalid channels count: %i", frame.channels );
400 FlushBuffer( p_sys, frame.bytesconsumed );
401 if( frame.channels == 0 )
403 p_sys->b_discontinuity = true;
404 return VLCDEC_SUCCESS;
408 if( frame.samples == 0 )
410 msg_Warn( p_dec, "decoded zero sample" );
411 FlushBuffer( p_sys, frame.bytesconsumed );
412 return VLCDEC_SUCCESS;
415 /* We decoded a valid frame */
416 if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
418 date_Init( &p_sys->date, frame.samplerate, 1 );
419 date_Set( &p_sys->date, i_pts );
422 p_dec->fmt_out.audio.i_rate = frame.samplerate;
424 /* Adjust stream info when dealing with SBR/PS */
425 bool b_sbr = (frame.sbr == 1) || (frame.sbr == 2);
426 if( p_sys->b_sbr != b_sbr || p_sys->b_ps != frame.ps )
428 const char *psz_ext = (b_sbr && frame.ps) ? "SBR+PS" :
429 b_sbr ? "SBR" : "PS";
431 msg_Dbg( p_dec, "AAC %s (channels: %u, samplerate: %lu)",
432 psz_ext, frame.channels, frame.samplerate );
434 if( !p_dec->p_description )
435 p_dec->p_description = vlc_meta_New();
436 if( p_dec->p_description )
437 vlc_meta_AddExtra( p_dec->p_description, _("AAC extension"), psz_ext );
439 p_sys->b_sbr = b_sbr;
440 p_sys->b_ps = frame.ps;
443 /* PS Enabled FAAD PCA bug hotfix (contribs has patch) */
444 if( frame.channels == 8 )
446 const uint8_t psbugconfig[3][8] = { { 2, 3, 2, 3, 2, 3, 6, 7 }, /* fdk 7.1 (4 Front) */
447 { 2, 3, 2, 3, 2, 3, 4, 5 }, /* 7.1 */
448 { 1, 2, 3, 4, 5, 6, 7, 9 } };/* fixed */
449 for( size_t i=0; i<2; i++ )
451 if( !memcmp( frame.channel_position, psbugconfig[i], 8 ) )
453 msg_Warn( p_dec, "Unpatched FAAD2 library with PS Bug. Trying to workaround !" );
454 memcpy( frame.channel_position, psbugconfig[2], 8 );
455 break;
460 /* Hotfix channels misdetection/repetition for FDK 7.1 */
461 const uint8_t fdk71config[] = { 1, 2, 3, 6, 7, 6, 7, 9 };
462 if( frame.channels == 8 && !memcmp( frame.channel_position, fdk71config, 8 ) )
464 msg_Warn( p_dec, "Patching for FDK encoded 7.1 4 Front channel repeat bug" );
465 frame.channel_position[3] = 4;
466 frame.channel_position[4] = 5;
469 /* Convert frame.channel_position to our own channel values */
470 p_dec->fmt_out.audio.i_physical_channels = 0;
471 uint32_t pi_faad_channels_positions[FAAD_CHANNEL_ID_COUNT] = {0};
472 uint8_t pi_neworder_table[AOUT_CHAN_MAX];
473 for( size_t i = 0; i < frame.channels; i++ )
475 unsigned pos = frame.channel_position[i];
476 if( likely(pos < FAAD_CHANNEL_ID_COUNT) )
478 pi_faad_channels_positions[i] = pi_tovlcmapping[pos];
479 p_dec->fmt_out.audio.i_physical_channels |= pi_faad_channels_positions[i];
481 else pi_faad_channels_positions[i] = 0;
484 aout_CheckChannelReorder( pi_faad_channels_positions, NULL,
485 p_dec->fmt_out.audio.i_physical_channels, pi_neworder_table );
488 p_dec->fmt_out.audio.i_original_channels = p_dec->fmt_out.audio.i_physical_channels;
489 p_dec->fmt_out.audio.i_channels = popcount(p_dec->fmt_out.audio.i_physical_channels);
491 if( !decoder_UpdateAudioFormat( p_dec ) && p_dec->fmt_out.audio.i_channels > 0 )
492 p_out = decoder_NewAudioBuffer( p_dec, frame.samples / p_dec->fmt_out.audio.i_channels );
494 if( p_out )
496 p_out->i_pts = date_Get( &p_sys->date );
497 p_out->i_length = date_Increment( &p_sys->date,
498 frame.samples / frame.channels )
499 - p_out->i_pts;
501 /* Don't kill speakers if some weird mapping does not gets 1:1 */
502 if( popcount(p_dec->fmt_out.audio.i_physical_channels) != frame.channels )
503 memset( p_out->p_buffer, 0, p_out->i_buffer );
505 /* FIXME: replace when aout_channel_reorder can take samples from a different buffer */
506 DoReordering( (uint32_t *)p_out->p_buffer, samples,
507 frame.samples / frame.channels, frame.channels,
508 pi_neworder_table );
510 if( p_sys->b_discontinuity )
512 p_out->i_flags |= BLOCK_FLAG_DISCONTINUITY;
513 p_sys->b_discontinuity = false;
516 decoder_QueueAudio( p_dec, p_out );
518 else
520 date_Increment( &p_sys->date, frame.samples / frame.channels );
523 FlushBuffer( p_sys, frame.bytesconsumed );
525 return VLCDEC_SUCCESS;
527 else
529 /* Drop byte of padding */
530 FlushBuffer( p_sys, 0 );
533 return VLCDEC_SUCCESS;
536 /*****************************************************************************
537 * Close:
538 *****************************************************************************/
539 static void Close( vlc_object_t *p_this )
541 decoder_t *p_dec = (decoder_t *)p_this;
542 decoder_sys_t *p_sys = p_dec->p_sys;
544 NeAACDecClose( p_sys->hfaad );
545 FlushBuffer( p_sys, SIZE_MAX );
546 free( p_sys );
549 /*****************************************************************************
550 * DoReordering: do some channel re-ordering (the ac3 channel order is
551 * different from the aac one).
552 *****************************************************************************/
553 static void DoReordering( uint32_t *p_out, uint32_t *p_in, int i_samples,
554 int i_nb_channels, uint8_t *pi_chan_positions )
556 #if HAVE_FPU
557 #define CAST_SAMPLE(a) a
558 #else
559 #define CAST_SAMPLE(a) ((uint16_t *)a)
560 #endif
561 /* Do the actual reordering */
562 for( int i = 0; i < i_samples; i++ )
564 for( int j = 0; j < i_nb_channels; j++ )
566 CAST_SAMPLE(p_out)[i * i_nb_channels + pi_chan_positions[j]] =
567 CAST_SAMPLE(p_in)[i * i_nb_channels + j];