rangecoder.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Range coder
  3. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  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. * Range coder.
  24. */
  25. #ifndef AVCODEC_RANGECODER_H
  26. #define AVCODEC_RANGECODER_H
  27. #include <stdint.h>
  28. #include "libavutil/common.h"
  29. #include "libavutil/avassert.h"
  30. typedef struct RangeCoder {
  31. int low;
  32. int range;
  33. int outstanding_count;
  34. int outstanding_byte;
  35. uint8_t zero_state[256];
  36. uint8_t one_state[256];
  37. uint8_t *bytestream_start;
  38. uint8_t *bytestream;
  39. uint8_t *bytestream_end;
  40. int overread;
  41. #define MAX_OVERREAD 2
  42. } RangeCoder;
  43. void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size);
  44. void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size);
  45. /**
  46. * Terminates the range coder
  47. * @param version version 0 requires the decoder to know the data size in bytes
  48. * version 1 needs about 1 bit more space but does not need to
  49. * carry the size from encoder to decoder
  50. */
  51. int ff_rac_terminate(RangeCoder *c, int version);
  52. /**
  53. * Check if at the current position there is a valid looking termination
  54. * @param version version 0 requires the decoder to know the data size in bytes
  55. * version 1 needs about 1 bit more space but does not need to
  56. * carry the size from encoder to decoder
  57. * @returns negative AVERROR code on error or non negative.
  58. */
  59. int ff_rac_check_termination(RangeCoder *c, int version);
  60. void ff_build_rac_states(RangeCoder *c, int factor, int max_p);
  61. static inline void renorm_encoder(RangeCoder *c)
  62. {
  63. // FIXME: optimize
  64. while (c->range < 0x100) {
  65. if (c->outstanding_byte < 0) {
  66. c->outstanding_byte = c->low >> 8;
  67. } else if (c->low <= 0xFF00) {
  68. *c->bytestream++ = c->outstanding_byte;
  69. for (; c->outstanding_count; c->outstanding_count--)
  70. *c->bytestream++ = 0xFF;
  71. c->outstanding_byte = c->low >> 8;
  72. } else if (c->low >= 0x10000) {
  73. *c->bytestream++ = c->outstanding_byte + 1;
  74. for (; c->outstanding_count; c->outstanding_count--)
  75. *c->bytestream++ = 0x00;
  76. c->outstanding_byte = (c->low >> 8) & 0xFF;
  77. } else {
  78. c->outstanding_count++;
  79. }
  80. c->low = (c->low & 0xFF) << 8;
  81. c->range <<= 8;
  82. }
  83. }
  84. static inline int get_rac_count(RangeCoder *c)
  85. {
  86. int x = c->bytestream - c->bytestream_start + c->outstanding_count;
  87. if (c->outstanding_byte >= 0)
  88. x++;
  89. return 8 * x - av_log2(c->range);
  90. }
  91. static inline void put_rac(RangeCoder *c, uint8_t *const state, int bit)
  92. {
  93. int range1 = (c->range * (*state)) >> 8;
  94. av_assert2(*state);
  95. av_assert2(range1 < c->range);
  96. av_assert2(range1 > 0);
  97. if (!bit) {
  98. c->range -= range1;
  99. *state = c->zero_state[*state];
  100. } else {
  101. c->low += c->range - range1;
  102. c->range = range1;
  103. *state = c->one_state[*state];
  104. }
  105. renorm_encoder(c);
  106. }
  107. static inline void refill(RangeCoder *c)
  108. {
  109. if (c->range < 0x100) {
  110. c->range <<= 8;
  111. c->low <<= 8;
  112. if (c->bytestream < c->bytestream_end) {
  113. c->low += c->bytestream[0];
  114. c->bytestream++;
  115. } else
  116. c->overread ++;
  117. }
  118. }
  119. static inline int get_rac(RangeCoder *c, uint8_t *const state)
  120. {
  121. int range1 = (c->range * (*state)) >> 8;
  122. c->range -= range1;
  123. if (c->low < c->range) {
  124. *state = c->zero_state[*state];
  125. refill(c);
  126. return 0;
  127. } else {
  128. c->low -= c->range;
  129. *state = c->one_state[*state];
  130. c->range = range1;
  131. refill(c);
  132. return 1;
  133. }
  134. }
  135. #endif /* AVCODEC_RANGECODER_H */