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 ESMTPS id 90FCE4DFF2 for ; Sun, 27 Apr 2025 11:47:22 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 88844687B1C; Sun, 27 Apr 2025 14:46:29 +0300 (EEST) Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CA6F868A953 for ; Sun, 27 Apr 2025 14:45:30 +0300 (EEST) Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-736c3e7b390so3938111b3a.2 for ; Sun, 27 Apr 2025 04:45:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1745754328; x=1746359128; darn=ffmpeg.org; 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=lKjwAFV+vDVMu179fDPsj8f3//d2YHP4s/QUPNBCdok=; b=Y4YjPxBOXdh5kLzrjP8zm/Nsb6A0xBcAplA+WOk4/pk+AW0NR9BjkdEV13NvAq4S/e 6FdpZVKh7TpOvgSWm7brm+Xam3vfFJeFS5Bwv7l97Qn0Gb3d647dXbvgstugzCfKZ9n6 xBtywK20ycglx7Kn0LUMD9sMNelmuDSVbG4oc29fX7bthnJEyTWlD3yu1taIkZK1mJxd PDhr/UfnNGmtZYFiqC0f1CfugY7e2tyw8uFClNxZ91pmZd7hDOEbgGqyq8/uwo7fK42G 4GjP396NmBLvcigF/vzBLbLljvTQTLoXk6jtDmr6mDMwFmT2zNndTEvIXTgMiZL1lmIF ygVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745754328; x=1746359128; 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=lKjwAFV+vDVMu179fDPsj8f3//d2YHP4s/QUPNBCdok=; b=Z/zgP+ZDga+pHAx8PM4hzwcF1hK4PPrKSv5tIVt6UExcXK5V7FiQlcq/G3WtvB3Vmx 2/xeaVGmZiFa5mcvIJwDIEkKK+YTuBX3JmiBH6F/fOGtBuwu2Z2I94p9YYpquHrhsvlI LIGHQMerlfTwqGDeUBPNho9uYOc2+aGDdIQ6+nqwGn+PVqEWQSVVg3mnFme0wKdeVjvu iGrqAMwgDRzKccQlm3KsaF7p2fL8+Z7RjzrRmZaag6nWkOJ2yH3/x1HadgF7F7+aFc48 jMjMeoa/7WthAKsdvA94+6aIZ9K9/ekZH3S/FfcAD5UaCIOqHWavqqO85Tk7hF7X7qbY vNrw== X-Gm-Message-State: AOJu0YygutjehqY7826atXzUPQXU9yvYHjy4RVcWmbzmFPLcpNJ4lE1/ ex6QhqqGYYMdTHNhr03TiOKZwjcHNZHih1KxZZVL52nuzTt/+3K4LEtSJPOJ X-Gm-Gg: ASbGnct8p+hj8ahzVwP+pMGnCPHrdh8jlNhbAkuYlDF2oiMbG8m9dzyv/WXDTIR7Os3 +NFoCiVDJj8iT5cjFLOIA5sioVQ64Q4olD5c92jYbXC08XIsas+hd+PefHLiLQiLk345PNbktSb 7a5kcuwhw4LQpH0nfOZyqV14jipKhKEHNbc9IvfHc1jBvxQjOOstijfW3ftJpD4DaBoRnLxbLsf EX1xRH/CE8zs1yvEbKstMtUdsAgfFaOKCz7SUPO8y4jsYEEeFMDbnC19gmFHqv5g1FGwIiq4/Ce bIhr0aiKD1YpMBjap2ow32E5w7Fdv4JR2tukGZwA9CpVK0lpIRevUu3ZdVETfvV5iTG2zpGV X-Google-Smtp-Source: AGHT+IGrjdMn8GF0SM9IHSvvvJXGk+OeVDxPGTpwnMbO3ZAyBMFyrmYhVN25E91Jxutl7Kd1CgeUbA== X-Received: by 2002:a05:6a20:12cf:b0:1fd:f48b:f397 with SMTP id adf61e73a8af0-2045b71e722mr10899498637.23.1745754328355; Sun, 27 Apr 2025 04:45:28 -0700 (PDT) Received: from localhost.localdomain ([124.79.129.75]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e259134ebsm6064452b3a.27.2025.04.27.04.45.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Apr 2025 04:45:28 -0700 (PDT) From: toqsxw@gmail.com X-Google-Original-From: toqsxw@outlook.com To: ffmpeg-devel@ffmpeg.org Date: Sun, 27 Apr 2025 19:44:40 +0800 Message-ID: <20250427114451.1236-10-toqsxw@outlook.com> X-Mailer: git-send-email 2.44.0.windows.1 In-Reply-To: <20250427114451.1236-1-toqsxw@outlook.com> References: <20250427114451.1236-1-toqsxw@outlook.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 10/20] avcodec/h274: add ff_h274_hash functions 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: nuomi2021@gmail.com, Wu Jianhua 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: From: Wu Jianhua Signed-off-by: Wu Jianhua --- libavcodec/h274.c | 155 ++++++++++++++++++++++++++++++++++++++++++++++ libavcodec/h274.h | 7 +++ 2 files changed, 162 insertions(+) diff --git a/libavcodec/h274.c b/libavcodec/h274.c index 5709200322..e46926e4cc 100644 --- a/libavcodec/h274.c +++ b/libavcodec/h274.c @@ -26,7 +26,11 @@ */ #include "libavutil/avassert.h" +#include "libavutil/bswap.h" +#include "libavutil/crc.h" #include "libavutil/imgutils.h" +#include "libavutil/md5.h" +#include "libavutil/mem.h" #include "h274.h" @@ -790,3 +794,154 @@ static const int8_t R64T[64][64] = { 17, -16, 15, -14, 13, -12, 11, -10, 9, -8, 7, -6, 4, -3, 2, -1, } }; + +static int verify_plane_md5(struct AVMD5 *ctx, + const uint8_t *src, const int w, const int h, const int stride, + const uint8_t *expected) +{ +#define MD5_SIZE 16 + uint8_t md5[MD5_SIZE]; + av_md5_init(ctx); + for (int j = 0; j < h; j++) { + av_md5_update(ctx, src, w); + src += stride; + } + av_md5_final(ctx, md5); + + if (memcmp(md5, expected, MD5_SIZE)) + return AVERROR_INVALIDDATA; + + return 0; +} + +static int verify_plane_crc(const uint8_t *src, const int w, const int h, const int stride, + uint16_t expected) +{ + uint32_t crc = 0x0F1D; // CRC-16-CCITT-AUG + const AVCRC *ctx = av_crc_get_table(AV_CRC_16_CCITT); + + expected = av_le2ne32(expected); + for (int j = 0; j < h; j++) { + crc = av_crc(ctx, crc, src, w); + src += stride; + } + crc = av_bswap16(crc); + + if (crc != expected) + return AVERROR_INVALIDDATA; + + return 0; +} + +#define CAL_CHECKSUM(pixel) ((pixel) ^ xor_mask) +static int verify_plane_checksum(const uint8_t *src, const int w, const int h, const int stride, const int ps, + uint32_t expected) +{ + uint32_t checksum = 0; + expected = av_le2ne32(expected); + + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + const int xor_mask = (x & 0xFF) ^ (y & 0xFF) ^ (x >> 8) ^ (y >> 8); + checksum += CAL_CHECKSUM(src[x << ps]); + if (ps) + checksum += CAL_CHECKSUM(src[(x << ps) + 1]); + } + src += stride; + } + + if (checksum != expected) + return AVERROR_INVALIDDATA; + + return 0; +} + +enum { + HASH_MD5SUM, + HASH_CRC, + HASH_CHECKSUM, + HASH_LAST = HASH_CHECKSUM, +}; + +struct H274HashContext { + int type; + struct AVMD5 *ctx; +}; + +void ff_h274_hash_freep(H274HashContext **ctx) +{ + if (*ctx) { + H274HashContext *c = *ctx; + if (c->ctx) + av_free(c->ctx); + av_freep(ctx); + } +} + +int ff_h274_hash_init(H274HashContext **ctx, const int type) +{ + H274HashContext *c; + + if (type > HASH_LAST || !ctx) + return AVERROR(EINVAL); + + c = *ctx; + if (c) { + if (c->type != type) { + if (c->type == HASH_MD5SUM) + av_freep(&c->ctx); + c->type = type; + } + } else { + c = av_mallocz(sizeof(H274HashContext)); + if (!c) + return AVERROR(ENOMEM); + c->type = type; + *ctx = c; + } + + if (type == HASH_MD5SUM && !c->ctx) { + c->ctx = av_md5_alloc(); + if (!c->ctx) + return AVERROR(ENOMEM); + } + + return 0; +} + +int ff_h274_hash_verify(H274HashContext *c, const H274SEIPictureHash *hash, + const AVFrame *frame, const int coded_width, const int coded_height) +{ + const AVPixFmtDescriptor *desc; + int err = 0; + + if (!c || !hash || !frame) + return AVERROR(EINVAL); + + if (c->type != hash->hash_type) + return AVERROR(EINVAL); + + desc = av_pix_fmt_desc_get(frame->format); + if (!desc) + return AVERROR(EINVAL); + + for (int i = 0; i < desc->nb_components; i++) { + const int w = i ? (coded_width >> desc->log2_chroma_w) : coded_width; + const int h = i ? (coded_height >> desc->log2_chroma_h) : coded_height; + const int ps = desc->comp[i].step - 1; + const uint8_t *src = frame->data[i]; + const int stride = frame->linesize[i]; + + if (c->type == HASH_MD5SUM) + err = verify_plane_md5(c->ctx, src, w << ps, h, stride, hash->md5[i]); + else if (c->type == HASH_CRC) + err = verify_plane_crc(src, w << ps, h, stride, hash->crc[i]); + else if (c->type == HASH_CHECKSUM) + err = verify_plane_checksum(src, w, h, stride, ps, hash->checksum[i]); + if (err < 0) + goto fail; + } + +fail: + return err; +} diff --git a/libavcodec/h274.h b/libavcodec/h274.h index e1803edaf3..0ca5f0cbab 100644 --- a/libavcodec/h274.h +++ b/libavcodec/h274.h @@ -64,6 +64,8 @@ int ff_h274_apply_film_grain(AVFrame *out, const AVFrame *in, H274FilmGrainDatabase *db, const AVFilmGrainParams *params); +typedef struct H274HashContext H274HashContext; + typedef struct H274SEIPictureHash { int present; union { @@ -74,4 +76,9 @@ typedef struct H274SEIPictureHash { uint8_t hash_type; } H274SEIPictureHash; +int ff_h274_hash_init(H274HashContext **c, int type); +int ff_h274_hash_verify(H274HashContext *c, const H274SEIPictureHash *hash, + const AVFrame *frame, int coded_width, int coded_height); +void ff_h274_hash_freep(H274HashContext **c); + #endif /* AVCODEC_H274_H */ -- 2.44.0.windows.1 _______________________________________________ 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".