1 /*****************************************************************************
2 * decoder.c: AAC decoder using libfaad2
3 *****************************************************************************
4 * Copyright (C) 2001, 2003 VideoLAN
5 * $Id: faad.c,v 1.1 2003/11/03 22:30:15 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 )
200 p_sys->output_format.i_rate = i_rate;
201 p_sys->output_format.i_physical_channels =
202 p_sys->output_format.i_original_channels =
203 pi_channels_maps[i_channels];
206 /* Decode all data */
207 while( i_used < p_sys->i_buffer )
210 faacDecFrameInfo frame;
213 samples = faacDecDecode( p_sys->hfaad, &frame,
214 &p_sys->p_buffer[i_used], p_sys->i_buffer - i_used );
216 if( frame.error > 0 )
218 msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
219 /* flush the buffer */
223 if( frame.channels <= 0 || frame.channels > 6 )
225 msg_Warn( p_dec, "invalid channels count" );
226 /* flush the buffer */
230 if( frame.samples <= 0 )
232 msg_Warn( p_dec, "decoded zero samples" );
233 /* flush the buffer */
238 /* we have decoded a valid frame */
239 /* msg_Dbg( p_dec, "consumed %d for %dHz %dc %lld", frame.bytesconsumed, frame.samplerate, frame.channels, i_pts ); */
240 i_used += frame.bytesconsumed;
243 /* create/recreate the output */
244 if( p_sys->p_aout_input &&
245 ( p_sys->output_format.i_original_channels != pi_channels_maps[frame.channels] ||
246 p_sys->output_format.i_rate != frame.samplerate ) )
248 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
249 p_sys->p_aout_input = NULL;
252 if( p_sys->p_aout_input == NULL )
254 p_sys->output_format.i_physical_channels =
255 p_sys->output_format.i_original_channels = pi_channels_maps[frame.channels];
256 p_sys->output_format.i_rate = frame.samplerate;
258 aout_DateInit( &p_sys->date, p_sys->output_format.i_rate );
259 aout_DateSet( &p_sys->date, 0 );
261 p_sys->p_aout_input = aout_DecNew( p_dec, &p_sys->p_aout, &p_sys->output_format );
264 if( p_sys->p_aout_input == NULL )
266 msg_Err( p_dec, "cannot create aout" );
270 if( i_pts != 0 && i_pts != aout_DateGet( &p_sys->date ) )
272 aout_DateSet( &p_sys->date, i_pts );
274 else if( !aout_DateGet( &p_sys->date ) )
276 /* Wait for a dated packet */
277 msg_Dbg( p_dec, "no date" );
281 i_pts = 0; /* PTS is valid only once */
283 if( ( out = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
284 frame.samples / frame.channels ) ) == NULL )
286 msg_Err( p_dec, "cannot get a new buffer" );
289 out->start_date = aout_DateGet( &p_sys->date );
290 out->end_date = aout_DateIncrement( &p_sys->date,
291 frame.samples / frame.channels );
292 memcpy( out->p_buffer, samples, out->i_nb_bytes );
294 aout_DecPlay( p_sys->p_aout, p_sys->p_aout_input, out );
297 p_sys->i_buffer -= i_used;
298 if( p_sys->i_buffer > 0 )
300 memmove( &p_sys->p_buffer[0], &p_sys->p_buffer[i_used], p_sys->i_buffer );
306 /*****************************************************************************
308 *****************************************************************************/
309 static int EndDecoder ( decoder_t *p_dec )
311 decoder_sys_t *p_sys = p_dec->p_sys;
313 if( p_sys->p_aout_input )
315 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
318 faacDecClose( p_sys->hfaad );