Optimize the h264_get_codeNum in libmix

BZ: 145915

Optimize the h264_get_codeNum in libmix to accelerate the
h264 parsing process.

Change-Id: I4a43da8331422f0d686dcee49545ab99bd7daaff
Signed-off-by: wfeng6 <wei.feng@intel.com>
Reviewed-on: http://android.intel.com:8080/140125
Reviewed-by: Shi, PingX <pingx.shi@intel.com>
Tested-by: Shi, PingX <pingx.shi@intel.com>
Reviewed-by: cactus <cactus@intel.com>
Tested-by: cactus <cactus@intel.com>
diff --git a/mixvbp/include/vbp_trace.h b/mixvbp/include/vbp_trace.h
index fde232c..ba916b9 100755
--- a/mixvbp/include/vbp_trace.h
+++ b/mixvbp/include/vbp_trace.h
@@ -37,7 +37,7 @@
 
 // For Android OS
 
-#define LOG_NDEBUG 0
+//#define LOG_NDEBUG 0
 
 #define LOG_TAG "MixVBP"
 
diff --git a/mixvbp/vbp_plugin/h264/h264parse_bsd.c b/mixvbp/vbp_plugin/h264/h264parse_bsd.c
old mode 100755
new mode 100644
index 84f94b2..e5664ce
--- a/mixvbp/vbp_plugin/h264/h264parse_bsd.c
+++ b/mixvbp/vbp_plugin/h264/h264parse_bsd.c
@@ -14,9 +14,9 @@
 #include "h264.h"
 #include "h264parse.h"
 #include "viddec_parser_ops.h"
-
-
-
+#include "viddec_pm_utils_bstream.h"
+#include "viddec_pm.h"
+#include "vbp_trace.h"
 
 
 /**
@@ -33,7 +33,7 @@
 uint32_t h264_get_codeNum(void *parent, h264_Info* pInfo)
 {
     int32_t    leadingZeroBits= 0;
-    uint32_t    temp = 0, match = 0, noOfBits = 0, count = 0;
+    uint32_t   temp = 0, match = 0, noOfBits = 0, count = 0;
     uint32_t   codeNum =0;
     uint32_t   bits_offset =0, byte_offset =0;
     uint8_t    is_emul =0;
@@ -42,120 +42,103 @@
     uint32_t   bits_need_add_in_first_byte =0;
     int32_t    bits_operation_result=0;
 
-    //remove warning
-    pInfo = pInfo;
+    viddec_pm_utils_bstream_cxt_t *cxt = &((viddec_pm_cxt_t *)parent)->getbits;
+    viddec_pm_utils_bstream_buf_cxt_t* bstream = &cxt->bstrm_buf;
+    uint8_t curr_byte;
 
-    ////// Step 1: parse through zero bits until we find a bit with value 1.
-    viddec_pm_get_au_pos(parent, &bits_offset, &byte_offset, &is_emul);
+    bits_offset = bstream->buf_bitoff;
 
+    uint32_t total_bits, act_bytes;
+    uint32_t isemul = 0;
+    uint8_t *curr_addr = bstream->buf + bstream->buf_index;
+
+    uint32_t i = 0;
+    VTRACE("bstream->buf_bitoff = %d", bstream->buf_bitoff);
+    VTRACE("bstream->buf_index = %d", bstream->buf_index);
 
     while (!match)
     {
-        if ((bits_offset != 0) && ( is_first_byte == 1))
+        curr_byte = *curr_addr++;
+        VTRACE("curr_byte = 0x%x", curr_byte);
+        if (cxt->phase >= 2 && curr_byte == 0x03) {
+            curr_byte = *curr_addr++;
+            isemul = 1;
+            cxt->phase = 0;
+        }
+        noOfBits = 8;
+        if (is_first_byte)
         {
-            //we handle byte at a time, if we have offset then for first
-            //   byte handle only 8 - offset bits
-            noOfBits = (uint8_t)(8 - bits_offset);
-            bits_operation_result = viddec_pm_peek_bits(parent, &temp, noOfBits);
-
-
-            temp = (temp << bits_offset);
-            if (temp != 0)
-            {
-                bits_need_add_in_first_byte = bits_offset;
-            }
             is_first_byte = 0;
+            if (bits_offset != 0)
+            {
+                noOfBits = 8 - bits_offset;
+                curr_byte = curr_byte << bits_offset;
+            }
         }
         else
         {
-            noOfBits = 8;/* always 8 bits as we read a byte at a time */
-            bits_operation_result = viddec_pm_peek_bits(parent, &temp, 8);
+            cxt->phase = curr_byte? 0: cxt->phase + 1;
         }
 
-        if (-1 == bits_operation_result)
+        if (curr_byte != 0)
         {
-            return MAX_INT32_VALUE;
-        }
-
-        if (temp != 0)
-        {
-            // if byte!=0 we have at least one bit with value 1.
-            count = 1;
-            while (((temp & 0x80) != 0x80) && (count <= noOfBits))
+            count=1;
+            VTRACE("curr_byte & 0x80 = 0x%x", curr_byte & 0x80);
+            while (((curr_byte & 0x80) != 0x80) && (count <= noOfBits))
             {
+                VTRACE("curr_byte & 0x80 = 0x%x", curr_byte & 0x80);
                 count++;
-                temp = temp <<1;
+                curr_byte = curr_byte <<1;
             }
-            //At this point we get the bit position of 1 in current byte(count).
-
             match = 1;
             leadingZeroBits += count;
         }
         else
         {
-            // we don't have a 1 in current byte
             leadingZeroBits += noOfBits;
         }
 
-        if (!match)
+        VTRACE("count = %d", count);
+
+        total_bits = match ? count : noOfBits;
+        total_bits = noOfBits == 8? total_bits: total_bits + bits_offset;
+
+        VTRACE("total_bits = %d", total_bits);
+
+        act_bytes = 1 + isemul;
+        cxt->emulation_byte_counter += isemul;
+        isemul = 0;
+        if ((total_bits & 0x7) == 0)
         {
-            //actually move the bitoff by viddec_pm_get_bits
-            viddec_pm_get_bits(parent, &temp, noOfBits);
+            bstream->buf_bitoff = 0;
+            bstream->buf_index +=act_bytes;
         }
         else
         {
-            //actually move the bitoff by viddec_pm_get_bits
-            viddec_pm_get_bits(parent, &temp, count);
+            bstream->buf_bitoff = total_bits & 0x7;
+            bstream->buf_index += (act_bytes - 1);
         }
-
     }
-    ////// step 2: Now read the next (leadingZeroBits-1) bits to get the encoded value.
 
+    VTRACE("leadingZeroBits = %d", leadingZeroBits);
+    VTRACE("bstream->buf_bitoff = %x", bstream->buf_bitoff);
+    VTRACE("bstream->buf_index = %x", bstream->buf_index);
 
     if (match)
     {
-
-        viddec_pm_get_au_pos(parent, &bits_offset, &byte_offset, &is_emul);
-        /* bit position in current byte */
-        //count = (uint8_t)((leadingZeroBits + bits_offset)& 0x7);
-        count = ((count + bits_need_add_in_first_byte)& 0x7);
-
-        leadingZeroBits --;
-        length = leadingZeroBits;
+        length = --leadingZeroBits;
         codeNum = 0;
-        noOfBits = 8 - count;
-
-
-        while (leadingZeroBits > 0)
+        if (length > 0)
         {
-            if (noOfBits < (uint32_t)leadingZeroBits)
+            bits_operation_result = viddec_pm_get_bits(parent, &temp, leadingZeroBits);
+            if (-1 == bits_operation_result)
             {
-                viddec_pm_get_bits(parent, &temp, noOfBits);
-
-
-                codeNum = (codeNum << noOfBits) | temp;
-                leadingZeroBits -= noOfBits;
+                VTRACE("h264_get_codeNum: viddec_pm_get_bits error!");
+                length = 0;
             }
-            else
-            {
-                viddec_pm_get_bits(parent, &temp, leadingZeroBits);
-
-                codeNum = (codeNum << leadingZeroBits) | temp;
-                leadingZeroBits = 0;
-            }
-
-
-            noOfBits = 8;
+            codeNum = temp;
         }
-        // update codeNum = 2 ** (leadingZeroBits) -1 + read_bits(leadingZeroBits).
         codeNum = codeNum + (1 << length) -1;
-
-    }
-
-    viddec_pm_get_au_pos(parent, &bits_offset, &byte_offset, &is_emul);
-    if (bits_offset!=0)
-    {
-        viddec_pm_peek_bits(parent, &temp, 8-bits_offset);
     }
 
     return codeNum;
@@ -173,8 +156,8 @@
 
     if (bIsSigned) //get signed integer golomb code else the value is unsigned
     {
-        sign = (sval & 0x1) ? 1 : -1;
-        sval = (sval + 1) >> 1;
+        sign = (sval & 0x1)?1:-1;
+        sval = (sval +1) >> 1;
         sval = sval * sign;
     }
 
@@ -188,11 +171,11 @@
 {
     uint8_t cnt = 0;
 
-    uint8_t is_emul =0;
-    uint8_t cur_byte = 0;
-    int32_t shift_bits = 0;
+    uint8_t  is_emul =0;
+    uint8_t  cur_byte = 0;
+    int32_t  shift_bits =0;
     uint32_t ctr_bit = 0;
-    uint32_t bits_offset = 0, byte_offset =0;
+    uint32_t bits_offset =0, byte_offset =0;
 
     //remove warning
     pInfo = pInfo;
@@ -202,12 +185,12 @@
 
     viddec_pm_get_au_pos(parent, &bits_offset, &byte_offset, &is_emul);
 
-    shift_bits = 7 - bits_offset;
+    shift_bits = 7-bits_offset;
 
     // read one byte
     viddec_pm_get_cur_byte(parent, &cur_byte);
 
-    ctr_bit = (cur_byte >> (shift_bits--)) & 0x01;
+    ctr_bit = ((cur_byte)>> (shift_bits--)) & 0x01;
 
     // a stop bit has to be one
     if (ctr_bit == 0)
@@ -215,13 +198,8 @@
 
     while (shift_bits >= 0 && !cnt)
     {
-        cnt |= (((cur_byte) >> (shift_bits--)) & 0x01);   // set up control bit
+        cnt |= (((cur_byte)>> (shift_bits--)) & 0x01);   // set up control bit
     }
 
     return (cnt);
 }
-
-
-
-///////////// EOF/////////////////////
-