PR #23354 opened by James Almer (jamrial)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23354
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23354.patch

Some encoders seem to place the SEI suffix NAL at the very end, with the RPU 
before it.


>From 54c47aeff96106eccc61d9faba0c0b039a552330 Mon Sep 17 00:00:00 2001
From: Hendrik Leppkes <[email protected]>
Date: Wed, 3 Jun 2026 11:24:26 +0200
Subject: [PATCH] avcodec/hevc: look for the DOVI RPU in all NALs, not just the
 last one

Some encoders seem to place the SEI suffix NAL at the very end, with the
RPU before it.

Signed-off-by: James Almer <[email protected]>
---
 libavcodec/hevc/hevcdec.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/libavcodec/hevc/hevcdec.c b/libavcodec/hevc/hevcdec.c
index 6fec59d31e..dfd6203dac 100644
--- a/libavcodec/hevc/hevcdec.c
+++ b/libavcodec/hevc/hevcdec.c
@@ -3740,25 +3740,31 @@ static int decode_nal_units(HEVCContext *s, const 
uint8_t *buf, int length)
      * Dolby Vision RPUs masquerade as unregistered NALs of type 62.
      *
      * We have to do this check here an create the rpu buffer, since RPUs are 
appended
-     * to the end of an AU; they are the last non-EOB/EOS NAL in the AU.
+     * to the end of an AU; they are normally the last non-EOB/EOS NAL in the 
AU.
      */
-    if (s->pkt.nb_nals > 1 && s->pkt.nals[s->pkt.nb_nals - 1].type == 
HEVC_NAL_UNSPEC62 &&
-        s->pkt.nals[s->pkt.nb_nals - 1].size > 2 && 
!s->pkt.nals[s->pkt.nb_nals - 1].nuh_layer_id
-        && !s->pkt.nals[s->pkt.nb_nals - 1].temporal_id) {
-        H2645NAL *nal = &s->pkt.nals[s->pkt.nb_nals - 1];
+    H2645NAL *rpu_nal = NULL;
+    for (int i = s->pkt.nb_nals - 1; i > 0 ; i--) {
+        if (s->pkt.nals[i].type == HEVC_NAL_UNSPEC62 && s->pkt.nals[i].size > 2
+            && !s->pkt.nals[i].nuh_layer_id && !s->pkt.nals[i].temporal_id) {
+                rpu_nal = &s->pkt.nals[i];
+                break;
+        }
+    }
+
+    if (rpu_nal) {
         if (s->rpu_buf) {
             av_buffer_unref(&s->rpu_buf);
             av_log(s->avctx, AV_LOG_WARNING, "Multiple Dolby Vision RPUs found 
in one AU. Skipping previous.\n");
         }
 
-        s->rpu_buf = av_buffer_alloc(nal->raw_size - 2);
+        s->rpu_buf = av_buffer_alloc(rpu_nal->raw_size - 2);
         if (!s->rpu_buf) {
             ret = AVERROR(ENOMEM);
             goto fail;
         }
-        memcpy(s->rpu_buf->data, nal->raw_data + 2, nal->raw_size - 2);
+        memcpy(s->rpu_buf->data, rpu_nal->raw_data + 2, rpu_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, rpu_nal->data + 2, rpu_nal->size 
- 2,
                                 s->avctx->err_recognition);
         if (ret < 0) {
             av_buffer_unref(&s->rpu_buf);
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to