From 0588935e64fec4e3e827ede83bc84a4a4d58733a Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 23 Dec 2012 16:50:06 -0500 Subject: [PATCH 1/5] adx: decode directly to the user-provided AVFrame --- libavcodec/adxdec.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/libavcodec/adxdec.c b/libavcodec/adxdec.c index d8ea351858..ea40993124 100644 --- a/libavcodec/adxdec.c +++ b/libavcodec/adxdec.c @@ -52,9 +52,6 @@ static av_cold int adx_decode_init(AVCodecContext *avctx) avctx->sample_fmt = AV_SAMPLE_FMT_S16P; - avcodec_get_frame_defaults(&c->frame); - avctx->coded_frame = &c->frame; - return 0; } @@ -98,6 +95,7 @@ static int adx_decode(ADXContext *c, int16_t *out, int offset, static int adx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { + AVFrame *frame = data; int buf_size = avpkt->size; ADXContext *c = avctx->priv_data; int16_t **samples; @@ -142,12 +140,12 @@ static int adx_decode_frame(AVCodecContext *avctx, void *data, } /* get output buffer */ - c->frame.nb_samples = num_blocks * BLOCK_SAMPLES; - if ((ret = ff_get_buffer(avctx, &c->frame)) < 0) { + frame->nb_samples = num_blocks * BLOCK_SAMPLES; + if ((ret = ff_get_buffer(avctx, frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } - samples = (int16_t **)c->frame.extended_data; + samples = (int16_t **)frame->extended_data; samples_offset = 0; while (num_blocks--) { @@ -163,8 +161,7 @@ static int adx_decode_frame(AVCodecContext *avctx, void *data, samples_offset += BLOCK_SAMPLES; } - *got_frame_ptr = 1; - *(AVFrame *)data = c->frame; + *got_frame_ptr = 1; return buf - avpkt->data; } From bae4f47938bbc63b7ab567edce9a5e778a37a3bf Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 23 Dec 2012 16:51:41 -0500 Subject: [PATCH 2/5] adxenc: alloc/free coded_frame instead of keeping it in the ADXContext --- libavcodec/adx.h | 1 - libavcodec/adxenc.c | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/libavcodec/adx.h b/libavcodec/adx.h index 47d9f24a85..ff4c2d6f35 100644 --- a/libavcodec/adx.h +++ b/libavcodec/adx.h @@ -40,7 +40,6 @@ typedef struct ADXChannelState { } ADXChannelState; typedef struct ADXContext { - AVFrame frame; int channels; ADXChannelState prev[2]; int header_parsed; diff --git a/libavcodec/adxenc.c b/libavcodec/adxenc.c index 8a50539626..7a9c06a591 100644 --- a/libavcodec/adxenc.c +++ b/libavcodec/adxenc.c @@ -107,6 +107,14 @@ static int adx_encode_header(AVCodecContext *avctx, uint8_t *buf, int bufsize) return HEADER_SIZE; } +#if FF_API_OLD_ENCODE_AUDIO +static av_cold int adx_encode_close(AVCodecContext *avctx) +{ + av_freep(&avctx->coded_frame); + return 0; +} +#endif + static av_cold int adx_encode_init(AVCodecContext *avctx) { ADXContext *c = avctx->priv_data; @@ -118,8 +126,8 @@ static av_cold int adx_encode_init(AVCodecContext *avctx) avctx->frame_size = BLOCK_SAMPLES; #if FF_API_OLD_ENCODE_AUDIO - avcodec_get_frame_defaults(&c->frame); - avctx->coded_frame = &c->frame; + if (!(avctx->coded_frame = avcodec_alloc_frame())) + return AVERROR(ENOMEM); #endif /* the cutoff can be adjusted, but this seems to work pretty well */ @@ -169,6 +177,9 @@ AVCodec ff_adpcm_adx_encoder = { .id = AV_CODEC_ID_ADPCM_ADX, .priv_data_size = sizeof(ADXContext), .init = adx_encode_init, +#if FF_API_OLD_ENCODE_AUDIO + .close = adx_encode_close, +#endif .encode2 = adx_encode_frame, .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE }, From 5cd597f22ff5b1587e005153ad5bd33ecf6907c8 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 23 Dec 2012 16:56:17 -0500 Subject: [PATCH 3/5] alac: decode directly to the user-provided AVFrame --- libavcodec/alac.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/libavcodec/alac.c b/libavcodec/alac.c index 1a3f769513..0c5bdd3e45 100644 --- a/libavcodec/alac.c +++ b/libavcodec/alac.c @@ -58,7 +58,6 @@ typedef struct { AVCodecContext *avctx; - AVFrame frame; GetBitContext gb; int channels; @@ -248,7 +247,7 @@ static void append_extra_bits(int32_t *buffer[2], int32_t *extra_bits_buffer[2], buffer[ch][i] = (buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i]; } -static int decode_element(AVCodecContext *avctx, void *data, int ch_index, +static int decode_element(AVCodecContext *avctx, AVFrame *frame, int ch_index, int channels) { ALACContext *alac = avctx->priv_data; @@ -283,8 +282,8 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, } if (!alac->nb_samples) { /* get output buffer */ - alac->frame.nb_samples = output_samples; - if ((ret = ff_get_buffer(avctx, &alac->frame)) < 0) { + frame->nb_samples = output_samples; + if ((ret = ff_get_buffer(avctx, frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } @@ -296,7 +295,7 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, alac->nb_samples = output_samples; if (alac->sample_size > 16) { for (ch = 0; ch < channels; ch++) - alac->output_samples_buffer[ch] = (int32_t *)alac->frame.extended_data[ch_index + ch]; + alac->output_samples_buffer[ch] = (int32_t *)frame->extended_data[ch_index + ch]; } if (is_compressed) { @@ -377,7 +376,7 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index, switch(alac->sample_size) { case 16: { for (ch = 0; ch < channels; ch++) { - int16_t *outbuffer = (int16_t *)alac->frame.extended_data[ch_index + ch]; + int16_t *outbuffer = (int16_t *)frame->extended_data[ch_index + ch]; for (i = 0; i < alac->nb_samples; i++) *outbuffer++ = alac->output_samples_buffer[ch][i]; }} @@ -397,6 +396,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { ALACContext *alac = avctx->priv_data; + AVFrame *frame = data; enum AlacRawDataBlockType element; int channels; int ch, ret, got_end; @@ -423,7 +423,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, return AVERROR_INVALIDDATA; } - ret = decode_element(avctx, data, + ret = decode_element(avctx, frame, ff_alac_channel_layout_offsets[alac->channels - 1][ch], channels); if (ret < 0 && get_bits_left(&alac->gb)) @@ -441,8 +441,7 @@ static int alac_decode_frame(AVCodecContext *avctx, void *data, avpkt->size * 8 - get_bits_count(&alac->gb)); } - *got_frame_ptr = 1; - *(AVFrame *)data = alac->frame; + *got_frame_ptr = 1; return avpkt->size; } @@ -563,9 +562,6 @@ static av_cold int alac_decode_init(AVCodecContext * avctx) return ret; } - avcodec_get_frame_defaults(&alac->frame); - avctx->coded_frame = &alac->frame; - return 0; } From 9b0b355e973133de393c5defc63dc905891c0760 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 23 Dec 2012 16:59:43 -0500 Subject: [PATCH 4/5] als: decode directly to the user-provided AVFrame --- libavcodec/alsdec.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/libavcodec/alsdec.c b/libavcodec/alsdec.c index ff2a735bdf..622522aaa6 100644 --- a/libavcodec/alsdec.c +++ b/libavcodec/alsdec.c @@ -192,7 +192,6 @@ typedef struct { typedef struct { AVCodecContext *avctx; - AVFrame frame; ALSSpecificConfig sconf; GetBitContext gb; DSPContext dsp; @@ -1432,6 +1431,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { ALSDecContext *ctx = avctx->priv_data; + AVFrame *frame = data; ALSSpecificConfig *sconf = &ctx->sconf; const uint8_t *buffer = avpkt->data; int buffer_size = avpkt->size; @@ -1461,8 +1461,8 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, ctx->frame_id++; /* get output buffer */ - ctx->frame.nb_samples = ctx->cur_frame_length; - if ((ret = ff_get_buffer(avctx, &ctx->frame)) < 0) { + frame->nb_samples = ctx->cur_frame_length; + if ((ret = ff_get_buffer(avctx, frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } @@ -1470,7 +1470,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, // transform decoded frame into output format #define INTERLEAVE_OUTPUT(bps) \ { \ - int##bps##_t *dest = (int##bps##_t*)ctx->frame.data[0]; \ + int##bps##_t *dest = (int##bps##_t*)frame->data[0]; \ shift = bps - ctx->avctx->bits_per_raw_sample; \ for (sample = 0; sample < ctx->cur_frame_length; sample++) \ for (c = 0; c < avctx->channels; c++) \ @@ -1488,7 +1488,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, int swap = HAVE_BIGENDIAN != sconf->msb_first; if (ctx->avctx->bits_per_raw_sample == 24) { - int32_t *src = (int32_t *)ctx->frame.data[0]; + int32_t *src = (int32_t *)frame->data[0]; for (sample = 0; sample < ctx->cur_frame_length * avctx->channels; @@ -1509,7 +1509,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, if (swap) { if (ctx->avctx->bits_per_raw_sample <= 16) { - int16_t *src = (int16_t*) ctx->frame.data[0]; + int16_t *src = (int16_t*) frame->data[0]; int16_t *dest = (int16_t*) ctx->crc_buffer; for (sample = 0; sample < ctx->cur_frame_length * avctx->channels; @@ -1517,12 +1517,12 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, *dest++ = av_bswap16(src[sample]); } else { ctx->dsp.bswap_buf((uint32_t*)ctx->crc_buffer, - (uint32_t *)ctx->frame.data[0], + (uint32_t *)frame->data[0], ctx->cur_frame_length * avctx->channels); } crc_source = ctx->crc_buffer; } else { - crc_source = ctx->frame.data[0]; + crc_source = frame->data[0]; } ctx->crc = av_crc(ctx->crc_table, ctx->crc, crc_source, @@ -1538,9 +1538,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, } } - *got_frame_ptr = 1; - *(AVFrame *)data = ctx->frame; - + *got_frame_ptr = 1; bytes_read = invalid_frame ? buffer_size : (get_bits_count(&ctx->gb) + 7) >> 3; @@ -1737,9 +1735,6 @@ static av_cold int decode_init(AVCodecContext *avctx) ff_dsputil_init(&ctx->dsp, avctx); - avcodec_get_frame_defaults(&ctx->frame); - avctx->coded_frame = &ctx->frame; - return 0; } From e3db34291f4401a16f6ac92721617a9f33cd4c31 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 23 Dec 2012 17:01:33 -0500 Subject: [PATCH 5/5] amrnb: decode directly to the user-provided AVFrame --- libavcodec/amrnbdec.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/libavcodec/amrnbdec.c b/libavcodec/amrnbdec.c index 7db12dd001..237d47b7cb 100644 --- a/libavcodec/amrnbdec.c +++ b/libavcodec/amrnbdec.c @@ -96,7 +96,6 @@ #define AMR_AGC_ALPHA 0.9 typedef struct AMRContext { - AVFrame avframe; ///< AVFrame for decoded samples AMRNBFrame frame; ///< decoded AMR parameters (lsf coefficients, codebook indexes, etc) uint8_t bad_frame_indicator; ///< bad frame ? 1 : 0 enum Mode cur_frame_mode; @@ -177,9 +176,6 @@ static av_cold int amrnb_decode_init(AVCodecContext *avctx) for (i = 0; i < 4; i++) p->prediction_error[i] = MIN_ENERGY; - avcodec_get_frame_defaults(&p->avframe); - avctx->coded_frame = &p->avframe; - return 0; } @@ -936,6 +932,7 @@ static int amrnb_decode_frame(AVCodecContext *avctx, void *data, { AMRContext *p = avctx->priv_data; // pointer to private data + AVFrame *frame = data; const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; float *buf_out; // pointer to the output data buffer @@ -947,12 +944,12 @@ static int amrnb_decode_frame(AVCodecContext *avctx, void *data, const float *synth_fixed_vector; // pointer to the fixed vector that synthesis should use /* get output buffer */ - p->avframe.nb_samples = AMR_BLOCK_SIZE; - if ((ret = ff_get_buffer(avctx, &p->avframe)) < 0) { + frame->nb_samples = AMR_BLOCK_SIZE; + if ((ret = ff_get_buffer(avctx, frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } - buf_out = (float *)p->avframe.data[0]; + buf_out = (float *)frame->data[0]; p->cur_frame_mode = unpack_bitstream(p, buf, buf_size); if (p->cur_frame_mode == NO_DATA) { @@ -1058,8 +1055,7 @@ static int amrnb_decode_frame(AVCodecContext *avctx, void *data, ff_weighted_vector_sumf(p->lsf_avg, p->lsf_avg, p->lsf_q[3], 0.84, 0.16, LP_FILTER_ORDER); - *got_frame_ptr = 1; - *(AVFrame *)data = p->avframe; + *got_frame_ptr = 1; /* return the amount of bytes consumed if everything was OK */ return frame_sizes_nb[p->cur_frame_mode] + 1; // +7 for rounding and +8 for TOC