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.

180 lines
4.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. /**
  21. * @file rangecoder.c
  22. * Range coder.
  23. * based upon
  24. * "Range encoding: an algorithm for removing redundancy from a digitised
  25. * message.
  26. * G. N. N. Martin Presented in March 1979 to the Video &
  27. * Data Recording Conference,
  28. * IBM UK Scientific Center held in Southampton July 24-27 1979."
  29. *
  30. */
  31. #include <string.h>
  32. #include "avcodec.h"
  33. #include "common.h"
  34. #include "rangecoder.h"
  35. void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size){
  36. c->bytestream_start=
  37. c->bytestream= buf;
  38. c->bytestream_end= buf + buf_size;
  39. c->low= 0;
  40. c->range= 0xFF00;
  41. c->outstanding_count= 0;
  42. c->outstanding_byte= -1;
  43. }
  44. void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size){
  45. /* cast to avoid compiler warning */
  46. ff_init_range_encoder(c, (uint8_t *) buf, buf_size);
  47. c->low =(*c->bytestream++)<<8;
  48. c->low+= *c->bytestream++;
  49. }
  50. void ff_build_rac_states(RangeCoder *c, int factor, int max_p){
  51. const int64_t one= 1LL<<32;
  52. int64_t p;
  53. int last_p8, p8, i;
  54. memset(c->zero_state, 0, sizeof(c->zero_state));
  55. memset(c-> one_state, 0, sizeof(c-> one_state));
  56. #if 0
  57. for(i=1; i<256; i++){
  58. if(c->one_state[i])
  59. continue;
  60. p= (i*one + 128) >> 8;
  61. last_p8= i;
  62. for(;;){
  63. p+= ((one-p)*factor + one/2) >> 32;
  64. p8= (256*p + one/2) >> 32; //FIXME try without the one
  65. if(p8 <= last_p8) p8= last_p8+1;
  66. if(p8 > max_p) p8= max_p;
  67. if(p8 < last_p8)
  68. break;
  69. c->one_state[last_p8]= p8;
  70. if(p8 == last_p8)
  71. break;
  72. last_p8= p8;
  73. }
  74. }
  75. #endif
  76. #if 1
  77. last_p8= 0;
  78. p= one/2;
  79. for(i=0; i<128; i++){
  80. p8= (256*p + one/2) >> 32; //FIXME try without the one
  81. if(p8 <= last_p8) p8= last_p8+1;
  82. if(last_p8 && last_p8<256 && p8<=max_p)
  83. c->one_state[last_p8]= p8;
  84. p+= ((one-p)*factor + one/2) >> 32;
  85. last_p8= p8;
  86. }
  87. #endif
  88. for(i=256-max_p; i<=max_p; i++){
  89. if(c->one_state[i])
  90. continue;
  91. p= (i*one + 128) >> 8;
  92. p+= ((one-p)*factor + one/2) >> 32;
  93. p8= (256*p + one/2) >> 32; //FIXME try without the one
  94. if(p8 <= i) p8= i+1;
  95. if(p8 > max_p) p8= max_p;
  96. c->one_state[ i]= p8;
  97. }
  98. for(i=1; i<255; i++)
  99. c->zero_state[i]= 256-c->one_state[256-i];
  100. #if 0
  101. for(i=0; i<256; i++)
  102. av_log(NULL, AV_LOG_DEBUG, "%3d %3d\n", i, c->one_state[i]);
  103. #endif
  104. }
  105. /**
  106. *
  107. * @return the number of bytes written
  108. */
  109. int ff_rac_terminate(RangeCoder *c){
  110. c->range=0xFF;
  111. c->low +=0xFF;
  112. renorm_encoder(c);
  113. c->range=0xFF;
  114. renorm_encoder(c);
  115. assert(c->low == 0);
  116. assert(c->range >= 0x100);
  117. return c->bytestream - c->bytestream_start;
  118. }
  119. #if 0 //selftest
  120. #define SIZE 10240
  121. int main(){
  122. RangeCoder c;
  123. uint8_t b[9*SIZE];
  124. uint8_t r[9*SIZE];
  125. int i;
  126. uint8_t state[10]= {0};
  127. ff_init_range_encoder(&c, b, SIZE);
  128. ff_build_rac_states(&c, 0.05*(1LL<<32), 128+64+32+16);
  129. memset(state, 128, sizeof(state));
  130. for(i=0; i<SIZE; i++){
  131. r[i]= random()%7;
  132. }
  133. for(i=0; i<SIZE; i++){
  134. START_TIMER
  135. put_rac(&c, state, r[i]&1);
  136. STOP_TIMER("put_rac")
  137. }
  138. ff_put_rac_terminate(&c);
  139. ff_init_range_decoder(&c, b, SIZE);
  140. memset(state, 128, sizeof(state));
  141. for(i=0; i<SIZE; i++){
  142. START_TIMER
  143. if( (r[i]&1) != get_rac(&c, state) )
  144. av_log(NULL, AV_LOG_DEBUG, "rac failure at %d\n", i);
  145. STOP_TIMER("get_rac")
  146. }
  147. return 0;
  148. }
  149. #endif