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.

1448 lines
47 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 Intel Corporation.
  10. ** Copyright (c) 1996 Intel Corporation.
  11. ** All Rights Reserved.
  12. **
  13. ** *************************************************************************
  14. */
  15. // $Author: CZHU $
  16. // $Date: 06 Feb 1997 15:35:30 $
  17. // $Archive: S:\h26x\src\dec\d3mvdec.cpv $
  18. // $Header: S:\h26x\src\dec\d3mvdec.cpv 1.47 06 Feb 1997 15:35:30 CZHU $
  19. // $Log: S:\h26x\src\dec\d3mvdec.cpv $
  20. //
  21. // Rev 1.47 06 Feb 1997 15:35:30 CZHU
  22. // Changed | to ||
  23. //
  24. // Rev 1.46 24 Jan 1997 13:32:56 CZHU
  25. //
  26. // Added fix to check uBitsReady when return from DecodingMB header for
  27. // packet loss detection.
  28. //
  29. // Rev 1.45 19 Dec 1996 16:07:44 JMCVEIGH
  30. //
  31. // Added initialization of forward-prediction-only flag if a block
  32. // is not coded. This is used in H.263+ only.
  33. //
  34. // Rev 1.44 16 Dec 1996 17:44:00 JMCVEIGH
  35. // Allow 8x8 motion vectors if deblocking filter selected and
  36. // support for decoding of improved PB-frame MODB.
  37. //
  38. // Rev 1.43 20 Oct 1996 15:51:06 AGUPTA2
  39. // Adjusted DbgLog trace levels; 4:Frame, 5:GOB, 6:MB, 8:everything
  40. //
  41. // Rev 1.42 20 Oct 1996 13:21:20 AGUPTA2
  42. // Changed DBOUT into DbgLog. ASSERT is not changed to DbgAssert.
  43. //
  44. //
  45. // Rev 1.41 11 Jul 1996 15:13:16 AGUPTA2
  46. // Changed assertion failures into errors when decoder goes past end of
  47. // the bitstream.
  48. //
  49. // Rev 1.40 03 May 1996 13:05:34 CZHU
  50. //
  51. // Check errors at MB header for packet faults and return PACKET_FAULT
  52. //
  53. // Rev 1.39 22 Mar 1996 17:25:18 AGUPTA2
  54. // Changes to accomodate MMX rtns.
  55. //
  56. // Rev 1.38 08 Mar 1996 16:46:28 AGUPTA2
  57. // Added pragmas code_seg and data_seg. Changed the size of some of local data.
  58. //
  59. //
  60. // Rev 1.37 23 Feb 1996 09:46:56 KLILLEVO
  61. // fixed decoding of Unrestricted Motion Vector mode
  62. //
  63. // Rev 1.36 17 Jan 1996 12:44:26 RMCKENZX
  64. // Added support for decoding motion vectors for UMV
  65. // Reorganized motion vector decoding processes, especially
  66. // for AP and eliminating the large HALF_PEL conversion tables.
  67. //
  68. // Rev 1.35 02 Jan 1996 17:55:50 RMCKENZX
  69. //
  70. // Updated copyright notice
  71. //
  72. // Rev 1.34 02 Jan 1996 15:48:54 RMCKENZX
  73. // Added code to preserve the Block Type in the block action stream
  74. // for P blocks when PB frames is on. This is read in H263IDCTandMC
  75. // when AP is on.
  76. //
  77. // Rev 1.33 18 Dec 1995 12:42:12 RMCKENZX
  78. // added copyright notice
  79. //
  80. // Rev 1.32 13 Dec 1995 22:10:06 RHAZRA
  81. // AP bug fix
  82. //
  83. // Rev 1.31 13 Dec 1995 22:01:26 TRGARDOS
  84. //
  85. // Added more parentheses to a logical statement.
  86. //
  87. // Rev 1.30 13 Dec 1995 15:10:08 TRGARDOS
  88. // Changed MV assert to be -32 <= MV <= 31, instead of strict
  89. // inequalities.
  90. //
  91. // Rev 1.29 13 Dec 1995 10:59:58 RHAZRA
  92. //
  93. // AP+PB changes
  94. //
  95. // Rev 1.28 11 Dec 1995 11:34:20 RHAZRA
  96. // 12-10-95 changes: added AP stuff
  97. //
  98. // Rev 1.27 09 Dec 1995 17:28:38 RMCKENZX
  99. // Gutted and re-built file to support decoder re-architecture.
  100. // New modules are:
  101. // H263ComputeMotionVectors
  102. // H263DecodeMBHeader
  103. // H263DecodeIDCTCoeffs
  104. // This module now contains code to support the first pass of the decoder
  105. //
  106. // Rev 1.26 05 Dec 1995 09:12:28 CZHU
  107. // Added fixes for proper MV prediction when GOB header is present
  108. //
  109. // Rev 1.25 22 Nov 1995 13:43:42 RMCKENZX
  110. //
  111. // changed calls to utilize assembly modules for bi-directional
  112. // motion compensation & removed corresponding C modules
  113. //
  114. // Rev 1.24 17 Nov 1995 12:58:22 RMCKENZX
  115. // added missing ()s to adjusted_mvx & adjusted_mvy in H263BiMotionCompLuma
  116. //
  117. // Rev 1.23 07 Nov 1995 11:01:10 CZHU
  118. // Include Fixes for boundary of bi-directional predictions
  119. //
  120. // Rev 1.22 26 Oct 1995 11:22:10 CZHU
  121. // Compute backward MV based on TRD, not TR
  122. //
  123. // Rev 1.21 13 Oct 1995 16:06:22 CZHU
  124. // First version that supports PB frames. Display B or P frames under
  125. // VfW for now.
  126. //
  127. // Rev 1.20 13 Oct 1995 13:42:46 CZHU
  128. // Added back the #define for debug messages.
  129. //
  130. // Rev 1.19 11 Oct 1995 17:51:00 CZHU
  131. //
  132. // Fixed bugs in passing MV back with DC.
  133. //
  134. // Rev 1.18 11 Oct 1995 13:26:08 CZHU
  135. // Added code to support PB frame
  136. //
  137. // Rev 1.17 09 Oct 1995 09:44:04 CZHU
  138. //
  139. // Use the optimized version of (half,half) interpolation
  140. //
  141. // Rev 1.16 08 Oct 1995 13:45:10 CZHU
  142. //
  143. // Optionally use C version of interpolation
  144. //
  145. // Rev 1.15 03 Oct 1995 15:05:26 CZHU
  146. // Cleaning up.
  147. //
  148. // Rev 1.14 02 Oct 1995 09:58:56 TRGARDOS
  149. // Added #ifdef to debug print statement.
  150. //
  151. // Rev 1.13 29 Sep 1995 16:22:06 CZHU
  152. //
  153. // Fixed the bug in GOB 0 when compute MV2
  154. //
  155. // Rev 1.12 29 Sep 1995 09:02:56 CZHU
  156. // Rearrange Chroma blocks processing
  157. //
  158. // Rev 1.11 28 Sep 1995 15:33:04 CZHU
  159. //
  160. // Call the right version of interpolation based on MV
  161. //
  162. // Rev 1.10 27 Sep 1995 11:54:50 CZHU
  163. // Integrated half pel motion compensation
  164. //
  165. // Rev 1.9 26 Sep 1995 15:33:06 CZHU
  166. //
  167. // Put place holder in for half pel interpolation
  168. //
  169. // Rev 1.8 20 Sep 1995 14:47:50 CZHU
  170. // Made the number of MBs in GOB a variableD
  171. //
  172. // Rev 1.7 19 Sep 1995 13:53:34 CZHU
  173. // Added assertion for half-pel motion vectors
  174. //
  175. // Rev 1.6 18 Sep 1995 10:20:58 CZHU
  176. // Scale the motion vectors for UV planes too.
  177. //
  178. // Rev 1.5 14 Sep 1995 10:12:36 CZHU
  179. //
  180. // Cleaning
  181. //
  182. // Rev 1.4 13 Sep 1995 11:56:30 CZHU
  183. // Fixed bugs in finding the predictors for motion vector.
  184. //
  185. // Rev 1.3 12 Sep 1995 18:18:58 CZHU
  186. //
  187. // Modified table for looking up UV MV's
  188. //
  189. // Rev 1.2 11 Sep 1995 16:41:14 CZHU
  190. // Added reference block address calculation
  191. //
  192. // Rev 1.1 11 Sep 1995 14:31:30 CZHU
  193. // Started to add function to calculate MVs and Reference block addresses.
  194. //
  195. // Rev 1.0 08 Sep 1995 11:45:56 CZHU
  196. // Initial revision.
  197. #include "precomp.h"
  198. /* MCBPC table format
  199. *
  200. * layout
  201. *
  202. * unused mbtype cbpc bits
  203. * 15-13 12-10 9-8 7-0
  204. */
  205. #pragma data_seg("IADATA1")
  206. #define MCBPC_MBTYPE(d) ((d>>10) & 0x7)
  207. #define MCBPC_CBPC(d) ((d>>8) & 0x3)
  208. #define MCBPC_BITS(d) (d & 0xFF)
  209. #define MCBPC_ENTRY(m,c,b) \
  210. ( ((m & 0x7) <<10) | ((c & 0x3) << 8) | (b & 0xFF) )
  211. U16 gNewTAB_MCBPC_INTRA[64] = {
  212. /* index 8 - stuffing */
  213. MCBPC_ENTRY(0,0,9),
  214. /* index 5 */
  215. MCBPC_ENTRY(4,1,6),
  216. /* index 6 */
  217. MCBPC_ENTRY(4,2,6),
  218. /* index 7 */
  219. MCBPC_ENTRY(4,3,6),
  220. /* index 4; 0001XX */
  221. MCBPC_ENTRY(4,0,4), MCBPC_ENTRY(4,0,4), MCBPC_ENTRY(4,0,4), MCBPC_ENTRY(4,0,4),
  222. /* index 1; 001XXX */
  223. MCBPC_ENTRY(3,1,3), MCBPC_ENTRY(3,1,3), MCBPC_ENTRY(3,1,3), MCBPC_ENTRY(3,1,3),
  224. MCBPC_ENTRY(3,1,3), MCBPC_ENTRY(3,1,3), MCBPC_ENTRY(3,1,3), MCBPC_ENTRY(3,1,3),
  225. /* index 2; 010XXX */
  226. MCBPC_ENTRY(3,2,3), MCBPC_ENTRY(3,2,3), MCBPC_ENTRY(3,2,3), MCBPC_ENTRY(3,2,3),
  227. MCBPC_ENTRY(3,2,3), MCBPC_ENTRY(3,2,3), MCBPC_ENTRY(3,2,3), MCBPC_ENTRY(3,2,3),
  228. /* index 3; 011XXX */
  229. MCBPC_ENTRY(3,3,3), MCBPC_ENTRY(3,3,3), MCBPC_ENTRY(3,3,3), MCBPC_ENTRY(3,3,3),
  230. MCBPC_ENTRY(3,3,3), MCBPC_ENTRY(3,3,3), MCBPC_ENTRY(3,3,3), MCBPC_ENTRY(3,3,3),
  231. /* index 0; 1XXXXX */
  232. MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1),
  233. MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1),
  234. MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1),
  235. MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1),
  236. MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1),
  237. MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1),
  238. MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1),
  239. MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1), MCBPC_ENTRY(3,0,1)
  240. };
  241. /* MCBPC table for INTER frame
  242. *
  243. * Format same as MCBPC for INTRA
  244. * layout
  245. *
  246. * unused mbtype cbpc bits
  247. * 15-13 12-10 9-8 7-0
  248. *
  249. */
  250. U16 gNewTAB_MCBPC_INTER[512] = {
  251. /* invalid code */
  252. MCBPC_ENTRY(0,0,0),
  253. MCBPC_ENTRY(0,0,9), //index 20, stuffing
  254. MCBPC_ENTRY(4,3,9), //19,
  255. MCBPC_ENTRY(4,2,9), //18
  256. MCBPC_ENTRY(4,1,9), //17
  257. MCBPC_ENTRY(1,3,9), //7
  258. //2 index 14
  259. MCBPC_ENTRY(3,2,8),MCBPC_ENTRY(3,2,8), //14
  260. //2 index 13
  261. MCBPC_ENTRY(3,1,8),MCBPC_ENTRY(3,1,8), //13
  262. //2 index 11
  263. MCBPC_ENTRY(2,3,8),MCBPC_ENTRY(2,3,8), //11
  264. //4 index 15
  265. MCBPC_ENTRY(3,3,7),MCBPC_ENTRY(3,3,7),MCBPC_ENTRY(3,3,7),MCBPC_ENTRY(3,3,7), //15
  266. //4 index 10
  267. MCBPC_ENTRY(2,2,7),MCBPC_ENTRY(2,2,7),MCBPC_ENTRY(2,2,7),MCBPC_ENTRY(2,2,7), //10
  268. //4 index 9
  269. MCBPC_ENTRY(2,1,7),MCBPC_ENTRY(2,1,7),MCBPC_ENTRY(2,1,7),MCBPC_ENTRY(2,1,7), //9
  270. //4 index 6
  271. MCBPC_ENTRY(1,2,7),MCBPC_ENTRY(1,2,7),MCBPC_ENTRY(1,2,7),MCBPC_ENTRY(1,2,7), //6
  272. //4 index 5
  273. MCBPC_ENTRY(1,1,7),MCBPC_ENTRY(1,1,7),MCBPC_ENTRY(1,1,7),MCBPC_ENTRY(1,1,7), //5
  274. //8 index 16
  275. MCBPC_ENTRY(4,0,6),MCBPC_ENTRY(4,0,6),MCBPC_ENTRY(4,0,6),MCBPC_ENTRY(4,0,6),//16
  276. MCBPC_ENTRY(4,0,6),MCBPC_ENTRY(4,0,6),MCBPC_ENTRY(4,0,6),MCBPC_ENTRY(4,0,6),
  277. //8 index 3
  278. MCBPC_ENTRY(0,3,6),MCBPC_ENTRY(0,3,6),MCBPC_ENTRY(0,3,6),MCBPC_ENTRY(0,3,6),//3
  279. MCBPC_ENTRY(0,3,6),MCBPC_ENTRY(0,3,6),MCBPC_ENTRY(0,3,6),MCBPC_ENTRY(0,3,6),//3
  280. //16 index 12
  281. MCBPC_ENTRY(3,0,5),MCBPC_ENTRY(3,0,5),MCBPC_ENTRY(3,0,5),MCBPC_ENTRY(3,0,5),//12
  282. MCBPC_ENTRY(3,0,5),MCBPC_ENTRY(3,0,5),MCBPC_ENTRY(3,0,5),MCBPC_ENTRY(3,0,5),
  283. MCBPC_ENTRY(3,0,5),MCBPC_ENTRY(3,0,5),MCBPC_ENTRY(3,0,5),MCBPC_ENTRY(3,0,5),
  284. MCBPC_ENTRY(3,0,5),MCBPC_ENTRY(3,0,5),MCBPC_ENTRY(3,0,5),MCBPC_ENTRY(3,0,5),
  285. //32 INDEX 2
  286. MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),
  287. MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),
  288. MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),
  289. MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),
  290. MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),
  291. MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),
  292. MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),
  293. MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),MCBPC_ENTRY(0,2,4),
  294. //32 index 1
  295. MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),
  296. MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),
  297. MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),
  298. MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),
  299. MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),
  300. MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),
  301. MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),
  302. MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),MCBPC_ENTRY(0,1,4),
  303. //64 index 8
  304. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  305. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  306. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  307. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  308. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  309. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  310. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  311. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  312. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  313. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  314. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  315. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  316. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  317. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  318. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  319. MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),MCBPC_ENTRY(2,0,3),
  320. //64 index 4
  321. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  322. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  323. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  324. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  325. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  326. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  327. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  328. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  329. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  330. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  331. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  332. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  333. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  334. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  335. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  336. MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),MCBPC_ENTRY(1,0,3),
  337. //256 index 0
  338. //0--63
  339. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  340. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  341. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  342. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  343. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  344. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  345. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  346. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  347. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  348. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  349. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  350. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  351. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  352. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  353. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  354. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  355. //64--127
  356. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  357. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  358. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  359. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  360. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  361. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  362. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  363. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  364. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  365. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  366. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  367. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  368. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  369. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  370. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  371. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  372. //128--128+64=192
  373. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  374. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  375. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  376. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  377. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  378. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  379. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  380. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  381. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  382. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  383. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  384. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  385. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  386. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  387. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  388. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  389. //192--255
  390. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  391. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  392. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  393. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  394. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  395. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  396. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  397. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  398. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  399. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  400. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  401. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  402. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  403. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  404. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),
  405. MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1),MCBPC_ENTRY(0,0,1)
  406. };
  407. /* CBPY table format
  408. *
  409. * layout
  410. *
  411. * intra inter bits
  412. * 15-12 11-8 7-0
  413. *
  414. * unused entries should have zero data
  415. */
  416. #define CBPY_INTRA(d) ((d>>12) & 0xf)
  417. #define CBPY_INTER(d) ((d>>8) & 0xf)
  418. #define CBPY_BITS(d) (d & 0xff)
  419. #define CBPY_ENTRY(a,r,b) \
  420. ( ((a & 0xf) <<12) | ((r & 0xf) << 8) | (b & 0xFF) )
  421. #define CBPY_NOT_USED() CBPY_ENTRY(0,0,0)
  422. U16 gNewTAB_CBPY[64] = {
  423. /* NotUsed - 0000 0X */
  424. CBPY_NOT_USED(), CBPY_NOT_USED(),
  425. /* Index 6 - 0000 10 */
  426. CBPY_ENTRY(6,9,6),
  427. /* Index 9 - 0000 11 */
  428. CBPY_ENTRY(9,6,6),
  429. /* Index 8 - 0001 0x */
  430. CBPY_ENTRY(8,7,5), CBPY_ENTRY(8,7,5),
  431. /* Index 4 - 0001 1x */
  432. CBPY_ENTRY(4,11,5), CBPY_ENTRY(4,11,5),
  433. /* Index 2 - 0010 0x */
  434. CBPY_ENTRY(2,13,5), CBPY_ENTRY(2,13,5),
  435. /* Index 1 - 0010 1x */
  436. CBPY_ENTRY(1,14,5), CBPY_ENTRY(1,14,5),
  437. /* Index 0 - 0011 xx */
  438. CBPY_ENTRY(0,15,4), CBPY_ENTRY(0,15,4), CBPY_ENTRY(0,15,4), CBPY_ENTRY(0,15,4),
  439. /* Index 12- 0100 xx */
  440. CBPY_ENTRY(12,3,4), CBPY_ENTRY(12,3,4), CBPY_ENTRY(12,3,4), CBPY_ENTRY(12,3,4),
  441. /* Index 10- 0101 xx */
  442. CBPY_ENTRY(10,5,4), CBPY_ENTRY(10,5,4), CBPY_ENTRY(10,5,4), CBPY_ENTRY(10,5,4),
  443. /* Index 14- 0110 xx */
  444. CBPY_ENTRY(14,1,4), CBPY_ENTRY(14,1,4), CBPY_ENTRY(14,1,4), CBPY_ENTRY(14,1,4),
  445. /* Index 5 - 0111 xx */
  446. CBPY_ENTRY(5,10,4), CBPY_ENTRY(5,10,4), CBPY_ENTRY(5,10,4), CBPY_ENTRY(5,10,4),
  447. /* Index 13- 1000 xx */
  448. CBPY_ENTRY(13,2,4), CBPY_ENTRY(13,2,4), CBPY_ENTRY(13,2,4), CBPY_ENTRY(13,2,4),
  449. /* Index 3 - 1001 xx */
  450. CBPY_ENTRY(3,12,4), CBPY_ENTRY(3,12,4), CBPY_ENTRY(3,12,4), CBPY_ENTRY(3,12,4),
  451. /* Index 11- 1010 xx */
  452. CBPY_ENTRY(11,4,4), CBPY_ENTRY(11,4,4), CBPY_ENTRY(11,4,4), CBPY_ENTRY(11,4,4),
  453. /* Index 7 - 1011 xx */
  454. CBPY_ENTRY(7,8,4), CBPY_ENTRY(7,8,4), CBPY_ENTRY(7,8,4), CBPY_ENTRY(7,8,4),
  455. /* Index 15- 11xx xx */
  456. CBPY_ENTRY(15,0,2), CBPY_ENTRY(15,0,2), CBPY_ENTRY(15,0,2), CBPY_ENTRY(15,0,2),
  457. CBPY_ENTRY(15,0,2), CBPY_ENTRY(15,0,2), CBPY_ENTRY(15,0,2), CBPY_ENTRY(15,0,2),
  458. CBPY_ENTRY(15,0,2), CBPY_ENTRY(15,0,2), CBPY_ENTRY(15,0,2), CBPY_ENTRY(15,0,2),
  459. CBPY_ENTRY(15,0,2), CBPY_ENTRY(15,0,2), CBPY_ENTRY(15,0,2), CBPY_ENTRY(15,0,2)
  460. };
  461. I16 gNewTAB_DQUANT[4] = { -1, -2, 1, 2 };
  462. #ifdef USE_MMX // { USE_MMX
  463. T_pFunc_VLD_RLD_IQ_Block pFunc_VLD_RLD_IQ_Block[2] = {VLD_RLD_IQ_Block, MMX_VLD_RLD_IQ_Block};
  464. #endif // } USE_MMX
  465. #pragma data_seg(".data")
  466. /*****************************************************************************
  467. *
  468. * H263DecodeMBHeader
  469. *
  470. * Decode the MB header
  471. */
  472. #pragma code_seg("IACODE1")
  473. I32 H263DecodeMBHeader(
  474. T_H263DecoderCatalog FAR * DC,
  475. BITSTREAM_STATE FAR * fpbsState,
  476. U32 **pN,
  477. T_MBInfo FAR * fpMBInfo)
  478. {
  479. I32 iReturn = ICERR_ERROR;
  480. U8 FAR * fpu8;
  481. U32 uBitsReady;
  482. U32 uWork;
  483. U32 uResult;
  484. U32 uCode;
  485. U32 uBitCount;
  486. U32 uMBType;
  487. U32 bCoded;
  488. U32 bStuffing;
  489. U32 bGetCBPB;
  490. U32 bGetMVDB;
  491. U32 i;
  492. FX_ENTRY("H263DecodeMBHeader");
  493. GET_BITS_RESTORE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  494. // COD -----------------------------------------------------
  495. ReadCOD:
  496. if (! DC->bKeyFrame)
  497. {
  498. GET_ONE_BIT(fpu8, uWork, uBitsReady, uResult);
  499. bCoded = !uResult; /* coded when bit is set to zero */
  500. }
  501. else
  502. bCoded = 1;
  503. DC->bCoded = (U16) bCoded;
  504. if (!bCoded)
  505. {
  506. /* when a block is not coded, "the remaining part of the macroblock
  507. * layer is empty; in that case the decoder shall treat the macroblock
  508. * as in INTER block with motion vector for the whole block equal to
  509. * zero and with no coefficient data" (5.3.1 p 16).
  510. */
  511. DC->uMBType = 0;
  512. fpMBInfo->i8MBType = 0; // AP-NEW
  513. DC->uCBPC = 0;
  514. DC->uCBPY = 0;
  515. /* Now update the pN array. Since the block is not coded, write 0
  516. * for all blocks in the macroblock.
  517. */
  518. if (DC->bPBFrame)
  519. { // 12 blocks for a PB frame
  520. fpMBInfo->i8MVDBx2 = fpMBInfo->i8MVDBy2 = 0;
  521. for (i=0; i<12; i++)
  522. { // PB-NEW
  523. **pN = 0;
  524. (*pN)++;
  525. }
  526. }
  527. else
  528. { // only 6 blocks for non PB frame
  529. for (i=0; i<6; i++)
  530. { // NEW
  531. **pN = 0;
  532. (*pN)++;
  533. }
  534. }
  535. #ifdef H263P
  536. // If block is not coded, we use the original PB-frame method in Annex G.
  537. // In other words, use bidirectional prediction (not forward only)
  538. fpMBInfo->bForwardPredOnly = FALSE;
  539. #endif
  540. GET_BITS_SAVE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  541. iReturn = ICERR_OK;
  542. goto done;
  543. }
  544. // MCBPC ---------------------------------------------------
  545. bStuffing = 0;
  546. if (DC->bKeyFrame)
  547. {
  548. GET_VARIABLE_BITS(6, fpu8, uWork, uBitsReady, uResult,
  549. uCode, uBitCount, gNewTAB_MCBPC_INTRA);
  550. if (uCode == 0)
  551. {
  552. /* start of the stuffing code - read the next 3-bits
  553. */
  554. GET_FIXED_BITS(3, fpu8, uWork, uBitsReady, uResult);
  555. if (uResult == 1)
  556. bStuffing = 1;
  557. else
  558. {
  559. ERRORMESSAGE(("%s: Incorrect key frame stuffing bits!\r\n", _fx_));
  560. //#ifdef LOSS_RECOVERY
  561. // Always True if (uBitsReady <0) uBitsReady += 9;//trick and trap, do not change it without consulting Chad
  562. GET_BITS_SAVE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  563. iReturn = PACKET_FAULT;
  564. //#else
  565. // iReturn = ICERR_ERROR;
  566. //#endif
  567. goto done;
  568. }
  569. }
  570. }
  571. else
  572. {
  573. // Delta Frame
  574. // mcpbc, VLD
  575. GET_VARIABLE_BITS(9, fpu8, uWork, uBitsReady, uResult,
  576. uCode, uBitCount, gNewTAB_MCBPC_INTER);
  577. if (uCode == 1)
  578. bStuffing = 1;
  579. //#ifdef LOSS_RECOVERY
  580. else if (uCode == 0) //catch the illegal code
  581. {
  582. ERRORMESSAGE(("%s: Incorrect stuffing bits!\r\n", _fx_));
  583. // Always True if (uBitsReady <0) uBitsReady += 9;//trick and trap, do not change it without consulting Chad
  584. GET_BITS_SAVE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  585. iReturn = PACKET_FAULT;
  586. goto done;
  587. }
  588. //#endif
  589. }
  590. /* When MCBPC==Stuffing, the remaining part of the macroblock layer is
  591. * skipped and the macroblock number is not incremented (5.3.2 p18)"
  592. * We support this by jumping to the start - to look for COD
  593. */
  594. if (bStuffing)
  595. goto ReadCOD;
  596. uMBType = MCBPC_MBTYPE(uResult);
  597. if (DC->bKeyFrame && (uMBType != 3 && uMBType != 4))
  598. {
  599. ERRORMESSAGE(("%s: Bad key frame MBType!\r\n", _fx_));
  600. iReturn = ICERR_ERROR;
  601. goto done;
  602. }
  603. DC->uMBType = uMBType;
  604. fpMBInfo->i8MBType = (I8) uMBType;
  605. DC->uCBPC = MCBPC_CBPC(uResult);
  606. // MODB ----------------------------------------------------
  607. bGetCBPB = 0;
  608. bGetMVDB = 0;
  609. if (DC->bPBFrame)
  610. {
  611. ASSERT( !DC->bKeyFrame);
  612. #ifdef H263P
  613. // Default is to use original PB-frame method in Annex G.
  614. fpMBInfo->bForwardPredOnly = FALSE;
  615. if (DC->bImprovedPBFrames)
  616. {
  617. // See modified TABLE 8/H.263, Annex M, document LBC-96-358
  618. GET_FIXED_BITS(1, fpu8, uWork, uBitsReady, uResult);
  619. if (uResult)
  620. {
  621. // 1xx
  622. GET_FIXED_BITS(1, fpu8, uWork, uBitsReady, uResult);
  623. bGetCBPB = uResult;
  624. if (!uResult)
  625. // 10x
  626. bGetMVDB = 1;
  627. else
  628. {
  629. // 11x
  630. GET_FIXED_BITS(1, fpu8, uWork, uBitsReady, uResult);
  631. bGetMVDB = !uResult;
  632. }
  633. }
  634. if (bGetMVDB)
  635. // B-block is forward predicted (otherwise it is bidirectionally predicted)
  636. fpMBInfo->bForwardPredOnly = TRUE;
  637. }
  638. else
  639. #endif // H263P
  640. {
  641. GET_FIXED_BITS(1, fpu8, uWork, uBitsReady, uResult);
  642. bGetCBPB = uResult; // see section 5.3.3 table 7/H.263
  643. if (bGetCBPB)
  644. {
  645. bGetMVDB = 1;
  646. GET_FIXED_BITS(1, fpu8, uWork, uBitsReady, uResult);
  647. bGetCBPB = uResult;
  648. }
  649. }
  650. }
  651. // CBPB ----------------------------------------------------
  652. DC->u8CBPB = 0;
  653. if (bGetCBPB)
  654. {
  655. ASSERT(!DC->bKeyFrame);
  656. GET_FIXED_BITS(6, fpu8, uWork, uBitsReady, uResult);
  657. DC->u8CBPB = (U8)uResult;
  658. }
  659. // CBPY ----------------------------------------------------
  660. GET_VARIABLE_BITS(6, fpu8, uWork, uBitsReady, uResult,
  661. uCode, uBitCount, gNewTAB_CBPY);
  662. if (uResult == 0)
  663. {
  664. ERRORMESSAGE(("%s: Undefined CBPY variable code!\r\n", _fx_));
  665. iReturn = ICERR_ERROR;
  666. goto done;
  667. }
  668. if (DC->uMBType > 2) //INTRA MB, not intra frame
  669. DC->uCBPY = CBPY_INTRA(uResult);
  670. else
  671. DC->uCBPY = CBPY_INTER(uResult);
  672. // DQUANT --------------------------------------------------
  673. if (DC->uMBType == 1 || DC->uMBType == 4)
  674. {
  675. GET_FIXED_BITS(2, fpu8, uWork, uBitsReady, uResult);
  676. DC->uDQuant = gNewTAB_DQUANT[uResult];
  677. DC->uGQuant += DC->uDQuant;
  678. DC->uPQuant = DC->uGQuant;
  679. } else
  680. DC->uDQuant = 0;
  681. DEBUGMSG(ZONE_DECODE_MB_HEADER, (" %s: MBType = %ld, MCBPC = 0x%lX, CBPY = 0x%lX, DQuant = 0x%lX\r\n", _fx_, DC->uMBType, DC->uCBPC, DC->uCBPY, DC->uDQuant));
  682. // MVD -----------------------------------------------------
  683. DC->i8MVDx2=DC->i8MVDy2=0; //Zero init it anyway.
  684. if ( DC->bPBFrame || DC->uMBType <= 2)
  685. {
  686. GET_VARIABLE_BITS_MV(13, fpu8, uWork, uBitsReady, uResult,uCode,
  687. uBitCount, gTAB_MVD_MAJOR, gTAB_MVD_MINOR);
  688. if (!uResult)
  689. {
  690. ERRORMESSAGE(("%s: Undefined Motion Vector code!\r\n", _fx_));
  691. iReturn = ICERR_ERROR;
  692. goto done;
  693. }
  694. DC->i8MVDx2 = (I8)(uResult>>8);
  695. GET_VARIABLE_BITS_MV(13, fpu8, uWork, uBitsReady, uResult,uCode,
  696. uBitCount, gTAB_MVD_MAJOR, gTAB_MVD_MINOR);
  697. if (!uResult)
  698. {
  699. ERRORMESSAGE(("%s: Bad Motion Vector VLC!\r\n", _fx_));
  700. iReturn = ICERR_ERROR;
  701. goto done;
  702. }
  703. DC->i8MVDy2 = (I8)(uResult>>8);
  704. }
  705. // MVD 2-4 -------------------------------------------------
  706. #ifdef H263P
  707. // In H.263+, 8x8 motion vectors are possible if the deblocking
  708. // filter is selected.
  709. if ((DC->bAdvancedPrediction || DC->bDeblockingFilter)
  710. && (DC->uMBType == 2) )
  711. #else
  712. if (DC->bAdvancedPrediction && (DC->uMBType == 2) )
  713. #endif
  714. {
  715. DC->i8MVD2x2 = DC->i8MVD2y2 = 0;
  716. GET_VARIABLE_BITS_MV(13, fpu8, uWork, uBitsReady, uResult,uCode,
  717. uBitCount, gTAB_MVD_MAJOR, gTAB_MVD_MINOR);
  718. if (!uResult)
  719. {
  720. ERRORMESSAGE(("%s: Bad Block Motion Vector VLC!\r\n", _fx_));
  721. iReturn = ICERR_ERROR;
  722. goto done;
  723. }
  724. DC->i8MVD2x2 = (I8)(uResult>>8);
  725. GET_VARIABLE_BITS_MV(13, fpu8, uWork, uBitsReady, uResult,uCode,
  726. uBitCount, gTAB_MVD_MAJOR, gTAB_MVD_MINOR);
  727. if (!uResult)
  728. {
  729. ERRORMESSAGE(("%s: Bad Block Motion Vector VLC!\r\n", _fx_));
  730. iReturn = ICERR_ERROR;
  731. goto done;
  732. }
  733. DC->i8MVD2y2 = (I8)(uResult>>8);
  734. DC->i8MVD3x2 = DC->i8MVD3y2 = 0;
  735. GET_VARIABLE_BITS_MV(13, fpu8, uWork, uBitsReady, uResult,uCode,
  736. uBitCount, gTAB_MVD_MAJOR, gTAB_MVD_MINOR);
  737. if (!uResult)
  738. {
  739. ERRORMESSAGE(("%s: Bad Block Motion Vector VLC!\r\n", _fx_));
  740. iReturn = ICERR_ERROR;
  741. goto done;
  742. }
  743. DC->i8MVD3x2 = (I8)(uResult>>8);
  744. GET_VARIABLE_BITS_MV(13, fpu8, uWork, uBitsReady, uResult,uCode,
  745. uBitCount, gTAB_MVD_MAJOR, gTAB_MVD_MINOR);
  746. if (!uResult)
  747. {
  748. ERRORMESSAGE(("%s: Bad Block Motion Vector VLC!\r\n", _fx_));
  749. iReturn = ICERR_ERROR;
  750. goto done;
  751. }
  752. DC->i8MVD3y2 = (I8)(uResult>>8);
  753. DC->i8MVD4x2 = DC->i8MVD4y2 = 0;
  754. GET_VARIABLE_BITS_MV(13, fpu8, uWork, uBitsReady, uResult,uCode,
  755. uBitCount, gTAB_MVD_MAJOR, gTAB_MVD_MINOR);
  756. if (!uResult)
  757. {
  758. ERRORMESSAGE(("%s: Bad Block Motion Vector VLC!\r\n", _fx_));
  759. iReturn = ICERR_ERROR;
  760. goto done;
  761. }
  762. DC->i8MVD4x2 = (I8)(uResult>>8);
  763. GET_VARIABLE_BITS_MV(13, fpu8, uWork, uBitsReady, uResult,uCode,
  764. uBitCount, gTAB_MVD_MAJOR, gTAB_MVD_MINOR);
  765. if (!uResult)
  766. {
  767. ERRORMESSAGE(("%s: Bad Block Motion Vector VLC!\r\n", _fx_));
  768. iReturn = ICERR_ERROR;
  769. goto done;
  770. }
  771. DC->i8MVD4y2 = (I8)(uResult>>8);
  772. DEBUGMSG(ZONE_DECODE_MB_HEADER, (" %s: MVD2x2 = %d, MVD2y2 = %d, MVD3x2 = %d, MVD3y2 = %d, MVD4x2 = %d, MVD4y2 = %d\r\n", _fx_, DC->i8MVD2x2, DC->i8MVD2y2, DC->i8MVD3x2, DC->i8MVD3y2, DC->i8MVD4x2, DC->i8MVD4y2));
  773. }
  774. // MVDB ----------------------------------------------------
  775. DC->i8MVDBx2 = DC->i8MVDBy2 = 0; //Zero init it anyway.
  776. fpMBInfo->i8MVDBx2 = fpMBInfo->i8MVDBy2 = 0;
  777. if (bGetMVDB)
  778. {
  779. ASSERT(DC->bPBFrame);
  780. ASSERT(!DC->bKeyFrame);
  781. GET_VARIABLE_BITS_MV(13, fpu8, uWork, uBitsReady, uResult,uCode,
  782. uBitCount, gTAB_MVD_MAJOR, gTAB_MVD_MINOR);
  783. if (!uResult)
  784. {
  785. ERRORMESSAGE(("%s: Bad Motion Vector MVDB!\r\n", _fx_));
  786. iReturn = ICERR_ERROR;
  787. goto done;
  788. }
  789. fpMBInfo->i8MVDBx2 = DC->i8MVDBx2 = (I8)(uResult>>8);
  790. GET_VARIABLE_BITS_MV(13, fpu8, uWork, uBitsReady, uResult,uCode,
  791. uBitCount, gTAB_MVD_MAJOR, gTAB_MVD_MINOR);
  792. if (!uResult)
  793. {
  794. ERRORMESSAGE(("%s: Bad Motion Vector MVDB!\r\n", _fx_));
  795. iReturn = ICERR_ERROR;
  796. goto done;
  797. }
  798. fpMBInfo->i8MVDBy2 = DC->i8MVDBy2 = (I8)(uResult>>8);
  799. }
  800. GET_BITS_SAVE_STATE(fpu8, uWork, uBitsReady, fpbsState)
  801. iReturn = ICERR_OK;
  802. done:
  803. return iReturn;
  804. } /* end H263DecodeMBHeader() */
  805. #pragma code_seg()
  806. /*****************************************************************************
  807. *
  808. * H263DecodeIDCTCoeffs
  809. *
  810. * Decode each of the blocks in this macro block
  811. */
  812. #pragma code_seg("IACODE1")
  813. I32 H263DecodeIDCTCoeffs(
  814. T_H263DecoderCatalog FAR * DC,
  815. T_BlkAction FAR * fpBlockAction,
  816. U32 uBlockNumber,
  817. BITSTREAM_STATE FAR * fpbsState,
  818. U8 FAR * fpu8MaxPtr,
  819. U32 **pN, // NEW
  820. T_IQ_INDEX **pRUN_INVERSE_Q) // NEW
  821. {
  822. I32 iResult = ICERR_ERROR;
  823. int iBlockPattern;
  824. int i;
  825. U8 u8PBlkType;
  826. U32 uBitsReady;
  827. U32 uBitsReadIn;
  828. U32 uBitsReadOut;
  829. U8 u8Quant; // quantization level for this Mblock
  830. U8 FAR * fpu8;
  831. U32 uByteCnt;
  832. T_BlkAction FAR * pActionStream;
  833. #ifdef USE_MMX // { USE_MMX
  834. T_pFunc_VLD_RLD_IQ_Block pFunc_VLD = pFunc_VLD_RLD_IQ_Block[DC->bMMXDecoder];
  835. #else // }{ USE_MMX
  836. T_pFunc_VLD_RLD_IQ_Block pFunc_VLD = VLD_RLD_IQ_Block;
  837. #endif // } USE_MMX
  838. pActionStream = fpBlockAction;
  839. FX_ENTRY("H263DecodeIDCTCoeffs");
  840. /* On input the pointer points to the next byte. We need to change it to
  841. * point to the current word on a 32-bit boundary.
  842. */
  843. fpu8 = fpbsState->fpu8 - 1; /* point to the current byte */
  844. uBitsReady = fpbsState->uBitsReady;
  845. while (uBitsReady >= 8)
  846. {
  847. fpu8--;
  848. uBitsReady -= 8;
  849. }
  850. uBitsReadIn = 8 - uBitsReady;
  851. u8Quant = (U8) (DC->uGQuant);
  852. if ( (DC->bPBFrame) || ((!DC->bKeyFrame) && (DC->uMBType <= 2)))
  853. {
  854. // calculate motion vectors for the 6 blocks in this MB
  855. iResult = H263ComputeMotionVectors(DC, fpBlockAction); // NEW
  856. if (iResult != ICERR_OK)
  857. {
  858. ERRORMESSAGE(("%s: Error decoding MV!\r\n", _fx_));
  859. goto done;
  860. }
  861. } // endif PB or (inter and not key)
  862. // create block pattern from CBPY & CBPC
  863. iBlockPattern = ( (int) DC->uCBPY ) << 2;
  864. iBlockPattern |= (int) DC->uCBPC;
  865. // Decode all 6 blocks up to, but not including, IDCT.
  866. for (i=0; i<6; i++)
  867. {
  868. if (iBlockPattern & 0x20)
  869. {
  870. if (DC->uMBType >= 3)
  871. fpBlockAction->u8BlkType = BT_INTRA;
  872. else
  873. fpBlockAction->u8BlkType = BT_INTER;
  874. }
  875. else
  876. {
  877. if (DC->uMBType >= 3)
  878. fpBlockAction->u8BlkType = BT_INTRA_DC;
  879. else
  880. fpBlockAction->u8BlkType = BT_EMPTY;
  881. }
  882. if (fpBlockAction->u8BlkType != BT_EMPTY)
  883. {
  884. fpBlockAction->u8Quant = u8Quant;
  885. ASSERT(fpBlockAction->pCurBlock != NULL);
  886. ASSERT(fpBlockAction->uBlkNumber == uBlockNumber);
  887. uBitsReadOut = (*pFunc_VLD)(
  888. fpBlockAction,
  889. fpu8,
  890. uBitsReadIn,
  891. (U32 *) *pN,
  892. (U32 *) *pRUN_INVERSE_Q);
  893. if (uBitsReadOut == 0)
  894. {
  895. ERRORMESSAGE(("%s: Error decoding P block: VLD_RLD_IQ_Block return 0 bits read...\r\n", _fx_));
  896. goto done;
  897. }
  898. ASSERT( **pN < 65);
  899. *pRUN_INVERSE_Q += **pN; // NEW
  900. if (fpBlockAction->u8BlkType != BT_INTER) // NEW
  901. **pN += 65; // NEW
  902. (*pN)++;
  903. uByteCnt = uBitsReadOut >> 3; /* divide by 8 */
  904. uBitsReadIn = uBitsReadOut & 0x7; /* mod 8 */
  905. fpu8 += uByteCnt;
  906. // allow the pointer to address up to four beyond the end - reading
  907. // by DWORD using postincrement; otherwise we have bitstream error.
  908. if (fpu8 > fpu8MaxPtr+4)
  909. goto done;
  910. // The test matrix includes the debug version of the driver. The
  911. // following assertion creates a problem when testing with VideoPhone
  912. // and so please do not check-in a version with the assertion
  913. // uncommented.
  914. // ASSERT(fpu8 <= fpu8MaxPtr+4);
  915. }
  916. else
  917. { // block type is empty
  918. **pN = 0; // NEW
  919. (*pN)++;
  920. }
  921. fpBlockAction++;
  922. iBlockPattern <<= 1;
  923. uBlockNumber++;
  924. } // end for (i=0; i<6; i++)
  925. //--------------------------------------------------------------------
  926. //
  927. // Now do the 6 B-blocks -- if needed
  928. //
  929. //--------------------------------------------------------------------
  930. if (DC->bPBFrame)
  931. { // we are doing PB frames
  932. fpBlockAction = pActionStream; // recover the block action stream pointer
  933. uBlockNumber -= 6;
  934. iBlockPattern = (int) DC->u8CBPB; // block pattern
  935. u8Quant = (U8) ( DC->uPQuant * (5 + DC->uDBQuant) / 4 );
  936. if (u8Quant > 31) u8Quant = 31;
  937. if (u8Quant < 1) u8Quant = 1;
  938. // Decode all 6 blocks up to, but not including, IDCT.
  939. for (i=0; i<6; i++)
  940. {
  941. // if the block is coded
  942. if (iBlockPattern & 0x20) {
  943. // preserve the block type of the P-frame block
  944. u8PBlkType = fpBlockAction->u8BlkType;
  945. fpBlockAction->u8BlkType = BT_INTER;
  946. fpBlockAction->u8Quant = u8Quant;
  947. ASSERT(fpBlockAction->pBBlock != NULL);
  948. ASSERT(fpBlockAction->uBlkNumber == uBlockNumber);
  949. uBitsReadOut = (*pFunc_VLD)(
  950. fpBlockAction,
  951. fpu8,
  952. uBitsReadIn,
  953. (U32 *) *pN,
  954. (U32 *) *pRUN_INVERSE_Q);
  955. if (uBitsReadOut == 0) {
  956. ERRORMESSAGE(("%s: Error decoding B block: VLD_RLD_IQ_Block return 0 bits read...\r\n", _fx_));
  957. goto done;
  958. }
  959. ASSERT( **pN < 65); // no B-frame Intra blocks
  960. *pRUN_INVERSE_Q += **pN; // NEW
  961. (*pN)++;
  962. uByteCnt = uBitsReadOut >> 3; // divide by 8
  963. uBitsReadIn = uBitsReadOut & 0x7; // mod 8
  964. fpu8 += uByteCnt;
  965. // allow the pointer to address up to four beyond the
  966. // end - reading by DWORD using postincrement; otherwise we
  967. // have bitstream error.
  968. if (fpu8 > fpu8MaxPtr+4)
  969. goto done;
  970. // The test matrix includes the debug version of the driver.
  971. // The following assertion creates a problem when testing with
  972. // VideoPhone and so please do not check-in a version with the
  973. // assertion uncommented.
  974. // ASSERT(fpu8 <= fpu8MaxPtr+4);
  975. // restore the block type of the P-frame block
  976. fpBlockAction->u8BlkType = u8PBlkType;
  977. }
  978. else
  979. { // block is not coded
  980. **pN = 0; // NEW
  981. (*pN)++;
  982. } // end if block is coded ... else ...
  983. fpBlockAction++;
  984. iBlockPattern <<= 1;
  985. uBlockNumber++;
  986. } // end for (i=0; i<6; i++)
  987. } // end if (DC->bPBFrame)
  988. /* restore the scanning pointers to point to the next byte and set the
  989. * uWork and uBitsReady values.
  990. */
  991. while (uBitsReadIn > 8)
  992. {
  993. fpu8++;
  994. uBitsReadIn -= 8;
  995. }
  996. fpbsState->uBitsReady = 8 - uBitsReadIn;
  997. fpbsState->uWork = *fpu8++; /* store the data and point to next byte */
  998. fpbsState->uWork &= GetBitsMask[fpbsState->uBitsReady];
  999. fpbsState->fpu8 = fpu8;
  1000. iResult = ICERR_OK;
  1001. done:
  1002. return iResult;
  1003. } /* END H263DecodeIDCTCoeffs() */
  1004. #pragma code_seg()
  1005. #pragma code_seg("IACODE1")
  1006. I32 H263ComputeMotionVectors(T_H263DecoderCatalog FAR * DC,
  1007. T_BlkAction FAR * fpBlockAction)
  1008. {
  1009. I32 mvx1, mvy1, mvx2, mvy2, mvx3, mvy3; //predictors
  1010. // motion vector predictors for AP
  1011. I32 mvxp[3], mvyp[3];
  1012. // motion vector differences for AP
  1013. I32 mvxd[4], mvyd[4];
  1014. int iAbove; // takes you up one GOB (-1 * # of blocks in a GOB)
  1015. I32 iMBNum;
  1016. I32 iMBOffset;
  1017. I32 mvx, mvy, scratch;
  1018. int i;
  1019. I32 iNumberOfMBsPerGOB; //assume QCIF for Now
  1020. BOOL bNoAbove, bNoRight, bUMV;
  1021. const char QuarterPelRound[] = {0, 1, 0, 0};
  1022. const char SixteenthPelRound[] =
  1023. {0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1};
  1024. FX_ENTRY("H263ComputeMotionVectors");
  1025. DEBUGMSG(ZONE_DECODE_COMPUTE_MOTION_VECTORS, (" %s: MB# = %d, BlockNumber = %d, (MVDx2,MVDy2) = (%d, %d)\r\n", _fx_, fpBlockAction->uBlkNumber/6, fpBlockAction->uBlkNumber, DC->i8MVDx2, DC->i8MVDy2));
  1026. #ifdef _DEBUG
  1027. if (DC->uMBType == 2)
  1028. {
  1029. DEBUGMSG(ZONE_DECODE_COMPUTE_MOTION_VECTORS, (" %s: (MVD2x2,MVD2y2) = (%d, %d), (MVD3x2,MVD3y2) = (%d, %d), (MVD4x2,MVD4y2) = (%d, %d)\r\n", _fx_, DC->i8MVD2x2, DC->i8MVD2y2, DC->i8MVD3x2, DC->i8MVD3y2, DC->i8MVD4x2, DC->i8MVD4y2));
  1030. }
  1031. #endif
  1032. iNumberOfMBsPerGOB = DC->iNumberOfMBsPerGOB;
  1033. iMBNum = fpBlockAction->uBlkNumber / 6;
  1034. iMBOffset = iMBNum % iNumberOfMBsPerGOB;
  1035. iAbove = -6 * iNumberOfMBsPerGOB;
  1036. bNoAbove = (DC->bGOBHeaderPresent) || (iMBNum < iNumberOfMBsPerGOB);
  1037. bNoRight = iMBOffset == (iNumberOfMBsPerGOB - 1);
  1038. bUMV = DC->bUnrestrictedMotionVectors;
  1039. if (DC->uMBType != 2)
  1040. { // One motion vector per macroblock
  1041. // when either a GOB header is present or
  1042. // we are on the first GOB, only look to the left
  1043. if ( bNoAbove )
  1044. {
  1045. // only one predictor
  1046. if (iMBOffset == 0) // when on the left edge,
  1047. mvx2 = mvy2 = 0; // use 0, else
  1048. else
  1049. {
  1050. mvx2 = fpBlockAction[-6 + 1].i8MVx2; // use upper right corner
  1051. mvy2 = fpBlockAction[-6 + 1].i8MVy2; // of MB to the left
  1052. }
  1053. }
  1054. // no GOB header and not the first GOB
  1055. // need all three predictors
  1056. else
  1057. {
  1058. // left predictor
  1059. if (iMBOffset == 0) // when on the left edge,
  1060. mvx1 = mvy1 = 0; // use 0, else
  1061. else
  1062. {
  1063. mvx1 = fpBlockAction[-6 + 1].i8MVx2; // use upper right corner
  1064. mvy1 = fpBlockAction[-6 + 1].i8MVy2; // of MB to the left
  1065. }
  1066. // above predictor
  1067. // use lower left corner
  1068. // of MB directly above
  1069. mvx2 = fpBlockAction[iAbove + 2].i8MVx2;
  1070. mvy2 = fpBlockAction[iAbove + 2].i8MVy2;
  1071. // upper right predictor
  1072. if ( bNoRight ) // when on the right edge
  1073. mvx3 = mvy3 = 0; // use 0
  1074. else
  1075. { // else use lower left corner
  1076. // of MB above & to the right
  1077. mvx3 = fpBlockAction[iAbove + 8].i8MVx2;
  1078. mvy3 = fpBlockAction[iAbove + 8].i8MVy2;
  1079. }
  1080. // select the medium value and place it in mvx2 & mvy2
  1081. MEDIAN(mvx1, mvx2, mvx3, scratch);
  1082. MEDIAN(mvy1, mvy2, mvy3, scratch);
  1083. } // end if (header or 1st GOB) ... else ...
  1084. // mvx2 and mvy2 have the medium predictors compute the motion vector
  1085. // by adding in the difference
  1086. mvx = DC->i8MVDx2 + mvx2;
  1087. mvy = DC->i8MVDy2 + mvy2;
  1088. // check for Unrestricted Motion Vector mode and adjust the motion
  1089. // vector if necessary using the appropriate strategy, finishing
  1090. // the decoding process.
  1091. if (bUMV)
  1092. {
  1093. if (mvx2 > 32)
  1094. {
  1095. if (mvx > 63) mvx -=64;
  1096. }
  1097. else if (mvx2 < -31)
  1098. {
  1099. if (mvx < -63) mvx +=64;
  1100. }
  1101. if (mvy2 > 32)
  1102. {
  1103. if (mvy > 63) mvy -=64;
  1104. }
  1105. else if (mvy2 < -31)
  1106. {
  1107. if (mvy < -63) mvy +=64;
  1108. }
  1109. }
  1110. else
  1111. { // UMV off
  1112. if (mvx > 31) mvx -= 64;
  1113. else if (mvx < -32) mvx += 64;
  1114. if (mvy > 31) mvy -= 64;
  1115. else if (mvy < -32) mvy += 64;
  1116. }
  1117. // save into the block action stream,
  1118. // duplicating for the other 3 Y blocks.
  1119. fpBlockAction[0].i8MVx2 =
  1120. fpBlockAction[1].i8MVx2 =
  1121. fpBlockAction[2].i8MVx2 =
  1122. fpBlockAction[3].i8MVx2 = (I8)mvx;
  1123. fpBlockAction[0].i8MVy2 =
  1124. fpBlockAction[1].i8MVy2 =
  1125. fpBlockAction[2].i8MVy2 =
  1126. fpBlockAction[3].i8MVy2 = (I8)mvy;
  1127. // Chroma motion vectors
  1128. // divide by 2 and round according to spec
  1129. fpBlockAction[4].i8MVx2 =
  1130. fpBlockAction[5].i8MVx2 =
  1131. (mvx >> 1) + QuarterPelRound[mvx & 0x03];
  1132. fpBlockAction[4].i8MVy2 =
  1133. fpBlockAction[5].i8MVy2 =
  1134. (mvy >> 1) + QuarterPelRound[mvy & 0x03];
  1135. } // end one motion vector per macroblock
  1136. else
  1137. {
  1138. // fpBlockAction[iNext[i][j]] points to block #i's (j+1)th predictor
  1139. int iNext[4][3] = {-5,2,8, 0,3,8, -3,0,1, 2,0,1};
  1140. // adjust iNext pointers which need to point to the GOB above
  1141. iNext[0][1] += iAbove; // block 0, mv2 -- block 2 of above MB
  1142. iNext[0][2] += iAbove; // block 0, mv3 -- block 2 of above-right MB
  1143. iNext[1][1] += iAbove; // block 1, mv2 -- block 3 of above MB
  1144. iNext[1][2] += iAbove; // block 1, mv3 -- block 2 of above-right MB
  1145. // fetch motion vector differences
  1146. mvxd[0] = DC->i8MVDx2;
  1147. mvyd[0] = DC->i8MVDy2;
  1148. mvxd[1] = DC->i8MVD2x2;
  1149. mvyd[1] = DC->i8MVD2y2;
  1150. mvxd[2] = DC->i8MVD3x2;
  1151. mvyd[2] = DC->i8MVD3y2;
  1152. mvxd[3] = DC->i8MVD4x2;
  1153. mvyd[3] = DC->i8MVD4y2;
  1154. // loop on Lumina blocks in this MB
  1155. for (i=0, mvx=0, mvy=0; i<4; i++)
  1156. {
  1157. // get predictor 1
  1158. if ( (i&1) || (iMBOffset) )
  1159. { // not on left edge
  1160. mvxp[0] = fpBlockAction[iNext[i][0]].i8MVx2;
  1161. mvyp[0] = fpBlockAction[iNext[i][0]].i8MVy2;
  1162. }
  1163. else
  1164. { // on left edge, zero the predictor
  1165. mvxp[0] = mvyp[0] = 0;
  1166. }
  1167. // for predictors 2 and 3, check if we can
  1168. // look above and that we are on blocks 0 or 1
  1169. if ( (bNoAbove) && (i < 2) )
  1170. {
  1171. // set predictor 2 equal to predictor 1
  1172. mvxp[1] = mvxp[0];
  1173. mvyp[1] = mvyp[0];
  1174. if (bNoRight)
  1175. {
  1176. // if on the right edge, zero predictor 3
  1177. mvxp[2] = mvyp[2] = 0;
  1178. }
  1179. else
  1180. { // else set predictor 3 equal to predictor 1
  1181. mvxp[2] = mvxp[0];
  1182. mvyp[2] = mvyp[0];
  1183. } // end predictor 3
  1184. }
  1185. else
  1186. { // okay to look up
  1187. // get predictor 2
  1188. mvxp[1] = fpBlockAction[iNext[i][1]].i8MVx2;
  1189. mvyp[1] = fpBlockAction[iNext[i][1]].i8MVy2;
  1190. // get predictor 3
  1191. if ( (bNoRight) && (i < 2) ) {
  1192. // if on the right edge, zero predictor 3
  1193. mvxp[2] = mvyp[2] = 0;
  1194. }
  1195. else
  1196. { // else fetch it from the block action stream
  1197. mvxp[2] = fpBlockAction[iNext[i][2]].i8MVx2;
  1198. mvyp[2] = fpBlockAction[iNext[i][2]].i8MVy2;
  1199. } // end predictor 3
  1200. } // end predictors 2 & 3
  1201. // got all of the candidate predictors now get the median
  1202. // output in mv-p[1]
  1203. MEDIAN( mvxp[0], mvxp[1], mvxp[2], scratch);
  1204. MEDIAN( mvyp[0], mvyp[1], mvyp[2], scratch);
  1205. // add in the difference,
  1206. // put the freshly constructed motion vector in mv-p[0]
  1207. // leaving the predictors in mv-p[1] for use if UMV is on.
  1208. mvxp[0] = mvxp[1] + mvxd[i];
  1209. mvyp[0] = mvyp[1] + mvyd[i];
  1210. // check for Unrestricted Motion Vector mode
  1211. // and, if necessary, adjust the motion vector according
  1212. // to the appropriate decoding strategy, thereby
  1213. // finishing the decoding process.
  1214. if ( bUMV )
  1215. {
  1216. if (mvxp[1] > 32)
  1217. {
  1218. if (mvxp[0] > 63) mvxp[0] -=64;
  1219. }
  1220. else if (mvxp[1] < -31)
  1221. {
  1222. if (mvxp[0] < -63) mvxp[0] +=64;
  1223. }
  1224. if (mvyp[1] > 32)
  1225. {
  1226. if (mvyp[0] > 63) mvyp[0] -=64;
  1227. }
  1228. else if (mvyp[1] < -31)
  1229. {
  1230. if (mvyp[0] < -63) mvyp[0] +=64;
  1231. }
  1232. }
  1233. else
  1234. { // UMV off
  1235. if (mvxp[0] > 31) mvxp[0] -= 64;
  1236. else if (mvxp[0] < -32) mvxp[0] += 64;
  1237. if (mvyp[0] > 31) mvyp[0] -= 64;
  1238. else if (mvyp[0] < -32) mvyp[0] += 64;
  1239. }
  1240. // finally store the result in the block action stream and
  1241. // accumulate the sum of Lumina for the Chroma
  1242. mvx += (fpBlockAction[i].i8MVx2 = (I8)mvxp[0]);
  1243. mvy += (fpBlockAction[i].i8MVy2 = (I8)mvyp[0]);
  1244. } // end Lumina vectors
  1245. // Compute the Chroma vectors
  1246. // divide sum of Lumina by 8 and round according to spec
  1247. fpBlockAction[4].i8MVx2 =
  1248. fpBlockAction[5].i8MVx2 =
  1249. (mvx >> 3) + SixteenthPelRound[mvx & 0x0f];
  1250. fpBlockAction[4].i8MVy2 =
  1251. fpBlockAction[5].i8MVy2 =
  1252. (mvy >> 3) + SixteenthPelRound[mvy & 0x0f];
  1253. } // end 4 motion vectors per macroblock
  1254. DEBUGMSG(ZONE_DECODE_COMPUTE_MOTION_VECTORS, (" %s: Motion vector = (%d, %d)\r\n", _fx_, fpBlockAction->i8MVx2, fpBlockAction->i8MVy2));
  1255. #ifdef _DEBUG
  1256. if (DC->uMBType == 2)
  1257. {
  1258. for (int iVector = 1; iVector < 6; iVector++)
  1259. {
  1260. DEBUGMSG(
  1261. ZONE_DECODE_COMPUTE_MOTION_VECTORS,
  1262. (" %s: Motion vector %d = (%d, %d)\r\n",
  1263. _fx_, iVector, fpBlockAction[iVector].i8MVx2, fpBlockAction[iVector].i8MVy2));
  1264. }
  1265. }
  1266. #endif
  1267. return ICERR_OK;
  1268. }
  1269. #pragma code_seg()