[FFmpeg-cvslog] avcodec/dovi_rpu: verify RPU data CRC32

Niklas Haas git at videolan.org
Wed Apr 3 17:33:37 EEST 2024


ffmpeg | branch: master | Niklas Haas <git at haasn.dev> | Sat Mar 23 16:29:12 2024 +0100| [b90c18b38c223e52b852e7156dd5bf1f59200ee7] | committer: Niklas Haas

avcodec/dovi_rpu: verify RPU data CRC32

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 <tcChlisop0 at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=b90c18b38c223e52b852e7156dd5bf1f59200ee7
---

 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 e5088fc7b5..824725c031 100644
--- a/libavcodec/av1dec.c
+++ b/libavcodec/av1dec.c
@@ -1002,7 +1002,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 be8118b4cf..85aded52bd 100644
--- a/libavcodec/dovi_rpu.c
+++ b/libavcodec/dovi_rpu.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/buffer.h"
 #include "libavutil/mem.h"
+#include "libavutil/crc.h"
 
 #include "avcodec.h"
 #include "dovi_rpu.h"
@@ -201,7 +202,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};
@@ -269,6 +271,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;
 
@@ -509,7 +524,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 755171c923..8dcc65bb40 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 752459af2d..727b02f0f4 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -3208,7 +3208,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 ddcd0708b4..f022a4ad05 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



More information about the ffmpeg-cvslog mailing list