]> git.sesse.net Git - vlc/blob - modules/codec/faad.c
zvbi: set default opaque to false as default is text mode now
[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     p_dec->fmt_out.i_codec = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
150     p_dec->pf_decode_audio = DecodeBlock;
151
152     p_dec->fmt_out.audio.i_physical_channels =
153         p_dec->fmt_out.audio.i_original_channels = 0;
154
155     if( p_dec->fmt_in.i_extra > 0 )
156     {
157         /* We have a decoder config so init the handle */
158         unsigned long i_rate;
159         unsigned char i_channels;
160
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 )
164         {
165             msg_Err( p_dec, "Failed to initialize faad using extra data" );
166             faacDecClose( p_sys->hfaad );
167             free( p_sys );
168             return VLC_EGENERIC;
169         }
170
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 );
177     }
178     else
179     {
180         /* Will be initalised from first frame */
181         p_dec->fmt_out.audio.i_rate = 0;
182         /*FIXME: Try to guess channel count, so transcode module doesn't burb and do funny stuff
183             Revert back to 0 when transcode module/audio encoders can reinit stuff after Open()*/
184         p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
185     }
186
187     /* Set the faad config */
188     cfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
189     cfg->outputFormat = HAVE_FPU ? FAAD_FMT_FLOAT : FAAD_FMT_16BIT;
190     faacDecSetConfiguration( p_sys->hfaad, cfg );
191
192     /* buffer */
193     p_sys->i_buffer = p_sys->i_buffer_size = 0;
194     p_sys->p_buffer = NULL;
195
196     /* Faad2 can't deal with truncated data (eg. from MPEG TS) */
197     p_dec->b_need_packetized = true;
198
199     p_sys->b_sbr = p_sys->b_ps = false;
200     return VLC_SUCCESS;
201 }
202
203 /*****************************************************************************
204  * DecodeBlock:
205  *****************************************************************************/
206 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
207 {
208     decoder_sys_t *p_sys = p_dec->p_sys;
209     block_t *p_block;
210
211     if( !pp_block || !*pp_block ) return NULL;
212
213     p_block = *pp_block;
214
215     if( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
216     {
217         block_Release( p_block );
218         return NULL;
219     }
220
221     /* Remove ADTS header if we have decoder specific config */
222     if( p_dec->fmt_in.i_extra && p_block->i_buffer > 7 )
223     {
224         if( p_block->p_buffer[0] == 0xff &&
225             ( p_block->p_buffer[1] & 0xf0 ) == 0xf0 ) /* syncword */
226         {   /* ADTS header present */
227             size_t i_header_size; /* 7 bytes (+ 2 bytes for crc) */
228             i_header_size = 7 + ( ( p_block->p_buffer[1] & 0x01 ) ? 0 : 2 );
229             /* FIXME: multiple blocks per frame */
230             if( p_block->i_buffer > i_header_size )
231             {
232                 p_block->p_buffer += i_header_size;
233                 p_block->i_buffer -= i_header_size;
234             }
235         }
236     }
237
238     /* Append the block to the temporary buffer */
239     if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
240     {
241         size_t  i_buffer_size = p_sys->i_buffer + p_block->i_buffer;
242         uint8_t *p_buffer     = realloc( p_sys->p_buffer, i_buffer_size );
243         if( p_buffer )
244         {
245             p_sys->i_buffer_size = i_buffer_size;
246             p_sys->p_buffer      = p_buffer;
247         }
248         else
249         {
250             p_block->i_buffer = 0;
251         }
252     }
253
254     if( p_block->i_buffer > 0 )
255     {
256         memcpy( &p_sys->p_buffer[p_sys->i_buffer],
257                      p_block->p_buffer, p_block->i_buffer );
258         p_sys->i_buffer += p_block->i_buffer;
259         p_block->i_buffer = 0;
260     }
261
262     if( p_dec->fmt_out.audio.i_rate == 0 && p_dec->fmt_in.i_extra > 0 )
263     {
264         /* We have a decoder config so init the handle */
265         unsigned long i_rate;
266         unsigned char i_channels;
267
268         if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
269                           p_dec->fmt_in.i_extra,
270                           &i_rate, &i_channels ) >= 0 )
271         {
272             p_dec->fmt_out.audio.i_rate = i_rate;
273             p_dec->fmt_out.audio.i_channels = i_channels;
274             p_dec->fmt_out.audio.i_physical_channels
275                 = p_dec->fmt_out.audio.i_original_channels
276                 = pi_channels_guessed[i_channels];
277
278             date_Init( &p_sys->date, i_rate, 1 );
279         }
280     }
281
282     if( p_dec->fmt_out.audio.i_rate == 0 && p_sys->i_buffer )
283     {
284         unsigned long i_rate;
285         unsigned char i_channels;
286
287         /* Init faad with the first frame */
288         if( faacDecInit( p_sys->hfaad,
289                          p_sys->p_buffer, p_sys->i_buffer,
290                          &i_rate, &i_channels ) < 0 )
291         {
292             block_Release( p_block );
293             return NULL;
294         }
295
296         p_dec->fmt_out.audio.i_rate = i_rate;
297         p_dec->fmt_out.audio.i_channels = i_channels;
298         p_dec->fmt_out.audio.i_physical_channels
299             = p_dec->fmt_out.audio.i_original_channels
300             = pi_channels_guessed[i_channels];
301         date_Init( &p_sys->date, i_rate, 1 );
302     }
303
304     if( p_block->i_pts > VLC_TS_INVALID && p_block->i_pts != date_Get( &p_sys->date ) )
305     {
306         date_Set( &p_sys->date, p_block->i_pts );
307     }
308     else if( !date_Get( &p_sys->date ) )
309     {
310         /* We've just started the stream, wait for the first PTS. */
311         block_Release( p_block );
312         p_sys->i_buffer = 0;
313         return NULL;
314     }
315
316     /* Decode all data */
317     if( p_sys->i_buffer )
318     {
319         void *samples;
320         faacDecFrameInfo frame;
321         block_t *p_out;
322
323         samples = faacDecDecode( p_sys->hfaad, &frame,
324                                  p_sys->p_buffer, p_sys->i_buffer );
325
326         if( frame.error > 0 )
327         {
328             msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
329
330             if( frame.error == 21 || frame.error == 12 )
331             {
332                 /*
333                  * Once an "Unexpected channel configuration change"
334                  * or a "Invalid number of channels" error
335                  * occurs, it will occurs afterwards, and we got no sound.
336                  * Reinitialization of the decoder is required.
337                  */
338                 unsigned long i_rate;
339                 unsigned char i_channels;
340                 faacDecHandle *hfaad;
341                 faacDecConfiguration *cfg,*oldcfg;
342
343                 oldcfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
344                 hfaad = faacDecOpen();
345                 cfg = faacDecGetCurrentConfiguration( hfaad );
346                 if( oldcfg->defSampleRate )
347                     cfg->defSampleRate = oldcfg->defSampleRate;
348                 cfg->defObjectType = oldcfg->defObjectType;
349                 cfg->outputFormat = oldcfg->outputFormat;
350                 faacDecSetConfiguration( hfaad, cfg );
351
352                 if( faacDecInit( hfaad, p_sys->p_buffer, p_sys->i_buffer,
353                                 &i_rate,&i_channels ) < 0 )
354                 {
355                     /* reinitialization failed */
356                     faacDecClose( hfaad );
357                     faacDecSetConfiguration( p_sys->hfaad, oldcfg );
358                 }
359                 else
360                 {
361                     faacDecClose( p_sys->hfaad );
362                     p_sys->hfaad = hfaad;
363                     p_dec->fmt_out.audio.i_rate = i_rate;
364                     p_dec->fmt_out.audio.i_channels = i_channels;
365                     p_dec->fmt_out.audio.i_physical_channels
366                         = p_dec->fmt_out.audio.i_original_channels
367                         = pi_channels_guessed[i_channels];
368                     date_Init( &p_sys->date, i_rate, 1 );
369                 }
370             }
371
372             /* Flush the buffer */
373             p_sys->i_buffer = 0;
374             block_Release( p_block );
375             return NULL;
376         }
377
378         if( frame.channels <= 0 || frame.channels > 8 || frame.channels == 7 )
379         {
380             msg_Warn( p_dec, "invalid channels count: %i", frame.channels );
381
382             /* Flush the buffer */
383             p_sys->i_buffer -= frame.bytesconsumed;
384             if( p_sys->i_buffer > 0 )
385             {
386                 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
387                          p_sys->i_buffer );
388             }
389             block_Release( p_block );
390             return NULL;
391         }
392
393         if( frame.samples <= 0 )
394         {
395             msg_Warn( p_dec, "decoded zero sample" );
396
397             /* Flush the buffer */
398             p_sys->i_buffer -= frame.bytesconsumed;
399             if( p_sys->i_buffer > 0 )
400             {
401                 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
402                          p_sys->i_buffer );
403             }
404             block_Release( p_block );
405             return NULL;
406         }
407
408         /* We decoded a valid frame */
409         if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
410         {
411             date_Init( &p_sys->date, frame.samplerate, 1 );
412             date_Set( &p_sys->date, p_block->i_pts );
413         }
414         p_block->i_pts = VLC_TS_INVALID;  /* PTS is valid only once */
415
416         p_dec->fmt_out.audio.i_rate = frame.samplerate;
417         p_dec->fmt_out.audio.i_channels = frame.channels;
418
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 )
422         {
423             const char *psz_ext = (b_sbr && frame.ps) ? "SBR+PS" :
424                                     b_sbr ? "SBR" : "PS";
425
426             msg_Dbg( p_dec, "AAC %s (channels: %u, samplerate: %lu)",
427                     psz_ext, frame.channels, frame.samplerate );
428
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 );
433
434             p_sys->b_sbr = b_sbr;
435             p_sys->b_ps = frame.ps;
436         }
437
438         /* Convert frame.channel_position to our own channel values */
439         p_dec->fmt_out.audio.i_physical_channels = 0;
440         const uint32_t nbChannels = frame.channels;
441         unsigned j;
442         for( unsigned i = 0; i < nbChannels; i++ )
443         {
444             /* Find the channel code */
445             for( j = 0; j < MAX_CHANNEL_POSITIONS; j++ )
446             {
447                 if( frame.channel_position[i] == pi_channels_in[j] )
448                     break;
449             }
450             if( j >= MAX_CHANNEL_POSITIONS )
451             {
452                 msg_Warn( p_dec, "unknown channel ordering" );
453                 /* Invent something */
454                 j = i;
455             }
456             /* */
457             p_sys->pi_channel_positions[i] = pi_channels_out[j];
458             if( p_dec->fmt_out.audio.i_physical_channels & pi_channels_out[j] )
459                 frame.channels--; /* We loose a duplicated channel */
460             else
461                 p_dec->fmt_out.audio.i_physical_channels |= pi_channels_out[j];
462         }
463         if ( nbChannels != frame.channels )
464         {
465             p_dec->fmt_out.audio.i_physical_channels
466                 = p_dec->fmt_out.audio.i_original_channels
467                 = pi_channels_guessed[nbChannels];
468         }
469         else
470         {
471             p_dec->fmt_out.audio.i_original_channels =
472                 p_dec->fmt_out.audio.i_physical_channels;
473         }
474         p_dec->fmt_out.audio.i_channels = nbChannels;
475         p_out = decoder_NewAudioBuffer( p_dec, frame.samples / nbChannels );
476         if( p_out == NULL )
477         {
478             p_sys->i_buffer = 0;
479             block_Release( p_block );
480             return NULL;
481         }
482
483         p_out->i_pts = date_Get( &p_sys->date );
484         p_out->i_length = date_Increment( &p_sys->date,
485                                           frame.samples / nbChannels )
486                           - p_out->i_pts;
487
488         DoReordering( (uint32_t *)p_out->p_buffer, samples,
489                       frame.samples / nbChannels, nbChannels,
490                       p_sys->pi_channel_positions );
491
492         p_sys->i_buffer -= frame.bytesconsumed;
493         if( p_sys->i_buffer > 0 )
494         {
495             memmove( p_sys->p_buffer, &p_sys->p_buffer[frame.bytesconsumed],
496                      p_sys->i_buffer );
497         }
498
499         return p_out;
500     }
501
502     block_Release( p_block );
503     return NULL;
504 }
505
506 /*****************************************************************************
507  * Close:
508  *****************************************************************************/
509 static void Close( vlc_object_t *p_this )
510 {
511     decoder_t *p_dec = (decoder_t *)p_this;
512     decoder_sys_t *p_sys = p_dec->p_sys;
513
514     faacDecClose( p_sys->hfaad );
515     free( p_sys->p_buffer );
516     free( p_sys );
517 }
518
519 /*****************************************************************************
520  * DoReordering: do some channel re-ordering (the ac3 channel order is
521  *   different from the aac one).
522  *****************************************************************************/
523 static void DoReordering( uint32_t *p_out, uint32_t *p_in, int i_samples,
524                           int i_nb_channels, uint32_t *pi_chan_positions )
525 {
526     int pi_chan_table[MAX_CHANNEL_POSITIONS] = {0};
527     int i, j, k;
528
529     /* Find the channels mapping */
530     for( i = 0, j = 0; i < MAX_CHANNEL_POSITIONS; i++ )
531     {
532         for( k = 0; k < i_nb_channels; k++ )
533         {
534             if( pi_channels_ordered[i] == pi_chan_positions[k] )
535             {
536                 pi_chan_table[k] = j++;
537                 break;
538             }
539         }
540     }
541
542     /* Do the actual reordering */
543     if( HAVE_FPU )
544         for( i = 0; i < i_samples; i++ )
545             for( j = 0; j < i_nb_channels; j++ )
546                 p_out[i * i_nb_channels + pi_chan_table[j]] =
547                     p_in[i * i_nb_channels + j];
548     else
549         for( i = 0; i < i_samples; i++ )
550             for( j = 0; j < i_nb_channels; j++ )
551                 ((uint16_t *)p_out)[i * i_nb_channels + pi_chan_table[j]] =
552                     ((uint16_t *)p_in)[i * i_nb_channels + j];
553 }
554