Heavily based and inspired by Christophe's cache branches.
Showing the changes to libavcodec (5 of 5 files). Show all
Unified · Split
| @@ -102,6 +102,7 @@ · 101 unmodified lines | |||
| 102 | # define bits_decode210 bits_decode210_le | 102 | # define bits_decode210 bits_decode210_le |
| 103 | # define bits_apply_sign bits_apply_sign_le | 103 | # define bits_apply_sign bits_apply_sign_le |
| 104 | # define bits_read_vlc bits_read_vlc_le | 104 | # define bits_read_vlc bits_read_vlc_le |
| 105 | # define bits_read_vlc_multi bits_read_vlc_multi_le | ||
| 105 | 106 | ||
| 106 | #elif defined(BITS_DEFAULT_BE) | 107 | #elif defined(BITS_DEFAULT_BE) |
| 107 | 108 | ||
| @@ -130,6 +131,7 @@ · 22 unmodified lines | |||
| 130 | # define bits_decode210 bits_decode210_be | 131 | # define bits_decode210 bits_decode210_be |
| 131 | # define bits_apply_sign bits_apply_sign_be | 132 | # define bits_apply_sign bits_apply_sign_be |
| 132 | # define bits_read_vlc bits_read_vlc_be | 133 | # define bits_read_vlc bits_read_vlc_be |
| 134 | # define bits_read_vlc_multi bits_read_vlc_multi_be | ||
| 133 | 135 | ||
| 134 | #endif | 136 | #endif |
| 135 | 137 | ||
| @@ -520,6 +520,35 @@ static inline int BS_FUNC(read_vlc)(BSCTX *bc, const VLCElem *table, · 519 unmodified lines | |||
| 520 | return code; | 520 | return code; |
| 521 | } | 521 | } |
| 522 | 522 | ||
| 523 | static inline int BS_FUNC(read_vlc_multi)(BSCTX *bc, uint8_t *dst, | ||
| 524 | const VLC_MULTI_ELEM *const Jtable, | ||
| 525 | const VLCElem *const table, | ||
| 526 | const int bits, const int max_depth) | ||
| 527 | { | ||
| 528 | unsigned idx = BS_FUNC(peek)(bc, bits); | ||
| 529 | int ret, nb_bits, code, n = Jtable[idx].len; | ||
| 530 | if (Jtable[idx].num) { | ||
| 531 | AV_COPY64U(dst, Jtable[idx].val); | ||
| 532 | ret = Jtable[idx].num; | ||
| 533 | } else { | ||
| 534 | code = table[idx].sym; | ||
| 535 | n = table[idx].len; | ||
| 536 | if (max_depth > 1 && n < 0) { | ||
| 537 | BS_FUNC(priv_skip_remaining)(bc, bits); | ||
| 538 | code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); | ||
| 539 | if (max_depth > 2 && n < 0) { | ||
| 540 | BS_FUNC(priv_skip_remaining)(bc, nb_bits); | ||
| 541 | code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); | ||
| 542 | } | ||
| 543 | } | ||
| 544 | AV_WN16(dst, code); | ||
| 545 | ret = n > 0; | ||
| 546 | } | ||
| 547 | BS_FUNC(priv_skip_remaining)(bc, n); | ||
| 548 | |||
| 549 | return ret; | ||
| 550 | } | ||
| 551 | |||
| 523 | #undef BSCTX | 552 | #undef BSCTX |
| 524 | #undef BS_FUNC | 553 | #undef BS_FUNC |
| 525 | #undef BS_JOIN3 | 554 | #undef BS_JOIN3 |
| @@ -93,6 +93,7 @@ typedef BitstreamContext GetBitContext; · 92 unmodified lines | |||
| 93 | #define init_get_bits8 bits_init8 | 93 | #define init_get_bits8 bits_init8 |
| 94 | #define align_get_bits bits_align | 94 | #define align_get_bits bits_align |
| 95 | #define get_vlc2 bits_read_vlc | 95 | #define get_vlc2 bits_read_vlc |
| 96 | #define get_vlc_multi bits_read_vlc_multi | ||
| 96 | 97 | ||
| 97 | #define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size) | 98 | #define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size) |
| 98 | #define get_bits_le(s, n) bits_read_le((BitstreamContextLE*)s, n) | 99 | #define get_bits_le(s, n) bits_read_le((BitstreamContextLE*)s, n) |
| @@ -643,6 +644,15 @@ static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, · 544 unmodified lines | |||
| 643 | return code; | 644 | return code; |
| 644 | } | 645 | } |
| 645 | 646 | ||
| 647 | static inline int get_vlc_multi(GetBitContext *s, uint8_t *dst, | ||
| 648 | const VLC_MULTI_ELEM *const Jtable, | ||
| 649 | const VLCElem *const table, | ||
| 650 | const int bits, const int max_depth) | ||
| 651 | { | ||
| 652 | dst[0] = get_vlc2(s, table, bits, max_depth); | ||
| 653 | return 1; | ||
| 654 | } | ||
| 655 | |||
| 646 | static inline int decode012(GetBitContext *gb) | 656 | static inline int decode012(GetBitContext *gb) |
| 647 | { | 657 | { |
| 648 | int n; | 658 | int n; |
| @@ -30,6 +30,7 @@ · 29 unmodified lines | |||
| 30 | #include "libavutil/avassert.h" | 30 | #include "libavutil/avassert.h" |
| 31 | #include "libavutil/error.h" | 31 | #include "libavutil/error.h" |
| 32 | #include "libavutil/internal.h" | 32 | #include "libavutil/internal.h" |
| 33 | #include "libavutil/intreadwrite.h" | ||
| 33 | #include "libavutil/log.h" | 34 | #include "libavutil/log.h" |
| 34 | #include "libavutil/macros.h" | 35 | #include "libavutil/macros.h" |
| 35 | #include "libavutil/mem.h" | 36 | #include "libavutil/mem.h" |
| @@ -372,6 +373,142 @@ fail: · 336 unmodified lines | |||
| 372 | return AVERROR_INVALIDDATA; | 373 | return AVERROR_INVALIDDATA; |
| 373 | } | 374 | } |
| 374 | 375 | ||
| 376 | static void add_level(VLC_MULTI_ELEM *table, const int nb_elems, | ||
| 377 | const int num, const int numbits, | ||
| 378 | const VLCcode *buf, | ||
| 379 | uint32_t curcode, int curlen, | ||
| 380 | int curlimit, int curlevel, | ||
| 381 | const int minlen, const int max, | ||
| 382 | unsigned* levelcnt, VLC_MULTI_ELEM *info) | ||
| 383 | { | ||
| 384 | if (nb_elems > 256 && curlevel > 2) | ||
| 385 | return; // No room | ||
| 386 | for (int i = num-1; i > max; i--) { | ||
| 387 | for (int j = 0; j < 2; j++) { | ||
| 388 | int newlimit, sym; | ||
| 389 | int t = j ? i-1 : i; | ||
| 390 | int l = buf[t].bits; | ||
| 391 | uint32_t code; | ||
| 392 | |||
| 393 | sym = buf[t].symbol; | ||
| 394 | if (l > curlimit) | ||
| 395 | return; | ||
| 396 | code = curcode + (buf[t].code >> curlen); | ||
| 397 | newlimit = curlimit - l; | ||
| 398 | l += curlen; | ||
| 399 | if (nb_elems>256) AV_WN16(info->val+2*curlevel, sym); | ||
| 400 | else info->val[curlevel] = sym&0xFF; | ||
| 401 | |||
| 402 | if (curlevel) { // let's not add single entries | ||
| 403 | uint32_t val = code >> (32 - numbits); | ||
| 404 | uint32_t nb = val + (1U << (numbits - l)); | ||
| 405 | info->len = l; | ||
| 406 | info->num = curlevel+1; | ||
| 407 | for (; val < nb; val++) | ||
| 408 | AV_COPY64(table+val, info); | ||
| 409 | levelcnt[curlevel-1]++; | ||
| 410 | } | ||
| 411 | |||
| 412 | if (curlevel+1 < VLC_MULTI_MAX_SYMBOLS && newlimit >= minlen) { | ||
| 413 | add_level(table, nb_elems, num, numbits, buf, | ||
| 414 | code, l, newlimit, curlevel+1, | ||
| 415 | minlen, max, levelcnt, info); | ||
| 416 | } | ||
| 417 | } | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | static int vlc_multi_gen(VLC_MULTI_ELEM *table, const VLC *single, | ||
| 422 | const int nb_elems, const int nb_codes, const int numbits, | ||
| 423 | VLCcode *buf, void *logctx) | ||
| 424 | { | ||
| 425 | int minbits, maxbits, max = nb_codes-1; | ||
| 426 | unsigned count[VLC_MULTI_MAX_SYMBOLS-1] = { 0, }; | ||
| 427 | VLC_MULTI_ELEM info = { { 0, }, 0, }; | ||
| 428 | |||
| 429 | minbits = buf[nb_codes-1].bits; | ||
| 430 | maxbits = FFMIN(buf[0].bits, numbits); | ||
| 431 | |||
| 432 | while (max >= nb_codes/2) { | ||
| 433 | if (buf[max].bits+minbits > maxbits) | ||
| 434 | break; | ||
| 435 | max--; | ||
| 436 | } | ||
| 437 | |||
| 438 | for (int j = 0; j < 1<<numbits; j++) { | ||
| 439 | table[j].len = single->table[j].len; | ||
| 440 | table[j].num = single->table[j].len > 0 ? 1 : 0; | ||
| 441 | AV_WN16(table[j].val, single->table[j].sym); | ||
| 442 | } | ||
| 443 | |||
| 444 | add_level(table, nb_elems, nb_codes, numbits, buf, | ||
| 445 | 0, 0, numbits, 0, minbits, max, count, &info); | ||
| 446 | |||
| 447 | av_log(NULL, AV_LOG_DEBUG, "Joint: %d/%d/%d/%d/%d codes min=%ubits max=%u\n", | ||
| 448 | count[0], count[1], count[2], count[3], count[4], minbits, max); | ||
| 449 | |||
| 450 | return 0; | ||
| 451 | } | ||
| 452 | |||
| 453 | int ff_init_vlc_multi_from_lengths(VLC *vlc, VLC_MULTI *multi, int nb_bits, int nb_elems, | ||
| 454 | int nb_codes, const int8_t *lens, int lens_wrap, | ||
| 455 | const void *symbols, int symbols_wrap, int symbols_size, | ||
| 456 | int offset, int flags, void *logctx) | ||
| 457 | { | ||
| 458 | VLCcode localbuf[LOCALBUF_ELEMS], *buf = localbuf; | ||
| 459 | uint64_t code; | ||
| 460 | int ret, j, len_max = FFMIN(32, 3 * nb_bits); | ||
| 461 | |||
| 462 | ret = vlc_common_init(vlc, nb_bits, nb_codes, &buf, flags); | ||
| 463 | if (ret < 0) | ||
| 464 | return ret; | ||
| 465 | |||
| 466 | multi->table = av_malloc(sizeof(*multi->table) << nb_bits); | ||
| 467 | if (!multi->table) | ||
| 468 | return AVERROR(ENOMEM); | ||
| 469 | |||
| 470 | j = code = 0; | ||
| 471 | for (int i = 0; i < nb_codes; i++, lens += lens_wrap) { | ||
| 472 | int len = *lens; | ||
| 473 | if (len > 0) { | ||
| 474 | unsigned sym; | ||
| 475 | |||
| 476 | buf[j].bits = len; | ||
| 477 | if (symbols) | ||
| 478 | GET_DATA(sym, symbols, i, symbols_wrap, symbols_size) | ||
| 479 | else | ||
| 480 | sym = i; | ||
| 481 | buf[j].symbol = sym + offset; | ||
| 482 | buf[j++].code = code; | ||
| 483 | } else if (len < 0) { | ||
| 484 | len = -len; | ||
| 485 | } else | ||
| 486 | continue; | ||
| 487 | if (len > len_max || code & ((1U << (32 - len)) - 1)) { | ||
| 488 | av_log(logctx, AV_LOG_ERROR, "Invalid VLC (length %u)\n", len); | ||
| 489 | goto fail; | ||
| 490 | } | ||
| 491 | code += 1U << (32 - len); | ||
| 492 | if (code > UINT32_MAX + 1ULL) { | ||
| 493 | av_log(logctx, AV_LOG_ERROR, "Overdetermined VLC tree\n"); | ||
| 494 | goto fail; | ||
| 495 | } | ||
| 496 | } | ||
| 497 | ret = vlc_common_end(vlc, nb_bits, j, buf, flags, localbuf); | ||
| 498 | if (ret < 0) | ||
| 499 | goto fail; | ||
| 500 | return vlc_multi_gen(multi->table, vlc, nb_elems, j, nb_bits, buf, logctx); | ||
| 501 | fail: | ||
| 502 | if (buf != localbuf) | ||
| 503 | av_free(buf); | ||
| 504 | return AVERROR_INVALIDDATA; | ||
| 505 | } | ||
| 506 | |||
| 507 | void ff_free_vlc_multi(VLC_MULTI *vlc) | ||
| 508 | { | ||
| 509 | av_freep(&vlc->table); | ||
| 510 | } | ||
| 511 | |||
| 375 | void ff_free_vlc(VLC *vlc) | 512 | void ff_free_vlc(VLC *vlc) |
| 376 | { | 513 | { |
| 377 | av_freep(&vlc->table); | 514 | av_freep(&vlc->table); |
| @@ -21,6 +21,8 @@ · 20 unmodified lines | |||
| 21 | 21 | ||
| 22 | #include <stdint.h> | 22 | #include <stdint.h> |
| 23 | 23 | ||
| 24 | #define VLC_MULTI_MAX_SYMBOLS 6 | ||
| 25 | |||
| 24 | // When changing this, be sure to also update tableprint_vlc.h accordingly. | 26 | // When changing this, be sure to also update tableprint_vlc.h accordingly. |
| 25 | typedef int16_t VLCBaseType; | 27 | typedef int16_t VLCBaseType; |
| 26 | 28 | ||
| @@ -34,6 +36,17 @@ typedef struct VLC { · 7 unmodified lines | |||
| 34 | int table_size, table_allocated; | 36 | int table_size, table_allocated; |
| 35 | } VLC; | 37 | } VLC; |
| 36 | 38 | ||
| 39 | typedef struct VLC_MULTI_ELEM { | ||
| 40 | uint8_t val[VLC_MULTI_MAX_SYMBOLS]; | ||
| 41 | int8_t len; // -31,32 | ||
| 42 | uint8_t num; | ||
| 43 | } VLC_MULTI_ELEM; | ||
| 44 | |||
| 45 | typedef struct VLC_MULTI { | ||
| 46 | VLC_MULTI_ELEM *table; | ||
| 47 | int table_size, table_allocated; | ||
| 48 | } VLC_MULTI; | ||
| 49 | |||
| 37 | typedef struct RL_VLC_ELEM { | 50 | typedef struct RL_VLC_ELEM { |
| 38 | int16_t level; | 51 | int16_t level; |
| 39 | int8_t len; | 52 | int8_t len; |
| @@ -89,6 +102,46 @@ int ff_init_vlc_from_lengths(VLC *vlc, int nb_bits, int nb_codes, · 49 unmodified lines | |||
| 89 | const void *symbols, int symbols_wrap, int symbols_size, | 102 | const void *symbols, int symbols_wrap, int symbols_size, |
| 90 | int offset, int flags, void *logctx); | 103 | int offset, int flags, void *logctx); |
| 91 | 104 | ||
| 105 | /** | ||
| 106 | * Build VLC decoding tables suitable for use with get_vlc_multi() | ||
| 107 | * | ||
| 108 | * This function takes lengths and symbols and calculates the codes from them. | ||
| 109 | * For this the input lengths and symbols have to be sorted according to "left | ||
| 110 | * nodes in the corresponding tree first". | ||
| 111 | * | ||
| 112 | * @param[in,out] vlc The VLC to be initialized; table and table_allocated | ||
| 113 | * must have been set when initializing a static VLC, | ||
| 114 | * otherwise this will be treated as uninitialized. | ||
| 115 | * @param[in,out] multi The VLC_MULTI to be initialized; table and table_allocated | ||
| 116 | * must have been set when initializing a static VLC, | ||
| 117 | * otherwise this will be treated as uninitialized. | ||
| 118 | * @param[in] nb_bits The number of bits to use for the VLC table; | ||
| 119 | * higher values take up more memory and cache, but | ||
| 120 | * allow to read codes with fewer reads. | ||
| 121 | * @param[in] nb_elems The max possible number of elements. | ||
| 122 | * @param[in] nb_codes The number of provided length and (if supplied) symbol | ||
| 123 | * entries. | ||
| 124 | * @param[in] lens The lengths of the codes. Entries > 0 correspond to | ||
| 125 | * valid codes; entries == 0 will be skipped and entries | ||
| 126 | * with len < 0 indicate that the tree is incomplete and | ||
| 127 | * has an open end of length -len at this position. | ||
| 128 | * @param[in] lens_wrap Stride (in bytes) of the lengths. | ||
| 129 | * @param[in] symbols The symbols, i.e. what is returned from get_vlc2() | ||
| 130 | * when the corresponding code is encountered. | ||
| 131 | * May be NULL, then 0, 1, 2, 3, 4,... will be used. | ||
| 132 | * @param[in] symbols_wrap Stride (in bytes) of the symbols. | ||
| 133 | * @param[in] symbols_size Size of the symbols. 1 and 2 are supported. | ||
| 134 | * @param[in] offset An offset to apply to all the valid symbols. | ||
| 135 | * @param[in] flags A combination of the INIT_VLC_* flags; notice that | ||
| 136 | * INIT_VLC_INPUT_LE is pointless and ignored. | ||
| 137 | */ | ||
| 138 | int ff_init_vlc_multi_from_lengths(VLC *vlc, VLC_MULTI *multi, int nb_bits, int nb_elems, | ||
| 139 | int nb_codes, const int8_t *lens, int lens_wrap, | ||
| 140 | const void *symbols, int symbols_wrap, int symbols_size, | ||
| 141 | int offset, int flags, void *logctx); | ||
| 142 | |||
| 143 | |||
| 144 | void ff_free_vlc_multi(VLC_MULTI *vlc); | ||
| 92 | void ff_free_vlc(VLC *vlc); | 145 | void ff_free_vlc(VLC *vlc); |
| 93 | 146 | ||
| 94 | /* If INIT_VLC_INPUT_LE is set, the LSB bit of the codes used to | 147 | /* If INIT_VLC_INPUT_LE is set, the LSB bit of the codes used to |