diff mbox series

[FFmpeg-devel,3/5] lavf/mxfdec: Add and use mxf_is_encrypted_triplet_key()

Message ID c1cebf85174bd399702ebc280567ba90367f77c5.camel@haerdin.se
State New
Headers show
Series [FFmpeg-devel,1/5] lavf/mxfdec: Speed up klv_read_packet() | expand

Checks

Context Check Description
yinshiyou/configure_loongarch64 warning Failed to apply patch
andriy/configure_x86 warning Failed to apply patch

Commit Message

Tomas Härdin Sept. 22, 2024, 4:36 p.m. UTC
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

Comments

Marton Balint Sept. 22, 2024, 9:36 p.m. UTC | #1
On Sun, 22 Sep 2024, Tomas Härdin wrote:

> 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

Yeah, this looks a bit overkill.

Regards,
Marton
diff mbox series

Patch

From ed92d030212e230c7a12d2b265feb470ffd5caa8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
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