You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
5.5KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVCODEC_VLC_H
  19. #define AVCODEC_VLC_H
  20. #include <stdint.h>
  21. #include "bitstream.h"
  22. #define VLC_TYPE int16_t
  23. typedef struct VLC {
  24. int bits;
  25. VLC_TYPE (*table)[2]; ///< code, bits
  26. int table_size, table_allocated;
  27. } VLC;
  28. typedef struct RL_VLC_ELEM {
  29. int16_t level;
  30. int8_t len;
  31. uint8_t run;
  32. } RL_VLC_ELEM;
  33. #define init_vlc(vlc, nb_bits, nb_codes, \
  34. bits, bits_wrap, bits_size, \
  35. codes, codes_wrap, codes_size, \
  36. flags) \
  37. ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
  38. bits, bits_wrap, bits_size, \
  39. codes, codes_wrap, codes_size, \
  40. NULL, 0, 0, flags)
  41. int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
  42. const void *bits, int bits_wrap, int bits_size,
  43. const void *codes, int codes_wrap, int codes_size,
  44. const void *symbols, int symbols_wrap, int symbols_size,
  45. int flags);
  46. void ff_free_vlc(VLC *vlc);
  47. #define INIT_VLC_LE 2
  48. #define INIT_VLC_USE_NEW_STATIC 4
  49. #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
  50. do { \
  51. static VLC_TYPE table[static_size][2]; \
  52. (vlc)->table = table; \
  53. (vlc)->table_allocated = static_size; \
  54. init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \
  55. } while (0)
  56. /* Return the LUT element for the given bitstream configuration. */
  57. static inline int set_idx(BitstreamContext *bc, int code, int *n, int *nb_bits,
  58. VLC_TYPE (*table)[2])
  59. {
  60. unsigned idx;
  61. *nb_bits = -*n;
  62. idx = bitstream_peek(bc, *nb_bits) + code;
  63. *n = table[idx][1];
  64. return table[idx][0];
  65. }
  66. /**
  67. * Parse a VLC code.
  68. * @param bits is the number of bits which will be read at once, must be
  69. * identical to nb_bits in init_vlc()
  70. * @param max_depth is the number of times bits bits must be read to completely
  71. * read the longest VLC code
  72. * = (max_vlc_length + bits - 1) / bits
  73. * If the VLC code is invalid and max_depth = 1, then no bits will be removed.
  74. * If the VLC code is invalid and max_depth > 1, then the number of bits removed
  75. * is undefined. */
  76. static inline int bitstream_read_vlc(BitstreamContext *bc, VLC_TYPE (*table)[2],
  77. int bits, int max_depth)
  78. {
  79. int nb_bits;
  80. unsigned idx = bitstream_peek(bc, bits);
  81. int code = table[idx][0];
  82. int n = table[idx][1];
  83. if (max_depth > 1 && n < 0) {
  84. skip_remaining(bc, bits);
  85. code = set_idx(bc, code, &n, &nb_bits, table);
  86. if (max_depth > 2 && n < 0) {
  87. skip_remaining(bc, nb_bits);
  88. code = set_idx(bc, code, &n, &nb_bits, table);
  89. }
  90. }
  91. skip_remaining(bc, n);
  92. return code;
  93. }
  94. #define BITSTREAM_RL_VLC(level, run, bc, table, bits, max_depth) \
  95. do { \
  96. int n, nb_bits; \
  97. unsigned index = bitstream_peek(bc, bits); \
  98. level = table[index].level; \
  99. n = table[index].len; \
  100. \
  101. if (max_depth > 1 && n < 0) { \
  102. bitstream_skip(bc, bits); \
  103. \
  104. nb_bits = -n; \
  105. \
  106. index = bitstream_peek(bc, nb_bits) + level; \
  107. level = table[index].level; \
  108. n = table[index].len; \
  109. if (max_depth > 2 && n < 0) { \
  110. bitstream_skip(bc, nb_bits); \
  111. nb_bits = -n; \
  112. \
  113. index = bitstream_peek(bc, nb_bits) + level; \
  114. level = table[index].level; \
  115. n = table[index].len; \
  116. } \
  117. } \
  118. run = table[index].run; \
  119. bitstream_skip(bc, n); \
  120. } while (0)
  121. #endif /* AVCODEC_VLC_H */