ts_indexer: use android ext-golomb. [1/1]

PD#SWPL-128709

Problem:
android ext-golomb is better.

Solution:
use android ext-golomb.

Verify:
ubuntu

Change-Id: I5b0862a19eeb13d4707a76f77f9eccaaffc2cb0e
Signed-off-by: Yahui Han <yahui.han@amlogic.com>
diff --git a/include/bitstrm.h b/include/bitstrm.h
new file mode 100644
index 0000000..aa18887
--- /dev/null
+++ b/include/bitstrm.h
@@ -0,0 +1,40 @@
+typedef unsigned int UWORD32;
+
+/*****************************************************************************/
+/* Define a macro for inlining of NEXTBITS_32                                */
+/*****************************************************************************/
+#define     NEXTBITS_32(u4_word, u4_offset, pu4_bitstream)                  \
+{                                                                           \
+    UWORD32 *pu4_buf =  (pu4_bitstream);                                    \
+    UWORD32 u4_word_off = ((u4_offset) >> 5);                               \
+    UWORD32 u4_bit_off = (u4_offset) & 0x1F;                                \
+                                                                            \
+    u4_word = pu4_buf[u4_word_off++] << u4_bit_off;                         \
+    if(u4_bit_off)                                                          \
+    u4_word |= (pu4_buf[u4_word_off] >> (INT_IN_BITS - u4_bit_off));        \
+}                                                                           \
+
+#define INT_IN_BITS 32
+/*****************************************************************************/
+/* Define a macro for inlining of GETBITS: u4_no_bits shall not exceed 32    */
+/*****************************************************************************/
+#define     GETBITS(u4_code, u4_offset, pu4_bitstream, u4_no_bits)          \
+{                                                                           \
+    UWORD32 *pu4_buf =  (pu4_bitstream);                                    \
+    UWORD32 u4_word_off = ((u4_offset) >> 5);                               \
+    UWORD32 u4_bit_off = (u4_offset) & 0x1F;                                \
+    u4_code = pu4_buf[u4_word_off++] << u4_bit_off;                         \
+                                                                            \
+    if(u4_bit_off)                                                          \
+        u4_code |= (pu4_buf[u4_word_off] >> (INT_IN_BITS - u4_bit_off));    \
+    u4_code = u4_code >> (INT_IN_BITS - u4_no_bits);                        \
+    (u4_offset) += u4_no_bits;                                              \
+}                                                                           \
+
+static inline uint32_t CLZ(uint32_t u4_word)
+{
+  if (u4_word)
+    return (__builtin_clz(u4_word));
+  else
+    return 31;
+}
diff --git a/src/ts_indexer.c b/src/ts_indexer.c
index 8c37f37..cd1a67f 100644
--- a/src/ts_indexer.c
+++ b/src/ts_indexer.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <string.h>
 #include "ts_indexer.h"
+#include "bitstrm.h"
 
 #define TS_PKT_SIZE (188)
 
@@ -331,40 +332,38 @@
 
 uint32_t golomb_uev(uint32_t *pu4_bitstrm_ofst, uint32_t *pu4_bitstrm_buf)
 {
-  int u4_bitstream_offset = *pu4_bitstrm_ofst;
-  uint32_t leadingZeroBits = -1;
-  uint32_t codeNum = 0;
-  uint32_t flip_bitstrm_value = 0;
-  uint8_t *pu1_bitstrm_buf = (uint8_t *)pu4_bitstrm_buf;
+  uint32_t u4_bitstream_offset = *pu4_bitstrm_ofst;
+  uint32_t u4_word, u4_ldz;
 
-  if (u4_bitstream_offset >= 32 || u4_bitstream_offset < 0) {
-    ERR("error!!!! ofset: %d\n", *pu4_bitstrm_ofst);
-    return -1;
-  }
+  /* Find leading zeros in next 32 bits */
+  NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
+  u4_ldz = CLZ(u4_word);
+  //printf("u4_ldz: %d, u4_word: %#x, offset: %d, pu4_bitstrm_buf: %#x\n",
+  //          u4_ldz, u4_word, u4_bitstream_offset, *pu4_bitstrm_buf);
 
-  for (int i = 0; i < 4; i++) {
-    flip_bitstrm_value += (pu1_bitstrm_buf[3 - i] << (8 * i));
-  }
+  /* Flush the ps_bitstrm */
+  u4_bitstream_offset += (u4_ldz + 1);
 
-  /* count the leading zero bits */
-  for (uint8_t b = 0; !b && u4_bitstream_offset < 32; leadingZeroBits++ )
-  {
-    b = (flip_bitstrm_value >> (31 - u4_bitstream_offset)) & 0x01;
-    if (!b) {
-      u4_bitstream_offset++;
-    }
-  }
+  /* Read the suffix from the ps_bitstrm */
+  u4_word = 0;
+  if (u4_ldz)
+    GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf, u4_ldz);
 
-  for (int i = 0; i < leadingZeroBits; i++) {
-    codeNum |= ((flip_bitstrm_value >> (31 - u4_bitstream_offset - 1 - i)) & 0x01);
-    if (i < leadingZeroBits - 1)
-      codeNum <<= 1;
-  }
+  *pu4_bitstrm_ofst = u4_bitstream_offset;
 
-  codeNum += ((1 << leadingZeroBits) - 1);
-  *pu4_bitstrm_ofst = (u4_bitstream_offset + leadingZeroBits + 1);
+  return ((1 << u4_ldz) + u4_word - 1);
+}
 
-  return codeNum;
+uint32_t reverseBytes(uint32_t num)
+{
+  uint32_t result = 0;
+
+  result |= (num & 0xff) << 24;
+  result |= (num & 0xff00) << 8;
+  result |= (num & 0xff0000) >> 8;
+  result |= (num & 0xff000000) >> 24;
+
+  return result;
 }
 
 static void find_h264(uint8_t *data, size_t len, TS_Indexer_t *indexer, TSParser *stream)
@@ -406,8 +405,9 @@
       uint16_t u2_first_mb_in_slice;
       uint8_t slice_type;
 
-      u2_first_mb_in_slice = golomb_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
-      slice_type = golomb_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
+      uint32_t reverseNum = reverseBytes(*pu4_bitstrm_buf);
+      u2_first_mb_in_slice = golomb_uev(pu4_bitstrm_ofst, &reverseNum);
+      slice_type = golomb_uev(pu4_bitstrm_ofst, &reverseNum);
 
       event.pts = stream->PES.pts;
       if (nal_unit_type == NAL_TYPE_IDR) {