1 /*****************************************************************************
2 * audio.c: audio decoder using ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2003 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
34 #include <vlc_codec.h>
35 #include <vlc_avcodec.h>
38 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
39 # include <libavcodec/avcodec.h>
40 #elif defined(HAVE_FFMPEG_AVCODEC_H)
41 # include <ffmpeg/avcodec.h>
48 static const unsigned int pi_channels_maps[9] =
52 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
53 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
54 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
55 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
56 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
57 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
58 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
59 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
60 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
61 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
62 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
63 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
64 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
67 /*****************************************************************************
68 * decoder_sys_t : decoder descriptor
69 *****************************************************************************/
74 /* Temporary buffer for libavcodec */
80 audio_sample_format_t aout_format;
81 audio_date_t end_date;
93 static void SetupOutputCodec( decoder_t *p_dec );
95 /*****************************************************************************
96 * InitAudioDec: initialize audio decoder
97 *****************************************************************************
98 * The ffmpeg codec will be opened, some memory allocated.
99 *****************************************************************************/
100 int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
101 AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
103 decoder_sys_t *p_sys;
105 /* Allocate the memory needed to store the decoder's structure */
106 if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
111 p_sys->p_context = p_context;
112 p_sys->p_codec = p_codec;
113 p_sys->i_codec_id = i_codec_id;
114 p_sys->psz_namecodec = psz_namecodec;
115 p_sys->b_delayed_open = false;
117 /* ***** Fill p_context with init values ***** */
118 p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
119 p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
120 if( !p_dec->fmt_in.audio.i_physical_channels )
122 msg_Warn( p_dec, "Physical channel configuration not set : guessing" );
123 p_dec->fmt_in.audio.i_original_channels =
124 p_dec->fmt_in.audio.i_physical_channels =
125 pi_channels_maps[p_sys->p_context->channels];
128 p_dec->fmt_out.audio.i_physical_channels =
129 p_dec->fmt_out.audio.i_original_channels =
130 p_dec->fmt_in.audio.i_physical_channels;
132 p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
133 p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
134 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
135 p_sys->p_context->bits_per_sample = p_dec->fmt_in.audio.i_bitspersample;
137 p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.audio.i_bitspersample;
140 if( p_dec->fmt_in.i_extra > 0 )
142 const uint8_t * const p_src = p_dec->fmt_in.p_extra;
146 if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
149 i_size = p_dec->fmt_in.i_extra - 8;
151 else if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'a', 'l', 'a', 'c' ) )
153 static const uint8_t p_pattern[] = { 0, 0, 0, 36, 'a', 'l', 'a', 'c' };
154 /* Find alac atom XXX it is a bit ugly */
155 for( i_offset = 0; i_offset < p_dec->fmt_in.i_extra - sizeof(p_pattern); i_offset++ )
157 if( !memcmp( &p_src[i_offset], p_pattern, sizeof(p_pattern) ) )
160 i_size = __MIN( p_dec->fmt_in.i_extra - i_offset, 36 );
167 i_size = p_dec->fmt_in.i_extra;
172 p_sys->p_context->extradata =
173 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
174 if( p_sys->p_context->extradata )
176 uint8_t *p_dst = p_sys->p_context->extradata;
178 p_sys->p_context->extradata_size = i_size;
180 memcpy( &p_dst[0], &p_src[i_offset], i_size );
181 memset( &p_dst[i_size], 0, FF_INPUT_BUFFER_PADDING_SIZE );
187 p_sys->p_context->extradata_size = 0;
188 p_sys->p_context->extradata = NULL;
191 /* ***** Open the codec ***** */
194 ret = avcodec_open( p_sys->p_context, p_sys->p_codec );
195 vlc_avcodec_unlock();
198 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
199 free( p_sys->p_context->extradata );
204 msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
206 p_sys->p_output = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
207 p_sys->p_samples = NULL;
208 p_sys->i_samples = 0;
209 p_sys->i_reject_count = 0;
211 aout_DateSet( &p_sys->end_date, 0 );
212 if( p_dec->fmt_in.audio.i_rate )
213 aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
215 /* Set output properties */
216 p_dec->fmt_out.i_cat = AUDIO_ES;
218 SetupOutputCodec( p_dec );
223 /*****************************************************************************
224 * SplitBuffer: Needed because aout really doesn't like big audio chunk and
225 * wma produces easily > 30000 samples...
226 *****************************************************************************/
227 static aout_buffer_t *SplitBuffer( decoder_t *p_dec )
229 decoder_sys_t *p_sys = p_dec->p_sys;
230 int i_samples = __MIN( p_sys->i_samples, 4096 );
231 aout_buffer_t *p_buffer;
233 if( i_samples == 0 ) return NULL;
235 if( ( p_buffer = decoder_NewAudioBuffer( p_dec, i_samples ) ) == NULL )
238 p_buffer->start_date = aout_DateGet( &p_sys->end_date );
239 p_buffer->end_date = aout_DateIncrement( &p_sys->end_date, i_samples );
241 memcpy( p_buffer->p_buffer, p_sys->p_samples, p_buffer->i_nb_bytes );
243 p_sys->p_samples += p_buffer->i_nb_bytes;
244 p_sys->i_samples -= i_samples;
249 /*****************************************************************************
250 * DecodeAudio: Called to decode one frame
251 *****************************************************************************/
252 aout_buffer_t * DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
254 decoder_sys_t *p_sys = p_dec->p_sys;
255 int i_used, i_output;
256 aout_buffer_t *p_buffer;
259 if( !pp_block || !*pp_block ) return NULL;
263 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
265 block_Release( p_block );
266 avcodec_flush_buffers( p_sys->p_context );
267 p_sys->i_samples = 0;
268 aout_DateSet( &p_sys->end_date, 0 );
270 if( p_sys->i_codec_id == CODEC_ID_MP2 || p_sys->i_codec_id == CODEC_ID_MP3 )
271 p_sys->i_reject_count = 3;
275 if( p_sys->i_samples > 0 )
278 p_buffer = SplitBuffer( p_dec );
279 if( !p_buffer ) block_Release( p_block );
283 if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
285 /* We've just started the stream, wait for the first PTS. */
286 block_Release( p_block );
290 if( p_block->i_buffer <= 0 )
292 block_Release( p_block );
295 if( p_block->i_buffer > AVCODEC_MAX_AUDIO_FRAME_SIZE )
297 /* Grow output buffer if necessary (eg. for PCM data) */
298 p_sys->p_output = realloc(p_sys->p_output, p_block->i_buffer);
301 *pp_block = p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
304 p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
305 memset( &p_block->p_buffer[p_block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
307 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(0<<8)+0)
308 i_output = __MAX( AVCODEC_MAX_AUDIO_FRAME_SIZE, p_block->i_buffer );
309 i_used = avcodec_decode_audio2( p_sys->p_context,
310 (int16_t*)p_sys->p_output, &i_output,
311 p_block->p_buffer, p_block->i_buffer );
313 i_used = avcodec_decode_audio( p_sys->p_context,
314 (int16_t*)p_sys->p_output, &i_output,
315 p_block->p_buffer, p_block->i_buffer );
318 if( i_used < 0 || i_output < 0 )
321 msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
324 block_Release( p_block );
327 else if( (size_t)i_used > p_block->i_buffer )
329 i_used = p_block->i_buffer;
332 p_block->i_buffer -= i_used;
333 p_block->p_buffer += i_used;
335 if( p_sys->p_context->channels <= 0 || p_sys->p_context->channels > 8 ||
336 p_sys->p_context->sample_rate <= 0 )
338 msg_Warn( p_dec, "invalid audio properties channels count %d, sample rate %d",
339 p_sys->p_context->channels, p_sys->p_context->sample_rate );
340 block_Release( p_block );
344 if( p_dec->fmt_out.audio.i_rate != (unsigned int)p_sys->p_context->sample_rate )
346 aout_DateInit( &p_sys->end_date, p_sys->p_context->sample_rate );
347 aout_DateSet( &p_sys->end_date, p_block->i_pts );
350 /* **** Set audio output parameters **** */
351 SetupOutputCodec( p_dec );
352 p_dec->fmt_out.audio.i_rate = p_sys->p_context->sample_rate;
353 p_dec->fmt_out.audio.i_channels = p_sys->p_context->channels;
354 p_dec->fmt_out.audio.i_original_channels =
355 p_dec->fmt_out.audio.i_physical_channels =
356 pi_channels_maps[p_sys->p_context->channels];
358 if( p_block->i_pts != 0 &&
359 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
361 aout_DateSet( &p_sys->end_date, p_block->i_pts );
365 /* **** Now we can output these samples **** */
366 p_sys->i_samples = i_output / (p_dec->fmt_out.audio.i_bitspersample / 8) / p_sys->p_context->channels;
367 p_sys->p_samples = p_sys->p_output;
369 /* Silent unwanted samples */
370 if( p_sys->i_reject_count > 0 )
372 memset( p_sys->p_output, 0, i_output );
373 p_sys->i_reject_count--;
376 p_buffer = SplitBuffer( p_dec );
377 if( !p_buffer ) block_Release( p_block );
381 /*****************************************************************************
382 * EndAudioDec: audio decoder destruction
383 *****************************************************************************/
384 void EndAudioDec( decoder_t *p_dec )
386 decoder_sys_t *p_sys = p_dec->p_sys;
388 free( p_sys->p_output );
391 /*****************************************************************************
393 *****************************************************************************/
394 static void SetupOutputCodec( decoder_t *p_dec )
396 decoder_sys_t *p_sys = p_dec->p_sys;
398 #if defined(AV_VERSION_INT) && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 51, 65, 0 )
399 switch( p_sys->p_context->sample_fmt )
402 p_dec->fmt_out.i_codec = VLC_FOURCC('u','8',' ',' ');
403 p_dec->fmt_out.audio.i_bitspersample = 8;
406 p_dec->fmt_out.i_codec = AOUT_FMT_S32_NE;
407 p_dec->fmt_out.audio.i_bitspersample = 32;
410 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
411 p_dec->fmt_out.audio.i_bitspersample = 32;
414 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','6','4');
415 p_dec->fmt_out.audio.i_bitspersample = 64;
420 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
421 p_dec->fmt_out.audio.i_bitspersample = 16;
425 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
426 p_dec->fmt_out.audio.i_bitspersample = 16;