diff mbox series

[FFmpeg-devel] avcodec/av1dec: implement FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM

Message ID 20240220223323.4165-1-jamrial@gmail.com
State Accepted
Commit 194414f62d987ad831654c45fff6ee27898b832e
Headers show
Series [FFmpeg-devel] avcodec/av1dec: implement FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | 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

James Almer Feb. 20, 2024, 10:33 p.m. UTC
Fixes errors when opening streams with no extradata (like those from raw OBU
sources). It also calls get_format() on new Sequence Headers when required.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/av1dec.c | 58 +++++++++++++++++++++++++++++----------------
 libavcodec/av1dec.h |  2 ++
 2 files changed, 39 insertions(+), 21 deletions(-)

Comments

James Almer Feb. 29, 2024, 1:32 p.m. UTC | #1
On 2/20/2024 7:33 PM, James Almer wrote:
> Fixes errors when opening streams with no extradata (like those from raw OBU
> sources). It also calls get_format() on new Sequence Headers when required.
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>   libavcodec/av1dec.c | 58 +++++++++++++++++++++++++++++----------------
>   libavcodec/av1dec.h |  2 ++
>   2 files changed, 39 insertions(+), 21 deletions(-)

Will apply unless someone objects.
diff mbox series

Patch

diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c
index 7debc4deda..48a3af2fca 100644
--- a/libavcodec/av1dec.c
+++ b/libavcodec/av1dec.c
@@ -620,6 +620,12 @@  static int get_pixel_format(AVCodecContext *avctx)
     *fmtp++ = pix_fmt;
     *fmtp = AV_PIX_FMT_NONE;
 
+    for (int i = 0; pix_fmts[i] != pix_fmt; i++)
+        if (pix_fmts[i] == avctx->pix_fmt) {
+            s->pix_fmt = pix_fmt;
+            return 1;
+        }
+
     ret = ff_get_format(avctx, pix_fmts);
 
     /**
@@ -715,6 +721,7 @@  static av_cold int av1_decode_free(AVCodecContext *avctx)
         av1_frame_unref(&s->cur_frame);
         av_frame_free(&s->cur_frame.f);
     }
+    av_buffer_unref(&s->seq_data_ref);
     ff_refstruct_unref(&s->seq_ref);
     ff_refstruct_unref(&s->header_ref);
     ff_refstruct_unref(&s->cll_ref);
@@ -771,6 +778,9 @@  static int set_context_with_sequence(AVCodecContext *avctx,
                                             seq->timing_info.num_units_in_display_tick,
                                             seq->timing_info.time_scale);
 
+    if (avctx->pix_fmt == AV_PIX_FMT_NONE)
+        avctx->pix_fmt = get_sw_pixel_format(avctx, seq);
+
     return 0;
 }
 
@@ -869,8 +879,6 @@  static av_cold int av1_decode_init(AVCodecContext *avctx)
             goto end;
         }
 
-        avctx->pix_fmt = get_sw_pixel_format(avctx, seq);
-
         end:
         ff_cbs_fragment_reset(&s->current_obu);
     }
@@ -1174,6 +1182,23 @@  static int get_current_frame(AVCodecContext *avctx)
         avctx->skip_frame >= AVDISCARD_ALL)
         return 0;
 
+    if (s->pix_fmt == AV_PIX_FMT_NONE) {
+        ret = get_pixel_format(avctx);
+        if (ret < 0) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to get pixel format.\n");
+            return ret;
+        }
+
+        if (!ret && FF_HW_HAS_CB(avctx, decode_params)) {
+            ret = FF_HW_CALL(avctx, decode_params, AV1_OBU_SEQUENCE_HEADER,
+                             s->seq_data_ref->data, s->seq_data_ref->size);
+            if (ret < 0) {
+                av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
+                return ret;
+            }
+        }
+    }
+
     ret = av1_frame_alloc(avctx, &s->cur_frame);
     if (ret < 0) {
         av_log(avctx, AV_LOG_ERROR,
@@ -1208,6 +1233,12 @@  static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
 
         switch (unit->type) {
         case AV1_OBU_SEQUENCE_HEADER:
+            ret = av_buffer_replace(&s->seq_data_ref, unit->data_ref);
+            if (ret < 0)
+                goto end;
+
+            s->seq_data_ref->data = unit->data;
+            s->seq_data_ref->size = unit->data_size;
             ff_refstruct_replace(&s->seq_ref, unit->content_ref);
 
             s->raw_seq = &obu->obu.sequence_header;
@@ -1221,25 +1252,8 @@  static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
 
             s->operating_point_idc = s->raw_seq->operating_point_idc[s->operating_point];
 
-            if (s->pix_fmt == AV_PIX_FMT_NONE) {
-                ret = get_pixel_format(avctx);
-                if (ret < 0) {
-                    av_log(avctx, AV_LOG_ERROR,
-                           "Failed to get pixel format.\n");
-                    s->raw_seq = NULL;
-                    goto end;
-                }
-            }
+            s->pix_fmt = AV_PIX_FMT_NONE;
 
-            if (FF_HW_HAS_CB(avctx, decode_params)) {
-                ret = FF_HW_CALL(avctx, decode_params, unit->type,
-                                 unit->data, unit->data_size);
-                if (ret < 0) {
-                    av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
-                    s->raw_seq = NULL;
-                    goto end;
-                }
-            }
             break;
         case AV1_OBU_REDUNDANT_FRAME_HEADER:
             if (s->raw_frame_header)
@@ -1416,6 +1430,8 @@  end:
         ff_cbs_fragment_reset(&s->current_obu);
         s->nb_unit = 0;
     }
+    if (!ret && !frame->buf[0])
+        ret = AVERROR(EAGAIN);
 
     return ret;
 }
@@ -1500,7 +1516,7 @@  const FFCodec ff_av1_decoder = {
     .close                 = av1_decode_free,
     FF_CODEC_RECEIVE_FRAME_CB(av1_receive_frame),
     .p.capabilities        = AV_CODEC_CAP_DR1,
-    .caps_internal         = FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal         = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
     .flush                 = av1_decode_flush,
     .p.profiles            = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
     .p.priv_class          = &av1_class,
diff --git a/libavcodec/av1dec.h b/libavcodec/av1dec.h
index b6a0c08e48..a6ad80c12a 100644
--- a/libavcodec/av1dec.h
+++ b/libavcodec/av1dec.h
@@ -23,6 +23,7 @@ 
 
 #include <stdint.h>
 
+#include "libavutil/buffer.h"
 #include "libavutil/fifo.h"
 #include "libavutil/frame.h"
 #include "libavutil/pixfmt.h"
@@ -69,6 +70,7 @@  typedef struct AV1DecContext {
     CodedBitstreamFragment current_obu;
     AVPacket *pkt;
 
+    AVBufferRef *seq_data_ref;
     AV1RawOBU *seq_ref;    ///< RefStruct reference backing raw_seq
     AV1RawSequenceHeader *raw_seq;
     AV1RawOBU *header_ref; ///< RefStruct reference backing raw_frame_header