From patchwork Wed Mar 8 20:03:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Izen X-Patchwork-Id: 40610 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:d046:b0:cd:afd7:272c with SMTP id hv6csp710214pzb; Wed, 8 Mar 2023 12:03:42 -0800 (PST) X-Google-Smtp-Source: AK7set/JBqO7opsPY+8yAKvkqNpzuobPPvGqy59WWiNw24m8ESap2o1DZlgKT4TII/QUio+6P4Sv X-Received: by 2002:a05:6402:128b:b0:4bf:4b5c:1d66 with SMTP id w11-20020a056402128b00b004bf4b5c1d66mr16070830edv.31.1678305822273; Wed, 08 Mar 2023 12:03:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678305822; cv=none; d=google.com; s=arc-20160816; b=YD3Shbk2YwQn/pCaNyBAhgnQUCgxrlftnTl45ZnYomf57k2qRilSZ9qvwabAvfzfff HnIf/yXcFKZDycAbZDPW5eYHMA7+COpjtY2nFoS2Qk2v+GnYDCCansdBpfOzJ6MFtir0 /4Eao0PheKz6gRDIF9xys62wveWbarZxTbUZMtUxrdSEwR7bhANN+ZiQHNH79cx2osOm jdMYrW9x7OWkzSdf7EzOP89iH2kZCgvde6XBRskJfgjkT5EJSM29368uHT0NDRBKUBP8 H2b+/4zQ9nTlMr+/wUHrK+oI1Qc+SmHAVHDoMA/JgAsi1PcPVKOXSz3sKPlDOzt2ps2G WYYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=jAnLBbE8jmr3I4Xkv4ZuMauAIBaRWyeQ3zg0zKKI6GA=; b=cnfcMDcB2josDtTEGfzxxuszlSiz4aec/l35AGzVNv3AYonpvxhpWvu37Dj6ifzIh3 FhLVsaa6AE4ky9Oa0y2EIpzvI2XjkhPJByt501ALRrKx7X6EqQy3Gzz4q/Q/JX2RsTOP T95kFiHsQekXly2It/EFBwrfLoRXcGqENFlSec53hywBQ170jfyJbTXSLsPpAyGP7hVP xoVn89jVhUhg/Yj/z/I8DxLF4pnwEwC3c/VuyP3nVBq+vY4x/Od0Lf8YcdJmru5VffdV wACYZTO4a0n/Sj9AZ0c9UNvgihilZhehKb6xizVQNAalaOatn3j/lM+3hhSug/wnOF3e ruRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=YmnjKKJR; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id l13-20020aa7c30d000000b004bcedde1496si5836463edq.287.2023.03.08.12.03.41; Wed, 08 Mar 2023 12:03:42 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=YmnjKKJR; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1EA4868BD45; Wed, 8 Mar 2023 22:03:28 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io1-f53.google.com (mail-io1-f53.google.com [209.85.166.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C0A2C689213 for ; Wed, 8 Mar 2023 22:03:20 +0200 (EET) Received: by mail-io1-f53.google.com with SMTP id 76so7251624iou.9 for ; Wed, 08 Mar 2023 12:03:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678305799; 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=1R8hvNo5pXOZhMrT2jTKjKedsCgsveF4mlm7xbvOzW8=; b=YmnjKKJRMJUB9qdkEeottdEbeWMoAXuoEr12zn3AEhWcS8sIZxB1iSYVkc83YwpKXA SQUvzvFS6SWNd95+TWwyLA8n93DuG5wjniClzkE3NyyWBk1QIjriM7A/LNWHsXoBR27h wwsqmnj4WwcgDQB2UqFRZoTZzAHF1sSJZwwR67JwXsXO6iAadwT7UUAyz06BXnVQub/K tzUYeMzyMG/Z1iIn2UoP7bVnxbLIa1BZYrMlQalcf9KaiMiJnKmF60a4uqyL6anCM7+6 2tFeQjd3xeoHyZmNeYRuGoj6bBq/NbgXG23ZOyeBThUq4XVdFos7Uq7XTbxCKDw7Y3sU eZbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678305799; 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=1R8hvNo5pXOZhMrT2jTKjKedsCgsveF4mlm7xbvOzW8=; b=fxxmAtJk1oHed0KeuqoEV8X0PEUI9geMv4/cjQ3NiroovkyTLIfrihgHbruXrqcEMy ZOTf0Q8sa4DRQS1uUwohmQNeiKA61tPzQ99yzH1WZWISIpat/YTQuQRCIfbpkoK4obFT e3TI8F+uiXTQSlgbucGDva4TV1SDC1sXpH7zg0LbCHTYrfhV4R3cY4lCpa+X1mG2DoR5 5b17mXO1b8e0J5RitEVtthf+HPdD6J/oo0+F+m/I23/yeq8MyR8PK+gx5rG9xOv8Kxer jIZnHrzSOgkU06eQRragzS4r9l+PZtxfcXQPIP0zQ6TYtzqjjF8qHdcHV/7Rae0IebpN FgZA== X-Gm-Message-State: AO0yUKU4spb6j2H9U5aPyW5YXKano0ENJnMbBJc2hQAu7A2EqjPxIZvs RiQK4l9gw93MGI/mMw5fy2s9tRVnQVUzSw== X-Received: by 2002:a05:6602:2208:b0:719:6a2:99d8 with SMTP id n8-20020a056602220800b0071906a299d8mr409977ion.0.1678305799323; Wed, 08 Mar 2023 12:03:19 -0800 (PST) Received: from gauss.local (c-98-224-219-15.hsd1.mi.comcast.net. [98.224.219.15]) by smtp.gmail.com with ESMTPSA id h2-20020a02cd22000000b003c4f96913c1sm5374817jaq.105.2023.03.08.12.03.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Mar 2023 12:03:19 -0800 (PST) From: Leo Izen To: ffmpeg-devel@ffmpeg.org Date: Wed, 8 Mar 2023 15:03:15 -0500 Message-Id: <20230308200316.45476-2-leo.izen@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230308200316.45476-1-leo.izen@gmail.com> References: <20230308200316.45476-1-leo.izen@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 1/2] avcodec/pngdec: support sBIT 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: R68nyFMSIApq Add support for reading sBIT chunks, which mark the significant bit depth of the PNG file. This passes the metadata using the field bits_per_raw_sample of AVCodecContext. Signed-off-by: Leo Izen --- libavcodec/pngdec.c | 45 ++++++++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c index 9403d72f2f..3c91896c9b 100644 --- a/libavcodec/pngdec.c +++ b/libavcodec/pngdec.c @@ -98,6 +98,7 @@ typedef struct PNGDecContext { int bpp; int has_trns; uint8_t transparent_color_be[6]; + int significant_bits; uint32_t palette[256]; uint8_t *crow_buf; @@ -716,6 +717,14 @@ static int populate_avctx_color_fields(AVCodecContext *avctx, AVFrame *frame) avctx->colorspace = frame->colorspace = AVCOL_SPC_RGB; avctx->color_range = frame->color_range = AVCOL_RANGE_JPEG; + /* + * tRNS sets alpha depth to full, so we ignore sBIT if set. + * As a result we must wait until now to set + * avctx->bits_per_raw_sample in case tRNS appears after sBIT + */ + if (!s->has_trns && s->significant_bits > 0) + avctx->bits_per_raw_sample = s->significant_bits; + return 0; } @@ -996,6 +1005,38 @@ fail: return ret; } +static int decode_sbit_chunk(AVCodecContext *avctx, PNGDecContext *s, + GetByteContext *gb) +{ + int bits = 0; + int channels; + + if (!(s->hdr_state & PNG_IHDR)) { + av_log(avctx, AV_LOG_ERROR, "sBIT before IHDR\n"); + return AVERROR_INVALIDDATA; + } + + if (s->pic_state & PNG_IDAT) { + av_log(avctx, AV_LOG_ERROR, "sBIT after IDAT\n"); + return AVERROR_INVALIDDATA; + } + + channels = ff_png_get_nb_channels(s->color_type); + + for (int i = 0; i < channels; i++) { + int b = bytestream2_get_byteu(gb); + bits = FFMAX(b, bits); + } + + if (bits < 0 || bits > s->bit_depth) { + av_log(avctx, AV_LOG_ERROR, "Invalid significant bits: %d\n", bits); + return AVERROR_INVALIDDATA; + } + s->significant_bits = bits; + + return 0; +} + static void handle_small_bpp(PNGDecContext *s, AVFrame *p) { if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) { @@ -1440,6 +1481,10 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, break; } + case MKTAG('s', 'B', 'I', 'T'): + if ((ret = decode_sbit_chunk(avctx, s, &gb_chunk)) < 0) + goto fail; + break; case MKTAG('g', 'A', 'M', 'A'): { AVBPrint bp; char *gamma_str; diff --git a/libavcodec/version.h b/libavcodec/version.h index da54f87887..39dbec0208 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -30,7 +30,7 @@ #include "version_major.h" #define LIBAVCODEC_VERSION_MINOR 6 -#define LIBAVCODEC_VERSION_MICRO 100 +#define LIBAVCODEC_VERSION_MICRO 101 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \