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.

452 lines
12 KiB

  1. /*
  2. * @DEC_COPYRIGHT@
  3. */
  4. /*
  5. * HISTORY
  6. * $Log: sv_h261_cdenc.c,v $
  7. * Revision 1.1.4.3 1995/12/18 21:39:02 Karen_Dintino
  8. * Porting to NT - added casting, removed unused vars, fixed includes
  9. * [1995/12/18 21:36:50 Karen_Dintino]
  10. *
  11. * Revision 1.1.4.2 1995/09/13 14:52:04 Hans_Graves
  12. * Some code optimizations.
  13. * [1995/09/13 14:33:22 Hans_Graves]
  14. *
  15. * Revision 1.1.2.7 1995/08/15 19:14:01 Karen_Dintino
  16. * fix reentrant problem
  17. * [1995/08/15 18:35:32 Karen_Dintino]
  18. *
  19. * Revision 1.1.2.6 1995/08/04 16:32:32 Karen_Dintino
  20. * Return approp errors on end of stream
  21. * [1995/08/04 16:27:06 Karen_Dintino]
  22. *
  23. * Revision 1.1.2.5 1995/08/03 18:02:13 Karen_Dintino
  24. * Fix error handling
  25. * [1995/08/03 17:58:43 Karen_Dintino]
  26. *
  27. * Revision 1.1.2.4 1995/07/17 16:12:25 Hans_Graves
  28. * Switched compression to ScBS* bitstreaming routines.
  29. * [1995/07/17 15:45:50 Hans_Graves]
  30. *
  31. * Revision 1.1.2.3 1995/07/11 22:11:41 Karen_Dintino
  32. * Start to clean up prototypes
  33. * [1995/07/11 22:01:21 Karen_Dintino]
  34. *
  35. * Revision 1.1.2.2 1995/06/19 20:31:26 Karen_Dintino
  36. * H.261 slib codec
  37. * [1995/06/19 19:49:18 Karen_Dintino]
  38. *
  39. * $EndLog$
  40. */
  41. /*
  42. **++
  43. ** FACILITY: Workstation Multimedia (WMM) v1.0
  44. **
  45. ** FILE NAME: sv_h261_cdenc.c
  46. ** MODULE NAME:
  47. **
  48. ** MODULE DESCRIPTION:
  49. **
  50. ** DESIGN OVERVIEW:
  51. **
  52. **--
  53. */
  54. /*****************************************************************************
  55. ** Copyright (c) Digital Equipment Corporation, 1994, 1997 **
  56. ** **
  57. ** All Rights Reserved. Unpublished rights reserved under the copyright **
  58. ** laws of the United States. **
  59. ** **
  60. ** The software contained on this media is proprietary to and embodies **
  61. ** the confidential technology of Digital Equipment Corporation. **
  62. ** Possession, use, duplication or dissemination of the software and **
  63. ** media is authorized only pursuant to a valid written license from **
  64. ** Digital Equipment Corporation. **
  65. ** **
  66. ** RESTRICTED RIGHTS LEGEND Use, duplication, or disclosure by the U.S. **
  67. ** Government is subject to restrictions as set forth in Subparagraph **
  68. ** (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, as applicable. **
  69. ******************************************************************************/
  70. /* Some Modifications were done to incorporate a scaled IDCT scheme
  71. on the DecodeXXX routines. These modifications are to
  72. improve the performance -S.I.S. September 29, 1993.
  73. */
  74. /*************************************************************
  75. This file contains the routines to run-length encode the ac and dc
  76. coefficients.
  77. *************************************************************/
  78. #include <stdio.h>
  79. #include <stdlib.h>
  80. #include <math.h>
  81. #include "sv_intrn.h"
  82. #include "SC_err.h"
  83. #include "sv_h261.h"
  84. #include "proto.h"
  85. #define fgetv mgetv
  86. #define fgetb mgetb
  87. #define fputvb mputvb
  88. #define fputbb mputbb
  89. #define MYPI 3.1415926535897931
  90. #define RSQ2 0.7071067811865
  91. #define COSM1P3 1.3065629648764
  92. #define COS1M3 0.5411961001462
  93. #define COS3 0.3826834323651
  94. #define sround(x) ( (x >= 0) ? (int) (x+0.5) : (int) (x-0.5) )
  95. #define SCLAMP(x) ( (x>127) ? 127 : ((x<-128) ? -128 : x) )
  96. #define Abs(value) ( (value < 0) ? (-value) : value)
  97. /*PUBLIC*/
  98. const unsigned int tdzz[64] = {
  99. 0, 1, 5, 6, 14, 15, 27, 28,
  100. 2, 4, 7, 13, 16, 26, 29, 42,
  101. 3, 8, 12, 17, 25, 30, 41, 43,
  102. 9, 11, 18, 24, 31, 40, 44, 53,
  103. 10, 19, 23, 32, 39, 45, 52, 54,
  104. 20, 22, 33, 38, 46, 51, 55, 60,
  105. 21, 34, 37, 47, 50, 56, 59, 61,
  106. 35, 36, 48, 49, 57, 58, 62, 63};
  107. const unsigned int tzz[64] = {
  108. 0, 1, 8, 16, 9, 2, 3, 10,
  109. 17, 24, 32, 25, 18, 11, 4, 5,
  110. 12, 19, 26, 33, 40, 48, 41, 34,
  111. 27, 20, 13, 6, 7, 14, 21, 28,
  112. 35, 42, 49, 56, 57, 50, 43, 36,
  113. 29, 22, 15, 23, 30, 37, 44, 51,
  114. 58, 59, 52, 45, 38, 31, 39, 46,
  115. 53, 60, 61, 54, 47, 55, 62, 63};
  116. float static qscale[32][64];
  117. float static qs[64], invq[32];
  118. float DCIscale;
  119. /*PRIVATE*/
  120. extern int bit_set_mask[];
  121. /*
  122. extern DHUFF *T1DHuff;
  123. extern DHUFF *T2DHuff;
  124. extern EHUFF *T1EHuff;
  125. extern EHUFF *T2EHuff;
  126. */
  127. int extend_mask[] = {
  128. 0xFFFFFFFE,
  129. 0xFFFFFFFC,
  130. 0xFFFFFFF8,
  131. 0xFFFFFFF0,
  132. 0xFFFFFFE0,
  133. 0xFFFFFFC0,
  134. 0xFFFFFF80,
  135. 0xFFFFFF00,
  136. 0xFFFFFE00,
  137. 0xFFFFFC00,
  138. 0xFFFFF800,
  139. 0xFFFFF000,
  140. 0xFFFFE000,
  141. 0xFFFFC000,
  142. 0xFFFF8000,
  143. 0xFFFF0000,
  144. 0xFFFE0000,
  145. 0xFFFC0000,
  146. 0xFFF80000,
  147. 0xFFF00000
  148. };
  149. /*START*/
  150. void GenScaleMat()
  151. {
  152. BEGIN("GenScaleMat");
  153. double dbli, dblj;
  154. float dij;
  155. int i, j, k, quantindex;
  156. for(quantindex=1;quantindex<32;quantindex++) {
  157. k=0;
  158. invq[quantindex] = (float) 1.0/(float)(2.0*quantindex);
  159. for(i=0;i<8;i++) {
  160. for(j=0;j<8;j++) {
  161. dbli = MYPI*i/16.0;
  162. dblj = MYPI*j/16.0;
  163. dij = (float)(16.0*(float)(cos(dbli)*cos(dblj)));
  164. if(i==0) dij = (float)(dij/sqrt(2.0));
  165. if(j==0) dij = (float)(dij/sqrt(2.0));
  166. qs[k] = (float)(1.0/dij);
  167. qscale[quantindex][k] = (float)(1.0/(2.0*quantindex*dij));
  168. k++;
  169. }
  170. }
  171. }
  172. }
  173. /*
  174. ** Function: EncodeAC()
  175. ** Purpose: Encodes the quantized coefficient matrix input by the first
  176. ** Huffman table. The index is an offset into the matrix.
  177. */
  178. SvStatus_t EncodeAC(SvH261Info_t *H261, ScBitstream_t *bs, int index,
  179. int *matrix)
  180. {
  181. BEGIN("EncodeAC");
  182. int k,r,l,code,retval;
  183. ScBSPosition_t Start;
  184. int tempbits;
  185. Start=ScBSBitPosition(bs); /* swtellb(H261); */
  186. for(r=0,k=index-1;++k<H261_BLOCKSIZE;)
  187. {
  188. l = matrix[k];
  189. if (!l) {r++;}
  190. else
  191. {
  192. code = Abs(l) | (r << 8);
  193. if (code != HUFFMAN_ESCAPE) {retval=sv_H261HuffEncode(H261,bs,code,H261->T1EHuff);}
  194. else {retval=0;}
  195. if (!retval)
  196. {
  197. sv_H261HuffEncode(H261,bs,HUFFMAN_ESCAPE,H261->T1EHuff);
  198. ScBSPutBits(bs,r,6); /* fputvb(H261,6,r); */
  199. ScBSPutBits(bs,l,8); /* fputvb(H261,8,l); */
  200. }
  201. else
  202. {
  203. if (l < 0)
  204. ScBSPutBit(bs,1); /* fputbb(H261,1); */
  205. else
  206. ScBSPutBit(bs,0); /* fputbb(H261,0); */
  207. }
  208. r=0;
  209. H261->NumberNZ++;
  210. }
  211. }
  212. H261->CurrentBlockBits = ScBSBitPosition(bs)-Start; /* swtellb(H261)-Start */
  213. H261->CodedBlockBits+=H261->CurrentBlockBits;
  214. tempbits = sv_H261HuffEncode(H261,bs,0,H261->T1EHuff);
  215. H261->EOBBits += tempbits;
  216. H261->CurrentBlockBits += tempbits;
  217. return (NoErrors);
  218. }
  219. /*
  220. ** Function: CBPEncodeAC()
  221. ** Purpose: Encodes the AC block matrix when we know there exists a
  222. ** non-zero coefficient in the matrix. Thus the EOB cannot
  223. ** occur as the first element and we save countless bits...
  224. */
  225. SvStatus_t CBPEncodeAC(SvH261Info_t *H261, ScBitstream_t *bs,
  226. int index, int *matrix)
  227. {
  228. int k,r,l,code,ovfl;
  229. ScBSPosition_t Start;
  230. int tempbits;
  231. _SlibDebug(_DEBUG_, printf("CBPEncodeAC()") );
  232. Start=ScBSBitPosition(bs); /* Start=swtellb(H261); */
  233. for (ovfl=1, r=0, k=index-1; ++k<H261_BLOCKSIZE; )
  234. {
  235. l = matrix[k];
  236. if (!l)
  237. r++;
  238. else
  239. {
  240. code = Abs(l) | (r << 8);
  241. if (code == HUFFMAN_ESCAPE || !sv_H261HuffEncode(H261,bs,code,H261->T2EHuff))
  242. {
  243. sv_H261HuffEncode(H261,bs,HUFFMAN_ESCAPE,H261->T2EHuff);
  244. ScBSPutBits(bs,r,6); /* fputvb(H261,6,r); */
  245. ScBSPutBits(bs,l,8); /* fputvb(H261,8,l); */
  246. }
  247. else if (l < 0)
  248. ScBSPutBit(bs,1); /* fputbb(H261,1); */
  249. else
  250. ScBSPutBit(bs,0); /* fputbb(H261,0); */
  251. ovfl=0;
  252. H261->NumberNZ++;
  253. break;
  254. }
  255. }
  256. if (ovfl)
  257. {
  258. _SlibDebug(_VERIFY_, printf("CBP block without any coefficients.\n") );
  259. return(SvErrorVideoInput);
  260. }
  261. for(r=0; ++k<H261_BLOCKSIZE; )
  262. {
  263. l = matrix[k];
  264. if (!l)
  265. r++;
  266. else
  267. {
  268. code = Abs(l) | (r << 8);
  269. if (code == HUFFMAN_ESCAPE || !sv_H261HuffEncode(H261,bs,code,H261->T1EHuff))
  270. {
  271. sv_H261HuffEncode(H261,bs,HUFFMAN_ESCAPE,H261->T1EHuff);
  272. ScBSPutBits(bs, r, 6); /* fputvb(H261,6,r); */
  273. ScBSPutBits(bs, l, 8); /* fputvb(H261,8,l); */
  274. }
  275. else if (l < 0)
  276. ScBSPutBit(bs,1); /* fputbb(H261,1); */
  277. else
  278. ScBSPutBit(bs,0); /* fputbb(H261,0); */
  279. r=0;
  280. H261->NumberNZ++;
  281. }
  282. }
  283. H261->CurrentBlockBits = ScBSBitPosition(bs)-Start; /* swtellb(H261)-Start;*/
  284. H261->CodedBlockBits+=H261->CurrentBlockBits;
  285. tempbits = sv_H261HuffEncode(H261,bs,0,H261->T1EHuff);
  286. H261->EOBBits += tempbits;
  287. H261->CurrentBlockBits += tempbits;
  288. return (NoErrors);
  289. }
  290. /*
  291. ** Function: EncodeDC()
  292. ** Purpose: Encodes the coefficient input into the output stream.
  293. */
  294. void EncodeDC(SvH261Info_t *H261, ScBitstream_t *bs, int coef)
  295. {
  296. _SlibDebug(_DEBUG_, printf("EncodeDC()") );
  297. if (coef > 254)
  298. {
  299. ScBSPutBits(bs, 254, 8);
  300. H261->NumberNZ++;
  301. }
  302. else if (coef <= 1)
  303. ScBSPutBits(bs, 1, 8);
  304. else if (coef==128)
  305. {
  306. ScBSPutBits(bs, 255, 8);
  307. H261->NumberNZ++;
  308. }
  309. else
  310. {
  311. ScBSPutBits(bs, coef, 8);
  312. H261->NumberNZ++;
  313. }
  314. H261->CodedBlockBits+=8;
  315. }
  316. void InterQuant(float *tdct, int *dct, int mq)
  317. {
  318. int i;
  319. float *pqs=qscale[mq];
  320. _SlibDebug(_DEBUG_, printf("InterQuant()") );
  321. for(i=H261_BLOCKSIZE; i; i--)
  322. *dct++ = (int)((*tdct++)*(*pqs++));
  323. }
  324. void IntraQuant(float *tdct, int *dct, int mq)
  325. {
  326. int i,temp;
  327. float *pqs=&qscale[mq][1];
  328. _SlibDebug(_DEBUG_, printf("IntraQuant()") );
  329. if (tdct[0] > 0)
  330. i = (int) (tdct[0] * qscale[4][0] + 0.5);
  331. else
  332. i = (int) (tdct[0] * qscale[4][0] - 0.5);
  333. if (i>254)
  334. *dct = 254;
  335. else if (i<1)
  336. *dct = 1;
  337. else
  338. *dct = i;
  339. for(i=1, dct++, tdct++; i<H261_BLOCKSIZE; i++){
  340. if(mq < 3){
  341. temp = (int)((*tdct++)*(*pqs++));
  342. if(temp > 127) *dct++ = 127;
  343. else if (temp < -127) *dct++ = -127;
  344. else *dct++ = temp;
  345. }
  346. else *dct++ = (int)((*tdct++)*(*pqs++));
  347. }
  348. }
  349. /*
  350. ** Function: ZigzagMatrix()
  351. ** Purpose: Performs a zig-zag translation on the input imatrix
  352. ** and puts the output in omatrix.
  353. */
  354. void ZigzagMatrix(int *imatrix, int *omatrix)
  355. {
  356. const unsigned int *ptdzz=tdzz;
  357. int k;
  358. _SlibDebug(_DEBUG_, printf("ZigzagMatrix") );
  359. for(k=H261_BLOCKSIZE; k; k--)
  360. omatrix[*ptdzz++] = *imatrix++;
  361. }
  362. void Inv_Quant(int *matrix, int QuantUse, int BlockType, float *fmatrix)
  363. {
  364. int k, l, temp;
  365. float *pqs=qs+1;
  366. _SlibDebug(_DEBUG_, printf("Inv_Quant()") );
  367. /* for(mptr=fmatrix;mptr<fmatrix+H261_BLOCKSIZE;mptr++) {*mptr = 0.0;}*/
  368. if (matrix[0])
  369. {
  370. if (BlockType==1)
  371. fmatrix[0] = (float)matrix[0];
  372. else
  373. {
  374. l = matrix[0];
  375. if (QuantUse&1)
  376. temp = (l>0) ? (((l*2)+1)*QuantUse):(((l*2)-1)*QuantUse);
  377. else
  378. temp = (l>0) ? (((l*2)+1)*QuantUse-1):(((l*2)-1)*QuantUse+1);
  379. fmatrix[0] = temp*qs[0];
  380. }
  381. }
  382. else
  383. fmatrix[0] = (float)0.0;
  384. matrix++;
  385. fmatrix++;
  386. if (QuantUse&1)
  387. {
  388. for (k=1; k<64; k++)
  389. {
  390. if ((l=*matrix++)>0)
  391. *fmatrix++ = (((l<<1)+1)*QuantUse) * (*pqs++);
  392. else if (l)
  393. *fmatrix++ = (((l<<1)-1)*QuantUse) * (*pqs++);
  394. else
  395. {
  396. *fmatrix++ = (float)0.0;
  397. pqs++;
  398. }
  399. }
  400. }
  401. else
  402. {
  403. for (k=1; k<64; k++)
  404. {
  405. if ((l=*matrix++)>0)
  406. *fmatrix++ = (((l<<1)+1)*QuantUse-1) * (*pqs++);
  407. else if (l)
  408. *fmatrix++ = (((l<<1)-1)*QuantUse+1) * (*pqs++);
  409. else
  410. {
  411. *fmatrix++ = (float)0.0;
  412. pqs++;
  413. }
  414. }
  415. }
  416. }