1
0

Reconstruction.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include "Decoder.h"
  2. #define sign(a) ((a) < 0 ? -1 : 1)
  3. /* private prototypes */
  4. static void recon_comp (unsigned char *src, unsigned char *dst,
  5. int lx, int lx2, int w, int h, int x, int y, int dx, int dy);
  6. static void rec(unsigned char *s, unsigned char *d, int lx, int lx2, int h);
  7. static void recc(unsigned char *s, unsigned char *d, int lx, int lx2, int h);
  8. static void rech(unsigned char *s, unsigned char *d, int lx, int lx2, int h);
  9. static void rechc(unsigned char *s, unsigned char *d, int lx, int lx2, int h);
  10. static void h263_recv(unsigned char *s, unsigned char *d, int lx, int lx2, int h);
  11. static void recvc(unsigned char *s, unsigned char *d, int lx, int lx2, int h);
  12. static void rec4(unsigned char *s, unsigned char *d, int lx, int lx2, int h);
  13. static void rec4c(unsigned char *s, unsigned char *d, int lx, int lx2, int h);
  14. static int roundtab[16]= {0,0,0,1,1,1,1,1,1,1,1,1,1,1,2,2};
  15. void Decoder::reconstruct(int bx, int by, int mode)
  16. {
  17. int w, h, lx, lx2, dx, dy, xp, yp, comp, sum;
  18. int x, y;
  19. unsigned char *src[3];
  20. x = bx / 16 + 1;
  21. y = by / 16 + 1;
  22. lx = coded_picture_width;
  23. lx2 = coded_picture_width + 64;
  24. src[0] = edgeframe[0];
  25. src[1] = edgeframe[1];
  26. src[2] = edgeframe[2];
  27. /* P prediction */
  28. w = 8;
  29. h = 8;
  30. /* Y */
  31. /* 4 MV can be used without OBMC in * deblocking filter mode */
  32. if (mode == MODE_INTER4V || mode == MODE_INTER4V_Q)
  33. {
  34. for (comp = 0; comp < 4; comp++)
  35. {
  36. dx = MV[0][comp + 1][y][x];
  37. dy = MV[1][comp + 1][y][x];
  38. xp = bx + ((comp & 1) << 3);
  39. yp = by + ((comp & 2) << 2);
  40. recon_comp(src[0], newframe[0], lx, lx2, w, h, xp, yp, dx, dy);
  41. }
  42. }
  43. else
  44. {
  45. dx = MV[0][0][y][x];
  46. dy = MV[1][0][y][x];
  47. recon_comp(src[0], newframe[0], lx, lx2, w << 1, h << 1, bx, by, dx, dy);
  48. // TODO: MC16x16
  49. }
  50. /* Chroma */
  51. if (mode == MODE_INTER4V || mode == MODE_INTER4V_Q)
  52. {
  53. sum = MV[0][1][y][x] + MV[0][2][y][x] + MV[0][3][y][x] + MV[0][4][y][x];
  54. dx = sign(sum) * (roundtab[abs(sum) % 16] + (abs(sum) / 16) * 2);
  55. sum = MV[1][1][y][x] + MV[1][2][y][x] + MV[1][3][y][x] + MV[1][4][y][x];
  56. dy = sign(sum) * (roundtab[abs(sum) % 16] + (abs(sum) / 16) * 2);
  57. }
  58. else
  59. {
  60. dx = MV[0][0][y][x];
  61. dy = MV[1][0][y][x];
  62. /* chroma rounding */
  63. dx = (dx % 4 == 0 ? dx >> 1 : (dx >> 1) | 1);
  64. dy = (dy % 4 == 0 ? dy >> 1 : (dy >> 1) | 1);
  65. }
  66. lx >>= 1;
  67. bx >>= 1;
  68. lx2 >>= 1;
  69. by >>= 1;
  70. /* Chroma */
  71. recon_comp(src[1], newframe[1], lx, lx2, w, h, bx, by, dx, dy);
  72. recon_comp(src[2], newframe[2], lx, lx2, w, h, bx, by, dx, dy);
  73. }
  74. static void recon_comp(unsigned char *src,unsigned char *dst,int lx,int lx2,int w,int h,int x,int y,int dx,int dy)
  75. {
  76. int xint, xh, yint, yh;
  77. unsigned char *s, *d;
  78. xint = dx>>1;
  79. xh = dx & 1;
  80. yint = dy>>1;
  81. yh = dy & 1;
  82. /* origins */
  83. s = src + lx2*(y+yint) + x + xint;
  84. d = dst + lx*y + x;
  85. if (!xh && !yh)
  86. if (w!=8)
  87. rec(s,d,lx,lx2,h);
  88. else
  89. recc(s,d,lx,lx2,h);
  90. else if (!xh && yh)
  91. if (w!=8)
  92. h263_recv(s,d,lx,lx2,h);
  93. else
  94. recvc(s,d,lx,lx2,h);
  95. else if (xh && !yh)
  96. if (w!=8)
  97. rech(s,d,lx,lx2,h);
  98. else
  99. rechc(s,d,lx,lx2,h);
  100. else /* if (xh && yh) */
  101. if (w!=8)
  102. rec4(s,d,lx,lx2,h);
  103. else
  104. rec4c(s,d,lx,lx2,h);
  105. }
  106. static void rec(unsigned char *s,unsigned char *d,int lx,int lx2,int h)
  107. {
  108. int j;
  109. for (j=0; j<h; j++)
  110. {
  111. d[0] = s[0];
  112. d[1] = s[1];
  113. d[2] = s[2];
  114. d[3] = s[3];
  115. d[4] = s[4];
  116. d[5] = s[5];
  117. d[6] = s[6];
  118. d[7] = s[7];
  119. d[8] = s[8];
  120. d[9] = s[9];
  121. d[10] = s[10];
  122. d[11] = s[11];
  123. d[12] = s[12];
  124. d[13] = s[13];
  125. d[14] = s[14];
  126. d[15] = s[15];
  127. s+= lx2;
  128. d+= lx;
  129. }
  130. }
  131. static void recc(unsigned char *s,unsigned char *d,int lx,int lx2,int h)
  132. {
  133. int j;
  134. for (j=0; j<h; j++)
  135. {
  136. d[0] = s[0];
  137. d[1] = s[1];
  138. d[2] = s[2];
  139. d[3] = s[3];
  140. d[4] = s[4];
  141. d[5] = s[5];
  142. d[6] = s[6];
  143. d[7] = s[7];
  144. s+= lx2;
  145. d+= lx;
  146. }
  147. }
  148. static void rech(unsigned char *s,unsigned char *d,int lx,int lx2,int h)
  149. {
  150. unsigned char *dp,*sp;
  151. int j;
  152. unsigned int s1,s2;
  153. sp = s;
  154. dp = d;
  155. for (j=0; j<h; j++)
  156. {
  157. s1=sp[0];
  158. dp[0] = (unsigned int)(s1+(s2=sp[1])+1)>>1;
  159. dp[1] = (unsigned int)(s2+(s1=sp[2])+1)>>1;
  160. dp[2] = (unsigned int)(s1+(s2=sp[3])+1)>>1;
  161. dp[3] = (unsigned int)(s2+(s1=sp[4])+1)>>1;
  162. dp[4] = (unsigned int)(s1+(s2=sp[5])+1)>>1;
  163. dp[5] = (unsigned int)(s2+(s1=sp[6])+1)>>1;
  164. dp[6] = (unsigned int)(s1+(s2=sp[7])+1)>>1;
  165. dp[7] = (unsigned int)(s2+(s1=sp[8])+1)>>1;
  166. dp[8] = (unsigned int)(s1+(s2=sp[9])+1)>>1;
  167. dp[9] = (unsigned int)(s2+(s1=sp[10])+1)>>1;
  168. dp[10] = (unsigned int)(s1+(s2=sp[11])+1)>>1;
  169. dp[11] = (unsigned int)(s2+(s1=sp[12])+1)>>1;
  170. dp[12] = (unsigned int)(s1+(s2=sp[13])+1)>>1;
  171. dp[13] = (unsigned int)(s2+(s1=sp[14])+1)>>1;
  172. dp[14] = (unsigned int)(s1+(s2=sp[15])+1)>>1;
  173. dp[15] = (unsigned int)(s2+sp[16]+1)>>1;
  174. sp+= lx2;
  175. dp+= lx;
  176. }
  177. }
  178. static void rechc(unsigned char *s,unsigned char *d,int lx,int lx2,int h)
  179. {
  180. unsigned char *dp,*sp;
  181. int j;
  182. unsigned int s1,s2;
  183. sp = s;
  184. dp = d;
  185. for (j=0; j<h; j++)
  186. {
  187. s1=sp[0];
  188. dp[0] = (unsigned int)(s1+(s2=sp[1])+1)>>1;
  189. dp[1] = (unsigned int)(s2+(s1=sp[2])+1)>>1;
  190. dp[2] = (unsigned int)(s1+(s2=sp[3])+1)>>1;
  191. dp[3] = (unsigned int)(s2+(s1=sp[4])+1)>>1;
  192. dp[4] = (unsigned int)(s1+(s2=sp[5])+1)>>1;
  193. dp[5] = (unsigned int)(s2+(s1=sp[6])+1)>>1;
  194. dp[6] = (unsigned int)(s1+(s2=sp[7])+1)>>1;
  195. dp[7] = (unsigned int)(s2+sp[8]+1)>>1;
  196. sp+= lx2;
  197. dp+= lx;
  198. }
  199. }
  200. static void h263_recv(unsigned char *s,unsigned char *d,int lx,int lx2,int h)
  201. {
  202. unsigned char *dp,*sp,*sp2;
  203. int j;
  204. sp = s;
  205. sp2 = s+lx2;
  206. dp = d;
  207. for (j=0; j<h; j++)
  208. {
  209. dp[0] = (unsigned int)(sp[0]+sp2[0]+1)>>1;
  210. dp[1] = (unsigned int)(sp[1]+sp2[1]+1)>>1;
  211. dp[2] = (unsigned int)(sp[2]+sp2[2]+1)>>1;
  212. dp[3] = (unsigned int)(sp[3]+sp2[3]+1)>>1;
  213. dp[4] = (unsigned int)(sp[4]+sp2[4]+1)>>1;
  214. dp[5] = (unsigned int)(sp[5]+sp2[5]+1)>>1;
  215. dp[6] = (unsigned int)(sp[6]+sp2[6]+1)>>1;
  216. dp[7] = (unsigned int)(sp[7]+sp2[7]+1)>>1;
  217. dp[8] = (unsigned int)(sp[8]+sp2[8]+1)>>1;
  218. dp[9] = (unsigned int)(sp[9]+sp2[9]+1)>>1;
  219. dp[10] = (unsigned int)(sp[10]+sp2[10]+1)>>1;
  220. dp[11] = (unsigned int)(sp[11]+sp2[11]+1)>>1;
  221. dp[12] = (unsigned int)(sp[12]+sp2[12]+1)>>1;
  222. dp[13] = (unsigned int)(sp[13]+sp2[13]+1)>>1;
  223. dp[14] = (unsigned int)(sp[14]+sp2[14]+1)>>1;
  224. dp[15] = (unsigned int)(sp[15]+sp2[15]+1)>>1;
  225. sp+= lx2;
  226. sp2+= lx2;
  227. dp+= lx;
  228. }
  229. }
  230. static void recvc(unsigned char *s,unsigned char *d,int lx,int lx2,int h)
  231. {
  232. unsigned char *dp,*sp,*sp2;
  233. int j;
  234. sp = s;
  235. sp2 = s+lx2;
  236. dp = d;
  237. for (j=0; j<h; j++)
  238. {
  239. dp[0] = (unsigned int)(sp[0]+sp2[0]+1)>>1;
  240. dp[1] = (unsigned int)(sp[1]+sp2[1]+1)>>1;
  241. dp[2] = (unsigned int)(sp[2]+sp2[2]+1)>>1;
  242. dp[3] = (unsigned int)(sp[3]+sp2[3]+1)>>1;
  243. dp[4] = (unsigned int)(sp[4]+sp2[4]+1)>>1;
  244. dp[5] = (unsigned int)(sp[5]+sp2[5]+1)>>1;
  245. dp[6] = (unsigned int)(sp[6]+sp2[6]+1)>>1;
  246. dp[7] = (unsigned int)(sp[7]+sp2[7]+1)>>1;
  247. sp+= lx2;
  248. sp2+= lx2;
  249. dp+= lx;
  250. }
  251. }
  252. static void rec4(unsigned char *s,unsigned char *d,int lx,int lx2,int h)
  253. {
  254. unsigned char *dp,*sp,*sp2;
  255. int j;
  256. unsigned int s1,s2,s3,s4;
  257. sp = s;
  258. sp2 = s+lx2;
  259. dp = d;
  260. for (j=0; j<h; j++)
  261. {
  262. s1=sp[0]; s3=sp2[0];
  263. dp[0] = (unsigned int)(s1+(s2=sp[1])+s3+(s4=sp2[1])+2)>>2;
  264. dp[1] = (unsigned int)(s2+(s1=sp[2])+s4+(s3=sp2[2])+2)>>2;
  265. dp[2] = (unsigned int)(s1+(s2=sp[3])+s3+(s4=sp2[3])+2)>>2;
  266. dp[3] = (unsigned int)(s2+(s1=sp[4])+s4+(s3=sp2[4])+2)>>2;
  267. dp[4] = (unsigned int)(s1+(s2=sp[5])+s3+(s4=sp2[5])+2)>>2;
  268. dp[5] = (unsigned int)(s2+(s1=sp[6])+s4+(s3=sp2[6])+2)>>2;
  269. dp[6] = (unsigned int)(s1+(s2=sp[7])+s3+(s4=sp2[7])+2)>>2;
  270. dp[7] = (unsigned int)(s2+(s1=sp[8])+s4+(s3=sp2[8])+2)>>2;
  271. dp[8] = (unsigned int)(s1+(s2=sp[9])+s3+(s4=sp2[9])+2)>>2;
  272. dp[9] = (unsigned int)(s2+(s1=sp[10])+s4+(s3=sp2[10])+2)>>2;
  273. dp[10] = (unsigned int)(s1+(s2=sp[11])+s3+(s4=sp2[11])+2)>>2;
  274. dp[11] = (unsigned int)(s2+(s1=sp[12])+s4+(s3=sp2[12])+2)>>2;
  275. dp[12] = (unsigned int)(s1+(s2=sp[13])+s3+(s4=sp2[13])+2)>>2;
  276. dp[13] = (unsigned int)(s2+(s1=sp[14])+s4+(s3=sp2[14])+2)>>2;
  277. dp[14] = (unsigned int)(s1+(s2=sp[15])+s3+(s4=sp2[15])+2)>>2;
  278. dp[15] = (unsigned int)(s2+sp[16]+s4+sp2[16]+2)>>2;
  279. sp+= lx2;
  280. sp2+= lx2;
  281. dp+= lx;
  282. }
  283. }
  284. static void rec4c(unsigned char *s,unsigned char *d,int lx,int lx2,int h)
  285. {
  286. unsigned char *dp,*sp,*sp2;
  287. int j;
  288. unsigned int s1,s2,s3,s4;
  289. sp = s;
  290. sp2 = s+lx2;
  291. dp = d;
  292. for (j=0; j<h; j++)
  293. {
  294. s1=sp[0]; s3=sp2[0];
  295. dp[0] = (unsigned int)(s1+(s2=sp[1])+s3+(s4=sp2[1])+2)>>2;
  296. dp[1] = (unsigned int)(s2+(s1=sp[2])+s4+(s3=sp2[2])+2)>>2;
  297. dp[2] = (unsigned int)(s1+(s2=sp[3])+s3+(s4=sp2[3])+2)>>2;
  298. dp[3] = (unsigned int)(s2+(s1=sp[4])+s4+(s3=sp2[4])+2)>>2;
  299. dp[4] = (unsigned int)(s1+(s2=sp[5])+s3+(s4=sp2[5])+2)>>2;
  300. dp[5] = (unsigned int)(s2+(s1=sp[6])+s4+(s3=sp2[6])+2)>>2;
  301. dp[6] = (unsigned int)(s1+(s2=sp[7])+s3+(s4=sp2[7])+2)>>2;
  302. dp[7] = (unsigned int)(s2+sp[8]+s4+sp2[8]+2)>>2;
  303. sp+= lx2;
  304. sp2+= lx2;
  305. dp+= lx;
  306. }
  307. }