diff mbox series

[FFmpeg-devel,v3] avcodec/av1dec: convert to receive_frame()

Message ID 20230520190032.3190-1-jamrial@gmail.com
State New
Headers show
Series [FFmpeg-devel,v3] avcodec/av1dec: convert to receive_frame() | 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 May 20, 2023, 7 p.m. UTC
This removes the overhead of inserting the av1_frame_split bsf as part of the
decoding process.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 configure           |  2 +-
 libavcodec/av1dec.c | 77 ++++++++++++++++++++++++++++++++++-----------
 libavcodec/av1dec.h |  4 +++
 3 files changed, 63 insertions(+), 20 deletions(-)

Comments

James Almer May 25, 2023, 2:05 p.m. UTC | #1
On 5/20/2023 4:00 PM, James Almer wrote:
> This removes the overhead of inserting the av1_frame_split bsf as part of the
> decoding process.
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>   configure           |  2 +-
>   libavcodec/av1dec.c | 77 ++++++++++++++++++++++++++++++++++-----------
>   libavcodec/av1dec.h |  4 +++
>   3 files changed, 63 insertions(+), 20 deletions(-)

Will apply.
diff mbox series

Patch

diff --git a/configure b/configure
index a54398c57f..87f7afc2e1 100755
--- a/configure
+++ b/configure
@@ -2776,7 +2776,7 @@  asv1_encoder_select="aandcttables bswapdsp fdctdsp pixblockdsp"
 asv2_decoder_select="blockdsp bswapdsp idctdsp"
 asv2_encoder_select="aandcttables bswapdsp fdctdsp pixblockdsp"
 atrac1_decoder_select="sinewin"
-av1_decoder_select="av1_frame_split_bsf cbs_av1 atsc_a53"
+av1_decoder_select="cbs_av1 atsc_a53"
 bink_decoder_select="blockdsp hpeldsp"
 binkaudio_dct_decoder_select="wma_freqs"
 binkaudio_rdft_decoder_select="wma_freqs"
diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c
index d46ee48335..6951e8ea1e 100644
--- a/libavcodec/av1dec.c
+++ b/libavcodec/av1dec.c
@@ -32,6 +32,7 @@ 
 #include "bytestream.h"
 #include "codec_internal.h"
 #include "decode.h"
+#include "internal.h"
 #include "hwconfig.h"
 #include "profiles.h"
 #include "thread.h"
@@ -760,6 +761,7 @@  static const CodedBitstreamUnitType decompose_unit_types[] = {
 
 static av_cold int av1_decode_init(AVCodecContext *avctx)
 {
+    AVCodecInternal *avci = avctx->internal;
     AV1DecContext *s = avctx->priv_data;
     AV1RawSequenceHeader *seq;
     int ret;
@@ -767,6 +769,8 @@  static av_cold int av1_decode_init(AVCodecContext *avctx)
     s->avctx = avctx;
     s->pix_fmt = AV_PIX_FMT_NONE;
 
+    s->pkt = avci->in_pkt;
+
     for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
         s->ref[i].f = av_frame_alloc();
         if (!s->ref[i].f) {
@@ -1041,11 +1045,11 @@  static int export_film_grain(AVCodecContext *avctx, AVFrame *frame)
     return 0;
 }
 
-static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
-                            const AVPacket *pkt, int *got_frame)
+static int set_output_frame(AVCodecContext *avctx, AVFrame *frame)
 {
     AV1DecContext *s = avctx->priv_data;
     const AVFrame *srcframe = s->cur_frame.f;
+    AVPacket *pkt = s->pkt;
     int ret;
 
     // TODO: all layers
@@ -1079,7 +1083,7 @@  FF_DISABLE_DEPRECATION_WARNINGS
 FF_ENABLE_DEPRECATION_WARNINGS
 #endif
 
-    *got_frame = 1;
+    av_packet_unref(pkt);
 
     return 0;
 }
@@ -1145,22 +1149,29 @@  static int get_current_frame(AVCodecContext *avctx)
     return ret;
 }
 
-static int av1_decode_frame(AVCodecContext *avctx, AVFrame *frame,
-                            int *got_frame, AVPacket *pkt)
+static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
 {
     AV1DecContext *s = avctx->priv_data;
     AV1RawTileGroup *raw_tile_group = NULL;
-    int ret;
+    int i = 0, ret;
 
-    ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
-    if (ret < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
-        goto end;
+    if (!s->current_obu.nb_units) {
+        ret = ff_decode_get_packet(avctx, s->pkt);
+        if (ret < 0)
+            return ret;
+
+        ret = ff_cbs_read_packet(s->cbc, &s->current_obu, s->pkt);
+        if (ret < 0) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
+            goto end;
+        }
+
+        s->nb_unit = 0;
+        av_log(avctx, AV_LOG_DEBUG, "Total OBUs on this packet: %d.\n",
+               s->current_obu.nb_units);
     }
-    av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
-           s->current_obu.nb_units);
 
-    for (int i = 0; i < s->current_obu.nb_units; i++) {
+    for (i = s->nb_unit; i < s->current_obu.nb_units; i++) {
         CodedBitstreamUnit *unit = &s->current_obu.units[i];
         AV1RawOBU *obu = unit->content;
         const AV1RawOBUHeader *header;
@@ -1251,13 +1262,15 @@  static int av1_decode_frame(AVCodecContext *avctx, AVFrame *frame,
                     goto end;
                 }
 
+                ret = 0;
                 if (s->cur_frame.f->buf[0]) {
-                    ret = set_output_frame(avctx, frame, pkt, got_frame);
+                    ret = set_output_frame(avctx, frame);
                     if (ret < 0)
                         av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
                 }
 
                 s->raw_frame_header = NULL;
+                i++;
 
                 goto end;
             }
@@ -1361,6 +1374,7 @@  static int av1_decode_frame(AVCodecContext *avctx, AVFrame *frame,
         }
 
         if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
+            int show_frame = s->raw_frame_header->show_frame;
             if (avctx->hwaccel && s->cur_frame.f->buf[0]) {
                 ret = avctx->hwaccel->end_frame(avctx);
                 if (ret < 0) {
@@ -1375,8 +1389,9 @@  static int av1_decode_frame(AVCodecContext *avctx, AVFrame *frame,
                 goto end;
             }
 
+            ret = 0;
             if (s->raw_frame_header->show_frame && s->cur_frame.f->buf[0]) {
-                ret = set_output_frame(avctx, frame, pkt, got_frame);
+                ret = set_output_frame(avctx, frame);
                 if (ret < 0) {
                     av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
                     goto end;
@@ -1384,13 +1399,36 @@  static int av1_decode_frame(AVCodecContext *avctx, AVFrame *frame,
             }
             raw_tile_group = NULL;
             s->raw_frame_header = NULL;
+            if (show_frame) {
+                i++;
+                goto end;
+            }
         }
     }
 
+    ret = 1;
 end:
-    ff_cbs_fragment_reset(&s->current_obu);
-    if (ret < 0)
+    av_assert0(i <= s->current_obu.nb_units);
+    s->nb_unit = i;
+
+    if (ret < 0 || s->current_obu.nb_units == i) {
         s->raw_frame_header = NULL;
+        av_packet_unref(s->pkt);
+        ff_cbs_fragment_reset(&s->current_obu);
+        s->nb_unit = 0;
+    }
+
+    return ret;
+}
+
+static int av1_receive_frame(AVCodecContext *avctx, AVFrame *frame)
+{
+    int ret;
+
+    do {
+        ret = av1_receive_frame_internal(avctx, frame);
+    } while (ret > 0);
+
     return ret;
 }
 
@@ -1404,6 +1442,7 @@  static void av1_decode_flush(AVCodecContext *avctx)
 
     av1_frame_unref(avctx, &s->cur_frame);
     s->operating_point_idc = 0;
+    s->nb_unit = 0;
     s->raw_frame_header = NULL;
     s->raw_seq = NULL;
     s->cll = NULL;
@@ -1411,6 +1450,7 @@  static void av1_decode_flush(AVCodecContext *avctx)
     while (av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0)
         av_buffer_unref(&itut_t35.payload_ref);
 
+    ff_cbs_fragment_reset(&s->current_obu);
     ff_cbs_flush(s->cbc);
 }
 
@@ -1437,14 +1477,13 @@  const FFCodec ff_av1_decoder = {
     .priv_data_size        = sizeof(AV1DecContext),
     .init                  = av1_decode_init,
     .close                 = av1_decode_free,
-    FF_CODEC_DECODE_CB(av1_decode_frame),
+    FF_CODEC_RECEIVE_FRAME_CB(av1_receive_frame),
     .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
     .caps_internal         = FF_CODEC_CAP_INIT_CLEANUP |
                              FF_CODEC_CAP_SETS_PKT_DTS,
     .flush                 = av1_decode_flush,
     .p.profiles            = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
     .p.priv_class          = &av1_class,
-    .bsfs                  = "av1_frame_split",
     .hw_configs            = (const AVCodecHWConfigInternal *const []) {
 #if CONFIG_AV1_DXVA2_HWACCEL
         HWACCEL_DXVA2(av1),
diff --git a/libavcodec/av1dec.h b/libavcodec/av1dec.h
index cef899f81f..59ffed1d9b 100644
--- a/libavcodec/av1dec.h
+++ b/libavcodec/av1dec.h
@@ -28,6 +28,7 @@ 
 #include "libavutil/frame.h"
 #include "libavutil/pixfmt.h"
 #include "avcodec.h"
+#include "packet.h"
 #include "cbs.h"
 #include "cbs_av1.h"
 
@@ -68,6 +69,7 @@  typedef struct AV1DecContext {
     enum AVPixelFormat pix_fmt;
     CodedBitstreamContext *cbc;
     CodedBitstreamFragment current_obu;
+    AVPacket *pkt;
 
     AVBufferRef *seq_ref;
     AV1RawSequenceHeader *raw_seq;
@@ -90,6 +92,8 @@  typedef struct AV1DecContext {
     AV1Frame ref[AV1_NUM_REF_FRAMES];
     AV1Frame cur_frame;
 
+    int nb_unit;
+
     // AVOptions
     int operating_point;
 } AV1DecContext;