From patchwork Wed Aug 9 17:46:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: quietvoid X-Patchwork-Id: 43193 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b1b:b0:130:ccc6:6c4b with SMTP id l27csp126201pzh; Wed, 9 Aug 2023 10:47:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGkbvX0KEVpK5pQ+zvCnv1rEguXxJZZmzaG4vIzYrmDVM+6J2B4Z2Y7mvTi2Gt55vWTaR1x X-Received: by 2002:a17:906:cc0c:b0:99b:d1cb:5add with SMTP id ml12-20020a170906cc0c00b0099bd1cb5addmr2434907ejb.35.1691603246376; Wed, 09 Aug 2023 10:47:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691603246; cv=none; d=google.com; s=arc-20160816; b=oFZe+lqzX/YSgimpmte7r82CGl0xi32SwhSzT2U+bsaQBU9XgTUZ8Cq6o/2Sd/aMPI 3CnC0s9OfHSL1sC9J8emqsa1O3DOfp1Jb8t6T+CFL0dn+wOyuaGsf2gtTGJBGQbVHfiT BlGDigAgEiTtDtPTUmeu003FRtX2F/GenZ9zbClNB+g/++NDy1f3nVLXgd8M/xhUE0bf iFsRvfVQ+zKTfnK02/um3EHNpetCzwaGSciblPufTzPCeKfqDR5+hkBqS88eNEHwjTOD 0NSEHsHJ3/cgTzY6XhWmqOms/+c8JI6nwUILguks1P2CeTdkgzrhnImEfIG3O93nftFg mgUA== 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:message-id:date:to:from :dkim-signature:delivered-to; bh=iEDVJC9dgMuolHKpvXUBpZmXvO9IQfq9v/pN2+5ubVk=; fh=Qwk25RKKSExhNuycMMj7kTYC1eti1Y95kg0/fk1yEig=; b=eTWhMFRNkEexnCLh8ENKdCdorAlOQT4qFhY8FSYViS7NTMjg+BmwsLI+l06HgiGruG aw0B8DYbvtXhpiEnRSRInBZrz/X9DwnrENL5CTPP709/W+2PRaLsyGPTk3JcHXOVJyD+ SZVGiO5mEsZEPzqO23cfITHhzvkKAvUNVmFikE89283rVByijJOaM4XC1SyDKmjVpcfc t7nqOaKJ5SsVrT1El92iVBaLnMmfBLirQSPvcLayPBwqaGFWrpDEiIAwuRs1u35tC/YG K9WkRSTfVVHpFanDzG07EqDcgCJwotcEaKGDjoxCmOH00II4r2/6agwlEb/frfkm4Qh4 JtXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20221208 header.b=dSOAhPGZ; 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 i20-20020a17090671d400b00992ac6d88acsi8208411ejk.274.2023.08.09.10.47.24; Wed, 09 Aug 2023 10:47:26 -0700 (PDT) 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=20221208 header.b=dSOAhPGZ; 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 68F6368C813; Wed, 9 Aug 2023 20:47:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 58D5C68C56A for ; Wed, 9 Aug 2023 20:47:15 +0300 (EEST) Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-767b6d6bb87so8506585a.2 for ; Wed, 09 Aug 2023 10:47:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691603233; x=1692208033; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=0x/KSct4zts087c3mtFKO071YTFo5j0JKXp3E8IKSTY=; b=dSOAhPGZfZG2xOGLOu1W1lBHzx+/m3k2oEGpVjkjJ6ULa0JrBc+dyAjIVcE0PdyFsx U7DKKvvOa6pyAyFTZy/Sij9tm7vgd5UjQsSymjgEhFkw0YfhtrWabxFt2S/mnAOqYEfJ R3EhwUkMMaifmOMMCMY/he9jRzipMI2FiLv4PsMBchfGx9vzcoFACA3Ruc12co/A/s0u Qig+IaCRcq685Cb8Dmgaop4RyGmFp4aqliU8P4rxFoaMCwh5c4jnmudmaDVF/XszYbUZ FA+6cxurASgQItSCxFRnWy9ArUHyPmJoTXB/bIuwQxd2ZT5QlahYhYtkwJSc+CBqZX2h JROQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691603233; x=1692208033; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0x/KSct4zts087c3mtFKO071YTFo5j0JKXp3E8IKSTY=; b=RRhG0PlrCiWaOU7OikvGnaFYHYGK60Y3+iAp0cr/NoZpH/Zuz5dXvy1JQBjtHWtsUJ 5orDxkNyowPfgzkD5+UX+DVgYmWxWb6EZhYhO4nj81ou2uIQKDKJ6AFRwY0yCZrm/QOn UTlUy4e3T8+9WXCqxckMwzzpnu2YUp67/pY7uzcqV6JnhLOXJM2qhA8FhBhaPVTptDBx kqASS4V81gdRTZ9M6MF8xtV7zk1G5NY3PklSGPo3psUA0KFpUf6zDbv2qyWnbUidXArS bjqLv8zgaDWwnq8J+aukAhs/n5MOB01ZblQ784a0e4YQZ/sEiofBI1/D6Iwq6kxqqcso WY3w== X-Gm-Message-State: AOJu0YyQ+7/NfJml0xmWIwCNcM19Bte8KxM2tNM6n7PQTqaCtbOIHOV4 GLzoHWufHSj3/cfy2vebB+MQSImwMdU= X-Received: by 2002:a05:620a:394c:b0:76c:7f00:c451 with SMTP id qs12-20020a05620a394c00b0076c7f00c451mr3992506qkn.1.1691603233533; Wed, 09 Aug 2023 10:47:13 -0700 (PDT) Received: from nark.. ([146.70.198.174]) by smtp.gmail.com with ESMTPSA id oo17-20020a05620a531100b0076cda271e54sm4153587qkn.112.2023.08.09.10.47.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 10:47:13 -0700 (PDT) From: quietvoid X-Google-Original-From: quietvoid To: ffmpeg-devel@ffmpeg.org Date: Wed, 9 Aug 2023 13:46:57 -0400 Message-ID: <20230809174657.76-1-tcChlisop0@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3] avcodec/dovi_rpu: verify RPU data CRC32 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: quietvoid Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: kV3I4wIPeMOx The Dolby Vision RPU contains a CRC32 to validate the payload against. The implementation is CRC32/MPEG-2. The CRC is only verified with the AV_EF_CRCCHECK flag. Signed-off-by: quietvoid --- libavcodec/dovi_rpu.c | 46 ++++++++++++++++++++++++++++++++++++++++--- libavcodec/dovi_rpu.h | 3 ++- libavcodec/hevcdec.c | 3 ++- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c index dd38936552..1dfeee7564 100644 --- a/libavcodec/dovi_rpu.c +++ b/libavcodec/dovi_rpu.c @@ -22,6 +22,7 @@ */ #include "libavutil/buffer.h" +#include "libavutil/crc.h" #include "dovi_rpu.h" #include "golomb.h" @@ -191,13 +192,17 @@ static inline int64_t get_se_coef(GetBitContext *gb, const AVDOVIRpuDataHeader * } \ } while (0) -int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size) +int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size, + int err_recognition) { AVDOVIRpuDataHeader *hdr = &s->header; GetBitContext *gb = &(GetBitContext){0}; DOVIVdrRef *vdr; int ret; + size_t actual_rpu_size; + uint8_t trailing_zeroes = 0; + uint8_t nal_prefix; uint8_t rpu_type; uint8_t vdr_seq_info_present; @@ -205,7 +210,22 @@ int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size) uint8_t use_prev_vdr_rpu; uint8_t use_nlq; uint8_t profile; - if ((ret = init_get_bits8(gb, rpu, rpu_size)) < 0) + + uint32_t rpu_data_crc32; + uint32_t computed_crc32; + + for (int i = rpu_size - 1; i > 0; i--) { + if (!rpu[i]) { + trailing_zeroes++; + } else { + break; + } + } + + actual_rpu_size = rpu_size - trailing_zeroes; + + /* Exclude trailing byte (0x80) from reader */ + if ((ret = init_get_bits8(gb, rpu, actual_rpu_size - 1)) < 0) return ret; /* RPU header, common values */ @@ -440,7 +460,27 @@ int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size) color->source_diagonal = get_bits(gb, 10); } - /* FIXME: verify CRC32, requires implementation of AV_CRC_32_MPEG_2 */ + if (!(err_recognition & AV_EF_CRCCHECK)) + return 0; + + /* Skip unsupported until CRC32 */ + skip_bits_long(gb, get_bits_left(gb) - 32); + + rpu_data_crc32 = get_bits_long(gb, 32); + + /* Verify CRC32, buffer excludes the prefix, CRC32 and trailing byte */ + computed_crc32 = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), + -1, rpu + 1, actual_rpu_size - 6)); + + if (rpu_data_crc32 != computed_crc32) { + av_log(s->logctx, AV_LOG_ERROR, + "RPU CRC mismatch! Expected %"PRIu32", received %"PRIu32"\n", + rpu_data_crc32, computed_crc32); + + if (err_recognition & AV_EF_EXPLODE) + goto fail; + } + return 0; fail: diff --git a/libavcodec/dovi_rpu.h b/libavcodec/dovi_rpu.h index f6ca5bbbc5..2b993a72c6 100644 --- a/libavcodec/dovi_rpu.h +++ b/libavcodec/dovi_rpu.h @@ -77,7 +77,8 @@ void ff_dovi_update_cfg(DOVIContext *s, const AVDOVIDecoderConfigurationRecord * * * Returns 0 or an error code. */ -int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size); +int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size, + int err_recognition); /** * Attach the decoded AVDOVIMetadata as side data to an AVFrame. diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c index df40c91ba6..81b1a84625 100644 --- a/libavcodec/hevcdec.c +++ b/libavcodec/hevcdec.c @@ -3182,7 +3182,8 @@ static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length) return AVERROR(ENOMEM); memcpy(s->rpu_buf->data, nal->raw_data + 2, nal->raw_size - 2); - ret = ff_dovi_rpu_parse(&s->dovi_ctx, nal->data + 2, nal->size - 2); + ret = ff_dovi_rpu_parse(&s->dovi_ctx, nal->data + 2, nal->size - 2, + s->avctx->err_recognition); if (ret < 0) { av_buffer_unref(&s->rpu_buf); av_log(s->avctx, AV_LOG_WARNING, "Error parsing DOVI NAL unit.\n");