From patchwork Tue Nov 19 16:12:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16340 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 350AA443FE3 for ; Tue, 19 Nov 2019 18:26:06 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1B6C668A212; Tue, 19 Nov 2019 18:26:06 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 81B72689EBF for ; Tue, 19 Nov 2019 18:25:59 +0200 (EET) Received: by mail-wr1-f67.google.com with SMTP id i12so24602563wro.5 for ; Tue, 19 Nov 2019 08:25:59 -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=3F9/gfzB0z6OM1Oh9GrJH9aH1O4nfbYWIkvlu7zfwBg=; b=NZ+5Wvpu2dAhJSCK127/XNamHh9vzmxEvpB0d6o6QBLn/cVRvW5ntNHi2R5kdLJ9Vm B8j5L37I2CNge7BxYrFBIdnLvWZfRNC47SsEpRZ33zA16CvTuAi0++pQ0zHKTI+p2GoF Qg6cFeLTggbJkRHHWiHRTouaa3oFZJV/wT8g7sn54T6KFnv/KXNXPx17jDGN2dmRTp4F /4ENpLxhpjqGwo59TqPbUe3a9d3XR4y/GVXHcoY9teK8wEOwWaYR5a7i6JAAEENjeVy9 9EIFvpv8eJSfuhJBAtOnnDN3xz3PGrFCLAXgt2ZnyDnpN9K1p/Km15lsGGJ5BcoeLlp0 +EqQ== 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=3F9/gfzB0z6OM1Oh9GrJH9aH1O4nfbYWIkvlu7zfwBg=; b=V5v7W0LGIVCc8eDrY4RcX91kTcDjB4WC0jR+9SAtiVaZm//QzXe6c1r0iw8miM0+ai 7WtAK90VWPNYnR4bc6ZKoa8Hs1dmi/lHb5mUteDvl1tRnqE/BK32LmMGU21EFrizZgPU +voj1d2ptEUha1Ej5xyfDQll+RfvPI1SqXDcLs5+MFDC+eEN3xcbGWQx9iRhVm5OfsIT THY1ha434gk67U8apfc5uGgzoW1w/dFXUvyUhLDbuTLWewvHIEB2BMtROIkykMwAJRE7 h5pGg4MVYAatVBtltl505pdjC5TcsdwXHd4B2WDyUZqi7jjOpJnwOuYoy8oTuh6vg0Wy 6NtQ== X-Gm-Message-State: APjAAAVvHx13ZCk1yzvpkiUSknz++IbSqAr8HNzbPhQ1aRgaq25b7+sw LdZpX5XcSedjAMHmy5ewyEapEroV X-Google-Smtp-Source: APXvYqyYzuArUkC5TyzRV6SxqtIpiAxiUHJ38UhjhsBIu9YQUxQQds6nrPg8hMaONg7+DQY5FeJlQQ== X-Received: by 2002:a5d:4589:: with SMTP id p9mr25750342wrq.61.1574180286566; Tue, 19 Nov 2019 08:18:06 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08e23.dynamic.kabel-deutschland.de. [188.192.142.35]) by smtp.gmail.com with ESMTPSA id f188sm3611784wmf.3.2019.11.19.08.18.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2019 08:18:05 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 19 Nov 2019 17:12:30 +0100 Message-Id: <20191119161234.3766-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191119161234.3766-1-andreas.rheinhardt@gmail.com> References: <20191119161234.3766-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_vp9.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/libavcodec/cbs_vp9.c b/libavcodec/cbs_vp9.c index a74c4f4cec..c58589f07f 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); + flush_put_bits(pbc); + data_size = header_size = put_bits_count(pbc) / 8; + unit->data_bit_padding = 0; + 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 ff_cbs_default_write_unit_data(ctx, unit, pbc); + 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 0; } static int cbs_vp9_assemble_fragment(CodedBitstreamContext *ctx,