From patchwork Tue Nov 20 11:38:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 11084 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 59A3844C76D for ; Tue, 20 Nov 2018 13:39:49 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CFE20689EED; Tue, 20 Nov 2018 13:39:49 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 21B1B688396 for ; Tue, 20 Nov 2018 13:39:44 +0200 (EET) Received: by mail-wm1-f46.google.com with SMTP id u13-v6so1867899wmc.4 for ; Tue, 20 Nov 2018 03:39:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lVw7b2YV0s1LSzhhWO8fA7bdgnzjo06YbCluffW/8yM=; b=jBaO65FaBdrnbeUt8uda1GjdfYY52qhkTncCd1/sY6ZYCOhz2zlusbhQLH6I5sT6W8 +3cu+C7qVIddLiO/qi92raYb5DHvHC1iRiAWQ5HOAOGXW0mewyE/e99jmbdhytEQrUVU vzgOp8NX896HWvPKdbdBfFqv+3h/4xv9WxSpM+eF7WdjTlNu3vTc2pMU6w4d6CO7GgRx xcc2ZD4pb+gb0EU9DiSryyjWtF4+xZdrEADRvRIhmYnYhVIsYLWkui7Y4PAh6pPLVxw6 F3IXJKV0TS3vuUyz1hx+y+auUmV2P42zlTt1Ghwh4AME9qxwu7/Aly3WAsra2wD62xXW 7n/A== 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=lVw7b2YV0s1LSzhhWO8fA7bdgnzjo06YbCluffW/8yM=; b=LXlDFjQPF6+i8YPECysVb2xnbpnjaAc7tFOPTGfgGWIjIPn5yIyYF3ZYXa9d+ICny5 9tUd+66N2f14XfyD2BAeeXoFz6IAVoOUhOEaUeTl+JH3NS3R/Os/ilWSaCyjnw6t6kAF jgUTXrLur2vAu5K0XksYce2DpXQD/sTZKAmKTPWXXYyZNYPKsR7o+02DoOcnw0xehOgp l0qvjWLnj0cyXrgB3cxTBocuj7HHlWVoIYqqw68YZdR36TtYI6nfKmaBeTnxnZjXe/An Q8+UlQFWA27ADdwU8c7cntT+/3PakdHgdIf1tKGPk9BX2kguT1c36SqHs3srCEriJYhL /pkg== X-Gm-Message-State: AA+aEWZ+WUf3sPlzOm4txbG2B6yDBqyaI4XPs+PhPsCdGWFD4qmgwHa5 AZuPyMeObpy/15LFDLYBv/51V09L X-Google-Smtp-Source: AFSGD/WNs+v3cH7G6fXyrGi9Ff4NQ160m4ZOf7/ighxOjiBrhe3x9L+boRtUXo5/pgVneARcjzlHsA== X-Received: by 2002:a1c:9e0d:: with SMTP id h13mr1894469wme.150.1542713985416; Tue, 20 Nov 2018 03:39:45 -0800 (PST) Received: from localhost.localdomain (ipbcc08c44.dynamic.kabel-deutschland.de. [188.192.140.68]) by smtp.googlemail.com with ESMTPSA id b13sm13007389wrn.28.2018.11.20.03.39.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Nov 2018 03:39:44 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 20 Nov 2018 12:38:39 +0100 Message-Id: <20181120113839.4512-5-andreas.rheinhardt@googlemail.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181120113839.4512-1-andreas.rheinhardt@googlemail.com> References: <20181120113839.4512-1-andreas.rheinhardt@googlemail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/4] cbs_h2645: Avoid memcpy when splitting fragment #2 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" Now memcpy can be avoided for NAL units containing escapes, too. Particularly improves performance for files with hardcoded black bars. For such a file, time spent in cbs_h2645_split_fragment went down from 369410 decicycles to 327677 decicycles. (It were 379114 decicycles when every NAL unit was copied.) Signed-off-by: Andreas Rheinhardt --- libavcodec/cbs_h2645.c | 54 +++++++++++++++++++----------------------- libavcodec/cbs_h2645.h | 2 ++ 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c index df2b5f3f5e..2543d5f226 100644 --- a/libavcodec/cbs_h2645.c +++ b/libavcodec/cbs_h2645.c @@ -527,35 +527,25 @@ static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const H2645Packet *packet) { + CodedBitstreamH2645Context *h2645 = ctx->priv_data; int err, i; for (i = 0; i < packet->nb_nals; i++) { const H2645NAL *nal = &packet->nals[i]; + AVBufferRef *ref; size_t size = nal->size; // Remove trailing zeroes. while (size > 0 && nal->data[size - 1] == 0) --size; av_assert0(size > 0); - if (nal->data == nal->raw_data) { - err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type, - (uint8_t*)nal->data, size, frag->data_ref); - if (err < 0) - return err; - } else { - uint8_t *data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE); - if (!data) - return AVERROR(ENOMEM); - memcpy(data, nal->data, size); - memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE); + ref = (nal->data == nal->raw_data) ? frag->data_ref + : h2645->rbsp_buffer_ref; - err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type, - data, size, NULL); - if (err < 0) { - av_freep(&data); - return err; - } - } + err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type, + (uint8_t*)nal->data, size, ref); + if (err < 0) + return err; } return 0; @@ -609,9 +599,9 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, } end = bytestream2_tell(&gbc); - err = ff_h2645_packet_split(&priv->read_packet, - frag->data + start, end - start, - ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, NULL); + err = ff_h2645_packet_split(&priv->read_packet, frag->data + start, + end - start, ctx->log_ctx, 1, 2, + AV_CODEC_ID_H264, 1, &priv->rbsp_buffer_ref); if (err < 0) { av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n"); return err; @@ -633,9 +623,9 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, } end = bytestream2_tell(&gbc); - err = ff_h2645_packet_split(&priv->read_packet, - frag->data + start, end - start, - ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, NULL); + err = ff_h2645_packet_split(&priv->read_packet, frag->data + start, + end - start, ctx->log_ctx, 1, 2, + AV_CODEC_ID_H264, 1, &priv->rbsp_buffer_ref); if (err < 0) { av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n"); return err; @@ -687,9 +677,9 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, } end = bytestream2_tell(&gbc); - err = ff_h2645_packet_split(&priv->read_packet, - frag->data + start, end - start, - ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1, NULL); + err = ff_h2645_packet_split(&priv->read_packet, frag->data + start, + end - start, ctx->log_ctx, 1, 2, + AV_CODEC_ID_HEVC, 1, &priv->rbsp_buffer_ref); if (err < 0) { av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split " "HVCC array %d (%d NAL units of type %d).\n", @@ -708,7 +698,7 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, frag->data, frag->data_size, ctx->log_ctx, priv->mp4, priv->nal_length_size, - codec_id, 1, NULL); + codec_id, 1, &priv->rbsp_buffer_ref); if (err < 0) return err; @@ -1510,7 +1500,9 @@ static void cbs_h264_close(CodedBitstreamContext *ctx) CodedBitstreamH264Context *h264 = ctx->priv_data; int i; - ff_h2645_packet_uninit(&h264->common.read_packet, NULL); + ff_h2645_packet_uninit(&h264->common.read_packet, + h264->common.rbsp_buffer_ref); + av_buffer_unref(&h264->common.rbsp_buffer_ref); av_freep(&h264->common.write_buffer); @@ -1525,7 +1517,9 @@ static void cbs_h265_close(CodedBitstreamContext *ctx) CodedBitstreamH265Context *h265 = ctx->priv_data; int i; - ff_h2645_packet_uninit(&h265->common.read_packet, NULL); + ff_h2645_packet_uninit(&h265->common.read_packet, + h265->common.rbsp_buffer_ref); + av_buffer_unref(&h265->common.rbsp_buffer_ref); av_freep(&h265->common.write_buffer); diff --git a/libavcodec/cbs_h2645.h b/libavcodec/cbs_h2645.h index f4cf65bdde..2ce6176454 100644 --- a/libavcodec/cbs_h2645.h +++ b/libavcodec/cbs_h2645.h @@ -33,6 +33,8 @@ typedef struct CodedBitstreamH2645Context { int nal_length_size; // Packet reader. H2645Packet read_packet; + // An AVBufferRef for the rbsp_buffer of read_packet. + AVBufferRef *rbsp_buffer_ref; // Write buffer uint8_t *write_buffer;