1 /*****************************************************************************
2 * decoder.c: AAC decoder using libfaad2
3 *****************************************************************************
4 * Copyright (C) 2001, 2003 VideoLAN
5 * $Id: faad.c,v 1.5 2003/11/22 23:39:14 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
26 #include <vlc/decoder.h>
30 /*****************************************************************************
32 *****************************************************************************/
33 static int Open( vlc_object_t * );
34 static void Close( vlc_object_t * );
37 set_description( _("AAC audio decoder (using libfaad2)") );
38 set_capability( "decoder", 60 );
39 set_callbacks( Open, Close );
43 /****************************************************************************
45 ****************************************************************************/
46 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
56 /* temporary buffer */
62 static unsigned int pi_channels_maps[7] =
66 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
67 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
68 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT |
70 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
73 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
74 AOUT_CHAN_REARRIGHT | AOUT_CHAN_REARCENTER
77 /*****************************************************************************
78 * OpenDecoder: probe the decoder and return score
79 *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
82 decoder_t *p_dec = (decoder_t*)p_this;
83 decoder_sys_t *p_sys = p_dec->p_sys;
84 faacDecConfiguration *cfg;
86 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','4','a') )
91 /* Allocate the memory needed to store the decoder's structure */
92 if( ( p_dec->p_sys = p_sys =
93 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
95 msg_Err( p_dec, "out of memory" );
99 /* Open a faad context */
100 if( ( p_sys->hfaad = faacDecOpen() ) == NULL )
102 msg_Err( p_dec, "Cannot initialize faad" );
107 aout_DateSet( &p_sys->date, 0 );
108 p_dec->fmt_out.i_cat = AUDIO_ES;
109 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
110 p_dec->pf_decode_audio = DecodeBlock;
112 if( p_dec->fmt_in.i_extra > 0 )
114 /* We have a decoder config so init the handle */
115 unsigned long i_rate;
116 unsigned char i_channels;
118 if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
119 p_dec->fmt_in.i_extra,
120 &i_rate, &i_channels ) < 0 )
125 p_dec->fmt_out.audio.i_rate = i_rate;
126 p_dec->fmt_out.audio.i_channels = i_channels;
127 p_dec->fmt_out.audio.i_physical_channels =
128 p_dec->fmt_out.audio.i_original_channels =
129 pi_channels_maps[i_channels];
131 aout_DateInit( &p_sys->date, i_rate );
135 /* Will be initalised from first frame */
136 p_dec->fmt_out.audio.i_rate = 0;
137 p_dec->fmt_out.audio.i_channels = 0;
138 p_dec->fmt_out.audio.i_physical_channels =
139 p_dec->fmt_out.audio.i_original_channels = 0;
142 /* Set the faad config */
143 cfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
144 cfg->outputFormat = FAAD_FMT_FLOAT;
145 faacDecSetConfiguration( p_sys->hfaad, cfg );
149 p_sys->i_buffer_size = 10000;
150 p_sys->p_buffer = malloc( p_sys->i_buffer_size );
155 /*****************************************************************************
157 *****************************************************************************/
158 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
160 decoder_sys_t *p_sys = p_dec->p_sys;
163 if( !pp_block || !*pp_block ) return NULL;
167 /* Append the block to the temporary buffer */
168 if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
170 p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer;
171 p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
174 if( p_block->i_buffer )
176 memcpy( &p_sys->p_buffer[p_sys->i_buffer],
177 p_block->p_buffer, p_block->i_buffer );
178 p_sys->i_buffer += p_block->i_buffer;
179 p_block->i_buffer = 0;
182 if( p_dec->fmt_out.audio.i_rate == 0 && p_sys->i_buffer )
184 unsigned long i_rate;
185 unsigned char i_channels;
187 /* Init faad with the first frame */
188 if( faacDecInit( p_sys->hfaad,
189 p_sys->p_buffer, p_sys->i_buffer,
190 &i_rate, &i_channels ) < 0 )
192 block_Release( p_block );
196 p_dec->fmt_out.audio.i_rate = i_rate;
197 p_dec->fmt_out.audio.i_channels = i_channels;
198 p_dec->fmt_out.audio.i_physical_channels =
199 p_dec->fmt_out.audio.i_original_channels =
200 pi_channels_maps[i_channels];
202 aout_DateInit( &p_sys->date, i_rate );
205 if( p_block->i_pts != 0 && p_block->i_pts != aout_DateGet( &p_sys->date ) )
207 aout_DateSet( &p_sys->date, p_block->i_pts );
209 else if( !aout_DateGet( &p_sys->date ) )
211 /* We've just started the stream, wait for the first PTS. */
212 block_Release( p_block );
217 /* Decode all data */
218 if( p_sys->i_buffer )
221 faacDecFrameInfo frame;
222 aout_buffer_t *p_out;
224 samples = faacDecDecode( p_sys->hfaad, &frame,
225 p_sys->p_buffer, p_sys->i_buffer );
227 if( frame.error > 0 )
229 msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
231 /* Flush the buffer */
233 block_Release( p_block );
237 if( frame.channels <= 0 || frame.channels > 6 )
239 msg_Warn( p_dec, "invalid channels count" );
241 /* Flush the buffer */
243 block_Release( p_block );
247 if( frame.samples <= 0 )
249 msg_Warn( p_dec, "decoded zero samples" );
251 /* Flush the buffer */
253 block_Release( p_block );
257 /* We decoded a valid frame */
258 if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
260 aout_DateInit( &p_sys->date, frame.samplerate );
261 aout_DateSet( &p_sys->date, p_block->i_pts );
263 p_block->i_pts = 0; /* PTS is valid only once */
265 p_dec->fmt_out.audio.i_rate = frame.samplerate;
266 p_dec->fmt_out.audio.i_channels = frame.channels;
267 p_dec->fmt_out.audio.i_physical_channels =
268 p_dec->fmt_out.audio.i_original_channels =
269 pi_channels_maps[frame.channels];
271 p_out = p_dec->pf_aout_buffer_new( p_dec,
272 frame.samples / frame.channels );
276 block_Release( p_block );
280 p_out->start_date = aout_DateGet( &p_sys->date );
281 p_out->end_date = aout_DateIncrement( &p_sys->date,
282 frame.samples / frame.channels );
284 memcpy( p_out->p_buffer, samples, p_out->i_nb_bytes );
286 p_sys->i_buffer -= frame.bytesconsumed;
287 if( p_sys->i_buffer > 0 )
289 memmove( p_sys->p_buffer, &p_sys->p_buffer[frame.bytesconsumed],
296 block_Release( p_block );
300 /*****************************************************************************
302 *****************************************************************************/
303 static void Close( vlc_object_t *p_this )
305 decoder_t *p_dec = (decoder_t *)p_this;
306 decoder_sys_t *p_sys = p_dec->p_sys;
308 faacDecClose( p_sys->hfaad );