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.

146 lines
4.0KB

  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. void ff_build_rac_states(RangeCoder *c, int factor, int max_p);
  53. static inline void renorm_encoder(RangeCoder *c)
  54. {
  55. // FIXME: optimize
  56. while (c->range < 0x100) {
  57. if (c->outstanding_byte < 0) {
  58. c->outstanding_byte = c->low >> 8;
  59. } else if (c->low <= 0xFF00) {
  60. *c->bytestream++ = c->outstanding_byte;
  61. for (; c->outstanding_count; c->outstanding_count--)
  62. *c->bytestream++ = 0xFF;
  63. c->outstanding_byte = c->low >> 8;
  64. } else if (c->low >= 0x10000) {
  65. *c->bytestream++ = c->outstanding_byte + 1;
  66. for (; c->outstanding_count; c->outstanding_count--)
  67. *c->bytestream++ = 0x00;
  68. c->outstanding_byte = (c->low >> 8) & 0xFF;
  69. } else {
  70. c->outstanding_count++;
  71. }
  72. c->low = (c->low & 0xFF) << 8;
  73. c->range <<= 8;
  74. }
  75. }
  76. static inline int get_rac_count(RangeCoder *c)
  77. {
  78. int x = c->bytestream - c->bytestream_start + c->outstanding_count;
  79. if (c->outstanding_byte >= 0)
  80. x++;
  81. return 8 * x - av_log2(c->range);
  82. }
  83. static inline void put_rac(RangeCoder *c, uint8_t *const state, int bit)
  84. {
  85. int range1 = (c->range * (*state)) >> 8;
  86. av_assert2(*state);
  87. av_assert2(range1 < c->range);
  88. av_assert2(range1 > 0);
  89. if (!bit) {
  90. c->range -= range1;
  91. *state = c->zero_state[*state];
  92. } else {
  93. c->low += c->range - range1;
  94. c->range = range1;
  95. *state = c->one_state[*state];
  96. }
  97. renorm_encoder(c);
  98. }
  99. static inline void refill(RangeCoder *c)
  100. {
  101. if (c->range < 0x100) {
  102. c->range <<= 8;
  103. c->low <<= 8;
  104. if (c->bytestream < c->bytestream_end) {
  105. c->low += c->bytestream[0];
  106. c->bytestream++;
  107. } else
  108. c->overread ++;
  109. }
  110. }
  111. static inline int get_rac(RangeCoder *c, uint8_t *const state)
  112. {
  113. int range1 = (c->range * (*state)) >> 8;
  114. c->range -= range1;
  115. if (c->low < c->range) {
  116. *state = c->zero_state[*state];
  117. refill(c);
  118. return 0;
  119. } else {
  120. c->low -= c->range;
  121. *state = c->one_state[*state];
  122. c->range = range1;
  123. refill(c);
  124. return 1;
  125. }
  126. }
  127. #endif /* AVCODEC_RANGECODER_H */