*
* alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
*
- * This file is part of FFmpeg.
+ * This file is part of Libav.
*
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
- * @file libavcodec/bitstream.c
+ * @file
* bitstream api.
*/
return -1;
table = &vlc->table[table_index];
- for(i=0;i<table_size;i++) {
+ for (i = 0; i < table_size; i++) {
table[i][1] = 0; //bits
table[i][0] = -1; //codes
}
/* first pass: map codes and compute auxillary table sizes */
- for(i=0;i<nb_codes;i++) {
+ for (i = 0; i < nb_codes; i++) {
n = codes[i].bits;
code = codes[i].code;
symbol = codes[i].symbol;
#if defined(DEBUG_VLC) && 0
av_log(NULL,AV_LOG_DEBUG,"i=%d n=%d code=0x%x\n", i, n, code);
#endif
- if (n <= table_nb_bits) {
- /* no need to add another table */
- j = code >> (32 - table_nb_bits);
- nb = 1 << (table_nb_bits - n);
- inc = 1;
- if (flags & INIT_VLC_LE) {
- j = bitswap_32(code);
- inc = 1 << n;
- }
- for(k=0;k<nb;k++) {
+ if (n <= table_nb_bits) {
+ /* no need to add another table */
+ j = code >> (32 - table_nb_bits);
+ nb = 1 << (table_nb_bits - n);
+ inc = 1;
+ if (flags & INIT_VLC_LE) {
+ j = bitswap_32(code);
+ inc = 1 << n;
+ }
+ for (k = 0; k < nb; k++) {
#ifdef DEBUG_VLC
- av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
- j, i, n);
+ av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
+ j, i, n);
#endif
- if (table[j][1] /*bits*/ != 0) {
- av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
- return -1;
- }
- table[j][1] = n; //bits
- table[j][0] = symbol;
- j += inc;
- }
- } else {
- /* fill auxiliary table recursively */
- n -= table_nb_bits;
- code_prefix = code >> (32 - table_nb_bits);
- subtable_bits = n;
- codes[i].bits = n;
- codes[i].code = code << table_nb_bits;
- for (k = i+1; k < nb_codes; k++) {
- n = codes[k].bits - table_nb_bits;
- if (n <= 0)
- break;
- code = codes[k].code;
- if (code >> (32 - table_nb_bits) != code_prefix)
- break;
- codes[k].bits = n;
- codes[k].code = code << table_nb_bits;
- subtable_bits = FFMAX(subtable_bits, n);
- }
- subtable_bits = FFMIN(subtable_bits, table_nb_bits);
- j = (flags & INIT_VLC_LE) ? bitswap_32(code_prefix) >> (32 - table_nb_bits) : code_prefix;
- table[j][1] = -subtable_bits;
+ if (table[j][1] /*bits*/ != 0) {
+ av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
+ return -1;
+ }
+ table[j][1] = n; //bits
+ table[j][0] = symbol;
+ j += inc;
+ }
+ } else {
+ /* fill auxiliary table recursively */
+ n -= table_nb_bits;
+ code_prefix = code >> (32 - table_nb_bits);
+ subtable_bits = n;
+ codes[i].bits = n;
+ codes[i].code = code << table_nb_bits;
+ for (k = i+1; k < nb_codes; k++) {
+ n = codes[k].bits - table_nb_bits;
+ if (n <= 0)
+ break;
+ code = codes[k].code;
+ if (code >> (32 - table_nb_bits) != code_prefix)
+ break;
+ codes[k].bits = n;
+ codes[k].code = code << table_nb_bits;
+ subtable_bits = FFMAX(subtable_bits, n);
+ }
+ subtable_bits = FFMIN(subtable_bits, table_nb_bits);
+ j = (flags & INIT_VLC_LE) ? bitswap_32(code_prefix) >> (32 - table_nb_bits) : code_prefix;
+ table[j][1] = -subtable_bits;
#ifdef DEBUG_VLC
- av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n",
- j, codes[i].bits + table_nb_bits);
+ av_log(NULL,AV_LOG_DEBUG,"%4x: n=%d (subtable)\n",
+ j, codes[i].bits + table_nb_bits);
#endif
- index = build_table(vlc, subtable_bits, k-i, codes+i, flags);
- if (index < 0)
- return -1;
- /* note: realloc has been done, so reload tables */
- table = &vlc->table[table_index];
- table[j][0] = index; //code
- i = k-1;
- }
+ index = build_table(vlc, subtable_bits, k-i, codes+i, flags);
+ if (index < 0)
+ return -1;
+ /* note: realloc has been done, so reload tables */
+ table = &vlc->table[table_index];
+ table[j][0] = index; //code
+ i = k-1;
+ }
}
return table_index;
}
const void *symbols, int symbols_wrap, int symbols_size,
int flags)
{
- VLCcode buf[nb_codes];
- int i, j;
+ VLCcode *buf;
+ int i, j, ret;
vlc->bits = nb_bits;
if(flags & INIT_VLC_USE_NEW_STATIC){
av_log(NULL,AV_LOG_DEBUG,"build table nb_codes=%d\n", nb_codes);
#endif
+ buf = av_malloc((nb_codes+1)*sizeof(VLCcode));
+
assert(symbols_size <= 2 || !symbols);
j = 0;
#define COPY(condition)\
COPY(buf[j].bits && buf[j].bits <= nb_bits);
nb_codes = j;
- if (build_table(vlc, nb_bits, nb_codes, buf, flags) < 0) {
+ ret = build_table(vlc, nb_bits, nb_codes, buf, flags);
+
+ av_free(buf);
+ if (ret < 0) {
av_freep(&vlc->table);
return -1;
}