[FFmpeg-devel] [PATCH v2] avcodec/av1dec: honor the requested skip_frame level

James Almer jamrial at gmail.com
Sun Jan 9 21:18:55 EET 2022


This supports dropping non-intra, non-key, or all frames.

Signed-off-by: James Almer <jamrial at gmail.com>
---
Now dropping the frames without decoding them.

Untested with actual hardware.

 libavcodec/av1dec.c | 68 ++++++++++++++++++++++++++-------------------
 1 file changed, 39 insertions(+), 29 deletions(-)

diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c
index 09df2bf421..62d459cded 100644
--- a/libavcodec/av1dec.c
+++ b/libavcodec/av1dec.c
@@ -585,16 +585,19 @@ static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *s
 {
     int ret;
 
-    ret = ff_thread_ref_frame(&dst->tf, &src->tf);
+    ret = av_buffer_replace(&dst->header_ref, src->header_ref);
     if (ret < 0)
         return ret;
 
-    dst->header_ref = av_buffer_ref(src->header_ref);
-    if (!dst->header_ref)
-        goto fail;
-
     dst->raw_frame_header = src->raw_frame_header;
 
+    if (!src->tf.f->buf[0])
+        return 0;
+
+    ret = ff_thread_ref_frame(&dst->tf, &src->tf);
+    if (ret < 0)
+        goto fail;
+
     if (src->hwaccel_picture_private) {
         dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
         if (!dst->hwaccel_priv_buf)
@@ -800,12 +803,6 @@ static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
         return ret;
     }
 
-    f->header_ref = av_buffer_ref(s->header_ref);
-    if (!f->header_ref)
-        return AVERROR(ENOMEM);
-
-    f->raw_frame_header = s->raw_frame_header;
-
     if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
         goto fail;
 
@@ -945,8 +942,7 @@ static int update_reference_list(AVCodecContext *avctx)
 
     for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
         if (header->refresh_frame_flags & (1 << i)) {
-            if (s->ref[i].tf.f->buf[0])
-                av1_frame_unref(avctx, &s->ref[i]);
+            av1_frame_unref(avctx, &s->ref[i]);
             if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
                 av_log(avctx, AV_LOG_ERROR,
                        "Failed to update frame %d in reference list\n", i);
@@ -962,19 +958,32 @@ static int get_current_frame(AVCodecContext *avctx)
     AV1DecContext *s = avctx->priv_data;
     int ret;
 
-    if (s->cur_frame.tf.f->buf[0])
-        av1_frame_unref(avctx, &s->cur_frame);
+    av1_frame_unref(avctx, &s->cur_frame);
 
-    ret = av1_frame_alloc(avctx, &s->cur_frame);
+    s->cur_frame.header_ref = av_buffer_ref(s->header_ref);
+    if (!s->cur_frame.header_ref)
+        return AVERROR(ENOMEM);
+
+    s->cur_frame.raw_frame_header = s->raw_frame_header;
+
+    ret = init_tile_data(s);
     if (ret < 0) {
-        av_log(avctx, AV_LOG_ERROR,
-               "Failed to allocate space for current frame.\n");
+        av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
         return ret;
     }
 
-    ret = init_tile_data(s);
+    if ((avctx->skip_frame >= AVDISCARD_NONINTRA &&
+            (s->raw_frame_header->frame_type != AV1_FRAME_KEY &&
+             s->raw_frame_header->frame_type != AV1_FRAME_INTRA_ONLY)) ||
+        (avctx->skip_frame >= AVDISCARD_NONKEY   &&
+             s->raw_frame_header->frame_type != AV1_FRAME_KEY) ||
+        avctx->skip_frame >= AVDISCARD_ALL)
+        return 0;
+
+    ret = av1_frame_alloc(avctx, &s->cur_frame);
     if (ret < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
+        av_log(avctx, AV_LOG_ERROR,
+               "Failed to allocate space for current frame.\n");
         return ret;
     }
 
@@ -1077,8 +1086,7 @@ static int av1_decode_frame(AVCodecContext *avctx, void *frame,
                 s->raw_frame_header = &obu->obu.frame_header;
 
             if (s->raw_frame_header->show_existing_frame) {
-                if (s->cur_frame.tf.f->buf[0])
-                    av1_frame_unref(avctx, &s->cur_frame);
+                av1_frame_unref(avctx, &s->cur_frame);
 
                 ret = av1_frame_ref(avctx, &s->cur_frame,
                                     &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
@@ -1093,9 +1101,11 @@ static int av1_decode_frame(AVCodecContext *avctx, void *frame,
                     goto end;
                 }
 
-                ret = set_output_frame(avctx, frame, pkt, got_frame);
-                if (ret < 0)
-                    av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
+                if (s->cur_frame.tf.f->buf[0]) {
+                    ret = set_output_frame(avctx, frame, pkt, got_frame);
+                    if (ret < 0)
+                        av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
+                }
 
                 s->raw_frame_header = NULL;
 
@@ -1111,7 +1121,7 @@ static int av1_decode_frame(AVCodecContext *avctx, void *frame,
             s->cur_frame.spatial_id  = header->spatial_id;
             s->cur_frame.temporal_id = header->temporal_id;
 
-            if (avctx->hwaccel) {
+            if (avctx->hwaccel && s->cur_frame.tf.f->buf[0]) {
                 ret = avctx->hwaccel->start_frame(avctx, unit->data,
                                                   unit->data_size);
                 if (ret < 0) {
@@ -1138,7 +1148,7 @@ static int av1_decode_frame(AVCodecContext *avctx, void *frame,
             if (ret < 0)
                 goto end;
 
-            if (avctx->hwaccel) {
+            if (avctx->hwaccel && s->cur_frame.tf.f->buf[0]) {
                 ret = avctx->hwaccel->decode_slice(avctx,
                                                    raw_tile_group->tile_data.data,
                                                    raw_tile_group->tile_data.data_size);
@@ -1161,7 +1171,7 @@ static int av1_decode_frame(AVCodecContext *avctx, void *frame,
         }
 
         if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
-            if (avctx->hwaccel) {
+            if (avctx->hwaccel && s->cur_frame.tf.f->buf[0]) {
                 ret = avctx->hwaccel->end_frame(avctx);
                 if (ret < 0) {
                     av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
@@ -1175,7 +1185,7 @@ static int av1_decode_frame(AVCodecContext *avctx, void *frame,
                 goto end;
             }
 
-            if (s->raw_frame_header->show_frame) {
+            if (s->raw_frame_header->show_frame && s->cur_frame.tf.f->buf[0]) {
                 ret = set_output_frame(avctx, frame, pkt, got_frame);
                 if (ret < 0) {
                     av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
-- 
2.34.1



More information about the ffmpeg-devel mailing list