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 72F5947014 for ; Mon, 25 Sep 2023 13:53:22 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8646768C980; Mon, 25 Sep 2023 16:53:19 +0300 (EEST) Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5EC2B68C947 for ; Mon, 25 Sep 2023 16:53:13 +0300 (EEST) Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4053c6f0d55so60468945e9.0 for ; Mon, 25 Sep 2023 06:53:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20230601.gappssmtp.com; s=20230601; t=1695649992; x=1696254792; darn=ffmpeg.org; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=gYbrrzTl8LR5N9KXAodwfWrW/2fQAWAtmvw69xpW8XA=; b=bm3ekZpEe/0Osv+9nCBWidrFt9oRvYKE+Ua81sC0PGhS+sliRcut4jLGh5/lv21ioq +2fZ5yzB4TxjP8OsP5wzbe9J592GPeNNlELxYol0EGkekSfDBJ4+gKaDrvc7n5nnUU1B 61QozrJUhy4Co+G31/1+TMdH1zukHshEjkB1+zlmU8V4G6aVkc0o9qqOq0VIopknqrdo BAxpQDBBsx0xXT94jy+EVUs4TO0k9hyxSCnwWgT34lOhxdnUIQin6Ki1mdQzlsLBP4YT eBWfSCTMNP8cAIAK7BlN9POcoCtiNXjgNPSh8mseQPCIeGKmcMnBE0q1jY2MO33cvZLG yBBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695649992; x=1696254792; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=gYbrrzTl8LR5N9KXAodwfWrW/2fQAWAtmvw69xpW8XA=; b=QzKUxkUoIo3vMnonGz3gBhSfJ4swVecqSj4skap8uPTZlMwggzWbpl2uXgpEP19w8+ tLSwIzIR5fdKqtVQ6tHX4Nvlq7p90ky35vWCjKUvcO9Ux+BFQOV0WxHScmPIM4c/tDaA zgBdZOumZknzw1t2cfP0UTSkDG99Fwym/zlJWNzwXfD4w5UiSBnIlKqQhs22M2swfmBi Yas1PEr0BhuHHSJyfhWVG8D4jzXrYRIIqqGYhgS05KAsLQR6yvqQekhuc5kMfSowWjLr fJ8yTh3cx/84gZIFuDxXPDqmrI+NiaLeuYIYkRBr+GSaPls/CgeKoPvpLBlz8ZR0kuln vLsw== X-Gm-Message-State: AOJu0YyqzhISgsl7Nx7thwGjZ//Vkv/CXb3LpWwsPUTbFMDTwGCK3CMf OWOWSEK4CVdUoFUsGBMC9II0I/KqPjRc7dQRxNQ= X-Google-Smtp-Source: AGHT+IHcBS6jrPtnNeBquZYmO1zBhmLeSY/yn/KKCM+RtxQxz3sK71qnBJKH4LP2dUDhH8+CMpTg0A== X-Received: by 2002:a05:600c:1c02:b0:405:7400:1e4c with SMTP id j2-20020a05600c1c0200b0040574001e4cmr4268800wms.35.1695649992191; Mon, 25 Sep 2023 06:53:12 -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 x15-20020adfcc0f000000b0032330e43590sm539973wrh.22.2023.09.25.06.53.11 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Sep 2023 06:53:11 -0700 (PDT) Message-ID: Date: Mon, 25 Sep 2023 14:53:19 +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 development discussions and patches From: Mark Thompson Subject: [FFmpeg-devel] [PATCH] 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 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: This is an option to modify the behaviour of the writer, not a syntax field. --- Tested by hacking av1_metadata. For example, adding: av_opt_set_int(ctx->common.output->priv_data, "fixed_obu_size_length", 7, 0); gets you OBU headers that look like: [trace_headers @ 0x55706fcb2880] OBU header [trace_headers @ 0x55706fcb2880] 0 obu_forbidden_bit 0 = 0 [trace_headers @ 0x55706fcb2880] 1 obu_type 0100 = 4 [trace_headers @ 0x55706fcb2880] 5 obu_extension_flag 0 = 0 [trace_headers @ 0x55706fcb2880] 6 obu_has_size_field 1 = 1 [trace_headers @ 0x55706fcb2880] 7 obu_reserved_1bit 0 = 0 [trace_headers @ 0x55706fcb2880] 8 obu_size 10101110100010101000000010000000100000001000000000000000 = 1326 It's not obvious that there is any value in exposing this option more generally, though? It could made a visible option of av1_metadata or others if there is any use-case for it. 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..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); + if (ctx->rc_mode->quality) { priv->q_idx_p = av_clip(ctx->rc_quality, 0, AV1_MAX_QUANT); if (fabs(avctx->i_quant_factor) > 0.0) @@ -634,7 +637,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; -- 2.39.2 _______________________________________________ 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".