@@ -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"
@@ -1169,6 +1169,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
@@ -60,6 +60,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"
new file mode 100644
@@ -0,0 +1,227 @@
+/*
+ * 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"
+
+#define IS_SLICE(nut) (nut <= VVC_RASL_NUT || (nut >= VVC_IDR_W_RADL && nut <= VVC_GDR_NUT))
+#define IS_IDR(nut) (nut == VVC_IDR_W_RADL || nut == VVC_IDR_N_LP)
+
+typedef struct VVCMetadataContext {
+ CBSBSFContext common;
+
+ H266RawAUD aud_nal;
+
+ int aud;
+
+ uint8_t prev_layer_id;
+ int prev_tid0_poc;
+ int prev_poc;
+} VVCMetadataContext;
+
+//8.3.1 Decoding process for picture order count.
+//VTM did not follow the spec, and it's much simpler than spec.
+//We follow the VTM.
+static int get_slice_poc(AVBSFContext *bsf, const H266RawPH *ph,
+ const H266RawSliceHeader *slice, int *poc)
+{
+ VVCMetadataContext *ctx = bsf->priv_data;
+ CodedBitstreamH266Context *h266 = ctx->common.input->priv_data;
+ int poc_msb, max_poc_lsb, poc_lsb;
+ const H266RawSPS* sps = h266->active_sps;
+
+ if (!sps) {
+ av_log(bsf, AV_LOG_ERROR, "no pps for pu.\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ max_poc_lsb = 1 << (sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4);
+ poc_lsb = ph->ph_pic_order_cnt_lsb;
+ if (IS_IDR(slice->nal_unit_header.nal_unit_type)) {
+ if (ph->ph_poc_msb_cycle_present_flag)
+ poc_msb = ph->ph_poc_msb_cycle_val * max_poc_lsb;
+ else
+ poc_msb = 0;
+ } else {
+ int prev_poc = ctx->prev_tid0_poc;
+ int prev_poc_lsb = prev_poc & (max_poc_lsb - 1);
+ int prev_poc_msb = prev_poc - prev_poc_lsb;
+ if (ph->ph_poc_msb_cycle_present_flag) {
+ poc_msb = ph->ph_poc_msb_cycle_val * max_poc_lsb;
+ } else {
+ if ((poc_lsb < prev_poc_lsb) && ((prev_poc_lsb - poc_lsb) >= (max_poc_lsb / 2)))
+ poc_msb = prev_poc_msb + max_poc_lsb;
+ else if ((poc_lsb > prev_poc_lsb) && ((poc_lsb - prev_poc_lsb) > (max_poc_lsb / 2)))
+ poc_msb = prev_poc_msb - max_poc_lsb;
+ else
+ poc_msb = prev_poc_msb;
+ }
+ }
+
+ *poc = poc_msb + poc_lsb;
+ return 0;
+}
+
+static int is_au_start(AVBSFContext *bsf, CodedBitstreamFragment *pu)
+{
+ //7.4.2.4.3
+ VVCMetadataContext *ctx = bsf->priv_data;
+ CodedBitstreamH266Context *h266 = ctx->common.input->priv_data;
+ int i, ret;
+ const H266RawNALUnitHeader *nal;
+ const H266RawPH *ph = h266->ph;
+ const H266RawSlice *slice = NULL;
+ int poc, nut;
+
+ if (!ph) {
+ av_log(bsf, AV_LOG_ERROR, "can't find picture header in picutre unit.\n");
+ return AVERROR_INVALIDDATA;
+ }
+ for (i = 0; i < pu->nb_units; i++) {
+ nal = pu->units[i].content;
+ if (!nal)
+ continue;
+ if (IS_SLICE(nal->nal_unit_type)) {
+ slice = pu->units[i].content;
+ break;
+ }
+ }
+ if (!slice) {
+ av_log(bsf, AV_LOG_ERROR, "can't find first slice.\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ if ((ret = get_slice_poc(bsf, ph, &slice->header, &poc)) < 0)
+ return ret;
+
+ ret = nal->nuh_layer_id <= ctx->prev_layer_id || poc != ctx->prev_poc;
+ ctx->prev_layer_id = nal->nuh_layer_id;
+ ctx->prev_poc = poc;
+
+ nut = nal->nal_unit_type;
+ if (nal->nuh_temporal_id_plus1 == 1 &&
+ !ph->ph_non_ref_pic_flag && nut != VVC_RADL_NUT && nut != VVC_RASL_NUT) {
+ ctx->prev_tid0_poc = poc;
+ }
+ return ret;
+}
+
+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 {
+ if (ctx->aud == BSF_ELEMENT_INSERT && is_au_start(bsf, pu)) {
+ H266RawAUD *aud = &ctx->aud_nal;
+ int pic_type = 0, temporal_id = 8, layer_id = 0;
+ CodedBitstreamH266Context *h266 = ctx->common.input->priv_data;
+ 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)
+{
+ VVCMetadataContext *ctx = bsf->priv_data;
+ ctx->prev_layer_id = UINT8_MAX;
+ ctx->prev_poc = INT_MAX;
+ ctx->prev_tid0_poc = INT_MAX;
+ 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,
+};