]> git.sesse.net Git - vlc/blob - modules/codec/faad.c
* block: added
[vlc] / modules / codec / faad.c
1 /*****************************************************************************
2  * decoder.c: AAC decoder using libfaad2
3  *****************************************************************************
4  * Copyright (C) 2001, 2003 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
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_category( CAT_INPUT );
41     set_subcategory( SUBCAT_INPUT_ACODEC );
42     set_callbacks( Open, Close );
43 vlc_module_end();
44
45 /****************************************************************************
46  * Local prototypes
47  ****************************************************************************/
48 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
49 static void DoReordering( decoder_t *, uint32_t *, uint32_t *, int, int,
50                           uint32_t * );
51
52 #define MAX_CHANNEL_POSITIONS 9
53
54 struct decoder_sys_t
55 {
56     /* faad handler */
57     faacDecHandle *hfaad;
58
59     /* samples */
60     audio_date_t date;
61
62     /* temporary buffer */
63     uint8_t *p_buffer;
64     int     i_buffer;
65     int     i_buffer_size;
66
67     /* Channel positions of the current stream (for re-ordering) */
68     uint32_t pi_channel_positions[MAX_CHANNEL_POSITIONS];
69 };
70
71 static const uint32_t pi_channels_in[MAX_CHANNEL_POSITIONS] =
72     { FRONT_CHANNEL_CENTER, FRONT_CHANNEL_LEFT, FRONT_CHANNEL_RIGHT,
73       SIDE_CHANNEL_LEFT, SIDE_CHANNEL_RIGHT,
74       BACK_CHANNEL_LEFT, BACK_CHANNEL_RIGHT,
75       BACK_CHANNEL_CENTER, LFE_CHANNEL };
76 static const uint32_t pi_channels_out[MAX_CHANNEL_POSITIONS] =
77     { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
78       AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
79       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
80       AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE };
81 static const uint32_t pi_channels_ordered[MAX_CHANNEL_POSITIONS] =
82     { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
83       AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
84       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
85       AOUT_CHAN_CENTER, AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE
86     };
87
88 /*****************************************************************************
89  * OpenDecoder: probe the decoder and return score
90  *****************************************************************************/
91 static int Open( vlc_object_t *p_this )
92 {
93     decoder_t *p_dec = (decoder_t*)p_this;
94     decoder_sys_t *p_sys = p_dec->p_sys;
95     faacDecConfiguration *cfg;
96
97     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','4','a') )
98     {
99         return VLC_EGENERIC;
100     }
101
102     /* Allocate the memory needed to store the decoder's structure */
103     if( ( p_dec->p_sys = p_sys =
104           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
105     {
106         msg_Err( p_dec, "out of memory" );
107         return VLC_EGENERIC;
108     }
109
110     /* Open a faad context */
111     if( ( p_sys->hfaad = faacDecOpen() ) == NULL )
112     {
113         msg_Err( p_dec, "cannot initialize faad" );
114         return VLC_EGENERIC;
115     }
116
117     /* Misc init */
118     aout_DateSet( &p_sys->date, 0 );
119     p_dec->fmt_out.i_cat = AUDIO_ES;
120
121     if (p_this->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
122         p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
123     else
124         p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
125     p_dec->pf_decode_audio = DecodeBlock;
126
127     p_dec->fmt_out.audio.i_physical_channels =
128         p_dec->fmt_out.audio.i_original_channels = 0;
129
130     if( p_dec->fmt_in.i_extra > 0 )
131     {
132         /* We have a decoder config so init the handle */
133         unsigned long i_rate;
134         unsigned char i_channels;
135
136         if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
137                           p_dec->fmt_in.i_extra,
138                           &i_rate, &i_channels ) < 0 )
139         {
140             return VLC_EGENERIC;
141         }
142
143         p_dec->fmt_out.audio.i_rate = i_rate;
144         p_dec->fmt_out.audio.i_channels = i_channels;
145         aout_DateInit( &p_sys->date, i_rate );
146     }
147     else
148     {
149         /* Will be initalised from first frame */
150         p_dec->fmt_out.audio.i_rate = 0;
151         p_dec->fmt_out.audio.i_channels = 0;
152     }
153
154     /* Set the faad config */
155     cfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
156     if (p_this->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
157         cfg->outputFormat = FAAD_FMT_FLOAT;
158     else
159         cfg->outputFormat = FAAD_FMT_16BIT;
160     faacDecSetConfiguration( p_sys->hfaad, cfg );
161
162     /* buffer */
163     p_sys->i_buffer = p_sys->i_buffer_size = 0;
164     p_sys->p_buffer = 0;
165
166     /* Faad2 can't deal with truncated data (eg. from MPEG TS) */
167     p_dec->b_need_packetized = VLC_TRUE;
168
169     return VLC_SUCCESS;
170 }
171
172 /*****************************************************************************
173  * DecodeBlock:
174  *****************************************************************************/
175 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
176 {
177     decoder_sys_t *p_sys = p_dec->p_sys;
178     block_t *p_block;
179
180     if( !pp_block || !*pp_block ) return NULL;
181
182     p_block = *pp_block;
183
184     if( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
185     {
186         block_Release( p_block );
187         return NULL;
188     }
189
190     /* Append the block to the temporary buffer */
191     if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
192     {
193         p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer;
194         p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
195     }
196
197     if( p_block->i_buffer )
198     {
199         memcpy( &p_sys->p_buffer[p_sys->i_buffer],
200                 p_block->p_buffer, p_block->i_buffer );
201         p_sys->i_buffer += p_block->i_buffer;
202         p_block->i_buffer = 0;
203     }
204
205     if( p_dec->fmt_out.audio.i_rate == 0 && p_dec->fmt_in.i_extra > 0 )
206     {
207         /* We have a decoder config so init the handle */
208         unsigned long i_rate;
209         unsigned char i_channels;
210
211         if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
212                           p_dec->fmt_in.i_extra,
213                           &i_rate, &i_channels ) >= 0 )
214         {
215             p_dec->fmt_out.audio.i_rate = i_rate;
216             p_dec->fmt_out.audio.i_channels = i_channels;
217             aout_DateInit( &p_sys->date, i_rate );
218         }
219     }
220
221     if( p_dec->fmt_out.audio.i_rate == 0 && p_sys->i_buffer )
222     {
223         unsigned long i_rate;
224         unsigned char i_channels;
225
226         /* Init faad with the first frame */
227         if( faacDecInit( p_sys->hfaad,
228                          p_sys->p_buffer, p_sys->i_buffer,
229                          &i_rate, &i_channels ) < 0 )
230         {
231             block_Release( p_block );
232             return NULL;
233         }
234
235         p_dec->fmt_out.audio.i_rate = i_rate;
236         p_dec->fmt_out.audio.i_channels = i_channels;
237         aout_DateInit( &p_sys->date, i_rate );
238     }
239
240     if( p_block->i_pts != 0 && p_block->i_pts != aout_DateGet( &p_sys->date ) )
241     {
242         aout_DateSet( &p_sys->date, p_block->i_pts );
243     }
244     else if( !aout_DateGet( &p_sys->date ) )
245     {
246         /* We've just started the stream, wait for the first PTS. */
247         block_Release( p_block );
248         p_sys->i_buffer = 0;
249         return NULL;
250     }
251
252     /* Decode all data */
253     if( p_sys->i_buffer )
254     {
255         void *samples;
256         faacDecFrameInfo frame;
257         aout_buffer_t *p_out;
258         int i, j;
259
260         samples = faacDecDecode( p_sys->hfaad, &frame,
261                                  p_sys->p_buffer, p_sys->i_buffer );
262
263         if( frame.error > 0 )
264         {
265             msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
266
267             /* Flush the buffer */
268             p_sys->i_buffer = 0;
269             block_Release( p_block );
270             return NULL;
271         }
272
273         if( frame.channels <= 0 || frame.channels > 8 || frame.channels == 7 )
274         {
275             msg_Warn( p_dec, "invalid channels count: %i", frame.channels );
276
277             /* Flush the buffer */
278             p_sys->i_buffer -= frame.bytesconsumed;
279             if( p_sys->i_buffer > 0 )
280             {
281                 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
282                          p_sys->i_buffer );
283             }
284             block_Release( p_block );
285             return NULL;
286         }
287
288         if( frame.samples <= 0 )
289         {
290             msg_Warn( p_dec, "decoded zero sample" );
291
292             /* Flush the buffer */
293             p_sys->i_buffer -= frame.bytesconsumed;
294             if( p_sys->i_buffer > 0 )
295             {
296                 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
297                          p_sys->i_buffer );
298             }
299             block_Release( p_block );
300             return NULL;
301         }
302
303         /* We decoded a valid frame */
304         if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
305         {
306             aout_DateInit( &p_sys->date, frame.samplerate );
307             aout_DateSet( &p_sys->date, p_block->i_pts );
308         }
309         p_block->i_pts = 0;  /* PTS is valid only once */
310
311         p_dec->fmt_out.audio.i_rate = frame.samplerate;
312         p_dec->fmt_out.audio.i_channels = frame.channels;
313
314
315         /* Convert frame.channel_position to our own channel values */
316         for( i = 0; i < frame.channels; i++ )
317         {
318             /* Find the channel code */
319             for( j = 0; j < MAX_CHANNEL_POSITIONS; j++ )
320             {
321                 if( frame.channel_position[i] == pi_channels_in[j] )
322                 {
323                     p_sys->pi_channel_positions[i] = pi_channels_out[j];
324                     p_dec->fmt_out.audio.i_physical_channels |=
325                         pi_channels_out[j];
326                     break;
327                 }
328             }
329
330             if( j == MAX_CHANNEL_POSITIONS )
331             {
332                 msg_Warn( p_dec, "unknown channel ordering" );
333                 block_Release( p_block );
334                 return NULL;
335             }
336         }
337         p_dec->fmt_out.audio.i_original_channels =
338             p_dec->fmt_out.audio.i_physical_channels;
339
340         p_out = p_dec->pf_aout_buffer_new(p_dec, frame.samples/frame.channels);
341         if( p_out == NULL )
342         {
343             p_sys->i_buffer = 0;
344             block_Release( p_block );
345             return NULL;
346         }
347
348         p_out->start_date = aout_DateGet( &p_sys->date );
349         p_out->end_date = aout_DateIncrement( &p_sys->date,
350                                               frame.samples / frame.channels );
351
352         DoReordering( p_dec, (uint32_t *)p_out->p_buffer, samples,
353                       frame.samples / frame.channels, frame.channels,
354                       p_sys->pi_channel_positions );
355
356         p_sys->i_buffer -= frame.bytesconsumed;
357         if( p_sys->i_buffer > 0 )
358         {
359             memmove( p_sys->p_buffer, &p_sys->p_buffer[frame.bytesconsumed],
360                      p_sys->i_buffer );
361         }
362
363         return p_out;
364     }
365
366     block_Release( p_block );
367     return NULL;
368 }
369
370 /*****************************************************************************
371  * Close:
372  *****************************************************************************/
373 static void Close( vlc_object_t *p_this )
374 {
375     decoder_t *p_dec = (decoder_t *)p_this;
376     decoder_sys_t *p_sys = p_dec->p_sys;
377
378     faacDecClose( p_sys->hfaad );
379     free( p_sys );
380 }
381
382 /*****************************************************************************
383  * DoReordering: do some channel re-ordering (the ac3 channel order is
384  *   different from the aac one).
385  *****************************************************************************/
386 static void DoReordering( decoder_t *p_dec,
387                           uint32_t *p_out, uint32_t *p_in, int i_samples,
388                           int i_nb_channels, uint32_t *pi_chan_positions )
389 {
390     int pi_chan_table[MAX_CHANNEL_POSITIONS];
391     int i, j, k;
392
393     /* Find the channels mapping */
394     for( i = 0, j = 0; i < MAX_CHANNEL_POSITIONS; i++ )
395     {
396         for( k = 0; k < i_nb_channels; k++ )
397         {
398             if( pi_channels_ordered[i] == pi_chan_positions[k] )
399             {
400                 pi_chan_table[k] = j++;
401                 break;
402             }
403         }
404     }
405
406     /* Do the actual reordering */
407     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_FPU )
408         for( i = 0; i < i_samples; i++ )
409             for( j = 0; j < i_nb_channels; j++ )
410                 p_out[i * i_nb_channels + pi_chan_table[j]] =
411                     p_in[i * i_nb_channels + j];
412     else
413         for( i = 0; i < i_samples; i++ )
414             for( j = 0; j < i_nb_channels; j++ )
415                 ((uint16_t *)p_out)[i * i_nb_channels + pi_chan_table[j]] =
416                     ((uint16_t *)p_in)[i * i_nb_channels + j];
417 }