diff mbox series

[FFmpeg-devel,2/4] avcodec/mpegvideo_enc: Fix unnecessary linear growth of buffer

Message ID DU2PR01MB79501D3129D82352772971F68FEA9@DU2PR01MB7950.eurprd01.prod.exchangelabs.com
State Accepted
Commit c3047da646bee6eeaac505fa11cfc97eaffa2286
Headers show
Series [FFmpeg-devel,1/4] avcodec/mpegvideo_enc: Ignore ICC profile size when not MJPEG | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt April 11, 2022, 11:50 p.m. UTC
If one encodes MJPEG with a single slice and uses input with
AV_FRAME_DATA_ICC_PROFILE side data, the current allocation code
in ff_mpv_encode_picture() will always increase the size of the
temporary buffer used for allocating packets by the size needed
for to write the ICC chunk even when the current buffer is actually
large enough. This commit fixes this.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegvideo_enc.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 4a5e5a5059..434bbb3a68 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -1684,9 +1684,8 @@  int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
     /* output? */
     if (s->new_picture->data[0]) {
         int growing_buffer = context_count == 1 && !pkt->data && !s->data_partitioning;
-        size_t pkt_size = growing_buffer ? FFMAX(s->mb_width*s->mb_height*64+10000, avctx->internal->byte_buffer_size) - AV_INPUT_BUFFER_PADDING_SIZE
-                                              :
-                                              s->mb_width*s->mb_height*(MAX_MB_BYTES+100)+10000;
+        size_t pkt_size = 10000 + s->mb_width * s->mb_height *
+                                  (growing_buffer ? 64 : (MAX_MB_BYTES + 100));
         if (CONFIG_MJPEG_ENCODER && avctx->codec_id == AV_CODEC_ID_MJPEG) {
             ret = ff_mjpeg_add_icc_profile_size(avctx, s->new_picture, &pkt_size);
             if (ret < 0)
@@ -1694,6 +1693,7 @@  int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
         }
         if ((ret = ff_alloc_packet(avctx, pkt, pkt_size)) < 0)
             return ret;
+        pkt->size = avctx->internal->byte_buffer_size - AV_INPUT_BUFFER_PADDING_SIZE;
         if (s->mb_info) {
             s->mb_info_ptr = av_packet_new_side_data(pkt,
                                  AV_PKT_DATA_H263_MB_INFO,