]> git.sesse.net Git - vlc/blob - modules/codec/faad.c
* modules/codec/faad.c: proper channels re-ordering for multi-channels audio.
[vlc] / modules / codec / faad.c
1 /*****************************************************************************
2  * decoder.c: AAC decoder using libfaad2
3  *****************************************************************************
4  * Copyright (C) 2001, 2003 VideoLAN
5  * $Id: faad.c,v 1.8 2003/12/12 23:15:40 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include <vlc/vlc.h>
26 #include <vlc/aout.h>
27 #include <vlc/decoder.h>
28
29 #include <faad.h>
30
31 /*****************************************************************************
32  * Module descriptor
33  *****************************************************************************/
34 static int  Open( vlc_object_t * );
35 static void Close( vlc_object_t * );
36
37 vlc_module_begin();
38     set_description( _("AAC audio decoder (using libfaad2)") );
39     set_capability( "decoder", 100 );
40     set_callbacks( Open, Close );
41 vlc_module_end();
42
43 /****************************************************************************
44  * Local prototypes
45  ****************************************************************************/
46 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
47 static void DoReordering( decoder_t *, uint32_t *, uint32_t *, int, int,
48                           uint32_t * );
49
50 #define MAX_CHANNEL_POSITIONS 9
51
52 struct decoder_sys_t
53 {
54     /* faad handler */
55     faacDecHandle *hfaad;
56
57     /* samples */
58     audio_date_t date;
59
60     /* temporary buffer */
61     uint8_t *p_buffer;
62     int     i_buffer;
63     int     i_buffer_size;
64
65     /* Channel positions of the current stream (for re-ordering) */
66     uint32_t pi_channel_positions[MAX_CHANNEL_POSITIONS];
67 };
68
69 static const uint32_t pi_channels_in[MAX_CHANNEL_POSITIONS] =
70     { FRONT_CHANNEL_CENTER, FRONT_CHANNEL_LEFT, FRONT_CHANNEL_RIGHT,
71       SIDE_CHANNEL_LEFT, SIDE_CHANNEL_RIGHT,
72       BACK_CHANNEL_LEFT, BACK_CHANNEL_RIGHT,
73       BACK_CHANNEL_CENTER, LFE_CHANNEL };
74 static const uint32_t pi_channels_out[MAX_CHANNEL_POSITIONS] =
75     { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
76       AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
77       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
78       AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE };
79 static const uint32_t pi_channels_ordered[MAX_CHANNEL_POSITIONS] =
80     { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
81       AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
82       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
83       AOUT_CHAN_CENTER, AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE
84     };
85
86 /*****************************************************************************
87  * OpenDecoder: probe the decoder and return score
88  *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
90 {
91     decoder_t *p_dec = (decoder_t*)p_this;
92     decoder_sys_t *p_sys = p_dec->p_sys;
93     faacDecConfiguration *cfg;
94
95     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','4','a') )
96     {
97         return VLC_EGENERIC;
98     }
99
100     /* Allocate the memory needed to store the decoder's structure */
101     if( ( p_dec->p_sys = p_sys =
102           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
103     {
104         msg_Err( p_dec, "out of memory" );
105         return VLC_EGENERIC;
106     }
107
108     /* Open a faad context */
109     if( ( p_sys->hfaad = faacDecOpen() ) == NULL )
110     {
111         msg_Err( p_dec, "Cannot initialize faad" );
112         return VLC_EGENERIC;
113     }
114
115     /* Misc init */
116     aout_DateSet( &p_sys->date, 0 );
117     p_dec->fmt_out.i_cat = AUDIO_ES;
118     p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
119     p_dec->pf_decode_audio = DecodeBlock;
120
121     p_dec->fmt_out.audio.i_physical_channels =
122         p_dec->fmt_out.audio.i_original_channels = 0;
123
124     if( p_dec->fmt_in.i_extra > 0 )
125     {
126         /* We have a decoder config so init the handle */
127         unsigned long i_rate;
128         unsigned char i_channels;
129
130         if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
131                           p_dec->fmt_in.i_extra,
132                           &i_rate, &i_channels ) < 0 )
133         {
134             return VLC_EGENERIC;
135         }
136
137         p_dec->fmt_out.audio.i_rate = i_rate;
138         p_dec->fmt_out.audio.i_channels = i_channels;
139         aout_DateInit( &p_sys->date, i_rate );
140     }
141     else
142     {
143         /* Will be initalised from first frame */
144         p_dec->fmt_out.audio.i_rate = 0;
145         p_dec->fmt_out.audio.i_channels = 0;
146     }
147
148     /* Set the faad config */
149     cfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
150     cfg->outputFormat = FAAD_FMT_FLOAT;
151     faacDecSetConfiguration( p_sys->hfaad, cfg );
152
153     /* buffer */
154     p_sys->i_buffer = p_sys->i_buffer_size = 0;
155     p_sys->p_buffer = 0;
156
157     return VLC_SUCCESS;
158 }
159
160 /*****************************************************************************
161  * DecodeBlock:
162  *****************************************************************************/
163 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
164 {
165     decoder_sys_t *p_sys = p_dec->p_sys;
166     block_t *p_block;
167
168     if( !pp_block || !*pp_block ) return NULL;
169
170     p_block = *pp_block;
171
172     if( p_block->b_discontinuity )
173     {
174         block_Release( p_block );
175         return NULL;
176     }
177
178     /* Append the block to the temporary buffer */
179     if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
180     {
181         p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer;
182         p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
183     }
184
185     if( p_block->i_buffer )
186     {
187         memcpy( &p_sys->p_buffer[p_sys->i_buffer],
188                 p_block->p_buffer, p_block->i_buffer );
189         p_sys->i_buffer += p_block->i_buffer;
190         p_block->i_buffer = 0;
191     }
192
193     if( p_dec->fmt_out.audio.i_rate == 0 && p_sys->i_buffer )
194     {
195         unsigned long i_rate;
196         unsigned char i_channels;
197
198         /* Init faad with the first frame */
199         if( faacDecInit( p_sys->hfaad,
200                          p_sys->p_buffer, p_sys->i_buffer,
201                          &i_rate, &i_channels ) < 0 )
202         {
203             block_Release( p_block );
204             return NULL;
205         }
206
207         p_dec->fmt_out.audio.i_rate = i_rate;
208         p_dec->fmt_out.audio.i_channels = i_channels;
209         aout_DateInit( &p_sys->date, i_rate );
210     }
211
212     if( p_block->i_pts != 0 && p_block->i_pts != aout_DateGet( &p_sys->date ) )
213     {
214         aout_DateSet( &p_sys->date, p_block->i_pts );
215     }
216     else if( !aout_DateGet( &p_sys->date ) )
217     {
218         /* We've just started the stream, wait for the first PTS. */
219         block_Release( p_block );
220         p_sys->i_buffer = 0;
221         return NULL;
222     }
223
224     /* Decode all data */
225     if( p_sys->i_buffer )
226     {
227         void *samples;
228         faacDecFrameInfo frame;
229         aout_buffer_t *p_out;
230         int i, j;
231
232         samples = faacDecDecode( p_sys->hfaad, &frame,
233                                  p_sys->p_buffer, p_sys->i_buffer );
234
235         if( frame.error > 0 )
236         {
237             msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
238
239             /* Flush the buffer */
240             p_sys->i_buffer = 0;
241             block_Release( p_block );
242             return NULL;
243         }
244
245         if( frame.channels <= 0 || frame.channels > 8 || frame.channels == 7 )
246         {
247             msg_Warn( p_dec, "invalid channels count: %i", frame.channels );
248
249             /* Flush the buffer */
250             p_sys->i_buffer -= frame.bytesconsumed;
251             if( p_sys->i_buffer > 0 )
252             {
253                 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
254                          p_sys->i_buffer );
255             }
256             block_Release( p_block );
257             return NULL;
258         }
259
260         if( frame.samples <= 0 )
261         {
262             msg_Warn( p_dec, "decoded zero samples" );
263
264             /* Flush the buffer */
265             p_sys->i_buffer -= frame.bytesconsumed;
266             if( p_sys->i_buffer > 0 )
267             {
268                 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
269                          p_sys->i_buffer );
270             }
271             block_Release( p_block );
272             return NULL;
273         }
274
275         /* We decoded a valid frame */
276         if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
277         {
278             aout_DateInit( &p_sys->date, frame.samplerate );
279             aout_DateSet( &p_sys->date, p_block->i_pts );
280         }
281         p_block->i_pts = 0;  /* PTS is valid only once */
282
283         p_dec->fmt_out.audio.i_rate = frame.samplerate;
284         p_dec->fmt_out.audio.i_channels = frame.channels;
285
286
287         /* Convert frame.channel_position to our own channel values */
288         for( i = 0; i < frame.channels; i++ )
289         {
290             /* Find the channel code */
291             for( j = 0; j < MAX_CHANNEL_POSITIONS; j++ )
292             {
293                 if( frame.channel_position[i] == pi_channels_in[j] )
294                 {
295                     p_sys->pi_channel_positions[i] = pi_channels_out[j];
296                     p_dec->fmt_out.audio.i_physical_channels |=
297                         pi_channels_out[j];
298                     break;
299                 }
300             }
301         }
302         p_dec->fmt_out.audio.i_original_channels =
303             p_dec->fmt_out.audio.i_physical_channels;
304
305         p_out = p_dec->pf_aout_buffer_new(p_dec, frame.samples/frame.channels);
306         if( p_out == NULL )
307         {
308             p_sys->i_buffer = 0;
309             block_Release( p_block );
310             return NULL;
311         }
312
313         p_out->start_date = aout_DateGet( &p_sys->date );
314         p_out->end_date = aout_DateIncrement( &p_sys->date,
315                                               frame.samples / frame.channels );
316
317         DoReordering( p_dec, (uint32_t *)p_out->p_buffer, samples,
318                       frame.samples / frame.channels, frame.channels,
319                       p_sys->pi_channel_positions );
320
321         p_sys->i_buffer -= frame.bytesconsumed;
322         if( p_sys->i_buffer > 0 )
323         {
324             memmove( p_sys->p_buffer, &p_sys->p_buffer[frame.bytesconsumed],
325                      p_sys->i_buffer );
326         }
327
328         return p_out;
329     }
330
331     block_Release( p_block );
332     return NULL;
333 }
334
335 /*****************************************************************************
336  * Close:
337  *****************************************************************************/
338 static void Close( vlc_object_t *p_this )
339 {
340     decoder_t *p_dec = (decoder_t *)p_this;
341     decoder_sys_t *p_sys = p_dec->p_sys;
342
343     faacDecClose( p_sys->hfaad );
344     free( p_sys );
345 }
346
347 /*****************************************************************************
348  * DoReordering: do some channel re-ordering (the ac3 channel order is
349  *   different from the aac one).
350  *****************************************************************************/
351 static void DoReordering( decoder_t *p_dec,
352                           uint32_t *p_out, uint32_t *p_in, int i_samples,
353                           int i_nb_channels, uint32_t *pi_chan_positions )
354 {
355     int pi_chan_table[MAX_CHANNEL_POSITIONS];
356     int i, j, k;
357
358     /* Find the channels mapping */
359     for( i = 0, j = 0; i < MAX_CHANNEL_POSITIONS; i++ )
360     {
361         for( k = 0; k < i_nb_channels; k++ )
362         {
363             if( pi_channels_ordered[i] == pi_chan_positions[k] )
364             {
365                 pi_chan_table[k] = j++;
366                 break;
367             }
368         }
369     }
370
371     /* Do the actual reordering */
372     for( i = 0; i < i_samples; i++ )
373     {
374         for( j = 0; j < i_nb_channels; j++ )
375         {
376             p_out[i * i_nb_channels + pi_chan_table[j]] =
377                 p_in[i * i_nb_channels + j];
378         }
379     }
380 }