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.

1422 lines
49 KiB

  1. /* File: sv_h263_getpic.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 <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "sv_h263.h"
  25. #include "sv_intrn.h"
  26. #include "SC_err.h"
  27. #include "proto.h"
  28. #ifdef _SLIBDEBUG_
  29. #include "sc_debug.h"
  30. #define _DEBUG_ 0 /* detailed debuging statements */
  31. #define _VERBOSE_ 1 /* show progress */
  32. #define _VERIFY_ 0 /* verify correct operation */
  33. #define _WARN_ 1 /* warnings about strange behavior */
  34. #endif
  35. /* private prototypes*/
  36. static void getMBs(SvH263DecompressInfo_t *H263Info, ScBitstream_t *BSIn);
  37. static void clearblock(SvH263DecompressInfo_t *H263Info, int comp);
  38. static int motion_decode(SvH263DecompressInfo_t *H263Info, int vec, int pmv);
  39. static int find_pmv(SvH263DecompressInfo_t *H263Info, int x, int y, int block, int comp);
  40. static void addblock(SvH263DecompressInfo_t *H263Info, int comp, int bx, int by, int addflag);
  41. static void reconblock_b(SvH263DecompressInfo_t *H263Info, int comp, int bx, int by,
  42. int mode, int bdx, int bdy);
  43. static void find_bidir_limits(int vec, int *start, int*stop, int nhv);
  44. static void find_bidir_chroma_limits(int vec, int *start, int*stop);
  45. static void make_edge_image(unsigned char *src, unsigned char *dst, int width, int height, int edge);
  46. void interpolate_image(unsigned char *in, unsigned char *out, int width, int height);
  47. static void IDCT_add(int comp,int bx,int by,int addflag) ;
  48. static int H263_roundtab[16]= {0,0,0,1,1,1,1,1,1,1,1,1,1,1,2,2};
  49. static int H263_codtab[2]= {0,1};
  50. static int H263_mcbpctab[21] = {0,16,32,48,1,17,33,49,2,18,34,50,3,19,35,51,4,20,36,52,255};
  51. static int H263_mcbpc_intratab[9] = {3,19,35,51,4,20,36,52,255};
  52. static int H263_modb_tab[3] = {0, 1, 2};
  53. static int H263_ycbpb_tab[2] = {0, 1};
  54. static int H263_uvcbpb_tab[2] = {0, 1};
  55. static int H263_cbpytab[16] = {15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
  56. static int H263_cbpy_intratab[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  57. static int H263_dquanttab[4] = {1,0,3,4};
  58. static int H263_mvdtab[64] = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  59. static int H263_intradctab[254] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,255,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254};
  60. static int H263_cumf_COD[3]={16383, 6849, 0};
  61. static int H263_cumf_MCBPC[22]={16383, 4105, 3088, 2367, 1988, 1621, 1612, 1609, 1608, 496, 353, 195, 77, 22, 17, 12, 5, 4, 3, 2, 1, 0};
  62. static int H263_cumf_MCBPC_intra[10]={16383, 7410, 6549, 5188, 442, 182, 181, 141, 1, 0};
  63. static int H263_cumf_MODB[4]={16383, 6062, 2130, 0};
  64. static int H263_cumf_YCBPB[3]={16383,6062,0};
  65. static int H263_cumf_UVCBPB[3]={16383,491,0};
  66. static int H263_cumf_CBPY[17]={16383, 14481, 13869, 13196, 12568, 11931, 11185, 10814, 9796, 9150, 8781, 7933, 6860, 6116, 4873, 3538, 0};
  67. static int H263_cumf_CBPY_intra[17]={16383, 13619, 13211, 12933, 12562, 12395, 11913, 11783, 11004, 10782, 10689, 9928, 9353, 8945, 8407, 7795, 0};
  68. static int H263_cumf_DQUANT[5]={16383, 12287, 8192, 4095, 0};
  69. static int H263_cumf_MVD[65]={16383, 16380, 16369, 16365, 16361, 16357, 16350, 16343, 16339, 16333, 16326, 16318, 16311, 16306, 16298, 16291, 16283, 16272, 16261, 16249, 16235, 16222, 16207, 16175, 16141, 16094, 16044, 15936, 15764, 15463, 14956, 13924, 11491, 4621, 2264, 1315, 854, 583, 420, 326, 273, 229, 196, 166, 148, 137, 123, 114, 101, 91, 82, 76, 66, 59, 53, 46, 36, 30, 26, 24, 18, 14, 10, 5, 0};
  70. static int H263_cumf_INTRADC[255]={16383, 16380, 16379, 16378, 16377, 16376, 16370, 16361, 16360, 16359, 16358, 16357, 16356, 16355, 16343, 16238, 16237, 16236, 16230, 16221, 16220, 16205, 16190, 16169, 16151, 16130, 16109, 16094, 16070, 16037, 16007, 15962, 15938, 15899, 15854, 15815, 15788, 15743, 15689, 15656, 15617, 15560, 15473, 15404, 15296, 15178, 15106, 14992, 14868, 14738, 14593, 14438, 14283, 14169, 14064, 14004, 13914, 13824, 13752, 13671, 13590, 13515, 13458, 13380, 13305, 13230, 13143, 13025, 12935, 12878, 12794, 12743, 12656, 12596, 12521, 12443, 12359, 12278, 12200, 12131, 12047, 12002, 11948, 11891, 11828, 11744, 11663, 11588, 11495, 11402, 11288, 11204, 11126, 11039, 10961, 10883, 10787, 10679, 10583, 10481, 10360, 10227, 10113, 9961, 9828, 9717, 9584, 9485, 9324, 9112, 9019, 8908, 8766, 8584, 8426, 8211, 7920, 7663, 7406, 7152, 6904, 6677, 6453, 6265, 6101, 5904, 5716, 5489, 5307, 5056, 4850, 4569, 4284, 3966, 3712, 3518, 3342, 3206, 3048, 2909, 2773, 2668, 2596, 2512, 2370, 2295, 2232, 2166, 2103, 2022, 1956, 1887, 1830, 1803, 1770, 1728, 1674, 1635, 1599, 1557, 1500, 1482, 1434, 1389, 1356, 1317, 1284, 1245, 1200, 1179, 1140, 1110, 1092, 1062, 1044, 1035, 1014, 1008, 993, 981, 954, 936, 912, 894, 876, 864, 849, 828, 816, 801, 792, 777, 756, 732, 690, 660, 642, 615, 597, 576, 555, 522, 489, 459, 435, 411, 405, 396, 387, 375, 360, 354, 345, 344, 329, 314, 293, 278, 251, 236, 230, 224, 215, 214, 208, 199, 193, 184, 178, 169, 154, 127, 100, 94, 73, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 20, 19, 18, 17, 16, 15, 9, 0};
  71. #define clearblk(comp) memset(H263Info->block[comp], 0, sizeof(int [66]));
  72. /* decode one frame or field picture */
  73. SvStatus_t sv_H263GetPicture(SvCodecInfo_t *Info)
  74. {
  75. int i;
  76. unsigned char *tmp;
  77. SvH263DecompressInfo_t *H263Info = Info->h263dcmp;
  78. ScBitstream_t *BSIn=Info->BSIn;
  79. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "sv_H263GetPicture()\n") );
  80. for (i=0; i<3; i++) {
  81. tmp = H263Info->oldrefframe[i];
  82. H263Info->oldrefframe[i] = H263Info->refframe[i];
  83. H263Info->refframe[i] = tmp;
  84. H263Info->newframe[i] = H263Info->refframe[i];
  85. }
  86. if (H263Info->mv_outside_frame && H263Info->framenum > 0) {
  87. make_edge_image(H263Info->oldrefframe[0],H263Info->edgeframe[0],H263Info->coded_picture_width,
  88. H263Info->coded_picture_height,32);
  89. make_edge_image(H263Info->oldrefframe[1],H263Info->edgeframe[1],H263Info->chrom_width, H263Info->chrom_height,16);
  90. make_edge_image(H263Info->oldrefframe[2],H263Info->edgeframe[2],H263Info->chrom_width, H263Info->chrom_height,16);
  91. }
  92. getMBs(H263Info, BSIn);
  93. if (H263Info->pb_frame) {
  94. if (H263Info->expand && H263Info->outtype == H263_T_X11) {
  95. interpolate_image(H263Info->bframe[0], H263Info->exnewframe[0],
  96. H263Info->coded_picture_width, H263Info->coded_picture_height);
  97. interpolate_image(H263Info->bframe[1], H263Info->exnewframe[1], H263Info->chrom_width, H263Info->chrom_height);
  98. interpolate_image(H263Info->bframe[2], H263Info->exnewframe[2], H263Info->chrom_width, H263Info->chrom_height);
  99. /* svH263Storeframe(H263Info->exnewframe, *framenum); */
  100. }
  101. else
  102. /* svH263Storeframe(H263Info->bframe,*framenum); */
  103. H263Info->framenum += H263Info->pb_frame;
  104. }
  105. if (H263Info->expand && H263Info->outtype == H263_T_X11) {
  106. interpolate_image(H263Info->newframe[0], H263Info->exnewframe[0],
  107. H263Info->coded_picture_width, H263Info->coded_picture_height);
  108. interpolate_image(H263Info->newframe[1], H263Info->exnewframe[1], H263Info->chrom_width, H263Info->chrom_height);
  109. interpolate_image(H263Info->newframe[2], H263Info->exnewframe[2], H263Info->chrom_width, H263Info->chrom_height);
  110. /* svH263Storeframe(H263Info->exnewframe, *framenum); */
  111. }
  112. else {
  113. /* svH263Storeframe(H263Info->newframe,*framenum); */
  114. }
  115. return(SvErrorNone);
  116. }
  117. /* decode all macroblocks of the current picture */
  118. #ifdef CAMERAMOTION
  119. extern void setwintitle(char *);
  120. #define MOVERIGHT (1<<0)
  121. #define MOVELEFT (1<<1)
  122. #define MOVEUP (1<<2)
  123. #define MOVEDOWN (1<<3)
  124. static int left_x,left_y;
  125. static int top_x,top_y;
  126. static int right_x,right_y;
  127. static int bottom_x,bottom_y;
  128. static int center_x,center_y;
  129. static int strip_width = 3;
  130. static int framewidth;
  131. static int frameheight;
  132. static int startoff;
  133. static int filtermean,havecut;
  134. static int loopstart = 0;
  135. static int loopend;
  136. static int scenenumber = 1;
  137. static void initmotion(int width,int height)
  138. {
  139. left_x = left_y = 0;
  140. right_x = right_y = 0;
  141. bottom_x = bottom_y = 0;
  142. top_x = top_y = 0;
  143. center_x = center_y = 0;
  144. framewidth = width;
  145. frameheight = height;
  146. startoff = svH263Tellbits();
  147. }
  148. static void analyzemotion(int xpos,int ypos,int mvx,int mvy)
  149. {
  150. /* Count Motion Vectors for 4 overlapping strips at the edges */
  151. /* and the center area between them */
  152. if (xpos < strip_width) { /* Left strip */
  153. left_x += mvx;
  154. left_y += mvy;
  155. }
  156. if (xpos >= (framewidth - strip_width)) { /* Right strip */
  157. right_x += mvx;
  158. right_y += mvy;
  159. }
  160. if (ypos < strip_width) { /* Top strip */
  161. top_x += mvx;
  162. top_y += mvy;
  163. }
  164. if (ypos >= (frameheight - strip_width)) { /* Bottom strip */
  165. bottom_x += mvx;
  166. bottom_y += mvy;
  167. }
  168. if (xpos >= strip_width && xpos < (framewidth - strip_width)
  169. && ypos >= strip_width && ypos < (frameheight - strip_width)) {
  170. center_x += mvx;
  171. center_y += mvy;
  172. }
  173. }
  174. static int last_movement = 0;
  175. static int keepdirection = 15;
  176. static int movestartframe = 0;
  177. static int moveendframe;
  178. static int movesumx;
  179. static int movesumy;
  180. static void summarizemotion(int frameno)
  181. {
  182. int threshold = 8;
  183. int striparea;
  184. int sx,sy;
  185. char title[128];
  186. int cur_movement = 0;
  187. int framesize;
  188. title[0] = 0;
  189. framesize = svH263Tellbits() - startoff;
  190. if (frameno < 1) {
  191. filtermean = framesize/2;
  192. havecut = 1;
  193. } else {
  194. filtermean = (4*filtermean + framesize)/5;
  195. }
  196. if (!havecut && framesize > 1.8 * filtermean && frameno > 5) {
  197. if (!last_movement) {
  198. printf("dump %d\n",movestartframe + ((moveendframe-movestartframe)/2));
  199. }
  200. printf("mark %d %d %d %d\n",scenenumber,loopstart,frameno-1,filtermean);
  201. printf("shot %d %d %d\n",scenenumber,loopstart,frameno-1);
  202. movestartframe = frameno;
  203. loopstart = frameno;
  204. havecut = 1;
  205. scenenumber++;
  206. } else {
  207. havecut = 0;
  208. }
  209. striparea = strip_width * framewidth;
  210. top_x = (top_x*10)/striparea;
  211. top_y = (top_y*10)/striparea;
  212. bottom_x = (bottom_x*10)/striparea;
  213. bottom_y = (bottom_y*10)/striparea;
  214. striparea = strip_width * frameheight;
  215. left_x = (left_x*10)/striparea;
  216. left_y = (left_y*10)/striparea;
  217. right_x = (right_x*10)/striparea;
  218. right_y = (right_y*10)/striparea;
  219. striparea = (framewidth - 2 * strip_width) * (frameheight - 2 * strip_width);
  220. center_x = (center_x*10)/striparea;
  221. center_y = (center_y*10)/striparea;
  222. /* Horizontal left motion */
  223. if (top_x < -threshold && bottom_x < -threshold
  224. && right_x < -threshold && left_x < -threshold) {
  225. strcat(title,"Left ");
  226. cur_movement |= MOVELEFT;
  227. }
  228. /* Horizontal right motion */
  229. if (top_x > threshold && bottom_x > threshold
  230. && left_x > threshold && right_x > threshold) {
  231. strcat(title,"Right ");
  232. cur_movement |= MOVERIGHT;
  233. }
  234. /* Vertical up motion */
  235. if (left_y < -threshold && right_y < -threshold
  236. && top_y < -threshold && bottom_y < -threshold) {
  237. strcat(title,"Up");
  238. cur_movement |= MOVEUP;
  239. }
  240. /* Vertical down motion */
  241. if (left_y > threshold && right_y > threshold
  242. && bottom_y > threshold && top_y > threshold) {
  243. strcat(title,"Down ");
  244. cur_movement |= MOVEDOWN;
  245. }
  246. if (last_movement) {
  247. movesumx += (top_x + bottom_x + right_x +left_x)/4;
  248. movesumy += (top_y + bottom_y + right_y +left_y)/4;
  249. }
  250. if (!cur_movement != !last_movement) { /* Possible movement change */
  251. if (--keepdirection < 0) { /* Seen move several times */
  252. keepdirection = 10;
  253. if (last_movement) { /* Have been in a move */
  254. int movelength;
  255. int i,images,fno;
  256. movesumx = movesumx/(16*framewidth);
  257. movesumy = movesumy/(16*frameheight);
  258. movelength = moveendframe - movestartframe;
  259. if (movelength > 25) {
  260. images = ((abs(movesumx) + abs(movesumy)) / 5) + 1;
  261. fno = movestartframe + movelength/(2*images);
  262. for (i = 0; i < images; i++) {
  263. printf("dump %d\n",fno);
  264. fno += movelength/images;
  265. }
  266. }
  267. } else { /* Last part was fixed */
  268. if (havecut) {
  269. /* Do nothing, was already handled */
  270. } else {
  271. printf("dump %d\n",movestartframe + ((moveendframe-movestartframe)/2));
  272. movestartframe = frameno;
  273. }
  274. }
  275. movesumx = 0;
  276. movesumy = 0;
  277. movestartframe = moveendframe+1;
  278. last_movement = cur_movement;
  279. if (H263Info->outtype == H263_T_X11) {
  280. if (title[0] != 0) {
  281. setwintitle(title);
  282. } else {
  283. setwintitle("fixed");
  284. }
  285. }
  286. }
  287. } else {
  288. moveendframe = frameno;
  289. }
  290. fflush(stdout);
  291. }
  292. #endif /* CAMERAMOTION */
  293. static void getMBs(SvH263DecompressInfo_t *H263Info, ScBitstream_t *BSIn)
  294. {
  295. int comp;
  296. int MBA, MBAmax;
  297. int bx, by;
  298. int COD=0,MCBPC, CBPY, CBP=0, CBPB=0, MODB=0, Mode=0, DQUANT;
  299. int COD_index, CBPY_index, MODB_index, DQUANT_index, MCBPC_index;
  300. int INTRADC_index, YCBPB_index, UVCBPB_index, mvdbx_index, mvdby_index;
  301. int mvx, mvy, mvy_index, mvx_index, pmv0, pmv1, xpos, ypos, gob, i,k;
  302. int mvdbx=0, mvdby=0, pmvdbx, pmvdby, gfid, YCBPB, UVCBPB, gobheader_read;
  303. int startmv,stopmv,offset,bsize,last_done=0,pCBP=0,pCBPB=0,pCOD=0;
  304. int DQ_tab[4] = {-1,-2,1,2};
  305. register int *bp;
  306. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs()\n") );
  307. /* number of macroblocks per picture */
  308. MBAmax = H263Info->mb_width*H263Info->mb_height;
  309. MBA = 0; /* macroblock address */
  310. H263Info->newgob = 0;
  311. /* mark MV's above the picture */
  312. for (i = 1; i < H263Info->mb_width+1; i++) {
  313. for (k = 0; k < 5; k++) {
  314. H263Info->MV[0][k][0][i] = H263_NO_VEC;
  315. H263Info->MV[1][k][0][i] = H263_NO_VEC;
  316. }
  317. H263Info->modemap[0][i] = H263_MODE_INTRA;
  318. }
  319. /* zero MV's on the sides of the picture */
  320. for (i = 0; i < H263Info->mb_height+1; i++) {
  321. for (k = 0; k < 5; k++) {
  322. H263Info->MV[0][k][i][0] = 0;
  323. H263Info->MV[1][k][i][0] = 0;
  324. H263Info->MV[0][k][i][H263Info->mb_width+1] = 0;
  325. H263Info->MV[1][k][i][H263Info->mb_width+1] = 0;
  326. }
  327. H263Info->modemap[i][0] = H263_MODE_INTRA;
  328. H263Info->modemap[i][H263Info->mb_width+1] = H263_MODE_INTRA;
  329. }
  330. H263Info->fault = 0;
  331. gobheader_read = 0;
  332. for (;;) {
  333. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "frame %d, MB %d, bytepos=%ld\n",
  334. H263Info->framenum,MBA,ScBSBytePosition(BSIn)) );
  335. /* if (MBA>15) return; */
  336. resync:
  337. /* This version of the decoder does not resync on every possible
  338. error, and it does not do all possible error checks. It is not
  339. difficult to make it much more error robust, but I do not think
  340. it is necessary to include this in the freely available
  341. version. */
  342. if (H263Info->fault) {
  343. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() fault\n") );
  344. printf("Warning: A Fault Condition Has Occurred - Resyncing \n");
  345. /* look for startcode */
  346. if (sv_H263StartCode(BSIn)!=SvErrorNone) /* sync on new startcode */
  347. {
  348. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() couldn't get startcode\n") );
  349. return;
  350. }
  351. H263Info->fault = 0;
  352. }
  353. if (!(ScBSPeekBits(BSIn, 22)>>6)) { /* startcode */
  354. if (sv_H263StartCode(BSIn)!=SvErrorNone) /* sync on new startcode */
  355. {
  356. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() couldn't get startcode\n") );
  357. return;
  358. }
  359. /* in case of byte aligned start code, ie. PSTUF, GSTUF or ESTUF
  360. is used */
  361. if (ScBSPeekBits(BSIn, 22) == (32|H263_SE_CODE)) { /* end of sequence */
  362. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() end of sequence\n") );
  363. if (!(H263Info->syntax_arith_coding && MBA < MBAmax))
  364. {
  365. return;
  366. }
  367. }
  368. else if ((ScBSPeekBits(BSIn, 22) == H263_PSC<<5) ) { /* new picture */
  369. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() new picture code, bytepos=%ld\n",
  370. ScBSBytePosition(BSIn)) );
  371. if (!(H263Info->syntax_arith_coding && MBA < MBAmax)) {
  372. #ifdef CAMERAMOTION
  373. goto summarize_motion;
  374. #else
  375. return;
  376. #endif
  377. }
  378. }
  379. else {
  380. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() new group code, bytepos=%ld\n",
  381. ScBSBytePosition(BSIn)) );
  382. if (!(H263Info->syntax_arith_coding && MBA%H263Info->mb_width)) {
  383. if (H263Info->syntax_arith_coding) { /* SAC hack to finish GOBs which */
  384. gob = (int)(ScBSPeekBits(BSIn, 22) & 31); /* end with MBs coded with no bits */
  385. if (gob * H263Info->mb_width != MBA)
  386. {
  387. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() goto finish_gob\n") );
  388. goto finish_gob;
  389. }
  390. }
  391. if (sv_H263GetHeader(H263Info, BSIn, &gob)!=SvErrorNone)
  392. {
  393. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() GetHeader failed\n") );
  394. return;
  395. }
  396. /* gob--; */
  397. if (gob > H263Info->mb_height) {
  398. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() GN out of range: gob=%d\n", gob) );
  399. return;
  400. }
  401. /* GFID is not allowed to change unless PTYPE in picture header
  402. changes */
  403. gfid = (int)ScBSGetBits(BSIn, 2);
  404. /* NB: in error-prone environments the decoder can use this
  405. value to determine whether a picture header where the PTYPE
  406. has changed, has been lost */
  407. H263Info->quant = (int)ScBSGetBits(BSIn, 5);
  408. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() GQUANT=%d gob=%d\n",
  409. H263Info->quant, gob) );
  410. xpos = 0;
  411. ypos = gob;
  412. MBA = ypos * H263Info->mb_width;
  413. H263Info->newgob = 1;
  414. gobheader_read = 1;
  415. if (H263Info->syntax_arith_coding)
  416. sv_H263SACDecoderReset(BSIn); /* init. arithmetic decoder buffer after gob */
  417. }
  418. }
  419. }
  420. finish_gob: /* SAC specific label */
  421. if (!gobheader_read) {
  422. xpos = MBA%H263Info->mb_width;
  423. ypos = MBA/H263Info->mb_width;
  424. if (xpos == 0 && ypos > 0)
  425. H263Info->newgob = 0;
  426. }
  427. else
  428. gobheader_read = 0;
  429. if (MBA>=MBAmax)
  430. {
  431. #ifdef CAMERAMOTION
  432. goto summarize_motion;
  433. #else
  434. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() done MBA=%d\n", MBA) );
  435. return; /* all macroblocks decoded */
  436. #endif
  437. }
  438. read_cod:
  439. if (H263Info->syntax_arith_coding) {
  440. if (H263Info->pict_type == H263_PCT_INTER) {
  441. COD_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_COD);
  442. COD = H263_codtab[COD_index];
  443. _SlibDebug(_DEBUG_, ScDebugPrintf(H263Info->dbg,
  444. "getMBs() Arithmetic Decoding, COD Index: %d COD: %d \n", COD_index, COD) );
  445. }
  446. else
  447. COD = 0; /* COD not used in I-pictures, set to zero */
  448. }
  449. else {
  450. if (H263Info->pict_type == H263_PCT_INTER)
  451. COD = (int)ScBSPeekBits(BSIn, 1);
  452. else
  453. COD = 0; /* Intra picture -> not skipped */
  454. }
  455. if (!COD) { /* COD == 0 --> not skipped */
  456. if (H263Info->syntax_arith_coding) {
  457. if (H263Info->pict_type == H263_PCT_INTER) {
  458. MCBPC_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_MCBPC);
  459. MCBPC = H263_mcbpctab[MCBPC_index];
  460. _SlibDebug(_DEBUG_, ScDebugPrintf(H263Info->dbg,
  461. "getMBs() (arith/inter) MCBPC Index: %ld MCBPC: %d \n",MCBPC_index, MCBPC) );
  462. }
  463. else {
  464. int lastMCBPC=MCBPC;
  465. MCBPC_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_MCBPC_intra);
  466. MCBPC = H263_mcbpc_intratab[MCBPC_index];
  467. _SlibDebug(_DEBUG_, ScDebugPrintf(H263Info->dbg,
  468. "getMBs() (arith) MCBPC Index: %ld MCBPC: %d \n",MCBPC_index, MCBPC) );
  469. if (MCBPC==255 && lastMCBPC==255)
  470. return;
  471. }
  472. }
  473. else {
  474. if (H263Info->pict_type == H263_PCT_INTER)
  475. ScBSSkipBit(BSIn); /* flush COD bit */
  476. if (H263Info->pict_type == H263_PCT_INTRA)
  477. MCBPC = sv_H263GetMCBPCintra(H263Info, BSIn);
  478. else
  479. MCBPC = sv_H263GetMCBPC(H263Info, BSIn);
  480. _SlibDebug(_DEBUG_, ScDebugPrintf(H263Info->dbg,
  481. "getMBs() MCBPC: %d \n", MCBPC) );
  482. }
  483. if (H263Info->fault) goto resync;
  484. if (MCBPC == 255) { /* stuffing */
  485. goto read_cod; /* read next COD without advancing MB count */
  486. }
  487. else { /* normal MB data */
  488. Mode = MCBPC & 7;
  489. /* MODB and CBPB */
  490. if (H263Info->pb_frame) {
  491. CBPB = 0;
  492. if (H263Info->syntax_arith_coding) {
  493. MODB_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_MODB);
  494. MODB = H263_modb_tab[MODB_index];
  495. }
  496. else
  497. MODB = sv_H263GetMODB(H263Info, BSIn);
  498. _SlibDebug(_DEBUG_, ScDebugPrintf(H263Info->dbg, "getMBs() MODB: %d\n", MODB) );
  499. if (MODB == H263_PBMODE_CBPB_MVDB) {
  500. if (H263Info->syntax_arith_coding) {
  501. for(i=0; i<4; i++) {
  502. YCBPB_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_YCBPB);
  503. YCBPB = H263_ycbpb_tab[YCBPB_index];
  504. CBPB |= (YCBPB << (6-1-i));
  505. }
  506. for(i=4; i<6; i++) {
  507. UVCBPB_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_UVCBPB);
  508. UVCBPB = H263_uvcbpb_tab[UVCBPB_index];
  509. CBPB |= (UVCBPB << (6-1-i));
  510. }
  511. }
  512. else
  513. CBPB = (int)ScBSGetBits(BSIn, 6);
  514. _SlibDebug(_DEBUG_, ScDebugPrintf(H263Info->dbg, "getMBs() CBPB = %d\n",CBPB) );
  515. }
  516. }
  517. if (H263Info->syntax_arith_coding) {
  518. if (Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q) { /* Intra */
  519. CBPY_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_CBPY_intra);
  520. CBPY = H263_cbpy_intratab[CBPY_index];
  521. }
  522. else {
  523. CBPY_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_CBPY);
  524. CBPY = H263_cbpytab[CBPY_index];
  525. }
  526. _SlibDebug(_DEBUG_, ScDebugPrintf(H263Info->dbg, "getMBs() CBPY Index: %d CBPY %d \n",CBPY_index, CBPY) );
  527. }
  528. else
  529. {
  530. CBPY = sv_H263GetCBPY(H263Info, BSIn);
  531. _SlibDebug(_DEBUG_, ScDebugPrintf(H263Info->dbg, "getMBs() CBPY %d \n",CBPY) );
  532. }
  533. /* Decode Mode and CBP */
  534. if (Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q)
  535. {/* Intra */
  536. if (!H263Info->syntax_arith_coding)
  537. CBPY = CBPY^15; /* needed in huffman coding only */
  538. }
  539. CBP = (CBPY << 2) | (MCBPC >> 4);
  540. }
  541. if (Mode == H263_MODE_INTER4V && !H263Info->adv_pred_mode)
  542. {
  543. _SlibDebug(_VERBOSE_,
  544. ScDebugPrintf(H263Info->dbg, "getMBs() 8x8 vectors not allowed in normal prediction mode\n") );
  545. /* Could set fault-flag and resync */
  546. }
  547. if (Mode == H263_MODE_INTER_Q || Mode == H263_MODE_INTRA_Q) {
  548. /* Read DQUANT if necessary */
  549. if (H263Info->syntax_arith_coding) {
  550. DQUANT_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_DQUANT);
  551. DQUANT = H263_dquanttab[DQUANT_index] - 2;
  552. H263Info->quant +=DQUANT;
  553. _SlibDebug(_DEBUG_, ScDebugPrintf(H263Info->dbg, "getMBs() DQUANT Index: %d DQUANT %d \n",DQUANT_index, DQUANT) );
  554. }
  555. else {
  556. DQUANT = (int)ScBSGetBits(BSIn, 2);
  557. H263Info->quant += DQ_tab[DQUANT];
  558. _SlibDebug(_DEBUG_, ScDebugPrintf(H263Info->dbg, "getMBs() DQUANT: %d\n", DQUANT) );
  559. #if 0
  560. if (H263Info->trace) {
  561. printf("DQUANT (");
  562. svH263Printbits(DQUANT,2,2);
  563. printf("): %d = %d\n",DQUANT,DQ_tab[DQUANT]);
  564. }
  565. #endif
  566. }
  567. if (H263Info->quant > 31 || H263Info->quant < 1) {
  568. _SlibDebug(_WARN_, ScDebugPrintf(H263Info->dbg, "GetMBs() Quantizer out of range: clipping\n") );
  569. H263Info->quant = mmax(1,mmin(31,H263Info->quant));
  570. /* could set fault-flag and resync here */
  571. }
  572. }
  573. /* motion vectors */
  574. if (Mode == H263_MODE_INTER || Mode == H263_MODE_INTER_Q ||
  575. Mode == H263_MODE_INTER4V || H263Info->pb_frame) {
  576. if (Mode == H263_MODE_INTER4V) { startmv = 1; stopmv = 4;}
  577. else { startmv = 0; stopmv = 0;}
  578. for (k = startmv; k <= stopmv; k++) {
  579. if (H263Info->syntax_arith_coding) {
  580. mvx_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_MVD);
  581. mvx = H263_mvdtab[mvx_index];
  582. mvy_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_MVD);
  583. mvy = H263_mvdtab[mvy_index];
  584. _SlibDebug(_WARN_, ScDebugPrintf(H263Info->dbg,
  585. "GetMBs() mvx_index: %d mvy_index: %d \n", mvx_index, mvy_index) );
  586. }
  587. else {
  588. mvx = sv_H263GetTMNMV(H263Info, BSIn);
  589. mvy = sv_H263GetTMNMV(H263Info, BSIn);
  590. }
  591. pmv0 = find_pmv(H263Info, xpos,ypos,k,0);
  592. pmv1 = find_pmv(H263Info, xpos,ypos,k,1);
  593. mvx = motion_decode(H263Info, mvx, pmv0);
  594. mvy = motion_decode(H263Info, mvy, pmv1);
  595. #ifdef CAMERAMOTION
  596. analyzemotion(xpos,ypos,mvx,mvy);
  597. #else
  598. _SlibDebug(_WARN_, ScDebugPrintf(H263Info->dbg,
  599. "GetMBs() mvx: %d mvy: %d\n", mvx, mvy) );
  600. #endif
  601. /* Check mv's to prevent seg.faults when error rate is high */
  602. if (!H263Info->mv_outside_frame) {
  603. bsize = k ? 8 : 16;
  604. offset = k ? (((k-1)&1)<<3) : 0;
  605. /* checking only integer component */
  606. if ((xpos<<4) + (mvx/2) + offset < 0 ||
  607. (xpos<<4) + (mvx/2) + offset > (H263Info->mb_width<<4) - bsize) {
  608. _SlibDebug(_WARN_, ScDebugPrintf(H263Info->dbg, "GetMBs() mvx out of range: searching for sync\n") );
  609. H263Info->fault = 1;
  610. }
  611. offset = k ? (((k-1)&2)<<2) : 0;
  612. if ((ypos<<4) + (mvy/2) + offset < 0 ||
  613. (ypos<<4) + (mvy/2) + offset > (H263Info->mb_height<<4) - bsize) {
  614. _SlibDebug(_WARN_, ScDebugPrintf(H263Info->dbg, "GetMBs() mvy out of range: searching for sync\n") );
  615. H263Info->fault = 1;
  616. }
  617. }
  618. H263Info->MV[0][k][ypos+1][xpos+1] = mvx;
  619. H263Info->MV[1][k][ypos+1][xpos+1] = mvy;
  620. }
  621. /* PB frame delta vectors */
  622. if (H263Info->pb_frame) {
  623. if (MODB == H263_PBMODE_MVDB || MODB == H263_PBMODE_CBPB_MVDB) {
  624. if (H263Info->syntax_arith_coding) {
  625. mvdbx_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_MVD);
  626. mvdbx = H263_mvdtab[mvdbx_index];
  627. mvdby_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_MVD);
  628. mvdby = H263_mvdtab[mvdby_index];
  629. }
  630. else {
  631. mvdbx = sv_H263GetTMNMV(H263Info, BSIn);
  632. mvdby = sv_H263GetTMNMV(H263Info, BSIn);
  633. }
  634. mvdbx = motion_decode(H263Info, mvdbx, 0);
  635. mvdby = motion_decode(H263Info, mvdby, 0);
  636. /* This will not work if the PB deltas are so large they
  637. require the second colums of the motion vector VLC
  638. table to be used. To fix this it is necessary to
  639. calculate the MV predictor for the PB delta: TRB*MV/TRD
  640. here, and use this as the second parameter to
  641. motion_decode(). The B vector itself will then be
  642. returned from motion_decode(). This will have to be
  643. changed to the PB delta again, since it is the PB delta
  644. which is used later in this program. I don't think PB
  645. deltas outside the range mentioned above is useful, but
  646. you never know... */
  647. }
  648. else {
  649. mvdbx = 0;
  650. mvdby = 0;
  651. }
  652. _SlibDebug(_DEBUG_, ScDebugPrintf(H263Info->dbg,
  653. "GetMBs() MVDB x: %d MVDB y: %d\n", mvdbx, mvdby) );
  654. }
  655. }
  656. if (H263Info->fault) goto resync;
  657. }
  658. else { /* COD == 1 --> skipped MB */
  659. if (MBA>=MBAmax)
  660. {
  661. #ifdef CAMERAMOTION /* Burkhard */
  662. goto summarize_motion;
  663. #else
  664. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() done MBA=%d\n", MBA) );
  665. return; /* all macroblocks decoded */
  666. #endif
  667. }
  668. if (!H263Info->syntax_arith_coding)
  669. if (H263Info->pict_type == H263_PCT_INTER)
  670. ScBSSkipBits(BSIn, 1);
  671. Mode = H263_MODE_INTER;
  672. /* Reset CBP */
  673. CBP = CBPB = 0;
  674. /* reset motion vectors */
  675. H263Info->MV[0][0][ypos+1][xpos+1] = 0;
  676. H263Info->MV[1][0][ypos+1][xpos+1] = 0;
  677. mvdbx = 0;
  678. mvdby = 0;
  679. }
  680. /* Store Mode*/
  681. H263Info->modemap[ypos+1][xpos+1] = Mode;
  682. if (Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q)
  683. if (!H263Info->pb_frame)
  684. H263Info->MV[0][0][ypos+1][xpos+1]=H263Info->MV[1][0][ypos+1][xpos+1] = 0;
  685. reconstruct_mb:
  686. /* pixel coordinates of top left corner of current macroblock */
  687. /* one delayed because of OBMC */
  688. if (xpos > 0) {
  689. bx = 16*(xpos-1);
  690. by = 16*ypos;
  691. }
  692. else {
  693. bx = H263Info->coded_picture_width-16;
  694. by = 16*(ypos-1);
  695. }
  696. if (MBA > 0) {
  697. Mode = H263Info->modemap[by/16+1][bx/16+1];
  698. /* forward motion compensation for B-frame */
  699. if (H263Info->pb_frame)
  700. {
  701. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() sv_H263Reconstruct(B-frame)\n") );
  702. sv_H263Reconstruct(H263Info,bx,by,0,pmvdbx,pmvdby);
  703. }
  704. /* motion compensation for P-frame */
  705. if (Mode == H263_MODE_INTER || Mode == H263_MODE_INTER_Q || Mode == H263_MODE_INTER4V)
  706. {
  707. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() sv_H263Reconstruct(P-frame)\n") );
  708. sv_H263Reconstruct(H263Info,bx,by,1,0,0);
  709. }
  710. /* copy or add block data into P-picture */
  711. for (comp=0; comp<H263Info->blk_cnt; comp++) {
  712. bp = H263Info->block[comp];
  713. /* inverse DCT */
  714. if (Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q) {
  715. addblock(H263Info, comp,bx,by,0);
  716. }
  717. else if ( (pCBP & (1<<(H263Info->blk_cnt-1-comp))) ) {
  718. /* No need to to do this for blocks with no coeffs */
  719. addblock(H263Info,comp,bx,by,1);
  720. }
  721. }
  722. if (H263Info->pb_frame) {
  723. /* add block data into B-picture */
  724. for (comp = 6; comp<H263Info->blk_cnt+6; comp++) {
  725. if (!pCOD || H263Info->adv_pred_mode)
  726. reconblock_b(H263Info, comp-6,bx,by,Mode,pmvdbx,pmvdby);
  727. if ( (pCBPB & (1<<(H263Info->blk_cnt-1-comp%6))) ) {
  728. bp = H263Info->block[comp];
  729. addblock(H263Info,comp,bx,by,1);
  730. }
  731. }
  732. }
  733. } /* end if (MBA > 0) */
  734. if (!COD) {
  735. Mode = H263Info->modemap[ypos+1][xpos+1];
  736. /* decode blocks */
  737. for (comp=0; comp<H263Info->blk_cnt; comp++) {
  738. clearblock(H263Info, comp);
  739. if (Mode == H263_MODE_INTRA || Mode == H263_MODE_INTRA_Q) { /* Intra */
  740. bp = H263Info->block[comp];
  741. if(H263Info->syntax_arith_coding) {
  742. INTRADC_index = sv_H263SACDecode_a_symbol(BSIn,H263_cumf_INTRADC);
  743. bp[0] = H263_intradctab[INTRADC_index];
  744. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg,
  745. "getMBs() INTRADC Index: %d INTRADC: %d \n", INTRADC_index, bp[0]) );
  746. }
  747. else {
  748. bp[0] = (int)ScBSGetBits(BSIn, 8);
  749. _SlibDebug(_DEBUG_, ScDebugPrintf(H263Info->dbg, "getMBs() DC: %d \n", (int)bp[0]));
  750. }
  751. _SlibDebug(_WARN_ && bp[0] == 128,
  752. ScDebugPrintf(H263Info->dbg, "getMBs() Illegal DC-coeff: 1000000\n"));
  753. if (bp[0] == 255) /* Spec. in H.26P, not in TMN4 */
  754. bp[0] = 128;
  755. bp[0] *= 8; /* Iquant */
  756. /* DC scale */
  757. bp[0] *= (int)((float)(1 << 20) * (float)0.125) ;
  758. if ( (CBP & (1<<(H263Info->blk_cnt-1-comp))) ) {
  759. if (!H263Info->syntax_arith_coding)
  760. sv_H263GetBlock(H263Info,BSIn,comp,0);
  761. else
  762. sv_H263GetSACblock(H263Info,BSIn,comp,0);
  763. }
  764. }
  765. else { /* Inter */
  766. if ( (CBP & (1<<(H263Info->blk_cnt-1-comp))) ) {
  767. if (!H263Info->syntax_arith_coding)
  768. sv_H263GetBlock(H263Info,BSIn,comp,1);
  769. else
  770. sv_H263GetSACblock(H263Info,BSIn,comp,1);
  771. }
  772. }
  773. if (H263Info->fault) goto resync;
  774. }
  775. /* Decode B blocks */
  776. if (H263Info->pb_frame) {
  777. for (comp=6; comp<H263Info->blk_cnt+6; comp++) {
  778. clearblock(H263Info, comp);
  779. if ( (CBPB & (1<<(H263Info->blk_cnt-1-comp%6))) ) {
  780. if (!H263Info->syntax_arith_coding)
  781. sv_H263GetBlock(H263Info,BSIn,comp,1);
  782. else
  783. sv_H263GetSACblock(H263Info,BSIn,comp,1);
  784. }
  785. if (H263Info->fault) goto resync;
  786. }
  787. }
  788. }
  789. /* advance to next macroblock */
  790. MBA++;
  791. pCBP = CBP; pCBPB = CBPB; pCOD = COD;
  792. pmvdbx = mvdbx; pmvdby = mvdby;
  793. /* fflush(stdout); */
  794. if (MBA >= MBAmax && !last_done) {
  795. COD = 1;
  796. xpos = 0;
  797. ypos++;
  798. last_done = 1;
  799. goto reconstruct_mb;
  800. }
  801. }
  802. _SlibDebug(_VERBOSE_, ScDebugPrintf(H263Info->dbg, "getMBs() exiting at MBA=%d\n", MBA) );
  803. /* Burkhard */
  804. #ifdef CAMERAMOTION
  805. summarize_motion:
  806. summarizemotion(framenum);
  807. #endif
  808. }
  809. /* set block to zero */
  810. static void clearblock(SvH263DecompressInfo_t *H263Info, int comp)
  811. {
  812. clearblk(comp) ;
  813. #if 0
  814. qword *bp;
  815. int i;
  816. bp = (qword *)H263Info->block[comp];
  817. for (i=0; i<8; i++)
  818. {
  819. bp[0] = bp[1] = 0;
  820. bp += 2;
  821. }
  822. #endif
  823. }
  824. /* move/add 8x8-Block from block[comp] to refframe or bframe */
  825. static void addblock(SvH263DecompressInfo_t *H263Info, int comp, int bx, int by, int addflag)
  826. {
  827. int cc, iincr, P = 1;
  828. unsigned char *rfp;
  829. #ifdef USE_C
  830. int i ;
  831. unsigned qword *lp;
  832. unsigned qword acc,src;
  833. #endif
  834. register int *bp;
  835. bp = H263Info->block[comp];
  836. if (comp >= 6) {
  837. /* This is a component for B-frame forward prediction */
  838. P = 0;
  839. addflag = 1;
  840. comp -= 6;
  841. }
  842. cc = (comp<4) ? 0 : (comp&1)+1; /* color component index */
  843. if (cc==0) {
  844. /* luminance */
  845. /* frame DCT coding */
  846. if (P)
  847. rfp = H263Info->newframe[0]
  848. + H263Info->coded_picture_width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
  849. else
  850. rfp = H263Info->bframe[0]
  851. + H263Info->coded_picture_width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
  852. iincr = H263Info->coded_picture_width;
  853. }
  854. else {
  855. /* chrominance */
  856. /* scale coordinates */
  857. bx >>= 1;
  858. by >>= 1;
  859. /* frame DCT coding */
  860. if (P)
  861. rfp = H263Info->newframe[cc] + H263Info->chrom_width*by + bx;
  862. else
  863. rfp = H263Info->bframe[cc] + H263Info->chrom_width*by + bx;
  864. iincr = H263Info->chrom_width;
  865. }
  866. if (addflag) {
  867. #ifdef USE_C
  868. ScScaleIDCT8x8i_C(bp, bp);
  869. for (i=0; i<8; i++) {
  870. lp = (unsigned qword *) rfp;
  871. src = lp[0];
  872. acc = (unsigned qword) H263Info->clp[bp[0]+(src & 0xff)];
  873. acc |= (unsigned qword) H263Info->clp[bp[1]+((src >> 8) & 0xff)] << 8;
  874. acc |= (unsigned qword) H263Info->clp[bp[2]+((src >> 16) & 0xff)] << 16;
  875. acc |= (unsigned qword) H263Info->clp[bp[3]+((src >> 24) & 0xff)] << 24;
  876. acc |= (unsigned qword) H263Info->clp[bp[4]+((src >> 32) & 0xff)] << 32;
  877. acc |= (unsigned qword) H263Info->clp[bp[5]+((src >> 40) & 0xff)] << 40;
  878. acc |= (unsigned qword) H263Info->clp[bp[6]+((src >> 48) & 0xff)] << 48;
  879. acc |= (unsigned qword) H263Info->clp[bp[7]+((src >> 56) & 0xff)] << 56;
  880. lp[0] = acc;
  881. bp += 8;
  882. rfp+= iincr;
  883. }
  884. #else
  885. sv_H263IDCTAddToFrameP_S(bp,rfp,iincr) ;
  886. #endif
  887. }
  888. else {
  889. #ifdef USE_C
  890. ScScaleIDCT8x8i_C(bp, bp);
  891. for (i=0; i<8; i++) {
  892. lp = (unsigned qword *) rfp;
  893. acc = (unsigned qword ) H263Info->clp[bp[0]];
  894. acc |= (unsigned qword ) H263Info->clp[bp[1]] << 8;
  895. acc |= (unsigned qword ) H263Info->clp[bp[2]] << 16;
  896. acc |= (unsigned qword ) H263Info->clp[bp[3]] << 24;
  897. acc |= (unsigned qword ) H263Info->clp[bp[4]] << 32;
  898. acc |= (unsigned qword ) H263Info->clp[bp[5]] << 40;
  899. acc |= (unsigned qword ) H263Info->clp[bp[6]] << 48;
  900. acc |= (unsigned qword ) H263Info->clp[bp[7]] << 56;
  901. lp[0] = acc;
  902. bp += 8;
  903. rfp += iincr;
  904. }
  905. #else
  906. sv_H263IDCTToFrameP_S(bp,rfp,iincr) ;
  907. #endif
  908. }
  909. }
  910. /* bidirectionally reconstruct 8x8-Block from block[comp] to bframe */
  911. static void reconblock_b(SvH263DecompressInfo_t *H263Info, int comp, int bx, int by,
  912. int mode, int bdx, int bdy)
  913. {
  914. int cc,i,j,k, ii;
  915. register unsigned char *bfr, *ffr;
  916. int BMVx, BMVy;
  917. int xa,xb,ya,yb,x,y,xvec,yvec,mvx,mvy;
  918. int xint,xhalf,yint,yhalf,pel;
  919. x = bx/16+1;y=by/16+1;
  920. if (mode == H263_MODE_INTER4V) {
  921. if (comp < 4) {
  922. /* luma */
  923. mvx = H263Info->MV[0][comp+1][y][x];
  924. mvy = H263Info->MV[1][comp+1][y][x];
  925. BMVx = (bdx == 0 ? (H263Info->trb-H263Info->trd)* mvx/H263Info->trd : H263Info->trb * mvx/H263Info->trd + bdx - mvx);
  926. BMVy = (bdy == 0 ? (H263Info->trb-H263Info->trd)* mvy/H263Info->trd : H263Info->trb * mvy/H263Info->trd + bdy - mvy);
  927. }
  928. else {
  929. /* chroma */
  930. xvec = yvec = 0;
  931. for (k = 1; k <= 4; k++) {
  932. mvx = H263Info->MV[0][k][y][x];
  933. mvy = H263Info->MV[1][k][y][x];
  934. xvec += (bdx == 0 ? (H263Info->trb-H263Info->trd)* mvx/H263Info->trd : H263Info->trb * mvx/H263Info->trd + bdx - mvx);
  935. yvec += (bdy == 0 ? (H263Info->trb-H263Info->trd)* mvy/H263Info->trd : H263Info->trb * mvy/H263Info->trd + bdy - mvy);
  936. }
  937. /* chroma rounding (table 16/H.263) */
  938. BMVx = sign(xvec)*(H263_roundtab[abs(xvec)%16] + (abs(xvec)/16)*2);
  939. BMVy = sign(yvec)*(H263_roundtab[abs(yvec)%16] + (abs(yvec)/16)*2);
  940. }
  941. }
  942. else {
  943. if (comp < 4) {
  944. /* luma */
  945. mvx = H263Info->MV[0][0][y][x];
  946. mvy = H263Info->MV[1][0][y][x];
  947. BMVx = (bdx == 0 ? (H263Info->trb-H263Info->trd)* mvx/H263Info->trd : H263Info->trb * mvx/H263Info->trd + bdx - mvx);
  948. BMVy = (bdy == 0 ? (H263Info->trb-H263Info->trd)* mvy/H263Info->trd : H263Info->trb * mvy/H263Info->trd + bdy - mvy);
  949. }
  950. else {
  951. /* chroma */
  952. mvx = H263Info->MV[0][0][y][x];
  953. mvy = H263Info->MV[1][0][y][x];
  954. xvec = (bdx == 0 ? (H263Info->trb-H263Info->trd)* mvx/H263Info->trd : H263Info->trb * mvx/H263Info->trd + bdx - mvx);
  955. yvec = (bdy == 0 ? (H263Info->trb-H263Info->trd)* mvy/H263Info->trd : H263Info->trb * mvy/H263Info->trd + bdy - mvy);
  956. xvec *= 4;
  957. yvec *= 4;
  958. /* chroma rounding (table 16/H.263) */
  959. BMVx = sign(xvec)*(H263_roundtab[abs(xvec)%16] + (abs(xvec)/16)*2);
  960. BMVy = sign(yvec)*(H263_roundtab[abs(yvec)%16] + (abs(yvec)/16)*2);
  961. }
  962. }
  963. cc = (comp<4) ? 0 : (comp&1)+1; /* color component index */
  964. if (cc==0) {
  965. /* luminance */
  966. find_bidir_limits(BMVx,&xa,&xb,comp&1);
  967. find_bidir_limits(BMVy,&ya,&yb,(comp&2)>>1);
  968. bfr = H263Info->bframe[0] +
  969. H263Info->coded_picture_width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
  970. ffr = H263Info->newframe[0] +
  971. H263Info->coded_picture_width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
  972. ii = H263Info->coded_picture_width;
  973. }
  974. else {
  975. /* chrominance */
  976. /* scale coordinates and vectors*/
  977. bx >>= 1;
  978. by >>= 1;
  979. find_bidir_chroma_limits(BMVx,&xa,&xb);
  980. find_bidir_chroma_limits(BMVy,&ya,&yb);
  981. bfr = H263Info->bframe[cc] + H263Info->chrom_width*(by+((comp&2)<<2)) + bx + (comp&8);
  982. ffr = H263Info->newframe[cc] + H263Info->chrom_width*(by+((comp&2)<<2)) + bx + (comp&8);
  983. ii = H263Info->chrom_width;
  984. }
  985. xint = BMVx>>1;
  986. xhalf = BMVx - 2*xint;
  987. yint = BMVy>>1;
  988. yhalf = BMVy - 2*yint;
  989. ffr += xint + (yint+ya)*ii;
  990. bfr += ya*ii;
  991. if (!xhalf && !yhalf) {
  992. for (j = ya; j < yb; j++) {
  993. for (i = xa; i < xb; i++) {
  994. pel = ffr[i];
  995. bfr[i] = ((unsigned int)(pel + bfr[i]))>>1;
  996. }
  997. bfr += ii;
  998. ffr += ii;
  999. }
  1000. }
  1001. else if (xhalf && !yhalf) {
  1002. for (j = ya; j < yb; j++) {
  1003. for (i = xa; i < xb; i++) {
  1004. pel = ((unsigned int)(ffr[i]+ffr[i+1]+1))>>1;
  1005. bfr[i] = ((unsigned int)(pel + bfr[i]))>>1;
  1006. }
  1007. bfr += ii;
  1008. ffr += ii;
  1009. }
  1010. }
  1011. else if (!xhalf && yhalf) {
  1012. for (j = ya; j < yb; j++) {
  1013. for (i = xa; i < xb; i++) {
  1014. pel = ((unsigned int)(ffr[i]+ffr[ii+i]+1))>>1;
  1015. bfr[i] = ((unsigned int)(pel + bfr[i]))>>1;
  1016. }
  1017. bfr += ii;
  1018. ffr += ii;
  1019. }
  1020. }
  1021. else { /* if (xhalf && yhalf) */
  1022. for (j = ya; j < yb; j++) {
  1023. for (i = xa; i < xb; i++) {
  1024. pel = ((unsigned int)(ffr[i]+ffr[i+1]+ffr[ii+i]+ffr[ii+i+1]+2))>>2;
  1025. bfr[i] = ((unsigned int)(pel + bfr[i]))>>1;
  1026. }
  1027. bfr += ii;
  1028. ffr += ii;
  1029. }
  1030. }
  1031. return;
  1032. }
  1033. static int motion_decode(SvH263DecompressInfo_t *H263Info, int vec, int pmv)
  1034. {
  1035. if (vec > 31) vec -= 64;
  1036. vec += pmv;
  1037. if (!H263Info->long_vectors) {
  1038. if (vec > 31)
  1039. vec -= 64;
  1040. if (vec < -32)
  1041. vec += 64;
  1042. }
  1043. else {
  1044. if (pmv < -31 && vec < -63)
  1045. vec += 64;
  1046. if (pmv > 32 && vec > 63)
  1047. vec -= 64;
  1048. }
  1049. return vec;
  1050. }
  1051. static int find_pmv(SvH263DecompressInfo_t *H263Info, int x, int y, int block, int comp)
  1052. {
  1053. int p1,p2,p3;
  1054. int xin1,xin2,xin3;
  1055. int yin1,yin2,yin3;
  1056. int vec1,vec2,vec3;
  1057. int l8,o8,or8;
  1058. x++;y++;
  1059. l8 = (H263Info->modemap[y][x-1] == H263_MODE_INTER4V ? 1 : 0);
  1060. o8 = (H263Info->modemap[y-1][x] == H263_MODE_INTER4V ? 1 : 0);
  1061. or8 = (H263Info->modemap[y-1][x+1] == H263_MODE_INTER4V ? 1 : 0);
  1062. switch (block) {
  1063. case 0:
  1064. vec1 = (l8 ? 2 : 0) ; yin1 = y ; xin1 = x-1;
  1065. vec2 = (o8 ? 3 : 0) ; yin2 = y-1; xin2 = x;
  1066. vec3 = (or8? 3 : 0) ; yin3 = y-1; xin3 = x+1;
  1067. break;
  1068. case 1:
  1069. vec1 = (l8 ? 2 : 0) ; yin1 = y ; xin1 = x-1;
  1070. vec2 = (o8 ? 3 : 0) ; yin2 = y-1; xin2 = x;
  1071. vec3 = (or8? 3 : 0) ; yin3 = y-1; xin3 = x+1;
  1072. break;
  1073. case 2:
  1074. vec1 = 1 ; yin1 = y ; xin1 = x;
  1075. vec2 = (o8 ? 4 : 0) ; yin2 = y-1; xin2 = x;
  1076. vec3 = (or8? 3 : 0) ; yin3 = y-1; xin3 = x+1;
  1077. break;
  1078. case 3:
  1079. vec1 = (l8 ? 4 : 0) ; yin1 = y ; xin1 = x-1;
  1080. vec2 = 1 ; yin2 = y ; xin2 = x;
  1081. vec3 = 2 ; yin3 = y ; xin3 = x;
  1082. break;
  1083. case 4:
  1084. vec1 = 3 ; yin1 = y ; xin1 = x;
  1085. vec2 = 1 ; yin2 = y ; xin2 = x;
  1086. vec3 = 2 ; yin3 = y ; xin3 = x;
  1087. break;
  1088. default:
  1089. fprintf(stderr,"Illegal block number in find_pmv (getpic.c)\n");
  1090. exit(1);
  1091. break;
  1092. }
  1093. p1 = H263Info->MV[comp][vec1][yin1][xin1];
  1094. p2 = H263Info->MV[comp][vec2][yin2][xin2];
  1095. p3 = H263Info->MV[comp][vec3][yin3][xin3];
  1096. if (H263Info->newgob && (block == 0 || block == 1 || block == 2))
  1097. p2 = H263_NO_VEC;
  1098. if (p2 == H263_NO_VEC) { p2 = p3 = p1; }
  1099. return p1+p2+p3 - mmax(p1,mmax(p2,p3)) - mmin(p1,mmin(p2,p3));
  1100. }
  1101. void find_bidir_limits(int vec, int *start, int *stop, int nhv)
  1102. {
  1103. /* limits taken from C loop in section G5 in H.263 */
  1104. *start = mmax(0,(-vec+1)/2 - nhv*8);
  1105. *stop = mmin(7,15-(vec+1)/2 - nhv*8);
  1106. (*stop)++; /* I use < and not <= in the loop */
  1107. }
  1108. void find_bidir_chroma_limits(int vec, int *start, int *stop)
  1109. {
  1110. /* limits taken from C loop in section G5 in H.263 */
  1111. *start = mmax(0,(-vec+1)/2);
  1112. *stop = mmin(7,7-(vec+1)/2);
  1113. (*stop)++; /* I use < and not <= in the loop */
  1114. return;
  1115. }
  1116. void make_edge_image(unsigned char *src, unsigned char *dst, int width, int height, int edge)
  1117. {
  1118. int i,j,incrp,incro;
  1119. register unsigned char *p1,*p2,*p3,*p4;
  1120. register unsigned char *o1,*o2,*o3,*o4;
  1121. register unsigned int *dp1, *do1 ;
  1122. /* center image */
  1123. /*
  1124. p1 = dst;
  1125. o1 = src;
  1126. for (j = 0; j < height;j++) {
  1127. for (i = 0; i < width; i++) {
  1128. *(p1 + i) = *(o1 + i);
  1129. }
  1130. p1 += width + (edge<<1);
  1131. o1 += width;
  1132. }
  1133. */
  1134. dp1 = (unsigned int *)dst ;
  1135. do1 = (unsigned int *)src ;
  1136. incrp = (width + (edge<<1))/4 ;
  1137. incro = width/4 ;
  1138. for (j = 0; j < height;j++) {
  1139. for (i = 0; i < width/4; i++) {
  1140. *(dp1 + i) = *(do1 + i);
  1141. }
  1142. dp1 += incrp;
  1143. do1 += incro;
  1144. }
  1145. /* left and right edges */
  1146. p1 = dst-1;
  1147. o1 = src;
  1148. incrp = width + (edge<<1) ;
  1149. incro = width ;
  1150. for (j = 0; j < height;j++) {
  1151. for (i = 0; i < edge; i++) {
  1152. *(p1 - i) = *o1;
  1153. *(p1 + width + i + 1) = *(o1 + width - 1);
  1154. }
  1155. p1 += incrp;
  1156. o1 += incro;
  1157. }
  1158. /* top and bottom edges */
  1159. p1 = dst;
  1160. p2 = dst + (width + (edge<<1))*(height-1);
  1161. o1 = src;
  1162. o2 = src + width*(height-1);
  1163. incrp = (width + (edge<<1)) ;
  1164. for (j = 0; j < edge;j++) {
  1165. p1 -= incrp ;
  1166. p2 += incrp ;
  1167. for (i = 0; i < width; i++) {
  1168. *(p1 + i) = *(o1 + i);
  1169. *(p2 + i) = *(o2 + i);
  1170. }
  1171. }
  1172. /* corners */
  1173. p1 = dst - (width+(edge<<1)) - 1;
  1174. p2 = p1 + width + 1;
  1175. p3 = dst + (width+(edge<<1))*(height)-1;
  1176. p4 = p3 + width + 1;
  1177. o1 = src;
  1178. o2 = o1 + width - 1;
  1179. o3 = src + width*(height-1);
  1180. o4 = o3 + width - 1;
  1181. incrp = (width + (edge<<1)) ;
  1182. for (j = 0; j < edge; j++) {
  1183. for (i = 0; i < edge; i++) {
  1184. *(p1 - i) = *o1;
  1185. *(p2 + i) = *o2;
  1186. *(p3 - i) = *o3;
  1187. *(p4 + i) = *o4;
  1188. }
  1189. p1 -= incrp ;
  1190. p2 -= incrp ;
  1191. p3 += incrp ;
  1192. p4 += incrp ;
  1193. }
  1194. }
  1195. /* only used for displayed interpolated frames, not reconstructed ones */
  1196. void interpolate_image(unsigned char *in, unsigned char *out, int width, int height)
  1197. {
  1198. register int x,xx,y,w2;
  1199. register unsigned char *pp,*ii;
  1200. return ;
  1201. w2 = 2*width;
  1202. /* Horizontally */
  1203. pp = out;
  1204. ii = in;
  1205. for (y = 0; y < height-1; y++) {
  1206. for (x = 0,xx=0; x < width-1; x++,xx+=2) {
  1207. *(pp + xx) = *(ii + x);
  1208. *(pp + xx+1) = ((unsigned int)(*(ii + x) + *(ii + x + 1)))>>1;
  1209. *(pp + w2 + xx) = ((unsigned int)(*(ii + x) + *(ii + x + width)))>>1;
  1210. *(pp + w2 + xx+1) = ((unsigned int)(*(ii + x) + *(ii + x + 1) +
  1211. *(ii + x + width) + *(ii + x + width + 1)))>>2;
  1212. }
  1213. *(pp + w2 - 2) = *(ii + width - 1);
  1214. *(pp + w2 - 1) = *(ii + width - 1);
  1215. *(pp + w2 + w2 - 2) = *(ii + width + width - 1);
  1216. *(pp + w2 + w2 - 1) = *(ii + width + width - 1);
  1217. pp += w2<<1;
  1218. ii += width;
  1219. }
  1220. /* last lines */
  1221. for (x = 0,xx=0; x < width-1; x++,xx+=2) {
  1222. *(pp+ xx) = *(ii + x);
  1223. *(pp+ xx+1) = ((unsigned int)(*(ii + x) + *(ii + x + 1) + 1))>>1;
  1224. *(pp+ w2+ xx) = *(ii + x);
  1225. *(pp+ w2+ xx+1) = ((unsigned int)(*(ii + x) + *(ii + x + 1) + 1))>>1;
  1226. }
  1227. /* bottom right corner pels */
  1228. *(pp + (width<<1) - 2) = *(ii + width -1);
  1229. *(pp + (width<<1) - 1) = *(ii + width -1);
  1230. *(pp + (width<<2) - 2) = *(ii + width -1);
  1231. *(pp + (width<<2) - 1) = *(ii + width -1);
  1232. return;
  1233. }
  1234.