When a reference picture set names a picture that was never received and
decoding goes on (AV_CODEC_FLAG_OUTPUT_CORRUPT or AV_CODEC_FLAG2_SHOW_ALL),
generate_missing_ref() fills the stand-in with mid-gray. Every picture
predicted from it up to the next IRAP is then built on a flat gray frame.

Copy the decoded picture closest to the missing one in output order
instead, preferring an earlier one, as h264_field_start() does for the
stand-ins of a frame_num gap. Gray stays the fallback when the DPB holds
no picture of the same size and format, and with a hwaccel.

Measured on a 1280x720 libx265 stream (one reference, no B-frames,
30-picture GOP), dropping 1, 2 or 6 consecutive pictures inside a GOP,
81 cases: the median over cases of the mean luma PSNR from the loss to the
end of the GOP, against the lossless decode, goes from 13.2 dB to 25.2 dB.
Repeating the last picture before the loss gives 25.3 dB. Decoding with
frame threads gives the same output. Pictures from the next IRAP on are
unchanged.

Signed-off-by: Omar Ramadan <[email protected]>
---
 libavcodec/hevc/refs.c          | 40 +++++++++++++++++++++++++++++++--
 tests/fate/hevc.mak             |  5 +++++
 tests/ref/fate/hevc-missing-ref | 29 ++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 tests/ref/fate/hevc-missing-ref

diff --git a/libavcodec/hevc/refs.c b/libavcodec/hevc/refs.c
index 84418d2104..326e655e06 100644
--- a/libavcodec/hevc/refs.c
+++ b/libavcodec/hevc/refs.c
@@ -462,15 +462,51 @@ static void mark_ref(HEVCFrame *frame, int flag)
     frame->flags |= flag;
 }
 
+/* The decoded picture to stand in for a missing reference: the closest one
+ * before it in output order, else the closest one after it. */
+static HEVCFrame *find_missing_ref_donor(HEVCContext *s, HEVCLayerContext *l,
+                                         const HEVCFrame *missing, int poc)
+{
+    HEVCFrame *best = NULL;
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
+        HEVCFrame *ref = &l->DPB[i];
+        if (!ref->f || ref == missing || ref == s->cur_frame ||
+            ref->flags & HEVC_FRAME_FLAG_UNAVAILABLE ||
+            ref->f->width  != missing->f->width  ||
+            ref->f->height != missing->f->height ||
+            ref->f->format != missing->f->format)
+            continue;
+        if (!best ||
+            (ref->poc < poc) > (best->poc < poc) ||
+            ((ref->poc < poc) == (best->poc < poc) &&
+             FFABS(ref->poc - poc) < FFABS(best->poc - poc)))
+            best = ref;
+    }
+    return best;
+}
+
 static HEVCFrame *generate_missing_ref(HEVCContext *s, HEVCLayerContext *l, 
int poc)
 {
-    HEVCFrame *frame;
+    HEVCFrame *frame, *donor;
 
     frame = alloc_frame(s, l);
     if (!frame)
         return NULL;
 
-    if (!s->avctx->hwaccel && !ff_decode_skip_all_pixels(s->avctx)) {
+    /* Like H.264 for a frame_num gap, predict from the nearest decoded
+     * picture rather than from mid-gray: the pictures until the next IRAP then
+     * stay close to correct instead of being built on a flat gray frame. */
+    donor = s->avctx->hwaccel || ff_decode_skip_all_pixels(s->avctx) ? NULL :
+            find_missing_ref_donor(s, l, frame, poc);
+    if (donor) {
+        if (s->avctx->active_thread_type == FF_THREAD_FRAME)
+            ff_progress_frame_await(&donor->tf, INT_MAX);
+        if (av_frame_copy(frame->f, donor->f) < 0)
+            donor = NULL;
+    }
+
+    if (!donor && !s->avctx->hwaccel && !ff_decode_skip_all_pixels(s->avctx)) {
         int nb_planes = l->sps->chroma_format_idc ? 3 : 1;
         if (!l->sps->pixel_shift) {
             for (int i = 0; i < nb_planes; i++)
diff --git a/tests/fate/hevc.mak b/tests/fate/hevc.mak
index 4dac1a2b4b..18ed9d78ea 100644
--- a/tests/fate/hevc.mak
+++ b/tests/fate/hevc.mak
@@ -269,6 +269,11 @@ fate-hevc-skipframe-dslice: CMD = framecrc -skip_frame 
nointra -err_detect explo
 fate-hevc-skipframe-slices: CMD = framecrc -skip_frame nointra -err_detect 
explode -i $(TARGET_SAMPLES)/hevc-conformance/SLICES_A_Rovi_3.bit
 FATE_HEVC-$(call FRAMECRC, HEVC, HEVC, HEVC_PARSER) += 
fate-hevc-skipframe-dslice fate-hevc-skipframe-slices
 
+# picture 4 is dropped: the pictures that reference it are predicted from the
+# nearest decoded picture, generated in its place
+fate-hevc-missing-ref: CMD = framecrc -flags output_corrupt -bsf:v 
"noise=drop=eq(n\,4)" -i $(TARGET_SAMPLES)/hevc-conformance/PS_A_VIDYO_3.bit
+FATE_HEVC-$(call FRAMECRC, HEVC, HEVC, HEVC_PARSER NOISE_BSF) += 
fate-hevc-missing-ref
+
 # this sample has two stsd entries and needs to reload extradata
 FATE_HEVC-$(call FRAMEMD5, MOV, HEVC, SCALE_FILTER) += 
fate-hevc-extradata-reload
 fate-hevc-extradata-reload: CMD = framemd5 -i 
$(TARGET_SAMPLES)/hevc/extradata-reload-multi-stsd.mov -sws_flags bitexact
diff --git a/tests/ref/fate/hevc-missing-ref b/tests/ref/fate/hevc-missing-ref
new file mode 100644
index 0000000000..a6008a409d
--- /dev/null
+++ b/tests/ref/fate/hevc-missing-ref
@@ -0,0 +1,29 @@
+#tb 0: 1/25
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 416x240
+#sar 0: 0/1
+0,          0,          0,        1,   149760, 0x88619f80
+0,          1,          1,        1,   149760, 0x550bdaf0
+0,          2,          2,        1,   149760, 0x4121f7a2
+0,          3,          3,        1,   149760, 0x210b1d07
+0,          4,          4,        1,   149760, 0x135d316e
+0,          5,          5,        1,   149760, 0x57d33f44
+0,          6,          6,        1,   149760, 0xac773596
+0,          7,          7,        1,   149760, 0x46d9f91a
+0,          8,          8,        1,   149760, 0x53cb1990
+0,          9,          9,        1,   149760, 0x51e13c03
+0,         10,         10,        1,   149760, 0x40fb07b1
+0,         11,         11,        1,   149760, 0xbd8375c4
+0,         12,         12,        1,   149760, 0x645eb1b7
+0,         13,         13,        1,   149760, 0xcda89837
+0,         14,         14,        1,   149760, 0xd1d7bca3
+0,         15,         15,        1,   149760, 0xd3612909
+0,         16,         16,        1,   149760, 0x733731f9
+0,         17,         17,        1,   149760, 0x8af7d792
+0,         18,         18,        1,   149760, 0xfab2b736
+0,         19,         19,        1,   149760, 0x859f4be0
+0,         20,         20,        1,   149760, 0xc2944d1e
+0,         21,         21,        1,   149760, 0x4c604450
+0,         22,         22,        1,   149760, 0x7a50123e
+0,         23,         23,        1,   149760, 0xd039f895
-- 
2.43.0


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

Reply via email to