Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avcodec/pngenc: include EXIF buffer in max_packet_size (PR #20539)
@ 2025-09-17 12:50 Leo Izen via ffmpeg-devel
  0 siblings, 0 replies; only message in thread
From: Leo Izen via ffmpeg-devel @ 2025-09-17 12:50 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Leo Izen

PR #20539 opened by Leo Izen (Traneptora)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20539
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20539.patch

When calculating the max size of an output PNG packet, we should
include the size of a possible eXIf chunk that we may write.

Signed-off-by: Leo Izen <leo.izen@gmail.com>


>From 7b7a90f4e93d36ffb6d5c939c61560daf91c2683 Mon Sep 17 00:00:00 2001
From: Leo Izen <leo.izen@gmail.com>
Date: Wed, 17 Sep 2025 08:48:02 -0400
Subject: [PATCH] avcodec/pngenc: include EXIF buffer in max_packet_size

When calculating the max size of an output PNG packet, we should
include the size of a possible eXIf chunk that we may write.

Signed-off-by: Leo Izen <leo.izen@gmail.com>
---
 libavcodec/pngenc.c | 64 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 58 insertions(+), 6 deletions(-)

diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index 5baad9aad5..1d6d2be8c4 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -644,24 +644,76 @@ static int add_icc_profile_size(AVCodecContext *avctx, const AVFrame *pict,
     return 0;
 }
 
-static int encode_png(AVCodecContext *avctx, AVPacket *pkt,
-                      const AVFrame *pict, int *got_packet)
+static int add_exif_profile_size(AVCodecContext *avctx, const AVFrame *pict,
+                                 uint64_t *max_packet_size)
+{
+    PNGEncContext *s = avctx->priv_data;
+    const AVFrameSideData *sd;
+    uint64_t new_pkt_size;
+    /* includes orientation tag */
+    const int base_exif_size = 92;
+    uint64_t estimated_exif_size;
+
+    if (!pict)
+        return 0;
+
+    sd = av_frame_get_side_data(pict, AV_FRAME_DATA_EXIF);
+    estimated_exif_size = sd ? sd->size : 0;
+    sd = av_frame_get_side_data(pict, AV_FRAME_DATA_DISPLAYMATRIX);
+    if (sd)
+        estimated_exif_size += base_exif_size;
+
+    if (!estimated_exif_size)
+        return 0;
+
+    /* 12 is the png chunk header size */
+    new_pkt_size = *max_packet_size + estimated_exif_size + 12;
+    if (new_pkt_size < *max_packet_size)
+        return AVERROR_INVALIDDATA;
+
+    *max_packet_size = new_pkt_size;
+
+    return 0;
+}
+
+static int get_max_packet_size(AVCodecContext *avctx, const AVFrame *pict,
+                               uint64_t *max_packet_size)
 {
     PNGEncContext *s = avctx->priv_data;
-    int ret;
     int enc_row_size;
     uint64_t max_packet_size;
+    int ret;
 
-    enc_row_size    = deflateBound(&s->zstream.zstream,
-                                   (avctx->width * s->bits_per_pixel + 7) >> 3);
+    enc_row_size = deflateBound(&s->zstream.zstream, (avctx->width * s->bits_per_pixel + 7) >> 3);
     max_packet_size =
         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)
         );
-    if ((ret = add_icc_profile_size(avctx, pict, &max_packet_size)))
+
+    ret = add_icc_profile_size(avctx, pict, &max_packet_size);
+    if (ret < 0)
         return ret;
+
+    ret = add_exif_profile_size(avctx, pict, &max_packet_size);
+    if (ret < 0)
+        return ret;
+
+    return 0;
+}
+
+static int encode_png(AVCodecContext *avctx, AVPacket *pkt,
+                      const AVFrame *pict, int *got_packet)
+{
+    PNGEncContext *s = avctx->priv_data;
+    int ret;
+    uint64_t max_packet_size;
+
+    ret = get_max_packet_size(avctx, pict, &max_packet_size);
+    if (ret < 0)
+        return ret;
+
     ret = ff_alloc_packet(avctx, pkt, max_packet_size);
     if (ret < 0)
         return ret;
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-09-17 12:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-17 12:50 [FFmpeg-devel] [PATCH] avcodec/pngenc: include EXIF buffer in max_packet_size (PR #20539) Leo Izen via ffmpeg-devel

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git