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