openbsd-ports/x11/vlc/patches/patch-modules_codec_avcodec_audio_c

314 lines
11 KiB
Plaintext

$OpenBSD: patch-modules_codec_avcodec_audio_c,v 1.5 2014/05/05 08:34:08 brad Exp $
Deal with newer FFmpeg API.
--- modules/codec/avcodec/audio.c.orig Fri Feb 21 10:27:16 2014
+++ modules/codec/avcodec/audio.c Thu Apr 24 21:38:21 2014
@@ -57,10 +57,6 @@ struct decoder_sys_t
{
AVCODEC_COMMON_MEMBERS
- /* Temporary buffer for libavcodec */
- int i_output_max;
- uint8_t *p_output;
-
/*
* Output properties
*/
@@ -116,6 +112,7 @@ do { \
#define BLOCK_FLAG_PRIVATE_REALLOCATED (1 << BLOCK_FLAG_PRIVATE_SHIFT)
static void SetupOutputFormat( decoder_t *p_dec, bool b_trust );
+static int GetAudioBuf( struct AVCodecContext *, AVFrame * );
static void InitDecoderConfig( decoder_t *p_dec, AVCodecContext *p_context )
{
@@ -190,6 +187,7 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_
p_codec->type = AVMEDIA_TYPE_AUDIO;
p_context->codec_type = AVMEDIA_TYPE_AUDIO;
p_context->codec_id = i_codec_id;
+ p_context->get_buffer = GetAudioBuf;
p_sys->p_context = p_context;
p_sys->p_codec = p_codec;
p_sys->i_codec_id = i_codec_id;
@@ -208,31 +206,6 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_
return VLC_EGENERIC;
}
- switch( i_codec_id )
- {
- case CODEC_ID_WAVPACK:
- p_sys->i_output_max = 8 * sizeof(int32_t) * 131072;
- break;
- case CODEC_ID_TTA:
- p_sys->i_output_max = p_sys->p_context->channels * sizeof(int32_t) * p_sys->p_context->sample_rate * 2;
- break;
- case CODEC_ID_FLAC:
- p_sys->i_output_max = 8 * sizeof(int32_t) * 65535;
- break;
-#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 52, 35, 0 )
- case CODEC_ID_WMAPRO:
- p_sys->i_output_max = 8 * sizeof(float) * 6144; /* (1 << 12) * 3/2 */
- break;
-#endif
- default:
- p_sys->i_output_max = 0;
- break;
- }
- if( p_sys->i_output_max < 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE )
- p_sys->i_output_max = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
- msg_Dbg( p_dec, "Using %d bytes output buffer", p_sys->i_output_max );
- p_sys->p_output = av_malloc( p_sys->i_output_max );
-
p_sys->i_reject_count = 0;
p_sys->b_extract = false;
p_sys->i_previous_channels = 0;
@@ -252,14 +225,59 @@ int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_
return VLC_SUCCESS;
}
+/**
+ * Allocates decoded audio buffer for libavcodec to use.
+ */
+static int GetAudioBuf( AVCodecContext *ctx, AVFrame *buf )
+{
+ block_t *block;
+ bool planar = av_sample_fmt_is_planar( ctx->sample_fmt );
+ unsigned channels = planar ? 1 : ctx->channels;
+ unsigned planes = planar ? ctx->channels : 1;
+
+ int bytes = av_samples_get_buffer_size( &buf->linesize[0], channels,
+ buf->nb_samples, ctx->sample_fmt,
+ 16 );
+ assert( bytes >= 0 );
+ block = block_Alloc( bytes * planes );
+ if( unlikely(block == NULL) )
+ return AVERROR(ENOMEM);
+
+ block->i_nb_samples = buf->nb_samples;
+ buf->opaque = block;
+
+ if( planes > AV_NUM_DATA_POINTERS )
+ {
+ uint8_t **ext = malloc( sizeof( *ext ) * planes );
+ if( unlikely(ext == NULL) )
+ {
+ block_Release( block );
+ return AVERROR(ENOMEM);
+ }
+ buf->extended_data = ext;
+ }
+ else
+ buf->extended_data = buf->data;
+
+ uint8_t *buffer = block->p_buffer;
+ for( unsigned i = 0; i < planes; i++ )
+ {
+ buf->linesize[i] = buf->linesize[0];
+ buf->extended_data[i] = buffer;
+ buffer += bytes;
+ }
+
+ return 0;
+}
+
+
/*****************************************************************************
* DecodeAudio: Called to decode one frame
*****************************************************************************/
aout_buffer_t * DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
{
decoder_sys_t *p_sys = p_dec->p_sys;
- int i_used, i_output;
- AVPacket pkt;
+ AVCodecContext *ctx = p_sys->p_context;
if( !pp_block || !*pp_block ) return NULL;
@@ -298,47 +316,37 @@ aout_buffer_t * DecodeAudio ( decoder_t *p_dec, block_
p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
if( !p_block )
return NULL;
- *pp_block = p_block;
p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
memset( &p_block->p_buffer[p_block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
p_block->i_flags |= BLOCK_FLAG_PRIVATE_REALLOCATED;
}
- do
- {
- i_output = __MAX( p_block->i_buffer, p_sys->i_output_max );
- if( i_output > p_sys->i_output_max )
- {
- /* Grow output buffer if necessary (eg. for PCM data) */
- p_sys->p_output = av_realloc( p_sys->p_output, i_output );
- }
+ AVFrame frame;
+ memset( &frame, 0, sizeof( frame ) );
+ for( int got_frame = 0; !got_frame; )
+ {
+ if( p_block->i_buffer == 0 )
+ goto end;
+
+ AVPacket pkt;
av_init_packet( &pkt );
pkt.data = p_block->p_buffer;
pkt.size = p_block->i_buffer;
- i_used = avcodec_decode_audio3( p_sys->p_context,
- (int16_t*)p_sys->p_output, &i_output,
- &pkt );
-
- if( i_used < 0 || i_output < 0 )
+ int used = avcodec_decode_audio4( ctx, &frame, &got_frame, &pkt );
+ if( used < 0 )
{
- if( i_used < 0 )
- msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
- p_block->i_buffer );
-
+ msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
+ p_block->i_buffer );
goto end;
}
- else if( (size_t)i_used > p_block->i_buffer )
- {
- i_used = p_block->i_buffer;
- }
- p_block->i_buffer -= i_used;
- p_block->p_buffer += i_used;
+ assert( p_block->i_buffer >= (unsigned)used );
+ p_block->p_buffer += used;
+ p_block->i_buffer -= used;
+ }
- } while( p_block->i_buffer > 0 && i_output <= 0 );
-
if( p_sys->p_context->channels <= 0 || p_sys->p_context->channels > 8 ||
p_sys->p_context->sample_rate <= 0 )
{
@@ -356,58 +364,77 @@ aout_buffer_t * DecodeAudio ( decoder_t *p_dec, block_
date_Set( &p_sys->end_date, p_block->i_pts );
}
- //block_Release( p_block );
-
+ if( p_block->i_buffer == 0 )
+ { /* Done with this buffer */
+ block_Release( p_block );
+ *pp_block = NULL;
+ }
+
+ /* NOTE WELL: Beyond this point, p_block now refers to the DECODED block */
+ p_block = frame.opaque;
SetupOutputFormat( p_dec, true );
/* Silent unwanted samples */
if( p_sys->i_reject_count > 0 )
{
- memset( p_sys->p_output, 0, i_output );
+ memset( p_block->p_buffer, 0, p_block->i_buffer );
p_sys->i_reject_count--;
}
- int i_samples = i_output / (p_dec->fmt_out.audio.i_bitspersample / 8) / p_sys->p_context->channels;
- if (i_samples == 0)
- return NULL;
-
- block_t *p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
+ block_t *p_buffer = decoder_NewAudioBuffer( p_dec, p_block->i_nb_samples );
if (!p_buffer)
return NULL;
- p_buffer->i_pts = date_Get( &p_sys->end_date );
- p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) - p_buffer->i_pts;
+ assert( p_block->i_nb_samples >= (unsigned)frame.nb_samples );
+ assert( p_buffer->i_buffer >= p_block->i_buffer );
- int sample_planar = av_sample_fmt_is_planar( p_sys->p_context->sample_fmt );
- if( sample_planar )
- Interleave( p_buffer->p_buffer, p_sys->p_output, i_samples, p_sys->p_context->channels, p_dec->fmt_out.audio.i_format );
+ /* Interleave audio if required */
+ if( av_sample_fmt_is_planar( ctx->sample_fmt ) )
+ {
+ aout_Interleave( p_buffer->p_buffer, p_block->p_buffer,
+ p_block->i_nb_samples, ctx->channels,
+ p_dec->fmt_out.audio.i_format );
+ if( ctx->channels > AV_NUM_DATA_POINTERS )
+ free( frame.extended_data );
+ block_Release( p_block );
+ p_block = p_buffer;
+ }
+ else /* FIXME: improve decoder_NewAudioBuffer(), avoid useless buffer... */
+ block_Release( p_buffer );
- if( p_sys->b_extract == !!sample_planar )
- memcpy( p_sys->p_output, p_buffer->p_buffer, p_buffer->i_buffer );
-
if (p_sys->b_extract)
- aout_ChannelExtract( p_buffer->p_buffer, p_dec->fmt_out.audio.i_channels,
- p_sys->p_output, p_sys->p_context->channels, i_samples,
- p_sys->pi_extraction, p_dec->fmt_out.audio.i_bitspersample );
+ { /* TODO: do not drop channels... at least not here */
+ p_buffer = block_Alloc( p_dec->fmt_out.audio.i_bytes_per_frame
+ * frame.nb_samples );
+ if( unlikely(p_buffer == NULL) )
+ {
+ block_Release( p_block );
+ return NULL;
+ }
+ aout_ChannelExtract( p_buffer->p_buffer,
+ p_dec->fmt_out.audio.i_channels,
+ p_block->p_buffer, ctx->channels,
+ frame.nb_samples, p_sys->pi_extraction,
+ p_dec->fmt_out.audio.i_bitspersample );
+ block_Release( p_block );
+ p_block = p_buffer;
+ }
- return p_buffer;
+ p_block->i_nb_samples = frame.nb_samples;
+ p_block->i_buffer = frame.nb_samples
+ * p_dec->fmt_out.audio.i_bytes_per_frame;
+ p_block->i_pts = date_Get( &p_sys->end_date );
+ p_block->i_length = date_Increment( &p_sys->end_date, frame.nb_samples )
+ - p_block->i_pts;
+ return p_block;
end:
block_Release(p_block);
+ *pp_block = NULL;
return NULL;
}
/*****************************************************************************
- * EndAudioDec: audio decoder destruction
- *****************************************************************************/
-void EndAudioDec( decoder_t *p_dec )
-{
- decoder_sys_t *p_sys = p_dec->p_sys;
-
- av_free( p_sys->p_output );
-}
-
-/*****************************************************************************
*
*****************************************************************************/
@@ -419,13 +446,11 @@ vlc_fourcc_t GetVlcAudioFormat( int fmt )
[AV_SAMPLE_FMT_S32] = VLC_CODEC_S32N,
[AV_SAMPLE_FMT_FLT] = VLC_CODEC_FL32,
[AV_SAMPLE_FMT_DBL] = VLC_CODEC_FL64,
-#ifdef HAVE_AVUTIL_PLANAR
[AV_SAMPLE_FMT_U8P] = VLC_CODEC_U8,
[AV_SAMPLE_FMT_S16P] = VLC_CODEC_S16N,
[AV_SAMPLE_FMT_S32P] = VLC_CODEC_S32N,
[AV_SAMPLE_FMT_FLTP] = VLC_CODEC_FL32,
[AV_SAMPLE_FMT_DBLP] = VLC_CODEC_FL64,
-#endif
};
if( (sizeof(fcc) / sizeof(fcc[0])) > (unsigned)fmt )
return fcc[fmt];