diff mbox series

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

Message ID 20230809150155.96-1-tcChlisop0@gmail.com
State New
Headers show
Series [FFmpeg-devel] avcodec/dovi_rpu: verify RPU data CRC32 | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

quietvoid Aug. 9, 2023, 3:01 p.m. UTC
The Dolby Vision RPU contains a CRC32 to validate the payload against.
It must be an identical match for the metadata to be used.

The implementation is CRC32/MPEG-2.

Signed-off-by: quietvoid <tcChlisop0@gmail.com>
---
 libavcodec/dovi_rpu.c | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
index dd38936552..00f811e3a6 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"
@@ -198,6 +199,9 @@  int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size)
     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 +209,21 @@  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 +458,22 @@  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 */
+    /* Skip unsupported until CRC32 */
+    skip_bits_long(gb, get_bits_left(gb) - 32);
+
+    rpu_data_crc32 = get_bits_long(gb, 32);
+
+    /* Verify CRC32, the 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,
+               "Invalid RPU CRC32: Received %"PRIu32", expected %"PRIu32"\n",
+               computed_crc32, rpu_data_crc32);
+        goto fail;
+    }
+
     return 0;
 
 fail: