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.

229 lines
5.1KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 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. * @file
  23. * Context Adaptive Binary Arithmetic Coder.
  24. */
  25. #include <string.h>
  26. #include "libavutil/common.h"
  27. #include "libavutil/timer.h"
  28. #include "get_bits.h"
  29. #include "cabac.h"
  30. #include "cabac_functions.h"
  31. #include "cabac_tablegen.h"
  32. /**
  33. *
  34. * @param buf_size size of buf in bits
  35. */
  36. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size){
  37. init_put_bits(&c->pb, buf, buf_size);
  38. c->low= 0;
  39. c->range= 0x1FE;
  40. c->outstanding_count= 0;
  41. c->pb.bit_left++; //avoids firstBitFlag
  42. }
  43. /**
  44. *
  45. * @param buf_size size of buf in bits
  46. */
  47. int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size){
  48. c->bytestream_start=
  49. c->bytestream= buf;
  50. c->bytestream_end= buf + buf_size;
  51. #if CABAC_BITS == 16
  52. c->low = (*c->bytestream++)<<18;
  53. c->low+= (*c->bytestream++)<<10;
  54. #else
  55. c->low = (*c->bytestream++)<<10;
  56. #endif
  57. c->low+= ((*c->bytestream++)<<2) + 2;
  58. c->range= 0x1FE;
  59. if ((c->range<<(CABAC_BITS+1)) < c->low)
  60. return AVERROR_INVALIDDATA;
  61. return 0;
  62. }
  63. void ff_init_cabac_states(void)
  64. {
  65. static int initialized = 0;
  66. if (initialized)
  67. return;
  68. cabac_tableinit();
  69. initialized = 1;
  70. }
  71. #ifdef TEST
  72. #define SIZE 10240
  73. #include "libavutil/lfg.h"
  74. #include "avcodec.h"
  75. static inline void put_cabac_bit(CABACContext *c, int b){
  76. put_bits(&c->pb, 1, b);
  77. for(;c->outstanding_count; c->outstanding_count--){
  78. put_bits(&c->pb, 1, 1-b);
  79. }
  80. }
  81. static inline void renorm_cabac_encoder(CABACContext *c){
  82. while(c->range < 0x100){
  83. //FIXME optimize
  84. if(c->low<0x100){
  85. put_cabac_bit(c, 0);
  86. }else if(c->low<0x200){
  87. c->outstanding_count++;
  88. c->low -= 0x100;
  89. }else{
  90. put_cabac_bit(c, 1);
  91. c->low -= 0x200;
  92. }
  93. c->range+= c->range;
  94. c->low += c->low;
  95. }
  96. }
  97. static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
  98. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
  99. if(bit == ((*state)&1)){
  100. c->range -= RangeLPS;
  101. *state = ff_h264_mlps_state[128 + *state];
  102. }else{
  103. c->low += c->range - RangeLPS;
  104. c->range = RangeLPS;
  105. *state= ff_h264_mlps_state[127 - *state];
  106. }
  107. renorm_cabac_encoder(c);
  108. }
  109. /**
  110. * @param bit 0 -> write zero bit, !=0 write one bit
  111. */
  112. static void put_cabac_bypass(CABACContext *c, int bit){
  113. c->low += c->low;
  114. if(bit){
  115. c->low += c->range;
  116. }
  117. //FIXME optimize
  118. if(c->low<0x200){
  119. put_cabac_bit(c, 0);
  120. }else if(c->low<0x400){
  121. c->outstanding_count++;
  122. c->low -= 0x200;
  123. }else{
  124. put_cabac_bit(c, 1);
  125. c->low -= 0x400;
  126. }
  127. }
  128. /**
  129. *
  130. * @return the number of bytes written
  131. */
  132. static int put_cabac_terminate(CABACContext *c, int bit){
  133. c->range -= 2;
  134. if(!bit){
  135. renorm_cabac_encoder(c);
  136. }else{
  137. c->low += c->range;
  138. c->range= 2;
  139. renorm_cabac_encoder(c);
  140. av_assert0(c->low <= 0x1FF);
  141. put_cabac_bit(c, c->low>>9);
  142. put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
  143. flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
  144. }
  145. return (put_bits_count(&c->pb)+7)>>3;
  146. }
  147. int main(void){
  148. CABACContext c;
  149. uint8_t b[9*SIZE];
  150. uint8_t r[9*SIZE];
  151. int i, ret = 0;
  152. uint8_t state[10]= {0};
  153. AVLFG prng;
  154. av_lfg_init(&prng, 1);
  155. ff_init_cabac_encoder(&c, b, SIZE);
  156. ff_init_cabac_states();
  157. for(i=0; i<SIZE; i++){
  158. if(2*i<SIZE) r[i] = av_lfg_get(&prng) % 7;
  159. else r[i] = (i>>8)&1;
  160. }
  161. for(i=0; i<SIZE; i++){
  162. put_cabac_bypass(&c, r[i]&1);
  163. }
  164. for(i=0; i<SIZE; i++){
  165. put_cabac(&c, state, r[i]&1);
  166. }
  167. put_cabac_terminate(&c, 1);
  168. ff_init_cabac_decoder(&c, b, SIZE);
  169. memset(state, 0, sizeof(state));
  170. for(i=0; i<SIZE; i++){
  171. if( (r[i]&1) != get_cabac_bypass(&c) ) {
  172. av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i);
  173. ret = 1;
  174. }
  175. }
  176. for(i=0; i<SIZE; i++){
  177. if( (r[i]&1) != get_cabac_noinline(&c, state) ) {
  178. av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i);
  179. ret = 1;
  180. }
  181. }
  182. if(!get_cabac_terminate(&c)) {
  183. av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");
  184. ret = 1;
  185. }
  186. return ret;
  187. }
  188. #endif /* TEST */