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.

3071 lines
104 KiB

  1. /* File: sv_h263_encode.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 <math.h>
  22. #include "sv_h263.h"
  23. #include "proto.h"
  24. #include "SC_err.h"
  25. #include "SC_conv.h"
  26. #ifndef USE_C
  27. #include "perr.h"
  28. #endif
  29. #ifdef WIN32
  30. #include <mmsystem.h>
  31. #endif
  32. #ifdef _SLIBDEBUG_
  33. #include "sc_debug.h"
  34. #define _SNR_ 1 /* calculate SNR */
  35. #define _DEBUG_ 0 /* detailed debuging statements */
  36. #define _VERBOSE_ 0 /* show progress */
  37. #define _VERIFY_ 0 /* verify correct operation */
  38. #define _WARN_ 0 /* warnings about strange behavior */
  39. #define _WRITE_ 0 /* write DEBUG.IMG */
  40. #include <stdio.h>
  41. int DEBUGIMG = -1;
  42. #endif /* _SLIBDEBUG_ */
  43. #define NTAPS 5
  44. static void SetDefPrefLevel(SvH263CompressInfo_t *H263Info);
  45. static void SetDefThresh(SvH263CompressInfo_t *H263Info);
  46. static void CheckPrefLevel(SvH263CompressInfo_t *H263Info, int depth) ;
  47. static short sv_H263MBDecode(SvH263CompressInfo_t *H263Info, short *qcoeff,
  48. H263_MB_Structure *mb_recon, int QP, int I, int CBP,
  49. unsigned dword quality);
  50. static int sv_H263MBEncode(H263_MB_Structure *mb_orig, int QP, int I, int *CBP,
  51. short *qcoeff, unsigned dword quality);
  52. static int NextTwoPB(SvH263CompressInfo_t *H263Info,
  53. H263_PictImage *next2, H263_PictImage *next1,
  54. H263_PictImage *prev,
  55. int bskip, int pskip, int seek_dist);
  56. static SvStatus_t sv_H263CodeOneOrTwo(SvCodecInfo_t *Info, int QP, int frameskip,
  57. H263_Bits *bits, H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2]);
  58. #ifdef _SNR_
  59. static void ComputeSNR(SvH263CompressInfo_t *H263Info,
  60. H263_PictImage *im1, H263_PictImage *im2,
  61. int lines, int pels);
  62. static void PrintResult(SvH263CompressInfo_t *H263Info, H263_Bits *bits, int num_units, int num);
  63. #endif
  64. static SvStatus_t sv_H263WriteExtBitstream(SvH263CompressInfo_t *H263Info,
  65. ScBitstream_t *bs);
  66. // #define GOB_RATE_CONTROL
  67. #ifdef GOB_RATE_CONTROL
  68. void sv_H263GOBInitRateCntrl();
  69. void sv_H263GOBUpdateRateCntrl(int bits);
  70. int sv_H263GOBInitQP(float bit_rate, float target_frame_rate, float QP_mean);
  71. int sv_H263GOBUpdateQP(int mb, float QP_mean, float bit_rate,int mb_width, int mb_height, int bitcount,
  72. int NOgob, int *VARgob, int pb_frame) ;
  73. #endif
  74. void sv_H263UpdateQuality(SvCodecInfo_t *Info)
  75. {
  76. if (Info->mode == SV_H263_ENCODE)
  77. {
  78. SvH263CompressInfo_t *H263Info=Info->h263comp;
  79. unsigned dword imagesize=Info->Width*Info->Height;
  80. unsigned dword bit_rate=H263Info->bit_rate;
  81. unsigned dword calc_quality;
  82. if (H263Info->quality==0) /* no quality setting */
  83. {
  84. calc_quality=H263_MAX_CALC_QUALITY;
  85. }
  86. else if (bit_rate==0 || imagesize==0) /* variable bitrate */
  87. {
  88. /* make the quant settings directly proportional to the quality */
  89. H263Info->QPI=(((100-H263Info->quality)*31)/100)+1;
  90. if (H263Info->QPI>31)
  91. H263Info->QPI=31;
  92. H263Info->QP_init=H263Info->QPI;
  93. calc_quality=H263_MAX_CALC_QUALITY;
  94. }
  95. else /* fixed bitrate */
  96. {
  97. /* Using calc_quality you get:
  98. bitrate framerate imagesize quality calc_quality QPI
  99. -------- ---------- ---------- ------- ------------ ---
  100. 57400 7 352x288 100% 82 9
  101. 57400 15 352x288 100% 38 22
  102. 13300 7 352x288 100% 19 26
  103. 13300 15 352x288 100% 8 28
  104. 13300 7 176x144 100% 79 10
  105. 13300 15 176x144 100% 36 22
  106. */
  107. calc_quality=(bit_rate*H263Info->quality)/(unsigned int)(H263Info->frame_rate*100);
  108. calc_quality/=imagesize/1024;
  109. if (calc_quality<H263_MIN_CALC_QUALITY)
  110. calc_quality=H263_MIN_CALC_QUALITY;
  111. else if (calc_quality>H263_MAX_CALC_QUALITY)
  112. calc_quality=H263_MAX_CALC_QUALITY;
  113. /* make the quant settings directly proportional to the calc_quality */
  114. if (calc_quality>200)
  115. H263Info->QPI=1;
  116. else
  117. {
  118. H263Info->QPI=(((200-calc_quality)*31)/200)+1;
  119. if (H263Info->QPI>31)
  120. H263Info->QPI=31;
  121. }
  122. H263Info->QP=H263Info->QP_init=H263Info->QPI;
  123. }
  124. H263Info->calc_quality=calc_quality;
  125. }
  126. }
  127. static SvStatus_t convert_to_411(SvCodecInfo_t *Info,
  128. u_char *dest_buff, u_char *ImagePtr)
  129. {
  130. SvH263CompressInfo_t *H263Info = Info->h263comp;
  131. unsigned long size = (Info->InputFormat.biWidth * Info->InputFormat.biHeight) ;
  132. if (IsYUV422Packed(Info->InputFormat.biCompression))
  133. {
  134. SvStatus_t status;
  135. /* Input is in NTSC format, convert */
  136. if ((Info->InputFormat.biWidth == NTSC_WIDTH) &&
  137. (Info->InputFormat.biHeight == NTSC_HEIGHT))
  138. status = ScConvertNTSC422toCIF411((unsigned char *)ImagePtr,
  139. (unsigned char *)(dest_buff),
  140. (unsigned char *)(dest_buff + size),
  141. (unsigned char *)(dest_buff + size +(size/4)),
  142. (int) Info->InputFormat.biWidth);
  143. else
  144. status = ScConvert422ToYUV_char_C(ImagePtr,
  145. (unsigned char *)(dest_buff), /* Y */
  146. (unsigned char *)(dest_buff+size), /* U */
  147. (unsigned char *)(dest_buff+size+(size/4)), /* V */
  148. Info->InputFormat.biWidth,Info->InputFormat.biHeight);
  149. return(status);
  150. }
  151. else if (IsYUV411Sep(Info->InputFormat.biCompression))
  152. {
  153. /*
  154. * If YUV 12 SEP, Not converting, so just copy data to the luminance
  155. * and chrominance appropriatelyi
  156. */
  157. memcpy(dest_buff, ImagePtr, (H263Info->pels*H263Info->lines*3)/2);
  158. }
  159. else if (IsYUV422Sep(Info->InputFormat.biCompression))
  160. {
  161. _SlibDebug(_DEBUG_, printf("ScConvert422PlanarTo411()\n") );
  162. ScConvert422PlanarTo411(ImagePtr,
  163. dest_buff, dest_buff+size, (dest_buff+size+(size/4)),
  164. Info->Width,Info->Height);
  165. }
  166. else
  167. {
  168. _SlibDebug(_WARN_, printf("Unsupported Video format\n") );
  169. return(SvErrorUnrecognizedFormat);
  170. }
  171. return(SvErrorNone);
  172. }
  173. /**********************************************************************
  174. *
  175. * Name: InitImage
  176. * Description: Allocates memory for structure of 4:2:0-image
  177. *
  178. * Input: image size
  179. * Returns: pointer to new structure
  180. * Side effects: memory allocated to structure
  181. *
  182. ***********************************************************************/
  183. H263_PictImage *sv_H263InitImage(int size)
  184. {
  185. H263_PictImage *new;
  186. unsigned char *image;
  187. if ((new = (H263_PictImage *)ScAlloc(sizeof(H263_PictImage))) == NULL) {
  188. svH263Error("Couldn't allocate (PictImage *)\n");
  189. return(NULL);
  190. }
  191. if ((image = (unsigned char *)ScPaMalloc((sizeof(char)*size*3)/2)) == NULL) {
  192. svH263Error("Couldn't allocate image\n");
  193. return(NULL);
  194. }
  195. new->lum = image;
  196. new->Cb = image+size;
  197. new->Cr = image+(size*5)/4;
  198. _SlibDebug(_DEBUG_, ScDebugPrintf(NULL,"sv_H263InitImage() %p\n", new) );
  199. return new;
  200. }
  201. /**********************************************************************
  202. *
  203. * Name: FreeImage
  204. * Description: Frees memory allocated to structure of 4:2:0-image
  205. *
  206. * Input: pointer to structure
  207. * Returns:
  208. * Side effects: memory of structure freed
  209. *
  210. ***********************************************************************/
  211. void sv_H263FreeImage(H263_PictImage *image)
  212. {
  213. _SlibDebug(_DEBUG_, ScDebugPrintf(NULL,"sv_H263FreeImage(%p)\n", image) );
  214. ScPaFree(image->lum);
  215. /* ScFree(image->Cr);
  216. ScPaFree(image->Cb); */
  217. ScFree(image);
  218. }
  219. /******************************************************************
  220. * Set the PREF_LEVEL matrix to the default values
  221. ******************************************************************/
  222. static void SetDefPrefLevel(SvH263CompressInfo_t *H263Info)
  223. {
  224. int i, j;
  225. unsigned char H263_DEF_PREF_LEVEL[4][3] = {{0, 0, 1},
  226. {0, 1, 1},
  227. {0, 1, 2},
  228. {0, 2, 2}};
  229. for(i=0; i<4; i++) {
  230. for(j=0; j<3; j++) {
  231. H263Info->PREF_LEVEL[i][j] = H263_DEF_PREF_LEVEL[i][j];
  232. }
  233. }
  234. }
  235. /*****************************************************************
  236. * Set the Threshold vectors to the default values
  237. *****************************************************************/
  238. static void SetDefThresh(SvH263CompressInfo_t *H263Info)
  239. {
  240. int i;
  241. unsigned char H263_DEF_MOTRESH[4]= {0, 2, 4, 7};
  242. int H263_DEF_PETRESH[3]= {2500, 3500, 6000};
  243. for(i=0; i<4; i++) {
  244. H263Info->MOTresh[i] = H263_DEF_MOTRESH[i];
  245. }
  246. for(i=0; i<3; i++) {
  247. H263Info->PETresh[i] = H263_DEF_PETRESH[i];
  248. }
  249. }
  250. /***********************************************************************
  251. * Cheks if all the selections in PREF_LEVEL are consistent with depth.
  252. ***********************************************************************/
  253. static void CheckPrefLevel(SvH263CompressInfo_t *H263Info, int depth)
  254. {
  255. int i, j;
  256. for(i=0; i<4; i++) {
  257. for(j=0; j<3; j++) {
  258. if (H263Info->PREF_LEVEL[i][j]>depth-1) H263Info->PREF_LEVEL[i][j] = depth-1;
  259. }
  260. }
  261. }
  262. static int svH263zeroflush(ScBitstream_t *BSOut)
  263. {
  264. int bits;
  265. bits = (int)(ScBSBitPosition(BSOut)%8);
  266. if(bits) {
  267. bits = 8-bits;
  268. ScBSPutBits(BSOut, 0, bits) ;
  269. }
  270. return bits;
  271. }
  272. /***************************************************/
  273. /***************************************************/
  274. static SvStatus_t sv_H263Compress(SvCodecInfo_t *Info);
  275. extern int arith_used;
  276. SvStatus_t svH263Compress(SvCodecInfo_t *Info, u_char *ImagePtr)
  277. {
  278. SvH263CompressInfo_t *H263Info = Info->h263comp;
  279. ScBitstream_t *BSOut=Info->BSOut;
  280. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg,"sv_H263Compress() bytepos=%ld\n",
  281. ScBSBytePosition(Info->BSOut)) );
  282. if (H263Info->frame_no == H263Info->start) /* Encode the first frame */
  283. {
  284. sv_H263UpdateQuality(Info); /* in case image size has changed */
  285. /* Intra image */
  286. /* svH263ReadImage(H263Info->curr_image, H263Info->start, H263Info->video_file); */
  287. convert_to_411(Info, H263Info->curr_image->lum, ImagePtr);
  288. H263Info->pic->picture_coding_type = H263_PCT_INTRA;
  289. H263Info->pic->QUANT = H263Info->QPI;
  290. if (H263Info->curr_recon==NULL)
  291. H263Info->curr_recon = sv_H263InitImage(H263Info->pels*H263Info->lines);
  292. sv_H263CodeOneIntra(Info, H263Info->curr_image, H263Info->curr_recon, H263Info->QPI,
  293. H263Info->bits, H263Info->pic);
  294. #ifdef _SNR_
  295. ComputeSNR(H263Info, H263Info->curr_image, H263Info->curr_recon,
  296. H263Info->lines, H263Info->pels);
  297. #endif
  298. if (arith_used)
  299. {
  300. H263Info->bits->header += sv_H263AREncoderFlush(H263Info, BSOut);
  301. arith_used = 0;
  302. }
  303. H263Info->bits->header += svH263zeroflush(BSOut); /* pictures shall be byte aligned */
  304. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "Frame %d = I frame\n", H263Info->frames) );
  305. sv_H263ZeroRes(H263Info->b_res);
  306. sv_H263AddBitsPicture(H263Info->bits);
  307. /* PrintResult(H263Info->bits, 1, 1); */
  308. memcpy(H263Info->intra_bits,H263Info->bits,sizeof(H263_Bits));
  309. sv_H263ZeroBits(H263Info->total_bits);
  310. sv_H263ZeroRes(H263Info->total_res);
  311. sv_H263ZeroRes(H263Info->res);
  312. H263Info->buffer_fullness = H263Info->intra_bits->total;
  313. /* number of seconds to encode */
  314. H263Info->seconds = (H263Info->end - H263Info->start + H263Info->chosen_frameskip)/H263Info->frame_rate;
  315. H263Info->first_frameskip = H263Info->chosen_frameskip;
  316. H263Info->distance_to_next_frame = H263Info->first_frameskip;
  317. _SlibDebug(_WARN_ && H263Info->first_frameskip>256,
  318. ScDebugPrintf(H263Info->dbg, "Warning: frameskip > 256\n") );
  319. H263Info->pic->picture_coding_type = H263_PCT_INTER;
  320. H263Info->pic->QUANT = H263Info->QP;
  321. H263Info->bdist = H263Info->chosen_frameskip;
  322. /* always encode the first frame after intra as P frame.
  323. This is not necessary, but something we chose to make
  324. the adaptive PB frames calculations a bit simpler */
  325. if (H263Info->pb_frames) {
  326. H263Info->pic->PB = 0;
  327. H263Info->pdist = 2*H263Info->chosen_frameskip - H263Info->bdist;
  328. }
  329. /* point to the 2nd frame */
  330. H263Info->frame_no = H263Info->start + H263Info->first_frameskip;
  331. H263Info->frames++;
  332. if (H263Info->extbitstream)
  333. {
  334. SvStatus_t status;
  335. status = sv_H263WriteExtBitstream(H263Info, BSOut);
  336. if (status!=SvErrorNone)
  337. return(status);
  338. }
  339. }
  340. else
  341. { /* the rest of frames */
  342. /***** Main loop *****/
  343. /* Set QP to pic->QUANT from previous encoded picture */
  344. H263Info->QP = H263Info->pic->QUANT;
  345. H263Info->next_frameskip = H263Info->distance_to_next_frame;
  346. if (!H263Info->pb_frames)
  347. {
  348. H263_PictImage *tmpimage;
  349. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "Frame %d = P frame\n", H263Info->frames) );
  350. if (H263Info->prev_image==NULL)
  351. H263Info->prev_image = sv_H263InitImage(H263Info->pels*H263Info->lines);
  352. /* swap current and prev images */
  353. tmpimage=H263Info->prev_image;
  354. H263Info->prev_image = H263Info->curr_image;
  355. H263Info->curr_image = tmpimage;
  356. /* swap recon images */
  357. if (H263Info->prev_recon==NULL)
  358. H263Info->prev_recon = sv_H263InitImage(H263Info->pels*H263Info->lines);
  359. if (H263Info->curr_recon==NULL)
  360. H263Info->curr_recon = sv_H263InitImage(H263Info->pels*H263Info->lines);
  361. tmpimage=H263Info->curr_recon;
  362. H263Info->curr_recon = H263Info->prev_recon;
  363. H263Info->prev_recon = tmpimage;
  364. convert_to_411(Info, H263Info->curr_image->lum, ImagePtr);
  365. H263Info->frames++;
  366. H263Info->next_frameskip = H263Info->pdist;
  367. return(sv_H263Compress(Info)); /* Encode P */
  368. }
  369. else if ((H263Info->frames%2)==1) /* this is a B frame */
  370. {
  371. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "Frame %d = B frame\n", H263Info->frames) );
  372. H263Info->PPFlag = 0;
  373. H263Info->bdist = H263Info->chosen_frameskip;
  374. H263Info->pdist = 2*H263Info->chosen_frameskip - H263Info->bdist;
  375. H263Info->pic->TRB = (int)(H263Info->bdist * H263Info->orig_frameskip);
  376. _SlibDebug(_WARN_ && H263Info->pic->TRB>8,
  377. ScDebugPrintf(H263Info->dbg, "distance too large for B-frame\n") );
  378. /* Read the frame to be coded as B */
  379. if (H263Info->B_image==NULL)
  380. H263Info->B_image = sv_H263InitImage(H263Info->pels*H263Info->lines);
  381. if (H263Info->B_recon==NULL)
  382. H263Info->B_recon = sv_H263InitImage(H263Info->pels*H263Info->lines);
  383. /* svH263ReadImage(H263Info->B_image,H263Info->frame_no - H263Info->pdist,H263Info->video_file); */
  384. convert_to_411(Info, H263Info->B_image->lum, ImagePtr);
  385. H263Info->first_loop_finished = 1;
  386. H263Info->pic->PB = 1;
  387. H263Info->frames++;
  388. /* need to reorder P+B frames - HWG */
  389. /* return now, we'll get the B frame on the next Compress call */
  390. return(SvErrorNone);
  391. }
  392. else /* this is a P frame of a PB or PP pair */
  393. {
  394. H263_PictImage *tmpimage;
  395. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "Frame %d = P frame\n", H263Info->frames) );
  396. if (H263Info->prev_image==NULL)
  397. H263Info->prev_image = sv_H263InitImage(H263Info->pels*H263Info->lines);
  398. /* swap current and prev images */
  399. tmpimage=H263Info->prev_image;
  400. H263Info->prev_image = H263Info->curr_image;
  401. H263Info->curr_image = tmpimage;
  402. /* swap recon images */
  403. if (H263Info->prev_recon==NULL)
  404. H263Info->prev_recon = sv_H263InitImage(H263Info->pels*H263Info->lines);
  405. if (H263Info->curr_recon==NULL)
  406. H263Info->curr_recon = sv_H263InitImage(H263Info->pels*H263Info->lines);
  407. tmpimage=H263Info->curr_recon;
  408. H263Info->curr_recon = H263Info->prev_recon;
  409. H263Info->prev_recon = tmpimage;
  410. /* svH263ReadImage(H263Info->curr_image, H263Info->frame_no, H263Info->video_file); */
  411. convert_to_411(Info, H263Info->curr_image->lum, ImagePtr);
  412. if (H263Info->pic->TRB > 8 || !NextTwoPB(H263Info, H263Info->curr_image,
  413. H263Info->B_image, H263Info->prev_image,
  414. H263Info->bdist, H263Info->pdist, H263Info->pic->seek_dist))
  415. {
  416. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "Encode PP\n") );
  417. H263Info->PPFlag = 1;
  418. /* curr_image and B_image were not suitable to be coded
  419. as a PB-frame - encoding as two P-frames instead */
  420. H263Info->pic->PB = 0;
  421. H263Info->next_frameskip = H263Info->bdist;
  422. /* swap B and current images - B_image gets encoded first as P frame */
  423. tmpimage = H263Info->curr_image;
  424. H263Info->curr_image = H263Info->B_image;
  425. H263Info->B_image = tmpimage;
  426. sv_H263Compress(Info); /* Encode first P */
  427. H263Info->next_frameskip = H263Info->pdist;
  428. /* swap current and prev images */
  429. tmpimage=H263Info->prev_image;
  430. H263Info->prev_image = H263Info->curr_image;
  431. H263Info->curr_image = tmpimage;
  432. /* swap current and B images */
  433. tmpimage=H263Info->B_image;
  434. H263Info->B_image = H263Info->curr_image;
  435. H263Info->curr_image = tmpimage;
  436. /* swap recon images */
  437. tmpimage=H263Info->curr_recon;
  438. H263Info->curr_recon = H263Info->prev_recon;
  439. H263Info->prev_recon = tmpimage;
  440. sv_H263Compress(Info); /* Encode second P */
  441. H263Info->frames++;
  442. H263Info->PPFlag = 0;
  443. }
  444. else
  445. {
  446. H263Info->pic->PB=1;
  447. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "Encode PB\n") );
  448. H263Info->frames++;
  449. return(sv_H263Compress(Info)); /* Encode PB */
  450. }
  451. }
  452. } /* the rest of frames */
  453. return(SvErrorNone);
  454. }
  455. static SvStatus_t sv_H263Compress(SvCodecInfo_t *Info)
  456. {
  457. SvH263CompressInfo_t *H263Info = Info->h263comp;
  458. ScBitstream_t *BSOut=Info->BSOut;
  459. H263Info->bframes += (H263Info->pic->PB ? 1 : 0);
  460. H263Info->pframes++;
  461. /* Temporal Reference is the distance between encoded frames compared
  462. the reference picture rate which is 25.0 or 30 fps */
  463. if (H263Info->next_frameskip*H263Info->orig_frameskip > 256)
  464. svH263Error("Warning: frameskip > 256\n");
  465. /* pic->TR += ((H263Info->next_frameskip*(int)H263Info->orig_frameskip) % 256); */
  466. H263Info->pic->TR = ((int) ( (int)((float)(H263Info->frame_no-H263Info->start)*H263Info->orig_frameskip) ) % 256);
  467. if (H263Info->pic->PB) { /* Code two frames as a PB-frame */
  468. if (H263Info->vsnr && H263Info->B_recon==NULL)
  469. H263Info->B_recon = sv_H263InitImage(H263Info->pels*H263Info->lines);
  470. /*
  471. fprintf(stdout,"Coding PB frames %d and %d... ",
  472. H263Info->frame_no - H263Info->pdist, H263Info->frame_no);
  473. */
  474. #if 0
  475. if(H263Info->prefilter) {
  476. if(H263Info->StaticPref)
  477. H263Info->B_clean = svH263AdaptClean(H263Info->B_image, H263Info->lines, H263Info->pels, -1, -1);
  478. else H263Info->B_clean = H263Info->B_image;
  479. if(H263Info->PrefPyrType == H263_GAUSS)
  480. H263Info->B_filtd = svH263GaussLayers(H263Info->B_clean, H263_PYR_DEPTH, H263Info->lines, H263Info->pels, NTAPS);
  481. else if(H263Info->PrefPyrType == H263_MORPH)
  482. H263Info->B_filtd = svH263MorphLayers(H263Info->B_clean, H263_PYR_DEPTH, H263Info->lines, H263Info->pels, 2);
  483. if(H263Info->StaticPref) sv_H263FreeImage(H263Info->B_clean);
  484. }
  485. fflush(stdout);
  486. #endif
  487. }
  488. else { /* Code the next frame as a normal P-frame */
  489. /* fprintf(stdout,"Coding P frame %d... ", H263Info->frame_no); */
  490. /* fflush(stdout); */
  491. }
  492. /* if (H263Info->curr_recon==NULL)
  493. H263Info->curr_recon = sv_H263InitImage(H263Info->pels*H263Info->lines); HWG */
  494. /* changed by Nuno on 06/27/96 to support prefiltering */
  495. #if 0
  496. if(H263Info->prefilter) {
  497. int m;
  498. if(H263Info->StaticPref)
  499. H263Info->curr_clean = svH263AdaptClean(H263Info->curr_image, H263Info->lines, H263Info->pels, -1, -1);
  500. else H263Info->curr_clean = H263Info->curr_image;
  501. if(H263Info->PrefPyrType == H263_GAUSS)
  502. H263Info->curr_filtd = svH263GaussLayers(H263Info->curr_clean, H263_PYR_DEPTH, H263Info->lines, H263Info->pels, NTAPS);
  503. else if(H263Info->PrefPyrType == H263_MORPH)
  504. H263Info->curr_filtd = svH263MorphLayers(H263Info->curr_clean, H263_PYR_DEPTH, H263Info->lines, H263Info->pels, 2);
  505. if(H263Info->StaticPref) sv_H263FreeImage(H263Info->curr_clean);
  506. PreFilterLevel = (unsigned char **) ScAlloc(H263Info->lines/H263_MB_SIZE*sizeof(char *));
  507. for(m=0; m<H263Info->mb_height; m++)
  508. PreFilterLevel[m]= (unsigned char *) ScAlloc(H263Info->pels/H263_MB_SIZE);
  509. }
  510. #endif
  511. sv_H263CodeOneOrTwo(Info, H263Info->QP,
  512. (int)(H263Info->next_frameskip*H263Info->orig_frameskip),
  513. H263Info->bits, H263Info->MV);
  514. #if 0
  515. if(H263Info->prefilter) {
  516. int i, j;
  517. fprintf(stdout, "Prefiltering level matrix\n");
  518. for(i=0; i<H263Info->mb_height; i++) {
  519. for(j=0; j<H263Info->mb_width; j++) {
  520. fprintf(stdout,"%4d ", PreFilterLevel[i][j]);
  521. }
  522. fprintf(stdout,"\n");
  523. }
  524. }
  525. #endif
  526. /* fprintf(stdout,"done\n"); */
  527. _SlibDebug(_VERBOSE_ && H263Info->bit_rate != 0,
  528. ScDebugPrintf(H263Info->dbg, "Inter QP: %d\n", H263Info->QP) );
  529. /* fflush(stdout); */
  530. if (arith_used) {
  531. H263Info->bits->header += sv_H263AREncoderFlush(H263Info, BSOut);
  532. arith_used = 0;
  533. }
  534. H263Info->bits->header += svH263zeroflush(BSOut); /* pictures shall be byte aligned */
  535. sv_H263AddBitsPicture(H263Info->bits);
  536. sv_H263AddBits(H263Info->total_bits, H263Info->bits);
  537. #ifdef GOB_RATE_CONTROL
  538. if (H263Info->bit_rate != 0) {
  539. sv_H263GOBUpdateRateCntrl(H263Info->bits->total);
  540. }
  541. #else
  542. /* Aim for the H263_targetrate with a once per frame rate control scheme */
  543. if (H263Info->bit_rate != 0 &&
  544. H263Info->frame_no - H263Info->start >
  545. (H263Info->end - H263Info->start) * H263Info->start_rate_control/100.0)
  546. {
  547. /* when generating the MPEG-4 anchors, rate control was started
  548. after 70% of the sequence was finished.
  549. Set H263Info->start_rate_control with option "-R <n>" */
  550. H263Info->buffer_fullness += H263Info->bits->total;
  551. H263Info->buffer_frames_stored = H263Info->frame_no;
  552. H263Info->pic->QUANT = sv_H263FrameUpdateQP(H263Info->buffer_fullness,
  553. H263Info->bits->total / (H263Info->pic->PB?2:1),
  554. (H263Info->end-H263Info->buffer_frames_stored) / H263Info->chosen_frameskip
  555. + H263Info->PPFlag,
  556. H263Info->QP, H263Info->bit_rate, H263Info->seconds);
  557. }
  558. #endif
  559. if (H263Info->pic->PB)
  560. {
  561. #ifdef _SNR_
  562. if (H263Info->B_recon)
  563. ComputeSNR(H263Info, H263Info->B_image, H263Info->B_recon,
  564. H263Info->lines, H263Info->pels);
  565. #endif
  566. /* fprintf(stdout,"Results for B-frame:\n");*/
  567. /* sv_H263FreeImage(H263Info->B_image); HWG */
  568. }
  569. #if 0
  570. if(H263Info->prefilter) ScFree(H263Info->B_filtd);
  571. #endif
  572. H263Info->distance_to_next_frame = (H263Info->PPFlag ? H263Info->pdist :
  573. (H263Info->pb_frames ? 2*H263Info->chosen_frameskip:
  574. H263Info->chosen_frameskip));
  575. /* if (H263Info->pb_frames) H263Info->pic->PB = 1; */
  576. /* fprintf(stdout,"Results for P-frame:\n"); */
  577. #ifdef _SNR_
  578. ComputeSNR(H263Info, H263Info->curr_image, H263Info->curr_recon,
  579. H263Info->lines, H263Info->pels);
  580. #endif
  581. /* PrintResult(H263Info->bits, 1, 1); */
  582. /*
  583. sv_H263FreeImage(H263Info->prev_image);
  584. H263Info->prev_image=NULL;
  585. sv_H263FreeImage(H263Info->prev_recon);
  586. H263Info->prev_recon=NULL; HWG */
  587. #if 0
  588. if(H263Info->prefilter) {
  589. int d;
  590. for(d=0; d<H263_PYR_DEPTH; d++) sv_H263FreeImage(H263Info->curr_filtd[d]);
  591. ScFree(H263Info->curr_filtd);
  592. for(d=0; d<H263Info->mb_height; d++) ScFree(PreFilterLevel[d]);
  593. ScFree(PreFilterLevel);
  594. }
  595. #endif
  596. if (H263Info->extbitstream)
  597. {
  598. SvStatus_t status;
  599. status = sv_H263WriteExtBitstream(H263Info, BSOut);
  600. if (status!=SvErrorNone)
  601. return(status);
  602. }
  603. /* point to next frame */
  604. H263Info->frame_no += H263Info->distance_to_next_frame;
  605. if (H263Info->frame_no>=H263Info->end) /* send an I frame */
  606. return(sv_H263RefreshCompressor(Info));
  607. return(SvErrorNone);
  608. }
  609. /*
  610. ** Purpose: Writes the RTP payload info out to the stream.
  611. */
  612. static SvStatus_t sv_H263WriteExtBitstream(SvH263CompressInfo_t *H263Info,
  613. ScBitstream_t *bs)
  614. {
  615. ScBSPosition_t pic_stop_position;
  616. int i;
  617. SvH263RTPInfo_t *RTPInfo=H263Info->RTPInfo;
  618. /* use this macro to byte reverse words */
  619. #define PutBits32(BS, a) ScBSPutBits(BS, (a) & 0xff, 8); \
  620. ScBSPutBits(BS, (a>>8)&0xff, 8); \
  621. ScBSPutBits(BS, (a>>16)&0xff, 8); \
  622. ScBSPutBits(BS, (a>>24)&0xff, 8);
  623. pic_stop_position=ScBSBitPosition(bs);
  624. /* round compressed size up to whole byte */
  625. RTPInfo->trailer.dwCompressedSize=(dword)(((pic_stop_position-RTPInfo->pic_start_position)+7)/8);
  626. /* Need to bitstuff here to make sure that these structures are DWORD aligned */
  627. if ((pic_stop_position%32)!=0)
  628. ScBSPutBits(bs, 0, 32-(unsigned int)(pic_stop_position % 32)); /* align on a DWORD boundary */
  629. for (i = 0; i < (int)H263Info->RTPInfo->trailer.dwNumberOfPackets; i++)
  630. {
  631. ScBSPutBits(bs,0,32) ; /* Flags = 0 */
  632. PutBits32(bs,RTPInfo->bsinfo[i].dwBitOffset);
  633. ScBSPutBits(bs,RTPInfo->bsinfo[i].Mode,8);
  634. ScBSPutBits(bs,RTPInfo->bsinfo[i].MBA,8);
  635. ScBSPutBits(bs,RTPInfo->bsinfo[i].Quant,8);
  636. ScBSPutBits(bs,RTPInfo->bsinfo[i].GOBN,8);
  637. ScBSPutBits(bs,RTPInfo->bsinfo[i].HMV1,8);
  638. ScBSPutBits(bs,RTPInfo->bsinfo[i].VMV1,8);
  639. ScBSPutBits(bs,RTPInfo->bsinfo[i].HMV2,8);
  640. ScBSPutBits(bs,RTPInfo->bsinfo[i].VMV2,8);
  641. }
  642. /* write RTP extension trailer */
  643. PutBits32(bs, RTPInfo->trailer.dwVersion);
  644. PutBits32(bs, RTPInfo->trailer.dwFlags);
  645. PutBits32(bs, RTPInfo->trailer.dwUniqueCode);
  646. PutBits32(bs, RTPInfo->trailer.dwCompressedSize);
  647. PutBits32(bs, RTPInfo->trailer.dwNumberOfPackets);
  648. ScBSPutBits(bs, RTPInfo->trailer.SourceFormat, 8);
  649. ScBSPutBits(bs, RTPInfo->trailer.TR, 8);
  650. ScBSPutBits(bs, RTPInfo->trailer.TRB, 8);
  651. ScBSPutBits(bs, RTPInfo->trailer.DBQ, 8);
  652. return (NoErrors);
  653. }
  654. /***************************************************/
  655. /*
  656. int start, int end, int source_format, int frameskip,
  657. int ME_method, int headerlength, char *seqfilename,
  658. int QP, int QPI, char *streamname, int unrestricted,
  659. int sac, int advanced, int pb_frame, int bit_rate)
  660. */
  661. /***************************************************/
  662. SvStatus_t svH263InitCompressor(SvCodecInfo_t *Info)
  663. {
  664. SvH263CompressInfo_t *H263Info = Info->h263comp;
  665. int i,j,k;
  666. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "sv_H263InitCompressor()") );
  667. if (H263Info->inited)
  668. return(SvErrorNone);
  669. if (Info->Width==SQCIF_WIDTH && Info->Height==SQCIF_HEIGHT)
  670. H263Info->source_format=H263_SF_SQCIF;
  671. else if (Info->Width==QCIF_WIDTH && Info->Height==QCIF_HEIGHT)
  672. H263Info->source_format=H263_SF_QCIF;
  673. else if (Info->Width==CIF_WIDTH && Info->Height==CIF_HEIGHT)
  674. H263Info->source_format=H263_SF_CIF;
  675. else if (Info->Width==CIF4_WIDTH && Info->Height==CIF4_HEIGHT)
  676. H263Info->source_format=H263_SF_4CIF;
  677. else if (Info->Width==CIF16_WIDTH && Info->Height==CIF16_HEIGHT)
  678. H263Info->source_format=H263_SF_16CIF;
  679. else
  680. {
  681. _SlibDebug(_WARN_, ScDebugPrintf(H263Info->dbg, "sv_H263InitCompressor() Illegal input format\n") );
  682. return(SvErrorUnrecognizedFormat);
  683. }
  684. /* start and stop frame numbers under to calculate rate control;
  685. * a more advanced rate control is still needed
  686. */
  687. H263Info->start = 0;
  688. H263Info->pdist = H263Info->bdist = 1;
  689. H263Info->first_loop_finished=0;
  690. H263Info->PPFlag = 0;
  691. H263Info->pic = (H263_Pict *)ScAlloc(sizeof(H263_Pict));
  692. H263Info->bits = (H263_Bits *)ScAlloc(sizeof(H263_Bits));
  693. H263Info->total_bits = (H263_Bits *)ScAlloc(sizeof(H263_Bits));
  694. H263Info->intra_bits = (H263_Bits *)ScAlloc(sizeof(H263_Bits));
  695. H263Info->res = (H263_Results *)ScAlloc(sizeof(H263_Results));
  696. H263Info->total_res = (H263_Results *)ScAlloc(sizeof(H263_Results));
  697. H263Info->b_res = (H263_Results *)ScAlloc(sizeof(H263_Results));
  698. /* woring buffers */
  699. H263Info->wk_buffers = ScAlloc(sizeof(H263_WORKING_BUFFER));
  700. /*
  701. fprintf(stdout,"\nH.263 coder (TMN)\n");
  702. fprintf(stdout,"(C) Digital Equipment Corp.\n");
  703. */
  704. H263Info->headerlength = H263_DEF_HEADERLENGTH;
  705. H263Info->refidct = 0;
  706. /* Initalize VLC_tables */
  707. sv_H263InitHuff(H263Info);
  708. /* allocate buffer for FAST search */
  709. H263Info->block_subs2 = (unsigned char *)ScAlloc(sizeof(char)*64);
  710. H263Info->srch_area_subs2=
  711. (unsigned char *)ScAlloc(sizeof(char)*H263_SRCH_RANGE*H263_SRCH_RANGE);
  712. if (H263Info->unrestricted){
  713. /* note that the Unrestricted Motion Vector mode turns on
  714. both long_vectors and mv_outside_frame */
  715. H263Info->pic->unrestricted_mv_mode = H263Info->unrestricted;
  716. H263Info->mv_outside_frame = H263_ON;
  717. H263Info->long_vectors = H263_ON;
  718. }
  719. if (H263Info->advanced)
  720. H263Info->mv_outside_frame = H263_ON;
  721. /* H263Info->ME_method = ME_method; --- gets set in sv_api.c */
  722. H263Info->HPME_method = H263_DEF_HPME_METHOD;
  723. H263Info->DCT_method = H263_DEF_DCT_METHOD;
  724. H263Info->vsnr = H263_DEF_VSNR;
  725. #if 0
  726. /*** prefilter ***/
  727. H263Info->prefilter = H263_NO;
  728. H263Info->PYR_DEPTH = H263_DEF_PYR_DEPTH;
  729. H263Info->PrefPyrType = H263_DEF_PREF_PYR_TYPE;
  730. H263Info->StaticPref = H263_DEF_STAT_PREF_STATE;
  731. #endif
  732. SetDefPrefLevel(H263Info);
  733. SetDefThresh(H263Info);
  734. /* BQUANT parameter for PB-frame coding
  735. * (n * QP / 4 )
  736. *
  737. * BQUANT n
  738. * 0 5
  739. * 1 6
  740. * 2 7
  741. * 3 8
  742. */
  743. H263Info->pic->BQUANT = 2;
  744. if (H263Info->frame_rate<=1.0F) /* frame_rate not yet initialized */
  745. H263Info->frame_rate = 30.0F;
  746. H263Info->ref_frame_rate = H263Info->frame_rate;
  747. H263Info->orig_frame_rate = H263Info->frame_rate;
  748. /* default skipped frames between encoded frames (P or B) */
  749. /* reference is original sequence */
  750. /* 3 means 8.33/10.0 fps encoded frame rate with 25.0/30.0 fps original */
  751. /* 1 means 8.33/10.0 fps encoded frame rate with 8.33/10.0 fps original */
  752. H263Info->chosen_frameskip = 1;
  753. /* default number of skipped frames in original sequence compared to */
  754. /* the reference picture rate ( also option "-O <n>" ) */
  755. /* 4 means that the original sequence is grabbed at 6.25/7.5 Hz */
  756. /* 1 means that the original sequence is grabbed at 25.0/30.0 Hz */
  757. H263Info->orig_frameskip = 1.0F;
  758. H263Info->start_rate_control = 0;
  759. H263Info->trace = H263_DEF_WRITE_TRACE;
  760. H263Info->pic->seek_dist = H263_DEF_SEEK_DIST;
  761. H263Info->pic->use_gobsync = H263_DEF_INSERT_SYNC;
  762. /* define GOB sync */
  763. H263Info->pic->use_gobsync = 1;
  764. /* H263Info->bit_rate = bit_rate; --- gets set in sv_api.c */
  765. /* default is variable bit rate (fixed quantizer) will be used */
  766. H263Info->frames = 0;
  767. H263Info->pframes = 0;
  768. H263Info->bframes = 0;
  769. H263Info->total_frames_passed = 0;
  770. H263Info->pic->PB = 0;
  771. H263Info->pic->TR = 0;
  772. H263Info->QP = H263Info->QP_init;
  773. H263Info->pic->QP_mean = (float)0.0;
  774. _SlibDebug(_WARN_ && (H263Info->QP == 0 || H263Info->QPI == 0),
  775. ScDebugPrintf(H263Info->dbg, "Warning: QP is zero. Bitstream will not be correctly decodable\n") );
  776. _SlibDebug(_WARN_ && (H263Info->ref_frame_rate != 25.0 && H263Info->ref_frame_rate != 30.0),
  777. ScDebugPrintf(H263Info->dbg, "Warning: Reference frame rate should be 25 or 30 fps\n") );
  778. H263Info->pic->source_format = H263Info->source_format;
  779. H263Info->pels = Info->Width;
  780. H263Info->lines = Info->Height;
  781. H263Info->PYR_DEPTH = H263Info->PYR_DEPTH>0 ? H263Info->PYR_DEPTH : 1;
  782. H263Info->PYR_DEPTH = H263Info->PYR_DEPTH<=H263_MAX_PYR_DEPTH ? H263Info->PYR_DEPTH : H263_MAX_PYR_DEPTH;
  783. CheckPrefLevel(H263Info, H263Info->PYR_DEPTH);
  784. H263Info->cpels = H263Info->pels/2;
  785. H263Info->mb_width = H263Info->pels / H263_MB_SIZE;
  786. H263Info->mb_height = H263Info->lines / H263_MB_SIZE;
  787. H263Info->orig_frameskip = H263Info->ref_frame_rate / H263Info->orig_frame_rate;
  788. H263Info->frame_rate = H263Info->ref_frame_rate / (float)(H263Info->orig_frameskip * H263Info->chosen_frameskip);
  789. _SlibDebug(_VERBOSE_,
  790. ScDebugPrintf(H263Info->dbg, "Encoding frame rate : %.2f\n", H263Info->frame_rate);
  791. ScDebugPrintf(H263Info->dbg, "Reference frame rate : %.2f\n", H263Info->ref_frame_rate);
  792. ScDebugPrintf(H263Info->dbg, "Orig. seq. frame rate: %.2f\n\n",
  793. H263Info->ref_frame_rate / (float)H263Info->orig_frameskip) );
  794. if (H263Info->refidct) sv_H263init_idctref();
  795. /* Open stream for writing */
  796. /* svH263mwopen(H263Info->streamname); */
  797. #if 0
  798. /* open video sequence */
  799. if ((H263Info->video_file = fopen(seqfilename,"rb")) == NULL) {
  800. fprintf(stderr,"Unable to open image_file: %s\n",seqfilename);
  801. exit(-1);
  802. }
  803. svH263RemovHead(H263Info->headerlength,start,H263Info->video_file);
  804. #endif
  805. /* for Motion Estimation */
  806. for (j = 0; j < H263Info->mb_height+1; j++)
  807. for (i = 0; i < H263Info->mb_width+2; i++)
  808. for (k = 0; k < 6; k++)
  809. H263Info->MV[k][j][i] = (H263_MotionVector *)ScAlloc(sizeof(H263_MotionVector));
  810. /* for Interpolation */
  811. if (H263Info->mv_outside_frame) {
  812. if (H263Info->long_vectors)
  813. H263Info->wk_buffers->ipol_image=(unsigned char *)ScAlloc(sizeof(char)*(H263Info->pels+64)*(H263Info->lines+64)*4);
  814. else
  815. H263Info->wk_buffers->ipol_image=(unsigned char *)ScAlloc(sizeof(char)*(H263Info->pels+32)*(H263Info->lines+32)*4);
  816. }
  817. else
  818. H263Info->wk_buffers->ipol_image =(unsigned char *)ScAlloc(sizeof(char)*H263Info->pels*H263Info->lines*4);
  819. if ((H263Info->wk_buffers->qcoeff_P=(short *)ScAlloc(sizeof(short)*384)) == 0)
  820. return(SvErrorMemory);
  821. /* allocate buffers for curr_image */
  822. H263Info->curr_image = sv_H263InitImage(H263Info->pels*H263Info->lines);
  823. if (H263Info->curr_image==NULL)
  824. return(SvErrorMemory);
  825. /* Point to the first frame to be coded */
  826. H263Info->frame_no = H263Info->start;
  827. /* initialization done */
  828. H263Info->inited = TRUE;
  829. H263Info->buffer_fullness = 0;
  830. H263Info->buffer_frames_stored = 0;
  831. if (H263Info->extbitstream)
  832. {
  833. H263Info->RTPInfo = (SvH263RTPInfo_t *) ScAlloc(sizeof(SvH263RTPInfo_t));
  834. if (H263Info->RTPInfo==NULL)
  835. return(SvErrorMemory);
  836. memset(H263Info->RTPInfo, 0, sizeof(SvH263RTPInfo_t)) ;
  837. }
  838. #ifdef GOB_RATE_CONTROL
  839. sv_H263GOBInitRateCntrl();
  840. #endif
  841. return(SvErrorNone);
  842. }
  843. /***************************************************/
  844. /***************************************************/
  845. SvStatus_t sv_H263RefreshCompressor(SvCodecInfo_t *Info)
  846. {
  847. SvH263CompressInfo_t *H263Info = Info->h263comp;
  848. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "Refresh Compressor()") );
  849. if (!H263Info->inited)
  850. return(SvErrorNone);
  851. H263Info->pdist = H263Info->bdist = 1;
  852. H263Info->first_loop_finished=0;
  853. H263Info->PPFlag = 0;
  854. H263Info->pic->BQUANT = 2;
  855. H263Info->frames = 0;
  856. H263Info->pframes = 0;
  857. H263Info->bframes = 0;
  858. H263Info->total_frames_passed = 0;
  859. H263Info->pic->PB = 0;
  860. H263Info->pic->TR = 0;
  861. H263Info->QP = H263Info->QP_init;
  862. H263Info->pic->QP_mean = (float)0.0;
  863. /* Point to the first frame to be coded */
  864. H263Info->frame_no = H263Info->start;
  865. /* initialization done */
  866. H263Info->inited = TRUE;
  867. H263Info->buffer_fullness = 0;
  868. H263Info->buffer_frames_stored = 0;
  869. /* next frame will be key so we can reset bit positions */
  870. ScBSResetCounters(Info->BSOut);
  871. return(SvErrorNone);
  872. }
  873. /***************************************************/
  874. /***************************************************/
  875. void svH263FreeCompressor(SvCodecInfo_t *Info)
  876. {
  877. SvH263CompressInfo_t *H263Info = Info->h263comp;
  878. int i,j,k;
  879. _SlibDebug(_WRITE_, ScFileClose(DEBUGIMG) );
  880. if (H263Info->inited)
  881. {
  882. /* Free memory */
  883. for (j = 0; j < H263Info->mb_height+1; j++)
  884. for (i = 0; i < H263Info->mb_width+2; i++)
  885. for (k = 0; k < 6; k++)
  886. ScFree(H263Info->MV[k][j][i]);
  887. if (H263Info->block_subs2)
  888. ScFree(H263Info->block_subs2);
  889. if (H263Info->srch_area_subs2)
  890. ScFree(H263Info->srch_area_subs2);
  891. ScFree(H263Info->wk_buffers->qcoeff_P);
  892. ScFree(H263Info->wk_buffers->ipol_image);
  893. ScFree(H263Info->wk_buffers);
  894. if (H263Info->curr_recon==H263Info->prev_recon ||
  895. H263Info->curr_recon==H263Info->B_recon)
  896. H263Info->curr_recon=NULL;
  897. if (H263Info->prev_recon==H263Info->B_recon)
  898. H263Info->prev_recon=NULL;
  899. if (H263Info->curr_image==H263Info->prev_image ||
  900. H263Info->curr_image==H263Info->B_image)
  901. H263Info->curr_image=NULL;
  902. if (H263Info->prev_image==H263Info->B_image)
  903. H263Info->prev_image=NULL;
  904. if (H263Info->curr_recon)
  905. {
  906. sv_H263FreeImage(H263Info->curr_recon);
  907. H263Info->curr_recon=NULL;
  908. }
  909. if (H263Info->curr_image)
  910. {
  911. sv_H263FreeImage(H263Info->curr_image);
  912. H263Info->curr_image=NULL;
  913. }
  914. if (H263Info->prev_recon)
  915. {
  916. sv_H263FreeImage(H263Info->prev_recon);
  917. H263Info->prev_recon=NULL;
  918. }
  919. if (H263Info->prev_image)
  920. {
  921. sv_H263FreeImage(H263Info->prev_image);
  922. H263Info->prev_image=NULL;
  923. }
  924. if (H263Info->B_image)
  925. {
  926. sv_H263FreeImage(H263Info->B_image);
  927. H263Info->B_image=NULL;
  928. }
  929. if (H263Info->B_recon)
  930. {
  931. sv_H263FreeImage(H263Info->B_recon);
  932. H263Info->B_recon=NULL;
  933. }
  934. sv_H263FreeHuff(H263Info);
  935. ScFree(H263Info->bits);
  936. ScFree(H263Info->total_bits);
  937. ScFree(H263Info->intra_bits);
  938. ScFree(H263Info->res);
  939. ScFree(H263Info->total_res);
  940. ScFree(H263Info->b_res);
  941. ScFree(H263Info->pic);
  942. H263Info->inited=FALSE;
  943. if (H263Info->RTPInfo)
  944. ScFree(H263Info->RTPInfo);
  945. }
  946. return;
  947. }
  948. /**********************************************************************
  949. *
  950. * Name: NextTwoPB
  951. * Description: Decides whether or not to code the next
  952. * two images as PB
  953. * Speed: This is not a very smart solution considering
  954. * the encoding speed, since motion vectors
  955. * have to be calculation several times. It
  956. * can be done together with the normal
  957. * motion vector search, or a tree search
  958. * instead of a full search can be used.
  959. *
  960. * Input: pointers to previous image, potential B-
  961. * and P-image, frame distances
  962. * Returns: 1 for yes, 0 otherwise
  963. * Side effects:
  964. *
  965. ***********************************************************************/
  966. /* static int NextTwoPB(PictImage *next2, PictImage *next1, PictImage *prev,
  967. int bskip, int pskip, int seek_dist) */
  968. static int NextTwoPB(SvH263CompressInfo_t *H263Info,
  969. H263_PictImage *next2, H263_PictImage *next1, H263_PictImage *prev,
  970. int bskip, int pskip, int seek_dist)
  971. {
  972. int adv_is_on = 0, mof_is_on = 0, lv_is_on = 0;
  973. int psad1, psad2, bsad, psad;
  974. int x,y,i,j,tmp;
  975. int ne2_pr_x, ne2_pr_y, mvbf_x, mvbf_y, mvbb_x, mvbb_y;
  976. short MVx, MVy, MVer;
  977. /* Temporarily disable some options to simplify motion estimation */
  978. if (H263Info->advanced) {
  979. H263Info->advanced = H263_OFF;
  980. adv_is_on = H263_ON;
  981. }
  982. if (H263Info->mv_outside_frame) {
  983. H263Info->mv_outside_frame = H263_OFF;
  984. mof_is_on = H263_ON;
  985. }
  986. if (H263Info->long_vectors) {
  987. H263Info->long_vectors = H263_OFF;
  988. lv_is_on = H263_ON;
  989. }
  990. bsad = psad = psad1 = psad2 = 0;
  991. /* Integer motion estimation */
  992. for ( j = 1; j < H263Info->mb_height - 1; j++) {
  993. for ( i = 1; i < H263Info->mb_width - 1 ; i++) {
  994. x = i*H263_MB_SIZE;
  995. y = j*H263_MB_SIZE;
  996. /* picture order: prev -> next1 -> next2 */
  997. /* next1 and next2 can be coded as PB or PP */
  998. /* prev is the previous encoded picture */
  999. /* computes vectors (prev <- next2) */
  1000. #if 1
  1001. /* faster estimation */
  1002. sv_H263FastME(H263Info, next2->lum,prev->lum,x,y,0,0,seek_dist,
  1003. &MVx,&MVy,&MVer,&tmp);
  1004. #else
  1005. svH263MotionEstimation(next2->lum,prev->lum,x,y,0,0,seek_dist,MV,&tmp);
  1006. #endif
  1007. /* not necessary to prefer zero vector here */
  1008. if (MVx == 0 && MVy == 0){
  1009. psad += (MVer + H263_PREF_NULL_VEC) ;
  1010. ne2_pr_x = ne2_pr_y = 0;
  1011. }
  1012. else{
  1013. psad += MVer ;
  1014. ne2_pr_x = MVx;
  1015. ne2_pr_y = MVy;
  1016. }
  1017. /* computes sad(prev <- next1) */
  1018. #if 1
  1019. /* faster estimation */
  1020. sv_H263FastME(H263Info, next1->lum,prev->lum,x,y,0,0,seek_dist,
  1021. &MVx,&MVy,&MVer,&tmp);
  1022. #else
  1023. svH263MotionEstimation(next1->lum,prev->lum,x,y,0,0,seek_dist,MV,&tmp);
  1024. #endif
  1025. if (MVx == 0 && MVy == 0)
  1026. psad2 += (MVer + H263_PREF_NULL_VEC);
  1027. else
  1028. psad2 += MVer;
  1029. /* computes vectors for (next1 <- next2) */
  1030. #if 1
  1031. /* faster estimation */
  1032. sv_H263FastME(H263Info, next2->lum,next1->lum,x,y,0,0,seek_dist,
  1033. &MVx,&MVy,&MVer,&tmp);
  1034. #else
  1035. svH263MotionEstimation(next2->lum,next1->lum,x,y,0,0,seek_dist,MV,&tmp);
  1036. #endif
  1037. if (MVx == 0 && MVy == 0)
  1038. psad1 += (MVer + H263_PREF_NULL_VEC);
  1039. else
  1040. psad1 += MVer ;
  1041. /* scales vectors for (prev <- next2 ) */
  1042. mvbf_x = bskip * ne2_pr_x / (bskip + pskip);
  1043. mvbb_x = - pskip * ne2_pr_x / (bskip + pskip);
  1044. mvbf_y = bskip * ne2_pr_y / (bskip + pskip);
  1045. mvbb_y = - pskip * ne2_pr_y / (bskip + pskip);
  1046. /* computes sad(prev <- next1 -> next2) */
  1047. #ifndef USE_C
  1048. bsad += sv_H263BError16x16_S(next1->lum + x + y*H263Info->pels,
  1049. next2->lum + x + mvbb_x + (y + mvbb_y)*H263Info->pels,
  1050. prev->lum + x + mvbf_x + (y + mvbf_y)*H263Info->pels,
  1051. H263Info->pels);
  1052. #else
  1053. bsad += sv_H263BError16x16_C(next1->lum + x + y*H263Info->pels,
  1054. next2->lum + x + mvbb_x + (y + mvbb_y)*H263Info->pels,
  1055. prev->lum + x + mvbf_x + (y + mvbf_y)*H263Info->pels,
  1056. H263Info->pels, INT_MAX);
  1057. #endif
  1058. }
  1059. }
  1060. /* restore advanced parameters */
  1061. H263Info->advanced = adv_is_on;
  1062. H263Info->mv_outside_frame = mof_is_on;
  1063. H263Info->long_vectors = lv_is_on;
  1064. /* do the decision */
  1065. if (bsad < (psad1+psad2)/2) {
  1066. /*
  1067. fprintf(stdout,"Chose PB - bsad %d, psad %d\n", bsad, (psad1+psad2)/2);
  1068. */
  1069. return 1;
  1070. }
  1071. else {
  1072. /*
  1073. fprintf(stdout,"Chose PP - bsad %d, psad %d\n", bsad, (psad1+psad2)/2);
  1074. */
  1075. return 0;
  1076. }
  1077. }
  1078. #ifdef _SLIBDEBUG_
  1079. /**********************************************************************
  1080. *
  1081. * Name: PrintResult
  1082. * Description: add bits and prints results
  1083. *
  1084. * Input: Bits struct
  1085. *
  1086. * Returns:
  1087. * Side effects:
  1088. *
  1089. ***********************************************************************/
  1090. void PrintResult(SvH263CompressInfo_t *H263Info, H263_Bits *bits,
  1091. int num_units, int num)
  1092. {
  1093. ScDebugPrintf(H263Info->dbg,"# intra : %d\n", bits->no_intra/num_units);
  1094. ScDebugPrintf(H263Info->dbg,"# inter : %d\n", bits->no_inter/num_units);
  1095. ScDebugPrintf(H263Info->dbg,"# inter4v : %d\n", bits->no_inter4v/num_units);
  1096. ScDebugPrintf(H263Info->dbg,"--------------\n");
  1097. ScDebugPrintf(H263Info->dbg,"Coeff_Y: %d\n", bits->Y/num);
  1098. ScDebugPrintf(H263Info->dbg,"Coeff_C: %d\n", bits->C/num);
  1099. ScDebugPrintf(H263Info->dbg,"Vectors: %d\n", bits->vec/num);
  1100. ScDebugPrintf(H263Info->dbg,"CBPY : %d\n", bits->CBPY/num);
  1101. ScDebugPrintf(H263Info->dbg,"MCBPC : %d\n", bits->CBPCM/num);
  1102. ScDebugPrintf(H263Info->dbg,"MODB : %d\n", bits->MODB/num);
  1103. ScDebugPrintf(H263Info->dbg,"CBPB : %d\n", bits->CBPB/num);
  1104. ScDebugPrintf(H263Info->dbg,"COD : %d\n", bits->COD/num);
  1105. ScDebugPrintf(H263Info->dbg,"DQUANT : %d\n", bits->DQUANT/num);
  1106. ScDebugPrintf(H263Info->dbg,"header : %d\n", bits->header/num);
  1107. ScDebugPrintf(H263Info->dbg,"==============\n");
  1108. ScDebugPrintf(H263Info->dbg,"Total : %d\n", bits->total/num);
  1109. ScDebugPrintf(H263Info->dbg,"\n");
  1110. return;
  1111. }
  1112. #endif
  1113. /*****************************************************************
  1114. *
  1115. * coder.c for H.263 encoder
  1116. * Wei-Lien Hsu
  1117. * Date: December 11, 1996
  1118. *
  1119. *****************************************************************/
  1120. static void SelectBounds(H263_PictImage *Img, unsigned char **PL, int rows, int cols) ;
  1121. static unsigned char LargeMv(H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2], int i, int j, int th) ;
  1122. static unsigned char LargePerror(H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2], int i, int j, int th);
  1123. static unsigned char BoundaryMB(SvH263CompressInfo_t *H263Info, int i, int j, int pels, int lines) ;
  1124. static int GetPrefLevel(SvH263CompressInfo_t *H263Info, H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2], int i, int j, int rows, int cols) ;
  1125. void FillLumBlock(SvH263CompressInfo_t *H263Info, int x, int y, H263_PictImage *image, H263_MB_Structure *data);
  1126. void FillChromBlock(SvH263CompressInfo_t *H263Info, int x_curr, int y_curr, H263_PictImage *image,
  1127. H263_MB_Structure *data);
  1128. void FillLumPredBlock(SvH263CompressInfo_t *H263Info, int x, int y, PredImage *image, H263_MB_Structure *data);
  1129. void FillChromPredBlock(SvH263CompressInfo_t *H263Info, int x_curr, int y_curr, PredImage *image,
  1130. H263_MB_Structure *data);
  1131. void ZeroMBlock(H263_MB_Structure *data);
  1132. void ReconImage(SvH263CompressInfo_t *H263Info, int i, int j, H263_MB_Structure *data, H263_PictImage *recon);
  1133. void ReconPredImage(SvH263CompressInfo_t *H263Info, int i, int j, H263_MB_Structure *data, PredImage *recon);
  1134. void InterpolateImage(unsigned char *image, unsigned char *ipol_image,
  1135. int width, int height);
  1136. void MotionEstimatePicture(SvH263CompressInfo_t *H263Info, unsigned char *curr, unsigned char *prev,
  1137. unsigned char *prev_ipol, int seek_dist,
  1138. H263_MotionVector *MV[5][H263_MBR+1][H263_MBC+2], int gobsync);
  1139. void MakeEdgeImage(unsigned char *src, unsigned char *dst, int width,
  1140. int height, int edge);
  1141. /**************************************************************************
  1142. * Function: SelectBounds
  1143. * Draws a boundary around each MacroBlock with width equal to its assigned
  1144. * prefilter level
  1145. *************************************************************************/
  1146. #if 0
  1147. void SelectBounds(H263_PictImage *Img, unsigned char **PL, int rows, int cols)
  1148. {
  1149. int i, j, l, m, n, r, c;
  1150. for(i=0; i<rows/H263_MB_SIZE; i++) {
  1151. for(j=0; j<cols/H263_MB_SIZE; j++) {
  1152. for(l=0; l<PL[i][j]; l++) {
  1153. r = i*H263_MB_SIZE+l;
  1154. for(n=l; n<H263_MB_SIZE-l; n++) {
  1155. c = j*H263_MB_SIZE+n;
  1156. Img->lum[r*cols+c] = 255;
  1157. Img->lum[(r+H263_MB_SIZE-1-l)*cols+c] = 255;
  1158. }
  1159. c = j*H263_MB_SIZE+l;
  1160. for(m=l; m<H263_MB_SIZE-l; m++) {
  1161. r = i*H263_MB_SIZE+m;
  1162. Img->lum[r*cols+c] = 255;
  1163. Img->lum[r*cols+(c+H263_MB_SIZE-1-l)] = 255;
  1164. }
  1165. }
  1166. }
  1167. }
  1168. }
  1169. #endif
  1170. /**********************************************************************
  1171. * Function: LargeMv
  1172. * Checks if the norm of the integer component of the motion vector
  1173. * of the macroblock i, j is largen than the threshold th. Returns 1 if
  1174. * yes, 0 if not.
  1175. * Added by Nuno on 07/1/96 to support adaptive prefiltering.
  1176. **********************************************************************/
  1177. unsigned char LargeMv(H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2], int i, int j, int th)
  1178. {
  1179. return(sqrt((double) MV[0][j+1][i+1]->x*MV[0][j+1][i+1]->x +
  1180. (double) MV[0][j+1][i+1]->y*MV[0][j+1][i+1]->y) > th);
  1181. }
  1182. /**********************************************************************
  1183. * Function: LargePerror
  1184. * Checks if the prediction error for macroblock i, j is largen than
  1185. * the threshold th. Returns 1 if yes, 0 if not.
  1186. * Added by Nuno on 07/1/96 to support adaptive prefiltering.
  1187. **********************************************************************/
  1188. unsigned char LargePerror(H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2], int i, int j, int th)
  1189. {
  1190. return(MV[0][j+1][i+1]->min_error > th);
  1191. }
  1192. /**********************************************************************
  1193. * Function: BoundaryMB
  1194. * Returns 1 if a MB is on the boundary of the image, o if not.
  1195. * Added by Nuno on 07/1/96 to support adaptive prefiltering.
  1196. **********************************************************************/
  1197. unsigned char BoundaryMB(SvH263CompressInfo_t *H263Info, int i, int j, int pels, int lines)
  1198. {
  1199. return(j==0 || i==0 || i==(H263Info->mb_width -1) || j==(H263Info->mb_height - 1));
  1200. }
  1201. /***********************************************************************
  1202. * Function: GetPrefLevel
  1203. * Selects the level of the pyramid of prefiltered images that is best
  1204. * suited for the encoding of the MacroBlock (i,j)
  1205. **********************************************************************/
  1206. int GetPrefLevel(SvH263CompressInfo_t *H263Info,
  1207. H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2], int i, int j, int rows, int cols)
  1208. {
  1209. int motbin, pebin;
  1210. motbin = 0;
  1211. while(LargeMv(MV, i, j, (int) H263Info->MOTresh[motbin]) && motbin<3) motbin++;
  1212. pebin = 0;
  1213. while(LargePerror(MV, i, j, H263Info->PETresh[pebin]) && pebin<2) pebin++;
  1214. if(BoundaryMB(H263Info, i, j, cols, rows) && motbin<3) motbin++;
  1215. return H263Info->PREF_LEVEL[motbin][pebin];
  1216. }
  1217. /**********************************************************************
  1218. *
  1219. * Name: sv_H263CodeOneOrTwo
  1220. * Description: code one image normally or two images
  1221. * as a PB-frame (CodeTwoPB and CodeOnePred merged)
  1222. *
  1223. * Input: pointer to image, prev_image, prev_recon, Q
  1224. *
  1225. * Returns: pointer to reconstructed image
  1226. * Side effects: memory is allocated to recon image
  1227. * changed by Nuno on 06/27/96 to support filtering of the prediction error
  1228. ***********************************************************************/
  1229. static SvStatus_t sv_H263CodeOneOrTwo(SvCodecInfo_t *Info, int QP, int frameskip,
  1230. H263_Bits *bits, H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2])
  1231. {
  1232. SvH263CompressInfo_t *H263Info = Info->h263comp;
  1233. ScBitstream_t *BSOut = Info->BSOut;
  1234. H263_Pict *pic=H263Info->pic;
  1235. unsigned char *prev_ipol,*pi_edge=NULL,*orig_lum;
  1236. H263_MotionVector ZERO = {0,0,0,0,0};
  1237. H263_PictImage *prev_recon=NULL, *pr_edge=NULL;
  1238. H263_MB_Structure *recon_data_P = (H263_MB_Structure *)ScAlloc(sizeof(H263_MB_Structure));
  1239. H263_MB_Structure *recon_data_B=NULL;
  1240. H263_MB_Structure *diff=(H263_MB_Structure *)ScAlloc(sizeof(H263_MB_Structure));
  1241. H263_MB_Structure *Bpred=NULL;
  1242. short *qcoeff_P;
  1243. short *qcoeff_B=NULL;
  1244. unsigned char *pi;
  1245. int Mode,B;
  1246. int CBP, CBPB=0;
  1247. int bquant[] = {5,6,7,8};
  1248. int QP_B;
  1249. int newgob;
  1250. int i,j,k;
  1251. /* buffer control vars */
  1252. float QP_cumulative = (float)0.0;
  1253. int abs_mb_num = 0, QuantChangePostponed = 0;
  1254. int QP_new, QP_prev, dquant, QP_xmitted=QP;
  1255. sv_H263ZeroBits(bits);
  1256. pi = H263Info->wk_buffers->ipol_image;
  1257. qcoeff_P= H263Info->wk_buffers->qcoeff_P;
  1258. if(pic->PB){
  1259. if ((qcoeff_B=(short *)ScAlloc(sizeof(short)*384)) == 0)
  1260. return(SvErrorMemory);
  1261. recon_data_B=(H263_MB_Structure *)ScAlloc(sizeof(H263_MB_Structure));
  1262. Bpred=(H263_MB_Structure *)ScAlloc(sizeof(H263_MB_Structure));
  1263. }
  1264. /* interpolate image */
  1265. if (H263Info->mv_outside_frame) {
  1266. if (H263Info->long_vectors) {
  1267. /* If the Extended Motion Vector range is used, motion vectors
  1268. may point further out of the picture than in the normal range,
  1269. and the edge images will have to be made larger */
  1270. B = 16;
  1271. }
  1272. else {
  1273. /* normal range */
  1274. B = 8;
  1275. }
  1276. pi_edge = (unsigned char *)ScAlloc(sizeof(char)*(H263Info->pels+4*B)*(H263Info->lines+4*B));
  1277. if (pi_edge == NULL)
  1278. return(SvErrorMemory);
  1279. MakeEdgeImage(H263Info->prev_recon->lum,pi_edge + (H263Info->pels + 4*B)*2*B+2*B,H263Info->pels,H263Info->lines,2*B);
  1280. InterpolateImage(pi_edge, pi, H263Info->pels+4*B, H263Info->lines+4*B);
  1281. ScFree(pi_edge);
  1282. prev_ipol = pi + (2*H263Info->pels + 8*B) * 4*B + 4*B;
  1283. /* luma of non_interpolated image */
  1284. pr_edge = sv_H263InitImage((H263Info->pels+4*B)*(H263Info->lines+4*B));
  1285. MakeEdgeImage(H263Info->prev_image->lum, pr_edge->lum + (H263Info->pels + 4*B)*2*B+2*B,
  1286. H263Info->pels,H263Info->lines,2*B);
  1287. orig_lum = pr_edge->lum + (H263Info->pels + 4*B)*2*B+2*B;
  1288. /* non-interpolated image */
  1289. MakeEdgeImage(H263Info->prev_recon->lum,pr_edge->lum + (H263Info->pels+4*B)*2*B + 2*B,H263Info->pels,H263Info->lines,2*B);
  1290. MakeEdgeImage(H263Info->prev_recon->Cr,pr_edge->Cr + (H263Info->pels/2 + 2*B)*B + B,H263Info->pels/2,H263Info->lines/2,B);
  1291. MakeEdgeImage(H263Info->prev_recon->Cb,pr_edge->Cb + (H263Info->pels/2 + 2*B)*B + B,H263Info->pels/2,H263Info->lines/2,B);
  1292. prev_recon = (H263_PictImage *)ScAlloc(sizeof(H263_PictImage));
  1293. prev_recon->lum = pr_edge->lum + (H263Info->pels + 4*B)*2*B + 2*B;
  1294. prev_recon->Cr = pr_edge->Cr + (H263Info->pels/2 + 2*B)*B + B;
  1295. prev_recon->Cb = pr_edge->Cb + (H263Info->pels/2 + 2*B)*B + B;
  1296. }
  1297. else {
  1298. InterpolateImage(H263Info->prev_recon->lum,pi,H263Info->pels,H263Info->lines);
  1299. prev_ipol = pi;
  1300. prev_recon = H263Info->prev_recon;
  1301. orig_lum = H263Info->prev_image->lum;
  1302. }
  1303. /* mark PMV's outside the frame */
  1304. for (i = 1; i < H263Info->mb_width+1; i++) {
  1305. for (k = 0; k < 6; k++) {
  1306. sv_H263MarkVec(MV[k][0][i]);
  1307. }
  1308. MV[0][0][i]->Mode = H263_MODE_INTRA;
  1309. }
  1310. /* zero out PMV's outside the frame */
  1311. for (i = 0; i < H263Info->mb_height+1; i++) {
  1312. for (k = 0; k < 6; k++) {
  1313. sv_H263ZeroVec(MV[k][i][0]);
  1314. sv_H263ZeroVec(MV[k][i][H263Info->mb_width+1]);
  1315. }
  1316. MV[0][i][0]->Mode = H263_MODE_INTRA;
  1317. MV[0][i][H263Info->mb_width+1]->Mode = H263_MODE_INTRA;
  1318. }
  1319. /* Integer and half pel motion estimation */
  1320. MotionEstimatePicture(H263Info, H263Info->curr_image->lum,prev_recon->lum,prev_ipol,
  1321. pic->seek_dist,MV, pic->use_gobsync);
  1322. /*
  1323. fprintf(stdout,"\nMotion Vector Magintudes\n");
  1324. for ( j = 0; j < H263Info->lines/H263_MB_SIZE; j++) {
  1325. for ( i = 0; i < H263Info->pels/H263_MB_SIZE; i++) {
  1326. fprintf(stdout, "%4.0lf ", sqrt((double) MV[0][j+1][i+1]->x*MV[0][j+1][i+1]->x
  1327. + MV[0][j+1][i+1]->y*MV[0][j+1][i+1]->y));
  1328. }
  1329. fprintf(stdout,"\n");
  1330. }
  1331. fprintf(stdout,"\nMacroBlock Prediction Error\n");
  1332. for ( j = 0; j < H263Info->lines/H263_MB_SIZE; j++) {
  1333. for ( i = 0; i < H263Info->pels/H263_MB_SIZE; i++) {
  1334. fprintf(stdout, "%4d ", MV[0][j+1][i+1]->min_error);
  1335. }
  1336. fprintf(stdout,"\n");
  1337. }
  1338. */
  1339. /* note: integer pel motion estimation is now based on previous
  1340. reconstructed image, not the previous original image. We have
  1341. found that this works better for some sequences and not worse for
  1342. others. Note that it can not easily be changed back by
  1343. substituting prev_recon->lum with orig_lum in the line above,
  1344. because SAD for zero vector is not re-calculated in the half
  1345. pel search. The half pel search has always been based on the
  1346. previous reconstructed image */
  1347. #ifdef GOB_RATE_CONTROL
  1348. if (H263Info->bit_rate != 0) {
  1349. /* Initialization routine for Rate Control */
  1350. QP_new = sv_H263GOBInitQP((float)H263Info->bit_rate,
  1351. (pic->PB ? H263Info->frame_rate/2 : H263Info->frame_rate),
  1352. pic->QP_mean);
  1353. QP_xmitted = QP_prev = QP_new;
  1354. }
  1355. else {
  1356. QP_new = QP_xmitted = QP_prev = QP; /* Copy the passed value of QP */
  1357. }
  1358. #else
  1359. QP_new = QP_prev = QP; /* Copy the passed value of QP */
  1360. #endif
  1361. dquant = 0;
  1362. /* TRAILER information */
  1363. if (H263Info->extbitstream)
  1364. {
  1365. SvH263RTPInfo_t *RTPInfo=H263Info->RTPInfo;
  1366. RTPInfo->trailer.dwVersion = 0;
  1367. RTPInfo->trailer.dwFlags = 0;
  1368. if(H263Info->syntax_arith_coding)
  1369. RTPInfo->trailer.dwFlags |= RTP_H263_SAC;
  1370. if(H263Info->advanced)
  1371. RTPInfo->trailer.dwFlags |= RTP_H263_AP;
  1372. if(H263Info->pb_frames)
  1373. H263Info->RTPInfo->trailer.dwFlags |= RTP_H263_PB_FRAME;
  1374. RTPInfo->trailer.dwUniqueCode = BI_DECH263DIB;
  1375. RTPInfo->trailer.dwNumberOfPackets = 1;
  1376. RTPInfo->trailer.SourceFormat = (unsigned char)H263Info->source_format;
  1377. RTPInfo->trailer.TR = (unsigned char)pic->TR;
  1378. RTPInfo->trailer.TRB = (unsigned char)pic->TRB;
  1379. RTPInfo->trailer.DBQ = (unsigned char)pic->BQUANT;
  1380. RTPInfo->pre_MB_position = RTPInfo->pre_GOB_position
  1381. = RTPInfo->pic_start_position = RTPInfo->packet_start_position
  1382. = ScBSBitPosition(BSOut); /* HWG - added pre_MB and pre_GOB */
  1383. RTPInfo->packet_id = 0 ;
  1384. RTPInfo->bsinfo[0].dwBitOffset = 0 ;
  1385. RTPInfo->bsinfo[0].Mode = H263_RTP_MODE_A;
  1386. RTPInfo->pic_start_position = ScBSBitPosition(BSOut);
  1387. }
  1388. for ( j = 0; j < H263Info->mb_height; j++)
  1389. {
  1390. /* If a rate control scheme which updates the quantizer for each
  1391. slice is used, it should be added here like this: */
  1392. #ifdef GOB_RATE_CONTROL
  1393. if (H263Info->bit_rate != 0) {
  1394. /* QP updated at the beginning of each row */
  1395. sv_H263AddBitsPicture(H263Info->bits);
  1396. QP_new = sv_H263GOBUpdateQP(abs_mb_num, pic->QP_mean,
  1397. (float)H263Info->bit_rate, H263Info->pels/H263_MB_SIZE,
  1398. H263Info->lines/H263_MB_SIZE, H263Info->bits->total,j,VARgob,
  1399. pic->PB);
  1400. }
  1401. #endif
  1402. /* In other words: you have to set QP_new with some function, not
  1403. necessarily called UpdateQuantizer. Check the source code for
  1404. version 1.5 if you would like to see how it can be done. Read the
  1405. comment in ratectrl.c to see why we removed this scheme. You mau
  1406. also have to add initializer functions before and after the
  1407. encoding of each frame. Special care has to be taken for the intra
  1408. frame if you are designing a system for fixed bitrate and small
  1409. delay.
  1410. If you calculate QP_new here, the rest of the code in the main
  1411. loop will support this.
  1412. If you think the TMN5 scheme worked well enough for you, and the
  1413. simplified scheme is too simple, you can easily add the TMN5 code
  1414. back. However, this will not work with the adaptive PB-frames at
  1415. all! */
  1416. newgob = 0;
  1417. if (j == 0) {
  1418. pic->QUANT = QP_new;
  1419. bits->header += sv_H263CountBitsPicture(H263Info, BSOut, pic);
  1420. QP_xmitted = QP_prev = QP_new;
  1421. }
  1422. else if (pic->use_gobsync && j%pic->use_gobsync == 0) {
  1423. /* insert gob sync */
  1424. bits->header += sv_H263CountBitsSlice(H263Info, BSOut, j,QP_new);
  1425. QP_xmitted = QP_prev = QP_new;
  1426. newgob = 1;
  1427. }
  1428. for ( i = 0; i < H263Info->mb_width; i++) {
  1429. /* Update of dquant, check and correct its limit */
  1430. dquant = QP_new - QP_prev;
  1431. if (dquant != 0 && i != 0 && MV[0][j+1][i+1]->Mode == H263_MODE_INTER4V) {
  1432. /* It is not possible to change the quantizer and at the same
  1433. time use 8x8 vectors. Turning off 8x8 vectors is not
  1434. possible at this stage because the previous macroblock
  1435. encoded assumed this one should use 8x8 vectors. Therefore
  1436. the change of quantizer is postponed until the first MB
  1437. without 8x8 vectors */
  1438. dquant = 0;
  1439. QP_xmitted = QP_prev;
  1440. QuantChangePostponed = 1;
  1441. }
  1442. else {
  1443. QP_xmitted = QP_new;
  1444. QuantChangePostponed = 0;
  1445. }
  1446. if (dquant > 2) { dquant = 2; QP_xmitted = QP_prev + dquant;}
  1447. if (dquant < -2) { dquant = -2; QP_xmitted = QP_prev + dquant;}
  1448. pic->DQUANT = dquant;
  1449. /* modify mode if dquant != 0 (e.g. MODE_INTER -> MODE_INTER_Q) */
  1450. Mode = sv_H263ModifyMode(MV[0][j+1][i+1]->Mode,pic->DQUANT);
  1451. MV[0][j+1][i+1]->Mode = (short)Mode;
  1452. pic->MB = i + j * H263Info->mb_width;
  1453. if (Mode == H263_MODE_INTER || Mode == H263_MODE_INTER_Q || Mode==H263_MODE_INTER4V) {
  1454. /* Predict P-MB */
  1455. if (H263Info->prefilter) {
  1456. H263Info->PreFilterLevel[j][i] = (unsigned char)GetPrefLevel(H263Info, MV, i, j, H263Info->lines, H263Info->pels);
  1457. sv_H263PredictP(H263Info, H263Info->curr_filtd[H263Info->PreFilterLevel[j][i]],prev_recon,prev_ipol,
  1458. i*H263_MB_SIZE,j*H263_MB_SIZE,MV,pic->PB,diff);
  1459. }
  1460. else
  1461. sv_H263PredictP(H263Info, H263Info->curr_image,prev_recon,prev_ipol, i*H263_MB_SIZE,
  1462. j*H263_MB_SIZE,MV,pic->PB,diff);
  1463. }
  1464. else {
  1465. if (H263Info->prefilter) {
  1466. H263Info->PreFilterLevel[j][i] = (unsigned char)GetPrefLevel(H263Info, MV, i, j, H263Info->lines, H263Info->pels);
  1467. FillLumBlock(H263Info, i*H263_MB_SIZE, j*H263_MB_SIZE, H263Info->curr_filtd[H263Info->PreFilterLevel[j][i]], diff);
  1468. FillChromBlock(H263Info, i*H263_MB_SIZE, j*H263_MB_SIZE, H263Info->curr_filtd[H263Info->PreFilterLevel[j][i]], diff);
  1469. }
  1470. else {
  1471. FillLumBlock(H263Info, i*H263_MB_SIZE, j*H263_MB_SIZE, H263Info->curr_image, diff);
  1472. FillChromBlock(H263Info, i*H263_MB_SIZE, j*H263_MB_SIZE, H263Info->curr_image, diff);
  1473. }
  1474. }
  1475. /* P or INTRA Macroblock DCT + Quantization and IQuant+IDCT*/
  1476. sv_H263MBEncode(diff, QP_xmitted, Mode, &CBP, qcoeff_P, H263Info->calc_quality);
  1477. if (CBP == 0 && (Mode == H263_MODE_INTER || Mode == H263_MODE_INTER_Q))
  1478. ZeroMBlock(diff);
  1479. else sv_H263MBDecode(H263Info, qcoeff_P, diff, QP_xmitted, Mode, CBP, H263Info->calc_quality);
  1480. sv_H263MBReconP(H263Info, prev_recon, prev_ipol,diff,
  1481. i*H263_MB_SIZE,j*H263_MB_SIZE,MV,pic->PB,recon_data_P);
  1482. sv_H263Clip(recon_data_P);
  1483. /* Predict B-MB using reconstructed P-MB and prev. recon. image */
  1484. if (pic->PB) {
  1485. if (H263Info->prefilter) {
  1486. H263Info->PreFilterLevel[j][i] = (unsigned char)GetPrefLevel(H263Info, MV, i, j, H263Info->lines, H263Info->pels);
  1487. sv_H263PredictB(H263Info, H263Info->B_filtd[H263Info->PreFilterLevel[j][i]],
  1488. prev_recon, prev_ipol,i*H263_MB_SIZE,
  1489. j*H263_MB_SIZE, MV, recon_data_P, frameskip, pic->TRB,
  1490. diff, Bpred);
  1491. }
  1492. else sv_H263PredictB(H263Info, H263Info->B_image, prev_recon, prev_ipol,
  1493. i*H263_MB_SIZE, j*H263_MB_SIZE,
  1494. MV, recon_data_P, frameskip, pic->TRB,
  1495. diff, Bpred);
  1496. if (QP_xmitted == 0) QP_B = 0; /* (QP = 0 means no quantization) */
  1497. else QP_B = mmax(1,mmin(31,bquant[pic->BQUANT]*QP_xmitted/4));
  1498. sv_H263MBEncode(diff, QP_B, H263_MODE_INTER, &CBPB, qcoeff_B, H263Info->calc_quality);
  1499. if(H263Info->vsnr) { /* reconstruction only for performance measurement */
  1500. if (CBPB) sv_H263MBDecode(H263Info, qcoeff_B, diff, QP_B, H263_MODE_INTER, CBP, H263Info->calc_quality);
  1501. else ZeroMBlock(diff);
  1502. sv_H263MBReconB(H263Info, prev_recon, diff,prev_ipol,
  1503. i*H263_MB_SIZE, j*H263_MB_SIZE,MV,recon_data_P,
  1504. frameskip, pic->TRB, recon_data_B, Bpred);
  1505. sv_H263Clip(recon_data_B);
  1506. }
  1507. /* decide MODB */
  1508. if (CBPB) pic->MODB = H263_PBMODE_CBPB_MVDB;
  1509. else {
  1510. if (MV[5][j+1][i+1]->x == 0 && MV[5][j+1][i+1]->y == 0)
  1511. pic->MODB = H263_PBMODE_NORMAL;
  1512. else pic->MODB = H263_PBMODE_MVDB;
  1513. }
  1514. }
  1515. else
  1516. sv_H263ZeroVec(MV[5][j+1][i+1]); /* Zero out PB deltas */
  1517. /* Entropy coding */
  1518. if ((CBP==0) && (CBPB==0) && (sv_H263EqualVec(MV[0][j+1][i+1],&ZERO)) &&
  1519. (sv_H263EqualVec(MV[5][j+1][i+1],&ZERO)) &&
  1520. (Mode == H263_MODE_INTER || Mode == H263_MODE_INTER_Q)) {
  1521. /* Skipped MB : both CBP and CBPB are zero, 16x16 vector is zero,
  1522. PB delta vector is zero and Mode = MODE_INTER */
  1523. if (Mode == H263_MODE_INTER_Q) {
  1524. /* DQUANT != 0 but not coded anyway */
  1525. QP_xmitted = QP_prev;
  1526. pic->DQUANT = 0;
  1527. Mode = H263_MODE_INTER;
  1528. }
  1529. if (!H263Info->syntax_arith_coding)
  1530. sv_H263CountBitsMB(BSOut, Mode,1,CBP,CBPB,pic,bits);
  1531. else
  1532. sv_H263CountSACBitsMB(H263Info, BSOut, Mode,1,CBP,CBPB,pic,bits);
  1533. }
  1534. else { /* Normal MB */
  1535. if (!H263Info->syntax_arith_coding) { /* VLC */
  1536. sv_H263CountBitsMB(BSOut, Mode,0,CBP,CBPB,pic,bits);
  1537. if (Mode == H263_MODE_INTER || Mode == H263_MODE_INTER_Q) {
  1538. bits->no_inter++;
  1539. sv_H263CountBitsVectors(H263Info, BSOut, MV, bits, i, j, Mode, newgob, pic);
  1540. }
  1541. else if (Mode == H263_MODE_INTER4V) {
  1542. bits->no_inter4v++;
  1543. sv_H263CountBitsVectors(H263Info, BSOut, MV, bits, i, j, Mode, newgob, pic);
  1544. }
  1545. else {
  1546. /* MODE_INTRA or MODE_INTRA_Q */
  1547. bits->no_intra++;
  1548. if (pic->PB)
  1549. sv_H263CountBitsVectors(H263Info, BSOut, MV, bits, i, j, Mode, newgob, pic);
  1550. }
  1551. if (CBP || Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q)
  1552. sv_H263CountBitsCoeff(BSOut, qcoeff_P, Mode, CBP, bits, 64);
  1553. if (CBPB)
  1554. sv_H263CountBitsCoeff(BSOut, qcoeff_B, H263_MODE_INTER, CBPB, bits, 64);
  1555. } /* end VLC */
  1556. else { /* SAC */
  1557. sv_H263CountSACBitsMB(H263Info, BSOut, Mode,0,CBP,CBPB,pic,bits);
  1558. if (Mode == H263_MODE_INTER || Mode == H263_MODE_INTER_Q) {
  1559. bits->no_inter++;
  1560. sv_H263CountSACBitsVectors(H263Info, BSOut, MV, bits, i, j, Mode, newgob, pic);
  1561. }
  1562. else if (Mode == H263_MODE_INTER4V) {
  1563. bits->no_inter4v++;
  1564. sv_H263CountSACBitsVectors(H263Info, BSOut, MV, bits, i, j, Mode, newgob, pic);
  1565. }
  1566. else {
  1567. /* MODE_INTRA or MODE_INTRA_Q */
  1568. bits->no_intra++;
  1569. if (pic->PB)
  1570. sv_H263CountSACBitsVectors(H263Info, BSOut, MV, bits, i, j, Mode, newgob, pic);
  1571. }
  1572. if (CBP || Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q)
  1573. sv_H263CountSACBitsCoeff(H263Info, BSOut, qcoeff_P, Mode, CBP, bits, 64);
  1574. if (CBPB)
  1575. sv_H263CountSACBitsCoeff(H263Info, BSOut, qcoeff_B, H263_MODE_INTER, CBPB, bits, 64);
  1576. } /* end SAC */
  1577. QP_prev = QP_xmitted;
  1578. } /* End Normal Block */
  1579. abs_mb_num++;
  1580. QP_cumulative += QP_xmitted;
  1581. #ifdef PRINTQ
  1582. /* most useful when quantizer changes within a picture */
  1583. if (QuantChangePostponed) fprintf(stdout,"@%2d",QP_xmitted);
  1584. else fprintf(stdout," %2d",QP_xmitted);
  1585. #endif
  1586. if (pic->PB && H263Info->vsnr)
  1587. ReconImage(H263Info, i,j,recon_data_B,H263Info->B_recon);
  1588. ReconImage(H263Info, i,j,recon_data_P,H263Info->curr_recon);
  1589. if ((H263Info->extbitstream&PARAM_FORMATEXT_RTPB)!=0)
  1590. {
  1591. SvH263RTPInfo_t *RTPInfo=H263Info->RTPInfo;
  1592. ScBSPosition_t cur_position = ScBSBitPosition(BSOut);
  1593. /* start a new packet */
  1594. if((cur_position - RTPInfo->packet_start_position) >= H263Info->packetsize)
  1595. {
  1596. SvH263BSInfo_t *RTPBSInfo=&RTPInfo->bsinfo[RTPInfo->packet_id];
  1597. if (RTPInfo->pre_MB_position>RTPBSInfo->dwBitOffset)
  1598. {
  1599. RTPBSInfo++; RTPInfo->packet_id++;
  1600. RTPInfo->trailer.dwNumberOfPackets++;
  1601. }
  1602. RTPBSInfo->dwBitOffset = (unsigned dword)H263Info->RTPInfo->packet_start_position;
  1603. RTPInfo->packet_start_position = RTPInfo->pre_MB_position;
  1604. RTPBSInfo->Mode = H263_RTP_MODE_B;
  1605. RTPBSInfo->Quant = (unsigned char)QP_xmitted;
  1606. RTPBSInfo->GOBN = (unsigned char)j;
  1607. if(Mode==H263_MODE_INTER4V) {
  1608. RTPBSInfo->HMV1 = (char)MV[1][j+1][i+1]->x;
  1609. RTPBSInfo->VMV1 = (char)MV[1][j+1][i+1]->y;
  1610. RTPBSInfo->HMV2 = (char)MV[2][j+1][i+1]->x;
  1611. RTPBSInfo->VMV2 = (char)MV[2][j+1][i+1]->y;
  1612. }
  1613. else {
  1614. RTPBSInfo->HMV1 = (char)MV[0][j+1][i+1]->x;
  1615. RTPBSInfo->VMV1 = (char)MV[0][j+1][i+1]->y;
  1616. RTPBSInfo->HMV2 = 0;
  1617. RTPBSInfo->VMV2 = 0;
  1618. }
  1619. }
  1620. RTPInfo->pre_MB_position = cur_position;
  1621. }
  1622. } /* end of line of blocks - j loop */
  1623. if ((H263Info->extbitstream&PARAM_FORMATEXT_RTPA)!=0)
  1624. {
  1625. SvH263RTPInfo_t *RTPInfo=H263Info->RTPInfo;
  1626. ScBSPosition_t cur_position = ScBSBitPosition(BSOut);
  1627. /* start a new packet */
  1628. if((cur_position - RTPInfo->packet_start_position) >= H263Info->packetsize)
  1629. {
  1630. SvH263BSInfo_t *RTPBSInfo=&RTPInfo->bsinfo[RTPInfo->packet_id];
  1631. if (RTPInfo->pre_GOB_position>RTPBSInfo->dwBitOffset)
  1632. {
  1633. RTPBSInfo++; RTPInfo->packet_id++;
  1634. RTPInfo->trailer.dwNumberOfPackets++;
  1635. }
  1636. RTPInfo->packet_start_position = RTPInfo->pre_GOB_position;
  1637. RTPBSInfo->dwBitOffset = (unsigned dword)RTPInfo->packet_start_position;
  1638. RTPBSInfo->Mode = H263_RTP_MODE_A;
  1639. }
  1640. RTPInfo->pre_GOB_position = cur_position;
  1641. }
  1642. #ifdef PRINTQ
  1643. fprintf(stdout,"\n");
  1644. #endif
  1645. } /* end of image - i loop */
  1646. pic->QP_mean = QP_cumulative/(float)abs_mb_num;
  1647. /* Free memory */
  1648. ScFree(diff);
  1649. ScFree(recon_data_P);
  1650. if (pic->PB) {
  1651. ScFree(recon_data_B);
  1652. ScFree(Bpred);
  1653. }
  1654. if (H263Info->mv_outside_frame)
  1655. {
  1656. ScFree(prev_recon);
  1657. sv_H263FreeImage(pr_edge);
  1658. }
  1659. if(pic->PB) ScFree(qcoeff_B);
  1660. return(SvErrorNone);
  1661. }
  1662. /**********************************************************************
  1663. *
  1664. * Name: CodeOneIntra
  1665. * Description: codes one image intra
  1666. *
  1667. * Input: pointer to image, QP
  1668. *
  1669. * Returns: pointer to reconstructed image
  1670. * Side effects: memory is allocated to recon image
  1671. *
  1672. ***********************************************************************/
  1673. H263_PictImage *sv_H263CodeOneIntra(SvCodecInfo_t *Info, H263_PictImage *curr,
  1674. H263_PictImage *recon, int QP, H263_Bits *bits, H263_Pict *pic)
  1675. {
  1676. SvH263CompressInfo_t *H263Info = Info->h263comp;
  1677. ScBitstream_t *BSOut=Info->BSOut;
  1678. H263_MB_Structure *data = (H263_MB_Structure *)ScAlloc(sizeof(H263_MB_Structure));
  1679. short *qcoeff;
  1680. int Mode = H263_MODE_INTRA;
  1681. int CBP,COD;
  1682. int i,j;
  1683. if ((qcoeff=(short *)ScAlloc(sizeof(short)*384)) == 0) {
  1684. _SlibDebug(_WARN_, ScDebugPrintf(H263Info->dbg, "mb_encode(): Couldn't allocate qcoeff.\n") );
  1685. return(NULL);
  1686. }
  1687. /* TRAILER information */
  1688. if (H263Info->extbitstream)
  1689. {
  1690. /* H263Info->RTPInfo->trailer.dwSrcVersion = 0; */
  1691. H263Info->RTPInfo->trailer.dwVersion = 0;
  1692. H263Info->RTPInfo->trailer.dwFlags = RTP_H263_INTRA_CODED;
  1693. if(H263Info->syntax_arith_coding)
  1694. H263Info->RTPInfo->trailer.dwFlags |= RTP_H263_SAC;
  1695. if(H263Info->advanced)
  1696. H263Info->RTPInfo->trailer.dwFlags |= RTP_H263_AP;
  1697. if(H263Info->pb_frames)
  1698. H263Info->RTPInfo->trailer.dwFlags |= RTP_H263_PB_FRAME;
  1699. H263Info->RTPInfo->trailer.dwUniqueCode = BI_DECH263DIB;
  1700. H263Info->RTPInfo->trailer.dwNumberOfPackets = 1;
  1701. H263Info->RTPInfo->trailer.SourceFormat = (unsigned char)H263Info->source_format;
  1702. H263Info->RTPInfo->trailer.TR = 0;
  1703. H263Info->RTPInfo->trailer.TRB = 0;
  1704. H263Info->RTPInfo->trailer.DBQ = 0;
  1705. H263Info->RTPInfo->pre_GOB_position = H263Info->RTPInfo->pre_MB_position
  1706. = H263Info->RTPInfo->pic_start_position
  1707. = H263Info->RTPInfo->packet_start_position = ScBSBitPosition(BSOut);
  1708. H263Info->RTPInfo->packet_id = 0 ;
  1709. H263Info->RTPInfo->bsinfo[0].dwBitOffset = 0 ;
  1710. H263Info->RTPInfo->bsinfo[0].Mode = H263_RTP_MODE_A;
  1711. }
  1712. sv_H263ZeroBits(bits);
  1713. pic->QUANT = QP;
  1714. bits->header += sv_H263CountBitsPicture(H263Info, BSOut, pic);
  1715. COD = 0; /* Every block is coded in Intra frame */
  1716. for ( j = 0; j < H263Info->mb_height; j++) {
  1717. /* insert sync in *every* slice if use_gobsync is chosen */
  1718. if (pic->use_gobsync && j != 0)
  1719. bits->header += sv_H263CountBitsSlice(H263Info, BSOut, j,QP);
  1720. for ( i = 0; i < H263Info->mb_width; i++) {
  1721. pic->MB = i + j * H263Info->mb_width;
  1722. bits->no_intra++;
  1723. FillLumBlock(H263Info, i*H263_MB_SIZE, j*H263_MB_SIZE, curr, data);
  1724. FillChromBlock(H263Info, i*H263_MB_SIZE, j*H263_MB_SIZE, curr, data);
  1725. sv_H263MBEncode(data, QP, Mode, &CBP, qcoeff, H263Info->calc_quality);
  1726. if (!H263Info->syntax_arith_coding) {
  1727. sv_H263CountBitsMB(BSOut, Mode,COD,CBP,0,pic,bits);
  1728. sv_H263CountBitsCoeff(BSOut, qcoeff, Mode, CBP,bits,64);
  1729. } else {
  1730. sv_H263CountSACBitsMB(H263Info, BSOut, Mode,COD,CBP,0,pic,bits);
  1731. sv_H263CountSACBitsCoeff(H263Info, BSOut, qcoeff, Mode, CBP,bits,64);
  1732. }
  1733. sv_H263MBDecode(H263Info, qcoeff, data, QP, Mode, CBP, H263Info->calc_quality);
  1734. sv_H263Clip(data);
  1735. ReconImage(H263Info, i,j,data,recon);
  1736. if ((H263Info->extbitstream&PARAM_FORMATEXT_RTPB)!=0)
  1737. {
  1738. SvH263RTPInfo_t *RTPInfo=H263Info->RTPInfo;
  1739. ScBSPosition_t cur_position = ScBSBitPosition(BSOut);
  1740. /* start a new packet */
  1741. if((cur_position - RTPInfo->packet_start_position) >= H263Info->packetsize)
  1742. {
  1743. SvH263BSInfo_t *RTPBSInfo=&RTPInfo->bsinfo[RTPInfo->packet_id];
  1744. if (RTPInfo->pre_MB_position>RTPBSInfo->dwBitOffset)
  1745. {
  1746. RTPBSInfo++; RTPInfo->packet_id++;
  1747. RTPInfo->trailer.dwNumberOfPackets++;
  1748. }
  1749. RTPInfo->packet_start_position = RTPInfo->pre_MB_position;
  1750. RTPBSInfo->dwBitOffset = (unsigned dword)RTPInfo->packet_start_position;
  1751. RTPBSInfo->Mode = H263_RTP_MODE_B;
  1752. RTPBSInfo->Quant = (unsigned char)QP;
  1753. RTPBSInfo->GOBN = (unsigned char)j;
  1754. RTPBSInfo->HMV1 = 0;
  1755. RTPBSInfo->VMV1 = 0;
  1756. RTPBSInfo->HMV2 = 0;
  1757. RTPBSInfo->VMV2 = 0;
  1758. }
  1759. RTPInfo->pre_MB_position = cur_position;
  1760. }
  1761. }
  1762. if ((H263Info->extbitstream&PARAM_FORMATEXT_RTPA)!=0)
  1763. {
  1764. SvH263RTPInfo_t *RTPInfo=H263Info->RTPInfo;
  1765. ScBSPosition_t cur_position = ScBSBitPosition(BSOut);
  1766. /* start a new packet */
  1767. if((cur_position - RTPInfo->packet_start_position) >= H263Info->packetsize)
  1768. {
  1769. SvH263BSInfo_t *RTPBSInfo=&RTPInfo->bsinfo[RTPInfo->packet_id];
  1770. if (RTPInfo->pre_GOB_position>RTPBSInfo->dwBitOffset)
  1771. {
  1772. RTPBSInfo++; RTPInfo->packet_id++;
  1773. RTPInfo->trailer.dwNumberOfPackets++;
  1774. }
  1775. RTPInfo->packet_start_position = RTPInfo->pre_GOB_position;
  1776. RTPBSInfo->dwBitOffset = (unsigned dword)RTPInfo->packet_start_position;
  1777. RTPBSInfo->Mode = H263_RTP_MODE_A;
  1778. }
  1779. RTPInfo->pre_GOB_position = cur_position;
  1780. }
  1781. }
  1782. pic->QP_mean = (float)QP;
  1783. ScFree(data);
  1784. ScFree(qcoeff);
  1785. return recon;
  1786. }
  1787. /**********************************************************************
  1788. *
  1789. * Name: MB_Encode
  1790. * Description: DCT and quantization of Macroblocks
  1791. *
  1792. * Input: MB data struct, mquant (1-31, 0 = no quant),
  1793. * MB info struct
  1794. * Returns: Pointer to quantized coefficients
  1795. * Side effects:
  1796. *
  1797. **********************************************************************/
  1798. static int sv_H263MBEncode(H263_MB_Structure *mb_orig, int QP, int I, int *CBP,
  1799. short *qcoeff, unsigned dword quality)
  1800. {
  1801. int i, k, l, row, blkid;
  1802. short fblock[64];
  1803. short *coeff_ind;
  1804. coeff_ind = qcoeff;
  1805. *CBP = 0;
  1806. blkid = 0;
  1807. for (k=0;k<16;k+=8) {
  1808. for (l=0;l<16;l+=8) {
  1809. for (i=k,row=0;row<64;i++,row+=8)
  1810. memcpy(fblock + row, &(mb_orig->lum[i][l]), 16) ;
  1811. #if 1
  1812. /* DCT in ZZ order */
  1813. if(quality > 40){
  1814. if(sv_H263DCT(fblock,coeff_ind,QP,I))
  1815. if(sv_H263Quant(coeff_ind,QP,I)) *CBP |= (32 >> blkid);
  1816. }
  1817. else {
  1818. if(sv_H263ZoneDCT(fblock,coeff_ind,QP,I))
  1819. if(sv_H263Quant(coeff_ind,QP,I)) *CBP |= (32 >> blkid);
  1820. }
  1821. #else
  1822. switch (DCT_method) {
  1823. case(H263_DCT16COEFF):
  1824. svH263Dct16coeff(fblock,coeff_ind);
  1825. break;
  1826. case(H263_DCT4BY4):
  1827. svH263Dct4by4(fblock,coeff_ind);
  1828. break;
  1829. }
  1830. if(sv_H263Quant(coeff_ind,QP,I) != 0) *CBP |= (32 >> blkid);
  1831. #endif
  1832. coeff_ind += 64;
  1833. blkid++;
  1834. }
  1835. }
  1836. #if 1
  1837. /* DCT in ZZ order */
  1838. if(quality > 40){
  1839. if(sv_H263DCT(&(mb_orig->Cb[0][0]),coeff_ind,QP,I))
  1840. if(sv_H263Quant(coeff_ind,QP,I)) *CBP |= (32 >> blkid);
  1841. }
  1842. else {
  1843. if(sv_H263ZoneDCT(&(mb_orig->Cb[0][0]),coeff_ind,QP,I))
  1844. if(sv_H263Quant(coeff_ind,QP,I)) *CBP |= (32 >> blkid);
  1845. }
  1846. #else
  1847. memcpy(&fblock[0], &(mb_orig->Cb[0][0]), 128);
  1848. switch (DCT_method) {
  1849. case(H263_DCT16COEFF):
  1850. svH263Dct16coeff(fblock,coeff_ind);
  1851. break;
  1852. case(H263_DCT4BY4):
  1853. svH263Dct4by4(fblock,coeff_ind);
  1854. break;
  1855. }
  1856. if(sv_H263Quant(coeff_ind,QP,I) != 0) *CBP |= (32 >> blkid);
  1857. #endif
  1858. coeff_ind += 64;
  1859. blkid++;
  1860. #if 1
  1861. /* DCT in ZZ order */
  1862. if(quality > 40){
  1863. if(sv_H263DCT( &(mb_orig->Cr[0][0]),coeff_ind,QP,I))
  1864. if(sv_H263Quant(coeff_ind,QP,I) != 0) *CBP |= (32 >> blkid);
  1865. }
  1866. else {
  1867. if(sv_H263ZoneDCT( &(mb_orig->Cr[0][0]),coeff_ind,QP,I))
  1868. if(sv_H263Quant(coeff_ind,QP,I) != 0) *CBP |= (32 >> blkid);
  1869. }
  1870. #else
  1871. memcpy(&fblock[0], &(mb_orig->Cr[0][0]),128);
  1872. switch (DCT_method) {
  1873. case(H263_DCT16COEFF):
  1874. svH263Dct16coeff(fblock,coeff_ind);
  1875. break;
  1876. case(H263_DCT4BY4):
  1877. svH263Dct4by4(fblock,coeff_ind);
  1878. break;
  1879. }
  1880. if(sv_H263Quant(coeff_ind,QP,I) != 0) *CBP |= (32 >> blkid);
  1881. #endif
  1882. return 1;
  1883. }
  1884. /**********************************************************************
  1885. *
  1886. * Name: MB_Decode
  1887. * Description: Reconstruction of quantized DCT-coded Macroblocks
  1888. *
  1889. * Input: Quantized coefficients, MB data
  1890. * QP (1-31, 0 = no quant), MB info block
  1891. * Returns: int (just 0)
  1892. * Side effects:
  1893. *
  1894. **********************************************************************/
  1895. /* de-quantization */
  1896. static short sv_H263MBDecode(SvH263CompressInfo_t *H263Info, short *qcoeff,
  1897. H263_MB_Structure *mb_recon, int QP, int I, int CBP,
  1898. unsigned dword quality)
  1899. {
  1900. int i, k, l, row, blkid;
  1901. short *iblock;
  1902. short *qcoeff_ind;
  1903. short *rcoeff, *rcoeff_ind;
  1904. if(H263Info->refidct) {
  1905. if ((rcoeff = (short *)ScAlloc(sizeof(short)*64)) == NULL) {
  1906. _SlibDebug(_WARN_, ScDebugPrintf(H263Info->dbg, "sv_H263MBDecode() Could not allocate space for rcoeff\n") );
  1907. return(0);
  1908. }
  1909. if ((iblock = (short *)ScAlloc(sizeof(short)*64)) == NULL) {
  1910. _SlibDebug(_WARN_, ScDebugPrintf(H263Info->dbg, "sv_H263MBDecode() Could not allocate space for iblock\n") );
  1911. return(0);
  1912. }
  1913. }
  1914. /* Zero data into lum-Cr-Cb, For control purposes */
  1915. memset(&(mb_recon->lum[0][0]), 0 , 768) ;
  1916. qcoeff_ind = qcoeff;
  1917. blkid = 0;
  1918. for (k=0;k<16;k+=8) {
  1919. for (l=0;l<16;l+=8) {
  1920. if((CBP & (32 >> blkid)) || I == H263_MODE_INTRA || I == H263_MODE_INTRA_Q)
  1921. {
  1922. if (H263Info->refidct) {
  1923. rcoeff_ind = rcoeff;
  1924. sv_H263Dequant(qcoeff_ind,rcoeff_ind,QP,I);
  1925. sv_H263idctref(rcoeff_ind,iblock);
  1926. for (i=k,row=0;row<64;i++,row+=8)
  1927. memcpy(&(mb_recon->lum[i][l]), iblock+row, 16) ;
  1928. }
  1929. else /* IDCT with ZZ and quantization */
  1930. {
  1931. if(quality > 40)
  1932. sv_H263IDCT(qcoeff_ind,&(mb_recon->lum[k][l]),QP,I,16);
  1933. else
  1934. sv_H263ZoneIDCT(qcoeff_ind,&(mb_recon->lum[k][l]),QP,I,16);
  1935. }
  1936. }
  1937. else {
  1938. for (i=k,row=0;row<64;i++,row+=8)
  1939. memset(&(mb_recon->lum[i][l]), 0, 16) ;
  1940. }
  1941. qcoeff_ind += 64;
  1942. blkid++;
  1943. }
  1944. }
  1945. if((CBP & (32 >> blkid)) || I == H263_MODE_INTRA || I == H263_MODE_INTRA_Q)
  1946. {
  1947. if (H263Info->refidct){
  1948. sv_H263Dequant(qcoeff_ind,rcoeff_ind,QP,I);
  1949. sv_H263idctref(rcoeff_ind,&(mb_recon->Cb[0][0]));
  1950. }
  1951. else /* IDCT with ZZ and quantization */
  1952. {
  1953. if(quality > 40)
  1954. sv_H263IDCT(qcoeff_ind,&(mb_recon->Cb[0][0]),QP,I,8);
  1955. else
  1956. sv_H263ZoneIDCT(qcoeff_ind,&(mb_recon->Cb[0][0]),QP,I,8);
  1957. }
  1958. }
  1959. blkid++ ;
  1960. qcoeff_ind += 64;
  1961. if((CBP & (32 >> blkid)) || I == H263_MODE_INTRA || I == H263_MODE_INTRA_Q) {
  1962. if (H263Info->refidct) {
  1963. sv_H263Dequant(qcoeff_ind,rcoeff_ind,QP,I);
  1964. sv_H263idctref(rcoeff_ind,&(mb_recon->Cr[0][0]));
  1965. }
  1966. else /* IDCT with ZZ and quantization */
  1967. {
  1968. if(quality > 40)
  1969. sv_H263IDCT(qcoeff_ind,&(mb_recon->Cr[0][0]),QP,I,8);
  1970. else
  1971. sv_H263ZoneIDCT(qcoeff_ind,&(mb_recon->Cr[0][0]),QP,I,8);
  1972. }
  1973. }
  1974. if (H263Info->refidct){
  1975. ScFree(rcoeff);
  1976. ScFree(iblock);
  1977. }
  1978. return 0;
  1979. }
  1980. /**********************************************************************
  1981. *
  1982. * Name: FillLumBlock
  1983. * Description: Fills the luminance of one block of PictImage
  1984. *
  1985. * Input: Position, pointer to PictImage, array to fill
  1986. * Returns:
  1987. * Side effects: fills array
  1988. *
  1989. ***********************************************************************/
  1990. #ifndef USE_C
  1991. void FillLumBlock(SvH263CompressInfo_t *H263Info,
  1992. int x, int y, H263_PictImage *image, H263_MB_Structure *data)
  1993. {
  1994. sv_H263FilLumBlk_S((image->lum + x + y*H263Info->pels), &(data->lum[0][0]), H263Info->pels);
  1995. return;
  1996. }
  1997. #else
  1998. void FillLumBlock(SvH263CompressInfo_t *H263Info,
  1999. int x, int y, H263_PictImage *image, H263_MB_Structure *data)
  2000. {
  2001. int n, m, off;
  2002. register short *ptnb;
  2003. unsigned char *ptna ;
  2004. ptna = image->lum + x + y*H263Info->pels ;
  2005. ptnb = &(data->lum[0][0]) ;
  2006. off = H263Info->pels - H263_MB_SIZE;
  2007. for (n = 0; n < H263_MB_SIZE; n++){
  2008. for (m = 0; m < H263_MB_SIZE; m++)
  2009. *(ptnb++) = (short) *(ptna++) ;
  2010. ptna += off;
  2011. }
  2012. return;
  2013. }
  2014. #endif
  2015. /**********************************************************************
  2016. *
  2017. * Name: FillChromBlock
  2018. * Description: Fills the chrominance of one block of PictImage
  2019. *
  2020. * Input: Position, pointer to PictImage, array to fill
  2021. * Returns:
  2022. * Side effects: fills array
  2023. * 128 subtracted from each
  2024. *
  2025. ***********************************************************************/
  2026. #ifndef USE_C
  2027. void FillChromBlock(SvH263CompressInfo_t *H263Info, int x_curr, int y_curr, H263_PictImage *image,
  2028. H263_MB_Structure *data)
  2029. {
  2030. int off;
  2031. off = (x_curr>>1) + (y_curr>>1)* H263Info->cpels;
  2032. sv_H263FilChmBlk_S(image->Cr + off, &(data->Cr[0][0]),
  2033. image->Cb + off, &(data->Cb[0][0]), H263Info->cpels) ;
  2034. return;
  2035. }
  2036. #else
  2037. void FillChromBlock(SvH263CompressInfo_t *H263Info, int x_curr, int y_curr, H263_PictImage *image,
  2038. H263_MB_Structure *data)
  2039. {
  2040. register int m, n;
  2041. int off;
  2042. short *ptnb, *ptnd;
  2043. unsigned char *ptna, *ptnc;
  2044. off = (x_curr>>1) + (y_curr>>1)* H263Info->cpels;
  2045. ptna = image->Cr + off; ptnb = &(data->Cr[0][0]) ;
  2046. ptnc = image->Cb + off; ptnd = &(data->Cb[0][0]) ;
  2047. off = H263Info->cpels - 8 ;
  2048. for (n = 0; n < 8; n++){
  2049. for (m = 0; m < 8; m++) {
  2050. *(ptnb++) = (short)*(ptna++);
  2051. *(ptnd++) = (short)*(ptnc++);
  2052. }
  2053. ptna += off;
  2054. ptnc += off;
  2055. }
  2056. return;
  2057. }
  2058. #endif
  2059. /**********************************************************************
  2060. *
  2061. * Name: FillLumPredBlock
  2062. * Description: Fills the luminance of one block of PredImage
  2063. *
  2064. * Input: Position, pointer to PredImage, array to fill
  2065. * Returns:
  2066. * Side effects: fills array
  2067. *
  2068. ***********************************************************************/
  2069. #if 1
  2070. void FillLumPredBlock(SvH263CompressInfo_t *H263Info, int x, int y, PredImage *image,
  2071. H263_MB_Structure *data)
  2072. {
  2073. int n;
  2074. register short *ptna, *ptnb;
  2075. ptna = image->lum + x + y*H263Info->pels ;
  2076. ptnb = &(data->lum[0][0]) ;
  2077. for (n = 0; n < H263_MB_SIZE; n++){
  2078. memcpy(ptnb,ptna,32);
  2079. ptnb+=16 ; ptna += H263Info->pels ;
  2080. }
  2081. return;
  2082. }
  2083. #else
  2084. void FillLumPredBlock(SvH263CompressInfo_t *H263Info, int x, int y, PredImage *image, H263_MB_Structure *data)
  2085. {
  2086. int n;
  2087. register int m;
  2088. for (n = 0; n < H263_MB_SIZE; n++)
  2089. for (m = 0; m < H263_MB_SIZE; m++)
  2090. data->lum[n][m] = *(image->lum + x+m + (y+n)*H263Info->pels);
  2091. return;
  2092. }
  2093. #endif
  2094. /**********************************************************************
  2095. *
  2096. * Name: FillChromPredBlock
  2097. * Description: Fills the chrominance of one block of PictImage
  2098. *
  2099. * Input: Position, pointer to PictImage, array to fill
  2100. * Returns:
  2101. * Side effects: fills array
  2102. * 128 subtracted from each
  2103. *
  2104. * Added by Nuno on 06/27/96 to support filtering of the prediction error
  2105. ***********************************************************************/
  2106. #if 1
  2107. void FillChromPredBlock(SvH263CompressInfo_t *H263Info, int x_curr, int y_curr, PredImage *image,
  2108. H263_MB_Structure *data)
  2109. {
  2110. int n, off;
  2111. register short *ptna, *ptnb, *ptnc, *ptnd ;
  2112. off = (x_curr>>1) + (y_curr>>1)* H263Info->cpels;
  2113. ptna = image->Cr + off; ptnb = &(data->Cr[0][0]) ;
  2114. ptnc = image->Cb + off; ptnd = &(data->Cb[0][0]) ;
  2115. for (n = 0; n < 8; n++){
  2116. memcpy(ptnb,ptna,16);
  2117. ptnb+=8 ; ptna += H263Info->cpels ;
  2118. memcpy(ptnd,ptnc,16);
  2119. ptnd+=8 ; ptnc += H263Info->cpels ;
  2120. }
  2121. return;
  2122. }
  2123. #else
  2124. void FillChromPredBlock(SvH263CompressInfo_t *H263Info,
  2125. int x_curr, int y_curr, PredImage *image,
  2126. H263_MB_Structure *data)
  2127. {
  2128. int n;
  2129. register int m;
  2130. int x, y;
  2131. x = x_curr>>1;
  2132. y = y_curr>>1;
  2133. for (n = 0; n < (H263_MB_SIZE>>1); n++)
  2134. for (m = 0; m < (H263_MB_SIZE>>1); m++) {
  2135. data->Cr[n][m] = *(image->Cr +x+m + (y+n)*H263Info->cpels);
  2136. data->Cb[n][m] = *(image->Cb +x+m + (y+n)*H263Info->cpels);
  2137. }
  2138. return;
  2139. }
  2140. #endif
  2141. /**********************************************************************
  2142. *
  2143. * Name: ZeroMBlock
  2144. * Description: Fills one MB with Zeros
  2145. *
  2146. * Input: MB_Structure to zero out
  2147. * Returns:
  2148. * Side effects:
  2149. *
  2150. ***********************************************************************/
  2151. #if 1
  2152. void ZeroMBlock(H263_MB_Structure *data)
  2153. {
  2154. memset(&(data->lum[0][0]), 0, 768) ;
  2155. return;
  2156. }
  2157. #else
  2158. void ZeroMBlock(H263_MB_Structure *data)
  2159. {
  2160. int n;
  2161. register int m;
  2162. for (n = 0; n < H263_MB_SIZE; n++)
  2163. for (m = 0; m < H263_MB_SIZE; m++)
  2164. data->lum[n][m] = 0;
  2165. for (n = 0; n < (H263_MB_SIZE>>1); n++)
  2166. for (m = 0; m < (H263_MB_SIZE>>1); m++) {
  2167. data->Cr[n][m] = 0;
  2168. data->Cb[n][m] = 0;
  2169. }
  2170. return;
  2171. }
  2172. #endif
  2173. /**********************************************************************
  2174. *
  2175. * Name: ReconImage
  2176. * Description: Puts together reconstructed image
  2177. *
  2178. * Input: position of curr block, reconstructed
  2179. * macroblock, pointer to recontructed image
  2180. * Returns:
  2181. * Side effects:
  2182. *
  2183. ***********************************************************************/
  2184. #ifndef USE_C
  2185. void ReconImage(SvH263CompressInfo_t *H263Info, int i, int j, H263_MB_Structure *data, H263_PictImage *recon)
  2186. {
  2187. unsigned char *ptna, *ptnb;
  2188. int x_curr, y_curr;
  2189. x_curr = i * H263_MB_SIZE;
  2190. y_curr = j * H263_MB_SIZE;
  2191. /* Fill in luminance data */
  2192. ptna = recon->lum + x_curr + y_curr*H263Info->pels;
  2193. sv_H263ItoC16A_S(&(data->lum[0][0]), ptna, H263Info->pels) ;
  2194. /* Fill in chrominance data */
  2195. ptna = recon->Cr + (x_curr>>1) + (y_curr>>1)*H263Info->cpels;
  2196. ptnb = recon->Cb + (x_curr>>1) + (y_curr>>1)*H263Info->cpels;
  2197. sv_H263ItoC8B_S(&(data->Cr[0][0]), ptna, &(data->Cb[0][0]), ptnb, H263Info->pels/2) ;
  2198. return;
  2199. }
  2200. #else
  2201. void ReconImage(SvH263CompressInfo_t *H263Info, int i, int j, H263_MB_Structure *data, H263_PictImage *recon)
  2202. {
  2203. int n;
  2204. register int m;
  2205. int x_curr, y_curr;
  2206. x_curr = i * H263_MB_SIZE;
  2207. y_curr = j * H263_MB_SIZE;
  2208. /* Fill in luminance data */
  2209. for (n = 0; n < H263_MB_SIZE; n++)
  2210. for (m= 0; m < H263_MB_SIZE; m++) {
  2211. *(recon->lum + x_curr+m + (y_curr+n)*H263Info->pels) = (unsigned char)data->lum[n][m];
  2212. }
  2213. /* Fill in chrominance data */
  2214. for (n = 0; n < H263_MB_SIZE>>1; n++)
  2215. for (m = 0; m < H263_MB_SIZE>>1; m++) {
  2216. *(recon->Cr + (x_curr>>1)+m + ((y_curr>>1)+n)*H263Info->cpels) = (unsigned char) data->Cr[n][m];
  2217. *(recon->Cb + (x_curr>>1)+m + ((y_curr>>1)+n)*H263Info->cpels) = (unsigned char) data->Cb[n][m];
  2218. }
  2219. return;
  2220. }
  2221. #endif
  2222. /**********************************************************************
  2223. *
  2224. * Name: ReconPredImage
  2225. * Description: Puts together prediction error image
  2226. *
  2227. * Input: position of curr block, reconstructed
  2228. * macroblock, pointer to recontructed image
  2229. * Returns:
  2230. * Side effects:
  2231. *
  2232. * Added by Nuno on 06/27/96 to support filtering of the prediction error
  2233. ***********************************************************************/
  2234. #if 1
  2235. void ReconPredImage(SvH263CompressInfo_t *H263Info, int i, int j, H263_MB_Structure *data, PredImage *recon)
  2236. {
  2237. int n;
  2238. int x_curr, y_curr;
  2239. register short *pta, *ptb, *ptc, *ptd;
  2240. x_curr = i * H263_MB_SIZE;
  2241. y_curr = j * H263_MB_SIZE;
  2242. /* Fill in luminance data */
  2243. pta = recon->lum + x_curr + y_curr * H263Info->pels ;
  2244. ptb = &(data->lum[0][0]) ;
  2245. for (n = 0; n < H263_MB_SIZE; n++){
  2246. memcpy(ptb,pta,32);
  2247. ptb+=H263_MB_SIZE ; pta += H263Info->pels ;
  2248. }
  2249. /* Fill in chrominance data */
  2250. pta = recon->Cr + (x_curr>>1) + (y_curr>>1)*H263Info->cpels ;
  2251. ptb = recon->Cb + (x_curr>>1) + (y_curr>>1)*H263Info->cpels ;
  2252. ptc = &(data->Cr[0][0]) ;
  2253. ptd = &(data->Cb[0][0]) ;
  2254. for (n = 0; n < H263_MB_SIZE>>1; n++){
  2255. memcpy(ptc,pta,16);
  2256. memcpy(ptd,ptb,16);
  2257. pta += H263Info->cpels; ptc+=H263_MB_SIZE;
  2258. ptb += H263Info->cpels; ptd+=H263_MB_SIZE;
  2259. }
  2260. return;
  2261. }
  2262. #else
  2263. void ReconPredImage(SvH263CompressInfo_t *H263Info, int i, int j, H263_MB_Structure *data, PredImage *recon)
  2264. {
  2265. int n;
  2266. register int m;
  2267. int x_curr, y_curr;
  2268. x_curr = i * H263_MB_SIZE;
  2269. y_curr = j * H263_MB_SIZE;
  2270. /* Fill in luminance data */
  2271. for (n = 0; n < H263_MB_SIZE; n++)
  2272. for (m= 0; m < H263_MB_SIZE; m++) {
  2273. *(recon->lum + x_curr+ m + (y_curr + n)*H263Info->pels) = data->lum[n][m];
  2274. }
  2275. /* Fill in chrominance data */
  2276. for (n = 0; n < H263_MB_SIZE>>1; n++)
  2277. for (m = 0; m < H263_MB_SIZE>>1; m++) {
  2278. *(recon->Cr + (x_curr>>1)+m + ((y_curr>>1)+n)*H263Info->cpels) = data->Cr[n][m];
  2279. *(recon->Cb + (x_curr>>1)+m + ((y_curr>>1)+n)*H263Info->cpels) = data->Cb[n][m];
  2280. }
  2281. return;
  2282. }
  2283. #endif
  2284. /**********************************************************************
  2285. *
  2286. * Name: InterpolateImage
  2287. * Description: Interpolates a complete image for easier half
  2288. * pel prediction
  2289. *
  2290. * Input: pointer to image structure
  2291. * Returns: pointer to interpolated image
  2292. * Side effects: allocates memory to interpolated image
  2293. *
  2294. ***********************************************************************/
  2295. void InterpolateImage(unsigned char *image, unsigned char *ipol_image,
  2296. int width, int height)
  2297. {
  2298. register unsigned char *ii, *oo, *ij, *oi;
  2299. int i,j,w2,w4,w1;
  2300. unsigned char tmp1;
  2301. #ifdef USE_C
  2302. unsigned char tmp2, tmp3;
  2303. #endif
  2304. ii = ipol_image;
  2305. oo = image;
  2306. w2 = (width<<1);
  2307. w4 = (width<<2);
  2308. w1 = width - 1;
  2309. /* main image */
  2310. #ifndef USE_C
  2311. for (j = 0; j < height-1; j++) {
  2312. sv_H263Intrpolt_S(oo, ii, oo + width, ii + w2, width) ;
  2313. ii += w4 ;
  2314. oo += width;
  2315. }
  2316. #else
  2317. for (j = 0; j < height-1; j++) {
  2318. oi = oo; ij = ii;
  2319. for (i = 0; i < width-1; i++, ij+=2, oi++) {
  2320. *(ij) = (tmp1 = *oi);
  2321. *(ij + 1) = (tmp1 + (tmp2 = *(oi + 1)) + 1)>>1;
  2322. *(ij + w2) = (tmp1 + (tmp3 = *(oi + width)) + 1)>>1;
  2323. *(ij + 1 + w2) = (tmp1 + tmp2 + tmp3 + *(oi+1+width) + 2)>>2;
  2324. }
  2325. /* last pels on each line */
  2326. *(ii+ w2 -2) = *(ii+ w2 -1) = *(oo + w1);
  2327. *(ii+ w4 -2) = *(ii+ w4 -1) = (*(oo+w1) + *(oo+width+w1) + 1)>>1;
  2328. ii += w4 ;
  2329. oo += width;
  2330. }
  2331. #endif
  2332. /* last lines */
  2333. ij = ii; oi = oo;
  2334. for (i=0; i < width-1; i++, ij+=2, oi++) {
  2335. *ij = *(ij+ w2) = (tmp1 = *oi );
  2336. *(ij + 1) = *(ij+ w2 + 1) = (tmp1 + *(oi + 1) + 1)>>1;
  2337. }
  2338. /* bottom right corner pels */
  2339. *(ii+w2-2)= *(ii+w2-1) = *(ii+w4-2) = *(ii+w4-1) = *(oo+w1);
  2340. return ;
  2341. }
  2342. /**********************************************************************
  2343. *
  2344. * Name: MotionEstimatePicture
  2345. * Description: Finds integer and half pel motion estimation
  2346. * and chooses 8x8 or 16x16
  2347. *
  2348. * Input: current image, previous image, interpolated
  2349. * reconstructed previous image, seek_dist,
  2350. * motion vector array
  2351. * Returns:
  2352. * Side effects: allocates memory for MV structure
  2353. *
  2354. ***********************************************************************/
  2355. void MotionEstimatePicture(SvH263CompressInfo_t *H263Info, unsigned char *curr, unsigned char *prev,
  2356. unsigned char *prev_ipol, int seek_dist,
  2357. H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2], int gobsync)
  2358. {
  2359. int i,j,k;
  2360. int pmv0,pmv1,xoff,yoff;
  2361. int sad8 = INT_MAX, sad16, sad0;
  2362. int newgob;
  2363. H263_MotionVector *f0,*f1,*f2,*f3,*f4;
  2364. int VARmb;
  2365. void (*MotionEst_Func)(SvH263CompressInfo_t *H263Info,
  2366. unsigned char *curr, unsigned char *prev,
  2367. int x_curr, int y_curr, int xoff, int yoff, int seek_dist,
  2368. H263_MotionVector *MV[6][H263_MBR+1][H263_MBC+2], int *SAD_0);
  2369. switch(H263Info->ME_method) {
  2370. default:
  2371. case(H263_FULL_SEARCH):
  2372. MotionEst_Func = sv_H263MotionEstimation;
  2373. break;
  2374. case(H263_TWO_LEVELS_7_1):
  2375. MotionEst_Func = sv_H263ME_2levels_7_1;
  2376. break;
  2377. case(H263_TWO_LEVELS_421_1):
  2378. MotionEst_Func = sv_H263ME_2levels_421_1;
  2379. break;
  2380. case(H263_TWO_LEVELS_7_polint):
  2381. MotionEst_Func = sv_H263ME_2levels_7_polint;
  2382. break;
  2383. case(H263_TWO_LEVELS_7_pihp):
  2384. MotionEst_Func = sv_H263ME_2levels_7_pihp;
  2385. break;
  2386. }
  2387. /* Do motion estimation and store result in array */
  2388. for ( j = 0; j < H263Info->mb_height; j++) {
  2389. newgob = 0;
  2390. if (gobsync && j%gobsync == 0) newgob = 1;
  2391. H263Info->VARgob[j] = 0;
  2392. for ( i = 0; i < H263Info->mb_width; i++) {
  2393. /* Integer pel search */
  2394. f0 = MV[0][j+1][i+1];
  2395. f1 = MV[1][j+1][i+1];
  2396. f2 = MV[2][j+1][i+1];
  2397. f3 = MV[3][j+1][i+1];
  2398. f4 = MV[4][j+1][i+1];
  2399. /* NBNB need to use newgob properly as last parameter */
  2400. sv_H263FindPMV(MV,i+1,j+1,&pmv0,&pmv1,0,newgob,0);
  2401. /* Here the PMV's are found using integer motion vectors */
  2402. /* (NB should add explanation for this )*/
  2403. if (H263Info->long_vectors) {
  2404. xoff = pmv0/2; /* always divisable by two */
  2405. yoff = pmv1/2;
  2406. }
  2407. else xoff = yoff = 0;
  2408. MotionEst_Func(H263Info, curr, prev, i*H263_MB_SIZE, j*H263_MB_SIZE,
  2409. xoff, yoff, seek_dist, MV, &sad0);
  2410. sad16 = f0->min_error;
  2411. if (H263Info->advanced)
  2412. sad8 = f1->min_error + f2->min_error + f3->min_error + f4->min_error;
  2413. f0->Mode = (short)sv_H263ChooseMode(H263Info, curr,i*H263_MB_SIZE,j*H263_MB_SIZE,
  2414. mmin(sad8,sad16), &VARmb);
  2415. H263Info->VARgob[j] += VARmb;
  2416. /* Half pel search */
  2417. if (f0->Mode != H263_MODE_INTRA) {
  2418. if(H263Info->advanced) {
  2419. #ifndef USE_C
  2420. /* performance Half-Pel Motion Search on 8x8 blocks */
  2421. sv_H263AdvHalfPel(H263Info, i*H263_MB_SIZE,j*H263_MB_SIZE,f0,f1,f2,f3,f4,prev_ipol,curr,16,0);
  2422. #else
  2423. sv_H263FindHalfPel(H263Info, i*H263_MB_SIZE,j*H263_MB_SIZE,f0, prev_ipol, curr, 16, 0);
  2424. sv_H263FindHalfPel(H263Info, i*H263_MB_SIZE,j*H263_MB_SIZE,f1, prev_ipol, curr, 8, 0);
  2425. sv_H263FindHalfPel(H263Info, i*H263_MB_SIZE,j*H263_MB_SIZE,f2, prev_ipol, curr, 8, 1);
  2426. sv_H263FindHalfPel(H263Info, i*H263_MB_SIZE,j*H263_MB_SIZE,f3, prev_ipol, curr, 8, 2);
  2427. sv_H263FindHalfPel(H263Info, i*H263_MB_SIZE,j*H263_MB_SIZE,f4, prev_ipol, curr, 8, 3);
  2428. #endif
  2429. sad16 = f0->min_error;
  2430. sad8 = f1->min_error +f2->min_error +f3->min_error +f4->min_error;
  2431. sad8 += H263_PREF_16_VEC;
  2432. /* Choose Zero Vector, 8x8 or 16x16 vectors */
  2433. if (sad0 < sad8 && sad0 < sad16) {
  2434. f0->x = f0->y = 0;
  2435. f0->x_half = f0->y_half = 0;
  2436. }
  2437. else { if (sad8 < sad16) f0->Mode = H263_MODE_INTER4V; }
  2438. }
  2439. else {
  2440. /* performance Half-Pel Motion Search on 16 x 16 blocks */
  2441. sv_H263FindHalfPel(H263Info, i*H263_MB_SIZE,j*H263_MB_SIZE,f0, prev_ipol, curr, 16, 0);
  2442. sad16 = f0->min_error;
  2443. /* Choose Zero Vector or 16x16 vectors */
  2444. if (sad0 < sad16) {
  2445. f0->x = f0->y = 0;
  2446. f0->x_half = f0->y_half = 0;
  2447. }
  2448. }
  2449. }
  2450. else for (k = 0; k < 5; k++) sv_H263ZeroVec(MV[k][j+1][i+1]);
  2451. }
  2452. H263Info->VARgob[j] /= (H263Info->mb_width);
  2453. }
  2454. #ifdef PRINTMV
  2455. fprintf(stdout,"Motion estimation\n");
  2456. fprintf(stdout,"16x16 vectors:\n");
  2457. for ( j = 0; j < H263Info->mb_height; j++) {
  2458. for ( i = 0; i < H263Info->pels/H263_MB_SIZE; i++) {
  2459. if (MV[0][j+1][i+1]->Mode != H263_MODE_INTRA)
  2460. fprintf(stdout," %3d%3d",
  2461. 2*MV[0][j+1][i+1]->x + MV[0][j+1][i+1]->x_half,
  2462. 2*MV[0][j+1][i+1]->y + MV[0][j+1][i+1]->y_half);
  2463. else
  2464. fprintf(stdout," . . ");
  2465. }
  2466. fprintf(stdout,"\n");
  2467. }
  2468. if (H263Info->advanced) {
  2469. fprintf(stdout,"8x8 vectors:\n");
  2470. for (k = 1; k < 5; k++) {
  2471. fprintf(stdout,"Block: %d\n", k-1);
  2472. for ( j = 0; j < H263Info->lines/H263_MB_SIZE; j++) {
  2473. for ( i = 0; i < H263Info->pels/H263_MB_SIZE; i++) {
  2474. if (MV[0][j+1][i+1]->Mode != H263_MODE_INTRA)
  2475. fprintf(stdout," %3d%3d",
  2476. 2*MV[k][j+1][i+1]->x + MV[k][j+1][i+1]->x_half,
  2477. 2*MV[k][j+1][i+1]->y + MV[k][j+1][i+1]->y_half);
  2478. else
  2479. fprintf(stdout," . . ");
  2480. }
  2481. fprintf(stdout,"\n");
  2482. }
  2483. }
  2484. }
  2485. #endif
  2486. return;
  2487. }
  2488. /**********************************************************************
  2489. *
  2490. * Name: MakeEdgeImage
  2491. * Description: Copies edge pels for use with unrestricted
  2492. * motion vector mode
  2493. *
  2494. * Input: pointer to source image, destination image
  2495. * width, height, edge
  2496. * Returns:
  2497. * Side effects:
  2498. *
  2499. ***********************************************************************/
  2500. void MakeEdgeImage(unsigned char *src, unsigned char *dst, int width,
  2501. int height, int edge)
  2502. {
  2503. int j;
  2504. unsigned char *p1,*p2,*p3,*p4;
  2505. unsigned char *o1,*o2,*o3,*o4;
  2506. int off, off1, off2;
  2507. unsigned char t1, t2, t3, t4 ;
  2508. /* center image */
  2509. p1 = dst;
  2510. o1 = src;
  2511. off = (edge<<1);
  2512. for (j = 0; j < height;j++) {
  2513. memcpy(p1,o1,width);
  2514. p1 += width + off;
  2515. o1 += width;
  2516. }
  2517. /* left and right edges */
  2518. p1 = dst-1;
  2519. o1 = src;
  2520. off1 = width + 1 ; off2 = width - 1 ;
  2521. for (j = 0; j < height;j++) {
  2522. t1 = *o1 ; t2 = *(o1 + off2);
  2523. memset(p1-edge+1,t1,edge);
  2524. memset(p1+off1,t2,edge);
  2525. p1 += width + off;
  2526. o1 += width;
  2527. }
  2528. /* top and bottom edges */
  2529. p1 = dst;
  2530. p2 = dst + (width + (edge<<1))*(height-1);
  2531. o1 = src;
  2532. o2 = src + width*(height-1);
  2533. off = width + (edge<<1) ;
  2534. for (j = 0; j < edge;j++) {
  2535. p1 -= off;
  2536. p2 += off;
  2537. memcpy(p1,o1,width);
  2538. memcpy(p2,o2,width);
  2539. }
  2540. /* corners */
  2541. p1 = dst - (width+(edge<<1)) - 1;
  2542. p2 = p1 + width + 1;
  2543. p3 = dst + (width+(edge<<1))*(height)-1;
  2544. p4 = p3 + width + 1;
  2545. o1 = src;
  2546. o2 = o1 + width - 1;
  2547. o3 = src + width*(height-1);
  2548. o4 = o3 + width - 1;
  2549. t1 = *o1; t2 = *o2; t3 = *o3; t4 = *o4;
  2550. for (j = 0; j < edge; j++) {
  2551. memset(p1-edge+1,t1,edge);
  2552. memset(p2,t2,edge);
  2553. memset(p3-edge+1,t3,edge);
  2554. memset(p4,t4,edge);
  2555. p1 -= off;
  2556. p2 -= off;
  2557. p3 += off;
  2558. p4 += off;
  2559. }
  2560. }
  2561. /**********************************************************************
  2562. *
  2563. * Name: Clip
  2564. * Description: clips recontructed data 0-255
  2565. *
  2566. * Input: pointer to recon. data structure
  2567. * Side effects: data structure clipped
  2568. *
  2569. ***********************************************************************/
  2570. void sv_H263Clip(H263_MB_Structure *data)
  2571. {
  2572. #ifdef USE_C
  2573. int m,n;
  2574. for (n = 0; n < 16; n++) {
  2575. for (m = 0; m < 16; m++) {
  2576. data->lum[n][m] = mmin(255,mmax(0,data->lum[n][m]));
  2577. }
  2578. }
  2579. for (n = 0; n < 8; n++) {
  2580. for (m = 0; m < 8; m++) {
  2581. data->Cr[n][m] = mmin(255,mmax(0,data->Cr[n][m]));
  2582. data->Cb[n][m] = mmin(255,mmax(0,data->Cb[n][m]));
  2583. }
  2584. }
  2585. #else
  2586. sv_H263Clp_S(&(data->lum[0][0]), 16);
  2587. sv_H263Clp_S(&(data->Cr[0][0]), 4);
  2588. sv_H263Clp_S(&(data->Cb[0][0]), 4);
  2589. #endif
  2590. }
  2591. #ifdef _SLIBDEBUG_
  2592. /**********************************************************************
  2593. *
  2594. * Description: calculate SNR
  2595. *
  2596. ***********************************************************************/
  2597. static int frame_id=0;
  2598. static float avg_SNR_l=0.0F, avg_SNR_Cr=0.0F, avg_SNR_Cb=0.0F;
  2599. void ComputeSNR(SvH263CompressInfo_t *H263Info,
  2600. H263_PictImage *im1, H263_PictImage *im2,
  2601. int lines, int pels)
  2602. {
  2603. int n;
  2604. register int m;
  2605. int quad, quad_Cr, quad_Cb, diff;
  2606. float SNR_l, SNR_Cr, SNR_Cb;
  2607. #if _WRITE_
  2608. if (!frame_id) DEBUGIMG = ScFileOpenForWriting("DEBUG.IMG", TRUE);
  2609. #endif
  2610. quad = 0;
  2611. quad_Cr = quad_Cb = 0;
  2612. /* Luminance */
  2613. quad = 0;
  2614. for (n = 0; n < lines; n++)
  2615. for (m = 0; m < pels; m++) {
  2616. diff = *(im1->lum + m + n*pels) - *(im2->lum + m + n*pels);
  2617. quad += diff * diff;
  2618. }
  2619. SNR_l = (float)quad/(float)(pels*lines);
  2620. if (SNR_l) {
  2621. SNR_l = (float)(255*255) / SNR_l;
  2622. SNR_l = (float)(10 * log10(SNR_l));
  2623. }
  2624. else SNR_l = (float) 99.99;
  2625. ScDebugPrintf(H263Info->dbg, "\n Frame %d : SNR of LUM = %f",frame_id++,SNR_l);
  2626. /* Chrominance */
  2627. for (n = 0; n < lines/2; n++)
  2628. for (m = 0; m < pels/2; m++) {
  2629. quad_Cr += (*(im1->Cr+m + n*pels/2) - *(im2->Cr + m + n*pels/2)) *
  2630. (*(im1->Cr+m + n*pels/2) - *(im2->Cr + m + n*pels/2));
  2631. quad_Cb += (*(im1->Cb+m + n*pels/2) - *(im2->Cb + m + n*pels/2)) *
  2632. (*(im1->Cb+m + n*pels/2) - *(im2->Cb + m + n*pels/2));
  2633. }
  2634. SNR_Cr = (float)quad_Cr/(float)(pels*lines/4);
  2635. if (SNR_Cr) {
  2636. SNR_Cr = (float)(255*255) / SNR_Cr;
  2637. SNR_Cr = (float)(10 * log10(SNR_Cr));
  2638. }
  2639. else SNR_Cr = (float) 99.99;
  2640. SNR_Cb = (float)quad_Cb/(float)(pels*lines/4);
  2641. if (SNR_Cb) {
  2642. SNR_Cb = (float)(255*255) / SNR_Cb;
  2643. SNR_Cb = (float)(10 * log10(SNR_Cb));
  2644. }
  2645. else SNR_Cb = (float)99.99;
  2646. ScDebugPrintf(H263Info->dbg, "SNR of Cr = %f Cb = %f \n",SNR_Cr, SNR_Cb);
  2647. avg_SNR_l += SNR_l;
  2648. avg_SNR_Cb += SNR_Cb;
  2649. avg_SNR_Cr += SNR_Cr;
  2650. ScDebugPrintf(H263Info->dbg, "AVG_SNR: lum %f Cr %f Cb %f\n",
  2651. avg_SNR_l/(float)frame_id,
  2652. avg_SNR_Cr/(float)frame_id,
  2653. avg_SNR_Cb/(float)frame_id);
  2654. #if _WRITE_
  2655. ScFileWrite(DEBUGIMG, im1->lum, pels*lines);
  2656. ScFileWrite(DEBUGIMG, im1->Cb, pels*lines/4);
  2657. ScFileWrite(DEBUGIMG, im1->Cr, pels*lines/4);
  2658. #endif
  2659. return;
  2660. }
  2661. #endif /* _SLIBDEBUG_ */
  2662.