From patchwork Fri Feb 10 17:40:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Siedel X-Patchwork-Id: 40352 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:5494:b0:bf:7b3a:fd32 with SMTP id i20csp1565718pzk; Fri, 10 Feb 2023 09:42:30 -0800 (PST) X-Google-Smtp-Source: AK7set+V4yQcLjKaKGfiez8qnDkJxxc3XAJs1px5CE4Rgp4VL3VAtIDxsyQe9o8lwHE2LwPZ/bW6 X-Received: by 2002:a17:906:c188:b0:878:a893:48db with SMTP id g8-20020a170906c18800b00878a89348dbmr15379986ejz.65.1676050950220; Fri, 10 Feb 2023 09:42:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676050950; cv=none; d=google.com; s=arc-20160816; b=enB6gupKH7Y3NXdcRzyYjrmd7T6y6eCBdAFxursDHLZoHNRkmCTwdjtvp6AFchIAzs bJQztrXqGp2OhKYv8XWb89JhRiv+PMvHbXtZeOT+ZVuTUEH04pbMmR7hYsfM0mJyhDxr Ec5GOx/rb6DGTRNJ2YKTjh80z1DaaLWJhQYWDtbYYWPkzZcEZpMMsMmkEhE6R2g6wM/R XibYeEgGCYsCnrHdYWnXj7RSYg1+yB2pW1mxXcAF2J9Dv6kbZJPJD/kS5NJxTTzBqBAZ PJHD66ciHjLjVT1uCXlptmIJ8yGiLrIcqNv1PCfQjX78L8Zo72/8J+2gSTaJ2jUO+Fl/ 7aQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=3n4YQx6NG0Y3bMj4c+jH98kZzcTGSoJzID+mayo+lBc=; b=hMNKJPmQxrUdfeJ+5gJzK5zIAdEdSE8sGfXzVuTV/oGpt6ynpD3FBaCtMRPpbI0yey Vmkfn2+9UF5pvcOfaB0cVpZNWmbC1gqbr5p8Qcdvxa5SY1uKzYguyqlTVOjgfJOwbo3F 9+/BLCVXISc2CHGhKbuUu+y0/EBZHYTJi8woRSQ+mWEAujHvCvZBeYPs64blPar2l+AG XR3BnGwf5nyAD33bCShuBBKk8r5DWJXhltJi9ZNODqmsEUgn3oiMKEz8AcIvJHHFYZtn i9f7KOafOUiXPVJpiT44RfZr4sfgdwbRFsHKhfiOTSsdXFsKatrJ0G7md/H3cOjzgXf9 PWtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@spin-digital-com.20210112.gappssmtp.com header.s=20210112 header.b=nwCU9Uyu; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z29-20020a17090674dd00b0088f1e51d2b2si5983080ejl.52.2023.02.10.09.42.29; Fri, 10 Feb 2023 09:42:30 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@spin-digital-com.20210112.gappssmtp.com header.s=20210112 header.b=nwCU9Uyu; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C9E4668B6B7; Fri, 10 Feb 2023 19:41:49 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8825968BD1D for ; Fri, 10 Feb 2023 19:41:41 +0200 (EET) Received: by mail-wr1-f50.google.com with SMTP id r2so5790163wrv.7 for ; Fri, 10 Feb 2023 09:41:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=spin-digital-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=NulldXcBsB5Gld75I/TjlO/JUGDd57U49mdDtgd3mDY=; b=nwCU9UyuKekhiyjFGnzgtb9GjDXAE+Gha+GH4H0+zg4aNOIVDe1MwtgY5U/ffVBbcJ L5obH4Zr30FEOLh2OCTv+yL3u21+1Or38PH1BYIWKx/nFOQFLuiXX9+WcfQwuobgX/tt HURr50kD2xmUxcN6r2Ebq5biBKTkwg++5Z7U49IHPL/IJBHXhih9rJXYpQQ7kz7EuQ83 Wk1ZgiwDtmKzgcabFTUptALLUUdHlWjoimqAr4pcbXkJvOzN6QgP8TyBqL9+6kqu3xGC LAhNGpf+6DxWm2Vosc5FZub5l46RRNpvvEQPrO7DJfF40yUnHwjlJ5U3R1WnwNW6KZ7p 9V/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NulldXcBsB5Gld75I/TjlO/JUGDd57U49mdDtgd3mDY=; b=qg0b884fLmu5iMMZgPfGmuGnk3QBR1Je3+b9T0tOALJynRfYXzxiZsE5zuuZjlBVHV w/AsRjQIoWgzfyqKno3HFMq8ojMNKreUljZjYnoXlg/YQ+XPKod2YL3D1kmiFtfjtDdX rd9AebmQK6XRoOKvFmT941DWhNyhS5KlPSAz2FAtJ/Z2jN85Q4+9x/DX+rcKHa8skZer w8DtfYbGBoSkhV5yRemDsQNQBP8DrdeXP5CiikDKhAXn9Da5Mw9uXH4y6fb1QKwa5hm3 YJjt0V3FiWcDgj1Lnc/icVjaJF1XK7TJll7kG3Om9/FbhkmsrNgXqvxv2b9rwLyrPXAC kXWQ== X-Gm-Message-State: AO0yUKXPaAIeef62qjT1RhMHMzcjK6RTD+cmckLBmH1UphkWviTHzmwJ ohuKDu/zieMjSTOvUw6jiM89N7sMycPSVwhH X-Received: by 2002:a05:6000:1b0e:b0:2bf:e05f:53ac with SMTP id f14-20020a0560001b0e00b002bfe05f53acmr15624572wrz.45.1676050900529; Fri, 10 Feb 2023 09:41:40 -0800 (PST) Received: from thomas-win.localdomain ([213.138.44.237]) by smtp.gmail.com with ESMTPSA id w13-20020a5d608d000000b002c54b6382c8sm1589245wrt.82.2023.02.10.09.41.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Feb 2023 09:41:40 -0800 (PST) From: Thomas Siedel To: ffmpeg-devel@ffmpeg.org Date: Fri, 10 Feb 2023 18:40:59 +0100 Message-Id: <20230210174106.44514-5-thomas.ff@spin-digital.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230210174106.44514-1-thomas.ff@spin-digital.com> References: <20230210174106.44514-1-thomas.ff@spin-digital.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v6 04/11] avcodec: add h266_metadata_bsf support for H266/VVC X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 8rECnDYzzGCw From: Nuo Mi Add H.266/VVC metadata bsf. --- configure | 1 + libavcodec/Makefile | 1 + libavcodec/bitstream_filters.c | 1 + libavcodec/h266_metadata_bsf.c | 147 +++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 libavcodec/h266_metadata_bsf.c diff --git a/configure b/configure index f375ec2f5e..e50ce5a484 100755 --- a/configure +++ b/configure @@ -3283,6 +3283,7 @@ filter_units_bsf_select="cbs" h264_metadata_bsf_deps="const_nan" h264_metadata_bsf_select="cbs_h264" h264_redundant_pps_bsf_select="cbs_h264" +h266_metadata_bsf_select="cbs_h266" hevc_metadata_bsf_select="cbs_h265" mjpeg2jpeg_bsf_select="jpegtables" mpeg2_metadata_bsf_select="cbs_mpeg2" diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 2cf4300575..4029e4f9e0 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1218,6 +1218,7 @@ OBJS-$(CONFIG_H264_METADATA_BSF) += h264_metadata_bsf.o h264_levels.o \ h2645data.o OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF) += h264_mp4toannexb_bsf.o OBJS-$(CONFIG_H264_REDUNDANT_PPS_BSF) += h264_redundant_pps_bsf.o +OBJS-$(CONFIG_H266_METADATA_BSF) += h266_metadata_bsf.o OBJS-$(CONFIG_HAPQA_EXTRACT_BSF) += hapqa_extract_bsf.o hap.o OBJS-$(CONFIG_HEVC_METADATA_BSF) += h265_metadata_bsf.o h265_profile_level.o \ h2645data.o diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c index e8216819ca..848f430014 100644 --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@ -39,6 +39,7 @@ extern const FFBitStreamFilter ff_filter_units_bsf; extern const FFBitStreamFilter ff_h264_metadata_bsf; extern const FFBitStreamFilter ff_h264_mp4toannexb_bsf; extern const FFBitStreamFilter ff_h264_redundant_pps_bsf; +extern const FFBitStreamFilter ff_h266_metadata_bsf; extern const FFBitStreamFilter ff_hapqa_extract_bsf; extern const FFBitStreamFilter ff_hevc_metadata_bsf; extern const FFBitStreamFilter ff_hevc_mp4toannexb_bsf; diff --git a/libavcodec/h266_metadata_bsf.c b/libavcodec/h266_metadata_bsf.c new file mode 100644 index 0000000000..05e476071f --- /dev/null +++ b/libavcodec/h266_metadata_bsf.c @@ -0,0 +1,147 @@ +/* + * 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 "h266.h" + +typedef struct H266MetadataContext { + CBSBSFContext common; + + H266RawAUD aud_nal; + + int aud; +} H266MetadataContext; + +static int h266_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt, + CodedBitstreamFragment *pu) +{ + H266MetadataContext *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 ( pkt && ctx->aud == BSF_ELEMENT_INSERT) { + const H266RawSlice *first_slice = NULL; + const H266RawPH *ph = NULL; + 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 ( nal->nal_unit_type == VVC_PH_NUT ) { + ph = pu->units[i].content; + } else if (IS_H266_SLICE(nal->nal_unit_type)) { + 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; + if (!first_slice) { + first_slice = slice; + if (first_slice->header. + sh_picture_header_in_slice_header_flag) + ph = &first_slice->header.sh_picture_header; + else if (!ph) + break; + } + } + } + if (!ph) { + av_log(bsf, AV_LOG_ERROR, "no avaliable picture header"); + return AVERROR_INVALIDDATA; + } + + 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 = 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; + } + } + + /* TODO: implement more metadata parsing, like VUI, Levels etc. */ + //for (i = 0; i < pu->nb_units; i++) { + // if (pu->units[i].type == VVC_SPS_NUT) { + // } + //} + return 0; +} + +static const CBSBSFType h266_metadata_type = { + .codec_id = AV_CODEC_ID_VVC, + .fragment_name = "access unit", + .unit_name = "NAL unit", + .update_fragment = &h266_metadata_update_fragment, +}; + +static int h266_metadata_init(AVBSFContext *bsf) +{ + return ff_cbs_bsf_generic_init(bsf, &h266_metadata_type); +} + +#define OFFSET(x) offsetof(H266MetadataContext, x) +#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM) +static const AVOption h266_metadata_options[] = { + BSF_ELEMENT_OPTIONS_PIR("aud", "Access Unit Delimiter NAL units", + aud, FLAGS), + + { NULL } +}; + +static const AVClass h266_metadata_class = { + .class_name = "h266_metadata_bsf", + .item_name = av_default_item_name, + .option = h266_metadata_options, + .version = LIBAVUTIL_VERSION_INT, +}; + +static const enum AVCodecID h266_metadata_codec_ids[] = { + AV_CODEC_ID_VVC, AV_CODEC_ID_NONE, +}; + +const FFBitStreamFilter ff_h266_metadata_bsf = { + .p.name = "h266_metadata", + .p.codec_ids = h266_metadata_codec_ids, + .p.priv_class = &h266_metadata_class, + .priv_data_size = sizeof(H266MetadataContext), + .init = &h266_metadata_init, + .close = &ff_cbs_bsf_generic_close, + .filter = &ff_cbs_bsf_generic_filter, +};