1 /*****************************************************************************
2 * decoder.c: AAC decoder using libfaad2
3 *****************************************************************************
4 * Copyright (C) 2001, 2003 VideoLAN
5 * $Id: faad.c,v 1.14 2004/02/25 17:48:52 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
27 #include <vlc/decoder.h>
31 /*****************************************************************************
33 *****************************************************************************/
34 static int Open( vlc_object_t * );
35 static void Close( vlc_object_t * );
38 set_description( _("AAC audio decoder (using libfaad2)") );
39 set_capability( "decoder", 100 );
40 set_callbacks( Open, Close );
43 /****************************************************************************
45 ****************************************************************************/
46 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
47 static void DoReordering( decoder_t *, uint32_t *, uint32_t *, int, int,
50 #define MAX_CHANNEL_POSITIONS 9
60 /* temporary buffer */
65 /* Channel positions of the current stream (for re-ordering) */
66 uint32_t pi_channel_positions[MAX_CHANNEL_POSITIONS];
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
86 /*****************************************************************************
87 * OpenDecoder: probe the decoder and return score
88 *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
91 decoder_t *p_dec = (decoder_t*)p_this;
92 decoder_sys_t *p_sys = p_dec->p_sys;
93 faacDecConfiguration *cfg;
95 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','4','a') )
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 )
104 msg_Err( p_dec, "out of memory" );
108 /* Open a faad context */
109 if( ( p_sys->hfaad = faacDecOpen() ) == NULL )
111 msg_Err( p_dec, "cannot initialize faad" );
116 aout_DateSet( &p_sys->date, 0 );
117 p_dec->fmt_out.i_cat = AUDIO_ES;
119 if (p_this->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
120 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
122 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
123 p_dec->pf_decode_audio = DecodeBlock;
125 p_dec->fmt_out.audio.i_physical_channels =
126 p_dec->fmt_out.audio.i_original_channels = 0;
128 if( p_dec->fmt_in.i_extra > 0 )
130 /* We have a decoder config so init the handle */
131 unsigned long i_rate;
132 unsigned char i_channels;
134 if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
135 p_dec->fmt_in.i_extra,
136 &i_rate, &i_channels ) < 0 )
141 p_dec->fmt_out.audio.i_rate = i_rate;
142 p_dec->fmt_out.audio.i_channels = i_channels;
143 aout_DateInit( &p_sys->date, i_rate );
147 /* Will be initalised from first frame */
148 p_dec->fmt_out.audio.i_rate = 0;
149 p_dec->fmt_out.audio.i_channels = 0;
152 /* Set the faad config */
153 cfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
154 if (p_this->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
155 cfg->outputFormat = FAAD_FMT_FLOAT;
157 cfg->outputFormat = FAAD_FMT_16BIT;
158 faacDecSetConfiguration( p_sys->hfaad, cfg );
161 p_sys->i_buffer = p_sys->i_buffer_size = 0;
167 /*****************************************************************************
169 *****************************************************************************/
170 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
172 decoder_sys_t *p_sys = p_dec->p_sys;
175 if( !pp_block || !*pp_block ) return NULL;
179 if( p_block->i_flags&BLOCK_FLAG_DISCONTINUITY )
181 block_Release( p_block );
185 /* Append the block to the temporary buffer */
186 if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
188 p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer;
189 p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
192 if( p_block->i_buffer )
194 memcpy( &p_sys->p_buffer[p_sys->i_buffer],
195 p_block->p_buffer, p_block->i_buffer );
196 p_sys->i_buffer += p_block->i_buffer;
197 p_block->i_buffer = 0;
200 if( p_dec->fmt_out.audio.i_rate == 0 && p_sys->i_buffer )
202 unsigned long i_rate;
203 unsigned char i_channels;
205 /* Init faad with the first frame */
206 if( faacDecInit( p_sys->hfaad,
207 p_sys->p_buffer, p_sys->i_buffer,
208 &i_rate, &i_channels ) < 0 )
210 block_Release( p_block );
214 p_dec->fmt_out.audio.i_rate = i_rate;
215 p_dec->fmt_out.audio.i_channels = i_channels;
216 aout_DateInit( &p_sys->date, i_rate );
219 if( p_block->i_pts != 0 && p_block->i_pts != aout_DateGet( &p_sys->date ) )
221 aout_DateSet( &p_sys->date, p_block->i_pts );
223 else if( !aout_DateGet( &p_sys->date ) )
225 /* We've just started the stream, wait for the first PTS. */
226 block_Release( p_block );
231 /* Decode all data */
232 if( p_sys->i_buffer )
235 faacDecFrameInfo frame;
236 aout_buffer_t *p_out;
239 samples = faacDecDecode( p_sys->hfaad, &frame,
240 p_sys->p_buffer, p_sys->i_buffer );
242 if( frame.error > 0 )
244 msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
246 /* Flush the buffer */
248 block_Release( p_block );
252 if( frame.channels <= 0 || frame.channels > 8 || frame.channels == 7 )
254 msg_Warn( p_dec, "invalid channels count: %i", frame.channels );
256 /* Flush the buffer */
257 p_sys->i_buffer -= frame.bytesconsumed;
258 if( p_sys->i_buffer > 0 )
260 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
263 block_Release( p_block );
267 if( frame.samples <= 0 )
269 msg_Warn( p_dec, "decoded zero sample" );
271 /* Flush the buffer */
272 p_sys->i_buffer -= frame.bytesconsumed;
273 if( p_sys->i_buffer > 0 )
275 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
278 block_Release( p_block );
282 /* We decoded a valid frame */
283 if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
285 aout_DateInit( &p_sys->date, frame.samplerate );
286 aout_DateSet( &p_sys->date, p_block->i_pts );
288 p_block->i_pts = 0; /* PTS is valid only once */
290 p_dec->fmt_out.audio.i_rate = frame.samplerate;
291 p_dec->fmt_out.audio.i_channels = frame.channels;
294 /* Convert frame.channel_position to our own channel values */
295 for( i = 0; i < frame.channels; i++ )
297 /* Find the channel code */
298 for( j = 0; j < MAX_CHANNEL_POSITIONS; j++ )
300 if( frame.channel_position[i] == pi_channels_in[j] )
302 p_sys->pi_channel_positions[i] = pi_channels_out[j];
303 p_dec->fmt_out.audio.i_physical_channels |=
309 p_dec->fmt_out.audio.i_original_channels =
310 p_dec->fmt_out.audio.i_physical_channels;
312 p_out = p_dec->pf_aout_buffer_new(p_dec, frame.samples/frame.channels);
316 block_Release( p_block );
320 p_out->start_date = aout_DateGet( &p_sys->date );
321 p_out->end_date = aout_DateIncrement( &p_sys->date,
322 frame.samples / frame.channels );
324 DoReordering( p_dec, (uint32_t *)p_out->p_buffer, samples,
325 frame.samples / frame.channels, frame.channels,
326 p_sys->pi_channel_positions );
328 p_sys->i_buffer -= frame.bytesconsumed;
329 if( p_sys->i_buffer > 0 )
331 memmove( p_sys->p_buffer, &p_sys->p_buffer[frame.bytesconsumed],
338 block_Release( p_block );
342 /*****************************************************************************
344 *****************************************************************************/
345 static void Close( vlc_object_t *p_this )
347 decoder_t *p_dec = (decoder_t *)p_this;
348 decoder_sys_t *p_sys = p_dec->p_sys;
350 faacDecClose( p_sys->hfaad );
354 /*****************************************************************************
355 * DoReordering: do some channel re-ordering (the ac3 channel order is
356 * different from the aac one).
357 *****************************************************************************/
358 static void DoReordering( decoder_t *p_dec,
359 uint32_t *p_out, uint32_t *p_in, int i_samples,
360 int i_nb_channels, uint32_t *pi_chan_positions )
362 int pi_chan_table[MAX_CHANNEL_POSITIONS];
365 /* Find the channels mapping */
366 for( i = 0, j = 0; i < MAX_CHANNEL_POSITIONS; i++ )
368 for( k = 0; k < i_nb_channels; k++ )
370 if( pi_channels_ordered[i] == pi_chan_positions[k] )
372 pi_chan_table[k] = j++;
378 /* Do the actual reordering */
379 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_FPU )
380 for( i = 0; i < i_samples; i++ )
381 for( j = 0; j < i_nb_channels; j++ )
382 p_out[i * i_nb_channels + pi_chan_table[j]] =
383 p_in[i * i_nb_channels + j];
385 for( i = 0; i < i_samples; i++ )
386 for( j = 0; j < i_nb_channels; j++ )
387 ((uint16_t *)p_out)[i * i_nb_channels + pi_chan_table[j]] =
388 ((uint16_t *)p_in)[i * i_nb_channels + j];