The 03_Tsinghua_2_1_4.0field conformance stream repeats its sequence
header every GOP with the low_delay flag alternating.  Picking the
output frame as DPB[!low_delay] drops the backlog frame across the
switch: after a non-low_delay GOP the delayed frame sits in DPB[1],
but low_delay output takes DPB[0], so one frame is lost and every
subsequent frame is shifted.

Track an outputed flag per DPB entry and output the backlog frame
first, then the current one when it is due (immediately for
low_delay, one picture late otherwise); the end-of-stream flush
outputs a pending frame regardless of low_delay.

Co-Authored-By: kimi-k3 with kimi-code
Signed-off-by: jianfeng.zheng <[email protected]>
---
 libavcodec/cavs.h    |  1 +
 libavcodec/cavsdec.c | 18 +++++++++++++++---
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/libavcodec/cavs.h b/libavcodec/cavs.h
index fdfac416d3..e62eaf5476 100644
--- a/libavcodec/cavs.h
+++ b/libavcodec/cavs.h
@@ -172,6 +172,7 @@ struct dec_2dvlc {
 typedef struct AVSFrame {
     AVFrame *f;
     int poc;
+    int already_output; ///< already returned to the caller
 } AVSFrame;
 
 typedef struct AVSContext {
diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index 46b93aa647..4ed3ac9a1d 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -2203,6 +2203,7 @@ static int decode_pic(AVSContext *h)
     }
 
     av_frame_unref(h->cur.f);
+    h->cur.already_output = 0;
 
     skip_bits(&h->gb, 16);//bbv_delay
     if (h->profile == AV_PROFILE_CAVS_BROADCASTING)
@@ -2710,9 +2711,10 @@ static int cavs_decode_frame(AVCodecContext *avctx, 
AVFrame *rframe,
     int frame_start = 0;
 
     if (buf_size == 0) {
-        if (!h->low_delay && h->DPB[0].f->data[0]) {
+        if (h->DPB[0].f->data[0] && !h->DPB[0].already_output) {
             *got_frame = 1;
             av_frame_move_ref(rframe, h->DPB[0].f);
+            h->DPB[0].already_output = 1;
         }
         return 0;
     }
@@ -2768,9 +2770,19 @@ static int cavs_decode_frame(AVCodecContext *avctx, 
AVFrame *rframe,
                 return ret;
             *got_frame = 1;
             if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
-                if (h->DPB[!h->low_delay].f->data[0]) {
-                    if ((ret = av_frame_ref(rframe, h->DPB[!h->low_delay].f)) 
< 0)
+                /* the display queue is one deep: output the backlog frame
+                 * first, then the current one when it is due (immediately
+                 * for low_delay, one picture late otherwise) */
+                AVSFrame *out = NULL;
+                if (h->DPB[1].f->data[0] && !h->DPB[1].already_output)
+                    out = &h->DPB[1];
+                else if (h->low_delay && h->DPB[0].f->data[0] &&
+                         !h->DPB[0].already_output)
+                    out = &h->DPB[0];
+                if (out) {
+                    if ((ret = av_frame_ref(rframe, out->f)) < 0)
                         return ret;
+                    out->already_output = 1;
                 } else {
                     *got_frame = 0;
                 }
-- 
2.34.1

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

Reply via email to