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.

256 lines
7.7KB

  1. /*
  2. * Copyright (C) 2016 Open Broadcast Systems Ltd.
  3. * Author 2016 Rostislav Pehlivanov <rpehlivanov@obe.tv>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "dirac_vlc.h"
  22. #define LUT_SIZE (1 << LUT_BITS)
  23. #define RSIZE_BITS (CHAR_BIT*sizeof(residual))
  24. #define CONVERT_TO_RESIDUE(a, b) \
  25. (((residual)(a)) << (RSIZE_BITS - (b)))
  26. #define INIT_RESIDUE(N) \
  27. residual N = 0; \
  28. av_unused int32_t N ## _bits = 0
  29. #define SET_RESIDUE(N, I, B) \
  30. N = CONVERT_TO_RESIDUE(I, B); \
  31. N ## _bits = B
  32. #define APPEND_RESIDUE(N, M) \
  33. N |= M >> (N ## _bits); \
  34. N ## _bits += (M ## _bits)
  35. int ff_dirac_golomb_read_32bit(DiracGolombLUT *lut_ctx, const uint8_t *buf,
  36. int bytes, uint8_t *_dst, int coeffs)
  37. {
  38. int i, b, c_idx = 0;
  39. int32_t *dst = (int32_t *)_dst;
  40. DiracGolombLUT *future[4], *l = &lut_ctx[2*LUT_SIZE + buf[0]];
  41. INIT_RESIDUE(res);
  42. for (b = 1; b <= bytes; b++) {
  43. future[0] = &lut_ctx[buf[b]];
  44. future[1] = future[0] + 1*LUT_SIZE;
  45. future[2] = future[0] + 2*LUT_SIZE;
  46. future[3] = future[0] + 3*LUT_SIZE;
  47. if ((c_idx + 1) > coeffs)
  48. return c_idx;
  49. if (res_bits >= RSIZE_BITS)
  50. res_bits = res = 0;
  51. /* res_bits is a hint for better branch prediction */
  52. if (res_bits && l->sign) {
  53. int32_t coeff = 1;
  54. APPEND_RESIDUE(res, l->preamble);
  55. for (i = 0; i < (res_bits >> 1) - 1; i++) {
  56. coeff <<= 1;
  57. coeff |= (res >> (RSIZE_BITS - 2*i - 2)) & 1;
  58. }
  59. dst[c_idx++] = l->sign * (coeff - 1);
  60. res_bits = res = 0;
  61. }
  62. memcpy(&dst[c_idx], l->ready, LUT_BITS*sizeof(int32_t));
  63. c_idx += l->ready_num;
  64. APPEND_RESIDUE(res, l->leftover);
  65. l = future[l->need_s ? 3 : !res_bits ? 2 : res_bits & 1];
  66. }
  67. return c_idx;
  68. }
  69. int ff_dirac_golomb_read_16bit(DiracGolombLUT *lut_ctx, const uint8_t *buf,
  70. int bytes, uint8_t *_dst, int coeffs)
  71. {
  72. int i, b, c_idx = 0;
  73. int16_t *dst = (int16_t *)_dst;
  74. DiracGolombLUT *future[4], *l = &lut_ctx[2*LUT_SIZE + buf[0]];
  75. INIT_RESIDUE(res);
  76. for (b = 1; b <= bytes; b++) {
  77. future[0] = &lut_ctx[buf[b]];
  78. future[1] = future[0] + 1*LUT_SIZE;
  79. future[2] = future[0] + 2*LUT_SIZE;
  80. future[3] = future[0] + 3*LUT_SIZE;
  81. if ((c_idx + 1) > coeffs)
  82. return c_idx;
  83. if (res_bits && l->sign) {
  84. int32_t coeff = 1;
  85. APPEND_RESIDUE(res, l->preamble);
  86. for (i = 0; i < (res_bits >> 1) - 1; i++) {
  87. coeff <<= 1;
  88. coeff |= (res >> (RSIZE_BITS - 2*i - 2)) & 1;
  89. }
  90. dst[c_idx++] = l->sign * (coeff - 1);
  91. res_bits = res = 0;
  92. }
  93. for (i = 0; i < LUT_BITS; i++)
  94. dst[c_idx + i] = l->ready[i];
  95. c_idx += l->ready_num;
  96. APPEND_RESIDUE(res, l->leftover);
  97. l = future[l->need_s ? 3 : !res_bits ? 2 : res_bits & 1];
  98. }
  99. return c_idx;
  100. }
  101. /* Searches for golomb codes in a residue */
  102. static inline void search_for_golomb(DiracGolombLUT *l, residual r, int bits)
  103. {
  104. int r_count = RSIZE_BITS - 1;
  105. int bits_start, bits_tot = bits, need_sign = 0;
  106. #define READ_BIT(N) (((N) >> (N ## _count--)) & 1)
  107. while (1) {
  108. int32_t coef = 1;
  109. bits_start = (RSIZE_BITS - 1) - r_count;
  110. while (1) {
  111. if (!bits--)
  112. goto leftover;
  113. if (READ_BIT(r))
  114. break;
  115. coef <<= 1;
  116. if (!bits--)
  117. goto leftover;
  118. coef |= READ_BIT(r);
  119. }
  120. l->ready[l->ready_num] = coef - 1;
  121. if (l->ready[l->ready_num]) {
  122. if (!bits--) {
  123. need_sign = 1;
  124. goto leftover;
  125. }
  126. l->ready[l->ready_num] *= READ_BIT(r) ? -1 : +1;
  127. }
  128. l->ready_num++;
  129. if (!bits)
  130. return;
  131. }
  132. leftover:
  133. l->leftover = r << bits_start;
  134. l->leftover_bits = bits_tot - bits_start;
  135. l->need_s = need_sign;
  136. }
  137. /* Parity LUTs - even and odd bit end positions */
  138. static void generate_parity_lut(DiracGolombLUT *lut, int even)
  139. {
  140. int idx;
  141. for (idx = 0; idx < LUT_SIZE; idx++) {
  142. DiracGolombLUT *l = &lut[idx];
  143. int symbol_end_loc = -1;
  144. uint32_t code;
  145. int i;
  146. INIT_RESIDUE(res);
  147. SET_RESIDUE(res, idx, LUT_BITS);
  148. for (i = 0; i < LUT_BITS; i++) {
  149. const int cond = even ? (i & 1) : !(i & 1);
  150. if (((res >> (RSIZE_BITS - i - 1)) & 1) && cond) {
  151. symbol_end_loc = i + 2;
  152. break;
  153. }
  154. }
  155. if (symbol_end_loc < 0 || symbol_end_loc > LUT_BITS) {
  156. l->preamble = 0;
  157. l->preamble_bits = 0;
  158. l->leftover_bits = LUT_BITS;
  159. l->leftover = CONVERT_TO_RESIDUE(idx, l->leftover_bits);
  160. if (even)
  161. l->need_s = idx & 1;
  162. continue;
  163. }
  164. /* Gets bits 0 through to (symbol_end_loc - 1) inclusive */
  165. code = idx >> ((LUT_BITS - 1) - (symbol_end_loc - 1));
  166. code &= ((1 << LUT_BITS) - 1) >> (LUT_BITS - symbol_end_loc);
  167. l->preamble_bits = symbol_end_loc;
  168. l->preamble = CONVERT_TO_RESIDUE(code, l->preamble_bits);
  169. l->sign = ((l->preamble >> (RSIZE_BITS - l->preamble_bits)) & 1) ? -1 : +1;
  170. search_for_golomb(l, res << symbol_end_loc, LUT_BITS - symbol_end_loc);
  171. }
  172. }
  173. /* Reset (off == 0) and needs-one-more-bit (off == 1) LUTs */
  174. static void generate_offset_lut(DiracGolombLUT *lut, int off)
  175. {
  176. int idx;
  177. for (idx = 0; idx < LUT_SIZE; idx++) {
  178. DiracGolombLUT *l = &lut[idx];
  179. INIT_RESIDUE(res);
  180. SET_RESIDUE(res, idx, LUT_BITS);
  181. l->preamble_bits = off;
  182. if (off) {
  183. l->preamble = CONVERT_TO_RESIDUE(res >> (RSIZE_BITS - off), off);
  184. l->sign = ((l->preamble >> (RSIZE_BITS - l->preamble_bits)) & 1) ? -1 : +1;
  185. } else {
  186. l->preamble = 0;
  187. l->sign = 1;
  188. }
  189. search_for_golomb(l, res << off, LUT_BITS - off);
  190. }
  191. }
  192. av_cold int ff_dirac_golomb_reader_init(DiracGolombLUT **lut_ctx)
  193. {
  194. DiracGolombLUT *lut;
  195. if (!(lut = av_calloc(4*LUT_SIZE, sizeof(DiracGolombLUT))))
  196. return AVERROR(ENOMEM);
  197. generate_parity_lut(&lut[0*LUT_SIZE], 0);
  198. generate_parity_lut(&lut[1*LUT_SIZE], 1);
  199. generate_offset_lut(&lut[2*LUT_SIZE], 0);
  200. generate_offset_lut(&lut[3*LUT_SIZE], 1);
  201. *lut_ctx = lut;
  202. return 0;
  203. }
  204. av_cold void ff_dirac_golomb_reader_end(DiracGolombLUT **lut_ctx)
  205. {
  206. av_freep(lut_ctx);
  207. }