[FFmpeg-devel] [PATCH 1/8] png: Clear up the calculation of max packet size

Donny Yang work at kota.moe
Sun Mar 29 13:05:40 CEST 2015


Signed-off-by: Donny Yang <work at kota.moe>
---
 libavcodec/pngenc.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index d6233d0..3697dbb 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -360,12 +360,23 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
         return -1;
 
     enc_row_size    = deflateBound(&s->zstream, row_size);
-    max_packet_size = avctx->height * (int64_t)(enc_row_size +
-                                       ((enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) * 12)
-                      + FF_MIN_BUFFER_SIZE;
+    max_packet_size =
+        8 +             // PNGSIG
+        13 + 12 +       // IHDR
+        9 + 12 +        // pHYs
+        1 + 12 +        // sRGB
+        32 + 12 +       // cHRM
+        4 + 12 +        // gAMA
+        256 * 3 + 12 +  // PLTE
+        256 + 12 +      // tRNS
+        avctx->height * (
+            enc_row_size +
+            12 * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // 12 * ceil(enc_row_size / IOBUF_SIZE)
+        );
     if (max_packet_size > INT_MAX)
         return AVERROR(ENOMEM);
-    if ((ret = ff_alloc_packet2(avctx, pkt, max_packet_size)) < 0)
+    ret = ff_alloc_packet2(avctx, pkt, max_packet_size < FF_MIN_BUFFER_SIZE ? FF_MIN_BUFFER_SIZE : max_packet_size);
+    if (ret)
         return ret;
 
     s->bytestream_start =
-- 
2.3.4


More information about the ffmpeg-devel mailing list