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, 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, uint8_t *src, int length)
00064 {
00065 const uint16_t *srcw= (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 if(use_static>1)
00113 abort();
00114 vlc->table_allocated += (1 << vlc->bits);
00115 if(use_static)
00116 vlc->table = ff_realloc_static(vlc->table,
00117 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
00118 else
00119 vlc->table = av_realloc(vlc->table,
00120 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
00121 if (!vlc->table)
00122 return -1;
00123 }
00124 return index;
00125 }
00126
00127 static int build_table(VLC *vlc, int table_nb_bits,
00128 int nb_codes,
00129 const void *bits, int bits_wrap, int bits_size,
00130 const void *codes, int codes_wrap, int codes_size,
00131 const void *symbols, int symbols_wrap, int symbols_size,
00132 uint32_t code_prefix, int n_prefix, int flags)
00133 {
00134 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2, symbol;
00135 uint32_t code;
00136 VLC_TYPE (*table)[2];
00137
00138 table_size = 1 << table_nb_bits;
00139 table_index = alloc_table(vlc, table_size, flags & (INIT_VLC_USE_STATIC|INIT_VLC_USE_NEW_STATIC));
00140 #ifdef DEBUG_VLC
00141 av_log(NULL,AV_LOG_DEBUG,"new table index=%d size=%d code_prefix=%x n=%d\n",
00142 table_index, table_size, code_prefix, n_prefix);
00143 #endif
00144 if (table_index < 0)
00145 return -1;
00146 table = &vlc->table[table_index];
00147
00148 for(i=0;i<table_size;i++) {
00149 table[i][1] = 0;
00150 table[i][0] = -1;
00151 }
00152
00153
00154 for(i=0;i<nb_codes;i++) {
00155 GET_DATA(n, bits, i, bits_wrap, bits_size);
00156 GET_DATA(code, codes, i, codes_wrap, codes_size);
00157
00158 if (n <= 0)
00159 continue;
00160 if (!symbols)
00161 symbol = i;
00162 else
00163 GET_DATA(symbol, symbols, i, symbols_wrap, symbols_size);
00164 #if defined(DEBUG_VLC) && 0
00165 av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code);
00166 #endif
00167
00168 n -= n_prefix;
00169 if(flags & INIT_VLC_LE)
00170 code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1);
00171 else
00172 code_prefix2= code >> n;
00173 if (n > 0 && code_prefix2 == code_prefix) {
00174 if (n <= table_nb_bits) {
00175
00176 j = (code << (table_nb_bits - n)) & (table_size - 1);
00177 nb = 1 << (table_nb_bits - n);
00178 for(k=0;k<nb;k++) {
00179 if(flags & INIT_VLC_LE)
00180 j = (code >> n_prefix) + (k<<n);
00181 #ifdef DEBUG_VLC
00182 av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
00183 j, i, n);
00184 #endif
00185 if (table[j][1] != 0) {
00186 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
00187 return -1;
00188 }
00189 table[j][1] = n;
00190 table[j][0] = symbol;
00191 j++;
00192 }
00193 } else {
00194 n -= table_nb_bits;
00195 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
00196 #ifdef DEBUG_VLC
00197 av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n",
00198 j, n);
00199 #endif
00200
00201 n1 = -table[j][1];
00202 if (n > n1)
00203 n1 = n;
00204 table[j][1] = -n1;
00205 }
00206 }
00207 }
00208
00209
00210 for(i=0;i<table_size;i++) {
00211 n = table[i][1];
00212 if (n < 0) {
00213 n = -n;
00214 if (n > table_nb_bits) {
00215 n = table_nb_bits;
00216 table[i][1] = -n;
00217 }
00218 index = build_table(vlc, n, nb_codes,
00219 bits, bits_wrap, bits_size,
00220 codes, codes_wrap, codes_size,
00221 symbols, symbols_wrap, symbols_size,
00222 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
00223 n_prefix + table_nb_bits, flags);
00224 if (index < 0)
00225 return -1;
00226
00227 table = &vlc->table[table_index];
00228 table[i][0] = index;
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 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 vlc->bits = nb_bits;
00268 if(flags & INIT_VLC_USE_NEW_STATIC){
00269 if(vlc->table_size && vlc->table_size == vlc->table_allocated){
00270 return 0;
00271 }else if(vlc->table_size){
00272 abort();
00273 }
00274 }else if(!(flags & INIT_VLC_USE_STATIC)) {
00275 vlc->table = NULL;
00276 vlc->table_allocated = 0;
00277 vlc->table_size = 0;
00278 } else {
00279
00280
00281 if(vlc->table)
00282 return 0;
00283 }
00284
00285 #ifdef DEBUG_VLC
00286 av_log(NULL,AV_LOG_DEBUG,"build table nb_codes=%d\n", nb_codes);
00287 #endif
00288
00289 if (build_table(vlc, nb_bits, nb_codes,
00290 bits, bits_wrap, bits_size,
00291 codes, codes_wrap, codes_size,
00292 symbols, symbols_wrap, symbols_size,
00293 0, 0, flags) < 0) {
00294 av_freep(&vlc->table);
00295 return -1;
00296 }
00297 if((flags & INIT_VLC_USE_NEW_STATIC) && vlc->table_size != vlc->table_allocated)
00298 av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated);
00299 return 0;
00300 }
00301
00302
00303 void free_vlc(VLC *vlc)
00304 {
00305 av_freep(&vlc->table);
00306 }
00307