[vlc-commits] packetizer: add AnnexB startcode helper

Francois Cartegnie git at videolan.org
Tue Jan 5 19:19:52 CET 2016


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Sat Jan  2 23:21:30 2016 +0100| [953dd0042d98628ef961ceb2fc5e4e40d4d6403b] | committer: Francois Cartegnie

packetizer: add AnnexB startcode helper

Improves startcode lookup by ~80%
(statistically/zero dependent)

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=953dd0042d98628ef961ceb2fc5e4e40d4d6403b
---

 modules/packetizer/Makefile.am        |    2 +-
 modules/packetizer/h264.c             |    3 +-
 modules/packetizer/hevc.c             |    3 +-
 modules/packetizer/mpeg4video.c       |    3 +-
 modules/packetizer/mpegvideo.c        |    3 +-
 modules/packetizer/startcode_helper.h |   67 +++++++++++++++++++++++++++++++++
 modules/packetizer/vc1.c              |    3 +-
 7 files changed, 78 insertions(+), 6 deletions(-)

diff --git a/modules/packetizer/Makefile.am b/modules/packetizer/Makefile.am
index 54f875a..cf39389 100644
--- a/modules/packetizer/Makefile.am
+++ b/modules/packetizer/Makefile.am
@@ -26,7 +26,7 @@ libpacketizer_avparser_plugin_la_CFLAGS = $(AVCODEC_CFLAGS) $(AVUTIL_CFLAGS) $(A
 libpacketizer_avparser_plugin_la_LIBADD = $(AVCODEC_LIBS) $(AVUTIL_LIBS) $(LIBM)
 
 
-noinst_HEADERS += packetizer/packetizer_helper.h
+noinst_HEADERS += packetizer/packetizer_helper.h packetizer/startcode_helper.h
 
 packetizer_LTLIBRARIES = \
 	libpacketizer_mpegvideo_plugin.la \
diff --git a/modules/packetizer/h264.c b/modules/packetizer/h264.c
index c0c1cc0..a918f58 100644
--- a/modules/packetizer/h264.c
+++ b/modules/packetizer/h264.c
@@ -45,6 +45,7 @@
 #include "hxxx_nal.h"
 #include "hxxx_common.h"
 #include "packetizer_helper.h"
+#include "startcode_helper.h"
 
 /*****************************************************************************
  * Module descriptor
@@ -195,7 +196,7 @@ static int Open( vlc_object_t *p_this )
     }
 
     packetizer_Init( &p_sys->packetizer,
-                     p_h264_startcode, sizeof(p_h264_startcode), NULL,
+                     p_h264_startcode, sizeof(p_h264_startcode), startcode_FindAnnexB,
                      p_h264_startcode, 1, 5,
                      PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
 
diff --git a/modules/packetizer/hevc.c b/modules/packetizer/hevc.c
index 2edfdb4..124f17d 100644
--- a/modules/packetizer/hevc.c
+++ b/modules/packetizer/hevc.c
@@ -36,6 +36,7 @@
 
 #include <vlc_block_helper.h>
 #include "packetizer_helper.h"
+#include "startcode_helper.h"
 #include "hevc_nal.h"
 #include "hxxx_nal.h"
 #include "hxxx_common.h"
@@ -100,7 +101,7 @@ static int Open(vlc_object_t *p_this)
     p_sys->pp_frame_last = &p_sys->p_frame;
 
     packetizer_Init(&p_dec->p_sys->packetizer,
-                    p_hevc_startcode, sizeof(p_hevc_startcode), NULL,
+                    p_hevc_startcode, sizeof(p_hevc_startcode), startcode_FindAnnexB,
                     p_hevc_startcode, 1, 5,
                     PacketizeReset, PacketizeParse, PacketizeValidate, p_dec);
 
diff --git a/modules/packetizer/mpeg4video.c b/modules/packetizer/mpeg4video.c
index 71e1548..bf8b864 100644
--- a/modules/packetizer/mpeg4video.c
+++ b/modules/packetizer/mpeg4video.c
@@ -40,6 +40,7 @@
 #include <vlc_bits.h>
 #include <vlc_block_helper.h>
 #include "packetizer_helper.h"
+#include "startcode_helper.h"
 
 /*****************************************************************************
  * Module descriptor
@@ -142,7 +143,7 @@ static int Open( vlc_object_t *p_this )
 
     /* Misc init */
     packetizer_Init( &p_sys->packetizer,
-                     p_mp4v_startcode, sizeof(p_mp4v_startcode), NULL,
+                     p_mp4v_startcode, sizeof(p_mp4v_startcode), startcode_FindAnnexB,
                      NULL, 0, 4,
                      PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
 
diff --git a/modules/packetizer/mpegvideo.c b/modules/packetizer/mpegvideo.c
index 84f9798..070a3aa 100644
--- a/modules/packetizer/mpegvideo.c
+++ b/modules/packetizer/mpegvideo.c
@@ -53,6 +53,7 @@
 #include <vlc_block_helper.h>
 #include "../codec/cc.h"
 #include "packetizer_helper.h"
+#include "startcode_helper.h"
 
 #define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame")
 #define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \
@@ -168,7 +169,7 @@ static int Open( vlc_object_t *p_this )
 
     /* Misc init */
     packetizer_Init( &p_sys->packetizer,
-                     p_mp2v_startcode, sizeof(p_mp2v_startcode), NULL,
+                     p_mp2v_startcode, sizeof(p_mp2v_startcode), startcode_FindAnnexB,
                      NULL, 0, 4,
                      PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
 
diff --git a/modules/packetizer/startcode_helper.h b/modules/packetizer/startcode_helper.h
new file mode 100644
index 0000000..9db67fd
--- /dev/null
+++ b/modules/packetizer/startcode_helper.h
@@ -0,0 +1,67 @@
+/*****************************************************************************
+ * startcode_helper.h: Startcodes helpers
+ *****************************************************************************
+ * Copyright (C) 2016 VideoLAN Authors
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+#ifndef _STARTCODE_HELPER_H
+#define _STARTCODE_HELPER_H 1
+
+/* Looks up efficiently for an AnnexB startcode 0x00 0x00 0x01
+ * by using a 4 times faster trick than single byte lookup.
+ *
+ * That code is adapted from libav's ff_avc_find_startcode_internal
+ * and i believe the trick originated from
+ * https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
+ */
+static inline const uint8_t * startcode_FindAnnexB( const uint8_t *p, const uint8_t *end )
+{
+    const uint8_t *a = p + 4 - ((intptr_t)p & 3);
+
+    for (end -= 3; p < a && p < end; p++) {
+        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
+            return p;
+    }
+
+    for (end -= 3; p < end; p += 4) {
+        uint32_t x = *(const uint32_t*)p;
+        if ((x - 0x01010101) & (~x) & 0x80808080)
+        {
+            /* matching DW isn't faster */
+            if (p[1] == 0) {
+                if (p[0] == 0 && p[2] == 1)
+                    return p;
+                if (p[2] == 0 && p[3] == 1)
+                    return p+1;
+            }
+            if (p[3] == 0) {
+                if (p[2] == 0 && p[4] == 1)
+                    return p+2;
+                if (p[4] == 0 && p[5] == 1)
+                    return p+3;
+            }
+        }
+    }
+
+    for (end += 3; p < end; p++) {
+        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
+            return p;
+    }
+
+    return NULL;
+}
+
+#endif
diff --git a/modules/packetizer/vc1.c b/modules/packetizer/vc1.c
index cb250f9..4808048 100644
--- a/modules/packetizer/vc1.c
+++ b/modules/packetizer/vc1.c
@@ -39,6 +39,7 @@
 #include <vlc_block_helper.h>
 #include "../codec/cc.h"
 #include "packetizer_helper.h"
+#include "startcode_helper.h"
 
 /*****************************************************************************
  * Module descriptor
@@ -155,7 +156,7 @@ static int Open( vlc_object_t *p_this )
         return VLC_ENOMEM;
 
     packetizer_Init( &p_sys->packetizer,
-                     p_vc1_startcode, sizeof(p_vc1_startcode), NULL,
+                     p_vc1_startcode, sizeof(p_vc1_startcode), startcode_FindAnnexB,
                      NULL, 0, 4,
                      PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
 



More information about the vlc-commits mailing list