* Build VLC decoding tables suitable for use with get_vlc(). * * @param vlc the context to be initted * * @param table_nb_bits max length of vlc codes to store directly in this table * (Longer codes are delegated to subtables.) * * @param nb_codes number of elements in codes[] * * @param codes descriptions of the vlc codes *
| 153 | * Sorting by VLCcode.code is sufficient, though not necessary. |
| 154 | */ |
| 155 | static int build_table(VLC *vlc, int table_nb_bits, int nb_codes, |
| 156 | VLCcode *codes, int flags) |
| 157 | { |
| 158 | int table_size, table_index, index, code_prefix, symbol, subtable_bits; |
| 159 | int i, j, k, n, nb, inc; |
| 160 | uint32_t code; |
| 161 | VLC_TYPE (*table)[2]; |
| 162 | |
| 163 | table_size = 1 << table_nb_bits; |
| 164 | table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_NEW_STATIC); |
| 165 | #ifdef DEBUG_VLC |
| 166 | av_log(NULL,AV_LOG_DEBUG,"new table index=%d size=%d\n", |
| 167 | table_index, table_size); |
| 168 | #endif |
| 169 | if (table_index < 0) |
| 170 | return -1; |
| 171 | table = &vlc->table[table_index]; |
| 172 | |
| 173 | for (i = 0; i < table_size; i++) { |
| 174 | table[i][1] = 0; //bits |
| 175 | table[i][0] = -1; //codes |
| 176 | } |
| 177 | |
| 178 | /* first pass: map codes and compute auxillary table sizes */ |
| 179 | for (i = 0; i < nb_codes; i++) { |
| 180 | n = codes[i].bits; |
| 181 | code = codes[i].code; |
| 182 | symbol = codes[i].symbol; |
| 183 | #if defined(DEBUG_VLC) && 0 |
| 184 | av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code); |
| 185 | #endif |
| 186 | if (n <= table_nb_bits) { |
| 187 | /* no need to add another table */ |
| 188 | j = code >> (32 - table_nb_bits); |
| 189 | nb = 1 << (table_nb_bits - n); |
| 190 | inc = 1; |
| 191 | if (flags & INIT_VLC_LE) { |
| 192 | j = bitswap_32(code); |
| 193 | inc = 1 << n; |
| 194 | } |
| 195 | for (k = 0; k < nb; k++) { |
| 196 | #ifdef DEBUG_VLC |
| 197 | av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n", |
| 198 | j, i, n); |
| 199 | #endif |
| 200 | if (table[j][1] /*bits*/ != 0) { |
| 201 | av_log(NULL, AV_LOG_ERROR, "incorrect codes\n"); |
| 202 | return -1; |
| 203 | } |
| 204 | table[j][1] = n; //bits |
| 205 | table[j][0] = symbol; |
| 206 | j += inc; |
| 207 | } |
| 208 | } else { |
| 209 | /* fill auxiliary table recursively */ |
| 210 | n -= table_nb_bits; |
| 211 | code_prefix = code >> (32 - table_nb_bits); |
| 212 | subtable_bits = n; |
no test coverage detected