Leaked source code of windows server 2003
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.

379 lines
11 KiB

  1. /* File: sv_h263_sac.c */
  2. /*****************************************************************************
  3. ** Copyright (c) Digital Equipment Corporation, 1995, 1997 **
  4. ** **
  5. ** All Rights Reserved. Unpublished rights reserved under the copyright **
  6. ** laws of the United States. **
  7. ** **
  8. ** The software contained on this media is proprietary to and embodies **
  9. ** the confidential technology of Digital Equipment Corporation. **
  10. ** Possession, use, duplication or dissemination of the software and **
  11. ** media is authorized only pursuant to a valid written license from **
  12. ** Digital Equipment Corporation. **
  13. ** **
  14. ** RESTRICTED RIGHTS LEGEND Use, duplication, or disclosure by the U.S. **
  15. ** Government is subject to restrictions as set forth in Subparagraph **
  16. ** (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, as applicable. **
  17. ******************************************************************************/
  18. /*
  19. #define _SLIBDEBUG_
  20. */
  21. #include "sv_h263.h"
  22. #include "sv_intrn.h"
  23. #include "SC_err.h"
  24. #include "proto.h"
  25. #ifdef _SLIBDEBUG_
  26. #include "sc_debug.h"
  27. #define _DEBUG_ 0 /* detailed debuging statements */
  28. #define _VERBOSE_ 1 /* show progress */
  29. #define _VERIFY_ 0 /* verify correct operation */
  30. #define _WARN_ 1 /* warnings about strange behavior */
  31. extern void *dbg;
  32. #endif
  33. #define q1 16384
  34. #define q2 32768
  35. #define q3 49152
  36. #define top 65535
  37. /* local prototypes */
  38. static void bit_out_psc_layer(ScBitstream_t *BSIn);
  39. static int sv_H263bit_opp_bits(ScBitstream_t *BSOut, int bit);
  40. static int sv_H263bit_in_psc_layer(ScBitstream_t *BSOut, int bit);
  41. /*********************************************************************
  42. * SAC Decoder Algorithm as Specified in H26P Annex -E
  43. *
  44. * Name: decode_a_symbol
  45. *
  46. * Description: Decodes an Aritmetically Encoded Symbol
  47. *
  48. * Input: array holding cumulative freq. data
  49. * also uses static data for decoding endpoints
  50. * and code_value variable
  51. *
  52. * Returns: Index to relevant symbol model
  53. *
  54. * Side Effects: Modifies low, high, length, cum and code_value
  55. *
  56. * Author: Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
  57. *
  58. *********************************************************************/
  59. static qword low=0, high=top, zerorun=0; /* decoder and encoder */
  60. static qword code_value, bit; /* decoder */
  61. static qword opposite_bits=0; /* encoder */
  62. int sv_H263SACDecode_a_symbol(ScBitstream_t *BSIn, int cumul_freq[ ])
  63. {
  64. qword length, cum, sacindex;
  65. length = high - low + 1;
  66. cum = (-1 + (code_value - low + 1) * cumul_freq[0]) / length;
  67. for (sacindex = 1; cumul_freq[sacindex] > cum; sacindex++);
  68. high = low - 1 + (length * cumul_freq[sacindex-1]) / cumul_freq[0];
  69. low += (length * cumul_freq[sacindex]) / cumul_freq[0];
  70. for ( ; ; ) {
  71. if (high < q2) ;
  72. else if (low >= q2) {
  73. code_value -= q2;
  74. low -= q2;
  75. high -= q2;
  76. }
  77. else if (low >= q1 && high < q3) {
  78. code_value -= q1;
  79. low -= q1;
  80. high -= q1;
  81. }
  82. else
  83. {
  84. _SlibDebug(_VERBOSE_,
  85. ScDebugPrintf(dbg, "sv_H263SACDecode_a_symbol() code_value=%ld sacindex=%ld\n",
  86. code_value, sacindex) );
  87. break;
  88. }
  89. low = low << 1;
  90. high = (high << 1) + 1;
  91. bit_out_psc_layer(BSIn);
  92. code_value = (code_value << 1) + bit;
  93. }
  94. return ((int)sacindex-1);
  95. }
  96. /*********************************************************************
  97. *
  98. * Name: decoder_reset
  99. *
  100. * Description: Fills Decoder FIFO after a fixed word length
  101. * string has been detected.
  102. *
  103. * Input: None
  104. *
  105. * Returns: Nothing
  106. *
  107. * Side Effects: Fills Arithmetic Decoder FIFO
  108. *
  109. * Author: Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
  110. *
  111. *********************************************************************/
  112. void sv_H263SACDecoderReset(ScBitstream_t *BSIn)
  113. {
  114. int i;
  115. _SlibDebug(_VERBOSE_, ScDebugPrintf(dbg, "sv_H263SACDecoderReset() bytepos=%d\n",
  116. (int)ScBSBytePosition(BSIn)) );
  117. zerorun = 0; /* clear consecutive zero's counter */
  118. code_value = 0;
  119. low = 0;
  120. high = top;
  121. for (i = 1; i <= 16; i++) {
  122. bit_out_psc_layer(BSIn);
  123. code_value = (code_value << 1) + bit;
  124. }
  125. }
  126. /*********************************************************************
  127. *
  128. * Name: bit_out_psc_layer
  129. *
  130. * Description: Gets a bit from the Encoded Stream, Checks for
  131. * and removes any PSC emulation prevention bits
  132. * inserted at the decoder, provides 'zeros' to the
  133. * Arithmetic Decoder FIFO to allow it to finish
  134. * data prior to the next PSC. (Garbage bits)
  135. *
  136. * Input: None
  137. *
  138. * Returns: Nothing
  139. *
  140. * Side Effects: Gets a bit from the Input Data Stream
  141. *
  142. * Author: Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
  143. *
  144. *********************************************************************/
  145. static void bit_out_psc_layer(ScBitstream_t *BSIn)
  146. {
  147. if (ScBSPeekBits(BSIn, 17)!=1) /* check for startcode in Arithmetic Decoder FIFO */
  148. {
  149. _SlibDebug(_DEBUG_, ScDebugPrintf(dbg, "bit_out_psc_layer()\n") );
  150. bit = ScBSGetBit(BSIn);
  151. if (zerorun > 13) { /* if number of consecutive zeros = 14 */
  152. if (!bit) {
  153. _SlibDebug(_WARN_,
  154. ScDebugPrintf(dbg, "bit_out_psc_layer() PSC/GBSC, Header Data, or Encoded Stream Error\n") );
  155. zerorun = 1;
  156. }
  157. else { /* if there is a 'stuffing bit present */
  158. /*
  159. if (H263_DEC_trace)
  160. printf("Removing Startcode Emulation Prevention bit \n");
  161. */
  162. bit = ScBSGetBit(BSIn); /* overwrite the last bit */
  163. zerorun = !bit; /* zerorun=1 if bit is a '0' */
  164. }
  165. }
  166. else { /* if consecutive zero's not exceeded 14 */
  167. if (!bit) zerorun++;
  168. else zerorun = 0;
  169. }
  170. } /* end of if !(showbits(17)) */
  171. else {
  172. _SlibDebug(_WARN_, ScDebugPrintf(dbg, "bit_out_psc_layer() startcode found, using 'Garbage bits'\n") );
  173. bit = 0;
  174. }
  175. /*
  176. printf("lastbit = %ld bit = %ld zerorun = %ld \n", lastbit, bit, zerorun);
  177. lastbit = bit;
  178. */
  179. /* latent diagnostics */
  180. }
  181. /*********************************************************************
  182. *
  183. * SAC Encoder Module
  184. * Algorithm as specified in H263 (Annex E)
  185. *
  186. *********************************************************************/
  187. /*********************************************************************
  188. *
  189. * Name: AR_Encode
  190. *
  191. * Description: Encodes a symbol using syntax based arithmetic
  192. * coding. Algorithm specified in H.263 (Annex E).
  193. *
  194. * Input: Array holding cumulative frequency data.
  195. * Index into specific cumulative frequency array.
  196. * Static data for encoding endpoints.
  197. *
  198. * Returns: Number of bits used while encoding symbol.
  199. *
  200. * Side Effects: Modifies low, high, length and opposite_bits
  201. * variables.
  202. *
  203. *********************************************************************/
  204. int sv_H263AREncode(SvH263CompressInfo_t *H263Info, ScBitstream_t *BSOut,
  205. int index, int cumul_freq[ ])
  206. {
  207. qword length;
  208. int bitcount=0;
  209. if (index<0)
  210. return -1; /* Escape Code */
  211. length = high - low + 1;
  212. high = low - 1 + (length * cumul_freq[index]) / cumul_freq[0];
  213. low += (length * cumul_freq[index+1]) / cumul_freq[0];
  214. for ( ; ; ) {
  215. if (high < q2) {
  216. bitcount+=sv_H263bit_opp_bits(BSOut, 0);
  217. }
  218. else if (low >= q2) {
  219. bitcount+=sv_H263bit_opp_bits(BSOut, 1);
  220. low -= q2;
  221. high -= q2;
  222. }
  223. else if (low >= q1 && high < q3) {
  224. opposite_bits += 1;
  225. low -= q1;
  226. high -= q1;
  227. }
  228. else break;
  229. low *= 2;
  230. high = 2*high+1;
  231. }
  232. return bitcount;
  233. }
  234. static int sv_H263bit_opp_bits(ScBitstream_t *BSOut, int bit) /* Output a bit and the following opposite bits */
  235. {
  236. int bitcount=0;
  237. bitcount = sv_H263bit_in_psc_layer(BSOut, bit);
  238. while(opposite_bits > 0){
  239. bitcount += sv_H263bit_in_psc_layer(BSOut, !bit);
  240. opposite_bits--;
  241. }
  242. return bitcount;
  243. }
  244. /*********************************************************************
  245. *
  246. * Name: encoder_flush
  247. *
  248. * Description: Completes arithmetic coding stream before any
  249. * fixed length codes are transmitted.
  250. *
  251. * Input: None
  252. *
  253. * Returns: Number of bits used.
  254. *
  255. * Side Effects: Resets low, high, zerorun and opposite_bits
  256. * variables.
  257. *
  258. *********************************************************************/
  259. int sv_H263AREncoderFlush(SvH263CompressInfo_t *H263Info, ScBitstream_t *BSOut)
  260. {
  261. int bitcount = 0;
  262. _SlibDebug(_VERBOSE_, ScDebugPrintf(dbg, "sv_H263AREncoderFlush() bytepos=%d\n",
  263. (int)ScBSBytePosition(BSOut)) );
  264. opposite_bits++;
  265. if (low < q1) {
  266. bitcount+=sv_H263bit_opp_bits(BSOut, 0);
  267. }
  268. else {
  269. bitcount+=sv_H263bit_opp_bits(BSOut, 1);
  270. }
  271. low = 0;
  272. high = top;
  273. zerorun=0;
  274. return bitcount;
  275. }
  276. /*********************************************************************
  277. *
  278. * Name: bit_in_psc_layer
  279. *
  280. * Description: Inserts a bit into output bitstream and avoids
  281. * picture start code emulation by stuffing a one
  282. * bit.
  283. *
  284. * Input: Bit to be output.
  285. *
  286. * Returns: Nothing
  287. *
  288. * Side Effects: Updates zerorun variable.
  289. *
  290. *********************************************************************/
  291. static int sv_H263bit_in_psc_layer(ScBitstream_t *BSOut, int bit)
  292. {
  293. int bitcount = 0;
  294. if (zerorun > 13) {
  295. _SlibDebug(_DEBUG_, ScDebugPrintf(dbg,
  296. "sv_H263bit_in_psc_layer() bytepos=%d, PSC emulation...Bit stuffed\n",
  297. (int)ScBSBytePosition(BSOut)) );
  298. svH263mputb(1);
  299. bitcount++;
  300. zerorun = 0;
  301. }
  302. svH263mputb(bit);
  303. bitcount++;
  304. if (bit)
  305. zerorun = 0;
  306. else
  307. zerorun++;
  308. return bitcount;
  309. }
  310. /*********************************************************************
  311. *
  312. * Name: indexfn
  313. *
  314. * Description: Translates between symbol value and symbol
  315. * index.
  316. *
  317. * Input: Symbol value, index table, max number of
  318. * values.
  319. *
  320. * Returns: Index into cumulative frequency tables or
  321. * escape code.
  322. *
  323. * Side Effects: none
  324. *
  325. *********************************************************************/
  326. int sv_H263IndexFN(int value, int table[], int max)
  327. {
  328. int n=0;
  329. while(1) {
  330. if (table[n++]==value) return n-1;
  331. if (n>max) return -1;
  332. }
  333. }