00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00031 #include "avcodec.h"
00032 #include "get_bits.h"
00033 #include "put_bits.h"
00034
00035 const uint8_t ff_log2_run[41]={
00036 0, 0, 0, 0, 1, 1, 1, 1,
00037 2, 2, 2, 2, 3, 3, 3, 3,
00038 4, 4, 5, 5, 6, 6, 7, 7,
00039 8, 9,10,11,12,13,14,15,
00040 16,17,18,19,20,21,22,23,
00041 24,
00042 };
00043
00044 void avpriv_align_put_bits(PutBitContext *s)
00045 {
00046 put_bits(s,s->bit_left & 7,0);
00047 }
00048
00049 void ff_put_string(PutBitContext *pb, const char *string, int terminate_string)
00050 {
00051 while(*string){
00052 put_bits(pb, 8, *string);
00053 string++;
00054 }
00055 if(terminate_string)
00056 put_bits(pb, 8, 0);
00057 }
00058
00059 void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
00060 {
00061 int words= length>>4;
00062 int bits= length&15;
00063 int i;
00064
00065 if(length==0) return;
00066
00067 if(CONFIG_SMALL || words < 16 || put_bits_count(pb)&7){
00068 for(i=0; i<words; i++) put_bits(pb, 16, AV_RB16(src + 2*i));
00069 }else{
00070 for(i=0; put_bits_count(pb)&31; i++)
00071 put_bits(pb, 8, src[i]);
00072 flush_put_bits(pb);
00073 memcpy(put_bits_ptr(pb), src+i, 2*words-i);
00074 skip_put_bytes(pb, 2*words-i);
00075 }
00076
00077 put_bits(pb, bits, AV_RB16(src + 2*words)>>(16-bits));
00078 }
00079
00080
00081
00082 #define GET_DATA(v, table, i, wrap, size) \
00083 {\
00084 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
00085 switch(size) {\
00086 case 1:\
00087 v = *(const uint8_t *)ptr;\
00088 break;\
00089 case 2:\
00090 v = *(const uint16_t *)ptr;\
00091 break;\
00092 default:\
00093 v = *(const uint32_t *)ptr;\
00094 break;\
00095 }\
00096 }
00097
00098
00099 static int alloc_table(VLC *vlc, int size, int use_static)
00100 {
00101 int index;
00102 index = vlc->table_size;
00103 vlc->table_size += size;
00104 if (vlc->table_size > vlc->table_allocated) {
00105 if(use_static)
00106 abort();
00107 vlc->table_allocated += (1 << vlc->bits);
00108 vlc->table = av_realloc_f(vlc->table,
00109 vlc->table_allocated, sizeof(VLC_TYPE) * 2);
00110 if (!vlc->table)
00111 return -1;
00112 }
00113 return index;
00114 }
00115
00116 static av_always_inline uint32_t bitswap_32(uint32_t x) {
00117 return (uint32_t)av_reverse[x&0xFF]<<24
00118 | (uint32_t)av_reverse[(x>>8)&0xFF]<<16
00119 | (uint32_t)av_reverse[(x>>16)&0xFF]<<8
00120 | (uint32_t)av_reverse[x>>24];
00121 }
00122
00123 typedef struct {
00124 uint8_t bits;
00125 uint16_t symbol;
00128 uint32_t code;
00129 } VLCcode;
00130
00131 static int compare_vlcspec(const void *a, const void *b)
00132 {
00133 const VLCcode *sa=a, *sb=b;
00134 return (sa->code >> 1) - (sb->code >> 1);
00135 }
00136
00151 static int build_table(VLC *vlc, int table_nb_bits, int nb_codes,
00152 VLCcode *codes, int flags)
00153 {
00154 int table_size, table_index, index, code_prefix, symbol, subtable_bits;
00155 int i, j, k, n, nb, inc;
00156 uint32_t code;
00157 VLC_TYPE (*table)[2];
00158
00159 table_size = 1 << table_nb_bits;
00160 if (table_nb_bits > 30)
00161 return -1;
00162 table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_NEW_STATIC);
00163 av_dlog(NULL, "new table index=%d size=%d\n", table_index, table_size);
00164 if (table_index < 0)
00165 return -1;
00166 table = &vlc->table[table_index];
00167
00168 for (i = 0; i < table_size; i++) {
00169 table[i][1] = 0;
00170 table[i][0] = -1;
00171 }
00172
00173
00174 for (i = 0; i < nb_codes; i++) {
00175 n = codes[i].bits;
00176 code = codes[i].code;
00177 symbol = codes[i].symbol;
00178 av_dlog(NULL, "i=%d n=%d code=0x%x\n", i, n, code);
00179 if (n <= table_nb_bits) {
00180
00181 j = code >> (32 - table_nb_bits);
00182 nb = 1 << (table_nb_bits - n);
00183 inc = 1;
00184 if (flags & INIT_VLC_LE) {
00185 j = bitswap_32(code);
00186 inc = 1 << n;
00187 }
00188 for (k = 0; k < nb; k++) {
00189 av_dlog(NULL, "%4x: code=%d n=%d\n", j, i, n);
00190 if (table[j][1] != 0) {
00191 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
00192 return -1;
00193 }
00194 table[j][1] = n;
00195 table[j][0] = symbol;
00196 j += inc;
00197 }
00198 } else {
00199
00200 n -= table_nb_bits;
00201 code_prefix = code >> (32 - table_nb_bits);
00202 subtable_bits = n;
00203 codes[i].bits = n;
00204 codes[i].code = code << table_nb_bits;
00205 for (k = i+1; k < nb_codes; k++) {
00206 n = codes[k].bits - table_nb_bits;
00207 if (n <= 0)
00208 break;
00209 code = codes[k].code;
00210 if (code >> (32 - table_nb_bits) != code_prefix)
00211 break;
00212 codes[k].bits = n;
00213 codes[k].code = code << table_nb_bits;
00214 subtable_bits = FFMAX(subtable_bits, n);
00215 }
00216 subtable_bits = FFMIN(subtable_bits, table_nb_bits);
00217 j = (flags & INIT_VLC_LE) ? bitswap_32(code_prefix) >> (32 - table_nb_bits) : code_prefix;
00218 table[j][1] = -subtable_bits;
00219 av_dlog(NULL, "%4x: n=%d (subtable)\n",
00220 j, codes[i].bits + table_nb_bits);
00221 index = build_table(vlc, subtable_bits, k-i, codes+i, flags);
00222 if (index < 0)
00223 return -1;
00224
00225 table = &vlc->table[table_index];
00226 table[j][0] = index;
00227 i = k-1;
00228 }
00229 }
00230 return table_index;
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
00261 const void *bits, int bits_wrap, int bits_size,
00262 const void *codes, int codes_wrap, int codes_size,
00263 const void *symbols, int symbols_wrap, int symbols_size,
00264 int flags)
00265 {
00266 VLCcode *buf;
00267 int i, j, ret;
00268
00269 vlc->bits = nb_bits;
00270 if(flags & INIT_VLC_USE_NEW_STATIC){
00271 if(vlc->table_size && vlc->table_size == vlc->table_allocated){
00272 return 0;
00273 }else if(vlc->table_size){
00274 abort();
00275 }
00276 }else {
00277 vlc->table = NULL;
00278 vlc->table_allocated = 0;
00279 vlc->table_size = 0;
00280 }
00281
00282 av_dlog(NULL, "build table nb_codes=%d\n", nb_codes);
00283
00284 buf = av_malloc((nb_codes+1)*sizeof(VLCcode));
00285
00286 assert(symbols_size <= 2 || !symbols);
00287 j = 0;
00288 #define COPY(condition)\
00289 for (i = 0; i < nb_codes; i++) {\
00290 GET_DATA(buf[j].bits, bits, i, bits_wrap, bits_size);\
00291 if (!(condition))\
00292 continue;\
00293 GET_DATA(buf[j].code, codes, i, codes_wrap, codes_size);\
00294 if (flags & INIT_VLC_LE)\
00295 buf[j].code = bitswap_32(buf[j].code);\
00296 else\
00297 buf[j].code <<= 32 - buf[j].bits;\
00298 if (symbols)\
00299 GET_DATA(buf[j].symbol, symbols, i, symbols_wrap, symbols_size)\
00300 else\
00301 buf[j].symbol = i;\
00302 j++;\
00303 }
00304 COPY(buf[j].bits > nb_bits);
00305
00306 qsort(buf, j, sizeof(VLCcode), compare_vlcspec);
00307 COPY(buf[j].bits && buf[j].bits <= nb_bits);
00308 nb_codes = j;
00309
00310 ret = build_table(vlc, nb_bits, nb_codes, buf, flags);
00311
00312 av_free(buf);
00313 if (ret < 0) {
00314 av_freep(&vlc->table);
00315 return -1;
00316 }
00317 if((flags & INIT_VLC_USE_NEW_STATIC) && vlc->table_size != vlc->table_allocated)
00318 av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated);
00319 return 0;
00320 }
00321
00322
00323 void ff_free_vlc(VLC *vlc)
00324 {
00325 av_freep(&vlc->table);
00326 }