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 From patchwork Sun Sep 22 16:35:21 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: 51699 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:d154:0:b0:48e:c0f8:d0de with SMTP id bt20csp2046712vqb; Sun, 22 Sep 2024 09:54:16 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWgrG+OvOEga6yI7v8BeQxUtE15i70BhN9n/t3KGgcwPeIo34mPW2t0MGR3HObNKcasGlTzrCSIltgjndOAdwlo@gmail.com X-Google-Smtp-Source: AGHT+IGLZLbo5+hIT9an67jMORQtU7dqx1szrBTt1c0y+lZ6+iZ0ccKLtRI451wp6KD/OZ1zwhLT X-Received: by 2002:a17:907:3f9f:b0:a8a:9054:83b5 with SMTP id a640c23a62f3a-a90d4fc3125mr430849366b.3.1727024055822; Sun, 22 Sep 2024 09:54:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1727024055; cv=none; d=google.com; s=arc-20240605; b=CwvfeyHTMwqQdEWrwY5zcNPQxoQJaHYP+NgYKxSxAcBE1gERqZaBcM9ZxswPdJMdhP oGI44DVuXfscEtPLYUq6REkhKBHgQbvNkKcCxnXfqiSF3h9AE/tNK+i1/rmwQvlQOUSJ c8AhJcUwtNiZxGrZuWySIHIKaq5F/uXglkyBGid4eS1yVXhnH6fzAQffjA2ahYkilpDj kn8dL8rfKlDN7R8ylNrpm362vBKxWPZmhjhLUQZqGGzE0IdDSxICF26H1ddG64HVTmYy fIjt067nGW59sgOpqXmveh41pvMkEE+dIoKCHoW3BwOUYMG3x8VQmZeaJtOleF9yEeRn VdLg== 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:references:in-reply-to:date:to:from :message-id:delivered-to; bh=jMp6K4a1gAz9bR/p4bSVCVBNs96OGBUI01WlulWQz6A=; fh=e5zN9xSzcxLA6bGo3lF+CqTbY/oLwzApV03EO/RBfgQ=; b=lybJv7sqtDGA7MbKn9LAIrn1b7Clc5Njc5QzRkV6c8qo8a5f4/D7VrENNxEr+qKKyL Ab1oPxLGnPmS3aAwAdLtJ/b0jUxFuIJ+7w3UvggXxG9FchXtEgP+AVTgn+b82cyRj1gO QIPE8wcDghK6aBde7/YIa8Mh/rir2hpXmr/YSCTAa+EKyuBXJ+MUImATQa+W5e56W0i/ FOk/JY7eWlVoP8jNAHzNOsHamvuYOrZ/076KWaXp/j5JUvaCj3OzrttsbfjqkMBuq34r IUh3n6AKcVfIKg6HsxH98xJCDndbpkSdWlXpo6SZp9J5OpO4COIilMxjwKeXe244yphK oWPA==; 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 a640c23a62f3a-a90612efd31si1267675266b.595.2024.09.22.09.54.15; Sun, 22 Sep 2024 09:54:15 -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 E7A6F68DA7D; Sun, 22 Sep 2024 19:35:29 +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 22E8B68CEEA for ; Sun, 22 Sep 2024 19:35:23 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by glom.nmugroup.com (Postfix) with ESMTP id B93C25423920 for ; Sun, 22 Sep 2024 18:35:22 +0200 (CEST) Received: from debian.lan (unknown [IPv6:2a00:66c0:a::72c]) (Authenticated sender: git01) by glom.nmugroup.com (Postfix) with ESMTPSA id 861175422B58 for ; Sun, 22 Sep 2024 18:35:22 +0200 (CEST) Message-ID: <449952b345f0a0c6a027948396823332a3dfc075.camel@haerdin.se> From: Tomas =?iso-8859-1?q?H=E4rdin?= To: FFmpeg development discussions and patches Date: Sun, 22 Sep 2024 18:35:21 +0200 In-Reply-To: <1de0719456ef2f015b83d214d73334853af26612.camel@haerdin.se> References: <1de0719456ef2f015b83d214d73334853af26612.camel@haerdin.se> User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/5] lavf/mxfdec: Add and use IS_KLV_KEY_FAST() in some places 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: DwhbSULdVe56 This gives some gains, but would probably be nicer if it leveraged the type system by defining a specific type for 12-byte keys so there can be no accidental mixup /Tomas From 367a59ac08624326841bfbecbb5cffb664567d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Sat, 14 Sep 2024 11:48:45 +0200 Subject: [PATCH 2/5] lavf/mxfdec: Add and use IS_KLV_KEY_FAST() in some places --- libavformat/mxfdec.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index 99bf352e00..5fe6918353 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -376,6 +376,8 @@ static const uint8_t mxf_mastering_display_uls[4][16] = { }; #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y))) +// IS_KLV_KEY_FAST can be used when we know the first four bytes are mxf_klv_key +#define IS_KLV_KEY_FAST(x, y) (!memcmp(&x[4], &y[4], sizeof(y) - 4)) static void mxf_free_metadataset(MXFMetadataSet **ctx, enum MXFMetadataSetType type) { @@ -3740,7 +3742,7 @@ static int mxf_read_header(AVFormatContext *s) size_t x; ret = klv_read_packet(mxf, &klv, s->pb); - if (ret < 0 || IS_KLV_KEY(klv.key, ff_mxf_random_index_pack_key)) { + if (ret < 0 || IS_KLV_KEY_FAST(klv.key, ff_mxf_random_index_pack_key)) { if (ret >= 0 && avio_size(s->pb) > klv.next_klv) av_log(s, AV_LOG_WARNING, "data after the RandomIndexPack, assuming end of file\n"); /* EOF - seek to previous partition or stop */ @@ -3753,11 +3755,11 @@ static int mxf_read_header(AVFormatContext *s) PRINT_KEY(s, "read header", klv.key); av_log(s, AV_LOG_TRACE, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset); if (mxf_match_uid(klv.key, mxf_encrypted_triplet_key, sizeof(mxf_encrypted_triplet_key)) || - IS_KLV_KEY(klv.key, mxf_essence_element_key) || - IS_KLV_KEY(klv.key, mxf_canopus_essence_element_key) || - IS_KLV_KEY(klv.key, mxf_avid_essence_element_key) || - IS_KLV_KEY(klv.key, mxf_system_item_key_cp) || - IS_KLV_KEY(klv.key, mxf_system_item_key_gc)) { + IS_KLV_KEY_FAST(klv.key, mxf_essence_element_key) || + IS_KLV_KEY_FAST(klv.key, mxf_canopus_essence_element_key) || + IS_KLV_KEY_FAST(klv.key, mxf_avid_essence_element_key) || + IS_KLV_KEY_FAST(klv.key, mxf_system_item_key_cp) || + IS_KLV_KEY_FAST(klv.key, mxf_system_item_key_gc)) { if (!mxf->current_partition) { av_log(mxf->fc, AV_LOG_ERROR, "found essence prior to first PartitionPack\n"); @@ -3785,7 +3787,7 @@ static int mxf_read_header(AVFormatContext *s) for (x = 0; x < FF_ARRAY_ELEMS(mxf_metadata_read_table); x++) { const MXFMetadataReadTableEntry *metadata = &mxf_metadata_read_table[x]; - if (IS_KLV_KEY(klv.key, metadata->key)) { + if (IS_KLV_KEY_FAST(klv.key, metadata->key)) { if (metadata->read) { if ((ret = mxf_parse_klv(mxf, klv, metadata->read, metadata->ctx_size, metadata->type)) < 0) return ret; @@ -4015,9 +4017,9 @@ static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt) klv = mxf->current_klv_data; max_data_size = klv.next_klv - pos; } - if (IS_KLV_KEY(klv.key, mxf_essence_element_key) || - IS_KLV_KEY(klv.key, mxf_canopus_essence_element_key) || - IS_KLV_KEY(klv.key, mxf_avid_essence_element_key)) { + if (IS_KLV_KEY_FAST(klv.key, mxf_essence_element_key) || + IS_KLV_KEY_FAST(klv.key, mxf_canopus_essence_element_key) || + IS_KLV_KEY_FAST(klv.key, mxf_avid_essence_element_key)) { int body_sid = find_body_sid_by_absolute_offset(mxf, klv.offset); int index = mxf_get_stream_index(s, &klv, body_sid); int64_t next_ofs; -- 2.39.2 From patchwork Sun Sep 22 16:36:42 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: 51697 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:d154:0:b0:48e:c0f8:d0de with SMTP id bt20csp2043417vqb; Sun, 22 Sep 2024 09:44:18 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW9Ic+evh5/0X2KbqCpPvmKyTwSc3gbCIhONUM3qT15gZtKiI5KklwHCr2cL9rP9ZCZgHKRbSLqtRN1Pa7KnG5p@gmail.com X-Google-Smtp-Source: AGHT+IGFmwr2aRv8o5JwCi4wuEmewBUwrxcPs/059Md7qeSIXvC7NjAXJxo+Nk2QNpEmyNH551mq X-Received: by 2002:a17:907:d16:b0:a8d:7b7d:8c47 with SMTP id a640c23a62f3a-a90d5197004mr1026270366b.59.1727023458108; Sun, 22 Sep 2024 09:44:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1727023458; cv=none; d=google.com; s=arc-20240605; b=NmqhXzEiKftJNiX0nik9FwWKOf7aiwbBBuCi5i52pDRrzBvBpinvuJFvytvnUH0IWB 7AuXcdmYfaG4wzx5d85r1TK8YbzkI+mHcdG+NqyZSUqlQqtdidSoI2AW7XwFlPkDJltw q45c/a9PdjISO9ywE/UGnY6mD7RVB4KXRnVRA30Z2tp3Vsz6+sBkthJK3FSYlS8tQ7Yw s+rIiwEdxHCDi74WgpN0tFZT757CRhm8PH04htdi3E34lVWneaJ+BlgTTigQe1iRbj/U eueacZOXG1B00OHDnP7bftrps/afj9Q5fmkW8713bQCLGiukuEkKKoVBrmkMfKA8SfyO j9OQ== 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:references:in-reply-to:date:to:from :message-id:delivered-to; bh=pjk8LzQsVmK7kf6oa23gA9/2gFkF2BSdQjdi4PMXBD8=; fh=e5zN9xSzcxLA6bGo3lF+CqTbY/oLwzApV03EO/RBfgQ=; b=Gxo7sZ2jj5aJG0OU+SrA4fSwp+CmzQZ2SCBbqwZ2kVsYhgbxVBCKcrPu8myxSPFLd9 IxPmUhZoNa4K1LlBDCZivGrL8W8c1IpkDk8CFhugqi50DMkdcjRvgXYpCwnQsUcHV5IO magivXQmIsXIhvc9S09FQ+q2CDYlsAqltnbrnViarLiMDHqQc4RwnZ96c4AXkcRTDw4a IZLcjlfOunvVsP4MTYgnRFdC9xVAehKNapzvpTSjI9VcNafwmuSbe2IeM2G0GcFLr9Nq f6Nfdo9JLYuvI3/AMEujTSbwALhD3iCUwVShbl1Tui1F9Fe+ypEZDOAQpcs4dobBDoLB ZUpg==; 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 a640c23a62f3a-a906109ab76si1256303466b.207.2024.09.22.09.44.17; Sun, 22 Sep 2024 09:44:18 -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 66C8B68DAE2; Sun, 22 Sep 2024 19:36:50 +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 20AA368CEEA for ; Sun, 22 Sep 2024 19:36:44 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by glom.nmugroup.com (Postfix) with ESMTP id AEFB15423920 for ; Sun, 22 Sep 2024 18:36:43 +0200 (CEST) Received: from debian.lan (unknown [IPv6:2a00:66c0:a::72c]) (Authenticated sender: git01) by glom.nmugroup.com (Postfix) with ESMTPSA id 78E4B5422937 for ; Sun, 22 Sep 2024 18:36:43 +0200 (CEST) Message-ID: From: Tomas =?iso-8859-1?q?H=E4rdin?= To: FFmpeg development discussions and patches Date: Sun, 22 Sep 2024 18:36:42 +0200 In-Reply-To: <1de0719456ef2f015b83d214d73334853af26612.camel@haerdin.se> References: <1de0719456ef2f015b83d214d73334853af26612.camel@haerdin.se> User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/5] lavf/mxfdec: Add and use mxf_is_encrypted_triplet_key() 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: dHiSGrc9ERVh mxf_match_uid() is rather slow and gets called a lot. Unrolling it like this saves some cycles, but probably not enough to justify making the code much more verbose /Tomas From ed92d030212e230c7a12d2b265feb470ffd5caa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Sat, 14 Sep 2024 11:19:41 +0200 Subject: [PATCH 3/5] lavf/mxfdec: Add and use mxf_is_encrypted_triplet_key() --- libavformat/mxfdec.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index 5fe6918353..4823aae135 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -1584,6 +1584,25 @@ static int mxf_match_uid(const UID key, const uint8_t uid_prefix[], int len) return 1; } +/* + * Checks if key is mxf_is_encrypted_triplet_key, while also knowing that key[0..3] == mxf_klv_key + * Returns: boolean + */ +static int mxf_is_encrypted_triplet_key(const UID key) +{ + return key[4 ] == mxf_encrypted_triplet_key[4 ] && + key[5 ] == mxf_encrypted_triplet_key[5 ] && + key[6 ] == mxf_encrypted_triplet_key[6 ] && + key[8 ] == mxf_encrypted_triplet_key[8 ] && + key[9 ] == mxf_encrypted_triplet_key[9 ] && + key[10] == mxf_encrypted_triplet_key[10] && + key[11] == mxf_encrypted_triplet_key[11] && + key[12] == mxf_encrypted_triplet_key[12] && + key[13] == mxf_encrypted_triplet_key[13] && + key[14] == mxf_encrypted_triplet_key[14] && + key[15] == mxf_encrypted_triplet_key[15]; +} + static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid) { while (uls->uid[0]) { @@ -3754,7 +3773,7 @@ static int mxf_read_header(AVFormatContext *s) PRINT_KEY(s, "read header", klv.key); av_log(s, AV_LOG_TRACE, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset); - if (mxf_match_uid(klv.key, mxf_encrypted_triplet_key, sizeof(mxf_encrypted_triplet_key)) || + if (mxf_is_encrypted_triplet_key(klv.key) || IS_KLV_KEY_FAST(klv.key, mxf_essence_element_key) || IS_KLV_KEY_FAST(klv.key, mxf_canopus_essence_element_key) || IS_KLV_KEY_FAST(klv.key, mxf_avid_essence_element_key) || @@ -4005,7 +4024,7 @@ static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt) pos = klv.next_klv - klv.length; PRINT_KEY(s, "read packet", klv.key); av_log(s, AV_LOG_TRACE, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset); - if (mxf_match_uid(klv.key, mxf_encrypted_triplet_key, sizeof(mxf_encrypted_triplet_key))) { + if (mxf_is_encrypted_triplet_key(klv.key)) { ret = mxf_decrypt_triplet(s, pkt, &klv); if (ret < 0) { av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n"); -- 2.39.2 From patchwork Sun Sep 22 16:38:05 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: 51696 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:d154:0:b0:48e:c0f8:d0de with SMTP id bt20csp2041262vqb; Sun, 22 Sep 2024 09:38:17 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVCu+T9wn5j/GUGQto81c4WIF0ZMQ0Hd5wv7x4EzZc8VryoF1y6l5dLhWYcK+wNrLntLK9eAY1VsfUIA4H+Qre5@gmail.com X-Google-Smtp-Source: AGHT+IFT/LE6GJ1m/WXm4xCQiHzedFBid75oCG9Q3gz1jEVml9SU6sgqcau8j4a330Zm4jh72t7+ X-Received: by 2002:a05:6512:1105:b0:533:cf5a:eb32 with SMTP id 2adb3069b0e04-536ac2e1c4dmr3998405e87.19.1727023097171; Sun, 22 Sep 2024 09:38:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1727023097; cv=none; d=google.com; s=arc-20240605; b=DbyJbCqgAy7Ngx5EoGaRXcCLObQ1onbjdDSzCZo3lqGN/KVRwDmtt1qVTfoDm8WblH cR7ZbpCbRa1uvLjz2DCPyiDebcNlUTI/SAGLEJ2/3otaKAogCv0g5A5NiCxJW559F2Ze wHFG34hr3SQL0hLKH4vtx6g/vjX6dhjCUla3TvhnKMThJinLLEcTraFffh6nldeavbYX FxNDloaZII+kqa9FerSfBKOIPbYJd0SisLuRnQD0uwerbw/D1YTBzN7rrDObqUGStldO 0zvbMkmqGSHHZdhummZQqTlFt5zqbEXe1NVnmKyoKPbtSZ/eMn3iQ160E9lVOJo/LdRd XfCA== 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:references:in-reply-to:date:to:from :message-id:delivered-to; bh=WHcj+2CYzNtsaSnTB5tze1Aq4VRfau0UzdW+9N5CXgE=; fh=e5zN9xSzcxLA6bGo3lF+CqTbY/oLwzApV03EO/RBfgQ=; b=FCtRbbQXQbozDA9D1PXWpmNavak7jltifp8O0qMjpPm8c1xadbqBgSguVs2KJKfnfW mEDYz+i0z3TejsctC5vwcE+hLatG5mKIQgjQ5DSWgMQ2Y2J5QSl25vc57VECenPa/vSw M1WJEjglNx98YHJRdSdsGJk1ZQ3JRoBHQvu+RUkwFlURRGViwE7rsjlcZ8KxTKnlEYyk OOt3QYy6L6PcWOnvmCcOrQsbUn2LAel7KIgV39TxThrmPO+Sx7sApbxCy7u6r6jWby1g quW0f9SH4pyQXn44nvjy4SSVKFTLcf21eMsJJNcIKvLFVFRkiGjm2FZkQfTLHYrF+Zpi lYAQ==; 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-536870afb92si6503178e87.487.2024.09.22.09.38.16; Sun, 22 Sep 2024 09:38:17 -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 C3A6E68DB05; Sun, 22 Sep 2024 19:38:13 +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 1E7E468DA1F for ; Sun, 22 Sep 2024 19:38:07 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by glom.nmugroup.com (Postfix) with ESMTP id A8B985423920 for ; Sun, 22 Sep 2024 18:38:06 +0200 (CEST) Received: from debian.lan (unknown [IPv6:2a00:66c0:a::72c]) (Authenticated sender: git01) by glom.nmugroup.com (Postfix) with ESMTPSA id 661845422937 for ; Sun, 22 Sep 2024 18:38:06 +0200 (CEST) Message-ID: From: Tomas =?iso-8859-1?q?H=E4rdin?= To: FFmpeg development discussions and patches Date: Sun, 22 Sep 2024 18:38:05 +0200 In-Reply-To: <1de0719456ef2f015b83d214d73334853af26612.camel@haerdin.se> References: <1de0719456ef2f015b83d214d73334853af26612.camel@haerdin.se> User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/5] lavf/mxfdec: Speed up mxf_edit_unit_absolute_offset() 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: tkL2AzzJ1oxp This is the big one. The offset_temp calculations that used to be part of mxf_edit_unit_absolute_offset() are moved to mxf_compute_index_tables() and are done only once. This also makes random access in mxf_edit_unit_absolute_offset() possible /Tomas From 8d1f84e6b047fcaeba74a76cfc6b8f3ebc450145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Sun, 15 Sep 2024 22:04:13 +0200 Subject: [PATCH 4/5] lavf/mxfdec: Speed up mxf_edit_unit_absolute_offset() This involves computing the approximate location of the desired index table segment and linearly searching from there. --- libavformat/mxfdec.c | 67 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index 4823aae135..3dd7c3bfdc 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -262,6 +262,7 @@ typedef struct MXFIndexTableSegment { int *flag_entries; uint64_t *stream_offset_entries; int nb_index_entries; + int64_t offset; } MXFIndexTableSegment; typedef struct MXFPackage { @@ -1920,18 +1921,44 @@ static int64_t mxf_essence_container_end(MXFContext *mxf, int body_sid) /* EditUnit -> absolute offset */ static int mxf_edit_unit_absolute_offset(MXFContext *mxf, MXFIndexTable *index_table, int64_t edit_unit, AVRational edit_rate, int64_t *edit_unit_out, int64_t *offset_out, MXFPartition **partition_out, int nag) { - int i; - int64_t offset_temp = 0; + int i = 0; + int64_t index_duration, index_end; + MXFIndexTableSegment *first_segment, *last_segment; + + if (!index_table->nb_segments) { + av_log(mxf->fc, AV_LOG_ERROR, "no index table segments\n"); + return AVERROR_INVALIDDATA; + } edit_unit = av_rescale_q(edit_unit, index_table->segments[0]->index_edit_rate, edit_rate); - for (i = 0; i < index_table->nb_segments; i++) { - MXFIndexTableSegment *s = index_table->segments[i]; + first_segment = index_table->segments[0]; + last_segment = index_table->segments[index_table->nb_segments - 1]; + + // clamp to actual range of index + index_end = av_sat_add64(last_segment->index_start_position, last_segment->index_duration); + edit_unit = FFMAX(FFMIN(edit_unit, index_end), first_segment->index_start_position); + + // guess which table segment this edit unit is in + // saturation is fine since it's just a guess + // if the guess is wrong we revert to a linear search + index_duration = av_sat_sub64(index_end, first_segment->index_start_position); + + // compute the guess, taking care not to cause overflow or division by zero + if (index_duration > 0 && edit_unit <= INT64_MAX / index_table->nb_segments) { + // a simple linear guesstimate + // this is accurate to within +-1 when partitions are generated at a constant rate like mxfenc does + int64_t i64 = index_table->nb_segments * edit_unit / index_duration; + // clamp and downcast to 32-bit + i = FFMAX(0, FFMIN(index_table->nb_segments - 1, i64)); + } - edit_unit = FFMAX(edit_unit, s->index_start_position); /* clamp if trying to seek before start */ + for (; i >= 0 && i < index_table->nb_segments;) { + MXFIndexTableSegment *s = index_table->segments[i]; - if (edit_unit < s->index_start_position + s->index_duration) { + if (s->index_start_position <= edit_unit && edit_unit < s->index_start_position + s->index_duration) { int64_t index = edit_unit - s->index_start_position; + int64_t offset_temp = s->offset; if (s->edit_unit_byte_count) { if (index > INT64_MAX / s->edit_unit_byte_count || @@ -1956,14 +1983,12 @@ static int mxf_edit_unit_absolute_offset(MXFContext *mxf, MXFIndexTable *index_t *edit_unit_out = av_rescale_q(edit_unit, edit_rate, s->index_edit_rate); return mxf_absolute_bodysid_offset(mxf, index_table->body_sid, offset_temp, offset_out, partition_out); + } else if (edit_unit < s->index_start_position) { + // the segments are sorted by IndexStartPosition, so this is guaranteed to terminate + i--; } else { - /* EditUnitByteCount == 0 for VBR indexes, which is fine since they use explicit StreamOffsets */ - if (s->edit_unit_byte_count && (s->index_duration > INT64_MAX / s->edit_unit_byte_count || - s->edit_unit_byte_count * s->index_duration > INT64_MAX - offset_temp) - ) - return AVERROR_INVALIDDATA; - - offset_temp += s->edit_unit_byte_count * s->index_duration; + // edit_unit >= s->index_start_position + s->index_duration + i++; } } @@ -2148,6 +2173,7 @@ static int mxf_compute_index_tables(MXFContext *mxf) for (int i = 0, j = 0; j < mxf->nb_index_tables; i += mxf->index_tables[j++].nb_segments) { MXFIndexTable *t = &mxf->index_tables[j]; MXFTrack *mxf_track = NULL; + int64_t offset_temp = 0; t->segments = av_calloc(t->nb_segments, sizeof(*t->segments)); if (!t->segments) { @@ -2176,8 +2202,10 @@ static int mxf_compute_index_tables(MXFContext *mxf) } } - /* fix zero IndexDurations */ + /* fix zero IndexDurations and compute segment offsets */ for (int k = 0; k < t->nb_segments; k++) { + MXFIndexTableSegment *s = t->segments[k]; + if (!t->segments[k]->index_edit_rate.num || !t->segments[k]->index_edit_rate.den) { av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i segment %i has invalid IndexEditRate\n", t->index_sid, k); @@ -2185,6 +2213,17 @@ static int mxf_compute_index_tables(MXFContext *mxf) t->segments[k]->index_edit_rate = mxf_track->edit_rate; } + s->offset = offset_temp; + + /* EditUnitByteCount == 0 for VBR indexes, which is fine since they use explicit StreamOffsets */ + if (s->edit_unit_byte_count && (s->index_duration > INT64_MAX / s->edit_unit_byte_count || + s->edit_unit_byte_count * s->index_duration > INT64_MAX - offset_temp)) { + ret = AVERROR_INVALIDDATA; + goto finish_decoding_index; + } + + offset_temp += t->segments[k]->edit_unit_byte_count * t->segments[k]->index_duration; + if (t->segments[k]->index_duration) continue; -- 2.39.2 From patchwork Sun Sep 22 16:38:30 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: 51698 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:d154:0:b0:48e:c0f8:d0de with SMTP id bt20csp2045165vqb; Sun, 22 Sep 2024 09:49:17 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXsLvO+bfTofTF6q8LZMRiF2BOyonn2ICnlrid+Dr+2XNOwtcf3RiunH7po+1+LzyJrQCswZciyX3VU0eohiq0N@gmail.com X-Google-Smtp-Source: AGHT+IGJOCyR5+hRkxRvYVU8thbdohDtWGNvMIkQI44FNYOLMiwnGKBuTSvhTPmPydBFxaP1l+MC X-Received: by 2002:a2e:a582:0:b0:2f5:2e2:eaf9 with SMTP id 38308e7fff4ca-2f7cb2cfea9mr50848951fa.4.1727023756968; Sun, 22 Sep 2024 09:49:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1727023756; cv=none; d=google.com; s=arc-20240605; b=GHPoZGygluoU0EsIWIhUhhNEIreAIf5TuuW3XFRyrr42016cAvh8r54/LyT7vqB7uU 5cW9PU7kUAWy6Xc0BvjQHQ4zmNubQ8tkMWq+xpf3TE1m2StjDG0q5cRXfasdZ+cixzEW XPi0QFWzRYKI93VEN5SNkyrg9AbdJF8ICd6BIdrVdrpY21ZsBOcmh8Bx7LYKiUECCcQ3 HB1ZQ41xXZ91FtT8/zEj4SMbKcayr/kl8CJQUWt87PqKZKf9RA+rNjK15w1TtU5afg/9 eSqzu/G3yzQT+0p5wo/+YsIDIu1O2gqfWlxpqUswi88Es25Rc8rkTGXY2k4+KW/YRPYx /4ig== 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:references:in-reply-to:date:to:from :message-id:delivered-to; bh=5LUsPNzh+Pk/I3bCfs8JOiIAvsoSVPht7OYUcfaMg4Y=; fh=e5zN9xSzcxLA6bGo3lF+CqTbY/oLwzApV03EO/RBfgQ=; b=Xo4+HJosnkDA/BPRDSfNmjpCMIQFFsXrtd+4NtqnzVRP3VWizPjxxRKJogmE17gxHf CR8vY8zKRWIDUlq+PpGBvGHAEB26CJI1bHUqhCP7vNbu5WSkPE4CAqU5bs7Qc/V45PYk Wes3qPdaB+O2X8+0S7Hp36lHvTxXSSXcTNpc86ju6kd2076ajQVkSmn81904oiiRjnUg P01ME4xDIeyOiYIKENM1CXCivY5VcToIvR+5N1bSCchbTN5QVGDxNoiNVMQU/NmxZW7i UjhY43hQ6xywCpUoTGp4p0Q3x5meXnpm8aitRXMJNk1nQFh069pZ448FbUi9LkgzsVyE a4Rg==; 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 38308e7fff4ca-2f79d2db320si50565531fa.179.2024.09.22.09.49.16; Sun, 22 Sep 2024 09:49:16 -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 2B6D668DB1B; Sun, 22 Sep 2024 19:38:39 +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 DE99C68DADA for ; Sun, 22 Sep 2024 19:38:31 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by glom.nmugroup.com (Postfix) with ESMTP id 8C7B65422B58 for ; Sun, 22 Sep 2024 18:38:31 +0200 (CEST) Received: from debian.lan (unknown [IPv6:2a00:66c0:a::72c]) (Authenticated sender: git01) by glom.nmugroup.com (Postfix) with ESMTPSA id 4752F5422937 for ; Sun, 22 Sep 2024 18:38:30 +0200 (CEST) Message-ID: <3e00db027d443e56ca679974d6a4f48635884487.camel@haerdin.se> From: Tomas =?iso-8859-1?q?H=E4rdin?= To: FFmpeg development discussions and patches Date: Sun, 22 Sep 2024 18:38:30 +0200 In-Reply-To: <1de0719456ef2f015b83d214d73334853af26612.camel@haerdin.se> References: <1de0719456ef2f015b83d214d73334853af26612.camel@haerdin.se> User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/5] lavf/mxfdec: Remove a call to avio_tell() in 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: uO5rrey3KACh From e036dec9bdd621db809bb23822b7d22f66ab43fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Sun, 15 Sep 2024 22:46:55 +0200 Subject: [PATCH 5/5] lavf/mxfdec: Remove a call to avio_tell() in klv_read_packet() --- libavformat/mxfdec.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index 3dd7c3bfdc..2ebfe82b7e 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -431,7 +431,7 @@ static void mxf_free_metadataset(MXFMetadataSet **ctx, enum MXFMetadataSetType t av_freep(ctx); } -static int64_t klv_decode_ber_length(AVIOContext *pb) +static int64_t klv_decode_ber_length(AVIOContext *pb, int *llen) { uint64_t size = avio_r8(pb); if (size & 0x80) { /* long form */ @@ -439,9 +439,13 @@ static int64_t klv_decode_ber_length(AVIOContext *pb) /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */ if (bytes_num > 8) return AVERROR_INVALIDDATA; + if (llen) + *llen = bytes_num + 1; size = 0; while (bytes_num--) size = size << 8 | avio_r8(pb); + } else if (llen) { + *llen = 1; } if (size > INT64_MAX) return AVERROR_INVALIDDATA; @@ -480,6 +484,8 @@ static int mxf_read_sync_klv(AVIOContext *pb) static int klv_read_packet(MXFContext *mxf, KLVPacket *klv, AVIOContext *pb) { int64_t length, pos; + int llen; + if (!mxf_read_sync_klv(pb)) return AVERROR_INVALIDDATA; klv->offset = avio_tell(pb) - 4; @@ -488,11 +494,11 @@ static int klv_read_packet(MXFContext *mxf, KLVPacket *klv, AVIOContext *pb) memcpy(klv->key, mxf_klv_key, 4); avio_read(pb, klv->key + 4, 12); - length = klv_decode_ber_length(pb); + length = klv_decode_ber_length(pb, &llen); if (length < 0) return length; klv->length = length; - pos = avio_tell(pb); + pos = klv->offset + 16 + llen; if (pos > INT64_MAX - length) return AVERROR_INVALIDDATA; klv->next_klv = pos + length; @@ -663,15 +669,15 @@ static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv av_aes_init(mxf->aesc, s->key, 128, 1); } // crypto context - size = klv_decode_ber_length(pb); + size = klv_decode_ber_length(pb, NULL); if (size < 0) return size; avio_skip(pb, size); // plaintext offset - klv_decode_ber_length(pb); + klv_decode_ber_length(pb ,NULL); plaintext_size = avio_rb64(pb); // source klv key - klv_decode_ber_length(pb); + klv_decode_ber_length(pb, NULL); avio_read(pb, klv->key, 16); if (!IS_KLV_KEY(klv, mxf_essence_element_key)) return AVERROR_INVALIDDATA; @@ -681,12 +687,12 @@ static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv if (index < 0) return AVERROR_INVALIDDATA; // source size - klv_decode_ber_length(pb); + klv_decode_ber_length(pb, NULL); orig_size = avio_rb64(pb); if (orig_size < plaintext_size) return AVERROR_INVALIDDATA; // enc. code - size = klv_decode_ber_length(pb); + size = klv_decode_ber_length(pb, NULL); if (size < 32 || size - 32 < orig_size || (int)orig_size != orig_size) return AVERROR_INVALIDDATA; avio_read(pb, ivec, 16); -- 2.39.2