From patchwork Thu Nov 14 20:21:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16275 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id C5027449D3F for ; Thu, 14 Nov 2019 22:21:56 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9E25068A626; Thu, 14 Nov 2019 22:21:56 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 905896881DE for ; Thu, 14 Nov 2019 22:21:50 +0200 (EET) Received: by mail-wm1-f67.google.com with SMTP id z19so7629638wmk.3 for ; Thu, 14 Nov 2019 12:21:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=HESxkxUpqD6urflggD+fbsatL48lAz2YajCV1rcN4GE=; b=hiDqj2KluqmZFggJkE9XvcNqXImk7KST8dATQgJM0rVNhfc1OKiSN303vkV3n1F8TH Zwj4yCypN7/phYpCM3R6lP79ZVK19NxF8N4jjjGn/28G2gK9ucZh1w7RjvJNfWBWGlWg wSPy2mCUz9dV77R26mEkdPg0a1t09DltrCo/jO/MSZcG3N9ndb0bxAsPyeZAyVQ0ih/U GHx4+xDTME1LV7OiSGj4u6w/W4twy/cB0UfFJ4kU6Kaqg+xNG41ZEs4YCg8I0WrVP/2X 604ZZIyfRO4fLt6NuhQWjvilUC9EekIIpg0JAthtSChg5QcVbHO52EeElduQ1RIvx8Ox s0oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=HESxkxUpqD6urflggD+fbsatL48lAz2YajCV1rcN4GE=; b=M6FewSc7OQzsPM7/cYJvMl1t9IPaPCUhGRbbI6aTOEtUrtaueOTSSa1Epuj3vn3IXn Dde13fVH7e6ub+1e8QLU3kOoIRPIw+JbrngMMxJ5PI1DK7FYahgLU1JafJaQGsN8tsTE Q+/TMlevTMfzUXeWCCo9YA1SYBOKXvn9+10KoNcyjQXMcFtU2BjP4AFuiSHGi5ppsnOh gaq2w7vxtXaFCk+6efWxghI+3y9tV/6rLXtDcO88rJxa3Hy1I1J7tgqvroQag/m0gnqF 4O3Hs8pM2CM9L5aXBpZ5nvBqejmDOLGJ2F7Qo7/rbPzEAe7Hl7Wc5PfgXF89M6u1Zy0f uX1A== X-Gm-Message-State: APjAAAU8J5m7SYIqoBgN5buACxQfH/pM2JKyMDdKIoeKuqWfTk/KfQUN h9UJqH+bKFYBF8TjC2y6kyMzFhWJ X-Google-Smtp-Source: APXvYqzLs1zYbaStXKL5Ng+HjKEyDPwKTOj/QKRt1AXPKbevLSEi8YH5eQJOIywTSE72Fdi8mZRDgA== X-Received: by 2002:a05:600c:22c3:: with SMTP id 3mr9643975wmg.139.1573762909871; Thu, 14 Nov 2019 12:21:49 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08937.dynamic.kabel-deutschland.de. [188.192.137.55]) by smtp.gmail.com with ESMTPSA id s17sm6868131wmh.41.2019.11.14.12.21.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Nov 2019 12:21:49 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 14 Nov 2019 21:21:42 +0100 Message-Id: <20191114202142.503-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avcodec/cbs_vp9: Write frame data directly X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Writing a unit (always a frame) in cbs_vp9 used an intermediate buffer to write the frame header followed by the frame data that was copied into said buffer. Afterwards, the final buffer for the frame was allocated and everything copied into this buffer. But it is trivial to compute the needed size of the final buffer after having written the header, so one can allocate the final buffer immediately and copy the frame data directly into it. Also, given that the intermediate buffer now only needs to keep the header, its size can be drastically reduced. Signed-off-by: Andreas Rheinhardt --- I think that 1KB is still quite generous for the header; one could probably even come up with a drastically lower bound upon reading the spec (one could probably put the intermediate buffer on the stack and remove the reallocation stuff). Similar improvements can be done for the other cbs_*. vp9 is first because it seems to be the easiest. libavcodec/cbs_vp9.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/libavcodec/cbs_vp9.c b/libavcodec/cbs_vp9.c index bd172100fc..ebb4c573ea 100644 --- a/libavcodec/cbs_vp9.c +++ b/libavcodec/cbs_vp9.c @@ -526,12 +526,13 @@ static int cbs_vp9_write_unit(CodedBitstreamContext *ctx, { CodedBitstreamVP9Context *priv = ctx->priv_data; VP9RawFrame *frame = unit->content; + size_t header_size, data_size; PutBitContext pbc; int err; if (!priv->write_buffer) { - // Initial write buffer size is 1MB. - priv->write_buffer_size = 1024 * 1024; + // Initial write buffer size is 1KB. + priv->write_buffer_size = 1024; reallocate_and_try_again: err = av_reallocp(&priv->write_buffer, priv->write_buffer_size); @@ -556,27 +557,26 @@ static int cbs_vp9_write_unit(CodedBitstreamContext *ctx, // Frame must be byte-aligned. av_assert0(put_bits_count(&pbc) % 8 == 0); - unit->data_size = put_bits_count(&pbc) / 8; + data_size = header_size = put_bits_count(&pbc) / 8; unit->data_bit_padding = 0; flush_put_bits(&pbc); if (frame->data) { - if (unit->data_size + frame->data_size > - priv->write_buffer_size) { - priv->write_buffer_size *= 2; - goto reallocate_and_try_again; - } + if (frame->data_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE + - header_size) + return AVERROR(ENOMEM); - memcpy(priv->write_buffer + unit->data_size, - frame->data, frame->data_size); - unit->data_size += frame->data_size; + data_size += frame->data_size; } - err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size); + err = ff_cbs_alloc_unit_data(ctx, unit, data_size); if (err < 0) return err; - memcpy(unit->data, priv->write_buffer, unit->data_size); + memcpy(unit->data, priv->write_buffer, header_size); + + if (frame->data) + memcpy(unit->data + header_size, frame->data, frame->data_size); return 0; }