From patchwork Wed Sep 28 01:39:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xiang, Haihao" X-Patchwork-Id: 38392 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp945456pzh; Tue, 27 Sep 2022 18:42:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7x9dxhJ96uTsu7BeBO40bKi1qBIq9E79xqcU6/C/SejKUufaLzhvQqtmeWzr8G1jExEy8z X-Received: by 2002:aa7:d28a:0:b0:457:caea:9585 with SMTP id w10-20020aa7d28a000000b00457caea9585mr4287204edq.400.1664329323792; Tue, 27 Sep 2022 18:42:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664329323; cv=none; d=google.com; s=arc-20160816; b=RWhNw+Y25fdlQtkILTwrmCVm5amJ3YN7ViUSevdQJbzRpLhJ9ucFVB3ddkl404h6Aw QQIRlDghmBU5pmPsMPk6RPdbhQ/1xb5lE413hJzZPPrUEKnHwa1hfhuVljvXwoo9yXak vFTXZHbWa6M6dquKmzkkg1295UlEW+SpBh9GKnN0Uwnsv3pxtlMnC8gu/rJkE25OSV0n M5qEEMG3E20Vru1z6aH3gt0ZcBy4bzgr8dF53uJmj7HWcuz4nyNQ9SFs+77NUxTT1Y4f Eb2vsC7H6EKsde0/d51SOqEyH707zSlHKaKjhOdPsrDgqJRLfjZPTy/BLjdadmy9qkp+ si3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=GS8kQtCAk+Jmi+5pFuJ1vGbnfmGMdDD1Q5Mm6wGtt2Y=; b=fIaoD9PWpi7bKPRbCog+V2526quU8qIko3SAbpMhMw5exrXJVLTGPLnv748X7Lm61x Xjw3iknPUo5gzibkTIwD3ulzaa4TRV+3raL2LFUkBvkPVrcskoKy3HZLQbMSsR+AK5HY nzcbaXRqIiImGyKQZOvi76WAxmX7M8i4/9U0TN0OY+nKTjHKy+U2FECkInX3Pu0hsGvn k03il/Sc5NTbxSvC9qBdx7AOeZRUTsXDa57ZGH0egN5AtoJI1tpyFXe/kA5OmcT8dyo3 z6XZXMfe+G37sF4HwDVDKOo2w1u6hTOB6bLK4YQZyWLPY5Ezh+6vM7pTjUTkH1Apwaz4 Lf5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=dZwNagHF; 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 di11-20020a170906730b00b00777a40d515dsi3510404ejc.456.2022.09.27.18.42.01; Tue, 27 Sep 2022 18:42:03 -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=@intel.com header.s=Intel header.b=dZwNagHF; 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 F373A68BAEF; Wed, 28 Sep 2022 04:41:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3AA5568B1B5 for ; Wed, 28 Sep 2022 04:41:50 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1664329316; x=1695865316; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=/91sr4G3FRm4EEjU6IYAXcS2+TKEBithwCkGKQT9PqU=; b=dZwNagHFNn+yvDPGl5RFwd9AdRqIoA3EO8KhbPNDa1LVMNO/3ZuLjyei 3B8LMnj4fm/zZKdVlDiD9i6k2vTyG4JE+LplaoCSAjTBn2KtAKL30zhVO +Gff3zF/geSmD5cN2+JQBARmAhIhha0Js5W+oUZOmdikKrCRiEHKrUO92 GNssCU7mu1CaDevfjQu20gYi5NBqhGGwm3r3VHMFgGf/q+ymPU1WsdCA/ VY8wMrl0NE7G6r9616fIuG5ciR/CLJyykW/OlYsMzv6awAHQ9Hyxy1WBW q6uHALtZm2WLblesrRN6/84sxsHWbQRkiyRX52sFHwaCbydvmXXZZ4BEY A==; X-IronPort-AV: E=McAfee;i="6500,9779,10483"; a="363317907" X-IronPort-AV: E=Sophos;i="5.93,350,1654585200"; d="scan'208";a="363317907" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Sep 2022 18:41:47 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10483"; a="684211150" X-IronPort-AV: E=Sophos;i="5.93,350,1654585200"; d="scan'208";a="684211150" Received: from xhh-tgl64.sh.intel.com ([10.238.0.174]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Sep 2022 18:41:46 -0700 From: "Xiang, Haihao" To: ffmpeg-devel@ffmpeg.org Date: Wed, 28 Sep 2022 09:39:32 +0800 Message-Id: <20220928013932.204723-1-haihao.xiang@intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] lavc/cbs_av1: restore CodedBitstreamAV1Context when AVERROR(ENOSPC) 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 Cc: Haihao Xiang Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: YgAoKlTZjM2y From: Haihao Xiang The current pbc might be small for an obu frame, so a new pbc is required then parse this obu frame again. Because CodedBitstreamAV1Context has already been updated for this obu frame, we need to restore CodedBitstreamAV1Context, otherwise CodedBitstreamAV1Context doesn't match this obu frame when parsing obu frame again, e.g. CodedBitstreamAV1Context.order_hint. $ ffmpeg -i input.ivf -c:v copy -f null - [...] [av1_frame_merge @ 0x558bc3d6f880] ref_order_hint[i] does not match inferred value: 20, but should be 22. [av1_frame_merge @ 0x558bc3d6f880] Failed to write unit 1 (type 6). [av1_frame_merge @ 0x558bc3d6f880] Failed to write packet. [obu @ 0x558bc3d6e040] av1_frame_merge filter failed to send output packet --- libavcodec/cbs_av1.c | 64 ++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/libavcodec/cbs_av1.c b/libavcodec/cbs_av1.c index 154d9156cf..45e1288a51 100644 --- a/libavcodec/cbs_av1.c +++ b/libavcodec/cbs_av1.c @@ -1058,15 +1058,31 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, AV1RawTileData *td; size_t header_size; int err, start_pos, end_pos, data_pos; + CodedBitstreamAV1Context av1ctx; // OBUs in the normal bitstream format must contain a size field // in every OBU (in annex B it is optional, but we don't support // writing that). obu->header.obu_has_size_field = 1; + av1ctx = *priv; + + if (priv->sequence_header_ref) { + av1ctx.sequence_header_ref = av_buffer_ref(priv->sequence_header_ref); + if (!av1ctx.sequence_header_ref) + return AVERROR(ENOMEM); + } + + if (priv->frame_header_ref) { + av1ctx.frame_header_ref = av_buffer_ref(priv->frame_header_ref); + if (!av1ctx.frame_header_ref) { + err = AVERROR(ENOMEM); + goto error; + } + } err = cbs_av1_write_obu_header(ctx, pbc, &obu->header); if (err < 0) - return err; + goto error; if (obu->header.obu_has_size_field) { pbc_tmp = *pbc; @@ -1084,18 +1100,21 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, err = cbs_av1_write_sequence_header_obu(ctx, pbc, &obu->obu.sequence_header); if (err < 0) - return err; + goto error; av_buffer_unref(&priv->sequence_header_ref); priv->sequence_header = NULL; err = ff_cbs_make_unit_refcounted(ctx, unit); if (err < 0) - return err; + goto error; priv->sequence_header_ref = av_buffer_ref(unit->content_ref); - if (!priv->sequence_header_ref) - return AVERROR(ENOMEM); + if (!priv->sequence_header_ref) { + err = AVERROR(ENOMEM); + goto error; + } + priv->sequence_header = &obu->obu.sequence_header; } break; @@ -1103,7 +1122,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, { err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc); if (err < 0) - return err; + goto error; } break; case AV1_OBU_FRAME_HEADER: @@ -1115,7 +1134,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, AV1_OBU_REDUNDANT_FRAME_HEADER, NULL); if (err < 0) - return err; + goto error; } break; case AV1_OBU_TILE_GROUP: @@ -1123,7 +1142,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, err = cbs_av1_write_tile_group_obu(ctx, pbc, &obu->obu.tile_group); if (err < 0) - return err; + goto error; td = &obu->obu.tile_group.tile_data; } @@ -1132,7 +1151,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, { err = cbs_av1_write_frame_obu(ctx, pbc, &obu->obu.frame, NULL); if (err < 0) - return err; + goto error; td = &obu->obu.frame.tile_group.tile_data; } @@ -1141,7 +1160,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, { err = cbs_av1_write_tile_list_obu(ctx, pbc, &obu->obu.tile_list); if (err < 0) - return err; + goto error; td = &obu->obu.tile_list.tile_data; } @@ -1150,18 +1169,19 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, { err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->obu.metadata); if (err < 0) - return err; + goto error; } break; case AV1_OBU_PADDING: { err = cbs_av1_write_padding_obu(ctx, pbc, &obu->obu.padding); if (err < 0) - return err; + goto error; } break; default: - return AVERROR(ENOSYS); + err = AVERROR(ENOSYS); + goto error; } end_pos = put_bits_count(pbc); @@ -1172,7 +1192,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, // Add trailing bits and recalculate. err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8); if (err < 0) - return err; + goto error; end_pos = put_bits_count(pbc); obu->obu_size = header_size = (end_pos - start_pos + 7) / 8; } else { @@ -1190,14 +1210,19 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, *pbc = pbc_tmp; err = cbs_av1_write_leb128(ctx, pbc, "obu_size", obu->obu_size); if (err < 0) - return err; + goto error; data_pos = put_bits_count(pbc) / 8; flush_put_bits(pbc); av_assert0(data_pos <= start_pos); - if (8 * obu->obu_size > put_bits_left(pbc)) + if (8 * obu->obu_size > put_bits_left(pbc)) { + av_buffer_unref(&priv->sequence_header_ref); + av_buffer_unref(&priv->frame_header_ref); + *priv = av1ctx; + return AVERROR(ENOSPC); + } if (obu->obu_size > 0) { memmove(pbc->buf + data_pos, @@ -1213,8 +1238,13 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, // OBU data must be byte-aligned. av_assert0(put_bits_count(pbc) % 8 == 0); + err = 0; - return 0; +error: + av_buffer_unref(&av1ctx.sequence_header_ref); + av_buffer_unref(&av1ctx.frame_header_ref); + + return err; } static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx,