diff mbox series

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

Message ID 20230201054129.124226-3-leo.izen@gmail.com
State New
Headers show
Series PNG sBIT chunk support | 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

Leo Izen Feb. 1, 2023, 5:41 a.m. UTC
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@gmail.com>
---
 libavcodec/pngenc.c | 7 +++++++
 1 file changed, 7 insertions(+)
diff mbox series

Patch

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;