libavcodec/bitstream.c
de6d9b64
 /*
  * Common bit i/o utils
406792e7
  * Copyright (c) 2000, 2001 Fabrice Bellard
8f2ab833
  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
de6d9b64
  *
7b94177e
  * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
ff4ec49e
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
de6d9b64
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
de6d9b64
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
de6d9b64
  *
ff4ec49e
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64
  */
983e3246
 
 /**
bad5537e
  * @file libavcodec/bitstream.c
caa336b4
  * bitstream api.
983e3246
  */
115329f1
 
df595131
 #include "avcodec.h"
caa336b4
 #include "bitstream.h"
c81f0349
 
b3bf98aa
 const uint8_t ff_log2_run[32]={
  0, 0, 0, 0, 1, 1, 1, 1,
  2, 2, 2, 2, 3, 3, 3, 3,
  4, 4, 5, 5, 6, 6, 7, 7,
  8, 9,10,11,12,13,14,15
 };
 
beebfdb1
 /**
  * Same as av_mallocz_static(), but does a realloc.
  *
  * @param[in] ptr The block of memory to reallocate.
  * @param[in] size The requested size.
  * @return Block of memory of requested size.
19032450
  * @deprecated. Code which uses ff_realloc_static is broken/misdesigned
671adb17
  * and should correctly use static arrays
beebfdb1
  */
9f6152df
 attribute_deprecated av_alloc_size(2)
09dafaeb
 static void *ff_realloc_static(void *ptr, unsigned int size);
 
 static void *ff_realloc_static(void *ptr, unsigned int size)
 {
b9c83887
     return av_realloc(ptr, size);
09dafaeb
 }
 
de6d9b64
 void align_put_bits(PutBitContext *s)
 {
17592475
 #ifdef ALT_BITSTREAM_WRITER
     put_bits(s,(  - s->index) & 7,0);
 #else
d8cf5aea
     put_bits(s,s->bit_left & 7,0);
17592475
 #endif
de6d9b64
 }
 
1701cbfa
 void ff_put_string(PutBitContext * pbc, const char *s, int put_zero)
9717dad8
 {
     while(*s){
         put_bits(pbc, 8, *s);
         s++;
     }
99683a30
     if(put_zero)
         put_bits(pbc, 8, 0);
9717dad8
 }
 
1701cbfa
 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
98f7b56b
 {
1701cbfa
     const uint16_t *srcw= (const uint16_t*)src;
98f7b56b
     int words= length>>4;
     int bits= length&15;
     int i;
 
     if(length==0) return;
 
49fb20cb
     if(CONFIG_SMALL || words < 16 || put_bits_count(pb)&7){
98f7b56b
         for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
     }else{
         for(i=0; put_bits_count(pb)&31; i++)
             put_bits(pb, 8, src[i]);
         flush_put_bits(pb);
         memcpy(pbBufPtr(pb), src+i, 2*words-i);
         skip_put_bytes(pb, 2*words-i);
     }
 
     put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits));
 }
 
de6d9b64
 /* VLC decoding */
 
 //#define DEBUG_VLC
 
 #define GET_DATA(v, table, i, wrap, size) \
 {\
0c1a9eda
     const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
de6d9b64
     switch(size) {\
     case 1:\
0c1a9eda
         v = *(const uint8_t *)ptr;\
de6d9b64
         break;\
     case 2:\
0c1a9eda
         v = *(const uint16_t *)ptr;\
de6d9b64
         break;\
     default:\
0c1a9eda
         v = *(const uint32_t *)ptr;\
de6d9b64
         break;\
     }\
 }
 
 
073c2593
 static int alloc_table(VLC *vlc, int size, int use_static)
de6d9b64
 {
     int index;
     index = vlc->table_size;
     vlc->table_size += size;
     if (vlc->table_size > vlc->table_allocated) {
ccc54864
         if(use_static>1)
             abort(); //cant do anything, init_vlc() is used with too little memory
de6d9b64
         vlc->table_allocated += (1 << vlc->bits);
073c2593
         if(use_static)
5dad0282
             vlc->table = ff_realloc_static(vlc->table,
073c2593
                                            sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
         else
             vlc->table = av_realloc(vlc->table,
                                     sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
8db1a1dd
         if (!vlc->table)
de6d9b64
             return -1;
     }
     return index;
 }
 
8db1a1dd
 static int build_table(VLC *vlc, int table_nb_bits,
de6d9b64
                        int nb_codes,
                        const void *bits, int bits_wrap, int bits_size,
                        const void *codes, int codes_wrap, int codes_size,
b613bacc
                        const void *symbols, int symbols_wrap, int symbols_size,
cea27ac7
                        uint32_t code_prefix, int n_prefix, int flags)
de6d9b64
 {
b613bacc
     int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2, symbol;
0c1a9eda
     uint32_t code;
8db1a1dd
     VLC_TYPE (*table)[2];
de6d9b64
 
     table_size = 1 << table_nb_bits;
ccc54864
     table_index = alloc_table(vlc, table_size, flags & (INIT_VLC_USE_STATIC|INIT_VLC_USE_NEW_STATIC));
de6d9b64
 #ifdef DEBUG_VLC
b8a99745
     av_log(NULL,AV_LOG_DEBUG,"new table index=%d size=%d code_prefix=%x n=%d\n",
de6d9b64
            table_index, table_size, code_prefix, n_prefix);
 #endif
     if (table_index < 0)
         return -1;
8db1a1dd
     table = &vlc->table[table_index];
de6d9b64
 
     for(i=0;i<table_size;i++) {
8db1a1dd
         table[i][1] = 0; //bits
         table[i][0] = -1; //codes
de6d9b64
     }
 
     /* first pass: map codes and compute auxillary table sizes */
     for(i=0;i<nb_codes;i++) {
         GET_DATA(n, bits, i, bits_wrap, bits_size);
         GET_DATA(code, codes, i, codes_wrap, codes_size);
         /* we accept tables with holes */
         if (n <= 0)
             continue;
b613bacc
         if (!symbols)
             symbol = i;
         else
             GET_DATA(symbol, symbols, i, symbols_wrap, symbols_size);
de6d9b64
 #if defined(DEBUG_VLC) && 0
b8a99745
         av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code);
de6d9b64
 #endif
         /* if code matches the prefix, it is in the table */
         n -= n_prefix;
cea27ac7
         if(flags & INIT_VLC_LE)
             code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1);
         else
             code_prefix2= code >> n;
         if (n > 0 && code_prefix2 == code_prefix) {
de6d9b64
             if (n <= table_nb_bits) {
                 /* no need to add another table */
                 j = (code << (table_nb_bits - n)) & (table_size - 1);
                 nb = 1 << (table_nb_bits - n);
                 for(k=0;k<nb;k++) {
cea27ac7
                     if(flags & INIT_VLC_LE)
                         j = (code >> n_prefix) + (k<<n);
de6d9b64
 #ifdef DEBUG_VLC
3d0ef6dd
                     av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
de6d9b64
                            j, i, n);
 #endif
8db1a1dd
                     if (table[j][1] /*bits*/ != 0) {
9b879566
                         av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
9fe5a7b8
                         return -1;
de6d9b64
                     }
8db1a1dd
                     table[j][1] = n; //bits
b613bacc
                     table[j][0] = symbol;
de6d9b64
                     j++;
                 }
             } else {
                 n -= table_nb_bits;
cea27ac7
                 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
de6d9b64
 #ifdef DEBUG_VLC
b8a99745
                 av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n",
de6d9b64
                        j, n);
 #endif
                 /* compute table size */
8db1a1dd
                 n1 = -table[j][1]; //bits
de6d9b64
                 if (n > n1)
                     n1 = n;
8db1a1dd
                 table[j][1] = -n1; //bits
de6d9b64
             }
         }
     }
 
     /* second pass : fill auxillary tables recursively */
     for(i=0;i<table_size;i++) {
8db1a1dd
         n = table[i][1]; //bits
de6d9b64
         if (n < 0) {
             n = -n;
             if (n > table_nb_bits) {
                 n = table_nb_bits;
8db1a1dd
                 table[i][1] = -n; //bits
de6d9b64
             }
             index = build_table(vlc, n, nb_codes,
                                 bits, bits_wrap, bits_size,
                                 codes, codes_wrap, codes_size,
b613bacc
                                 symbols, symbols_wrap, symbols_size,
cea27ac7
                                 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
                                 n_prefix + table_nb_bits, flags);
de6d9b64
             if (index < 0)
                 return -1;
             /* note: realloc has been done, so reload tables */
8db1a1dd
             table = &vlc->table[table_index];
6300c80a
             table[i][0] = index; //code
de6d9b64
         }
     }
     return table_index;
 }
 
 
4e66ab3b
 /* Build VLC decoding tables suitable for use with get_vlc().
 
    'nb_bits' set thee decoding table size (2^nb_bits) entries. The
    bigger it is, the faster is the decoding. But it should not be too
    big to save memory and L1 cache. '9' is a good compromise.
115329f1
 
4e66ab3b
    'nb_codes' : number of vlcs codes
 
    'bits' : table which gives the size (in bits) of each vlc code.
 
    'codes' : table which gives the bit pattern of of each vlc code.
 
b613bacc
    'symbols' : table which gives the values to be returned from get_vlc().
 
4e66ab3b
    'xxx_wrap' : give the number of bytes between each entry of the
    'bits' or 'codes' tables.
 
    'xxx_size' : gives the number of bytes of each entry of the 'bits'
    or 'codes' tables.
 
    'wrap' and 'size' allows to use any memory configuration and types
b613bacc
    (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables.
073c2593
 
    'use_static' should be set to 1 for tables, which should be freed
    with av_free_static(), 0 if free_vlc() will be used.
4e66ab3b
 */
b613bacc
 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
de6d9b64
              const void *bits, int bits_wrap, int bits_size,
073c2593
              const void *codes, int codes_wrap, int codes_size,
b613bacc
              const void *symbols, int symbols_wrap, int symbols_size,
d7645fb9
              int flags)
de6d9b64
 {
     vlc->bits = nb_bits;
ccc54864
     if(flags & INIT_VLC_USE_NEW_STATIC){
         if(vlc->table_size && vlc->table_size == vlc->table_allocated){
             return 0;
         }else if(vlc->table_size){
             abort(); // fatal error, we are called on a partially initialized table
         }
     }else if(!(flags & INIT_VLC_USE_STATIC)) {
073c2593
         vlc->table = NULL;
         vlc->table_allocated = 0;
         vlc->table_size = 0;
     } else {
         /* Static tables are initially always NULL, return
            if vlc->table != NULL to avoid double allocation */
         if(vlc->table)
             return 0;
     }
 
de6d9b64
 #ifdef DEBUG_VLC
b8a99745
     av_log(NULL,AV_LOG_DEBUG,"build table nb_codes=%d\n", nb_codes);
de6d9b64
 #endif
 
     if (build_table(vlc, nb_bits, nb_codes,
                     bits, bits_wrap, bits_size,
                     codes, codes_wrap, codes_size,
b613bacc
                     symbols, symbols_wrap, symbols_size,
d7645fb9
                     0, 0, flags) < 0) {
85d366fd
         av_freep(&vlc->table);
de6d9b64
         return -1;
     }
ccc54864
     if((flags & INIT_VLC_USE_NEW_STATIC) && vlc->table_size != vlc->table_allocated)
         av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated);
de6d9b64
     return 0;
 }
 
 
 void free_vlc(VLC *vlc)
 {
85d366fd
     av_freep(&vlc->table);
de6d9b64
 }