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.

134 lines
3.6KB

  1. /*
  2. * Range coder
  3. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 <assert.h>
  29. #include "libavutil/common.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. } RangeCoder;
  41. void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size);
  42. void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size);
  43. int ff_rac_terminate(RangeCoder *c);
  44. void ff_build_rac_states(RangeCoder *c, int factor, int max_p);
  45. static inline void renorm_encoder(RangeCoder *c)
  46. {
  47. // FIXME: optimize
  48. while (c->range < 0x100) {
  49. if (c->outstanding_byte < 0) {
  50. c->outstanding_byte = c->low >> 8;
  51. } else if (c->low <= 0xFF00) {
  52. *c->bytestream++ = c->outstanding_byte;
  53. for (; c->outstanding_count; c->outstanding_count--)
  54. *c->bytestream++ = 0xFF;
  55. c->outstanding_byte = c->low >> 8;
  56. } else if (c->low >= 0x10000) {
  57. *c->bytestream++ = c->outstanding_byte + 1;
  58. for (; c->outstanding_count; c->outstanding_count--)
  59. *c->bytestream++ = 0x00;
  60. c->outstanding_byte = (c->low >> 8) & 0xFF;
  61. } else {
  62. c->outstanding_count++;
  63. }
  64. c->low = (c->low & 0xFF) << 8;
  65. c->range <<= 8;
  66. }
  67. }
  68. static inline int get_rac_count(RangeCoder *c)
  69. {
  70. int x = c->bytestream - c->bytestream_start + c->outstanding_count;
  71. if (c->outstanding_byte >= 0)
  72. x++;
  73. return 8 * x - av_log2(c->range);
  74. }
  75. static inline void put_rac(RangeCoder *c, uint8_t *const state, int bit)
  76. {
  77. int range1 = (c->range * (*state)) >> 8;
  78. assert(*state);
  79. assert(range1 < c->range);
  80. assert(range1 > 0);
  81. if (!bit) {
  82. c->range -= range1;
  83. *state = c->zero_state[*state];
  84. } else {
  85. c->low += c->range - range1;
  86. c->range = range1;
  87. *state = c->one_state[*state];
  88. }
  89. renorm_encoder(c);
  90. }
  91. static inline void refill(RangeCoder *c)
  92. {
  93. if (c->range < 0x100) {
  94. c->range <<= 8;
  95. c->low <<= 8;
  96. if (c->bytestream < c->bytestream_end)
  97. c->low += c->bytestream[0];
  98. c->bytestream++;
  99. }
  100. }
  101. static inline int get_rac(RangeCoder *c, uint8_t *const state)
  102. {
  103. int range1 = (c->range * (*state)) >> 8;
  104. c->range -= range1;
  105. if (c->low < c->range) {
  106. *state = c->zero_state[*state];
  107. refill(c);
  108. return 0;
  109. } else {
  110. c->low -= c->range;
  111. *state = c->one_state[*state];
  112. c->range = range1;
  113. refill(c);
  114. return 1;
  115. }
  116. }
  117. #endif /* AVCODEC_RANGECODER_H */