From patchwork Wed May 2 01:40:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 8741 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:155:0:0:0:0:0 with SMTP id c82-v6csp132695jad; Tue, 1 May 2018 18:41:40 -0700 (PDT) X-Google-Smtp-Source: AB8JxZozBBRoqrL2d9FgBwoldXpWIhjdv8dzeODRqF/ofu0FZYm2wJtVDFmoBIbSF838qgsmgL7d X-Received: by 10.28.146.136 with SMTP id u130mr11760513wmd.125.1525225300345; Tue, 01 May 2018 18:41:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525225300; cv=none; d=google.com; s=arc-20160816; b=fZCSg3n5F2m81WkFvxe+SJojOlKW3AVtp+MIlWbt/0IkIY3c0Wdlv7AaIuutconEw0 a7smw4qQAIPILE0n2nZZIR9mCD0VNL4BEGY251U/qUOEtCRohYBhX/x/29Iy8pxhsv4m A64o+LAU+t2BMRHpiwYA8GCvB5WtArXZi1uuwZt4R79/Qo3FDben/lQ/A+G9ErZYsak0 1FUsQmnSRMj8BlpHCxavB/Y2bw/3SJr/zJA2bj/J4vD8q+AAN2ZACc/QimQJCzcftM5W bHGhkXM5BXsuD/7Tk4Yu9QuD0QH6yqr/NvBDVIbJv14+laxwrMqEgOjbi9oRYbwyHmXI LNsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=55wvXX3CkJEYcFsg2PWO13WgKp67mj9YGQFqdnla10M=; b=v+VSdQmWPEvKRhLZ9ZQgPNN8KH3JwRJYWgIGbdKiyhJNbgRSxBTQHv8cA8xgj1M6Dg YljDYO1IwFcyRFjMoePjnmOgQj4cJFmI2hv/so9WCVBdVKVIIGAYgdD4opgu0SS/ge5f TMpfWwHYF3V/7AxRPCYX5Qeuqy0aKXAfKP2361ZW9lPLZ7ZujMjg4o/lCmQauofSPZCN 9Q0RH8F9822qSdmav14IpIgRifVqFrMhQ0J56oEiKGzQQHNaTb5FO+ayQnqnuT0faxQp Ov9ATTI+VEL5Wqb5x83XLfUUMmIBuS5pX6sFVz5Yp5BWnXauuGPB/Yp3t4e0nRBOJvNa yDLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=eIGgiQq9; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id j16-v6si10083387wrd.1.2018.05.01.18.41.39; Tue, 01 May 2018 18:41:40 -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=@gmail.com header.s=20161025 header.b=eIGgiQq9; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5B29B68A175; Wed, 2 May 2018 04:41:05 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt0-f196.google.com (mail-qt0-f196.google.com [209.85.216.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 11D4868A109 for ; Wed, 2 May 2018 04:40:59 +0300 (EEST) Received: by mail-qt0-f196.google.com with SMTP id e8-v6so11698889qth.0 for ; Tue, 01 May 2018 18:41:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=5oLJMhycPX5XK5mk3AY+ZWsgThX7pDwn4uDJxD4P3f4=; b=eIGgiQq9TfXG5RkNBV7IeqjEBgF7A+oL4DZYyulI7fsUm+HpyA+E6Y4kWrkcOSfGqn HHn/IpvtxZjunKpJlsf86rZDIkHBjIfg121vsc73iV/WRo72Skwy7WkC9OMISzholtDc DIyzkbTEReg3v0WbYvXT7XI3Z+fF+ufAk76o/71c2FHxIM7vSo4zU7Rn8NjzSe9YTy3X N+/8nu2kUTWfxmbfg0ntz/8GERx/ehlvZzusmYM5cd4MCwKjdNR3Zv0/+MUahelP8NZv qvfZW9T6hAWexvWByYTeyTIFXrAVNvzY28PK22eSVxHh1Un3bQeOSk1Txk7ojqJb9PAK GfAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=5oLJMhycPX5XK5mk3AY+ZWsgThX7pDwn4uDJxD4P3f4=; b=kklsD2qmm92KCAVtqQl5oZXolu1kPGt0xg7qlqWC1fspLCZv94aPybXgfoLLqgEGoa appNio/l3fQ6jXs8/IRZ7c7xWVOjBtBK0TMnrWiT5hYUi1XqkehsEYghDwX2aGp5oSWW RnCexq1pNQI2X5DWeBDlh8ttaV27c47EKQrYgUy+kd+KLPdlCijTgOJMNv/DuZMNMDlh ZasZrj+7gYUkfi6OY2uoO+w0fcM7iOFiqsOKLDNtHc+bL7Cxq1IKePH4Kgb2t66uiAId cQ+HsUjadgB9v/piV3WtLwPLk1AH6oSm8Ygu5WeR3rXFf46xk9qgNcF4PMCyEZA024B2 G1aw== X-Gm-Message-State: ALQs6tD22jSCNXh5T7iyT1ygJPlqry0oZkgwMvr8+BASHbgrXRblfrUq b3dvDS1+rghOragx2lomjMNU2A== X-Received: by 2002:a0c:a465:: with SMTP id w92-v6mr14829676qvw.46.1525225290325; Tue, 01 May 2018 18:41:30 -0700 (PDT) Received: from localhost.localdomain ([190.188.171.140]) by smtp.gmail.com with ESMTPSA id b127sm7964326qkf.76.2018.05.01.18.41.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 May 2018 18:41:29 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Tue, 1 May 2018 22:40:35 -0300 Message-Id: <20180502014035.10628-1-jamrial@gmail.com> X-Mailer: git-send-email 2.17.0 Subject: [FFmpeg-devel] [PATCH] avcodec/cbs: use av_fast_malloc in case of unit write buffer overflow 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Use it instead of av_reallocp, as the contents of the existing buffer are ultimately discarded. Signed-off-by: James Almer --- Barely any speed gain in most cases, but realloc of this data is nonetheless unnecessary. libavcodec/cbs_h2645.c | 13 +++++++------ libavcodec/cbs_h2645.h | 2 +- libavcodec/cbs_mpeg2.c | 13 +++++++------ libavcodec/cbs_mpeg2.h | 2 +- libavcodec/cbs_vp9.c | 15 ++++++++------- libavcodec/cbs_vp9.h | 2 +- 6 files changed, 25 insertions(+), 22 deletions(-) diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c index 64a1a2d1ee..ccb4e5190c 100644 --- a/libavcodec/cbs_h2645.c +++ b/libavcodec/cbs_h2645.c @@ -1235,19 +1235,20 @@ static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx, CodedBitstreamH2645Context *priv = ctx->priv_data; enum AVCodecID codec_id = ctx->codec->codec_id; PutBitContext pbc; + size_t write_buffer_size; int err; if (!priv->write_buffer) { // Initial write buffer size is 1MB. - priv->write_buffer_size = 1024 * 1024; + write_buffer_size = 1024 * 1024; reallocate_and_try_again: - err = av_reallocp(&priv->write_buffer, priv->write_buffer_size); - if (err < 0) { + av_fast_malloc(&priv->write_buffer, &priv->write_buffer_size, write_buffer_size); + if (!priv->write_buffer) { av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a " "sufficiently large write buffer (last attempt " - "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size); - return err; + "%"SIZE_SPECIFIER" bytes).\n", write_buffer_size); + return AVERROR(ENOMEM); } } @@ -1260,7 +1261,7 @@ static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx, if (err == AVERROR(ENOSPC)) { // Overflow. - priv->write_buffer_size *= 2; + write_buffer_size = priv->write_buffer_size * 2; goto reallocate_and_try_again; } // Overflow but we didn't notice. diff --git a/libavcodec/cbs_h2645.h b/libavcodec/cbs_h2645.h index f4cf65bdde..29630ea7b5 100644 --- a/libavcodec/cbs_h2645.h +++ b/libavcodec/cbs_h2645.h @@ -36,7 +36,7 @@ typedef struct CodedBitstreamH2645Context { // Write buffer uint8_t *write_buffer; - size_t write_buffer_size; + unsigned write_buffer_size; } CodedBitstreamH2645Context; diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c index 0df4234b12..c081a63737 100644 --- a/libavcodec/cbs_mpeg2.c +++ b/libavcodec/cbs_mpeg2.c @@ -298,19 +298,20 @@ static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx, { CodedBitstreamMPEG2Context *priv = ctx->priv_data; PutBitContext pbc; + size_t write_buffer_size; int err; if (!priv->write_buffer) { // Initial write buffer size is 1MB. - priv->write_buffer_size = 1024 * 1024; + write_buffer_size = 1024 * 1024; reallocate_and_try_again: - err = av_reallocp(&priv->write_buffer, priv->write_buffer_size); - if (err < 0) { + av_fast_malloc(&priv->write_buffer, &priv->write_buffer_size, write_buffer_size); + if (!priv->write_buffer) { av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a " "sufficiently large write buffer (last attempt " - "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size); - return err; + "%"SIZE_SPECIFIER" bytes).\n", write_buffer_size); + return AVERROR(ENOMEM); } } @@ -323,7 +324,7 @@ static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx, if (err == AVERROR(ENOSPC)) { // Overflow. - priv->write_buffer_size *= 2; + write_buffer_size = priv->write_buffer_size * 2; goto reallocate_and_try_again; } if (err < 0) { diff --git a/libavcodec/cbs_mpeg2.h b/libavcodec/cbs_mpeg2.h index 92caa99dc1..824e15b524 100644 --- a/libavcodec/cbs_mpeg2.h +++ b/libavcodec/cbs_mpeg2.h @@ -222,7 +222,7 @@ typedef struct CodedBitstreamMPEG2Context { // Write buffer. uint8_t *write_buffer; - size_t write_buffer_size; + unsigned write_buffer_size; } CodedBitstreamMPEG2Context; diff --git a/libavcodec/cbs_vp9.c b/libavcodec/cbs_vp9.c index 7498be4b73..05bf2da66b 100644 --- a/libavcodec/cbs_vp9.c +++ b/libavcodec/cbs_vp9.c @@ -514,19 +514,20 @@ static int cbs_vp9_write_unit(CodedBitstreamContext *ctx, CodedBitstreamVP9Context *priv = ctx->priv_data; VP9RawFrame *frame = unit->content; PutBitContext pbc; + size_t write_buffer_size; int err; if (!priv->write_buffer) { // Initial write buffer size is 1MB. - priv->write_buffer_size = 1024 * 1024; + write_buffer_size = 1024 * 1024; reallocate_and_try_again: - err = av_reallocp(&priv->write_buffer, priv->write_buffer_size); - if (err < 0) { + av_fast_malloc(&priv->write_buffer, &priv->write_buffer_size, write_buffer_size); + if (!priv->write_buffer) { av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a " "sufficiently large write buffer (last attempt " - "%zu bytes).\n", priv->write_buffer_size); - return err; + "%"SIZE_SPECIFIER" bytes).\n", write_buffer_size); + return AVERROR(ENOMEM); } } @@ -534,7 +535,7 @@ static int cbs_vp9_write_unit(CodedBitstreamContext *ctx, err = cbs_vp9_write_frame(ctx, &pbc, frame); if (err == AVERROR(ENOSPC)) { - priv->write_buffer_size *= 2; + write_buffer_size = priv->write_buffer_size * 2; goto reallocate_and_try_again; } if (err < 0) @@ -550,7 +551,7 @@ static int cbs_vp9_write_unit(CodedBitstreamContext *ctx, if (frame->data) { if (unit->data_size + frame->data_size > priv->write_buffer_size) { - priv->write_buffer_size *= 2; + write_buffer_size = priv->write_buffer_size * 2; goto reallocate_and_try_again; } diff --git a/libavcodec/cbs_vp9.h b/libavcodec/cbs_vp9.h index 5b99c90c2e..b517e4345a 100644 --- a/libavcodec/cbs_vp9.h +++ b/libavcodec/cbs_vp9.h @@ -194,7 +194,7 @@ typedef struct CodedBitstreamVP9Context { // Write buffer. uint8_t *write_buffer; - size_t write_buffer_size; + unsigned write_buffer_size; } CodedBitstreamVP9Context;