]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/cabac.h
well that does not need to be there anymore
[ffmpeg] / libavcodec / cabac.h
index 1377c61f38ebba9f1ab32cc6cebbe0177959fe3a..21085b21e8a60c7c113b3a0e01e0d2ccd55745ea 100644 (file)
@@ -37,17 +37,19 @@ typedef struct CABACContext{
     uint8_t lps_range[2*64][4];   ///< rangeTabLPS
     uint8_t lps_state[2*64];      ///< transIdxLPS
     uint8_t mps_state[2*64];      ///< transIdxMPS
-    uint8_t *bytestream;
+    const uint8_t *bytestream_start;
+    const uint8_t *bytestream;
+    const uint8_t *bytestream_end;
     int bits_left;                ///<
     PutBitContext pb;
 }CABACContext;
 
-const uint8_t ff_h264_lps_range[64][4];
-const uint8_t ff_h264_mps_state[64];
-const uint8_t ff_h264_lps_state[64];
+extern const uint8_t ff_h264_lps_range[64][4];
+extern const uint8_t ff_h264_mps_state[64];
+extern const uint8_t ff_h264_lps_state[64];
 
 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
-void ff_init_cabac_decoder(CABACContext *c, uint8_t *buf, int buf_size);
+void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4], 
                           uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
 
@@ -138,7 +140,11 @@ static inline void put_cabac_bypass(CABACContext *c, int bit){
 #endif
 }
 
-static inline void put_cabac_terminate(CABACContext *c, int bit){
+/**
+ *
+ * @return the number of bytes written
+ */
+static inline int put_cabac_terminate(CABACContext *c, int bit){
     c->range -= 2;
 
     if(!bit){
@@ -159,6 +165,8 @@ static inline void put_cabac_terminate(CABACContext *c, int bit){
 #ifdef STRICT_LIMITS
     c->symCount++;
 #endif
+
+    return (put_bits_count(&c->pb)+7)>>3;
 }
 
 /**
@@ -199,12 +207,16 @@ static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max,
 /**
  * put unary exp golomb k-th order binarization.
  */
-static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int sign, int max, int is_signed, int k, int max_index){
+static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
     int i;
     
     if(v==0)
         put_cabac(c, state, 0);
     else{
+        const int sign= v < 0;
+        
+        if(is_signed) v= ABS(v);
+        
         if(v<max){
             for(i=0; i<v; i++){
                 put_cabac(c, state, 1);
@@ -242,7 +254,9 @@ static inline void renorm_cabac_decoder(CABACContext *c){
         c->range+= c->range;
         c->low+= c->low;
         if(--c->bits_left == 0){
-            c->low+= *c->bytestream++;
+            if(c->bytestream < c->bytestream_end)
+                c->low+= *c->bytestream;
+            c->bytestream++;
             c->bits_left= 8;
         }
     }
@@ -287,7 +301,9 @@ static inline int get_cabac_bypass(CABACContext *c){
     c->low += c->low;
 
     if(--c->bits_left == 0){
-        c->low+= *c->bytestream++;
+        if(c->bytestream < c->bytestream_end)
+            c->low+= *c->bytestream;
+        c->bytestream++;
         c->bits_left= 8;
     }
     
@@ -299,13 +315,17 @@ static inline int get_cabac_bypass(CABACContext *c){
     }
 }
 
+/**
+ *
+ * @return the number of bytes read or 0 if no end
+ */
 static inline int get_cabac_terminate(CABACContext *c){
     c->range -= 2<<8;
     if(c->low < c->range){
         renorm_cabac_decoder(c);    
         return 0;
     }else{
-        return 1;
+        return c->bytestream - c->bytestream_start;
     }    
 }