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.

607 lines
18 KiB

  1. /* File: sv_h261_init.c */
  2. /*****************************************************************************
  3. ** Copyright (c) Digital Equipment Corporation, 1994, 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 <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "SC_err.h"
  25. #include "sv_intrn.h"
  26. #include "sv_h261.h"
  27. #include "proto.h"
  28. #include "sv_proto.h"
  29. #ifdef WIN32
  30. #include <mmsystem.h>
  31. #endif
  32. #ifdef _SLIBDEBUG_
  33. #define _DEBUG_ 0 /* detailed debuging statements */
  34. #define _VERBOSE_ 0 /* show progress */
  35. #define _VERIFY_ 1 /* verify correct operation */
  36. #define _WARN_ 1 /* warnings about strange behavior */
  37. #endif
  38. /*
  39. ** Name: sv_InitH261
  40. ** Purpose: Initalize fields for compression or decompression.
  41. **
  42. ** Note: XXX - This version is hardcode to handle 4:2:2 subsampling
  43. ** eg. the calculation of numMCU
  44. */
  45. SvStatus_t svH261Init(SvCodecInfo_t *Info)
  46. {
  47. SvH261Info_t *H261;
  48. if (!Info || !Info->h261)
  49. return (SvErrorMemory);
  50. H261=Info->h261;
  51. if (H261->inited)
  52. return(SvErrorNone);
  53. H261->YWidth = Info->Width;
  54. H261->YHeight = Info->Height;
  55. H261->PICSIZE = Info->Width*Info->Height;
  56. H261->PICSIZEBY4 = Info->h261->PICSIZE>>2;
  57. /* common - compression & decompression */
  58. H261->C_U_Frames=0;
  59. H261->CodedFrames=0;
  60. H261->TemporalReference=1;
  61. H261->TemporalOffset = 0;
  62. H261->PType=0x0;
  63. H261->Type2=0x0;
  64. H261->MType=0x0;
  65. H261->GQuant=8;
  66. H261->MQuant=8;
  67. H261->ParityEnable=0;
  68. H261->PSpareEnable=0;
  69. H261->GSpareEnable=0;
  70. H261->Parity=0;
  71. H261->PSpare=0;
  72. H261->GSpare=0;
  73. H261->NumberMDU=0;
  74. H261->CurrentMDU=0;
  75. H261->NumberGOB=0;
  76. H261->CurrentGOB=0;
  77. H261->CurrentFrame=0;
  78. H261->StartFrame = 0;
  79. H261->LastFrame=0;
  80. H261->PreviousFrame = 0;
  81. H261->NumberFrames=0;
  82. H261->TransmittedFrames=0;
  83. H261->InitialQuant=0;
  84. /* compression */
  85. H261->MVDH=0;
  86. H261->MVDV=0;
  87. H261->CBP=0x3f;
  88. H261->CBPThreshold = 1;
  89. H261->VAR = 0;
  90. H261->VAROR = 0;
  91. H261->MWOR=0;
  92. H261->LastMVDV=0;
  93. H261->LastMVDH=0;
  94. H261->GRead=0;
  95. H261->MBA=0;
  96. H261->LastMBA=0;
  97. H261->LastMType=0;
  98. H261->FrameSkip=1;
  99. /* these are initialized by sv_api.c */
  100. /* H261->ME_search = 5;
  101. H261->ME_method = ME_FASTEST;
  102. H261->ME_threshold = 600;
  103. H261->frame_rate=(float)30.0F;
  104. H261->bit_rate=0;
  105. */
  106. H261->FileSizeBits=0;
  107. H261->BufferOffset=0;
  108. H261->QDFact=1;
  109. H261->QUpdateFrequency=11;
  110. H261->QUse=0;
  111. H261->QSum=0;
  112. H261->CBPThreshold=1;
  113. H261->ForceCIF=0;
  114. H261->NumberNZ=0;
  115. H261->FirstFrameBits=0;
  116. H261->NumberOvfl=0;
  117. H261->YCoefBits=0;
  118. H261->UCoefBits=0;
  119. H261->VCoefBits=0;
  120. H261->ForceCIF = 1;
  121. /* decompression */
  122. H261->NumberNZ = 0;
  123. H261->TemporalReference=1;
  124. H261->TemporalOffset=0;
  125. H261->PType=0x0;
  126. H261->Type2=0x0;
  127. H261->MType=0x0;
  128. H261->GQuant=8;
  129. H261->MQuant=8;
  130. H261->MVDH=0;
  131. H261->MVDV=0;
  132. H261->VAR=0;
  133. H261->VAROR=0;
  134. H261->MWOR=0;
  135. H261->LastMVDV=0;
  136. H261->LastMVDH=0;
  137. H261->CBP=0x3f;
  138. H261->GRead=0;
  139. H261->MBA=0;
  140. H261->LastMBA=0;
  141. H261->ImageType=IT_NTSC;
  142. H261->LastMType=0;
  143. H261->CBPThreshold=1; /* abs threshold before we use CBP */
  144. /* H261->ErrorValue=0;*/
  145. /* H261->Trace=NULL;*/
  146. H261->ForceCIF=0;
  147. H261->UseQuant = 8;
  148. if ((Info->h261->workloc=(unsigned int *)ScAlloc(512*sizeof(int)))
  149. ==NULL)
  150. return(SvErrorMemory);
  151. sv_H261HuffInit(Info->h261); /* Put Huffman tables on */
  152. H261->makekey = 0; /* disable key-frame trigger */
  153. H261->inited=TRUE;
  154. return (NoErrors);
  155. }
  156. /*
  157. ** Name: sv_InitH261Encoder
  158. ** Purpose: Prepare the encoder by loading default values
  159. **
  160. */
  161. SvStatus_t svH261CompressInit(SvCodecInfo_t *Info)
  162. {
  163. SvH261Info_t *H261;
  164. SvStatus_t status;
  165. int i;
  166. if (!Info || !Info->h261)
  167. return (SvErrorMemory);
  168. H261=Info->h261;
  169. if (H261->inited)
  170. return(SvErrorNone);
  171. /* init common stuff */
  172. status=svH261Init(Info);
  173. if (status!=SvErrorNone)
  174. return(status);
  175. /*
  176. * Initialize size-related paramters
  177. */
  178. H261->YWidth = Info->Width;
  179. H261->YHeight = Info->Height;
  180. if (H261->YWidth == CIF_WIDTH && H261->YHeight == CIF_HEIGHT)
  181. {
  182. H261->ImageType = IT_CIF;
  183. H261->NumberGOB = 12; /* Parameters for CIF design */
  184. H261->NumberMDU = 33;
  185. H261->PType=0x04;
  186. }
  187. else if (H261->YWidth == QCIF_WIDTH && H261->YHeight == QCIF_HEIGHT)
  188. {
  189. H261->ImageType = IT_QCIF;
  190. H261->NumberGOB = 3; /* Parameters for QCIF design */
  191. H261->NumberMDU = 33;
  192. H261->PType=0x00;
  193. }
  194. else if (H261->YWidth == NTSC_WIDTH && H261->YHeight == NTSC_HEIGHT)
  195. {
  196. H261->ImageType = IT_NTSC;
  197. H261->NumberGOB = 10; /* Parameters for NTSC design */
  198. H261->NumberMDU = 33;
  199. H261->PType=0x04;
  200. H261->PSpareEnable=1;
  201. H261->PSpare=0x8c;
  202. }
  203. else
  204. {
  205. H261->ImageType = 0;
  206. return (SvErrorUnrecognizedFormat);
  207. }
  208. H261->CWidth = H261->YWidth/2;
  209. H261->CHeight = H261->YHeight/2;
  210. H261->YW4 = H261->YWidth/4;
  211. H261->CW4 = H261->CWidth/4;
  212. H261->PICSIZE = H261->YWidth*H261->YHeight;
  213. H261->PICSIZEBY4 = H261->PICSIZE>>2;
  214. /*
  215. * Allocate memory
  216. */
  217. if ((H261->LastIntra = (unsigned char **)
  218. ScAlloc(H261->NumberGOB*sizeof(unsigned char *)))==NULL)
  219. return(SvErrorMemory);
  220. for(i=0; i<H261->NumberGOB; i++)
  221. {
  222. H261->LastIntra[i] = (unsigned char *)ScAlloc(H261->NumberMDU);
  223. if (H261->LastIntra[i]==NULL)
  224. return(SvErrorMemory);
  225. memset(H261->LastIntra[i],0,H261->NumberMDU);
  226. }
  227. if ((H261->YREF=(unsigned char *)ScAlloc(H261->PICSIZE))==NULL)
  228. return(SvErrorMemory);
  229. if ((H261->YRECON=(unsigned char *)ScAlloc(H261->PICSIZE))==NULL)
  230. return(SvErrorMemory);
  231. if ((H261->UREF=(unsigned char *)ScAlloc(H261->PICSIZEBY4))==NULL)
  232. return(SvErrorMemory);
  233. if ((H261->URECON=(unsigned char *)ScAlloc(H261->PICSIZEBY4))==NULL)
  234. return(SvErrorMemory);
  235. if ((H261->VREF=(unsigned char *)ScAlloc(H261->PICSIZEBY4))==NULL)
  236. return(SvErrorMemory);
  237. if ((H261->VRECON=(unsigned char *)ScAlloc(H261->PICSIZEBY4))==NULL)
  238. return (SvErrorMemory);
  239. if ((H261->Y=(unsigned char *)ScAlloc(H261->PICSIZE))==NULL)
  240. return(SvErrorMemory);
  241. if ((H261->U=(unsigned char *)ScAlloc(H261->PICSIZEBY4))==NULL)
  242. return(SvErrorMemory);
  243. if ((H261->V=(unsigned char *)ScAlloc(H261->PICSIZEBY4))==NULL)
  244. return(SvErrorMemory);
  245. if ((H261->YDEC=(unsigned char *)ScAlloc(H261->PICSIZE))==NULL)
  246. return(SvErrorMemory);
  247. if ((H261->UDEC=(unsigned char *)ScAlloc(H261->PICSIZEBY4))==NULL)
  248. return(SvErrorMemory);
  249. if ((H261->VDEC=(unsigned char *)ScAlloc(H261->PICSIZEBY4))==NULL)
  250. return(SvErrorMemory);
  251. if (H261->extbitstream)
  252. {
  253. H261->RTPInfo = (SvH261RTPInfo_t *) ScAlloc(sizeof(SvH261RTPInfo_t));
  254. if (H261->RTPInfo==NULL)
  255. return(SvErrorMemory);
  256. memset(H261->RTPInfo, 0, sizeof(SvH261RTPInfo_t)) ;
  257. }
  258. H261->PBUFF = 3;
  259. H261->PBUFF_Factor = 3;
  260. H261->CodeLength = 100;
  261. H261->TotalByteOffset = 0;
  262. H261->TotalMB[0] = 0;
  263. H261->TotalMB[1] = 0;
  264. H261->SkipMB = 0;
  265. H261->MBBits[0] = 0;
  266. H261->MBBits[1] = 0;
  267. if (H261->frame_rate>0)
  268. H261->NBitsPerFrame = (int)((float)H261->bit_rate/H261->frame_rate);
  269. else
  270. H261->NBitsPerFrame = 0;
  271. H261->Buffer_All = H261->PBUFF*H261->NBitsPerFrame;
  272. H261->BitsLeft = H261->Buffer_All;
  273. H261->MAX_MQUANT = 31;
  274. H261->MIN_MQUANT = 4;
  275. H261->alpha1 = 4.5;
  276. H261->alpha2 = 2.5;
  277. H261->LowerQuant = 0;
  278. H261->LowerQuant_FIX = 0;
  279. H261->FineQuant = 0;
  280. H261->ZBDecide = 30.0;
  281. H261->MSmooth = 1;
  282. if(H261->frame_rate==30)
  283. H261->MIN_MQUANT += 2;
  284. H261->ActThr2 = ACTIVE_THRESH;
  285. H261->ActThr = ACTIVE_THRESH;
  286. H261->ActThr5 = H261->ActThr2;
  287. H261->ActThr6 = H261->ActThr;
  288. #if 0
  289. if (H261->bit_rate > 300000)
  290. #else
  291. if ((H261->bit_rate > 300000) || /* for VBR */
  292. (H261->bit_rate == 0 && H261->QP < 10))
  293. #endif
  294. {
  295. H261->ActThr4 = ACTIVE_THRESH*24/32;
  296. H261->ActThr2 = H261->ActThr4;
  297. H261->ActThr = H261->ActThr2;
  298. H261->LowerQuant_FIX=12;
  299. H261->FineQuant = 0;
  300. H261->ZBDecide = 30.0;
  301. H261->CBPThreshold = 2;
  302. H261->MSmooth = 2;
  303. H261->PBUFF=3;
  304. }
  305. #if 0
  306. if (H261->frame_rate < 20)
  307. #else
  308. if ((H261->frame_rate < 20) || /* for VBR */
  309. (H261->bit_rate == 0 && H261->QP > 30))
  310. #endif
  311. {
  312. H261->ActThr3 = ACTIVE_THRESH*23/40;
  313. H261->LowerQuant_FIX=12;
  314. }
  315. #if 0
  316. if (H261->bit_rate<128001)
  317. #else
  318. if ((H261->bit_rate<128001) || /* for VBR */
  319. (H261->bit_rate == 0 && H261->QP > 20))
  320. #endif
  321. {
  322. H261->ActThr2 = ACTIVE_THRESH;
  323. H261->ActThr = ACTIVE_THRESH;
  324. H261->ActThr5 = H261->ActThr2;
  325. H261->ActThr6 = H261->ActThr;
  326. H261->ZBDecide = 32.0;
  327. H261->PBUFF=3;
  328. H261->FineQuant = 0;
  329. H261->MSmooth = 3;
  330. H261->CBPThreshold = 1;
  331. }
  332. #if 0
  333. if(H261->bit_rate<70001)
  334. #else
  335. if ((H261->bit_rate<70001) || /* for VBR */
  336. (H261->bit_rate == 0 && H261->QP > 28))
  337. #endif
  338. {
  339. H261->ActThr2 = ACTIVE_THRESH;
  340. H261->ActThr = ACTIVE_THRESH;
  341. H261->ActThr5 = H261->ActThr2;
  342. H261->ActThr6 = H261->ActThr;
  343. H261->ZBDecide = 36.0;
  344. H261->FineQuant = 0;
  345. H261->PBUFF=4;
  346. H261->PBUFF_Factor=4;
  347. H261->MSmooth = 4;
  348. H261->CBPThreshold = 1;
  349. }
  350. /* ndef WIN32 */
  351. if(H261->bit_rate) { /* CBR */
  352. if(H261->NBitsPerFrame < 31000)
  353. H261->GQuant = (31 - (int)H261->NBitsPerFrame/1000);
  354. else
  355. H261->GQuant = 1;
  356. }
  357. else
  358. H261->GQuant = H261->QPI; /* VBR */
  359. H261->InitialQuant = H261->GQuant;
  360. memset(H261->CBPFreq, 0, sizeof(H261->CBPFreq));
  361. GenScaleMat();
  362. if (H261->FileSizeBits) /* Rate is determined by bits/second. */
  363. H261->bit_rate=(int)(H261->FileSizeBits*H261->frame_rate)/
  364. (H261->FrameSkip*(H261->LastFrame-H261->CurrentFrame+1));
  365. if (H261->bit_rate)
  366. {
  367. H261->QDFact = (H261->bit_rate/320);
  368. H261->QOffs = 1;
  369. }
  370. H261->GQuant=H261->MQuant=H261->InitialQuant;
  371. H261->BufferOffset=0;
  372. H261->TotalBits=0;
  373. H261->NumberOvfl=0;
  374. H261->FirstFrameBits=0;
  375. H261->TransmittedFrames=0;
  376. H261->QDFact = 1;
  377. H261->QOffs= 1;
  378. H261->QUpdateFrequency = 11;
  379. return (NoErrors);
  380. }
  381. SvStatus_t svH261SetParamInt(SvHandle_t Svh, SvParameter_t param,
  382. qword value)
  383. {
  384. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  385. if (!Svh)
  386. return(SvErrorCodecHandle);
  387. if (Info->mode == SV_H261_DECODE)
  388. switch (param)
  389. {
  390. case SV_PARAM_QUALITY:
  391. if (value>99)
  392. Info->h261->quality=99;
  393. else if (value<0)
  394. Info->h261->quality=0;
  395. else
  396. Info->h261->quality=(int)value;
  397. break;
  398. case SV_PARAM_DEBUG:
  399. Info->h261->dbg=(void *)value;
  400. break;
  401. }
  402. else if (Info->mode == SV_H261_ENCODE)
  403. switch (param)
  404. {
  405. case SV_PARAM_MOTIONALG:
  406. Info->h261->ME_method=(int)value;
  407. return(SvErrorNone);
  408. case SV_PARAM_MOTIONSEARCH:
  409. Info->h261->ME_search=(int)value;
  410. break;
  411. case SV_PARAM_MOTIONTHRESH:
  412. Info->h261->ME_threshold=(int)value;
  413. break;
  414. case SV_PARAM_BITRATE:
  415. if (value>=0)
  416. Info->h261->bit_rate = (int)value;
  417. break;
  418. case SV_PARAM_QUALITY:
  419. if (value>99)
  420. Info->h261->quality=99;
  421. else if (value<0)
  422. Info->h261->quality=0;
  423. else
  424. Info->h261->quality=(int)value;
  425. break;
  426. case SV_PARAM_DEBUG:
  427. Info->h261->dbg=(void *)value;
  428. break;
  429. case SV_PARAM_FORMATEXT:
  430. Info->h261->extbitstream = (int)value;
  431. return(SvErrorNone);
  432. case SV_PARAM_PACKETSIZE:
  433. Info->h261->packetsize = (int)value * 8;
  434. break;
  435. case SV_PARAM_QUANTI: /* for VBR */
  436. Info->h261->QPI = (int)value;
  437. break;
  438. case SV_PARAM_QUANTP: /* for VBR */
  439. Info->h261->QP = (int)value;
  440. break;
  441. case SV_PARAM_KEYSPACING:
  442. break;
  443. case SV_PARAM_FRAMETYPE:
  444. if (value==FRAME_TYPE_I)
  445. Info->h261->makekey = 1; /* send key-frame */
  446. return(SvErrorNone);
  447. }
  448. return(SvErrorNone);
  449. }
  450. SvStatus_t svH261SetParamFloat(SvHandle_t Svh, SvParameter_t param,
  451. float value)
  452. {
  453. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  454. if (!Svh)
  455. return(SvErrorCodecHandle);
  456. if (Info->mode == SV_H261_ENCODE)
  457. switch (param)
  458. {
  459. case SV_PARAM_FPS:
  460. if (value<1.0)
  461. Info->h261->frame_rate = 1.0F;
  462. else if (value>30.0)
  463. Info->h261->frame_rate = 30.0F;
  464. else
  465. Info->h261->frame_rate = value;
  466. Info->h261->FrameRate_Fix=(int)(Info->h261->frame_rate+0.5);
  467. _SlibDebug(_DEBUG_,
  468. printf("frame_rate = %f\n", Info->h261->frame_rate) );
  469. return(SvErrorNone);
  470. }
  471. return(svH261SetParamInt(Svh, param, (long)value));
  472. }
  473. qword svH261GetParamInt(SvHandle_t Svh, SvParameter_t param)
  474. {
  475. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  476. if (!Svh)
  477. return((qword)0);
  478. if (Info->mode == SV_H261_ENCODE)
  479. switch (param)
  480. {
  481. case SV_PARAM_BITRATE:
  482. return((qword)Info->h261->bit_rate);
  483. case SV_PARAM_FPS:
  484. return((qword)svH261GetParamFloat(Svh, param));
  485. case SV_PARAM_QUALITY:
  486. return((qword)Info->h261->quality);
  487. case SV_PARAM_NATIVEFORMAT:
  488. return((qword)BI_YU12SEP);
  489. case SV_PARAM_FINALFORMAT:
  490. return((qword)BI_YU12SEP);
  491. case SV_PARAM_ALGFLAGS:
  492. {
  493. qword flags=0;
  494. /* flags|=Info->h261->extbitstream ? PARAM_ALGFLAG_EXTBS : 0; */
  495. return(flags);
  496. }
  497. break;
  498. case SV_PARAM_PACKETSIZE:
  499. return((qword)Info->h261->packetsize/8);
  500. case SV_PARAM_MOTIONALG:
  501. return((qword)Info->h261->ME_method);
  502. case SV_PARAM_MOTIONSEARCH:
  503. return((qword)Info->h261->ME_search);
  504. case SV_PARAM_MOTIONTHRESH:
  505. return((qword)Info->h261->ME_threshold);
  506. }
  507. else if (Info->mode == SV_H261_DECODE)
  508. switch (param)
  509. {
  510. case SV_PARAM_BITRATE:
  511. return((qword)Info->h261->bit_rate);
  512. case SV_PARAM_FPS:
  513. return((qword)svH261GetParamFloat(Svh, param));
  514. case SV_PARAM_WIDTH:
  515. return((qword)Info->h261->YWidth);
  516. case SV_PARAM_HEIGHT:
  517. return((qword)Info->h261->YHeight);
  518. case SV_PARAM_FRAME:
  519. return((qword)Info->h261->CurrentFrame);
  520. case SV_PARAM_NATIVEFORMAT:
  521. return((qword)BI_YU12SEP);
  522. case SV_PARAM_FINALFORMAT:
  523. return((qword)BI_YU12SEP);
  524. case SV_PARAM_QUALITY:
  525. return((qword)Info->h261->quality);
  526. }
  527. return((qword)0);
  528. }
  529. float svH261GetParamFloat(SvHandle_t Svh, SvParameter_t param)
  530. {
  531. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  532. if (!Svh)
  533. return((float)0.0);
  534. if (Info->mode == SV_H261_ENCODE)
  535. switch (param)
  536. {
  537. case SV_PARAM_FPS:
  538. return((float)Info->h261->frame_rate);
  539. }
  540. else if (Info->mode == SV_H261_DECODE)
  541. switch (param)
  542. {
  543. case SV_PARAM_FPS:
  544. return((float)Info->h261->frame_rate);
  545. }
  546. return((float)svH261GetParamInt(Svh, param));
  547. }
  548. SvStatus_t svH261SetParamBoolean(SvHandle_t Svh, SvParameter_t param,
  549. ScBoolean_t value)
  550. {
  551. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  552. if (!Svh)
  553. return(0);
  554. #if 0
  555. if (Info->mode == SV_H261_ENCODE)
  556. switch (param)
  557. {
  558. }
  559. #endif
  560. return(SvErrorNone);
  561. }
  562. ScBoolean_t svH261GetParamBoolean(SvHandle_t Svh, SvParameter_t param)
  563. {
  564. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  565. if (!Svh)
  566. return(0);
  567. if (Info->mode == SV_H261_ENCODE)
  568. switch (param)
  569. {
  570. case SV_PARAM_BITSTREAMING:
  571. return(TRUE);
  572. }
  573. else if (Info->mode == SV_H261_DECODE)
  574. switch (param)
  575. {
  576. case SV_PARAM_BITSTREAMING:
  577. return(TRUE);
  578. }
  579. return(FALSE);
  580. }