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 uint64_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 block_t *DecodeAudio( decoder_t *, block_t ** );
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 < i_size - (int)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 av_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 static int OpenAudioCodec( decoder_t *p_dec )
121 decoder_sys_t *p_sys = p_dec->p_sys;
123 if( p_sys->p_context->extradata_size <= 0 )
125 if( p_sys->p_codec->id == AV_CODEC_ID_VORBIS ||
126 ( p_sys->p_codec->id == AV_CODEC_ID_AAC &&
127 !p_dec->fmt_in.b_packetized ) )
129 msg_Warn( p_dec, "waiting for extra data for codec %s",
130 p_sys->p_codec->name );
135 p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
136 p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
137 p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
138 p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
139 p_sys->p_context->bits_per_coded_sample =
140 p_dec->fmt_in.audio.i_bitspersample;
142 if( p_sys->p_codec->id == AV_CODEC_ID_ADPCM_G726 &&
143 p_sys->p_context->bit_rate > 0 &&
144 p_sys->p_context->sample_rate > 0)
145 p_sys->p_context->bits_per_coded_sample = p_sys->p_context->bit_rate
146 / p_sys->p_context->sample_rate;
148 return ffmpeg_OpenCodec( p_dec );
152 * Allocates decoded audio buffer for libavcodec to use.
154 #if (LIBAVCODEC_VERSION_MAJOR >= 55)
161 static void vlc_av_frame_Release(block_t *block)
163 vlc_av_frame_t *b = (void *)block;
165 av_frame_free(&b->frame);
169 static block_t *vlc_av_frame_Wrap(AVFrame *frame)
171 for (unsigned i = 1; i < AV_NUM_DATA_POINTERS; i++)
172 assert(frame->linesize[i] == 0); /* only packed frame supported */
174 if (av_frame_make_writable(frame)) /* TODO: read-only block_t */
177 vlc_av_frame_t *b = malloc(sizeof (*b));
178 if (unlikely(b == NULL))
181 block_t *block = &b->self;
183 block_Init(block, frame->extended_data[0], frame->linesize[0]);
184 block->i_nb_samples = frame->nb_samples;
185 block->pf_release = vlc_av_frame_Release;
190 static int GetAudioBuf( AVCodecContext *ctx, AVFrame *buf )
193 bool planar = av_sample_fmt_is_planar( ctx->sample_fmt );
194 unsigned channels = planar ? 1 : ctx->channels;
195 unsigned planes = planar ? ctx->channels : 1;
197 int bytes = av_samples_get_buffer_size( &buf->linesize[0], channels,
198 buf->nb_samples, ctx->sample_fmt,
200 assert( bytes >= 0 );
201 block = block_Alloc( bytes * planes );
202 if( unlikely(block == NULL) )
203 return AVERROR(ENOMEM);
205 block->i_nb_samples = buf->nb_samples;
208 if( planes > AV_NUM_DATA_POINTERS )
210 uint8_t **ext = malloc( sizeof( *ext ) * planes );
211 if( unlikely(ext == NULL) )
213 block_Release( block );
214 return AVERROR(ENOMEM);
216 buf->extended_data = ext;
219 buf->extended_data = buf->data;
221 uint8_t *buffer = block->p_buffer;
222 for( unsigned i = 0; i < planes; i++ )
224 buf->linesize[i] = buf->linesize[0];
225 buf->extended_data[i] = buffer;
233 /*****************************************************************************
234 * InitAudioDec: initialize audio decoder
235 *****************************************************************************
236 * The avcodec codec will be opened, some memory allocated.
237 *****************************************************************************/
238 int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
239 const AVCodec *p_codec )
241 decoder_sys_t *p_sys;
243 /* Allocate the memory needed to store the decoder's structure */
244 if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
249 #if (LIBAVCODEC_VERSION_MAJOR >= 55)
250 p_context->refcounted_frames = true;
252 p_context->get_buffer = GetAudioBuf;
254 p_sys->p_context = p_context;
255 p_sys->p_codec = p_codec;
256 p_sys->b_delayed_open = true;
258 // Initialize decoder extradata
259 InitDecoderConfig( p_dec, p_context);
261 /* ***** Open the codec ***** */
262 if( OpenAudioCodec( p_dec ) < 0 )
264 av_free( p_context->extradata );
269 p_sys->i_reject_count = 0;
270 p_sys->b_extract = false;
271 p_sys->i_previous_channels = 0;
272 p_sys->i_previous_layout = 0;
275 p_dec->fmt_out.i_cat = AUDIO_ES;
276 /* Try to set as much information as possible but do not trust it */
277 SetupOutputFormat( p_dec, false );
279 date_Set( &p_sys->end_date, 0 );
280 if( p_dec->fmt_out.audio.i_rate )
281 date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
282 else if( p_dec->fmt_in.audio.i_rate )
283 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
285 p_dec->pf_decode_audio = DecodeAudio;
289 /*****************************************************************************
290 * DecodeAudio: Called to decode one frame
291 *****************************************************************************/
292 static block_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
294 decoder_sys_t *p_sys = p_dec->p_sys;
295 AVCodecContext *ctx = p_sys->p_context;
297 if( !pp_block || !*pp_block )
300 block_t *p_block = *pp_block;
302 if( !ctx->extradata_size && p_dec->fmt_in.i_extra && p_sys->b_delayed_open)
304 InitDecoderConfig( p_dec, ctx );
305 OpenAudioCodec( p_dec );
308 if( p_sys->b_delayed_open )
311 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
313 avcodec_flush_buffers( ctx );
314 date_Set( &p_sys->end_date, 0 );
316 if( ctx->codec_id == AV_CODEC_ID_MP2 ||
317 ctx->codec_id == AV_CODEC_ID_MP3 )
318 p_sys->i_reject_count = 3;
323 /* We've just started the stream, wait for the first PTS. */
324 if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID )
327 if( p_block->i_buffer <= 0 )
330 if( (p_block->i_flags & BLOCK_FLAG_PRIVATE_REALLOCATED) == 0 )
332 p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
336 p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
337 memset( &p_block->p_buffer[p_block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
339 p_block->i_flags |= BLOCK_FLAG_PRIVATE_REALLOCATED;
342 #if (LIBAVCODEC_VERSION_MAJOR >= 55)
343 AVFrame *frame = av_frame_alloc();
344 if (unlikely(frame == NULL))
347 AVFrame *frame = &(AVFrame) { };
350 for( int got_frame = 0; !got_frame; )
352 if( p_block->i_buffer == 0 )
356 av_init_packet( &pkt );
357 pkt.data = p_block->p_buffer;
358 pkt.size = p_block->i_buffer;
360 int used = avcodec_decode_audio4( ctx, frame, &got_frame, &pkt );
363 msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
368 assert( p_block->i_buffer >= (unsigned)used );
369 p_block->p_buffer += used;
370 p_block->i_buffer -= used;
373 if( ctx->channels <= 0 || ctx->channels > 8 || ctx->sample_rate <= 0 )
375 msg_Warn( p_dec, "invalid audio properties channels count %d, sample rate %d",
376 ctx->channels, ctx->sample_rate );
380 if( p_dec->fmt_out.audio.i_rate != (unsigned int)ctx->sample_rate )
381 date_Init( &p_sys->end_date, ctx->sample_rate, 1 );
383 if( p_block->i_pts > VLC_TS_INVALID &&
384 p_block->i_pts > date_Get( &p_sys->end_date ) )
386 date_Set( &p_sys->end_date, p_block->i_pts );
389 if( p_block->i_buffer == 0 )
390 { /* Done with this buffer */
391 block_Release( p_block );
396 #if (LIBAVCODEC_VERSION_MAJOR < 55)
397 /* NOTE WELL: Beyond this point, p_block refers to the DECODED block! */
398 p_block = frame->opaque;
400 SetupOutputFormat( p_dec, true );
401 if( decoder_UpdateAudioFormat( p_dec ) )
404 /* Interleave audio if required */
405 if( av_sample_fmt_is_planar( ctx->sample_fmt ) )
406 #if (LIBAVCODEC_VERSION_MAJOR >= 55)
408 p_block = block_Alloc(frame->linesize[0] * ctx->channels);
409 if (unlikely(p_block == NULL))
412 const void *planes[ctx->channels];
413 for (int i = 0; i < ctx->channels; i++)
414 planes[i] = frame->extended_data[i];
416 aout_Interleave(p_block->p_buffer, planes, frame->nb_samples,
417 ctx->channels, p_dec->fmt_out.audio.i_format);
418 p_block->i_nb_samples = frame->nb_samples;
419 av_frame_free(&frame);
423 p_block = vlc_av_frame_Wrap(frame);
424 if (unlikely(p_block == NULL))
429 block_t *p_buffer = block_Alloc( p_block->i_buffer );
430 if( unlikely(p_buffer == NULL) )
433 const void *planes[ctx->channels];
434 for( int i = 0; i < ctx->channels; i++)
435 planes[i] = frame->extended_data[i];
437 aout_Interleave( p_buffer->p_buffer, planes, frame->nb_samples,
438 ctx->channels, p_dec->fmt_out.audio.i_format );
439 if( ctx->channels > AV_NUM_DATA_POINTERS )
440 free( frame->extended_data );
441 block_Release( p_block );
444 p_block->i_nb_samples = frame->nb_samples;
447 if (p_sys->b_extract)
448 { /* TODO: do not drop channels... at least not here */
449 block_t *p_buffer = block_Alloc( p_dec->fmt_out.audio.i_bytes_per_frame
450 * p_block->i_nb_samples );
451 if( unlikely(p_buffer == NULL) )
453 aout_ChannelExtract( p_buffer->p_buffer,
454 p_dec->fmt_out.audio.i_channels,
455 p_block->p_buffer, ctx->channels,
456 p_block->i_nb_samples, p_sys->pi_extraction,
457 p_dec->fmt_out.audio.i_bitspersample );
458 p_buffer->i_nb_samples = p_block->i_nb_samples;
459 block_Release( p_block );
463 /* Silent unwanted samples */
464 if( p_sys->i_reject_count > 0 )
466 memset( p_block->p_buffer, 0, p_block->i_buffer );
467 p_sys->i_reject_count--;
470 p_block->i_buffer = p_block->i_nb_samples
471 * p_dec->fmt_out.audio.i_bytes_per_frame;
472 p_block->i_pts = date_Get( &p_sys->end_date );
473 p_block->i_length = date_Increment( &p_sys->end_date,
474 p_block->i_nb_samples ) - p_block->i_pts;
480 if( p_block != NULL )
481 block_Release(p_block);
485 /*****************************************************************************
487 *****************************************************************************/
489 vlc_fourcc_t GetVlcAudioFormat( int fmt )
491 static const vlc_fourcc_t fcc[] = {
492 [AV_SAMPLE_FMT_U8] = VLC_CODEC_U8,
493 [AV_SAMPLE_FMT_S16] = VLC_CODEC_S16N,
494 [AV_SAMPLE_FMT_S32] = VLC_CODEC_S32N,
495 [AV_SAMPLE_FMT_FLT] = VLC_CODEC_FL32,
496 [AV_SAMPLE_FMT_DBL] = VLC_CODEC_FL64,
497 [AV_SAMPLE_FMT_U8P] = VLC_CODEC_U8,
498 [AV_SAMPLE_FMT_S16P] = VLC_CODEC_S16N,
499 [AV_SAMPLE_FMT_S32P] = VLC_CODEC_S32N,
500 [AV_SAMPLE_FMT_FLTP] = VLC_CODEC_FL32,
501 [AV_SAMPLE_FMT_DBLP] = VLC_CODEC_FL64,
503 if( (sizeof(fcc) / sizeof(fcc[0])) > (unsigned)fmt )
505 return VLC_CODEC_S16N;
508 static const uint64_t pi_channels_map[][2] =
510 { AV_CH_FRONT_LEFT, AOUT_CHAN_LEFT },
511 { AV_CH_FRONT_RIGHT, AOUT_CHAN_RIGHT },
512 { AV_CH_FRONT_CENTER, AOUT_CHAN_CENTER },
513 { AV_CH_LOW_FREQUENCY, AOUT_CHAN_LFE },
514 { AV_CH_BACK_LEFT, AOUT_CHAN_REARLEFT },
515 { AV_CH_BACK_RIGHT, AOUT_CHAN_REARRIGHT },
516 { AV_CH_FRONT_LEFT_OF_CENTER, 0 },
517 { AV_CH_FRONT_RIGHT_OF_CENTER, 0 },
518 { AV_CH_BACK_CENTER, AOUT_CHAN_REARCENTER },
519 { AV_CH_SIDE_LEFT, AOUT_CHAN_MIDDLELEFT },
520 { AV_CH_SIDE_RIGHT, AOUT_CHAN_MIDDLERIGHT },
521 { AV_CH_TOP_CENTER, 0 },
522 { AV_CH_TOP_FRONT_LEFT, 0 },
523 { AV_CH_TOP_FRONT_CENTER, 0 },
524 { AV_CH_TOP_FRONT_RIGHT, 0 },
525 { AV_CH_TOP_BACK_LEFT, 0 },
526 { AV_CH_TOP_BACK_CENTER, 0 },
527 { AV_CH_TOP_BACK_RIGHT, 0 },
528 { AV_CH_STEREO_LEFT, 0 },
529 { AV_CH_STEREO_RIGHT, 0 },
532 static void SetupOutputFormat( decoder_t *p_dec, bool b_trust )
534 decoder_sys_t *p_sys = p_dec->p_sys;
536 p_dec->fmt_out.i_codec = GetVlcAudioFormat( p_sys->p_context->sample_fmt );
537 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
538 p_dec->fmt_out.audio.i_rate = p_sys->p_context->sample_rate;
541 if( p_sys->i_previous_channels == p_sys->p_context->channels &&
542 p_sys->i_previous_layout == p_sys->p_context->channel_layout )
546 p_sys->i_previous_channels = p_sys->p_context->channels;
547 p_sys->i_previous_layout = p_sys->p_context->channel_layout;
551 * FIXME should we use fmt_in.audio.i_physical_channels or not ?
553 const unsigned i_order_max = 8 * sizeof(p_sys->p_context->channel_layout);
554 uint32_t pi_order_src[i_order_max];
555 int i_channels_src = 0;
557 if( p_sys->p_context->channel_layout )
559 for( unsigned i = 0; i < sizeof(pi_channels_map)/sizeof(*pi_channels_map); i++ )
561 if( p_sys->p_context->channel_layout & pi_channels_map[i][0] )
562 pi_order_src[i_channels_src++] = pi_channels_map[i][1];
567 /* Create default order */
569 msg_Warn( p_dec, "Physical channel configuration not set : guessing" );
570 for( unsigned int i = 0; i < __MIN( i_order_max, (unsigned)p_sys->p_context->channels ); i++ )
572 if( i < sizeof(pi_channels_map)/sizeof(*pi_channels_map) )
573 pi_order_src[i_channels_src++] = pi_channels_map[i][1];
576 if( i_channels_src != p_sys->p_context->channels && b_trust )
577 msg_Err( p_dec, "Channel layout not understood" );
579 uint32_t i_layout_dst;
581 p_sys->b_extract = aout_CheckChannelExtraction( p_sys->pi_extraction,
582 &i_layout_dst, &i_channels_dst,
583 NULL, pi_order_src, i_channels_src );
584 if( i_channels_dst != i_channels_src && b_trust )
585 msg_Warn( p_dec, "%d channels are dropped", i_channels_src - i_channels_dst );
587 p_dec->fmt_out.audio.i_physical_channels =
588 p_dec->fmt_out.audio.i_original_channels = i_layout_dst;
589 aout_FormatPrepare( &p_dec->fmt_out.audio );