From patchwork Sat Jun 17 22:00:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 42181 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c526:b0:117:ac03:c9de with SMTP id gm38csp2448691pzb; Sat, 17 Jun 2023 15:01:45 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6lwuwjiL2d15ZA+ZW+peH/0lWk7iz4uZmWEwI+Pp9Y+A+e58Z52sDITc8TbGyUakSko4T5 X-Received: by 2002:a17:907:1c96:b0:977:cad5:1580 with SMTP id nb22-20020a1709071c9600b00977cad51580mr6130876ejc.40.1687039305450; Sat, 17 Jun 2023 15:01:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687039305; cv=none; d=google.com; s=arc-20160816; b=uQdCP8opsaLIlF1Fz/dKL0JgnD+iPvPz7KzfaLR/qQc38AC4GDWg0mn2uQebNf/12m yc0pp1xBPhtV6NYqUXzLCDRQJeGb+VcNGvcTjQ/7TjpNCeNXPPkPiPAq7iFbRood9zsD EDZgmsZQZCfew6bBKi96qRsu3/FrGPjrXW1rxjojyBqWeoJTan8iy9lEZrmfa2Kw+7p1 suaWoyDqSYS0yGhlzvauh/IY+XEUdXLMHJ5UCZiTHv9YX6jZ4McQ5gnGiLeJCeLkU2KD pfCCZP+6ge8ZIJWJRIXG/h2kT1S5IzkJoiVU9MhWzPJ9F3+7u492/gJ3ZNurNt1j1wBH JV9Q== 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=CmEsQYd2kt1d9NGxl/gCMh4BGH99HLfBh+/MYzXH+ek=; b=YYDgs6TrJXoQoKWqAWyN+YlTfyY6NufrTkPYef47KYxQeCBWR6Lm1IkKAXYnuNqI+E 9wHJA7ULIt3Z0SKWZPgl7yAmHpF8G0uyN8X9wSDRQDAzb5zGf+ZhrfYKWPJ462ui8nlJ bn2O5ELq9q4JHjmP49mKupyMrY5ncByOOINDPk5ljVdfHjX1HUMRuaiox5wv4csL/aLp CrjE4I7LNJ4AypVPWVp/4vhz2RrgQLfhGRm2VA0RAAtIdvceFXHPTbyVSRDnDqa9wDRK pmzKqBrMJWnr2qS68rBeEOK6OksPQHFDmGzr9d0Qfu+Zvh10sJfc1Qlrwm4U2r8xw9sq OU/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20221208 header.b=gFvoHI1u; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id p1-20020a17090628c100b00977c4ffb2d4si12565236ejd.310.2023.06.17.15.01.45; Sat, 17 Jun 2023 15:01:45 -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=@gmail.com header.s=20221208 header.b=gFvoHI1u; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D95B868BF88; Sun, 18 Jun 2023 01:00:56 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oa1-f43.google.com (mail-oa1-f43.google.com [209.85.160.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7D0F968BF66 for ; Sun, 18 Jun 2023 01:00:50 +0300 (EEST) Received: by mail-oa1-f43.google.com with SMTP id 586e51a60fabf-1a6a2cb4659so2226224fac.1 for ; Sat, 17 Jun 2023 15:00:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687039249; x=1689631249; 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=0uv2zyfNQ0Y7+UuVwSoP62jugHjIkPPW82E0tzK1/fE=; b=gFvoHI1uIQSX2o1rWfSdX/XhsYgsdKc02dBJPynDClia53y6/0W0rgjatCL/8HSuwr OXxqiU6zEHN7Xe6ZMUxIACF+lVv+C8f/LrfUkcBEFxyF6wxVaZA1RYjVAIAjVuNGrKYB +LhFtfdgSHzspr1JMQmsIlOJBvCACZ76fluLOZbqh2vGZvP+rCfSSp/Td5mIY9C2xs51 wLoKnqg6L1AiaEzS42/Dfc5GCD4v3pmVvG2hCEk6iILLMyVMZovIGgGIv5qW3kihyqtS QOL/sNt3AvEXbYJO6BpAGEI+BtGCcK011Dy0Lh2j1NpEg7uV7pFeK44LTYOBuVY1l3HW cvJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687039249; x=1689631249; 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=0uv2zyfNQ0Y7+UuVwSoP62jugHjIkPPW82E0tzK1/fE=; b=Denc3bCvzUF6VgRoAUK3FNiqNFkHdK4aS1raWe0Sz5TVYM6YTa6Wn63pQLO1RRHZJe 5l7LEYOKy+Bk8EqgMssT7JBv3TP+riGXNz+mUQE0nAQUph/HMnQbmxyZcVymfon1HxFT oXK3DAt1qufAmd30XfP24WfG76gXzSi9ZLAd8STKmvkqH7urvULtssBmG/GRg1jUZGgZ BEBAxfbr2NIYxeRu9WRFQQhkDm5mPnJoag5/ZLi1q73a70YpLOGKWPgrTbJdFI+IE2Af qqZ0yx5hoLJKEUFOUyrPBnHHKJwmD6vhxpt3nNcBX02KCQ86BbGvl/ekieNXhS2gvYED MHlA== X-Gm-Message-State: AC+VfDxN4XBYcX2OieZkk8xs9pkKjqq6qmzJc96f2Bwo4pebQ0TSobX7 oHmyFSvWo6TJ1+thn0B/g8vEShgNn2c= X-Received: by 2002:a05:6871:6a87:b0:192:8551:6d03 with SMTP id zf7-20020a0568716a8700b0019285516d03mr1619653oab.27.1687039248779; Sat, 17 Jun 2023 15:00:48 -0700 (PDT) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id o42-20020a05687096aa00b0019280078be5sm6246497oaq.6.2023.06.17.15.00.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Jun 2023 15:00:48 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sat, 17 Jun 2023 19:00:14 -0300 Message-ID: <20230617220015.12669-6-jamrial@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230617151848.1378-1-jamrial@gmail.com> References: <20230617151848.1378-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/10] avcodec/evc_frame_merge_bsf: make ff_evc_parse_nal_unit() local to the filter 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: Yd0hAXtmtfu2 Signed-off-by: James Almer --- libavcodec/evc_frame_merge_bsf.c | 114 +++++++++++++++++++++++++------ 1 file changed, 92 insertions(+), 22 deletions(-) diff --git a/libavcodec/evc_frame_merge_bsf.c b/libavcodec/evc_frame_merge_bsf.c index f497780afb..817136a551 100644 --- a/libavcodec/evc_frame_merge_bsf.c +++ b/libavcodec/evc_frame_merge_bsf.c @@ -35,20 +35,27 @@ typedef struct AccessUnitBuffer { typedef struct EVCFMergeContext { AVPacket *in; - EVCParserContext parser_ctx; + EVCParamSets ps; + EVCParserPoc poc; AccessUnitBuffer au_buffer; } EVCFMergeContext; -static int end_of_access_unit_found(EVCParserContext *parser_ctx) +static int end_of_access_unit_found(const EVCParamSets *ps, const EVCParserSliceHeader *sh, + const EVCParserPoc *poc, enum EVCNALUnitType nalu_type) { - if (parser_ctx->profile == 0) { // BASELINE profile - if (parser_ctx->nalu_type == EVC_NOIDR_NUT || parser_ctx->nalu_type == EVC_IDR_NUT) + EVCParserPPS *pps = ps->pps[sh->slice_pic_parameter_set_id]; + EVCParserSPS *sps = ps->sps[pps->pps_seq_parameter_set_id]; + + av_assert0(sps && pps); + + if (sps->profile_idc == 0) { // BASELINE profile + if (nalu_type == EVC_NOIDR_NUT || nalu_type == EVC_IDR_NUT) return 1; } else { // MAIN profile - if (parser_ctx->nalu_type == EVC_NOIDR_NUT) { - if (parser_ctx->poc.PicOrderCntVal != parser_ctx->poc.prevPicOrderCntVal) + if (nalu_type == EVC_NOIDR_NUT) { + if (poc->PicOrderCntVal != poc->prevPicOrderCntVal) return 1; - } else if (parser_ctx->nalu_type == EVC_IDR_NUT) + } else if (nalu_type == EVC_IDR_NUT) return 1; } return 0; @@ -58,7 +65,7 @@ static void evc_frame_merge_flush(AVBSFContext *bsf) { EVCFMergeContext *ctx = bsf->priv_data; - ff_evc_ps_free(&ctx->parser_ctx.ps); + ff_evc_ps_free(&ctx->ps); av_packet_unref(ctx->in); ctx->au_buffer.data_size = 0; } @@ -66,12 +73,10 @@ static void evc_frame_merge_flush(AVBSFContext *bsf) static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out) { EVCFMergeContext *ctx = bsf->priv_data; - EVCParserContext *parser_ctx = &ctx->parser_ctx; - AVPacket *in = ctx->in; - - size_t nalu_size = 0; uint8_t *buffer, *nalu = NULL; + enum EVCNALUnitType nalu_type; + int tid, nalu_size = 0; int au_end_found = 0; int err; @@ -81,29 +86,91 @@ static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out) nalu_size = evc_read_nal_unit_length(in->data, EVC_NALU_LENGTH_PREFIX_SIZE, bsf); if (nalu_size <= 0) { - av_packet_unref(in); - return AVERROR_INVALIDDATA; + err = AVERROR_INVALIDDATA; + goto end; } nalu = in->data + EVC_NALU_LENGTH_PREFIX_SIZE; nalu_size = in->size - EVC_NALU_LENGTH_PREFIX_SIZE; // NAL unit parsing needed to determine if end of AU was found - err = ff_evc_parse_nal_unit(parser_ctx, nalu, nalu_size, bsf); - if (err < 0) { - av_log(bsf, AV_LOG_ERROR, "NAL Unit parsing error\n"); - av_packet_unref(in); + if (nalu_size <= 0) { + av_log(bsf, AV_LOG_ERROR, "Invalid NAL unit size: (%d)\n", nalu_size); + err = AVERROR_INVALIDDATA; + goto end; + } - return err; + // @see ISO_IEC_23094-1_2020, 7.4.2.2 NAL unit header semantic (Table 4 - NAL unit type codes and NAL unit type classes) + // @see enum EVCNALUnitType in evc.h + nalu_type = evc_get_nalu_type(nalu, nalu_size, bsf); + if (nalu_type < EVC_NOIDR_NUT || nalu_type > EVC_UNSPEC_NUT62) { + av_log(bsf, AV_LOG_ERROR, "Invalid NAL unit type: (%d)\n", nalu_type); + err = AVERROR_INVALIDDATA; + goto end; } - au_end_found = end_of_access_unit_found(parser_ctx); + tid = ff_evc_get_temporal_id(nalu, nalu_size, bsf); + if (tid < 0) { + av_log(bsf, AV_LOG_ERROR, "Invalid temporial id: (%d)\n", tid); + err = AVERROR_INVALIDDATA; + goto end; + } + + nalu += EVC_NALU_HEADER_SIZE; + nalu_size -= EVC_NALU_HEADER_SIZE; + + switch (nalu_type) { + case EVC_SPS_NUT: { + EVCParserSPS *sps = ff_evc_parse_sps(&ctx->ps, nalu, nalu_size); + if (!sps) { + av_log(bsf, AV_LOG_ERROR, "SPS parsing error\n"); + err = AVERROR_INVALIDDATA; + goto end; + } + break; + } + case EVC_PPS_NUT: { + EVCParserPPS *pps = ff_evc_parse_pps(&ctx->ps, nalu, nalu_size); + if (!pps) { + av_log(bsf, AV_LOG_ERROR, "PPS parsing error\n"); + err = AVERROR_INVALIDDATA; + goto end; + } + break; + } + case EVC_IDR_NUT: // Coded slice of a IDR or non-IDR picture + case EVC_NOIDR_NUT: { + EVCParserSliceHeader sh; + + err = ff_evc_parse_slice_header(&sh, &ctx->ps, nalu_type, nalu, nalu_size); + if (err < 0) { + av_log(bsf, AV_LOG_ERROR, "Slice header parsing error\n"); + goto end; + } + + // POC (picture order count of the current picture) derivation + // @see ISO/IEC 23094-1:2020(E) 8.3.1 Decoding process for picture order count + err = ff_evc_derive_poc(&ctx->ps, &sh, &ctx->poc, nalu_type, tid); + if (err < 0) + goto end; + + au_end_found = end_of_access_unit_found(&ctx->ps, &sh, &ctx->poc, nalu_type); + + break; + } + case EVC_SEI_NUT: // Supplemental Enhancement Information + case EVC_APS_NUT: // Adaptation parameter set + case EVC_FD_NUT: // Filler data + default: + break; + } buffer = av_fast_realloc(ctx->au_buffer.data, &ctx->au_buffer.capacity, ctx->au_buffer.data_size + in->size); if (!buffer) { av_freep(&ctx->au_buffer.data); - return AVERROR(ENOMEM); + err = AVERROR_INVALIDDATA; + goto end; } ctx->au_buffer.data = buffer; @@ -128,6 +195,9 @@ static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out) if (err < 0 && err != AVERROR(EAGAIN)) ctx->au_buffer.data_size = 0; +end: + if (err < 0) + av_packet_unref(in); return err; } @@ -147,7 +217,7 @@ static void evc_frame_merge_close(AVBSFContext *bsf) EVCFMergeContext *ctx = bsf->priv_data; av_packet_free(&ctx->in); - ff_evc_ps_free(&ctx->parser_ctx.ps); + ff_evc_ps_free(&ctx->ps); ctx->au_buffer.capacity = 0; av_freep(&ctx->au_buffer.data);