diff mbox series

[FFmpeg-devel,v6,9/9] avcodec: add vvc metadata bsf

Message ID 20210217015146.19724-10-nuomi2021@gmail.com
State Superseded
Headers show
Series add vvc raw demuxer, muxer, parser, metadata bsf
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Nuo Mi Feb. 17, 2021, 1:51 a.m. UTC
We run following command with all 265 VTM-11.0 clips:
ffmpeg -i in.bit  -c:v copy -bsf vvc_metadata -f vvc out.bit

The output bitstream can get same yuv as original.
---
 configure                      |   1 +
 libavcodec/Makefile            |   1 +
 libavcodec/bitstream_filters.c |   1 +
 libavcodec/h266_metadata_bsf.c | 129 +++++++++++++++++++++++++++++++++
 4 files changed, 132 insertions(+)
 create mode 100644 libavcodec/h266_metadata_bsf.c
diff mbox series

Patch

diff --git a/configure b/configure
index 24463595d2..c3121fe25f 100755
--- a/configure
+++ b/configure
@@ -3184,6 +3184,7 @@  mjpeg2jpeg_bsf_select="jpegtables"
 mpeg2_metadata_bsf_select="cbs_mpeg2"
 trace_headers_bsf_select="cbs"
 vp9_metadata_bsf_select="cbs_vp9"
+vvc_metadata_bsf_select="cbs_h266"
 
 # external libraries
 aac_at_decoder_deps="audiotoolbox"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 5adb96c8df..a2b68969c0 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1173,6 +1173,7 @@  OBJS-$(CONFIG_VP9_METADATA_BSF)           += vp9_metadata_bsf.o
 OBJS-$(CONFIG_VP9_RAW_REORDER_BSF)        += vp9_raw_reorder_bsf.o
 OBJS-$(CONFIG_VP9_SUPERFRAME_BSF)         += vp9_superframe_bsf.o
 OBJS-$(CONFIG_VP9_SUPERFRAME_SPLIT_BSF)   += vp9_superframe_split_bsf.o
+OBJS-$(CONFIG_VVC_METADATA_BSF)           += h266_metadata_bsf.o
 
 # thread libraries
 OBJS-$(HAVE_LIBC_MSVCRT)               += file_open.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index 02d33abb18..e642bffa47 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -61,6 +61,7 @@  extern const AVBitStreamFilter ff_vp9_metadata_bsf;
 extern const AVBitStreamFilter ff_vp9_raw_reorder_bsf;
 extern const AVBitStreamFilter ff_vp9_superframe_bsf;
 extern const AVBitStreamFilter ff_vp9_superframe_split_bsf;
+extern const AVBitStreamFilter ff_vvc_metadata_bsf;
 
 #include "libavcodec/bsf_list.c"
 
diff --git a/libavcodec/h266_metadata_bsf.c b/libavcodec/h266_metadata_bsf.c
new file mode 100644
index 0000000000..50d618205f
--- /dev/null
+++ b/libavcodec/h266_metadata_bsf.c
@@ -0,0 +1,129 @@ 
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/common.h"
+#include "libavutil/opt.h"
+
+#include "bsf.h"
+#include "bsf_internal.h"
+#include "cbs.h"
+#include "cbs_bsf.h"
+#include "cbs_h266.h"
+#include "vvc.h"
+
+typedef struct VVCMetadataContext {
+    CBSBSFContext common;
+
+    H266RawAUD aud_nal;
+
+    int aud;
+} VVCMetadataContext;
+
+static int h266_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
+                                         CodedBitstreamFragment *pu)
+{
+    VVCMetadataContext *ctx = bsf->priv_data;
+    int err, i;
+
+    // If an AUD is present, it must be the first NAL unit.
+    if (pu->units[0].type == VVC_AUD_NUT) {
+        if (ctx->aud == BSF_ELEMENT_REMOVE)
+            ff_cbs_delete_unit(pu, 0);
+    } else {
+        CodedBitstreamH266Context *h266 = ctx->common.input->priv_data;
+        if (ctx->aud == BSF_ELEMENT_INSERT && h266_is_au_start(h266, pu, bsf) > 0) {
+            H266RawAUD *aud = &ctx->aud_nal;
+            int pic_type = 0, temporal_id = 8, layer_id = 0;
+            for (i = 0; i < pu->nb_units; i++) {
+                const H266RawNALUnitHeader *nal = pu->units[i].content;
+                if (!nal)
+                    continue;
+                if (nal->nuh_temporal_id_plus1 < temporal_id + 1)
+                    temporal_id = nal->nuh_temporal_id_plus1 - 1;
+
+                if (pu->units[i].type <= VVC_RSV_IRAP_11) {
+                    const H266RawSlice *slice = pu->units[i].content;
+                    layer_id = nal->nuh_layer_id;
+                    if (slice->header.sh_slice_type == VVC_SLICE_TYPE_B &&
+                        pic_type < 2)
+                        pic_type = 2;
+                    if (slice->header.sh_slice_type == VVC_SLICE_TYPE_P &&
+                        pic_type < 1)
+                        pic_type = 1;
+                }
+            }
+
+            aud->nal_unit_header = (H266RawNALUnitHeader) {
+                .nal_unit_type         = VVC_AUD_NUT,
+                .nuh_layer_id          = layer_id,
+                .nuh_temporal_id_plus1 = temporal_id + 1,
+            };
+            aud->aud_pic_type = pic_type;
+            aud->aud_irap_or_gdr_flag = h266->ph->ph_gdr_or_irap_pic_flag;
+
+            err = ff_cbs_insert_unit_content(pu, 0, VVC_AUD_NUT, aud, NULL);
+            if (err < 0) {
+                av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
+                return err;
+            }
+        }
+    }
+    return 0;
+}
+
+static const CBSBSFType h266_metadata_type = {
+    .codec_id        = AV_CODEC_ID_VVC,
+    .fragment_name   = "picture unit",
+    .unit_name       = "NAL unit",
+    .update_fragment = &h266_metadata_update_fragment,
+};
+
+static int vvc_metadata_init(AVBSFContext *bsf)
+{
+    return ff_cbs_bsf_generic_init(bsf, &h266_metadata_type);
+}
+
+#define OFFSET(x) offsetof(VVCMetadataContext, x)
+#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
+static const AVOption vvc_metadata_options[] = {
+    BSF_ELEMENT_OPTIONS_PIR("aud", "Access Unit Delimiter NAL units",
+                            aud, FLAGS),
+
+    { NULL }
+};
+
+static const AVClass vvc_metadata_class = {
+    .class_name = "vvc_metadata_bsf",
+    .item_name  = av_default_item_name,
+    .option     = vvc_metadata_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+static const enum AVCodecID vvc_metadata_codec_ids[] = {
+    AV_CODEC_ID_VVC, AV_CODEC_ID_NONE,
+};
+
+const AVBitStreamFilter ff_vvc_metadata_bsf = {
+    .name           = "vvc_metadata",
+    .priv_data_size = sizeof(VVCMetadataContext),
+    .priv_class     = &vvc_metadata_class,
+    .init           = &vvc_metadata_init,
+    .close          = &ff_cbs_bsf_generic_close,
+    .filter         = &ff_cbs_bsf_generic_filter,
+    .codec_ids      = vvc_metadata_codec_ids,
+};