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 93A6F47883 for ; Thu, 26 Oct 2023 21:44:10 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1EC8D68CB89; Fri, 27 Oct 2023 00:44:09 +0300 (EEST) Received: from relay1-d.mail.gandi.net (relay1-d.mail.gandi.net [217.70.183.193]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F0BE96881B1 for ; Fri, 27 Oct 2023 00:44:01 +0300 (EEST) Received: by mail.gandi.net (Postfix) with ESMTPSA id 2F7F5240004 for ; Thu, 26 Oct 2023 21:44:00 +0000 (UTC) Date: Thu, 26 Oct 2023 23:44:00 +0200 From: Michael Niedermayer To: FFmpeg development discussions and patches Message-ID: <20231026214400.GM3543730@pb2> References: <20230809174657.76-1-tcChlisop0@gmail.com> MIME-Version: 1.0 In-Reply-To: <20230809174657.76-1-tcChlisop0@gmail.com> X-GND-Sasl: michael@niedermayer.cc Subject: Re: [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 Content-Type: multipart/mixed; boundary="===============2048240078078905010==" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: --===============2048240078078905010== Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="QIjdi1eMORvAc8Ct" Content-Disposition: inline --QIjdi1eMORvAc8Ct Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Aug 09, 2023 at 01:46:57PM -0400, quietvoid wrote: > The Dolby Vision RPU contains a CRC32 to validate the payload against. > The implementation is CRC32/MPEG-2. >=20 > The CRC is only verified with the AV_EF_CRCCHECK flag. >=20 > 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(-) >=20 > 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 @@ > */ > =20 > #include "libavutil/buffer.h" > +#include "libavutil/crc.h" > =20 > #include "dovi_rpu.h" > #include "golomb.h" > @@ -191,13 +192,17 @@ static inline int64_t get_se_coef(GetBitContext *gb= , const AVDOVIRpuDataHeader * > } = \ > } while (0) > =20 > -int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_siz= e) > +int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_siz= e, > + int err_recognition) > { > AVDOVIRpuDataHeader *hdr =3D &s->header; > GetBitContext *gb =3D &(GetBitContext){0}; > DOVIVdrRef *vdr; > int ret; > =20 > + size_t actual_rpu_size; > + uint8_t trailing_zeroes =3D 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 =3D init_get_bits8(gb, rpu, rpu_size)) < 0) > + > + uint32_t rpu_data_crc32; > + uint32_t computed_crc32; > + > + for (int i =3D rpu_size - 1; i > 0; i--) { > + if (!rpu[i]) { > + trailing_zeroes++; > + } else { > + break; > + } > + } > + > + actual_rpu_size =3D rpu_size - trailing_zeroes; > + > + /* Exclude trailing byte (0x80) from reader */ > + if ((ret =3D init_get_bits8(gb, rpu, actual_rpu_size - 1)) < 0) > return ret; > =20 > /* 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 =3D get_bits(gb, 10); > } > =20 > - /* 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 =3D get_bits_long(gb, 32); > + > + /* Verify CRC32, buffer excludes the prefix, CRC32 and trailing byte= */ > + computed_crc32 =3D av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE= ), > + -1, rpu + 1, actual_rpu_size - 6)= ); > + > + if (rpu_data_crc32 !=3D 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; > + } (correctly designed) CRCs have the beautifull symmetry that you can merge the crc32 value into the crc computation and then a 0 means no CRC missmatch (there are many other cool properties but this one allows to simplify the c= ode) This works too: (and is simpler) /* Skip unsupported until CRC32 */ skip_bits_long(gb, get_bits_left(gb)); /* Verify CRC32, buffer excludes the prefix, CRC32 and trailing byte */ computed_crc32 =3D av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, rpu + 1, actual_rpu_size - 2)); if (computed_crc32) { av_log(s->logctx, AV_LOG_ERROR, "RPU CRC mismatch! %"PRIX32"\n", computed_crc32); if (err_recognition & AV_EF_EXPLODE) goto fail; } [...] --=20 Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Into a blind darkness they enter who follow after the Ignorance, they as if into a greater darkness enter who devote themselves to the Knowledge alone. -- Isha Upanishad --QIjdi1eMORvAc8Ct Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iF0EABEIAB0WIQSf8hKLFH72cwut8TNhHseHBAsPqwUCZTrdnAAKCRBhHseHBAsP q9KLAKCcPVD9IZdII+ZTt5bYAQHZrF7tpACfU35qz9cxPvf+mf1usltLKve/2K4= =e3J6 -----END PGP SIGNATURE----- --QIjdi1eMORvAc8Ct-- --===============2048240078078905010== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ 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". --===============2048240078078905010==--