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.

429 lines
14 KiB

  1. /* *************************************************************************
  2. ** INTEL Corporation Proprietary Information
  3. **
  4. ** This listing is supplied under the terms of a license
  5. ** agreement with INTEL Corporation and may not be copied
  6. ** nor disclosed except in accordance with the terms of
  7. ** that agreement.
  8. **
  9. ** Copyright (c) 1995 Intel Corporation.
  10. ** Copyright (c) 1996 Intel Corporation.
  11. ** All Rights Reserved.
  12. **
  13. ** *************************************************************************
  14. //
  15. // e3rtp.cpp
  16. //
  17. // Description:
  18. // This file is for RTP payload generation. See EPS for details.
  19. //
  20. // Routines:
  21. // getRTPPacketSizeThreshold
  22. // H263RTP_InitBsInfoStream
  23. // H263RTP_ResetBsInfoStream
  24. // H263RTPFindMVs
  25. // H263RTP_UpdateBsInfo
  26. // H263RTP_TermBsInfoStream
  27. // H263RTP_AttachBsInfoStream
  28. // IsIntraCoded
  29. // H263RTP_GetMaxBsInfoStreamSize()
  30. //
  31. // -------------------------------------------------------------------------
  32. //
  33. // $Author: gmlim $
  34. // $Date: 17 Apr 1997 16:54:02 $
  35. // $Archive: S:\h26x\src\enc\e3rtp.cpv $
  36. // $Header: S:\h26x\src\enc\e3rtp.cpv 1.14 17 Apr 1997 16:54:02 gmlim $
  37. // $Log: S:\h26x\src\enc\e3rtp.cpv $
  38. //
  39. // Rev 1.14 17 Apr 1997 16:54:02 gmlim
  40. // Added H263RTP_GetMaxBsInfoStreamSize().
  41. //
  42. // Rev 1.13 06 Mar 1997 16:06:26 gmlim
  43. // Changed RTP to generate mode A packet at the beginning of a GOB.
  44. //
  45. // Rev 1.12 18 Feb 1997 15:33:06 CZHU
  46. // Changed UpdateBSInfo() not to force packet at GOB all the time.
  47. //
  48. // Rev 1.11 07 Feb 1997 10:57:28 CZHU
  49. // Added three entry in EC to remove static variable used in e3rtp.cpp
  50. //
  51. // Rev 1.10 24 Jan 1997 13:33:36 CZHU
  52. //
  53. // Stop generating more packets when internal buffer is to overflow.
  54. //
  55. // Rev 1.9 11 Dec 1996 10:38:24 gmlim
  56. // Removed unused pBsInfoStream from H263RTP_AttachBsInfoStream().
  57. //
  58. // Rev 1.8 05 Dec 1996 17:01:08 GMLIM
  59. // Changed the way RTP packetization was done to guarantee proper packet
  60. // size. Created H263RTP_ResetBsInfoStream() and replaced two previous
  61. // bitstream info update fucntions with H263RTP_UpdateBsInfo().
  62. //
  63. // Rev 1.7 06 Nov 1996 16:31:06 gmlim
  64. // Removed H263ModeC def.s and did some cleanup.
  65. //
  66. // Rev 1.6 03 Nov 1996 18:44:42 gmlim
  67. // Added support for mode c.
  68. //
  69. // Rev 1.5 24 Oct 1996 16:27:50 KLILLEVO
  70. // changed from DBOUT to DbgLog
  71. //
  72. // Rev 1.4 25 Sep 1996 10:55:28 CZHU
  73. // Added checking null pointers at allocation and before use.
  74. //
  75. // Rev 1.3 16 Sep 1996 16:50:48 CZHU
  76. // changed RTP BS Init for smaller packet size
  77. //
  78. // Rev 1.2 29 Aug 1996 09:31:00 CZHU
  79. // Added a function checking intra-GOB
  80. //
  81. // Rev 1.1 28 Apr 1996 20:09:04 BECHOLS
  82. //
  83. // Removed RTP_HEADER IFDEFs.
  84. //
  85. // Rev 1.0 22 Apr 1996 17:46:10 BECHOLS
  86. // Initial revision.
  87. //
  88. // Rev 1.7 10 Apr 1996 13:33:04 CZHU
  89. // Moved packet loss sim to c3rtp.cpp
  90. //
  91. // Rev 1.6 29 Mar 1996 13:37:42 CZHU
  92. //
  93. // Rev 1.5 01 Mar 1996 16:37:08 DBRUCKS
  94. // change to use 3/4ths of packet size as the threshold
  95. // change to make packet size a parameter
  96. //
  97. // Rev 1.4 23 Feb 1996 17:36:48 CZHU
  98. //
  99. // Rev 1.3 23 Feb 1996 16:18:28 CZHU
  100. // integrate with build 29
  101. //
  102. // Rev 1.2 15 Feb 1996 12:00:42 CZHU
  103. // Clean up
  104. //
  105. // Rev 1.1 14 Feb 1996 14:59:36 CZHU
  106. // Support both mode A and mode B payload modes.
  107. //
  108. // Rev 1.0 12 Feb 1996 17:04:44 CZHU
  109. // Initial revision.
  110. //
  111. // Rev 1.5 25 Jan 1996 16:14:34 CZHU
  112. // name changes
  113. //
  114. // Rev 1.4 15 Dec 1995 13:06:46 CZHU
  115. //
  116. // Rev 1.3 11 Dec 1995 14:52:42 CZHU
  117. // Added support for per MB packetization
  118. //
  119. // Rev 1.2 04 Dec 1995 16:50:26 CZHU
  120. //
  121. // Rev 1.1 01 Dec 1995 15:53:52 CZHU
  122. // Included Init() and Term() functions.
  123. //
  124. // Rev 1.0 01 Dec 1995 15:31:02 CZHU
  125. // Initial revision.
  126. ** *************************************************************************/
  127. #include "precomp.h"
  128. #ifdef TRACK_ALLOCATIONS
  129. char gsz1[32];
  130. #endif
  131. static U32 uBitOffset_currPacket;
  132. static U8 *pBitStream_currPacket;
  133. static U8 *pBitStream_lastPacket;
  134. // ---------------------------------------------------------------------------
  135. // getRTPPacketSizeThreshold()
  136. // Helper function to calculate the threshold of packet size
  137. // for given maximum packet size and data rate
  138. // ---------------------------------------------------------------------------
  139. static U32 getRTPPacketSizeThreshold(U32 uRequested)
  140. {
  141. U32 uSize;
  142. // uSize = uRequested * 90 / 100;
  143. uSize = uRequested;
  144. ASSERT(uSize);
  145. return uSize;
  146. }
  147. // ---------------------------------------------------------------------------
  148. // H263RTP_InitBsInfoStream()
  149. // ---------------------------------------------------------------------------
  150. I32 H263RTP_InitBsInfoStream(LPCODINST lpInst, T_H263EncoderCatalog *EC)
  151. {
  152. U32 uBsInfoSize = getRTPBsInfoSize(lpInst);
  153. FX_ENTRY("H263RTP_InitBsInfoStream")
  154. if (EC->hBsInfoStream != NULL)
  155. {
  156. HeapFree(GetProcessHeap(), NULL, EC->pBaseBsInfoStream);
  157. #ifdef TRACK_ALLOCATIONS
  158. // Track memory allocation
  159. RemoveName((unsigned int)EC->pBaseBsInfoStream);
  160. #endif
  161. }
  162. EC->pBaseBsInfoStream = HeapAlloc(GetProcessHeap(), NULL, uBsInfoSize);
  163. if (EC->pBaseBsInfoStream == NULL)
  164. {
  165. lpInst->Configuration.bRTPHeader = FALSE;
  166. return FALSE;
  167. }
  168. #ifdef TRACK_ALLOCATIONS
  169. // Track memory allocation
  170. wsprintf(gsz1, "E3RTP: %7ld Ln %5ld\0", uBsInfoSize, __LINE__);
  171. AddName((unsigned int)EC->pBaseBsInfoStream, gsz1);
  172. #endif
  173. EC->hBsInfoStream = (void *) uBsInfoSize;
  174. EC->uPacketSizeThreshold = getRTPPacketSizeThreshold(lpInst->Configuration.unPacketSize);
  175. DEBUGMSG(ZONE_INIT, ("%s: BsInfoStream initialized\r\n", _fx_));
  176. return TRUE;
  177. }
  178. // ---------------------------------------------------------------------------
  179. // H263RTP_ResetBsInfoStream()
  180. // ---------------------------------------------------------------------------
  181. void H263RTP_ResetBsInfoStream(T_H263EncoderCatalog *EC)
  182. {
  183. FX_ENTRY("H263RTP_ResetBsInfoStream")
  184. EC->pBsInfoStream = EC->pBaseBsInfoStream;
  185. EC->uBase = 0;
  186. EC->uNumOfPackets = 0;
  187. uBitOffset_currPacket = 0;
  188. pBitStream_currPacket = EC->PictureHeader.PB ? EC->pU8_BitStrCopy :
  189. EC->pU8_BitStream;
  190. pBitStream_lastPacket = pBitStream_currPacket;
  191. DEBUGMSG(ZONE_ENCODE_RTP, ("%s: BsInfoStream reset\r\n", _fx_));
  192. }
  193. // ---------------------------------------------------------------------------
  194. // H263RTPFindMVs()
  195. // Find motion vector predictors for current MB and return in arraryMVs[]
  196. // ---------------------------------------------------------------------------
  197. U32 H263RTPFindMVs(
  198. T_H263EncoderCatalog * EC,
  199. T_MBlockActionStream * pMBlockAction,
  200. U32 uMBA,
  201. U32 uGOBN,
  202. I8 arrayMVs[2]
  203. )
  204. {
  205. if (!uMBA)
  206. {
  207. arrayMVs[0] = 0;
  208. arrayMVs[1] = 0;
  209. }
  210. else // revisit for AP
  211. {
  212. arrayMVs[0] = pMBlockAction[-1].BlkY1.PHMV;
  213. arrayMVs[1] = pMBlockAction[-1].BlkY1.PVMV;
  214. }
  215. return TRUE;
  216. }
  217. // ---------------------------------------------------------------------------
  218. // H263RTP_UpdateBsInfo()
  219. // This routine is called at the beginning of each MB to update the bitstream
  220. // info buffer
  221. // ---------------------------------------------------------------------------
  222. I32 H263RTP_UpdateBsInfo(
  223. T_H263EncoderCatalog *EC,
  224. T_MBlockActionStream *pMBlockAction,
  225. U32 uQuant,
  226. U32 uMBA,
  227. U32 uGOBN,
  228. U8 *pBitStream,
  229. U32 uBitOffset
  230. )
  231. {
  232. U32 uNewBytes;
  233. T_RTP_H263_BSINFO *pBsInfoStream;
  234. I8 arrayMVs[2];
  235. FX_ENTRY("H263RTP_UpdateBsInfo")
  236. if (EC->pBsInfoStream == NULL) return FALSE;
  237. if (uMBA)
  238. {
  239. if ((U32) (pBitStream - pBitStream_lastPacket) <
  240. EC->uPacketSizeThreshold)
  241. {
  242. pBitStream_currPacket = pBitStream;
  243. uBitOffset_currPacket = uBitOffset;
  244. return TRUE;
  245. }
  246. pBsInfoStream = (T_RTP_H263_BSINFO *) EC->pBsInfoStream;
  247. pBsInfoStream->u8Mode = EC->PictureHeader.PB ? RTP_H263_MODE_C :
  248. RTP_H263_MODE_B;
  249. pBsInfoStream->u8MBA = (U8) uMBA;
  250. pBsInfoStream->u8Quant = (U8) uQuant;
  251. pBsInfoStream->u8GOBN = (U8) uGOBN;
  252. H263RTPFindMVs(EC, pMBlockAction, uMBA, uGOBN, arrayMVs);
  253. pBsInfoStream->i8HMV1 = arrayMVs[0];
  254. pBsInfoStream->i8VMV1 = arrayMVs[1];
  255. }
  256. else
  257. {
  258. pBsInfoStream = (T_RTP_H263_BSINFO *) EC->pBsInfoStream;
  259. pBsInfoStream->u8Mode = RTP_H263_MODE_A;
  260. pBsInfoStream->u8MBA = 0;
  261. pBsInfoStream->u8Quant = 0;
  262. pBsInfoStream->u8GOBN = (U8) uGOBN;
  263. pBsInfoStream->i8HMV1 = 0;
  264. pBsInfoStream->i8VMV1 = 0;
  265. }
  266. uNewBytes = (U32) (pBitStream_currPacket - pBitStream_lastPacket);
  267. EC->uBase += uNewBytes;
  268. pBsInfoStream->uBitOffset = uBitOffset_currPacket + (EC->uBase << 3);
  269. pBsInfoStream->i8HMV2 = 0;
  270. pBsInfoStream->i8VMV2 = 0;
  271. pBsInfoStream->uFlags = 0;
  272. DEBUGMSG(ZONE_ENCODE_RTP, ("%s: Flag=%d,Mode=%d,GOB=%d,MB=%d,Quant=%d,BitOffset=%d,pBitStream=%lx,LastPacketSz=%d B\r\n", _fx_, pBsInfoStream->uFlags, pBsInfoStream->u8Mode, pBsInfoStream->u8GOBN, pBsInfoStream->u8MBA, pBsInfoStream->u8Quant, pBsInfoStream->uBitOffset, (U32) pBitStream_currPacket, uNewBytes));
  273. // update packet pointers
  274. pBitStream_lastPacket = pBitStream_currPacket;
  275. pBitStream_currPacket = pBitStream;
  276. uBitOffset_currPacket = uBitOffset;
  277. // create a new packet: update counter and pointer
  278. EC->uNumOfPackets ++;
  279. EC->pBsInfoStream = (void *) ++ pBsInfoStream;
  280. ASSERT((DWORD) EC->hBsInfoStream >
  281. (DWORD) EC->pBsInfoStream - (DWORD) EC->pBaseBsInfoStream);
  282. return TRUE;
  283. } // H263RTP_UpdateBsInfo()
  284. // ---------------------------------------------------------------------------
  285. // H263RTP_TermBsInfoStream()
  286. // ---------------------------------------------------------------------------
  287. void H263RTP_TermBsInfoStream(T_H263EncoderCatalog * EC)
  288. {
  289. FX_ENTRY("H263RTP_TermBsInfoStream")
  290. DEBUGMSG(ZONE_INIT, ("%s: BsInfoStream freed\r\n", _fx_));
  291. HeapFree(GetProcessHeap(), NULL, EC->pBaseBsInfoStream);
  292. #ifdef TRACK_ALLOCATIONS
  293. // Track memory allocation
  294. RemoveName((unsigned int)EC->pBaseBsInfoStream);
  295. #endif
  296. EC->hBsInfoStream= NULL;
  297. return;
  298. }
  299. // ---------------------------------------------------------------------------
  300. // H263RTP_AttachBsInfoStream()
  301. // ---------------------------------------------------------------------------
  302. U32 H263RTP_AttachBsInfoStream(
  303. T_H263EncoderCatalog * EC,
  304. U8 *lpOutput,
  305. U32 uSize
  306. )
  307. {
  308. U32 uIncreasedSize;
  309. U8 *lpAligned;
  310. T_H263_RTP_BSINFO_TRAILER BsInfoTrailer;
  311. // build bsinfo for the last packets
  312. BsInfoTrailer.uVersion = H263_RTP_PAYLOAD_VERSION;
  313. BsInfoTrailer.uFlags = 0;
  314. BsInfoTrailer.uUniqueCode = H263_RTP_BS_START_CODE;
  315. BsInfoTrailer.uCompressedSize = uSize;
  316. BsInfoTrailer.uNumOfPackets = EC->uNumOfPackets;
  317. BsInfoTrailer.u8Src = EC->FrameSz;
  318. BsInfoTrailer.u8TR = EC->PictureHeader.TR;
  319. if (EC->PictureHeader.PicCodType == INTRAPIC)
  320. BsInfoTrailer.uFlags |= RTP_H26X_INTRA_CODED;
  321. if (EC->PictureHeader.PB == ON)
  322. {
  323. BsInfoTrailer.u8TRB = EC->PictureHeader.TRB;
  324. BsInfoTrailer.u8DBQ = EC->PictureHeader.DBQUANT;
  325. BsInfoTrailer.uFlags |= RTP_H263_PB;
  326. }
  327. else
  328. {
  329. BsInfoTrailer.u8TRB = 0;
  330. BsInfoTrailer.u8DBQ = 0;
  331. }
  332. if (EC->PictureHeader.AP == ON)
  333. BsInfoTrailer.uFlags |= RTP_H263_AP;
  334. if (EC->PictureHeader.SAC == ON)
  335. BsInfoTrailer.uFlags |= RTP_H263_SAC;
  336. // update size field for the last BsInfoTrailer
  337. uIncreasedSize = EC->uNumOfPackets * sizeof(T_RTP_H263_BSINFO);
  338. // copy extended BS info and trailer to the given output buffer
  339. lpAligned = (U8 *) ((U32) (lpOutput + uSize + 3) & 0xfffffffc);
  340. memcpy(lpAligned, EC->pBaseBsInfoStream, uIncreasedSize);
  341. memcpy(lpAligned + uIncreasedSize, &BsInfoTrailer,
  342. sizeof(T_H263_RTP_BSINFO_TRAILER));
  343. return(uIncreasedSize + sizeof(T_H263_RTP_BSINFO_TRAILER)
  344. + (U32) (lpAligned - lpOutput - uSize));
  345. }
  346. // ---------------------------------------------------------------------------
  347. // IsIntraCoded(EC, GOB)
  348. // return TRUE if current GOB is intra coded.
  349. // other wise FALSE;
  350. // Chad for intra GOB
  351. // ---------------------------------------------------------------------------
  352. BOOL IsIntraCoded(T_H263EncoderCatalog * EC, U32 Gob)
  353. {
  354. U32 uGobMax, uGobMin;
  355. if (EC->uNumberForcedIntraMBs)
  356. {
  357. // for those GOBs are forced intra
  358. uGobMax = EC->uNextIntraMB / EC->NumMBPerRow;
  359. uGobMin = uGobMax - EC->uNumberForcedIntraMBs / EC->NumMBPerRow;
  360. if (Gob >= uGobMin && Gob < uGobMax)
  361. return TRUE;
  362. }
  363. return FALSE;
  364. }
  365. // ---------------------------------------------------------------------------
  366. // H263RTP_GetMaxBsInfoStreamSize()
  367. // return max size of EBS with trailer + 3 allignment bytes - 4/16/97 Gim
  368. // ---------------------------------------------------------------------------
  369. U32 H263RTP_GetMaxBsInfoStreamSize(T_H263EncoderCatalog *EC)
  370. {
  371. return (EC->uNumOfPackets * sizeof(T_RTP_H263_BSINFO) +
  372. sizeof(T_H263_RTP_BSINFO_TRAILER) + 3);
  373. }