changeset 1025:1f9afd8b9131 libavcodec

GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
author michaelni
date Tue, 21 Jan 2003 17:34:12 +0000
parents 9cc1031e1864
children d6ba0641cc36
files common.c common.h dv.c h263.c h263dec.c huffyuv.c mjpeg.c mpeg12.c mpegaudiodec.c msmpeg4.c rv10.c svq1.c wmadec.c wmv2.c
diffstat 14 files changed, 63 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/common.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/common.c	Tue Jan 21 17:34:12 2003 +0000
@@ -99,10 +99,12 @@
 /* bit input functions */
 
 void init_get_bits(GetBitContext *s,
-                   UINT8 *buffer, int buffer_size)
+                   UINT8 *buffer, int bit_size)
 {
+    const int buffer_size= (bit_size+7)>>3;
+
     s->buffer= buffer;
-    s->size= buffer_size;
+    s->size_in_bits= bit_size;
     s->buffer_end= buffer + buffer_size;
 #ifdef ALT_BITSTREAM_READER
     s->index=0;
--- a/common.h	Mon Jan 20 22:50:14 2003 +0000
+++ b/common.h	Tue Jan 21 17:34:12 2003 +0000
@@ -261,7 +261,7 @@
     UINT32 cache1;
     int bit_count;
 #endif
-    int size;
+    int size_in_bits;
 } GetBitContext;
 
 static inline int get_bits_count(GetBitContext *s);
@@ -690,6 +690,12 @@
              const void *codes, int codes_wrap, int codes_size);
 void free_vlc(VLC *vlc);
 
+/**
+ *
+ * if the vlc code is invalid and max_depth=1 than no bits will be removed
+ * if the vlc code is invalid and max_depth>1 than the number of bits removed
+ * is undefined
+ */
 #define GET_VLC(code, name, gb, table, bits, max_depth)\
 {\
     int n, index, nb_bits;\
--- a/dv.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/dv.c	Tue Jan 21 17:34:12 2003 +0000
@@ -195,7 +195,7 @@
                v, partial_bit_count, (mb->partial_bit_buffer << l));
 #endif
         /* try to read the codeword */
-        init_get_bits(&gb1, buf, 4);
+        init_get_bits(&gb1, buf, 4*8);
         {
             OPEN_READER(re1, &gb1);
             UPDATE_CACHE(re1, &gb1);
@@ -333,7 +333,7 @@
         block = block1;
         for(j = 0;j < 6; j++) {
             /* NOTE: size is not important here */
-            init_get_bits(&s->gb, buf_ptr, 14);
+            init_get_bits(&s->gb, buf_ptr, 14*8);
             
             /* get the dc */
             dc = get_bits(&s->gb, 9);
@@ -382,7 +382,7 @@
 #endif
         block = block1;
         mb = mb1;
-        init_get_bits(&s->gb, mb_bit_buffer, 80);
+        init_get_bits(&s->gb, mb_bit_buffer, 80*8);
         for(j = 0;j < 6; j++) {
             if (!mb->eob_reached && s->gb.index < mb_bit_count) {
                 dv_decode_ac(s, mb, block, mb_bit_count);
@@ -421,7 +421,7 @@
 #endif
     block = &s->block[0][0];
     mb = mb_data;
-    init_get_bits(&s->gb, vs_bit_buffer, 5 * 80);
+    init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8);
     for(mb_index = 0; mb_index < 5; mb_index++) {
         for(j = 0;j < 6; j++) {
             if (!mb->eob_reached) {
@@ -501,7 +501,7 @@
     const UINT16 *mb_pos_ptr;
     
     /* parse id */
-    init_get_bits(&s->gb, buf, buf_size);
+    init_get_bits(&s->gb, buf, buf_size*8);
     sct = get_bits(&s->gb, 3);
     if (sct != 0)
         return -1;
--- a/h263.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/h263.c	Tue Jan 21 17:34:12 2003 +0000
@@ -2335,7 +2335,7 @@
 
         /* We have a GBSC probably with GSTUFF */
     skip_bits(&s->gb, 16); /* Drop the zeros */
-    left= s->gb.size*8 - get_bits_count(&s->gb);
+    left= s->gb.size_in_bits - get_bits_count(&s->gb);
     //MN: we must check the bits left or we might end in a infinite loop (or segfault)
     for(;left>13; left--){
         if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
@@ -2435,7 +2435,7 @@
         return 0;
     }
 
-    if(bits_count + 8 >= s->gb.size*8){
+    if(bits_count + 8 >= s->gb.size_in_bits){
         int v= show_bits(&s->gb, 8);
         v|= 0x7F >> (7-(bits_count&7));
                 
@@ -2472,7 +2472,7 @@
     int header_extension=0, mb_num, len;
     
     /* is there enough space left for a video packet + header */
-    if( get_bits_count(&s->gb) > s->gb.size*8-20) return -1;
+    if( get_bits_count(&s->gb) > s->gb.size_in_bits-20) return -1;
 
     for(len=0; len<32; len++){
         if(get_bits1(&s->gb)) break;
@@ -2604,7 +2604,7 @@
     //ok, its not where its supposed to be ...
     s->gb= s->last_resync_gb;
     align_get_bits(&s->gb);
-    left= s->gb.size*8 - get_bits_count(&s->gb);
+    left= s->gb.size_in_bits - get_bits_count(&s->gb);
     
     for(;left>16+1+5+5; left-=8){ 
         if(show_bits(&s->gb, 16)==0){
@@ -3058,7 +3058,7 @@
     /* per-MB end of slice check */
 
     if(--s->mb_num_left <= 0){
-//printf("%06X %d\n", show_bits(&s->gb, 24), s->gb.size*8 - get_bits_count(&s->gb));
+//printf("%06X %d\n", show_bits(&s->gb, 24), s->gb.size_in_bits - get_bits_count(&s->gb));
         if(mpeg4_is_resync(s))
             return SLICE_END;
         else
@@ -3422,8 +3422,8 @@
     }else{
         int v= show_bits(&s->gb, 16);
     
-        if(get_bits_count(&s->gb) + 16 > s->gb.size*8){
-            v>>= get_bits_count(&s->gb) + 16 - s->gb.size*8;
+        if(get_bits_count(&s->gb) + 16 > s->gb.size_in_bits){
+            v>>= get_bits_count(&s->gb) + 16 - s->gb.size_in_bits;
         }
 
         if(v==0)
@@ -4688,7 +4688,7 @@
              printf("qp:%d fc:%d,%d %s size:%d pro:%d alt:%d top:%d %spel part:%d resync:%d w:%d a:%d\n", 
                  s->qscale, s->f_code, s->b_code, 
                  s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")), 
-                 gb->size,s->progressive_sequence, s->alternate_scan, s->top_field_first, 
+                 gb->size_in_bits,s->progressive_sequence, s->alternate_scan, s->top_field_first, 
                  s->quarter_sample ? "q" : "h", s->data_partitioning, s->resync_marker, s->num_sprite_warping_points,
                  s->sprite_warping_accuracy); 
          }
@@ -4743,9 +4743,9 @@
         v = get_bits(gb, 8);
         startcode = ((startcode << 8) | v) & 0xffffffff;
         
-        if(get_bits_count(gb) >= gb->size*8){
-            if(gb->size==1 && s->divx_version){
-                printf("frame skip %d\n", gb->size);
+        if(get_bits_count(gb) >= gb->size_in_bits){
+            if(gb->size_in_bits==8 && s->divx_version){
+                printf("frame skip %d\n", gb->size_in_bits);
                 return FRAME_SKIPED; //divx bug
             }else
                 return -1; //end of stream
--- a/h263dec.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/h263dec.c	Tue Jan 21 17:34:12 2003 +0000
@@ -249,13 +249,13 @@
     /* try to detect the padding bug */
     if(      s->codec_id==CODEC_ID_MPEG4
        &&   (s->workaround_bugs&FF_BUG_AUTODETECT) 
-       &&    s->gb.size*8 - get_bits_count(&s->gb) >=0
-       &&    s->gb.size*8 - get_bits_count(&s->gb) < 48
+       &&    s->gb.size_in_bits - get_bits_count(&s->gb) >=0
+       &&    s->gb.size_in_bits - get_bits_count(&s->gb) < 48
 //       &&   !s->resync_marker
        &&   !s->data_partitioning){
         
         const int bits_count= get_bits_count(&s->gb);
-        const int bits_left = s->gb.size*8 - bits_count;
+        const int bits_left = s->gb.size_in_bits - bits_count;
         
         if(bits_left==0){
             s->padding_bug_score+=16;
@@ -274,7 +274,7 @@
 
     // handle formats which dont have unique end markers
     if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
-        int left= s->gb.size*8 - get_bits_count(&s->gb);
+        int left= s->gb.size_in_bits - get_bits_count(&s->gb);
         int max_extra=7;
         
         /* no markers in M$ crap */
@@ -299,7 +299,7 @@
     }
 
     fprintf(stderr, "slice end not reached but screenspace end (%d left %06X)\n", 
-            s->gb.size*8 - get_bits_count(&s->gb),
+            s->gb.size_in_bits - get_bits_count(&s->gb),
             show_bits(&s->gb, 24));
     return -1;
 }
@@ -441,9 +441,9 @@
 retry:
     
     if(s->bitstream_buffer_size && buf_size<20){ //divx 5.01+ frame reorder
-        init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
+        init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
     }else
-        init_get_bits(&s->gb, buf, buf_size);
+        init_get_bits(&s->gb, buf, buf_size*8);
     s->bitstream_buffer_size=0;
 
     if (!s->context_initialized) {
@@ -460,7 +460,7 @@
         if(s->avctx->extradata_size && s->picture_number==0){
             GetBitContext gb;
             
-            init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size);
+            init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
             ret = ff_mpeg4_decode_picture_header(s, &gb);
         }
         ret = ff_mpeg4_decode_picture_header(s, &s->gb);
@@ -632,7 +632,7 @@
     
     decode_slice(s);
     s->error_status_table[0]|= VP_START;
-    while(s->mb_y<s->mb_height && s->gb.size*8 - get_bits_count(&s->gb)>16){
+    while(s->mb_y<s->mb_height && s->gb.size_in_bits - get_bits_count(&s->gb)>16){
         if(s->msmpeg4_version){
             if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0)
                 break;
--- a/huffyuv.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/huffyuv.c	Tue Jan 21 17:34:12 2003 +0000
@@ -271,7 +271,7 @@
     GetBitContext gb;
     int i;
     
-    init_get_bits(&gb, src, length);
+    init_get_bits(&gb, src, length*8);
     
     for(i=0; i<3; i++){
         read_len_table(s->len[i], &gb);
@@ -295,9 +295,9 @@
     GetBitContext gb;
     int i;
 
-    init_get_bits(&gb, classic_shift_luma, sizeof(classic_shift_luma));
+    init_get_bits(&gb, classic_shift_luma, sizeof(classic_shift_luma)*8);
     read_len_table(s->len[0], &gb);
-    init_get_bits(&gb, classic_shift_chroma, sizeof(classic_shift_chroma));
+    init_get_bits(&gb, classic_shift_chroma, sizeof(classic_shift_chroma)*8);
     read_len_table(s->len[1], &gb);
     
     for(i=0; i<256; i++) s->bits[0][i] = classic_add_luma  [i];
@@ -680,7 +680,7 @@
 
     bswap_buf((uint32_t*)s->bitstream_buffer, (uint32_t*)buf, buf_size/4);
     
-    init_get_bits(&s->gb, s->bitstream_buffer, buf_size);
+    init_get_bits(&s->gb, s->bitstream_buffer, buf_size*8);
 
     p->reference= 0;
     if(avctx->get_buffer(avctx, p) < 0){
--- a/mjpeg.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/mjpeg.c	Tue Jan 21 17:34:12 2003 +0000
@@ -735,7 +735,7 @@
     if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
     {
 	printf("mjpeg: using external huffman table\n");
-	init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size);
+	init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
 	mjpeg_decode_dht(s);
 	/* should check for error - but dunno */
     }
@@ -1404,13 +1404,13 @@
 				break;
 			}
 		    }
-		    init_get_bits(&s->gb, s->buffer, dst - s->buffer);
+		    init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
 		    
 		    dprintf("escaping removed %d bytes\n",
 			(buf_end - buf_ptr) - (dst - s->buffer));
 		}
 		else
-		    init_get_bits(&s->gb, buf_ptr, buf_end - buf_ptr);
+		    init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
 		
 		s->start_code = start_code;
 
@@ -1548,7 +1548,7 @@
     /* reset on every SOI */
     s->restart_interval = 0;
 
-    init_get_bits(&hgb, buf_ptr, /*buf_size*/buf_end - buf_ptr);
+    init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
 
     skip_bits(&hgb, 32); /* reserved zeros */
     
@@ -1570,7 +1570,7 @@
     dprintf("dqt offs: 0x%x\n", dqt_offs);
     if (dqt_offs)
     {
-	init_get_bits(&s->gb, buf+dqt_offs, buf_end - (buf+dqt_offs));
+	init_get_bits(&s->gb, buf+dqt_offs, (buf_end - (buf+dqt_offs))*8);
 	s->start_code = DQT;
 	mjpeg_decode_dqt(s);
     }
@@ -1579,7 +1579,7 @@
     dprintf("dht offs: 0x%x\n", dht_offs);
     if (dht_offs)
     {
-	init_get_bits(&s->gb, buf+dht_offs, buf_end - (buf+dht_offs));
+	init_get_bits(&s->gb, buf+dht_offs, (buf_end - (buf+dht_offs))*8);
 	s->start_code = DHT;
 	mjpeg_decode_dht(s);
     }
@@ -1588,7 +1588,7 @@
     dprintf("sof offs: 0x%x\n", sof_offs);
     if (sof_offs)
     {
-	init_get_bits(&s->gb, buf+sof_offs, buf_end - (buf+sof_offs));
+	init_get_bits(&s->gb, buf+sof_offs, (buf_end - (buf+sof_offs))*8);
 	s->start_code = SOF0;
 	if (mjpeg_decode_sof0(s) < 0)
 	    return -1;
@@ -1598,8 +1598,8 @@
     dprintf("sos offs: 0x%x\n", sos_offs);
     if (sos_offs)
     {
-//	init_get_bits(&s->gb, buf+sos_offs, buf_end - (buf+sos_offs));
-	init_get_bits(&s->gb, buf+sos_offs, field_size);
+//	init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8);
+	init_get_bits(&s->gb, buf+sos_offs, field_size*8);
 	s->start_code = SOS;
 	mjpeg_decode_sos(s);
     }
--- a/mpeg12.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/mpeg12.c	Tue Jan 21 17:34:12 2003 +0000
@@ -1467,7 +1467,7 @@
     MpegEncContext *s = &s1->mpeg_enc_ctx;
     int ref, f_code;
 
-    init_get_bits(&s->gb, buf, buf_size);
+    init_get_bits(&s->gb, buf, buf_size*8);
 
     ref = get_bits(&s->gb, 10); /* temporal ref */
     s->pict_type = get_bits(&s->gb, 3);
@@ -1619,7 +1619,7 @@
     MpegEncContext *s = &s1->mpeg_enc_ctx;
     int ext_type;
 
-    init_get_bits(&s->gb, buf, buf_size);
+    init_get_bits(&s->gb, buf, buf_size*8);
     
     ext_type = get_bits(&s->gb, 4);
     switch(ext_type) {
@@ -1684,7 +1684,7 @@
         }
     }
 
-    init_get_bits(&s->gb, buf, buf_size);
+    init_get_bits(&s->gb, buf, buf_size*8);
 
     s->qscale = get_qscale(s);
     /* extra slice info */
@@ -1793,7 +1793,7 @@
     int width, height, i, v, j;
     float aspect;
 
-    init_get_bits(&s->gb, buf, buf_size);
+    init_get_bits(&s->gb, buf, buf_size*8);
 
     width = get_bits(&s->gb, 12);
     height = get_bits(&s->gb, 12);
--- a/mpegaudiodec.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/mpegaudiodec.c	Tue Jan 21 17:34:12 2003 +0000
@@ -1460,7 +1460,7 @@
     memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] + 
            BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
     /* init get bits again */
-    init_get_bits(&s->gb, ptr, s->frame_size + backstep);
+    init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
 
     /* prepare next buffer */
     s->inbuf_index ^= 1;
@@ -2280,7 +2280,7 @@
     short *samples_ptr;
 
     init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, 
-                  s->inbuf_ptr - s->inbuf - HEADER_SIZE);
+                  (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
     
     /* skip error protection field */
     if (s->error_protection)
--- a/msmpeg4.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/msmpeg4.c	Tue Jan 21 17:34:12 2003 +0000
@@ -1195,7 +1195,7 @@
 #if 0
 {
 int i;
-for(i=0; i<s->gb.size*8; i++)
+for(i=0; i<s->gb.size_in_bits; i++)
     printf("%d", get_bits1(&s->gb));
 //    get_bits1(&s->gb);
 printf("END\n");
@@ -1869,7 +1869,7 @@
         if (i > 62){
             i-= 192;
             if(i&(~63)){
-                const int left= s->gb.size*8 - get_bits_count(&s->gb);
+                const int left= s->gb.size_in_bits - get_bits_count(&s->gb);
                 if(((i+192 == 64 && level/qmul==-1) || s->error_resilience<=1) && left>=0){
                     fprintf(stderr, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
                     break;
--- a/rv10.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/rv10.c	Tue Jan 21 17:34:12 2003 +0000
@@ -395,7 +395,7 @@
     MpegEncContext *s = avctx->priv_data;
     int i, mb_count, mb_pos, left;
 
-    init_get_bits(&s->gb, buf, buf_size);
+    init_get_bits(&s->gb, buf, buf_size*8);
     
     mb_count = rv10_decode_picture_header(s);
     if (mb_count < 0) {
--- a/svq1.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/svq1.c	Tue Jan 21 17:34:12 2003 +0000
@@ -1066,7 +1066,7 @@
   AVFrame *pict = data; 
 
   /* initialize bit buffer */
-  init_get_bits(&s->gb,buf,buf_size);
+  init_get_bits(&s->gb,buf,buf_size*8);
 
   /* decode frame header */
   s->f_code = get_bits (&s->gb, 22);
--- a/wmadec.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/wmadec.c	Tue Jan 21 17:34:12 2003 +0000
@@ -1226,7 +1226,7 @@
 
     samples = data;
 
-    init_get_bits(&s->gb, buf, buf_size);
+    init_get_bits(&s->gb, buf, buf_size*8);
     
     if (s->use_bit_reservoir) {
         /* read super frame header */
@@ -1252,7 +1252,7 @@
             }
             
             /* XXX: bit_offset bits into last frame */
-            init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE);
+            init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
             /* skip unused bits */
             if (s->last_bitoffset > 0)
                 skip_bits(&s->gb, s->last_bitoffset);
@@ -1265,7 +1265,7 @@
 
         /* read each frame starting from bit_offset */
         pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
-        init_get_bits(&s->gb, buf + (pos >> 3), MAX_CODED_SUPERFRAME_SIZE - (pos >> 3));
+        init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
         len = pos & 7;
         if (len > 0)
             skip_bits(&s->gb, len);
--- a/wmv2.c	Mon Jan 20 22:50:14 2003 +0000
+++ b/wmv2.c	Tue Jan 21 17:34:12 2003 +0000
@@ -313,7 +313,7 @@
 
     if(s->avctx->extradata_size<4) return -1;
     
-    init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size);
+    init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
 
     fps                = get_bits(&gb, 5);
     s->bit_rate        = get_bits(&gb, 11)*1024;