00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include "avcodec.h"
00031 #include "bitstream.h"
00032
00042 attribute_deprecated void *ff_realloc_static(void *ptr, unsigned int size);
00043
00044 void align_put_bits(PutBitContext *s)
00045 {
00046 #ifdef ALT_BITSTREAM_WRITER
00047 put_bits(s,( - s->index) & 7,0);
00048 #else
00049 put_bits(s,s->bit_left & 7,0);
00050 #endif
00051 }
00052
00053 void ff_put_string(PutBitContext * pbc, const char *s, int put_zero)
00054 {
00055 while(*s){
00056 put_bits(pbc, 8, *s);
00057 s++;
00058 }
00059 if(put_zero)
00060 put_bits(pbc, 8, 0);
00061 }
00062
00063 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
00064 {
00065 const uint16_t *srcw= (const uint16_t*)src;
00066 int words= length>>4;
00067 int bits= length&15;
00068 int i;
00069
00070 if(length==0) return;
00071
00072 if(ENABLE_SMALL || words < 16 || put_bits_count(pb)&7){
00073 for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
00074 }else{
00075 for(i=0; put_bits_count(pb)&31; i++)
00076 put_bits(pb, 8, src[i]);
00077 flush_put_bits(pb);
00078 memcpy(pbBufPtr(pb), src+i, 2*words-i);
00079 skip_put_bytes(pb, 2*words-i);
00080 }
00081
00082 put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits));
00083 }
00084
00085
00086
00087
00088
00089 #define GET_DATA(v, table, i, wrap, size) \
00090 {\
00091 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
00092 switch(size) {\
00093 case 1:\
00094 v = *(const uint8_t *)ptr;\
00095 break;\
00096 case 2:\
00097 v = *(const uint16_t *)ptr;\
00098 break;\
00099 default:\
00100 v = *(const uint32_t *)ptr;\
00101 break;\
00102 }\
00103 }
00104
00105
00106 static int alloc_table(VLC *vlc, int size, int use_static)
00107 {
00108 int index;
00109 index = vlc->table_size;
00110 vlc->table_size += size;
00111 if (vlc->table_size > vlc->table_allocated) {
00112 vlc->table_allocated += (1 << vlc->bits);
00113 if(use_static)
00114 vlc->table = ff_realloc_static(vlc->table,
00115 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
00116 else
00117 vlc->table = av_realloc(vlc->table,
00118 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
00119 if (!vlc->table)
00120 return -1;
00121 }
00122 return index;
00123 }
00124
00125 static int build_table(VLC *vlc, int table_nb_bits,
00126 int nb_codes,
00127 const void *bits, int bits_wrap, int bits_size,
00128 const void *codes, int codes_wrap, int codes_size,
00129 const void *symbols, int symbols_wrap, int symbols_size,
00130 uint32_t code_prefix, int n_prefix, int flags)
00131 {
00132 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2, symbol;
00133 uint32_t code;
00134 VLC_TYPE (*table)[2];
00135
00136 table_size = 1 << table_nb_bits;
00137 table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_STATIC);
00138 #ifdef DEBUG_VLC
00139 av_log(NULL,AV_LOG_DEBUG,"new table index=%d size=%d code_prefix=%x n=%d\n",
00140 table_index, table_size, code_prefix, n_prefix);
00141 #endif
00142 if (table_index < 0)
00143 return -1;
00144 table = &vlc->table[table_index];
00145
00146 for(i=0;i<table_size;i++) {
00147 table[i][1] = 0;
00148 table[i][0] = -1;
00149 }
00150
00151
00152 for(i=0;i<nb_codes;i++) {
00153 GET_DATA(n, bits, i, bits_wrap, bits_size);
00154 GET_DATA(code, codes, i, codes_wrap, codes_size);
00155
00156 if (n <= 0)
00157 continue;
00158 if (!symbols)
00159 symbol = i;
00160 else
00161 GET_DATA(symbol, symbols, i, symbols_wrap, symbols_size);
00162 #if defined(DEBUG_VLC) && 0
00163 av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code);
00164 #endif
00165
00166 n -= n_prefix;
00167 if(flags & INIT_VLC_LE)
00168 code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1);
00169 else
00170 code_prefix2= code >> n;
00171 if (n > 0 && code_prefix2 == code_prefix) {
00172 if (n <= table_nb_bits) {
00173
00174 j = (code << (table_nb_bits - n)) & (table_size - 1);
00175 nb = 1 << (table_nb_bits - n);
00176 for(k=0;k<nb;k++) {
00177 if(flags & INIT_VLC_LE)
00178 j = (code >> n_prefix) + (k<<n);
00179 #ifdef DEBUG_VLC
00180 av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
00181 j, i, n);
00182 #endif
00183 if (table[j][1] != 0) {
00184 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
00185 return -1;
00186 }
00187 table[j][1] = n;
00188 table[j][0] = symbol;
00189 j++;
00190 }
00191 } else {
00192 n -= table_nb_bits;
00193 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
00194 #ifdef DEBUG_VLC
00195 av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n",
00196 j, n);
00197 #endif
00198
00199 n1 = -table[j][1];
00200 if (n > n1)
00201 n1 = n;
00202 table[j][1] = -n1;
00203 }
00204 }
00205 }
00206
00207
00208 for(i=0;i<table_size;i++) {
00209 n = table[i][1];
00210 if (n < 0) {
00211 n = -n;
00212 if (n > table_nb_bits) {
00213 n = table_nb_bits;
00214 table[i][1] = -n;
00215 }
00216 index = build_table(vlc, n, nb_codes,
00217 bits, bits_wrap, bits_size,
00218 codes, codes_wrap, codes_size,
00219 symbols, symbols_wrap, symbols_size,
00220 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
00221 n_prefix + table_nb_bits, flags);
00222 if (index < 0)
00223 return -1;
00224
00225 table = &vlc->table[table_index];
00226 table[i][0] = index;
00227 }
00228 }
00229 return table_index;
00230 }
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 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
00260 const void *bits, int bits_wrap, int bits_size,
00261 const void *codes, int codes_wrap, int codes_size,
00262 const void *symbols, int symbols_wrap, int symbols_size,
00263 int flags)
00264 {
00265 vlc->bits = nb_bits;
00266 if(!(flags & INIT_VLC_USE_STATIC)) {
00267 vlc->table = NULL;
00268 vlc->table_allocated = 0;
00269 vlc->table_size = 0;
00270 } else {
00271
00272
00273 if(vlc->table)
00274 return 0;
00275 }
00276
00277 #ifdef DEBUG_VLC
00278 av_log(NULL,AV_LOG_DEBUG,"build table nb_codes=%d\n", nb_codes);
00279 #endif
00280
00281 if (build_table(vlc, nb_bits, nb_codes,
00282 bits, bits_wrap, bits_size,
00283 codes, codes_wrap, codes_size,
00284 symbols, symbols_wrap, symbols_size,
00285 0, 0, flags) < 0) {
00286 av_freep(&vlc->table);
00287 return -1;
00288 }
00289 return 0;
00290 }
00291
00292
00293 void free_vlc(VLC *vlc)
00294 {
00295 av_freep(&vlc->table);
00296 }
00297