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.

166 lines
4.0KB

  1. /*
  2. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavcodec/cabac.c"
  21. #define SIZE 10240
  22. #include "libavutil/lfg.h"
  23. #include "libavcodec/avcodec.h"
  24. static inline void put_cabac_bit(CABACContext *c, int b){
  25. put_bits(&c->pb, 1, b);
  26. for(;c->outstanding_count; c->outstanding_count--){
  27. put_bits(&c->pb, 1, 1-b);
  28. }
  29. }
  30. static inline void renorm_cabac_encoder(CABACContext *c){
  31. while(c->range < 0x100){
  32. //FIXME optimize
  33. if(c->low<0x100){
  34. put_cabac_bit(c, 0);
  35. }else if(c->low<0x200){
  36. c->outstanding_count++;
  37. c->low -= 0x100;
  38. }else{
  39. put_cabac_bit(c, 1);
  40. c->low -= 0x200;
  41. }
  42. c->range+= c->range;
  43. c->low += c->low;
  44. }
  45. }
  46. static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
  47. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
  48. if(bit == ((*state)&1)){
  49. c->range -= RangeLPS;
  50. *state = ff_h264_mlps_state[128 + *state];
  51. }else{
  52. c->low += c->range - RangeLPS;
  53. c->range = RangeLPS;
  54. *state= ff_h264_mlps_state[127 - *state];
  55. }
  56. renorm_cabac_encoder(c);
  57. }
  58. /**
  59. * @param bit 0 -> write zero bit, !=0 write one bit
  60. */
  61. static void put_cabac_bypass(CABACContext *c, int bit){
  62. c->low += c->low;
  63. if(bit){
  64. c->low += c->range;
  65. }
  66. //FIXME optimize
  67. if(c->low<0x200){
  68. put_cabac_bit(c, 0);
  69. }else if(c->low<0x400){
  70. c->outstanding_count++;
  71. c->low -= 0x200;
  72. }else{
  73. put_cabac_bit(c, 1);
  74. c->low -= 0x400;
  75. }
  76. }
  77. /**
  78. *
  79. * @return the number of bytes written
  80. */
  81. static int put_cabac_terminate(CABACContext *c, int bit){
  82. c->range -= 2;
  83. if(!bit){
  84. renorm_cabac_encoder(c);
  85. }else{
  86. c->low += c->range;
  87. c->range= 2;
  88. renorm_cabac_encoder(c);
  89. av_assert0(c->low <= 0x1FF);
  90. put_cabac_bit(c, c->low>>9);
  91. put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
  92. flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
  93. }
  94. return (put_bits_count(&c->pb)+7)>>3;
  95. }
  96. int main(void){
  97. CABACContext c;
  98. uint8_t b[9*SIZE];
  99. uint8_t r[9*SIZE];
  100. int i, ret = 0;
  101. uint8_t state[10]= {0};
  102. AVLFG prng;
  103. av_lfg_init(&prng, 1);
  104. ff_init_cabac_encoder(&c, b, SIZE);
  105. for(i=0; i<SIZE; i++){
  106. if(2*i<SIZE) r[i] = av_lfg_get(&prng) % 7;
  107. else r[i] = (i>>8)&1;
  108. }
  109. for(i=0; i<SIZE; i++){
  110. put_cabac_bypass(&c, r[i]&1);
  111. }
  112. for(i=0; i<SIZE; i++){
  113. put_cabac(&c, state, r[i]&1);
  114. }
  115. i= put_cabac_terminate(&c, 1);
  116. b[i++] = av_lfg_get(&prng);
  117. b[i ] = av_lfg_get(&prng);
  118. ff_init_cabac_decoder(&c, b, SIZE);
  119. memset(state, 0, sizeof(state));
  120. for(i=0; i<SIZE; i++){
  121. if( (r[i]&1) != get_cabac_bypass(&c) ) {
  122. av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i);
  123. ret = 1;
  124. }
  125. }
  126. for(i=0; i<SIZE; i++){
  127. if( (r[i]&1) != get_cabac_noinline(&c, state) ) {
  128. av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i);
  129. ret = 1;
  130. }
  131. }
  132. if(!get_cabac_terminate(&c)) {
  133. av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");
  134. ret = 1;
  135. }
  136. return ret;
  137. }