Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Leo Izen <leo.izen@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Leo Izen <leo.izen@gmail.com>
Subject: [FFmpeg-devel] [PATCH v2 3/3] avcodec/png: support cICP chunks
Date: Tue, 17 Jan 2023 13:50:51 -0500
Message-ID: <20230117185051.475157-4-leo.izen@gmail.com> (raw)
In-Reply-To: <20230117185051.475157-1-leo.izen@gmail.com>

This commit adds both decode and encode support for cICP chunks, which
allow a PNG image's pixel data to be tagged by any of the enum values in
H.273, without an ICC profile.

Upon decode, if a cICP chunk is present, the PNG decoder will tag output
AVFrames with the resulting enum color, and ignore iCCP, sRGB, gAMA, and
cHRM chunks, as per the spec.

Upon encode, if the color space is known and specified, and it is not sRGB,
the PNG encoder will output a cICP chunk containing the color space. If the
color space is sRGB, then it will output an sRGB chunk instead of a cICP
chunk. If the color space of the input is not unspecified, it will not output
a cICP chunk tagging the PNG as unspecified.

In either the sRGB case or the non-SRGB case, gAMA and cHRM are still written
as fallbacks provided the info is known.
---
 libavcodec/pngdec.c | 35 ++++++++++++++++++++++++++++++++---
 libavcodec/pngenc.c |  8 ++++++++
 2 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c
index 1f9ed894de..0d969decf2 100644
--- a/libavcodec/pngdec.c
+++ b/libavcodec/pngdec.c
@@ -76,6 +76,10 @@ typedef struct PNGDecContext {
     uint32_t white_point[2];
     uint32_t display_primaries[3][2];
     int have_srgb;
+    int have_cicp;
+    enum AVColorPrimaries cicp_primaries;
+    enum AVColorTransferCharacteristic cicp_trc;
+    enum AVColorRange cicp_range;
 
     enum PNGHeaderState hdr_state;
     enum PNGImageState pic_state;
@@ -1314,6 +1318,19 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
             }
             break;
         }
+        case MKTAG('c', 'I', 'C', 'P'):
+            s->cicp_primaries = bytestream2_get_byte(&gb_chunk);
+            s->cicp_trc = bytestream2_get_byte(&gb_chunk);
+            if (bytestream2_get_byte(&gb_chunk) != 0)
+                av_log(avctx, AV_LOG_WARNING, "nonzero cICP matrix\n");
+            s->cicp_range = bytestream2_get_byte(&gb_chunk);
+            if (s->cicp_range != 0 && s->cicp_range != 1) {
+                av_log(avctx, AV_LOG_ERROR, "invalid cICP range: %d\n", s->cicp_range);
+                ret = AVERROR_INVALIDDATA;
+                goto fail;
+            }
+            s->have_cicp = 1;
+            break;
         case MKTAG('s', 'R', 'G', 'B'):
             /* skip rendering intent byte */
             bytestream2_skip(&gb_chunk, 1);
@@ -1475,6 +1492,7 @@ static void clear_frame_metadata(PNGDecContext *s)
 
     s->have_chrm = 0;
     s->have_srgb = 0;
+    s->have_cicp = 0;
 
     av_dict_free(&s->frame_metadata);
 }
@@ -1484,7 +1502,18 @@ static int output_frame(PNGDecContext *s, AVFrame *f)
     AVCodecContext *avctx = s->avctx;
     int ret;
 
-    if (s->iccp_data) {
+    if (s->have_cicp) {
+        if (s->cicp_primaries >= AVCOL_PRI_NB)
+            av_log(avctx, AV_LOG_WARNING, "unrecognized cICP primaries\n");
+        else
+            avctx->color_primaries = f->color_primaries = s->cicp_primaries;
+        if (s->cicp_trc >= AVCOL_TRC_NB)
+            av_log(avctx, AV_LOG_WARNING, "unrecognized cICP transfer\n");
+        else
+            avctx->color_trc = f->color_trc = s->cicp_trc;
+        avctx->color_range = f->color_range =
+            s->cicp_range == 0 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
+    } else if (s->iccp_data) {
         AVFrameSideData *sd = av_frame_new_side_data(f, AV_FRAME_DATA_ICC_PROFILE, s->iccp_data_len);
         if (!sd) {
             ret = AVERROR(ENOMEM);
@@ -1514,8 +1543,8 @@ static int output_frame(PNGDecContext *s, AVFrame *f)
             av_log(avctx, AV_LOG_WARNING, "unknown cHRM primaries\n");
     }
 
-    /* these chunks overrides gAMA */
-    if (s->iccp_data || s->have_srgb)
+    /* these chunks override gAMA */
+    if (s->iccp_data || s->have_srgb || s->have_cicp)
         av_dict_set(&s->frame_metadata, "gamma", NULL, 0);
 
     avctx->colorspace = f->colorspace = AVCOL_SPC_RGB;
diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index 4333d5837f..0ce684342e 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -417,6 +417,14 @@ static int encode_headers(AVCodecContext *avctx, const AVFrame *pict)
         pict->color_trc == AVCOL_TRC_IEC61966_2_1) {
         s->buf[0] = 1; /* rendering intent, relative colorimetric by default */
         png_write_chunk(&s->bytestream, MKTAG('s', 'R', 'G', 'B'), s->buf, 1);
+    } else if (pict->color_primaries != AVCOL_PRI_UNSPECIFIED ||
+        pict->color_trc != AVCOL_TRC_UNSPECIFIED) {
+        /* these values match H.273 so no translation is needed */
+        s->buf[0] = pict->color_primaries;
+        s->buf[1] = pict->color_trc;
+        s->buf[2] = 0; /* colorspace = RGB */
+        s->buf[3] = pict->color_range == AVCOL_RANGE_MPEG ? 0 : 1;
+        png_write_chunk(&s->bytestream, MKTAG('c', 'I', 'C', 'P'), s->buf, 4);
     }
 
     if (png_get_chrm(pict->color_primaries, s->buf))
-- 
2.39.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2023-01-17 18:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-17 18:50 [FFmpeg-devel] [PATCH v2 0/3] Proper color support in PNG Leo Izen
2023-01-17 18:50 ` [FFmpeg-devel] [PATCH v2 1/3] avcodec/png: use libavutil/csp.h for cHRM chunks Leo Izen
2023-01-17 18:50 ` [FFmpeg-devel] [PATCH v2 2/3] avcodec/pngdec: support decoding sRGB chunks Leo Izen
2023-01-17 18:50 ` Leo Izen [this message]
2023-01-24 14:02 ` [FFmpeg-devel] [PATCH v2 0/3] Proper color support in PNG Leo Izen
2023-01-24 21:25   ` Leo Izen
2023-01-25 13:40     ` Leo Izen

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=20230117185051.475157-4-leo.izen@gmail.com \
    --to=leo.izen@gmail.com \
    --cc=ffmpeg-devel@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