rangecoder.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 void put_rac(RangeCoder *c, uint8_t * const state, int bit){
  64. int range1= (c->range * (*state)) >> 8;
  65. assert(*state);
  66. assert(range1 < c->range);
  67. assert(range1 > 0);
  68. if(!bit){
  69. c->range -= range1;
  70. *state= c->zero_state[*state];
  71. }else{
  72. c->low += c->range - range1;
  73. c->range = range1;
  74. *state= c->one_state[*state];
  75. }
  76. renorm_encoder(c);
  77. }
  78. static inline void refill(RangeCoder *c){
  79. if(c->range < 0x100){
  80. c->range <<= 8;
  81. c->low <<= 8;
  82. if(c->bytestream < c->bytestream_end)
  83. c->low+= c->bytestream[0];
  84. c->bytestream++;
  85. }
  86. }
  87. static inline int get_rac(RangeCoder *c, uint8_t * const state){
  88. int range1= (c->range * (*state)) >> 8;
  89. int attribute_unused one_mask;
  90. c->range -= range1;
  91. #if 1
  92. if(c->low < c->range){
  93. *state= c->zero_state[*state];
  94. refill(c);
  95. return 0;
  96. }else{
  97. c->low -= c->range;
  98. *state= c->one_state[*state];
  99. c->range = range1;
  100. refill(c);
  101. return 1;
  102. }
  103. #else
  104. one_mask= (c->range - c->low-1)>>31;
  105. c->low -= c->range & one_mask;
  106. c->range += (range1 - c->range) & one_mask;
  107. *state= c->zero_state[(*state) + (256&one_mask)];
  108. refill(c);
  109. return one_mask&1;
  110. #endif
  111. }