dering.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /****************************************************************************
  2. *
  3. * Module Title : Dering.c
  4. *
  5. * Description : Post-processing de-rining filter routines.
  6. *
  7. ***************************************************************************/
  8. #define STRICT /* Strict type checking */
  9. /****************************************************************************
  10. * Header Frames
  11. ****************************************************************************/
  12. #include "postp.h"
  13. #include "stdlib.h" /* to get abs() */
  14. /****************************************************************************
  15. * Macros
  16. ****************************************************************************/
  17. #if ( defined(_MSC_VER) || defined(MAPCA) )
  18. #define abs(x) ( (x>0) ? (x) : (-(x)) )
  19. #endif
  20. #define Clamp(val) ( (val)<0 ? 0 : ((val)>255 ? 255 : (val)) )
  21. /****************************************************************************
  22. * Exported Global Variables
  23. ****************************************************************************/
  24. UINT32 DeringModifierV1[Q_TABLE_SIZE];
  25. /*const*/ UINT32 DeringModifierV2[Q_TABLE_SIZE] =
  26. {
  27. 9, 9, 8, 8, 7, 7, 7, 7,
  28. 6, 6, 6, 6, 6, 6, 6, 6,
  29. 6, 6, 6, 6, 6, 6, 6, 6,
  30. 5, 5, 5, 5, 5, 5, 5, 5,
  31. 5, 5, 5, 5, 5, 5, 5, 5,
  32. 4, 4, 4, 4, 4, 4, 4, 4,
  33. 4, 4, 4, 4, 4, 4, 4, 4,
  34. 3, 3, 3, 3, 2, 2, 2, 2
  35. };
  36. /*const*/ UINT32 DeringModifierV3[Q_TABLE_SIZE] =
  37. {
  38. 9, 9, 9, 9, 8, 8, 8, 8,
  39. 7, 7, 7, 7, 7, 7, 7, 7,
  40. 6, 6, 6, 6, 6, 6, 6, 6,
  41. 6, 6, 6, 6, 6, 6, 6, 6,
  42. 6, 6, 6, 6, 6, 6, 6, 6,
  43. 6, 6, 5, 5, 5, 5, 5, 5,
  44. 4, 4, 4, 4, 3, 3, 3, 3,
  45. 2, 2, 2, 0, 0, 0, 0, 0
  46. };
  47. /*const*/ INT32 SharpenModifier[Q_TABLE_SIZE] =
  48. {
  49. -12, -11, -10, -10, -9, -9, -9, -9,
  50. -6, -6, -6, -6, -6, -6, -6, -6,
  51. -4, -4, -4, -4, -4, -4, -4, -4,
  52. -2, -2, -2, -2, -2, -2, -2, -2,
  53. -2, -2, -2, -2, -2, -2, -2, -2,
  54. 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, 0, 0, 0, 0,
  56. 0, 0, 0, 0, 0, 0, 0, 0
  57. };
  58. /****************************************************************************
  59. *
  60. * ROUTINE : DeringBlockStrong_C
  61. *
  62. * INPUTS : const POSTPROC_INSTANCE *pbi : Pointer to post-processor instance.
  63. * const UINT8 *SrcPtr : Pointer to input image.
  64. * UINT8 *DstPtr : Pointer to output image.
  65. * const INT32 Pitch : Stride of SrcPtr & DstPtr.
  66. * UINT32 FragQIndex : Quantizer index to use.
  67. * UINT32 *QuantScale :
  68. *
  69. * OUTPUTS : None.
  70. *
  71. * RETURNS : void
  72. *
  73. * FUNCTION : Applies a strong de-ringing filter to a block.
  74. *
  75. * SPECIAL NOTES : None.
  76. *
  77. ****************************************************************************/
  78. void DeringBlockStrong_C
  79. (
  80. const POSTPROC_INSTANCE *pbi,
  81. const UINT8 *SrcPtr,
  82. UINT8 *DstPtr,
  83. const INT32 Pitch,
  84. UINT32 FragQIndex,
  85. UINT32 *QuantScale
  86. )
  87. {
  88. int B;
  89. int al;
  90. int ar;
  91. int au;
  92. int ad;
  93. int atot;
  94. int High;
  95. int Low;
  96. int TmpMod;
  97. int newVal;
  98. short UDMod[72];
  99. short LRMod[72];
  100. unsigned int j,k;
  101. unsigned char p;
  102. unsigned char pl;
  103. unsigned char pr;
  104. unsigned char pu;
  105. unsigned char pd;
  106. unsigned int rowOffset = 0;
  107. unsigned int round = (1<<6);
  108. unsigned int QValue = QuantScale[FragQIndex];
  109. int Sharpen = SharpenModifier[FragQIndex];
  110. const unsigned char *Src = SrcPtr;
  111. const unsigned char *curRow = SrcPtr;
  112. const unsigned char *lastRow = SrcPtr-Pitch;
  113. const unsigned char *nextRow = SrcPtr+Pitch;
  114. unsigned char *dstRow = DstPtr;
  115. (void) pbi;
  116. Low = 0;
  117. High = 3 * QValue;
  118. if ( High>32 )
  119. High = 32;
  120. /* Initialize the Mod Data */
  121. for ( k=0; k<9; k++ )
  122. {
  123. for ( j=0; j<8; j++ )
  124. {
  125. TmpMod = 32 + QValue - (abs(Src[j]-Src[j-Pitch]));
  126. if ( TmpMod < -64 )
  127. TmpMod = Sharpen;
  128. else if ( TmpMod < Low )
  129. TmpMod = Low;
  130. else if ( TmpMod > High )
  131. TmpMod = High;
  132. UDMod[k*8+j] = (INT16)TmpMod;
  133. }
  134. Src += Pitch;
  135. }
  136. Src = SrcPtr;
  137. for ( k=0; k<8; k++ )
  138. {
  139. for ( j=0; j<9; j++ )
  140. {
  141. TmpMod = 32 + QValue - (abs(Src[j]-Src[j-1]));
  142. if ( TmpMod < -64 )
  143. TmpMod = Sharpen;
  144. else if ( TmpMod < 0 )
  145. TmpMod = Low;
  146. else if ( TmpMod > High )
  147. TmpMod = High;
  148. LRMod[k*9+j] = (INT16)TmpMod;
  149. }
  150. Src += Pitch;
  151. }
  152. for ( k=0; k<8; k++ )
  153. {
  154. // In the case that this function called with
  155. // same buffer for source and destination, To
  156. // keep the c and the mmx version to have
  157. // consistant results, intermediate buffer is
  158. // used to store the eight pixel value before
  159. // writing them to destination(i.e. Overwriting
  160. // souce for the speical case)
  161. // column 0
  162. int newPixel[8];
  163. atot = 128;
  164. B = round;
  165. p = curRow[rowOffset+0];
  166. pl = curRow[rowOffset+0-1];
  167. al = LRMod[k*9+0];
  168. atot -= al;
  169. B += al * pl;
  170. pu = lastRow[rowOffset+0];
  171. au = UDMod[k*8+0];
  172. atot -= au;
  173. B += au * pu;
  174. pd = nextRow[rowOffset+0];
  175. ad = UDMod[(k+1)*8+0];
  176. atot -= ad;
  177. B += ad * pd;
  178. pr = curRow[rowOffset+0+1];
  179. ar = LRMod[k*9+0+1];
  180. atot -= ar;
  181. B += ar * pr;
  182. newVal = ( atot * p + B) >> 7;
  183. newPixel[0] = Clamp( newVal );
  184. // column 1
  185. atot = 128;
  186. B = round;
  187. p = curRow[rowOffset+1];
  188. pl = curRow[rowOffset+1-1];
  189. al = LRMod[k*9+1];
  190. atot -= al;
  191. B += al * pl;
  192. pu = lastRow[rowOffset+1];
  193. au = UDMod[k*8+1];
  194. atot -= au;
  195. B += au * pu;
  196. pd = nextRow[rowOffset+1];
  197. ad = UDMod[(k+1)*8+1];
  198. atot -= ad;
  199. B += ad * pd;
  200. pr = curRow[rowOffset+1+1];
  201. ar = LRMod[k*9+1+1];
  202. atot -= ar;
  203. B += ar * pr;
  204. newVal = ( atot * p + B) >> 7;
  205. newPixel[1] = Clamp( newVal );
  206. // column 2
  207. atot = 128;
  208. B = round;
  209. p = curRow[rowOffset+2];
  210. pl = curRow[rowOffset+2-1];
  211. al = LRMod[k*9+2];
  212. atot -= al;
  213. B += al * pl;
  214. pu = lastRow[rowOffset+2];
  215. au = UDMod[k*8+2];
  216. atot -= au;
  217. B += au * pu;
  218. pd = nextRow[rowOffset+2];
  219. ad = UDMod[(k+1)*8+2];
  220. atot -= ad;
  221. B += ad * pd;
  222. pr = curRow[rowOffset+2+1];
  223. ar = LRMod[k*9+2+1];
  224. atot -= ar;
  225. B += ar * pr;
  226. newVal = ( atot * p + B) >> 7;
  227. newPixel[2] = Clamp( newVal );
  228. // column 3
  229. atot = 128;
  230. B = round;
  231. p = curRow[rowOffset+3];
  232. pl = curRow[rowOffset+3-1];
  233. al = LRMod[k*9+3];
  234. atot -= al;
  235. B += al * pl;
  236. pu = lastRow[rowOffset+3];
  237. au = UDMod[k*8+3];
  238. atot -= au;
  239. B += au * pu;
  240. pd = nextRow[rowOffset+3];
  241. ad = UDMod[(k+1)*8+3];
  242. atot -= ad;
  243. B += ad * pd;
  244. pr = curRow[rowOffset+3+1];
  245. ar = LRMod[k*9+3+1];
  246. atot -= ar;
  247. B += ar * pr;
  248. newVal = ( atot * p + B) >> 7;
  249. newPixel[3] = Clamp( newVal );
  250. // column 4
  251. atot = 128;
  252. B = round;
  253. p = curRow[rowOffset+4];
  254. pl = curRow[rowOffset+4-1];
  255. al = LRMod[k*9+4];
  256. atot -= al;
  257. B += al * pl;
  258. pu = lastRow[rowOffset+4];
  259. au = UDMod[k*8+4];
  260. atot -= au;
  261. B += au * pu;
  262. pd = nextRow[rowOffset+4];
  263. ad = UDMod[(k+1)*8+4];
  264. atot -= ad;
  265. B += ad * pd;
  266. pr = curRow[rowOffset+4+1];
  267. ar = LRMod[k*9+4+1];
  268. atot -= ar;
  269. B += ar * pr;
  270. newVal = ( atot * p + B) >> 7;
  271. newPixel[4] = Clamp( newVal );
  272. // column 5
  273. atot = 128;
  274. B = round;
  275. p = curRow[rowOffset+5];
  276. pl = curRow[rowOffset+5-1];
  277. al = LRMod[k*9+5];
  278. atot -= al;
  279. B += al * pl;
  280. pu = lastRow[rowOffset+5];
  281. au = UDMod[k*8+5];
  282. atot -= au;
  283. B += au * pu;
  284. pd = nextRow[rowOffset+5];
  285. ad = UDMod[(k+1)*8+5];
  286. atot -= ad;
  287. B += ad * pd;
  288. pr = curRow[rowOffset+5+1];
  289. ar = LRMod[k*9+5+1];
  290. atot -= ar;
  291. B += ar * pr;
  292. newVal = ( atot * p + B) >> 7;
  293. newPixel[5] = Clamp( newVal );
  294. // column 6
  295. atot = 128;
  296. B = round;
  297. p = curRow[rowOffset+6];
  298. pl = curRow[rowOffset+6-1];
  299. al = LRMod[k*9+6];
  300. atot -= al;
  301. B += al * pl;
  302. pu = lastRow[rowOffset+6];
  303. au = UDMod[k*8+6];
  304. atot -= au;
  305. B += au * pu;
  306. pd = nextRow[rowOffset+6];
  307. ad = UDMod[(k+1)*8+6];
  308. atot -= ad;
  309. B += ad * pd;
  310. pr = curRow[rowOffset+6+1];
  311. ar = LRMod[k*9+6+1];
  312. atot -= ar;
  313. B += ar * pr;
  314. newVal = ( atot * p + B) >> 7;
  315. newPixel[6] = Clamp( newVal );
  316. // column 7
  317. atot = 128;
  318. B = round;
  319. p = curRow[rowOffset+7];
  320. pl = curRow[rowOffset+7-1];
  321. al = LRMod[k*9+7];
  322. atot -= al;
  323. B += al * pl;
  324. pu = lastRow[rowOffset+7];
  325. au = UDMod[k*8+7];
  326. atot -= au;
  327. B += au * pu;
  328. pd = nextRow[rowOffset+7];
  329. ad = UDMod[(k+1)*8+7];
  330. atot -= ad;
  331. B += ad * pd;
  332. pr = curRow[rowOffset+7+1];
  333. ar = LRMod[k*9+7+1];
  334. atot -= ar;
  335. B += ar * pr;
  336. newVal = ( atot * p + B) >> 7;
  337. newPixel[7] = Clamp( newVal );
  338. dstRow[rowOffset+0] = (INT8)newPixel[0];
  339. dstRow[rowOffset+1] = (INT8)newPixel[1];
  340. dstRow[rowOffset+2] = (INT8)newPixel[2];
  341. dstRow[rowOffset+3] = (INT8)newPixel[3];
  342. dstRow[rowOffset+4] = (INT8)newPixel[4];
  343. dstRow[rowOffset+5] = (INT8)newPixel[5];
  344. dstRow[rowOffset+6] = (INT8)newPixel[6];
  345. dstRow[rowOffset+7] = (INT8)newPixel[7];
  346. rowOffset += Pitch;
  347. }
  348. }
  349. /****************************************************************************
  350. *
  351. * ROUTINE : DeringBlockWeak_C
  352. *
  353. * INPUTS : const POSTPROC_INSTANCE *pbi : Pointer to post-processor instance.
  354. * const UINT8 *SrcPtr : Pointer to input image.
  355. * UINT8 *DstPtr : Pointer to output image.
  356. * const INT32 Pitch : Stride of SrcPtr & DstPtr.
  357. * UINT32 FragQIndex : Quantizer index to use.
  358. * UINT32 *QuantScale :
  359. *
  360. * OUTPUTS : None.
  361. *
  362. * RETURNS : void
  363. *
  364. * FUNCTION : Applies a weak de-ringing filter to a block.
  365. *
  366. * SPECIAL NOTES : None.
  367. *
  368. ****************************************************************************/
  369. void DeringBlockWeak_C
  370. (
  371. const POSTPROC_INSTANCE *pbi,
  372. const UINT8 *SrcPtr,
  373. UINT8 *DstPtr,
  374. const INT32 Pitch,
  375. UINT32 FragQIndex,
  376. UINT32 *QuantScale
  377. )
  378. {
  379. int B;
  380. int al;
  381. int ar;
  382. int au;
  383. int ad;
  384. int atot;
  385. int High;
  386. int Low;
  387. int newVal;
  388. int TmpMod;
  389. short UDMod[72];
  390. short LRMod[72];
  391. unsigned int j, k;
  392. unsigned char p;
  393. unsigned char pl;
  394. unsigned char pr;
  395. unsigned char pu;
  396. unsigned char pd;
  397. unsigned int rowOffset = 0;
  398. unsigned int round = (1<<6);
  399. unsigned int QValue = QuantScale[FragQIndex];
  400. int Sharpen = SharpenModifier[FragQIndex];
  401. const unsigned char *Src = SrcPtr;
  402. const unsigned char *curRow = SrcPtr;
  403. const unsigned char *lastRow = SrcPtr-Pitch;
  404. const unsigned char *nextRow = SrcPtr+Pitch;
  405. unsigned char *dstRow = DstPtr;
  406. (void) pbi;
  407. Low = 0;
  408. High = 3 * QValue;
  409. if ( High>24 )
  410. High = 24;
  411. /* Initialize the Mod Data */
  412. for ( k=0; k<9; k++ )
  413. {
  414. for ( j=0; j<8; j++ )
  415. {
  416. TmpMod = 32 + QValue - 2*(abs(Src[j]-Src[j-Pitch]));
  417. if ( TmpMod < -64 )
  418. TmpMod = Sharpen;
  419. else if ( TmpMod < Low )
  420. TmpMod = Low;
  421. else if ( TmpMod > High )
  422. TmpMod = High;
  423. UDMod[k*8+j] = (INT16)TmpMod;
  424. }
  425. Src += Pitch;
  426. }
  427. Src = SrcPtr;
  428. for ( k=0; k<8; k++ )
  429. {
  430. for ( j=0; j<9; j++ )
  431. {
  432. TmpMod = 32 + QValue - 2*(abs(Src[j]-Src[j-1]));
  433. if ( TmpMod < -64 )
  434. TmpMod = Sharpen;
  435. else if ( TmpMod < Low )
  436. TmpMod = Low;
  437. else if ( TmpMod > High )
  438. TmpMod = High;
  439. LRMod[k*9+j] = (INT16)TmpMod;
  440. }
  441. Src += Pitch;
  442. }
  443. for ( k=0; k<8; k++ )
  444. {
  445. // loop expanded for speed
  446. for ( j=0; j<8; j++ )
  447. {
  448. // column 0
  449. atot = 128;
  450. B = round;
  451. p = curRow[rowOffset+j];
  452. pl = curRow[rowOffset+j-1];
  453. al = LRMod[k*9+j];
  454. atot -= al;
  455. B += al * pl;
  456. pu = lastRow[rowOffset+j];
  457. au = UDMod[k*8+j];
  458. atot -= au;
  459. B += au * pu;
  460. pd = nextRow[rowOffset+j];
  461. ad = UDMod[(k+1)*8+j];
  462. atot -= ad;
  463. B += ad * pd;
  464. pr = curRow[rowOffset+j+1];
  465. ar = LRMod[k*9+j+1];
  466. atot -= ar;
  467. B += ar * pr;
  468. newVal = ( atot * p + B) >> 7;
  469. dstRow[ rowOffset+j] = (INT8) Clamp( newVal );
  470. }
  471. rowOffset += Pitch;
  472. }
  473. }
  474. /****************************************************************************
  475. *
  476. * ROUTINE : DeringBlock
  477. *
  478. * INPUTS : const POSTPROC_INSTANCE *pbi : Pointer to post-processor instance.
  479. * const UINT8 *SrcPtr : Pointer to input image.
  480. * UINT8 *DstPtr : Pointer to output image.
  481. * const INT32 Pitch : Stride of SrcPtr & DstPtr.
  482. * UINT32 FragQIndex : Quantizer index to use.
  483. * UINT32 *QuantScale :
  484. *
  485. * OUTPUTS : None.
  486. *
  487. * RETURNS : void
  488. *
  489. * FUNCTION : Applies a de-ringing filter to a block.
  490. *
  491. * SPECIAL NOTES : None.
  492. *
  493. ****************************************************************************/
  494. void DeringBlock
  495. (
  496. const POSTPROC_INSTANCE *pbi,
  497. const UINT8 *SrcPtr,
  498. UINT8 *DstPtr,
  499. const INT32 Pitch,
  500. UINT32 FragQIndex,
  501. const UINT32 *QuantScale,
  502. UINT32 Variance
  503. )
  504. {
  505. int B;
  506. int atot;
  507. int newVal;
  508. int High;
  509. int Low;
  510. int TmpMod;
  511. int N[8]; // neighbors
  512. unsigned int j, k, l;
  513. unsigned int QValue = QuantScale[FragQIndex];
  514. int Slope = 4;
  515. unsigned int round = (1<<7);
  516. const unsigned char *srcRow = SrcPtr;
  517. unsigned char *dstRow = DstPtr;
  518. int Sharpen = SharpenModifier[FragQIndex];
  519. if ( pbi->PostProcessingLevel > 100 )
  520. QValue = pbi->PostProcessingLevel - 100;
  521. if ( Variance > 32768)
  522. Slope = 4;
  523. else if (Variance > 2048)
  524. Slope = 8;
  525. Low = 0;
  526. High = 3 * QValue;
  527. if ( High > 32 )
  528. High = 32;
  529. for ( k=0; k<8; k++ )
  530. {
  531. // loop expanded for speed
  532. for ( j=0; j<8; j++ )
  533. {
  534. // set up 8 neighbors of pixel srcRow[j]
  535. N[0] = srcRow[j-Pitch-1];
  536. N[1] = srcRow[j-Pitch ];
  537. N[2] = srcRow[j-Pitch+1];
  538. N[3] = srcRow[j -1];
  539. N[4] = srcRow[j +1];
  540. N[5] = srcRow[j+Pitch-1];
  541. N[6] = srcRow[j+Pitch ];
  542. N[7] = srcRow[j+Pitch+1];
  543. // column 0
  544. atot = 256;
  545. B = round;
  546. for ( l=0; l<8; l++ )
  547. {
  548. TmpMod = 32 + QValue - (Slope *(abs(srcRow[j]-N[l])) >> 2);
  549. if ( TmpMod < -64 )
  550. TmpMod = Sharpen;
  551. else if ( TmpMod < Low )
  552. TmpMod = Low;
  553. else if ( TmpMod > High )
  554. TmpMod = High;
  555. atot -= TmpMod;
  556. B += TmpMod * N[l];
  557. }
  558. newVal = ( atot * srcRow[j] + B) >> 8;
  559. dstRow[j] = (INT8) Clamp( newVal );
  560. }
  561. dstRow += Pitch;
  562. srcRow += Pitch;
  563. }
  564. }
  565. /***************************************************************************
  566. *
  567. * ROUTINE : DiagonalBlur
  568. *
  569. * INPUTS : const POSTPROC_INSTANCE *pbi : Pointer to post-processor instance (NOT USED).
  570. * const UINT8 *SrcPtr : Pointer to input image.
  571. * UINT8 *DstPtr : Pointer to output image.
  572. * const INT32 Pitch : Stride of SrcPtr & DstPtr.
  573. *
  574. * OUTPUTS : None.
  575. *
  576. * RETURNS : void
  577. *
  578. * FUNCTION : Applies a de-ringing filter to a block.
  579. *
  580. * SPECIAL NOTES : None.
  581. *
  582. ***************************************************************************/
  583. void DiagonalBlur
  584. (
  585. const POSTPROC_INSTANCE *pbi,
  586. const UINT8 *SrcPtr,
  587. UINT8 *DstPtr,
  588. const INT32 Pitch
  589. )
  590. {
  591. unsigned int j, k;
  592. unsigned char *dstRow = DstPtr;
  593. const unsigned char *srcRow = SrcPtr;
  594. for ( k=0; k<8; k++ )
  595. {
  596. // loop expanded for speed
  597. for ( j=0; j<8; j++ )
  598. {
  599. int sum;
  600. sum = 16;
  601. sum += 8*srcRow[j];
  602. sum += 2*srcRow[j-2*Pitch-2];
  603. sum += 2*srcRow[j-2*Pitch+2];
  604. sum += 4*srcRow[j-Pitch -1];
  605. sum += 4*srcRow[j-Pitch +1];
  606. sum += 4*srcRow[j+Pitch -1];
  607. sum += 4*srcRow[j+Pitch +1];
  608. sum += 2*srcRow[j+2*Pitch-2];
  609. sum += 2*srcRow[j+2*Pitch+2];
  610. sum >>= 5;
  611. dstRow[j] = sum;
  612. }
  613. dstRow += Pitch;
  614. srcRow += Pitch;
  615. }
  616. for ( k=0; k<8; k++ )
  617. {
  618. // loop expanded for speed
  619. for ( j=0; j<8; j++ )
  620. {
  621. int sum;
  622. sum = 1;
  623. sum += 6*srcRow[j];
  624. sum += -1 * srcRow[j-Pitch];
  625. sum += -1 * srcRow[j+Pitch];
  626. sum += -1 * srcRow[j-1];
  627. sum += -1 * srcRow[j+1];
  628. sum >>= 1;
  629. if ( sum<0 )
  630. sum = 0;
  631. if ( sum>255 )
  632. sum = 255;
  633. dstRow[j] = sum;
  634. }
  635. dstRow += Pitch;
  636. srcRow += Pitch;
  637. }
  638. }
  639. /****************************************************************************
  640. *
  641. * ROUTINE : DeringFrame
  642. *
  643. * INPUTS : POSTPROC_INSTANCE *pbi : Pointer to post-processor instance.
  644. * UINT8 *Src : Pointer to input image.
  645. * UINT8 *Dst : Pointer to output image.
  646. *
  647. * OUTPUTS : None.
  648. *
  649. * RETURNS : void
  650. *
  651. * FUNCTION : Applies a de-ringing filter to a frame.
  652. *
  653. * SPECIAL NOTES : None.
  654. *
  655. ****************************************************************************/
  656. void DeringFrame ( POSTPROC_INSTANCE *pbi, UINT8 *Src, UINT8 *Dst )
  657. {
  658. UINT32 Block;
  659. UINT32 col, row;
  660. UINT32 BlocksAcross, BlocksDown;
  661. UINT32 *QuantScale;
  662. UINT32 LineLength;
  663. INT32 Thresh1,Thresh2,Thresh3,Thresh4;
  664. UINT8 *SrcPtr; // Pointer to line of source image data
  665. UINT8 *DestPtr; // Pointer to line of destination image data
  666. INT32 Quality = pbi->FrameQIndex;
  667. if ( pbi->Vp3VersionNo >= 5 )
  668. {
  669. Thresh1 = 384;
  670. Thresh2 = 6 * Thresh1;
  671. Thresh3 = 5 * Thresh2/4;
  672. Thresh4 = 5 * Thresh2/2;
  673. }
  674. else
  675. {
  676. Thresh1 = 2048;
  677. Thresh2 = 15 * Thresh1;
  678. Thresh3 = 3 * Thresh2;
  679. Thresh4 = 4 * Thresh2;
  680. }
  681. if ( pbi->Vp3VersionNo >= 5 )
  682. QuantScale = DeringModifierV3;
  683. else if ( pbi->Vp3VersionNo >= 2 )
  684. QuantScale = DeringModifierV2;
  685. else
  686. QuantScale = DeringModifierV1;
  687. BlocksAcross = pbi->HFragments;
  688. BlocksDown = pbi->VFragments;
  689. SrcPtr = Src + pbi->ReconYDataOffset;
  690. DestPtr = Dst + pbi->ReconYDataOffset;
  691. LineLength = pbi->YStride;
  692. Block = 0;
  693. // De-ring Y plane
  694. for ( row=0 ; row<BlocksDown; row++ )
  695. {
  696. for ( col=0; col<BlocksAcross; col++ )
  697. {
  698. INT32 Variance = pbi->FragmentVariances[Block];
  699. if ( (pbi->PostProcessingLevel>5) && (Variance > Thresh3) )
  700. {
  701. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  702. if( (col > 0 && pbi->FragmentVariances[Block-1] > Thresh4 ) ||
  703. (col + 1 < BlocksAcross && pbi->FragmentVariances[Block+1] > Thresh4 ) ||
  704. (row + 1 < BlocksDown && pbi->FragmentVariances[Block+BlocksAcross] > Thresh4) ||
  705. (row > 0 && pbi->FragmentVariances[Block-BlocksAcross] > Thresh4) )
  706. {
  707. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  708. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  709. }
  710. }
  711. else if ( Variance > Thresh2 )
  712. {
  713. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  714. }
  715. else if(Variance > Thresh1 )
  716. {
  717. DeringBlockWeak ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  718. }
  719. else
  720. {
  721. CopyBlock ( SrcPtr+8*col, DestPtr+8*col, LineLength );
  722. }
  723. ++Block;
  724. }
  725. SrcPtr += 8 * LineLength;
  726. DestPtr += 8 * LineLength;
  727. }
  728. // De-ring U plane
  729. BlocksAcross /= 2;
  730. BlocksDown /= 2;
  731. LineLength /= 2;
  732. SrcPtr = Src + pbi->ReconUDataOffset;
  733. DestPtr = Dst + pbi->ReconUDataOffset;
  734. for ( row=0; row<BlocksDown; row++ )
  735. {
  736. for ( col=0; col<BlocksAcross; col++ )
  737. {
  738. INT32 Variance = pbi->FragmentVariances[Block];
  739. if ( pbi->Vp3VersionNo < 5)
  740. Quality = pbi->FragQIndex[Block];
  741. if ( (pbi->PostProcessingLevel>5) && (Variance > Thresh4) )
  742. {
  743. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  744. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  745. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  746. }
  747. else if ( Variance > Thresh2 )
  748. {
  749. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  750. }
  751. else if ( Variance > Thresh1 )
  752. {
  753. DeringBlockWeak ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  754. }
  755. else
  756. {
  757. CopyBlock ( SrcPtr+8*col, DestPtr+8*col, LineLength );
  758. }
  759. ++Block;
  760. }
  761. SrcPtr += 8 * LineLength;
  762. DestPtr += 8 * LineLength;
  763. }
  764. // De-ring U plane
  765. SrcPtr = Src + pbi->ReconVDataOffset;
  766. DestPtr = Dst + pbi->ReconVDataOffset;
  767. for ( row=0; row<BlocksDown; row++ )
  768. {
  769. for ( col=0; col<BlocksAcross; col++ )
  770. {
  771. INT32 Variance = pbi->FragmentVariances[Block];
  772. if ( pbi->Vp3VersionNo < 5 )
  773. Quality = pbi->FragQIndex[Block];
  774. if ( (pbi->PostProcessingLevel>5) && (Variance > Thresh4) )
  775. {
  776. DeringBlockStrong (pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  777. DeringBlockStrong (pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  778. DeringBlockStrong (pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  779. }
  780. else if ( Variance > Thresh2 )
  781. {
  782. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  783. }
  784. else if ( Variance > Thresh1 )
  785. {
  786. DeringBlockWeak ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  787. }
  788. else
  789. {
  790. CopyBlock ( SrcPtr+8*col, DestPtr+8*col, LineLength );
  791. }
  792. ++Block;
  793. }
  794. SrcPtr += 8 * LineLength;
  795. DestPtr += 8 * LineLength;
  796. }
  797. }
  798. /****************************************************************************
  799. *
  800. * ROUTINE : DeringFrameInterlaced
  801. *
  802. * INPUTS : POSTPROC_INSTANCE *pbi : Pointer to post-processor instance.
  803. * UINT8 *Src : Pointer to input image.
  804. * UINT8 *Dst : Pointer to output image.
  805. *
  806. * OUTPUTS : None.
  807. *
  808. * RETURNS : void
  809. *
  810. * FUNCTION : Applies a de-ringing filter to an INTERLACED frame.
  811. *
  812. * SPECIAL NOTES : None.
  813. *
  814. ****************************************************************************/
  815. void DeringFrameInterlaced ( POSTPROC_INSTANCE *pbi, UINT8 *Src, UINT8 *Dst )
  816. {
  817. UINT32 Block;
  818. UINT32 col, row;
  819. UINT32 BlocksAcross,BlocksDown;
  820. UINT32 LineLength;
  821. UINT32 *QuantScale;
  822. INT32 Thresh1,Thresh2,Thresh3,Thresh4;
  823. UINT8 *SrcPtr; // Pointer to line of source image data
  824. UINT8 *DestPtr; // Pointer to line of destination image data
  825. INT32 Quality = pbi->FrameQIndex;
  826. if ( pbi->Vp3VersionNo >= 5 )
  827. {
  828. Thresh1 = 384;
  829. Thresh2 = 6 * Thresh1;
  830. Thresh3 = 5 * Thresh2/4;
  831. Thresh4 = 5 * Thresh2/2;
  832. }
  833. else
  834. {
  835. Thresh1 = 2048;
  836. Thresh2 = 15 * Thresh1;
  837. Thresh3 = 3 * Thresh2;
  838. Thresh4 = 4 * Thresh2;
  839. }
  840. if ( pbi->Vp3VersionNo >= 5 )
  841. QuantScale = DeringModifierV3;
  842. else if ( pbi->Vp3VersionNo >= 2 )
  843. QuantScale = DeringModifierV2;
  844. else
  845. QuantScale = DeringModifierV1;
  846. BlocksAcross = pbi->HFragments;
  847. BlocksDown = pbi->VFragments/2; // Y plane will be done in two passes
  848. SrcPtr = Src + pbi->ReconYDataOffset;
  849. DestPtr = Dst + pbi->ReconYDataOffset;
  850. LineLength = pbi->YStride * 2; // pitch is doubled for interlacing
  851. Block = 0;
  852. // De-ring Y Plane: Top Field
  853. for ( row=0; row<BlocksDown; row++ )
  854. {
  855. for ( col=0; col<BlocksAcross; col++ )
  856. {
  857. INT32 Variance = pbi->FragmentVariances[Block];
  858. if ( (pbi->PostProcessingLevel>5) && (Variance > Thresh3) )
  859. {
  860. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  861. if( (col > 0 && pbi->FragmentVariances[Block-1] > Thresh4 ) ||
  862. (col + 1 < BlocksAcross && pbi->FragmentVariances[Block+1] > Thresh4 ) ||
  863. (row + 1 < BlocksDown && pbi->FragmentVariances[Block+BlocksAcross] > Thresh4) ||
  864. (row > 0 && pbi->FragmentVariances[Block-BlocksAcross] > Thresh4) )
  865. {
  866. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  867. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  868. }
  869. }
  870. else if ( Variance > Thresh2 )
  871. {
  872. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  873. }
  874. else if ( Variance > Thresh1 )
  875. {
  876. DeringBlockWeak ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  877. }
  878. else
  879. {
  880. CopyBlock ( SrcPtr+8*col, DestPtr+8*col, LineLength );
  881. }
  882. ++Block;
  883. }
  884. SrcPtr += 8 * LineLength;
  885. DestPtr += 8 * LineLength;
  886. }
  887. // De-ring Y Plane: Bottom Field
  888. SrcPtr = Src + pbi->ReconYDataOffset + pbi->YStride;
  889. DestPtr = Dst + pbi->ReconYDataOffset + pbi->YStride;
  890. for ( row=0; row<BlocksDown; row++ )
  891. {
  892. for ( col=0; col<BlocksAcross; col++ )
  893. {
  894. INT32 Variance = pbi->FragmentVariances[Block];
  895. if ( (pbi->PostProcessingLevel>5) && (Variance > Thresh3) )
  896. {
  897. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  898. if( (col > 0 && pbi->FragmentVariances[Block-1] > Thresh4 ) ||
  899. (col + 1 < BlocksAcross && pbi->FragmentVariances[Block+1] > Thresh4 ) ||
  900. (row + 1 < BlocksDown && pbi->FragmentVariances[Block+BlocksAcross] > Thresh4) ||
  901. (row > 0 && pbi->FragmentVariances[Block-BlocksAcross] > Thresh4) )
  902. {
  903. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  904. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  905. }
  906. }
  907. else if ( Variance > Thresh2 )
  908. {
  909. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  910. }
  911. else if(Variance > Thresh1 )
  912. {
  913. DeringBlockWeak ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  914. }
  915. else
  916. {
  917. CopyBlock ( SrcPtr+8*col, DestPtr+8*col, LineLength );
  918. }
  919. ++Block;
  920. }
  921. SrcPtr += 8 * LineLength;
  922. DestPtr += 8 * LineLength;
  923. }
  924. // NOTE: BlocksDown for UV Planes is same as in Y for interlaced frame.
  925. // De-ring U Plane
  926. BlocksAcross /= 2;
  927. LineLength /= 4;
  928. SrcPtr = Src + pbi->ReconUDataOffset;
  929. DestPtr = Dst + pbi->ReconUDataOffset;
  930. for ( row=0; row<BlocksDown; row++ )
  931. {
  932. for ( col=0; col<BlocksAcross; col++ )
  933. {
  934. INT32 Variance = pbi->FragmentVariances[Block];
  935. if ( (pbi->PostProcessingLevel>5) && (Variance > Thresh4) )
  936. {
  937. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  938. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  939. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  940. }
  941. else if ( Variance > Thresh2 )
  942. {
  943. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  944. }
  945. else if ( Variance > Thresh1 )
  946. {
  947. DeringBlockWeak ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  948. }
  949. else
  950. {
  951. CopyBlock ( SrcPtr+8*col, DestPtr+8*col, LineLength );
  952. }
  953. ++Block;
  954. }
  955. SrcPtr += 8 * LineLength;
  956. DestPtr += 8 * LineLength;
  957. }
  958. // De-ring V Plane
  959. SrcPtr = Src + pbi->ReconVDataOffset;
  960. DestPtr = Dst + pbi->ReconVDataOffset;
  961. for ( row=0; row<BlocksDown; row++ )
  962. {
  963. for ( col=0; col<BlocksAcross; col++ )
  964. {
  965. INT32 Variance = pbi->FragmentVariances[Block];
  966. if ( (pbi->PostProcessingLevel>5) && (Variance > Thresh4) )
  967. {
  968. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  969. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  970. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  971. }
  972. else if ( Variance > Thresh2 )
  973. {
  974. DeringBlockStrong ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  975. }
  976. else if ( Variance > Thresh1 )
  977. {
  978. DeringBlockWeak ( pbi, SrcPtr+8*col, DestPtr+8*col, LineLength, Quality, QuantScale );
  979. }
  980. else
  981. {
  982. CopyBlock ( SrcPtr+8*col, DestPtr+8*col, LineLength );
  983. }
  984. ++Block;
  985. }
  986. SrcPtr += 8 * LineLength;
  987. DestPtr += 8 * LineLength;
  988. }
  989. }