demux: heif: send extradata with avif
[vlc.git] / modules / codec / faad.c
blob286c7aab5af1f215161f4b19280233c777e12d7d
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( "audio 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 typedef struct
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;
83 } decoder_sys_t;
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 p_dec->fmt_out.audio.channel_type = p_dec->fmt_in.audio.channel_type;
133 if( p_dec->fmt_in.i_extra > 0 )
135 /* We have a decoder config so init the handle */
136 unsigned long i_rate;
137 unsigned char i_channels;
139 if( NeAACDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
140 p_dec->fmt_in.i_extra,
141 &i_rate, &i_channels ) < 0 )
143 msg_Err( p_dec, "Failed to initialize faad using extra data" );
144 NeAACDecClose( p_sys->hfaad );
145 free( p_sys );
146 return VLC_EGENERIC;
149 p_dec->fmt_out.audio.i_rate = i_rate;
150 p_dec->fmt_out.audio.i_channels = i_channels;
151 p_dec->fmt_out.audio.i_physical_channels
152 = mpeg4_asc_channelsbyindex[i_channels];
153 date_Init( &p_sys->date, i_rate, 1 );
155 else
157 p_dec->fmt_out.audio.i_physical_channels = 0;
158 /* Will be initalised from first frame */
159 p_dec->fmt_out.audio.i_rate = 0;
160 p_dec->fmt_out.audio.i_channels = 0;
161 date_Set( &p_sys->date, VLC_TICK_INVALID );
164 p_dec->fmt_out.i_codec = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
165 p_dec->fmt_out.audio.i_chan_mode = p_dec->fmt_in.audio.i_chan_mode;
167 /* Set the faad config */
168 cfg = NeAACDecGetCurrentConfiguration( p_sys->hfaad );
169 if( p_dec->fmt_in.audio.i_rate )
170 cfg->defSampleRate = p_dec->fmt_in.audio.i_rate;
171 cfg->outputFormat = HAVE_FPU ? FAAD_FMT_FLOAT : FAAD_FMT_16BIT;
172 NeAACDecSetConfiguration( p_sys->hfaad, cfg );
174 /* buffer */
175 p_sys->p_block = NULL;
177 p_sys->b_discontinuity =
178 p_sys->b_sbr = p_sys->b_ps = false;
180 p_dec->pf_decode = DecodeBlock;
181 p_dec->pf_flush = Flush;
182 return VLC_SUCCESS;
185 /*****************************************************************************
186 * FlushBuffer:
187 *****************************************************************************/
188 static void FlushBuffer( decoder_sys_t *p_sys, size_t i_used )
190 block_t *p_block = p_sys->p_block;
191 if( p_block )
193 if( i_used < p_block->i_buffer )
195 /* Drop padding */
196 for( ; i_used < p_block->i_buffer; i_used++ )
197 if( p_block->p_buffer[i_used] != 0x00 )
198 break;
200 p_block->i_buffer -= i_used;
201 p_block->p_buffer += i_used;
203 else p_block->i_buffer = 0;
204 if( p_block->i_buffer == 0 )
206 block_Release( p_block );
207 p_sys->p_block = NULL;
212 /*****************************************************************************
213 * Flush:
214 *****************************************************************************/
215 static void Flush( decoder_t *p_dec )
217 decoder_sys_t *p_sys = p_dec->p_sys;
219 date_Set( &p_sys->date, VLC_TICK_INVALID );
220 FlushBuffer( p_sys, SIZE_MAX );
223 /*****************************************************************************
224 * DecodeBlock:
225 *****************************************************************************/
226 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
228 decoder_sys_t *p_sys = p_dec->p_sys;
230 if( !p_block ) /* No Drain */
231 return VLCDEC_SUCCESS;
233 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
235 Flush( p_dec );
236 if( p_block->i_flags & (BLOCK_FLAG_CORRUPTED) )
238 block_Release( p_block );
239 return VLCDEC_SUCCESS;
243 /* Remove ADTS header if we have decoder specific config */
244 if( p_dec->fmt_in.i_extra && p_block->i_buffer > 7 )
246 if( p_block->p_buffer[0] == 0xff &&
247 ( p_block->p_buffer[1] & 0xf0 ) == 0xf0 ) /* syncword */
248 { /* ADTS header present */
249 size_t i_header_size; /* 7 bytes (+ 2 bytes for crc) */
250 i_header_size = 7 + ( ( p_block->p_buffer[1] & 0x01 ) ? 0 : 2 );
251 /* FIXME: multiple blocks per frame */
252 if( p_block->i_buffer > i_header_size )
254 p_block->p_buffer += i_header_size;
255 p_block->i_buffer -= i_header_size;
260 const vlc_tick_t i_pts = p_block->i_pts;
262 /* Append block as temporary buffer */
263 if( p_sys->p_block == NULL )
265 p_sys->p_block = p_block;
267 else
269 p_sys->p_block->p_next = p_block;
270 block_t *p_prev = p_sys->p_block;
271 p_sys->p_block = block_ChainGather( p_sys->p_block );
272 if( p_sys->p_block == NULL )
273 block_ChainRelease( p_prev );
276 /* !Warn: do not use p_block beyond this point */
278 if( p_dec->fmt_out.audio.i_rate == 0 )
280 unsigned long i_rate = 0;
281 unsigned char i_channels;
283 /* Init from DecoderConfig */
284 if( p_dec->fmt_in.i_extra > 0 &&
285 NeAACDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
286 p_dec->fmt_in.i_extra, &i_rate, &i_channels ) != 0 )
288 /* Failed, will try from data */
289 i_rate = 0;
292 if( i_rate == 0 && p_sys->p_block && p_sys->p_block->i_buffer )
294 /* Init faad with the first frame */
295 long i_read = NeAACDecInit( p_sys->hfaad,
296 p_sys->p_block->p_buffer, p_sys->p_block->i_buffer,
297 &i_rate, &i_channels );
298 if( i_read < 0 || (size_t) i_read > p_sys->p_block->i_buffer )
299 i_rate = 0;
300 else
301 FlushBuffer( p_sys, i_read );
304 if( i_rate == 0 )
306 /* Can not init decoder at all for now */
307 FlushBuffer( p_sys, SIZE_MAX );
308 return VLCDEC_SUCCESS;
311 /* Decoder Initialized */
312 p_dec->fmt_out.audio.i_rate = i_rate;
313 p_dec->fmt_out.audio.i_channels = i_channels;
314 p_dec->fmt_out.audio.i_physical_channels
315 = mpeg4_asc_channelsbyindex[i_channels];
316 date_Init( &p_sys->date, i_rate, 1 );
319 if( i_pts != VLC_TICK_INVALID && i_pts != date_Get( &p_sys->date ) )
321 date_Set( &p_sys->date, i_pts );
323 else if( date_Get( &p_sys->date ) == VLC_TICK_INVALID )
325 /* We've just started the stream, wait for the first PTS. */
326 FlushBuffer( p_sys, SIZE_MAX );
327 return VLCDEC_SUCCESS;
330 /* Decode all data */
331 while( p_sys->p_block && p_sys->p_block->i_buffer > 0 )
333 void *samples;
334 NeAACDecFrameInfo frame;
335 block_t *p_out = NULL;
337 samples = NeAACDecDecode( p_sys->hfaad, &frame,
338 p_sys->p_block->p_buffer,
339 p_sys->p_block->i_buffer );
341 if( frame.error > 0 )
343 msg_Warn( p_dec, "%s", NeAACDecGetErrorMessage( frame.error ) );
345 if( frame.error == 21 || frame.error == 12 )
348 * Once an "Unexpected channel configuration change"
349 * or a "Invalid number of channels" error
350 * occurs, it will occurs afterwards, and we got no sound.
351 * Reinitialization of the decoder is required.
353 unsigned long i_rate;
354 unsigned char i_channels;
355 NeAACDecHandle *hfaad;
356 NeAACDecConfiguration *cfg,*oldcfg;
358 oldcfg = NeAACDecGetCurrentConfiguration( p_sys->hfaad );
359 hfaad = NeAACDecOpen();
360 cfg = NeAACDecGetCurrentConfiguration( hfaad );
361 if( oldcfg->defSampleRate )
362 cfg->defSampleRate = oldcfg->defSampleRate;
363 cfg->defObjectType = oldcfg->defObjectType;
364 cfg->outputFormat = oldcfg->outputFormat;
365 NeAACDecSetConfiguration( hfaad, cfg );
367 if( NeAACDecInit( hfaad,
368 p_sys->p_block->p_buffer,
369 p_sys->p_block->i_buffer,
370 &i_rate,&i_channels ) < 0 )
372 /* reinitialization failed */
373 NeAACDecClose( hfaad );
374 NeAACDecSetConfiguration( p_sys->hfaad, oldcfg );
376 else
378 NeAACDecClose( p_sys->hfaad );
379 p_sys->hfaad = hfaad;
380 p_dec->fmt_out.audio.i_rate = i_rate;
381 p_dec->fmt_out.audio.i_channels = i_channels;
382 p_dec->fmt_out.audio.i_physical_channels
383 = mpeg4_asc_channelsbyindex[i_channels];
384 date_Init( &p_sys->date, i_rate, 1 );
388 Flush( p_dec );
389 p_sys->b_discontinuity = true;
391 continue;
394 if( frame.channels == 0 || frame.channels >= 64 )
396 msg_Warn( p_dec, "invalid channels count: %i", frame.channels );
397 if( frame.channels == 0 )
398 p_sys->b_discontinuity = true;
399 FlushBuffer( p_sys, frame.bytesconsumed ? frame.bytesconsumed : SIZE_MAX );
400 continue;
403 if( frame.samples == 0 )
405 msg_Warn( p_dec, "decoded zero sample" );
406 FlushBuffer( p_sys, frame.bytesconsumed ? frame.bytesconsumed : SIZE_MAX );
407 continue;
410 /* We decoded a valid frame */
411 if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
413 date_Init( &p_sys->date, frame.samplerate, 1 );
414 date_Set( &p_sys->date, i_pts );
417 p_dec->fmt_out.audio.i_rate = frame.samplerate;
419 /* Adjust stream info when dealing with SBR/PS */
420 bool b_sbr = (frame.sbr == 1) || (frame.sbr == 2);
421 if( p_sys->b_sbr != b_sbr || p_sys->b_ps != frame.ps )
423 const char *psz_ext = (b_sbr && frame.ps) ? "SBR+PS" :
424 b_sbr ? "SBR" : "PS";
426 msg_Dbg( p_dec, "AAC %s (channels: %u, samplerate: %lu)",
427 psz_ext, frame.channels, frame.samplerate );
429 if( !p_dec->p_description )
430 p_dec->p_description = vlc_meta_New();
431 if( p_dec->p_description )
432 vlc_meta_AddExtra( p_dec->p_description, _("AAC extension"), psz_ext );
434 p_sys->b_sbr = b_sbr;
435 p_sys->b_ps = frame.ps;
438 #ifndef FAAD2_VIDEOLAN_PATCHED
439 /* PS Enabled FAAD PCA bug hotfix (contribs has patch) */
440 if( frame.channels == 8 )
442 const uint8_t psbugconfig[3][8] = { { 2, 3, 2, 3, 2, 3, 6, 7 }, /* fdk 7.1 (4 Front) */
443 { 2, 3, 2, 3, 2, 3, 4, 5 }, /* 7.1 */
444 { 1, 2, 3, 4, 5, 6, 7, 9 } };/* fixed */
445 for( size_t i=0; i<2; i++ )
447 if( !memcmp( frame.channel_position, psbugconfig[i], 8 ) )
449 msg_Warn( p_dec, "Unpatched FAAD2 library with PS Bug. Trying to workaround !" );
450 memcpy( frame.channel_position, psbugconfig[2], 8 );
451 break;
456 /* Hotfix channels misdetection/repetition for FDK 7.1 */
457 struct
459 const uint8_t chans;
460 const uint8_t faulty[8];
461 const uint8_t fixed[8];
462 } const channel_repeat_fixes[] = {
463 { 7, { 2, 3, 2, 3, 2, 3, 6 }, { 1, 2, 3, 6, 7, 8, 9 } }, /* 3F 3R LFE #18273 */
464 { 8, { 1, 2, 3, 6, 7, 6, 7, 9 }, { 1, 2, 3, 6, 7, 4, 5, 9 } }, /* FDK encoded 7.1 */
467 for( size_t i=0; i<ARRAY_SIZE(channel_repeat_fixes); i++ )
469 if( channel_repeat_fixes[i].chans == frame.channels &&
470 !memcmp( frame.channel_position, channel_repeat_fixes[i].faulty,
471 channel_repeat_fixes[i].chans ) )
473 msg_Warn( p_dec, "Patching for Front channel repeat bug" );
474 memcpy( frame.channel_position, channel_repeat_fixes[i].fixed,
475 channel_repeat_fixes[i].chans );
476 break;
479 #endif
480 /* Handle > 1 local pair 5.1 setups.
481 In case of more than 1 channel pair per area, faad will have repeats
482 in channels sequence. We need to remap to available surround channels.
483 Front > Middle > Rear:
484 In case of 4 middle, it maps to 2F 2M if no previous front.
485 In case of 4 rear, it maps to 2M 2R if no previous rear.
487 unsigned i_faadused = 0;
488 for( unsigned i=0; i<frame.channels; i++ )
489 if( frame.channel_position[i] > 0 )
490 i_faadused |= 1 << frame.channel_position[i];
492 for( size_t i=3; i<frame.channels; i++ )
494 if( frame.channel_position[i - 3] == frame.channel_position[i - 1] &&
495 frame.channel_position[i - 2] == frame.channel_position[i] &&
496 frame.channel_position[i - 1] >= SIDE_CHANNEL_LEFT &&
497 frame.channel_position[i - 1] <= BACK_CHANNEL_CENTER &&
498 frame.channel_position[i - 1] >= SIDE_CHANNEL_LEFT &&
499 frame.channel_position[i - 1] <= BACK_CHANNEL_CENTER )
501 if( ( (1 << (frame.channel_position[i - 3] - 2)) & i_faadused ) == 0 &&
502 ( (1 << (frame.channel_position[i - 2] - 2)) & i_faadused ) == 0 )
504 frame.channel_position[i - 3] -= 2;
505 frame.channel_position[i - 2] -= 2;
506 i_faadused |= 1 << frame.channel_position[i - 3];
507 i_faadused |= 1 << frame.channel_position[i - 2];
512 /* Convert frame.channel_position to our own channel values */
513 p_dec->fmt_out.audio.i_physical_channels = 0;
515 uint8_t pi_neworder_table[AOUT_CHAN_MAX];
516 uint32_t pi_faad_channels_positions[FAAD_CHANNEL_ID_COUNT + 1] = {0};
518 bool b_reorder = false;
519 if (p_dec->fmt_out.audio.channel_type == AUDIO_CHANNEL_TYPE_BITMAP)
521 for( size_t i = 0; i < frame.channels && i < FAAD_CHANNEL_ID_COUNT; i++ )
523 unsigned pos = frame.channel_position[i];
524 if( likely(pos < FAAD_CHANNEL_ID_COUNT) )
526 pi_faad_channels_positions[i] = pi_tovlcmapping[pos];
527 p_dec->fmt_out.audio.i_physical_channels |= pi_faad_channels_positions[i];
529 else pi_faad_channels_positions[i] = 0;
532 else if (p_dec->fmt_out.audio.channel_type == AUDIO_CHANNEL_TYPE_AMBISONICS
533 && frame.channels == 4)
535 pi_faad_channels_positions[0] = AOUT_CHAN_REARCENTER;
536 pi_faad_channels_positions[1] = AOUT_CHAN_LEFT;
537 pi_faad_channels_positions[2] = AOUT_CHAN_RIGHT;
538 pi_faad_channels_positions[3] = AOUT_CHAN_CENTER;
539 p_dec->fmt_out.audio.i_physical_channels =
540 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT
541 | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
544 b_reorder = aout_CheckChannelReorder( pi_faad_channels_positions, NULL,
545 p_dec->fmt_out.audio.i_physical_channels, pi_neworder_table );
547 p_dec->fmt_out.audio.i_channels = vlc_popcount(p_dec->fmt_out.audio.i_physical_channels);
549 if( !decoder_UpdateAudioFormat( p_dec ) && p_dec->fmt_out.audio.i_channels > 0 )
550 p_out = decoder_NewAudioBuffer( p_dec, frame.samples / p_dec->fmt_out.audio.i_channels );
552 if( p_out )
554 p_out->i_pts = date_Get( &p_sys->date );
555 p_out->i_length = date_Increment( &p_sys->date,
556 frame.samples / frame.channels )
557 - p_out->i_pts;
559 if ( p_dec->fmt_out.audio.channel_type == AUDIO_CHANNEL_TYPE_BITMAP )
561 /* Don't kill speakers if some weird mapping does not gets 1:1 */
562 if( vlc_popcount(p_dec->fmt_out.audio.i_physical_channels) != frame.channels )
563 memset( p_out->p_buffer, 0, p_out->i_buffer );
566 /* FIXME: replace when aout_channel_reorder can take samples from a different buffer */
567 if( b_reorder )
568 DoReordering( (uint32_t *)p_out->p_buffer, samples,
569 frame.samples / frame.channels, frame.channels,
570 pi_neworder_table );
571 else
572 memcpy( p_out->p_buffer, samples, p_out->i_buffer );
574 if( p_sys->b_discontinuity )
576 p_out->i_flags |= BLOCK_FLAG_DISCONTINUITY;
577 p_sys->b_discontinuity = false;
580 decoder_QueueAudio( p_dec, p_out );
582 else
584 date_Increment( &p_sys->date, frame.samples / frame.channels );
587 FlushBuffer( p_sys, frame.bytesconsumed ? frame.bytesconsumed : SIZE_MAX );
589 if( p_sys->p_block && p_sys->p_block->i_buffer == 1 )
591 /* Drop byte of padding */
592 FlushBuffer( p_sys, 0 );
595 continue;
598 return VLCDEC_SUCCESS;
601 /*****************************************************************************
602 * Close:
603 *****************************************************************************/
604 static void Close( vlc_object_t *p_this )
606 decoder_t *p_dec = (decoder_t *)p_this;
607 decoder_sys_t *p_sys = p_dec->p_sys;
609 NeAACDecClose( p_sys->hfaad );
610 FlushBuffer( p_sys, SIZE_MAX );
611 free( p_sys );
614 /*****************************************************************************
615 * DoReordering: do some channel re-ordering (the ac3 channel order is
616 * different from the aac one).
617 *****************************************************************************/
618 static void DoReordering( uint32_t *p_out, uint32_t *p_in, int i_samples,
619 int i_nb_channels, uint8_t *pi_chan_positions )
621 #if HAVE_FPU
622 #define CAST_SAMPLE(a) a
623 #else
624 #define CAST_SAMPLE(a) ((uint16_t *)a)
625 #endif
626 /* Do the actual reordering */
627 for( int i = 0; i < i_samples; i++ )
629 for( int j = 0; j < i_nb_channels; j++ )
631 CAST_SAMPLE(p_out)[i * i_nb_channels + pi_chan_positions[j]] =
632 CAST_SAMPLE(p_in)[i * i_nb_channels + j];