From patchwork Sun Sep 22 16:33:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Tomas_H=C3=A4rdin?= X-Patchwork-Id: 51695 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:d154:0:b0:48e:c0f8:d0de with SMTP id bt20csp2039654vqb; Sun, 22 Sep 2024 09:34:04 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW9CC+tpEKWuLPXqVrP8fZdl4LQBMHYVpQe9xNQnCiQpSHyK2FpgE632BdqrxcU4r3VmgJtTYxwXamcp4Ox70u+@gmail.com X-Google-Smtp-Source: AGHT+IGj++y5ihpcfL58W8c9i9lwSXR+DMBdKtOiBahIJiDLowT6hGeegFOFHsXGQHH/x1se0JEw X-Received: by 2002:a05:6512:a8f:b0:536:a4da:8d86 with SMTP id 2adb3069b0e04-536ac2e0382mr4072551e87.15.1727022843788; Sun, 22 Sep 2024 09:34:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1727022843; cv=none; d=google.com; s=arc-20240605; b=NzkS36qDGEAlNN+DcNk+IpcqESLiLuhwqeLIXxJWj5bv2Xs05RFiSQKU3+vwLbFusO hq+pNACom5zieFtm++ovh4de0LX99+oEtSEUvloQUGSJpkdf5eMkIeY6+1JYYYPhHQAN PWZBHXsMDyM+a0yOK/Tq931oo+KOoXwUTWRw3G37c248njN2noEM3Q2RsmjZy5NVAFEj 5C6tsu3xL+eUPqfxtuUZq7YinC9aP668ysRELx/NpVNeIsgRxQj4DMT091ynblrK2/+p tP21tCiWCf5dzJroFOkR08FRqPHMEEUbMEPI7MTJ5cLN3oViiTabcgcVyT5kFbJsaX/y MUjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:user-agent:date:to:from:message-id:delivered-to; bh=bAeB3Iw5olyUHpw7oplRRaRj3kBIeftf81Tj+mhZN50=; fh=e5zN9xSzcxLA6bGo3lF+CqTbY/oLwzApV03EO/RBfgQ=; b=MsT4P/YIGDSkMRjh4t/2bAzj9keAQiBDwDgjFOhRxrJkRX6XHxT4y6z2TeOyCuNWsT hjm6rq0d4H8RDKPU1UrJjuc4M73SPU/aK/VAgZYbAVznWOGlTZFHc6vORH/83jzAIZwE ycCKDCrN1DfFRlH+rG7QT9aTK387AI2AdC6Cy+TfxJoZeMZWb4KzMcHopzfJhhAshHvM Z5NFy2HDvueQAdtOD37UUl7V3XD4ulAUJqyoNqSI7XDz+e6QX/oZWjvBJZjcmATkBjKf z0AikvW0s7GEmI00HQubnoX/g5uI/UBGv4t5uRpBXC5KdBzmGuMTrqKjOrAJQoBmY9Rs acbg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 2adb3069b0e04-5368709debcsi5852971e87.378.2024.09.22.09.34.03; Sun, 22 Sep 2024 09:34:03 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7A23D68DA9B; Sun, 22 Sep 2024 19:33:58 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from glom.nmugroup.com (glom.nmugroup.com [193.183.80.6]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EF6FA68D9E2 for ; Sun, 22 Sep 2024 19:33:50 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by glom.nmugroup.com (Postfix) with ESMTP id 87FC45422B58 for ; Sun, 22 Sep 2024 18:33:50 +0200 (CEST) Received: from debian.lan (unknown [IPv6:2a00:66c0:a::72c]) (Authenticated sender: git01) by glom.nmugroup.com (Postfix) with ESMTPSA id 4AE68542288C for ; Sun, 22 Sep 2024 18:33:50 +0200 (CEST) Message-ID: <1de0719456ef2f015b83d214d73334853af26612.camel@haerdin.se> From: Tomas =?iso-8859-1?q?H=E4rdin?= To: FFmpeg development discussions and patches Date: Sun, 22 Sep 2024 18:33:49 +0200 User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/5] lavf/mxfdec: Speed up klv_read_packet() X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: blIetO8zdrjp This patchset speeds up mxfdec in various ways. The test file has been generated with ffmpeg -t 10000 -f lavfi -i testsrc -s 160x120 out2.mxf Performance is measured with callgrind using the command valgrind --tool=callgrind ./ffmpeg_g -loglevel quiet -i out2.mxf -codec copy -f null - The callgraph has then been inspected using kcachegrind. The results are as follows: task_wrapper 5 812 306 937 a577d31 5 669 552 343 Speed up klv_read_packet() 5 648 440 947 Add and use IS_KLV_KEY_FAST() in some places 5 633 846 074 Add and use mxf_is_encrypted_triplet_key() 3 667 721 703 Speed up mxf_edit_unit_absolute_offset() 3 587 869 726 Remove a call to avio_tell() in klv_read_packet() mxf_read_packet (250 001 calls) 3 821 662 859 a577d31 3 665 058 265 Speed up klv_read_packet() 3 647 320 931 Add and use IS_KLV_KEY_FAST() in some places 3 624 081 036 Add and use mxf_is_encrypted_triplet_key() 1 660 495 552 Speed up mxf_edit_unit_absolute_offset() 1 592 469 709 Remove a call to avio_tell() in klv_read_packet() The biggest difference is made by speeding up mxf_edit_unit_absolute_offset(). Here's how many cycles it uses before and after patch 4: mxf_edit_unit_absolute_offset.constprop.31 2 076 774 255 Add and use mxf_is_encrypted_triplet_key() 105 047 878 Speed up mxf_edit_unit_absolute_offset() Of the remaining cycles in mxf_edit_unit_absolute_offset(), 67 882 294 are spent in mxf_absolute_bodysid_offset() (272 cycles per call). Since it already does a binary search it didn't seem worthwhile to mess with. Patches 2 and 3 are somewhat dubious, but I've included them anyway to get some feedback. We could both speed up the demuxer and cut down on .text by omitting the first 4 bytes of every key. /Tomas From da4daac750955ccdf578c703fca7a90c93f7a1a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Sat, 14 Sep 2024 11:48:09 +0200 Subject: [PATCH 1/5] lavf/mxfdec: Speed up klv_read_packet() --- libavformat/mxfdec.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index 24f4ed1c33..99bf352e00 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -458,10 +458,26 @@ static int mxf_read_sync(AVIOContext *pb, const uint8_t *key, unsigned size) return i == size; } +// special case of mxf_read_sync for mxf_klv_key +static int mxf_read_sync_klv(AVIOContext *pb) +{ + uint32_t key = avio_rb32(pb); + // key will never match mxf_klv_key on EOF + if (key == AV_RB32(mxf_klv_key)) + return 1; + + while (!avio_feof(pb)) { + key = (key << 8) | avio_r8(pb); + if (key == AV_RB32(mxf_klv_key)) + return 1; + } + return 0; +} + static int klv_read_packet(MXFContext *mxf, KLVPacket *klv, AVIOContext *pb) { int64_t length, pos; - if (!mxf_read_sync(pb, mxf_klv_key, 4)) + if (!mxf_read_sync_klv(pb)) return AVERROR_INVALIDDATA; klv->offset = avio_tell(pb) - 4; if (klv->offset < mxf->run_in) @@ -3982,6 +3998,7 @@ static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt) ret = klv_read_packet(mxf, &klv, s->pb); if (ret < 0) break; + // klv.key[0..3] == mxf_klv_key from here forward max_data_size = klv.length; pos = klv.next_klv - klv.length; PRINT_KEY(s, "read packet", klv.key); -- 2.39.2