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.

353 lines
9.7 KiB

  1. /* File: sv_h261_marker.c */
  2. /*****************************************************************************
  3. ** Copyright (c) Digital Equipment Corporation, 1995, 1997 **
  4. ** **
  5. ** All Rights Reserved. Unpublished rights reserved under the copyright **
  6. ** laws of the United States. **
  7. ** **
  8. ** The software contained on this media is proprietary to and embodies **
  9. ** the confidential technology of Digital Equipment Corporation. **
  10. ** Possession, use, duplication or dissemination of the software and **
  11. ** media is authorized only pursuant to a valid written license from **
  12. ** Digital Equipment Corporation. **
  13. ** **
  14. ** RESTRICTED RIGHTS LEGEND Use, duplication, or disclosure by the U.S. **
  15. ** Government is subject to restrictions as set forth in Subparagraph **
  16. ** (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, as applicable. **
  17. ******************************************************************************/
  18. /*************************************************************
  19. This file contains most of the marker information.
  20. *************************************************************/
  21. /*
  22. #define _VERBOSE_
  23. */
  24. /*LABEL marker.c */
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "sv_intrn.h"
  29. #include "SC.h"
  30. #include "SC_err.h"
  31. #include "sv_h261.h"
  32. #include "proto.h"
  33. #include "sv_proto.h"
  34. #include "h261.h"
  35. /*PRIVATE*/
  36. /*extern int TemporalReference;
  37. extern int PType;
  38. */
  39. extern int Type2;
  40. /*
  41. extern int MType;
  42. extern int GQuant;
  43. extern int MQuant;
  44. */
  45. /*
  46. extern int MVDH;
  47. extern int MVDV;
  48. extern int CBP;
  49. */
  50. /*
  51. extern int ParityEnable;
  52. extern int PSpareEnable;
  53. extern int GSpareEnable;
  54. extern int Parity;
  55. extern int PSpare;
  56. extern int GSpare;
  57. extern int GRead;
  58. extern int MBA;
  59. extern int LastMBA;
  60. extern int LastMVDV;
  61. extern int LastMVDH;
  62. extern int LastMType;
  63. */
  64. extern int QuantMType[];
  65. extern int CBPMType[];
  66. extern int MFMType[];
  67. extern int extend_mask[];
  68. const int bit_set_mask[] =
  69. {0x00000001,0x00000002,0x00000004,0x00000008,
  70. 0x00000010,0x00000020,0x00000040,0x00000080,
  71. 0x00000100,0x00000200,0x00000400,0x00000800,
  72. 0x00001000,0x00002000,0x00004000,0x00008000,
  73. 0x00010000,0x00020000,0x00040000,0x00080000,
  74. 0x00100000,0x00200000,0x00400000,0x00800000,
  75. 0x01000000,0x02000000,0x04000000,0x08000000,
  76. 0x10000000,0x20000000,0x40000000,0x80000000};
  77. /*
  78. ** Function: WritePictureHeader()
  79. ** Purpose: Writes the header of picture out to the stream.
  80. ** One of these is necessary before every frame is transmitted.
  81. */
  82. void WritePictureHeader(SvH261Info_t *H261, ScBitstream_t *bs)
  83. {
  84. sc_vprintf("WritePictureHeader()\n");
  85. ScBSPutBits(bs, H261_PICTURE_START_CODE, H261_PICTURE_START_CODE_LEN);
  86. ScBSPutBits(bs, H261->TemporalReference, 5);
  87. ScBSPutBits(bs, H261->PType, 6);
  88. if (H261->PSpareEnable)
  89. {
  90. ScBSPutBit(bs, 1);
  91. ScBSPutBits(bs, H261->PSpare, 8);
  92. }
  93. ScBSPutBit(bs, 0);
  94. }
  95. /*
  96. ** Function: ReadPictureHeader()
  97. ** Purpose: Reads the header off of the stream. It assumes that the
  98. ** first PSC has already been read in. (Necessary to tell the
  99. ** difference between a new picture and another GOB.)
  100. */
  101. void ReadPictureHeader(SvH261Info_t *H261, ScBitstream_t *bs)
  102. {
  103. sc_vprintf ("ReadPictureHeader \n");
  104. H261->TemporalReference = (int) ScBSGetBits(bs,5);
  105. H261->PType = (int)ScBSGetBits(bs,6);
  106. for(H261->PSpareEnable = 0;ScBSGetBit(bs);)
  107. {
  108. H261->PSpareEnable=1;
  109. H261->PSpare = (int)ScBSGetBits(bs,8);
  110. }
  111. }
  112. /*
  113. ** Function: WriteGOBHeader()
  114. ** Purpose: Writes a GOB out to the stream.
  115. */
  116. void WriteGOBHeader(SvH261Info_t *H261, ScBitstream_t *bs)
  117. {
  118. sc_vprintf("WriteGOBHeader()\n");
  119. ScBSPutBits(bs, H261_GOB_START_CODE, H261_GOB_START_CODE_LEN);
  120. ScBSPutBits(bs, H261->GRead+1, 4);
  121. ScBSPutBits(bs, H261->GQuant, 5);
  122. if (H261->GSpareEnable)
  123. {
  124. ScBSPutBit(bs, 1);
  125. ScBSPutBits(bs, H261->GSpare, 8);
  126. }
  127. ScBSPutBit(bs, 0);
  128. }
  129. /*
  130. ** Function: ReadHeaderTrailer()
  131. ** Purpose: Reads the trailer of the PSC or H261_GOB_START_CODE code. It is
  132. ** used to determine whether it is just a GOB or a new picture.
  133. */
  134. void ReadHeaderTrailer(SvH261Info_t *H261, ScBitstream_t *bs)
  135. {
  136. sc_vprintf("ReadHeaderTrailer \n");
  137. H261->GRead = (int)ScBSGetBits(bs, 4)-1;
  138. }
  139. /*
  140. ** Function: ReadHeaderHeader()
  141. ** Purpose: Reads the header header off of the stream. This is
  142. ** a precursor to the GOB read or the PSC read.
  143. ** Return: -1 on error
  144. */
  145. SvStatus_t ReadHeaderHeader(SvH261Info_t *H261, ScBitstream_t *bs)
  146. {
  147. int input;
  148. sc_vprintf("ReadHeaderHeader\n");
  149. input = (int)ScBSPeekBits(bs, H261_GOB_START_CODE_LEN);
  150. if (input != H261_GOB_START_CODE)
  151. {
  152. if (!ScBSSeekStopBefore(bs, H261_GOB_START_CODE, H261_GOB_START_CODE_LEN))
  153. {
  154. sc_dprintf("Illegal GOB Start Code. Read: %d\n",input);
  155. return(SvErrorIllegalGBSC);
  156. }
  157. }
  158. input = (int)ScBSGetBits(bs, H261_GOB_START_CODE_LEN);
  159. return(NoErrors);
  160. }
  161. /*
  162. ** Function: ReadGOBHeader()
  163. ** Purpose: Reads the GOB information off of the stream. We assume
  164. ** that the first bits have been read in by ReadHeaderHeader...
  165. ** or some such routine.
  166. */
  167. void ReadGOBHeader(SvH261Info_t *H261, ScBitstream_t *bs)
  168. {
  169. sc_vprintf("ReadGOBHeader()\n");
  170. H261->GQuant =(int)ScBSGetBits(bs,5);
  171. for(H261->GSpareEnable=0; ScBSGetBit(bs);)
  172. {
  173. H261->GSpareEnable = 1;
  174. H261->GSpare = (int)ScBSGetBits(bs,8);
  175. }
  176. }
  177. /*
  178. ** Function: WriteMBHeader()
  179. ** Purpose: Writes a macro-block out to the stream.
  180. */
  181. SvStatus_t WriteMBHeader(SvH261Info_t *H261, ScBitstream_t *bs)
  182. {
  183. int TempH,TempV;
  184. ScBSPosition_t Start;
  185. sc_vprintf("WriteMBHeader()\n");
  186. sc_dprintf("\n Macro Block Type is %d and MQuant is %d",
  187. H261->MType, H261->MQuant);
  188. Start=ScBSBitPosition(bs); /* Start=swtellb(H261); */
  189. if (!sv_H261HuffEncode(H261,bs,H261->MBA,H261->MBAEHuff))
  190. {
  191. sc_dprintf("Attempting to write an empty Huffman code.\n");
  192. return (SvErrorEmptyHuff);
  193. }
  194. if (!sv_H261HuffEncode(H261,bs,H261->MType,H261->T3EHuff))
  195. {
  196. sc_dprintf("Attempting to write an empty Huffman code.\n");
  197. return (SvErrorEmptyHuff);
  198. }
  199. if (QuantMType[H261->MType])
  200. ScBSPutBits(bs, H261->MQuant, 5); /* mputvb(H261, 5, H261->MQuant); */
  201. H261->NumberBitsCoded=0;
  202. if (MFMType[H261->MType])
  203. {
  204. if ((!MFMType[H261->LastMType])||(H261->MBA!=1)||
  205. (H261->LastMBA==-1)||(H261->LastMBA==10)||(H261->LastMBA==21))
  206. {
  207. if (!sv_H261HuffEncode(H261,bs,(H261->MVDH&0x1f),H261->MVDEHuff)||
  208. !sv_H261HuffEncode(H261,bs,(H261->MVDV&0x1f),H261->MVDEHuff))
  209. {
  210. sc_dprintf("Cannot encode motion vectors.\n");
  211. return (SvErrorEncodingMV);
  212. }
  213. }
  214. else
  215. {
  216. TempH = H261->MVDH - H261->LastMVDH;
  217. if (TempH < -16) TempH += 32;
  218. if (TempH > 15) TempH -= 32;
  219. TempV = H261->MVDV - H261->LastMVDV;
  220. if (TempV < -16) TempV += 32;
  221. if (TempV > 15) TempV -= 32;
  222. if (!sv_H261HuffEncode(H261,bs,TempH&0x1f,H261->MVDEHuff)||
  223. !sv_H261HuffEncode(H261,bs,TempV&0x1f,H261->MVDEHuff))
  224. {
  225. sc_dprintf("Cannot encode motion vectors.\n");
  226. return (SvErrorEncodingMV);
  227. }
  228. }
  229. H261->LastMVDV = H261->MVDV;
  230. H261->LastMVDH = H261->MVDH;
  231. }
  232. else
  233. {
  234. H261->LastMVDV=H261->LastMVDH=H261->MVDV=H261->MVDH=0; /* Redundant in most cases */
  235. }
  236. H261->MotionVectorBits+=H261->NumberBitsCoded;
  237. if (CBPMType[H261->MType])
  238. {
  239. if (!sv_H261HuffEncode(H261,bs,H261->CBP,H261->CBPEHuff))
  240. {
  241. sc_dprintf("CBP write error\n");
  242. return (SvErrorCBPWrite);
  243. }
  244. }
  245. H261->Current_MBBits = ScBSBitPosition(bs)-Start; /* (swtellb(H261)-Start); */
  246. H261->MacroAttributeBits+=H261->Current_MBBits ;
  247. return (NoErrors);
  248. }
  249. /*
  250. ** Function: ReadMBHeader()
  251. ** Purpose: Reads the macroblock header from the stream.
  252. */
  253. int ReadMBHeader(SvH261Info_t *H261, ScBitstream_t *bs)
  254. {
  255. DHUFF *huff = H261->MBADHuff;
  256. register unsigned short cb;
  257. register int State, temp;
  258. do {
  259. DecodeHuff(bs, huff, State, cb, temp);
  260. } while (State == 34 && ! bs->EOI); /* Get rid of stuff bits */
  261. H261->MBA = State;
  262. if (H261->MBA == 35 || bs->EOI)
  263. return(-1); /* Start of Picture Headers */
  264. H261->LastMType = H261->MType;
  265. huff = H261->T3DHuff;
  266. DecodeHuff(bs, huff, State, cb, temp);
  267. H261->MType = State;
  268. if (QuantMType[H261->MType])
  269. H261->MQuant = (int)ScBSGetBits(bs,5);
  270. huff = H261->MVDDHuff;
  271. if (MFMType[H261->MType])
  272. {
  273. if ((!MFMType[H261->LastMType])||(H261->MBA!=1)||
  274. (H261->LastMBA==-1)||(H261->LastMBA==10)||(H261->LastMBA==21))
  275. {
  276. DecodeHuff(bs, huff, State, cb, temp);
  277. if (State & bit_set_mask[4])
  278. H261->MVDH = State | extend_mask[4];
  279. else
  280. H261->MVDH = State;
  281. DecodeHuff(bs, huff, State, cb, temp);
  282. if (State & bit_set_mask[4])
  283. H261->MVDV = State | extend_mask[4];
  284. else
  285. H261->MVDV = State;
  286. }
  287. else
  288. {
  289. DecodeHuff(bs, huff, State, cb, temp);
  290. if (State & bit_set_mask[4])
  291. State |= extend_mask[4];
  292. H261->MVDH += State;
  293. DecodeHuff(bs, huff, State, cb, temp);
  294. if (State & bit_set_mask[4])
  295. State |= extend_mask[4];
  296. H261->MVDV += State;
  297. if (H261->MVDH < -16) H261->MVDH += 32;
  298. if (H261->MVDH > 15) H261->MVDH -= 32;
  299. if (H261->MVDV < -16) H261->MVDV += 32;
  300. if (H261->MVDV > 15) H261->MVDV -= 32;
  301. }
  302. }
  303. else
  304. H261->MVDV=H261->MVDH=0; /* Theoretically redundant */
  305. if (CBPMType[H261->MType])
  306. {
  307. huff = H261->CBPDHuff;
  308. DecodeHuff(bs, huff, State, cb, temp);
  309. H261->CBP = State;
  310. }
  311. return(0);
  312. }