Revision b23cf13c

View differences:

libavcodec/bitstream.c
170 170
        return -1;
171 171
    table = &vlc->table[table_index];
172 172

  
173
    for(i=0;i<table_size;i++) {
173
    for (i = 0; i < table_size; i++) {
174 174
        table[i][1] = 0; //bits
175 175
        table[i][0] = -1; //codes
176 176
    }
177 177

  
178 178
    /* first pass: map codes and compute auxillary table sizes */
179
    for(i=0;i<nb_codes;i++) {
179
    for (i = 0; i < nb_codes; i++) {
180 180
        n = codes[i].bits;
181 181
        code = codes[i].code;
182 182
        symbol = codes[i].symbol;
183 183
#if defined(DEBUG_VLC) && 0
184 184
        av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code);
185 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++) {
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 196
#ifdef DEBUG_VLC
197
                        av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
198
                               j, i, n);
197
                av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
198
                       j, i, n);
199 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;
213
                    codes[i].bits = n;
214
                    codes[i].code = code << table_nb_bits;
215
                    for (k = i+1; k < nb_codes; k++) {
216
                        n = codes[k].bits - table_nb_bits;
217
                        if (n <= 0)
218
                            break;
219
                        code = codes[k].code;
220
                        if (code >> (32 - table_nb_bits) != code_prefix)
221
                            break;
222
                        codes[k].bits = n;
223
                        codes[k].code = code << table_nb_bits;
224
                        subtable_bits = FFMAX(subtable_bits, n);
225
                    }
226
                    subtable_bits = FFMIN(subtable_bits, table_nb_bits);
227
                    j = (flags & INIT_VLC_LE) ? bitswap_32(code_prefix) >> (32 - table_nb_bits) : code_prefix;
228
                    table[j][1] = -subtable_bits;
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;
213
            codes[i].bits = n;
214
            codes[i].code = code << table_nb_bits;
215
            for (k = i+1; k < nb_codes; k++) {
216
                n = codes[k].bits - table_nb_bits;
217
                if (n <= 0)
218
                    break;
219
                code = codes[k].code;
220
                if (code >> (32 - table_nb_bits) != code_prefix)
221
                    break;
222
                codes[k].bits = n;
223
                codes[k].code = code << table_nb_bits;
224
                subtable_bits = FFMAX(subtable_bits, n);
225
            }
226
            subtable_bits = FFMIN(subtable_bits, table_nb_bits);
227
            j = (flags & INIT_VLC_LE) ? bitswap_32(code_prefix) >> (32 - table_nb_bits) : code_prefix;
228
            table[j][1] = -subtable_bits;
229 229
#ifdef DEBUG_VLC
230
                    av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n",
231
                           j, codes[i].bits + table_nb_bits);
230
            av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n",
231
                   j, codes[i].bits + table_nb_bits);
232 232
#endif
233
                    index = build_table(vlc, subtable_bits, k-i, codes+i, flags);
234
                    if (index < 0)
235
                        return -1;
236
                    /* note: realloc has been done, so reload tables */
237
                    table = &vlc->table[table_index];
238
                    table[j][0] = index; //code
239
                    i = k-1;
240
                }
233
            index = build_table(vlc, subtable_bits, k-i, codes+i, flags);
234
            if (index < 0)
235
                return -1;
236
            /* note: realloc has been done, so reload tables */
237
            table = &vlc->table[table_index];
238
            table[j][0] = index; //code
239
            i = k-1;
240
        }
241 241
    }
242 242
    return table_index;
243 243
}

Also available in: Unified diff