1 /* 2 * This file is part of FFmpeg. 3 * 4 * FFmpeg is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * FFmpeg is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with FFmpeg; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17 */ 18 19 #ifndef AVCODEC_VLC_H 20 #define AVCODEC_VLC_H 21 22 #include <stdint.h> 23 24 // When changing this, be sure to also update tableprint_vlc.h accordingly. 25 typedef int16_t VLCBaseType; 26 27 typedef struct VLCElem { 28 VLCBaseType sym, len; 29 } VLCElem; 30 31 typedef struct VLC { 32 int bits; 33 VLCElem *table; 34 int table_size, table_allocated; 35 } VLC; 36 37 typedef struct RL_VLC_ELEM { 38 int16_t level; 39 int8_t len; 40 uint8_t run; 41 } RL_VLC_ELEM; 42 43 #define init_vlc(vlc, nb_bits, nb_codes, \ 44 bits, bits_wrap, bits_size, \ 45 codes, codes_wrap, codes_size, \ 46 flags) \ 47 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \ 48 bits, bits_wrap, bits_size, \ 49 codes, codes_wrap, codes_size, \ 50 NULL, 0, 0, flags) 51 52 int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, 53 const void *bits, int bits_wrap, int bits_size, 54 const void *codes, int codes_wrap, int codes_size, 55 const void *symbols, int symbols_wrap, int symbols_size, 56 int flags); 57 58 /** 59 * Build VLC decoding tables suitable for use with get_vlc2() 60 * 61 * This function takes lengths and symbols and calculates the codes from them. 62 * For this the input lengths and symbols have to be sorted according to "left 63 * nodes in the corresponding tree first". 64 * 65 * @param[in,out] vlc The VLC to be initialized; table and table_allocated 66 * must have been set when initializing a static VLC, 67 * otherwise this will be treated as uninitialized. 68 * @param[in] nb_bits The number of bits to use for the VLC table; 69 * higher values take up more memory and cache, but 70 * allow to read codes with fewer reads. 71 * @param[in] nb_codes The number of provided length and (if supplied) symbol 72 * entries. 73 * @param[in] lens The lengths of the codes. Entries > 0 correspond to 74 * valid codes; entries == 0 will be skipped and entries 75 * with len < 0 indicate that the tree is incomplete and 76 * has an open end of length -len at this position. 77 * @param[in] lens_wrap Stride (in bytes) of the lengths. 78 * @param[in] symbols The symbols, i.e. what is returned from get_vlc2() 79 * when the corresponding code is encountered. 80 * May be NULL, then 0, 1, 2, 3, 4,... will be used. 81 * @param[in] symbols_wrap Stride (in bytes) of the symbols. 82 * @param[in] symbols_size Size of the symbols. 1 and 2 are supported. 83 * @param[in] offset An offset to apply to all the valid symbols. 84 * @param[in] flags A combination of the INIT_VLC_* flags; notice that 85 * INIT_VLC_INPUT_LE is pointless and ignored. 86 */ 87 int ff_init_vlc_from_lengths(VLC *vlc, int nb_bits, int nb_codes, 88 const int8_t *lens, int lens_wrap, 89 const void *symbols, int symbols_wrap, int symbols_size, 90 int offset, int flags, void *logctx); 91 92 void ff_free_vlc(VLC *vlc); 93 94 /* If INIT_VLC_INPUT_LE is set, the LSB bit of the codes used to 95 * initialize the VLC table is the first bit to be read. */ 96 #define INIT_VLC_INPUT_LE 2 97 /* If set the VLC is intended for a little endian bitstream reader. */ 98 #define INIT_VLC_OUTPUT_LE 8 99 #define INIT_VLC_LE (INIT_VLC_INPUT_LE | INIT_VLC_OUTPUT_LE) 100 #define INIT_VLC_USE_NEW_STATIC 4 101 #define INIT_VLC_STATIC_OVERLONG (1 | INIT_VLC_USE_NEW_STATIC) 102 103 #define INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, \ 104 h, i, j, flags, static_size) \ 105 do { \ 106 static VLCElem table[static_size]; \ 107 (vlc)->table = table; \ 108 (vlc)->table_allocated = static_size; \ 109 ff_init_vlc_sparse(vlc, bits, a, b, c, d, e, f, g, h, i, j, \ 110 flags | INIT_VLC_USE_NEW_STATIC); \ 111 } while (0) 112 113 #define INIT_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, h, i, j, static_size) \ 114 INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, \ 115 h, i, j, 0, static_size) 116 117 #define INIT_LE_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, h, i, j, static_size) \ 118 INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, \ 119 h, i, j, INIT_VLC_LE, static_size) 120 121 #define INIT_CUSTOM_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, flags, static_size) \ 122 INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, \ 123 NULL, 0, 0, flags, static_size) 124 125 #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \ 126 INIT_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, NULL, 0, 0, static_size) 127 128 #define INIT_LE_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \ 129 INIT_LE_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, NULL, 0, 0, static_size) 130 131 #define INIT_VLC_STATIC_FROM_LENGTHS(vlc, bits, nb_codes, lens, len_wrap, \ 132 symbols, symbols_wrap, symbols_size, \ 133 offset, flags, static_size) \ 134 do { \ 135 static VLCElem table[static_size]; \ 136 (vlc)->table = table; \ 137 (vlc)->table_allocated = static_size; \ 138 ff_init_vlc_from_lengths(vlc, bits, nb_codes, lens, len_wrap, \ 139 symbols, symbols_wrap, symbols_size, \ 140 offset, flags | INIT_VLC_USE_NEW_STATIC, \ 141 NULL); \ 142 } while (0) 143 144 #endif /* AVCODEC_VLC_H */ 145