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.

126 lines
3.4KB

  1. /*
  2. * Range coder
  3. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file rangecoder.h
  22. * Range coder.
  23. */
  24. typedef struct RangeCoder{
  25. int low;
  26. int range;
  27. int outstanding_count;
  28. int outstanding_byte;
  29. uint8_t zero_state[256];
  30. uint8_t one_state[256];
  31. uint8_t *bytestream_start;
  32. uint8_t *bytestream;
  33. uint8_t *bytestream_end;
  34. }RangeCoder;
  35. void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size);
  36. void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size);
  37. int ff_rac_terminate(RangeCoder *c);
  38. void ff_build_rac_states(RangeCoder *c, int factor, int max_p);
  39. static inline void renorm_encoder(RangeCoder *c){
  40. //FIXME optimize
  41. while(c->range < 0x100){
  42. if(c->outstanding_byte < 0){
  43. c->outstanding_byte= c->low>>8;
  44. }else if(c->low <= 0xFF00){
  45. *c->bytestream++ = c->outstanding_byte;
  46. for(;c->outstanding_count; c->outstanding_count--)
  47. *c->bytestream++ = 0xFF;
  48. c->outstanding_byte= c->low>>8;
  49. }else if(c->low >= 0x10000){
  50. *c->bytestream++ = c->outstanding_byte + 1;
  51. for(;c->outstanding_count; c->outstanding_count--)
  52. *c->bytestream++ = 0x00;
  53. c->outstanding_byte= (c->low>>8) & 0xFF;
  54. }else{
  55. c->outstanding_count++;
  56. }
  57. c->low = (c->low & 0xFF)<<8;
  58. c->range <<= 8;
  59. }
  60. }
  61. static inline void put_rac(RangeCoder *c, uint8_t * const state, int bit){
  62. int range1= (c->range * (*state)) >> 8;
  63. assert(*state);
  64. assert(range1 < c->range);
  65. assert(range1 > 0);
  66. if(!bit){
  67. c->range -= range1;
  68. *state= c->zero_state[*state];
  69. }else{
  70. c->low += c->range - range1;
  71. c->range = range1;
  72. *state= c->one_state[*state];
  73. }
  74. renorm_encoder(c);
  75. }
  76. static inline void refill(RangeCoder *c){
  77. if(c->range < 0x100){
  78. c->range <<= 8;
  79. c->low <<= 8;
  80. if(c->bytestream < c->bytestream_end)
  81. c->low+= c->bytestream[0];
  82. c->bytestream++;
  83. }
  84. }
  85. static inline int get_rac(RangeCoder *c, uint8_t * const state){
  86. int range1= (c->range * (*state)) >> 8;
  87. int attribute_unused one_mask;
  88. c->range -= range1;
  89. #if 1
  90. if(c->low < c->range){
  91. *state= c->zero_state[*state];
  92. refill(c);
  93. return 0;
  94. }else{
  95. c->low -= c->range;
  96. *state= c->one_state[*state];
  97. c->range = range1;
  98. refill(c);
  99. return 1;
  100. }
  101. #else
  102. one_mask= (c->range - c->low-1)>>31;
  103. c->low -= c->range & one_mask;
  104. c->range += (range1 - c->range) & one_mask;
  105. *state= c->zero_state[(*state) + (256&one_mask)];
  106. refill(c);
  107. return one_mask&1;
  108. #endif
  109. }