1 /*****************************************************************************
2 * audio.c: audio decoder using libavcodec library
3 *****************************************************************************
4 * Copyright (C) 1999-2003 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <vlc_codec.h>
37 #include <vlc_avcodec.h>
39 #include <libavcodec/avcodec.h>
40 #include <libavutil/mem.h>
42 #include <libavutil/audioconvert.h>
46 /*****************************************************************************
47 * decoder_sys_t : decoder descriptor
48 *****************************************************************************/
51 AVCODEC_COMMON_MEMBERS
56 audio_sample_format_t aout_format;
64 int pi_extraction[AOUT_CHAN_MAX];
65 int i_previous_channels;
66 int64_t i_previous_layout;
69 #define BLOCK_FLAG_PRIVATE_REALLOCATED (1 << BLOCK_FLAG_PRIVATE_SHIFT)
71 static void SetupOutputFormat( decoder_t *p_dec, bool b_trust );
72 static int GetAudioBuf( struct AVCodecContext *, AVFrame * );
74 static void InitDecoderConfig( decoder_t *p_dec, AVCodecContext *p_context )
76 if( p_dec->fmt_in.i_extra > 0 )
78 const uint8_t * const p_src = p_dec->fmt_in.p_extra;
81 int i_size = p_dec->fmt_in.i_extra;
83 if( p_dec->fmt_in.i_codec == VLC_CODEC_ALAC )
85 static const uint8_t p_pattern[] = { 0, 0, 0, 36, 'a', 'l', 'a', 'c' };
86 /* Find alac atom XXX it is a bit ugly */
87 for( i_offset = 0; i_offset < p_dec->fmt_in.i_extra - sizeof(p_pattern); i_offset++ )
89 if( !memcmp( &p_src[i_offset], p_pattern, sizeof(p_pattern) ) )
92 i_size = __MIN( p_dec->fmt_in.i_extra - i_offset, 36 );
99 p_context->extradata =
100 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
101 if( p_context->extradata )
103 uint8_t *p_dst = p_context->extradata;
105 p_context->extradata_size = i_size;
107 memcpy( &p_dst[0], &p_src[i_offset], i_size );
108 memset( &p_dst[i_size], 0, FF_INPUT_BUFFER_PADDING_SIZE );
114 p_context->extradata_size = 0;
115 p_context->extradata = NULL;
119 /*****************************************************************************
120 * InitAudioDec: initialize audio decoder
121 *****************************************************************************
122 * The avcodec codec will be opened, some memory allocated.
123 *****************************************************************************/
124 int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
125 AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
127 decoder_sys_t *p_sys;
129 /* Allocate the memory needed to store the decoder's structure */
130 if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
135 p_codec->type = AVMEDIA_TYPE_AUDIO;
136 p_context->codec_type = AVMEDIA_TYPE_AUDIO;
137 p_context->codec_id = i_codec_id;
138 p_context->get_buffer = GetAudioBuf;
139 p_sys->p_context = p_context;
140 p_sys->p_codec = p_codec;
141 p_sys->i_codec_id = i_codec_id;
142 p_sys->psz_namecodec = psz_namecodec;
143 p_sys->b_delayed_open = true;
145 // Initialize decoder extradata
146 InitDecoderConfig( p_dec, p_context);
148 /* ***** Open the codec ***** */
149 if( ffmpeg_OpenCodec( p_dec ) < 0 )
151 msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
152 free( p_sys->p_context->extradata );
157 p_sys->i_reject_count = 0;
158 p_sys->b_extract = false;
159 p_sys->i_previous_channels = 0;
160 p_sys->i_previous_layout = 0;
163 p_dec->fmt_out.i_cat = AUDIO_ES;
164 /* Try to set as much information as possible but do not trust it */
165 SetupOutputFormat( p_dec, false );
167 date_Set( &p_sys->end_date, 0 );
168 if( p_dec->fmt_out.audio.i_rate )
169 date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
170 else if( p_dec->fmt_in.audio.i_rate )
171 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
177 * Allocates decoded audio buffer for libavcodec to use.
179 static int GetAudioBuf( AVCodecContext *ctx, AVFrame *buf )
182 bool planar = av_sample_fmt_is_planar( ctx->sample_fmt );
183 unsigned channels = planar ? 1 : ctx->channels;
184 unsigned planes = planar ? ctx->channels : 1;
186 int bytes = av_samples_get_buffer_size( &buf->linesize[0], channels,
187 buf->nb_samples, ctx->sample_fmt,
189 assert( bytes >= 0 );
190 block = block_Alloc( bytes * planes );
191 if( unlikely(block == NULL) )
192 return AVERROR(ENOMEM);
194 block->i_nb_samples = buf->nb_samples;
197 if( planes > AV_NUM_DATA_POINTERS )
199 uint8_t **ext = malloc( sizeof( *ext ) * planes );
200 if( unlikely(ext == NULL) )
202 block_Release( block );
203 return AVERROR(ENOMEM);
205 buf->extended_data = ext;
208 buf->extended_data = buf->data;
210 uint8_t *buffer = block->p_buffer;
211 for( unsigned i = 0; i < planes; i++ )
213 buf->linesize[i] = buf->linesize[0];
214 buf->extended_data[i] = buffer;
221 /*****************************************************************************
222 * DecodeAudio: Called to decode one frame
223 *****************************************************************************/
224 block_t * DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
226 decoder_sys_t *p_sys = p_dec->p_sys;
227 AVCodecContext *ctx = p_sys->p_context;
229 if( !pp_block || !*pp_block )
232 block_t *p_block = *pp_block;
234 if( !ctx->extradata_size && p_dec->fmt_in.i_extra && p_sys->b_delayed_open)
236 InitDecoderConfig( p_dec, ctx );
237 if( ffmpeg_OpenCodec( p_dec ) )
238 msg_Err( p_dec, "Cannot open decoder %s", p_sys->psz_namecodec );
241 if( p_sys->b_delayed_open )
244 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
246 avcodec_flush_buffers( ctx );
247 date_Set( &p_sys->end_date, 0 );
249 if( p_sys->i_codec_id == AV_CODEC_ID_MP2 || p_sys->i_codec_id == AV_CODEC_ID_MP3 )
250 p_sys->i_reject_count = 3;
255 /* We've just started the stream, wait for the first PTS. */
256 if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID )
259 if( p_block->i_buffer <= 0 )
262 if( (p_block->i_flags & BLOCK_FLAG_PRIVATE_REALLOCATED) == 0 )
264 p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
267 p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
268 memset( &p_block->p_buffer[p_block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
270 p_block->i_flags |= BLOCK_FLAG_PRIVATE_REALLOCATED;
274 memset( &frame, 0, sizeof( frame ) );
276 for( int got_frame = 0; !got_frame; )
278 if( p_block->i_buffer == 0 )
282 av_init_packet( &pkt );
283 pkt.data = p_block->p_buffer;
284 pkt.size = p_block->i_buffer;
286 int used = avcodec_decode_audio4( ctx, &frame, &got_frame, &pkt );
289 msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
294 assert( p_block->i_buffer >= (unsigned)used );
295 p_block->p_buffer += used;
296 p_block->i_buffer -= used;
299 if( ctx->channels <= 0 || ctx->channels > 8 || ctx->sample_rate <= 0 )
301 msg_Warn( p_dec, "invalid audio properties channels count %d, sample rate %d",
302 ctx->channels, ctx->sample_rate );
306 if( p_dec->fmt_out.audio.i_rate != (unsigned int)ctx->sample_rate )
307 date_Init( &p_sys->end_date, ctx->sample_rate, 1 );
309 if( p_block->i_pts > VLC_TS_INVALID &&
310 p_block->i_pts > date_Get( &p_sys->end_date ) )
312 date_Set( &p_sys->end_date, p_block->i_pts );
315 if( p_block->i_buffer == 0 )
316 { /* Done with this buffer */
317 block_Release( p_block );
321 /* NOTE WELL: Beyond this point, p_block now refers to the DECODED block */
322 p_block = frame.opaque;
323 SetupOutputFormat( p_dec, true );
325 /* Silent unwanted samples */
326 if( p_sys->i_reject_count > 0 )
328 memset( p_block->p_buffer, 0, p_block->i_buffer );
329 p_sys->i_reject_count--;
332 block_t *p_buffer = decoder_NewAudioBuffer( p_dec, p_block->i_nb_samples );
335 assert( p_block->i_nb_samples >= (unsigned)frame.nb_samples );
336 assert( p_block->i_nb_samples == p_buffer->i_nb_samples );
337 p_block->i_buffer = p_buffer->i_buffer; /* drop buffer padding */
339 /* Interleave audio if required */
340 if( av_sample_fmt_is_planar( ctx->sample_fmt ) )
342 aout_Interleave( p_buffer->p_buffer, p_block->p_buffer,
343 p_block->i_nb_samples, ctx->channels,
344 p_dec->fmt_out.audio.i_format );
345 if( ctx->channels > AV_NUM_DATA_POINTERS )
346 free( frame.extended_data );
347 block_Release( p_block );
350 else /* FIXME: improve decoder_NewAudioBuffer(), avoid useless buffer... */
351 block_Release( p_buffer );
353 if (p_sys->b_extract)
354 { /* TODO: do not drop channels... at least not here */
355 p_buffer = block_Alloc( p_dec->fmt_out.audio.i_bytes_per_frame
356 * frame.nb_samples );
357 if( unlikely(p_buffer == NULL) )
359 block_Release( p_block );
362 aout_ChannelExtract( p_buffer->p_buffer,
363 p_dec->fmt_out.audio.i_channels,
364 p_block->p_buffer, ctx->channels,
365 frame.nb_samples, p_sys->pi_extraction,
366 p_dec->fmt_out.audio.i_bitspersample );
367 block_Release( p_block );
371 p_block->i_nb_samples = frame.nb_samples;
372 p_block->i_buffer = frame.nb_samples
373 * p_dec->fmt_out.audio.i_bytes_per_frame;
374 p_block->i_pts = date_Get( &p_sys->end_date );
375 p_block->i_length = date_Increment( &p_sys->end_date, frame.nb_samples )
380 block_Release(p_block);
385 /*****************************************************************************
387 *****************************************************************************/
389 vlc_fourcc_t GetVlcAudioFormat( int fmt )
391 static const vlc_fourcc_t fcc[] = {
392 [AV_SAMPLE_FMT_U8] = VLC_CODEC_U8,
393 [AV_SAMPLE_FMT_S16] = VLC_CODEC_S16N,
394 [AV_SAMPLE_FMT_S32] = VLC_CODEC_S32N,
395 [AV_SAMPLE_FMT_FLT] = VLC_CODEC_FL32,
396 [AV_SAMPLE_FMT_DBL] = VLC_CODEC_FL64,
397 [AV_SAMPLE_FMT_U8P] = VLC_CODEC_U8,
398 [AV_SAMPLE_FMT_S16P] = VLC_CODEC_S16N,
399 [AV_SAMPLE_FMT_S32P] = VLC_CODEC_S32N,
400 [AV_SAMPLE_FMT_FLTP] = VLC_CODEC_FL32,
401 [AV_SAMPLE_FMT_DBLP] = VLC_CODEC_FL64,
403 if( (sizeof(fcc) / sizeof(fcc[0])) > (unsigned)fmt )
405 return VLC_CODEC_S16N;
408 static const uint64_t pi_channels_map[][2] =
410 { AV_CH_FRONT_LEFT, AOUT_CHAN_LEFT },
411 { AV_CH_FRONT_RIGHT, AOUT_CHAN_RIGHT },
412 { AV_CH_FRONT_CENTER, AOUT_CHAN_CENTER },
413 { AV_CH_LOW_FREQUENCY, AOUT_CHAN_LFE },
414 { AV_CH_BACK_LEFT, AOUT_CHAN_REARLEFT },
415 { AV_CH_BACK_RIGHT, AOUT_CHAN_REARRIGHT },
416 { AV_CH_FRONT_LEFT_OF_CENTER, 0 },
417 { AV_CH_FRONT_RIGHT_OF_CENTER, 0 },
418 { AV_CH_BACK_CENTER, AOUT_CHAN_REARCENTER },
419 { AV_CH_SIDE_LEFT, AOUT_CHAN_MIDDLELEFT },
420 { AV_CH_SIDE_RIGHT, AOUT_CHAN_MIDDLERIGHT },
421 { AV_CH_TOP_CENTER, 0 },
422 { AV_CH_TOP_FRONT_LEFT, 0 },
423 { AV_CH_TOP_FRONT_CENTER, 0 },
424 { AV_CH_TOP_FRONT_RIGHT, 0 },
425 { AV_CH_TOP_BACK_LEFT, 0 },
426 { AV_CH_TOP_BACK_CENTER, 0 },
427 { AV_CH_TOP_BACK_RIGHT, 0 },
428 { AV_CH_STEREO_LEFT, 0 },
429 { AV_CH_STEREO_RIGHT, 0 },
432 static void SetupOutputFormat( decoder_t *p_dec, bool b_trust )
434 decoder_sys_t *p_sys = p_dec->p_sys;
436 p_dec->fmt_out.i_codec = GetVlcAudioFormat( p_sys->p_context->sample_fmt );
437 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
438 p_dec->fmt_out.audio.i_rate = p_sys->p_context->sample_rate;
441 if( p_sys->i_previous_channels == p_sys->p_context->channels &&
442 p_sys->i_previous_layout == p_sys->p_context->channel_layout )
446 p_sys->i_previous_channels = p_sys->p_context->channels;
447 p_sys->i_previous_layout = p_sys->p_context->channel_layout;
451 * FIXME should we use fmt_in.audio.i_physical_channels or not ?
453 const unsigned i_order_max = 8 * sizeof(p_sys->p_context->channel_layout);
454 uint32_t pi_order_src[i_order_max];
455 int i_channels_src = 0;
457 if( p_sys->p_context->channel_layout )
459 for( unsigned i = 0; i < sizeof(pi_channels_map)/sizeof(*pi_channels_map); i++ )
461 if( p_sys->p_context->channel_layout & pi_channels_map[i][0] )
462 pi_order_src[i_channels_src++] = pi_channels_map[i][1];
467 /* Create default order */
469 msg_Warn( p_dec, "Physical channel configuration not set : guessing" );
470 for( unsigned int i = 0; i < __MIN( i_order_max, (unsigned)p_sys->p_context->channels ); i++ )
472 if( i < sizeof(pi_channels_map)/sizeof(*pi_channels_map) )
473 pi_order_src[i_channels_src++] = pi_channels_map[i][1];
476 if( i_channels_src != p_sys->p_context->channels && b_trust )
477 msg_Err( p_dec, "Channel layout not understood" );
479 uint32_t i_layout_dst;
481 p_sys->b_extract = aout_CheckChannelExtraction( p_sys->pi_extraction,
482 &i_layout_dst, &i_channels_dst,
483 NULL, pi_order_src, i_channels_src );
484 if( i_channels_dst != i_channels_src && b_trust )
485 msg_Warn( p_dec, "%d channels are dropped", i_channels_src - i_channels_dst );
487 p_dec->fmt_out.audio.i_physical_channels =
488 p_dec->fmt_out.audio.i_original_channels = i_layout_dst;
489 aout_FormatPrepare( &p_dec->fmt_out.audio );