From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id CE46A452FB for ; Tue, 17 Jan 2023 18:51:30 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E129568BEAD; Tue, 17 Jan 2023 20:51:06 +0200 (EET) Received: from mail-il1-f174.google.com (mail-il1-f174.google.com [209.85.166.174]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 84BC168BE1B for ; Tue, 17 Jan 2023 20:50:58 +0200 (EET) Received: by mail-il1-f174.google.com with SMTP id f8so9761302ilj.5 for ; Tue, 17 Jan 2023 10:50:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pqi1pa1Gn3kUB2PIfL1GzAITfGu26FI2aQZFRyG7pro=; b=NySl4/5Zx4oVTJHW+P/AHmY5A7X+xvlz3fuc2jSDyWmN9swS7PLJ9Q3opfh++Jsx0H ifN66l974vDGFQZlpSl75vPNUntC29GDen3WbfNGfofw0pvM7wANaqk66CUrQ9yYjvEG fvbqbvsPhbp5cUNKjCQC8EPiRPhYBKfyhvBqfu1WE+xZ0JlOohRovDO2L1qTfBsimcaN ZM94az4m/12nyhaEwoOpeqwRAl3kbkjFOG/nlQrvXrOe02FKccmRi2QbzQNtvNszGgF/ fLDo6iAdsVbc8WmFxfO6Y40F1j7rpuzPujIjCepD1K7MG3myd/qbsuBzX9lwtgn7Sak9 jl3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pqi1pa1Gn3kUB2PIfL1GzAITfGu26FI2aQZFRyG7pro=; b=i6CW151pgZYaTJpFzAk9wAOSJiKmTuVNGGP42LrqDhH35caer3jv5jMm4ffW8dsgxe VlsllVVlD1dhDFlL1/XFA52NG4k6MtGHF6QXyLFKAxrIDMAmblHvE4fd+54sAktEZ2QW LnBFEEqmYVnD2056v93OTJIcrFFpbOvpwOwbNZEFTSd95GsPD/l/JOGAEsVc+9hpR5HY /HWGc3HXlgbwXIaUFJSkGGJXD6Oi61jqPsj1sLfW2o+QxA8qXpVyn23PA3ymKPcI7Kso KSIyndeCax1ImmDGMmXDcnNhT8+z1wfIVJeHzgZHl1oE0s6GaEszMBWa0tTQbM3bv78O h1dg== X-Gm-Message-State: AFqh2kqPHnQDpY1lVJjy29xr+06DDKRF0lZRRVjE96xsjqgXlPiz3BPw 1gRyjEhUjfJpSPjIFPnV8O3lkhuvVPU= X-Google-Smtp-Source: AMrXdXuq7kjuJS4tcwH0HszqhdPxEAnCttYo+EChFGgnOs0YPbfJ5PxpFG5JI2BE8zUOLU69lleIDw== X-Received: by 2002:a92:d481:0:b0:30b:e6c7:b223 with SMTP id p1-20020a92d481000000b0030be6c7b223mr3324630ilg.32.1673981457227; Tue, 17 Jan 2023 10:50:57 -0800 (PST) Received: from gauss.local (c-68-41-54-207.hsd1.mi.comcast.net. [68.41.54.207]) by smtp.gmail.com with ESMTPSA id y93-20020a029566000000b003a4894d46e0sm2262223jah.176.2023.01.17.10.50.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jan 2023 10:50:56 -0800 (PST) From: Leo Izen To: ffmpeg-devel@ffmpeg.org Date: Tue, 17 Jan 2023 13:50:51 -0500 Message-Id: <20230117185051.475157-4-leo.izen@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230117185051.475157-1-leo.izen@gmail.com> References: <20230117185051.475157-1-leo.izen@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 3/3] avcodec/png: support cICP chunks X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Leo Izen Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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".