From: Thomas Siedel <thomas.ff@spin-digital.com> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH v6 04/11] avcodec: add h266_metadata_bsf support for H266/VVC Date: Fri, 10 Feb 2023 18:40:59 +0100 Message-ID: <20230210174106.44514-5-thomas.ff@spin-digital.com> (raw) In-Reply-To: <20230210174106.44514-1-thomas.ff@spin-digital.com> From: Nuo Mi <nuomi2021@gmail.com> 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, +}; -- 2.25.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2023-02-10 17:42 UTC|newest] Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-02-10 17:40 [FFmpeg-devel] [PATCH v6 00/11] Add " Thomas Siedel 2023-02-10 17:40 ` [FFmpeg-devel] [PATCH v6 01/11] avcodec: add enum types " Thomas Siedel 2023-02-10 17:40 ` [FFmpeg-devel] [PATCH v6 02/11] avcodec: add cbs " Thomas Siedel 2023-02-10 17:40 ` [FFmpeg-devel] [PATCH v6 03/11] avcodec: add bitstream parser " Thomas Siedel 2023-02-10 17:40 ` Thomas Siedel [this message] 2023-02-10 17:41 ` [FFmpeg-devel] [PATCH v6 05/11] avcodec: add MP4 to annexb support " Thomas Siedel 2023-02-10 17:41 ` [FFmpeg-devel] [PATCH v6 06/11] avformat: add demuxer and probe " Thomas Siedel 2023-02-10 17:41 ` [FFmpeg-devel] [PATCH v6 07/11] avformat: add muxer " Thomas Siedel 2023-02-10 17:41 ` [FFmpeg-devel] [PATCH v6 08/11] avcodec: add external decoder libvvdec " Thomas Siedel 2023-02-10 17:41 ` [FFmpeg-devel] [PATCH v6 09/11] avcodec: add external encoder libvvenc " Thomas Siedel 2023-02-10 17:41 ` [FFmpeg-devel] [PATCH v6 10/11] avformat: add ts stream types " Thomas Siedel 2023-02-10 17:41 ` [FFmpeg-devel] [PATCH v6 11/11] avcodec: increase minor version " Thomas Siedel
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230210174106.44514-5-thomas.ff@spin-digital.com \ --to=thomas.ff@spin-digital.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git