From patchwork Wed Nov 21 18:34:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 11112 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 A81FB44C5E9 for ; Wed, 21 Nov 2018 20:36:11 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 29154689F85; Wed, 21 Nov 2018 20:36:12 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0E5A4689F35 for ; Wed, 21 Nov 2018 20:36:09 +0200 (EET) Received: by mail-wr1-f43.google.com with SMTP id l9so6736922wrt.13 for ; Wed, 21 Nov 2018 10:36:12 -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=SAmzYJfoVD5QYFaDjG7z4mwwXI2UzHyFZlnDTJoraIeh+u+DpRTYHz1SWLvwd4IOPv HVmhMmZwieMVfds55oO7Jqnm9OzmJZwWTKIhjuuqa710nwT7v6zqtFSFSDcp7iIAhkye eDfOG2Jc+EfgYYOHhDYKNdo99mUIP6SgBofYKV92XT9NW6cnwg9XZTzp0lOYm1tc/rwA nEcCvbzcRhf3x9Ux9lqFV2uvV0uQHNztu6z8SEHiC54+lR6tvshx9QIxIlEURQjlD4PN /qqaN6/SAvOck+hH4a548uK3x6gIe2LIIaS7dUvHP8KfJdjOo9QqZONhy13v5wqji/DA YonQ== 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=k4ExTwVyYeodM3EN65EglSOTqP8pLxv1ssOK6kkk11058yLTSp/QtdWoUC1Jyo/x4c ROieeTm6BaQrkPreBHFwhYoA4MINvi4lT0FPPLaMME0g7vCRBl+TXwtSerILzgIXRlTK caT0uNADbhjBnwCvc+Zo1a9LwWriH++k3qoZhGJ3wQ/6O/qx5Wl2kqx+Hgg2Qu2l5VSD yGaMGufeMNpVpRWJtHWcMvTO91dO0ZSdvC9WQz0WWYLv+3zevadJMTem3EcowQcTU0mj ef2CJvfwRKqZdheS6XVqsB4lsENyl56mKkBFfqPylq7YkzrGlFoMHmyQgqkMoCldUexm mR5w== X-Gm-Message-State: AA+aEWYXYNUE8tJDIuXg19Do+VILgXyICz5jfIFJ2dTVTf/OizFy9viC +Uk7kQZNwH3XipGO8iCtGQFWudy4 X-Google-Smtp-Source: AFSGD/XzAOe2ghNY9OGTtj7f6eXZRiWIYDHvBvtnOtWAPrVE3G1Hsg+ctItnWMvOZKx/9A9gVWci3Q== X-Received: by 2002:adf:9d85:: with SMTP id p5mr6566255wre.41.1542825371804; Wed, 21 Nov 2018 10:36:11 -0800 (PST) Received: from localhost.localdomain (ipbcc08c44.dynamic.kabel-deutschland.de. [188.192.140.68]) by smtp.googlemail.com with ESMTPSA id 11-v6sm2167079wms.24.2018.11.21.10.36.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Nov 2018 10:36:11 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 21 Nov 2018 19:34:31 +0100 Message-Id: <20181121183431.6404-4-andreas.rheinhardt@googlemail.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181121183431.6404-1-andreas.rheinhardt@googlemail.com> References: <20181121183431.6404-1-andreas.rheinhardt@googlemail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3] 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;