]> git.sesse.net Git - vlc/blob - modules/codec/faad.c
LGPL
[vlc] / modules / codec / faad.c
1 /*****************************************************************************
2  * faad.c: AAC decoder using libfaad2
3  *****************************************************************************
4  * Copyright (C) 2001, 2003 VLC authors and VideoLAN
5  * $Id$
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
43 #include <faad.h>
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 static int  Open( vlc_object_t * );
49 static void Close( vlc_object_t * );
50
51 vlc_module_begin ()
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 )
57 vlc_module_end ()
58
59 /****************************************************************************
60  * Local prototypes
61  ****************************************************************************/
62 static block_t *DecodeBlock( decoder_t *, block_t ** );
63 static void DoReordering( uint32_t *, uint32_t *, int, int, uint32_t * );
64
65 #define MAX_CHANNEL_POSITIONS 9
66
67 struct decoder_sys_t
68 {
69     /* faad handler */
70     faacDecHandle *hfaad;
71
72     /* samples */
73     date_t date;
74
75     /* temporary buffer */
76     uint8_t *p_buffer;
77     int     i_buffer;
78     size_t  i_buffer_size;
79
80     /* Channel positions of the current stream (for re-ordering) */
81     uint32_t pi_channel_positions[MAX_CHANNEL_POSITIONS];
82
83     bool b_sbr, b_ps;
84 };
85
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
101     };
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
113           | AOUT_CHAN_CENTER,
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
117     };
118
119 /*****************************************************************************
120  * OpenDecoder: probe the decoder and return score
121  *****************************************************************************/
122 static int Open( vlc_object_t *p_this )
123 {
124     decoder_t *p_dec = (decoder_t*)p_this;
125     decoder_sys_t *p_sys = p_dec->p_sys;
126     faacDecConfiguration *cfg;
127
128     if( p_dec->fmt_in.i_codec != VLC_CODEC_MP4A )
129     {
130         return VLC_EGENERIC;
131     }
132
133     /* Allocate the memory needed to store the decoder's structure */
134     if( ( p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) ) ) == NULL )
135         return VLC_ENOMEM;
136
137     /* Open a faad context */
138     if( ( p_sys->hfaad = faacDecOpen() ) == NULL )
139     {
140         msg_Err( p_dec, "cannot initialize faad" );
141         free( p_sys );
142         return VLC_EGENERIC;
143     }
144
145     /* Misc init */
146     date_Set( &p_sys->date, 0 );
147     p_dec->fmt_out.i_cat = AUDIO_ES;
148
149     if (HAVE_FPU)
150         p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
151     else
152         p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
153     p_dec->pf_decode_audio = DecodeBlock;
154
155     p_dec->fmt_out.audio.i_physical_channels =
156         p_dec->fmt_out.audio.i_original_channels = 0;
157
158     if( p_dec->fmt_in.i_extra > 0 )
159     {
160         /* We have a decoder config so init the handle */
161         unsigned long i_rate;
162         unsigned char i_channels;
163
164         if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
165                           p_dec->fmt_in.i_extra,
166                           &i_rate, &i_channels ) < 0 )
167         {
168             msg_Err( p_dec, "Failed to initialize faad using extra data" );
169             faacDecClose( p_sys->hfaad );
170             free( p_sys );
171             return VLC_EGENERIC;
172         }
173
174         p_dec->fmt_out.audio.i_rate = i_rate;
175         p_dec->fmt_out.audio.i_channels = i_channels;
176         p_dec->fmt_out.audio.i_physical_channels
177             = p_dec->fmt_out.audio.i_original_channels
178             = pi_channels_guessed[i_channels];
179         date_Init( &p_sys->date, i_rate, 1 );
180     }
181     else
182     {
183         /* Will be initalised from first frame */
184         p_dec->fmt_out.audio.i_rate = 0;
185         p_dec->fmt_out.audio.i_channels = 0;
186     }
187
188     /* Set the faad config */
189     cfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
190     if (HAVE_FPU)
191         cfg->outputFormat = FAAD_FMT_FLOAT;
192     else
193         cfg->outputFormat = FAAD_FMT_16BIT;
194     faacDecSetConfiguration( p_sys->hfaad, cfg );
195
196     /* buffer */
197     p_sys->i_buffer = p_sys->i_buffer_size = 0;
198     p_sys->p_buffer = NULL;
199
200     /* Faad2 can't deal with truncated data (eg. from MPEG TS) */
201     p_dec->b_need_packetized = true;
202
203     p_sys->b_sbr = p_sys->b_ps = false;
204     return VLC_SUCCESS;
205 }
206
207 /*****************************************************************************
208  * DecodeBlock:
209  *****************************************************************************/
210 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
211 {
212     decoder_sys_t *p_sys = p_dec->p_sys;
213     block_t *p_block;
214
215     if( !pp_block || !*pp_block ) return NULL;
216
217     p_block = *pp_block;
218
219     if( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
220     {
221         block_Release( p_block );
222         return NULL;
223     }
224
225     /* Remove ADTS header if we have decoder specific config */
226     if( p_dec->fmt_in.i_extra && p_block->i_buffer > 7 )
227     {
228         if( p_block->p_buffer[0] == 0xff &&
229             ( p_block->p_buffer[1] & 0xf0 ) == 0xf0 ) /* syncword */
230         {   /* ADTS header present */
231             size_t i_header_size; /* 7 bytes (+ 2 bytes for crc) */
232             i_header_size = 7 + ( ( p_block->p_buffer[1] & 0x01 ) ? 0 : 2 );
233             /* FIXME: multiple blocks per frame */
234             if( p_block->i_buffer > i_header_size )
235             {
236                 p_block->p_buffer += i_header_size;
237                 p_block->i_buffer -= i_header_size;
238             }
239         }
240     }
241
242     /* Append the block to the temporary buffer */
243     if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
244     {
245         size_t  i_buffer_size = p_sys->i_buffer + p_block->i_buffer;
246         uint8_t *p_buffer     = realloc( p_sys->p_buffer, i_buffer_size );
247         if( p_buffer )
248         {
249             p_sys->i_buffer_size = i_buffer_size;
250             p_sys->p_buffer      = p_buffer;
251         }
252         else
253         {
254             p_block->i_buffer = 0;
255         }
256     }
257
258     if( p_block->i_buffer > 0 )
259     {
260         memcpy( &p_sys->p_buffer[p_sys->i_buffer],
261                      p_block->p_buffer, p_block->i_buffer );
262         p_sys->i_buffer += p_block->i_buffer;
263         p_block->i_buffer = 0;
264     }
265
266     if( p_dec->fmt_out.audio.i_rate == 0 && p_dec->fmt_in.i_extra > 0 )
267     {
268         /* We have a decoder config so init the handle */
269         unsigned long i_rate;
270         unsigned char i_channels;
271
272         if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
273                           p_dec->fmt_in.i_extra,
274                           &i_rate, &i_channels ) >= 0 )
275         {
276             p_dec->fmt_out.audio.i_rate = i_rate;
277             p_dec->fmt_out.audio.i_channels = i_channels;
278             p_dec->fmt_out.audio.i_physical_channels
279                 = p_dec->fmt_out.audio.i_original_channels
280                 = pi_channels_guessed[i_channels];
281
282             date_Init( &p_sys->date, i_rate, 1 );
283         }
284     }
285
286     if( p_dec->fmt_out.audio.i_rate == 0 && p_sys->i_buffer )
287     {
288         unsigned long i_rate;
289         unsigned char i_channels;
290
291         /* Init faad with the first frame */
292         if( faacDecInit( p_sys->hfaad,
293                          p_sys->p_buffer, p_sys->i_buffer,
294                          &i_rate, &i_channels ) < 0 )
295         {
296             block_Release( p_block );
297             return NULL;
298         }
299
300         p_dec->fmt_out.audio.i_rate = i_rate;
301         p_dec->fmt_out.audio.i_channels = i_channels;
302         p_dec->fmt_out.audio.i_physical_channels
303             = p_dec->fmt_out.audio.i_original_channels
304             = pi_channels_guessed[i_channels];
305         date_Init( &p_sys->date, i_rate, 1 );
306     }
307
308     if( p_block->i_pts > VLC_TS_INVALID && p_block->i_pts != date_Get( &p_sys->date ) )
309     {
310         date_Set( &p_sys->date, p_block->i_pts );
311     }
312     else if( !date_Get( &p_sys->date ) )
313     {
314         /* We've just started the stream, wait for the first PTS. */
315         block_Release( p_block );
316         p_sys->i_buffer = 0;
317         return NULL;
318     }
319
320     /* Decode all data */
321     if( p_sys->i_buffer )
322     {
323         void *samples;
324         faacDecFrameInfo frame;
325         block_t *p_out;
326
327         samples = faacDecDecode( p_sys->hfaad, &frame,
328                                  p_sys->p_buffer, p_sys->i_buffer );
329
330         if( frame.error > 0 )
331         {
332             msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
333
334             if( frame.error == 21 )
335             {
336                 /*
337                  * Once an "Unexpected channel configuration change" error
338                  * occurs, it will occurs afterwards, and we got no sound.
339                  * Reinitialization of the decoder is required.
340                  */
341                 unsigned long i_rate;
342                 unsigned char i_channels;
343                 faacDecHandle *hfaad;
344                 faacDecConfiguration *cfg,*oldcfg;
345
346                 oldcfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
347                 hfaad = faacDecOpen();
348                 cfg = faacDecGetCurrentConfiguration( hfaad );
349                 if( oldcfg->defSampleRate )
350                     cfg->defSampleRate = oldcfg->defSampleRate;
351                 cfg->defObjectType = oldcfg->defObjectType;
352                 cfg->outputFormat = oldcfg->outputFormat;
353                 faacDecSetConfiguration( hfaad, cfg );
354
355                 if( faacDecInit( hfaad, p_sys->p_buffer, p_sys->i_buffer,
356                                 &i_rate,&i_channels ) < 0 )
357                 {
358                     /* reinitialization failed */
359                     faacDecClose( hfaad );
360                     faacDecSetConfiguration( p_sys->hfaad, oldcfg );
361                 }
362                 else
363                 {
364                     faacDecClose( p_sys->hfaad );
365                     p_sys->hfaad = hfaad;
366                     p_dec->fmt_out.audio.i_rate = i_rate;
367                     p_dec->fmt_out.audio.i_channels = i_channels;
368                     p_dec->fmt_out.audio.i_physical_channels
369                         = p_dec->fmt_out.audio.i_original_channels
370                         = pi_channels_guessed[i_channels];
371                     date_Init( &p_sys->date, i_rate, 1 );
372                 }
373             }
374
375             /* Flush the buffer */
376             p_sys->i_buffer = 0;
377             block_Release( p_block );
378             return NULL;
379         }
380
381         if( frame.channels <= 0 || frame.channels > 8 || frame.channels == 7 )
382         {
383             msg_Warn( p_dec, "invalid channels count: %i", frame.channels );
384
385             /* Flush the buffer */
386             p_sys->i_buffer -= frame.bytesconsumed;
387             if( p_sys->i_buffer > 0 )
388             {
389                 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
390                          p_sys->i_buffer );
391             }
392             block_Release( p_block );
393             return NULL;
394         }
395
396         if( frame.samples <= 0 )
397         {
398             msg_Warn( p_dec, "decoded zero sample" );
399
400             /* Flush the buffer */
401             p_sys->i_buffer -= frame.bytesconsumed;
402             if( p_sys->i_buffer > 0 )
403             {
404                 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
405                          p_sys->i_buffer );
406             }
407             block_Release( p_block );
408             return NULL;
409         }
410
411         /* We decoded a valid frame */
412         if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
413         {
414             date_Init( &p_sys->date, frame.samplerate, 1 );
415             date_Set( &p_sys->date, p_block->i_pts );
416         }
417         p_block->i_pts = VLC_TS_INVALID;  /* PTS is valid only once */
418
419         p_dec->fmt_out.audio.i_rate = frame.samplerate;
420         p_dec->fmt_out.audio.i_channels = frame.channels;
421
422         /* Adjust stream info when dealing with SBR/PS */
423         bool b_sbr = (frame.sbr == 1) || (frame.sbr == 2);
424         if( p_sys->b_sbr != b_sbr || p_sys->b_ps != frame.ps )
425         {
426             const char *psz_ext = (b_sbr && frame.ps) ? "SBR+PS" :
427                                     b_sbr ? "SBR" : "PS";
428
429             msg_Dbg( p_dec, "AAC %s (channels: %u, samplerate: %lu)",
430                     psz_ext, frame.channels, frame.samplerate );
431
432             if( !p_dec->p_description )
433                 p_dec->p_description = vlc_meta_New();
434             if( p_dec->p_description )
435                 vlc_meta_AddExtra( p_dec->p_description, _("AAC extension"), psz_ext );
436
437             p_sys->b_sbr = b_sbr;
438             p_sys->b_ps = frame.ps;
439         }
440
441         /* Convert frame.channel_position to our own channel values */
442         p_dec->fmt_out.audio.i_physical_channels = 0;
443         const uint32_t nbChannels = frame.channels;
444         unsigned j;
445         for( unsigned i = 0; i < nbChannels; i++ )
446         {
447             /* Find the channel code */
448             for( j = 0; j < MAX_CHANNEL_POSITIONS; j++ )
449             {
450                 if( frame.channel_position[i] == pi_channels_in[j] )
451                     break;
452             }
453             if( j >= MAX_CHANNEL_POSITIONS )
454             {
455                 msg_Warn( p_dec, "unknown channel ordering" );
456                 /* Invent something */
457                 j = i;
458             }
459             /* */
460             p_sys->pi_channel_positions[i] = pi_channels_out[j];
461             if( p_dec->fmt_out.audio.i_physical_channels & pi_channels_out[j] )
462                 frame.channels--; /* We loose a duplicated channel */
463             else
464                 p_dec->fmt_out.audio.i_physical_channels |= pi_channels_out[j];
465         }
466         if ( nbChannels != frame.channels )
467         {
468             p_dec->fmt_out.audio.i_physical_channels
469                 = p_dec->fmt_out.audio.i_original_channels
470                 = pi_channels_guessed[nbChannels];
471         }
472         else
473         {
474             p_dec->fmt_out.audio.i_original_channels =
475                 p_dec->fmt_out.audio.i_physical_channels;
476         }
477         p_dec->fmt_out.audio.i_channels = nbChannels;
478         p_out = decoder_NewAudioBuffer( p_dec, frame.samples / nbChannels );
479         if( p_out == NULL )
480         {
481             p_sys->i_buffer = 0;
482             block_Release( p_block );
483             return NULL;
484         }
485
486         p_out->i_pts = date_Get( &p_sys->date );
487         p_out->i_length = date_Increment( &p_sys->date,
488                                           frame.samples / nbChannels )
489                           - p_out->i_pts;
490
491         DoReordering( (uint32_t *)p_out->p_buffer, samples,
492                       frame.samples / nbChannels, nbChannels,
493                       p_sys->pi_channel_positions );
494
495         p_sys->i_buffer -= frame.bytesconsumed;
496         if( p_sys->i_buffer > 0 )
497         {
498             memmove( p_sys->p_buffer, &p_sys->p_buffer[frame.bytesconsumed],
499                      p_sys->i_buffer );
500         }
501
502         return p_out;
503     }
504
505     block_Release( p_block );
506     return NULL;
507 }
508
509 /*****************************************************************************
510  * Close:
511  *****************************************************************************/
512 static void Close( vlc_object_t *p_this )
513 {
514     decoder_t *p_dec = (decoder_t *)p_this;
515     decoder_sys_t *p_sys = p_dec->p_sys;
516
517     faacDecClose( p_sys->hfaad );
518     free( p_sys->p_buffer );
519     free( p_sys );
520 }
521
522 /*****************************************************************************
523  * DoReordering: do some channel re-ordering (the ac3 channel order is
524  *   different from the aac one).
525  *****************************************************************************/
526 static void DoReordering( uint32_t *p_out, uint32_t *p_in, int i_samples,
527                           int i_nb_channels, uint32_t *pi_chan_positions )
528 {
529     int pi_chan_table[MAX_CHANNEL_POSITIONS] = {0};
530     int i, j, k;
531
532     /* Find the channels mapping */
533     for( i = 0, j = 0; i < MAX_CHANNEL_POSITIONS; i++ )
534     {
535         for( k = 0; k < i_nb_channels; k++ )
536         {
537             if( pi_channels_ordered[i] == pi_chan_positions[k] )
538             {
539                 pi_chan_table[k] = j++;
540                 break;
541             }
542         }
543     }
544
545     /* Do the actual reordering */
546     if( HAVE_FPU )
547         for( i = 0; i < i_samples; i++ )
548             for( j = 0; j < i_nb_channels; j++ )
549                 p_out[i * i_nb_channels + pi_chan_table[j]] =
550                     p_in[i * i_nb_channels + j];
551     else
552         for( i = 0; i < i_samples; i++ )
553             for( j = 0; j < i_nb_channels; j++ )
554                 ((uint16_t *)p_out)[i * i_nb_channels + pi_chan_table[j]] =
555                     ((uint16_t *)p_in)[i * i_nb_channels + j];
556 }
557