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.

143 lines
3.8KB

  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. #ifndef AVCODEC_RANGECODER_H
  27. #define AVCODEC_RANGECODER_H
  28. #include <stdint.h>
  29. #include <assert.h>
  30. #include "common.h"
  31. typedef struct RangeCoder{
  32. int low;
  33. int range;
  34. int outstanding_count;
  35. int outstanding_byte;
  36. uint8_t zero_state[256];
  37. uint8_t one_state[256];
  38. uint8_t *bytestream_start;
  39. uint8_t *bytestream;
  40. uint8_t *bytestream_end;
  41. }RangeCoder;
  42. void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size);
  43. void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size);
  44. int ff_rac_terminate(RangeCoder *c);
  45. void ff_build_rac_states(RangeCoder *c, int factor, int max_p);
  46. static inline void renorm_encoder(RangeCoder *c){
  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. int x= c->bytestream - c->bytestream_start + c->outstanding_count;
  70. if(c->outstanding_byte >= 0)
  71. x++;
  72. return 8*x - av_log2(c->range);
  73. }
  74. static inline void put_rac(RangeCoder *c, uint8_t * const state, int bit){
  75. int range1= (c->range * (*state)) >> 8;
  76. assert(*state);
  77. assert(range1 < c->range);
  78. assert(range1 > 0);
  79. if(!bit){
  80. c->range -= range1;
  81. *state= c->zero_state[*state];
  82. }else{
  83. c->low += c->range - range1;
  84. c->range = range1;
  85. *state= c->one_state[*state];
  86. }
  87. renorm_encoder(c);
  88. }
  89. static inline void refill(RangeCoder *c){
  90. if(c->range < 0x100){
  91. c->range <<= 8;
  92. c->low <<= 8;
  93. if(c->bytestream < c->bytestream_end)
  94. c->low+= c->bytestream[0];
  95. c->bytestream++;
  96. }
  97. }
  98. static inline int get_rac(RangeCoder *c, uint8_t * const state){
  99. int range1= (c->range * (*state)) >> 8;
  100. int av_unused one_mask;
  101. c->range -= range1;
  102. #if 1
  103. if(c->low < c->range){
  104. *state= c->zero_state[*state];
  105. refill(c);
  106. return 0;
  107. }else{
  108. c->low -= c->range;
  109. *state= c->one_state[*state];
  110. c->range = range1;
  111. refill(c);
  112. return 1;
  113. }
  114. #else
  115. one_mask= (c->range - c->low-1)>>31;
  116. c->low -= c->range & one_mask;
  117. c->range += (range1 - c->range) & one_mask;
  118. *state= c->zero_state[(*state) + (256&one_mask)];
  119. refill(c);
  120. return one_mask&1;
  121. #endif
  122. }
  123. #endif