From patchwork Sun Nov 17 07:34:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16300 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 710F44499FD for ; Sun, 17 Nov 2019 09:35:30 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5A4A168A476; Sun, 17 Nov 2019 09:35:30 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 87F6768A4BE for ; Sun, 17 Nov 2019 09:35:22 +0200 (EET) Received: by mail-wr1-f65.google.com with SMTP id r10so15805940wrx.3 for ; Sat, 16 Nov 2019 23:35:22 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=8wF/v2YGFFDkBxF6fgXeRlGWYbyeuiUlCJ3NDhBVexM=; b=CKMD7rj+eHjFf810z5HyPl/C0P0yfzLB7LGglrr9xLwkX6sfZqaP9Ez1mcJkf/HO2n 2EeIEg4nWzA40ObQYTbun6a55Pl3q1Hd1IpmiwsGUJPsQfNNjLuRgs3VMKv4OSOpXYXZ eFJER6+Sg7xVTcbMVPeBd5tvp8om1LCVQKVAnKZgpR6JF+WA0QL9CnWRYHQDT9nu4/FT KOkWBAcn2ZHLQ3n+1xy3hVZJP3OC1GbtWJidn981GRrzerlJ2W3Mj5YNfuNIhx02USTQ FoFHdPJBHz3kCaUHPCHSfNRBmKsQnlUjrosr535lR3vh/gxS34eFOQ8aFq1mkNqReQFf D6Uw== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=8wF/v2YGFFDkBxF6fgXeRlGWYbyeuiUlCJ3NDhBVexM=; b=d+MBNArZaQ97EjTfE0xRfycqavhAp8o4EAF8JOFQ11qDflH0IzYDlUd3HD10yjhcbJ arDi1ltHZYa2n80s2aGWCv+hqXpTPaYgjneaDUXG43DPkgF/aglxGR5pPpXdGPMMDUFT uhvwRpvTgj5uaaxvhU/94Bm87XmooGbQID4u5PovcUL3O4qdjmwfGR/m2kyodv+MecPn xQwBVPm8WE0wx5mfNpTsidaQBCcmStCTad4f0kJ6dwNHt87+xy0+y2lIzZVnVGxLpJWy R/DdwXYrAzxmXmxwg8V0ibVfoUDyNQIK5EfRQ7bChf8QbHQGn+X1UAXiHnXSj0hOljVg eB1g== X-Gm-Message-State: APjAAAViQ0ZWOd+YUs/9s8le24k4mEj6jC9fZZhFm/QHNJvuFD0gVf8y oXVUlKxQMXlx9NxBCOI64VKRynm5M/0= X-Google-Smtp-Source: APXvYqweY2fJJx1tX1qG+Q9hbFZQk8xsI+AAA1HpeTWiAVL6cx8XXPPlGIvLCP0/mf995OKTvNkhbQ== X-Received: by 2002:a05:6000:1602:: with SMTP id u2mr9685230wrb.249.1573976121574; Sat, 16 Nov 2019 23:35:21 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08e23.dynamic.kabel-deutschland.de. [188.192.142.35]) by smtp.gmail.com with ESMTPSA id q15sm16713082wmq.0.2019.11.16.23.35.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Nov 2019 23:35:21 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sun, 17 Nov 2019 08:34:37 +0100 Message-Id: <20191117073440.22718-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191117073440.22718-1-andreas.rheinhardt@gmail.com> References: <20191117073440.22718-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 3/6] 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. Signed-off-by: Andreas Rheinhardt --- libavcodec/cbs.c | 4 ++++ libavcodec/cbs_internal.h | 6 ++++-- libavcodec/cbs_vp9.c | 25 +++++++++++++++++++------ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c index 0badb192d9..9ad2641f6d 100644 --- a/libavcodec/cbs.c +++ b/libavcodec/cbs.c @@ -306,6 +306,10 @@ static int cbs_write_unit_data(CodedBitstreamContext *ctx, init_put_bits(&pbc, ctx->write_buffer, ctx->write_buffer_size); ret = ctx->codec->write_unit(ctx, unit, &pbc); + if (ret == 1) { + // write_unit has already finished the unit. + return 0; + } if (ret < 0) { if (ret == AVERROR(ENOSPC)) { // Overflow. diff --git a/libavcodec/cbs_internal.h b/libavcodec/cbs_internal.h index 4c5a535ca6..5768baa9ca 100644 --- a/libavcodec/cbs_internal.h +++ b/libavcodec/cbs_internal.h @@ -44,8 +44,10 @@ typedef struct CodedBitstreamType { int (*read_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit); - // Write the data bitstream from unit->content into pbc. - // Return value AVERROR(ENOSPC) indicates that pbc was too small. + // Write the data bitstream from unit->content into pbc or into unit->data. + // Return value AVERROR(ENOSPC) indicates that pbc was too small; + // 1 indicates that the unit has already been finished by write_unit + // (i.e. unit->data and unit->data_ref have been allocated and filled). int (*write_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc); diff --git a/libavcodec/cbs_vp9.c b/libavcodec/cbs_vp9.c index 42e4dcf5ac..bc074c4631 100644 --- a/libavcodec/cbs_vp9.c +++ b/libavcodec/cbs_vp9.c @@ -526,6 +526,7 @@ static int cbs_vp9_write_unit(CodedBitstreamContext *ctx, PutBitContext *pbc) { VP9RawFrame *frame = unit->content; + size_t data_size, header_size; int err; err = cbs_vp9_write_frame(ctx, pbc, frame); @@ -535,16 +536,28 @@ static int cbs_vp9_write_unit(CodedBitstreamContext *ctx, // Frame must be byte-aligned. av_assert0(put_bits_count(pbc) % 8 == 0); + data_size = header_size = put_bits_count(pbc) / 8; + unit->data_bit_padding = 0; + flush_put_bits(pbc); + if (frame->data) { - if (frame->data_size > put_bits_left(pbc) / 8) - return AVERROR(ENOSPC); + if (frame->data_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE + - header_size) + return AVERROR(ENOMEM); - flush_put_bits(pbc); - memcpy(put_bits_ptr(pbc), frame->data, frame->data_size); - skip_put_bytes(pbc, frame->data_size); + data_size += frame->data_size; } - return 0; + err = ff_cbs_alloc_unit_data(ctx, unit, data_size); + if (err < 0) + return err; + + memcpy(unit->data, pbc->buf, header_size); + + if (frame->data) + memcpy(unit->data + header_size, frame->data, frame->data_size); + + return 1; } static int cbs_vp9_assemble_fragment(CodedBitstreamContext *ctx,