From patchwork Wed Nov 27 12:22:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16450 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 68302448C25 for ; Wed, 27 Nov 2019 14:22:26 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4103B68B091; Wed, 27 Nov 2019 14:22:26 +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 0C8FB68B04F for ; Wed, 27 Nov 2019 14:22:20 +0200 (EET) Received: by mail-wm1-f67.google.com with SMTP id j18so4775924wmk.1 for ; Wed, 27 Nov 2019 04:22:20 -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=L0FCYF8M6E/3D5wODgozRI4reSlzigev3Gy3KJLbMyM=; b=jqkrioKMk60D0iEX5slOHs85SaTs+/YZ3441LjgyrCUXmVOxPW4sbzom/kl2JwIHsh 5G9M8kPjOF550WYjSBjwy+4ssz6qIbPgV1xpCsGY8KTF6t19Pc0bvB0kpn/mHmLhddm7 vLuRj+O1zA2UPiVTimLLer2/R88I3qRotDAheorD+lQqrG2orjGPz4CD5L4ILaqLSZ+U ZNHr7oS3OCBbNoz5u8bDBMIxWpygc7W6CU6tKsGzO3CeOZDsDQGnkyNTq42IfrvLHgG/ EYFHuSS9EnxKVNigC+B9wJwYQ1moPbhrRmvbU18prTLu0pf+NHoVPEdIRjJ6mjy+WAGy wKhQ== 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=L0FCYF8M6E/3D5wODgozRI4reSlzigev3Gy3KJLbMyM=; b=hXsr3Robp4dDdL++XU2rIBXQe+lZK1KrJzGrbkJtQzftq7JjjANYHGDzfvv6ypOQAA U5F06YrYh/nBbUhdjTALMnkEgmxoBoVgAWygoYA93QzFvOwROpb9eXlExcteC0taEU5Q /tCtZykKlLcTY5nxA8at5KyFRrTR5LQe7LBxiyzgp+EcwL0tuunpCjXTL5ZEvmTjuyyr beU2QK8dEPUSs5TGl+Agg36ZzTdcStiTnCPz/ONhNOBDoSMLL6r0sHLhXVFMVfvIAV2U KGK1I1SX0+EbFO1d3tPJ4BMwbS5v13vsLAwkmmaHR3sfJOUYa37TNFeh9ITePOZeDYB6 DJTQ== X-Gm-Message-State: APjAAAVKNMNP5orrbqfmCPtje5omQsiBJymQ5yZpWv6rRBCoFD+4bohz VD2Dj0P5l85BUcyan1DB2XLs4u7d X-Google-Smtp-Source: APXvYqzIxmBv5t8xqEVSlmF5A6cENJwXN4V28bDxz2ErpEXMRxaAYDFI3UsrvPXvkjuc1Qwz/P0fNQ== X-Received: by 2002:a1c:f00a:: with SMTP id a10mr3996280wmb.167.1574857339173; Wed, 27 Nov 2019 04:22:19 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08e23.dynamic.kabel-deutschland.de. [188.192.142.35]) by smtp.gmail.com with ESMTPSA id r2sm6905614wma.44.2019.11.27.04.22.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2019 04:22:18 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 27 Nov 2019 13:22:05 +0100 Message-Id: <20191127122211.6352-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/7] avformat/aviobuf: Avoid allocating buffer when using dynamic buffer 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" Up until now, using a dynamic buffer entailed at least three allocations: One for the AVIOContext, one for the AVIOContext's opaque (which, among other things, contains the small write buffer), and one for the big buffer that is independently allocated that is returned when calling avio_close_dyn_buf(). It is possible to avoid the third allocation if one doesn't use a packetized dynamic buffer, if all the data written so far fit into the write buffer and if one does not require the actual (big) buffer to have an indefinite lifetime. This is done by making avio_get_dyn_buf() return a pointer to the data in the write buffer if nothing has been written to the main buffer yet. The dynamic buffer will then be freed using ffio_free_dynamic_buffer (which needed to be modified not to call avio_close_dyn_buf() internally). So a typical use-case like: size = avio_close_dyn_buf(dyn_pb, &buf); do something with buf av_free(buf); can be converted to: size = avio_get_dyn_buf(dyn_pb, &buf); do something with buf ffio_free_dynamic_buffer(&dyn_pb); In more complex scenarios this can simplify freeing as well, because it is now clear that freeing always has to be performed via ffio_free_dynamic_buffer(). Of course, in case this saves an allocation it also saves a memcpy. Signed-off-by: Andreas Rheinhardt --- libavformat/aviobuf.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 2dfce8af8b..70e1d2ca10 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -1443,14 +1443,19 @@ int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer) { DynBuffer *d; - if (!s) { + if (!s || s->error) { *pbuffer = NULL; return 0; } + d = s->opaque; + + if (!d->size) { + *pbuffer = d->io_buffer; + return FFMAX(s->buf_ptr, s->buf_ptr_max) - s->buffer; + } avio_flush(s); - d = s->opaque; *pbuffer = d->buffer; return d->size; @@ -1488,12 +1493,15 @@ int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer) void ffio_free_dyn_buf(AVIOContext **s) { - uint8_t *tmp; + DynBuffer *d; + if (!*s) return; - avio_close_dyn_buf(*s, &tmp); - av_free(tmp); - *s = NULL; + + d = (*s)->opaque; + av_free(d->buffer); + av_free(d); + avio_context_free(s); } static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)