Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avutil/aes: use pthread_once to fill the static tables
@ 2025-04-07  0:00 James Almer
  2025-04-07 16:26 ` [FFmpeg-devel] [PATCH 2/2] avutil/x86/aes: remove a few branches James Almer
  0 siblings, 1 reply; 2+ messages in thread
From: James Almer @ 2025-04-07  0:00 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavutil/aes.c | 53 ++++++++++++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 23 deletions(-)

diff --git a/libavutil/aes.c b/libavutil/aes.c
index 1f4cceb700..5f31412149 100644
--- a/libavutil/aes.c
+++ b/libavutil/aes.c
@@ -29,6 +29,7 @@
 #include "intreadwrite.h"
 #include "macros.h"
 #include "mem.h"
+#include "thread.h"
 
 const int av_aes_size= sizeof(AVAES);
 
@@ -197,6 +198,34 @@ static void init_multbl2(uint32_t tbl[][256], const int c[4],
     }
 }
 
+static AVOnce aes_static_init = AV_ONCE_INIT;
+
+static void aes_init_static(void)
+{
+    uint8_t log8[256];
+    uint8_t alog8[512];
+    int i, j = 1;
+
+    for (i = 0; i < 255; i++) {
+        alog8[i] = alog8[i + 255] = j;
+        log8[j] = i;
+        j ^= j + j;
+        if (j > 255)
+            j ^= 0x11B;
+    }
+    for (i = 0; i < 256; i++) {
+        j = i ? alog8[255 - log8[i]] : 0;
+        j ^= (j << 1) ^ (j << 2) ^ (j << 3) ^ (j << 4);
+        j = (j ^ (j >> 8) ^ 99) & 255;
+        inv_sbox[j] = i;
+        sbox[i]     = j;
+    }
+    init_multbl2(dec_multbl, (const int[4]) { 0xe, 0x9, 0xd, 0xb },
+                 log8, alog8, inv_sbox);
+    init_multbl2(enc_multbl, (const int[4]) { 0x2, 0x1, 0x1, 0x3 },
+                 log8, alog8, sbox);
+}
+
 // this is based on the reference AES code by Paulo Barreto and Vincent Rijmen
 int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
 {
@@ -204,34 +233,12 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
     uint8_t tk[8][4];
     int KC = key_bits >> 5;
     int rounds = KC + 6;
-    uint8_t log8[256];
-    uint8_t alog8[512];
 
     a->crypt = decrypt ? aes_decrypt : aes_encrypt;
     if (ARCH_X86)
         ff_init_aes_x86(a, decrypt);
 
-    if (!enc_multbl[FF_ARRAY_ELEMS(enc_multbl) - 1][FF_ARRAY_ELEMS(enc_multbl[0]) - 1]) {
-        j = 1;
-        for (i = 0; i < 255; i++) {
-            alog8[i] = alog8[i + 255] = j;
-            log8[j] = i;
-            j ^= j + j;
-            if (j > 255)
-                j ^= 0x11B;
-        }
-        for (i = 0; i < 256; i++) {
-            j = i ? alog8[255 - log8[i]] : 0;
-            j ^= (j << 1) ^ (j << 2) ^ (j << 3) ^ (j << 4);
-            j = (j ^ (j >> 8) ^ 99) & 255;
-            inv_sbox[j] = i;
-            sbox[i]     = j;
-        }
-        init_multbl2(dec_multbl, (const int[4]) { 0xe, 0x9, 0xd, 0xb },
-                     log8, alog8, inv_sbox);
-        init_multbl2(enc_multbl, (const int[4]) { 0x2, 0x1, 0x1, 0x3 },
-                     log8, alog8, sbox);
-    }
+    ff_thread_once(&aes_static_init, aes_init_static);
 
     if (key_bits != 128 && key_bits != 192 && key_bits != 256)
         return AVERROR(EINVAL);
-- 
2.49.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-04-07 16:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-07  0:00 [FFmpeg-devel] [PATCH] avutil/aes: use pthread_once to fill the static tables James Almer
2025-04-07 16:26 ` [FFmpeg-devel] [PATCH 2/2] avutil/x86/aes: remove a few branches James Almer

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git