From: Leo Izen via ffmpeg-devel <ffmpeg-devel@ffmpeg.org> To: ffmpeg-devel@ffmpeg.org Cc: Leo Izen <code@ffmpeg.org> Subject: [FFmpeg-devel] [PATCH] avcodec/pngenc: include EXIF buffer in max_packet_size (PR #20539) Date: Wed, 17 Sep 2025 12:50:04 -0000 Message-ID: <175811340502.25.10695898061121140139@463a07221176> (raw) 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
reply other threads:[~2025-09-17 12:50 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=175811340502.25.10695898061121140139@463a07221176 \ --to=ffmpeg-devel@ffmpeg.org \ --cc=code@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
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