From patchwork Tue Sep 26 20:30:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 43917 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:2a18:b0:15d:8365:d4b8 with SMTP id e24csp2155414pzh; Tue, 26 Sep 2023 13:30:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGvX0Co3mIAaAqSt1jbefckpcR7+kF5MDOaRAQ434z45dZKJhbk2Hp8jNEqCpgtb7fOpdlz X-Received: by 2002:a19:6710:0:b0:500:9a45:62f with SMTP id b16-20020a196710000000b005009a45062fmr8883069lfc.8.1695760255758; Tue, 26 Sep 2023 13:30:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695760255; cv=none; d=google.com; s=arc-20160816; b=Q4f6kHP8IRBJTo6AyP10V5YjPYOc6JWOwzgYYS2bIV/zENjtW62culvRAExoORZl5W +ZwpmBXfopBnuTimn0Pdq4ndm6Uzj8s2EIzZGAnGhGoiJyrtZ2Ob36pu36lm4ir9bWL7 chtsZcVCB1TzdUiEHeQitzU2JPcvA9hAhHJP7qeprPh+kWUApqxssovgRdjv58lHSEAU JfM10Dk43aI5vI7vnawxUTKeE0Eh5iYhXOkS5zIejOatA3WTnT5zHDrtcLnBXLZV362r QTgANA8vEOxpXI9EoQrUJco2gyAyyP5jVj/6b0UR2PlpsDW19S4U1A6zA1Rf43rQjRj2 qJlA== 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:in-reply-to:from:references:to:content-language :user-agent:mime-version:date:message-id:dkim-signature:delivered-to; bh=yvsBoQJ1fDljAol6beTTl9iW4JmHyLgEGW0j7pkoQb8=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=JcU3S7//AeFLbUlfjZtWPjMNPqR3YCMeIp6MDsSDZHf01RvOmGGqrJqY9NEsZDl/9P AObN+a9UqJCMqTRbM0q1+F80n8A2yLA5uVqpi2E9V5oPk5eVZHZWODxBFQnGbNktRF4O Xkp2KQk99KvWP6ejkE+8bCPxmqBAjs3+Edp3zxLWYzSUi2Cv6qLJkMcy5enD2cTULlkC I9T6jvbefOZJBI8gmr5uS6NS8iMzA5LjiVXbRAYzOzy65apVuLayt6Be1kgxuPQiUblo 3WlpRlz+zaL5haj9kyLtLTnVkWke2PFeRrQY2B/ui92nxbXqVAx795jRwcxAu4z3yTTe ZCAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20230601.gappssmtp.com header.s=20230601 header.b=z1O1E9up; 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 d24-20020a056402401800b005334b249997si12870576eda.213.2023.09.26.13.30.54; Tue, 26 Sep 2023 13:30: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=@jkqxz-net.20230601.gappssmtp.com header.s=20230601 header.b=z1O1E9up; 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 C825968CA58; Tue, 26 Sep 2023 23:30:51 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1237568CA03 for ; Tue, 26 Sep 2023 23:30:45 +0300 (EEST) Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-40572aeb73cso64973365e9.3 for ; Tue, 26 Sep 2023 13:30:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20230601.gappssmtp.com; s=20230601; t=1695760244; x=1696365044; darn=ffmpeg.org; 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:message-id:reply-to; bh=JLRU4VOTNum+QXVCYXsyKnRIHTGs9E8ucafiG14byvs=; b=z1O1E9upJpDlX0j1iw7t7vJBHTNE1OrZY4p5m66Ryc7kVoMthiqZ3O1MiWdUzTlikK OePQAdpVIp550GKH28PNpdFOs1+j0+bR7dHW/laZKRzPeittblNInD7YcNo0w6ExSaDt 8cAR0TRaodYGEm++vpN2mW3T7FJtamGqJC68sCXhcatBDHyPqfr2M00l+V3XfaE8R+Ol 2rWDiOU2V8vHXHmmTVrlQHbVIUN+fwLyrUFm6inXz0SAnf/p32TO9y6wo3zc45BPgtJx 72d9PIhX79ttMRkj05YiztQiDT7VsP9cgTy8BHK1jRyFvEADWMnJPmgsAF5FOBe0aJsw 7zFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695760244; x=1696365044; 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:message-id:reply-to; bh=JLRU4VOTNum+QXVCYXsyKnRIHTGs9E8ucafiG14byvs=; b=Q/SACXIgcxN1PvYvzOj4jqs3yqgsMmh9N6feB7ggERISHBs+1YWGAmQMxR9eTVWC2M dJsE74y7UcSxx0xNOIiHf3zVF/SWdG6paxs8IjyfWCJW6aiJ5Xu3bwjt5GiA2jxnXMqL vQbdCbUixsek5inpmUEbA2v8N+EYGyBGsjVjPDVQRUYQ30NjJIyJpA7kiauyVK+yjYwj 80DKweqFuXKCgrv9u9jaK33DOtnO0+o5imBDAOftdd5bdQxLlhxAbR+85a5+1OkH0/TP ztmMHqUEl0QGN4C3g51psDhRxR8ptGRlg0/M9SvXVNhdlZTtHU5ia/6Ut1AH/ttkRt+A H1LA== X-Gm-Message-State: AOJu0YyDOZmPJ50c70SQF2EfWRMW8IZvwKux4EDOFhG3MKL21OQPAwP6 qtK79jYspt3pwvWEqETZfqLq6pJTtzuIneBfdnA= X-Received: by 2002:a5d:4e83:0:b0:31c:3136:60af with SMTP id e3-20020a5d4e83000000b0031c313660afmr8774434wru.61.1695760244346; Tue, 26 Sep 2023 13:30:44 -0700 (PDT) Received: from [192.168.0.15] (cpc92320-cmbg19-2-0-cust383.5-4.cable.virginm.net. [82.13.65.128]) by smtp.gmail.com with ESMTPSA id f1-20020a5d50c1000000b003142e438e8csm15518644wrt.26.2023.09.26.13.30.43 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 26 Sep 2023 13:30:43 -0700 (PDT) Message-ID: <82270518-8c7a-d360-ba1a-e2ceca913302@jkqxz.net> Date: Tue, 26 Sep 2023 21:30:51 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Content-Language: en-US To: ffmpeg-devel@ffmpeg.org References: From: Mark Thompson In-Reply-To: Subject: [FFmpeg-devel] [PATCH v2] cbs_av1: Make fake OBU size length field a write option 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: fGE8eey17G8S This is an option to modify the behaviour of the writer, not a syntax field. --- On 26/09/2023 03:34, Wang, Fei W wrote: > On Mon, 2023-09-25 at 14:53 +0100, Mark Thompson wrote: >> ... >> diff --git a/libavcodec/vaapi_encode_av1.c >> b/libavcodec/vaapi_encode_av1.c >> index 3ff1c47b53..861bf4a13b 100644 >> --- a/libavcodec/vaapi_encode_av1.c >> +++ b/libavcodec/vaapi_encode_av1.c >> @@ -133,6 +133,9 @@ static av_cold int >> vaapi_encode_av1_configure(AVCodecContext *avctx) >> priv->cbc->trace_context = ctx; >> priv->cbc->trace_write_callback = >> vaapi_encode_av1_trace_write_log; >> >> + av_opt_set_int(priv->cbc->priv_data, "fixed_obu_size_length", >> + priv->attr_ext2.bits.obu_size_bytes_minus1 + 1, >> 0); >> + > > This should be put after querying priv.attr_ext2 in > vaapi_encode_av1_init(). Ah, true - I got my init and configure mixed up, but don't have convenient hardware to test. Fixed in this version. Thanks, - Mark libavcodec/cbs_av1.c | 31 +++++++++++++++++++++---------- libavcodec/cbs_av1.h | 5 ++++- libavcodec/vaapi_encode_av1.c | 4 +++- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/libavcodec/cbs_av1.c b/libavcodec/cbs_av1.c index 4e687ace79..ed9a7b80d4 100644 --- a/libavcodec/cbs_av1.c +++ b/libavcodec/cbs_av1.c @@ -138,19 +138,25 @@ static int cbs_av1_read_leb128(CodedBitstreamContext *ctx, GetBitContext *gbc, return 0; } -/** Minimum byte length will be used to indicate the len128 of value if byte_len is 0. */ static int cbs_av1_write_leb128(CodedBitstreamContext *ctx, PutBitContext *pbc, - const char *name, uint64_t value, uint8_t byte_len) + const char *name, uint64_t value, int fixed_length) { int len, i; uint8_t byte; CBS_TRACE_WRITE_START(); - if (byte_len) - av_assert0(byte_len >= (av_log2(value) + 7) / 7); + len = (av_log2(value) + 7) / 7; - len = byte_len ? byte_len : (av_log2(value) + 7) / 7; + if (fixed_length) { + if (fixed_length < len) { + av_log(ctx->log_ctx, AV_LOG_ERROR, "OBU is too large for " + "fixed length size field (%d > %d).\n", + len, fixed_length); + return AVERROR(EINVAL); + } + len = fixed_length; + } for (i = 0; i < len; i++) { if (put_bits_left(pbc) < 8) @@ -1006,8 +1012,8 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, if (obu->header.obu_has_size_field) { pbc_tmp = *pbc; - if (obu->obu_size_byte_len) { - for (int i = 0; i < obu->obu_size_byte_len; i++) + if (priv->fixed_obu_size_length) { + for (int i = 0; i < priv->fixed_obu_size_length; i++) put_bits(pbc, 8, 0); } else { // Add space for the size field to fill later. @@ -1133,7 +1139,8 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, end_pos /= 8; *pbc = pbc_tmp; - err = cbs_av1_write_leb128(ctx, pbc, "obu_size", obu->obu_size, obu->obu_size_byte_len); + err = cbs_av1_write_leb128(ctx, pbc, "obu_size", obu->obu_size, + priv->fixed_obu_size_length); if (err < 0) goto error; @@ -1150,10 +1157,12 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx, } if (obu->obu_size > 0) { - if (!obu->obu_size_byte_len) { - obu->obu_size_byte_len = start_pos - data_pos; + if (!priv->fixed_obu_size_length) { memmove(pbc->buf + data_pos, pbc->buf + start_pos, header_size); + } else { + // The size was fixed so the following data was + // already written in the correct place. } skip_put_bytes(pbc, header_size); @@ -1273,6 +1282,8 @@ static const CodedBitstreamUnitTypeDescriptor cbs_av1_unit_types[] = { static const AVOption cbs_av1_options[] = { { "operating_point", "Set operating point to select layers to parse from a scalable bitstream", OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AV1_MAX_OPERATING_POINTS - 1, 0 }, + { "fixed_obu_size_length", "Set fixed length of the obu_size field", + OFFSET(fixed_obu_size_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8, 0 }, { NULL } }; diff --git a/libavcodec/cbs_av1.h b/libavcodec/cbs_av1.h index a9e2d2284f..7924257164 100644 --- a/libavcodec/cbs_av1.h +++ b/libavcodec/cbs_av1.h @@ -401,7 +401,6 @@ typedef struct AV1RawOBU { AV1RawOBUHeader header; size_t obu_size; - uint8_t obu_size_byte_len; union { AV1RawSequenceHeader sequence_header; @@ -468,6 +467,10 @@ typedef struct CodedBitstreamAV1Context { // AVOptions int operating_point; + // When writing, fix the length in bytes of the obu_size field. + // Writing will fail with an error if an OBU larger than can be + // represented by the fixed size is encountered. + int fixed_obu_size_length; } CodedBitstreamAV1Context; diff --git a/libavcodec/vaapi_encode_av1.c b/libavcodec/vaapi_encode_av1.c index 3ff1c47b53..5a9ff0f798 100644 --- a/libavcodec/vaapi_encode_av1.c +++ b/libavcodec/vaapi_encode_av1.c @@ -634,7 +634,6 @@ static int vaapi_encode_av1_init_picture_params(AVCodecContext *avctx, } } - fh_obu->obu_size_byte_len = priv->attr_ext2.bits.obu_size_bytes_minus1 + 1; ret = vaapi_encode_av1_add_obu(avctx, obu, AV1_OBU_FRAME_HEADER, &priv->fh); if (ret < 0) goto end; @@ -839,6 +838,9 @@ static av_cold int vaapi_encode_av1_init(AVCodecContext *avctx) priv->attr_ext2.value = attr.value; } + av_opt_set_int(priv->cbc->priv_data, "fixed_obu_size_length", + priv->attr_ext2.bits.obu_size_bytes_minus1 + 1, 0); + ret = vaapi_encode_av1_set_tile(avctx); if (ret < 0) return ret;