[vlc-devel] [PATCH 10/25] packetizer: optimize startcode_FindAnnexB wrapper

Victorien Le Couviour--Tuffet victorien.lecouviour.tuffet at gmail.com
Tue Apr 14 12:40:21 CEST 2020


Instead of checking for CPU capabilities at each call, return a function
pointer once.
---
 modules/packetizer/h264.c             |  3 ++-
 modules/packetizer/hevc.c             |  3 ++-
 modules/packetizer/hxxx_nal.h         |  8 ++++++--
 modules/packetizer/mpeg4video.c       |  3 ++-
 modules/packetizer/mpegvideo.c        |  3 ++-
 modules/packetizer/startcode_helper.h | 15 +++++++--------
 modules/packetizer/vc1.c              |  3 ++-
 test/modules/packetizer/helpers.c     |  7 ++++---
 8 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/modules/packetizer/h264.c b/modules/packetizer/h264.c
index ea4d2f994b..8ba661e95a 100644
--- a/modules/packetizer/h264.c
+++ b/modules/packetizer/h264.c
@@ -336,7 +336,8 @@ static int Open( vlc_object_t *p_this )
     }
 
     packetizer_Init( &p_sys->packetizer,
-                     p_h264_startcode, sizeof(p_h264_startcode), startcode_FindAnnexB,
+                     p_h264_startcode, sizeof(p_h264_startcode),
+                     startcode_FindAnnexB_helper(),
                      p_h264_startcode, 1, 5,
                      PacketizeReset, PacketizeParse, PacketizeValidate, PacketizeDrain,
                      p_dec );
diff --git a/modules/packetizer/hevc.c b/modules/packetizer/hevc.c
index a26c45a964..8cd81a11c9 100644
--- a/modules/packetizer/hevc.c
+++ b/modules/packetizer/hevc.c
@@ -212,7 +212,8 @@ static int Open(vlc_object_t *p_this)
     INITQ(post);
 
     packetizer_Init(&p_sys->packetizer,
-                    p_hevc_startcode, sizeof(p_hevc_startcode), startcode_FindAnnexB,
+                    p_hevc_startcode, sizeof(p_hevc_startcode),
+                    startcode_FindAnnexB_helper(),
                     p_hevc_startcode, 1, 5,
                     PacketizeReset, PacketizeParse, PacketizeValidate, PacketizeDrain,
                     p_dec);
diff --git a/modules/packetizer/hxxx_nal.h b/modules/packetizer/hxxx_nal.h
index b972c52fbb..1fc68eb2a8 100644
--- a/modules/packetizer/hxxx_nal.h
+++ b/modules/packetizer/hxxx_nal.h
@@ -57,6 +57,7 @@ static inline bool hxxx_strip_AnnexB_startcode( const uint8_t **pp_data, size_t
 
 typedef struct
 {
+    block_startcode_helper_t pf_startcode_FindAnnexB;
     const uint8_t *p_head;
     const uint8_t *p_tail;
     uint8_t i_nal_length_size;
@@ -65,6 +66,7 @@ typedef struct
 static inline void hxxx_iterator_init( hxxx_iterator_ctx_t *p_ctx, const uint8_t *p_data, size_t i_data,
                                        uint8_t i_nal_length_size )
 {
+    p_ctx->pf_startcode_FindAnnexB = startcode_FindAnnexB_helper();
     p_ctx->p_head = p_data;
     p_ctx->p_tail = p_data + i_data;
     if( vlc_popcount(i_nal_length_size) == 1 && i_nal_length_size <= 4 )
@@ -100,11 +102,13 @@ static inline bool hxxx_annexb_iterate_next( hxxx_iterator_ctx_t *p_ctx, const u
     if( !p_ctx->p_head )
         return false;
 
-    p_ctx->p_head = startcode_FindAnnexB( p_ctx->p_head, p_ctx->p_tail );
+    p_ctx->p_head = p_ctx->pf_startcode_FindAnnexB(p_ctx->p_head,
+                                                   p_ctx->p_tail);
     if( !p_ctx->p_head )
         return false;
 
-    const uint8_t *p_end = startcode_FindAnnexB( p_ctx->p_head + 3, p_ctx->p_tail );
+    const uint8_t *p_end = p_ctx->pf_startcode_FindAnnexB(p_ctx->p_head + 3,
+                                                          p_ctx->p_tail);
     if( !p_end )
         p_end = p_ctx->p_tail;
 
diff --git a/modules/packetizer/mpeg4video.c b/modules/packetizer/mpeg4video.c
index f2709f1206..9e15abda87 100644
--- a/modules/packetizer/mpeg4video.c
+++ b/modules/packetizer/mpeg4video.c
@@ -143,7 +143,8 @@ static int Open( vlc_object_t *p_this )
 
     /* Misc init */
     packetizer_Init( &p_sys->packetizer,
-                     p_mp4v_startcode, sizeof(p_mp4v_startcode), startcode_FindAnnexB,
+                     p_mp4v_startcode, sizeof(p_mp4v_startcode),
+                     startcode_FindAnnexB_helper(),
                      NULL, 0, 4,
                      PacketizeReset, PacketizeParse, PacketizeValidate, NULL,
                      p_dec );
diff --git a/modules/packetizer/mpegvideo.c b/modules/packetizer/mpegvideo.c
index 2388cc5488..a089e20e88 100644
--- a/modules/packetizer/mpegvideo.c
+++ b/modules/packetizer/mpegvideo.c
@@ -218,7 +218,8 @@ static int Open( vlc_object_t *p_this )
 
     /* Misc init */
     packetizer_Init( &p_sys->packetizer,
-                     p_mp2v_startcode, sizeof(p_mp2v_startcode), startcode_FindAnnexB,
+                     p_mp2v_startcode, sizeof(p_mp2v_startcode),
+                     startcode_FindAnnexB_helper(),
                      NULL, 0, 4,
                      PacketizeReset, PacketizeParse, PacketizeValidate, PacketizeDrain,
                      p_dec );
diff --git a/modules/packetizer/startcode_helper.h b/modules/packetizer/startcode_helper.h
index ddaa94c546..e6045b9708 100644
--- a/modules/packetizer/startcode_helper.h
+++ b/modules/packetizer/startcode_helper.h
@@ -20,6 +20,7 @@
 #ifndef VLC_STARTCODE_HELPER_H_
 #define VLC_STARTCODE_HELPER_H_
 
+#include <vlc_block_helper.h>
 #include <vlc_cpu.h>
 
 #if !defined(CAN_COMPILE_SSE2) && defined(HAVE_SSE2_INTRINSICS)
@@ -143,17 +144,15 @@ startcode_FindAnnexB_Bits(uint8_t const *p, uint8_t const *end)
 #undef TRY_MATCH
 
 
-#if defined(CAN_COMPILE_SSE2) || defined(HAVE_SSE2_INTRINSICS)
-static inline uint8_t const *
-startcode_FindAnnexB(uint8_t const *ptr, uint8_t const *end)
+static inline block_startcode_helper_t
+startcode_FindAnnexB_helper(void)
 {
+#if defined(CAN_COMPILE_SSE2) || defined(HAVE_SSE2_INTRINSICS)
     if (vlc_CPU_SSE2())
-        return startcode_FindAnnexB_SSE2(ptr, end);
+        return startcode_FindAnnexB_SSE2;
     else
-        return startcode_FindAnnexB_Bits(ptr, end);
-}
-#else
-    #define startcode_FindAnnexB startcode_FindAnnexB_Bits
 #endif
+        return startcode_FindAnnexB_Bits;
+}
 
 #endif
diff --git a/modules/packetizer/vc1.c b/modules/packetizer/vc1.c
index 7317d440e2..ec83d220e9 100644
--- a/modules/packetizer/vc1.c
+++ b/modules/packetizer/vc1.c
@@ -160,7 +160,8 @@ static int Open( vlc_object_t *p_this )
     p_dec->pf_get_cc = GetCc;
 
     packetizer_Init( &p_sys->packetizer,
-                     p_vc1_startcode, sizeof(p_vc1_startcode), startcode_FindAnnexB,
+                     p_vc1_startcode, sizeof(p_vc1_startcode),
+                     startcode_FindAnnexB_helper(),
                      NULL, 0, 4,
                      PacketizeReset, PacketizeParse, PacketizeValidate, PacketizeDrain,
                      p_dec );
diff --git a/test/modules/packetizer/helpers.c b/test/modules/packetizer/helpers.c
index 0ef5e5db48..63fd8de4b7 100644
--- a/test/modules/packetizer/helpers.c
+++ b/test/modules/packetizer/helpers.c
@@ -77,15 +77,16 @@ static int run_annexb_sets( const uint8_t *p_set, const uint8_t *p_end,
         return i_ret;
 
     /* Perform same tests on simd optimized code */
-    if( startcode_FindAnnexB_Bits != startcode_FindAnnexB )
+#if defined(CAN_COMPILE_SSE2) || defined(HAVE_SSE2_INTRINSICS)
+    if (vlc_CPU_SSE2())
     {
         printf("checking asm:\n");
         i_ret = check_set( p_set, p_end, p_results, i_results, i_results_offset,
-                           startcode_FindAnnexB );
+                           startcode_FindAnnexB_SSE2 );
         if( i_ret != 0 )
             return i_ret;
     }
-    else printf("asm not built in, skipping test:\n");
+#endif
 
     return 0;
 }
-- 
2.24.1



More information about the vlc-devel mailing list