1 /*****************************************************************************
2  * faad.c: AAC decoder using libfaad2
3  *****************************************************************************
4  * Copyright (C) 2001, 2003 VLC authors and VideoLAN
5  * $Id: 4352b48ea0a16a67fb79035c5d2592a9edf7ac7f $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24 
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  *****************************************************************************/
31 
32 
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36 
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>
43 
44 #include <neaacdec.h>
45 #include "../packetizer/mpeg4audio.h"
46 
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 static int  Open( vlc_object_t * );
51 static void Close( vlc_object_t * );
52 
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 ()
60 
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 * );
67 
68 struct decoder_sys_t
69 {
70     /* faad handler */
71     NeAACDecHandle *hfaad;
72 
73     /* samples */
74     date_t date;
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 };
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  *****************************************************************************/
Open(vlc_object_t * p_this)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     {
115         return VLC_EGENERIC;
116     }
117 
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;
121 
122     /* Open a faad context */
123     if( ( p_sys->hfaad = NeAACDecOpen() ) == NULL )
124     {
125         msg_Err( p_dec, "cannot initialize faad" );
126         free( p_sys );
127         return VLC_EGENERIC;
128     }
129 
130     /* Misc init */
131     date_Set( &p_sys->date, 0 );
132 
133     p_dec->fmt_out.audio.channel_type = p_dec->fmt_in.audio.channel_type;
134 
135     if( p_dec->fmt_in.i_extra > 0 )
136     {
137         /* We have a decoder config so init the handle */
138         unsigned long i_rate;
139         unsigned char i_channels;
140 
141         if( NeAACDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
142                            p_dec->fmt_in.i_extra,
143                            &i_rate, &i_channels ) < 0 ||
144                 i_channels >= MPEG4_ASC_MAX_INDEXEDPOS )
145         {
146             msg_Err( p_dec, "Failed to initialize faad using extra data" );
147             NeAACDecClose( p_sys->hfaad );
148             free( p_sys );
149             return VLC_EGENERIC;
150         }
151 
152         p_dec->fmt_out.audio.i_rate = i_rate;
153         p_dec->fmt_out.audio.i_channels = i_channels;
154         p_dec->fmt_out.audio.i_physical_channels
155             = mpeg4_asc_channelsbyindex[i_channels];
156         date_Init( &p_sys->date, i_rate, 1 );
157     }
158     else
159     {
160         p_dec->fmt_out.audio.i_physical_channels = 0;
161         /* Will be initalised from first frame */
162         p_dec->fmt_out.audio.i_rate = 0;
163         p_dec->fmt_out.audio.i_channels = 0;
164     }
165 
166     p_dec->fmt_out.i_codec = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
167     p_dec->fmt_out.audio.i_chan_mode = p_dec->fmt_in.audio.i_chan_mode;
168 
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 );
175 
176     /* buffer */
177     p_sys->p_block = NULL;
178 
179     p_sys->b_discontinuity =
180     p_sys->b_sbr = p_sys->b_ps = false;
181 
182     p_dec->pf_decode = DecodeBlock;
183     p_dec->pf_flush  = Flush;
184     return VLC_SUCCESS;
185 }
186 
187 /*****************************************************************************
188  * FlushBuffer:
189  *****************************************************************************/
FlushBuffer(decoder_sys_t * p_sys,size_t i_used)190 static void FlushBuffer( decoder_sys_t *p_sys, size_t i_used )
191 {
192     block_t *p_block = p_sys->p_block;
193     if( p_block )
194     {
195         if( i_used < p_block->i_buffer )
196         {
197             /* Drop padding */
198             for( ; i_used < p_block->i_buffer; i_used++ )
199                 if( p_block->p_buffer[i_used] != 0x00 )
200                     break;
201 
202             p_block->i_buffer -= i_used;
203             p_block->p_buffer += i_used;
204         }
205         else p_block->i_buffer = 0;
206         if( p_block->i_buffer == 0 )
207         {
208             block_Release( p_block );
209             p_sys->p_block = NULL;
210         }
211     }
212 }
213 
214 /*****************************************************************************
215  * Flush:
216  *****************************************************************************/
Flush(decoder_t * p_dec)217 static void Flush( decoder_t *p_dec )
218 {
219     decoder_sys_t *p_sys = p_dec->p_sys;
220 
221     date_Set( &p_sys->date, VLC_TS_INVALID );
222     FlushBuffer( p_sys, SIZE_MAX );
223 }
224 
225 /*****************************************************************************
226  * DecodeBlock:
227  *****************************************************************************/
DecodeBlock(decoder_t * p_dec,block_t * p_block)228 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
229 {
230     decoder_sys_t *p_sys = p_dec->p_sys;
231 
232     if( !p_block ) /* No Drain */
233         return VLCDEC_SUCCESS;
234 
235     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
236     {
237         Flush( p_dec );
238         if( p_block->i_flags & (BLOCK_FLAG_CORRUPTED) )
239         {
240             block_Release( p_block );
241             return VLCDEC_SUCCESS;
242         }
243     }
244 
245     /* Remove ADTS header if we have decoder specific config */
246     if( p_dec->fmt_in.i_extra && p_block->i_buffer > 7 )
247     {
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 )
255             {
256                 p_block->p_buffer += i_header_size;
257                 p_block->i_buffer -= i_header_size;
258             }
259         }
260     }
261 
262     const mtime_t i_pts = p_block->i_pts;
263 
264     /* Append block as temporary buffer */
265     if( p_sys->p_block == NULL )
266     {
267         p_sys->p_block = p_block;
268     }
269     else
270     {
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 );
276     }
277 
278     /* !Warn: do not use p_block beyond this point */
279 
280     if( p_dec->fmt_out.audio.i_rate == 0 )
281     {
282         unsigned long i_rate = 0;
283         unsigned char i_channels;
284 
285         /* Init from DecoderConfig */
286         if( p_dec->fmt_in.i_extra > 0 &&
287             NeAACDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
288                            p_dec->fmt_in.i_extra, &i_rate, &i_channels ) != 0 )
289         {
290             /* Failed, will try from data */
291             i_rate = 0;
292         }
293 
294         if( i_rate == 0 && p_sys->p_block && p_sys->p_block->i_buffer )
295         {
296             /* Init faad with the first frame */
297             long i_read = NeAACDecInit( p_sys->hfaad,
298                                         p_sys->p_block->p_buffer, p_sys->p_block->i_buffer,
299                                         &i_rate, &i_channels );
300             if( i_read < 0 || (size_t) i_read > p_sys->p_block->i_buffer )
301                 i_rate = 0;
302             else
303                 FlushBuffer( p_sys, i_read );
304         }
305 
306         if( i_rate == 0 )
307         {
308             /* Can not init decoder at all for now */
309             FlushBuffer( p_sys, SIZE_MAX );
310             return VLCDEC_SUCCESS;
311         }
312 
313         /* Decoder Initialized */
314         p_dec->fmt_out.audio.i_rate = i_rate;
315         p_dec->fmt_out.audio.i_channels = i_channels;
316         p_dec->fmt_out.audio.i_physical_channels
317             = mpeg4_asc_channelsbyindex[i_channels];
318         date_Init( &p_sys->date, i_rate, 1 );
319     }
320 
321     if( i_pts > VLC_TS_INVALID && i_pts != date_Get( &p_sys->date ) )
322     {
323         date_Set( &p_sys->date, i_pts );
324     }
325     else if( !date_Get( &p_sys->date ) )
326     {
327         /* We've just started the stream, wait for the first PTS. */
328         FlushBuffer( p_sys, SIZE_MAX );
329         return VLCDEC_SUCCESS;
330     }
331 
332     /* Decode all data */
333     while( p_sys->p_block && p_sys->p_block->i_buffer > 0 )
334     {
335         void *samples;
336         NeAACDecFrameInfo frame;
337         block_t *p_out = NULL;
338 
339         samples = NeAACDecDecode( p_sys->hfaad, &frame,
340                                   p_sys->p_block->p_buffer,
341                                   p_sys->p_block->i_buffer );
342 
343         if( frame.error > 0 )
344         {
345             msg_Warn( p_dec, "%s", NeAACDecGetErrorMessage( frame.error ) );
346 
347             if( frame.error == 21 || frame.error == 12 )
348             {
349                 /*
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.
354                  */
355                 unsigned long i_rate;
356                 unsigned char i_channels;
357                 NeAACDecHandle *hfaad;
358                 NeAACDecConfiguration *cfg,*oldcfg;
359 
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 );
368 
369                 if( NeAACDecInit( hfaad,
370                                   p_sys->p_block->p_buffer,
371                                   p_sys->p_block->i_buffer,
372                                   &i_rate,&i_channels ) < 0 )
373                 {
374                     /* reinitialization failed */
375                     NeAACDecClose( hfaad );
376                     NeAACDecSetConfiguration( p_sys->hfaad, oldcfg );
377                 }
378                 else
379                 {
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                         = mpeg4_asc_channelsbyindex[i_channels];
386                     date_Init( &p_sys->date, i_rate, 1 );
387                 }
388             }
389 
390             Flush( p_dec );
391             p_sys->b_discontinuity = true;
392 
393             continue;
394         }
395 
396         if( frame.channels == 0 || frame.channels >= 64 )
397         {
398             msg_Warn( p_dec, "invalid channels count: %i", frame.channels );
399             if( frame.channels == 0 )
400                 p_sys->b_discontinuity = true;
401             FlushBuffer( p_sys, frame.bytesconsumed ? frame.bytesconsumed : SIZE_MAX );
402             continue;
403         }
404 
405         if( frame.samples == 0 )
406         {
407             msg_Warn( p_dec, "decoded zero sample" );
408             FlushBuffer( p_sys, frame.bytesconsumed ? frame.bytesconsumed : SIZE_MAX );
409             continue;
410         }
411 
412         /* We decoded a valid frame */
413         if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
414         {
415             date_Init( &p_sys->date, frame.samplerate, 1 );
416             date_Set( &p_sys->date, i_pts );
417         }
418 
419         p_dec->fmt_out.audio.i_rate = frame.samplerate;
420 
421         /* Adjust stream info when dealing with SBR/PS */
422         bool b_sbr = (frame.sbr == 1) || (frame.sbr == 2);
423         if( p_sys->b_sbr != b_sbr || p_sys->b_ps != frame.ps )
424         {
425             const char *psz_ext = (b_sbr && frame.ps) ? "SBR+PS" :
426                                     b_sbr ? "SBR" : "PS";
427 
428             msg_Dbg( p_dec, "AAC %s (channels: %u, samplerate: %lu)",
429                     psz_ext, frame.channels, frame.samplerate );
430 
431             if( !p_dec->p_description )
432                 p_dec->p_description = vlc_meta_New();
433             if( p_dec->p_description )
434                 vlc_meta_AddExtra( p_dec->p_description, _("AAC extension"), psz_ext );
435 
436             p_sys->b_sbr = b_sbr;
437             p_sys->b_ps = frame.ps;
438         }
439 
440 #ifndef FAAD2_VIDEOLAN_PATCHED
441         /* PS Enabled FAAD PCA bug hotfix (contribs has patch) */
442         if( frame.channels == 8 )
443         {
444             const uint8_t psbugconfig[3][8] = { { 2, 3, 2, 3, 2, 3, 6, 7 },  /* fdk 7.1 (4 Front) */
445                                                 { 2, 3, 2, 3, 2, 3, 4, 5 },  /* 7.1 */
446                                                 { 1, 2, 3, 4, 5, 6, 7, 9 } };/* fixed */
447             for( size_t i=0; i<2; i++ )
448             {
449                 if( !memcmp( frame.channel_position, psbugconfig[i], 8 ) )
450                 {
451                     msg_Warn( p_dec, "Unpatched FAAD2 library with PS Bug. Trying to workaround !" );
452                     memcpy( frame.channel_position, psbugconfig[2], 8 );
453                     break;
454                 }
455             }
456         }
457 
458         /* Hotfix channels misdetection/repetition for FDK 7.1 */
459         struct
460         {
461             const uint8_t chans;
462             const uint8_t faulty[8];
463             const uint8_t fixed[8];
464         } const channel_repeat_fixes[] = {
465             { 7, { 2, 3, 2, 3, 2, 3, 6 },    { 1, 2, 3, 6, 7, 8, 9 }    }, /* 3F 3R LFE #18273 */
466             { 8, { 1, 2, 3, 6, 7, 6, 7, 9 }, { 1, 2, 3, 6, 7, 4, 5, 9 } }, /* FDK encoded 7.1 */
467         };
468 
469         for( size_t i=0; i<ARRAY_SIZE(channel_repeat_fixes); i++ )
470         {
471             if( channel_repeat_fixes[i].chans == frame.channels &&
472                 !memcmp( frame.channel_position, channel_repeat_fixes[i].faulty,
473                                                  channel_repeat_fixes[i].chans ) )
474             {
475                 msg_Warn( p_dec, "Patching for Front channel repeat bug" );
476                 memcpy( frame.channel_position, channel_repeat_fixes[i].fixed,
477                                                 channel_repeat_fixes[i].chans );
478                 break;
479             }
480         }
481 #endif
482         /* Handle > 1 local pair 5.1 setups.
483            In case of more than 1 channel pair per area, faad will have repeats
484            in channels sequence. We need to remap to available surround channels.
485            Front > Middle > Rear:
486            In case of 4 middle, it maps to 2F 2M if no previous front.
487            In case of 4 rear, it maps to 2M 2R if no previous rear.
488         */
489         unsigned i_faadused = 0;
490         for( unsigned i=0; i<frame.channels; i++ )
491             if( frame.channel_position[i] > 0 )
492                 i_faadused |= 1 << frame.channel_position[i];
493 
494         for( size_t i=3; i<frame.channels; i++ )
495         {
496              if( frame.channel_position[i - 3] == frame.channel_position[i - 1] &&
497                  frame.channel_position[i - 2] == frame.channel_position[i] &&
498                  frame.channel_position[i - 1] >= SIDE_CHANNEL_LEFT &&
499                  frame.channel_position[i - 1] <= BACK_CHANNEL_CENTER &&
500                  frame.channel_position[i - 1] >= SIDE_CHANNEL_LEFT &&
501                  frame.channel_position[i - 1] <= BACK_CHANNEL_CENTER )
502              {
503                 if( ( (1 << (frame.channel_position[i - 3] - 2)) & i_faadused ) == 0 &&
504                     ( (1 << (frame.channel_position[i - 2] - 2)) & i_faadused ) == 0 )
505                 {
506                     frame.channel_position[i - 3] -= 2;
507                     frame.channel_position[i - 2] -= 2;
508                     i_faadused |= 1 << frame.channel_position[i - 3];
509                     i_faadused |= 1 << frame.channel_position[i - 2];
510                 }
511              }
512         }
513 
514         /* Convert frame.channel_position to our own channel values */
515         p_dec->fmt_out.audio.i_physical_channels = 0;
516 
517         uint8_t  pi_neworder_table[64] = {0};
518         uint32_t pi_faad_channels_positions[64 + 1] = {0};
519 
520         bool b_reorder = false;
521         if (p_dec->fmt_out.audio.channel_type == AUDIO_CHANNEL_TYPE_BITMAP)
522         {
523             for( size_t i = 0; i < frame.channels; i++ )
524             {
525                 unsigned pos = frame.channel_position[i];
526                 if( likely(pos < FAAD_CHANNEL_ID_COUNT) )
527                 {
528                     pi_faad_channels_positions[i] = pi_tovlcmapping[pos];
529                     p_dec->fmt_out.audio.i_physical_channels |= pi_faad_channels_positions[i];
530                 }
531                 else pi_faad_channels_positions[i] = 0;
532             }
533         }
534         else if (p_dec->fmt_out.audio.channel_type == AUDIO_CHANNEL_TYPE_AMBISONICS
535             && frame.channels == 4)
536         {
537             pi_faad_channels_positions[0] = AOUT_CHAN_REARCENTER;
538             pi_faad_channels_positions[1] = AOUT_CHAN_LEFT;
539             pi_faad_channels_positions[2] = AOUT_CHAN_RIGHT;
540             pi_faad_channels_positions[3] = AOUT_CHAN_CENTER;
541             p_dec->fmt_out.audio.i_physical_channels =
542                 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT
543                 | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
544         }
545 
546         b_reorder = aout_CheckChannelReorder( pi_faad_channels_positions, NULL,
547             p_dec->fmt_out.audio.i_physical_channels, pi_neworder_table );
548 
549         p_dec->fmt_out.audio.i_channels = popcount(p_dec->fmt_out.audio.i_physical_channels);
550 
551         if( !decoder_UpdateAudioFormat( p_dec ) && p_dec->fmt_out.audio.i_channels > 0 )
552             p_out = decoder_NewAudioBuffer( p_dec, frame.samples / p_dec->fmt_out.audio.i_channels );
553 
554         if( p_out )
555         {
556             p_out->i_pts = date_Get( &p_sys->date );
557             p_out->i_length = date_Increment( &p_sys->date,
558                                               frame.samples / frame.channels )
559                               - p_out->i_pts;
560 
561             if ( p_dec->fmt_out.audio.channel_type == AUDIO_CHANNEL_TYPE_BITMAP )
562             {
563                 /* Don't kill speakers if some weird mapping does not gets 1:1 */
564                 if( popcount(p_dec->fmt_out.audio.i_physical_channels) != frame.channels )
565                     memset( p_out->p_buffer, 0, p_out->i_buffer );
566             }
567 
568             /* FIXME: replace when aout_channel_reorder can take samples from a different buffer */
569             if( b_reorder )
570                 DoReordering( (uint32_t *)p_out->p_buffer, samples,
571                               frame.samples / frame.channels, frame.channels,
572                               pi_neworder_table );
573             else
574                  memcpy( p_out->p_buffer, samples, p_out->i_buffer );
575 
576             if( p_sys->b_discontinuity )
577             {
578                 p_out->i_flags |= BLOCK_FLAG_DISCONTINUITY;
579                 p_sys->b_discontinuity = false;
580             }
581 
582             decoder_QueueAudio( p_dec, p_out );
583         }
584         else
585         {
586             date_Increment( &p_sys->date, frame.samples / frame.channels );
587         }
588 
589         FlushBuffer( p_sys, frame.bytesconsumed ? frame.bytesconsumed : SIZE_MAX );
590 
591         if( p_sys->p_block && p_sys->p_block->i_buffer == 1 )
592         {
593             /* Drop byte of padding */
594             FlushBuffer( p_sys, 0 );
595         }
596 
597         continue;
598     }
599 
600     return VLCDEC_SUCCESS;
601 }
602 
603 /*****************************************************************************
604  * Close:
605  *****************************************************************************/
Close(vlc_object_t * p_this)606 static void Close( vlc_object_t *p_this )
607 {
608     decoder_t *p_dec = (decoder_t *)p_this;
609     decoder_sys_t *p_sys = p_dec->p_sys;
610 
611     NeAACDecClose( p_sys->hfaad );
612     FlushBuffer( p_sys, SIZE_MAX );
613     free( p_sys );
614 }
615 
616 /*****************************************************************************
617  * DoReordering: do some channel re-ordering (the ac3 channel order is
618  *   different from the aac one).
619  *****************************************************************************/
DoReordering(uint32_t * p_out,uint32_t * p_in,int i_samples,int i_nb_channels,uint8_t * pi_chan_positions)620 static void DoReordering( uint32_t *p_out, uint32_t *p_in, int i_samples,
621                           int i_nb_channels, uint8_t *pi_chan_positions )
622 {
623 #if HAVE_FPU
624     #define CAST_SAMPLE(a) a
625 #else
626     #define CAST_SAMPLE(a) ((uint16_t *)a)
627 #endif
628     /* Do the actual reordering */
629     for( int i = 0; i < i_samples; i++ )
630     {
631         for( int j = 0; j < i_nb_channels; j++ )
632         {
633             CAST_SAMPLE(p_out)[i * i_nb_channels + pi_chan_positions[j]] =
634                 CAST_SAMPLE(p_in)[i * i_nb_channels + j];
635         }
636     }
637 }
638 
639