From patchwork Mon Sep 25 13:53:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 43895 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:2a18:b0:15d:8365:d4b8 with SMTP id e24csp1242395pzh; Mon, 25 Sep 2023 06:53:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEytN1zgq/0rRN2MH/6l1Vt5J6GtLdaiaBb7haeBTa6YRepA7VEwxupCisnVePR+8zDnjJc X-Received: by 2002:adf:ed84:0:b0:321:6833:b930 with SMTP id c4-20020adfed84000000b003216833b930mr6743765wro.16.1695650003676; Mon, 25 Sep 2023 06:53:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695650003; cv=none; d=google.com; s=arc-20160816; b=kwdaZ0HjOTNoamKeMBTTUWvqKGdtBmYxMycmuz0QUXRJowd9Bxw7uF2DEe9GJ6h7OF dvs+KK1eRi7mbq2eveWvS0KJDka7fFl7LZipIVIvjUUTXZOCtj9weflw27ShdVmeJZvt yNWsse6q992EVbWttKgjvTHFyIAJfffLQ39TMVOiNqMk9bhwsChXL7P4UNAkIq2sKrud 7PINAw3K59gmITOhz3ac1Bjl05mewHdTsGWzAvADSyHWDIDtr1I2WaTJ4RwMfiGHfHJT hcTj3VQz+KRyW5UhX7lQWfaRfBvDs+3YzmJuQHPJ7a4YU47gMlY3NDi8S0bVxnE8jozr wDzA== 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:from:to:content-language:user-agent:mime-version :date:message-id:dkim-signature:delivered-to; bh=wQrkEiTiVfjdWoinn6QVlxq6UN5tz+OdyE+SPOSIkHg=; fh=e5zN9xSzcxLA6bGo3lF+CqTbY/oLwzApV03EO/RBfgQ=; b=GOKjopsMr2CuDT5r6EQvN5cthfK/iesKK6suOF7Ck91nibeia56/XXFh5Cmm7Kst35 9eCEutxj6nMtgIN/EySMT+EvYn/+udknOCP60RyiOrlPLWVOwW+qd1FWL005Rt5X6xka yBhQje84NdQlF1pfy6QqsrqUL5m30cM8AQ71KE0MYw/84J5iZouMrr0G7kW34ruDmjtb 5TOJI3iw7+vWxAzOTF6qFNbZdeLU3bzS6ljPD+9xyFD/r+WJT5O6SmOz91yDCtZ9CkXE BTodkU5Cm6fVBLmfyNTG6KG8HhR489qRSrBPK5ynYATwCDddzu+yBcbRdXEMLzRorhhR UDwQ== 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=bm3ekZpE; 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 gl24-20020a170906e0d800b00997e8ac3332si8482601ejb.340.2023.09.25.06.53.23; Mon, 25 Sep 2023 06:53:23 -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=bm3ekZpE; 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 B8D8568C983; Mon, 25 Sep 2023 16:53:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org 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-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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Hcp+09BO39br 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;