1
0

Picture.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. #include "Decoder.h"
  2. #define sign(a) ((a) < 0 ? -1 : 1)
  3. /* private prototypes*/
  4. static int motion_decode(int vec,int pmv);
  5. static void make_edge_image(const unsigned char *src, unsigned char *dst, int width, int height, int edge);
  6. /* decode one frame or field picture */
  7. void Decoder::getpicture(Frame decodedFrame)
  8. {
  9. int i;
  10. unsigned char *tmp;
  11. for (i=0; i<3; i++)
  12. {
  13. tmp = oldrefframe[i];
  14. oldrefframe[i] = refframe[i];
  15. refframe[i] = tmp;
  16. newframe[i] = refframe[i];
  17. }
  18. if (!firstFrame)
  19. {
  20. make_edge_image(oldrefframe[0],edgeframe[0],coded_picture_width,
  21. coded_picture_height,32);
  22. make_edge_image(oldrefframe[1],edgeframe[1],chrom_width, chrom_height,16);
  23. make_edge_image(oldrefframe[2],edgeframe[2],chrom_width, chrom_height,16);
  24. }
  25. //getMBs();
  26. get_I_P_MBs();
  27. if (deblock)
  28. edge_filter(newframe[0], newframe[1], newframe[2],
  29. coded_picture_width, coded_picture_height);
  30. /*
  31. PostFilter(newframe[0], newframe[1], newframe[2],
  32. coded_picture_width, coded_picture_height);
  33. */
  34. decodedFrame[0] = newframe[0];
  35. decodedFrame[1] = newframe[1];
  36. decodedFrame[2] = newframe[2];
  37. firstFrame=false;
  38. }
  39. /* decode all macroblocks of the current picture */
  40. void Decoder::clearblock(int comp)
  41. {
  42. int *bp;
  43. int i;
  44. bp = (int *)block[comp];
  45. for (i=0; i<8; i++)
  46. {
  47. bp[0] = bp[1] = bp[2] = bp[3] = 0;
  48. bp += 4;
  49. }
  50. }
  51. /* move/add 8x8-Block from block[comp] to refframe or bframe */
  52. void Decoder::addblock(int comp, int bx, int by, int addflag)
  53. {
  54. int cc,i, iincr;
  55. unsigned char *rfp;
  56. short *bp;
  57. bp = block[comp];
  58. /* TODO: benski>
  59. ippiCopy8x8_8u_C1R (addflag = 0)
  60. ippiAdd8x8_16s8u_C1IRS (addflag = 1)
  61. */
  62. cc = (comp<4) ? 0 : (comp&1)+1; /* color component index */
  63. if (cc==0)
  64. {
  65. /* luminance */
  66. /* frame DCT coding */
  67. rfp = newframe[0]
  68. + coded_picture_width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
  69. iincr = coded_picture_width;
  70. }
  71. else
  72. {
  73. /* chrominance */
  74. /* scale coordinates */
  75. bx >>= 1;
  76. by >>= 1;
  77. /* frame DCT coding */
  78. rfp = newframe[cc] + chrom_width*by + bx;
  79. iincr = chrom_width;
  80. }
  81. if (addflag)
  82. {
  83. for (i=0; i<8; i++)
  84. {
  85. rfp[0] = clp[bp[0]+rfp[0]];
  86. rfp[1] = clp[bp[1]+rfp[1]];
  87. rfp[2] = clp[bp[2]+rfp[2]];
  88. rfp[3] = clp[bp[3]+rfp[3]];
  89. rfp[4] = clp[bp[4]+rfp[4]];
  90. rfp[5] = clp[bp[5]+rfp[5]];
  91. rfp[6] = clp[bp[6]+rfp[6]];
  92. rfp[7] = clp[bp[7]+rfp[7]];
  93. bp += 8;
  94. rfp+= iincr;
  95. }
  96. }
  97. else
  98. {
  99. for (i=0; i<8; i++)
  100. {
  101. rfp[0] = clp[bp[0]];
  102. rfp[1] = clp[bp[1]];
  103. rfp[2] = clp[bp[2]];
  104. rfp[3] = clp[bp[3]];
  105. rfp[4] = clp[bp[4]];
  106. rfp[5] = clp[bp[5]];
  107. rfp[6] = clp[bp[6]];
  108. rfp[7] = clp[bp[7]];
  109. bp += 8;
  110. rfp += iincr;
  111. }
  112. }
  113. }
  114. int motion_decode(int vec, int pmv)
  115. {
  116. if (vec > 31) vec -= 64;
  117. vec += pmv;
  118. if (vec > 31)
  119. vec -= 64;
  120. if (vec < -32)
  121. vec += 64;
  122. return vec;
  123. }
  124. int Decoder::find_pmv(int x, int y, int block, int comp)
  125. {
  126. int p1,p2,p3;
  127. int xin1,xin2,xin3;
  128. int yin1,yin2,yin3;
  129. int vec1,vec2,vec3;
  130. int l8,o8,or8;
  131. x++;y++;
  132. l8 = (modemap[y][x-1] == MODE_INTER4V ? 1 : 0);
  133. o8 = (modemap[y-1][x] == MODE_INTER4V ? 1 : 0);
  134. or8 = (modemap[y-1][x+1] == MODE_INTER4V ? 1 : 0);
  135. switch (block)
  136. {
  137. case 0:
  138. vec1 = (l8 ? 2 : 0) ; yin1 = y ; xin1 = x-1;
  139. vec2 = (o8 ? 3 : 0) ; yin2 = y-1; xin2 = x;
  140. vec3 = (or8? 3 : 0) ; yin3 = y-1; xin3 = x+1;
  141. break;
  142. case 1:
  143. vec1 = (l8 ? 2 : 0) ; yin1 = y ; xin1 = x-1;
  144. vec2 = (o8 ? 3 : 0) ; yin2 = y-1; xin2 = x;
  145. vec3 = (or8? 3 : 0) ; yin3 = y-1; xin3 = x+1;
  146. break;
  147. case 2:
  148. vec1 = 1 ; yin1 = y ; xin1 = x;
  149. vec2 = (o8 ? 4 : 0) ; yin2 = y-1; xin2 = x;
  150. vec3 = (or8? 3 : 0) ; yin3 = y-1; xin3 = x+1;
  151. break;
  152. case 3:
  153. vec1 = (l8 ? 4 : 0) ; yin1 = y ; xin1 = x-1;
  154. vec2 = 1 ; yin2 = y ; xin2 = x;
  155. vec3 = 2 ; yin3 = y ; xin3 = x;
  156. break;
  157. case 4:
  158. vec1 = 3 ; yin1 = y ; xin1 = x;
  159. vec2 = 1 ; yin2 = y ; xin2 = x;
  160. vec3 = 2 ; yin3 = y ; xin3 = x;
  161. break;
  162. default:
  163. exit(1);
  164. break;
  165. }
  166. p1 = MV[comp][vec1][yin1][xin1];
  167. p2 = MV[comp][vec2][yin2][xin2];
  168. p3 = MV[comp][vec3][yin3][xin3];
  169. if (p2 == NO_VEC)
  170. {
  171. p2 = p3 = p1;
  172. }
  173. return p1+p2+p3 - max(p1,max(p2,p3)) - min(p1,min(p2,p3));
  174. }
  175. void make_edge_image(const unsigned char *src,unsigned char *dst,int width,int height,int edge)
  176. {
  177. int i,j;
  178. unsigned char *p1,*p2,*p3,*p4;
  179. const unsigned char *o1,*o2,*o3,*o4;
  180. /* center image */
  181. p1 = dst;
  182. o1 = src;
  183. for (j = 0; j < height;j++)
  184. {
  185. for (i = 0; i < width; i++)
  186. {
  187. *(p1 + i) = *(o1 + i);
  188. }
  189. p1 += width + (edge<<1);
  190. o1 += width;
  191. }
  192. /* left and right edges */
  193. p1 = dst-1;
  194. o1 = src;
  195. for (j = 0; j < height;j++)
  196. {
  197. for (i = 0; i < edge; i++)
  198. {
  199. *(p1 - i) = *o1;
  200. *(p1 + width + i + 1) = *(o1 + width - 1);
  201. }
  202. p1 += width + (edge<<1);
  203. o1 += width;
  204. }
  205. /* top and bottom edges */
  206. p1 = dst;
  207. p2 = dst + (width + (edge<<1))*(height-1);
  208. o1 = src;
  209. o2 = src + width*(height-1);
  210. for (j = 0; j < edge;j++)
  211. {
  212. p1 = p1 - (width + (edge<<1));
  213. p2 = p2 + (width + (edge<<1));
  214. for (i = 0; i < width; i++)
  215. {
  216. *(p1 + i) = *(o1 + i);
  217. *(p2 + i) = *(o2 + i);
  218. }
  219. }
  220. /* corners */
  221. p1 = dst - (width+(edge<<1)) - 1;
  222. p2 = p1 + width + 1;
  223. p3 = dst + (width+(edge<<1))*(height)-1;
  224. p4 = p3 + width + 1;
  225. o1 = src;
  226. o2 = o1 + width - 1;
  227. o3 = src + width*(height-1);
  228. o4 = o3 + width - 1;
  229. for (j = 0; j < edge; j++)
  230. {
  231. for (i = 0; i < edge; i++)
  232. {
  233. *(p1 - i) = *o1;
  234. *(p2 + i) = *o2;
  235. *(p3 - i) = *o3;
  236. *(p4 + i) = *o4;
  237. }
  238. p1 = p1 - (width + (edge<<1));
  239. p2 = p2 - (width + (edge<<1));
  240. p3 = p3 + width + (edge<<1);
  241. p4 = p4 + width + (edge<<1);
  242. }
  243. }
  244. static bool Mode_IsInter(int Mode)
  245. {
  246. return (Mode == MODE_INTER || Mode == MODE_INTER_Q ||
  247. Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q);
  248. }
  249. static bool Mode_IsIntra(int Mode)
  250. {
  251. return (Mode == MODE_INTRA || Mode == MODE_INTRA_Q);
  252. }
  253. void Decoder::get_I_P_MBs()
  254. {
  255. int comp;
  256. int MBA, MBAmax;
  257. int COD = 0, MCBPC, CBPY, CBP = 0, CBPB = 0, MODB = 0, Mode = 0, DQUANT;
  258. int mvx = 0, mvy = 0, pmv0, pmv1, xpos, ypos, k;
  259. int startmv, stopmv, last_done = 0, pCBP = 0, pCBPB = 0, pCOD = 0, pMODB = 0;
  260. int DQ_tab[4] = {-1, -2, 1, 2};
  261. unsigned int i;
  262. short *bp;
  263. /* number of macroblocks per picture */
  264. MBAmax = mb_width * mb_height;
  265. MBA = 0; /* macroblock address */
  266. xpos = ypos = 0;
  267. /* mark MV's above the picture */
  268. for (i = 1; i < mb_width + 1; i++)
  269. {
  270. for (k = 0; k < 5; k++)
  271. {
  272. MV[0][k][0][i] = NO_VEC;
  273. MV[1][k][0][i] = NO_VEC;
  274. }
  275. modemap[0][i] = MODE_INTRA;
  276. }
  277. /* zero MV's on the sides of the picture */
  278. for (i = 0; i < mb_height + 1; i++)
  279. {
  280. for (k = 0; k < 5; k++)
  281. {
  282. MV[0][k][i][0] = 0;
  283. MV[1][k][i][0] = 0;
  284. MV[0][k][i][mb_width + 1] = 0;
  285. MV[1][k][i][mb_width + 1] = 0;
  286. }
  287. modemap[i][0] = MODE_INTRA;
  288. modemap[i][mb_width + 1] = MODE_INTRA;
  289. }
  290. /* initialize the qcoeff used in advanced intra coding */
  291. fault = 0;
  292. for (;;)
  293. {
  294. resync:
  295. /* This version of the decoder does not resync on every possible
  296. * error, and it does not do all possible error checks. It is not
  297. * difficult to make it much more error robust, but I do not think it
  298. * is necessary to include this in the freely available version. */
  299. if (fault)
  300. {
  301. startcode(); /* sync on new startcode */
  302. fault = 0;
  303. }
  304. xpos = MBA % mb_width;
  305. ypos = MBA / mb_width;
  306. if (MBA >= MBAmax)
  307. {
  308. /* all macroblocks decoded */
  309. return;
  310. }
  311. read_cod:
  312. if (PCT_INTER == pict_type || PCT_DISPOSABLE_INTER == pict_type)
  313. {
  314. COD = buffer.showbits(1);
  315. }
  316. else
  317. {
  318. COD = 0; /* Intra picture -> not skipped */
  319. coded_map[ypos + 1][xpos + 1] = 1;
  320. }
  321. if (!COD)
  322. {
  323. /* COD == 0 --> not skipped */
  324. if (PCT_INTER == pict_type || PCT_DISPOSABLE_INTER == pict_type)
  325. {
  326. /* flush COD bit */
  327. buffer.flushbits(1);
  328. }
  329. if (PCT_INTRA == pict_type)
  330. {
  331. MCBPC = getMCBPCintra();
  332. }
  333. else
  334. {
  335. MCBPC = getMCBPC();
  336. }
  337. if (fault)
  338. goto resync;
  339. if (MCBPC == 255)
  340. {
  341. /* stuffing - read next COD without advancing MB count. */
  342. goto read_cod;
  343. }
  344. else
  345. {
  346. /* normal MB data */
  347. Mode = MCBPC & 7;
  348. /* MODB and CBPB */
  349. CBPY = getCBPY();
  350. }
  351. /* Decode Mode and CBP */
  352. if ((Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
  353. {
  354. /* Intra */
  355. coded_map[ypos + 1][xpos + 1] = 1;
  356. CBPY = CBPY ^ 15; /* needed in huffman coding only */
  357. }
  358. CBP = (CBPY << 2) | (MCBPC >> 4);
  359. if (Mode == MODE_INTER_Q || Mode == MODE_INTRA_Q || Mode == MODE_INTER4V_Q)
  360. {
  361. /* Read DQUANT if necessary */
  362. DQUANT = buffer.getbits(2);
  363. quant += DQ_tab[DQUANT];
  364. if (quant > 31 || quant < 1)
  365. {
  366. quant = max(1, (31, quant));
  367. /* could set fault-flag and resync here */
  368. fault = 1;
  369. }
  370. }
  371. /* motion vectors */
  372. if (Mode == MODE_INTER || Mode == MODE_INTER_Q ||
  373. Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q)
  374. {
  375. if (Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q)
  376. {
  377. startmv = 1;
  378. stopmv = 4;
  379. }
  380. else
  381. {
  382. startmv = 0;
  383. stopmv = 0;
  384. }
  385. for (k = startmv; k <= stopmv; k++)
  386. {
  387. mvx = getTMNMV();
  388. mvy = getTMNMV();
  389. pmv0 = find_pmv(xpos, ypos, k, 0);
  390. pmv1 = find_pmv(xpos, ypos, k, 1);
  391. mvx = motion_decode(mvx, pmv0);
  392. mvy = motion_decode(mvy, pmv1);
  393. /* store coded or not-coded */
  394. coded_map[ypos + 1][xpos + 1] = 1;
  395. MV[0][k][ypos+1][xpos+1] = mvx;
  396. MV[1][k][ypos+1][xpos+1] = mvy;
  397. }
  398. }
  399. /* Intra. */
  400. else
  401. {
  402. }
  403. if (fault)
  404. goto resync;
  405. }
  406. else
  407. {
  408. /* COD == 1 --> skipped MB */
  409. if (MBA >= MBAmax)
  410. {
  411. /* all macroblocks decoded */
  412. return;
  413. }
  414. if (PCT_INTER == pict_type || PCT_DISPOSABLE_INTER == pict_type)
  415. buffer.flushbits(1);
  416. Mode = MODE_INTER;
  417. /* Reset CBP */
  418. CBP = CBPB = 0;
  419. coded_map[ypos + 1][xpos + 1] = 0;
  420. /* reset motion vectors */
  421. MV[0][0][ypos + 1][xpos + 1] = 0;
  422. MV[1][0][ypos + 1][xpos + 1] = 0;
  423. }
  424. /* Store mode and prediction type */
  425. modemap[ypos + 1][xpos + 1] = Mode;
  426. /* store defaults for advanced intra coding mode */
  427. if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
  428. {
  429. MV[0][0][ypos + 1][xpos + 1] = MV[1][0][ypos + 1][xpos + 1] = 0;
  430. }
  431. if (!COD)
  432. {
  433. Mode = modemap[ypos + 1][xpos + 1];
  434. /* decode blocks */
  435. for (comp = 0; comp < 6; comp++)
  436. {
  437. clearblock(comp);
  438. if ((Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
  439. {
  440. /* Intra (except in advanced intra coding mode) */
  441. bp = block[comp];
  442. bp[0] = buffer.getbits(8);
  443. if (bp[0] == 255) /* Spec. in H.26P, not in TMN4 */
  444. bp[0] = 128;
  445. bp[0] *= 8; /* Iquant */
  446. if ((CBP & (1 << (6 - 1 - comp))))
  447. {
  448. getblock(comp, 0);
  449. }
  450. }
  451. else
  452. {
  453. /* Inter (or Intra in advanced intra coding mode) */
  454. if ((CBP & (1 << (6 - 1 - comp))))
  455. {
  456. getblock(comp, 1);
  457. }
  458. }
  459. if (fault)
  460. goto resync;
  461. }
  462. }
  463. /* decode the last MB if data is missing */
  464. /* advance to next macroblock */
  465. MBA++;
  466. pCBP = CBP;
  467. pCBPB = CBPB;
  468. pCOD = COD;
  469. pMODB = MODB;
  470. quant_map[ypos + 1][xpos + 1] = quant;
  471. int bx = 16 * xpos;
  472. int by = 16 * ypos;
  473. Mode = modemap[by / 16 + 1][bx / 16 + 1];
  474. /* motion compensation for P-frame */
  475. if (Mode == MODE_INTER || Mode == MODE_INTER_Q ||
  476. Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q)
  477. {
  478. reconstruct(bx, by, Mode);
  479. }
  480. /* copy or add block data into P-picture */
  481. for (comp = 0; comp < 6; comp++)
  482. {
  483. /* inverse DCT */
  484. if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
  485. {
  486. idct.idct(block[comp]);
  487. addblock(comp, bx, by, 0);
  488. }
  489. else if ((pCBP & (1 << (6 - 1 - comp))))
  490. {
  491. /* No need to to do this for blocks with no coeffs */
  492. idct.idct(block[comp]);
  493. addblock(comp, bx, by, 1);
  494. }
  495. }
  496. }
  497. }
  498. static int STRENGTH[] = {1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12};
  499. void Decoder::horiz_edge_filter(unsigned char *rec, int width, int height, int chr)
  500. {
  501. int i, j;
  502. int delta, d1, d2;
  503. int mbc, mbr, do_filter;
  504. int QP;
  505. int mbr_above;
  506. /* horizontal edges */
  507. for (j = 8; j < height; j += 8)
  508. {
  509. if (!chr)
  510. {
  511. mbr = j >> 4;
  512. mbr_above = (j - 8) >> 4;
  513. }
  514. else
  515. {
  516. mbr = j >> 3;
  517. mbr_above = mbr - 1;
  518. }
  519. const int * const cur_coded_map = coded_map[mbr + 1];
  520. for (i = 0; i < width; i++)
  521. {
  522. // TODO: replace all below with FilterDeblocking8x8HorEdge_H263(rec+i+(j+1)*width, width, QP) and i+=8 ?
  523. if (!chr)
  524. {
  525. mbc = i >> 4;
  526. }
  527. else
  528. {
  529. mbc = i >> 3;
  530. }
  531. do_filter = cur_coded_map[mbc + 1] || coded_map[mbr_above + 1][mbc + 1];
  532. if (do_filter)
  533. {
  534. QP = cur_coded_map[mbc + 1] ? quant_map[mbr + 1][mbc + 1] : quant_map[mbr_above + 1][mbc + 1];
  535. delta = (int)(((int)(*(rec + i + (j - 2) * width)) +
  536. (int)(*(rec + i + (j - 1) * width) * (-4)) +
  537. (int)(*(rec + i + (j) * width) * (4)) +
  538. (int)(*(rec + i + (j + 1) * width) * (-1))) / 8.0);
  539. d1 = sign(delta) * max(0, abs(delta) - max(0, 2 * (abs(delta) - STRENGTH[QP - 1])));
  540. d2 = min(abs(d1 / 2), max(-abs(d1 / 2), (int)(((*(rec + i + (j - 2) * width) -
  541. *(rec + i + (j + 1) * width))) / 4)));
  542. *(rec + i + (j + 1) * width) += d2; /* D */
  543. *(rec + i + (j) * width) = min(255, max(0, (int)(*(rec + i + (j) * width)) - d1)); /* C */
  544. *(rec + i + (j - 1) * width) = min(255, max(0, (int)(*(rec + i + (j - 1) * width)) + d1)); /* B */
  545. *(rec + i + (j - 2) * width) -= d2; /* A */
  546. }
  547. }
  548. }
  549. return;
  550. }
  551. void Decoder::vert_edge_filter(unsigned char *rec, int width, int height, int chr)
  552. {
  553. int i, j;
  554. int delta, d1, d2;
  555. int mbc, mbr;
  556. int do_filter;
  557. int QP;
  558. int mbc_left;
  559. /* vertical edges */
  560. for (i = 8; i < width; i += 8)
  561. {
  562. if (!chr)
  563. {
  564. mbc = i >> 4;
  565. mbc_left = (i - 8) >> 4;
  566. }
  567. else
  568. {
  569. mbc = i >> 3;
  570. mbc_left = mbc - 1;
  571. }
  572. // TODO: replace all below with FilterDeblocking8x8VerEdge_H263(rec+i +j*width, width, QP) and i+=8 ?
  573. for (j = 0; j < height; j++)
  574. {
  575. if (!chr)
  576. {
  577. mbr = j >> 4;
  578. }
  579. else
  580. {
  581. mbr = j >> 3;
  582. }
  583. do_filter = coded_map[mbr + 1][mbc + 1] || coded_map[mbr + 1][mbc_left + 1];
  584. if (do_filter)
  585. {
  586. QP = coded_map[mbr + 1][mbc + 1] ?
  587. quant_map[mbr + 1][mbc + 1] : quant_map[mbr + 1][mbc_left + 1];
  588. delta = (int)(((int)(*(rec + i - 2 + j * width)) +
  589. (int)(*(rec + i - 1 + j * width) * (-4)) +
  590. (int)(*(rec + i + j * width) * (4)) +
  591. (int)(*(rec + i + 1 + j * width) * (-1))) / 8.0);
  592. d1 = sign(delta) * max(0, abs(delta) -
  593. max(0, 2 * (abs(delta) - STRENGTH[QP - 1])));
  594. d2 = min(abs(d1 / 2), max(-abs(d1 / 2),
  595. (int)((*(rec + i - 2 + j * width) -
  596. *(rec + i + 1 + j * width)) / 4)));
  597. *(rec + i + 1 + j * width) += d2; /* D */
  598. *(rec + i + j * width) = min(255, max(0, (int)(*(rec + i + j * width)) - d1)); /* C */
  599. *(rec + i - 1 + j * width) = min(255, max(0, (int)(*(rec + i - 1 + j * width)) + d1)); /* B */
  600. *(rec + i - 2 + j * width) -= d2; /* A */
  601. }
  602. }
  603. }
  604. return;
  605. }
  606. void Decoder::edge_filter(unsigned char *lum, unsigned char *Cb, unsigned char *Cr, int width, int height)
  607. {
  608. /* Luma */
  609. horiz_edge_filter(lum, width, height, 0);
  610. vert_edge_filter(lum, width, height, 0);
  611. /* Chroma */
  612. horiz_edge_filter(Cb, width / 2, height / 2, 1);
  613. vert_edge_filter(Cb, width / 2, height / 2, 1);
  614. horiz_edge_filter(Cr, width / 2, height / 2, 1);
  615. vert_edge_filter(Cr, width / 2, height / 2, 1);
  616. /* that's it */
  617. return;
  618. }
  619. void Decoder::PostFilter(unsigned char *lum, unsigned char *Cb, unsigned char *Cr,
  620. int width, int height)
  621. {
  622. /* Luma */
  623. horiz_post_filter(lum, width, height, 0);
  624. vert_post_filter(lum, width, height, 0);
  625. /* Chroma */
  626. horiz_post_filter(Cb, width / 2, height / 2, 1);
  627. vert_post_filter(Cb, width / 2, height / 2, 1);
  628. horiz_post_filter(Cr, width / 2, height / 2, 1);
  629. vert_post_filter(Cr, width / 2, height / 2, 1);
  630. /* that's it */
  631. return;
  632. }
  633. /***********************************************************************/
  634. static int STRENGTH1[] = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4};
  635. void Decoder::horiz_post_filter(unsigned char *rec, int width, int height, int chr)
  636. {
  637. int i, j;
  638. int delta, d1;
  639. int mbc, mbr;
  640. int QP;
  641. int mbr_above;
  642. /* horizontal edges */
  643. for (j = 8; j < height; j += 8)
  644. {
  645. for (i = 0; i < width; i++)
  646. {
  647. if (!chr)
  648. {
  649. mbr = j >> 4;
  650. mbc = i >> 4;
  651. mbr_above = (j - 8) >> 4;
  652. }
  653. else
  654. {
  655. mbr = j >> 3;
  656. mbc = i >> 3;
  657. mbr_above = mbr - 1;
  658. }
  659. QP = coded_map[mbr + 1][mbc + 1] ?
  660. quant_map[mbr + 1][mbc + 1] : quant_map[mbr_above + 1][mbc + 1];
  661. delta = (int)(((int)(*(rec + i + (j - 3) * width)) +
  662. (int)(*(rec + i + (j - 2) * width)) +
  663. (int)(*(rec + i + (j - 1) * width)) +
  664. (int)(*(rec + i + (j) * width) * (-6)) +
  665. (int)(*(rec + i + (j + 1) * width)) +
  666. (int)(*(rec + i + (j + 2) * width)) +
  667. (int)(*(rec + i + (j + 3) * width))) / 8.0);
  668. d1 = sign(delta) * max(0, abs(delta) - max(0, 2 * (abs(delta) - STRENGTH1[QP - 1])));
  669. /* Filter D */
  670. *(rec + i + (j) * width) += d1;
  671. }
  672. }
  673. return;
  674. }
  675. static int STRENGTH2[] = {1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  676. void Decoder::vert_post_filter(unsigned char *rec, int width, int height, int chr)
  677. {
  678. int i, j;
  679. int delta, d1;
  680. int mbc, mbr;
  681. int QP;
  682. int mbc_left;
  683. /* vertical edges */
  684. for (i = 8; i < width; i += 8)
  685. {
  686. for (j = 0; j < height; j++)
  687. {
  688. if (!chr)
  689. {
  690. mbr = j >> 4;
  691. mbc = i >> 4;
  692. mbc_left = (i - 8) >> 4;
  693. }
  694. else
  695. {
  696. mbr = j >> 3;
  697. mbc = i >> 3;
  698. mbc_left = mbc - 1;
  699. }
  700. QP = coded_map[mbr + 1][mbc + 1] ?
  701. quant_map[mbr + 1][mbc + 1] : quant_map[mbr + 1][mbc_left + 1];
  702. delta = (int)(((int)(*(rec + i - 3 + j * width)) +
  703. (int)(*(rec + i - 2 + j * width)) +
  704. (int)(*(rec + i - 1 + j * width)) +
  705. (int)(*(rec + i + j * width) * (-6)) +
  706. (int)(*(rec + i + 1 + j * width)) +
  707. (int)(*(rec + i + 2 + j * width)) +
  708. (int)(*(rec + i + 3 + j * width))) / 8.0);
  709. d1 = sign(delta) * max(0, abs(delta) - max(0, 2 * (abs(delta) - STRENGTH2[QP - 1])));
  710. /* Post Filter D */
  711. *(rec + i + j * width) += d1;
  712. }
  713. }
  714. return;
  715. }