[FFmpeg-devel] [PATCH 2/2] avcodec/pngenc: write sBIT chunks

Leo Izen leo.izen at gmail.com
Wed Feb 1 07:41:29 EET 2023


Add support for writing sBIT chunks, which mark the significant
bit depth of the PNG file. This obtains the metadata using the field
bits_per_raw_sample of AVCodecContext.

Signed-off-by: Leo Izen <leo.izen at gmail.com>
---
 libavcodec/pngenc.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index 2393161c3b..0961125f0a 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -432,6 +432,13 @@ static int encode_headers(AVCodecContext *avctx, const AVFrame *pict)
     if (png_get_gama(pict->color_trc, s->buf))
         png_write_chunk(&s->bytestream, MKTAG('g', 'A', 'M', 'A'), s->buf, 4);
 
+    if (avctx->bits_per_raw_sample > 0 && avctx->bits_per_raw_sample < s->bit_depth) {
+        size_t len = s->color_type == 0 ? 1 : s->color_type == 2 || s->color_type == 3 ? 3 :
+                     s->color_type == 4 ? 2 : 4;
+        memset(s->buf, avctx->bits_per_raw_sample, len);
+        png_write_chunk(&s->bytestream, MKTAG('s', 'B', 'I', 'T'), s->buf, len);
+    }
+
     side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_ICC_PROFILE);
     if ((ret = png_write_iccp(s, side_data)))
         return ret;
-- 
2.39.1



More information about the ffmpeg-devel mailing list