| 6 |
| * This file is part of FFmpeg. |
| 7 | * |
| 8 | * FFmpeg is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2.1 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * FFmpeg is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with FFmpeg; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 |
| 23 | /** |
| 24 | * @file |
| 25 | * @brief |
| 26 | * exp golomb vlc stuff |
| 27 | * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi |
| 28 | */ |
| 29 |
| 30 | #ifndef AVCODEC_GOLOMB_H |
| 31 | #define AVCODEC_GOLOMB_H |
| 32 |
| 33 | #include <stdint.h> |
| 34 |
| 35 | #include "get_bits.h" |
| 36 |
| 37 | #define INVALID_VLC 0x80000000 |
| 38 |
| 39 | extern const uint8_t ff_golomb_vlc_len[512]; |
| 40 | extern const uint8_t ff_ue_golomb_vlc_code[512]; |
| 41 | extern const int8_t ff_se_golomb_vlc_code[512]; |
| 42 | extern const uint8_t ff_ue_golomb_len[256]; |
| 43 |
| 44 | extern const uint8_t ff_interleaved_golomb_vlc_len[256]; |
| 45 | extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256]; |
| 46 | extern const int8_t ff_interleaved_se_golomb_vlc_code[256]; |
| 47 | extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256]; |
| 48 |
| 49 | /** |
| 50 | * Read an unsigned Exp-Golomb code in the range 0 to 8190. |
| 51 | * |
| 52 | * @returns the read value or a negative error code. |
| 53 | */ |
| 54 | static inline int get_ue_golomb(GetBitContext *gb) |
| 55 | { |
| 56 | unsigned int buf; |
| 57 |
| 58 | #if CACHED_BITSTREAM_READER |
| 59 | buf = show_bits_long(gb, 32); |
| 60 |
| 61 | if (buf >= (1 << 27)) { |
| 62 | buf >>= 32 - 9; |
| 63 | skip_bits_long(gb, ff_golomb_vlc_len[buf]); |
| 64 |
| 65 | return ff_ue_golomb_vlc_code[buf]; |
| 66 | } else { |
| 67 | int log = 2 * av_log2(buf) - 31; |
| 68 | buf >>= log; |
| 69 | buf--; |
| 70 | skip_bits_long(gb, 32 - log); |
| 71 |
| 72 | return buf; |
| 73 | } |
| 74 | #else |
| 75 | OPEN_READER(re, gb); |
| 76 | UPDATE_CACHE(re, gb); |
| 77 | buf = GET_CACHE(re, gb); |
| 78 |
| 79 | if (buf >= (1 << 27)) { |
| 80 | buf >>= 32 - 9; |
| 81 | LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); |
| 82 | CLOSE_READER(re, gb); |
| 83 |
| 84 | return ff_ue_golomb_vlc_code[buf]; |
| 85 | } else { |
| 86 | int log = 2 * av_log2(buf) - 31; |
| 87 | LAST_SKIP_BITS(re, gb, 32 - log); |
| 88 | CLOSE_READER(re, gb); |
| 89 | if (log < 7) { |
| 90 | av_log(NULL, AV_LOG_ERROR, "Invalid UE golomb code\n"); |
| 91 | return AVERROR_INVALIDDATA; |
| 92 | } |
| 93 | buf >>= log; |
| 94 | buf--; |
| 95 |
| 96 | return buf; |
| 97 | } |
| 98 | #endif |
| 99 | } |
| 100 |
| 101 | /** |
| 102 | * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1. |
| 103 | */ |
| 104 | static inline unsigned get_ue_golomb_long(GetBitContext *gb) |
| 105 | { |
| 106 | unsigned buf, log; |
| 107 |
| 108 | buf = show_bits_long(gb, 32); |
| 109 | log = 31 - av_log2(buf); |
| 110 | skip_bits_long(gb, log); |
| 111 |
| 112 | return get_bits_long(gb, log + 1) - 1; |
| 113 | } |
| 114 |
| 115 | /** |
| 116 | * read unsigned exp golomb code, constraint to a max of 31. |
| 117 | * the return value is undefined if the stored value exceeds 31. |
| 118 | */ |
| 119 | static inline int get_ue_golomb_31(GetBitContext *gb) |
| 120 | { |
| 121 | unsigned int buf; |
| 122 |
| 123 | #if CACHED_BITSTREAM_READER |
| 124 | buf = show_bits_long(gb, 32); |
| 125 |
| 126 | buf >>= 32 - 9; |
| 127 | skip_bits_long(gb, ff_golomb_vlc_len[buf]); |
| 128 | #else |
| 129 |
| 130 | OPEN_READER(re, gb); |
| 131 | UPDATE_CACHE(re, gb); |
| 132 | buf = GET_CACHE(re, gb); |
| 133 |
| 134 | buf >>= 32 - 9; |
| 135 | LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); |
| 136 | CLOSE_READER(re, gb); |
| 137 | #endif |
| 138 |
| 139 | return ff_ue_golomb_vlc_code[buf]; |
| 140 | } |
| 141 |
| 142 | static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb) |
| 143 | { |
| 144 | uint32_t buf; |
| 145 |
| 146 | #if CACHED_BITSTREAM_READER |
| 147 | buf = show_bits_long(gb, 32); |
| 148 |
| 149 | if (buf & 0xAA800000) { |
| 150 | buf >>= 32 - 8; |
| 151 | skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]); |
| 152 |
| 153 | return ff_interleaved_ue_golomb_vlc_code[buf]; |
| 154 | } else { |
| 155 | unsigned ret = 1; |
| 156 |
| 157 | do { |
| 158 | buf >>= 32 - 8; |
| 159 | skip_bits_long(gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8)); |
| 160 |
| 161 | if (ff_interleaved_golomb_vlc_len[buf] != 9) { |
| 162 | ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1; |
| 163 | ret |= ff_interleaved_dirac_golomb_vlc_code[buf]; |
| 164 | break; |
| 165 | } |
| 166 | ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf]; |
| 167 | buf = show_bits_long(gb, 32); |
| 168 | } while (get_bits_left(gb) > 0); |
| 169 |
| 170 | return ret - 1; |
| 171 | } |
| 172 | #else |
| 173 | OPEN_READER(re, gb); |
| 174 | UPDATE_CACHE(re, gb); |
| 175 | buf = GET_CACHE(re, gb); |
| 176 |
| 177 | if (buf & 0xAA800000) { |
| 178 | buf >>= 32 - 8; |
| 179 | LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]); |
| 180 | CLOSE_READER(re, gb); |
| 181 |
| 182 | return ff_interleaved_ue_golomb_vlc_code[buf]; |
| 183 | } else { |
| 184 | unsigned ret = 1; |
| 185 |
| 186 | do { |
| 187 | buf >>= 32 - 8; |
| 188 | LAST_SKIP_BITS(re, gb, |
| 189 | FFMIN(ff_interleaved_golomb_vlc_len[buf], 8)); |
| 190 |
| 191 | if (ff_interleaved_golomb_vlc_len[buf] != 9) { |
| 192 | ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1; |
| 193 | ret |= ff_interleaved_dirac_golomb_vlc_code[buf]; |
| 194 | break; |
| 195 | } |
| 196 | ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf]; |
| 197 | UPDATE_CACHE(re, gb); |
| 198 | buf = GET_CACHE(re, gb); |
| 199 | } while (ret<0x8000000U && BITS_AVAILABLE(re, gb)); |
| 200 |
| 201 | CLOSE_READER(re, gb); |
| 202 | return ret - 1; |
| 203 | } |
| 204 | #endif |
| 205 | } |
| 206 |
| 207 | /** |
| 208 | * read unsigned truncated exp golomb code. |
| 209 | */ |
| 210 | static inline int get_te0_golomb(GetBitContext *gb, int range) |
| 211 | { |
| 212 | av_assert2(range >= 1); |
| 213 |
| 214 | if (range == 1) |
| 215 | return 0; |
| 216 | else if (range == 2) |
| 217 | return get_bits1(gb) ^ 1; |
| 218 | else |
| 219 | return get_ue_golomb(gb); |
| 220 | } |
| 221 |
| 222 | /** |
| 223 | * read unsigned truncated exp golomb code. |
| 224 | */ |
| 225 | static inline int get_te_golomb(GetBitContext *gb, int range) |
| 226 | { |
| 227 | av_assert2(range >= 1); |
| 228 |
| 229 | if (range == 2) |
| 230 | return get_bits1(gb) ^ 1; |
| 231 | else |
| 232 | return get_ue_golomb(gb); |
| 233 | } |
| 234 |
| 235 | /** |
| 236 | * read signed exp golomb code. |
| 237 | */ |
| 238 | static inline int get_se_golomb(GetBitContext *gb) |
| 239 | { |
| 240 | unsigned int buf; |
| 241 |
| 242 | #if CACHED_BITSTREAM_READER |
| 243 | buf = show_bits_long(gb, 32); |
| 244 |
| 245 | if (buf >= (1 << 27)) { |
| 246 | buf >>= 32 - 9; |
| 247 | skip_bits_long(gb, ff_golomb_vlc_len[buf]); |
| 248 |
| 249 | return ff_se_golomb_vlc_code[buf]; |
| 250 | } else { |
| 251 | int log = 2 * av_log2(buf) - 31; |
| 252 | buf >>= log; |
| 253 |
| 254 | skip_bits_long(gb, 32 - log); |
| 255 |
| 256 | if (buf & 1) |
| 257 | buf = -(buf >> 1); |
| 258 | else |
| 259 | buf = (buf >> 1); |
| 260 |
| 261 | return buf; |
| 262 | } |
| 263 | #else |
| 264 | OPEN_READER(re, gb); |
| 265 | UPDATE_CACHE(re, gb); |
| 266 | buf = GET_CACHE(re, gb); |
| 267 |
| 268 | if (buf >= (1 << 27)) { |
| 269 | buf >>= 32 - 9; |
| 270 | LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); |
| 271 | CLOSE_READER(re, gb); |
| 272 |
| 273 | return ff_se_golomb_vlc_code[buf]; |
| 274 | } else { |
| 275 | int log = av_log2(buf), sign; |
| 276 | LAST_SKIP_BITS(re, gb, 31 - log); |
| 277 | UPDATE_CACHE(re, gb); |
| 278 | buf = GET_CACHE(re, gb); |
| 279 |
| 280 | buf >>= log; |
| 281 |
| 282 | LAST_SKIP_BITS(re, gb, 32 - log); |
| 283 | CLOSE_READER(re, gb); |
| 284 |
| 285 | sign = -(buf & 1); |
| 286 | buf = ((buf >> 1) ^ sign) - sign; |
| 287 |
| 288 | return buf; |
| 289 | } |
| 290 | #endif |
| 291 | } |
| 292 |
| 293 | static inline int get_se_golomb_long(GetBitContext *gb) |
| 294 | { |
| 295 | unsigned int buf = get_ue_golomb_long(gb); |
| 296 | int sign = (buf & 1) - 1; |
| 297 | return ((buf >> 1) ^ sign) + 1; |
| 298 | } |
| 299 |
| 300 | static inline int get_interleaved_se_golomb(GetBitContext *gb) |
| 301 | { |
| 302 | unsigned int buf; |
| 303 |
| 304 | #if CACHED_BITSTREAM_READER |
| 305 | buf = show_bits_long(gb, 32); |
| 306 |
| 307 | if (buf & 0xAA800000) { |
| 308 | buf >>= 32 - 8; |
| 309 | skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]); |
| 310 |
| 311 | return ff_interleaved_se_golomb_vlc_code[buf]; |
| 312 | } else { |
| 313 | int log; |
| 314 | skip_bits(gb, 8); |
| 315 | buf |= 1 | show_bits(gb, 24); |
| 316 |
| 317 | if ((buf & 0xAAAAAAAA) == 0) |
| 318 | return INVALID_VLC; |
| 319 |
| 320 | for (log = 31; (buf & 0x80000000) == 0; log--) |
| 321 | buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30); |
| 322 |
| 323 | skip_bits_long(gb, 63 - 2 * log - 8); |
| 324 |
| 325 | return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1; |
| 326 | } |
| 327 | #else |
| 328 | OPEN_READER(re, gb); |
| 329 | UPDATE_CACHE(re, gb); |
| 330 | buf = GET_CACHE(re, gb); |
| 331 |
| 332 | if (buf & 0xAA800000) { |
| 333 | buf >>= 32 - 8; |
| 334 | LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]); |
| 335 | CLOSE_READER(re, gb); |
| 336 |
| 337 | return ff_interleaved_se_golomb_vlc_code[buf]; |
| 338 | } else { |
| 339 | int log; |
| 340 | LAST_SKIP_BITS(re, gb, 8); |
| 341 | UPDATE_CACHE(re, gb); |
| 342 | buf |= 1 | (GET_CACHE(re, gb) >> 8); |
| 343 |
| 344 | if ((buf & 0xAAAAAAAA) == 0) |
| 345 | return INVALID_VLC; |
| 346 |
| 347 | for (log = 31; (buf & 0x80000000) == 0; log--) |
| 348 | buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30); |
| 349 |
| 350 | LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8); |
| 351 | CLOSE_READER(re, gb); |
| 352 |
| 353 | return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1; |
| 354 | } |
| 355 | #endif |
| 356 | } |
| 357 |
| 358 | static inline int dirac_get_se_golomb(GetBitContext *gb) |
| 359 | { |
| 360 | uint32_t ret = get_interleaved_ue_golomb(gb); |
| 361 |
| 362 | if (ret) { |
| 363 | int sign = -get_bits1(gb); |
| 364 | ret = (ret ^ sign) - sign; |
| 365 | } |
| 366 |
| 367 | return ret; |
| 368 | } |
| 369 |
| 370 | /** |
| 371 | * read unsigned golomb rice code (ffv1). |
| 372 | */ |
| 373 | static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, |
| 374 | int esc_len) |
| 375 | { |
| 376 | unsigned int buf; |
| 377 | int log; |
| 378 |
| 379 | #if CACHED_BITSTREAM_READER |
| 380 | buf = show_bits_long(gb, 32); |
| 381 |
| 382 | log = av_log2(buf); |
| 383 |
| 384 | if (log > 31 - limit) { |
| 385 | buf >>= log - k; |
| 386 | buf += (30 - log) << k; |
| 387 | skip_bits_long(gb, 32 + k - log); |
| 388 |
| 389 | return buf; |
| 390 | } else { |
| 391 | skip_bits_long(gb, limit); |
| 392 | buf = get_bits_long(gb, esc_len); |
| 393 |
| 394 | return buf + limit - 1; |
| 395 | } |
| 396 | #else |
| 397 | OPEN_READER(re, gb); |
| 398 | UPDATE_CACHE(re, gb); |
| 399 | buf = GET_CACHE(re, gb); |
| 400 |
| 401 | log = av_log2(buf); |
| 402 |
| 403 | if (log > 31 - limit) { |
| 404 | buf >>= log - k; |
| 405 | buf += (30U - log) << k; |
| 406 | LAST_SKIP_BITS(re, gb, 32 + k - log); |
| 407 | CLOSE_READER(re, gb); |
| 408 |
| 409 | return buf; |
| 410 | } else { |
| 411 | LAST_SKIP_BITS(re, gb, limit); |
| 412 | UPDATE_CACHE(re, gb); |
| 413 |
| 414 | buf = SHOW_UBITS(re, gb, esc_len); |
| 415 |
| 416 | LAST_SKIP_BITS(re, gb, esc_len); |
| 417 | CLOSE_READER(re, gb); |
| 418 |
| 419 | return buf + limit - 1; |
| 420 | } |
| 421 | #endif |
| 422 | } |
| 423 |
| 424 | #ifdef TRACE |
| 425 |
| 426 | static inline int get_ue(GetBitContext *s, const char *file, const char *func, |
| 427 | int line) |
| 428 | { |
| 429 | int show = show_bits(s, 24); |
| 430 | int pos = get_bits_count(s); |
| 431 | int i = get_ue_golomb(s); |
| 432 | int len = get_bits_count(s) - pos; |
| 433 | int bits = show >> (24 - len); |
| 434 |
| 435 | av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", |
| 436 | bits, len, i, pos, file, func, line); |
| 437 |
| 438 | return i; |
| 439 | } |
| 440 |
| 441 | static inline int get_se(GetBitContext *s, const char *file, const char *func, |
| 442 | int line) |
| 443 | { |
| 444 | int show = show_bits(s, 24); |
| 445 | int pos = get_bits_count(s); |
| 446 | int i = get_se_golomb(s); |
| 447 | int len = get_bits_count(s) - pos; |
| 448 | int bits = show >> (24 - len); |
| 449 |
| 450 | av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", |
| 451 | bits, len, i, pos, file, func, line); |
| 452 |
| 453 | return i; |
| 454 | } |
| 455 |
| 456 | static inline int get_te(GetBitContext *s, int r, char *file, const char *func, |
| 457 | int line) |
| 458 | { |
| 459 | int show = show_bits(s, 24); |
| 460 | int pos = get_bits_count(s); |
| 461 | int i = get_te0_golomb(s, r); |
| 462 | int len = get_bits_count(s) - pos; |
| 463 | int bits = show >> (24 - len); |
| 464 |
| 465 | av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", |
| 466 | bits, len, i, pos, file, func, line); |
| 467 |
| 468 | return i; |
| 469 | } |
| 470 |
| 471 | #define get_ue_golomb(a) get_ue(a, __FILE__, __func__, __LINE__) |
| 472 | #define get_se_golomb(a) get_se(a, __FILE__, __func__, __LINE__) |
| 473 | #define get_te_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__) |
| 474 | #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__) |
| 475 |
| 476 | #endif /* TRACE */ |
| 477 |
| 478 | #endif /* AVCODEC_GOLOMB_H */ |
| 479 |