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.

139 lines
3.7KB

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