1 /*****************************************************************************
2 * faad.c: AAC decoder using libfaad2
3 *****************************************************************************
4 * Copyright (C) 2001, 2003 VLC authors and VideoLAN
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 *****************************************************************************/
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_input.h>
40 #include <vlc_codec.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open( vlc_object_t * );
49 static void Close( vlc_object_t * );
52 set_description( N_("AAC audio decoder (using libfaad2)") )
53 set_capability( "decoder", 100 )
54 set_category( CAT_INPUT )
55 set_subcategory( SUBCAT_INPUT_ACODEC )
56 set_callbacks( Open, Close )
59 /****************************************************************************
61 ****************************************************************************/
62 static block_t *DecodeBlock( decoder_t *, block_t ** );
63 static void DoReordering( uint32_t *, uint32_t *, int, int, uint32_t * );
65 #define MAX_CHANNEL_POSITIONS 9
75 /* temporary buffer */
80 /* Channel positions of the current stream (for re-ordering) */
81 uint32_t pi_channel_positions[MAX_CHANNEL_POSITIONS];
86 static const uint32_t pi_channels_in[MAX_CHANNEL_POSITIONS] =
87 { FRONT_CHANNEL_CENTER, FRONT_CHANNEL_LEFT, FRONT_CHANNEL_RIGHT,
88 SIDE_CHANNEL_LEFT, SIDE_CHANNEL_RIGHT,
89 BACK_CHANNEL_LEFT, BACK_CHANNEL_RIGHT,
90 BACK_CHANNEL_CENTER, LFE_CHANNEL };
91 static const uint32_t pi_channels_out[MAX_CHANNEL_POSITIONS] =
92 { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
93 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
94 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
95 AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE };
96 static const uint32_t pi_channels_ordered[MAX_CHANNEL_POSITIONS] =
97 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
98 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
99 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
100 AOUT_CHAN_CENTER, AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE
102 static const uint32_t pi_channels_guessed[MAX_CHANNEL_POSITIONS] =
103 { 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
104 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE,
105 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
106 | AOUT_CHAN_REARRIGHT,
107 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
108 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_CENTER,
109 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
110 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE,
111 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_MIDDLELEFT
112 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
114 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_MIDDLELEFT
115 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
116 | AOUT_CHAN_CENTER | AOUT_CHAN_LFE
119 /*****************************************************************************
120 * OpenDecoder: probe the decoder and return score
121 *****************************************************************************/
122 static int Open( vlc_object_t *p_this )
124 decoder_t *p_dec = (decoder_t*)p_this;
125 decoder_sys_t *p_sys = p_dec->p_sys;
126 faacDecConfiguration *cfg;
128 if( p_dec->fmt_in.i_codec != VLC_CODEC_MP4A )
133 /* Allocate the memory needed to store the decoder's structure */
134 if( ( p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) ) ) == NULL )
137 /* Open a faad context */
138 if( ( p_sys->hfaad = faacDecOpen() ) == NULL )
140 msg_Err( p_dec, "cannot initialize faad" );
146 date_Set( &p_sys->date, 0 );
147 p_dec->fmt_out.i_cat = AUDIO_ES;
149 p_dec->fmt_out.i_codec = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
150 p_dec->pf_decode_audio = DecodeBlock;
152 p_dec->fmt_out.audio.i_physical_channels =
153 p_dec->fmt_out.audio.i_original_channels = 0;
155 if( p_dec->fmt_in.i_extra > 0 )
157 /* We have a decoder config so init the handle */
158 unsigned long i_rate;
159 unsigned char i_channels;
161 if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
162 p_dec->fmt_in.i_extra,
163 &i_rate, &i_channels ) < 0 )
165 msg_Err( p_dec, "Failed to initialize faad using extra data" );
166 faacDecClose( p_sys->hfaad );
171 p_dec->fmt_out.audio.i_rate = i_rate;
172 p_dec->fmt_out.audio.i_channels = i_channels;
173 p_dec->fmt_out.audio.i_physical_channels
174 = p_dec->fmt_out.audio.i_original_channels
175 = pi_channels_guessed[i_channels];
176 date_Init( &p_sys->date, i_rate, 1 );
180 /* Will be initalised from first frame */
181 p_dec->fmt_out.audio.i_rate = 0;
182 p_dec->fmt_out.audio.i_channels = 0;
185 /* Set the faad config */
186 cfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
187 cfg->outputFormat = HAVE_FPU ? FAAD_FMT_FLOAT : FAAD_FMT_16BIT;
188 faacDecSetConfiguration( p_sys->hfaad, cfg );
191 p_sys->i_buffer = p_sys->i_buffer_size = 0;
192 p_sys->p_buffer = NULL;
194 /* Faad2 can't deal with truncated data (eg. from MPEG TS) */
195 p_dec->b_need_packetized = true;
197 p_sys->b_sbr = p_sys->b_ps = false;
201 /*****************************************************************************
203 *****************************************************************************/
204 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
206 decoder_sys_t *p_sys = p_dec->p_sys;
209 if( !pp_block || !*pp_block ) return NULL;
213 if( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
215 block_Release( p_block );
219 /* Remove ADTS header if we have decoder specific config */
220 if( p_dec->fmt_in.i_extra && p_block->i_buffer > 7 )
222 if( p_block->p_buffer[0] == 0xff &&
223 ( p_block->p_buffer[1] & 0xf0 ) == 0xf0 ) /* syncword */
224 { /* ADTS header present */
225 size_t i_header_size; /* 7 bytes (+ 2 bytes for crc) */
226 i_header_size = 7 + ( ( p_block->p_buffer[1] & 0x01 ) ? 0 : 2 );
227 /* FIXME: multiple blocks per frame */
228 if( p_block->i_buffer > i_header_size )
230 p_block->p_buffer += i_header_size;
231 p_block->i_buffer -= i_header_size;
236 /* Append the block to the temporary buffer */
237 if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
239 size_t i_buffer_size = p_sys->i_buffer + p_block->i_buffer;
240 uint8_t *p_buffer = realloc( p_sys->p_buffer, i_buffer_size );
243 p_sys->i_buffer_size = i_buffer_size;
244 p_sys->p_buffer = p_buffer;
248 p_block->i_buffer = 0;
252 if( p_block->i_buffer > 0 )
254 memcpy( &p_sys->p_buffer[p_sys->i_buffer],
255 p_block->p_buffer, p_block->i_buffer );
256 p_sys->i_buffer += p_block->i_buffer;
257 p_block->i_buffer = 0;
260 if( p_dec->fmt_out.audio.i_rate == 0 && p_dec->fmt_in.i_extra > 0 )
262 /* We have a decoder config so init the handle */
263 unsigned long i_rate;
264 unsigned char i_channels;
266 if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
267 p_dec->fmt_in.i_extra,
268 &i_rate, &i_channels ) >= 0 )
270 p_dec->fmt_out.audio.i_rate = i_rate;
271 p_dec->fmt_out.audio.i_channels = i_channels;
272 p_dec->fmt_out.audio.i_physical_channels
273 = p_dec->fmt_out.audio.i_original_channels
274 = pi_channels_guessed[i_channels];
276 date_Init( &p_sys->date, i_rate, 1 );
280 if( p_dec->fmt_out.audio.i_rate == 0 && p_sys->i_buffer )
282 unsigned long i_rate;
283 unsigned char i_channels;
285 /* Init faad with the first frame */
286 if( faacDecInit( p_sys->hfaad,
287 p_sys->p_buffer, p_sys->i_buffer,
288 &i_rate, &i_channels ) < 0 )
290 block_Release( p_block );
294 p_dec->fmt_out.audio.i_rate = i_rate;
295 p_dec->fmt_out.audio.i_channels = i_channels;
296 p_dec->fmt_out.audio.i_physical_channels
297 = p_dec->fmt_out.audio.i_original_channels
298 = pi_channels_guessed[i_channels];
299 date_Init( &p_sys->date, i_rate, 1 );
302 if( p_block->i_pts > VLC_TS_INVALID && p_block->i_pts != date_Get( &p_sys->date ) )
304 date_Set( &p_sys->date, p_block->i_pts );
306 else if( !date_Get( &p_sys->date ) )
308 /* We've just started the stream, wait for the first PTS. */
309 block_Release( p_block );
314 /* Decode all data */
315 if( p_sys->i_buffer )
318 faacDecFrameInfo frame;
321 samples = faacDecDecode( p_sys->hfaad, &frame,
322 p_sys->p_buffer, p_sys->i_buffer );
324 if( frame.error > 0 )
326 msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
328 if( frame.error == 21 || frame.error == 12 )
331 * Once an "Unexpected channel configuration change"
332 * or a "Invalid number of channels" error
333 * occurs, it will occurs afterwards, and we got no sound.
334 * Reinitialization of the decoder is required.
336 unsigned long i_rate;
337 unsigned char i_channels;
338 faacDecHandle *hfaad;
339 faacDecConfiguration *cfg,*oldcfg;
341 oldcfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
342 hfaad = faacDecOpen();
343 cfg = faacDecGetCurrentConfiguration( hfaad );
344 if( oldcfg->defSampleRate )
345 cfg->defSampleRate = oldcfg->defSampleRate;
346 cfg->defObjectType = oldcfg->defObjectType;
347 cfg->outputFormat = oldcfg->outputFormat;
348 faacDecSetConfiguration( hfaad, cfg );
350 if( faacDecInit( hfaad, p_sys->p_buffer, p_sys->i_buffer,
351 &i_rate,&i_channels ) < 0 )
353 /* reinitialization failed */
354 faacDecClose( hfaad );
355 faacDecSetConfiguration( p_sys->hfaad, oldcfg );
359 faacDecClose( p_sys->hfaad );
360 p_sys->hfaad = hfaad;
361 p_dec->fmt_out.audio.i_rate = i_rate;
362 p_dec->fmt_out.audio.i_channels = i_channels;
363 p_dec->fmt_out.audio.i_physical_channels
364 = p_dec->fmt_out.audio.i_original_channels
365 = pi_channels_guessed[i_channels];
366 date_Init( &p_sys->date, i_rate, 1 );
370 /* Flush the buffer */
372 block_Release( p_block );
376 if( frame.channels <= 0 || frame.channels > 8 || frame.channels == 7 )
378 msg_Warn( p_dec, "invalid channels count: %i", frame.channels );
380 /* Flush the buffer */
381 p_sys->i_buffer -= frame.bytesconsumed;
382 if( p_sys->i_buffer > 0 )
384 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
387 block_Release( p_block );
391 if( frame.samples <= 0 )
393 msg_Warn( p_dec, "decoded zero sample" );
395 /* Flush the buffer */
396 p_sys->i_buffer -= frame.bytesconsumed;
397 if( p_sys->i_buffer > 0 )
399 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
402 block_Release( p_block );
406 /* We decoded a valid frame */
407 if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
409 date_Init( &p_sys->date, frame.samplerate, 1 );
410 date_Set( &p_sys->date, p_block->i_pts );
412 p_block->i_pts = VLC_TS_INVALID; /* PTS is valid only once */
414 p_dec->fmt_out.audio.i_rate = frame.samplerate;
415 p_dec->fmt_out.audio.i_channels = frame.channels;
417 /* Adjust stream info when dealing with SBR/PS */
418 bool b_sbr = (frame.sbr == 1) || (frame.sbr == 2);
419 if( p_sys->b_sbr != b_sbr || p_sys->b_ps != frame.ps )
421 const char *psz_ext = (b_sbr && frame.ps) ? "SBR+PS" :
422 b_sbr ? "SBR" : "PS";
424 msg_Dbg( p_dec, "AAC %s (channels: %u, samplerate: %lu)",
425 psz_ext, frame.channels, frame.samplerate );
427 if( !p_dec->p_description )
428 p_dec->p_description = vlc_meta_New();
429 if( p_dec->p_description )
430 vlc_meta_AddExtra( p_dec->p_description, _("AAC extension"), psz_ext );
432 p_sys->b_sbr = b_sbr;
433 p_sys->b_ps = frame.ps;
436 /* Convert frame.channel_position to our own channel values */
437 p_dec->fmt_out.audio.i_physical_channels = 0;
438 const uint32_t nbChannels = frame.channels;
440 for( unsigned i = 0; i < nbChannels; i++ )
442 /* Find the channel code */
443 for( j = 0; j < MAX_CHANNEL_POSITIONS; j++ )
445 if( frame.channel_position[i] == pi_channels_in[j] )
448 if( j >= MAX_CHANNEL_POSITIONS )
450 msg_Warn( p_dec, "unknown channel ordering" );
451 /* Invent something */
455 p_sys->pi_channel_positions[i] = pi_channels_out[j];
456 if( p_dec->fmt_out.audio.i_physical_channels & pi_channels_out[j] )
457 frame.channels--; /* We loose a duplicated channel */
459 p_dec->fmt_out.audio.i_physical_channels |= pi_channels_out[j];
461 if ( nbChannels != frame.channels )
463 p_dec->fmt_out.audio.i_physical_channels
464 = p_dec->fmt_out.audio.i_original_channels
465 = pi_channels_guessed[nbChannels];
469 p_dec->fmt_out.audio.i_original_channels =
470 p_dec->fmt_out.audio.i_physical_channels;
472 p_dec->fmt_out.audio.i_channels = nbChannels;
473 p_out = decoder_NewAudioBuffer( p_dec, frame.samples / nbChannels );
477 block_Release( p_block );
481 p_out->i_pts = date_Get( &p_sys->date );
482 p_out->i_length = date_Increment( &p_sys->date,
483 frame.samples / nbChannels )
486 DoReordering( (uint32_t *)p_out->p_buffer, samples,
487 frame.samples / nbChannels, nbChannels,
488 p_sys->pi_channel_positions );
490 p_sys->i_buffer -= frame.bytesconsumed;
491 if( p_sys->i_buffer > 0 )
493 memmove( p_sys->p_buffer, &p_sys->p_buffer[frame.bytesconsumed],
500 block_Release( p_block );
504 /*****************************************************************************
506 *****************************************************************************/
507 static void Close( vlc_object_t *p_this )
509 decoder_t *p_dec = (decoder_t *)p_this;
510 decoder_sys_t *p_sys = p_dec->p_sys;
512 faacDecClose( p_sys->hfaad );
513 free( p_sys->p_buffer );
517 /*****************************************************************************
518 * DoReordering: do some channel re-ordering (the ac3 channel order is
519 * different from the aac one).
520 *****************************************************************************/
521 static void DoReordering( uint32_t *p_out, uint32_t *p_in, int i_samples,
522 int i_nb_channels, uint32_t *pi_chan_positions )
524 int pi_chan_table[MAX_CHANNEL_POSITIONS] = {0};
527 /* Find the channels mapping */
528 for( i = 0, j = 0; i < MAX_CHANNEL_POSITIONS; i++ )
530 for( k = 0; k < i_nb_channels; k++ )
532 if( pi_channels_ordered[i] == pi_chan_positions[k] )
534 pi_chan_table[k] = j++;
540 /* Do the actual reordering */
542 for( i = 0; i < i_samples; i++ )
543 for( j = 0; j < i_nb_channels; j++ )
544 p_out[i * i_nb_channels + pi_chan_table[j]] =
545 p_in[i * i_nb_channels + j];
547 for( i = 0; i < i_samples; i++ )
548 for( j = 0; j < i_nb_channels; j++ )
549 ((uint16_t *)p_out)[i * i_nb_channels + pi_chan_table[j]] =
550 ((uint16_t *)p_in)[i * i_nb_channels + j];