From patchwork Tue Feb 5 20:08:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 11975 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 57AFB448EC1 for ; Tue, 5 Feb 2019 22:10:32 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3666868AA86; Tue, 5 Feb 2019 22:10:32 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5978468A0A0 for ; Tue, 5 Feb 2019 22:10:26 +0200 (EET) Received: by mail-wr1-f65.google.com with SMTP id l9so5034396wrt.13 for ; Tue, 05 Feb 2019 12:10:26 -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=SA2W/PBpU4a5ENs255PAZ1LvjkFzvBRmjokiQ8iHEUA=; b=A0xLr3NOVbjuFXIcWxeRL5hhrgAt6sYr7Qw1Di6qExdaWYcFEWrkHwCrYAeedwSHHE lCNoAn3+xJki/gwBONMqxRzgG1yW0y/ih+aXSCguY1CHWjc1cL6GdPo/lY7weBHMRtu/ AQ/fSHZsnOcIQlkuL6gjtd3/zjYbZQALQXin1NOkv+tx/xP+jyvcqyBTTPv6X4XUNq3o 2H+PhY0D6CmuV3lwrI/PLkD6u+X+O8dns6xCZbN1zQRhsCDzm3ZIOiuvhS6liXK1IIae hlQltbNSTdsffuvIdx1+yMUCYeYkjuq6XkXX7l/PyFAC0qCJ5cpqmWgrIA51NID+z9pT ZXMQ== 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=SA2W/PBpU4a5ENs255PAZ1LvjkFzvBRmjokiQ8iHEUA=; b=GfdAKDespQPNPtTM1RzUyVfd3oFvzF1WwpSdF1MsPTdYlrn5cc2gRwwaX2pDnO8Kus j5DOLC9+q7ix5Ofi6JkuVWVpVklJATV73k4+0uvyh2SidFaISEylZ436XH0pDu1MUGqv Y6A0khKtGSnyAcj6JnYRICgBcGNrR6FFrJqFNqh6pZc0TCmS6zbijkAEWTI6g0ELYIzV InZ/KKEj5dIF7Bxs7tiTxExrAURTXkr3A5TL2ROEbGrHVX76EkQFW2VNBxv8/xX+5Vv0 nt52+wtc3kzN3ZKU5sc+KS9dMQBuOHiz/917e5TLkEftRbzD8XIscseG+jJr4qqLJ7Ax Pq2Q== X-Gm-Message-State: AHQUAuZbZdmsxp0AA6tDQk0oNHjHJQAmhm1KVlYpmrUk5bbZOhMQOJSL hmQGozWeh4dsF9vB7NQ3mncfYuytQ68= X-Google-Smtp-Source: AHgI3IaIaRYx6gQ8JrKO3OI655ggHJ/+R8HaAkMc6MeQdqqxvej8Z9Cw5JaSiN49FabBuDLcMD3vrg== X-Received: by 2002:adf:f703:: with SMTP id r3mr4914254wrp.93.1549397425379; Tue, 05 Feb 2019 12:10:25 -0800 (PST) Received: from localhost.localdomain (ipbcc08c44.dynamic.kabel-deutschland.de. [188.192.140.68]) by smtp.googlemail.com with ESMTPSA id x3sm25570144wrd.19.2019.02.05.12.10.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Feb 2019 12:10:24 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 5 Feb 2019 21:08:52 +0100 Message-Id: <20190205200852.2160-4-andreas.rheinhardt@googlemail.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190205200852.2160-1-andreas.rheinhardt@googlemail.com> References: <20190205200852.2160-1-andreas.rheinhardt@googlemail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3] cbs: Stop reallocating fragment unit arrays 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" This commit changes various places that make use of cbs to keep the fragments' unit arrays instead of constantly reallocating them. The more units a packet is split into, the bigger the benefit. So MPEG-2 benefits the most; for a video coming from an NTSC-DVD (usually 32 units per frame) the average cost of cbs_insert_unit (for a single unit) went down from 6717 decicycles to 450 decicycles (based upon 10 runs with 4194304 runs each); if each packet consists of only one unit, it went down from 2425 to 448; for a H.264 video where most packets contain nine units, it went from 4431 to 450. Signed-off-by: Andreas Rheinhardt --- libavcodec/av1_metadata_bsf.c | 6 ++++-- libavcodec/av1_parser.c | 5 +++-- libavcodec/filter_units_bsf.c | 13 +++++++------ libavcodec/h264_metadata_bsf.c | 6 ++++-- libavcodec/h264_redundant_pps_bsf.c | 6 ++++-- libavcodec/h265_metadata_bsf.c | 6 ++++-- libavcodec/mpeg2_metadata_bsf.c | 6 ++++-- libavcodec/trace_headers_bsf.c | 14 ++++++++------ libavcodec/vp9_metadata_bsf.c | 4 +++- 9 files changed, 41 insertions(+), 25 deletions(-) diff --git a/libavcodec/av1_metadata_bsf.c b/libavcodec/av1_metadata_bsf.c index b08a1379e7..5e4dc10b89 100644 --- a/libavcodec/av1_metadata_bsf.c +++ b/libavcodec/av1_metadata_bsf.c @@ -170,7 +170,7 @@ static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *out) err = 0; fail: - ff_cbs_fragment_uninit(ctx->cbc, frag, 1); + ff_cbs_fragment_uninit(ctx->cbc, frag, 0); if (err < 0) av_packet_unref(out); @@ -215,13 +215,15 @@ static int av1_metadata_init(AVBSFContext *bsf) err = 0; fail: - ff_cbs_fragment_uninit(ctx->cbc, frag, 1); + ff_cbs_fragment_uninit(ctx->cbc, frag, 0); return err; } static void av1_metadata_close(AVBSFContext *bsf) { AV1MetadataContext *ctx = bsf->priv_data; + + ff_cbs_fragment_uninit(ctx->cbc, &ctx->access_unit, 1); ff_cbs_close(&ctx->cbc); } diff --git a/libavcodec/av1_parser.c b/libavcodec/av1_parser.c index 071227eed6..9e26f6517a 100644 --- a/libavcodec/av1_parser.c +++ b/libavcodec/av1_parser.c @@ -72,7 +72,7 @@ static int av1_parser_parse(AVCodecParserContext *ctx, goto end; } - ff_cbs_fragment_uninit(s->cbc, td, 1); + ff_cbs_fragment_uninit(s->cbc, td, 0); } ret = ff_cbs_read(s->cbc, td, data, size, 1); @@ -159,7 +159,7 @@ static int av1_parser_parse(AVCodecParserContext *ctx, } end: - ff_cbs_fragment_uninit(s->cbc, td, 1); + ff_cbs_fragment_uninit(s->cbc, td, 0); s->cbc->log_ctx = NULL; @@ -193,6 +193,7 @@ static void av1_parser_close(AVCodecParserContext *ctx) { AV1ParseContext *s = ctx->priv_data; + ff_cbs_fragment_uninit(s->cbc, &s->temporal_unit, 1); ff_cbs_close(&s->cbc); } diff --git a/libavcodec/filter_units_bsf.c b/libavcodec/filter_units_bsf.c index 9980601ce5..440b9b6d58 100644 --- a/libavcodec/filter_units_bsf.c +++ b/libavcodec/filter_units_bsf.c @@ -139,7 +139,7 @@ static int filter_units_filter(AVBSFContext *bsf, AVPacket *out) // Don't return packets with nothing in them. av_packet_free(&in); - ff_cbs_fragment_uninit(ctx->cbc, frag, 1); + ff_cbs_fragment_uninit(ctx->cbc, frag, 0); } err = ff_cbs_write_packet(ctx->cbc, out, frag); @@ -153,7 +153,7 @@ static int filter_units_filter(AVBSFContext *bsf, AVPacket *out) goto fail; fail: - ff_cbs_fragment_uninit(ctx->cbc, frag, 1); + ff_cbs_fragment_uninit(ctx->cbc, frag, 0); av_packet_free(&in); return err; @@ -199,18 +199,18 @@ static int filter_units_init(AVBSFContext *bsf) ctx->cbc->nb_decompose_unit_types = 0; if (bsf->par_in->extradata) { - CodedBitstreamFragment ps; + CodedBitstreamFragment *frag = &ctx->fragment; - err = ff_cbs_read_extradata(ctx->cbc, &ps, bsf->par_in, 0); + err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in, 1); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n"); } else { - err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, &ps); + err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, frag); if (err < 0) av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n"); } - ff_cbs_fragment_uninit(ctx->cbc, &ps, 1); + ff_cbs_fragment_uninit(ctx->cbc, frag, 0); } return err; @@ -222,6 +222,7 @@ static void filter_units_close(AVBSFContext *bsf) av_freep(&ctx->type_list); + ff_cbs_fragment_uninit(ctx->cbc, &ctx->fragment, 1); ff_cbs_close(&ctx->cbc); } diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c index 53ba623b70..37a5c9a9b1 100644 --- a/libavcodec/h264_metadata_bsf.c +++ b/libavcodec/h264_metadata_bsf.c @@ -604,7 +604,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out) err = 0; fail: - ff_cbs_fragment_uninit(ctx->cbc, au, 1); + ff_cbs_fragment_uninit(ctx->cbc, au, 0); av_freep(&displaymatrix_side_data); if (err < 0) @@ -648,13 +648,15 @@ static int h264_metadata_init(AVBSFContext *bsf) err = 0; fail: - ff_cbs_fragment_uninit(ctx->cbc, au, 1); + ff_cbs_fragment_uninit(ctx->cbc, au, 0); return err; } static void h264_metadata_close(AVBSFContext *bsf) { H264MetadataContext *ctx = bsf->priv_data; + + ff_cbs_fragment_uninit(ctx->cbc, &ctx->access_unit, 1); ff_cbs_close(&ctx->cbc); } diff --git a/libavcodec/h264_redundant_pps_bsf.c b/libavcodec/h264_redundant_pps_bsf.c index 1df93dd121..4c3d020b27 100644 --- a/libavcodec/h264_redundant_pps_bsf.c +++ b/libavcodec/h264_redundant_pps_bsf.c @@ -118,7 +118,7 @@ static int h264_redundant_pps_filter(AVBSFContext *bsf, AVPacket *out) err = 0; fail: - ff_cbs_fragment_uninit(ctx->output, au, 1); + ff_cbs_fragment_uninit(ctx->output, au, 0); av_packet_free(&in); if (err < 0) av_packet_unref(out); @@ -167,7 +167,7 @@ static int h264_redundant_pps_init(AVBSFContext *bsf) err = 0; fail: - ff_cbs_fragment_uninit(ctx->output, au, 1); + ff_cbs_fragment_uninit(ctx->output, au, 0); return err; } @@ -180,6 +180,8 @@ static void h264_redundant_pps_flush(AVBSFContext *bsf) static void h264_redundant_pps_close(AVBSFContext *bsf) { H264RedundantPPSContext *ctx = bsf->priv_data; + + ff_cbs_fragment_uninit(ctx->output, &ctx->access_unit, 1); ff_cbs_close(&ctx->input); ff_cbs_close(&ctx->output); } diff --git a/libavcodec/h265_metadata_bsf.c b/libavcodec/h265_metadata_bsf.c index 53d35f0e09..d7c44c90ce 100644 --- a/libavcodec/h265_metadata_bsf.c +++ b/libavcodec/h265_metadata_bsf.c @@ -322,7 +322,7 @@ static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *out) err = 0; fail: - ff_cbs_fragment_uninit(ctx->cbc, au, 1); + ff_cbs_fragment_uninit(ctx->cbc, au, 0); if (err < 0) av_packet_unref(out); @@ -370,13 +370,15 @@ static int h265_metadata_init(AVBSFContext *bsf) err = 0; fail: - ff_cbs_fragment_uninit(ctx->cbc, au, 1); + ff_cbs_fragment_uninit(ctx->cbc, au, 0); return err; } static void h265_metadata_close(AVBSFContext *bsf) { H265MetadataContext *ctx = bsf->priv_data; + + ff_cbs_fragment_uninit(ctx->cbc, &ctx->access_unit, 1); ff_cbs_close(&ctx->cbc); } diff --git a/libavcodec/mpeg2_metadata_bsf.c b/libavcodec/mpeg2_metadata_bsf.c index 43e6ac8c15..69e794c0ee 100644 --- a/libavcodec/mpeg2_metadata_bsf.c +++ b/libavcodec/mpeg2_metadata_bsf.c @@ -214,7 +214,7 @@ static int mpeg2_metadata_filter(AVBSFContext *bsf, AVPacket *out) err = 0; fail: - ff_cbs_fragment_uninit(ctx->cbc, frag, 1); + ff_cbs_fragment_uninit(ctx->cbc, frag, 0); if (err < 0) av_packet_unref(out); @@ -255,13 +255,15 @@ static int mpeg2_metadata_init(AVBSFContext *bsf) err = 0; fail: - ff_cbs_fragment_uninit(ctx->cbc, frag, 1); + ff_cbs_fragment_uninit(ctx->cbc, frag, 0); return err; } static void mpeg2_metadata_close(AVBSFContext *bsf) { MPEG2MetadataContext *ctx = bsf->priv_data; + + ff_cbs_fragment_uninit(ctx->cbc, &ctx->fragment, 1); ff_cbs_close(&ctx->cbc); } diff --git a/libavcodec/trace_headers_bsf.c b/libavcodec/trace_headers_bsf.c index c6a47e6d6d..3062c529c8 100644 --- a/libavcodec/trace_headers_bsf.c +++ b/libavcodec/trace_headers_bsf.c @@ -28,6 +28,7 @@ typedef struct TraceHeadersContext { CodedBitstreamContext *cbc; + CodedBitstreamFragment fragment; } TraceHeadersContext; @@ -44,13 +45,13 @@ static int trace_headers_init(AVBSFContext *bsf) ctx->cbc->trace_level = AV_LOG_INFO; if (bsf->par_in->extradata) { - CodedBitstreamFragment ps; + CodedBitstreamFragment *frag = &ctx->fragment; av_log(bsf, AV_LOG_INFO, "Extradata\n"); - err = ff_cbs_read_extradata(ctx->cbc, &ps, bsf->par_in, 0); + err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in, 1); - ff_cbs_fragment_uninit(ctx->cbc, &ps, 1); + ff_cbs_fragment_uninit(ctx->cbc, frag, 0); } return err; @@ -60,13 +61,14 @@ static void trace_headers_close(AVBSFContext *bsf) { TraceHeadersContext *ctx = bsf->priv_data; + ff_cbs_fragment_uninit(ctx->cbc, &ctx->fragment, 1); ff_cbs_close(&ctx->cbc); } static int trace_headers(AVBSFContext *bsf, AVPacket *pkt) { TraceHeadersContext *ctx = bsf->priv_data; - CodedBitstreamFragment au; + CodedBitstreamFragment *frag = &ctx->fragment; char tmp[256] = { 0 }; int err; @@ -92,9 +94,9 @@ static int trace_headers(AVBSFContext *bsf, AVPacket *pkt) av_log(bsf, AV_LOG_INFO, "Packet: %d bytes%s.\n", pkt->size, tmp); - err = ff_cbs_read_packet(ctx->cbc, &au, pkt, 0); + err = ff_cbs_read_packet(ctx->cbc, frag, pkt, 1); - ff_cbs_fragment_uninit(ctx->cbc, &au, 1); + ff_cbs_fragment_uninit(ctx->cbc, frag, 0); if (err < 0) av_packet_unref(pkt); diff --git a/libavcodec/vp9_metadata_bsf.c b/libavcodec/vp9_metadata_bsf.c index 26f1e65f20..f67f288289 100644 --- a/libavcodec/vp9_metadata_bsf.c +++ b/libavcodec/vp9_metadata_bsf.c @@ -86,7 +86,7 @@ static int vp9_metadata_filter(AVBSFContext *bsf, AVPacket *out) err = 0; fail: - ff_cbs_fragment_uninit(ctx->cbc, frag, 1); + ff_cbs_fragment_uninit(ctx->cbc, frag, 0); if (err < 0) av_packet_unref(out); @@ -105,6 +105,8 @@ static int vp9_metadata_init(AVBSFContext *bsf) static void vp9_metadata_close(AVBSFContext *bsf) { VP9MetadataContext *ctx = bsf->priv_data; + + ff_cbs_fragment_uninit(ctx->cbc, &ctx->fragment, 1); ff_cbs_close(&ctx->cbc); }