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