biaridecod.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*!
  2. *************************************************************************************
  3. * \file biaridecod.c
  4. *
  5. * \brief
  6. * Binary arithmetic decoder routines.
  7. *
  8. * This modified implementation of the M Coder is based on JVT-U084
  9. * with the choice of M_BITS = 16.
  10. *
  11. * \date
  12. * 21. Oct 2000
  13. * \author
  14. * Main contributors (see contributors.h for copyright, address and affiliation details)
  15. * - Detlev Marpe <[email protected]>
  16. * - Gabi Blaettermann
  17. * - Gunnar Marten
  18. *************************************************************************************
  19. */
  20. #include "global.h"
  21. #include "memalloc.h"
  22. #include "biaridecod.h"
  23. #define B_BITS 10 // Number of bits to represent the whole coding interval
  24. #define HALF 0x01FE //(1 << (B_BITS-1)) - 2
  25. #define QUARTER 0x0100 //(1 << (B_BITS-2))
  26. /************************************************************************
  27. ************************************************************************
  28. init / exit decoder
  29. ************************************************************************
  30. ************************************************************************/
  31. /*!
  32. ************************************************************************
  33. * \brief
  34. * Allocates memory for the DecodingEnvironment struct
  35. * \return DecodingContextPtr
  36. * allocates memory
  37. ************************************************************************
  38. */
  39. DecodingEnvironmentPtr arideco_create_decoding_environment()
  40. {
  41. DecodingEnvironmentPtr dep;
  42. if ((dep = calloc(1,sizeof(DecodingEnvironment))) == NULL)
  43. no_mem_exit("arideco_create_decoding_environment: dep");
  44. return dep;
  45. }
  46. /*!
  47. ***********************************************************************
  48. * \brief
  49. * Frees memory of the DecodingEnvironment struct
  50. ***********************************************************************
  51. */
  52. void arideco_delete_decoding_environment(DecodingEnvironmentPtr dep)
  53. {
  54. if (dep == NULL)
  55. {
  56. snprintf(errortext, ET_SIZE, "Error freeing dep (NULL pointer)");
  57. error (errortext, 200);
  58. }
  59. else
  60. free(dep);
  61. }
  62. /*!
  63. ************************************************************************
  64. * \brief
  65. * finalize arithetic decoding():
  66. ************************************************************************
  67. */
  68. void arideco_done_decoding(DecodingEnvironmentPtr dep)
  69. {
  70. (*dep->Dcodestrm_len)++;
  71. #if(TRACE==2)
  72. fprintf(p_trace, "done_decoding: %d\n", *dep->Dcodestrm_len);
  73. #endif
  74. }
  75. /*!
  76. ************************************************************************
  77. * \brief
  78. * read one byte from the bitstream
  79. ************************************************************************
  80. */
  81. unsigned int getbyte(DecodingEnvironmentPtr dep)
  82. {
  83. #if(TRACE==2)
  84. fprintf(p_trace, "get_byte: %d\n", (*dep->Dcodestrm_len));
  85. #endif
  86. return dep->Dcodestrm[(*dep->Dcodestrm_len)++];
  87. }
  88. /*!
  89. ************************************************************************
  90. * \brief
  91. * read two bytes from the bitstream
  92. ************************************************************************
  93. */
  94. static unsigned int getword(DecodingEnvironmentPtr dep)
  95. {
  96. int d = *dep->Dcodestrm_len;
  97. *dep->Dcodestrm_len += 2;
  98. return ((dep->Dcodestrm[d]<<8) | dep->Dcodestrm[d+1]);
  99. }
  100. /*!
  101. ************************************************************************
  102. * \brief
  103. * Initializes the DecodingEnvironment for the arithmetic coder
  104. ************************************************************************
  105. */
  106. void arideco_start_decoding(DecodingEnvironmentPtr dep, unsigned char *code_buffer,
  107. int firstbyte, int *code_len)
  108. {
  109. dep->Dcodestrm = code_buffer;
  110. dep->Dcodestrm_len = code_len;
  111. *dep->Dcodestrm_len = firstbyte;
  112. dep->Dvalue = getbyte(dep);
  113. dep->Dvalue = (dep->Dvalue << 16) | getword(dep); // lookahead of 2 bytes: always make sure that bitstream buffer
  114. // contains 2 more bytes than actual bitstream
  115. dep->DbitsLeft = 15;
  116. dep->Drange = HALF;
  117. #if (2==TRACE)
  118. fprintf(p_trace, "value: %d firstbyte: %d code_len: %d\n", dep->Dvalue >> dep->DbitsLeft, firstbyte, *code_len);
  119. #endif
  120. }
  121. /*!
  122. ************************************************************************
  123. * \brief
  124. * biari_decode_symbol():
  125. * \return
  126. * the decoded symbol
  127. ************************************************************************
  128. */
  129. /* random notes
  130. max rLPS = 240 1111 1 111
  131. max state = 63
  132. max renorm = 6, min 1
  133. max bitsleft = 16
  134. max range = (1<<10) ????? (1024)
  135. */
  136. #if !defined(_M_IX86) || defined(_DEBUG)
  137. unsigned int biari_decode_symbol(DecodingEnvironmentPtr dep, BiContextTypePtr bi_ct )
  138. {
  139. unsigned int state = bi_ct->state;
  140. unsigned int bit = bi_ct->MPS;
  141. unsigned int value = dep->Dvalue;
  142. unsigned int range = dep->Drange;
  143. const unsigned int rLPS = rLPS_table_64x4[(range>>6)&3][state];
  144. range -= rLPS;
  145. if(value >= (range << dep->DbitsLeft))
  146. { // LPS
  147. int renorm;
  148. bi_ct->state = AC_next_state_LPS_64[state]; // next state
  149. value -= (range << dep->DbitsLeft);
  150. bit ^= 0x01;
  151. //if (!state) // switch meaning of MPS if necessary
  152. // bi_ct->MPS = bit;
  153. bi_ct->MPS ^= !state;//0x01;
  154. renorm = renorm_table_256[rLPS];
  155. range = (rLPS << renorm);
  156. dep->Drange = range;
  157. dep->DbitsLeft -= renorm;
  158. if( dep->DbitsLeft > 0 )
  159. {
  160. dep->Dvalue = value;
  161. return(bit);
  162. }
  163. dep->Dvalue = (value << 16) | getword(dep); // lookahead of 2 bytes: always make sure that bitstream buffer
  164. // contains 2 more bytes than actual bitstream
  165. dep->DbitsLeft += 16;
  166. return(bit);
  167. }
  168. else
  169. { //MPS
  170. bi_ct->state = AC_next_state_MPS_64[state]; // next state
  171. if( range < QUARTER )
  172. {
  173. dep->Drange = range << 1;
  174. dep->DbitsLeft -= 1;
  175. if( dep->DbitsLeft > 0 )
  176. {
  177. return(bit);
  178. }
  179. dep->Dvalue = (value << 16) | getword(dep); // lookahead of 2 bytes: always make sure that bitstream buffer
  180. // contains 2 more bytes than actual bitstream
  181. dep->DbitsLeft += 16;
  182. return(bit);
  183. }
  184. else
  185. {
  186. dep->Drange = range;
  187. return (bit);
  188. }
  189. }
  190. }
  191. #endif
  192. /*!
  193. ************************************************************************
  194. * \brief
  195. * biari_decode_symbol_eq_prob():
  196. * \return
  197. * the decoded symbol
  198. ************************************************************************
  199. */
  200. unsigned int biari_decode_symbol_eq_prob(DecodingEnvironmentPtr dep)
  201. {
  202. int tmp_value;
  203. int value = dep->Dvalue;
  204. if(--(dep->DbitsLeft) == 0)
  205. {
  206. value = (value << 16) | getword( dep ); // lookahead of 2 bytes: always make sure that bitstream buffer
  207. // contains 2 more bytes than actual bitstream
  208. dep->DbitsLeft = 16;
  209. }
  210. tmp_value = value - (dep->Drange << dep->DbitsLeft);
  211. if (tmp_value < 0)
  212. {
  213. dep->Dvalue = value;
  214. return 0;
  215. }
  216. else
  217. {
  218. dep->Dvalue = tmp_value;
  219. return 1;
  220. }
  221. }
  222. /*!
  223. ************************************************************************
  224. * \brief
  225. * biari_decode_symbol_final():
  226. * \return
  227. * the decoded symbol
  228. ************************************************************************
  229. */
  230. unsigned int biari_decode_final(DecodingEnvironmentPtr dep)
  231. {
  232. unsigned int range = dep->Drange - 2;
  233. int value = dep->Dvalue;
  234. value -= (range << dep->DbitsLeft);
  235. if (value < 0)
  236. {
  237. if( range >= QUARTER )
  238. {
  239. dep->Drange = range;
  240. return 0;
  241. }
  242. else
  243. {
  244. dep->Drange = (range << 1);
  245. if( --(dep->DbitsLeft) > 0 )
  246. return 0;
  247. else
  248. {
  249. dep->Dvalue = (dep->Dvalue << 16) | getword( dep ); // lookahead of 2 bytes: always make sure that bitstream buffer
  250. // contains 2 more bytes than actual bitstream
  251. dep->DbitsLeft = 16;
  252. return 0;
  253. }
  254. }
  255. }
  256. else
  257. {
  258. return 1;
  259. }
  260. }
  261. /*!
  262. ************************************************************************
  263. * \brief
  264. * Initializes a given context with some pre-defined probability state
  265. ************************************************************************
  266. */
  267. void biari_init_context (int qp, BiContextTypePtr ctx, const char* ini)
  268. {
  269. int pstate = ((ini[0]* qp )>>4) + ini[1];
  270. if ( pstate >= 64 )
  271. {
  272. pstate = imin(126, pstate);
  273. ctx->state = (uint16) (pstate - 64);
  274. ctx->MPS = 1;
  275. }
  276. else
  277. {
  278. pstate = imax(1, pstate);
  279. ctx->state = (uint16) (63 - pstate);
  280. ctx->MPS = 0;
  281. }
  282. }