From patchwork Sat Mar 23 19:19:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 47388 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c889:b0:1a3:b6bb:3029 with SMTP id hb9csp369213pzb; Sat, 23 Mar 2024 12:25:48 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUVtl8Q78xFmNBQlM4fwZso+ANcVbf/QdHgCZafx7mLz120ScgdkRVi3Pfe2ciWeBmng7EULyEhZmML2aFXFNbxzMtc78U4GWJBQA== X-Google-Smtp-Source: AGHT+IEHMDBuhz8B4tMraHhLbGaTC6mwhaAw0xSCUxT/qmjFHbH7X94+pk8M0/EM5QCmOe8EFUN9 X-Received: by 2002:a17:906:5288:b0:a46:fbc6:d0cd with SMTP id c8-20020a170906528800b00a46fbc6d0cdmr2044111ejm.1.1711221948238; Sat, 23 Mar 2024 12:25:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1711221948; cv=none; d=google.com; s=arc-20160816; b=EOzWjCleVONRXhevwtOfXzUCSkrIacm3Y3hSRp0w1jtP+KZMTY72b47CIWRVeEaI6Q tGPucHTp+SYWr5NiPTtwBOeWA7aL7uJPNBH+OxNsX+tiFbyx5YndhOMIDwoqqces5r4F njYTMexLXKtF+PJGWUObASWNsa+/DxbkKYGA+zD3gYBCcEY18jUynTPmWJe3PPMY0PLf u4Xf9qvHBpo1vVRJCkWu2PlysstEEB3j8bkyaQikl955TEzWfrrk+pnk1OkjjTJEg+Az ro7AoYHNlY62GoSjv3udVDLVtMaxN4sYRFIwCW5cD5rV8RB5yC+9fyk5vTlNZhVzqODR Hv1g== 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=jXkGN1LA5wWnw7ZTkWEApjPJNOBzkNoDDe8WpVc8ZVY=; fh=C56SNERGsx7JRMjLZiHmpTAKnRl/jfaA+FIG9zHupRY=; b=OVpwG4IO+oE8KEJIVk5t/D6W+ydJJeyvsXBrCLLlyr2PTutMvHfHTqEt9z99zhymgQ nJWZvBKgdACVJNTFqmCQPXw4oXrkAzw2/25s5UE25rF6f/qmGuwz7pNPYG8/BmTHT7tT 235w8NxQhstQ6h5/Dre6kYS++3ckwkChnwBWoSuo9/dMuSVopIGaQW4wzLa6d6ZhBbNT ZWyM+LM+x1DX1/Jl0eqGSV45FXktTnKtn0FtFQxeTYahb7caCZKTwVKBLUdpixNie7ah /tt8WAvD+iOpFytSUH62n65NELbh/OXbZGvt9B0qWBDaZLS6txWg/5j3EqWkV8eBc2z0 qi7g==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=LWgQpyzm; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id m10-20020a1709060d8a00b00a4663b17910si1013224eji.739.2024.03.23.12.25.47; Sat, 23 Mar 2024 12:25:48 -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=@haasn.xyz header.s=mail header.b=LWgQpyzm; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AAF0568D5AB; Sat, 23 Mar 2024 21:24:57 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1620868D4C7 for ; Sat, 23 Mar 2024 21:24:49 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1711221884; bh=cvFD+Lty1iX4TRbXO2nCANAW/L1HnhBlZcUWAyIJUQ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LWgQpyzmYbdCs2MD7yTQ/eGOP62CfmauTHIoZ4LOeiAx0ZHWgEDfEVEBnVOOuG3yj Rj70pFtl/ODTRoDttaxZ8bPJgNrgtmHRUX95nYFEOmbr/AIF87MqOScXButgTAQZy0 6sPOjA60xHxO9HY0tVKm48OJThC7uszUFx5Ao9rg= Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id B0349429F3; Sat, 23 Mar 2024 20:24:44 +0100 (CET) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Sat, 23 Mar 2024 20:19:54 +0100 Message-ID: <20240323192440.38264-6-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240323192440.38264-1-ffmpeg@haasn.xyz> References: <20240323192440.38264-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 5/8] 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 , Niklas Haas Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: f+UFEpLg+ulr From: Niklas Haas 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. Co-authored-by: quietvoid --- libavcodec/av1dec.c | 3 ++- libavcodec/dovi_rpu.c | 18 ++++++++++++++++-- libavcodec/dovi_rpu.h | 3 ++- libavcodec/hevcdec.c | 3 ++- libavcodec/libdav1d.c | 3 ++- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c index 32a795e7580..e8753d302ae 100644 --- a/libavcodec/av1dec.c +++ b/libavcodec/av1dec.c @@ -1001,7 +1001,8 @@ static int export_itut_t35(AVCodecContext *avctx, AVFrame *frame, provider_oriented_code != 0x800) break; - ret = ff_dovi_rpu_parse(&s->dovi, gb.buffer, gb.buffer_end - gb.buffer); + ret = ff_dovi_rpu_parse(&s->dovi, gb.buffer, gb.buffer_end - gb.buffer, + avctx->err_recognition); if (ret < 0) { av_log(avctx, AV_LOG_WARNING, "Error parsing DOVI OBU.\n"); break; // ignore diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c index d792a429217..d584d99590a 100644 --- a/libavcodec/dovi_rpu.c +++ b/libavcodec/dovi_rpu.c @@ -22,6 +22,7 @@ */ #include "libavutil/buffer.h" +#include "libavutil/crc.h" #include "avcodec.h" #include "dovi_rpu.h" @@ -200,7 +201,8 @@ static inline unsigned get_variable_bits(GetBitContext *gb, int n) } \ } 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}; @@ -268,6 +270,19 @@ int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size) rpu_size--; } + if (!rpu_size || rpu[rpu_size - 1] != 0x80) + goto fail; + + if (err_recognition & AV_EF_CRCCHECK) { + uint32_t crc = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), + -1, rpu, rpu_size - 1)); /* exclude 0x80 */ + if (crc) { + av_log(s->logctx, AV_LOG_ERROR, "RPU CRC mismatch: %X\n", crc); + if (err_recognition & AV_EF_EXPLODE) + goto fail; + } + } + if ((ret = init_get_bits8(gb, rpu, rpu_size)) < 0) return ret; @@ -508,7 +523,6 @@ 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 */ return 0; fail: diff --git a/libavcodec/dovi_rpu.h b/libavcodec/dovi_rpu.h index 755171c9237..8dcc65bb40a 100644 --- a/libavcodec/dovi_rpu.h +++ b/libavcodec/dovi_rpu.h @@ -79,7 +79,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 575836e340c..3c8ec531a5d 100644 --- a/libavcodec/hevcdec.c +++ b/libavcodec/hevcdec.c @@ -3207,7 +3207,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"); diff --git a/libavcodec/libdav1d.c b/libavcodec/libdav1d.c index ddcd0708b4f..f022a4ad05c 100644 --- a/libavcodec/libdav1d.c +++ b/libavcodec/libdav1d.c @@ -567,7 +567,8 @@ static int libdav1d_receive_frame(AVCodecContext *c, AVFrame *frame) provider_oriented_code != 0x800) break; - res = ff_dovi_rpu_parse(&dav1d->dovi, gb.buffer, gb.buffer_end - gb.buffer); + res = ff_dovi_rpu_parse(&dav1d->dovi, gb.buffer, gb.buffer_end - gb.buffer, + c->err_recognition); if (res < 0) { av_log(c, AV_LOG_WARNING, "Error parsing DOVI OBU.\n"); break; // ignore