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.

2003 lines
53 KiB

  1. /* *************************************************************************
  2. ** INTEL Corporation Proprietary Information
  3. **
  4. ** This listing is supplied under the terms of a license
  5. ** agreement with INTEL Corporation and may not be copied
  6. ** nor disclosed except in accordance with the terms of
  7. ** that agreement.
  8. **
  9. ** Copyright (c) 1995-1996 Intel Corporation.
  10. ** All Rights Reserved.
  11. **
  12. ** *************************************************************************
  13. */
  14. /******************************************************************************
  15. * e1mbenc.cpp
  16. *
  17. * DESCRIPTION:
  18. * Specific encoder compression functions.
  19. *
  20. * Routines: Prototypes in:
  21. * GOB_Q_RLE_VLC_WriteBS
  22. * MB_Quantize_RLE
  23. * ComputeCheckSum
  24. * WriteMBCheckSum
  25. */
  26. /* $Header: S:\h26x\src\enc\e1mbenc.cpv 1.47 30 Oct 1996 09:58:46 MBODART $
  27. * $Log: S:\h26x\src\enc\e1mbenc.cpv $
  28. //
  29. // Rev 1.47 30 Oct 1996 09:58:46 MBODART
  30. // Fixed assertion failure. Need to reclamp unMQuant after adding delta.
  31. //
  32. // Rev 1.46 29 Oct 1996 11:18:18 RHAZRA
  33. // Bug fix: in the IA code we previously modified MQuant on a MB basis
  34. // even if we were operating with a fixed quantizer. Now we don't
  35. //
  36. // Rev 1.45 21 Oct 1996 09:05:16 RHAZRA
  37. //
  38. // MMX integration
  39. //
  40. // Rev 1.44 21 Aug 1996 19:06:02 RHAZRA
  41. // Added RTP generatio code; fixed additional divide-by-zero possibilities.
  42. //
  43. // Rev 1.42 21 Jun 1996 10:08:34 AKASAI
  44. // Changes to e1enc.cpp, e1mbenc.cpp, ex5me.asm to support "improved
  45. // bit rate control", changing MacroBlock Quantization within a
  46. // row of MB's in addition to changing the Quantization change
  47. // between rows of macro blocks.
  48. //
  49. // ex5me.asm had a problem with SLF SWD. Brian updated asm code.
  50. //
  51. //
  52. // Rev 1.41 14 May 1996 11:41:18 AKASAI
  53. // Needed to test 0th and 1st coefficient to avoid clamping errors.
  54. //
  55. // Rev 1.40 14 May 1996 10:39:04 AKASAI
  56. // Two files changed to hopefully eliminate Quantization clamping
  57. // artifacts and to reduce the max buffer overflow case: e1enc.cpp
  58. // and e1mbenc.cpp.
  59. //
  60. // In e1mbenc.cpp when the MQuant level is < 6 I test to see if
  61. // the 0th coefficient is larger than the values representable
  62. // at that Quant level if it is I increase the Quant level until
  63. // the clamping artifact will not occur. Note: I am test only
  64. // the Oth coefficient, there is the possibility that some other
  65. // coefficient is larger but the performance trade off seems to
  66. // indicate this is good for now and if we still see clamping
  67. // artifacts we can add more testing later.
  68. //
  69. // In e1enc.cpp I modified when the Overflow types of warnings are
  70. // turn on as well as changing the rate the Quantization level
  71. // changes at.
  72. //
  73. // Rev 1.39 24 Apr 1996 12:18:22 AKASAI
  74. // Added re-compression strategy to encoder. Had to change e1enc.cpp,
  75. // e1enc.h and e1mbenc.cpp.
  76. // Basic strategy is if spending too many bits in a GOB quantize the
  77. // next GOB at a higher rate. If after compressing the frame too
  78. // many bits have been used, re-compress the last GOB at a higher
  79. // QUANT level if that still doesn't work send a "Skip" GOB.
  80. // Needed to add extra parameter to GOB+Q_RLE_VLC_WriteBS because
  81. // CalcMBQuant kept decreasing the QUANT when we were in trouble with
  82. // possibly overflowing the buffer.
  83. //
  84. // Rev 1.38 22 Apr 1996 11:02:14 AKASAI
  85. // Two files changed e1enc.cpp and e1mbenc.cpp to try and support
  86. // allowing the Quantization values to go down to 2 instead of
  87. // CLAMP to 6.
  88. // This is part 1 of implementing the re-compression (what to do
  89. // if exceed max compressed buffer size 8KBytes QCIF, 32KBytes FCIF).
  90. // Also changed in e1enc was to limit request uFrameSize to 8KB or
  91. // 32KB. Problem was if user specified too large of a datarate
  92. // request frame size would be larger than the allowed buffer size.
  93. // If you try to compress qnoise10.avi or fnoise5.avi you get an
  94. // ASSERT error until rest of re-compression is implemented.
  95. //
  96. // Rev 1.37 19 Apr 1996 14:26:28 SCDAY
  97. // Added adaptive bit usage profile (Karl's BRC changes)
  98. //
  99. // Rev 1.36 08 Jan 1996 10:11:16 DBRUCKS
  100. // add an assert
  101. //
  102. // Rev 1.35 29 Dec 1995 18:11:42 DBRUCKS
  103. //
  104. // optimize walking pCurrMB and add CLAMP_N_TO(qp,6,31)
  105. //
  106. // Rev 1.34 27 Dec 1995 16:48:06 DBRUCKS
  107. // moved incrementing InterCodeCnt from e1enc.cpp
  108. //
  109. // Rev 1.33 26 Dec 1995 17:45:18 DBRUCKS
  110. // moved statistics to e1stat
  111. //
  112. // Rev 1.32 20 Dec 1995 14:56:52 DBRUCKS
  113. // add timing stats
  114. //
  115. // Rev 1.31 18 Dec 1995 15:38:04 DBRUCKS
  116. // improve stats
  117. //
  118. // Rev 1.30 15 Dec 1995 10:53:34 AKASAI
  119. // Fixed bug that encoded the wrong type when spatial loop filter on
  120. // bug 0 MV. Was incorrectly been encoded with no spatial loop filter.
  121. // This seemed to have caused the "#" bug.
  122. //
  123. // Rev 1.29 13 Dec 1995 13:59:08 DBRUCKS
  124. // added include exutil.h
  125. // parameter change in call to cnvt_fdct_output - uses INTRA boolean instead
  126. // of blocktype
  127. //
  128. // Rev 1.28 07 Dec 1995 12:50:54 DBRUCKS
  129. // integrate Macroblock checksum fixes
  130. //
  131. // Rev 1.27 04 Dec 1995 12:12:30 DBRUCKS
  132. // Unsigned compares using MQuant and lastencoded -2 yielded
  133. // unexpected results when lastencoded was 1.
  134. //
  135. // Rev 1.26 01 Dec 1995 15:33:14 DBRUCKS
  136. //
  137. // Added the bit rate controller support. The one possibly confusing
  138. // part is that the quantizer can change in the encoder on a macro block
  139. // that is skipped or on one that does not have coefficients. In either
  140. // case the decoder is not told of the change. The decoder is told of
  141. // the change on the next macroblock that has coefficients.
  142. //
  143. // Rev 1.25 27 Nov 1995 17:53:40 DBRUCKS
  144. // add spatial loop filtering
  145. //
  146. // Rev 1.24 22 Nov 1995 17:37:34 DBRUCKS
  147. // cleanup me changes
  148. //
  149. // Rev 1.23 22 Nov 1995 15:34:52 DBRUCKS
  150. //
  151. // Motion Estimation works - but needs to be cleaned up
  152. //
  153. // Rev 1.22 17 Nov 1995 14:26:20 BECHOLS
  154. //
  155. // Made modifications so that this file can be made for ring 0.
  156. //
  157. // Rev 1.21 15 Nov 1995 14:40:34 AKASAI
  158. // Union thing change ...
  159. // (Integration point)
  160. //
  161. // Rev 1.20 01 Nov 1995 09:00:16 DBRUCKS
  162. // cleanup
  163. //
  164. // Rev 1.19 27 Oct 1995 17:21:12 DBRUCKS
  165. // fix MTYPE calc, improve var names and debug
  166. //
  167. // Rev 1.18 27 Oct 1995 15:06:30 DBRUCKS
  168. // update cnvt_fdct_output
  169. //
  170. // Rev 1.17 27 Oct 1995 14:30:36 DBRUCKS
  171. // delta frame support "coded", key frames tested
  172. //
  173. // Rev 1.15 17 Oct 1995 15:56:56 DBRUCKS
  174. // cleanup debug message
  175. //
  176. // Rev 1.14 17 Oct 1995 15:52:10 DBRUCKS
  177. //
  178. // turn off a debug message
  179. //
  180. // Rev 1.13 16 Oct 1995 11:41:44 DBRUCKS
  181. // fix the sign part of the checksum
  182. //
  183. // Rev 1.12 29 Sep 1995 10:31:02 DBRUCKS
  184. // change to use e35qrle to get the latest
  185. //
  186. // Rev 1.11 27 Sep 1995 16:53:48 DBRUCKS
  187. // move MB checksum before MB
  188. //
  189. // Rev 1.10 26 Sep 1995 13:33:14 DBRUCKS
  190. // fixed TCOEFF table 4,1 and on used earlier values
  191. //
  192. // Rev 1.9 26 Sep 1995 09:29:46 DBRUCKS
  193. // turn on MBEncodeVLC
  194. //
  195. // Rev 1.8 26 Sep 1995 09:09:24 DBRUCKS
  196. // add checksum test code
  197. //
  198. // Rev 1.7 25 Sep 1995 10:23:16 DBRUCKS
  199. //
  200. // add checksum info AND
  201. // fix the final param that is passed to MBEncodeVLC
  202. //
  203. // Rev 1.6 21 Sep 1995 20:37:56 BECHOLS
  204. // Modified the VLC tables for H261. I included a placeholder for the
  205. // sign bit, so that I can achieve an optimization in the code.
  206. //
  207. // Rev 1.5 21 Sep 1995 18:14:48 BECHOLS
  208. // Changed the initialization of the VLC tables for efficient use of memory,
  209. // and proper operation with VLC code. This is an intermediate step towards
  210. // completion, but is not operable yet.
  211. //
  212. // Rev 1.4 20 Sep 1995 17:50:18 BECHOLS
  213. //
  214. // Removed VLC_TCOEF_LAST_TBL and changed the initialization code that
  215. // use to assume 2 DWORDS to now make use of a single DWORD to conserve
  216. // memory.
  217. //
  218. // Rev 1.3 20 Sep 1995 16:34:28 BECHOLS
  219. // Moved the data declared in E1VLC.H to this module, where it is used.
  220. //
  221. // Rev 1.2 20 Sep 1995 12:39:38 DBRUCKS
  222. // turn on complete mb processing and
  223. // cleanup two routines
  224. //
  225. // Rev 1.1 18 Sep 1995 10:09:54 DBRUCKS
  226. //
  227. // activate more of the mb processing
  228. //
  229. // Rev 1.0 12 Sep 1995 18:57:16 BECHOLS
  230. // Initial revision.
  231. */
  232. #include "precomp.h"
  233. #ifdef CHECKSUM_MACRO_BLOCK
  234. static U32 ComputeCheckSum(I8 * pi8MBRunValTriplets, I8 * pi8EndAddress, I32 iBlockNumber);
  235. static void WriteMBCheckSum(U32 uCheckSum, U8 * pu8PictureStart, U8 ** ppu8BitStream, U8 * pu8BitOffset, UN unCurrentMB);
  236. #endif
  237. static I8 * MB_Quantize_RLE(I32 **DCTCoefs, I8 *MBRunValPairs, U8 * CodedBlocks, U8 BlockType, I32 QP, U32 *puChecksum);
  238. extern char string[128];
  239. /*
  240. * VLC table for TCOEFs
  241. * Table entries are size INCLUDING PLACE HOLDER FOR SIGN BIT, code.
  242. * Stored as (size, value)
  243. */
  244. int VLC_TCOEF[102*2] = {
  245. 0X0003, 0x0006, // 0
  246. 0X0005, 0x0008,
  247. 0X0006, 0x000A,
  248. 0X0008, 0x000C,
  249. 0X0009, 0x004C,
  250. 0X0009, 0x0042,
  251. 0X000B, 0x0014,
  252. 0X000D, 0x003A,
  253. 0X000D, 0x0030,
  254. 0X000D, 0x0026,
  255. 0X000D, 0x0020,
  256. 0X000E, 0x0034,
  257. 0X000E, 0x0032,
  258. 0X000E, 0x0030,
  259. 0X000E, 0x002E,
  260. 0X0004, 0x0006, // 1
  261. 0X0007, 0x000C,
  262. 0X0009, 0x004A,
  263. 0X000B, 0x0018,
  264. 0X000D, 0x0036,
  265. 0X000E, 0x002C,
  266. 0X000E, 0x002A,
  267. 0X0005, 0x000A, // 2
  268. 0X0008, 0x0008,
  269. 0X000B, 0x0016,
  270. 0X000D, 0x0028,
  271. 0X000E, 0x0028,
  272. 0X0006, 0x000E, // 3
  273. 0X0009, 0x0048,
  274. 0X000D, 0x0038,
  275. 0X000E, 0x0026,
  276. 0X0006, 0x000C, // 4
  277. 0X000B, 0x001E,
  278. 0X000D, 0x0024,
  279. 0X0007, 0x000E, // 5
  280. 0X000B, 0x0012,
  281. 0X000E, 0x0024,
  282. 0X0007, 0x000A, // 6
  283. 0X000D, 0x003C,
  284. 0X0007, 0x0008, // 7
  285. 0X000D, 0x002A,
  286. 0X0008, 0x000E, // 8
  287. 0X000D, 0x0022,
  288. 0X0008, 0x000A, // 9
  289. 0X000E, 0x0022,
  290. 0X0009, 0x004E, // 10
  291. 0X000E, 0x0020,
  292. 0X0009, 0x0046, // 11
  293. 0X0009, 0x0044, // 12
  294. 0X0009, 0x0040, // 13
  295. 0X000B, 0x001C, // 14
  296. 0X000B, 0x001A, // 15
  297. 0X000B, 0x0010, // 16
  298. 0X000D, 0x003E, // 17
  299. 0X000D, 0x0034, // 18
  300. 0X000D, 0x0032, // 19
  301. 0X000D, 0x002E, // 20
  302. 0X000D, 0x002C, // 21
  303. 0X000E, 0x003E, // 22
  304. 0X000E, 0x003C, // 23
  305. 0X000E, 0x003A, // 24
  306. 0X000E, 0x0038, // 25
  307. 0X000E, 0x0036 // 26
  308. };
  309. /*
  310. * This table lists the maximum level represented in the
  311. * VLC table for a given run. If the level exceeds the
  312. * max, then escape codes must be used to encode the
  313. * run & level.
  314. * The table entries are of the form {maxlevel, ptr to table for this run}.
  315. */
  316. T_MAXLEVEL_PTABLE TCOEF_RUN_MAXLEVEL[65] = {
  317. {15, &VLC_TCOEF[0]}, // run of 0
  318. { 7, &VLC_TCOEF[15*2]}, // run of 1
  319. { 5, &VLC_TCOEF[22*2]}, // run of 2
  320. { 4, &VLC_TCOEF[27*2]}, // run of 3
  321. { 3, &VLC_TCOEF[31*2]}, // run of 4
  322. { 3, &VLC_TCOEF[34*2]}, // run of 5
  323. { 2, &VLC_TCOEF[37*2]}, // run of 6
  324. { 2, &VLC_TCOEF[39*2]}, // run of 7
  325. { 2, &VLC_TCOEF[41*2]}, // run of 8
  326. { 2, &VLC_TCOEF[43*2]}, // run of 9
  327. { 2, &VLC_TCOEF[45*2]}, // run of 10
  328. { 1, &VLC_TCOEF[47*2]}, // run of 11
  329. { 1, &VLC_TCOEF[48*2]}, // run of 12
  330. { 1, &VLC_TCOEF[49*2]}, // run of 13
  331. { 1, &VLC_TCOEF[50*2]}, // run of 14
  332. { 1, &VLC_TCOEF[51*2]}, // run of 15
  333. { 1, &VLC_TCOEF[52*2]}, // run of 16
  334. { 1, &VLC_TCOEF[53*2]}, // run of 17
  335. { 1, &VLC_TCOEF[54*2]}, // run of 18
  336. { 1, &VLC_TCOEF[55*2]}, // run of 19
  337. { 1, &VLC_TCOEF[56*2]}, // run of 20
  338. { 1, &VLC_TCOEF[57*2]}, // run of 21
  339. { 1, &VLC_TCOEF[58*2]}, // run of 22
  340. { 1, &VLC_TCOEF[59*2]}, // run of 23
  341. { 1, &VLC_TCOEF[60*2]}, // run of 24
  342. { 1, &VLC_TCOEF[61*2]}, // run of 25
  343. { 1, &VLC_TCOEF[62*2]}, // run of 26
  344. { 0, 0}, // run of 27 not in VLC table
  345. { 0, 0}, // run of 28 not in VLC table
  346. { 0, 0}, // run of 29 not in VLC table
  347. { 0, 0}, // run of 30 not in VLC table
  348. { 0, 0}, // run of 31 not in VLC table
  349. { 0, 0}, // run of 32 not in VLC table
  350. { 0, 0}, // run of 33 not in VLC table
  351. { 0, 0}, // run of 34 not in VLC table
  352. { 0, 0}, // run of 35 not in VLC table
  353. { 0, 0}, // run of 36 not in VLC table
  354. { 0, 0}, // run of 37 not in VLC table
  355. { 0, 0}, // run of 38 not in VLC table
  356. { 0, 0}, // run of 39 not in VLC table
  357. { 0, 0}, // run of 40 not in VLC table
  358. { 0, 0}, // run of 41 not in VLC table
  359. { 0, 0}, // run of 42 not in VLC table
  360. { 0, 0}, // run of 43 not in VLC table
  361. { 0, 0}, // run of 44 not in VLC table
  362. { 0, 0}, // run of 45 not in VLC table
  363. { 0, 0}, // run of 46 not in VLC table
  364. { 0, 0}, // run of 47 not in VLC table
  365. { 0, 0}, // run of 48 not in VLC table
  366. { 0, 0}, // run of 49 not in VLC table
  367. { 0, 0}, // run of 50 not in VLC table
  368. { 0, 0}, // run of 51 not in VLC table
  369. { 0, 0}, // run of 52 not in VLC table
  370. { 0, 0}, // run of 53 not in VLC table
  371. { 0, 0}, // run of 54 not in VLC table
  372. { 0, 0}, // run of 55 not in VLC table
  373. { 0, 0}, // run of 56 not in VLC table
  374. { 0, 0}, // run of 57 not in VLC table
  375. { 0, 0}, // run of 58 not in VLC table
  376. { 0, 0}, // run of 59 not in VLC table
  377. { 0, 0}, // run of 60 not in VLC table
  378. { 0, 0}, // run of 61 not in VLC table
  379. { 0, 0}, // run of 62 not in VLC table
  380. { 0, 0}, // run of 63 not in VLC table
  381. { 0, 0} // run of 64 not in VLC table
  382. };
  383. /* VLC table for MBA
  384. * Table is stored as {number of bits, code}.
  385. * The index to the table should be the MBA value.
  386. * The zero entry is not used.
  387. */
  388. int VLC_MBA[34][2] =
  389. { {0, 0}, /* Not Used */
  390. {1, 0x1}, /* 1 */
  391. {3, 0x3}, /* 2 */
  392. {3, 0x2}, /* 3 */
  393. {4, 0x3}, /* 4 */
  394. {4, 0x2}, /* 5 */
  395. {5, 0x3}, /* 6 */
  396. {5, 0x2}, /* 7 */
  397. {7, 0x7}, /* 8 */
  398. {7, 0x6}, /* 9 */
  399. {8, 0xB}, /* 10 */
  400. {8, 0xA}, /* 11 */
  401. {8, 0x9}, /* 12 */
  402. {8, 0x8}, /* 13 */
  403. {8, 0x7}, /* 14 */
  404. {8, 0x6}, /* 15 */
  405. {10, 0x17}, /* 16 */
  406. {10, 0x16}, /* 17 */
  407. {10, 0x15}, /* 18 */
  408. {10, 0x14}, /* 19 */
  409. {10, 0x13}, /* 20 */
  410. {10, 0x12}, /* 21 */
  411. {11, 0x23}, /* 22 */
  412. {11, 0x22}, /* 23 */
  413. {11, 0x21}, /* 24 */
  414. {11, 0x20}, /* 25 */
  415. {11, 0x1F}, /* 26 */
  416. {11, 0x1E}, /* 27 */
  417. {11, 0x1D}, /* 28 */
  418. {11, 0x1C}, /* 29 */
  419. {11, 0x1B}, /* 30 */
  420. {11, 0x1A}, /* 31 */
  421. {11, 0x19}, /* 32 */
  422. {11, 0x18} /* 33 */
  423. };
  424. /* VLC table for MTYPE
  425. * Table is stored as {number of bits, code}.
  426. */
  427. int VLC_MTYPE[10][2] =
  428. { {4, 0x1}, /* Intra : TCOEFF */
  429. {7, 0x1}, /* Intra : MQUANT TCOEEF */
  430. {1, 0x1}, /* Inter : CBP TCOEFF */
  431. {5, 0x1}, /* Inter : MQUANT CBP TCOEFF */
  432. {9, 0x1}, /* Inter MC : MVD */
  433. {8, 0x1}, /* Inter MC : MVD CBP TCOEFF */
  434. {10,0x1}, /* Inter MC : MQUANT MVD CBP TCOEFF */
  435. {3, 0x1}, /* Inter MC FIL : MVD */
  436. {2, 0x1}, /* Inter MC FIL : MVD CBP TCOEFF */
  437. {6, 0x1} /* Inter MC FIL : MQUANT MVD CBP TCOEFF */
  438. };
  439. /* VLC table for CBP
  440. * Table is stored as {number of bits, code}.
  441. */
  442. int VLC_CBP[64][2] =
  443. { {0, 0}, /* Not Used - if zero it is not coded */
  444. {5, 0x0B},/* 1 */
  445. {5, 0x09},/* 2 */
  446. {6, 0x0D},/* 3 */
  447. {4, 0xD}, /* 4 */
  448. {7, 0x17},/* 5 */
  449. {7, 0x13},/* 6 */
  450. {8, 0x1F},/* 7 */
  451. {4, 0xC}, /* 8 */
  452. {7, 0x16},/* 9 */
  453. {7, 0x12},/* 10 */
  454. {8, 0x1E},/* 11 */
  455. {5, 0x13},/* 12 */
  456. {8, 0x1B},/* 13 */
  457. {8, 0x17},/* 14 */
  458. {8, 0x13},/* 15 */
  459. {4, 0xB}, /* 16 */
  460. {7, 0x15},/* 17 */
  461. {7, 0x11},/* 18 */
  462. {8, 0x1D},/* 19 */
  463. {5, 0x11},/* 20 */
  464. {8, 0x19},/* 21 */
  465. {8, 0x15},/* 22 */
  466. {8, 0x11},/* 23 */
  467. {6, 0x0F},/* 24 */
  468. {8, 0x0F},/* 25 */
  469. {8, 0x0D},/* 26 */
  470. {9, 0x03},/* 27 */
  471. {5, 0x0F},/* 28 */
  472. {8, 0x0B},/* 29 */
  473. {8, 0x07},/* 30 */
  474. {9, 0x07},/* 31 */
  475. {4, 0xA}, /* 32 */
  476. {7, 0x14},/* 33 */
  477. {7, 0x10},/* 34 */
  478. {8, 0x1C},/* 35 */
  479. {6, 0x0E},/* 36 */
  480. {8, 0x0E},/* 37 */
  481. {8, 0x0C},/* 38 */
  482. {9, 0x02},/* 39 */
  483. {5, 0x10},/* 40 */
  484. {8, 0x18},/* 41 */
  485. {8, 0x14},/* 42 */
  486. {8, 0x10},/* 43 */
  487. {5, 0x0E},/* 44 */
  488. {8, 0x0A},/* 45 */
  489. {8, 0x06},/* 46 */
  490. {9, 0x06},/* 47 */
  491. {5, 0x12},/* 48 */
  492. {8, 0x1A},/* 49 */
  493. {8, 0x16},/* 50 */
  494. {8, 0x12},/* 51 */
  495. {5, 0x0D},/* 52 */
  496. {8, 0x09},/* 53 */
  497. {8, 0x05},/* 54 */
  498. {9, 0x05},/* 55 */
  499. {5, 0x0C},/* 56 */
  500. {8, 0x08},/* 57 */
  501. {8, 0x04},/* 58 */
  502. {9, 0x04},/* 59 */
  503. {3, 0x7}, /* 60 */
  504. {5, 0x0A},/* 61 */
  505. {5, 0x08},/* 62 */
  506. {6, 0x0C},/* 63 */
  507. };
  508. /* VLC table for MVD
  509. * Table is stored as {number of bits, code}.
  510. */
  511. int VLC_MVD[32][2] =
  512. { {11, 0x19}, /* -16 & 16 */
  513. {11, 0x1B}, /* -15 & 17 */
  514. {11, 0x1D}, /* -14 & 18 */
  515. {11, 0x1F}, /* -13 & 19 */
  516. {11, 0x21}, /* -12 & 20 */
  517. {11, 0x23}, /* -11 & 21 */
  518. {10, 0x13}, /* -10 & 22 */
  519. {10, 0x15}, /* -9 & 23 */
  520. {10, 0x17}, /* -8 & 24 */
  521. { 8, 0x07}, /* -7 & 25 */
  522. { 8, 0x09}, /* -6 & 26 */
  523. { 8, 0x0B}, /* -5 & 27 */
  524. { 7, 0x07}, /* -4 & 28 */
  525. { 5, 0x03}, /* -3 & 29 */
  526. { 4, 0x3}, /* -2 & 30 */
  527. { 3, 0x3}, /* -1 */
  528. { 1, 0x1}, /* 0 */
  529. { 3, 0x2}, /* 1 */
  530. { 4, 0x2}, /* 2 & -30 */
  531. { 5, 0x02}, /* 3 & -29 */
  532. { 7, 0x06}, /* 4 & -28 */
  533. { 8, 0x0A}, /* 5 & -27 */
  534. { 8, 0x08}, /* 6 & -26 */
  535. { 8, 0x06}, /* 7 & -25 */
  536. {10, 0x16}, /* 8 & -24 */
  537. {10, 0x14}, /* 9 & -23 */
  538. {10, 0x12}, /* 10 & -22 */
  539. {11, 0x22}, /* 11 & -21 */
  540. {11, 0x20}, /* 12 & -20 */
  541. {11, 0x1E}, /* 13 & -19 */
  542. {11, 0x1C}, /* 14 & -18 */
  543. {11, 0x1A} /* 15 & -17 */
  544. };
  545. /* Table to limit quant changes through out a row of Macro Blocks */
  546. static U8 QPMaxTbl[32] =
  547. { 0, /* Not Used */
  548. 1, /* Not Used when clamp to (2,31) */
  549. 1, /* 2 */
  550. 1, /* 3 */
  551. 2, /* 4 */
  552. 2, /* 5 */
  553. 2, /* 6 */
  554. 2, /* 7 */
  555. 2, /* 8 */
  556. 2, /* 9 */
  557. 2, /* 10 */
  558. 2, /* 11 */
  559. 2, /* 12 */
  560. 2, /* 13 */
  561. 2, /* 14 */
  562. 2, /* 15 */
  563. 2, /* 16 */
  564. 2, /* 17 */
  565. 2, /* 18 */
  566. 2, /* 19 */
  567. 2, /* 20 */
  568. 2, /* 21 */
  569. 2, /* 22 */
  570. 2, /* 23 */
  571. 2, /* 24 */
  572. 2, /* 25 */
  573. 2, /* 26 */
  574. 2, /* 27 */
  575. 2, /* 28 */
  576. 2, /* 29 */
  577. 2, /* 30 */
  578. 2 /* 31 */
  579. };
  580. /* Table to limit Quant changes between Rows of Marco Blocks */
  581. extern U8 MaxChangeRowMBTbl[32];
  582. /*****************************************************************************
  583. *
  584. * GOB_Q_RLE_VLC_WriteBS
  585. *
  586. * Quantize and RLE each macroblock, then VLC and write to stream
  587. */
  588. void GOB_Q_RLE_VLC_WriteBS(
  589. T_H263EncoderCatalog * EC,
  590. I32 *piDCTCoefs,
  591. U8 **ppu8BitStream,
  592. U8 *pu8BitOffset,
  593. UN unStartingMB,
  594. UN unGQuant,
  595. BOOL bOverFlowWarningFlag,
  596. BOOL bRTPHeader, //RTP: switch
  597. U32 uGOBNumber, // RTP: info
  598. U8 u8QPMin
  599. )
  600. {
  601. T_MBlockActionStream *pCurrMB = NULL;
  602. T_MBlockActionStream *pLastMB = NULL;
  603. int iMBIndex;
  604. int iLastMBIndex = -1;
  605. UN unCurrentMB;
  606. U32 uCheckSum;
  607. UN unMBA;
  608. UN unLastEncodedMBA=0; // RTP: information
  609. UN unLastCodedMB = 0; // RTP: information
  610. UN unCBP;
  611. UN unMQuant;
  612. UN unLastEncodedMQuant;
  613. UN unMType;
  614. UN bWriteTCOEFF;
  615. UN bWriteMVD;
  616. UN bWriteMQuant;
  617. I8 MBRunValSign[65*3*6], *pi8EndAddress, *rvs;
  618. T_MBlockActionStream *pMBActionStream = EC->pU8_MBlockActionStream;
  619. int bIntraBlock;
  620. int inPrecedingHMV;
  621. int inPrecedingVMV;
  622. int inHDelta;
  623. int inVDelta;
  624. U32 uCumFrmSize;
  625. U32 uBlockCount;
  626. ENC_BITSTREAM_INFO * pBSInfo = &EC->BSInfo;
  627. UN unMQuantLast;
  628. U32 SWDmax[3] = {0,0,0};
  629. U32 SWDmin[3] = {65536,65536,65536};
  630. U32 SWDrange[3] = {0,0,0};
  631. U32 SWDSum[3] = {0,0,0};
  632. U32 SWDNum[3] = {0,0,0};
  633. double SWDAvg[3] = {0.0,0.0,0.0};
  634. double Step, Delta;
  635. int QPMax;
  636. int NeedClamp=0;
  637. int irow;
  638. U8 SaveQuants[3];
  639. UN unSaveMQuant;
  640. unMQuant = unGQuant;
  641. unMQuantLast = unMQuant; // save last MQuant so can reset if needed
  642. /* initially it should be the same because the GOB header
  643. * included the GQuant.
  644. */
  645. unLastEncodedMQuant = unMQuant;
  646. unSaveMQuant = unGQuant;
  647. SaveQuants[0] = unSaveMQuant;
  648. /* New code to modify Quant inside a row of MB based on SWD */
  649. /* Loop through each macroblock of the GOB to find min and max SWD
  650. */
  651. pCurrMB = &pMBActionStream[unStartingMB];
  652. for(irow = 0; irow < 3; irow++)
  653. {
  654. for(iMBIndex = irow*11 ; iMBIndex < (irow+1)*11; iMBIndex++, pLastMB = pCurrMB++)
  655. {
  656. if (pCurrMB->BlockType != INTRABLOCK)
  657. {
  658. // ASSERT(pCurrMB->SWD >= 0); Always True
  659. SWDSum[irow] += pCurrMB->SWD;
  660. SWDNum[irow]++;
  661. if (pCurrMB->SWD > SWDmax[irow])
  662. SWDmax[irow] = pCurrMB->SWD;
  663. if (pCurrMB->SWD < SWDmin[irow])
  664. SWDmin[irow] = pCurrMB->SWD;
  665. }
  666. }
  667. }
  668. SWDrange[0] = SWDmax[0] - SWDmin[0];
  669. SWDrange[1] = SWDmax[1] - SWDmin[1];
  670. SWDrange[2] = SWDmax[2] - SWDmin[2];
  671. if (SWDNum[0] != 0)
  672. SWDAvg[0] = (double) SWDSum[0] / SWDNum[0];
  673. else
  674. SWDAvg[0] = 0.0;
  675. if (SWDNum[1] != 0)
  676. SWDAvg[1] = (double) SWDSum[1] / SWDNum[1];
  677. else
  678. SWDAvg[1] = 0.0;
  679. if (SWDNum[2] != 0)
  680. SWDAvg[2] = (double) SWDSum[2] / SWDNum[2];
  681. else
  682. SWDAvg[2] = 0.0;
  683. QPMax = unGQuant + QPMaxTbl[unGQuant];
  684. if (QPMax > 31)
  685. QPMax = 32;
  686. if ((SWDAvg[0] - SWDmin[0]) != 0)
  687. Step = (double) (QPMax - unGQuant)/(SWDAvg[0] - SWDmin[0]);
  688. else
  689. Step = 0.0;
  690. /* Loop through each macroblock of the GOB.
  691. */
  692. pLastMB = NULL;
  693. pCurrMB = &pMBActionStream[unStartingMB];
  694. for(iMBIndex = 0 ; iMBIndex < 33; iMBIndex++, pLastMB = pCurrMB++)
  695. {
  696. unCurrentMB = unStartingMB + (unsigned int)iMBIndex;
  697. #ifdef DEBUG_ENC
  698. wsprintf(string, "MB #%d: QP=%d", unCurrentMB, unMQuant);
  699. trace(string);
  700. #endif
  701. if (bRTPHeader)
  702. {
  703. H261RTP_MBUpdateBsInfo(EC,
  704. pCurrMB,
  705. unLastEncodedMQuant,
  706. (U32 )unLastEncodedMBA,
  707. uGOBNumber,
  708. *ppu8BitStream,
  709. (U32) *pu8BitOffset,
  710. unCurrentMB,
  711. unLastCodedMB
  712. );
  713. }
  714. unMQuant = unMQuantLast; // reset MQuant in case needed to
  715. // to raise on previous MB to avoid
  716. // Quant clamping artifact.
  717. /* Look to update the Quant on each new row.
  718. */
  719. if (EC->bBitRateControl && ((iMBIndex == 11) || (iMBIndex == 22)))
  720. {
  721. /* Calculate number of bytes used in frame so far.
  722. */
  723. uCumFrmSize = *ppu8BitStream - EC->pU8_BitStream;
  724. unMQuant = CalcMBQUANT(&(EC->BRCState), EC->uBitUsageProfile[unCurrentMB], EC->uBitUsageProfile[EC->NumMBs], uCumFrmSize, EC->PictureHeader.PicCodType);
  725. QPMax = unMQuant + QPMaxTbl[unMQuant];
  726. if (QPMax > 31)
  727. QPMax = 32;
  728. if ((SWDAvg[iMBIndex/11] - SWDmin[iMBIndex/11]) != 0)
  729. Step = (double) (QPMax - unMQuant)/(SWDAvg[iMBIndex/11] - SWDmin[iMBIndex/11]);
  730. else
  731. Step = 0.0;
  732. EC->uBitUsageProfile[unCurrentMB] = uCumFrmSize;
  733. if (bOverFlowWarningFlag)
  734. {
  735. DBOUT("DON'T CHANGE QUANT SET unMQuant = unGQuant");
  736. unMQuant = unGQuant;
  737. }
  738. else if ((int)unMQuant > ((int)unLastEncodedMQuant + MaxChangeRowMBTbl[unGQuant]))
  739. {
  740. DBOUT("Slowing MQuant increase + [1-4]");
  741. unMQuant = unLastEncodedMQuant + MaxChangeRowMBTbl[unMQuant];
  742. }
  743. else if ((int)unMQuant < ((int)unLastEncodedMQuant -2))
  744. {
  745. DBOUT("Slowing MQuant decrease to -2");
  746. unMQuant = unLastEncodedMQuant -2;
  747. }
  748. //CLAMP_N_TO(unMQuant,6,31);
  749. if (EC->BRCState.uTargetFrmSize == 0)
  750. {
  751. CLAMP_N_TO(unMQuant,6,31);
  752. }
  753. else
  754. {
  755. CLAMP_N_TO(unMQuant, u8QPMin, 31);
  756. }
  757. #ifdef DEBUG_BRC
  758. wsprintf(string,"At MB %d MQuant=%d", unCurrentMB, unMQuant);
  759. DBOUT(string);
  760. #endif
  761. #ifdef DEBUG_RECOMPRESS
  762. wsprintf(string,"At MB %d MQuant=%d uCumFrmSize=%d", unCurrentMB, unMQuant,uCumFrmSize*8);
  763. DBOUT(string);
  764. //trace(string);
  765. #endif
  766. //EC->uQP_cumulative += unMQuant;
  767. //EC->uQP_count++;
  768. unSaveMQuant = unMQuant;
  769. if (iMBIndex == 11)
  770. SaveQuants[1] = unSaveMQuant;
  771. else
  772. SaveQuants[2] = unSaveMQuant;
  773. }
  774. /* new MB Quant code */
  775. if (pCurrMB->BlockType != INTRABLOCK)
  776. {
  777. if (EC->BRCState.uTargetFrmSize != 0)
  778. {
  779. if (pCurrMB->SWD >= SWDAvg[iMBIndex/11])
  780. {
  781. Delta = (double) -1.0 * ((double) (pCurrMB->SWD - SWDAvg[iMBIndex/11]) * Step);
  782. if (Delta < -2.0)
  783. {
  784. Delta = -2.0;
  785. NeedClamp++;
  786. }
  787. }
  788. else
  789. {
  790. Delta = (double) (SWDAvg[iMBIndex/11] - pCurrMB->SWD)*Step;
  791. }
  792. }
  793. else
  794. Delta = 0.0;
  795. if (Delta > 0.0)
  796. {
  797. unMQuant = unSaveMQuant + (int) (Delta);
  798. /* Need to clamp again, but only worry about upper limit */
  799. if (unMQuant > 31)
  800. unMQuant = 31;
  801. }
  802. else
  803. {
  804. unMQuant = unSaveMQuant + (int) (Delta - 0.5);
  805. /* Need to clamp again, but only worry about lower limit */
  806. if (EC->BRCState.uTargetFrmSize == 0)
  807. {
  808. if (unMQuant < 6)
  809. unMQuant = 6;
  810. }
  811. else
  812. {
  813. if (unMQuant < 2)
  814. unMQuant = 2;
  815. }
  816. }
  817. }
  818. /* end new stuff */
  819. /* Quantize and RLE each block in the macroblock, skipping empty blocks as denoted by pu8CodedBlocks.
  820. * If any more blocks are empty after quantization then the appropriate pu8CodedBlocks bit is cleared.
  821. */
  822. //ASSERT(unMQuant >= 6 && unMQuant <= 31); /* CLAMP_N_TO(var,6,31) */
  823. if (EC->BRCState.uTargetFrmSize == 0)
  824. {
  825. ASSERT(unMQuant >= 6 && unMQuant <= 31); /* CLAMP_N_TO(var,6,31) */
  826. }
  827. else
  828. {
  829. ASSERT(unMQuant >= 2 && unMQuant <= 31); /* CLAMP_N_TO(var,6,31) */
  830. }
  831. /* Check iDCTCoefs to see if need to raise quant level to avoid
  832. * clamping artifacts.
  833. */
  834. // first block is at piDCTCoefs
  835. // second block is at piDCTCoefs + 0x80 and so on
  836. // coefficients are unsigned shorts
  837. // first coefficient is at 6 bytes, 3 words
  838. // second coefficient is at 38 bytes, 19 words
  839. // third coefficient is at 4 bytes, 2 words
  840. // forth coefficient is at 36 bytes, 18 words
  841. unMQuantLast = unMQuant;
  842. if (unMQuant < 6)
  843. {
  844. I8 iBlockNum;
  845. U8 u8Bitmask = 1;
  846. I32 * ptmpiDCTCoefs = piDCTCoefs;
  847. int coef0, coef1;
  848. int biggestcoefval = -2048;
  849. int smallestcoefval = 2048;
  850. #ifdef DEBUG_QUANT
  851. wsprintf(string,"At MB %d MQuant=%d", unCurrentMB, unMQuant);
  852. DBOUT(string);
  853. //trace(string);
  854. #endif
  855. for(iBlockNum = 0; iBlockNum < 6; iBlockNum++, u8Bitmask <<= 1)
  856. {
  857. /* Skip this block if not coded.
  858. */
  859. if( (pCurrMB->CodedBlocks & u8Bitmask) == 0)
  860. {
  861. continue;
  862. }
  863. if(IsIntraBlock(pCurrMB->BlockType)) // if Intra
  864. {
  865. coef0 = ((int)*((U16*)ptmpiDCTCoefs+3)) >> 4;
  866. }
  867. else
  868. {
  869. coef0 = ((int)(*((U16*)ptmpiDCTCoefs+3) - 0x8000) ) >> 4;
  870. }
  871. coef1 = ((int)(*((U16*)ptmpiDCTCoefs+19) - 0x8000)) >> 4;
  872. #ifdef DEBUG_QUANT
  873. wsprintf(string,"At Block %d 0 = %x %d", iBlockNum,coef0,coef0);
  874. //DBOUT(string);
  875. //trace(string);
  876. #endif
  877. if (coef0 > biggestcoefval)
  878. {
  879. biggestcoefval = coef0;
  880. }
  881. if (coef1 > biggestcoefval)
  882. {
  883. biggestcoefval = coef1;
  884. }
  885. if (coef0 < smallestcoefval)
  886. {
  887. smallestcoefval = coef0;
  888. }
  889. if (coef1 < smallestcoefval)
  890. {
  891. smallestcoefval = coef1;
  892. }
  893. ptmpiDCTCoefs += 32;
  894. }
  895. #ifdef DEBUG_QUANT
  896. wsprintf(string,"biggest = %x %d, smallest = %x %d",
  897. biggestcoefval, biggestcoefval, smallestcoefval, smallestcoefval);
  898. DBOUT(string);
  899. // trace(string);
  900. #endif
  901. if (unMQuant == 5) {
  902. if ((biggestcoefval > 1275) || (smallestcoefval < -1275))
  903. unMQuant = 6;
  904. }
  905. else if (unMQuant == 4) {
  906. if ((biggestcoefval > 1275) || (smallestcoefval < -1275))
  907. unMQuant = 6;
  908. else if ((biggestcoefval > 1019) || (smallestcoefval < -1019))
  909. unMQuant = 5;
  910. }
  911. else if (unMQuant == 3) {
  912. if ((biggestcoefval > 1275) || (smallestcoefval < -1275))
  913. unMQuant = 6;
  914. else if ((biggestcoefval > 1019) || (smallestcoefval < -1019))
  915. unMQuant = 5;
  916. else if ((biggestcoefval > 765) || (smallestcoefval < -765))
  917. unMQuant = 4;
  918. }
  919. else {
  920. if ((biggestcoefval > 1275) || (smallestcoefval < -1275))
  921. unMQuant = 6;
  922. else if ((biggestcoefval > 1019) || (smallestcoefval < -1019))
  923. unMQuant = 5;
  924. else if ((biggestcoefval > 765) || (smallestcoefval < -765))
  925. unMQuant = 4;
  926. else if ((biggestcoefval > 509) || (smallestcoefval < -509))
  927. unMQuant = 3;
  928. }
  929. #ifdef DEBUG_QUANT
  930. wsprintf(string,"At MB %d MQuant=%d", unCurrentMB, unMQuant);
  931. DBOUT(string);
  932. //trace(string);
  933. #endif
  934. }
  935. /* This is the place to trace how Quant on a MB bases is varying
  936. */
  937. EC->uQP_cumulative += unMQuant;
  938. EC->uQP_count++;
  939. pi8EndAddress = MB_Quantize_RLE(
  940. &piDCTCoefs,
  941. (I8 *) MBRunValSign,
  942. &(pCurrMB->CodedBlocks),
  943. pCurrMB->BlockType,
  944. unMQuant,
  945. &uCheckSum
  946. );
  947. pBSInfo->uQuantsUsedOnBlocks[unMQuant] += 6;
  948. bWriteMVD = (pCurrMB->BlkY1.PHMV != 0) ||
  949. (pCurrMB->BlkY1.PVMV != 0) ||
  950. (IsSLFBlock(pCurrMB->BlockType)) ;
  951. if (IsInterBlock(pCurrMB->BlockType))
  952. {
  953. /* Check if the Inter block is not coded?
  954. */
  955. if ( ((pCurrMB->CodedBlocks & 0x3f) == 0) &&
  956. (! bWriteMVD) )
  957. {
  958. #ifdef DEBUG_MBLK
  959. wsprintf(string, "Inter MB (index=#%d) has neither Coeff nor MV - skipping", unCurrentMB);
  960. DBOUT(string);
  961. #endif
  962. #ifdef FORCE_STUFFING
  963. PutBits(FIELDVAL_MBA_STUFFING, FIELDLEN_MBA_STUFFING, ppu8CurBitStream, pu8BitOffset);
  964. #endif
  965. continue;
  966. }
  967. }
  968. #ifdef CHECKSUM_MACRO_BLOCK
  969. /* Write a checksum before all coded blocks
  970. */
  971. WriteMBCheckSum(uCheckSum, EC->pU8_BitStream,ppu8BitStream, pu8BitOffset, unCurrentMB);
  972. #endif
  973. /* Calculate the MB header information
  974. */
  975. unMBA = iMBIndex - iLastMBIndex;
  976. iLastMBIndex = iMBIndex;
  977. unLastEncodedMBA = unMBA;
  978. unLastCodedMB = iMBIndex;
  979. /* Note: The calculation of whether to write MQuant is done after
  980. * skipping macro blocks in order to handle the case that the 11th
  981. * or 22nd macro blocks are skipped. If they are skipped then
  982. * the next macro block will be used to write the new quant value.
  983. */
  984. if(IsIntraBlock(pCurrMB->BlockType))
  985. {
  986. ASSERT(pCurrMB->BlockType == INTRABLOCK);
  987. if (EC->PictureHeader.PicCodType != INTRAPIC)
  988. {
  989. pCurrMB->InterCodeCnt = ((U8)unCurrentMB)&0x7;
  990. }
  991. bIntraBlock = 1;
  992. unCBP = 0; /* Never write CBP for Intra blocks */
  993. uBlockCount = 6;
  994. bWriteTCOEFF = 1; /* Always include TCOEFF for Intra blocks */
  995. /* Since we always have coefficients for Intra MBs we can always update
  996. * the MQuant value.
  997. */
  998. bWriteMQuant = (unMQuant != unLastEncodedMQuant);
  999. unLastEncodedMQuant = unMQuant;
  1000. unMType = 0 + bWriteMQuant; /* Calculate MTYPE */
  1001. bWriteMVD = 0; /* No motion vectors for INTRA */
  1002. }
  1003. else
  1004. {
  1005. ASSERT(IsInterBlock(pCurrMB->BlockType));
  1006. bIntraBlock = 0;
  1007. unCBP = (pCurrMB->CodedBlocks & 0x1) << 5; /* x0 0000 */
  1008. unCBP |= (pCurrMB->CodedBlocks & 0x2) << 3; /* 0x 0000 */
  1009. unCBP |= (pCurrMB->CodedBlocks & 0x4) << 1; /* 00 x000 */
  1010. unCBP |= (pCurrMB->CodedBlocks & 0x8) >> 1; /* 00 0x00 */
  1011. unCBP |= (pCurrMB->CodedBlocks & 0x10) >> 3; /* 00 00x0 */
  1012. unCBP |= (pCurrMB->CodedBlocks & 0x20) >> 5; /* 00 000x */
  1013. uBlockCount = 0;
  1014. if (unCBP & 0x1) uBlockCount++;
  1015. if (unCBP & 0x2) uBlockCount++;
  1016. if (unCBP & 0x4) uBlockCount++;
  1017. if (unCBP & 0x8) uBlockCount++;
  1018. if (unCBP & 0x10) uBlockCount++;
  1019. if (unCBP & 0x20) uBlockCount++;
  1020. /* Increment the count if it is transmitted
  1021. * "should be forcibly updated at least once every
  1022. * 132 times it is transmitted" 3.4
  1023. */
  1024. if (uBlockCount != 0 )
  1025. {
  1026. pCurrMB->InterCodeCnt++;
  1027. }
  1028. bWriteTCOEFF = (unCBP != 0);
  1029. if (bWriteTCOEFF)
  1030. {
  1031. /* We can only update the MQuant value when we have coefficients
  1032. */
  1033. bWriteMQuant = (unMQuant != unLastEncodedMQuant);
  1034. unLastEncodedMQuant = unMQuant;
  1035. }
  1036. else
  1037. {
  1038. bWriteMQuant = 0;
  1039. }
  1040. #ifdef CHECKSUM_MACRO_BLOCK
  1041. /* Either there are coefficients or the checksum should equal zero
  1042. */
  1043. ASSERT(bWriteTCOEFF || uCheckSum == 0);
  1044. #endif
  1045. /* Calculate MType
  1046. */
  1047. unMType = 1;
  1048. if (bWriteMVD)
  1049. {
  1050. unMType += 3;
  1051. if (IsSLFBlock(pCurrMB->BlockType))
  1052. {
  1053. unMType += 3;
  1054. }
  1055. }
  1056. unMType += bWriteTCOEFF;
  1057. unMType += bWriteMQuant;
  1058. ASSERT(unMType > 1 && unMType < 10);
  1059. }
  1060. ASSERT(unMQuant >= 1 && unMQuant <= 31);
  1061. ASSERT(uBlockCount <= 6);
  1062. pBSInfo->uQuantsTransmittedOnBlocks[unMQuant] += uBlockCount;
  1063. if (bWriteMVD)
  1064. {
  1065. /* Find the preceding motion vectors
  1066. */
  1067. if ( (unMBA != 1) || /* skipped one or more MB */
  1068. ((unCurrentMB % 11) == 0) ) /* first MB in each row */
  1069. {
  1070. inPrecedingHMV = 0;
  1071. inPrecedingVMV = 0;
  1072. }
  1073. else
  1074. {
  1075. inPrecedingHMV = pLastMB->BlkY1.PHMV;
  1076. inPrecedingVMV = pLastMB->BlkY1.PVMV;
  1077. }
  1078. /* adjust vectors:
  1079. */
  1080. inHDelta = pCurrMB->BlkY1.PHMV - inPrecedingHMV;
  1081. ASSERT((inHDelta & 0x1) == 0);
  1082. ASSERT((inHDelta >> 1) == (inHDelta / 2));
  1083. inHDelta >>= 1; /* Adjust to integer pels */
  1084. if(inHDelta > 15) /* Adjust to the range of -16...+15 */
  1085. inHDelta -= 32;
  1086. if(inHDelta < -16)
  1087. inHDelta += 32;
  1088. inHDelta = inHDelta + 16; /* 0 is at offset 16 */
  1089. inVDelta = pCurrMB->BlkY1.PVMV - inPrecedingVMV;
  1090. ASSERT((inVDelta & 0x1) == 0);
  1091. ASSERT((inVDelta >> 1) == (inVDelta / 2));
  1092. inVDelta >>= 1;
  1093. if(inVDelta > 15)
  1094. inVDelta -= 32;
  1095. if(inVDelta < -16)
  1096. inVDelta += 32;
  1097. inVDelta = inVDelta + 16;
  1098. #ifndef RING0
  1099. #ifdef DEBUG_PRINTMV
  1100. {
  1101. char buf132[132];
  1102. int iLength;
  1103. iLength = wsprintf(buf132, "MB # %d :: H MVD = %d; index = %d :: V MVD = %d; index = %d", unCurrentMB,
  1104. pCurrMB->BlkY1.PHMV / 2, inHDelta,
  1105. pCurrMB->BlkY1.PVMV / 2, inVDelta);
  1106. DBOUT(buf132);
  1107. ASSERT(iLength < 132);
  1108. }
  1109. #endif
  1110. #endif
  1111. }
  1112. else
  1113. {
  1114. /* MBs without MVD need to have zero motion vectors because of
  1115. * Rule 3) under 4.2.3.4
  1116. */
  1117. pCurrMB->BlkY1.PHMV = 0;
  1118. pCurrMB->BlkY1.PVMV = 0;
  1119. }
  1120. /* we should only have MQuant if we have coefficients
  1121. */
  1122. if (bWriteMQuant)
  1123. {
  1124. ASSERT(bWriteTCOEFF);
  1125. }
  1126. /* we should only have CBP if we have coefficients
  1127. */
  1128. if (unCBP)
  1129. {
  1130. ASSERT(bWriteTCOEFF);
  1131. ASSERT(uBlockCount > 0);
  1132. }
  1133. /* Write the MacroBlock Header
  1134. */
  1135. #ifndef RING0
  1136. #ifdef DEBUG_MBLK
  1137. {
  1138. int iLength;
  1139. char buf180[180];
  1140. iLength = wsprintf(buf180, "Enc #%d: MBType=%ld unNextMQuant=%d MQuant=%ld bWriteMVD=%d MVDH=%ld MVDV=%ld CBP=%ld",
  1141. (int) unCurrentMB,
  1142. unMType,
  1143. (int) bWriteMQuant,
  1144. unMQuant,
  1145. (int) bWriteMVD,
  1146. pCurrMB->BlkY1.PHMV / 2,
  1147. pCurrMB->BlkY1.PVMV / 2,
  1148. unCBP);
  1149. DBOUT(buf180);
  1150. ASSERT(iLength < 180);
  1151. }
  1152. #endif
  1153. #endif
  1154. /* MBA
  1155. */
  1156. PutBits(VLC_MBA[unMBA][1], VLC_MBA[unMBA][0], ppu8BitStream, pu8BitOffset);
  1157. /* MTYPE
  1158. */
  1159. pBSInfo->uMTypeCount[unMType]++;
  1160. pBSInfo->uBlockCount[unMType] += uBlockCount;
  1161. PutBits(VLC_MTYPE[unMType][1], VLC_MTYPE[unMType][0], ppu8BitStream, pu8BitOffset);
  1162. /* MQUANT
  1163. */
  1164. if (bWriteMQuant)
  1165. {
  1166. ASSERT(unMQuant > 0 && unMQuant < 32); /* 4.2.2.3 */
  1167. PutBits((int)unMQuant, FIELDLEN_MQUANT, ppu8BitStream, pu8BitOffset);
  1168. }
  1169. /* MVD
  1170. */
  1171. if (bWriteMVD)
  1172. {
  1173. ASSERT(inHDelta >= 0 && inHDelta < 32);
  1174. ASSERT(inVDelta >= 0 && inVDelta < 32);
  1175. PutBits(VLC_MVD[inHDelta][1], VLC_MVD[inHDelta][0], ppu8BitStream, pu8BitOffset);
  1176. PutBits(VLC_MVD[inVDelta][1], VLC_MVD[inVDelta][0], ppu8BitStream, pu8BitOffset);
  1177. }
  1178. /* CBP
  1179. */
  1180. if (unCBP != 0)
  1181. {
  1182. PutBits(VLC_CBP[unCBP][1], VLC_CBP[unCBP][0], ppu8BitStream, pu8BitOffset);
  1183. }
  1184. /* TCOEFF
  1185. */
  1186. if (bWriteTCOEFF)
  1187. {
  1188. /*
  1189. * Encode intra DC and all run/val pairs.
  1190. */
  1191. rvs = MBRunValSign;
  1192. MBEncodeVLC(
  1193. &rvs,
  1194. NULL,
  1195. pCurrMB->CodedBlocks,
  1196. ppu8BitStream,
  1197. pu8BitOffset,
  1198. bIntraBlock,
  1199. FALSE);
  1200. }
  1201. } /* for iMBIndex */
  1202. } /* end of GOB_Q_RLE_VLC_WriteBS() */
  1203. void GOB_VLC_WriteBS(
  1204. T_H263EncoderCatalog * EC,
  1205. I8 *pMBRVS_Luma,
  1206. I8 *pMBRVS_Chroma,
  1207. U8 **ppu8BitStream,
  1208. U8 *pu8BitOffset,
  1209. UN unGQuant,
  1210. UN unStartingMB,
  1211. BOOL bRTPHeader, //RTP: switch
  1212. U32 uGOBNumber // RTP: info
  1213. )
  1214. {
  1215. T_MBlockActionStream *pCurrMB = NULL;
  1216. T_MBlockActionStream *pLastMB = NULL;
  1217. int iMBIndex;
  1218. int iLastMBIndex = -1;
  1219. UN unCurrentMB;
  1220. UN unMBA;
  1221. UN unLastEncodedMBA=0; // RTP: information
  1222. UN unLastCodedMB = 0; // RTP: information
  1223. UN unCBP;
  1224. UN unMQuant;
  1225. UN unLastEncodedMQuant;
  1226. UN unMType;
  1227. UN bWriteTCOEFF;
  1228. UN bWriteMVD;
  1229. UN bWriteMQuant;
  1230. // I8 MBRunValSign[65*3*6], *pi8EndAddress, *rvs;
  1231. T_MBlockActionStream *pMBActionStream = EC->pU8_MBlockActionStream;
  1232. int bIntraBlock;
  1233. int inPrecedingHMV;
  1234. int inPrecedingVMV;
  1235. int inHDelta;
  1236. int inVDelta;
  1237. // U32 uCumFrmSize;
  1238. U32 uBlockCount;
  1239. ENC_BITSTREAM_INFO * pBSInfo = &EC->BSInfo;
  1240. unMQuant = unGQuant;
  1241. unLastEncodedMQuant = unGQuant;
  1242. /* Loop through each macroblock of the GOB.
  1243. */
  1244. pLastMB = NULL;
  1245. pCurrMB = &pMBActionStream[unStartingMB];
  1246. for(iMBIndex = 0 ; iMBIndex < 33; iMBIndex++, pLastMB = pCurrMB++)
  1247. {
  1248. unCurrentMB = unStartingMB + (unsigned int)iMBIndex;
  1249. #ifdef DEBUG_ENC
  1250. wsprintf(string, "MB #%d: QP=%d", unCurrentMB, unMQuant);
  1251. trace(string);
  1252. #endif
  1253. if (bRTPHeader)
  1254. {
  1255. H261RTP_MBUpdateBsInfo(EC,
  1256. pCurrMB,
  1257. unLastEncodedMQuant,
  1258. (U32 )unLastEncodedMBA,
  1259. uGOBNumber,
  1260. *ppu8BitStream,
  1261. (U32) *pu8BitOffset,
  1262. unCurrentMB,
  1263. unLastCodedMB
  1264. );
  1265. }
  1266. EC->uQP_cumulative += unMQuant;
  1267. EC->uQP_count++;
  1268. bWriteMVD = (pCurrMB->BlkY1.PHMV != 0) ||
  1269. (pCurrMB->BlkY1.PVMV != 0) ||
  1270. (IsSLFBlock(pCurrMB->BlockType)) ;
  1271. if (IsInterBlock(pCurrMB->BlockType))
  1272. {
  1273. /* Check if the Inter block is not coded?
  1274. */
  1275. if ( ((pCurrMB->CodedBlocks & 0x3f) == 0) &&
  1276. (! bWriteMVD) )
  1277. {
  1278. #ifdef DEBUG_MBLK
  1279. wsprintf(string, "Inter MB (index=#%d) has neither Coeff nor MV - skipping", unCurrentMB);
  1280. DBOUT(string);
  1281. #endif
  1282. #ifdef FORCE_STUFFING
  1283. PutBits(FIELDVAL_MBA_STUFFING, FIELDLEN_MBA_STUFFING, ppu8CurBitStream, pu8BitOffset);
  1284. #endif
  1285. continue;
  1286. }
  1287. }
  1288. #ifdef CHECKSUM_MACRO_BLOCK
  1289. /* Write a checksum before all coded blocks
  1290. */
  1291. WriteMBCheckSum(uCheckSum, EC->pU8_BitStream,ppu8BitStream, pu8BitOffset, unCurrentMB);
  1292. #endif
  1293. /* Calculate the MB header information
  1294. */
  1295. unMBA = iMBIndex - iLastMBIndex;
  1296. iLastMBIndex = iMBIndex;
  1297. unLastEncodedMBA = unMBA;
  1298. unLastCodedMB = iMBIndex;
  1299. /* Note: The calculation of whether to write MQuant is done after
  1300. * skipping macro blocks in order to handle the case that the 11th
  1301. * or 22nd macro blocks are skipped. If they are skipped then
  1302. * the next macro block will be used to write the new quant value.
  1303. */
  1304. if(IsIntraBlock(pCurrMB->BlockType))
  1305. {
  1306. ASSERT(pCurrMB->BlockType == INTRABLOCK);
  1307. if (EC->PictureHeader.PicCodType != INTRAPIC)
  1308. {
  1309. pCurrMB->InterCodeCnt = ((U8)unCurrentMB)&0x7;
  1310. }
  1311. bIntraBlock = 1;
  1312. unCBP = 0; /* Never write CBP for Intra blocks */
  1313. uBlockCount = 6;
  1314. bWriteTCOEFF = 1; /* Always include TCOEFF for Intra blocks */
  1315. /* Since we always have coefficients for Intra MBs we can always update
  1316. * the MQuant value.
  1317. */
  1318. //bWriteMQuant = (unMQuant != unLastEncodedMQuant);
  1319. //unLastEncodedMQuant = unMQuant;
  1320. bWriteMQuant=0;
  1321. unMType = 0; // + bWriteMQuant; /* Calculate MTYPE */
  1322. bWriteMVD = 0; /* No motion vectors for INTRA */
  1323. }
  1324. else
  1325. {
  1326. ASSERT(IsInterBlock(pCurrMB->BlockType));
  1327. bIntraBlock = 0;
  1328. unCBP = (pCurrMB->CodedBlocks & 0x1) << 5; /* x0 0000 */
  1329. unCBP |= (pCurrMB->CodedBlocks & 0x2) << 3; /* 0x 0000 */
  1330. unCBP |= (pCurrMB->CodedBlocks & 0x4) << 1; /* 00 x000 */
  1331. unCBP |= (pCurrMB->CodedBlocks & 0x8) >> 1; /* 00 0x00 */
  1332. unCBP |= (pCurrMB->CodedBlocks & 0x10) >> 3; /* 00 00x0 */
  1333. unCBP |= (pCurrMB->CodedBlocks & 0x20) >> 5; /* 00 000x */
  1334. uBlockCount = 0;
  1335. if (unCBP & 0x1) uBlockCount++;
  1336. if (unCBP & 0x2) uBlockCount++;
  1337. if (unCBP & 0x4) uBlockCount++;
  1338. if (unCBP & 0x8) uBlockCount++;
  1339. if (unCBP & 0x10) uBlockCount++;
  1340. if (unCBP & 0x20) uBlockCount++;
  1341. /* Increment the count if it is transmitted
  1342. * "should be forcibly updated at least once every
  1343. * 132 times it is transmitted" 3.4
  1344. */
  1345. if (uBlockCount != 0 )
  1346. {
  1347. pCurrMB->InterCodeCnt++;
  1348. }
  1349. bWriteTCOEFF = (unCBP != 0);
  1350. bWriteMQuant = 0;
  1351. #ifdef CHECKSUM_MACRO_BLOCK
  1352. /* Either there are coefficients or the checksum should equal zero
  1353. */
  1354. ASSERT(bWriteTCOEFF || uCheckSum == 0);
  1355. #endif
  1356. /* Calculate MType
  1357. */
  1358. unMType = 1;
  1359. if (bWriteMVD)
  1360. {
  1361. unMType += 3;
  1362. if (IsSLFBlock(pCurrMB->BlockType))
  1363. {
  1364. unMType += 3;
  1365. }
  1366. }
  1367. unMType += bWriteTCOEFF;
  1368. unMType += bWriteMQuant;
  1369. ASSERT(unMType > 1 && unMType < 10);
  1370. }
  1371. ASSERT(unMQuant >= 1 && unMQuant <= 31);
  1372. ASSERT(uBlockCount <= 6);
  1373. pBSInfo->uQuantsTransmittedOnBlocks[unMQuant] += uBlockCount;
  1374. if (bWriteMVD)
  1375. {
  1376. /* Find the preceding motion vectors
  1377. */
  1378. if ( (unMBA != 1) || /* skipped one or more MB */
  1379. ((unCurrentMB % 11) == 0) ) /* first MB in each row */
  1380. {
  1381. inPrecedingHMV = 0;
  1382. inPrecedingVMV = 0;
  1383. }
  1384. else
  1385. {
  1386. inPrecedingHMV = pLastMB->BlkY1.PHMV;
  1387. inPrecedingVMV = pLastMB->BlkY1.PVMV;
  1388. }
  1389. /* adjust vectors:
  1390. */
  1391. inHDelta = pCurrMB->BlkY1.PHMV - inPrecedingHMV;
  1392. ASSERT((inHDelta & 0x1) == 0);
  1393. ASSERT((inHDelta >> 1) == (inHDelta / 2));
  1394. inHDelta >>= 1; /* Adjust to integer pels */
  1395. if(inHDelta > 15) /* Adjust to the range of -16...+15 */
  1396. inHDelta -= 32;
  1397. if(inHDelta < -16)
  1398. inHDelta += 32;
  1399. inHDelta = inHDelta + 16; /* 0 is at offset 16 */
  1400. inVDelta = pCurrMB->BlkY1.PVMV - inPrecedingVMV;
  1401. ASSERT((inVDelta & 0x1) == 0);
  1402. ASSERT((inVDelta >> 1) == (inVDelta / 2));
  1403. inVDelta >>= 1;
  1404. if(inVDelta > 15)
  1405. inVDelta -= 32;
  1406. if(inVDelta < -16)
  1407. inVDelta += 32;
  1408. inVDelta = inVDelta + 16;
  1409. #ifndef RING0
  1410. #ifdef DEBUG_PRINTMV
  1411. {
  1412. char buf132[132];
  1413. int iLength;
  1414. iLength = wsprintf(buf132, "MB # %d :: H MVD = %d; index = %d :: V MVD = %d; index = %d", unCurrentMB,
  1415. pCurrMB->BlkY1.PHMV / 2, inHDelta,
  1416. pCurrMB->BlkY1.PVMV / 2, inVDelta);
  1417. DBOUT(buf132);
  1418. ASSERT(iLength < 132);
  1419. }
  1420. #endif
  1421. #endif
  1422. }
  1423. else
  1424. {
  1425. /* MBs without MVD need to have zero motion vectors because of
  1426. * Rule 3) under 4.2.3.4
  1427. */
  1428. pCurrMB->BlkY1.PHMV = 0;
  1429. pCurrMB->BlkY1.PVMV = 0;
  1430. }
  1431. /* we should only have MQuant if we have coefficients
  1432. */
  1433. if (bWriteMQuant)
  1434. {
  1435. ASSERT(bWriteTCOEFF);
  1436. }
  1437. /* we should only have CBP if we have coefficients
  1438. */
  1439. if (unCBP)
  1440. {
  1441. ASSERT(bWriteTCOEFF);
  1442. ASSERT(uBlockCount > 0);
  1443. }
  1444. /* Write the MacroBlock Header
  1445. */
  1446. #ifndef RING0
  1447. #ifdef DEBUG_MBLK
  1448. {
  1449. int iLength;
  1450. char buf180[180];
  1451. iLength = wsprintf(buf180,
  1452. "Enc #%d: MBType=%ld bWriteMQuant=%ld MQuant=%ld bWriteMVD=%d MVDH=%ld MVDV=%ld CBP=%ld",
  1453. (int) unCurrentMB,
  1454. unMType,
  1455. (int) bWriteMQuant,
  1456. unMQuant,
  1457. (int) bWriteMVD,
  1458. pCurrMB->BlkY1.PHMV / 2,
  1459. pCurrMB->BlkY1.PVMV / 2,
  1460. unCBP);
  1461. DBOUT(buf180);
  1462. ASSERT(iLength < 180);
  1463. }
  1464. #endif
  1465. #endif
  1466. /* MBA
  1467. */
  1468. PutBits(VLC_MBA[unMBA][1], VLC_MBA[unMBA][0], ppu8BitStream, pu8BitOffset);
  1469. /* MTYPE
  1470. */
  1471. pBSInfo->uMTypeCount[unMType]++;
  1472. pBSInfo->uBlockCount[unMType] += uBlockCount;
  1473. PutBits(VLC_MTYPE[unMType][1], VLC_MTYPE[unMType][0], ppu8BitStream, pu8BitOffset);
  1474. /* MQUANT
  1475. */
  1476. if (bWriteMQuant)
  1477. {
  1478. ASSERT(unMQuant > 0 && unMQuant < 32); /* 4.2.2.3 */
  1479. PutBits((int)unMQuant, FIELDLEN_MQUANT, ppu8BitStream, pu8BitOffset);
  1480. }
  1481. /* MVD
  1482. */
  1483. if (bWriteMVD)
  1484. {
  1485. ASSERT(inHDelta >= 0 && inHDelta < 32);
  1486. ASSERT(inVDelta >= 0 && inVDelta < 32);
  1487. PutBits(VLC_MVD[inHDelta][1], VLC_MVD[inHDelta][0], ppu8BitStream, pu8BitOffset);
  1488. PutBits(VLC_MVD[inVDelta][1], VLC_MVD[inVDelta][0], ppu8BitStream, pu8BitOffset);
  1489. }
  1490. /* CBP
  1491. */
  1492. if (unCBP != 0)
  1493. {
  1494. PutBits(VLC_CBP[unCBP][1], VLC_CBP[unCBP][0], ppu8BitStream, pu8BitOffset);
  1495. }
  1496. /* TCOEFF
  1497. */
  1498. if (bWriteTCOEFF)
  1499. {
  1500. /*
  1501. * Encode intra DC and all run/val pairs.
  1502. */
  1503. MBEncodeVLC(
  1504. &pMBRVS_Luma,
  1505. &pMBRVS_Chroma,
  1506. pCurrMB->CodedBlocks,
  1507. ppu8BitStream,
  1508. pu8BitOffset,
  1509. bIntraBlock,
  1510. 1);
  1511. }
  1512. } /* for iMBIndex */
  1513. } /* end of GOB_VLC_WriteBS() */
  1514. /*****************************************************************************
  1515. *
  1516. * MB_Quantize_RLE
  1517. *
  1518. * Takes the list of coefficient pairs from the DCT routine and returns a list
  1519. * of Run/Level/Sign triples (each 1 byte). The end of the run/level/sign
  1520. * triples for a block is signalled by an illegal combination (TBD).
  1521. */
  1522. static I8 * MB_Quantize_RLE(
  1523. I32 ** ppiDCTCoefs,
  1524. I8 * pi8MBRunValTriplets,
  1525. U8 * pu8CodedBlocks,
  1526. U8 u8BlockType,
  1527. I32 iQP,
  1528. U32 * puCheckSum
  1529. )
  1530. {
  1531. I32 iBlockNumber;
  1532. U8 u8Bitmask = 1;
  1533. I8 * pi8EndAddress;
  1534. U32 uCheckSum;
  1535. #ifdef DEBUG_DCT
  1536. int iDCTArray[64];
  1537. #endif
  1538. /*
  1539. * Loop through all 6 blocks of macroblock.
  1540. */
  1541. uCheckSum = 0;
  1542. for(iBlockNumber = 0; iBlockNumber < 6; iBlockNumber++, u8Bitmask <<= 1)
  1543. {
  1544. #ifdef DEBUG_ENC
  1545. wsprintf(string, "Block #%d", iBlockNumber);
  1546. trace(string);
  1547. #endif
  1548. /* Skip this block if not coded.
  1549. */
  1550. if( (*pu8CodedBlocks & u8Bitmask) == 0)
  1551. {
  1552. continue;
  1553. }
  1554. #ifdef DEBUG_DCT
  1555. cnvt_fdct_output((unsigned short *) *ppiDCTCoefs, iDCTArray, IsIntraBlock(u8BlockType));
  1556. #endif
  1557. /*
  1558. * Quantize and run-length encode a block.
  1559. */
  1560. pi8EndAddress = QUANTRLE(*ppiDCTCoefs, pi8MBRunValTriplets, iQP, (I32)u8BlockType);
  1561. #ifdef DEBUG_ENC
  1562. I8 * pi8;
  1563. for(pi8 = pi8MBRunValTriplets; pi8 < pi8EndAddress; pi8+=3)
  1564. {
  1565. wsprintf(string, "(%u, %u, %d)", (unsigned char)*pi8, (unsigned char)*(pi8+1), (int)*(pi8+2) );
  1566. trace(string);
  1567. }
  1568. #endif
  1569. #ifdef CHECKSUM_MACRO_BLOCK
  1570. uCheckSum += ComputeCheckSum(pi8MBRunValTriplets, pi8EndAddress, iBlockNumber);
  1571. #endif
  1572. /* Clear coded block bit for this block.
  1573. */
  1574. if ( pi8EndAddress == pi8MBRunValTriplets)
  1575. {
  1576. ASSERT(u8BlockType != INTRABLOCK) /* should have at least INTRADC in an INTRA blck */
  1577. *pu8CodedBlocks &= ~u8Bitmask;
  1578. }
  1579. else if ( (pi8EndAddress == (pi8MBRunValTriplets+3)) && (u8BlockType == INTRABLOCK) )
  1580. {
  1581. *pu8CodedBlocks &= ~u8Bitmask;
  1582. pi8MBRunValTriplets = pi8EndAddress;
  1583. }
  1584. else
  1585. {
  1586. pi8MBRunValTriplets = pi8EndAddress;
  1587. *pi8MBRunValTriplets = -1; /* Assign an illegal run to signal end of block. */
  1588. pi8MBRunValTriplets += 3; /* Increment to the next triple. */
  1589. }
  1590. /* Increment DCT Coefficient pointer to next block.
  1591. */
  1592. *ppiDCTCoefs += 32;
  1593. }
  1594. *puCheckSum = uCheckSum;
  1595. return pi8MBRunValTriplets;
  1596. } /* end MB_Quantize_RLE() */
  1597. void InitVLC(void)
  1598. {
  1599. int i;
  1600. int run, level;
  1601. /*
  1602. * initialize INTRADC fixed length code table.
  1603. */
  1604. for(i = 1; i < 254; i++)
  1605. {
  1606. FLC_INTRADC[i] = i;
  1607. }
  1608. FLC_INTRADC[0] = 1;
  1609. FLC_INTRADC[128] = 255;
  1610. FLC_INTRADC[254] = 254;
  1611. FLC_INTRADC[255] = 254;
  1612. /*
  1613. * Initialize tcoef tables.
  1614. */
  1615. for(i = 0; i < (NUMBER_OF_TCOEF_ENTRIES); i++) {
  1616. VLC_TCOEF_TBL[i] = 0x0000FFFF;
  1617. }
  1618. for(run = 0; run < 64; run++) {
  1619. for(level = 1; level <= TCOEF_RUN_MAXLEVEL[run].maxlevel; level++) {
  1620. DWORD dwSize, dwCode;
  1621. dwSize = *(TCOEF_RUN_MAXLEVEL[run].ptable + (level - 1) * 2);
  1622. dwSize <<= 16;
  1623. dwCode = *(TCOEF_RUN_MAXLEVEL[run].ptable + (level - 1) * 2 + 1);
  1624. VLC_TCOEF_TBL[run + (level - 1) * 64] = dwCode;
  1625. VLC_TCOEF_TBL[run + (level - 1) * 64] |= dwSize;
  1626. } // end of for level
  1627. } // end of for run
  1628. } // InitVLC.
  1629. #ifdef CHECKSUM_MACRO_BLOCK
  1630. /*****************************************************************************
  1631. *
  1632. * ComputeCheckSum
  1633. *
  1634. * Compute the checksum for this block
  1635. */
  1636. static U32 ComputeCheckSum(
  1637. I8 * pi8MBRunValTriplets,
  1638. I8 * pi8EndAddress,
  1639. I32 iBlockNumber)
  1640. {
  1641. I8 * pi8;
  1642. U32 uRun;
  1643. U32 uLevel;
  1644. I32 iSign;
  1645. U32 uSignBit;
  1646. U32 uCheckSum = 0;
  1647. #if CHECKSUM_MACRO_BLOCK_DETAIL
  1648. char buf80[80];
  1649. int iLength;
  1650. #endif
  1651. for (pi8 = pi8MBRunValTriplets; pi8 < pi8EndAddress; )
  1652. {
  1653. uRun = (U32)*pi8++;
  1654. uLevel = (U32)(U8)*pi8++;
  1655. iSign = (I32)*pi8++;
  1656. if (iSign == 0)
  1657. {
  1658. uSignBit = 0;
  1659. }
  1660. else
  1661. {
  1662. ASSERT(iSign == 0xFFFFFFFF);
  1663. uSignBit = 1;
  1664. }
  1665. uCheckSum += uRun << 24;
  1666. uCheckSum += uLevel << 8;
  1667. uCheckSum += uSignBit;
  1668. #ifdef CHECKSUM_MACRO_BLOCK_DETAIL
  1669. iLength = wsprintf(buf80,"Block=%d R=0x%x L=0x%x S=%d, CheckSum=0x%x", iBlockNumber, uRun, uLevel, uSignBit, uCheckSum);
  1670. DBOUT(buf80);
  1671. ASSERT(iLength < 80);
  1672. #endif
  1673. }
  1674. return uCheckSum;
  1675. } /* end ComputeCheckSum() */
  1676. /*****************************************************************************
  1677. *
  1678. * WriteMBCheckSum
  1679. *
  1680. * Write the macro block checksum information.
  1681. */
  1682. static void WriteMBCheckSum(
  1683. U32 uCheckSum,
  1684. U8 * pu8PictureStart,
  1685. U8 ** ppu8BitStream,
  1686. U8 * pu8BitOffset,
  1687. UN unCurrentMB)
  1688. {
  1689. U32 uBytes;
  1690. U32 uTempBytes;
  1691. U8 u8Bits;
  1692. U8 u8TempBits;
  1693. UN unCount;
  1694. UN unKey;
  1695. UN unData;
  1696. uBytes = *ppu8BitStream - pu8PictureStart;
  1697. u8Bits = *pu8BitOffset;
  1698. /* Add in the space for the checksum info (eleven 8-bit fields + 12 "1"s + MBA stuffing)
  1699. */
  1700. uBytes += 12;
  1701. u8Bits += 4 + FIELDLEN_MBA_STUFFING;
  1702. /* Adjust bits to < 7
  1703. */
  1704. while (u8Bits > 7)
  1705. {
  1706. u8Bits -= 8;
  1707. uBytes++;
  1708. }
  1709. #if _DEBUG
  1710. #if CHECKSUM_MACRO_BLOCK_DETAIL
  1711. {
  1712. char buf80[80];
  1713. int iLength;
  1714. iLength = wsprintf(buf80,"MB=%d CHK=0x%x Bytes=%ld Bits=%d", unCurrentMB, uCheckSum, uBytes, (int) u8Bits);
  1715. DBOUT(buf80);
  1716. ASSERT(iLength < 80);
  1717. }
  1718. #endif
  1719. #endif
  1720. /* Write the MBASTUFFING value
  1721. */
  1722. PutBits(FIELDVAL_MBA_STUFFING, FIELDLEN_MBA_STUFFING, ppu8BitStream, pu8BitOffset);
  1723. /* Write the data to the bitstream
  1724. */
  1725. /* Key - a value of 1 in an 8-bit field following a "1"
  1726. */
  1727. unKey = 1;
  1728. PutBits(1, 1, ppu8BitStream, pu8BitOffset);
  1729. PutBits(unKey, 8, ppu8BitStream, pu8BitOffset);
  1730. /* Count - number of bits after the Count field.
  1731. */
  1732. unCount = 9*8 + 10*1; /* nine 8-bit value and 10 "1"s. */
  1733. PutBits(1, 1, ppu8BitStream, pu8BitOffset);
  1734. PutBits(unCount, 8, ppu8BitStream, pu8BitOffset);
  1735. /* Bytes - high to low bytes
  1736. */
  1737. unData = (UN) ((uBytes >> 24) & 0xFF);
  1738. PutBits(1, 1, ppu8BitStream, pu8BitOffset);
  1739. PutBits(unData, 8, ppu8BitStream, pu8BitOffset);
  1740. unData = (UN) ((uBytes >> 16) & 0xFF);
  1741. PutBits(1, 1, ppu8BitStream, pu8BitOffset);
  1742. PutBits(unData, 8, ppu8BitStream, pu8BitOffset);
  1743. unData = (UN) ((uBytes >> 8) & 0xFF);
  1744. PutBits(1, 1, ppu8BitStream, pu8BitOffset);
  1745. PutBits(unData, 8, ppu8BitStream, pu8BitOffset);
  1746. unData = (UN) (uBytes & 0xFF);
  1747. PutBits(1, 1, ppu8BitStream, pu8BitOffset);
  1748. PutBits(unData, 8, ppu8BitStream, pu8BitOffset);
  1749. /* Bits
  1750. */
  1751. unData = (UN) u8Bits;
  1752. PutBits(1, 1, ppu8BitStream, pu8BitOffset);
  1753. PutBits(unData, 8, ppu8BitStream, pu8BitOffset);
  1754. /* Checksum - high to low bytes
  1755. */
  1756. unData = (UN) ((uCheckSum >> 24) & 0xFF);
  1757. PutBits(1, 1, ppu8BitStream, pu8BitOffset);
  1758. PutBits(unData, 8, ppu8BitStream, pu8BitOffset);
  1759. unData = (UN) ((uCheckSum >> 16) & 0xFF);
  1760. PutBits(1, 1, ppu8BitStream, pu8BitOffset);
  1761. PutBits(unData, 8, ppu8BitStream, pu8BitOffset);
  1762. unData = (UN) ((uCheckSum >> 8) & 0xFF);
  1763. PutBits(1, 1, ppu8BitStream, pu8BitOffset);
  1764. PutBits(unData, 8, ppu8BitStream, pu8BitOffset);
  1765. unData = (UN) (uCheckSum & 0xFF);
  1766. PutBits(1, 1, ppu8BitStream, pu8BitOffset);
  1767. PutBits(unData, 8, ppu8BitStream, pu8BitOffset);
  1768. /* Trailing 1 bit to avoid start code duplication.
  1769. */
  1770. PutBits(1, 1, ppu8BitStream, pu8BitOffset);
  1771. /* Check that the pointers are correct
  1772. */
  1773. uTempBytes = *ppu8BitStream - pu8PictureStart;
  1774. u8TempBits = *pu8BitOffset;
  1775. while (u8TempBits > 7)
  1776. {
  1777. u8TempBits -= 8;
  1778. uTempBytes++;
  1779. }
  1780. ASSERT(uTempBytes == uBytes);
  1781. ASSERT(u8TempBits == u8Bits);
  1782. } /* end WriteMBCheckSum() */
  1783. #endif