5df25ea17bfcd6d7a692b35fb4970a28b6c335c2
[vlc.git] / modules / codec / faad.c
1 /*****************************************************************************
2  * faad.c: AAC decoder using libfaad2
3  *****************************************************************************
4  * Copyright (C) 2001, 2003 VLC authors and VideoLAN
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * NOTA BENE: this module requires the linking against a library which is
26  * known to require licensing under the GNU General Public License version 2
27  * (or later). Therefore, the result of compiling this module will normally
28  * be subject to the terms of that later license.
29  *****************************************************************************/
30
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_input_item.h>
39 #include <vlc_codec.h>
40 #include <vlc_cpu.h>
41 #include <vlc_aout.h>
42
43 #include <neaacdec.h>
44 #include "../packetizer/mpeg4audio.h"
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 static int  Open( vlc_object_t * );
50 static void Close( vlc_object_t * );
51
52 vlc_module_begin ()
53     set_description( N_("AAC audio decoder (using libfaad2)") )
54     set_capability( "audio decoder", 100 )
55     set_category( CAT_INPUT )
56     set_subcategory( SUBCAT_INPUT_ACODEC )
57     set_callbacks( Open, Close )
58 vlc_module_end ()
59
60 /****************************************************************************
61  * Local prototypes
62  ****************************************************************************/
63 static int DecodeBlock( decoder_t *, block_t * );
64 static void Flush( decoder_t * );
65 static void DoReordering( uint32_t *, uint32_t *, int, int, uint8_t * );
66
67 typedef struct
68 {
69     /* faad handler */
70     NeAACDecHandle *hfaad;
71
72     /* samples */
73     date_t date;
74     vlc_tick_t i_last_length;
75
76     /* temporary buffer */
77     block_t *p_block;
78
79     /* Channel positions of the current stream (for re-ordering) */
80     uint32_t pi_channel_positions[MPEG4_ASC_MAX_INDEXEDPOS];
81
82     bool b_sbr, b_ps, b_discontinuity;
83 } decoder_sys_t;
84
85 #if MPEG4_ASC_MAX_INDEXEDPOS != LFE_CHANNEL
86     #error MPEG4_ASC_MAX_INDEXEDPOS != LFE_CHANNEL
87 #endif
88
89 #define FAAD_CHANNEL_ID_COUNT (LFE_CHANNEL + 1)
90 static const uint32_t pi_tovlcmapping[FAAD_CHANNEL_ID_COUNT] =
91 {
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
102 };
103
104 /*****************************************************************************
105  * OpenDecoder: probe the decoder and return score
106  *****************************************************************************/
107 static int Open( vlc_object_t *p_this )
108 {
109     decoder_t *p_dec = (decoder_t*)p_this;
110     decoder_sys_t *p_sys;
111     NeAACDecConfiguration *cfg;
112
113     if( p_dec->fmt_in.i_codec != VLC_CODEC_MP4A ||
114         p_dec->fmt_in.i_profile == AAC_PROFILE_ELD ||
115         (p_dec->fmt_in.i_extra > 1 &&
116          (GetWBE(p_dec->fmt_in.p_extra) & 0xffe0) == 0xf8e0)) /* ELD AOT */
117     {
118         return VLC_EGENERIC;
119     }
120
121     /* Allocate the memory needed to store the decoder's structure */
122     if( ( p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) ) ) == NULL )
123         return VLC_ENOMEM;
124
125     /* Open a faad context */
126     if( ( p_sys->hfaad = NeAACDecOpen() ) == NULL )
127     {
128         msg_Err( p_dec, "cannot initialize faad" );
129         free( p_sys );
130         return VLC_EGENERIC;
131     }
132
133     /* Misc init */
134     p_dec->fmt_out.audio.channel_type = p_dec->fmt_in.audio.channel_type;
135
136     if( p_dec->fmt_in.i_extra > 0 )
137     {
138         /* We have a decoder config so init the handle */
139         unsigned long i_rate;
140         unsigned char i_channels;
141
142         if( NeAACDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
143                            p_dec->fmt_in.i_extra,
144                            &i_rate, &i_channels ) < 0 ||
145                 i_channels >= MPEG4_ASC_MAX_INDEXEDPOS )
146         {
147             msg_Err( p_dec, "Failed to initialize faad using extra data" );
148             NeAACDecClose( p_sys->hfaad );
149             free( p_sys );
150             return VLC_EGENERIC;
151         }
152
153         p_dec->fmt_out.audio.i_rate = i_rate;
154         p_dec->fmt_out.audio.i_channels = i_channels;
155         p_dec->fmt_out.audio.i_physical_channels
156             = mpeg4_asc_channelsbyindex[i_channels];
157         date_Init( &p_sys->date, i_rate, 1 );
158     }
159     else
160     {
161         p_dec->fmt_out.audio.i_physical_channels = 0;
162         /* Will be initalised from first frame */
163         p_dec->fmt_out.audio.i_rate = 0;
164         p_dec->fmt_out.audio.i_channels = 0;
165         date_Set( &p_sys->date, VLC_TICK_INVALID );
166     }
167
168     p_dec->fmt_out.i_codec = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
169     p_dec->fmt_out.audio.i_chan_mode = p_dec->fmt_in.audio.i_chan_mode;
170
171     /* Set the faad config */
172     cfg = NeAACDecGetCurrentConfiguration( p_sys->hfaad );
173     if( p_dec->fmt_in.audio.i_rate )
174         cfg->defSampleRate = p_dec->fmt_in.audio.i_rate;
175     cfg->outputFormat = HAVE_FPU ? FAAD_FMT_FLOAT : FAAD_FMT_16BIT;
176     NeAACDecSetConfiguration( p_sys->hfaad, cfg );
177
178     p_sys->i_last_length = 0;
179
180     /* buffer */
181     p_sys->p_block = NULL;
182
183     p_sys->b_discontinuity =
184     p_sys->b_sbr = p_sys->b_ps = false;
185
186     p_dec->pf_decode = DecodeBlock;
187     p_dec->pf_flush  = Flush;
188     return VLC_SUCCESS;
189 }
190
191 /*****************************************************************************
192  * FlushBuffer:
193  *****************************************************************************/
194 static void FlushBuffer( decoder_sys_t *p_sys, size_t i_used )
195 {
196     block_t *p_block = p_sys->p_block;
197     if( p_block )
198     {
199         if( i_used < p_block->i_buffer )
200         {
201             /* Drop padding */
202             for( ; i_used < p_block->i_buffer; i_used++ )
203                 if( p_block->p_buffer[i_used] != 0x00 )
204                     break;
205
206             p_block->i_buffer -= i_used;
207             p_block->p_buffer += i_used;
208         }
209         else p_block->i_buffer = 0;
210         if( p_block->i_buffer == 0 )
211         {
212             block_Release( p_block );
213             p_sys->p_block = NULL;
214         }
215     }
216 }
217
218 /*****************************************************************************
219  * Flush:
220  *****************************************************************************/
221 static void Flush( decoder_t *p_dec )
222 {
223     decoder_sys_t *p_sys = p_dec->p_sys;
224
225     date_Set( &p_sys->date, VLC_TICK_INVALID );
226     FlushBuffer( p_sys, SIZE_MAX );
227 }
228
229 /*****************************************************************************
230  * DecodeBlock:
231  *****************************************************************************/
232 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
233 {
234     decoder_sys_t *p_sys = p_dec->p_sys;
235
236     if( !p_block ) /* No Drain */
237         return VLCDEC_SUCCESS;
238
239     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
240     {
241         Flush( p_dec );
242         if( p_block->i_flags & (BLOCK_FLAG_CORRUPTED) )
243         {
244             block_Release( p_block );
245             return VLCDEC_SUCCESS;
246         }
247     }
248
249     /* Remove ADTS header if we have decoder specific config */
250     if( p_dec->fmt_in.i_extra && p_block->i_buffer > 7 )
251     {
252         if( p_block->p_buffer[0] == 0xff &&
253             ( p_block->p_buffer[1] & 0xf0 ) == 0xf0 ) /* syncword */
254         {   /* ADTS header present */
255             size_t i_header_size; /* 7 bytes (+ 2 bytes for crc) */
256             i_header_size = 7 + ( ( p_block->p_buffer[1] & 0x01 ) ? 0 : 2 );
257             /* FIXME: multiple blocks per frame */
258             if( p_block->i_buffer > i_header_size )
259             {
260                 p_block->p_buffer += i_header_size;
261                 p_block->i_buffer -= i_header_size;
262             }
263         }
264     }
265
266     const vlc_tick_t i_pts = p_block->i_pts;
267
268     /* Append block as temporary buffer */
269     if( p_sys->p_block == NULL )
270     {
271         p_sys->p_block = p_block;
272     }
273     else
274     {
275         p_sys->p_block->p_next = p_block;
276         block_t *p_prev = p_sys->p_block;
277         p_sys->p_block = block_ChainGather( p_sys->p_block );
278         if( p_sys->p_block == NULL )
279             block_ChainRelease( p_prev );
280     }
281
282     /* !Warn: do not use p_block beyond this point */
283
284     if( p_dec->fmt_out.audio.i_rate == 0 )
285     {
286         unsigned long i_rate = 0;
287         unsigned char i_channels;
288
289         /* Init from DecoderConfig */
290         if( p_dec->fmt_in.i_extra > 0 &&
291             NeAACDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
292                            p_dec->fmt_in.i_extra, &i_rate, &i_channels ) != 0 )
293         {
294             /* Failed, will try from data */
295             i_rate = 0;
296         }
297
298         if( i_rate == 0 && p_sys->p_block && p_sys->p_block->i_buffer )
299         {
300             /* Init faad with the first frame */
301             long i_read = NeAACDecInit( p_sys->hfaad,
302                                         p_sys->p_block->p_buffer, p_sys->p_block->i_buffer,
303                                         &i_rate, &i_channels );
304             if( i_read < 0 || (size_t) i_read > p_sys->p_block->i_buffer )
305                 i_rate = 0;
306             else
307                 FlushBuffer( p_sys, i_read );
308         }
309
310         if( i_rate == 0 )
311         {
312             /* Can not init decoder at all for now */
313             FlushBuffer( p_sys, SIZE_MAX );
314             return VLCDEC_SUCCESS;
315         }
316
317         /* Decoder Initialized */
318         p_dec->fmt_out.audio.i_rate = i_rate;
319         p_dec->fmt_out.audio.i_channels = i_channels;
320         p_dec->fmt_out.audio.i_physical_channels
321             = mpeg4_asc_channelsbyindex[i_channels];
322         date_Init( &p_sys->date, i_rate, 1 );
323     }
324
325     if( i_pts != VLC_TICK_INVALID && i_pts != date_Get( &p_sys->date ) )
326     {
327         if( p_sys->i_last_length == 0 ||
328             /* We need to be permissive and rebase dts when it's really way off */
329             llabs( i_pts - date_Get( &p_sys->date ) ) > p_sys->i_last_length * 3 / 2  )
330             date_Set( &p_sys->date, i_pts );
331     }
332     else if( date_Get( &p_sys->date ) == VLC_TICK_INVALID )
333     {
334         /* We've just started the stream, wait for the first PTS. */
335         FlushBuffer( p_sys, SIZE_MAX );
336         return VLCDEC_SUCCESS;
337     }
338
339     /* Decode all data */
340     while( p_sys->p_block && p_sys->p_block->i_buffer > 0 )
341     {
342         void *samples;
343         NeAACDecFrameInfo frame;
344         block_t *p_out = NULL;
345
346         samples = NeAACDecDecode( p_sys->hfaad, &frame,
347                                   p_sys->p_block->p_buffer,
348                                   p_sys->p_block->i_buffer );
349
350         if( frame.error > 0 )
351         {
352             msg_Warn( p_dec, "%s", NeAACDecGetErrorMessage( frame.error ) );
353
354             if( frame.error == 21 || frame.error == 12 )
355             {
356                 /*
357                  * Once an "Unexpected channel configuration change"
358                  * or a "Invalid number of channels" error
359                  * occurs, it will occurs afterwards, and we got no sound.
360                  * Reinitialization of the decoder is required.
361                  */
362                 unsigned long i_rate;
363                 unsigned char i_channels;
364                 NeAACDecHandle *hfaad;
365                 NeAACDecConfiguration *cfg,*oldcfg;
366
367                 oldcfg = NeAACDecGetCurrentConfiguration( p_sys->hfaad );
368                 hfaad = NeAACDecOpen();
369                 cfg = NeAACDecGetCurrentConfiguration( hfaad );
370                 if( oldcfg->defSampleRate )
371                     cfg->defSampleRate = oldcfg->defSampleRate;
372                 cfg->defObjectType = oldcfg->defObjectType;
373                 cfg->outputFormat = oldcfg->outputFormat;
374                 NeAACDecSetConfiguration( hfaad, cfg );
375
376                 if( NeAACDecInit( hfaad,
377                                   p_sys->p_block->p_buffer,
378                                   p_sys->p_block->i_buffer,
379                                   &i_rate,&i_channels ) < 0 )
380                 {
381                     /* reinitialization failed */
382                     NeAACDecClose( hfaad );
383                     NeAACDecSetConfiguration( p_sys->hfaad, oldcfg );
384                 }
385                 else
386                 {
387                     NeAACDecClose( p_sys->hfaad );
388                     p_sys->hfaad = hfaad;
389                     p_dec->fmt_out.audio.i_rate = i_rate;
390                     p_dec->fmt_out.audio.i_channels = i_channels;
391                     p_dec->fmt_out.audio.i_physical_channels
392                         = mpeg4_asc_channelsbyindex[i_channels];
393                     date_Init( &p_sys->date, i_rate, 1 );
394                 }
395             }
396
397             Flush( p_dec );
398             p_sys->b_discontinuity = true;
399
400             continue;
401         }
402
403         if( frame.channels == 0 || frame.channels >= 64 )
404         {
405             msg_Warn( p_dec, "invalid channels count: %i", frame.channels );
406             if( frame.channels == 0 )
407                 p_sys->b_discontinuity = true;
408             FlushBuffer( p_sys, frame.bytesconsumed ? frame.bytesconsumed : SIZE_MAX );
409             continue;
410         }
411
412         if( frame.samples == 0 )
413         {
414             msg_Warn( p_dec, "decoded zero sample" );
415             FlushBuffer( p_sys, frame.bytesconsumed ? frame.bytesconsumed : SIZE_MAX );
416             continue;
417         }
418
419         /* We decoded a valid frame */
420         if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
421         {
422             date_Init( &p_sys->date, frame.samplerate, 1 );
423             date_Set( &p_sys->date, i_pts );
424         }
425
426         p_dec->fmt_out.audio.i_rate = frame.samplerate;
427
428         /* Adjust stream info when dealing with SBR/PS */
429         bool b_sbr = (frame.sbr == 1) || (frame.sbr == 2);
430         if( p_sys->b_sbr != b_sbr || p_sys->b_ps != frame.ps )
431         {
432             const char *psz_ext = (b_sbr && frame.ps) ? "SBR+PS" :
433                                     b_sbr ? "SBR" : "PS";
434
435             msg_Dbg( p_dec, "AAC %s (channels: %u, samplerate: %lu)",
436                     psz_ext, frame.channels, frame.samplerate );
437
438             if( !p_dec->p_description )
439                 p_dec->p_description = vlc_meta_New();
440             if( p_dec->p_description )
441                 vlc_meta_AddExtra( p_dec->p_description, _("AAC extension"), psz_ext );
442
443             p_sys->b_sbr = b_sbr;
444             p_sys->b_ps = frame.ps;
445         }
446
447 #ifndef FAAD2_VIDEOLAN_PATCHED
448         /* PS Enabled FAAD PCA bug hotfix (contribs has patch) */
449         if( frame.channels == 8 )
450         {
451             const uint8_t psbugconfig[3][8] = { { 2, 3, 2, 3, 2, 3, 6, 7 },  /* fdk 7.1 (4 Front) */
452                                                 { 2, 3, 2, 3, 2, 3, 4, 5 },  /* 7.1 */
453                                                 { 1, 2, 3, 4, 5, 6, 7, 9 } };/* fixed */
454             for( size_t i=0; i<2; i++ )
455             {
456                 if( !memcmp( frame.channel_position, psbugconfig[i], 8 ) )
457                 {
458                     msg_Warn( p_dec, "Unpatched FAAD2 library with PS Bug. Trying to workaround !" );
459                     memcpy( frame.channel_position, psbugconfig[2], 8 );
460                     break;
461                 }
462             }
463         }
464
465         /* Hotfix channels misdetection/repetition for FDK 7.1 */
466         struct
467         {
468             const uint8_t chans;
469             const uint8_t faulty[8];
470             const uint8_t fixed[8];
471         } const channel_repeat_fixes[] = {
472             { 7, { 2, 3, 2, 3, 2, 3, 6 },    { 1, 2, 3, 6, 7, 8, 9 }    }, /* 3F 3R LFE #18273 */
473             { 8, { 1, 2, 3, 6, 7, 6, 7, 9 }, { 1, 2, 3, 6, 7, 4, 5, 9 } }, /* FDK encoded 7.1 */
474         };
475
476         for( size_t i=0; i<ARRAY_SIZE(channel_repeat_fixes); i++ )
477         {
478             if( channel_repeat_fixes[i].chans == frame.channels &&
479                 !memcmp( frame.channel_position, channel_repeat_fixes[i].faulty,
480                                                  channel_repeat_fixes[i].chans ) )
481             {
482                 msg_Warn( p_dec, "Patching for Front channel repeat bug" );
483                 memcpy( frame.channel_position, channel_repeat_fixes[i].fixed,
484                                                 channel_repeat_fixes[i].chans );
485                 break;
486             }
487         }
488 #endif
489         /* Handle > 1 local pair 5.1 setups.
490            In case of more than 1 channel pair per area, faad will have repeats
491            in channels sequence. We need to remap to available surround channels.
492            Front > Middle > Rear:
493            In case of 4 middle, it maps to 2F 2M if no previous front.
494            In case of 4 rear, it maps to 2M 2R if no previous rear.
495         */
496         unsigned i_faadused = 0;
497         for( unsigned i=0; i<frame.channels; i++ )
498             if( frame.channel_position[i] > 0 )
499                 i_faadused |= 1 << frame.channel_position[i];
500
501         for( size_t i=3; i<frame.channels; i++ )
502         {
503              if( frame.channel_position[i - 3] == frame.channel_position[i - 1] &&
504                  frame.channel_position[i - 2] == frame.channel_position[i] &&
505                  frame.channel_position[i - 1] >= SIDE_CHANNEL_LEFT &&
506                  frame.channel_position[i - 1] <= BACK_CHANNEL_CENTER &&
507                  frame.channel_position[i - 1] >= SIDE_CHANNEL_LEFT &&
508                  frame.channel_position[i - 1] <= BACK_CHANNEL_CENTER )
509              {
510                 if( ( (1 << (frame.channel_position[i - 3] - 2)) & i_faadused ) == 0 &&
511                     ( (1 << (frame.channel_position[i - 2] - 2)) & i_faadused ) == 0 )
512                 {
513                     frame.channel_position[i - 3] -= 2;
514                     frame.channel_position[i - 2] -= 2;
515                     i_faadused |= 1 << frame.channel_position[i - 3];
516                     i_faadused |= 1 << frame.channel_position[i - 2];
517                 }
518              }
519         }
520
521         /* Convert frame.channel_position to our own channel values */
522         p_dec->fmt_out.audio.i_physical_channels = 0;
523
524         uint8_t  pi_neworder_table[64] = {0};
525         uint32_t pi_faad_channels_positions[64 + 1] = {0};
526
527         bool b_reorder = false;
528         if (p_dec->fmt_out.audio.channel_type == AUDIO_CHANNEL_TYPE_BITMAP)
529         {
530             for( size_t i = 0; i < frame.channels; i++ )
531             {
532                 unsigned pos = frame.channel_position[i];
533                 if( likely(pos < FAAD_CHANNEL_ID_COUNT) )
534                 {
535                     pi_faad_channels_positions[i] = pi_tovlcmapping[pos];
536                     p_dec->fmt_out.audio.i_physical_channels |= pi_faad_channels_positions[i];
537                 }
538                 else pi_faad_channels_positions[i] = 0;
539             }
540         }
541         else if (p_dec->fmt_out.audio.channel_type == AUDIO_CHANNEL_TYPE_AMBISONICS
542             && frame.channels == 4)
543         {
544             pi_faad_channels_positions[0] = AOUT_CHAN_REARCENTER;
545             pi_faad_channels_positions[1] = AOUT_CHAN_LEFT;
546             pi_faad_channels_positions[2] = AOUT_CHAN_RIGHT;
547             pi_faad_channels_positions[3] = AOUT_CHAN_CENTER;
548             p_dec->fmt_out.audio.i_physical_channels =
549                 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT
550                 | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
551         }
552
553         b_reorder = aout_CheckChannelReorder( pi_faad_channels_positions, NULL,
554             p_dec->fmt_out.audio.i_physical_channels, pi_neworder_table );
555
556         p_dec->fmt_out.audio.i_channels = vlc_popcount(p_dec->fmt_out.audio.i_physical_channels);
557
558         if( !decoder_UpdateAudioFormat( p_dec ) && p_dec->fmt_out.audio.i_channels > 0 )
559             p_out = decoder_NewAudioBuffer( p_dec, frame.samples / p_dec->fmt_out.audio.i_channels );
560
561         if( p_out )
562         {
563             p_out->i_pts = date_Get( &p_sys->date );
564             p_out->i_length = date_Increment( &p_sys->date,
565                                               frame.samples / frame.channels )
566                               - p_out->i_pts;
567             p_sys->i_last_length = p_out->i_length;
568             if ( p_dec->fmt_out.audio.channel_type == AUDIO_CHANNEL_TYPE_BITMAP )
569             {
570                 /* Don't kill speakers if some weird mapping does not gets 1:1 */
571                 if( vlc_popcount(p_dec->fmt_out.audio.i_physical_channels) != frame.channels )
572                     memset( p_out->p_buffer, 0, p_out->i_buffer );
573             }
574
575             /* FIXME: replace when aout_channel_reorder can take samples from a different buffer */
576             if( b_reorder )
577                 DoReordering( (uint32_t *)p_out->p_buffer, samples,
578                               frame.samples / frame.channels, frame.channels,
579                               pi_neworder_table );
580             else
581                  memcpy( p_out->p_buffer, samples, p_out->i_buffer );
582
583             if( p_sys->b_discontinuity )
584             {
585                 p_out->i_flags |= BLOCK_FLAG_DISCONTINUITY;
586                 p_sys->b_discontinuity = false;
587             }
588
589             decoder_QueueAudio( p_dec, p_out );
590         }
591         else
592         {
593             date_Increment( &p_sys->date, frame.samples / frame.channels );
594         }
595
596         FlushBuffer( p_sys, frame.bytesconsumed ? frame.bytesconsumed : SIZE_MAX );
597
598         if( p_sys->p_block && p_sys->p_block->i_buffer == 1 )
599         {
600             /* Drop byte of padding */
601             FlushBuffer( p_sys, 0 );
602         }
603     }
604
605     return VLCDEC_SUCCESS;
606 }
607
608 /*****************************************************************************
609  * Close:
610  *****************************************************************************/
611 static void Close( vlc_object_t *p_this )
612 {
613     decoder_t *p_dec = (decoder_t *)p_this;
614     decoder_sys_t *p_sys = p_dec->p_sys;
615
616     NeAACDecClose( p_sys->hfaad );
617     FlushBuffer( p_sys, SIZE_MAX );
618     free( p_sys );
619 }
620
621 /*****************************************************************************
622  * DoReordering: do some channel re-ordering (the ac3 channel order is
623  *   different from the aac one).
624  *****************************************************************************/
625 static void DoReordering( uint32_t *p_out, uint32_t *p_in, int i_samples,
626                           int i_nb_channels, uint8_t *pi_chan_positions )
627 {
628 #if HAVE_FPU
629     #define CAST_SAMPLE(a) a
630 #else
631     #define CAST_SAMPLE(a) ((uint16_t *)a)
632 #endif
633     /* Do the actual reordering */
634     for( int i = 0; i < i_samples; i++ )
635     {
636         for( int j = 0; j < i_nb_channels; j++ )
637         {
638             CAST_SAMPLE(p_out)[i * i_nb_channels + pi_chan_positions[j]] =
639                 CAST_SAMPLE(p_in)[i * i_nb_channels + j];
640         }
641     }
642 }
643