avcodec/decode: add a flags parameter to ff_reget_buffer()

Some decoders may not need a writable buffer in some specific cases, but only
a reference to the existing buffer with updated frame properties instead, for
the purpose of returning duplicate frames. For this, the
FF_REGET_BUFFER_FLAG_READONLY flag is added, which will prevent potential
allocations and buffer copies when they are not needed.

Signed-off-by: James Almer <jamrial@gmail.com>
pull/321/head^2
James Almer 5 years ago
parent 39f1295937
commit 9ea6d2149e
  1. 2
      libavcodec/aasc.c
  2. 2
      libavcodec/anm.c
  3. 2
      libavcodec/ansi.c
  4. 2
      libavcodec/avs.c
  5. 2
      libavcodec/bethsoftvideo.c
  6. 2
      libavcodec/bink.c
  7. 2
      libavcodec/c93.c
  8. 2
      libavcodec/cdgraphics.c
  9. 2
      libavcodec/cinepak.c
  10. 4
      libavcodec/clearvideo.c
  11. 2
      libavcodec/cpia.c
  12. 2
      libavcodec/cscd.c
  13. 8
      libavcodec/decode.c
  14. 2
      libavcodec/dsicinvideo.c
  15. 4
      libavcodec/fic.c
  16. 2
      libavcodec/flashsv.c
  17. 6
      libavcodec/flicvideo.c
  18. 2
      libavcodec/gifdec.c
  19. 2
      libavcodec/indeo2.c
  20. 7
      libavcodec/internal.h
  21. 2
      libavcodec/jvdec.c
  22. 2
      libavcodec/mmvideo.c
  23. 2
      libavcodec/motionpixels.c
  24. 2
      libavcodec/msrle.c
  25. 2
      libavcodec/mss1.c
  26. 2
      libavcodec/mss2.c
  27. 2
      libavcodec/mss3.c
  28. 2
      libavcodec/mss4.c
  29. 2
      libavcodec/msvideo1.c
  30. 2
      libavcodec/nuv.c
  31. 2
      libavcodec/pafvideo.c
  32. 2
      libavcodec/qtrle.c
  33. 2
      libavcodec/roqvideodec.c
  34. 2
      libavcodec/rpza.c
  35. 2
      libavcodec/rscc.c
  36. 2
      libavcodec/scpr.c
  37. 2
      libavcodec/screenpresso.c
  38. 2
      libavcodec/smacker.c
  39. 2
      libavcodec/smc.c
  40. 2
      libavcodec/tiertexseqv.c
  41. 2
      libavcodec/truemotion1.c
  42. 2
      libavcodec/truemotion2.c
  43. 2
      libavcodec/tscc.c
  44. 2
      libavcodec/tscc2.c
  45. 2
      libavcodec/ulti.c
  46. 2
      libavcodec/vmnc.c
  47. 2
      libavcodec/xxan.c
  48. 2
      libavcodec/yop.c

@ -91,7 +91,7 @@ static int aasc_decode_frame(AVCodecContext *avctx,
return AVERROR_INVALIDDATA;
}
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
compr = AV_RL32(buf);

@ -122,7 +122,7 @@ static int decode_frame(AVCodecContext *avctx,
if (buf_size < 7)
return AVERROR_INVALIDDATA;
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
dst = s->frame->data[0];
dst_end = s->frame->data[0] + s->frame->linesize[0]*avctx->height;

@ -362,7 +362,7 @@ static int decode_frame(AVCodecContext *avctx,
const uint8_t *buf_end = buf+buf_size;
int ret, i, count;
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
if (!avctx->frame_number) {
for (i=0; i<avctx->height; i++)

@ -59,7 +59,7 @@ avs_decode_frame(AVCodecContext * avctx,
AvsBlockType type;
GetBitContext change_map = {0}; //init to silence warning
if ((ret = ff_reget_buffer(avctx, p)) < 0)
if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
return ret;
p->pict_type = AV_PICTURE_TYPE_P;
p->key_frame = 0;

@ -79,7 +79,7 @@ static int bethsoftvid_decode_frame(AVCodecContext *avctx,
int code, ret;
int yoffset;
if ((ret = ff_reget_buffer(avctx, vid->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, vid->frame, 0)) < 0)
return ret;
wrap_to_next_line = vid->frame->linesize[0] - avctx->width;

@ -1261,7 +1261,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPac
if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
return ret;
} else {
if ((ret = ff_reget_buffer(avctx, c->last)) < 0)
if ((ret = ff_reget_buffer(avctx, c->last, 0)) < 0)
return ret;
if ((ret = av_frame_ref(frame, c->last)) < 0)
return ret;

@ -138,7 +138,7 @@ static int decode_frame(AVCodecContext *avctx, void *data,
c93->currentpic ^= 1;
if ((ret = ff_reget_buffer(avctx, newpic)) < 0)
if ((ret = ff_reget_buffer(avctx, newpic, 0)) < 0)
return ret;
stride = newpic->linesize[0];

@ -283,7 +283,7 @@ static int cdg_decode_frame(AVCodecContext *avctx,
bytestream2_init(&gb, avpkt->data, avpkt->size);
if ((ret = ff_reget_buffer(avctx, cc->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, cc->frame, 0)) < 0)
return ret;
if (!cc->cleared) {
memset(cc->frame->data[0], 0, cc->frame->linesize[0] * avctx->height);

@ -473,7 +473,7 @@ static int cinepak_decode_frame(AVCodecContext *avctx,
return ret;
}
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
if (s->palette_video) {

@ -524,7 +524,7 @@ static int clv_decode_frame(AVCodecContext *avctx, void *data,
return AVERROR_INVALIDDATA;
}
if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
return ret;
c->pic->key_frame = 1;
@ -558,7 +558,7 @@ static int clv_decode_frame(AVCodecContext *avctx, void *data,
if (c->pmb_width * c->pmb_height > 8LL*(buf_size - bytestream2_tell(&gb)))
return AVERROR_INVALIDDATA;
if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
return ret;
ret = av_frame_copy(c->pic, c->prev);

@ -100,7 +100,7 @@ static int cpia_decode_frame(AVCodecContext *avctx,
}
// Get buffer filled with previous frame
if ((ret = ff_reget_buffer(avctx, frame)) < 0)
if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
return ret;

@ -77,7 +77,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
return AVERROR_INVALIDDATA;
}
if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
return ret;
// decompress data

@ -1970,7 +1970,7 @@ int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
return ret;
}
static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
{
AVFrame *tmp;
int ret;
@ -1986,7 +1986,7 @@ static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
if (!frame->data[0])
return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
if (av_frame_is_writable(frame))
if ((flags & FF_REGET_BUFFER_FLAG_READONLY) || av_frame_is_writable(frame))
return ff_decode_frame_props(avctx, frame);
tmp = av_frame_alloc();
@ -2007,9 +2007,9 @@ static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
return 0;
}
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
{
int ret = reget_buffer_internal(avctx, frame);
int ret = reget_buffer_internal(avctx, frame, flags);
if (ret < 0)
av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
return ret;

@ -290,7 +290,7 @@ static int cinvideo_decode_frame(AVCodecContext *avctx,
break;
}
if ((res = ff_reget_buffer(avctx, cin->frame)) < 0)
if ((res = ff_reget_buffer(avctx, cin->frame, 0)) < 0)
return res;
memcpy(cin->frame->data[1], cin->palette, sizeof(cin->palette));

@ -278,7 +278,7 @@ static int fic_decode_frame(AVCodecContext *avctx, void *data,
int skip_cursor = ctx->skip_cursor;
uint8_t *sdata;
if ((ret = ff_reget_buffer(avctx, ctx->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, ctx->frame, 0)) < 0)
return ret;
/* Header + at least one slice (4) */
@ -421,7 +421,7 @@ static int fic_decode_frame(AVCodecContext *avctx, void *data,
}
/* Make sure we use a user-supplied buffer. */
if ((ret = ff_reget_buffer(avctx, ctx->final_frame)) < 0) {
if ((ret = ff_reget_buffer(avctx, ctx->final_frame, 0)) < 0) {
av_log(avctx, AV_LOG_ERROR, "Could not make frame writable.\n");
return ret;
}

@ -368,7 +368,7 @@ static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
s->image_width, s->image_height, s->block_width, s->block_height,
h_blocks, v_blocks, h_part, v_part);
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
/* loop over all block columns */

@ -185,7 +185,7 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx,
bytestream2_init(&g2, buf, buf_size);
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
pixels = s->frame->data[0];
@ -519,7 +519,7 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
bytestream2_init(&g2, buf, buf_size);
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
pixels = s->frame->data[0];
@ -817,7 +817,7 @@ static int flic_decode_frame_24BPP(AVCodecContext *avctx,
bytestream2_init(&g2, buf, buf_size);
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
pixels = s->frame->data[0];

@ -513,7 +513,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
return AVERROR_INVALIDDATA;
}
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
s->frame->pict_type = AV_PICTURE_TYPE_P;

@ -161,7 +161,7 @@ static int ir2_decode_frame(AVCodecContext *avctx,
int start, ret;
int ltab, ctab;
if ((ret = ff_reget_buffer(avctx, p)) < 0)
if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
return ret;
start = 48; /* hardcoded for now */

@ -320,11 +320,12 @@ static av_always_inline float ff_exp2fi(int x) {
*/
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags);
#define FF_REGET_BUFFER_FLAG_READONLY 1 ///< the returned buffer does not need to be writable
/**
* Identical in function to av_frame_make_writable(), except it uses
* ff_get_buffer() to allocate the buffer when needed.
* Identical in function to ff_get_buffer(), except it reuses the existing buffer
* if available.
*/
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame);
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags);
int ff_thread_can_start_frame(AVCodecContext *avctx);

@ -168,7 +168,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
GetBitContext gb;
init_get_bits(&gb, buf, 8 * video_size);
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
if (avctx->height/8 * (avctx->width/8) > 4 * video_size) {

@ -201,7 +201,7 @@ static int mm_decode_frame(AVCodecContext *avctx,
buf_size -= MM_PREAMBLE_SIZE;
bytestream2_init(&s->gb, buf, buf_size);
if ((res = ff_reget_buffer(avctx, s->frame)) < 0)
if ((res = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return res;
switch(type) {

@ -292,7 +292,7 @@ static int mp_decode_frame(AVCodecContext *avctx,
GetBitContext gb;
int i, count1, count2, sz, ret;
if ((ret = ff_reget_buffer(avctx, mp->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, mp->frame, 0)) < 0)
return ret;
/* le32 bitstream msb first */

@ -93,7 +93,7 @@ static int msrle_decode_frame(AVCodecContext *avctx,
if (buf_size < 2) //Minimally a end of picture code should be there
return AVERROR_INVALIDDATA;
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
if (avctx->bits_per_coded_sample > 1 && avctx->bits_per_coded_sample <= 8) {

@ -154,7 +154,7 @@ static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
arith_init(&acoder, &gb);
if ((ret = ff_reget_buffer(avctx, ctx->pic)) < 0)
if ((ret = ff_reget_buffer(avctx, ctx->pic, 0)) < 0)
return ret;
c->pal_pic = ctx->pic->data[0] + ctx->pic->linesize[0] * (avctx->height - 1);

@ -618,7 +618,7 @@ static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
return AVERROR_INVALIDDATA;
}
} else {
if ((ret = ff_reget_buffer(avctx, ctx->last_pic)) < 0)
if ((ret = ff_reget_buffer(avctx, ctx->last_pic, 0)) < 0)
return ret;
if ((ret = av_frame_ref(frame, ctx->last_pic)) < 0)
return ret;

@ -737,7 +737,7 @@ static int mss3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
return buf_size;
c->got_error = 0;
if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
return ret;
c->pic->key_frame = keyframe;
c->pic->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;

@ -558,7 +558,7 @@ static int mss4_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
if (frame_type != SKIP_FRAME && 8*buf_size < 8*HEADER_SIZE + mb_width*mb_height)
return AVERROR_INVALIDDATA;
if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
return ret;
c->pic->key_frame = (frame_type == INTRA_FRAME);
c->pic->pict_type = (frame_type == INTRA_FRAME) ? AV_PICTURE_TYPE_I

@ -310,7 +310,7 @@ static int msvideo1_decode_frame(AVCodecContext *avctx,
return AVERROR_INVALIDDATA;
}
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
if (s->mode_8bit) {

@ -268,7 +268,7 @@ retry:
init_frame = 1;
}
if ((result = ff_reget_buffer(avctx, c->pic)) < 0)
if ((result = ff_reget_buffer(avctx, c->pic, 0)) < 0)
return result;
if (init_frame) {
memset(c->pic->data[0], 0, avctx->height * c->pic->linesize[0]);

@ -289,7 +289,7 @@ static int paf_video_decode(AVCodecContext *avctx, void *data,
c->video_size / 32 - (int64_t)bytestream2_get_bytes_left(&c->gb) > c->video_size / 32 * (int64_t)avctx->discard_damaged_percentage / 100)
return AVERROR_INVALIDDATA;
if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
return ret;
if (code & 0x20) { // frame is keyframe

@ -490,7 +490,7 @@ static int qtrle_decode_frame(AVCodecContext *avctx,
start_line = 0;
height = s->avctx->height;
}
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
row_ptr = s->frame->linesize[0] * start_line;

@ -206,7 +206,7 @@ static int roq_decode_frame(AVCodecContext *avctx,
int copy = !s->current_frame->data[0] && s->last_frame->data[0];
int ret;
if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->current_frame, 0)) < 0)
return ret;
if (copy) {

@ -108,7 +108,7 @@ static int rpza_decode_stream(RpzaContext *s)
if (total_blocks / 32 > bytestream2_get_bytes_left(&s->gb))
return AVERROR_INVALIDDATA;
if ((ret = ff_reget_buffer(s->avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(s->avctx, s->frame, 0)) < 0)
return ret;
pixels = (uint16_t *)s->frame->data[0];
stride = s->frame->linesize[0] / 2;

@ -310,7 +310,7 @@ static int rscc_decode_frame(AVCodecContext *avctx, void *data,
}
/* Allocate when needed */
ret = ff_reget_buffer(avctx, ctx->reference);
ret = ff_reget_buffer(avctx, ctx->reference, 0);
if (ret < 0)
goto end;

@ -504,7 +504,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
return ret;
}
if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->current_frame, 0)) < 0)
return ret;
bytestream2_init(gb, avpkt->data, avpkt->size);

@ -145,7 +145,7 @@ static int screenpresso_decode_frame(AVCodecContext *avctx, void *data,
return AVERROR_UNKNOWN;
}
ret = ff_reget_buffer(avctx, ctx->current);
ret = ff_reget_buffer(avctx, ctx->current, 0);
if (ret < 0)
return ret;

@ -428,7 +428,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
if (avpkt->size <= 769)
return AVERROR_INVALIDDATA;
if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0)
if ((ret = ff_reget_buffer(avctx, smk->pic, 0)) < 0)
return ret;
/* make the palette available on the way out */

@ -445,7 +445,7 @@ static int smc_decode_frame(AVCodecContext *avctx,
bytestream2_init(&s->gb, buf, buf_size);
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
if (pal && pal_size == AVPALETTE_SIZE) {

@ -239,7 +239,7 @@ static int seqvideo_decode_frame(AVCodecContext *avctx,
SeqVideoContext *seq = avctx->priv_data;
if ((ret = ff_reget_buffer(avctx, seq->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, seq->frame, 0)) < 0)
return ret;
if (seqvideo_decode(seq, buf, buf_size))

@ -882,7 +882,7 @@ static int truemotion1_decode_frame(AVCodecContext *avctx,
if ((ret = truemotion1_decode_header(s)) < 0)
return ret;
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
if (compression_types[s->compression].algorithm == ALGO_RGB24H) {

@ -915,7 +915,7 @@ static int decode_frame(AVCodecContext *avctx,
return AVERROR(ENOMEM);
}
if ((ret = ff_reget_buffer(avctx, p)) < 0)
if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
return ret;
l->bdsp.bswap_buf((uint32_t *) l->buffer, (const uint32_t *) buf,

@ -103,7 +103,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
return AVERROR_UNKNOWN;
}
if ((ret = ff_reget_buffer(avctx, frame)) < 0)
if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
return ret;
if (ret != Z_DATA_ERROR) {

@ -240,7 +240,7 @@ static int tscc2_decode_frame(AVCodecContext *avctx, void *data,
return buf_size;
}
if ((ret = ff_reget_buffer(avctx, c->pic)) < 0) {
if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0) {
return ret;
}

@ -230,7 +230,7 @@ static int ulti_decode_frame(AVCodecContext *avctx,
int skip;
int tmp;
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
bytestream2_init(&s->gb, buf, buf_size);

@ -339,7 +339,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
if (12LL * chunks > bytestream2_get_bytes_left(gb))
return AVERROR_INVALIDDATA;
if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
return ret;
c->pic->key_frame = 0;

@ -410,7 +410,7 @@ static int xan_decode_frame(AVCodecContext *avctx,
int ftype;
int ret;
if ((ret = ff_reget_buffer(avctx, s->pic)) < 0)
if ((ret = ff_reget_buffer(avctx, s->pic, 0)) < 0)
return ret;
bytestream2_init(&s->gb, avpkt->data, avpkt->size);

@ -204,7 +204,7 @@ static int yop_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
return AVERROR_INVALIDDATA;
}
if ((ret = ff_reget_buffer(avctx, frame)) < 0)
if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
return ret;
if (!avctx->frame_number)

Loading…
Cancel
Save