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.

3137 lines
102 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. * d1dec.cpp
  17. *
  18. * DESCRIPTION:
  19. * H261 decoder top level functions
  20. *
  21. * Routines: Prototypes in:
  22. * H263InitDecoderGlobal d1dec.h
  23. * H263InitDecoderInstance d1dec.h
  24. * H263Decompress d1dec.h
  25. * H263TermDecoderInstance d1dec.h
  26. */
  27. // $Header: S:\h26x\src\dec\d1dec.cpv 1.69 24 Mar 1997 11:34:36 mbodart $
  28. // $Log: S:\h26x\src\dec\d1dec.cpv $
  29. //
  30. // Rev 1.69 24 Mar 1997 11:34:36 mbodart
  31. // Added check for PREROLL, if so don't display.
  32. //
  33. // Rev 1.68 19 Mar 1997 16:24:36 mbodart
  34. // Fixed potential problem where aspect ratio adjustment to uNewOffsetToLine0
  35. // should not occur for positive pitches.
  36. //
  37. // Rev 1.67 19 Mar 1997 15:01:46 mbodart
  38. // Changes to DibXY to support RGB output with a negative bitmap height.
  39. //
  40. // Rev 1.66 24 Jan 1997 17:05:16 RHAZRA
  41. // RTP change: we now look for an EBS for every frame. If there is one
  42. // then we copy the H.261 bits and the EBS separately into our local
  43. // bitstream buffer, inserting two zero bytes between the H261 bits and
  44. // the EBS. We need the two zero bytes to mark the end of the frame for
  45. // the pass 1 code. If there is no EBS, then we proceed as before by
  46. // copying the bitstream and then adding two zero bytes at the end.
  47. //
  48. // Rev 1.65 22 Jan 1997 13:33:40 RHAZRA
  49. // Since PPM now fills in the source format even for a PSC packet loss,
  50. // the check for format change has been moved back into d1pict.cpp. This
  51. // was how the check was initially designed in the pre-RTP era.
  52. //
  53. // Rev 1.64 23 Dec 1996 16:32:38 MBODART
  54. // Fixed a bug where we allowed more than 33 macro blocks to be present
  55. // in a GOB. Now we return an error in this case.
  56. // Also removed some dead code involving mb_start.
  57. //
  58. // Rev 1.63 16 Dec 1996 14:41:08 RHAZRA
  59. // Changed a bitstream error ASSERT to a bonafide error.
  60. //
  61. // Rev 1.62 16 Dec 1996 09:09:42 RHAZRA
  62. // Now LOSS_RECOVERY mode is turned on by default in Pass 1
  63. //
  64. // Rev 1.61 12 Dec 1996 09:36:04 SCDAY
  65. //
  66. // Changed size of a couple of data structures in H263InitDecoderInstance
  67. // to improve memory footprint
  68. //
  69. // Rev 1.60 18 Nov 1996 17:12:38 MBODART
  70. // Replaced all debug message invocations with Active Movie's DbgLog.
  71. //
  72. // Rev 1.59 13 Nov 1996 11:35:56 RHAZRA
  73. // Added MMX_autosensing.
  74. //
  75. // Rev 1.58 11 Nov 1996 11:03:28 MBODART
  76. // Fixed bug where block action block type was not explicitly initialized for
  77. // skipped macro blocks. This led to the block edge filter being used more
  78. // often than needed.
  79. //
  80. // Rev 1.57 04 Nov 1996 08:43:18 RHAZRA
  81. // Fixed setting MMX on or off via the INI file when the MMX key
  82. // has an illegal value (<0 or > 1) assigned to it.
  83. //
  84. // Rev 1.56 31 Oct 1996 08:58:34 SCDAY
  85. // Raj added support for MMX decoder
  86. //
  87. // Rev 1.55 30 Oct 1996 09:59:46 MBODART
  88. // Fixed mirroring. Need to use absolute value of dst biWidth in most context
  89. // Also made cosmetic changes to DibXY. It's identical to H.263's DibXY, we
  90. // should probably put it into a common file.
  91. //
  92. // Rev 1.54 27 Sep 1996 14:59:32 MBODART
  93. // DECODE_STATS enabled build will now compile, but numbers aren't accurate.
  94. //
  95. // Rev 1.53 26 Sep 1996 12:30:00 RHAZRA
  96. // Added (i) MMX sensing in the decoder and ini file reading (requires a new
  97. // "MMX" section in h263test.ini to turn off MMX on a MMX CPU) and (ii)
  98. // MMX & PentiumPro CCs.
  99. //
  100. // Rev 1.52 25 Sep 1996 17:35:20 BECHOLS
  101. //
  102. // Added code just prior to color conversion that will perform the
  103. // Snapshot copy on request.
  104. //
  105. // Rev 1.51 24 Sep 1996 13:52:24 RHAZRA
  106. // Changed fpBlockAction synchronization to deal with MBAP being biased
  107. // by -1 in the RTP extension.
  108. //
  109. // Rev 1.50 17 Sep 1996 22:08:36 RHAZRA
  110. // Added code in RTP packet loss recovery to read GOB number from the
  111. // bitstream when the packet following the lost packet starts with a
  112. // GOB start code.
  113. //
  114. // Rev 1.49 16 Sep 1996 09:28:56 RHAZRA
  115. // Fixed a bug in MB-level fragmentation recovery.
  116. //
  117. // Rev 1.48 12 Sep 1996 14:23:12 MBODART
  118. // Replaced GlobalAlloc family with HeapAlloc in the H.261 decoder.
  119. //
  120. // Rev 1.47 10 Sep 1996 15:51:42 RHAZRA
  121. // Bug fixes in RTP packet loss recovery when bad GBSC or MBA is
  122. // detected in the PPM generated lost packet.
  123. //
  124. // Rev 1.45 04 Sep 1996 09:52:32 RHAZRA
  125. // Added a new pass 1 function to enable RTp decoder resiliency when
  126. // LOSS_RECOVERY is defined.
  127. //
  128. // Rev 1.44 14 Aug 1996 08:41:04 RHAZRA
  129. //
  130. // Added support for YUV12 and YUY2 color convertors
  131. //
  132. // Rev 1.43 09 Aug 1996 17:23:10 MBODART
  133. // Fixed uninitialized variable bugs: one in decoder rearchitecture, where
  134. // MB type needed to be defined for skipped blocks; and one previously
  135. // existing bug where the block action u8BlkType needed to be defined
  136. // for skip blocks, in order to suppress the BEF on those blocks.
  137. // These bugs render build 027 of H.261 broken.
  138. //
  139. // Rev 1.42 05 Aug 1996 11:00:30 MBODART
  140. //
  141. // H.261 decoder rearchitecture:
  142. // Files changed: d1gob.cpp, d1mblk.{cpp,h}, d1dec.{cpp,h},
  143. // filelist.261, h261_32.mak
  144. // New files: d1bvriq.cpp, d1idct.cpp
  145. // Obsolete files: d1block.cpp
  146. // Work still to be done:
  147. // Update h261_mf.mak
  148. // Optimize uv pairing in d1bvriq.cpp and d1idct.cpp
  149. // Fix checksum code (it doesn't work now)
  150. // Put back in decoder stats
  151. //
  152. // Rev 1.41 10 Jul 1996 08:20:44 SCDAY
  153. // Increased memory allocation for I420
  154. //
  155. // Rev 1.40 03 Jun 1996 12:21:52 AKASAI
  156. // Initialized DC = NULL and added tests so that don't try to free
  157. // and unlock if DC == NULL. This effected the "done" return area
  158. // of H263Decompress and one other place.
  159. //
  160. // Also added checking of return status from reading GOB start code.
  161. //
  162. // Rev 1.39 03 May 1996 15:54:26 AKASAI
  163. // Eliminate allocating space for B frame in decoder. This frame is
  164. // not used.
  165. //
  166. // Rev 1.38 17 Apr 1996 18:36:30 AKASAI
  167. // Updates to use non-distructive color convertors.
  168. // Color Convertor has modified parameter list.
  169. // FrameCopy is called only when BlockEdgeFilter is enabled or
  170. // AdjustPels is enabled or when mirroring is enabled.
  171. // For H.261 bitstreams.
  172. // A frame copy is used for YUV12 when mirroring is enabled or
  173. // AdjustPels is enabled.
  174. //
  175. // Basically normal processing without BEF you don't have to do
  176. // a frame copy which saves ~2msec per frame QCIF.
  177. //
  178. // Rev 1.37 05 Apr 1996 14:22:18 AKASAI
  179. //
  180. // Added support for BlockEdgeFilter.
  181. // Need to change where ReInitializeBlockActionStream was called.
  182. //
  183. // Rev 1.36 21 Mar 1996 16:59:54 AKASAI
  184. // Needed to move location of picture checksum calculation because
  185. // of the swap of Previous and Current Frames.
  186. //
  187. // Rev 1.35 18 Mar 1996 15:52:06 AKASAI
  188. // Many, many changes.
  189. // 1) To optimize for performance eliminated memcpy of current to
  190. // previous frame. Now switch the pointers and re-initialize
  191. // block Action stream. New routine H263ReInitializeBlockActionStream
  192. // written and called after each frame is compressed. This
  193. // change accounted to 3-4 of the 4-5 msec improvment.
  194. // 2) Needed to add call to BlockCopy (NOTE: maybe BlockCopySpecial would
  195. // be faster) to copy any skip blocks at the end of a GOB from
  196. // previous to current. Change was necessary after 1).
  197. // 3) Deleted some dead code
  198. // 4) Changed timing statistic code some.
  199. //
  200. // Rev 1.34 29 Feb 1996 09:20:30 SCDAY
  201. // Added support for mirroring
  202. //
  203. // Rev 1.33 14 Feb 1996 11:54:26 AKASAI
  204. // Update to use new color convertors that fix palette flash.
  205. // Also corrected data alignment problem which improves performance
  206. // of decoder.
  207. //
  208. // Rev 1.32 09 Feb 1996 13:33:36 AKASAI
  209. //
  210. // Updated interface to call new AdjustPels routine. CustomChange
  211. // Brightness, Saturation and Contrast seem to be working but very
  212. // little testing has been done.
  213. //
  214. // Rev 1.31 12 Jan 1996 15:12:34 AKASAI
  215. // Fixed pink blocks in RING0 QCIF TO FCIF by fixing static initialzation
  216. // of GOBUpdate arrays. Was based on input parameter but now on constant.
  217. //
  218. // Rev 1.30 11 Jan 1996 16:57:00 DBRUCKS
  219. //
  220. // added GetDecoderOptions
  221. // added use of bUseBlockEdgeFilter
  222. // added use of bForceOnAspectRatioCorrection
  223. // Changed to do aspect ratio correction for both I420 and H261 if either
  224. // forced or specified by result of the DecompressQuery
  225. //
  226. // Rev 1.29 26 Dec 1995 17:40:54 DBRUCKS
  227. //
  228. // changed bTimerIsOn to bTimingThisFrame because it is used after STOP_TIMER
  229. // fixed YUV12 decode when timer ifdefs are defined
  230. //
  231. // Rev 1.28 26 Dec 1995 12:48:18 DBRUCKS
  232. // remove TIMING code
  233. // add general purpose timing code using d1stat.*
  234. //
  235. // Rev 1.26 21 Dec 1995 17:49:06 AKASAI
  236. // Replaced an uninitialized variable to AdjustPels with the correct on.
  237. // Change of Contrast, Brightness and Saturation is not working correctly.
  238. //
  239. // Rev 1.25 13 Dec 1995 14:23:52 AKASAI
  240. // Deleted setting of Initialized to False; Added calling of H263TermDecoderIn
  241. // if Initialized == True.
  242. //
  243. // Rev 1.24 05 Dec 1995 10:20:12 SCDAY
  244. // Cleaned up warnings
  245. //
  246. // Rev 1.23 17 Nov 1995 15:21:48 BECHOLS
  247. //
  248. // Added ring 0 stuff.
  249. //
  250. // Rev 1.22 17 Nov 1995 15:13:18 SCDAY
  251. //
  252. // Added key field to picture checksum data
  253. //
  254. // Rev 1.21 16 Nov 1995 18:11:42 AGANTZX
  255. // Added p5 timing code (#define TIMING)
  256. //
  257. // Rev 1.20 15 Nov 1995 19:04:12 AKASAI
  258. // Should now be able to play raw YUV12 files. Note: funny white stop
  259. // when I play downriv4.avi.
  260. //
  261. // Rev 1.19 15 Nov 1995 14:27:22 AKASAI
  262. // Added support for YUV12 "if 0" old code with aspec correction and
  263. // 8 to 7 bit conversion. Added FrameCopy calls and DispFrame into structure.
  264. // (Integration point)
  265. //
  266. // Rev 1.18 08 Nov 1995 14:58:02 SCDAY
  267. // Added picture layer checksums
  268. //
  269. // Rev 1.17 03 Nov 1995 11:42:54 AKASAI
  270. //
  271. // Added and changed code to handle MB checksum hopefully better.
  272. //
  273. // Rev 1.16 01 Nov 1995 13:46:02 AKASAI
  274. //
  275. // Added allocation of temporary buffer for loop filter. uFilterBBuffer
  276. // right after uMBBuffer.
  277. //
  278. // Rev 1.15 30 Oct 1995 16:20:26 AKASAI
  279. // Fixed up extra bytes some more. Doug and Sylvia had already decided
  280. // on 2 extra bytes for the decoder instead of 4. We now copy 2 zeros
  281. // at the end of the biSizeImage.
  282. //
  283. // Rev 1.14 30 Oct 1995 15:38:22 AKASAI
  284. // Frame 94 of grouch read past the end of the bit stream finding junk.
  285. // Enabled code Sylvia had put in to copy 4 bytes of zero after biSizeImage.
  286. // This seems to fix the problem playing grouch.avi.
  287. //
  288. // Rev 1.13 27 Oct 1995 19:11:26 AKASAI
  289. // Added some special case code to handle when skip macroblock is last
  290. // in a gob.
  291. //
  292. // Rev 1.12 27 Oct 1995 18:17:22 AKASAI
  293. //
  294. // Put in fix "hack" to keep the block action stream pointers
  295. // in sync between d1dec and d1mblk. With skip macro blocks some
  296. // macroblocks were being processed multiple times. Still a problem
  297. // when gob ends with a skip macroblock.
  298. //
  299. // Rev 1.11 26 Oct 1995 15:33:10 SCDAY
  300. //
  301. // Delta frames partially working -- changed main loops to accommodate
  302. // skipped macroblocks by detecting next startcode
  303. //
  304. // Rev 1.10 16 Oct 1995 13:53:46 SCDAY
  305. //
  306. // Added macroblock level checksum
  307. //
  308. // Rev 1.9 10 Oct 1995 15:44:02 SCDAY
  309. // clean up
  310. //
  311. // Rev 1.8 10 Oct 1995 14:58:10 SCDAY
  312. //
  313. // added support for FCIF
  314. //
  315. // Rev 1.7 06 Oct 1995 15:32:28 SCDAY
  316. //
  317. // Integrated with latest AKK d1block
  318. //
  319. // Rev 1.6 04 Oct 1995 15:24:46 SCDAY
  320. // changed test pattern stuff
  321. //
  322. // Rev 1.5 22 Sep 1995 15:07:02 SCDAY
  323. // Doug fixed ASSERT bug, scd debug changes
  324. //
  325. // Rev 1.2 19 Sep 1995 15:25:32 SCDAY
  326. //
  327. // added H261 pict, GOB, MB/MBA parsing
  328. //
  329. // Rev 1.1 12 Sep 1995 15:52:24 DBRUCKS
  330. // add SKIP_DECODE option for encoder work
  331. //
  332. // Rev 1.0 11 Sep 1995 13:51:48 SCDAY
  333. // Initial revision.
  334. //
  335. // Rev 1.18 05 Sep 1995 17:22:12 DBRUCKS
  336. // u & v are offset by 8 from Y in YVU12ForEnc
  337. //
  338. // Rev 1.17 01 Sep 1995 17:13:52 DBRUCKS
  339. // add adjustpels
  340. //
  341. // Rev 1.16 01 Sep 1995 09:49:34 DBRUCKS
  342. // checkin partial ajdust pels changes
  343. //
  344. // Rev 1.15 29 Aug 1995 16:50:40 DBRUCKS
  345. // add support for YVU9 playback
  346. //
  347. // Rev 1.14 28 Aug 1995 17:45:58 DBRUCKS
  348. // add yvu12forenc
  349. //
  350. // Rev 1.13 28 Aug 1995 10:15:14 DBRUCKS
  351. // update to 5 July Spec and 8/25 Errata
  352. //
  353. // Rev 1.12 24 Aug 1995 08:51:30 CZHU
  354. // Turned off apsect ratio correction.
  355. //
  356. // Rev 1.11 23 Aug 1995 12:25:10 DBRUCKS
  357. // Turn on the color converters
  358. //
  359. // Rev 1.10 14 Aug 1995 16:40:34 DBRUCKS
  360. // initialize block action stream
  361. //
  362. // Rev 1.9 11 Aug 1995 17:47:58 DBRUCKS
  363. // cleanup
  364. //
  365. // Rev 1.8 11 Aug 1995 17:30:00 DBRUCKS
  366. // copy source to bitstream
  367. //
  368. // Rev 1.7 11 Aug 1995 16:12:14 DBRUCKS
  369. // add ptr check to MB data and add #ifndef early exit
  370. //
  371. // Rev 1.6 11 Aug 1995 15:10:18 DBRUCKS
  372. // get ready to integrate with block level code and hook up macro block level code
  373. //
  374. // Rev 1.5 03 Aug 1995 14:57:56 DBRUCKS
  375. // Add ASSERT macro
  376. //
  377. // Rev 1.4 02 Aug 1995 15:31:34 DBRUCKS
  378. // added GOB header parsing
  379. //
  380. // Rev 1.3 01 Aug 1995 12:27:38 DBRUCKS
  381. // add PSC parsing
  382. //
  383. // Rev 1.2 31 Jul 1995 16:28:00 DBRUCKS
  384. // move loacl BITS defs to D3DEC.CPP
  385. //
  386. // Rev 1.1 31 Jul 1995 15:32:22 CZHU
  387. // Moved global tables to d3tables.h
  388. //
  389. // Rev 1.0 31 Jul 1995 13:00:04 DBRUCKS
  390. // Initial revision.
  391. //
  392. // Rev 1.3 28 Jul 1995 13:57:36 CZHU
  393. // Started to add picture level decoding of fixed length codes.
  394. //
  395. // Rev 1.2 24 Jul 1995 14:57:52 CZHU
  396. // Added global tables for VLD decoding. Also added instance initialization
  397. // and termination. Several data structures are updated for H.263.
  398. //
  399. // Rev 1.1 17 Jul 1995 14:46:20 CZHU
  400. //
  401. //
  402. // Rev 1.0 17 Jul 1995 14:14:40 CZHU
  403. // Initial revision.
  404. //////////////////////////////////////////////////////////////////////////////
  405. #include "precomp.h"
  406. static int iNumberOfGOBsBySourceFormat[2] = {
  407. 3, /* QCIF */
  408. // 10,
  409. 12, /* CIF */
  410. };
  411. static int iNumberOfMBsInAGOBBySourceFormat[2] = {
  412. 33, /* QCIF */
  413. 33, /* CIF */
  414. };
  415. // rearch
  416. //#ifndef LOSS_RECOVERY
  417. #if 0
  418. static LRESULT IAPass1ProcessFrame(
  419. T_H263DecoderCatalog *DC,
  420. T_BlkAction *fpBlockAction,
  421. T_MBInfo *fpMBInfo,
  422. BITSTREAM_STATE *fpbsState,
  423. U8 *fpu8MaxPtr,
  424. U32 *pN,
  425. T_IQ_INDEX *pRUN_INVERSE_Q,
  426. const I32 iNumberOfGOBs,
  427. const I32 iNumberOfMBs,
  428. const I32 iGOB_start,
  429. const I32 iMB_start
  430. );
  431. #else
  432. static LRESULT IAPass1ProcessFrameRTP(
  433. T_H263DecoderCatalog *DC,
  434. T_BlkAction *fpBlockAction,
  435. T_MBInfo *fpMBInfo,
  436. BITSTREAM_STATE *fpbsState,
  437. U8 *fpu8MaxPtr,
  438. U32 *pN,
  439. T_IQ_INDEX *pRUN_INVERSE_Q,
  440. const I32 iNumberOfGOBs,
  441. const I32 iNumberOfMBs,
  442. const I32 iGOB_start,
  443. const I32 iMB_start
  444. );
  445. #endif
  446. static void IAPass2ProcessFrame(
  447. T_H263DecoderCatalog *DC,
  448. T_BlkAction *fpBlockAction,
  449. T_MBInfo *fpMBInfo,
  450. U32 *pN,
  451. T_IQ_INDEX *pRUN_INVERSE_Q,
  452. const I32 iNumberOfGOBs,
  453. const I32 iNumberOfMBs
  454. );
  455. // rearch
  456. static long DibXY(ICDECOMPRESSEX FAR *lpicDecEx, LPINT lpiPitch, UINT yScale, BOOL bIsDCI);
  457. static void GetDecoderOptions(T_H263DecoderCatalog *);
  458. #define START_CODE 0xff18
  459. static void ZeroFill(HPBYTE hpbY, HPBYTE hpbU, HPBYTE hpbV, int iPitch, U32 uWidth, U32 uHeight);
  460. extern T_H263ColorConvertorCatalog ColorConvertorCatalog[];
  461. extern void BlockCopy(
  462. U32 uDstBlock,
  463. U32 uSrcBlock);
  464. extern void BlockEdgeFilter(U8 *YPlane, int Height, int Width, int Pitch, T_BlkAction *lpBlockAction);
  465. LRESULT H263InitDecoderGlobal(void)
  466. { //For 32-bit decoder, this is empty for now, 7/29/95
  467. //need to add code for 16 bit version.
  468. return ICERR_OK;
  469. }
  470. /////////////////////////////////////////////////////////////////////////
  471. //
  472. // H263InitializeBlockActionStream
  473. //
  474. // Initialize the block action stream
  475. //
  476. static void H263InitializeBlockActionStream(
  477. T_H263DecoderCatalog * DC)
  478. {
  479. U8 FAR * pu8;
  480. U32 uFrameHeight = DC->uFrameHeight;
  481. U32 uFrameWidth = DC->uFrameWidth;
  482. U32 uCurBlock;
  483. U32 uRefBlock;
  484. U32 uBBlock;
  485. U32 uYOffset;
  486. U32 uUOffset;
  487. U32 uVOffset;
  488. U32 x;
  489. U32 y;
  490. U32 g;
  491. U32 uPitch16;
  492. U32 uPitch8;
  493. U32 uYUpdate;
  494. U32 uUVUpdate;
  495. U32 uBlkNumber;
  496. T_BlkAction FAR * fpBlockAction;
  497. // Offsets for stepping thru GOBs for FCIF processing
  498. static U32 uYGOBFCIFUpdate[12] =
  499. {
  500. (PITCH*3*16)-(FCIF_WIDTH>>1),
  501. (FCIF_WIDTH>>1),
  502. (PITCH*3*16)-(FCIF_WIDTH>>1),
  503. (FCIF_WIDTH>>1),
  504. (PITCH*3*16)-(FCIF_WIDTH>>1),
  505. (FCIF_WIDTH>>1),
  506. (PITCH*3*16)-(FCIF_WIDTH>>1),
  507. (FCIF_WIDTH>>1),
  508. (PITCH*3*16)-(FCIF_WIDTH>>1),
  509. (FCIF_WIDTH>>1),
  510. (PITCH*3*16)-(FCIF_WIDTH>>1),
  511. (FCIF_WIDTH>>1),
  512. };
  513. static U32 uUVGOBFCIFUpdate[12] =
  514. {
  515. (PITCH*3*8)-(FCIF_WIDTH>>2),
  516. (FCIF_WIDTH>>2),
  517. (PITCH*3*8)-(FCIF_WIDTH>>2),
  518. (FCIF_WIDTH>>2),
  519. (PITCH*3*8)-(FCIF_WIDTH>>2),
  520. (FCIF_WIDTH>>2),
  521. (PITCH*3*8)-(FCIF_WIDTH>>2),
  522. (FCIF_WIDTH>>2),
  523. (PITCH*3*8)-(FCIF_WIDTH>>2),
  524. (FCIF_WIDTH>>2),
  525. (PITCH*3*8)-(FCIF_WIDTH>>2),
  526. (FCIF_WIDTH>>2),
  527. };
  528. // assume that the width and height are multiples of 16
  529. ASSERT((uFrameHeight & 0xF) == 0);
  530. ASSERT((uFrameWidth & 0xF) == 0);
  531. // Init uPitch16 and uPitch8
  532. uPitch16 = PITCH*16;
  533. uPitch8 = PITCH*8;
  534. // Point to the allocated space
  535. pu8 = (U8 FAR *) DC;
  536. uCurBlock = (U32) (pu8 + DC->CurrFrame.X32_YPlane);
  537. uRefBlock = (U32) (pu8 + DC->PrevFrame.X32_YPlane);
  538. uBBlock = (U32) (pu8 + DC->PBFrame.X32_YPlane);
  539. // skip the padding used for unconstrained motion vectors
  540. uYOffset = Y_START;
  541. uUOffset = DC->uSz_YPlane + UV_START;
  542. uVOffset = uUOffset + (PITCH >> 1);
  543. // start with block zero
  544. uBlkNumber = 0;
  545. if (uFrameWidth == QCIF_WIDTH)
  546. { /* if QCIF */
  547. // calculate distance to the next row.
  548. uYUpdate = (16 * PITCH) - uFrameWidth;
  549. uUVUpdate = (8 * PITCH) - (uFrameWidth >> 1);
  550. // Initialize the array
  551. fpBlockAction = (T_BlkAction FAR *) (pu8 + DC->X16_BlkActionStream);
  552. for (y = 0 ; y < uFrameHeight ; y += 16) {
  553. for (x = 0 ; x < uFrameWidth ; x += 16) {
  554. // Four Y Blocks
  555. // Y0 Y1
  556. // Y2 Y3
  557. fpBlockAction->pCurBlock = uCurBlock + uYOffset;
  558. fpBlockAction->pRefBlock = uRefBlock + uYOffset;
  559. fpBlockAction->pBBlock = uBBlock + uYOffset;
  560. fpBlockAction->u8BlkType = BT_EMPTY;
  561. fpBlockAction->uBlkNumber = uBlkNumber++;
  562. uYOffset += 8;
  563. fpBlockAction++;
  564. fpBlockAction->pCurBlock = uCurBlock + uYOffset;
  565. fpBlockAction->pRefBlock = uRefBlock + uYOffset;
  566. fpBlockAction->pBBlock = uBBlock + uYOffset;
  567. fpBlockAction->u8BlkType = BT_EMPTY;
  568. fpBlockAction->uBlkNumber = uBlkNumber++;
  569. uYOffset = uYOffset - 8 + (8 * PITCH);
  570. fpBlockAction++;
  571. fpBlockAction->pCurBlock = uCurBlock + uYOffset;
  572. fpBlockAction->pRefBlock = uRefBlock + uYOffset;
  573. fpBlockAction->pBBlock = uBBlock + uYOffset;
  574. fpBlockAction->u8BlkType = BT_EMPTY;
  575. fpBlockAction->uBlkNumber = uBlkNumber++;
  576. uYOffset += 8;
  577. fpBlockAction++;
  578. fpBlockAction->pCurBlock = uCurBlock + uYOffset;
  579. fpBlockAction->pRefBlock = uRefBlock + uYOffset;
  580. fpBlockAction->pBBlock = uBBlock + uYOffset;
  581. fpBlockAction->u8BlkType = BT_EMPTY;
  582. fpBlockAction->uBlkNumber = uBlkNumber++;
  583. uYOffset = uYOffset + 8 - (8 * PITCH);
  584. fpBlockAction++;
  585. // One CR (V) Block
  586. fpBlockAction->pCurBlock = uCurBlock + uVOffset;
  587. fpBlockAction->pRefBlock = uRefBlock + uVOffset;
  588. fpBlockAction->pBBlock = uBBlock + uVOffset;
  589. fpBlockAction->u8BlkType = BT_EMPTY;
  590. fpBlockAction->uBlkNumber = uBlkNumber++;
  591. uVOffset += 8;
  592. fpBlockAction++;
  593. // One CB (U) Block
  594. fpBlockAction->pCurBlock = uCurBlock + uUOffset;
  595. fpBlockAction->pRefBlock = uRefBlock + uUOffset;
  596. fpBlockAction->pBBlock = uBBlock + uUOffset;
  597. fpBlockAction->u8BlkType = BT_EMPTY;
  598. fpBlockAction->uBlkNumber = uBlkNumber++;
  599. uUOffset += 8;
  600. fpBlockAction++;
  601. }
  602. uYOffset += uYUpdate;
  603. uUOffset += uUVUpdate;
  604. uVOffset += uUVUpdate;
  605. }
  606. } /* end if QCIF */
  607. if (uFrameWidth == FCIF_WIDTH)
  608. { /* if FCIF */
  609. // calculate distance to the next row.
  610. uYUpdate = (16 * PITCH) - (uFrameWidth >> 1);
  611. uUVUpdate = (8 * PITCH) - (uFrameWidth >> 2);
  612. // Initialize the array
  613. fpBlockAction = (T_BlkAction FAR *) (pu8 + DC->X16_BlkActionStream);
  614. for (g = 0; g < 12; g++) { /* for each GOB */
  615. for (y = 0 ; y < 3 ; y++) { /* for each row in GOB */
  616. for (x = 0 ; x < (uFrameWidth >> 1) ; x += 16) {
  617. // Four Y Blocks
  618. // Y0 Y1
  619. // Y2 Y3
  620. fpBlockAction->pCurBlock = uCurBlock + uYOffset;
  621. fpBlockAction->pRefBlock = uRefBlock + uYOffset;
  622. fpBlockAction->pBBlock = uBBlock + uYOffset;
  623. fpBlockAction->u8BlkType = BT_EMPTY;
  624. fpBlockAction->uBlkNumber = uBlkNumber++;
  625. uYOffset += 8;
  626. fpBlockAction++;
  627. fpBlockAction->pCurBlock = uCurBlock + uYOffset;
  628. fpBlockAction->pRefBlock = uRefBlock + uYOffset;
  629. fpBlockAction->pBBlock = uBBlock + uYOffset;
  630. fpBlockAction->u8BlkType = BT_EMPTY;
  631. fpBlockAction->uBlkNumber = uBlkNumber++;
  632. uYOffset = uYOffset - 8 + (8 * PITCH);
  633. fpBlockAction++;
  634. fpBlockAction->pCurBlock = uCurBlock + uYOffset;
  635. fpBlockAction->pRefBlock = uRefBlock + uYOffset;
  636. fpBlockAction->pBBlock = uBBlock + uYOffset;
  637. fpBlockAction->u8BlkType = BT_EMPTY;
  638. fpBlockAction->uBlkNumber = uBlkNumber++;
  639. uYOffset += 8;
  640. fpBlockAction++;
  641. fpBlockAction->pCurBlock = uCurBlock + uYOffset;
  642. fpBlockAction->pRefBlock = uRefBlock + uYOffset;
  643. fpBlockAction->pBBlock = uBBlock + uYOffset;
  644. fpBlockAction->u8BlkType = BT_EMPTY;
  645. fpBlockAction->uBlkNumber = uBlkNumber++;
  646. uYOffset = uYOffset + 8 - (8 * PITCH);
  647. fpBlockAction++;
  648. // One CR (V) Block
  649. fpBlockAction->pCurBlock = uCurBlock + uVOffset;
  650. fpBlockAction->pRefBlock = uRefBlock + uVOffset;
  651. fpBlockAction->pBBlock = uBBlock + uVOffset;
  652. fpBlockAction->u8BlkType = BT_EMPTY;
  653. fpBlockAction->uBlkNumber = uBlkNumber++;
  654. uVOffset += 8;
  655. fpBlockAction++;
  656. // One CB (U) Block
  657. fpBlockAction->pCurBlock = uCurBlock + uUOffset;
  658. fpBlockAction->pRefBlock = uRefBlock + uUOffset;
  659. fpBlockAction->pBBlock = uBBlock + uUOffset;
  660. fpBlockAction->u8BlkType = BT_EMPTY;
  661. fpBlockAction->uBlkNumber = uBlkNumber++;
  662. uUOffset += 8;
  663. fpBlockAction++;
  664. }
  665. uYOffset += uPitch16 - (uFrameWidth >> 1);
  666. uUOffset += uPitch8 - (uFrameWidth >> 2);
  667. uVOffset += uPitch8 - (uFrameWidth >> 2);
  668. }
  669. uYOffset -= uYGOBFCIFUpdate[g];
  670. uUOffset -= uUVGOBFCIFUpdate[g];
  671. uVOffset -= uUVGOBFCIFUpdate[g];
  672. }
  673. } /* end if FCIF */
  674. } // end H263InitializeBlockActionStream()
  675. /////////////////////////////////////////////////////////////////////////
  676. //
  677. // H261ReInitializeBlockActionStream
  678. //
  679. // ReInitialize the block action stream
  680. //
  681. static void H261ReInitializeBlockActionStream(
  682. T_H263DecoderCatalog * DC)
  683. {
  684. U8 FAR * pu8;
  685. U32 uFrameHeight = DC->uFrameHeight;
  686. U32 uFrameWidth = DC->uFrameWidth;
  687. U32 utemp;
  688. U32 x;
  689. U32 y;
  690. U32 g;
  691. T_BlkAction FAR * fpBlockAction;
  692. pu8 = (U8 FAR *) DC;
  693. if (uFrameWidth == QCIF_WIDTH)
  694. { /* if QCIF */
  695. // Initialize the array
  696. fpBlockAction = (T_BlkAction FAR *) (pu8 + DC->X16_BlkActionStream);
  697. for (y = 0 ; y < uFrameHeight ; y += 16) {
  698. for (x = 0 ; x < uFrameWidth ; x += 16) {
  699. // Four Y Blocks
  700. // Y0 Y1
  701. // Y2 Y3
  702. utemp = fpBlockAction->pCurBlock;
  703. fpBlockAction->pCurBlock = fpBlockAction->pRefBlock;
  704. fpBlockAction->pRefBlock = utemp;
  705. fpBlockAction->i8MVX=0;
  706. fpBlockAction->i8MVY=0;
  707. fpBlockAction->u8BlkType = BT_EMPTY;
  708. fpBlockAction++;
  709. utemp = fpBlockAction->pCurBlock;
  710. fpBlockAction->pCurBlock = fpBlockAction->pRefBlock;
  711. fpBlockAction->pRefBlock = utemp;
  712. fpBlockAction->i8MVX=0;
  713. fpBlockAction->i8MVY=0;
  714. fpBlockAction->u8BlkType = BT_EMPTY;
  715. fpBlockAction++;
  716. utemp = fpBlockAction->pCurBlock;
  717. fpBlockAction->pCurBlock = fpBlockAction->pRefBlock;
  718. fpBlockAction->pRefBlock = utemp;
  719. fpBlockAction->i8MVX=0;
  720. fpBlockAction->i8MVY=0;
  721. fpBlockAction->u8BlkType = BT_EMPTY;
  722. fpBlockAction++;
  723. utemp = fpBlockAction->pCurBlock;
  724. fpBlockAction->pCurBlock = fpBlockAction->pRefBlock;
  725. fpBlockAction->pRefBlock = utemp;
  726. fpBlockAction->i8MVX=0;
  727. fpBlockAction->i8MVY=0;
  728. fpBlockAction->u8BlkType = BT_EMPTY;
  729. fpBlockAction++;
  730. // One CR (V) Block
  731. utemp = fpBlockAction->pCurBlock;
  732. fpBlockAction->pCurBlock = fpBlockAction->pRefBlock;
  733. fpBlockAction->pRefBlock = utemp;
  734. fpBlockAction->i8MVX=0;
  735. fpBlockAction->i8MVY=0;
  736. fpBlockAction->u8BlkType = BT_EMPTY;
  737. fpBlockAction++;
  738. // One CB (U) Block
  739. utemp = fpBlockAction->pCurBlock;
  740. fpBlockAction->pCurBlock = fpBlockAction->pRefBlock;
  741. fpBlockAction->pRefBlock = utemp;
  742. fpBlockAction->i8MVX=0;
  743. fpBlockAction->i8MVY=0;
  744. fpBlockAction->u8BlkType = BT_EMPTY;
  745. fpBlockAction++;
  746. }
  747. }
  748. } /* end if QCIF */
  749. if (uFrameWidth == FCIF_WIDTH)
  750. { /* if FCIF */
  751. // Initialize the array
  752. fpBlockAction = (T_BlkAction FAR *) (pu8 + DC->X16_BlkActionStream);
  753. for (g = 0; g < 12; g++) { /* for each GOB */
  754. for (y = 0 ; y < 3 ; y++) { /* for each row in GOB */
  755. for (x = 0 ; x < (uFrameWidth >> 1) ; x += 16) {
  756. // Four Y Blocks
  757. // Y0 Y1
  758. // Y2 Y3
  759. utemp = fpBlockAction->pCurBlock;
  760. fpBlockAction->pCurBlock = fpBlockAction->pRefBlock;
  761. fpBlockAction->pRefBlock = utemp;
  762. fpBlockAction->i8MVX=0;
  763. fpBlockAction->i8MVY=0;
  764. fpBlockAction->u8BlkType = BT_EMPTY;
  765. fpBlockAction++;
  766. utemp = fpBlockAction->pCurBlock;
  767. fpBlockAction->pCurBlock = fpBlockAction->pRefBlock;
  768. fpBlockAction->pRefBlock = utemp;
  769. fpBlockAction->i8MVX=0;
  770. fpBlockAction->i8MVY=0;
  771. fpBlockAction->u8BlkType = BT_EMPTY;
  772. fpBlockAction++;
  773. utemp = fpBlockAction->pCurBlock;
  774. fpBlockAction->pCurBlock = fpBlockAction->pRefBlock;
  775. fpBlockAction->pRefBlock = utemp;
  776. fpBlockAction->i8MVX=0;
  777. fpBlockAction->i8MVY=0;
  778. fpBlockAction->u8BlkType = BT_EMPTY;
  779. fpBlockAction++;
  780. utemp = fpBlockAction->pCurBlock;
  781. fpBlockAction->pCurBlock = fpBlockAction->pRefBlock;
  782. fpBlockAction->pRefBlock = utemp;
  783. fpBlockAction->i8MVX=0;
  784. fpBlockAction->i8MVY=0;
  785. fpBlockAction->u8BlkType = BT_EMPTY;
  786. fpBlockAction++;
  787. // One CR (V) Block
  788. utemp = fpBlockAction->pCurBlock;
  789. fpBlockAction->pCurBlock = fpBlockAction->pRefBlock;
  790. fpBlockAction->pRefBlock = utemp;
  791. fpBlockAction->i8MVX=0;
  792. fpBlockAction->i8MVY=0;
  793. fpBlockAction->u8BlkType = BT_EMPTY;
  794. fpBlockAction++;
  795. // One CB (U) Block
  796. utemp = fpBlockAction->pCurBlock;
  797. fpBlockAction->pCurBlock = fpBlockAction->pRefBlock;
  798. fpBlockAction->pRefBlock = utemp;
  799. fpBlockAction->i8MVX=0;
  800. fpBlockAction->i8MVY=0;
  801. fpBlockAction->u8BlkType = BT_EMPTY;
  802. fpBlockAction++;
  803. }
  804. }
  805. }
  806. } /* end if FCIF */
  807. } // end H261ReInitializeBlockActionStream()
  808. //////////////////////////////////////////////////////////////////////////////
  809. //
  810. // H263InitDecoderInstance
  811. //
  812. // This function allocates and initializes the per-instance tables used by
  813. // the H263 decoder. Note that in 16-bit Windows, the non-instance-specific
  814. // global tables are copied to the per-instance data segment, so that they
  815. // can be used without segment override prefixes.
  816. //
  817. LRESULT H263InitDecoderInstance(LPDECINST lpInst, int CodecID)
  818. {
  819. U32 u32YActiveHeight, u32YActiveWidth;
  820. U32 u32UVActiveHeight, u32UVActiveWidth;
  821. U32 u32YPlane, u32VUPlanes ,u32YVUPlanes,u32SizeBlkActionStream;
  822. U32 uSizeBitStreamBuffer;
  823. U32 uSizeDecTimingInfo;
  824. U32 lOffset=0;
  825. U32 u32TotalSize;
  826. LRESULT iReturn= ICERR_OK;
  827. U32 * pInitLimit;
  828. U32 * pInitPtr;
  829. // rearch
  830. U32 u32SizeT_IQ_INDEXBuffer, u32SizepNBuffer, u32SizeMBInfoStream; // NEW
  831. // rearch
  832. T_H263DecoderCatalog * DC;
  833. U8 * P32Inst;
  834. SECURITY_ATTRIBUTES EventAttributes; // Used with Snapshot.
  835. if(IsBadWritePtr((LPVOID)lpInst, sizeof(DECINSTINFO)))
  836. {
  837. DBOUT("ERROR :: H263InitDecoderInstance :: ICERR_BADPARAM");
  838. iReturn = ICERR_BADPARAM;
  839. goto done;
  840. }
  841. if ((CodecID == YUV12_CODEC && (lpInst->yres > 480 || lpInst->xres > 640)) ||
  842. (CodecID == H263_CODEC && (lpInst->yres > 288 || lpInst->xres > 352)))
  843. {
  844. DBOUT("ERROR :: H263InitDecoderInstance :: ICERR_BADSIZE");
  845. iReturn = ICERR_BADSIZE;
  846. goto done;
  847. }
  848. if (CodecID == YUV12_CODEC)
  849. {
  850. /* The active height and width must be padded to a multiple of 8
  851. * since the adjustpels routine relies on it.
  852. */
  853. u32YActiveHeight = ((lpInst->yres + 0x7) & (~ 0x7));
  854. u32YActiveWidth = ((lpInst->xres + 0x7) & (~ 0x7));
  855. u32UVActiveHeight = ((lpInst->yres + 0xF) & (~ 0xF)) >> 1;
  856. u32UVActiveWidth = ((lpInst->xres + 0xF) & (~ 0xF)) >> 1;
  857. u32YPlane = u32YActiveWidth * u32YActiveHeight;
  858. u32VUPlanes = u32UVActiveWidth * u32UVActiveHeight * 2;
  859. u32YVUPlanes = u32YPlane + u32VUPlanes;
  860. // added for I420 output support
  861. // wasn't allocating enough memory for YUV12 output, no color convert case
  862. // calculate the block action stream size. The Y portion has one block for
  863. // every 8x8 region. The U and V portion has one block for every 16x16 region.
  864. // We also want to make sure that the size is aligned to a cache line.
  865. u32SizeBlkActionStream = (lpInst->xres >> 3) * (lpInst->yres >> 3);
  866. u32SizeBlkActionStream += ((lpInst->xres >> 4) * (lpInst->yres >> 4)) * 2;
  867. u32SizeBlkActionStream *= sizeof (T_BlkAction);
  868. u32SizeBlkActionStream = (u32SizeBlkActionStream + 31) & ~0x1F;
  869. // calculate the bitstream buffer size. We copy the input data to a buffer
  870. // in our space because we read ahead up to 4 bytes beyond the end of the
  871. // input data. The input data size changes for each frame. So the following
  872. // is a very safe upper bound estimate.
  873. // Add + 2 for extra zeros for start code emulation. AKK
  874. uSizeBitStreamBuffer = lpInst->yres * lpInst->xres + 2;
  875. #ifdef DECODE_STATS
  876. uSizeDecTimingInfo = DEC_TIMING_INFO_FRAME_COUNT * sizeof (DEC_TIMING_INFO);
  877. #else
  878. uSizeDecTimingInfo = 0;
  879. #endif
  880. u32TotalSize = INSTANCE_DATA_FIXED_SIZE +
  881. u32SizeBlkActionStream +
  882. u32YVUPlanes + // current frame
  883. u32YVUPlanes + // prev frame
  884. BLOCK_BUFFER_SIZE +
  885. FILTER_BLOCK_BUFFER_SIZE +
  886. uSizeBitStreamBuffer + // input data
  887. uSizeDecTimingInfo +
  888. 0x1F;
  889. // u32TotalSize = 512L + 0x1FL; /* Just enough space for Decoder Catalog. */
  890. }
  891. else
  892. {
  893. ASSERT(CodecID == H263_CODEC);
  894. u32YActiveHeight = lpInst->yres + UMV_EXPAND_Y + UMV_EXPAND_Y ;
  895. u32YActiveWidth = lpInst->xres + UMV_EXPAND_Y + UMV_EXPAND_Y ;
  896. u32UVActiveHeight = u32YActiveHeight/2;
  897. u32UVActiveWidth = u32YActiveWidth /2;
  898. u32YPlane = PITCH * u32YActiveHeight;
  899. u32VUPlanes = PITCH * u32UVActiveHeight;
  900. u32YVUPlanes = u32YPlane + u32VUPlanes;
  901. // calculate the block action stream size. The Y portion has one block for
  902. // every 8x8 region. The U and V portion has one block for every 16x16 region.
  903. // We also want to make sure that the size is aligned to a cache line.
  904. u32SizeBlkActionStream = (lpInst->xres >> 3) * (lpInst->yres >> 3);
  905. u32SizeBlkActionStream += ((lpInst->xres >> 4) * (lpInst->yres >> 4)) * 2;
  906. u32SizeBlkActionStream *= sizeof (T_BlkAction);
  907. u32SizeBlkActionStream = (u32SizeBlkActionStream + 31) & ~0x1F;
  908. // calculate the bitstream buffer size. We copy the input data to a buffer
  909. // in our space because we read ahead up to 4 bytes beyond the end of the
  910. // input data. The input data size changes for each frame. So the following
  911. // is a very safe upper bound estimate.
  912. // Add + 2 for extra zeros for start code emulation. AKK
  913. // Add some additional to make sure stay dword align (rearch)
  914. uSizeBitStreamBuffer = (lpInst->yres * lpInst->xres + 2 + 4) & ~0x3;
  915. // rearch
  916. // calculate sizes of NEW data structures
  917. u32SizeT_IQ_INDEXBuffer = (lpInst->xres)*(lpInst->yres*2)*
  918. sizeof(T_IQ_INDEX);
  919. u32SizepNBuffer = (lpInst->xres>>4)*(lpInst->yres>>4)*sizeof(U32)*6;
  920. u32SizeMBInfoStream = (lpInst->xres>>4)*(lpInst->yres>>4)*
  921. sizeof(T_MBInfo);
  922. // rearch
  923. #ifdef DECODE_STATS
  924. uSizeDecTimingInfo = DEC_TIMING_INFO_FRAME_COUNT * sizeof (DEC_TIMING_INFO);
  925. #else
  926. uSizeDecTimingInfo = 0;
  927. #endif
  928. u32TotalSize = INSTANCE_DATA_FIXED_SIZE +
  929. u32SizeBlkActionStream +
  930. u32YVUPlanes + // current frame
  931. u32YVUPlanes + // prev frame
  932. BLOCK_BUFFER_SIZE +
  933. FILTER_BLOCK_BUFFER_SIZE +
  934. uSizeBitStreamBuffer + // input data
  935. u32SizeT_IQ_INDEXBuffer + // NEW
  936. u32SizepNBuffer + // NEW
  937. u32SizeMBInfoStream + // PB-NEW
  938. uSizeDecTimingInfo +
  939. 0x1F;
  940. }
  941. /* If already initialized, terminate this instance before allocating
  942. * another.
  943. */
  944. if(lpInst->Initialized == TRUE)
  945. {
  946. H263TermDecoderInstance(lpInst);
  947. }
  948. // allocate the memory for the instance
  949. lpInst->pDecoderInst = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
  950. u32TotalSize);
  951. if (lpInst->pDecoderInst == NULL)
  952. {
  953. DBOUT("ERROR :: H263InitDecoderInstance :: ICERR_MEMORY");
  954. iReturn = ICERR_MEMORY;
  955. goto done;
  956. }
  957. //build the decoder catalog
  958. P32Inst = (U8 *) ((((U32) lpInst->pDecoderInst) + 31) & ~0x1F);
  959. //The catalog of per-instance data is at the start of the per-instance data.
  960. DC = (T_H263DecoderCatalog *) P32Inst;
  961. DC->DecoderType = CodecID;
  962. DC->uFrameHeight = lpInst->yres;
  963. DC->uFrameWidth = lpInst->xres;
  964. DC->uYActiveHeight = u32YActiveHeight;
  965. DC->uYActiveWidth = u32YActiveWidth;
  966. DC->uUVActiveHeight = u32UVActiveHeight;
  967. DC->uUVActiveWidth = u32UVActiveWidth;
  968. DC->uSz_YPlane = u32YPlane;
  969. DC->uSz_VUPlanes = u32VUPlanes;
  970. DC->uSz_YVUPlanes = u32YVUPlanes;
  971. DC->BrightnessSetting = H26X_DEFAULT_BRIGHTNESS;
  972. DC->ContrastSetting = H26X_DEFAULT_CONTRAST;
  973. DC->SaturationSetting = H26X_DEFAULT_SATURATION;
  974. DC->iAPColorConvPrev = 0;
  975. DC->pAPInstPrev = NULL; // assume no previous AP instance.
  976. DC->p16InstPostProcess = NULL;
  977. DC->a16InstPostProcess = NULL;
  978. DC->bReadSrcFormat = 0;
  979. EventAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
  980. EventAttributes.lpSecurityDescriptor = NULL;
  981. EventAttributes.bInheritHandle = FALSE;
  982. DC->SnapshotEvent = CreateEvent(&EventAttributes, TRUE, FALSE, NULL);
  983. /* Get the Options
  984. */
  985. GetDecoderOptions(DC);
  986. if (CodecID == H263_CODEC)
  987. {
  988. lOffset = INSTANCE_DATA_FIXED_SIZE;
  989. DC->Ticker = 127;
  990. //instance dependent table here
  991. DC->X16_BlkActionStream = lOffset;
  992. lOffset += u32SizeBlkActionStream;
  993. DC-> CurrFrame.X32_YPlane = lOffset;
  994. lOffset += DC->uSz_YPlane;
  995. DC->CurrFrame.X32_VPlane = lOffset;
  996. DC->CurrFrame.X32_UPlane = DC->CurrFrame.X32_VPlane + U_OFFSET;
  997. lOffset += DC->uSz_VUPlanes;
  998. //no padding is needed
  999. DC->PrevFrame.X32_YPlane = lOffset;
  1000. lOffset += DC->uSz_YPlane;
  1001. DC->PrevFrame.X32_VPlane = lOffset;
  1002. DC->PrevFrame.X32_UPlane = DC->PrevFrame.X32_VPlane + U_OFFSET;
  1003. lOffset += DC->uSz_VUPlanes;
  1004. DC->uMBBuffer = lOffset;
  1005. lOffset += BLOCK_BUFFER_SIZE;
  1006. DC->uFilterBBuffer = lOffset;
  1007. lOffset += FILTER_BLOCK_BUFFER_SIZE;
  1008. // Bitstream
  1009. ASSERT((lOffset & 0x3) == 0); // DWORD alignment
  1010. DC->X32_BitStream = lOffset;
  1011. lOffset += uSizeBitStreamBuffer;
  1012. DC->uSizeBitStreamBuffer = uSizeBitStreamBuffer;
  1013. // rearch
  1014. ASSERT((lOffset & 0x3) == 0); // DWORD alignment
  1015. DC->X32_InverseQuant = lOffset;
  1016. lOffset += u32SizeT_IQ_INDEXBuffer;
  1017. ASSERT((lOffset & 0x3) == 0); // DWORD alignment
  1018. DC->X32_pN = lOffset;
  1019. lOffset += u32SizepNBuffer;
  1020. ASSERT((lOffset & 0x3) == 0); // DWORD alignment
  1021. DC->X32_uMBInfoStream = lOffset;
  1022. lOffset += u32SizeMBInfoStream;
  1023. // rearch
  1024. #ifdef DECODE_STATS
  1025. // Decode Timing Info
  1026. DC->X32_DecTimingInfo = lOffset;
  1027. lOffset += uSizeDecTimingInfo;
  1028. #endif
  1029. // init the data
  1030. ASSERT((U32)lOffset <= u32TotalSize);
  1031. pInitLimit = (U32 *) (P32Inst + lOffset);
  1032. pInitPtr = (U32 *) (P32Inst + DC->CurrFrame.X32_YPlane);
  1033. for (;pInitPtr < pInitLimit;pInitPtr++) *pInitPtr =0;
  1034. // Fill the Y,U,V Previous Frame space with black, this way
  1035. // even if we lost an I frame, the background will remain black
  1036. ZeroFill((HPBYTE)P32Inst + DC->PrevFrame.X32_YPlane + Y_START,
  1037. (HPBYTE)P32Inst + DC->PrevFrame.X32_UPlane + UV_START,
  1038. (HPBYTE)P32Inst + DC->PrevFrame.X32_VPlane + UV_START,
  1039. PITCH,
  1040. DC->uFrameWidth,
  1041. DC->uFrameHeight);
  1042. H263InitializeBlockActionStream(DC);
  1043. } // not YVU9
  1044. lpInst->Initialized = TRUE;
  1045. iReturn = ICERR_OK;
  1046. done:
  1047. return iReturn;
  1048. }
  1049. /***********************************************************************
  1050. * ZeroFill
  1051. * Fill the YVU data area with black.
  1052. ***********************************************************************/
  1053. static void ZeroFill(HPBYTE hpbY, HPBYTE hpbU, HPBYTE hpbV, int iPitch, U32 uWidth, U32 uHeight)
  1054. {
  1055. U32 w,h;
  1056. int y,u,v;
  1057. U32 uNext;
  1058. HPBYTE pY, pU, pV;
  1059. y = 32;
  1060. uNext = iPitch - uWidth;
  1061. for (h = 0 ; h < uHeight ; h++) {
  1062. pY = hpbY;
  1063. for (w = 0; w < uWidth ; w++) {
  1064. *hpbY++ = (U8)16;
  1065. }
  1066. hpbY += uNext;
  1067. }
  1068. uWidth = uWidth / 2;
  1069. uHeight = uHeight / 2;
  1070. uNext = iPitch - uWidth;
  1071. for (h = 0 ; h < uHeight ; h++) {
  1072. pV = hpbV;
  1073. pU = hpbU;
  1074. for (w = 0; w < uWidth ; w++) {
  1075. *hpbV++ = (U8)128;
  1076. *hpbU++ = (U8)128;
  1077. }
  1078. hpbV += uNext;
  1079. hpbU += uNext;
  1080. }
  1081. }
  1082. //***********************************************************************
  1083. //
  1084. // TestFill
  1085. //
  1086. // Fill the YVU data area with a test pattern.
  1087. //
  1088. #if 0
  1089. static void
  1090. TestFill(
  1091. HPBYTE hpbY,
  1092. HPBYTE hpbU,
  1093. HPBYTE hpbV,
  1094. int iPitch,
  1095. U32 uWidth,
  1096. U32 uHeight)
  1097. {
  1098. U32 w,h;
  1099. int y,u,v;
  1100. U32 uNext;
  1101. HPBYTE pY, pU, pV;
  1102. y = 32;
  1103. uNext = iPitch - uWidth;
  1104. for (h = 0 ; h < uHeight ; h++) {
  1105. pY = hpbY;
  1106. for (w = 0; w < uWidth ; w++) {
  1107. *hpbY++ = (U8) (y + (w & ~0xF));
  1108. }
  1109. hpbY += uNext;
  1110. }
  1111. uWidth = uWidth / 2;
  1112. uHeight = uHeight / 2;
  1113. u = 0x4e * 2;
  1114. v = 44;
  1115. uNext = iPitch - uWidth;
  1116. for (h = 0 ; h < uHeight ; h++) {
  1117. pV = hpbV;
  1118. pU = hpbU;
  1119. for (w = 0; w < uWidth ; w++) {
  1120. *hpbV++ = (U8) v;
  1121. *hpbU++ = (U8) u;
  1122. }
  1123. hpbV += uNext;
  1124. hpbU += uNext;
  1125. }
  1126. } /* end TestFill */
  1127. static void
  1128. TestFillUV(
  1129. HPBYTE hpbU,
  1130. HPBYTE hpbV,
  1131. int iPitch,
  1132. U32 uWidth,
  1133. U32 uHeight)
  1134. {
  1135. U32 w,h;
  1136. int u,v;
  1137. U32 uNext;
  1138. HPBYTE pU, pV;
  1139. uWidth = uWidth / 2;
  1140. uHeight = uHeight / 2;
  1141. u = 128;
  1142. v = 128;
  1143. uNext = iPitch - uWidth;
  1144. for (h = 0 ; h < uHeight ; h++) {
  1145. pV = hpbV;
  1146. pU = hpbU;
  1147. for (w = 0; w < uWidth ; w++) {
  1148. *hpbV++ = (U8) v;
  1149. *hpbU++ = (U8) u;
  1150. }
  1151. hpbV += uNext;
  1152. hpbU += uNext;
  1153. }
  1154. } /* end TestFill */
  1155. #endif
  1156. //*********************************************************************
  1157. //H263Decompress -- This function drives the decompress
  1158. // and display of one frame
  1159. //*********************************************************************
  1160. LRESULT H263Decompress(
  1161. LPDECINST lpInst,
  1162. ICDECOMPRESSEX FAR * lpicDecEx,
  1163. BOOL bIsDCI)
  1164. {
  1165. LRESULT iReturn = ICERR_ERROR;
  1166. U8 FAR * fpSrc;
  1167. U8 FAR * P32Inst;
  1168. U8 FAR * fpu8MaxPtr;
  1169. T_H263DecoderCatalog * DC = NULL;
  1170. int iNumberOfGOBs;
  1171. int iNumberOfMBs;
  1172. T_BlkAction FAR * fpBlockAction;
  1173. LONG lOutput;
  1174. int intPitch;
  1175. U32 uNewOffsetToLine0;
  1176. U16 u16NewFrameHeight;
  1177. int bShapingFlag;
  1178. int uYPitch;
  1179. int uUVPitch;
  1180. U8 bMirror;
  1181. HPBYTE pSource, pDestination;
  1182. U32 utemp;
  1183. // rearch
  1184. T_IQ_INDEX * pRUN_INVERSE_Q;
  1185. U32 * pN;
  1186. T_MBInfo FAR * fpMBInfo;
  1187. I32 gob_start = 1, mb_start = 1;
  1188. // rearch
  1189. /* new variables added when change to color convertor/bef */
  1190. U32 uYPlane, uVPlane, uUPlane;
  1191. U8 *pFrame, *lpAligned;
  1192. T_H26X_RTP_BSINFO_TRAILER *pBsTrailer;
  1193. /* the following is for MB Checksum */
  1194. U32 uReadChecksum = 0;
  1195. #ifdef DECODE_STATS
  1196. U32 uStartLow;
  1197. U32 uStartHigh;
  1198. U32 uElapsed;
  1199. U32 uBefore;
  1200. U32 uDecodeFrameSum = 0;
  1201. U32 uHeadersSum = 0;
  1202. U32 uMemcpySum = 0;
  1203. U32 uFrameCopySum = 0;
  1204. U32 uOutputCCSum = 0;
  1205. U32 uInitBlkActStrSum = 0;
  1206. U32 uBEFSum = 0;
  1207. int bTimingThisFrame = 0;
  1208. DEC_TIMING_INFO * pDecTimingInfo = NULL;
  1209. #endif
  1210. #ifdef CHECKSUM_PICTURE
  1211. /* the following is for Picture Checksum */
  1212. YVUCheckSum pReadYVUCksum;
  1213. YVUCheckSum YVUChkSum;
  1214. U32 uCheckSumValid = 0; // flag to skip checksum check if
  1215. // encoder calling decoder before
  1216. // checksum valid
  1217. #endif
  1218. /* The following are used for reading bits */
  1219. U32 uWork;
  1220. U32 uBitsReady;
  1221. BITSTREAM_STATE bsState;
  1222. BITSTREAM_STATE FAR * fpbsState = &bsState;
  1223. #ifdef SKIP_DECODE
  1224. TBD("Skipping Decode");
  1225. iReturn = ICERR_OK;
  1226. goto done;
  1227. #endif
  1228. /* check the input pointers
  1229. */
  1230. if (IsBadWritePtr((LPVOID)lpInst, sizeof(DECINSTINFO))||
  1231. IsBadReadPtr((LPVOID)lpicDecEx, sizeof(ICDECOMPRESSEX)))
  1232. {
  1233. DBOUT("ERROR :: H263Decompress :: ICERR_BADPARAM");
  1234. iReturn = ICERR_BADPARAM;
  1235. goto done;
  1236. }
  1237. /* Check for a bad length
  1238. */
  1239. if (lpicDecEx->lpbiSrc->biSizeImage == 0) {
  1240. DBOUT("ERROR :: H263Decompress :: ICERR_BADIMAGESIZE");
  1241. iReturn = ICERR_BADIMAGESIZE;
  1242. goto done;
  1243. }
  1244. /* Lock the memory
  1245. */
  1246. if (lpInst->pDecoderInst == NULL)
  1247. {
  1248. DBOUT("ERROR :: H263Decompress :: ICERR_MEMORY");
  1249. iReturn = ICERR_MEMORY;
  1250. goto done;
  1251. }
  1252. /* Set the frame mirroring flag
  1253. */
  1254. bMirror = FALSE;
  1255. if (lpicDecEx->lpbiDst != 0)
  1256. {
  1257. if(lpicDecEx->lpbiSrc->biWidth * lpicDecEx->lpbiDst->biWidth < 0)
  1258. bMirror = TRUE;
  1259. }
  1260. /* for testing */
  1261. /* bMirror = TRUE; */
  1262. /* Build the decoder catalog pointer
  1263. */
  1264. P32Inst = (U8 FAR *) ((((U32) lpInst->pDecoderInst) + 31) & ~0x1F);
  1265. DC = (T_H263DecoderCatalog FAR *) P32Inst;
  1266. if (DC->DecoderType == H263_CODEC)
  1267. {
  1268. #ifdef DECODE_STATS
  1269. if ((DC->uStatFrameCount < DEC_TIMING_INFO_FRAME_COUNT) &&
  1270. (DC->ColorConvertor != YUV12ForEnc))
  1271. {
  1272. ASSERT(DC->X32_DecTimingInfo > 0);
  1273. DC->pDecTimingInfo = (DEC_TIMING_INFO FAR *)( ((U8 FAR *)P32Inst) + DC->X32_DecTimingInfo );
  1274. TIMER_START(bTimingThisFrame,uStartLow,uStartHigh);
  1275. ASSERT(bTimingThisFrame);
  1276. DC->uStartLow = uStartLow;
  1277. DC->uStartHigh = uStartHigh;
  1278. }
  1279. else
  1280. {
  1281. DC->pDecTimingInfo = (DEC_TIMING_INFO FAR *) NULL;
  1282. ASSERT(!bTimingThisFrame);
  1283. }
  1284. DC->bTimingThisFrame = bTimingThisFrame;
  1285. #endif
  1286. /* Is there room to copy the bitstream? We could at most add 2 (zeros) and 3
  1287. padding bytes for DWORD alignment to the original bitstream */\
  1288. ASSERT(lpicDecEx->lpbiSrc->biSizeImage + 5 <= DC->uSizeBitStreamBuffer);
  1289. if ((lpicDecEx->lpbiSrc->biSizeImage + 5) > DC->uSizeBitStreamBuffer)
  1290. {
  1291. DBOUT("ERROR :: H263Decompress :: ICERR_ERROR: not enough room for bitstream");
  1292. iReturn = ICERR_ERROR;
  1293. goto done;
  1294. }
  1295. /* Copy the source data to the bitstream region.
  1296. * OPTIMIZE: Integrate MRV's BLKCOPY.ASM
  1297. */
  1298. #ifdef DECODE_STATS
  1299. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  1300. #endif
  1301. fpSrc = (U8 FAR *)(P32Inst + DC->X32_BitStream);
  1302. // New: we will first look for an EBS from the PPM. If there is one, then we will
  1303. // insert two bytes of zero between the H.261 bistream and the EBS part with
  1304. // DWORD alignment and update the total bitstream size. If no EBS is found,
  1305. // then we proceed as before.
  1306. DC->iVerifiedBsExt = FALSE;
  1307. DC->Sz_BitStream = lpicDecEx->lpbiSrc->biSizeImage ;
  1308. H26XRTP_VerifyBsInfoStream(DC,(U8 *) lpicDecEx->lpSrc,lpicDecEx->lpbiSrc->biSizeImage);
  1309. if (!DC->iValidBsExt)
  1310. {
  1311. memcpy((char FAR *)fpSrc, (const char FAR *) lpicDecEx->lpSrc, lpicDecEx->lpbiSrc->biSizeImage);
  1312. // also copy 16 bits of zero for end of frame detection
  1313. fpSrc[lpicDecEx->lpbiSrc->biSizeImage] = 0;
  1314. fpSrc[lpicDecEx->lpbiSrc->biSizeImage+1] = 0;
  1315. DC->Sz_BitStream += 2;
  1316. fpu8MaxPtr = fpSrc;
  1317. fpu8MaxPtr += (lpicDecEx->lpbiSrc->biSizeImage + 2 - 1);
  1318. }
  1319. else
  1320. {
  1321. // First the H.261 stream data - relying on PPM to fill the compressed size correctly
  1322. // in the trailer.
  1323. pBsTrailer = ( (T_H26X_RTP_BSINFO_TRAILER *)(DC->pBsTrailer) );
  1324. memcpy((char FAR *)fpSrc, (const char FAR *) lpicDecEx->lpSrc, pBsTrailer->uCompressedSize);
  1325. // Now write out two bytes of zeros at the end of the H.261 bitstream
  1326. fpSrc[pBsTrailer->uCompressedSize] = 0;
  1327. fpSrc[pBsTrailer->uCompressedSize + 1] = 0;
  1328. // Now tack on the EBS after DWORD alignment.
  1329. lpAligned = (U8 *) ( (U32) (fpSrc + (pBsTrailer->uCompressedSize + 2) + 3) &
  1330. 0xfffffffc);
  1331. memcpy(lpAligned, DC->pBsInfo, DC->uNumOfPackets*sizeof(T_RTP_H261_BSINFO));
  1332. memcpy(lpAligned + DC->uNumOfPackets*sizeof(T_RTP_H261_BSINFO), DC->pBsTrailer,
  1333. sizeof(T_H26X_RTP_BSINFO_TRAILER));
  1334. // update lpicDecEx->lpbiSrc->biSizeImage
  1335. DC->Sz_BitStream = lpAligned + DC->uNumOfPackets*sizeof(T_RTP_H261_BSINFO) +
  1336. sizeof(T_H26X_RTP_BSINFO_TRAILER) - fpSrc;
  1337. fpu8MaxPtr = fpSrc;
  1338. fpu8MaxPtr += (pBsTrailer->uCompressedSize + 2 - 1);
  1339. }
  1340. #ifdef DECODE_STATS
  1341. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uMemcpySum)
  1342. #endif
  1343. /* Initialize the bit stream reader
  1344. */
  1345. GET_BITS_INIT(uWork, uBitsReady);
  1346. // rearch
  1347. // Initialize pointers to data structures which carry info
  1348. // between passes
  1349. pRUN_INVERSE_Q = (T_IQ_INDEX *)(P32Inst + DC->X32_InverseQuant);
  1350. pN = (U32 *)(P32Inst + DC->X32_pN);
  1351. fpMBInfo = (T_MBInfo FAR *) (P32Inst + DC->X32_uMBInfoStream);
  1352. // rearch
  1353. // #ifdef LOSS_RECOVERY
  1354. #if 1
  1355. DC->iVerifiedBsExt = FALSE;
  1356. #endif
  1357. /* Decode the Picture Header */
  1358. #ifdef DECODE_STATS
  1359. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  1360. #endif
  1361. #ifdef CHECKSUM_PICTURE
  1362. iReturn = H263DecodePictureHeader(DC, fpSrc, uBitsReady, uWork, fpbsState, &pReadYVUCksum, &uCheckSumValid);
  1363. #else
  1364. iReturn = H263DecodePictureHeader(DC, fpSrc, uBitsReady, uWork, fpbsState);
  1365. #endif
  1366. if (iReturn != ICERR_OK)
  1367. {
  1368. DBOUT("ERROR :: H263Decompress :: Error reading the picture header");
  1369. goto done;
  1370. }
  1371. #ifdef DECODE_STATS
  1372. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uHeadersSum)
  1373. #endif
  1374. /* Set a limit for testing for bitstream over-run
  1375. */
  1376. /* For each GOB do... */
  1377. iNumberOfGOBs = iNumberOfGOBsBySourceFormat[DC->uSrcFormat];
  1378. iNumberOfMBs = iNumberOfMBsInAGOBBySourceFormat[DC->uSrcFormat];
  1379. /* In H263 a GOB is a single row of MB, and a MB is 16x16 */
  1380. /* In H261 a GOB is 33 MBs, and a MB is 16x16 */
  1381. /* Order of GOBs depends on source format */
  1382. if (DC->uSrcFormat == SRC_FORMAT_QCIF)
  1383. {
  1384. ASSERT(((U32)iNumberOfGOBs * 3 * 16) == DC->uFrameHeight);
  1385. if (((U32)iNumberOfGOBs * 3 * 16) != DC->uFrameHeight)
  1386. {
  1387. DBOUT("ERROR :: H263Decompress :: Error matching picture header SRC field and actual frame height");
  1388. iReturn = ICERR_ERROR;
  1389. goto done;
  1390. }
  1391. ASSERT(((U32)iNumberOfMBs / 3 * 16) == DC->uFrameWidth);
  1392. if (((U32)iNumberOfMBs / 3 * 16) != DC->uFrameWidth)
  1393. {
  1394. DBOUT("ERROR :: H263Decompress :: Error matching picture header SRC field and actual frame width");
  1395. iReturn = ICERR_ERROR;
  1396. goto done;
  1397. }
  1398. }
  1399. if (DC->uSrcFormat == SRC_FORMAT_CIF)
  1400. {
  1401. ASSERT(((U32)iNumberOfGOBs / 2 * 3 * 16) == DC->uFrameHeight);
  1402. if (((U32)iNumberOfGOBs / 2 * 3 * 16) != DC->uFrameHeight)
  1403. {
  1404. DBOUT("ERROR :: H263Decompress :: Error matching picture header SRC field and actual frame height");
  1405. iReturn = ICERR_ERROR;
  1406. goto done;
  1407. }
  1408. ASSERT(((U32)iNumberOfMBs / 3 * 2 * 16) == DC->uFrameWidth);
  1409. if (((U32)iNumberOfMBs / 3 * 2 * 16) != DC->uFrameWidth)
  1410. {
  1411. DBOUT("ERROR :: H263Decompress :: Error matching picture header SRC field and actual frame width");
  1412. iReturn = ICERR_ERROR;
  1413. goto done;
  1414. }
  1415. }
  1416. fpBlockAction = (T_BlkAction FAR *) (P32Inst + DC->X16_BlkActionStream);
  1417. // rearch
  1418. // H261, re initialize the block action stream for entire Frame
  1419. // at end of H263Decompress. High bit is set in BlockType to
  1420. // indicate if need to do BEF so can't re-init between GOBs.
  1421. // H261ReInitializeBlockActionStream(DC);
  1422. /*****************************************************************
  1423. FIRST PASS - bitream parsing and IDCT prep work
  1424. ***************************************************************/
  1425. // #ifndef LOSS_RECOVERY
  1426. #if 0
  1427. iReturn = IAPass1ProcessFrame(DC,
  1428. fpBlockAction,
  1429. fpMBInfo,
  1430. fpbsState,
  1431. fpu8MaxPtr,
  1432. pN,
  1433. pRUN_INVERSE_Q,
  1434. iNumberOfGOBs,
  1435. iNumberOfMBs,
  1436. gob_start,
  1437. mb_start);
  1438. #else
  1439. iReturn = IAPass1ProcessFrameRTP(DC,
  1440. fpBlockAction,
  1441. fpMBInfo,
  1442. fpbsState,
  1443. fpu8MaxPtr,
  1444. pN,
  1445. pRUN_INVERSE_Q,
  1446. iNumberOfGOBs,
  1447. iNumberOfMBs,
  1448. gob_start,
  1449. mb_start);
  1450. #endif
  1451. if (iReturn != ICERR_OK) {
  1452. DBOUT("H261Decompress : Pass 1 error");
  1453. goto done;
  1454. }
  1455. /*****************************************************************
  1456. SECOND PASS - IDCT and motion compensation (MC)
  1457. *****************************************************************/
  1458. fpBlockAction = (T_BlkAction FAR *)(P32Inst + DC->X16_BlkActionStream);
  1459. pRUN_INVERSE_Q = (T_IQ_INDEX *)(P32Inst + DC->X32_InverseQuant);
  1460. pN = (U32 *)(P32Inst + DC->X32_pN);
  1461. fpMBInfo = (T_MBInfo FAR *)(P32Inst + DC->X32_uMBInfoStream);
  1462. IAPass2ProcessFrame(DC,
  1463. fpBlockAction,
  1464. fpMBInfo,
  1465. pN,
  1466. pRUN_INVERSE_Q,
  1467. iNumberOfGOBs,
  1468. iNumberOfMBs);
  1469. // rearch
  1470. //Prepare which frame to display for inter frames
  1471. DC->DispFrame.X32_YPlane = DC->CurrFrame.X32_YPlane;
  1472. DC->DispFrame.X32_VPlane = DC->CurrFrame.X32_VPlane;
  1473. DC->DispFrame.X32_UPlane = DC->CurrFrame.X32_UPlane;
  1474. utemp = DC->CurrFrame.X32_YPlane;
  1475. DC->CurrFrame.X32_YPlane = DC->PrevFrame.X32_YPlane;
  1476. DC->PrevFrame.X32_YPlane = utemp;
  1477. utemp = DC->CurrFrame.X32_VPlane ;
  1478. DC->CurrFrame.X32_VPlane = DC->PrevFrame.X32_VPlane;
  1479. DC->PrevFrame.X32_VPlane = utemp;
  1480. utemp = DC->CurrFrame.X32_UPlane ;
  1481. DC->CurrFrame.X32_UPlane = DC->PrevFrame.X32_UPlane;
  1482. DC->PrevFrame.X32_UPlane = utemp;
  1483. #ifdef CHECKSUM_PICTURE
  1484. if (uCheckSumValid)
  1485. {
  1486. /* compute and compare picture checksum data */
  1487. iReturn = H261ComputePictureCheckSum(P32Inst, &YVUChkSum);
  1488. iReturn = H261ComparePictureCheckSum(&YVUChkSum, &pReadYVUCksum);
  1489. }
  1490. #endif
  1491. } /* end if (DC->DecoderType == H263_CODEC) */
  1492. else
  1493. {
  1494. ASSERT(DC->DecoderType == YUV12_CODEC);
  1495. DC->DispFrame.X32_YPlane = DC->CurrFrame.X32_YPlane;
  1496. DC->DispFrame.X32_VPlane = DC->CurrFrame.X32_VPlane;
  1497. DC->DispFrame.X32_UPlane = DC->CurrFrame.X32_UPlane;
  1498. }
  1499. /* Return if there is no need to update screen yet.
  1500. */
  1501. if ((lpicDecEx->dwFlags & ICDECOMPRESS_HURRYUP)
  1502. || (lpicDecEx->dwFlags & ICDECOMPRESS_PREROLL))
  1503. {
  1504. DBOUT("H261Decompress : Display suppressed, HURRYUP or PREROLL");
  1505. iReturn = ICERR_DONTDRAW;
  1506. goto done;
  1507. }
  1508. #if 0
  1509. /* Fill the Y,U,V Current Frame space with a test pattern
  1510. */
  1511. TestFill((HPBYTE)P32Inst + DC->CurrFrame.X32_YPlane + Y_START,
  1512. (HPBYTE)P32Inst + DC->CurrFrame.X32_UPlane + UV_START,
  1513. (HPBYTE)P32Inst + DC->CurrFrame.X32_VPlane + UV_START,
  1514. PITCH,
  1515. DC->uFrameWidth,
  1516. DC->uFrameHeight);
  1517. #endif
  1518. #if MAKE_GRAY
  1519. /* Fill the U,V Current Frame space with a test pattern
  1520. */
  1521. TestFillUV((HPBYTE)P32Inst + DC->CurrFrame.X32_UPlane + UV_START,
  1522. (HPBYTE)P32Inst + DC->CurrFrame.X32_VPlane + UV_START,
  1523. PITCH,
  1524. DC->uFrameWidth,
  1525. DC->uFrameHeight);
  1526. #endif
  1527. /* Special case the YUV12 for the encoder because it should not include
  1528. * BEF, Shaping or aspect ratio correction...
  1529. */
  1530. if (DC->ColorConvertor == YUV12ForEnc)
  1531. {
  1532. H26x_YUV12ForEnc ((HPBYTE)P32Inst,
  1533. DC->PrevFrame.X32_YPlane + Y_START,
  1534. DC->PrevFrame.X32_VPlane + UV_START,
  1535. DC->PrevFrame.X32_UPlane + UV_START,
  1536. DC->uFrameWidth,
  1537. DC->uFrameHeight,
  1538. PITCH,
  1539. (HPBYTE)lpicDecEx->lpDst,
  1540. (DWORD)Y_START,
  1541. (DWORD)(MAX_HEIGHT + 2L*UMV_EXPAND_Y) * PITCH + 8 + UV_START + PITCH / 2,
  1542. (DWORD)(MAX_HEIGHT + 2L*UMV_EXPAND_Y) * PITCH + 8 + UV_START);
  1543. iReturn = ICERR_OK;
  1544. goto done;
  1545. }
  1546. /* Copy Planes to Post Processing area if mirror and/or block edge filter.
  1547. */
  1548. if (DC->DecoderType == H263_CODEC)
  1549. {
  1550. #ifdef DECODE_STATS
  1551. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  1552. #endif
  1553. if(bMirror) { // copy with mirroring
  1554. pFrame = (U8 *)DC->p16InstPostProcess;
  1555. uYPlane = DC->PostFrame.X32_YPlane;
  1556. uUPlane = DC->PostFrame.X32_UPlane;
  1557. uVPlane = DC->PostFrame.X32_VPlane;
  1558. FrameMirror(((HPBYTE) P32Inst) + DC->DispFrame.X32_YPlane + Y_START,
  1559. ((HPBYTE) DC->p16InstPostProcess) + DC->PostFrame.X32_YPlane,
  1560. DC->uFrameHeight,
  1561. DC->uFrameWidth,
  1562. PITCH);
  1563. FrameMirror(((HPBYTE) P32Inst)+ DC->DispFrame.X32_UPlane + UV_START,
  1564. ((HPBYTE) DC->p16InstPostProcess) + DC->PostFrame.X32_UPlane,
  1565. DC->uFrameHeight/2,
  1566. DC->uFrameWidth/2,
  1567. PITCH);
  1568. FrameMirror(((HPBYTE) P32Inst)+ DC->DispFrame.X32_VPlane + UV_START,
  1569. ((HPBYTE) DC->p16InstPostProcess) + DC->PostFrame.X32_VPlane,
  1570. DC->uFrameHeight/2,
  1571. DC->uFrameWidth/2,
  1572. PITCH);
  1573. }
  1574. else
  1575. { /* no mirroring */
  1576. if ((DC->bUseBlockEdgeFilter) || (DC->bAdjustLuma) ||
  1577. (DC->bAdjustChroma))
  1578. {
  1579. /* copy for BEF */
  1580. pFrame = (U8 *)DC->p16InstPostProcess;
  1581. uYPlane = DC->PostFrame.X32_YPlane;
  1582. uUPlane = DC->PostFrame.X32_UPlane;
  1583. uVPlane = DC->PostFrame.X32_VPlane;
  1584. FrameCopy (((HPBYTE) P32Inst) +DC->DispFrame.X32_YPlane+Y_START,
  1585. ((HPBYTE) DC->p16InstPostProcess) +DC->PostFrame.X32_YPlane,
  1586. DC->uFrameHeight,
  1587. DC->uFrameWidth,
  1588. PITCH);
  1589. FrameCopy (((HPBYTE) P32Inst)+DC->DispFrame.X32_UPlane+UV_START,
  1590. ((HPBYTE) DC->p16InstPostProcess) +DC->PostFrame.X32_UPlane,
  1591. DC->uFrameHeight/2,
  1592. DC->uFrameWidth/2,
  1593. PITCH);
  1594. FrameCopy (((HPBYTE) P32Inst)+DC->DispFrame.X32_VPlane+UV_START,
  1595. ((HPBYTE) DC->p16InstPostProcess) +DC->PostFrame.X32_VPlane,
  1596. DC->uFrameHeight/2,
  1597. DC->uFrameWidth/2,
  1598. PITCH);
  1599. } /* end if BEF on */
  1600. else
  1601. {
  1602. /* no BEF or mirror so don't need copy */
  1603. pFrame = (U8 *) DC;
  1604. uYPlane = DC->DispFrame.X32_YPlane + Y_START;
  1605. uUPlane = DC->DispFrame.X32_UPlane + UV_START;
  1606. uVPlane = DC->DispFrame.X32_VPlane + UV_START;
  1607. } /* end of else no BEF */
  1608. } /* end else no mirroring */
  1609. #ifdef DECODE_STATS
  1610. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uFrameCopySum)
  1611. #endif
  1612. uYPitch = PITCH;
  1613. uUVPitch = PITCH;
  1614. if (DC->bUseBlockEdgeFilter)
  1615. {
  1616. #ifdef DECODE_STATS
  1617. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  1618. #endif
  1619. fpBlockAction=(T_BlkAction FAR *) (P32Inst+DC->X16_BlkActionStream);
  1620. BlockEdgeFilter(((HPBYTE) DC->p16InstPostProcess) + DC->PostFrame.X32_YPlane,
  1621. DC->uFrameHeight,
  1622. DC->uFrameWidth,
  1623. PITCH,
  1624. fpBlockAction);
  1625. #ifdef DECODE_STATS
  1626. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uBEFSum)
  1627. #endif
  1628. }
  1629. }
  1630. else
  1631. { /* YUV12 */
  1632. const U32 uHeight = DC->uFrameHeight;
  1633. const U32 uWidth = DC->uFrameWidth;
  1634. const U32 uYPlaneSize = uHeight*uWidth;
  1635. if(bMirror) // mirroring and YUV12 need to do copy
  1636. {
  1637. pFrame = (U8 *)DC->p16InstPostProcess;
  1638. uYPlane = DC->PostFrame.X32_YPlane;
  1639. uUPlane = uYPlane + uYPlaneSize;
  1640. uVPlane = uUPlane + (uYPlaneSize>>2);
  1641. pSource = (HPBYTE)lpicDecEx->lpSrc;
  1642. pDestination = (HPBYTE)(DC->p16InstPostProcess + (DWORD)DC->PostFrame.X32_YPlane);
  1643. FrameMirror (pSource, pDestination, uHeight, uWidth, uWidth);
  1644. pSource += uYPlaneSize;
  1645. pDestination += uYPlaneSize;
  1646. FrameMirror (pSource, pDestination, (uHeight>>1), (uWidth>>1), (uWidth>>1));
  1647. pSource += (uYPlaneSize>>2);
  1648. pDestination += (uYPlaneSize>>2);
  1649. FrameMirror (pSource, pDestination, (uHeight>>1), (uWidth>>1), (uWidth>>1));
  1650. }
  1651. else // no mirroring
  1652. {
  1653. if ((DC->bAdjustLuma)||(DC->bAdjustChroma)) // copy when adjust pels
  1654. {
  1655. pFrame = (U8 *)DC->p16InstPostProcess;
  1656. //uYPlane = 0;
  1657. uYPlane = DC->PostFrame.X32_YPlane;
  1658. uUPlane = uYPlane + uYPlaneSize;
  1659. uVPlane = uUPlane + (uYPlaneSize>>2);
  1660. pSource = (HPBYTE)lpicDecEx->lpSrc;
  1661. pDestination = (HPBYTE)(DC->p16InstPostProcess + (DWORD)DC->PostFrame.X32_YPlane);
  1662. FrameCopy (pSource, pDestination, uHeight, uWidth, uWidth);
  1663. pSource += uYPlaneSize;
  1664. pDestination += uYPlaneSize;
  1665. FrameCopy (pSource, pDestination, (uHeight>>1), (uWidth>>1), (uWidth>>1));
  1666. pSource += (uYPlaneSize>>2);
  1667. pDestination += (uYPlaneSize>>2);
  1668. FrameCopy (pSource, pDestination, (uHeight>>1), (uWidth>>1), (uWidth>>1));
  1669. }
  1670. else
  1671. {
  1672. /* Do not have to do memcpy because color convertors don't
  1673. * destroy input planes.
  1674. */
  1675. pFrame = (HPBYTE)lpicDecEx->lpSrc;
  1676. uYPlane = 0;
  1677. uUPlane = uYPlane + uYPlaneSize;
  1678. uVPlane = uUPlane + (uYPlaneSize>>2);
  1679. //memcpy(((char FAR *)(DC->p16InstPostProcess + (DWORD)DC->PostFrame.X32_YPlane)),
  1680. // (const char FAR *)lpicDecEx->lpSrc,
  1681. // lpicDecEx->lpbiSrc->biSizeImage);
  1682. }
  1683. } /* end else if no mirroring */
  1684. uYPitch = DC->uFrameWidth;
  1685. uUVPitch = DC->uFrameWidth >> 1;
  1686. } /* end else YUV12 */
  1687. if (DC->bForceOnAspectRatioCorrection || lpInst->bCorrectAspectRatio) {
  1688. bShapingFlag = 1;
  1689. u16NewFrameHeight = (U16) (DC->uFrameHeight * 11 / 12);
  1690. } else {
  1691. bShapingFlag = 0;
  1692. u16NewFrameHeight = (U16) DC->uFrameHeight;
  1693. }
  1694. /* Do the PEL color adjustments if necessary.
  1695. */
  1696. if(DC->bAdjustLuma) {
  1697. /* width is rounded up to a multiple of 8
  1698. */
  1699. AdjustPels(pFrame,
  1700. uYPlane,
  1701. DC->uFrameWidth,
  1702. uYPitch,
  1703. DC->uFrameHeight,
  1704. (U32) DC->X16_LumaAdjustment);
  1705. }
  1706. if(DC->bAdjustChroma) {
  1707. /* width = Y-Width / 4 and then rounded up to a multiple of 8
  1708. */
  1709. AdjustPels(pFrame,
  1710. uUPlane,
  1711. (DC->uFrameWidth >> 1),
  1712. uUVPitch,
  1713. (DC->uFrameHeight >> 1),
  1714. (U32) DC->X16_ChromaAdjustment);
  1715. AdjustPels(pFrame,
  1716. uVPlane,
  1717. (DC->uFrameWidth >> 1),
  1718. uUVPitch,
  1719. (DC->uFrameHeight >> 1),
  1720. (U32) DC->X16_ChromaAdjustment);
  1721. }
  1722. /* Determine parameters (lOutput, intPitch, uNewOffsetToLine0)
  1723. * needed for color conversion.
  1724. */
  1725. if (lpicDecEx->lpbiDst->biCompression == FOURCC_YUY2)
  1726. {
  1727. // We are assuming here a positive pitch for YUY2.
  1728. // This typically corresponds to a negative value for
  1729. // the destination bit map height.
  1730. // If we're ever asked to use YUY2 with a positive bit map
  1731. // height, we'll have to revisit these calculations.
  1732. intPitch = (lpicDecEx->lpbiDst->biBitCount >> 3)
  1733. * abs ((int)(lpicDecEx->lpbiDst->biWidth));
  1734. lOutput = 0;
  1735. uNewOffsetToLine0 = 0;
  1736. #if 0
  1737. // Aspect ratio correction is now supported for YUY2.
  1738. // This is necessary to enable direct draw under Active Movie 1.0.
  1739. bShapingFlag=FALSE;
  1740. #endif
  1741. DBOUT("Using YUY2 ........");
  1742. }
  1743. else if ((lpicDecEx->lpbiDst->biCompression == FOURCC_YUV12) || (lpicDecEx->lpbiDst->biCompression == FOURCC_IYUV))
  1744. {
  1745. intPitch = 0xdeadbeef; // should not be used
  1746. lOutput = 0;
  1747. uNewOffsetToLine0 = DC->CCOffsetToLine0;
  1748. bShapingFlag=FALSE;
  1749. DBOUT("Using YUV ........");
  1750. }
  1751. else if (lpicDecEx->lpbiDst->biCompression == FOURCC_IF09)
  1752. {
  1753. lOutput=0;
  1754. intPitch = abs((int)(lpicDecEx->lpbiDst->biWidth));
  1755. uNewOffsetToLine0 = DC->CCOffsetToLine0;
  1756. DBOUT("USing IF09........");
  1757. }
  1758. else
  1759. {
  1760. lOutput = DibXY(lpicDecEx, &intPitch, lpInst->YScale, bIsDCI);
  1761. uNewOffsetToLine0 = DC->CCOffsetToLine0;
  1762. if (!bIsDCI)
  1763. {
  1764. // DC->CCOffsetToLine0 was initialized without taking into
  1765. // account the sign of the destination bitmap height. Let's
  1766. // compensate for that here.
  1767. if (lpicDecEx->lpbiDst->biHeight < 0)
  1768. uNewOffsetToLine0 = 0;
  1769. // Adjust uNewOffsetToLine0 for aspect ratio correction.
  1770. if (uNewOffsetToLine0 > 0)
  1771. {
  1772. ASSERT(intPitch < 0);
  1773. if (lpInst->YScale == 2)
  1774. {
  1775. uNewOffsetToLine0 += 2 * (U32)intPitch *
  1776. ((U32)DC->uFrameHeight - (U32)u16NewFrameHeight);
  1777. }
  1778. else
  1779. {
  1780. uNewOffsetToLine0 += (U32)intPitch *
  1781. ((U32)DC->uFrameHeight - (U32)u16NewFrameHeight);
  1782. }
  1783. }
  1784. }
  1785. }
  1786. /* Call the color convertors
  1787. */
  1788. /////////////////////////////////////////////////////////////////////////////
  1789. // Check to see if we need to copy a Snapshot into the output buffer.
  1790. // I added new fields to the Decoder Catalog to permit asynchronous
  1791. // transfer of data. These fields are:
  1792. // DC->SnapshotRequest
  1793. // DC->SnapshotBuffer
  1794. // DC->SnapshotEvent
  1795. // Ben - 09/25/96
  1796. /////////////////////////////////////////////////////////////////////////////
  1797. if(DC->SnapshotRequest == SNAPSHOT_REQUESTED)
  1798. {
  1799. UINT uiSZ_Snapshot;
  1800. DBOUT("D1DEC:DECOMPRESS::Snapshot requested");
  1801. uiSZ_Snapshot = (DC->uFrameWidth * DC->uFrameHeight * 12) >> 3;
  1802. if(!(IsBadWritePtr(DC->SnapshotBuffer, uiSZ_Snapshot)))
  1803. {
  1804. DC->SnapshotRequest = SNAPSHOT_COPY_STARTED;
  1805. DBOUT("D1DEC:DECOMPRESS::Snapshot copy started");
  1806. ColorConvertorCatalog[YUV12NOPITCH].ColorConvertor[0]
  1807. (
  1808. (LPSTR) pFrame + uYPlane,
  1809. (LPSTR) pFrame + uVPlane,
  1810. (LPSTR) pFrame + uUPlane,
  1811. (UN) DC->uFrameWidth,
  1812. (UN) DC->uFrameHeight,
  1813. (UN) uYPitch,
  1814. (UN) uUVPitch,
  1815. (UN) (bShapingFlag ? 12 : 9999),
  1816. (LPSTR) DC->SnapshotBuffer,
  1817. 0,
  1818. 0,
  1819. (int) DC->uFrameWidth,
  1820. YUV12NOPITCH
  1821. );
  1822. DC->SnapshotRequest = SNAPSHOT_COPY_FINISHED;
  1823. DBOUT("D1DEC:DECOMPRESS::Snapshot copy finished");
  1824. }
  1825. else
  1826. {
  1827. DC->SnapshotRequest = SNAPSHOT_COPY_REJECTED;
  1828. DBOUT("D1DEC:DECOMPRESS::Snapshot copy rejected");
  1829. }
  1830. SetEvent(DC->SnapshotEvent);
  1831. }
  1832. #ifndef RING0
  1833. #ifdef _DEBUG
  1834. {
  1835. char msg[180];
  1836. wsprintf(msg, "Decompress before CC: (%d,%d,%d,%d) (%d,%d,%d,%d) lOut %ld, NewOff %ld, DC->Off %ld, pitch %ld",
  1837. lpicDecEx->xSrc, lpicDecEx->ySrc, lpicDecEx->dxSrc, lpicDecEx->dySrc,
  1838. lpicDecEx->xDst, lpicDecEx->yDst, lpicDecEx->dxDst, lpicDecEx->dyDst,
  1839. lOutput, uNewOffsetToLine0, DC->CCOffsetToLine0, intPitch);
  1840. DBOUT(msg);
  1841. }
  1842. #endif
  1843. #endif
  1844. #ifdef DECODE_STATS
  1845. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  1846. #endif
  1847. ColorConvertorCatalog[DC->ColorConvertor].ColorConvertor[PENTIUM_CC](
  1848. (LPSTR) pFrame + uYPlane,
  1849. (LPSTR) pFrame + uVPlane,
  1850. (LPSTR) pFrame + uUPlane,
  1851. (UN) DC->uFrameWidth,
  1852. (UN) DC->uFrameHeight,
  1853. (UN) uYPitch,
  1854. (UN) uUVPitch, // ??? BSE ??? //
  1855. (UN) (bShapingFlag ? 12 : 9999), // ??? BSE ??? //
  1856. (LPSTR) lpicDecEx->lpDst,
  1857. (U32) lOutput,
  1858. (U32) uNewOffsetToLine0,
  1859. (int) intPitch, // Color converter pitch
  1860. DC->ColorConvertor);
  1861. #ifdef DECODE_STATS
  1862. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uOutputCCSum);
  1863. #endif
  1864. iReturn = ICERR_OK;
  1865. done:
  1866. if (DC != NULL)
  1867. {
  1868. if (DC->DecoderType == H263_CODEC)
  1869. {
  1870. #ifdef DECODE_STATS
  1871. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  1872. #endif
  1873. H261ReInitializeBlockActionStream(DC);
  1874. #ifdef DECODE_STATS
  1875. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uInitBlkActStrSum)
  1876. #endif
  1877. } /* end if (DC->DecoderType == H263_CODEC) */
  1878. #ifdef DECODE_STATS
  1879. TIMER_STOP(bTimingThisFrame,uStartLow,uStartHigh,uDecodeFrameSum);
  1880. if (bTimingThisFrame)
  1881. {
  1882. pDecTimingInfo = DC->pDecTimingInfo + DC->uStatFrameCount;
  1883. pDecTimingInfo->uDecodeFrame = uDecodeFrameSum;
  1884. pDecTimingInfo->uHeaders += uHeadersSum;
  1885. pDecTimingInfo->uMemcpy = uMemcpySum;
  1886. pDecTimingInfo->uFrameCopy = uFrameCopySum;
  1887. pDecTimingInfo->uOutputCC = uOutputCCSum;
  1888. pDecTimingInfo->uInitBlkActStr = uInitBlkActStrSum;
  1889. pDecTimingInfo->uBEF = uBEFSum;
  1890. DC->uStatFrameCount++;
  1891. /* Verify that we have time for all the required steps
  1892. */
  1893. ASSERT(pDecTimingInfo->uDecodeFrame);
  1894. ASSERT(pDecTimingInfo->uHeaders);
  1895. ASSERT(pDecTimingInfo->uMemcpy);
  1896. ASSERT(pDecTimingInfo->uFrameCopy);
  1897. ASSERT(pDecTimingInfo->uOutputCC);
  1898. /* ASSERT(pDecTimingInfo->uDecodeBlock); 0 if all are empty */
  1899. ASSERT(pDecTimingInfo->uInitBlkActStr);
  1900. ASSERT(pDecTimingInfo->uBEF);
  1901. }
  1902. #endif
  1903. }
  1904. return iReturn;
  1905. }
  1906. //************************************************************************
  1907. //
  1908. //H263TermDecoderInstance -- This function frees the space allocated for an
  1909. // instance of the H263 decoder.
  1910. //
  1911. //************************************************************************
  1912. LRESULT H263TermDecoderInstance(LPDECINST lpInst)
  1913. {
  1914. LRESULT iReturn = ICERR_OK;
  1915. T_H263DecoderCatalog * DC;
  1916. if(IsBadWritePtr((LPVOID)lpInst, sizeof(DECINSTINFO)))
  1917. {
  1918. DBOUT("ERROR :: H263TermDecoderInstance :: ICERR_BADPARAM");
  1919. iReturn = ICERR_BADPARAM;
  1920. }
  1921. if(lpInst->Initialized == FALSE)
  1922. {
  1923. DBOUT("Warning: H263TermDecoderInstance(): Uninitialized instance")
  1924. return(ICERR_OK);
  1925. }
  1926. lpInst->Initialized = FALSE;
  1927. DC = (T_H263DecoderCatalog *) ((((U32) lpInst->pDecoderInst) + 31) & ~0x1F);
  1928. CloseHandle(DC->SnapshotEvent);
  1929. if (DC->a16InstPostProcess != NULL)
  1930. {
  1931. HeapFree(GetProcessHeap(), 0, DC->a16InstPostProcess);
  1932. // PhilF: Also freed in H263TerminateDecoderInstance! For now set to NULL to avoid second HeapFree.
  1933. // Investigate reason for 2nd call later...
  1934. DC->a16InstPostProcess = NULL;
  1935. }
  1936. HeapFree(GetProcessHeap(), 0, lpInst->pDecoderInst);
  1937. return iReturn;
  1938. }
  1939. //****************************************************************************
  1940. //DibXY -- This function is used to map color converted output to the screen.
  1941. //note: this function came from the H261 code base.
  1942. //****************************************************************************
  1943. static long DibXY(ICDECOMPRESSEX FAR *lpicDecEx, LPINT lpiPitch, UINT yScale, BOOL bIsDCI)
  1944. {
  1945. int iPitch; /* width of DIB */
  1946. long lOffset = 0;
  1947. LPBITMAPINFOHEADER lpbi = lpicDecEx->lpbiDst;
  1948. iPitch = (((abs((int)lpbi->biWidth) * (int)lpbi->biBitCount) >> 3) + 3) & ~3;
  1949. // The source and destination rectangles in lpicDecEx are only
  1950. // meaningful if bIsDCI is true (because throughout our codec, if bIsDCI
  1951. // is FALSE, we put zeroes in these rectangles). This may change, at
  1952. // some later point, if we decide (or are required) to make use of the
  1953. // rcSource and rcTarget rectangles that are associated with an Active
  1954. // Movie media sample.
  1955. if (!bIsDCI)
  1956. {
  1957. if (lpbi->biHeight >= 0)
  1958. {
  1959. // Typically for RGB, a positive bitmap height corresponds
  1960. // to a negative pitch.
  1961. iPitch = -iPitch;
  1962. }
  1963. }
  1964. else
  1965. {
  1966. if(lpicDecEx->xDst > 0) /* go to proper X position */
  1967. lOffset += ((long)lpicDecEx->xDst * (long)lpbi->biBitCount) >> 3;
  1968. if(lpbi->biHeight * lpicDecEx->dxSrc < 0)
  1969. { /* DIB is bottom to top */
  1970. lOffset += (long) abs((int)lpbi->biWidth) *
  1971. (long) abs((int)lpbi->biHeight) *
  1972. ((long) lpbi->biBitCount >> 3) -
  1973. (long) iPitch;
  1974. /***************************************************************************/
  1975. /***** This next line is used to subtract the amount that Brian added *****/
  1976. /***** to CCOffsetToLine0 in COLOR.C during initialization. This is *****/
  1977. /***** needed because for DCI, the pitch he used is incorrect. *****/
  1978. /***************************************************************************/
  1979. lOffset -= ((long) yScale * (long)lpicDecEx->dySrc - 1) *
  1980. (long) lpicDecEx->dxDst * ((long) lpbi->biBitCount >> 3);
  1981. iPitch = -iPitch;
  1982. }
  1983. if(lpicDecEx->yDst > 0) /* go to proper Y position */
  1984. lOffset += ((long)lpicDecEx->yDst * (long)iPitch);
  1985. if(lpicDecEx->dxSrc > 0) {
  1986. lOffset += ((long)lpicDecEx->dyDst * (long)iPitch) - (long)iPitch;
  1987. iPitch = -iPitch;
  1988. }
  1989. if((lpicDecEx->dxDst == 0) && (lpicDecEx->dyDst == 0))
  1990. iPitch = -iPitch;
  1991. }
  1992. *lpiPitch = iPitch;
  1993. return(lOffset);
  1994. }
  1995. /************************************************************************
  1996. *
  1997. * GetDecoderOptions
  1998. *
  1999. * Get the options, saving them in the catalog
  2000. */
  2001. static void GetDecoderOptions(
  2002. T_H263DecoderCatalog * DC)
  2003. {
  2004. int bSetOptions = 1;
  2005. /* Default Options
  2006. */
  2007. const int bDefaultForceOnAspectRatioCorrection = 0;
  2008. const int bDefaultUseBlockEdgeFilter = 1;
  2009. /* INI file variables
  2010. */
  2011. #ifndef RING0
  2012. UN unResult;
  2013. #define SECTION_NAME "Decode"
  2014. #define INI_FILE_NAME "h261test.ini"
  2015. #ifdef _DEBUG
  2016. char buf132[132];
  2017. #endif
  2018. #endif
  2019. /* Read the options from the INI file
  2020. */
  2021. #ifndef RING0
  2022. {
  2023. DBOUT("Getting decode options from the ini file h261test.ini");
  2024. /* BlockEdgeFilter
  2025. */
  2026. unResult = GetPrivateProfileInt(SECTION_NAME, "BlockEdgeFilter", bDefaultUseBlockEdgeFilter, INI_FILE_NAME);
  2027. if (unResult != 0 && unResult != 1)
  2028. {
  2029. #ifdef _DEBUG
  2030. wsprintf(buf132,"BlockEdgeFilter ini value error (should be 0 or 1) - using default=%d",
  2031. (int) bDefaultUseBlockEdgeFilter);
  2032. DBOUT(buf132);
  2033. #endif
  2034. unResult = bDefaultUseBlockEdgeFilter;
  2035. }
  2036. DC->bUseBlockEdgeFilter = unResult;
  2037. /* Force on aspect ratio correction.
  2038. */
  2039. unResult = GetPrivateProfileInt(SECTION_NAME, "ForceOnAspectRatioCorrection", bDefaultForceOnAspectRatioCorrection, INI_FILE_NAME);
  2040. if (unResult != 0 && unResult != 1)
  2041. {
  2042. #ifdef _DEBUG
  2043. wsprintf(buf132,"ForceOnAspectRatioCorrection ini value error (should be 0 or 1) - using default=%d",
  2044. (int) bDefaultForceOnAspectRatioCorrection);
  2045. DBOUT(buf132);
  2046. #endif
  2047. unResult = bDefaultForceOnAspectRatioCorrection;
  2048. }
  2049. DC->bForceOnAspectRatioCorrection = unResult;
  2050. bSetOptions = 0;
  2051. }
  2052. #endif
  2053. if (bSetOptions)
  2054. {
  2055. DC->bUseBlockEdgeFilter = bDefaultUseBlockEdgeFilter;
  2056. DC->bForceOnAspectRatioCorrection = bDefaultForceOnAspectRatioCorrection;
  2057. }
  2058. /* Can only use force aspect ratio correction on if SQCIF, QCIF, or CIF
  2059. */
  2060. if (DC->bForceOnAspectRatioCorrection)
  2061. {
  2062. if (! ( ((DC->uFrameWidth == 128) && (DC->uFrameHeight == 96)) ||
  2063. ((DC->uFrameWidth == 176) && (DC->uFrameHeight == 144)) ||
  2064. ((DC->uFrameWidth == 352) && (DC->uFrameHeight == 288)) ) )
  2065. {
  2066. DBOUT("Aspect ratio correction can not be forced on unless the dimensions are SQCIF, QCIF, or CIF");
  2067. DC->bForceOnAspectRatioCorrection = 0;
  2068. }
  2069. }
  2070. /* Display the options
  2071. */
  2072. if (DC->bUseBlockEdgeFilter)
  2073. {
  2074. DBOUT("Decoder option (BlockEdgeFilter) is ON");
  2075. }
  2076. else
  2077. {
  2078. DBOUT("Decoder option (BlockEdgeFilter) is OFF");
  2079. }
  2080. if (DC->bForceOnAspectRatioCorrection)
  2081. {
  2082. DBOUT("Decoder option (ForceOnAspectRatioCorrection) is ON");
  2083. }
  2084. else
  2085. {
  2086. DBOUT("Decoder option (ForceOnAspectRatioCorrection) is OFF");
  2087. }
  2088. DBOUT("Decoder option (MMX) is OFF: get a life, get MMX");
  2089. } /* end GetDecoderOptions() */
  2090. /***********************************************************************
  2091. * Description:
  2092. * This routine parses the bit-stream and initializes two major streams:
  2093. * 1) pN: no of coefficients in each of the block (biased by 65 for INTRA)
  2094. * 2) pRun_INVERSE_Q: de-quantized coefficient stream for the frame;
  2095. * MMX stream is scaled because we use scaled IDCT.
  2096. * Other information (e.g. MVs) is kept in decoder catalog, block action
  2097. * stream, and MB infor stream.
  2098. * Parameters:
  2099. * DC: Decoder catalog ptr
  2100. * fpBlockAction: block action stream ptr
  2101. * fpMBInfo: Macroblock info ptr
  2102. * fpbsState: bit-stream state pointer
  2103. * fpu8MaxPtr: sentinel value to check for bit-stream overruns
  2104. * pN: stream of no. of coeffs (biased by block type) for each block
  2105. * pRun_INVERSE_Q:stream of de-quantized (and scaled if using MMX) coefficients
  2106. * iNumberOfGOBs: no. of GOBs in the frame
  2107. * iNumberOfMBs: no. of MBs in a GOB in the frame
  2108. * iGOB_start:
  2109. * iMB_start:
  2110. * Note:
  2111. ***********************************************************************/
  2112. #pragma code_seg("IACODE1")
  2113. // #ifndef LOSS_RECOVERY
  2114. #if 0
  2115. static LRESULT IAPass1ProcessFrame(
  2116. T_H263DecoderCatalog *DC,
  2117. T_BlkAction *fpBlockAction,
  2118. T_MBInfo *fpMBInfo,
  2119. BITSTREAM_STATE *fpbsState,
  2120. U8 *fpu8MaxPtr,
  2121. U32 *pN,
  2122. T_IQ_INDEX *pRUN_INVERSE_Q,
  2123. const I32 iNumberOfGOBs,
  2124. const I32 iNumberOfMBs,
  2125. const I32 iGOB_start,
  2126. const I32 iMB_start
  2127. )
  2128. {
  2129. I32 g, iReturn, iBlockNumber = 0 ;
  2130. I32 mb_start = iMB_start;
  2131. U32 *pNnew;
  2132. U32 uReadChecksum = 0;
  2133. I8 i;
  2134. I8 tmpcnt;
  2135. #ifdef DECODE_STATS
  2136. U32 uStartLow = DC->uStartLow;
  2137. U32 uStartHigh = DC->uStartHigh;
  2138. U32 uElapsed;
  2139. U32 uBefore;
  2140. U32 uHeadersSum = 0;
  2141. int bTimingThisFrame = DC->bTimingThisFrame;
  2142. DEC_TIMING_INFO *pDecTimingInfo = NULL;
  2143. #endif
  2144. #ifdef DECODE_STATS
  2145. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  2146. #endif
  2147. /* move decode of GOB start code outside of GOB header processing */
  2148. /* because if processing skipped macroblocks, looking for the last MBA */
  2149. /* will find the next start code */
  2150. iReturn = H263DecodeGOBStartCode(DC, fpbsState);
  2151. if (iReturn != ICERR_OK)
  2152. {
  2153. DBOUT("ERROR :: H263Decompress :: Error reading the GOB StartCode");
  2154. goto done;
  2155. }
  2156. #ifdef DECODE_STATS
  2157. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uHeadersSum)
  2158. #endif
  2159. for (g = 1 ; g <= iNumberOfGOBs; g++)
  2160. {
  2161. #ifdef DECODE_STATS
  2162. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  2163. #endif
  2164. iReturn = H263DecodeGOBHeader(DC, fpbsState, g);
  2165. if (iReturn != ICERR_OK)
  2166. {
  2167. DBOUT("ERROR :: H263Decompress :: Error reading the GOB header");
  2168. goto done;
  2169. }
  2170. #ifdef DECODE_STATS
  2171. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uHeadersSum)
  2172. #endif
  2173. DC->i16LastMBA = -1;
  2174. DC->i8MVDH = DC->i8MVDV = 0;
  2175. // re-sync iBlockNumber, fpBlockAction, fpMBInfo at this point
  2176. iBlockNumber = (g - 1) * iNumberOfMBs*6;
  2177. fpBlockAction = (T_BlkAction FAR *)((U8 *)DC + DC->X16_BlkActionStream);
  2178. fpMBInfo = (T_MBInfo FAR *) ((U8 *)DC + DC->X32_uMBInfoStream);
  2179. fpBlockAction += iBlockNumber;
  2180. fpMBInfo += iBlockNumber/6;
  2181. pNnew = (U32 *)((U8 *)DC + DC->X32_pN) + iBlockNumber;
  2182. while (pN < pNnew ) *pN++ = 0;
  2183. /* For each MB until START_CODE detected do ...
  2184. */
  2185. for (; ; iBlockNumber += 6, fpBlockAction += 6, fpMBInfo++)
  2186. {
  2187. #ifdef DECODE_STATS
  2188. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  2189. #endif
  2190. iReturn = H263DecodeMBHeader(DC, fpbsState, &uReadChecksum);
  2191. #ifdef DECODE_STATS
  2192. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uHeadersSum)
  2193. #endif
  2194. if (iReturn == START_CODE)
  2195. break;
  2196. /* If we didn't see a start code, then we either got an error,
  2197. * or we have another MBA delta in DC->uMBA.
  2198. */
  2199. if (iReturn != ICERR_OK) {
  2200. DBOUT("ERROR :: H263Decompress (First Pass) :: Error reading MB header");
  2201. goto error;
  2202. }
  2203. /* Update MBA */
  2204. DC->i16LastMBA += (I16)DC->uMBA;
  2205. if (DC->i16LastMBA > 32)
  2206. {
  2207. DBOUT("ERROR :: H263Decompress :: Bad Macro Block Address");
  2208. goto done;
  2209. }
  2210. /* New for rearch */
  2211. /* adjust for empty macroblocks */
  2212. for ( tmpcnt = (I8)DC->uMBA; tmpcnt > 1; tmpcnt--)
  2213. {
  2214. for (i=0; i<6; i++)
  2215. {
  2216. *pN = 0;
  2217. pN++;
  2218. }
  2219. iBlockNumber += 6;
  2220. fpBlockAction += 6;
  2221. /* Default fpBlockAction values were already initialized
  2222. * in (Re)InitializeBlockActionStream.
  2223. */
  2224. fpMBInfo->i8MBType = 2;
  2225. fpMBInfo++;
  2226. }
  2227. fpMBInfo->i8MBType = (I8)DC->uMBType; // New rearch
  2228. /* end of new rearch */
  2229. // decode and inverse quantize the transform coefficients
  2230. iReturn = H263DecodeMBData(DC,
  2231. fpBlockAction,
  2232. iBlockNumber,
  2233. fpbsState,
  2234. fpu8MaxPtr,
  2235. &uReadChecksum,
  2236. &pN,
  2237. &pRUN_INVERSE_Q);
  2238. if (iReturn != ICERR_OK) {
  2239. DBOUT("ERROR :: H263Decompress (First Pass) :: Error parsing MB data");
  2240. goto error;
  2241. }
  2242. } // end for each MB
  2243. /* Fill in arrays and advance Block Action stream when there
  2244. are skip MB at the end of each GOB
  2245. */
  2246. while (iBlockNumber != (I32)g*198) {
  2247. for (i=0; i<6; i++)
  2248. {
  2249. *pN = 0;
  2250. pN++;
  2251. }
  2252. iBlockNumber += 6;
  2253. fpBlockAction+= 6;
  2254. /* Default fpBlockAction values were already initialized
  2255. * in (Re)InitializeBlockActionStream.
  2256. */
  2257. fpMBInfo->i8MBType = 2;
  2258. fpMBInfo++;
  2259. }
  2260. /* allow the pointer to address up to four beyond the end - reading
  2261. * by DWORD using postincrement.
  2262. */
  2263. // ASSERT(fpbsState->fpu8 <= fpu8MaxPtr+4);
  2264. if (fpbsState->fpu8 > fpu8MaxPtr+4)
  2265. goto error;
  2266. } // End for each GOB
  2267. #ifdef DECODE_STATS
  2268. if (bTimingThisFrame)
  2269. {
  2270. pDecTimingInfo = DC->pDecTimingInfo + DC->uStatFrameCount;
  2271. pDecTimingInfo->uHeaders += uHeadersSum;
  2272. }
  2273. #endif
  2274. done:
  2275. return ICERR_OK;
  2276. error:
  2277. return ICERR_ERROR;
  2278. }
  2279. #else
  2280. static LRESULT IAPass1ProcessFrameRTP(
  2281. T_H263DecoderCatalog *DC,
  2282. T_BlkAction *fpBlockAction,
  2283. T_MBInfo *fpMBInfo,
  2284. BITSTREAM_STATE *fpbsState,
  2285. U8 *fpu8MaxPtr,
  2286. U32 *pN,
  2287. T_IQ_INDEX *pRUN_INVERSE_Q,
  2288. const I32 iNumberOfGOBs,
  2289. const I32 iNumberOfMBs,
  2290. const I32 iGOB_start,
  2291. const I32 iMB_start
  2292. )
  2293. {
  2294. BITSTREAM_STATE fpbsStateSave;
  2295. I32 g, current_g, iReturn, iBlockNumber = 0 ;
  2296. I32 mb_start = iMB_start;
  2297. U32 *pNnew;
  2298. U32 uReadChecksum = 0;
  2299. I8 i;
  2300. I8 tmpcnt;
  2301. I32 g_skip, gtmp;
  2302. I32 uMaxGOBNumber, uGOBStep, uMaxBlockNumber;
  2303. #ifdef DECODE_STATS
  2304. U32 uStartLow = DC->uStartLow;
  2305. U32 uStartHigh = DC->uStartHigh;
  2306. U32 uElapsed;
  2307. U32 uBefore;
  2308. U32 uHeadersSum = 0;
  2309. int bTimingThisFrame = DC->bTimingThisFrame;
  2310. DEC_TIMING_INFO *pDecTimingInfo = NULL;
  2311. #endif
  2312. #ifdef DECODE_STATS
  2313. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  2314. #endif
  2315. /* move decode of GOB start code outside of GOB header processing */
  2316. /* because if processing skipped macroblocks, looking for the last MBA */
  2317. /* will find the next start code */
  2318. iReturn = H263DecodeGOBStartCode(DC, fpbsState);
  2319. if (iReturn != ICERR_OK)
  2320. {
  2321. DBOUT("ERROR :: H261Decompress :: Error reading the GOB StartCode");
  2322. goto done;
  2323. }
  2324. #ifdef DECODE_STATS
  2325. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uHeadersSum)
  2326. #endif
  2327. if (iNumberOfGOBs == 3)
  2328. {
  2329. uMaxGOBNumber = 5;
  2330. uGOBStep = 2;
  2331. }
  2332. else
  2333. {
  2334. uMaxGOBNumber = 12;
  2335. uGOBStep = 1;
  2336. }
  2337. for (g = 1; g <= uMaxGOBNumber; g+=uGOBStep)
  2338. {
  2339. current_g = g;
  2340. #ifdef DECODE_STATS
  2341. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  2342. #endif
  2343. iReturn = H263DecodeGOBHeader(DC, fpbsState, g);
  2344. // #ifndef LOSS_RECOVERY
  2345. #if 0
  2346. if (iReturn != ICERR_OK)
  2347. {
  2348. DBOUT("ERROR :: H261Decompress :: Error reading the GOB header");
  2349. goto done;
  2350. }
  2351. #else
  2352. if (iReturn == PACKET_FAULT_AT_MB_OR_GOB)
  2353. {
  2354. DBOUT("Packet fault at MBA or GBSC detected.");
  2355. current_g -= uGOBStep; // back up to previous GOB
  2356. iReturn = RtpH261FindNextPacket(DC, fpbsState, &pN,
  2357. (U32 *)&(DC->uPQuant), (int *)&mb_start, (int *) &g
  2358. );
  2359. switch (iReturn)
  2360. {
  2361. case NEXT_MODE_STARTS_GOB:
  2362. // Next packet is the start of a GOB; mark missing
  2363. // macroblocks as skipped, then read GOB start code,
  2364. // and continue in the GOB loop.
  2365. // Save bitstream state
  2366. DBOUT("Next packet is NEXT_MODE_STARTS_GOB");
  2367. fpbsStateSave.fpu8 = fpbsState->fpu8;
  2368. fpbsStateSave.uWork = fpbsState->uWork;
  2369. fpbsStateSave.uBitsReady = fpbsState->uBitsReady;
  2370. // Read GOB start code
  2371. iReturn = H263DecodeGOBStartCode(DC, fpbsState);
  2372. if (iReturn != ICERR_OK)
  2373. {
  2374. DBOUT("ERROR :: H261Decompress :: Error reading the GOB StartCode");
  2375. goto done;
  2376. }
  2377. // Read GOB Header
  2378. iReturn = H263DecodeGOBHeader(DC, fpbsState, g);
  2379. if (iReturn != ICERR_OK)
  2380. {
  2381. DBOUT("ERROR :: H261Decompress :: Error reading the GOB header");
  2382. goto done;
  2383. }
  2384. g = DC->uGroupNumber;
  2385. // Restore bitstream state
  2386. fpbsState->fpu8 = fpbsStateSave.fpu8;
  2387. fpbsState->uWork = fpbsStateSave.uWork;
  2388. fpbsState->uBitsReady = fpbsStateSave.uBitsReady;
  2389. // re-sync iBlockNumber, fpBlockAction, fpMBInfo at this point
  2390. if (DC->uSrcFormat == SRC_FORMAT_QCIF)
  2391. g_skip = (g - 1) >> 1;
  2392. else
  2393. g_skip = g - 1 ;
  2394. iBlockNumber = g_skip * iNumberOfMBs * 6;
  2395. fpBlockAction = (T_BlkAction FAR *)((U8 *)DC + DC->X16_BlkActionStream);
  2396. fpMBInfo = (T_MBInfo FAR *) ((U8 *)DC + DC->X32_uMBInfoStream);
  2397. fpBlockAction += iBlockNumber;
  2398. fpMBInfo += iBlockNumber/6;
  2399. pNnew = (U32 *)((U8 *)DC + DC->X32_pN) + iBlockNumber;
  2400. while (pN < pNnew )
  2401. *pN++ = 0;
  2402. // Now read the GOB start code and get ready to
  2403. // process the new GOB.
  2404. iReturn = H263DecodeGOBStartCode(DC, fpbsState);
  2405. if (iReturn != ICERR_OK)
  2406. {
  2407. DBOUT("ERROR :: H261Decompress :: Error reading the GOB StartCode");
  2408. goto done;
  2409. }
  2410. g -= uGOBStep;
  2411. continue;
  2412. break;
  2413. case NEXT_MODE_STARTS_MB :
  2414. // Next packet starts with a macroblock; check the
  2415. // GOB Number and mark all lost macroblocks as
  2416. // skipped; initialize MBA and motion vector
  2417. // predictors from the block action stream and
  2418. // jump to the macroblock loop
  2419. DBOUT("Next packet is NEXT_MODE_STARTS_MB");
  2420. if (DC->uSrcFormat == SRC_FORMAT_QCIF)
  2421. g_skip = (g - 1) >> 1;
  2422. else
  2423. g_skip = g - 1;
  2424. iBlockNumber = iNumberOfMBs * g_skip * 6 +
  2425. (mb_start+1) * 6;
  2426. fpBlockAction = (T_BlkAction FAR *)((U8 *)DC + DC->X16_BlkActionStream);
  2427. fpMBInfo = (T_MBInfo FAR *) ((U8 *)DC + DC->X32_uMBInfoStream);
  2428. fpBlockAction += iBlockNumber;
  2429. fpMBInfo += iBlockNumber/6;
  2430. DC->uMQuant = DC->uPQuant;
  2431. //DC->i16LastMBA = (U16) (mb_start - 1);
  2432. DC->i16LastMBA = (U16) (mb_start);
  2433. pNnew = (U32 *)((U8 *)DC + DC->X32_pN) + iBlockNumber;
  2434. while (pN < pNnew )
  2435. *pN++ = 0;
  2436. goto MB_LOOP;
  2437. break;
  2438. case NEXT_MODE_LAST: // all remaining packets in frame lost !!
  2439. DBOUT("Next packet is NEXT_MODE_LAST");
  2440. uMaxBlockNumber = iNumberOfMBs * iNumberOfGOBs * 6;
  2441. pNnew = (U32 *)((U8 *)DC + DC->X32_pN) + uMaxBlockNumber;
  2442. while (pN < pNnew )
  2443. *pN++ = 0;
  2444. iReturn = ICERR_OK;
  2445. goto done;
  2446. break;
  2447. default: // should never happen !!
  2448. iReturn = ICERR_ERROR;
  2449. goto done;
  2450. } // end switch
  2451. }
  2452. else
  2453. {
  2454. if (iReturn == PACKET_FAULT_AT_PSC) // can only happen for the PSC packet
  2455. {
  2456. DBOUT("PSC packet fault detected");
  2457. iReturn = RtpGetPicHeaderFromBsExt(DC);
  2458. if (iReturn != ICERR_OK)
  2459. {
  2460. DBOUT("ERROR:: cannot read Picture Header from RTP Trailer");
  2461. goto done;
  2462. }
  2463. iReturn = RtpH261FindNextPacket(DC, fpbsState, &pN,
  2464. (U32 *)&(DC->uPQuant), (int *)&mb_start, (int *) &g);
  2465. switch (iReturn)
  2466. {
  2467. case NEXT_MODE_STARTS_GOB:
  2468. // Next packet is the start of a GOB; mark missing
  2469. // macroblocks as skipped, then read GOB start code,
  2470. // and continue in the GOB loop.
  2471. // re-sync iBlockNumber, fpBlockAction, fpMBInfo at this point
  2472. // Save bitstream state
  2473. DBOUT("Next packet is NEXT_MODE_STARTS_GOB");
  2474. fpbsStateSave.fpu8 = fpbsState->fpu8;
  2475. fpbsStateSave.uWork = fpbsState->uWork;
  2476. fpbsStateSave.uBitsReady = fpbsState->uBitsReady;
  2477. // Read GOB start code
  2478. iReturn = H263DecodeGOBStartCode(DC, fpbsState);
  2479. if (iReturn != ICERR_OK)
  2480. {
  2481. DBOUT("ERROR :: H261Decompress :: Error reading the GOB StartCode");
  2482. goto done;
  2483. }
  2484. // Read GOB Header
  2485. iReturn = H263DecodeGOBHeader(DC, fpbsState, g);
  2486. if (iReturn != ICERR_OK)
  2487. {
  2488. DBOUT("ERROR :: H261Decompress :: Error reading the GOB header");
  2489. goto done;
  2490. }
  2491. g = DC->uGroupNumber;
  2492. // Restore bitstream state
  2493. fpbsState->fpu8 = fpbsStateSave.fpu8;
  2494. fpbsState->uWork = fpbsStateSave.uWork;
  2495. fpbsState->uBitsReady = fpbsStateSave.uBitsReady;
  2496. if (DC->uSrcFormat == SRC_FORMAT_QCIF)
  2497. g_skip = (g - 1) >> 1;
  2498. else
  2499. g_skip = g - 1;
  2500. iBlockNumber = g_skip * iNumberOfMBs * 6;
  2501. fpBlockAction = (T_BlkAction FAR *)((U8 *)DC + DC->X16_BlkActionStream);
  2502. fpMBInfo = (T_MBInfo FAR *) ((U8 *)DC + DC->X32_uMBInfoStream);
  2503. fpBlockAction += iBlockNumber;
  2504. fpMBInfo += iBlockNumber/6;
  2505. pNnew = (U32 *)((U8 *)DC + DC->X32_pN) + iBlockNumber;
  2506. while (pN < pNnew )
  2507. *pN++ = 0;
  2508. // Now read the GOB start code and get ready to
  2509. // process the new GOB.
  2510. iReturn = H263DecodeGOBStartCode(DC, fpbsState);
  2511. if (iReturn != ICERR_OK)
  2512. {
  2513. DBOUT("ERROR :: H261Decompress :: Error reading the GOB StartCode");
  2514. goto done;
  2515. }
  2516. g -= uGOBStep;
  2517. continue;
  2518. break;
  2519. case NEXT_MODE_STARTS_MB :
  2520. // Next packet starts with a macroblock; check the
  2521. // GOB Number and mark all lost macroblocks as
  2522. // skipped; initialize MBA and motion vector
  2523. // predictors from the block action stream and
  2524. // jump to the macroblock loop
  2525. DBOUT("Next packet is NEXT_MODE_STARTS_MB");
  2526. if (DC->uSrcFormat == SRC_FORMAT_QCIF)
  2527. g_skip = (g - 1) >> 1;
  2528. else
  2529. g_skip = g - 1;
  2530. iBlockNumber = iNumberOfMBs * g_skip * 6 +
  2531. (mb_start+1) * 6;
  2532. fpBlockAction = (T_BlkAction FAR *)((U8 *)DC + DC->X16_BlkActionStream);
  2533. fpMBInfo = (T_MBInfo FAR *) ((U8 *)DC + DC->X32_uMBInfoStream);
  2534. fpBlockAction += iBlockNumber;
  2535. fpMBInfo += iBlockNumber/6;
  2536. DC->uMQuant = DC->uPQuant;
  2537. //DC->i16LastMBA = (U16) (mb_start - 1);
  2538. DC->i16LastMBA = (U16) (mb_start);
  2539. pNnew = (U32 *)((U8 *)DC + DC->X32_pN) + iBlockNumber;
  2540. while (pN < pNnew )
  2541. *pN++ = 0;
  2542. goto MB_LOOP;
  2543. break;
  2544. case NEXT_MODE_LAST: // all remaining packets in frame lost !!
  2545. DBOUT("Next packet is NEXT_MODE_LAST");
  2546. uMaxBlockNumber = iNumberOfMBs * iNumberOfGOBs * 6;
  2547. pNnew = (U32 *)((U8 *)DC + DC->X32_pN) + uMaxBlockNumber;
  2548. while (pN < pNnew )
  2549. *pN++ = 0;
  2550. iReturn = ICERR_OK;
  2551. goto done;
  2552. break;
  2553. default: // should never happen !!
  2554. iReturn = ICERR_ERROR;
  2555. goto done;
  2556. } // end switch
  2557. } // if .. PACKET_FAULT_AT_PSC
  2558. else
  2559. {
  2560. if (iReturn == ICERR_ERROR)
  2561. {
  2562. DBOUT("ERROR :: H261Decompress :: Error reading GOB header");
  2563. DBOUT(" Packet fault not detected");
  2564. goto done;
  2565. }
  2566. // Outdated: Do the source format check here when it is known that
  2567. // the PSC was not the canned one from the PPM.
  2568. /* if (DC->bReadSrcFormat && DC->uPrevSrcFormat != DC->uSrcFormat)
  2569. {
  2570. DBOUT("ERROR::src format changed detected with no packet loss");
  2571. DBOUT(" not supported ... bailing out");
  2572. iReturn=ICERR_ERROR;
  2573. goto done;
  2574. }
  2575. DC->uPrevSrcFormat = DC->uSrcFormat;
  2576. DC->bReadSrcFormat = TRUE; */
  2577. }
  2578. }
  2579. #endif
  2580. #ifdef DECODE_STATS
  2581. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uHeadersSum)
  2582. #endif
  2583. DC->i16LastMBA = -1;
  2584. DC->i8MVDH = DC->i8MVDV = 0;
  2585. // re-sync iBlockNumber, fpBlockAction, fpMBInfo at this point
  2586. if (DC->uSrcFormat == SRC_FORMAT_QCIF)
  2587. iBlockNumber = ((g - 1)>>1) * iNumberOfMBs*6;
  2588. else
  2589. iBlockNumber = (g - 1)* iNumberOfMBs*6;
  2590. fpBlockAction = (T_BlkAction FAR *)((U8 *)DC + DC->X16_BlkActionStream);
  2591. fpMBInfo = (T_MBInfo FAR *) ((U8 *)DC + DC->X32_uMBInfoStream);
  2592. fpBlockAction += iBlockNumber;
  2593. fpMBInfo += iBlockNumber/6;
  2594. pNnew = (U32 *)((U8 *)DC + DC->X32_pN) + iBlockNumber;
  2595. while (pN < pNnew ) *pN++ = 0;
  2596. /* For each MB until START_CODE detected do ...
  2597. */
  2598. MB_LOOP:
  2599. for (; ; iBlockNumber += 6, fpBlockAction += 6, fpMBInfo++)
  2600. {
  2601. #ifdef DECODE_STATS
  2602. TIMER_BEFORE(bTimingThisFrame,uStartLow,uStartHigh,uBefore);
  2603. #endif
  2604. iReturn = H263DecodeMBHeader(DC, fpbsState, &uReadChecksum);
  2605. #ifdef DECODE_STATS
  2606. TIMER_AFTER_P5(bTimingThisFrame,uStartLow,uStartHigh,uBefore,uElapsed,uHeadersSum)
  2607. #endif
  2608. if (iReturn == START_CODE)
  2609. break;
  2610. /* If we didn't see a start code, then we either got an error,
  2611. * or we have another MBA delta in DC->uMBA.
  2612. */
  2613. if (iReturn != ICERR_OK) {
  2614. DBOUT("ERROR :: H263Decompress (First Pass) :: Error reading MB header");
  2615. goto error;
  2616. }
  2617. /* Update MBA */
  2618. DC->i16LastMBA += (I16)DC->uMBA;
  2619. if (DC->i16LastMBA > 32)
  2620. {
  2621. DBOUT("ERROR :: H263Decompress :: Bad Macro Block Address");
  2622. goto done;
  2623. }
  2624. /* New for rearch */
  2625. /* adjust for empty macroblocks */
  2626. for ( tmpcnt = (I8)DC->uMBA; tmpcnt > 1; tmpcnt--)
  2627. {
  2628. for (i=0; i<6; i++)
  2629. {
  2630. *pN = 0;
  2631. pN++;
  2632. }
  2633. iBlockNumber += 6;
  2634. fpBlockAction += 6;
  2635. /* Default fpBlockAction values were already initialized
  2636. * in (Re)InitializeBlockActionStream.
  2637. */
  2638. fpMBInfo->i8MBType = 2;
  2639. fpMBInfo++;
  2640. }
  2641. fpMBInfo->i8MBType = (I8)DC->uMBType; // New rearch
  2642. /* end of new rearch */
  2643. // decode and inverse quantize the transform coefficients
  2644. iReturn = H263DecodeMBData(DC,
  2645. fpBlockAction,
  2646. iBlockNumber,
  2647. fpbsState,
  2648. fpu8MaxPtr,
  2649. &uReadChecksum,
  2650. &pN,
  2651. &pRUN_INVERSE_Q);
  2652. if (iReturn != ICERR_OK) {
  2653. DBOUT("ERROR :: H263Decompress (First Pass) :: Error parsing MB data");
  2654. goto error;
  2655. }
  2656. } // end for each MB
  2657. /* Fill in arrays and advance Block Action stream when there
  2658. are skip MB at the end of each GOB
  2659. */
  2660. if (DC->uSrcFormat == SRC_FORMAT_QCIF)
  2661. {
  2662. switch (g)
  2663. {
  2664. case 1:
  2665. gtmp = 1;
  2666. break;
  2667. case 3:
  2668. gtmp = 2;
  2669. break;
  2670. case 5:
  2671. gtmp = 3;
  2672. break;
  2673. default:
  2674. DBOUT("Bad GOB Number");
  2675. iReturn = ICERR_ERROR;
  2676. goto error;
  2677. break;
  2678. }
  2679. }
  2680. else
  2681. gtmp = g;
  2682. while (iBlockNumber != (I32)gtmp*198) {
  2683. for (i=0; i<6; i++)
  2684. {
  2685. *pN = 0;
  2686. pN++;
  2687. }
  2688. iBlockNumber += 6;
  2689. fpBlockAction+= 6;
  2690. /* Default fpBlockAction values were already initialized
  2691. * in (Re)InitializeBlockActionStream.
  2692. */
  2693. fpMBInfo->i8MBType = 2;
  2694. fpMBInfo++;
  2695. }
  2696. /* allow the pointer to address up to four beyond the end - reading
  2697. * by DWORD using postincrement.
  2698. */
  2699. ASSERT(fpbsState->fpu8 <= fpu8MaxPtr+4);
  2700. } // End for each GOB
  2701. #ifdef DECODE_STATS
  2702. if (bTimingThisFrame)
  2703. {
  2704. pDecTimingInfo = DC->pDecTimingInfo + DC->uStatFrameCount;
  2705. pDecTimingInfo->uHeaders += uHeadersSum;
  2706. }
  2707. #endif
  2708. done:
  2709. return ICERR_OK;
  2710. error:
  2711. return ICERR_ERROR;
  2712. }
  2713. #endif
  2714. #pragma code_seg()
  2715. /***********************************************************************
  2716. * Description:
  2717. * This routines does IDCT and motion compensation.
  2718. * Parameters:
  2719. * DC: Decoder catalog ptr
  2720. * fpBlockAction: block action stream ptr
  2721. * fpMBInfo: Macroblock info ptr
  2722. * pN: stream of no. of coeffs (biased by block type) for each block
  2723. * pRun_INVERSE_Q:stream of de-quantized (and scaled if using MMX) coefficients
  2724. * iNumberOfGOBs: no. of GOBs in the frame
  2725. * iNumberOfMBs: no. of MBs in a GOB in the frame
  2726. * Note:
  2727. ***********************************************************************/
  2728. #pragma code_seg("IACODE2")
  2729. static void IAPass2ProcessFrame(
  2730. T_H263DecoderCatalog *DC,
  2731. T_BlkAction *fpBlockAction,
  2732. T_MBInfo *fpMBInfo,
  2733. U32 *pN,
  2734. T_IQ_INDEX *pRUN_INVERSE_Q,
  2735. const I32 iNumberOfGOBs,
  2736. const I32 iNumberOfMBs
  2737. )
  2738. {
  2739. I32 g, m, b, iEdgeFlag=0;
  2740. // for each GOB do
  2741. for (g = 1 ; g <= iNumberOfGOBs; g++)
  2742. {
  2743. // for each MB do
  2744. for (m = 1; m <= iNumberOfMBs; m++, fpBlockAction+=6, fpMBInfo++)
  2745. {
  2746. // for each block do
  2747. for (b = 0; b < 6; b++) { // AP-NEW
  2748. // do inverse transform & motion compensation for the block
  2749. H263IDCTandMC(DC, fpBlockAction, b, m, g, pN, pRUN_INVERSE_Q,
  2750. fpMBInfo, iEdgeFlag); // AP-NEW
  2751. // Adjust pointers for next block
  2752. if ( *pN >= 65 )
  2753. pRUN_INVERSE_Q += *pN - 65;
  2754. else
  2755. pRUN_INVERSE_Q += *pN;
  2756. pN++;
  2757. } // end for each block
  2758. } // end for each MB
  2759. } // End for each GOB
  2760. }
  2761. #pragma code_seg()
  2762. // rearch