[FFmpeg-devel] [PATCH] avcodec/avcodec: Deprecate AV_INPUT_BUFFER_MIN_SIZE
Andreas Rheinhardt
andreas.rheinhardt at outlook.com
Tue Feb 20 01:24:55 EET 2024
Andreas Rheinhardt:
> It used to be used with preallocated packet buffers with
> the old encode API, but said API is no more and therefore
> there is no reason for this to be public any more.
> So deprecate it and use an internal replacement
> for the encoders using it as an upper bound for the
> size of their headers.
>
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
> ---
> doc/APIchanges | 3 +++
> libavcodec/asvenc.c | 2 +-
> libavcodec/avcodec.h | 4 ++++
> libavcodec/encode.h | 6 ++++++
> libavcodec/ffv1enc.c | 4 ++--
> libavcodec/flashsv2enc.c | 2 +-
> libavcodec/gif.c | 2 +-
> libavcodec/huffyuvenc.c | 2 +-
> libavcodec/j2kenc.c | 2 +-
> libavcodec/jpeglsenc.c | 2 +-
> libavcodec/libxvid.c | 2 +-
> libavcodec/ljpegenc.c | 2 +-
> libavcodec/msrleenc.c | 2 +-
> libavcodec/msvideo1enc.c | 2 +-
> libavcodec/pngenc.c | 6 +++---
> libavcodec/proresenc_anatoliy.c | 4 ++--
> libavcodec/proresenc_kostya.c | 2 +-
> libavcodec/snowenc.c | 2 +-
> libavcodec/svq1enc.c | 2 +-
> libavcodec/tiffenc.c | 2 +-
> libavcodec/version_major.h | 1 +
> 21 files changed, 35 insertions(+), 21 deletions(-)
>
> diff --git a/doc/APIchanges b/doc/APIchanges
> index 77b9740891..27030ee03e 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -2,6 +2,9 @@ The last version increases of all libraries were on 2023-02-09
>
> API changes, most recent first:
>
> +2024-02-17 - xxxxxxxxxx - lavc 60.yy.100 - avcodec.h
> + Deprecate AV_INPUT_BUFFER_MIN_SIZE without replacement.
> +
> 2024-02-13 - xxxxxxxxxx - lavf 60.21.100 - avformat.h
> Add AVStreamGroup.disposition.
>
> diff --git a/libavcodec/asvenc.c b/libavcodec/asvenc.c
> index 50da46738c..6179b50583 100644
> --- a/libavcodec/asvenc.c
> +++ b/libavcodec/asvenc.c
> @@ -272,7 +272,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> }
>
> if ((ret = ff_alloc_packet(avctx, pkt, c->mb_height * c->mb_width * MAX_MB_SIZE +
> - AV_INPUT_BUFFER_MIN_SIZE)) < 0)
> + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
> return ret;
>
> init_put_bits(&a->pb, pkt->data, pkt->size);
> diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
> index 0018ccbb0c..43859251cc 100644
> --- a/libavcodec/avcodec.h
> +++ b/libavcodec/avcodec.h
> @@ -187,12 +187,16 @@ struct AVCodecParameters;
> * @{
> */
>
> +#if FF_API_BUFFER_MIN_SIZE
> /**
> * @ingroup lavc_encoding
> * minimum encoding buffer size
> * Used to avoid some checks during header writing.
> + * @deprecated Unused: avcodec_receive_packet() does not work
> + * with preallocated packet buffers.
> */
> #define AV_INPUT_BUFFER_MIN_SIZE 16384
> +#endif
>
> /**
> * @ingroup lavc_encoding
> diff --git a/libavcodec/encode.h b/libavcodec/encode.h
> index e019cd7702..85331e04b7 100644
> --- a/libavcodec/encode.h
> +++ b/libavcodec/encode.h
> @@ -26,6 +26,12 @@
> #include "avcodec.h"
> #include "packet.h"
>
> +/**
> + * Used by some encoders as upper bound for the length of headers.
> + * TODO: Use proper codec-specific upper bounds.
> + */
> +#define FF_INPUT_BUFFER_MIN_SIZE 16384
> +
> /**
> * Called by encoders to get the next frame for encoding.
> *
> diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
> index 4f581fbf6b..e510abf6ff 100644
> --- a/libavcodec/ffv1enc.c
> +++ b/libavcodec/ffv1enc.c
> @@ -1104,7 +1104,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> uint8_t keystate = 128;
> uint8_t *buf_p;
> int i, ret;
> - int64_t maxsize = AV_INPUT_BUFFER_MIN_SIZE
> + int64_t maxsize = FF_INPUT_BUFFER_MIN_SIZE
> + avctx->width*avctx->height*37LL*4;
>
> if(!pict) {
> @@ -1154,7 +1154,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> }
>
> if (f->version > 3)
> - maxsize = AV_INPUT_BUFFER_MIN_SIZE + avctx->width*avctx->height*3LL*4;
> + maxsize = FF_INPUT_BUFFER_MIN_SIZE + avctx->width*avctx->height*3LL*4;
>
> if (maxsize > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32) {
> av_log(avctx, AV_LOG_WARNING, "Cannot allocate worst case packet size, the encoding could fail\n");
> diff --git a/libavcodec/flashsv2enc.c b/libavcodec/flashsv2enc.c
> index 75b48eb1fd..6dcb9102a8 100644
> --- a/libavcodec/flashsv2enc.c
> +++ b/libavcodec/flashsv2enc.c
> @@ -857,7 +857,7 @@ static int flashsv2_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> int res;
> int keyframe = 0;
>
> - if ((res = ff_alloc_packet(avctx, pkt, s->frame_size + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
> + if ((res = ff_alloc_packet(avctx, pkt, s->frame_size + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
> return res;
>
> /* First frame needs to be a keyframe */
> diff --git a/libavcodec/gif.c b/libavcodec/gif.c
> index 7a32f8fc8b..49356236e7 100644
> --- a/libavcodec/gif.c
> +++ b/libavcodec/gif.c
> @@ -477,7 +477,7 @@ static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> const uint32_t *palette = NULL;
> int ret;
>
> - if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*7/5 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
> + if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*7/5 + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
> return ret;
> outbuf_ptr = pkt->data;
> end = pkt->data + pkt->size;
> diff --git a/libavcodec/huffyuvenc.c b/libavcodec/huffyuvenc.c
> index d49566cb5b..494ca7e603 100644
> --- a/libavcodec/huffyuvenc.c
> +++ b/libavcodec/huffyuvenc.c
> @@ -762,7 +762,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> const AVFrame * const p = pict;
> int i, j, size = 0, ret;
>
> - if ((ret = ff_alloc_packet(avctx, pkt, width * height * 3 * 4 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
> + if ((ret = ff_alloc_packet(avctx, pkt, width * height * 3 * 4 + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
> return ret;
>
> if (s->context) {
> diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
> index ebf21f6e7a..a80e74d4ec 100644
> --- a/libavcodec/j2kenc.c
> +++ b/libavcodec/j2kenc.c
> @@ -1534,7 +1534,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> uint8_t *chunkstart, *jp2cstart, *jp2hstart;
> const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
>
> - if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
> + if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*9 + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
> return ret;
>
> // init:
> diff --git a/libavcodec/jpeglsenc.c b/libavcodec/jpeglsenc.c
> index 4345ae44fa..3481c61ee3 100644
> --- a/libavcodec/jpeglsenc.c
> +++ b/libavcodec/jpeglsenc.c
> @@ -432,7 +432,7 @@ static av_cold int encode_jpegls_init(AVCodecContext *avctx)
> ctx->comps = 1;
> else
> ctx->comps = 3;
> - size = AV_INPUT_BUFFER_MIN_SIZE;
> + size = FF_INPUT_BUFFER_MIN_SIZE;
> /* INT_MAX due to PutBit-API. */
> if (avctx->width * (unsigned)avctx->height > (INT_MAX - size) / 4 / ctx->comps)
> return AVERROR(ERANGE);
> diff --git a/libavcodec/libxvid.c b/libavcodec/libxvid.c
> index d5232d4ee6..b9ac39429d 100644
> --- a/libavcodec/libxvid.c
> +++ b/libavcodec/libxvid.c
> @@ -739,7 +739,7 @@ static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> xvid_enc_frame_t xvid_enc_frame = { 0 };
> xvid_enc_stats_t xvid_enc_stats = { 0 };
>
> - if ((ret = ff_alloc_packet(avctx, pkt, mb_width*(int64_t)mb_height*MAX_MB_BYTES + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
> + if ((ret = ff_alloc_packet(avctx, pkt, mb_width*(int64_t)mb_height*MAX_MB_BYTES + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
> return ret;
>
> /* Start setting up the frame */
> diff --git a/libavcodec/ljpegenc.c b/libavcodec/ljpegenc.c
> index 71457220dd..46546e2160 100644
> --- a/libavcodec/ljpegenc.c
> +++ b/libavcodec/ljpegenc.c
> @@ -214,7 +214,7 @@ static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> const int height = avctx->height;
> const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
> const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
> - size_t max_pkt_size = AV_INPUT_BUFFER_MIN_SIZE;
> + size_t max_pkt_size = FF_INPUT_BUFFER_MIN_SIZE;
> int ret, header_bits;
>
> if( avctx->pix_fmt == AV_PIX_FMT_BGR0
> diff --git a/libavcodec/msrleenc.c b/libavcodec/msrleenc.c
> index 931e7af053..cc39aa308f 100644
> --- a/libavcodec/msrleenc.c
> +++ b/libavcodec/msrleenc.c
> @@ -252,7 +252,7 @@ static int msrle_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
>
> if ((ret = ff_alloc_packet(avctx, pkt, (
> avctx->width*2 /* worst case = rle every pixel */ + 2 /*end of line */
> - ) * avctx->height + 2 /* end of bitmap */ + AV_INPUT_BUFFER_MIN_SIZE)))
> + ) * avctx->height + 2 /* end of bitmap */ + FF_INPUT_BUFFER_MIN_SIZE)))
> return ret;
>
> if (pict->data[1]) {
> diff --git a/libavcodec/msvideo1enc.c b/libavcodec/msvideo1enc.c
> index 1fb8be8883..3bea3ed297 100644
> --- a/libavcodec/msvideo1enc.c
> +++ b/libavcodec/msvideo1enc.c
> @@ -78,7 +78,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> int skips = 0;
> int quality = 24;
>
> - if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
> + if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*9 + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
> return ret;
> dst= buf= pkt->data;
>
> diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
> index 7f7137b34d..28d645ea48 100644
> --- a/libavcodec/pngenc.c
> +++ b/libavcodec/pngenc.c
> @@ -637,7 +637,7 @@ static int encode_png(AVCodecContext *avctx, AVPacket *pkt,
> enc_row_size = deflateBound(&s->zstream.zstream,
> (avctx->width * s->bits_per_pixel + 7) >> 3);
> max_packet_size =
> - AV_INPUT_BUFFER_MIN_SIZE + // headers
> + FF_INPUT_BUFFER_MIN_SIZE + // headers
> avctx->height * (
> enc_row_size +
> 12 * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // IDAT * ceil(enc_row_size / IOBUF_SIZE)
> @@ -968,7 +968,7 @@ static int encode_apng(AVCodecContext *avctx, AVPacket *pkt,
> enc_row_size = deflateBound(&s->zstream.zstream,
> (avctx->width * s->bits_per_pixel + 7) >> 3);
> max_packet_size =
> - AV_INPUT_BUFFER_MIN_SIZE + // headers
> + FF_INPUT_BUFFER_MIN_SIZE + // headers
> avctx->height * (
> enc_row_size +
> (4 + 12) * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // fdAT * ceil(enc_row_size / IOBUF_SIZE)
> @@ -982,7 +982,7 @@ static int encode_apng(AVCodecContext *avctx, AVPacket *pkt,
> if (!pict)
> return AVERROR(EINVAL);
>
> - s->bytestream = s->extra_data = av_malloc(AV_INPUT_BUFFER_MIN_SIZE);
> + s->bytestream = s->extra_data = av_malloc(FF_INPUT_BUFFER_MIN_SIZE);
> if (!s->extra_data)
> return AVERROR(ENOMEM);
>
> diff --git a/libavcodec/proresenc_anatoliy.c b/libavcodec/proresenc_anatoliy.c
> index 5c93ea6566..02ab07d28e 100644
> --- a/libavcodec/proresenc_anatoliy.c
> +++ b/libavcodec/proresenc_anatoliy.c
> @@ -733,10 +733,10 @@ static int prores_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> uint8_t *buf;
> int compress_frame_size, pic_size, ret, is_top_field_first = 0;
> uint8_t frame_flags;
> - int frame_size = FFALIGN(avctx->width, 16) * FFALIGN(avctx->height, 16)*16 + 500 + AV_INPUT_BUFFER_MIN_SIZE; //FIXME choose tighter limit
> + int frame_size = FFALIGN(avctx->width, 16) * FFALIGN(avctx->height, 16)*16 + 500 + FF_INPUT_BUFFER_MIN_SIZE; //FIXME choose tighter limit
>
>
> - if ((ret = ff_alloc_packet(avctx, pkt, frame_size + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
> + if ((ret = ff_alloc_packet(avctx, pkt, frame_size + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
> return ret;
>
> buf = pkt->data;
> diff --git a/libavcodec/proresenc_kostya.c b/libavcodec/proresenc_kostya.c
> index 6720774e14..45385213ac 100644
> --- a/libavcodec/proresenc_kostya.c
> +++ b/libavcodec/proresenc_kostya.c
> @@ -981,7 +981,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> ctx->pic = pic;
> pkt_size = ctx->frame_size_upper_bound;
>
> - if ((ret = ff_alloc_packet(avctx, pkt, pkt_size + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
> + if ((ret = ff_alloc_packet(avctx, pkt, pkt_size + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
> return ret;
>
> orig_buf = pkt->data;
> diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
> index 6d79a90184..d81ff6f2aa 100644
> --- a/libavcodec/snowenc.c
> +++ b/libavcodec/snowenc.c
> @@ -1761,7 +1761,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> uint8_t rc_header_bak[sizeof(s->header_state)];
> uint8_t rc_block_bak[sizeof(s->block_state)];
>
> - if ((ret = ff_alloc_packet(avctx, pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
> + if ((ret = ff_alloc_packet(avctx, pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
> return ret;
>
> ff_init_range_encoder(c, pkt->data, pkt->size);
> diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c
> index c4861010f1..5675ae5218 100644
> --- a/libavcodec/svq1enc.c
> +++ b/libavcodec/svq1enc.c
> @@ -655,7 +655,7 @@ static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> int i, ret;
>
> ret = ff_alloc_packet(avctx, pkt, s->y_block_width * s->y_block_height *
> - MAX_MB_BYTES * 3 + AV_INPUT_BUFFER_MIN_SIZE);
> + MAX_MB_BYTES * 3 + FF_INPUT_BUFFER_MIN_SIZE);
> if (ret < 0)
> return ret;
>
> diff --git a/libavcodec/tiffenc.c b/libavcodec/tiffenc.c
> index 61d7316c4b..dfe308ee17 100644
> --- a/libavcodec/tiffenc.c
> +++ b/libavcodec/tiffenc.c
> @@ -334,7 +334,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
> bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
> s->subsampling[0] * s->subsampling[1] + 7) >> 3;
> packet_size = avctx->height * bytes_per_row * 2 +
> - avctx->height * 4 + AV_INPUT_BUFFER_MIN_SIZE;
> + avctx->height * 4 + FF_INPUT_BUFFER_MIN_SIZE;
>
> if ((ret = ff_alloc_packet(avctx, pkt, packet_size)) < 0)
> return ret;
> diff --git a/libavcodec/version_major.h b/libavcodec/version_major.h
> index 45209c0a4f..161442df95 100644
> --- a/libavcodec/version_major.h
> +++ b/libavcodec/version_major.h
> @@ -53,6 +53,7 @@
> #define FF_API_AVFFT (LIBAVCODEC_VERSION_MAJOR < 62)
> #define FF_API_FF_PROFILE_LEVEL (LIBAVCODEC_VERSION_MAJOR < 62)
> #define FF_API_AVCODEC_CLOSE (LIBAVCODEC_VERSION_MAJOR < 62)
> +#define FF_API_BUFFER_MIN_SIZE (LIBAVCODEC_VERSION_MAJOR < 62)
>
> // reminder to remove CrystalHD decoders on next major bump
> #define FF_CODEC_CRYSTAL_HD (LIBAVCODEC_VERSION_MAJOR < 61)
Will apply this patch tomorrow unless there are objections.
- Andreas
More information about the ffmpeg-devel
mailing list