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.

355 lines
11 KiB

  1. /* File: sv_h263_decode.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. #define _SLIBDEBUG_
  20. */
  21. #include "sv_h263.h"
  22. #include "sv_intrn.h"
  23. #include "SC_err.h"
  24. #include "sv_proto.h"
  25. #include "proto.h"
  26. #ifdef _SLIBDEBUG_
  27. #include "sc_debug.h"
  28. #define _DEBUG_ 0 /* detailed debuging statements */
  29. #define _VERBOSE_ 1 /* show progress */
  30. #define _VERIFY_ 1 /* verify correct operation */
  31. #define _WARN_ 1 /* warnings about strange behavior */
  32. #endif
  33. #ifdef USE_TIME
  34. #ifndef WIN32
  35. #include <sys/time.h>
  36. #else
  37. #include <windows.h>
  38. #endif
  39. #endif
  40. #ifdef WIN32
  41. #include <io.h>
  42. #endif
  43. #ifdef WINDOWS
  44. int initDisplay (int pels, int lines);
  45. int closeDisplay ();
  46. #endif
  47. /************************************************************/
  48. /************************************************************/
  49. SvStatus_t svH263Decompress(SvCodecInfo_t *Info, u_char **ImagePtr)
  50. {
  51. SvH263DecompressInfo_t *H263Info = Info->h263dcmp;
  52. SvStatus_t status;
  53. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "sv_H263Decompress() bytepos=%ld\n",
  54. ScBSBytePosition(Info->BSIn)) );
  55. if (H263Info->framenum==0)
  56. {
  57. sv_H263GetPicture(Info);
  58. H263Info->framenum++;
  59. }
  60. else
  61. {
  62. status=sv_H263GetHeader(H263Info, Info->BSIn, NULL);
  63. if (status==SvErrorNone)
  64. {
  65. sv_H263GetPicture(Info);
  66. H263Info->framenum++;
  67. }
  68. else
  69. return(status); /* error */
  70. }
  71. if (H263Info->pb_frame)
  72. *ImagePtr=H263Info->bframe[0];
  73. else
  74. *ImagePtr=H263Info->newframe[0];
  75. return(SvErrorNone);
  76. }
  77. /************************************************************/
  78. /************************************************************/
  79. SvStatus_t svH263InitDecompressor(SvCodecInfo_t *Info)
  80. {
  81. SvH263DecompressInfo_t *H263Info = Info->h263dcmp;
  82. ScBitstream_t *BSIn = Info->BSIn;
  83. int i, cc, size;
  84. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "sv_H263InitDecoder()\n") );
  85. /* svH263Initbits(H263Info->inputfilename); */
  86. H263Info->temp_ref = 0;
  87. /*
  88. verbose : verbose level
  89. outtype :
  90. T_YUV 0 : YUV
  91. H263_T_SIF 1 : SIF
  92. H263_T_TGA 2 : TGA
  93. H263_T_PPM 3 : PPM
  94. H263_T_X11 4 : X11 Unix Display
  95. H263_T_YUV_CONC 5 : YUV concatenated
  96. H263_T_WIN 6 : Windows 95/NT Display
  97. quiet : disable warnings to stderr\n\
  98. refidct : use double precision reference IDCT\n\
  99. trace : enable low level tracing\n");
  100. frame_rate :
  101. n=0 : as fast as possible\n\
  102. n=99 : read frame rate from bitstream (default)\n");
  103. */
  104. if (!H263Info->inited)
  105. {
  106. H263Info->frame_rate = 0;
  107. H263Info->verbose = 0;
  108. H263Info->outtype = H263_T_WIN;
  109. H263Info->refidct = 0;
  110. H263Info->expand = 0;
  111. H263Info->trace = 0;
  112. H263Info->quiet = 1;
  113. /* pointer to name of output files */
  114. if (H263Info->outtype==H263_T_X11 || H263Info->outtype == H263_T_WIN)
  115. H263Info->outputname = "";
  116. else H263Info->outputname = H263_DEF_OUTPUTNAME;
  117. }
  118. /* initial frame number */
  119. H263Info->framenum = 0;
  120. if (BSIn && sv_H263GetHeader(H263Info, BSIn, NULL)!=SvErrorNone)
  121. return(SvErrorEndBitstream);
  122. /* MPEG-1 = TMN parameters */
  123. H263Info->matrix_coefficients = 5;
  124. switch (H263Info->source_format) {
  125. case (H263_SF_SQCIF):
  126. H263Info->horizontal_size = 128;
  127. H263Info->vertical_size = 96;
  128. break;
  129. case (H263_SF_QCIF):
  130. H263Info->horizontal_size = 176;
  131. H263Info->vertical_size = 144;
  132. break;
  133. case (H263_SF_CIF):
  134. H263Info->horizontal_size = 352;
  135. H263Info->vertical_size = 288;
  136. break;
  137. case (H263_SF_4CIF):
  138. H263Info->horizontal_size = 704;
  139. H263Info->vertical_size = 576;
  140. break;
  141. case (H263_SF_16CIF):
  142. H263Info->horizontal_size = 1408;
  143. H263Info->vertical_size = 1152;
  144. break;
  145. default:
  146. _SlibDebug(_VERBOSE_ || _WARN_,
  147. ScDebugPrintf(H263Info->dbg, "svH263InitDecompressor() Illegal input format\n") );
  148. return(ScErrorUnrecognizedFormat);
  149. }
  150. H263Info->mb_width = H263Info->horizontal_size/16;
  151. H263Info->mb_height = H263Info->vertical_size/16;
  152. H263Info->coded_picture_width = H263Info->horizontal_size;
  153. H263Info->coded_picture_height = H263Info->vertical_size;
  154. H263Info->chrom_width = H263Info->coded_picture_width>>1;
  155. H263Info->chrom_height = H263Info->coded_picture_height>>1;
  156. H263Info->blk_cnt = 6;
  157. if (!H263Info->inited)
  158. {
  159. unsigned char *frameptr;
  160. unsigned int ysize, chromsize;
  161. ysize = H263Info->coded_picture_width*H263Info->coded_picture_height;
  162. chromsize = H263Info->chrom_width*H263Info->chrom_height;
  163. /* clip table */
  164. if (!(H263Info->clp=(unsigned char *)ScAlloc(1024)))
  165. return(SvErrorMemory);
  166. H263Info->clp += 384;
  167. for (i=-384; i<640; i++)
  168. H263Info->clp[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
  169. H263Info->block=(int (*)[66])ScPaMalloc(12*sizeof(int [66]));
  170. if (H263Info->block==NULL)
  171. return(SvErrorMemory);
  172. /* allocate buffers for P, reference, B frames */
  173. if ((frameptr=(unsigned char *)ScPaMalloc(ysize + chromsize*2))==NULL)
  174. return(SvErrorMemory);
  175. H263Info->refframe[0] = frameptr;
  176. H263Info->refframe[1] = frameptr+ysize;
  177. H263Info->refframe[2] = frameptr+ysize+chromsize;
  178. /* initialize image buffer with black */
  179. memset(H263Info->refframe[0], 16, ysize);
  180. memset(H263Info->refframe[1], 128, chromsize);
  181. memset(H263Info->refframe[2], 128, chromsize);
  182. if ((frameptr=(unsigned char *)ScPaMalloc(ysize + chromsize*2))==NULL)
  183. return(SvErrorMemory);
  184. H263Info->oldrefframe[0] = frameptr;
  185. H263Info->oldrefframe[1] = frameptr+ysize;
  186. H263Info->oldrefframe[2] = frameptr+ysize+chromsize;
  187. /* initialize image buffer with black */
  188. memset(H263Info->oldrefframe[0], 16, ysize);
  189. memset(H263Info->oldrefframe[1], 128, chromsize);
  190. memset(H263Info->oldrefframe[2], 128, chromsize);
  191. if ((frameptr=(unsigned char *)ScPaMalloc(ysize + chromsize*2))==NULL)
  192. return(SvErrorMemory);
  193. H263Info->bframe[0] = frameptr;
  194. H263Info->bframe[1] = frameptr+ysize;
  195. H263Info->bframe[2] = frameptr+ysize+chromsize;
  196. /* initialize image buffer with black */
  197. memset(H263Info->bframe[0], 16, ysize);
  198. memset(H263Info->bframe[1], 128, chromsize);
  199. memset(H263Info->bframe[2], 128, chromsize);
  200. /* allocate buffers for edge frames */
  201. for (cc=0; cc<3; cc++) {
  202. if (cc==0) {
  203. size = (H263Info->coded_picture_width+64)*(H263Info->coded_picture_height+64);
  204. if (!(H263Info->edgeframeorig[cc] = (unsigned char *)ScAlloc(size)))
  205. return(SvErrorMemory);
  206. H263Info->edgeframe[cc] = H263Info->edgeframeorig[cc] +
  207. (H263Info->coded_picture_width+64) * 32 + 32;
  208. }
  209. else {
  210. size = (H263Info->chrom_width+32)*(H263Info->chrom_height+32);
  211. if (!(H263Info->edgeframeorig[cc] = (unsigned char *)ScAlloc(size)))
  212. return(SvErrorMemory);
  213. H263Info->edgeframe[cc] = H263Info->edgeframeorig[cc] + (H263Info->chrom_width+32) * 16 + 16;
  214. }
  215. }
  216. if (H263Info->expand) {
  217. for (cc=0; cc<3; cc++) {
  218. if (cc==0)
  219. size = H263Info->coded_picture_width*H263Info->coded_picture_height*4;
  220. else
  221. size = H263Info->chrom_width*H263Info->chrom_height*4;
  222. if (!(H263Info->exnewframe[cc] = (unsigned char *)ScAlloc(size)))
  223. return(SvErrorMemory);
  224. }
  225. }
  226. /* IDCT */
  227. #ifdef H263_C_CODE
  228. if (H263Info->refidct)
  229. svH263Init_idctref();
  230. else
  231. svH263Init_idct();
  232. #endif
  233. }
  234. #if 0
  235. /* Clear output file for concatenated storing */
  236. if (H263Info->outtype == H263_T_YUV_CONC) {
  237. FILE *cleared;
  238. if ((cleared = fopen(H263Info->outputname,"wb")) == NULL) {
  239. fclose(cleared);
  240. svH263Error("couldn't clear outputfile\n");
  241. }
  242. else
  243. fclose(cleared);
  244. }
  245. #ifdef DISPLAY
  246. if (H263Info->outtype==H263_T_X11) {
  247. svH263Init_display("");
  248. }
  249. #endif
  250. #ifdef WINDOWS
  251. if (H263Info->outtype==H263_T_WIN)
  252. initDisplay(H263Info->coded_picture_width, H263Info->coded_picture_height);
  253. #endif
  254. #endif
  255. H263Info->inited=TRUE;
  256. return(SvErrorNone);
  257. }
  258. /************************************************************/
  259. /************************************************************/
  260. SvStatus_t svH263FreeDecompressor(SvCodecInfo_t *Info)
  261. {
  262. SvH263DecompressInfo_t *H263Info = Info->h263dcmp;
  263. int cc;
  264. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "sv_H263FreeDecoder()\n") );
  265. if (!H263Info->inited)
  266. return(SvErrorNone);
  267. #ifdef DISPLAY
  268. if (H263Info->outtype==H263_T_X11)
  269. svH263Exit_display();
  270. #endif
  271. #ifdef WINDOWS
  272. if (H263Info->outtype == H263_T_WIN)
  273. closeDisplay();
  274. #endif
  275. #if 0
  276. #if SC_READ
  277. svH263Stopbits();
  278. #else
  279. /* close input file */
  280. close(H263Info->base.infile);
  281. #endif
  282. #endif
  283. /* clip table */
  284. H263Info->clp -= 384;
  285. ScFree(H263Info->clp);
  286. ScPaFree(H263Info->block);
  287. /* allocate buffers for P, reference, B frames */
  288. ScPaFree(H263Info->refframe[0]) ;
  289. ScPaFree(H263Info->oldrefframe[0]);
  290. ScPaFree(H263Info->bframe[0]);
  291. /* allocate buffers for edge frames */
  292. for (cc=0; cc<3; cc++) {
  293. if (cc==0) ScFree(H263Info->edgeframeorig[cc]);
  294. else ScFree(H263Info->edgeframeorig[cc]);
  295. }
  296. if (H263Info->expand)
  297. for (cc=0; cc<3; cc++)
  298. ScFree(H263Info->exnewframe[cc]);
  299. H263Info->inited=FALSE;
  300. return(SvErrorNone);
  301. }
  302. /************************************************************/
  303. /************************************************************/
  304. void svH263Error(char *text)
  305. {
  306. /* fprintf(stderr,text); */
  307. /* exit(1); */
  308. }
  309. /************************************************************/
  310. /************************************************************/
  311. #if 0
  312. /* trace output */
  313. void svH263Printbits(code,bits,len)
  314. int code,bits,len;
  315. {
  316. int i;
  317. for (i=0; i<len; i++) printf("%d",(code>>(bits-1-i))&1);
  318. return;
  319. }
  320. #endif