From patchwork Thu Feb 11 14:36:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuo Mi X-Patchwork-Id: 25562 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id EC25A44B34F for ; Thu, 11 Feb 2021 16:37:28 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D731D68AA6E; Thu, 11 Feb 2021 16:37:28 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9E4A768AA25 for ; Thu, 11 Feb 2021 16:37:26 +0200 (EET) Received: by mail-pf1-f182.google.com with SMTP id m6so3812928pfk.1 for ; Thu, 11 Feb 2021 06:37:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JCEExfCZPYcs0WJBRRGcSers/Kx5ONRQlvfvnw8HOvc=; b=JIKq5n2HtFeCQfaH2bXPkT39ioWv7TTgguNxHoXRm3FWhUqtmXQ2fSAPaehbJf0BBo tEbdB5GxxVDqs0BYV+SGDeS/yvo1OMK4aLi8ZKbVIt+7sVO52gEui7iNYB/l0gJqvBZq Wkiu2oUFK6Fewy8N19i9jDeZr34wHD4S/6QjwpXYNdegdrWHKCQ+BJ+pHdfntnykNzwC 7HZhmQaLjkpKZ7bE95sE9jxqUUP7Q3QcicB1YOSpprMdY0WRADSZN6zQAI53VAz6+gGU LfB0UdwWdaZHP187YtgdpVxUVfxtD8RF7uCTdikLt5OotEa3vOthTXPdqX3Ee4mxwi/u C+Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JCEExfCZPYcs0WJBRRGcSers/Kx5ONRQlvfvnw8HOvc=; b=AuqMASnsCZ78D8q4MZemUiXusgqbf+QMHIm+eL+gSsLMeCyl7TW8twcI4ASPrW6okt 9bI6828Pr3a/iY9vkG3d4T6SKtXaiwJ281o2rqWBQqyHWraN47gDOccy30B84v7cZJbq C3EbBhjB24lc0jN8I2+e66rHqbBYd0whb+/VYGRpvS+eRqEcs6RSHS5fXHPE+tKPoeT8 UsthEPJDC/4eH3vxurokjdduP6tr/XmlQbnTt0EvBhWNtLewpFfoFXDG9S/rqaChuKa1 UDR1ahE4SC3uB61ifRK0OgiWpuwcZrsgKFOflDsdyyuxzWm16cSV5AgbKPNrsELOhgHH rbNQ== X-Gm-Message-State: AOAM532mmQE1xwrjpOFT9JC5j7bnY/q7W5jDFt7OkJ839t9x+fO6yw/4 LUpG806iszmedbbbbdp1AGtvxlv1ods= X-Google-Smtp-Source: ABdhPJyCOdVLGpoqvxLQzmnHEhBqM3wIGv9EHXgy8i4AQJxY91pd0kfxy4z95xOxr91qXtXo8t9aSw== X-Received: by 2002:a63:1c09:: with SMTP id c9mr8343419pgc.185.1613054244559; Thu, 11 Feb 2021 06:37:24 -0800 (PST) Received: from Nuomi.localdomain (23.83.245.51.16clouds.com. [23.83.245.51]) by smtp.gmail.com with ESMTPSA id p29sm6564587pgm.64.2021.02.11.06.37.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Feb 2021 06:37:24 -0800 (PST) From: Nuo Mi To: ffmpeg-devel@ffmpeg.org Date: Thu, 11 Feb 2021 22:36:11 +0800 Message-Id: <20210211143611.5663-11-nuomi2021@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210211143611.5663-1-nuomi2021@gmail.com> References: <20210125141507.11012-1-nuomi2021@gmail.com> <20210211143611.5663-1-nuomi2021@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 10/10] avcodec: add vvc metadata bsf X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Nuo Mi Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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 the origin. --- 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 --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 65f81f5b68..6764a8f73c 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1172,6 +1172,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 b26d6a910e..001a7bb3a4 100644 --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@ -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" 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, +};