From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 54F4A448E4 for ; Tue, 27 Sep 2022 03:32:04 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 62A7468B9F1; Tue, 27 Sep 2022 06:32:02 +0300 (EEST) Received: from mail-oa1-f41.google.com (mail-oa1-f41.google.com [209.85.160.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BACCF68AD28 for ; Tue, 27 Sep 2022 06:31:55 +0300 (EEST) Received: by mail-oa1-f41.google.com with SMTP id 586e51a60fabf-13175b79807so1689238fac.9 for ; Mon, 26 Sep 2022 20:31:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date; bh=BOGG25jayTQfhIQBC6Hd5pvmR/dBGpfOR7rk7zUmro4=; b=mMRBW6x8xuMkqA/6FM+e2izfUz8l55jMYAwlYkPnoZX8fUdrnZviI9NkS8ckbKBl7p +kZ9qTMv9YBybgXjWRRf8GqjDdsV+18ar009y9H59zbk4LfBB2u1+elhArBV1QZubF/X sI6dcyulnqvTHePfc/dyZyBwwGO3CPcRMI64/tXW2BOkxHOoQmsWjAJayfy/YHFLhCLa DnLUAf2s0E334CSAuVU+8OI3AuyVk++gcBfaUGJkD75Ny9i7g8KODIAULyfzHSNxUJRk FNVjjs2a+jkRbh8WISqgCgLhRHS9CusEOoYD2txUiMMdbFmBQbR9nX2byJCmoTuFEK1e 9SlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date; bh=BOGG25jayTQfhIQBC6Hd5pvmR/dBGpfOR7rk7zUmro4=; b=kbDWWxxHKM/1wT5wlUzFMbmoqv6uAtU0AJ9vw0bHJQSM4e5asvrFdZ/oupuS2X1v/v c5Y2GlP7ZTkwf7ndFAkoEl02t4TQQO9xCV2ubcuJT1lvC938ONOTHKXr2M2wLw3J8lB8 ilF6OgaKssf1e5qwZpirorYjabzzawKAfFwyy1PuK3EQKz5B1JraBF+bSly6lLRuAc4P h2qV3ACOa5xwrQDJJMhWHSPhEsoz4k4eNcHCKPGDYLWeCrboZVhFoBmZavdrrHLe4Rpd bUsiCatBwnqcJgbus1wSewFQqhsi+JoBbxUZ2DW91B75nweA3mrjqhGzzsmpHlSt7gkU 8z4w== X-Gm-Message-State: ACrzQf2oOX0yyhyZcxQ62Qn4Cq6IdMlsYYM1koe3WPE+mj1U5/9X8uRO SUos+NYKp2F0J8QZVRS1IgXMRv2aJt4= X-Google-Smtp-Source: AMsMyM4sE6bR9HDATEvDqcKy376ELkwCHPMmeGSdo5NJ+OZdNs+QnQ5iiPtnn8WUSM+yeUIQOAbf7Q== X-Received: by 2002:a05:6870:529:b0:130:b1cd:f6f8 with SMTP id j41-20020a056870052900b00130b1cdf6f8mr1030058oao.55.1664249512931; Mon, 26 Sep 2022 20:31:52 -0700 (PDT) Received: from [192.168.0.13] ([191.97.187.183]) by smtp.gmail.com with ESMTPSA id u7-20020a4a5707000000b004763bad30f1sm190093ooa.11.2022.09.26.20.31.51 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 26 Sep 2022 20:31:52 -0700 (PDT) Message-ID: Date: Tue, 27 Sep 2022 00:31:50 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.3.0 Content-Language: en-US To: ffmpeg-devel@ffmpeg.org References: <20220926072315.26559-1-haihao.xiang@intel.com> From: James Almer In-Reply-To: <20220926072315.26559-1-haihao.xiang@intel.com> Subject: Re: [FFmpeg-devel] [PATCH] 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 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: On 9/26/2022 4:23 AM, Xiang, Haihao wrote: > 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 | 85 ++++++++++++++++++++++++++++++++++---------- > 1 file changed, 67 insertions(+), 18 deletions(-) > > diff --git a/libavcodec/cbs_av1.c b/libavcodec/cbs_av1.c > index 154d9156cf..585bc72cdb 100644 > --- a/libavcodec/cbs_av1.c > +++ b/libavcodec/cbs_av1.c > @@ -1058,15 +1058,33 @@ 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) { > + err = AVERROR(ENOMEM); > + goto error; If this is called, av1ctx.frame_header_ref will be equal to priv->frame_header_ref and as such unreferenced twice if not NULL. So just return AVERROR(ENOMEM) here. > + } > + } > + > + 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 +1102,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 +1124,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 +1136,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 +1144,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 +1153,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 +1162,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 +1171,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 +1194,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 +1212,36 @@ 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)) > - return AVERROR(ENOSPC); > + 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; > + > + if (av1ctx.sequence_header_ref) { > + priv->sequence_header_ref = av_buffer_ref(av1ctx.sequence_header_ref); > + if (!priv->sequence_header_ref) { > + err = AVERROR(ENOMEM); > + goto error; > + } > + } > + > + if (av1ctx.frame_header_ref) { > + priv->frame_header_ref = av_buffer_ref(av1ctx.frame_header_ref); > + if (!priv->frame_header_ref) { > + err = AVERROR(ENOMEM); > + goto error; > + } > + } Why are you creating new references? They were already copied to priv and av1ctx is in the stack, so just return AVERROR(ENOSPC) directly. > + > + err = AVERROR(ENOSPC); > + goto error; > + } > > if (obu->obu_size > 0) { > memmove(pbc->buf + data_pos, > @@ -1213,8 +1257,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, _______________________________________________ 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".