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 #define VLC_TYPE int16_t 25 26 typedef struct VLC { 27 int bits; 28 VLC_TYPE (*table)[2]; ///< code, bits 29 int table_size, table_allocated; 30 } VLC; 31 32 typedef struct RL_VLC_ELEM { 33 int16_t level; 34 int8_t len; 35 uint8_t run; 36 } RL_VLC_ELEM; 37 38 #define init_vlc(vlc, nb_bits, nb_codes, \ 39 bits, bits_wrap, bits_size, \ 40 codes, codes_wrap, codes_size, \ 41 flags) \ 42 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \ 43 bits, bits_wrap, bits_size, \ 44 codes, codes_wrap, codes_size, \ 45 NULL, 0, 0, flags) 46 47 int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, 48 const void *bits, int bits_wrap, int bits_size, 49 const void *codes, int codes_wrap, int codes_size, 50 const void *symbols, int symbols_wrap, int symbols_size, 51 int flags); 52 53 /** 54 * Build VLC decoding tables suitable for use with get_vlc2() 55 * 56 * This function takes lengths and symbols and calculates the codes from them. 57 * For this the input lengths and symbols have to be sorted according to "left 58 * nodes in the corresponding tree first". 59 * 60 * @param[in,out] vlc The VLC to be initialized; table and table_allocated 61 * must have been set when initializing a static VLC, 62 * otherwise this will be treated as uninitialized. 63 * @param[in] nb_bits The number of bits to use for the VLC table; 64 * higher values take up more memory and cache, but 65 * allow to read codes with fewer reads. 66 * @param[in] nb_codes The number of provided length and (if supplied) symbol 67 * entries. 68 * @param[in] lens The lengths of the codes. Entries > 0 correspond to 69 * valid codes; entries == 0 will be skipped and entries 70 * with len < 0 indicate that the tree is incomplete and 71 * has an open end of length -len at this position. 72 * @param[in] lens_wrap Stride (in bytes) of the lengths. 73 * @param[in] symbols The symbols, i.e. what is returned from get_vlc2() 74 * when the corresponding code is encountered. 75 * May be NULL, then 0, 1, 2, 3, 4,... will be used. 76 * @param[in] symbols_wrap Stride (in bytes) of the symbols. 77 * @param[in] symbols_size Size of the symbols. 1 and 2 are supported. 78 * @param[in] offset An offset to apply to all the valid symbols. 79 * @param[in] flags A combination of the INIT_VLC_* flags; notice that 80 * INIT_VLC_INPUT_LE is pointless and ignored. 81 */ 82 int ff_init_vlc_from_lengths(VLC *vlc, int nb_bits, int nb_codes, 83 const int8_t *lens, int lens_wrap, 84 const void *symbols, int symbols_wrap, int symbols_size, 85 int offset, int flags, void *logctx); 86 87 void ff_free_vlc(VLC *vlc); 88 89 /* If INIT_VLC_INPUT_LE is set, the LSB bit of the codes used to 90 * initialize the VLC table is the first bit to be read. */ 91 #define INIT_VLC_INPUT_LE 2 92 /* If set the VLC is intended for a little endian bitstream reader. */ 93 #define INIT_VLC_OUTPUT_LE 8 94 #define INIT_VLC_LE (INIT_VLC_INPUT_LE | INIT_VLC_OUTPUT_LE) 95 #define INIT_VLC_USE_NEW_STATIC 4 96 #define INIT_VLC_STATIC_OVERLONG (1 | INIT_VLC_USE_NEW_STATIC) 97 98 #define INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, \ 99 h, i, j, flags, static_size) \ 100 do { \ 101 static VLC_TYPE table[static_size][2]; \ 102 (vlc)->table = table; \ 103 (vlc)->table_allocated = static_size; \ 104 ff_init_vlc_sparse(vlc, bits, a, b, c, d, e, f, g, h, i, j, \ 105 flags | INIT_VLC_USE_NEW_STATIC); \ 106 } while (0) 107 108 #define INIT_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, h, i, j, static_size) \ 109 INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, \ 110 h, i, j, 0, static_size) 111 112 #define INIT_LE_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, h, i, j, static_size) \ 113 INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, \ 114 h, i, j, INIT_VLC_LE, static_size) 115 116 #define INIT_CUSTOM_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, flags, static_size) \ 117 INIT_CUSTOM_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, \ 118 NULL, 0, 0, flags, static_size) 119 120 #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \ 121 INIT_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, NULL, 0, 0, static_size) 122 123 #define INIT_LE_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \ 124 INIT_LE_VLC_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, NULL, 0, 0, static_size) 125 126 #define INIT_VLC_STATIC_FROM_LENGTHS(vlc, bits, nb_codes, lens, len_wrap, \ 127 symbols, symbols_wrap, symbols_size, \ 128 offset, flags, static_size) \ 129 do { \ 130 static VLC_TYPE table[static_size][2]; \ 131 (vlc)->table = table; \ 132 (vlc)->table_allocated = static_size; \ 133 ff_init_vlc_from_lengths(vlc, bits, nb_codes, lens, len_wrap, \ 134 symbols, symbols_wrap, symbols_size, \ 135 offset, flags | INIT_VLC_USE_NEW_STATIC, \ 136 NULL); \ 137 } while (0) 138 139 #endif /* AVCODEC_VLC_H */ 140