diff mbox series

[FFmpeg-devel,4/4] avcodec/av1dec: add an option to select an operating point

Message ID 20200920172443.4763-4-jamrial@gmail.com
State Superseded
Headers show
Series [FFmpeg-devel,1/4] avcodec/cbs: add an AVClass to CodedBitstreamType for option handling | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

James Almer Sept. 20, 2020, 5:24 p.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/av1dec.c | 36 ++++++++++++++++++++++++++++++++++++
 libavcodec/av1dec.h |  7 +++++++
 2 files changed, 43 insertions(+)
diff mbox series

Patch

diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c
index 0bb04a3e44..178fbdc2f0 100644
--- a/libavcodec/av1dec.c
+++ b/libavcodec/av1dec.c
@@ -19,6 +19,7 @@ 
  */
 
 #include "libavutil/pixdesc.h"
+#include "libavutil/opt.h"
 #include "avcodec.h"
 #include "av1dec.h"
 #include "bytestream.h"
@@ -343,6 +344,7 @@  static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
     ff_thread_release_buffer(avctx, &f->tf);
     av_buffer_unref(&f->hwaccel_priv_buf);
     f->hwaccel_picture_private = NULL;
+    f->spatial_id = f->temporal_id = 0;
 }
 
 static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
@@ -360,6 +362,8 @@  static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *s
         dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
     }
 
+    dst->spatial_id = src->spatial_id;
+    dst->temporal_id = src->temporal_id;
     dst->loop_filter_delta_enabled = src->loop_filter_delta_enabled;
     memcpy(dst->loop_filter_ref_deltas,
            src->loop_filter_ref_deltas,
@@ -495,6 +499,8 @@  static av_cold int av1_decode_init(AVCodecContext *avctx)
     if (ret < 0)
         return ret;
 
+    av_opt_set_int(s->cbc->priv_data, "oppoint", s->operating_point, 0);
+
     if (avctx->extradata && avctx->extradata_size) {
         ret = ff_cbs_read(s->cbc, &s->current_obu, avctx->extradata,
                           avctx->extradata_size);
@@ -578,6 +584,11 @@  static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
     const AVFrame *srcframe = s->cur_frame.tf.f;
     int ret;
 
+    // TODO: all layers
+    if (s->operating_point_idc &&
+        av_log2(s->operating_point_idc >> 8) > s->cur_frame.spatial_id)
+        return 0;
+
     ret = av_frame_ref(frame, srcframe);
     if (ret < 0)
         return ret;
@@ -660,6 +671,8 @@  static int av1_decode_frame(AVCodecContext *avctx, void *frame,
     for (int i = 0; i < s->current_obu.nb_units; i++) {
         CodedBitstreamUnit *unit = &s->current_obu.units[i];
         AV1RawOBU *obu = unit->content;
+        const AV1RawOBUHeader *header = &obu->header;
+        int op = 0;
         av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
 
         switch (unit->type) {
@@ -679,6 +692,10 @@  static int av1_decode_frame(AVCodecContext *avctx, void *frame,
                 goto end;
             }
 
+            if (s->operating_point <= s->raw_seq->operating_points_cnt_minus_1)
+                op = s->operating_point;
+            s->operating_point_idc = s->raw_seq->operating_point_idc[op];
+
             if (s->pix_fmt == AV_PIX_FMT_NONE) {
                 ret = get_pixel_format(avctx);
                 if (ret < 0) {
@@ -753,6 +770,9 @@  static int av1_decode_frame(AVCodecContext *avctx, void *frame,
                 goto end;
             }
 
+            s->cur_frame.spatial_id  = header->spatial_id;
+            s->cur_frame.temporal_id = header->temporal_id;
+
             if (avctx->hwaccel) {
                 ret = avctx->hwaccel->start_frame(avctx, unit->data,
                                                   unit->data_size);
@@ -846,6 +866,21 @@  static void av1_decode_flush(AVCodecContext *avctx)
     s->raw_seq = NULL;
 }
 
+#define OFFSET(x) offsetof(AV1DecContext, x)
+#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
+static const AVOption av1_options[] = {
+    { "oppoint",  "Select an operating point of the scalable bitstream", OFFSET(operating_point),
+                  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, AV1_MAX_OPERATING_POINTS - 1, VD },
+    { NULL }
+};
+
+static const AVClass av1_class = {
+    .class_name = "AV1 decoder",
+    .item_name  = av_default_item_name,
+    .option     = av1_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 AVCodec ff_av1_decoder = {
     .name                  = "av1",
     .long_name             = NULL_IF_CONFIG_SMALL("Alliance for Open Media AV1"),
@@ -861,6 +896,7 @@  AVCodec ff_av1_decoder = {
                              FF_CODEC_CAP_SETS_PKT_DTS,
     .flush                 = av1_decode_flush,
     .profiles              = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
+    .priv_class            = &av1_class,
     .hw_configs            = (const AVCodecHWConfigInternal * []) {
         NULL
     },
diff --git a/libavcodec/av1dec.h b/libavcodec/av1dec.h
index 3604870299..67ae4c2c9f 100644
--- a/libavcodec/av1dec.h
+++ b/libavcodec/av1dec.h
@@ -36,6 +36,9 @@  typedef struct AV1Frame {
     AVBufferRef *hwaccel_priv_buf;
     void *hwaccel_picture_private;
 
+    int temporal_id;
+    int spatial_id;
+
     uint8_t loop_filter_delta_enabled;
     int8_t  loop_filter_ref_deltas[AV1_NUM_REF_FRAMES];
     int8_t  loop_filter_mode_deltas[2];
@@ -67,9 +70,13 @@  typedef struct AV1DecContext {
     uint16_t tg_start;
     uint16_t tg_end;
 
+    int operating_point_idc;
+
     AV1Frame ref[AV1_NUM_REF_FRAMES];
     AV1Frame cur_frame;
 
+    // AVOptions
+    int operating_point;
 } AV1DecContext;
 
 #endif /* AVCODEC_AV1DEC_H */