annotate ffmpeg/libavcodec/bitstream.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Common bit i/o utils
yading@10 3 * Copyright (c) 2000, 2001 Fabrice Bellard
yading@10 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
yading@10 5 * Copyright (c) 2010 Loren Merritt
yading@10 6 *
yading@10 7 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
yading@10 8 *
yading@10 9 * This file is part of FFmpeg.
yading@10 10 *
yading@10 11 * FFmpeg is free software; you can redistribute it and/or
yading@10 12 * modify it under the terms of the GNU Lesser General Public
yading@10 13 * License as published by the Free Software Foundation; either
yading@10 14 * version 2.1 of the License, or (at your option) any later version.
yading@10 15 *
yading@10 16 * FFmpeg is distributed in the hope that it will be useful,
yading@10 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 19 * Lesser General Public License for more details.
yading@10 20 *
yading@10 21 * You should have received a copy of the GNU Lesser General Public
yading@10 22 * License along with FFmpeg; if not, write to the Free Software
yading@10 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 24 */
yading@10 25
yading@10 26 /**
yading@10 27 * @file
yading@10 28 * bitstream api.
yading@10 29 */
yading@10 30
yading@10 31 #include "libavutil/avassert.h"
yading@10 32 #include "avcodec.h"
yading@10 33 #include "mathops.h"
yading@10 34 #include "get_bits.h"
yading@10 35 #include "put_bits.h"
yading@10 36
yading@10 37 const uint8_t ff_log2_run[41]={
yading@10 38 0, 0, 0, 0, 1, 1, 1, 1,
yading@10 39 2, 2, 2, 2, 3, 3, 3, 3,
yading@10 40 4, 4, 5, 5, 6, 6, 7, 7,
yading@10 41 8, 9,10,11,12,13,14,15,
yading@10 42 16,17,18,19,20,21,22,23,
yading@10 43 24,
yading@10 44 };
yading@10 45
yading@10 46 void avpriv_align_put_bits(PutBitContext *s)
yading@10 47 {
yading@10 48 put_bits(s,s->bit_left & 7,0);
yading@10 49 }
yading@10 50
yading@10 51 void avpriv_put_string(PutBitContext *pb, const char *string, int terminate_string)
yading@10 52 {
yading@10 53 while(*string){
yading@10 54 put_bits(pb, 8, *string);
yading@10 55 string++;
yading@10 56 }
yading@10 57 if(terminate_string)
yading@10 58 put_bits(pb, 8, 0);
yading@10 59 }
yading@10 60
yading@10 61 void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
yading@10 62 {
yading@10 63 int words= length>>4;
yading@10 64 int bits= length&15;
yading@10 65 int i;
yading@10 66
yading@10 67 if(length==0) return;
yading@10 68
yading@10 69 if(CONFIG_SMALL || words < 16 || put_bits_count(pb)&7){
yading@10 70 for(i=0; i<words; i++) put_bits(pb, 16, AV_RB16(src + 2*i));
yading@10 71 }else{
yading@10 72 for(i=0; put_bits_count(pb)&31; i++)
yading@10 73 put_bits(pb, 8, src[i]);
yading@10 74 flush_put_bits(pb);
yading@10 75 memcpy(put_bits_ptr(pb), src+i, 2*words-i);
yading@10 76 skip_put_bytes(pb, 2*words-i);
yading@10 77 }
yading@10 78
yading@10 79 put_bits(pb, bits, AV_RB16(src + 2*words)>>(16-bits));
yading@10 80 }
yading@10 81
yading@10 82 /* VLC decoding */
yading@10 83
yading@10 84 #define GET_DATA(v, table, i, wrap, size) \
yading@10 85 {\
yading@10 86 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
yading@10 87 switch(size) {\
yading@10 88 case 1:\
yading@10 89 v = *(const uint8_t *)ptr;\
yading@10 90 break;\
yading@10 91 case 2:\
yading@10 92 v = *(const uint16_t *)ptr;\
yading@10 93 break;\
yading@10 94 default:\
yading@10 95 v = *(const uint32_t *)ptr;\
yading@10 96 break;\
yading@10 97 }\
yading@10 98 }
yading@10 99
yading@10 100
yading@10 101 static int alloc_table(VLC *vlc, int size, int use_static)
yading@10 102 {
yading@10 103 int index;
yading@10 104 index = vlc->table_size;
yading@10 105 vlc->table_size += size;
yading@10 106 if (vlc->table_size > vlc->table_allocated) {
yading@10 107 if(use_static)
yading@10 108 abort(); // cannot do anything, init_vlc() is used with too little memory
yading@10 109 vlc->table_allocated += (1 << vlc->bits);
yading@10 110 vlc->table = av_realloc_f(vlc->table,
yading@10 111 vlc->table_allocated, sizeof(VLC_TYPE) * 2);
yading@10 112 if (!vlc->table)
yading@10 113 return -1;
yading@10 114 }
yading@10 115 return index;
yading@10 116 }
yading@10 117
yading@10 118 static av_always_inline uint32_t bitswap_32(uint32_t x) {
yading@10 119 return (uint32_t)ff_reverse[x&0xFF]<<24
yading@10 120 | (uint32_t)ff_reverse[(x>>8)&0xFF]<<16
yading@10 121 | (uint32_t)ff_reverse[(x>>16)&0xFF]<<8
yading@10 122 | (uint32_t)ff_reverse[x>>24];
yading@10 123 }
yading@10 124
yading@10 125 typedef struct {
yading@10 126 uint8_t bits;
yading@10 127 uint16_t symbol;
yading@10 128 /** codeword, with the first bit-to-be-read in the msb
yading@10 129 * (even if intended for a little-endian bitstream reader) */
yading@10 130 uint32_t code;
yading@10 131 } VLCcode;
yading@10 132
yading@10 133 static int compare_vlcspec(const void *a, const void *b)
yading@10 134 {
yading@10 135 const VLCcode *sa=a, *sb=b;
yading@10 136 return (sa->code >> 1) - (sb->code >> 1);
yading@10 137 }
yading@10 138
yading@10 139 /**
yading@10 140 * Build VLC decoding tables suitable for use with get_vlc().
yading@10 141 *
yading@10 142 * @param vlc the context to be initted
yading@10 143 *
yading@10 144 * @param table_nb_bits max length of vlc codes to store directly in this table
yading@10 145 * (Longer codes are delegated to subtables.)
yading@10 146 *
yading@10 147 * @param nb_codes number of elements in codes[]
yading@10 148 *
yading@10 149 * @param codes descriptions of the vlc codes
yading@10 150 * These must be ordered such that codes going into the same subtable are contiguous.
yading@10 151 * Sorting by VLCcode.code is sufficient, though not necessary.
yading@10 152 */
yading@10 153 static int build_table(VLC *vlc, int table_nb_bits, int nb_codes,
yading@10 154 VLCcode *codes, int flags)
yading@10 155 {
yading@10 156 int table_size, table_index, index, code_prefix, symbol, subtable_bits;
yading@10 157 int i, j, k, n, nb, inc;
yading@10 158 uint32_t code;
yading@10 159 VLC_TYPE (*table)[2];
yading@10 160
yading@10 161 table_size = 1 << table_nb_bits;
yading@10 162 if (table_nb_bits > 30)
yading@10 163 return -1;
yading@10 164 table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_NEW_STATIC);
yading@10 165 av_dlog(NULL, "new table index=%d size=%d\n", table_index, table_size);
yading@10 166 if (table_index < 0)
yading@10 167 return -1;
yading@10 168 table = &vlc->table[table_index];
yading@10 169
yading@10 170 for (i = 0; i < table_size; i++) {
yading@10 171 table[i][1] = 0; //bits
yading@10 172 table[i][0] = -1; //codes
yading@10 173 }
yading@10 174
yading@10 175 /* first pass: map codes and compute auxiliary table sizes */
yading@10 176 for (i = 0; i < nb_codes; i++) {
yading@10 177 n = codes[i].bits;
yading@10 178 code = codes[i].code;
yading@10 179 symbol = codes[i].symbol;
yading@10 180 av_dlog(NULL, "i=%d n=%d code=0x%x\n", i, n, code);
yading@10 181 if (n <= table_nb_bits) {
yading@10 182 /* no need to add another table */
yading@10 183 j = code >> (32 - table_nb_bits);
yading@10 184 nb = 1 << (table_nb_bits - n);
yading@10 185 inc = 1;
yading@10 186 if (flags & INIT_VLC_LE) {
yading@10 187 j = bitswap_32(code);
yading@10 188 inc = 1 << n;
yading@10 189 }
yading@10 190 for (k = 0; k < nb; k++) {
yading@10 191 av_dlog(NULL, "%4x: code=%d n=%d\n", j, i, n);
yading@10 192 if (table[j][1] /*bits*/ != 0) {
yading@10 193 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
yading@10 194 return -1;
yading@10 195 }
yading@10 196 table[j][1] = n; //bits
yading@10 197 table[j][0] = symbol;
yading@10 198 j += inc;
yading@10 199 }
yading@10 200 } else {
yading@10 201 /* fill auxiliary table recursively */
yading@10 202 n -= table_nb_bits;
yading@10 203 code_prefix = code >> (32 - table_nb_bits);
yading@10 204 subtable_bits = n;
yading@10 205 codes[i].bits = n;
yading@10 206 codes[i].code = code << table_nb_bits;
yading@10 207 for (k = i+1; k < nb_codes; k++) {
yading@10 208 n = codes[k].bits - table_nb_bits;
yading@10 209 if (n <= 0)
yading@10 210 break;
yading@10 211 code = codes[k].code;
yading@10 212 if (code >> (32 - table_nb_bits) != code_prefix)
yading@10 213 break;
yading@10 214 codes[k].bits = n;
yading@10 215 codes[k].code = code << table_nb_bits;
yading@10 216 subtable_bits = FFMAX(subtable_bits, n);
yading@10 217 }
yading@10 218 subtable_bits = FFMIN(subtable_bits, table_nb_bits);
yading@10 219 j = (flags & INIT_VLC_LE) ? bitswap_32(code_prefix) >> (32 - table_nb_bits) : code_prefix;
yading@10 220 table[j][1] = -subtable_bits;
yading@10 221 av_dlog(NULL, "%4x: n=%d (subtable)\n",
yading@10 222 j, codes[i].bits + table_nb_bits);
yading@10 223 index = build_table(vlc, subtable_bits, k-i, codes+i, flags);
yading@10 224 if (index < 0)
yading@10 225 return -1;
yading@10 226 /* note: realloc has been done, so reload tables */
yading@10 227 table = &vlc->table[table_index];
yading@10 228 table[j][0] = index; //code
yading@10 229 i = k-1;
yading@10 230 }
yading@10 231 }
yading@10 232 return table_index;
yading@10 233 }
yading@10 234
yading@10 235
yading@10 236 /* Build VLC decoding tables suitable for use with get_vlc().
yading@10 237
yading@10 238 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
yading@10 239 bigger it is, the faster is the decoding. But it should not be too
yading@10 240 big to save memory and L1 cache. '9' is a good compromise.
yading@10 241
yading@10 242 'nb_codes' : number of vlcs codes
yading@10 243
yading@10 244 'bits' : table which gives the size (in bits) of each vlc code.
yading@10 245
yading@10 246 'codes' : table which gives the bit pattern of of each vlc code.
yading@10 247
yading@10 248 'symbols' : table which gives the values to be returned from get_vlc().
yading@10 249
yading@10 250 'xxx_wrap' : give the number of bytes between each entry of the
yading@10 251 'bits' or 'codes' tables.
yading@10 252
yading@10 253 'xxx_size' : gives the number of bytes of each entry of the 'bits'
yading@10 254 or 'codes' tables.
yading@10 255
yading@10 256 'wrap' and 'size' allows to use any memory configuration and types
yading@10 257 (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables.
yading@10 258
yading@10 259 'use_static' should be set to 1 for tables, which should be freed
yading@10 260 with av_free_static(), 0 if ff_free_vlc() will be used.
yading@10 261 */
yading@10 262 int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
yading@10 263 const void *bits, int bits_wrap, int bits_size,
yading@10 264 const void *codes, int codes_wrap, int codes_size,
yading@10 265 const void *symbols, int symbols_wrap, int symbols_size,
yading@10 266 int flags)
yading@10 267 {
yading@10 268 VLCcode *buf;
yading@10 269 int i, j, ret;
yading@10 270
yading@10 271 vlc->bits = nb_bits;
yading@10 272 if(flags & INIT_VLC_USE_NEW_STATIC){
yading@10 273 VLC dyn_vlc = *vlc;
yading@10 274
yading@10 275 if (vlc->table_size)
yading@10 276 return 0;
yading@10 277
yading@10 278 ret = ff_init_vlc_sparse(&dyn_vlc, nb_bits, nb_codes,
yading@10 279 bits, bits_wrap, bits_size,
yading@10 280 codes, codes_wrap, codes_size,
yading@10 281 symbols, symbols_wrap, symbols_size,
yading@10 282 flags & ~INIT_VLC_USE_NEW_STATIC);
yading@10 283 av_assert0(ret >= 0);
yading@10 284 av_assert0(dyn_vlc.table_size <= vlc->table_allocated);
yading@10 285 if(dyn_vlc.table_size < vlc->table_allocated)
yading@10 286 av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", dyn_vlc.table_size, vlc->table_allocated);
yading@10 287 memcpy(vlc->table, dyn_vlc.table, dyn_vlc.table_size * sizeof(*vlc->table));
yading@10 288 vlc->table_size = dyn_vlc.table_size;
yading@10 289 ff_free_vlc(&dyn_vlc);
yading@10 290 return 0;
yading@10 291 }else {
yading@10 292 vlc->table = NULL;
yading@10 293 vlc->table_allocated = 0;
yading@10 294 vlc->table_size = 0;
yading@10 295 }
yading@10 296
yading@10 297 av_dlog(NULL, "build table nb_codes=%d\n", nb_codes);
yading@10 298
yading@10 299 buf = av_malloc((nb_codes+1)*sizeof(VLCcode));
yading@10 300
yading@10 301 av_assert0(symbols_size <= 2 || !symbols);
yading@10 302 j = 0;
yading@10 303 #define COPY(condition)\
yading@10 304 for (i = 0; i < nb_codes; i++) {\
yading@10 305 GET_DATA(buf[j].bits, bits, i, bits_wrap, bits_size);\
yading@10 306 if (!(condition))\
yading@10 307 continue;\
yading@10 308 if (buf[j].bits > 3*nb_bits || buf[j].bits>32) {\
yading@10 309 av_log(NULL, AV_LOG_ERROR, "Too long VLC in init_vlc\n");\
yading@10 310 return -1;\
yading@10 311 }\
yading@10 312 GET_DATA(buf[j].code, codes, i, codes_wrap, codes_size);\
yading@10 313 if (buf[j].code >= (1LL<<buf[j].bits)) {\
yading@10 314 av_log(NULL, AV_LOG_ERROR, "Invalid code in init_vlc\n");\
yading@10 315 return -1;\
yading@10 316 }\
yading@10 317 if (flags & INIT_VLC_LE)\
yading@10 318 buf[j].code = bitswap_32(buf[j].code);\
yading@10 319 else\
yading@10 320 buf[j].code <<= 32 - buf[j].bits;\
yading@10 321 if (symbols)\
yading@10 322 GET_DATA(buf[j].symbol, symbols, i, symbols_wrap, symbols_size)\
yading@10 323 else\
yading@10 324 buf[j].symbol = i;\
yading@10 325 j++;\
yading@10 326 }
yading@10 327 COPY(buf[j].bits > nb_bits);
yading@10 328 // qsort is the slowest part of init_vlc, and could probably be improved or avoided
yading@10 329 qsort(buf, j, sizeof(VLCcode), compare_vlcspec);
yading@10 330 COPY(buf[j].bits && buf[j].bits <= nb_bits);
yading@10 331 nb_codes = j;
yading@10 332
yading@10 333 ret = build_table(vlc, nb_bits, nb_codes, buf, flags);
yading@10 334
yading@10 335 av_free(buf);
yading@10 336 if (ret < 0) {
yading@10 337 av_freep(&vlc->table);
yading@10 338 return -1;
yading@10 339 }
yading@10 340 return 0;
yading@10 341 }
yading@10 342
yading@10 343
yading@10 344 void ff_free_vlc(VLC *vlc)
yading@10 345 {
yading@10 346 av_freep(&vlc->table);
yading@10 347 }