1 /*****************************************************************************
2 * decoder.c: AAC decoder using libfaad2
3 *****************************************************************************
4 * Copyright (C) 2001, 2003 VideoLAN
5 * $Id: faad.c,v 1.3 2003/11/05 01:47:40 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 *****************************************************************************/
27 #include <vlc/decoder.h>
28 #include <vlc/input.h>
33 /*****************************************************************************
35 *****************************************************************************/
36 static int Open( vlc_object_t * );
39 set_description( _("AAC audio decoder (using libfaad2)") );
40 set_capability( "decoder", 60 );
41 set_callbacks( Open, NULL );
45 /****************************************************************************
47 ****************************************************************************/
48 static int InitDecoder ( decoder_t * );
49 static int RunDecoder ( decoder_t *, block_t * );
50 static int EndDecoder ( decoder_t * );
58 audio_sample_format_t output_format;
59 aout_instance_t * p_aout; /* opaque */
60 aout_input_t * p_aout_input; /* opaque */
65 /* temporary buffer */
71 /*****************************************************************************
72 * OpenDecoder: probe the decoder and return score
73 *****************************************************************************/
74 static int Open( vlc_object_t *p_this )
76 decoder_t *p_dec = (decoder_t*)p_this;
78 if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('m','p','4','a') )
83 p_dec->pf_init = InitDecoder;
84 p_dec->pf_decode = RunDecoder;
85 p_dec->pf_end = EndDecoder;
87 p_dec->p_sys = malloc( sizeof( decoder_sys_t ) );
92 static unsigned int pi_channels_maps[7] =
96 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
97 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
98 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
99 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
101 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_REARCENTER
104 /*****************************************************************************
106 *****************************************************************************/
107 static int InitDecoder ( decoder_t *p_dec )
109 decoder_sys_t *p_sys = p_dec->p_sys;
110 WAVEFORMATEX wf, *p_wf;
111 faacDecConfiguration *cfg;
113 if( ( p_wf = (WAVEFORMATEX*)p_dec->p_fifo->p_waveformatex ) == NULL )
116 memset( p_wf, 0, sizeof( WAVEFORMATEX ) );
119 /* Open a faad context */
120 if( ( p_sys->hfaad = faacDecOpen() ) == NULL )
122 msg_Err( p_dec, "Cannot initialize faad" );
126 if( p_wf->cbSize > 0 )
128 /* We have a decoder config so init the handle */
129 unsigned long i_rate;
130 unsigned char i_channels;
132 if( faacDecInit2( p_sys->hfaad,
133 (uint8_t*)&p_wf[1], p_wf->cbSize,
134 &i_rate, &i_channels ) < 0 )
139 p_sys->output_format.i_rate = i_rate;
140 p_sys->output_format.i_physical_channels =
141 p_sys->output_format.i_original_channels =
142 pi_channels_maps[i_channels];
146 /* Will be initalised from first frame */
147 p_sys->output_format.i_rate = 0;
148 p_sys->output_format.i_physical_channels =
149 p_sys->output_format.i_original_channels = 0;
151 p_sys->output_format.i_format = VLC_FOURCC('f','l','3','2');
153 p_sys->p_aout = NULL;
154 p_sys->p_aout_input = NULL;
156 /* set the faad config */
157 cfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
158 cfg->outputFormat = FAAD_FMT_FLOAT;
159 faacDecSetConfiguration( p_sys->hfaad, cfg );
163 p_sys->i_buffer_size = 10000;
164 p_sys->p_buffer = malloc( p_sys->i_buffer_size );
169 /*****************************************************************************
171 *****************************************************************************/
172 static int RunDecoder ( decoder_t *p_dec, block_t *p_block )
174 decoder_sys_t *p_sys = p_dec->p_sys;
177 mtime_t i_pts = p_block->i_pts;
179 /* Append the block to the temporary buffer */
180 if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
182 p_sys->i_buffer_size += p_block->i_buffer;
183 p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
185 memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer, p_block->i_buffer );
186 p_sys->i_buffer += p_block->i_buffer;
188 if( p_sys->output_format.i_rate == 0 )
190 unsigned long i_rate;
191 unsigned char i_channels;
193 /* Init faad with the first frame */
194 if( faacDecInit( p_sys->hfaad,
195 p_sys->p_buffer, p_sys->i_buffer,
196 &i_rate, &i_channels ) < 0 )
198 block_Release( p_block );
201 p_sys->output_format.i_rate = i_rate;
202 p_sys->output_format.i_physical_channels =
203 p_sys->output_format.i_original_channels =
204 pi_channels_maps[i_channels];
207 /* Decode all data */
208 while( i_used < p_sys->i_buffer )
211 faacDecFrameInfo frame;
214 samples = faacDecDecode( p_sys->hfaad, &frame,
215 &p_sys->p_buffer[i_used], p_sys->i_buffer - i_used );
217 if( frame.error > 0 )
219 msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
220 /* flush the buffer */
222 block_Release( p_block );
225 if( frame.channels <= 0 || frame.channels > 6 )
227 msg_Warn( p_dec, "invalid channels count" );
228 /* flush the buffer */
230 block_Release( p_block );
233 if( frame.samples <= 0 )
235 msg_Warn( p_dec, "decoded zero samples" );
236 /* flush the buffer */
238 block_Release( p_block );
242 /* we have decoded a valid frame */
243 /* msg_Dbg( p_dec, "consumed %d for %dHz %dc %lld", frame.bytesconsumed, frame.samplerate, frame.channels, i_pts ); */
244 i_used += frame.bytesconsumed;
247 /* create/recreate the output */
248 if( p_sys->p_aout_input &&
249 ( p_sys->output_format.i_original_channels != pi_channels_maps[frame.channels] ||
250 p_sys->output_format.i_rate != frame.samplerate ) )
252 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
253 p_sys->p_aout_input = NULL;
256 if( p_sys->p_aout_input == NULL )
258 p_sys->output_format.i_physical_channels =
259 p_sys->output_format.i_original_channels = pi_channels_maps[frame.channels];
260 p_sys->output_format.i_rate = frame.samplerate;
262 aout_DateInit( &p_sys->date, p_sys->output_format.i_rate );
263 aout_DateSet( &p_sys->date, 0 );
265 p_sys->p_aout_input = aout_DecNew( p_dec, &p_sys->p_aout, &p_sys->output_format );
268 if( p_sys->p_aout_input == NULL )
270 msg_Err( p_dec, "cannot create aout" );
271 block_Release( p_block );
275 if( i_pts != 0 && i_pts != aout_DateGet( &p_sys->date ) )
277 aout_DateSet( &p_sys->date, i_pts );
279 else if( !aout_DateGet( &p_sys->date ) )
281 /* Wait for a dated packet */
282 msg_Dbg( p_dec, "no date" );
286 i_pts = 0; /* PTS is valid only once */
288 if( ( out = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
289 frame.samples / frame.channels ) ) == NULL )
291 msg_Err( p_dec, "cannot get a new buffer" );
292 block_Release( p_block );
295 out->start_date = aout_DateGet( &p_sys->date );
296 out->end_date = aout_DateIncrement( &p_sys->date,
297 frame.samples / frame.channels );
298 memcpy( out->p_buffer, samples, out->i_nb_bytes );
300 aout_DecPlay( p_sys->p_aout, p_sys->p_aout_input, out );
303 p_sys->i_buffer -= i_used;
304 if( p_sys->i_buffer > 0 )
306 memmove( &p_sys->p_buffer[0], &p_sys->p_buffer[i_used], p_sys->i_buffer );
309 block_Release( p_block );
313 /*****************************************************************************
315 *****************************************************************************/
316 static int EndDecoder ( decoder_t *p_dec )
318 decoder_sys_t *p_sys = p_dec->p_sys;
320 if( p_sys->p_aout_input )
322 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
325 faacDecClose( p_sys->hfaad );