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.

135 lines
3.6KB

  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. /**
  23. * @file rangecoder.h
  24. * Range coder.
  25. */
  26. typedef struct RangeCoder{
  27. int low;
  28. int range;
  29. int outstanding_count;
  30. int outstanding_byte;
  31. uint8_t zero_state[256];
  32. uint8_t one_state[256];
  33. uint8_t *bytestream_start;
  34. uint8_t *bytestream;
  35. uint8_t *bytestream_end;
  36. }RangeCoder;
  37. void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size);
  38. void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size);
  39. int ff_rac_terminate(RangeCoder *c);
  40. void ff_build_rac_states(RangeCoder *c, int factor, int max_p);
  41. static inline void renorm_encoder(RangeCoder *c){
  42. //FIXME optimize
  43. while(c->range < 0x100){
  44. if(c->outstanding_byte < 0){
  45. c->outstanding_byte= c->low>>8;
  46. }else if(c->low <= 0xFF00){
  47. *c->bytestream++ = c->outstanding_byte;
  48. for(;c->outstanding_count; c->outstanding_count--)
  49. *c->bytestream++ = 0xFF;
  50. c->outstanding_byte= c->low>>8;
  51. }else if(c->low >= 0x10000){
  52. *c->bytestream++ = c->outstanding_byte + 1;
  53. for(;c->outstanding_count; c->outstanding_count--)
  54. *c->bytestream++ = 0x00;
  55. c->outstanding_byte= (c->low>>8) & 0xFF;
  56. }else{
  57. c->outstanding_count++;
  58. }
  59. c->low = (c->low & 0xFF)<<8;
  60. c->range <<= 8;
  61. }
  62. }
  63. static inline int get_rac_count(RangeCoder *c){
  64. int x= c->bytestream - c->bytestream_start + c->outstanding_count;
  65. if(c->outstanding_byte >= 0)
  66. x++;
  67. return 8*x - av_log2(c->range);
  68. }
  69. static inline void put_rac(RangeCoder *c, uint8_t * const state, int bit){
  70. int range1= (c->range * (*state)) >> 8;
  71. assert(*state);
  72. assert(range1 < c->range);
  73. assert(range1 > 0);
  74. if(!bit){
  75. c->range -= range1;
  76. *state= c->zero_state[*state];
  77. }else{
  78. c->low += c->range - range1;
  79. c->range = range1;
  80. *state= c->one_state[*state];
  81. }
  82. renorm_encoder(c);
  83. }
  84. static inline void refill(RangeCoder *c){
  85. if(c->range < 0x100){
  86. c->range <<= 8;
  87. c->low <<= 8;
  88. if(c->bytestream < c->bytestream_end)
  89. c->low+= c->bytestream[0];
  90. c->bytestream++;
  91. }
  92. }
  93. static inline int get_rac(RangeCoder *c, uint8_t * const state){
  94. int range1= (c->range * (*state)) >> 8;
  95. int av_unused one_mask;
  96. c->range -= range1;
  97. #if 1
  98. if(c->low < c->range){
  99. *state= c->zero_state[*state];
  100. refill(c);
  101. return 0;
  102. }else{
  103. c->low -= c->range;
  104. *state= c->one_state[*state];
  105. c->range = range1;
  106. refill(c);
  107. return 1;
  108. }
  109. #else
  110. one_mask= (c->range - c->low-1)>>31;
  111. c->low -= c->range & one_mask;
  112. c->range += (range1 - c->range) & one_mask;
  113. *state= c->zero_state[(*state) + (256&one_mask)];
  114. refill(c);
  115. return one_mask&1;
  116. #endif
  117. }