dirac_arith.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
  3. * Copyright (C) 2009 David Conrad
  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. /**
  22. * @file
  23. * Arithmetic decoder for Dirac
  24. * @author Marco Gerards <marco@gnu.org>
  25. */
  26. #ifndef AVCODEC_DIRAC_ARITH_H
  27. #define AVCODEC_DIRAC_ARITH_H
  28. #include "libavutil/x86/asm.h"
  29. #include "bytestream.h"
  30. #include "get_bits.h"
  31. enum dirac_arith_contexts {
  32. CTX_ZPZN_F1,
  33. CTX_ZPNN_F1,
  34. CTX_NPZN_F1,
  35. CTX_NPNN_F1,
  36. CTX_ZP_F2,
  37. CTX_ZP_F3,
  38. CTX_ZP_F4,
  39. CTX_ZP_F5,
  40. CTX_ZP_F6,
  41. CTX_NP_F2,
  42. CTX_NP_F3,
  43. CTX_NP_F4,
  44. CTX_NP_F5,
  45. CTX_NP_F6,
  46. CTX_COEFF_DATA,
  47. CTX_SIGN_NEG,
  48. CTX_SIGN_ZERO,
  49. CTX_SIGN_POS,
  50. CTX_ZERO_BLOCK,
  51. CTX_DELTA_Q_F,
  52. CTX_DELTA_Q_DATA,
  53. CTX_DELTA_Q_SIGN,
  54. DIRAC_CTX_COUNT
  55. };
  56. // Dirac resets the arith decoder between decoding various types of data,
  57. // so many contexts are never used simultaneously. Thus, we can reduce
  58. // the number of contexts needed by reusing them.
  59. #define CTX_SB_F1 CTX_ZP_F5
  60. #define CTX_SB_DATA 0
  61. #define CTX_PMODE_REF1 0
  62. #define CTX_PMODE_REF2 1
  63. #define CTX_GLOBAL_BLOCK 2
  64. #define CTX_MV_F1 CTX_ZP_F2
  65. #define CTX_MV_DATA 0
  66. #define CTX_DC_F1 CTX_ZP_F5
  67. #define CTX_DC_DATA 0
  68. typedef struct {
  69. unsigned low;
  70. uint16_t range;
  71. int16_t counter;
  72. const uint8_t *bytestream;
  73. const uint8_t *bytestream_end;
  74. uint16_t contexts[DIRAC_CTX_COUNT];
  75. int error;
  76. int overread;
  77. } DiracArith;
  78. extern const uint8_t ff_dirac_next_ctx[DIRAC_CTX_COUNT];
  79. extern const uint16_t ff_dirac_prob[256];
  80. extern int16_t ff_dirac_prob_branchless[256][2];
  81. static inline void renorm(DiracArith *c)
  82. {
  83. #if HAVE_FAST_CLZ
  84. int shift = 14 - av_log2_16bit(c->range-1) + ((c->range-1)>>15);
  85. c->low <<= shift;
  86. c->range <<= shift;
  87. c->counter += shift;
  88. #else
  89. while (c->range <= 0x4000) {
  90. c->low <<= 1;
  91. c->range <<= 1;
  92. c->counter++;
  93. }
  94. #endif
  95. }
  96. static inline void refill(DiracArith *c)
  97. {
  98. int counter = c->counter;
  99. if (counter >= 0) {
  100. int new = bytestream_get_be16(&c->bytestream);
  101. // the spec defines overread bits to be 1, and streams rely on this
  102. if (c->bytestream > c->bytestream_end) {
  103. new |= 0xff;
  104. if (c->bytestream > c->bytestream_end+1)
  105. new |= 0xff00;
  106. c->bytestream = c->bytestream_end;
  107. c->overread ++;
  108. if (c->overread > 4)
  109. c->error = AVERROR_INVALIDDATA;
  110. }
  111. c->low += new << counter;
  112. counter -= 16;
  113. }
  114. c->counter = counter;
  115. }
  116. static inline int dirac_get_arith_bit(DiracArith *c, int ctx)
  117. {
  118. int prob_zero = c->contexts[ctx];
  119. int range_times_prob, bit;
  120. unsigned low = c->low;
  121. int range = c->range;
  122. range_times_prob = (c->range * prob_zero) >> 16;
  123. #if ARCH_X86 && HAVE_FAST_CMOV && HAVE_INLINE_ASM && HAVE_6REGS
  124. low -= range_times_prob << 16;
  125. range -= range_times_prob;
  126. bit = 0;
  127. __asm__(
  128. "cmpl %5, %4 \n\t"
  129. "setae %b0 \n\t"
  130. "cmovb %3, %2 \n\t"
  131. "cmovb %5, %1 \n\t"
  132. : "+q"(bit), "+r"(range), "+r"(low)
  133. : "r"(c->low), "r"(c->low>>16),
  134. "r"(range_times_prob)
  135. );
  136. #else
  137. bit = (low >> 16) >= range_times_prob;
  138. if (bit) {
  139. low -= range_times_prob << 16;
  140. range -= range_times_prob;
  141. } else {
  142. range = range_times_prob;
  143. }
  144. #endif
  145. c->contexts[ctx] += ff_dirac_prob_branchless[prob_zero>>8][bit];
  146. c->low = low;
  147. c->range = range;
  148. renorm(c);
  149. refill(c);
  150. return bit;
  151. }
  152. static inline int dirac_get_arith_uint(DiracArith *c, int follow_ctx, int data_ctx)
  153. {
  154. int ret = 1;
  155. while (!dirac_get_arith_bit(c, follow_ctx)) {
  156. if (ret >= 0x40000000) {
  157. av_log(NULL, AV_LOG_ERROR, "dirac_get_arith_uint overflow\n");
  158. c->error = AVERROR_INVALIDDATA;
  159. return -1;
  160. }
  161. ret <<= 1;
  162. ret += dirac_get_arith_bit(c, data_ctx);
  163. follow_ctx = ff_dirac_next_ctx[follow_ctx];
  164. }
  165. return ret-1;
  166. }
  167. static inline int dirac_get_arith_int(DiracArith *c, int follow_ctx, int data_ctx)
  168. {
  169. int ret = dirac_get_arith_uint(c, follow_ctx, data_ctx);
  170. if (ret && dirac_get_arith_bit(c, data_ctx+1))
  171. ret = -ret;
  172. return ret;
  173. }
  174. void ff_dirac_init_arith_tables(void);
  175. void ff_dirac_init_arith_decoder(DiracArith *c, GetBitContext *gb, int length);
  176. #endif /* AVCODEC_DIRAC_ARITH_H */