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.

891 lines
27 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. *
  16. * d1mblk.cpp
  17. *
  18. * DESCRIPTION:
  19. * Decoder macro block functions
  20. *
  21. * Routines: Prototypes in:
  22. * H263DecodeMBHeader d1dec.h
  23. * H263DecodeMBData d1dec.h
  24. */
  25. // $Header: S:\h26x\src\dec\d1mblk.cpv 1.23 20 Dec 1996 16:58:06 RHAZRA $
  26. // $Log: S:\h26x\src\dec\d1mblk.cpv $
  27. //
  28. // Rev 1.23 20 Dec 1996 16:58:06 RHAZRA
  29. // Fixed bitstream docoding for the case where MB stuffing is inserted
  30. // between MBs. This was identified by a PTEL bitstream. This fix needs
  31. // to be verified with our other tests.
  32. //
  33. // Rev 1.22 16 Dec 1996 14:41:46 RHAZRA
  34. //
  35. // Changed a bitstream error ASSERT to a bonafide error
  36. //
  37. // Rev 1.21 18 Nov 1996 17:12:22 MBODART
  38. // Replaced all debug message invocations with Active Movie's DbgLog.
  39. //
  40. // Rev 1.20 07 Nov 1996 15:44:08 SCDAY
  41. //
  42. // Added MMX_ClipAndScale to replace Raj's glue code
  43. //
  44. // Rev 1.19 04 Nov 1996 10:28:10 RHAZRA
  45. // Changed the IDCT scaling table to be a DWORD table (with rounding
  46. // factored in) that is declared as a static.
  47. //
  48. // Rev 1.18 31 Oct 1996 08:58:28 SCDAY
  49. // Raj added support for MMX decoder
  50. //
  51. // Rev 1.17 26 Sep 1996 12:35:06 RHAZRA
  52. // Forced the decoder to use the IA version of VLD_RLD_IQ routine even
  53. // when MMX is on (since we don't have a corresponding MMX routine ... yet)
  54. //
  55. // Rev 1.16 05 Aug 1996 11:00:26 MBODART
  56. //
  57. // H.261 decoder rearchitecture:
  58. // Files changed: d1gob.cpp, d1mblk.{cpp,h}, d1dec.{cpp,h},
  59. // filelist.261, h261_32.mak
  60. // New files: d1bvriq.cpp, d1idct.cpp
  61. // Obsolete files: d1block.cpp
  62. // Work still to be done:
  63. // Update h261_mf.mak
  64. // Optimize uv pairing in d1bvriq.cpp and d1idct.cpp
  65. // Fix checksum code (it doesn't work now)
  66. // Put back in decoder stats
  67. //
  68. // Rev 1.15 18 Mar 1996 17:02:12 AKASAI
  69. //
  70. // Added pragma code_seg("IACODE2") and changed the timing statistics.
  71. // At one point changed GET_VAR_BITS into subroutine to save code
  72. // space but it didn't so left it as a macro.
  73. //
  74. // Rev 1.14 26 Dec 1995 17:42:14 DBRUCKS
  75. // changed bTimerIsOn to bTimingThisFrame
  76. //
  77. // Rev 1.13 26 Dec 1995 12:50:00 DBRUCKS
  78. //
  79. // fix copyright
  80. // add timing code
  81. // comment out define of DEBUG_MBLK
  82. //
  83. // Rev 1.12 05 Dec 1995 10:19:46 SCDAY
  84. //
  85. // Added assembler version of Spatial Loop Filter
  86. //
  87. // Rev 1.11 03 Nov 1995 11:44:30 AKASAI
  88. //
  89. // Changed the processing of MB checksum and MBA stuffing. Changed
  90. // GET_VAR_BITS & GET_GT8_BITS for how to detect MBA stuffing code.
  91. //
  92. // Rev 1.10 01 Nov 1995 13:43:48 AKASAI
  93. //
  94. // Added support for loop filter. New routines call LpFilter,
  95. // BlockAddSpecial and BlockCopySpecial.
  96. //
  97. // Rev 1.9 27 Oct 1995 18:17:20 AKASAI
  98. //
  99. // Put in fix "hack" to keep the block action stream pointers
  100. // in sync between d1dec and d1mblk. With skip macro blocks some
  101. // macroblocks were being processed multiple times. Still a problem
  102. // when gob ends with a skip macroblock.
  103. //
  104. // Rev 1.8 26 Oct 1995 15:36:28 SCDAY
  105. //
  106. // Delta frames partially working -- changed main loops to accommodate
  107. // skipped macroblocks by detecting next startcode
  108. //
  109. // Rev 1.7 17 Oct 1995 11:28:56 SCDAY
  110. // Added error message if (MBA stuffing code found && Checksum not enabled)
  111. //
  112. // Rev 1.6 16 Oct 1995 16:28:02 AKASAI
  113. // Fixed bug when CHECKSUM_MACRO_BLOCK_DETAIL & CHECKSUM_MACRO_BLOCK are
  114. // both defined.
  115. //
  116. // Rev 1.5 16 Oct 1995 13:53:24 SCDAY
  117. //
  118. // Added macroblock level checksum
  119. //
  120. // Rev 1.4 06 Oct 1995 15:32:54 SCDAY
  121. //
  122. // Integrated with latest AKK d1block
  123. //
  124. // Rev 1.3 22 Sep 1995 14:48:46 SCDAY
  125. //
  126. // added more mblock header and data decoding
  127. //
  128. // Rev 1.2 20 Sep 1995 09:52:22 SCDAY
  129. //
  130. // eliminated a warning
  131. //
  132. // Rev 1.1 19 Sep 1995 15:24:10 SCDAY
  133. //
  134. // added H261 MBA parsing
  135. //
  136. // Rev 1.0 11 Sep 1995 13:51:52 SCDAY
  137. // Initial revision.
  138. //
  139. // Rev 1.11 25 Aug 1995 09:16:32 DBRUCKS
  140. // add ifdef DEBUG_MBLK
  141. //
  142. // Rev 1.10 23 Aug 1995 19:12:02 AKASAI
  143. // Fixed gNewTAB_CBPY table building. Was using 8 as mask instead of 0xf.
  144. //
  145. // Rev 1.9 18 Aug 1995 15:03:22 CZHU
  146. //
  147. // Output more error message when DecodeBlock returns error.
  148. //
  149. // Rev 1.8 16 Aug 1995 14:26:54 CZHU
  150. //
  151. // Changed DWORD adjustment back to byte oriented reading.
  152. //
  153. // Rev 1.7 15 Aug 1995 09:54:18 DBRUCKS
  154. // improve stuffing handling and add debug msg
  155. //
  156. // Rev 1.6 14 Aug 1995 18:00:40 DBRUCKS
  157. // add chroma parsing
  158. //
  159. // Rev 1.5 11 Aug 1995 17:47:58 DBRUCKS
  160. // cleanup
  161. //
  162. // Rev 1.4 11 Aug 1995 16:12:28 DBRUCKS
  163. // add ptr check to MB data
  164. //
  165. // Rev 1.3 11 Aug 1995 15:10:58 DBRUCKS
  166. // finish INTRA mb header parsing and callblock
  167. //
  168. // Rev 1.2 03 Aug 1995 14:30:26 CZHU
  169. // Take block level operations out to d3block.cpp
  170. //
  171. // Rev 1.1 02 Aug 1995 10:21:12 CZHU
  172. // Added asm codes for VLD of TCOEFF, inverse quantization, run-length decode.
  173. //
  174. // Rev 1.0 31 Jul 1995 13:00:08 DBRUCKS
  175. // Initial revision.
  176. //
  177. // Rev 1.2 31 Jul 1995 11:45:42 CZHU
  178. // changed the parameter list
  179. //
  180. // Rev 1.1 28 Jul 1995 16:25:52 CZHU
  181. //
  182. // Added per block decoding framework.
  183. //
  184. // Rev 1.0 28 Jul 1995 15:20:16 CZHU
  185. // Initial revision.
  186. //Block level decoding for H.26x decoder
  187. #include "precomp.h" // rearch idct
  188. /*****************************************************************************
  189. *
  190. * GET_VAR_BITS
  191. *
  192. * Read a variable number of bits using a lookup table.
  193. *
  194. * The input count should be the number of bits used to index the table.
  195. * The output count is the number of bits in that symbol.
  196. *
  197. * The table should be initialized such that all don't care symbols match to
  198. * the same value. Thus if the table is indexed by 6-bits a two bit symbol
  199. * 01XX XX will be used to initialize all entries 0100 00 -> 0111 11. These
  200. * entries will include an 8-bit length in the least significant byte.
  201. *
  202. * uCount - IN
  203. * fpu8 - IN and OUT
  204. * uWork - IN and OUT
  205. * uBitsReady - IN and OUT
  206. * uResult - OUT
  207. * uCode - OUT
  208. * fpTable - IN
  209. */
  210. #define GET_VAR_BITS(uCount, fpu8, uWork, uBitsReady, uResult, uCode, uBitCount, fpTable) { \
  211. while (uBitsReady < uCount) { \
  212. uWork <<= 8; \
  213. uBitsReady += 8; \
  214. uWork |= *fpu8++; \
  215. } \
  216. /* calculate how much to shift off */ \
  217. /* and get the code */ \
  218. uCode = uBitsReady - uCount; \
  219. uCode = (uWork >> uCode); \
  220. /* read the data */ \
  221. uResult = fpTable[uCode]; \
  222. /* count of bits used */ \
  223. /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ \
  224. /* H.261 tables are reverse order from H.263 */ \
  225. /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ \
  226. uBitCount = uResult & 0xff00; \
  227. uBitCount >>= 8; \
  228. /* bits remaining */ \
  229. uBitsReady = uBitsReady - uBitCount; \
  230. /* special case for stuffing processing */ \
  231. /* if (uBitsReady < 0) */ \
  232. /* kluged to test for negative */ \
  233. if (uBitsReady > 33) \
  234. /* if (bStuffing) */ \
  235. { \
  236. uWork <<= 8; \
  237. uBitsReady += 8; \
  238. uWork |= *fpu8++; \
  239. } \
  240. /* end special case for stuffing */ \
  241. uWork &= GetBitsMask[uBitsReady]; \
  242. }
  243. #define GET_GT8_BITS(uCount, fpu8, uWork, uBitsReady, uResult, uCode, uBitCount, fpTable) { \
  244. while (uBitsReady < uCount) { \
  245. uWork <<= 8; \
  246. uBitsReady += 8; \
  247. uWork |= *fpu8++; \
  248. } \
  249. /* calculate how much to shift off */ \
  250. /* and get the code */ \
  251. uCode = uBitsReady - uCount; \
  252. uCode = (uWork >> uCode); \
  253. /* read the data */ \
  254. uResult = fpTable[uCode]; \
  255. /* count of bits used */ \
  256. /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ \
  257. /* H.261 tables are reverse order from H.263 */ \
  258. /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ \
  259. uBitCount = uResult & 0xff00; \
  260. if ((uBitCount & 0x8000) == 0) /* if not negative */ \
  261. { \
  262. uBitCount >>= 8; \
  263. /* bits remaining */ \
  264. uBitsReady = uBitsReady - uBitCount; \
  265. /* special case for stuffing processing */ \
  266. /* if (uBitsReady < 0) */ \
  267. /* kluged to test for negative */ \
  268. if (uBitsReady > 33) \
  269. /* if (bStuffing) */ \
  270. { \
  271. uWork <<= 8; \
  272. uBitsReady += 8; \
  273. uWork |= *fpu8++; \
  274. } \
  275. /* end special case for stuffing */ \
  276. uWork &= GetBitsMask[uBitsReady]; \
  277. } \
  278. else \
  279. uWork &= GetBitsMask[uBitsReady-8]; \
  280. }
  281. extern void BlockCopy(
  282. U32 uDstBlock,
  283. U32 uSrcBlock);
  284. extern void BlockCopySpecial(
  285. U32 uDstBlock,
  286. U32 uSrcBlock);
  287. extern void BlockAdd (
  288. U32 uResidual,
  289. U32 uRefBlock,
  290. U32 uDstBlock);
  291. extern void BlockAddSpecial (
  292. U32 uResidual,
  293. U32 uRefBlock,
  294. U32 uDstBlock);
  295. T_pFunc_VLD_RLD_IQ_Block pFunc_VLD_RLD_IQ_Block[2] = {VLD_RLD_IQ_Block,VLD_RLD_IQ_Block}; // New rearch
  296. //T_pFunc_VLD_RLD_IQ_Block pFunc_VLD_RLD_IQ_Block[2] = {VLD_RLD_IQ_Block, MMX_VLD_RLD_IQ_Block}; // New rearch
  297. /*****************************************************************************
  298. *
  299. * H263DecodeMBHeader
  300. *
  301. * Decode the MB header
  302. */
  303. #pragma code_seg("IACODE1")
  304. I32 H263DecodeMBHeader(
  305. T_H263DecoderCatalog FAR * DC,
  306. BITSTREAM_STATE FAR * fpbsState,
  307. U32 * uReadChecksum)
  308. {
  309. I32 iReturn = ICERR_ERROR;
  310. U8 FAR * fpu8;
  311. U32 uBitsReady;
  312. U32 uWork;
  313. U32 uResult;
  314. U32 uCode;
  315. U32 uBitCount;
  316. int bStuffing;
  317. #define START_CODE 0xff18
  318. #define STUFFING_CODE 0x0b22
  319. //#define DEBUG_MBLK -- Turn this on with a define in the makefile.
  320. #ifndef RING0
  321. #ifdef DEBUG_MBLK
  322. char buf120[120];
  323. int iLength;
  324. #endif
  325. #endif
  326. GET_BITS_RESTORE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  327. /* MBA --------------- */
  328. /* ********************************************* */
  329. /* minor table decode (>8 bits) not fully tested */
  330. /* to do note: */
  331. /* this is hacked */
  332. /* change >8 bit processing to use major/minor */
  333. /* tables and ONE GET_BITS routine */
  334. /* ********************************************* */
  335. ReadMBA:
  336. bStuffing = 0;
  337. GET_GT8_BITS(8, fpu8, uWork, uBitsReady, uResult,
  338. uCode, uBitCount, gTAB_MBA_MAJOR);
  339. if (uResult == STUFFING_CODE)
  340. { /* is stuffing code */
  341. bStuffing = 1;
  342. /* do MB checksum stuff here */
  343. #ifdef CHECKSUM_MACRO_BLOCK
  344. GET_BITS_SAVE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  345. /* might want to move this to a separate function for readability */
  346. GET_ONE_BIT(fpu8, uWork, uBitsReady, uResult);
  347. if (uResult == 1)
  348. {
  349. GET_FIXED_BITS(8, fpu8, uWork, uBitsReady, uResult);
  350. if (uResult == 1)
  351. { /* indicates TCOEFF checksum processing */
  352. /* read off all but the real checksum data */
  353. GET_FIXED_BITS(9, fpu8, uWork, uBitsReady, uResult);
  354. GET_FIXED_BITS(9, fpu8, uWork, uBitsReady, uResult);
  355. GET_FIXED_BITS(9, fpu8, uWork, uBitsReady, uResult);
  356. GET_FIXED_BITS(9, fpu8, uWork, uBitsReady, uResult);
  357. GET_FIXED_BITS(9, fpu8, uWork, uBitsReady, uResult);
  358. GET_FIXED_BITS(9, fpu8, uWork, uBitsReady, uResult);
  359. /* now get real checksum data */
  360. /* run */
  361. GET_FIXED_BITS(9, fpu8, uWork, uBitsReady, uResult);
  362. *uReadChecksum = ((uResult & 0xff) << 24);
  363. /* level */
  364. GET_FIXED_BITS(9, fpu8, uWork, uBitsReady, uResult);
  365. *uReadChecksum = (*uReadChecksum | ((uResult & 0xff) << 16));
  366. GET_FIXED_BITS(9, fpu8, uWork, uBitsReady, uResult);
  367. *uReadChecksum = (*uReadChecksum | ((uResult & 0xff) << 8));
  368. /* sign */
  369. GET_FIXED_BITS(9, fpu8, uWork, uBitsReady, uResult);
  370. *uReadChecksum = (*uReadChecksum | (uResult & 0xff));
  371. GET_ONE_BIT(fpu8, uWork, uBitsReady, uResult);
  372. }
  373. else
  374. {
  375. DBOUT("ERROR :: H261MBChecksum :: Invalid Checksum Data :: ERROR");
  376. iReturn = ICERR_ERROR;
  377. goto done;
  378. }
  379. }
  380. else
  381. {
  382. GET_BITS_RESTORE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  383. goto ReadMBA;
  384. }
  385. #else /* is MBA stuffing, but checksum not enabled */
  386. GET_BITS_SAVE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  387. // GET_ONE_BIT(fpu8, uWork, uBitsReady, uResult);
  388. /*if (uResult == 1) {
  389. DbgLog((LOG_ERROR, HDBG_ALWAYS, TEXT("ERROR :: Stuffing code found, Checksum not enabled :: ERROR")));
  390. iReturn = ICERR_ERROR;
  391. goto done;
  392. } */
  393. // if (uResult == 1)
  394. // {
  395. // GET_BITS_RESTORE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  396. // }
  397. // else {
  398. //GET_BITS_RESTORE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  399. // }
  400. #endif
  401. } /* end if (uResult == STUFFING_CODE) */
  402. /* try this for now */
  403. else
  404. {
  405. if (uResult == START_CODE)
  406. {
  407. I8 temp;
  408. temp = (I8)(uResult & 0xff);
  409. GET_VAR_BITS(16, fpu8, uWork, uBitsReady, uResult, uCode, uBitCount, (gTAB_MBA_MINOR + temp));
  410. if (uResult != 0x1023)
  411. {
  412. DBOUT("ERROR :: Invalid startcode :: ERROR");
  413. iReturn = ICERR_ERROR;
  414. }
  415. else
  416. iReturn = START_CODE;
  417. GET_BITS_SAVE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  418. goto done;
  419. } /* end if (uResult == START_CODE) */
  420. else /* is not stuffing */
  421. { /* if uResult negative, get more bits */
  422. if (uResult & 0x8000)
  423. {
  424. I8 temp;
  425. temp = (I8)(uResult & 0xff);
  426. GET_VAR_BITS(11, fpu8, uWork, uBitsReady, uResult, uCode, uBitCount, (gTAB_MBA_MINOR + temp));
  427. }
  428. DC->uMBA = (uResult & 0xff);
  429. }/* end else is not stuffing */
  430. }
  431. /* When MBA==Stuffing, we jump back to the start to look for MBA */
  432. if (bStuffing)
  433. goto ReadMBA;
  434. /* MTYPE ---------------------------------------- */
  435. GET_GT8_BITS(8, fpu8, uWork, uBitsReady, uResult,
  436. uCode, uBitCount, gTAB_MTYPE_MAJOR);
  437. if (uResult & 0x8000)
  438. {
  439. I8 temp;
  440. temp = (I8)(uResult & 0xff);
  441. GET_VAR_BITS(10, fpu8, uWork, uBitsReady, uResult,
  442. uCode, uBitCount, (gTAB_MTYPE_MINOR + temp));
  443. }
  444. DC->uMBType = (uResult & 0xff);
  445. /* MQUANT ---------------------------------------- */
  446. if (DC->uMBType == 1 || DC->uMBType == 3 || DC->uMBType == 6 || DC->uMBType == 9)
  447. { /* get 5-bit MQuant */
  448. GET_FIXED_BITS(5, fpu8, uWork, uBitsReady, uResult);
  449. DC->uMQuant = (uResult & 0xff);
  450. }
  451. /* MVD ------------------------------------------- */
  452. /* reset previous motion vectors */
  453. /* if MB 0,11,22 */
  454. /* if MBA != 1 or */
  455. /* if previous MB was not MC */
  456. if (DC->uMBType >3)
  457. {
  458. if ((DC->uMBA != 1) || (DC->i16LastMBA == 10) || (DC->i16LastMBA == 21))
  459. DC->i8MVDH = DC->i8MVDV = 0;
  460. /* get X motion vector */
  461. GET_GT8_BITS(8, fpu8, uWork, uBitsReady, uResult,
  462. uCode, uBitCount, gTAB_MVD_MAJOR);
  463. if (uResult & 0x8000)
  464. {
  465. I8 temp;
  466. temp = (I8)(uResult & 0xff);
  467. GET_VAR_BITS(11, fpu8, uWork, uBitsReady, uResult,
  468. uCode, uBitCount, (gTAB_MVD_MINOR + temp));
  469. }
  470. /* convert and make incremental */
  471. DC->i8MVDH = gTAB_MV_ADJUST[DC->i8MVDH + (I8)(uResult & 0xff) + 32];
  472. /* get Y motion vector */
  473. GET_GT8_BITS(8, fpu8, uWork, uBitsReady, uResult,
  474. uCode, uBitCount, gTAB_MVD_MAJOR);
  475. if (uResult & 0x8000)
  476. {
  477. I8 temp;
  478. temp = (I8)(uResult & 0xff);
  479. GET_VAR_BITS(11, fpu8, uWork, uBitsReady, uResult,
  480. uCode, uBitCount, (gTAB_MVD_MINOR + temp));
  481. }
  482. /* convert and make incremental */
  483. DC->i8MVDV = gTAB_MV_ADJUST[DC->i8MVDV + (I8)(uResult & 0xff) + 32];
  484. } /* end if (DC->MBType > 3) */
  485. else
  486. DC->i8MVDH = DC->i8MVDV = 0;
  487. /* CBP --------------------------------------------- */
  488. /* brute force method */
  489. DC->uCBP = 0; /* for MType = 4 or 7 */
  490. if (DC->uMBType == 2 || DC->uMBType == 3 || DC->uMBType == 5 || DC->uMBType == 6 || DC->uMBType == 8 || DC->uMBType == 9)
  491. { /* get CBP */
  492. GET_VAR_BITS(9, fpu8, uWork, uBitsReady, uResult,
  493. uCode, uBitCount, gTAB_CBP);
  494. DC->uCBP = (uResult & 0xff);
  495. } /* end get CBP */
  496. else
  497. if (DC->uMBType < 2) /* is intra */
  498. DC->uCBP = 63; /* force CBP to 63 */
  499. GET_BITS_SAVE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  500. iReturn = ICERR_OK;
  501. #ifndef RING0
  502. #ifdef DEBUG_MBLK
  503. iLength = wsprintf(buf120, "MBType=%ld MQuant=%ld MVDH=%ld MVDV=%ld CBP=%ld",
  504. DC->uMBType,
  505. DC->uMQuant,
  506. DC->i8MVDH,
  507. DC->i8MVDV,
  508. DC->uCBP);
  509. DBOUT(buf120);
  510. ASSERT(iLength < 120);
  511. #endif
  512. #endif
  513. done:
  514. return iReturn;
  515. } /* end H263DecodeMBHeader() */
  516. #pragma code_seg()
  517. /*****************************************************************************
  518. *
  519. * H263DecodeMBData
  520. *
  521. * Decode each of the blocks in this macro block
  522. */
  523. #pragma code_seg("IACODE1")
  524. I32 H263DecodeMBData(
  525. T_H263DecoderCatalog FAR * DC,
  526. T_BlkAction FAR * fpBlockAction,
  527. I32 iBlockNumber,
  528. BITSTREAM_STATE FAR * fpbsState,
  529. U8 FAR * fpu8MaxPtr,
  530. U32 * uReadChecksum,
  531. U32 **pN, // New rearch
  532. T_IQ_INDEX ** pRUN_INVERSE_Q) // New rearch
  533. {
  534. I32 iResult = ICERR_ERROR;
  535. int iCBP = (int) DC->uCBP;
  536. int i;
  537. U32 uBitsReady;
  538. U32 uBitsReadIn;
  539. U32 uBitsReadOut;
  540. U8 u8Quant; /* quantization level for this block */
  541. U8 FAR * fpu8;
  542. U32 uByteCnt;
  543. I8 mvx, mvy, mvx2, mvy2;
  544. T_pFunc_VLD_RLD_IQ_Block pFunc_VLD =pFunc_VLD_RLD_IQ_Block[0];
  545. U32 uCheckSum; /* checksum data returned from DecodeBlock */
  546. #ifdef CHECKSUM_MACRO_BLOCK_DETAIL
  547. char buf80[80];
  548. int iMBDLength;
  549. #endif
  550. #ifdef CHECKSUM_MACRO_BLOCK
  551. char buff80[80];
  552. int iLength;
  553. #endif
  554. #ifdef DECODE_STATS
  555. U32 uStartLow = DC->uStartLow;
  556. U32 uStartHigh = DC->uStartHigh;
  557. U32 uElapsed;
  558. U32 uBefore;
  559. U32 uDecodeBlockSum = 0;
  560. U32 uLoopFilterSum = 0;
  561. U32 uBlockCopySum = 0;
  562. U32 uBlockCopySpSum = 0;
  563. U32 uBlockAddSum = 0;
  564. U32 uBlockAddSpSum = 0;
  565. int bTimingThisFrame = DC->bTimingThisFrame;
  566. DEC_TIMING_INFO * pDecTimingInfo = NULL;
  567. #endif
  568. /* On input the pointer points to the next byte. */
  569. /* We need to change it to */
  570. /* point to the current word on a 32-bit boundary. */
  571. fpu8 = fpbsState->fpu8 - 1; /* point to the current byte */
  572. uBitsReady = fpbsState->uBitsReady;
  573. while (uBitsReady >= 8) {
  574. fpu8--;
  575. uBitsReady -= 8;
  576. }
  577. uBitsReadIn = 8 - uBitsReady;
  578. u8Quant = (U8) (DC->uMQuant);
  579. if (DC->uMBType > 1)
  580. {
  581. /* calculate motion vectors */
  582. mvx = DC->i8MVDH;
  583. mvy = DC->i8MVDV;
  584. // calculate UV blocks MV
  585. mvx2 = mvx / 2;
  586. mvy2 = mvy / 2;
  587. fpBlockAction->i8MVX = mvx;
  588. fpBlockAction->i8MVY = mvy;
  589. // duplicate other 3 Y blocks
  590. fpBlockAction[1].i8MVX = mvx;
  591. fpBlockAction[1].i8MVY = mvy;
  592. fpBlockAction[2].i8MVX = mvx;
  593. fpBlockAction[2].i8MVY = mvy;
  594. fpBlockAction[3].i8MVX = mvx;
  595. fpBlockAction[3].i8MVY = mvy;
  596. // init UV blocks
  597. fpBlockAction[4].i8MVX = mvx2;
  598. fpBlockAction[4].i8MVY = mvy2;
  599. fpBlockAction[5].i8MVX = mvx2;
  600. fpBlockAction[5].i8MVY = mvy2;
  601. }
  602. uCheckSum = 0; /* Init MB Checksum */
  603. for (i = 0; i < 6; i++)
  604. {
  605. if (DC->uMBType <= 1) /* is intra */
  606. fpBlockAction->u8BlkType = BT_INTRA;
  607. else
  608. if (iCBP & 0x20) /* if coded */
  609. fpBlockAction->u8BlkType = BT_INTER;
  610. else
  611. fpBlockAction->u8BlkType = BT_EMPTY;
  612. if (fpBlockAction->u8BlkType != BT_EMPTY)
  613. {
  614. fpBlockAction->u8Quant = u8Quant;
  615. ASSERT(fpBlockAction->pCurBlock != NULL);
  616. ASSERT(fpBlockAction->uBlkNumber == (U32)iBlockNumber);
  617. /*----- DecodeBlock ----*/
  618. #ifdef DECODE_STATS
  619. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  620. #endif
  621. #ifdef CHECKSUM_MACRO_BLOCK
  622. // uBitsReadOut = DecodeBlock(fpBlockAction, fpu8, uBitsReadIn, (U32)DC+DC->uMBBuffer+i*256, fpBlockAction->pCurBlock, &uCheckSum);
  623. #else
  624. // rearch
  625. uBitsReadOut = (*pFunc_VLD) ( fpBlockAction,
  626. fpu8,
  627. uBitsReadIn,
  628. (U32 *) *pN,
  629. (U32 *) *pRUN_INVERSE_Q);
  630. // rearch
  631. #endif
  632. #ifdef DECODE_STATS
  633. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uDecodeBlockSum)
  634. #endif
  635. if (uBitsReadOut == 0)
  636. {
  637. DBOUT("ERROR :: H263DecodeMBData :: Error decoding a Y block :: ERROR");
  638. DBOUT("ERROR :: DecodeBlock return 0 bits read....");
  639. goto done;
  640. }
  641. uByteCnt = uBitsReadOut >> 3; /* divide by 8 */
  642. uBitsReadIn = uBitsReadOut & 0x7; /* mod 8 */
  643. fpu8 += uByteCnt;
  644. /* New for rearch */
  645. ASSERT ( **pN < 65 );
  646. ////////////////////////////////////////////////
  647. // End hack //
  648. ////////////////////////////////////////////////
  649. *pRUN_INVERSE_Q += **pN;
  650. if ((0xf & fpBlockAction->u8BlkType) != BT_INTER)
  651. **pN += 65;
  652. (*pN)++;
  653. /* end of new rearch */
  654. /* allow the pointer to address up to four beyond */
  655. /* the end reading by DWORD using postincrement. */
  656. /* changed for detection of stuffing code at */
  657. /* end of frame */
  658. // ASSERT(fpu8 <= (fpu8MaxPtr+14));
  659. if (fpu8 > (fpu8MaxPtr+14))
  660. {
  661. iResult = ICERR_ERROR; // probably not needed
  662. goto done;
  663. }
  664. } /* end if not empty */
  665. else /* is empty */
  666. { /* zero out intermediate data structure and advance pointers */
  667. /* New for rearch */
  668. **pN = 0;
  669. (*pN)++;
  670. /* end of new rearch */
  671. }
  672. fpBlockAction++;
  673. iCBP <<= 1;
  674. iBlockNumber++;
  675. } /* end for each block in macroblock */
  676. #ifdef CHECKSUM_MACRO_BLOCK
  677. /* Compare checksum */
  678. if ((uCheckSum != *uReadChecksum) && (*uReadChecksum != 0))
  679. {
  680. iLength = wsprintf(buff80,"WARNING:MB CheckSum miss match, Enc Checksum=0x%x Dec Checksum=0x%x",
  681. *uReadChecksum, uCheckSum);
  682. DBOUT(buff80);
  683. ASSERT(iLength < 80);
  684. }
  685. #ifdef CHECKSUM_MACRO_BLOCK_DETAIL
  686. iMBDLength = wsprintf(buf80,"Block=%d CheckSum=0x%x", i, uCheckSum);
  687. DBOUT(buf80);
  688. ASSERT(iMBDLength < 80);
  689. #endif
  690. #endif
  691. /* restore the scanning pointers to point to the next byte */
  692. /* and set the uWork and uBitsReady values. */
  693. while (uBitsReadIn > 8)
  694. {
  695. fpu8++;
  696. uBitsReadIn -= 8;
  697. }
  698. fpbsState->uBitsReady = 8 - uBitsReadIn;
  699. fpbsState->uWork = *fpu8++; /* store the data and point to next byte */
  700. fpbsState->uWork &= GetBitsMask[fpbsState->uBitsReady];
  701. fpbsState->fpu8 = fpu8;
  702. #ifdef DECODE_STATS
  703. if (bTimingThisFrame)
  704. {
  705. pDecTimingInfo = DC->pDecTimingInfo + DC->uStatFrameCount;
  706. pDecTimingInfo->uDecodeBlock += uDecodeBlockSum;
  707. pDecTimingInfo->uLoopFilter += uLoopFilterSum;
  708. pDecTimingInfo->uBlockCopy += uBlockCopySum;
  709. pDecTimingInfo->uBlockCopySp += uBlockCopySpSum;
  710. pDecTimingInfo->uBlockAdd += uBlockAddSum;
  711. pDecTimingInfo->uBlockAddSp += uBlockAddSpSum;
  712. }
  713. #endif
  714. iResult = ICERR_OK;
  715. done:
  716. return iResult;
  717. } /* H263DecodeMBData() */
  718. #pragma code_seg()
  719. /*****************************************************************************
  720. *
  721. * H263IDCTandMC
  722. *
  723. * Inverse Discrete Cosine Transform and
  724. * Motion Compensation for each block
  725. *
  726. */
  727. #pragma code_seg("IACODE2")
  728. void H263IDCTandMC(
  729. T_H263DecoderCatalog FAR *DC,
  730. T_BlkAction FAR *fpBlockAction,
  731. int iBlock,
  732. int iMBNum, // AP-NEW
  733. int iGOBNum, // AP-NEW
  734. U32 *pN,
  735. T_IQ_INDEX *pRUN_INVERSE_Q,
  736. T_MBInfo *fpMBInfo, // AP-NEW
  737. int iEdgeFlag
  738. )
  739. {
  740. I32 pRef;
  741. I32 mvx, mvy;
  742. ASSERT(*pN != 65);
  743. if (*pN < 65) // Inter block
  744. {
  745. // first do motion compensation
  746. // result will be pointed to by pRef
  747. mvx = fpBlockAction[iBlock].i8MVX;
  748. mvy = fpBlockAction[iBlock].i8MVY;
  749. pRef = fpBlockAction[iBlock].pRefBlock + (I32) mvx + PITCH * (I32) mvy;
  750. // now do the inverse transform (where appropriate) & combine
  751. if (*pN > 0) // and, of course, < 65.
  752. {
  753. // Get residual block; output at DC+DC->uMBBuffer+BLOCK_BUFFER_OFFSET
  754. // Finally add the residual to the reference block
  755. // TODO
  756. DecodeBlock_IDCT(
  757. (U32)pRUN_INVERSE_Q,
  758. *pN,
  759. fpBlockAction[iBlock].pCurBlock, // not used here
  760. (U32) DC + DC->uMBBuffer + BLOCK_BUFFER_OFFSET);// inter output
  761. if (fpMBInfo->i8MBType >=7)
  762. {
  763. // do spatial loop filter
  764. LoopFilter((U8 *)pRef, (U8*)DC+DC->uFilterBBuffer, PITCH);
  765. BlockAddSpecial((U32)DC+DC->uMBBuffer + BLOCK_BUFFER_OFFSET,
  766. (U32)DC+DC->uFilterBBuffer,
  767. fpBlockAction[iBlock].pCurBlock);
  768. }
  769. else
  770. {
  771. BlockAdd(
  772. (U32) DC + DC->uMBBuffer + BLOCK_BUFFER_OFFSET, // output
  773. pRef, // prediction
  774. fpBlockAction[iBlock].pCurBlock); // destination
  775. }
  776. }
  777. else // *pN == 0, so no transform coefficients for this block
  778. {
  779. // Just copy motion compensated reference block
  780. if (fpMBInfo->i8MBType >=7)
  781. {
  782. // do spatial loop filter
  783. LoopFilter((U8 *)pRef, (U8*)DC+DC->uFilterBBuffer, PITCH);
  784. //MMX_LoopFilter((U8 *)pRef, (U8*)DC+DC->uFilterBBuffer, 8);
  785. BlockCopySpecial(fpBlockAction[iBlock].pCurBlock,
  786. (U32)DC+DC->uFilterBBuffer);
  787. }
  788. else
  789. BlockCopy(
  790. fpBlockAction[iBlock].pCurBlock, // destination
  791. pRef); // prediction
  792. }
  793. }
  794. else // *pN >= 65, hence intRA
  795. {
  796. // TODO
  797. DecodeBlock_IDCT(
  798. (U32)pRUN_INVERSE_Q,
  799. *pN,
  800. fpBlockAction[iBlock].pCurBlock, // intRA transform output
  801. (U32) DC + DC->uMBBuffer + BLOCK_BUFFER_OFFSET);
  802. } // end if (*pN < 65) ... else ...
  803. }
  804. // End IDCTandMC
  805. ////////////////////////////////////////////////////////////////////////////////
  806. #pragma code_seg()