yading@10: /* yading@10: * Common bit i/o utils yading@10: * Copyright (c) 2000, 2001 Fabrice Bellard yading@10: * Copyright (c) 2002-2004 Michael Niedermayer yading@10: * Copyright (c) 2010 Loren Merritt yading@10: * yading@10: * alternative bitstream reader & writer by Michael Niedermayer yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * bitstream api. yading@10: */ yading@10: yading@10: #include "libavutil/avassert.h" yading@10: #include "avcodec.h" yading@10: #include "mathops.h" yading@10: #include "get_bits.h" yading@10: #include "put_bits.h" yading@10: yading@10: const uint8_t ff_log2_run[41]={ yading@10: 0, 0, 0, 0, 1, 1, 1, 1, yading@10: 2, 2, 2, 2, 3, 3, 3, 3, yading@10: 4, 4, 5, 5, 6, 6, 7, 7, yading@10: 8, 9,10,11,12,13,14,15, yading@10: 16,17,18,19,20,21,22,23, yading@10: 24, yading@10: }; yading@10: yading@10: void avpriv_align_put_bits(PutBitContext *s) yading@10: { yading@10: put_bits(s,s->bit_left & 7,0); yading@10: } yading@10: yading@10: void avpriv_put_string(PutBitContext *pb, const char *string, int terminate_string) yading@10: { yading@10: while(*string){ yading@10: put_bits(pb, 8, *string); yading@10: string++; yading@10: } yading@10: if(terminate_string) yading@10: put_bits(pb, 8, 0); yading@10: } yading@10: yading@10: void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length) yading@10: { yading@10: int words= length>>4; yading@10: int bits= length&15; yading@10: int i; yading@10: yading@10: if(length==0) return; yading@10: yading@10: if(CONFIG_SMALL || words < 16 || put_bits_count(pb)&7){ yading@10: for(i=0; i>(16-bits)); yading@10: } yading@10: yading@10: /* VLC decoding */ yading@10: yading@10: #define GET_DATA(v, table, i, wrap, size) \ yading@10: {\ yading@10: const uint8_t *ptr = (const uint8_t *)table + i * wrap;\ yading@10: switch(size) {\ yading@10: case 1:\ yading@10: v = *(const uint8_t *)ptr;\ yading@10: break;\ yading@10: case 2:\ yading@10: v = *(const uint16_t *)ptr;\ yading@10: break;\ yading@10: default:\ yading@10: v = *(const uint32_t *)ptr;\ yading@10: break;\ yading@10: }\ yading@10: } yading@10: yading@10: yading@10: static int alloc_table(VLC *vlc, int size, int use_static) yading@10: { yading@10: int index; yading@10: index = vlc->table_size; yading@10: vlc->table_size += size; yading@10: if (vlc->table_size > vlc->table_allocated) { yading@10: if(use_static) yading@10: abort(); // cannot do anything, init_vlc() is used with too little memory yading@10: vlc->table_allocated += (1 << vlc->bits); yading@10: vlc->table = av_realloc_f(vlc->table, yading@10: vlc->table_allocated, sizeof(VLC_TYPE) * 2); yading@10: if (!vlc->table) yading@10: return -1; yading@10: } yading@10: return index; yading@10: } yading@10: yading@10: static av_always_inline uint32_t bitswap_32(uint32_t x) { yading@10: return (uint32_t)ff_reverse[x&0xFF]<<24 yading@10: | (uint32_t)ff_reverse[(x>>8)&0xFF]<<16 yading@10: | (uint32_t)ff_reverse[(x>>16)&0xFF]<<8 yading@10: | (uint32_t)ff_reverse[x>>24]; yading@10: } yading@10: yading@10: typedef struct { yading@10: uint8_t bits; yading@10: uint16_t symbol; yading@10: /** codeword, with the first bit-to-be-read in the msb yading@10: * (even if intended for a little-endian bitstream reader) */ yading@10: uint32_t code; yading@10: } VLCcode; yading@10: yading@10: static int compare_vlcspec(const void *a, const void *b) yading@10: { yading@10: const VLCcode *sa=a, *sb=b; yading@10: return (sa->code >> 1) - (sb->code >> 1); yading@10: } yading@10: yading@10: /** yading@10: * Build VLC decoding tables suitable for use with get_vlc(). yading@10: * yading@10: * @param vlc the context to be initted yading@10: * yading@10: * @param table_nb_bits max length of vlc codes to store directly in this table yading@10: * (Longer codes are delegated to subtables.) yading@10: * yading@10: * @param nb_codes number of elements in codes[] yading@10: * yading@10: * @param codes descriptions of the vlc codes yading@10: * These must be ordered such that codes going into the same subtable are contiguous. yading@10: * Sorting by VLCcode.code is sufficient, though not necessary. yading@10: */ yading@10: static int build_table(VLC *vlc, int table_nb_bits, int nb_codes, yading@10: VLCcode *codes, int flags) yading@10: { yading@10: int table_size, table_index, index, code_prefix, symbol, subtable_bits; yading@10: int i, j, k, n, nb, inc; yading@10: uint32_t code; yading@10: VLC_TYPE (*table)[2]; yading@10: yading@10: table_size = 1 << table_nb_bits; yading@10: if (table_nb_bits > 30) yading@10: return -1; yading@10: table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_NEW_STATIC); yading@10: av_dlog(NULL, "new table index=%d size=%d\n", table_index, table_size); yading@10: if (table_index < 0) yading@10: return -1; yading@10: table = &vlc->table[table_index]; yading@10: yading@10: for (i = 0; i < table_size; i++) { yading@10: table[i][1] = 0; //bits yading@10: table[i][0] = -1; //codes yading@10: } yading@10: yading@10: /* first pass: map codes and compute auxiliary table sizes */ yading@10: for (i = 0; i < nb_codes; i++) { yading@10: n = codes[i].bits; yading@10: code = codes[i].code; yading@10: symbol = codes[i].symbol; yading@10: av_dlog(NULL, "i=%d n=%d code=0x%x\n", i, n, code); yading@10: if (n <= table_nb_bits) { yading@10: /* no need to add another table */ yading@10: j = code >> (32 - table_nb_bits); yading@10: nb = 1 << (table_nb_bits - n); yading@10: inc = 1; yading@10: if (flags & INIT_VLC_LE) { yading@10: j = bitswap_32(code); yading@10: inc = 1 << n; yading@10: } yading@10: for (k = 0; k < nb; k++) { yading@10: av_dlog(NULL, "%4x: code=%d n=%d\n", j, i, n); yading@10: if (table[j][1] /*bits*/ != 0) { yading@10: av_log(NULL, AV_LOG_ERROR, "incorrect codes\n"); yading@10: return -1; yading@10: } yading@10: table[j][1] = n; //bits yading@10: table[j][0] = symbol; yading@10: j += inc; yading@10: } yading@10: } else { yading@10: /* fill auxiliary table recursively */ yading@10: n -= table_nb_bits; yading@10: code_prefix = code >> (32 - table_nb_bits); yading@10: subtable_bits = n; yading@10: codes[i].bits = n; yading@10: codes[i].code = code << table_nb_bits; yading@10: for (k = i+1; k < nb_codes; k++) { yading@10: n = codes[k].bits - table_nb_bits; yading@10: if (n <= 0) yading@10: break; yading@10: code = codes[k].code; yading@10: if (code >> (32 - table_nb_bits) != code_prefix) yading@10: break; yading@10: codes[k].bits = n; yading@10: codes[k].code = code << table_nb_bits; yading@10: subtable_bits = FFMAX(subtable_bits, n); yading@10: } yading@10: subtable_bits = FFMIN(subtable_bits, table_nb_bits); yading@10: j = (flags & INIT_VLC_LE) ? bitswap_32(code_prefix) >> (32 - table_nb_bits) : code_prefix; yading@10: table[j][1] = -subtable_bits; yading@10: av_dlog(NULL, "%4x: n=%d (subtable)\n", yading@10: j, codes[i].bits + table_nb_bits); yading@10: index = build_table(vlc, subtable_bits, k-i, codes+i, flags); yading@10: if (index < 0) yading@10: return -1; yading@10: /* note: realloc has been done, so reload tables */ yading@10: table = &vlc->table[table_index]; yading@10: table[j][0] = index; //code yading@10: i = k-1; yading@10: } yading@10: } yading@10: return table_index; yading@10: } yading@10: yading@10: yading@10: /* Build VLC decoding tables suitable for use with get_vlc(). yading@10: yading@10: 'nb_bits' set thee decoding table size (2^nb_bits) entries. The yading@10: bigger it is, the faster is the decoding. But it should not be too yading@10: big to save memory and L1 cache. '9' is a good compromise. yading@10: yading@10: 'nb_codes' : number of vlcs codes yading@10: yading@10: 'bits' : table which gives the size (in bits) of each vlc code. yading@10: yading@10: 'codes' : table which gives the bit pattern of of each vlc code. yading@10: yading@10: 'symbols' : table which gives the values to be returned from get_vlc(). yading@10: yading@10: 'xxx_wrap' : give the number of bytes between each entry of the yading@10: 'bits' or 'codes' tables. yading@10: yading@10: 'xxx_size' : gives the number of bytes of each entry of the 'bits' yading@10: or 'codes' tables. yading@10: yading@10: 'wrap' and 'size' allows to use any memory configuration and types yading@10: (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables. yading@10: yading@10: 'use_static' should be set to 1 for tables, which should be freed yading@10: with av_free_static(), 0 if ff_free_vlc() will be used. yading@10: */ yading@10: int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, yading@10: const void *bits, int bits_wrap, int bits_size, yading@10: const void *codes, int codes_wrap, int codes_size, yading@10: const void *symbols, int symbols_wrap, int symbols_size, yading@10: int flags) yading@10: { yading@10: VLCcode *buf; yading@10: int i, j, ret; yading@10: yading@10: vlc->bits = nb_bits; yading@10: if(flags & INIT_VLC_USE_NEW_STATIC){ yading@10: VLC dyn_vlc = *vlc; yading@10: yading@10: if (vlc->table_size) yading@10: return 0; yading@10: yading@10: ret = ff_init_vlc_sparse(&dyn_vlc, nb_bits, nb_codes, yading@10: bits, bits_wrap, bits_size, yading@10: codes, codes_wrap, codes_size, yading@10: symbols, symbols_wrap, symbols_size, yading@10: flags & ~INIT_VLC_USE_NEW_STATIC); yading@10: av_assert0(ret >= 0); yading@10: av_assert0(dyn_vlc.table_size <= vlc->table_allocated); yading@10: if(dyn_vlc.table_size < vlc->table_allocated) yading@10: av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", dyn_vlc.table_size, vlc->table_allocated); yading@10: memcpy(vlc->table, dyn_vlc.table, dyn_vlc.table_size * sizeof(*vlc->table)); yading@10: vlc->table_size = dyn_vlc.table_size; yading@10: ff_free_vlc(&dyn_vlc); yading@10: return 0; yading@10: }else { yading@10: vlc->table = NULL; yading@10: vlc->table_allocated = 0; yading@10: vlc->table_size = 0; yading@10: } yading@10: yading@10: av_dlog(NULL, "build table nb_codes=%d\n", nb_codes); yading@10: yading@10: buf = av_malloc((nb_codes+1)*sizeof(VLCcode)); yading@10: yading@10: av_assert0(symbols_size <= 2 || !symbols); yading@10: j = 0; yading@10: #define COPY(condition)\ yading@10: for (i = 0; i < nb_codes; i++) {\ yading@10: GET_DATA(buf[j].bits, bits, i, bits_wrap, bits_size);\ yading@10: if (!(condition))\ yading@10: continue;\ yading@10: if (buf[j].bits > 3*nb_bits || buf[j].bits>32) {\ yading@10: av_log(NULL, AV_LOG_ERROR, "Too long VLC in init_vlc\n");\ yading@10: return -1;\ yading@10: }\ yading@10: GET_DATA(buf[j].code, codes, i, codes_wrap, codes_size);\ yading@10: if (buf[j].code >= (1LL< nb_bits); yading@10: // qsort is the slowest part of init_vlc, and could probably be improved or avoided yading@10: qsort(buf, j, sizeof(VLCcode), compare_vlcspec); yading@10: COPY(buf[j].bits && buf[j].bits <= nb_bits); yading@10: nb_codes = j; yading@10: yading@10: ret = build_table(vlc, nb_bits, nb_codes, buf, flags); yading@10: yading@10: av_free(buf); yading@10: if (ret < 0) { yading@10: av_freep(&vlc->table); yading@10: return -1; yading@10: } yading@10: return 0; yading@10: } yading@10: yading@10: yading@10: void ff_free_vlc(VLC *vlc) yading@10: { yading@10: av_freep(&vlc->table); yading@10: }