From patchwork Tue Mar 21 15:01:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Siedel X-Patchwork-Id: 40757 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:d046:b0:cd:afd7:272c with SMTP id hv6csp2661012pzb; Tue, 21 Mar 2023 08:02:55 -0700 (PDT) X-Google-Smtp-Source: AK7set8DvqZHruJD6OKzWoJSWnADUDdvwjrNYFwohkOtH2XOESPaF0ZTWIw3pbqOVmoOc81WPY6k X-Received: by 2002:a17:906:3913:b0:8dd:76d5:a82 with SMTP id f19-20020a170906391300b008dd76d50a82mr3249280eje.47.1679410975614; Tue, 21 Mar 2023 08:02:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679410975; cv=none; d=google.com; s=arc-20160816; b=vlp2udYFuNxm2tQ4GwrrVevfDI2B9bOFr7oaXOB4z7T/W3t/hxqD6ITn8ZaMPl/RlG Qd1wq1h/bmRP6LII0BV17ziMQaE6ulQWiiXAr08CaYEkSrAZSwongrf1o0FWw4W9RH9F 111c/1SLLkXb6xXjvnSwVl6tw9czGoXrpQ3Ljcz+CN23JfByqtZgBSP1ioejBDYI+mIR knRHPUtMBQ/mMGoCUv3NFyhMOJVTyEv6ZCUwHcJ+92VxhKQKJ4OlKKCQV9MCQSATjrVY PYCVo5zFl+IBYy/Xvce09nez5i34cpJ0CIiO8hv0aQ6r0fl8ffLt9SKJTb9c71f9Aklv RUjQ== 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=MCPhBioGyL+iqA1RigPO1G9UCdj91g9NA8CnMlN8kJQ=; b=G485oEuDtN7yDiAFHCkevSvctO1UWegg+ij+3ntXWBanhcqXnYm+1WbibZtLKOsXRi jqx6KNUJmEtrdcHHSH01kB0HFqN2+Ql4J8ZJ3nopK1FBfuNACHi30A0NLsAO8ogkZOoo Bpz6LWYKfKm5t1gmS2L2yYYpGTt+nyKvBAJ76kfdqnV13M9vDTtXJJq3XQh5Y4ABR117 m94dSKI7lQYXHrlcBCj40RLKidjlHPqojvfB7X/CsNrjIGpBDbLvpZ8gPPy/P1Vs3DE0 AGFvwEUSihk6HpcsbERW1CeZN5/N8etA7cXHkUIjP8TgiZYzX0CIkWDerG6/zNxGjXI0 0KJA== 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=SOv4bw90; 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 q23-20020a1709064c9700b00931756b05b5si12289224eju.108.2023.03.21.08.02.54; Tue, 21 Mar 2023 08:02:55 -0700 (PDT) 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=SOv4bw90; 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 3901168C6A3; Tue, 21 Mar 2023 17:01:51 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f49.google.com (mail-ed1-f49.google.com [209.85.208.49]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6832F68C5A7 for ; Tue, 21 Mar 2023 17:01:42 +0200 (EET) Received: by mail-ed1-f49.google.com with SMTP id y4so60939356edo.2 for ; Tue, 21 Mar 2023 08:01:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=spin-digital-com.20210112.gappssmtp.com; s=20210112; t=1679410901; 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=NlzSXyzikR7xNViVc6Qa6MpgRBBp4Lp2Yc0PVx0+PhY=; b=SOv4bw90uT1RFTz3d7PxLHKr+1KrWy6z8nqSX4TBfrgI4axWSICGiogaJTEh5UpNd7 AgFTdHJaSlTCpMnu3brF/38qbbC/bB9A2hHHlhclKEBo3P3QGIliPAhiMojSwDiesKwo CV3WQdiWwLJqQ8mioflZuXuvBu9qXDDit+/1Y6UBnLJU2jEkyjKbEllTpBzcxCr+vXW8 zY6yXXrXHguYt9zKJcR0LRX9HpJvFmgpA2ad2qt7DIu6SV436tpq7MRDxID4JEeEtFFr KsJsMNOu4l23/peg+Kwa7hg7jpAiW4hRkdwUGgk891+CCR3Z/4ZmPihT4Uu1Qnfc4ivj Ka8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679410901; 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=NlzSXyzikR7xNViVc6Qa6MpgRBBp4Lp2Yc0PVx0+PhY=; b=eiEsWj+/puH+Fme4plhPUR9FbKskCCqW4f6eNcjTuuOIKZ0EVY9GPRGRMjMy7oDu2/ GCOjC9HNpVyAzYA5oyRTm5K4FvA67lazJyq5jo1zoNa4DOGulAQNvtLDtCyuxQsXO+nR zU0BCQstwt7EoEgrQJyKJJI4NQUWkontw8ejMFL7LP47zL26tzHhRaw6oLaVYLHmCgBS lfrZlE3ljLoW16n9+1wJYxti4CGn4uO8jMeIKRitTJr0LxBAk89lsv5SYdGNItyx+GRG N+FzZmxag1Pu7ul1eoar9OGMjPvTL1k4fqH1eAKwAXNQiG2TEomJJYFxoLHhVQYH63p/ 5rZg== X-Gm-Message-State: AO0yUKWR8w6vMOORpDti68PuRHXN6bk6r183lDFMlD9m4fG4bptDZwD7 2iPZKwiWlo3daFh9fnb5cWx0qFyq1ERg2Diy1G8= X-Received: by 2002:a05:6402:4d6:b0:501:d532:d84e with SMTP id n22-20020a05640204d600b00501d532d84emr3163685edw.39.1679410901341; Tue, 21 Mar 2023 08:01:41 -0700 (PDT) Received: from thomas-win.localdomain ([213.138.44.237]) by smtp.gmail.com with ESMTPSA id x93-20020a50bae6000000b004fbf6b35a56sm6405817ede.76.2023.03.21.08.01.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Mar 2023 08:01:40 -0700 (PDT) From: Thomas Siedel To: ffmpeg-devel@ffmpeg.org Date: Tue, 21 Mar 2023 16:01:18 +0100 Message-Id: <20230321150124.21999-6-thomas.ff@spin-digital.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230321150124.21999-1-thomas.ff@spin-digital.com> References: <20230321150124.21999-1-thomas.ff@spin-digital.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v7 05/11] avcodec: add MP4 to annexb 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: 51OWSCW1lvDI Add parser for H.266/VVC MP4 to Annex B byte stream format. --- libavcodec/Makefile | 1 + libavcodec/bitstream_filters.c | 1 + libavcodec/h266_mp4toannexb_bsf.c | 329 ++++++++++++++++++++++++++++++ 3 files changed, 331 insertions(+) create mode 100644 libavcodec/h266_mp4toannexb_bsf.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index c3989df358..81fda09597 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1220,6 +1220,7 @@ OBJS-$(CONFIG_H264_METADATA_BSF) += h264_metadata_bsf.o h264_levels.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_H266_MP4TOANNEXB_BSF) += h266_mp4toannexb_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 848f430014..e0e3116eb4 100644 --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@ -40,6 +40,7 @@ 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_h266_mp4toannexb_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_mp4toannexb_bsf.c b/libavcodec/h266_mp4toannexb_bsf.c new file mode 100644 index 0000000000..1f7bf0618d --- /dev/null +++ b/libavcodec/h266_mp4toannexb_bsf.c @@ -0,0 +1,329 @@ +/* + * H.266/VVC MP4 to Annex B byte stream format filter + * Copyright (c) 2022, Thomas Siedel + * + * 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 + +#include "libavutil/intreadwrite.h" +#include "libavutil/mem.h" + +#include "avcodec.h" +#include "bsf.h" +#include "bsf_internal.h" +#include "bytestream.h" +#include "h266.h" + +#include "libavcodec/get_bits.h" + +#define MIN_VVCC_LENGTH 23 + +typedef struct H266BSFContext { + uint8_t length_size; + int extradata_parsed; +} H266BSFContext; + +static int h266_extradata_to_annexb(AVBSFContext *ctx) +{ + GetByteContext gb; + int length_size, num_arrays, i, j; + int ret = 0; + int temp = 0; + int ptl_present; + + uint8_t *new_extradata = NULL; + size_t new_extradata_size = 0; + + int max_picture_width = 0; + int max_picture_height = 0; + int avg_frame_rate = 0; + + bytestream2_init(&gb, ctx->par_in->extradata, ctx->par_in->extradata_size); + temp = bytestream2_get_byte(&gb); + length_size = ((temp & 6) >> 1) + 1; + ptl_present = temp & 1; + if (ptl_present) { + int num_bytes_constraint_info; + int general_profile_idc; + int general_tier_flag; + int general_level_idc; + int ptl_frame_only_constraint_flag; + int ptl_multi_layer_enabled_flag; + int ptl_num_sub_profiles; + int temp3, temp4, temp5; + int temp2 = bytestream2_get_be16(&gb); + int ols_idx = (temp2 >> 7) & 0x1ff; + int num_sublayers = (temp2 >> 4) & 0x7; + int constant_frame_rate = (temp2 >> 2) & 0x3; + int chroma_format_idc = temp2 & 0x3; + int bit_depth_minus8 = (bytestream2_get_byte(&gb) >> 5) & 0x7; + av_log(ctx, AV_LOG_DEBUG, + "bit_depth_minus8 %d chroma_format_idc %d\n", bit_depth_minus8, + chroma_format_idc); + av_log(ctx, AV_LOG_DEBUG, "constant_frame_rate %d, ols_idx %d\n", + constant_frame_rate, ols_idx); + // VvcPTLRecord(num_sublayers) native_ptl + temp3 = bytestream2_get_byte(&gb); + num_bytes_constraint_info = (temp3) & 0x3f; + temp4 = bytestream2_get_byte(&gb); + general_profile_idc = (temp4 >> 1) & 0x7f; + general_tier_flag = (temp4) & 1; + general_level_idc = bytestream2_get_byte(&gb); + av_log(ctx, AV_LOG_DEBUG, + "general_profile_idc %d, general_tier_flag %d, general_level_idc %d, num_sublayers %d num_bytes_constraint_info %d\n", + general_profile_idc, general_tier_flag, general_level_idc, + num_sublayers, num_bytes_constraint_info); + + temp5 = bytestream2_get_byte(&gb); + ptl_frame_only_constraint_flag = (temp5 >> 7) & 0x1; + ptl_multi_layer_enabled_flag = (temp5 >> 6) & 0x1; + for (i = 0; i < num_bytes_constraint_info - 1; i++) { + // unsigned int(8*num_bytes_constraint_info - 2) general_constraint_info; + bytestream2_get_byte(&gb); + } + + av_log(ctx, AV_LOG_DEBUG, + "ptl_multi_layer_enabled_flag %d, ptl_frame_only_constraint_flag %d\n", + ptl_multi_layer_enabled_flag, ptl_frame_only_constraint_flag); + + if (num_sublayers > 1) { + int temp6 = bytestream2_get_byte(&gb); + uint8_t ptl_sublayer_level_present_flag[8] = { 0 }; + //uint8_t sublayer_level_idc[8] = {0}; + for (i = num_sublayers - 2; i >= 0; i--) { + ptl_sublayer_level_present_flag[i] = + (temp6 >> (7 - (num_sublayers - 2 - i))) & 0x01; + } + // for (j=num_sublayers; j<=8 && num_sublayers > 1; j++) + // bit(1) ptl_reserved_zero_bit = 0; + for (i = num_sublayers - 2; i >= 0; i--) { + if (ptl_sublayer_level_present_flag[i]) { + //sublayer_level_idc[i] = bytestream2_get_byte(&gb); + } + } + } + + ptl_num_sub_profiles = bytestream2_get_byte(&gb); + for (j = 0; j < ptl_num_sub_profiles; j++) { + // unsigned int(32) general_sub_profile_idc[j]; + bytestream2_get_be16(&gb); + bytestream2_get_be16(&gb); + } + + max_picture_width = bytestream2_get_be16(&gb); // unsigned_int(16) max_picture_width; + max_picture_height = bytestream2_get_be16(&gb); // unsigned_int(16) max_picture_height; + avg_frame_rate = bytestream2_get_be16(&gb); // unsigned int(16) avg_frame_rate; } + av_log(ctx, AV_LOG_DEBUG, + "max_picture_width %d, max_picture_height %d, avg_frame_rate %d\n", + max_picture_width, max_picture_height, avg_frame_rate); + } + + num_arrays = bytestream2_get_byte(&gb); + + for (i = 0; i < num_arrays; i++) { + int cnt; + int type = bytestream2_get_byte(&gb) & 0x1f; + + if (type == VVC_OPI_NUT || type == VVC_DCI_NUT) + cnt = 1; + else + cnt = bytestream2_get_be16(&gb); + + av_log(ctx, AV_LOG_DEBUG, "nalu_type %d cnt %d\n", type, cnt); + + if (!(type == VVC_OPI_NUT || type == VVC_DCI_NUT || + type == VVC_VPS_NUT || type == VVC_SPS_NUT || type == VVC_PPS_NUT + || type == VVC_PREFIX_SEI_NUT || type == VVC_SUFFIX_SEI_NUT)) { + av_log(ctx, AV_LOG_ERROR, + "Invalid NAL unit type in extradata: %d\n", type); + ret = AVERROR_INVALIDDATA; + goto fail; + } + + for (j = 0; j < cnt; j++) { + int nalu_len = bytestream2_get_be16(&gb); + + if (4 + AV_INPUT_BUFFER_PADDING_SIZE + nalu_len > + SIZE_MAX - new_extradata_size) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + ret = av_reallocp(&new_extradata, new_extradata_size + nalu_len + 4 + + AV_INPUT_BUFFER_PADDING_SIZE); + if (ret < 0) + goto fail; + + AV_WB32(new_extradata + new_extradata_size, 1); // add the startcode + bytestream2_get_buffer(&gb, new_extradata + new_extradata_size + 4, + nalu_len); + new_extradata_size += 4 + nalu_len; + memset(new_extradata + new_extradata_size, 0, + AV_INPUT_BUFFER_PADDING_SIZE); + } + } + + av_freep(&ctx->par_out->extradata); + ctx->par_out->extradata = new_extradata; + ctx->par_out->extradata_size = new_extradata_size; + + if (!new_extradata_size) + av_log(ctx, AV_LOG_WARNING, "No parameter sets in the extradata\n"); + + return length_size; + fail: + av_freep(&new_extradata); + return ret; +} + +static int h266_mp4toannexb_init(AVBSFContext *ctx) +{ + H266BSFContext *s = ctx->priv_data; + int ret; + + if (ctx->par_in->extradata_size < MIN_VVCC_LENGTH || + AV_RB24(ctx->par_in->extradata) == 1 || + AV_RB32(ctx->par_in->extradata) == 1) { + av_log(ctx, AV_LOG_VERBOSE, + "The input looks like it is Annex B already\n"); + } else { + ret = h266_extradata_to_annexb(ctx); + if (ret < 0) + return ret; + s->length_size = ret; + s->extradata_parsed = 1; + } + + return 0; +} + +static int h266_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) +{ + H266BSFContext *s = ctx->priv_data; + AVPacket *in; + GetByteContext gb; + + int is_irap = 0; + int added_extra = 0; + int i, ret = 0; + + ret = ff_bsf_get_packet(ctx, &in); + if (ret < 0) + return ret; + + if (!s->extradata_parsed) { + av_packet_move_ref(out, in); + av_packet_free(&in); + return 0; + } + + bytestream2_init(&gb, in->data, in->size); + + /* check if this packet contains an IRAP. The extradata will need to be added before any potential PH_NUT */ + while (bytestream2_get_bytes_left(&gb)) { + uint32_t nalu_size = 0; + int nalu_type; + + if (bytestream2_get_bytes_left(&gb) < s->length_size) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + for (i = 0; i < s->length_size; i++) + nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb); + + if (nalu_size < 2 || nalu_size > bytestream2_get_bytes_left(&gb)) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + nalu_type = (bytestream2_peek_be16(&gb) >> 3) & 0x1f; + is_irap = nalu_type >= VVC_IDR_W_RADL && nalu_type <= VVC_RSV_IRAP_11; + if (is_irap) { + break; + } + bytestream2_seek(&gb, nalu_size, SEEK_CUR); + } + + bytestream2_seek(&gb, 0, SEEK_SET); + while (bytestream2_get_bytes_left(&gb)) { + uint32_t nalu_size = 0; + int nalu_type; + int add_extradata, extra_size, prev_size; + + if (bytestream2_get_bytes_left(&gb) < s->length_size) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + for (i = 0; i < s->length_size; i++) + nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb); + + if (nalu_size < 2 || nalu_size > bytestream2_get_bytes_left(&gb)) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + nalu_type = (bytestream2_peek_be16(&gb) >> 3) & 0x1f; + + /* prepend extradata to IRAP frames */ + add_extradata = is_irap && nalu_type != VVC_AUD_NUT && !added_extra; + extra_size = add_extradata * ctx->par_out->extradata_size; + added_extra |= add_extradata; + + if (FFMIN(INT_MAX, SIZE_MAX) < 4ULL + nalu_size + extra_size) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + prev_size = out->size; + + ret = av_grow_packet(out, 4 + nalu_size + extra_size); + if (ret < 0) + goto fail; + + if (extra_size) + memcpy(out->data + prev_size, ctx->par_out->extradata, extra_size); + AV_WB32(out->data + prev_size + extra_size, 1); + bytestream2_get_buffer(&gb, out->data + prev_size + 4 + extra_size, + nalu_size); + } + + ret = av_packet_copy_props(out, in); + if (ret < 0) + goto fail; + + fail: + if (ret < 0) + av_packet_unref(out); + av_packet_free(&in); + + return ret; +} + +static const enum AVCodecID codec_ids[] = { + AV_CODEC_ID_VVC, AV_CODEC_ID_NONE, +}; + +const FFBitStreamFilter ff_h266_mp4toannexb_bsf = { + .p.name = "h266_mp4toannexb", + .p.codec_ids = codec_ids, + .priv_data_size = sizeof(H266BSFContext), + .init = h266_mp4toannexb_init, + .filter = h266_mp4toannexb_filter, +};