History

avcodec: add multi vlc reader

Heavily based and inspired by Christophe's cache branches.

Paul B Mahol committed 3 years ago

Commit 29b9fca1 parent 3a5be285 files · +231 −0Browse files

Unified · Split

5 files
  1. libavcodec/bitstream.h +2 −0
  2. libavcodec/bitstream_template.h +29 −0
  3. libavcodec/get_bits.h +10 −0
  4. libavcodec/vlc.c +137 −0
  5. libavcodec/vlc.h +53 −0
libavcodec/bitstream.h+2−0
@@ -102,6 +102,7 @@ · 101 unmodified lines
102102 # define bits_decode210 bits_decode210_le
103103 # define bits_apply_sign bits_apply_sign_le
104104 # define bits_read_vlc bits_read_vlc_le
105+# define bits_read_vlc_multi bits_read_vlc_multi_le
105106
106107 #elif defined(BITS_DEFAULT_BE)
107108
@@ -130,6 +131,7 @@ · 22 unmodified lines
130131 # define bits_decode210 bits_decode210_be
131132 # define bits_apply_sign bits_apply_sign_be
132133 # define bits_read_vlc bits_read_vlc_be
134+# define bits_read_vlc_multi bits_read_vlc_multi_be
133135
134136 #endif
135137
libavcodec/bitstream_template.h+29−0
@@ -520,6 +520,35 @@ static inline int BS_FUNC(read_vlc)(BSCTX *bc, const VLCElem *table, · 519 unmodified lines
520520 return code;
521521 }
522522
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+
523552 #undef BSCTX
524553 #undef BS_FUNC
525554 #undef BS_JOIN3
libavcodec/get_bits.h+10−0
@@ -93,6 +93,7 @@ typedef BitstreamContext GetBitContext; · 92 unmodified lines
9393 #define init_get_bits8 bits_init8
9494 #define align_get_bits bits_align
9595 #define get_vlc2 bits_read_vlc
96+#define get_vlc_multi bits_read_vlc_multi
9697
9798 #define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size)
9899 #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
643644 return code;
644645 }
645646
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+
646656 static inline int decode012(GetBitContext *gb)
647657 {
648658 int n;
libavcodec/vlc.c+137−0
@@ -30,6 +30,7 @@ · 29 unmodified lines
3030 #include "libavutil/avassert.h"
3131 #include "libavutil/error.h"
3232 #include "libavutil/internal.h"
33+#include "libavutil/intreadwrite.h"
3334 #include "libavutil/log.h"
3435 #include "libavutil/macros.h"
3536 #include "libavutil/mem.h"
@@ -372,6 +373,142 @@ fail: · 336 unmodified lines
372373 return AVERROR_INVALIDDATA;
373374 }
374375
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+
375512 void ff_free_vlc(VLC *vlc)
376513 {
377514 av_freep(&vlc->table);
libavcodec/vlc.h+53−0
@@ -21,6 +21,8 @@ · 20 unmodified lines
2121
2222 #include <stdint.h>
2323
24+#define VLC_MULTI_MAX_SYMBOLS 6
25+
2426 // When changing this, be sure to also update tableprint_vlc.h accordingly.
2527 typedef int16_t VLCBaseType;
2628
@@ -34,6 +36,17 @@ typedef struct VLC { · 7 unmodified lines
3436 int table_size, table_allocated;
3537 } VLC;
3638
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+
3750 typedef struct RL_VLC_ELEM {
3851 int16_t level;
3952 int8_t len;
@@ -89,6 +102,46 @@ int ff_init_vlc_from_lengths(VLC *vlc, int nb_bits, int nb_codes, · 49 unmodified lines
89102 const void *symbols, int symbols_wrap, int symbols_size,
90103 int offset, int flags, void *logctx);
91104
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);
92145 void ff_free_vlc(VLC *vlc);
93146
94147 /* If INIT_VLC_INPUT_LE is set, the LSB bit of the codes used to

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t