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.

863 lines
32 KiB

  1. /*
  2. * @DEC_COPYRIGHT@
  3. */
  4. /*
  5. * HISTORY
  6. * $Log: slib_video.c,v $
  7. * Revision 1.1.6.13 1996/12/13 18:19:11 Hans_Graves
  8. * Added initialization of VideoPTimeBase.
  9. * [1996/12/13 18:07:51 Hans_Graves]
  10. *
  11. * Revision 1.1.6.12 1996/12/10 19:22:01 Hans_Graves
  12. * Made calculate video positions more accurate using slibFrameToTime100().
  13. * [1996/12/10 19:16:24 Hans_Graves]
  14. *
  15. * Revision 1.1.6.11 1996/11/18 23:07:40 Hans_Graves
  16. * Make use of presentation timestamps. Make seeking time-based.
  17. * [1996/11/18 22:48:05 Hans_Graves]
  18. *
  19. * Revision 1.1.6.10 1996/11/11 18:21:11 Hans_Graves
  20. * Moved setting of VideoMainStream to slib_api.c
  21. * [1996/11/11 18:02:11 Hans_Graves]
  22. *
  23. * Revision 1.1.6.9 1996/11/08 21:51:09 Hans_Graves
  24. * Added AC3 support. Better seperation of stream types.
  25. * [1996/11/08 21:28:03 Hans_Graves]
  26. *
  27. * Revision 1.1.6.8 1996/10/28 17:32:36 Hans_Graves
  28. * MME-1402, 1431, 1435: Timestamp related changes.
  29. * [1996/10/28 17:23:11 Hans_Graves]
  30. *
  31. * Revision 1.1.6.7 1996/10/12 17:18:59 Hans_Graves
  32. * Seperated TYPE_MPEG2_SYSTEMS into TRANSPORT and PROGRAM.
  33. * [1996/10/12 17:03:19 Hans_Graves]
  34. *
  35. * Revision 1.1.6.6 1996/09/29 22:19:45 Hans_Graves
  36. * Added Stride support. YUY2 fixups.
  37. * [1996/09/29 21:32:24 Hans_Graves]
  38. *
  39. * Revision 1.1.6.5 1996/09/25 19:16:51 Hans_Graves
  40. * Fix up support for YUY2. Add SLIB_INTERNAL define.
  41. * [1996/09/25 19:01:18 Hans_Graves]
  42. *
  43. * Revision 1.1.6.4 1996/09/23 18:04:06 Hans_Graves
  44. * Add reallocation of ScaleBuf if width/height changes.
  45. * [1996/09/23 17:58:27 Hans_Graves]
  46. *
  47. * Revision 1.1.6.3 1996/09/18 23:47:25 Hans_Graves
  48. * Added MPEG2 YUV 4:2:2 handling
  49. * [1996/09/18 22:04:18 Hans_Graves]
  50. *
  51. * Revision 1.1.6.2 1996/05/07 19:56:25 Hans_Graves
  52. * Added HUFF_SUPPORT.
  53. * [1996/05/07 17:21:23 Hans_Graves]
  54. *
  55. * Revision 1.1.4.7 1996/05/02 17:10:37 Hans_Graves
  56. * Reject a data type when header info is not found. Fixes MME-01234
  57. * [1996/05/02 17:09:53 Hans_Graves]
  58. *
  59. * Revision 1.1.4.6 1996/04/22 15:04:56 Hans_Graves
  60. * Renamed slibVerifyVideoParams() to slibValidateVideoParams()
  61. * [1996/04/22 14:44:29 Hans_Graves]
  62. *
  63. * Revision 1.1.4.5 1996/04/19 21:52:28 Hans_Graves
  64. * Fix Height and Width checking for H261
  65. * [1996/04/19 21:46:27 Hans_Graves]
  66. *
  67. * Revision 1.1.4.4 1996/04/01 19:07:58 Hans_Graves
  68. * And some error checking
  69. * [1996/04/01 19:04:42 Hans_Graves]
  70. *
  71. * Revision 1.1.4.3 1996/03/29 22:21:37 Hans_Graves
  72. * Added MPEG/JPEG/H261_SUPPORT ifdefs
  73. * [1996/03/29 21:57:04 Hans_Graves]
  74. *
  75. * Added MPEG-I Systems encoding support
  76. * [1996/03/27 21:56:00 Hans_Graves]
  77. *
  78. * Revision 1.1.4.2 1996/03/08 18:46:51 Hans_Graves
  79. * Added slibVerifyVideoParams()
  80. * [1996/03/08 18:36:51 Hans_Graves]
  81. *
  82. * Revision 1.1.2.12 1996/02/19 18:04:00 Hans_Graves
  83. * Fixed a number of MPEG related bugs
  84. * [1996/02/19 17:57:50 Hans_Graves]
  85. *
  86. * Revision 1.1.2.11 1996/02/07 23:24:01 Hans_Graves
  87. * Added SEEK_EXACT. Fixed most frame counting problems.
  88. * [1996/02/07 23:20:39 Hans_Graves]
  89. *
  90. * Revision 1.1.2.10 1996/02/02 17:36:06 Hans_Graves
  91. * Enhanced audio info. Cleaned up API
  92. * [1996/02/02 17:29:51 Hans_Graves]
  93. *
  94. * Revision 1.1.2.9 1996/01/30 22:23:10 Hans_Graves
  95. * Added AVI YUV support
  96. * [1996/01/30 22:21:45 Hans_Graves]
  97. *
  98. * Revision 1.1.2.8 1996/01/15 16:26:33 Hans_Graves
  99. * No video if SLIB_TYPE_MPEG1_AUDIO or SLIB_TYPE_WAVE
  100. * [1996/01/15 15:47:40 Hans_Graves]
  101. *
  102. * Revision 1.1.2.7 1996/01/11 16:17:36 Hans_Graves
  103. * Added MPEG II Systems decode support
  104. * [1996/01/11 16:12:41 Hans_Graves]
  105. *
  106. * Revision 1.1.2.6 1996/01/08 16:41:35 Hans_Graves
  107. * Added MPEG II decoding support
  108. * [1996/01/08 15:53:10 Hans_Graves]
  109. *
  110. * Revision 1.1.2.5 1995/12/08 20:01:24 Hans_Graves
  111. * Added H.261 compression support.
  112. * [1995/12/08 20:00:52 Hans_Graves]
  113. *
  114. * Revision 1.1.2.4 1995/12/07 19:31:37 Hans_Graves
  115. * Added JPEG Decoding and MPEG encoding support
  116. * [1995/12/07 18:30:12 Hans_Graves]
  117. *
  118. * Revision 1.1.2.3 1995/11/09 23:14:08 Hans_Graves
  119. * Added GetVideoTime()
  120. * [1995/11/09 23:09:19 Hans_Graves]
  121. *
  122. * Revision 1.1.2.2 1995/11/06 18:47:57 Hans_Graves
  123. * First time under SLIB
  124. * [1995/11/06 18:36:05 Hans_Graves]
  125. *
  126. * $EndLog$
  127. */
  128. /*****************************************************************************
  129. ** Copyright (c) Digital Equipment Corporation, 1995 **
  130. ** **
  131. ** All Rights Reserved. Unpublished rights reserved under the copyright **
  132. ** laws of the United States. **
  133. ** **
  134. ** The software contained on this media is proprietary to and embodies **
  135. ** the confidential technology of Digital Equipment Corporation. **
  136. ** Possession, use, duplication or dissemination of the software and **
  137. ** media is authorized only pursuant to a valid written license from **
  138. ** Digital Equipment Corporation. **
  139. ** **
  140. ** RESTRICTED RIGHTS LEGEND Use, duplication, or disclosure by the U.S. **
  141. ** Government is subject to restrictions as set forth in Subparagraph **
  142. ** (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, as applicable. **
  143. ******************************************************************************/
  144. /*
  145. #define _SLIBDEBUG_
  146. */
  147. #define SLIB_INTERNAL
  148. #include "slib.h"
  149. #include "mpeg.h"
  150. #include "h261.h"
  151. #include "h263.h"
  152. #include "jpeg.h"
  153. #include "avi.h"
  154. #ifdef _SLIBDEBUG_
  155. #define _DEBUG_ 1 /* detailed debuging statements */
  156. #define _VERBOSE_ 1 /* show progress */
  157. #define _VERIFY_ 1 /* verify correct operation */
  158. #define _WARN_ 1 /* warnings about strange behavior */
  159. #endif
  160. int slibCalcBits(unsigned dword fourcc, int currentbits)
  161. {
  162. switch (fourcc)
  163. {
  164. case BI_DECMPEGDIB:
  165. case BI_DECH261DIB:
  166. case BI_MSH261DIB:
  167. case BI_DECH263DIB:
  168. case BI_MSH263DIB:
  169. case JPEG_DIB:
  170. return(24);
  171. case MJPG_DIB:
  172. case BI_YU12SEP: /* YUV 4:1:1 Planar */
  173. return(24);
  174. case BI_DECYUVDIB: /* YUV 4:2:2 Packed */
  175. case BI_YUY2: /* YUV 4:2:2 Packed */
  176. return(16);
  177. case BI_YU16SEP: /* YUV 4:2:2 Planar */
  178. return(24);
  179. case BI_YVU9SEP: /* YUV 16:1:1 Planar */
  180. return(24);
  181. }
  182. return(currentbits);
  183. }
  184. static unsigned dword slibCalcImageSize(unsigned dword fourcc, int bits,
  185. int width, int height)
  186. {
  187. unsigned dword imagesize=0;
  188. if (width<0) width=-width;
  189. if (height<0) height=-height;
  190. switch (fourcc)
  191. {
  192. case BI_YVU9SEP: /* YUV 16:1:1 Planar */
  193. imagesize = (width*height*5)/4;
  194. break;
  195. case BI_YU12SEP: /* YUV 4:1:1 Planar */
  196. imagesize = (width*height*3)/2;
  197. break;
  198. case BI_DECYUVDIB: /* YUV 4:2:2 Packed */
  199. case BI_YUY2: /* YUV 4:2:2 Packed */
  200. case BI_YU16SEP: /* YUV 4:2:2 Planar */
  201. imagesize = width*height*2;
  202. break;
  203. #ifndef WIN32
  204. case BI_DECXIMAGEDIB:
  205. imagesize = width*height*(bits==24 ? 4 : 1);
  206. break;
  207. #endif /* !WIN32 */
  208. case BI_RGB:
  209. case BI_BITFIELDS:
  210. imagesize = width*height*(bits/8);
  211. break;
  212. default:
  213. imagesize = width*height;
  214. }
  215. return(imagesize);
  216. }
  217. static dword slibFOURCCtoVideoType(dword *fourcc)
  218. {
  219. switch (*fourcc)
  220. {
  221. case BI_DECH261DIB:
  222. case BI_MSH261DIB:
  223. *fourcc=BI_DECH261DIB;
  224. return(SLIB_TYPE_H261);
  225. case BI_DECH263DIB:
  226. case BI_MSH263DIB:
  227. *fourcc=BI_DECH263DIB;
  228. return(SLIB_TYPE_H263);
  229. case JPEG_DIB:
  230. return(SLIB_TYPE_JPEG);
  231. case MJPG_DIB:
  232. return(SLIB_TYPE_MJPG);
  233. case BI_DECYUVDIB: /* YUV 4:2:2 Packed */
  234. case BI_YUY2: /* YUV 4:2:2 Packed */
  235. case BI_YU16SEP: /* YUV 4:2:2 Planar */
  236. case BI_YU12SEP: /* YUV 4:1:1 Planar */
  237. case BI_YVU9SEP: /* YUV 16:1:1 Planar */
  238. return(SLIB_TYPE_YUV);
  239. default:
  240. _SlibDebug(_WARN_, printf("Unsupported AVI format\n") );
  241. }
  242. return(0);
  243. }
  244. static void slibUpdateVideoFrames(SlibInfo_t *Info)
  245. {
  246. #ifdef MPEG_SUPPORT
  247. if (Info->VideoLengthKnown==FALSE && slibDataOnPin(Info, SLIB_DATA_VIDEO) &&
  248. Info->FileSize>0 && Info->FileSize<Info->OverflowSize)
  249. {
  250. if (SlibTypeIsMPEGVideo(Info->Type))
  251. {
  252. dword frames = slibCountCodesOnPin(Info,
  253. slibGetPin(Info, SLIB_DATA_VIDEO),
  254. MPEG_PICTURE_START, 4, 0);
  255. if (Info->FramesPerSec)
  256. Info->VideoLength=slibFrameToTime(Info, frames);
  257. Info->VideoLengthKnown=TRUE;
  258. }
  259. }
  260. #endif /* MPEG_SUPPORT */
  261. }
  262. void SlibUpdateVideoInfo(SlibInfo_t *Info)
  263. {
  264. int inbpp=24, outbpp=24, compformat=0, dcmpformat=0;
  265. SlibTime_t ptime;
  266. _SlibDebug(_DEBUG_, printf("SlibUpdateVideoInfo()\n") );
  267. if (SlibTypeIsAudioOnly(Info->Type)) /* no video? */
  268. return;
  269. if (Info->Mode == SLIB_MODE_COMPRESS)
  270. {
  271. switch (Info->Type)
  272. {
  273. #ifdef MPEG_SUPPORT
  274. case SLIB_TYPE_MPEG1_VIDEO:
  275. case SLIB_TYPE_MPEG2_VIDEO:
  276. case SLIB_TYPE_MPEG_SYSTEMS:
  277. case SLIB_TYPE_MPEG_SYSTEMS_MPEG2:
  278. compformat=BI_DECMPEGDIB;
  279. dcmpformat=BI_YU12SEP;
  280. Info->Width = SIF_WIDTH;
  281. Info->Height = SIF_HEIGHT;
  282. Info->FramesPerSec = 25.0F;
  283. Info->VideoBitRate = 1152000;
  284. break;
  285. #endif /* MPEG_SUPPORT */
  286. #ifdef H261_SUPPORT
  287. case SLIB_TYPE_H261:
  288. case SLIB_TYPE_RTP_H261:
  289. compformat=BI_DECH261DIB;
  290. dcmpformat=BI_YU12SEP;
  291. Info->Width = CIF_WIDTH;
  292. Info->Height = CIF_HEIGHT;
  293. Info->FramesPerSec = 15.0F;
  294. Info->VideoBitRate = 352000;
  295. break;
  296. #endif /* H261_SUPPORT */
  297. #ifdef H263_SUPPORT
  298. case SLIB_TYPE_H263:
  299. case SLIB_TYPE_RTP_H263:
  300. compformat=BI_DECH263DIB;
  301. dcmpformat=BI_YU12SEP;
  302. Info->Width = CIF_WIDTH;
  303. Info->Height = CIF_HEIGHT;
  304. Info->FramesPerSec = 30.0F;
  305. Info->VideoBitRate = 0;
  306. break;
  307. #endif /* H263_SUPPORT */
  308. #ifdef HUFF_SUPPORT
  309. case SLIB_TYPE_SHUFF:
  310. compformat=BI_DECHUFFDIB;
  311. dcmpformat=BI_YU12SEP;
  312. Info->Width = 320;
  313. Info->Height = 240;
  314. Info->FramesPerSec = 30.0F;
  315. Info->VideoBitRate = 0;
  316. break;
  317. #endif /* HUFF_SUPPORT */
  318. default:
  319. break;
  320. }
  321. }
  322. else if (Info->Mode == SLIB_MODE_DECOMPRESS)
  323. {
  324. unsigned char *buf;
  325. unsigned dword size;
  326. Info->VideoStreams=1;
  327. switch (Info->Type)
  328. {
  329. #ifdef MPEG_SUPPORT
  330. case SLIB_TYPE_MPEG1_VIDEO:
  331. case SLIB_TYPE_MPEG2_VIDEO:
  332. case SLIB_TYPE_MPEG_SYSTEMS:
  333. case SLIB_TYPE_MPEG_SYSTEMS_MPEG2:
  334. case SLIB_TYPE_MPEG_TRANSPORT:
  335. case SLIB_TYPE_MPEG_PROGRAM:
  336. _SlibDebug(_DEBUG_,
  337. printf("SlibUpdateVideoInfo() MPEG1 or MPEG2\n") );
  338. if (!slibLoadPin(Info, SLIB_DATA_VIDEO))
  339. {
  340. _SlibDebug(_DEBUG_,
  341. printf("SlibUpdateVideoInfo() No VIDEO data\n") );
  342. Info->Type=SLIB_TYPE_UNKNOWN;
  343. Info->VideoStreams=0;
  344. return;
  345. }
  346. buf = slibSearchBuffersOnPin(Info,
  347. slibGetPin(Info, SLIB_DATA_VIDEO),
  348. NULL, &size, MPEG_SEQ_HEAD,
  349. MPEG_SEQ_HEAD_LEN/8, FALSE);
  350. if (buf)
  351. {
  352. const float fps[16] = {
  353. 30.0F, 23.976F, 24.0F, 25.0F, 29.97F, 30.0F, 50.0F, 59.94F,
  354. 60.0F, 30.0F, 30.0F, 30.0F, 30.0F, 30.0F, 30.0F, 30.0F
  355. };
  356. /* ScDumpChar(buf, size, 0); */
  357. Info->Width = ((int)buf[0])*16+(int)(buf[1]>>4);
  358. Info->Height = ((int)buf[1]&0x0F)*256+(int)buf[2];
  359. /* must be 16x16 because of Render limitations, round up */
  360. Info->Width += (Info->Width%16) ? 16-(Info->Width%16) : 0;
  361. Info->Height += (Info->Height%16) ? 16-(Info->Height%16) : 0;
  362. Info->FramesPerSec = fps[buf[3]&0x0F];
  363. Info->VideoBitRate = (((dword)buf[4]&0xFF)<<10) +
  364. (((dword)buf[5])<<2) +
  365. (dword)(buf[6]>>6);
  366. Info->VideoBitRate *= 400;
  367. Info->VBVbufSize = ((int)buf[6]&0x1F)<<5 | (int)(buf[7]>>3);
  368. Info->VBVbufSize *= 2*1024;
  369. _SlibDebug(_DEBUG_, printf("VBVbufSize=%d\n", Info->VBVbufSize) );
  370. if (Info->VideoBitRate)
  371. {
  372. qword secs=(qword)(((qword)Info->FileSize*80L)
  373. /Info->VideoBitRate);
  374. Info->VideoLength = secs*100;
  375. _SlibDebug(_DEBUG_,
  376. printf("SlibUpdateVideoInfo() VideoLength = %ld Bitrate=%ld\n",
  377. Info->VideoLength, Info->VideoBitRate) );
  378. }
  379. }
  380. else /* invalid format */
  381. {
  382. _SlibDebug(_DEBUG_,
  383. printf("SlibUpdateVideoInfo() Didn't find MPEG sequence header\n") );
  384. Info->Type=SLIB_TYPE_UNKNOWN;
  385. Info->VideoStreams=0;
  386. return;
  387. }
  388. compformat=BI_DECMPEGDIB;
  389. dcmpformat=BI_YU12SEP;
  390. Info->VideoType=SLIB_TYPE_MPEG1_VIDEO;
  391. /* check to see if this is MPEG 2 */
  392. _SlibDebug(_DEBUG_,
  393. printf("Searching for MPEG 2 extensions...\n") );
  394. do {
  395. buf = slibSearchBuffersOnPin(Info,
  396. slibGetPin(Info, SLIB_DATA_VIDEO),
  397. buf, &size, MPEG_START_CODE,
  398. MPEG_START_CODE_LEN/8, FALSE);
  399. if (buf && buf[0]==MPEG_EXT_START_BASE)
  400. {
  401. _SlibDebug(_DEBUG_,
  402. printf("Found START CODE %X, ID=%d\n", buf[0], buf[1]>>4) );
  403. if ((buf[1]>>4)==MPEG_SEQ_ID) /* has to be MPEG 2 */
  404. {
  405. if (Info->Type==SLIB_TYPE_MPEG1_VIDEO)
  406. Info->Type=SLIB_TYPE_MPEG2_VIDEO;
  407. else if (Info->Type==SLIB_TYPE_MPEG_SYSTEMS)
  408. Info->Type=SLIB_TYPE_MPEG_SYSTEMS_MPEG2;
  409. Info->VideoType=SLIB_TYPE_MPEG2_VIDEO;
  410. switch ((buf[2]>>1)&0x03)
  411. {
  412. default:
  413. case 1: /* 4:1:1 */ dcmpformat=BI_YU12SEP;
  414. _SlibDebug(_DEBUG_, printf("4:1:1\n") );
  415. break;
  416. case 2: /* 4:2:2 */ dcmpformat=BI_YU16SEP;
  417. _SlibDebug(_DEBUG_, printf("4:2:2\n") );
  418. break;
  419. case 3: /* 4:4:4 */ dcmpformat=BI_YU16SEP;
  420. _SlibDebug(_DEBUG_, printf("4:4:4\n") );
  421. break;
  422. }
  423. break;
  424. }
  425. }
  426. else
  427. break;
  428. } while (1);
  429. _SlibDebug(_DEBUG_,
  430. printf("Done searching for MPEG 2 extensions.\n") );
  431. Info->KeySpacing=12;
  432. Info->SubKeySpacing=3;
  433. break;
  434. #endif /* MPEG_SUPPORT */
  435. #ifdef H261_SUPPORT
  436. case SLIB_TYPE_H261:
  437. case SLIB_TYPE_RTP_H261:
  438. slibLoadPin(Info, SLIB_DATA_VIDEO);
  439. buf = slibSearchBuffersOnPin(Info,
  440. slibGetPin(Info, SLIB_DATA_VIDEO),
  441. NULL, &size, H261_START_CODE,
  442. H261_START_CODE_LEN/8, FALSE);
  443. if (buf)
  444. {
  445. if ((buf[0]&0xF0)==0) /* picture start code */
  446. {
  447. if (buf[1]&0x08)
  448. {
  449. Info->Width = 352;
  450. Info->Height = 288;
  451. }
  452. else
  453. {
  454. Info->Width = 176;
  455. Info->Height = 144;
  456. }
  457. }
  458. Info->FramesPerSec = 15.0F;
  459. }
  460. compformat=BI_DECH261DIB;
  461. dcmpformat=BI_YU12SEP;
  462. Info->VideoType=SLIB_TYPE_H261;
  463. break;
  464. #endif /* H261_SUPPORT */
  465. #ifdef H263_SUPPORT
  466. case SLIB_TYPE_H263:
  467. case SLIB_TYPE_RTP_H263:
  468. slibLoadPin(Info, SLIB_DATA_VIDEO);
  469. buf = slibSearchBuffersOnPin(Info,
  470. slibGetPin(Info, SLIB_DATA_VIDEO),
  471. NULL, &size, 0x000080,
  472. 3, FALSE);
  473. if (buf)
  474. {
  475. switch ((buf[1]>>2)&0x07)
  476. {
  477. case H263_SF_SQCIF:
  478. Info->Width = SQCIF_WIDTH;
  479. Info->Height = SQCIF_HEIGHT;
  480. break;
  481. case H263_SF_QCIF:
  482. Info->Width = QCIF_WIDTH;
  483. Info->Height = QCIF_HEIGHT;
  484. break;
  485. case H263_SF_4CIF:
  486. Info->Width = CIF4_WIDTH;
  487. Info->Height = CIF4_HEIGHT;
  488. break;
  489. case H263_SF_16CIF:
  490. Info->Width = CIF16_WIDTH;
  491. Info->Height = CIF16_HEIGHT;
  492. break;
  493. case H263_SF_CIF:
  494. default:
  495. Info->Width = CIF_WIDTH;
  496. Info->Height = CIF_HEIGHT;
  497. }
  498. Info->FramesPerSec = 30.0F;
  499. }
  500. compformat=BI_DECH263DIB;
  501. dcmpformat=BI_YU12SEP;
  502. Info->VideoType=SLIB_TYPE_H263;
  503. break;
  504. #endif /* H263_SUPPORT */
  505. #ifdef HUFF_SUPPORT
  506. case SLIB_TYPE_SHUFF:
  507. slibLoadPin(Info, SLIB_DATA_VIDEO);
  508. buf=slibPeekBufferOnPin(Info,
  509. slibGetPin(Info,SLIB_DATA_VIDEO), &size, NULL);
  510. if (buf)
  511. {
  512. _SlibDebug(_DEBUG_,
  513. printf("%2X %2X %2X %2X %2X %2X\n", buf[0], buf[1],
  514. buf[2], buf[3], buf[4], buf[5]) );
  515. Info->Width = ((int)buf[3]*256)+(int)buf[4];
  516. Info->Height = ((int)buf[5]*256)+(int)buf[6];
  517. }
  518. Info->FramesPerSec = 30.0F;
  519. Info->VideoBitRate = 0;
  520. compformat=BI_DECHUFFDIB;
  521. dcmpformat=BI_YU12SEP;
  522. Info->KeySpacing=1;
  523. Info->SubKeySpacing=1;
  524. Info->VideoType=SLIB_TYPE_SHUFF;
  525. break;
  526. #endif /* HUFF_SUPPORT */
  527. case SLIB_TYPE_RASTER:
  528. buf=slibPeekBufferOnPin(Info,
  529. slibGetPin(Info,SLIB_DATA_COMPRESSED), &size, NULL);
  530. Info->FramesPerSec = 30.0F;
  531. if (buf)
  532. {
  533. _SlibDebug(_DEBUG_,
  534. printf("%2X %2X %2X %2X %2X %2X\n", buf[0], buf[1],
  535. buf[2], buf[3], buf[4], buf[5]) );
  536. Info->Width = ((int)buf[4]<<24)+((int)buf[5]<<16)+((int)buf[6]<<8)+(int)buf[7];
  537. Info->Height = ((int)buf[8]<<24)+((int)buf[9]<<16)+((int)buf[10]<<8)+(int)buf[11];
  538. Info->VideoLength = slibFrameToTime(Info,
  539. (signed qword)Info->FileSize/(Info->Width*Info->Height*3));
  540. }
  541. else
  542. Info->VideoLength = slibFrameToTime(Info, 1);
  543. Info->VideoBitRate = 0;
  544. compformat=BI_RGB;
  545. dcmpformat=BI_YU12SEP;
  546. Info->KeySpacing=1;
  547. Info->SubKeySpacing=1;
  548. Info->VideoType=SLIB_TYPE_RASTER;
  549. break;
  550. case SLIB_TYPE_BMP:
  551. buf=slibPeekBufferOnPin(Info,
  552. slibGetPin(Info,SLIB_DATA_COMPRESSED), &size, NULL);
  553. Info->FramesPerSec = 30.0F;
  554. if (buf)
  555. {
  556. _SlibDebug(_DEBUG_,
  557. printf("%2X %2X %2X %2X %2X %2X\n", buf[0], buf[1],
  558. buf[2], buf[3], buf[4], buf[5]) );
  559. Info->Width = ((dword)buf[15]<<24)+((dword)buf[16]<<16)+((dword)buf[17]<<8)+(dword)buf[18];
  560. Info->Height = ((dword)buf[19]<<24)+((dword)buf[20]<<16)+((dword)buf[21]<<8)+(dword)buf[22];
  561. compformat=((dword)buf[30]<<24)+((dword)buf[25]<<16)+((dword)buf[26]<<8)+(dword)buf[27];
  562. inbpp=(dword)buf[28];
  563. Info->VideoLength = slibFrameToTime(Info,
  564. (signed qword)Info->FileSize/(Info->Width*Info->Height*3));
  565. }
  566. else
  567. Info->VideoLength = slibFrameToTime(Info, 1);
  568. Info->VideoBitRate = 0;
  569. Info->VideoType = slibFOURCCtoVideoType(&compformat);
  570. dcmpformat=BI_YU16SEP;
  571. outbpp=slibCalcBits(dcmpformat, outbpp);
  572. Info->KeySpacing=1;
  573. Info->SubKeySpacing=1;
  574. break;
  575. case SLIB_TYPE_RIFF:
  576. case SLIB_TYPE_AVI:
  577. slibLoadPin(Info, SLIB_DATA_COMPRESSED);
  578. buf = slibSearchBuffersOnPin(Info,
  579. slibGetPin(Info,SLIB_DATA_COMPRESSED),
  580. NULL, &size, AVI_MAINHDR, 4, FALSE);
  581. if (buf)
  582. {
  583. AVI_MainHeader hdr;
  584. /* printf("%d %d %d %d\n", buf[4], buf[5], buf[6], buf[7]); */
  585. memcpy(&hdr, buf+4, sizeof(AVI_MainHeader));
  586. Info->Width = (short)hdr.dwWidth;
  587. Info->Height = (short)hdr.dwHeight;
  588. Info->FramesPerSec = 1000000.0F/hdr.dwMicroSecPerFrame;
  589. if (Info->FramesPerSec==0.0F)
  590. Info->FramesPerSec = 30.0F;
  591. Info->VideoLength = slibFrameToTime(Info, hdr.dwTotalFrames);
  592. Info->VideoLengthKnown = TRUE;
  593. }
  594. buf = slibSearchBuffersOnPin(Info,
  595. slibGetPin(Info,SLIB_DATA_COMPRESSED),
  596. NULL, &size, AVI_STREAMFORMAT, 4, FALSE);
  597. if (buf)
  598. {
  599. AVI_StreamHeader hdr;
  600. /* printf("%c %c %c %c\n", buf[4], buf[5], buf[6], buf[7]); */
  601. memcpy(&hdr, buf+20, sizeof(AVI_StreamHeader));
  602. compformat=hdr.fccType;
  603. Info->VideoType = slibFOURCCtoVideoType(&compformat);
  604. switch (Info->VideoType)
  605. {
  606. case SLIB_TYPE_JPEG:
  607. Info->Type = SLIB_TYPE_JPEG_AVI;
  608. dcmpformat=BI_YU16SEP;
  609. break;
  610. case SLIB_TYPE_MJPG:
  611. Info->Type = SLIB_TYPE_MJPG_AVI;
  612. dcmpformat=BI_YU16SEP;
  613. break;
  614. case SLIB_TYPE_YUV:
  615. Info->Type = SLIB_TYPE_YUV_AVI;
  616. dcmpformat=compformat;
  617. if (IsYUV422Packed(dcmpformat) || IsYUV422Sep(dcmpformat))
  618. dcmpformat=BI_YU16SEP;
  619. break;
  620. default:
  621. _SlibDebug(_WARN_, printf("Unsupported AVI format\n") );
  622. return;
  623. }
  624. inbpp=slibCalcBits(compformat, inbpp);
  625. outbpp=slibCalcBits(dcmpformat, outbpp);
  626. Info->KeySpacing=1;
  627. Info->SubKeySpacing=1;
  628. }
  629. break;
  630. #ifdef JPEG_SUPPORT
  631. case SLIB_TYPE_JPEG_QUICKTIME:
  632. case SLIB_TYPE_JFIF:
  633. /* not supported - need to know how to parse */
  634. slibLoadPin(Info, SLIB_DATA_VIDEO);
  635. buf = slibSearchBuffersOnPin(Info,
  636. slibGetPin(Info, SLIB_DATA_VIDEO),
  637. NULL, &size, (JPEG_MARKER<<8)|JPEG_SOF0,
  638. 2, FALSE);
  639. if (buf)
  640. {
  641. Info->Width = ((int)buf[5]<<8) + (int)buf[6];
  642. Info->Height = ((int)buf[3]<<8) + (int)buf[4];
  643. Info->FramesPerSec = 30.0F;
  644. }
  645. compformat=MJPG_DIB;
  646. dcmpformat=BI_YU16SEP;
  647. Info->VideoType = SLIB_TYPE_JPEG;
  648. break;
  649. #endif /* JPEG_SUPPORT */
  650. }
  651. slibUpdateVideoFrames(Info);
  652. }
  653. if (SlibTypeHasTimeStamps(Info->Type))
  654. {
  655. ptime=slibGetNextTimeOnPin(Info, slibGetPin(Info, SLIB_DATA_VIDEO), 100*1024);
  656. if (SlibTimeIsValid(ptime))
  657. Info->VideoPTimeBase=ptime;
  658. }
  659. if (Info->CompVideoFormat==NULL)
  660. Info->CompVideoFormat=(BITMAPINFOHEADER *)ScAlloc(sizeof(BITMAPINFOHEADER));
  661. _SlibDebug(_VERBOSE_,
  662. printf("Width=%d Height=%d Stride=%d\n",
  663. Info->Width, Info->Height, Info->Stride) );
  664. if (Info->CompVideoFormat!=NULL)
  665. {
  666. Info->CompVideoFormat->biSize = sizeof(BITMAPINFOHEADER);
  667. Info->CompVideoFormat->biWidth = Info->Width;
  668. Info->CompVideoFormat->biHeight = Info->Height;
  669. Info->CompVideoFormat->biPlanes = 1;
  670. Info->CompVideoFormat->biBitCount = (WORD)inbpp;
  671. Info->CompVideoFormat->biCompression = compformat;
  672. Info->CompVideoFormat->biSizeImage = 0;
  673. Info->CompVideoFormat->biXPelsPerMeter = 0;
  674. Info->CompVideoFormat->biYPelsPerMeter = 0;
  675. Info->CompVideoFormat->biClrUsed = 0;
  676. Info->CompVideoFormat->biClrImportant = 0;
  677. }
  678. if (Info->VideoFormat==NULL)
  679. Info->VideoFormat=(BITMAPINFOHEADER *)ScAlloc(sizeof(BITMAPINFOHEADER));
  680. if (Info->VideoFormat!=NULL)
  681. {
  682. Info->VideoFormat->biSize = sizeof(BITMAPINFOHEADER);
  683. Info->VideoFormat->biWidth = Info->Width;
  684. Info->VideoFormat->biHeight = Info->Height;
  685. Info->VideoFormat->biPlanes = 1;
  686. Info->VideoFormat->biBitCount = (WORD)outbpp;
  687. Info->VideoFormat->biCompression = dcmpformat;
  688. Info->VideoFormat->biSizeImage = 0;
  689. Info->VideoFormat->biXPelsPerMeter = 0;
  690. Info->VideoFormat->biYPelsPerMeter = 0;
  691. Info->VideoFormat->biClrUsed = 0;
  692. Info->VideoFormat->biClrImportant = 0;
  693. }
  694. if (Info->CodecVideoFormat==NULL)
  695. {
  696. Info->CodecVideoFormat=(BITMAPINFOHEADER *)ScAlloc(sizeof(BITMAPINFOHEADER));
  697. if (Info->CodecVideoFormat!=NULL)
  698. memcpy(Info->CodecVideoFormat, Info->VideoFormat,
  699. sizeof(BITMAPINFOHEADER));
  700. }
  701. slibValidateVideoParams(Info);
  702. }
  703. SlibStatus_t slibValidateVideoParams(SlibInfo_t *Info)
  704. {
  705. dword oldimagesize, codecwidth, codecheight;
  706. SlibStatus_t status=SlibErrorNone;
  707. if (Info->CodecVideoFormat)
  708. {
  709. codecwidth=Info->CodecVideoFormat->biWidth;
  710. codecheight=Info->CodecVideoFormat->biHeight;
  711. }
  712. else
  713. {
  714. codecwidth=Info->Width;
  715. codecheight=Info->Height;
  716. }
  717. if (Info->Mode==SLIB_MODE_COMPRESS)
  718. {
  719. switch (Info->Type)
  720. {
  721. case SLIB_TYPE_H261:
  722. if (Info->Width!=CIF_WIDTH && Info->Width!=QCIF_WIDTH)
  723. status=SlibErrorImageSize;
  724. if (Info->Height!=CIF_HEIGHT && Info->Height!=QCIF_HEIGHT)
  725. status=SlibErrorImageSize;
  726. if (status!=SlibErrorNone) /* set to closest size */
  727. {
  728. if (Info->Width<=300)
  729. {
  730. codecwidth=QCIF_WIDTH;
  731. codecheight=QCIF_HEIGHT;
  732. }
  733. else
  734. {
  735. codecwidth=CIF_WIDTH;
  736. codecheight=CIF_HEIGHT;
  737. }
  738. }
  739. break;
  740. case SLIB_TYPE_H263:
  741. if (Info->Width!=CIF_WIDTH && Info->Width!=SQCIF_WIDTH && Info->Width!=QCIF_WIDTH &&
  742. Info->Width!=CIF4_WIDTH && Info->Width!=CIF16_WIDTH)
  743. status=SlibErrorImageSize;
  744. if (Info->Height!=CIF_HEIGHT && Info->Height!=SQCIF_HEIGHT && Info->Height!=QCIF_HEIGHT &&
  745. Info->Height!=CIF4_HEIGHT && Info->Height!=CIF16_HEIGHT)
  746. status=SlibErrorImageSize;
  747. if (status!=SlibErrorNone) /* set to closest size */
  748. {
  749. if (Info->Width<=168)
  750. {
  751. codecwidth=SQCIF_WIDTH;
  752. codecheight=SQCIF_HEIGHT;
  753. }
  754. else if (Info->Width<=300)
  755. {
  756. codecwidth=QCIF_WIDTH;
  757. codecheight=QCIF_HEIGHT;
  758. }
  759. else if (Info->Width<=(CIF4_WIDTH+CIF_WIDTH)/2)
  760. {
  761. codecwidth=CIF_WIDTH;
  762. codecheight=CIF_HEIGHT;
  763. }
  764. else if (Info->Width<=(CIF16_WIDTH+CIF4_WIDTH)/2)
  765. {
  766. codecwidth=CIF4_WIDTH;
  767. codecheight=CIF4_HEIGHT;
  768. }
  769. else
  770. {
  771. codecwidth=CIF16_WIDTH;
  772. codecheight=CIF16_HEIGHT;
  773. }
  774. }
  775. break;
  776. }
  777. /* height and width must be mults of 8 */
  778. if (codecwidth%8 || codecheight%8)
  779. return(SlibErrorImageSize);
  780. if (status==SlibErrorImageSize)
  781. {
  782. if (Info->CodecVideoFormat)
  783. {
  784. Info->CodecVideoFormat->biWidth=codecwidth;
  785. Info->CodecVideoFormat->biHeight=codecheight;
  786. }
  787. if (Info->CompVideoFormat)
  788. {
  789. Info->CompVideoFormat->biWidth=codecwidth;
  790. Info->CompVideoFormat->biHeight=codecheight;
  791. }
  792. }
  793. }
  794. if (Info->VideoFormat)
  795. {
  796. oldimagesize=Info->ImageSize;
  797. Info->ImageSize=slibCalcImageSize(Info->VideoFormat->biCompression,
  798. Info->VideoFormat->biBitCount,
  799. Info->VideoFormat->biWidth,
  800. Info->VideoFormat->biHeight);
  801. if (Info->ImageSize!=oldimagesize && Info->Imagebuf)
  802. {
  803. SlibFreeBuffer(Info->Imagebuf);
  804. Info->Imagebuf=NULL;
  805. }
  806. Info->VideoFormat->biBitCount=(WORD)slibCalcBits(
  807. Info->VideoFormat->biCompression,
  808. Info->VideoFormat->biBitCount);
  809. Info->VideoFormat->biSizeImage=Info->ImageSize;
  810. }
  811. if (Info->CodecVideoFormat)
  812. {
  813. oldimagesize=Info->CodecImageSize;
  814. Info->CodecImageSize=slibCalcImageSize(
  815. Info->CodecVideoFormat->biCompression,
  816. Info->CodecVideoFormat->biBitCount,
  817. Info->CodecVideoFormat->biWidth,
  818. Info->CodecVideoFormat->biHeight);
  819. if (Info->CodecImageSize!=oldimagesize && Info->CodecImagebuf)
  820. {
  821. SlibFreeBuffer(Info->CodecImagebuf);
  822. Info->CodecImagebuf=NULL;
  823. }
  824. Info->CodecVideoFormat->biBitCount=(WORD)slibCalcBits(
  825. Info->CodecVideoFormat->biCompression,
  826. Info->CodecVideoFormat->biBitCount);
  827. Info->CodecVideoFormat->biSizeImage=Info->CodecImageSize;
  828. }
  829. if (Info->VideoFormat && Info->CodecVideoFormat)
  830. {
  831. oldimagesize=Info->IntImageSize;
  832. Info->IntImageSize=slibCalcImageSize(Info->VideoFormat->biCompression,
  833. Info->VideoFormat->biBitCount,
  834. Info->CodecVideoFormat->biWidth,
  835. Info->CodecVideoFormat->biHeight);
  836. if (Info->IntImageSize!=oldimagesize && Info->IntImagebuf)
  837. {
  838. SlibFreeBuffer(Info->IntImagebuf);
  839. Info->IntImagebuf=NULL;
  840. }
  841. }
  842. /* close format converter since formats may have changed */
  843. if (Info->Sch)
  844. {
  845. SconClose(Info->Sch);
  846. Info->Sch=NULL;
  847. }
  848. if (Info->FramesPerSec)
  849. Info->VideoFrameDuration=slibFrameToTime100(Info, 1);
  850. return(status);
  851. }