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.

1144 lines
32KB

  1. /*
  2. Copyright (C) 2001-2003 Michael Niedermayer (michaelni@gmx.at)
  3. AltiVec optimizations (C) 2004 Romain Dolbeau <romain@dolbeau.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. /**
  17. * @file postprocess.c
  18. * postprocessing.
  19. */
  20. /*
  21. C MMX MMX2 3DNow AltiVec
  22. isVertDC Ec Ec Ec
  23. isVertMinMaxOk Ec Ec Ec
  24. doVertLowPass E e e Ec
  25. doVertDefFilter Ec Ec e e Ec
  26. isHorizDC Ec Ec Ec
  27. isHorizMinMaxOk a E Ec
  28. doHorizLowPass E e e Ec
  29. doHorizDefFilter Ec Ec e e Ec
  30. do_a_deblock Ec E Ec E
  31. deRing E e e* Ecp
  32. Vertical RKAlgo1 E a a
  33. Horizontal RKAlgo1 a a
  34. Vertical X1# a E E
  35. Horizontal X1# a E E
  36. LinIpolDeinterlace e E E*
  37. CubicIpolDeinterlace a e e*
  38. LinBlendDeinterlace e E E*
  39. MedianDeinterlace# E Ec Ec
  40. TempDeNoiser# E e e Ec
  41. * i dont have a 3dnow CPU -> its untested, but noone said it doesnt work so it seems to work
  42. # more or less selfinvented filters so the exactness isnt too meaningfull
  43. E = Exact implementation
  44. e = allmost exact implementation (slightly different rounding,...)
  45. a = alternative / approximate impl
  46. c = checked against the other implementations (-vo md5)
  47. p = partially optimized, still some work to do
  48. */
  49. /*
  50. TODO:
  51. reduce the time wasted on the mem transfer
  52. unroll stuff if instructions depend too much on the prior one
  53. move YScale thing to the end instead of fixing QP
  54. write a faster and higher quality deblocking filter :)
  55. make the mainloop more flexible (variable number of blocks at once
  56. (the if/else stuff per block is slowing things down)
  57. compare the quality & speed of all filters
  58. split this huge file
  59. optimize c versions
  60. try to unroll inner for(x=0 ... loop to avoid these damn if(x ... checks
  61. ...
  62. */
  63. //Changelog: use the CVS log
  64. #include "config.h"
  65. #include <inttypes.h>
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69. #ifdef HAVE_MALLOC_H
  70. #include <malloc.h>
  71. #endif
  72. //#undef HAVE_MMX2
  73. //#define HAVE_3DNOW
  74. //#undef HAVE_MMX
  75. //#undef ARCH_X86
  76. //#define DEBUG_BRIGHTNESS
  77. #ifdef USE_FASTMEMCPY
  78. #include "fastmemcpy.h"
  79. #endif
  80. #include "postprocess.h"
  81. #include "postprocess_internal.h"
  82. #include "mangle.h" //FIXME should be supressed
  83. #ifdef HAVE_ALTIVEC_H
  84. #include <altivec.h>
  85. #endif
  86. #ifndef HAVE_MEMALIGN
  87. #define memalign(a,b) malloc(b)
  88. #endif
  89. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  90. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  91. #define ABS(a) ((a) > 0 ? (a) : (-(a)))
  92. #define SIGN(a) ((a) > 0 ? 1 : -1)
  93. #define GET_MODE_BUFFER_SIZE 500
  94. #define OPTIONS_ARRAY_SIZE 10
  95. #define BLOCK_SIZE 8
  96. #define TEMP_STRIDE 8
  97. //#define NUM_BLOCKS_AT_ONCE 16 //not used yet
  98. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  99. # define attribute_used __attribute__((used))
  100. # define always_inline __attribute__((always_inline)) inline
  101. #else
  102. # define attribute_used
  103. # define always_inline inline
  104. #endif
  105. #ifdef ARCH_X86
  106. static uint64_t __attribute__((aligned(8))) attribute_used w05= 0x0005000500050005LL;
  107. static uint64_t __attribute__((aligned(8))) attribute_used w04= 0x0004000400040004LL;
  108. static uint64_t __attribute__((aligned(8))) attribute_used w20= 0x0020002000200020LL;
  109. static uint64_t __attribute__((aligned(8))) attribute_used b00= 0x0000000000000000LL;
  110. static uint64_t __attribute__((aligned(8))) attribute_used b01= 0x0101010101010101LL;
  111. static uint64_t __attribute__((aligned(8))) attribute_used b02= 0x0202020202020202LL;
  112. static uint64_t __attribute__((aligned(8))) attribute_used b08= 0x0808080808080808LL;
  113. static uint64_t __attribute__((aligned(8))) attribute_used b80= 0x8080808080808080LL;
  114. #endif
  115. static uint8_t clip_table[3*256];
  116. static uint8_t * const clip_tab= clip_table + 256;
  117. static const int verbose= 0;
  118. static const int attribute_used deringThreshold= 20;
  119. static struct PPFilter filters[]=
  120. {
  121. {"hb", "hdeblock", 1, 1, 3, H_DEBLOCK},
  122. {"vb", "vdeblock", 1, 2, 4, V_DEBLOCK},
  123. /* {"hr", "rkhdeblock", 1, 1, 3, H_RK1_FILTER},
  124. {"vr", "rkvdeblock", 1, 2, 4, V_RK1_FILTER},*/
  125. {"h1", "x1hdeblock", 1, 1, 3, H_X1_FILTER},
  126. {"v1", "x1vdeblock", 1, 2, 4, V_X1_FILTER},
  127. {"ha", "ahdeblock", 1, 1, 3, H_A_DEBLOCK},
  128. {"va", "avdeblock", 1, 2, 4, V_A_DEBLOCK},
  129. {"dr", "dering", 1, 5, 6, DERING},
  130. {"al", "autolevels", 0, 1, 2, LEVEL_FIX},
  131. {"lb", "linblenddeint", 1, 1, 4, LINEAR_BLEND_DEINT_FILTER},
  132. {"li", "linipoldeint", 1, 1, 4, LINEAR_IPOL_DEINT_FILTER},
  133. {"ci", "cubicipoldeint", 1, 1, 4, CUBIC_IPOL_DEINT_FILTER},
  134. {"md", "mediandeint", 1, 1, 4, MEDIAN_DEINT_FILTER},
  135. {"fd", "ffmpegdeint", 1, 1, 4, FFMPEG_DEINT_FILTER},
  136. {"l5", "lowpass5", 1, 1, 4, LOWPASS5_DEINT_FILTER},
  137. {"tn", "tmpnoise", 1, 7, 8, TEMP_NOISE_FILTER},
  138. {"fq", "forcequant", 1, 0, 0, FORCE_QUANT},
  139. {NULL, NULL,0,0,0,0} //End Marker
  140. };
  141. static char *replaceTable[]=
  142. {
  143. "default", "hdeblock:a,vdeblock:a,dering:a",
  144. "de", "hdeblock:a,vdeblock:a,dering:a",
  145. "fast", "x1hdeblock:a,x1vdeblock:a,dering:a",
  146. "fa", "x1hdeblock:a,x1vdeblock:a,dering:a",
  147. "ac", "ha:a:128:7,va:a,dering:a",
  148. NULL //End Marker
  149. };
  150. #ifdef ARCH_X86
  151. static inline void prefetchnta(void *p)
  152. {
  153. asm volatile( "prefetchnta (%0)\n\t"
  154. : : "r" (p)
  155. );
  156. }
  157. static inline void prefetcht0(void *p)
  158. {
  159. asm volatile( "prefetcht0 (%0)\n\t"
  160. : : "r" (p)
  161. );
  162. }
  163. static inline void prefetcht1(void *p)
  164. {
  165. asm volatile( "prefetcht1 (%0)\n\t"
  166. : : "r" (p)
  167. );
  168. }
  169. static inline void prefetcht2(void *p)
  170. {
  171. asm volatile( "prefetcht2 (%0)\n\t"
  172. : : "r" (p)
  173. );
  174. }
  175. #endif
  176. // The horizontal Functions exist only in C cuz the MMX code is faster with vertical filters and transposing
  177. /**
  178. * Check if the given 8x8 Block is mostly "flat"
  179. */
  180. static inline int isHorizDC_C(uint8_t src[], int stride, PPContext *c)
  181. {
  182. int numEq= 0;
  183. int y;
  184. const int dcOffset= ((c->nonBQP*c->ppMode.baseDcDiff)>>8) + 1;
  185. const int dcThreshold= dcOffset*2 + 1;
  186. for(y=0; y<BLOCK_SIZE; y++)
  187. {
  188. if(((unsigned)(src[0] - src[1] + dcOffset)) < dcThreshold) numEq++;
  189. if(((unsigned)(src[1] - src[2] + dcOffset)) < dcThreshold) numEq++;
  190. if(((unsigned)(src[2] - src[3] + dcOffset)) < dcThreshold) numEq++;
  191. if(((unsigned)(src[3] - src[4] + dcOffset)) < dcThreshold) numEq++;
  192. if(((unsigned)(src[4] - src[5] + dcOffset)) < dcThreshold) numEq++;
  193. if(((unsigned)(src[5] - src[6] + dcOffset)) < dcThreshold) numEq++;
  194. if(((unsigned)(src[6] - src[7] + dcOffset)) < dcThreshold) numEq++;
  195. src+= stride;
  196. }
  197. return numEq > c->ppMode.flatnessThreshold;
  198. }
  199. /**
  200. * Check if the middle 8x8 Block in the given 8x16 block is flat
  201. */
  202. static inline int isVertDC_C(uint8_t src[], int stride, PPContext *c){
  203. int numEq= 0;
  204. int y;
  205. const int dcOffset= ((c->nonBQP*c->ppMode.baseDcDiff)>>8) + 1;
  206. const int dcThreshold= dcOffset*2 + 1;
  207. src+= stride*4; // src points to begin of the 8x8 Block
  208. for(y=0; y<BLOCK_SIZE-1; y++)
  209. {
  210. if(((unsigned)(src[0] - src[0+stride] + dcOffset)) < dcThreshold) numEq++;
  211. if(((unsigned)(src[1] - src[1+stride] + dcOffset)) < dcThreshold) numEq++;
  212. if(((unsigned)(src[2] - src[2+stride] + dcOffset)) < dcThreshold) numEq++;
  213. if(((unsigned)(src[3] - src[3+stride] + dcOffset)) < dcThreshold) numEq++;
  214. if(((unsigned)(src[4] - src[4+stride] + dcOffset)) < dcThreshold) numEq++;
  215. if(((unsigned)(src[5] - src[5+stride] + dcOffset)) < dcThreshold) numEq++;
  216. if(((unsigned)(src[6] - src[6+stride] + dcOffset)) < dcThreshold) numEq++;
  217. if(((unsigned)(src[7] - src[7+stride] + dcOffset)) < dcThreshold) numEq++;
  218. src+= stride;
  219. }
  220. return numEq > c->ppMode.flatnessThreshold;
  221. }
  222. static inline int isHorizMinMaxOk_C(uint8_t src[], int stride, int QP)
  223. {
  224. int i;
  225. #if 1
  226. for(i=0; i<2; i++){
  227. if((unsigned)(src[0] - src[5] + 2*QP) > 4*QP) return 0;
  228. src += stride;
  229. if((unsigned)(src[2] - src[7] + 2*QP) > 4*QP) return 0;
  230. src += stride;
  231. if((unsigned)(src[4] - src[1] + 2*QP) > 4*QP) return 0;
  232. src += stride;
  233. if((unsigned)(src[6] - src[3] + 2*QP) > 4*QP) return 0;
  234. src += stride;
  235. }
  236. #else
  237. for(i=0; i<8; i++){
  238. if((unsigned)(src[0] - src[7] + 2*QP) > 4*QP) return 0;
  239. src += stride;
  240. }
  241. #endif
  242. return 1;
  243. }
  244. static inline int isVertMinMaxOk_C(uint8_t src[], int stride, int QP)
  245. {
  246. #if 1
  247. #if 1
  248. int x;
  249. src+= stride*4;
  250. for(x=0; x<BLOCK_SIZE; x+=4)
  251. {
  252. if((unsigned)(src[ x + 0*stride] - src[ x + 5*stride] + 2*QP) > 4*QP) return 0;
  253. if((unsigned)(src[1+x + 2*stride] - src[1+x + 7*stride] + 2*QP) > 4*QP) return 0;
  254. if((unsigned)(src[2+x + 4*stride] - src[2+x + 1*stride] + 2*QP) > 4*QP) return 0;
  255. if((unsigned)(src[3+x + 6*stride] - src[3+x + 3*stride] + 2*QP) > 4*QP) return 0;
  256. }
  257. #else
  258. int x;
  259. src+= stride*3;
  260. for(x=0; x<BLOCK_SIZE; x++)
  261. {
  262. if((unsigned)(src[x + stride] - src[x + (stride<<3)] + 2*QP) > 4*QP) return 0;
  263. }
  264. #endif
  265. return 1;
  266. #else
  267. int x;
  268. src+= stride*4;
  269. for(x=0; x<BLOCK_SIZE; x++)
  270. {
  271. int min=255;
  272. int max=0;
  273. int y;
  274. for(y=0; y<8; y++){
  275. int v= src[x + y*stride];
  276. if(v>max) max=v;
  277. if(v<min) min=v;
  278. }
  279. if(max-min > 2*QP) return 0;
  280. }
  281. return 1;
  282. #endif
  283. }
  284. static inline int horizClassify_C(uint8_t src[], int stride, PPContext *c){
  285. if( isHorizDC_C(src, stride, c) ){
  286. if( isHorizMinMaxOk_C(src, stride, c->QP) )
  287. return 1;
  288. else
  289. return 0;
  290. }else{
  291. return 2;
  292. }
  293. }
  294. static inline int vertClassify_C(uint8_t src[], int stride, PPContext *c){
  295. if( isVertDC_C(src, stride, c) ){
  296. if( isVertMinMaxOk_C(src, stride, c->QP) )
  297. return 1;
  298. else
  299. return 0;
  300. }else{
  301. return 2;
  302. }
  303. }
  304. static inline void doHorizDefFilter_C(uint8_t dst[], int stride, PPContext *c)
  305. {
  306. int y;
  307. for(y=0; y<BLOCK_SIZE; y++)
  308. {
  309. const int middleEnergy= 5*(dst[4] - dst[3]) + 2*(dst[2] - dst[5]);
  310. if(ABS(middleEnergy) < 8*c->QP)
  311. {
  312. const int q=(dst[3] - dst[4])/2;
  313. const int leftEnergy= 5*(dst[2] - dst[1]) + 2*(dst[0] - dst[3]);
  314. const int rightEnergy= 5*(dst[6] - dst[5]) + 2*(dst[4] - dst[7]);
  315. int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
  316. d= MAX(d, 0);
  317. d= (5*d + 32) >> 6;
  318. d*= SIGN(-middleEnergy);
  319. if(q>0)
  320. {
  321. d= d<0 ? 0 : d;
  322. d= d>q ? q : d;
  323. }
  324. else
  325. {
  326. d= d>0 ? 0 : d;
  327. d= d<q ? q : d;
  328. }
  329. dst[3]-= d;
  330. dst[4]+= d;
  331. }
  332. dst+= stride;
  333. }
  334. }
  335. /**
  336. * Do a horizontal low pass filter on the 10x8 block (dst points to middle 8x8 Block)
  337. * using the 9-Tap Filter (1,1,2,2,4,2,2,1,1)/16 (C version)
  338. */
  339. static inline void doHorizLowPass_C(uint8_t dst[], int stride, PPContext *c)
  340. {
  341. int y;
  342. for(y=0; y<BLOCK_SIZE; y++)
  343. {
  344. const int first= ABS(dst[-1] - dst[0]) < c->QP ? dst[-1] : dst[0];
  345. const int last= ABS(dst[8] - dst[7]) < c->QP ? dst[8] : dst[7];
  346. int sums[10];
  347. sums[0] = 4*first + dst[0] + dst[1] + dst[2] + 4;
  348. sums[1] = sums[0] - first + dst[3];
  349. sums[2] = sums[1] - first + dst[4];
  350. sums[3] = sums[2] - first + dst[5];
  351. sums[4] = sums[3] - first + dst[6];
  352. sums[5] = sums[4] - dst[0] + dst[7];
  353. sums[6] = sums[5] - dst[1] + last;
  354. sums[7] = sums[6] - dst[2] + last;
  355. sums[8] = sums[7] - dst[3] + last;
  356. sums[9] = sums[8] - dst[4] + last;
  357. dst[0]= (sums[0] + sums[2] + 2*dst[0])>>4;
  358. dst[1]= (sums[1] + sums[3] + 2*dst[1])>>4;
  359. dst[2]= (sums[2] + sums[4] + 2*dst[2])>>4;
  360. dst[3]= (sums[3] + sums[5] + 2*dst[3])>>4;
  361. dst[4]= (sums[4] + sums[6] + 2*dst[4])>>4;
  362. dst[5]= (sums[5] + sums[7] + 2*dst[5])>>4;
  363. dst[6]= (sums[6] + sums[8] + 2*dst[6])>>4;
  364. dst[7]= (sums[7] + sums[9] + 2*dst[7])>>4;
  365. dst+= stride;
  366. }
  367. }
  368. /**
  369. * Experimental Filter 1 (Horizontal)
  370. * will not damage linear gradients
  371. * Flat blocks should look like they where passed through the (1,1,2,2,4,2,2,1,1) 9-Tap filter
  372. * can only smooth blocks at the expected locations (it cant smooth them if they did move)
  373. * MMX2 version does correct clipping C version doesnt
  374. * not identical with the vertical one
  375. */
  376. static inline void horizX1Filter(uint8_t *src, int stride, int QP)
  377. {
  378. int y;
  379. static uint64_t *lut= NULL;
  380. if(lut==NULL)
  381. {
  382. int i;
  383. lut= (uint64_t*)memalign(8, 256*8);
  384. for(i=0; i<256; i++)
  385. {
  386. int v= i < 128 ? 2*i : 2*(i-256);
  387. /*
  388. //Simulate 112242211 9-Tap filter
  389. uint64_t a= (v/16) & 0xFF;
  390. uint64_t b= (v/8) & 0xFF;
  391. uint64_t c= (v/4) & 0xFF;
  392. uint64_t d= (3*v/8) & 0xFF;
  393. */
  394. //Simulate piecewise linear interpolation
  395. uint64_t a= (v/16) & 0xFF;
  396. uint64_t b= (v*3/16) & 0xFF;
  397. uint64_t c= (v*5/16) & 0xFF;
  398. uint64_t d= (7*v/16) & 0xFF;
  399. uint64_t A= (0x100 - a)&0xFF;
  400. uint64_t B= (0x100 - b)&0xFF;
  401. uint64_t C= (0x100 - c)&0xFF;
  402. uint64_t D= (0x100 - c)&0xFF;
  403. lut[i] = (a<<56) | (b<<48) | (c<<40) | (d<<32) |
  404. (D<<24) | (C<<16) | (B<<8) | (A);
  405. //lut[i] = (v<<32) | (v<<24);
  406. }
  407. }
  408. for(y=0; y<BLOCK_SIZE; y++)
  409. {
  410. int a= src[1] - src[2];
  411. int b= src[3] - src[4];
  412. int c= src[5] - src[6];
  413. int d= MAX(ABS(b) - (ABS(a) + ABS(c))/2, 0);
  414. if(d < QP)
  415. {
  416. int v = d * SIGN(-b);
  417. src[1] +=v/8;
  418. src[2] +=v/4;
  419. src[3] +=3*v/8;
  420. src[4] -=3*v/8;
  421. src[5] -=v/4;
  422. src[6] -=v/8;
  423. }
  424. src+=stride;
  425. }
  426. }
  427. /**
  428. * accurate deblock filter
  429. */
  430. static always_inline void do_a_deblock_C(uint8_t *src, int step, int stride, PPContext *c){
  431. int y;
  432. const int QP= c->QP;
  433. const int dcOffset= ((c->nonBQP*c->ppMode.baseDcDiff)>>8) + 1;
  434. const int dcThreshold= dcOffset*2 + 1;
  435. //START_TIMER
  436. src+= step*4; // src points to begin of the 8x8 Block
  437. for(y=0; y<8; y++){
  438. int numEq= 0;
  439. if(((unsigned)(src[-1*step] - src[0*step] + dcOffset)) < dcThreshold) numEq++;
  440. if(((unsigned)(src[ 0*step] - src[1*step] + dcOffset)) < dcThreshold) numEq++;
  441. if(((unsigned)(src[ 1*step] - src[2*step] + dcOffset)) < dcThreshold) numEq++;
  442. if(((unsigned)(src[ 2*step] - src[3*step] + dcOffset)) < dcThreshold) numEq++;
  443. if(((unsigned)(src[ 3*step] - src[4*step] + dcOffset)) < dcThreshold) numEq++;
  444. if(((unsigned)(src[ 4*step] - src[5*step] + dcOffset)) < dcThreshold) numEq++;
  445. if(((unsigned)(src[ 5*step] - src[6*step] + dcOffset)) < dcThreshold) numEq++;
  446. if(((unsigned)(src[ 6*step] - src[7*step] + dcOffset)) < dcThreshold) numEq++;
  447. if(((unsigned)(src[ 7*step] - src[8*step] + dcOffset)) < dcThreshold) numEq++;
  448. if(numEq > c->ppMode.flatnessThreshold){
  449. int min, max, x;
  450. if(src[0] > src[step]){
  451. max= src[0];
  452. min= src[step];
  453. }else{
  454. max= src[step];
  455. min= src[0];
  456. }
  457. for(x=2; x<8; x+=2){
  458. if(src[x*step] > src[(x+1)*step]){
  459. if(src[x *step] > max) max= src[ x *step];
  460. if(src[(x+1)*step] < min) min= src[(x+1)*step];
  461. }else{
  462. if(src[(x+1)*step] > max) max= src[(x+1)*step];
  463. if(src[ x *step] < min) min= src[ x *step];
  464. }
  465. }
  466. if(max-min < 2*QP){
  467. const int first= ABS(src[-1*step] - src[0]) < QP ? src[-1*step] : src[0];
  468. const int last= ABS(src[8*step] - src[7*step]) < QP ? src[8*step] : src[7*step];
  469. int sums[10];
  470. sums[0] = 4*first + src[0*step] + src[1*step] + src[2*step] + 4;
  471. sums[1] = sums[0] - first + src[3*step];
  472. sums[2] = sums[1] - first + src[4*step];
  473. sums[3] = sums[2] - first + src[5*step];
  474. sums[4] = sums[3] - first + src[6*step];
  475. sums[5] = sums[4] - src[0*step] + src[7*step];
  476. sums[6] = sums[5] - src[1*step] + last;
  477. sums[7] = sums[6] - src[2*step] + last;
  478. sums[8] = sums[7] - src[3*step] + last;
  479. sums[9] = sums[8] - src[4*step] + last;
  480. src[0*step]= (sums[0] + sums[2] + 2*src[0*step])>>4;
  481. src[1*step]= (sums[1] + sums[3] + 2*src[1*step])>>4;
  482. src[2*step]= (sums[2] + sums[4] + 2*src[2*step])>>4;
  483. src[3*step]= (sums[3] + sums[5] + 2*src[3*step])>>4;
  484. src[4*step]= (sums[4] + sums[6] + 2*src[4*step])>>4;
  485. src[5*step]= (sums[5] + sums[7] + 2*src[5*step])>>4;
  486. src[6*step]= (sums[6] + sums[8] + 2*src[6*step])>>4;
  487. src[7*step]= (sums[7] + sums[9] + 2*src[7*step])>>4;
  488. }
  489. }else{
  490. const int middleEnergy= 5*(src[4*step] - src[3*step]) + 2*(src[2*step] - src[5*step]);
  491. if(ABS(middleEnergy) < 8*QP)
  492. {
  493. const int q=(src[3*step] - src[4*step])/2;
  494. const int leftEnergy= 5*(src[2*step] - src[1*step]) + 2*(src[0*step] - src[3*step]);
  495. const int rightEnergy= 5*(src[6*step] - src[5*step]) + 2*(src[4*step] - src[7*step]);
  496. int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
  497. d= MAX(d, 0);
  498. d= (5*d + 32) >> 6;
  499. d*= SIGN(-middleEnergy);
  500. if(q>0)
  501. {
  502. d= d<0 ? 0 : d;
  503. d= d>q ? q : d;
  504. }
  505. else
  506. {
  507. d= d>0 ? 0 : d;
  508. d= d<q ? q : d;
  509. }
  510. src[3*step]-= d;
  511. src[4*step]+= d;
  512. }
  513. }
  514. src += stride;
  515. }
  516. /*if(step==16){
  517. STOP_TIMER("step16")
  518. }else{
  519. STOP_TIMER("stepX")
  520. }*/
  521. }
  522. //Note: we have C, MMX, MMX2, 3DNOW version there is no 3DNOW+MMX2 one
  523. //Plain C versions
  524. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  525. #define COMPILE_C
  526. #endif
  527. #ifdef ARCH_POWERPC
  528. #ifdef HAVE_ALTIVEC
  529. #define COMPILE_ALTIVEC
  530. #endif //HAVE_ALTIVEC
  531. #endif //ARCH_POWERPC
  532. #ifdef ARCH_X86
  533. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  534. #define COMPILE_MMX
  535. #endif
  536. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  537. #define COMPILE_MMX2
  538. #endif
  539. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  540. #define COMPILE_3DNOW
  541. #endif
  542. #endif //ARCH_X86
  543. #undef HAVE_MMX
  544. #undef HAVE_MMX2
  545. #undef HAVE_3DNOW
  546. #undef HAVE_ALTIVEC
  547. #undef ARCH_X86
  548. #ifdef COMPILE_C
  549. #undef HAVE_MMX
  550. #undef HAVE_MMX2
  551. #undef HAVE_3DNOW
  552. #undef ARCH_X86
  553. #define RENAME(a) a ## _C
  554. #include "postprocess_template.c"
  555. #endif
  556. #ifdef ARCH_POWERPC
  557. #ifdef COMPILE_ALTIVEC
  558. #undef RENAME
  559. #define HAVE_ALTIVEC
  560. #define RENAME(a) a ## _altivec
  561. #include "postprocess_altivec_template.c"
  562. #include "postprocess_template.c"
  563. #endif
  564. #endif //ARCH_POWERPC
  565. //MMX versions
  566. #ifdef COMPILE_MMX
  567. #undef RENAME
  568. #define HAVE_MMX
  569. #undef HAVE_MMX2
  570. #undef HAVE_3DNOW
  571. #define ARCH_X86
  572. #define RENAME(a) a ## _MMX
  573. #include "postprocess_template.c"
  574. #endif
  575. //MMX2 versions
  576. #ifdef COMPILE_MMX2
  577. #undef RENAME
  578. #define HAVE_MMX
  579. #define HAVE_MMX2
  580. #undef HAVE_3DNOW
  581. #define ARCH_X86
  582. #define RENAME(a) a ## _MMX2
  583. #include "postprocess_template.c"
  584. #endif
  585. //3DNOW versions
  586. #ifdef COMPILE_3DNOW
  587. #undef RENAME
  588. #define HAVE_MMX
  589. #undef HAVE_MMX2
  590. #define HAVE_3DNOW
  591. #define ARCH_X86
  592. #define RENAME(a) a ## _3DNow
  593. #include "postprocess_template.c"
  594. #endif
  595. // minor note: the HAVE_xyz is messed up after that line so dont use it
  596. static inline void postProcess(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
  597. QP_STORE_T QPs[], int QPStride, int isColor, pp_mode_t *vm, pp_context_t *vc)
  598. {
  599. PPContext *c= (PPContext *)vc;
  600. PPMode *ppMode= (PPMode *)vm;
  601. c->ppMode= *ppMode; //FIXME
  602. // useing ifs here as they are faster than function pointers allthough the
  603. // difference wouldnt be messureable here but its much better because
  604. // someone might exchange the cpu whithout restarting mplayer ;)
  605. #ifdef RUNTIME_CPUDETECT
  606. #ifdef ARCH_X86
  607. // ordered per speed fasterst first
  608. if(c->cpuCaps & PP_CPU_CAPS_MMX2)
  609. postProcess_MMX2(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  610. else if(c->cpuCaps & PP_CPU_CAPS_3DNOW)
  611. postProcess_3DNow(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  612. else if(c->cpuCaps & PP_CPU_CAPS_MMX)
  613. postProcess_MMX(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  614. else
  615. postProcess_C(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  616. #else
  617. #ifdef ARCH_POWERPC
  618. #ifdef HAVE_ALTIVEC
  619. else if(c->cpuCaps & PP_CPU_CAPS_ALTIVEC)
  620. postProcess_altivec(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  621. else
  622. #endif
  623. #endif
  624. postProcess_C(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  625. #endif
  626. #else //RUNTIME_CPUDETECT
  627. #ifdef HAVE_MMX2
  628. postProcess_MMX2(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  629. #elif defined (HAVE_3DNOW)
  630. postProcess_3DNow(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  631. #elif defined (HAVE_MMX)
  632. postProcess_MMX(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  633. #elif defined (HAVE_ALTIVEC)
  634. postProcess_altivec(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  635. #else
  636. postProcess_C(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  637. #endif
  638. #endif //!RUNTIME_CPUDETECT
  639. }
  640. //static void postProcess(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
  641. // QP_STORE_T QPs[], int QPStride, int isColor, struct PPMode *ppMode);
  642. /* -pp Command line Help
  643. */
  644. char *pp_help=
  645. "<filterName>[:<option>[:<option>...]][[,|/][-]<filterName>[:<option>...]]...\n"
  646. "long form example:\n"
  647. "vdeblock:autoq/hdeblock:autoq/linblenddeint default,-vdeblock\n"
  648. "short form example:\n"
  649. "vb:a/hb:a/lb de,-vb\n"
  650. "more examples:\n"
  651. "tn:64:128:256\n"
  652. "Filters Options\n"
  653. "short long name short long option Description\n"
  654. "* * a autoq CPU power dependent enabler\n"
  655. " c chrom chrominance filtering enabled\n"
  656. " y nochrom chrominance filtering disabled\n"
  657. "hb hdeblock (2 threshold) horizontal deblocking filter\n"
  658. " 1. difference factor: default=32, higher -> more deblocking\n"
  659. " 2. flatness threshold: default=39, lower -> more deblocking\n"
  660. " the h & v deblocking filters share these\n"
  661. " so you can't set different thresholds for h / v\n"
  662. "vb vdeblock (2 threshold) vertical deblocking filter\n"
  663. "ha hadeblock (2 threshold) horizontal deblocking filter\n"
  664. "va vadeblock (2 threshold) vertical deblocking filter\n"
  665. "h1 x1hdeblock experimental h deblock filter 1\n"
  666. "v1 x1vdeblock experimental v deblock filter 1\n"
  667. "dr dering deringing filter\n"
  668. "al autolevels automatic brightness / contrast\n"
  669. " f fullyrange stretch luminance to (0..255)\n"
  670. "lb linblenddeint linear blend deinterlacer\n"
  671. "li linipoldeint linear interpolating deinterlace\n"
  672. "ci cubicipoldeint cubic interpolating deinterlacer\n"
  673. "md mediandeint median deinterlacer\n"
  674. "fd ffmpegdeint ffmpeg deinterlacer\n"
  675. "de default hb:a,vb:a,dr:a\n"
  676. "fa fast h1:a,v1:a,dr:a\n"
  677. "tn tmpnoise (3 threshold) temporal noise reducer\n"
  678. " 1. <= 2. <= 3. larger -> stronger filtering\n"
  679. "fq forceQuant <quantizer> force quantizer\n"
  680. ;
  681. pp_mode_t *pp_get_mode_by_name_and_quality(char *name, int quality)
  682. {
  683. char temp[GET_MODE_BUFFER_SIZE];
  684. char *p= temp;
  685. char *filterDelimiters= ",/";
  686. char *optionDelimiters= ":";
  687. struct PPMode *ppMode;
  688. char *filterToken;
  689. ppMode= memalign(8, sizeof(PPMode));
  690. ppMode->lumMode= 0;
  691. ppMode->chromMode= 0;
  692. ppMode->maxTmpNoise[0]= 700;
  693. ppMode->maxTmpNoise[1]= 1500;
  694. ppMode->maxTmpNoise[2]= 3000;
  695. ppMode->maxAllowedY= 234;
  696. ppMode->minAllowedY= 16;
  697. ppMode->baseDcDiff= 256/8;
  698. ppMode->flatnessThreshold= 56-16-1;
  699. ppMode->maxClippedThreshold= 0.01;
  700. ppMode->error=0;
  701. strncpy(temp, name, GET_MODE_BUFFER_SIZE);
  702. if(verbose>1) printf("pp: %s\n", name);
  703. for(;;){
  704. char *filterName;
  705. int q= 1000000; //PP_QUALITY_MAX;
  706. int chrom=-1;
  707. char *option;
  708. char *options[OPTIONS_ARRAY_SIZE];
  709. int i;
  710. int filterNameOk=0;
  711. int numOfUnknownOptions=0;
  712. int enable=1; //does the user want us to enabled or disabled the filter
  713. filterToken= strtok(p, filterDelimiters);
  714. if(filterToken == NULL) break;
  715. p+= strlen(filterToken) + 1; // p points to next filterToken
  716. filterName= strtok(filterToken, optionDelimiters);
  717. if(verbose>1) printf("pp: %s::%s\n", filterToken, filterName);
  718. if(*filterName == '-')
  719. {
  720. enable=0;
  721. filterName++;
  722. }
  723. for(;;){ //for all options
  724. option= strtok(NULL, optionDelimiters);
  725. if(option == NULL) break;
  726. if(verbose>1) printf("pp: option: %s\n", option);
  727. if(!strcmp("autoq", option) || !strcmp("a", option)) q= quality;
  728. else if(!strcmp("nochrom", option) || !strcmp("y", option)) chrom=0;
  729. else if(!strcmp("chrom", option) || !strcmp("c", option)) chrom=1;
  730. else
  731. {
  732. options[numOfUnknownOptions] = option;
  733. numOfUnknownOptions++;
  734. }
  735. if(numOfUnknownOptions >= OPTIONS_ARRAY_SIZE-1) break;
  736. }
  737. options[numOfUnknownOptions] = NULL;
  738. /* replace stuff from the replace Table */
  739. for(i=0; replaceTable[2*i]!=NULL; i++)
  740. {
  741. if(!strcmp(replaceTable[2*i], filterName))
  742. {
  743. int newlen= strlen(replaceTable[2*i + 1]);
  744. int plen;
  745. int spaceLeft;
  746. if(p==NULL) p= temp, *p=0; //last filter
  747. else p--, *p=','; //not last filter
  748. plen= strlen(p);
  749. spaceLeft= p - temp + plen;
  750. if(spaceLeft + newlen >= GET_MODE_BUFFER_SIZE)
  751. {
  752. ppMode->error++;
  753. break;
  754. }
  755. memmove(p + newlen, p, plen+1);
  756. memcpy(p, replaceTable[2*i + 1], newlen);
  757. filterNameOk=1;
  758. }
  759. }
  760. for(i=0; filters[i].shortName!=NULL; i++)
  761. {
  762. // printf("Compareing %s, %s, %s\n", filters[i].shortName,filters[i].longName, filterName);
  763. if( !strcmp(filters[i].longName, filterName)
  764. || !strcmp(filters[i].shortName, filterName))
  765. {
  766. ppMode->lumMode &= ~filters[i].mask;
  767. ppMode->chromMode &= ~filters[i].mask;
  768. filterNameOk=1;
  769. if(!enable) break; // user wants to disable it
  770. if(q >= filters[i].minLumQuality)
  771. ppMode->lumMode|= filters[i].mask;
  772. if(chrom==1 || (chrom==-1 && filters[i].chromDefault))
  773. if(q >= filters[i].minChromQuality)
  774. ppMode->chromMode|= filters[i].mask;
  775. if(filters[i].mask == LEVEL_FIX)
  776. {
  777. int o;
  778. ppMode->minAllowedY= 16;
  779. ppMode->maxAllowedY= 234;
  780. for(o=0; options[o]!=NULL; o++)
  781. {
  782. if( !strcmp(options[o],"fullyrange")
  783. ||!strcmp(options[o],"f"))
  784. {
  785. ppMode->minAllowedY= 0;
  786. ppMode->maxAllowedY= 255;
  787. numOfUnknownOptions--;
  788. }
  789. }
  790. }
  791. else if(filters[i].mask == TEMP_NOISE_FILTER)
  792. {
  793. int o;
  794. int numOfNoises=0;
  795. for(o=0; options[o]!=NULL; o++)
  796. {
  797. char *tail;
  798. ppMode->maxTmpNoise[numOfNoises]=
  799. strtol(options[o], &tail, 0);
  800. if(tail!=options[o])
  801. {
  802. numOfNoises++;
  803. numOfUnknownOptions--;
  804. if(numOfNoises >= 3) break;
  805. }
  806. }
  807. }
  808. else if(filters[i].mask == V_DEBLOCK || filters[i].mask == H_DEBLOCK
  809. || filters[i].mask == V_A_DEBLOCK || filters[i].mask == H_A_DEBLOCK)
  810. {
  811. int o;
  812. for(o=0; options[o]!=NULL && o<2; o++)
  813. {
  814. char *tail;
  815. int val= strtol(options[o], &tail, 0);
  816. if(tail==options[o]) break;
  817. numOfUnknownOptions--;
  818. if(o==0) ppMode->baseDcDiff= val;
  819. else ppMode->flatnessThreshold= val;
  820. }
  821. }
  822. else if(filters[i].mask == FORCE_QUANT)
  823. {
  824. int o;
  825. ppMode->forcedQuant= 15;
  826. for(o=0; options[o]!=NULL && o<1; o++)
  827. {
  828. char *tail;
  829. int val= strtol(options[o], &tail, 0);
  830. if(tail==options[o]) break;
  831. numOfUnknownOptions--;
  832. ppMode->forcedQuant= val;
  833. }
  834. }
  835. }
  836. }
  837. if(!filterNameOk) ppMode->error++;
  838. ppMode->error += numOfUnknownOptions;
  839. }
  840. if(verbose>1) printf("pp: lumMode=%X, chromMode=%X\n", ppMode->lumMode, ppMode->chromMode);
  841. if(ppMode->error)
  842. {
  843. fprintf(stderr, "%d errors in postprocess string \"%s\"\n", ppMode->error, name);
  844. free(ppMode);
  845. return NULL;
  846. }
  847. return ppMode;
  848. }
  849. void pp_free_mode(pp_mode_t *mode){
  850. if(mode) free(mode);
  851. }
  852. static void reallocAlign(void **p, int alignment, int size){
  853. if(*p) free(*p);
  854. *p= memalign(alignment, size);
  855. memset(*p, 0, size);
  856. }
  857. static void reallocBuffers(PPContext *c, int width, int height, int stride, int qpStride){
  858. int mbWidth = (width+15)>>4;
  859. int mbHeight= (height+15)>>4;
  860. int i;
  861. c->stride= stride;
  862. c->qpStride= qpStride;
  863. reallocAlign((void **)&c->tempDst, 8, stride*24);
  864. reallocAlign((void **)&c->tempSrc, 8, stride*24);
  865. reallocAlign((void **)&c->tempBlocks, 8, 2*16*8);
  866. reallocAlign((void **)&c->yHistogram, 8, 256*sizeof(uint64_t));
  867. for(i=0; i<256; i++)
  868. c->yHistogram[i]= width*height/64*15/256;
  869. for(i=0; i<3; i++)
  870. {
  871. //Note:the +17*1024 is just there so i dont have to worry about r/w over te end
  872. reallocAlign((void **)&c->tempBlured[i], 8, stride*mbHeight*16 + 17*1024);
  873. reallocAlign((void **)&c->tempBluredPast[i], 8, 256*((height+7)&(~7))/2 + 17*1024);//FIXME size
  874. }
  875. reallocAlign((void **)&c->deintTemp, 8, 2*width+32);
  876. reallocAlign((void **)&c->nonBQPTable, 8, qpStride*mbHeight*sizeof(QP_STORE_T));
  877. reallocAlign((void **)&c->stdQPTable, 8, qpStride*mbHeight*sizeof(QP_STORE_T));
  878. reallocAlign((void **)&c->forcedQPTable, 8, mbWidth*sizeof(QP_STORE_T));
  879. }
  880. static void global_init(void){
  881. int i;
  882. memset(clip_table, 0, 256);
  883. for(i=256; i<512; i++)
  884. clip_table[i]= i;
  885. memset(clip_table+512, 0, 256);
  886. }
  887. pp_context_t *pp_get_context(int width, int height, int cpuCaps){
  888. PPContext *c= memalign(32, sizeof(PPContext));
  889. int stride= (width+15)&(~15); //assumed / will realloc if needed
  890. int qpStride= (width+15)/16 + 2; //assumed / will realloc if needed
  891. global_init();
  892. memset(c, 0, sizeof(PPContext));
  893. c->cpuCaps= cpuCaps;
  894. if(cpuCaps&PP_FORMAT){
  895. c->hChromaSubSample= cpuCaps&0x3;
  896. c->vChromaSubSample= (cpuCaps>>4)&0x3;
  897. }else{
  898. c->hChromaSubSample= 1;
  899. c->vChromaSubSample= 1;
  900. }
  901. reallocBuffers(c, width, height, stride, qpStride);
  902. c->frameNum=-1;
  903. return c;
  904. }
  905. void pp_free_context(void *vc){
  906. PPContext *c = (PPContext*)vc;
  907. int i;
  908. for(i=0; i<3; i++) free(c->tempBlured[i]);
  909. for(i=0; i<3; i++) free(c->tempBluredPast[i]);
  910. free(c->tempBlocks);
  911. free(c->yHistogram);
  912. free(c->tempDst);
  913. free(c->tempSrc);
  914. free(c->deintTemp);
  915. free(c->stdQPTable);
  916. free(c->nonBQPTable);
  917. free(c->forcedQPTable);
  918. memset(c, 0, sizeof(PPContext));
  919. free(c);
  920. }
  921. void pp_postprocess(uint8_t * src[3], int srcStride[3],
  922. uint8_t * dst[3], int dstStride[3],
  923. int width, int height,
  924. QP_STORE_T *QP_store, int QPStride,
  925. pp_mode_t *vm, void *vc, int pict_type)
  926. {
  927. int mbWidth = (width+15)>>4;
  928. int mbHeight= (height+15)>>4;
  929. PPMode *mode = (PPMode*)vm;
  930. PPContext *c = (PPContext*)vc;
  931. int minStride= MAX(srcStride[0], dstStride[0]);
  932. if(c->stride < minStride || c->qpStride < QPStride)
  933. reallocBuffers(c, width, height,
  934. MAX(minStride, c->stride),
  935. MAX(c->qpStride, QPStride));
  936. if(QP_store==NULL || (mode->lumMode & FORCE_QUANT))
  937. {
  938. int i;
  939. QP_store= c->forcedQPTable;
  940. QPStride= 0;
  941. if(mode->lumMode & FORCE_QUANT)
  942. for(i=0; i<mbWidth; i++) QP_store[i]= mode->forcedQuant;
  943. else
  944. for(i=0; i<mbWidth; i++) QP_store[i]= 1;
  945. }
  946. //printf("pict_type:%d\n", pict_type);
  947. if(pict_type & PP_PICT_TYPE_QP2){
  948. int i;
  949. const int count= mbHeight * QPStride;
  950. for(i=0; i<(count>>2); i++){
  951. ((uint32_t*)c->stdQPTable)[i] = (((uint32_t*)QP_store)[i]>>1) & 0x7F7F7F7F;
  952. }
  953. for(i<<=2; i<count; i++){
  954. c->stdQPTable[i] = QP_store[i]>>1;
  955. }
  956. QP_store= c->stdQPTable;
  957. }
  958. if(0){
  959. int x,y;
  960. for(y=0; y<mbHeight; y++){
  961. for(x=0; x<mbWidth; x++){
  962. printf("%2d ", QP_store[x + y*QPStride]);
  963. }
  964. printf("\n");
  965. }
  966. printf("\n");
  967. }
  968. if((pict_type&7)!=3)
  969. {
  970. int i;
  971. const int count= mbHeight * QPStride;
  972. for(i=0; i<(count>>2); i++){
  973. ((uint32_t*)c->nonBQPTable)[i] = ((uint32_t*)QP_store)[i] & 0x3F3F3F3F;
  974. }
  975. for(i<<=2; i<count; i++){
  976. c->nonBQPTable[i] = QP_store[i] & 0x3F;
  977. }
  978. }
  979. if(verbose>2)
  980. {
  981. printf("using npp filters 0x%X/0x%X\n", mode->lumMode, mode->chromMode);
  982. }
  983. postProcess(src[0], srcStride[0], dst[0], dstStride[0],
  984. width, height, QP_store, QPStride, 0, mode, c);
  985. width = (width )>>c->hChromaSubSample;
  986. height = (height)>>c->vChromaSubSample;
  987. if(mode->chromMode)
  988. {
  989. postProcess(src[1], srcStride[1], dst[1], dstStride[1],
  990. width, height, QP_store, QPStride, 1, mode, c);
  991. postProcess(src[2], srcStride[2], dst[2], dstStride[2],
  992. width, height, QP_store, QPStride, 2, mode, c);
  993. }
  994. else if(srcStride[1] == dstStride[1] && srcStride[2] == dstStride[2])
  995. {
  996. memcpy(dst[1], src[1], srcStride[1]*height);
  997. memcpy(dst[2], src[2], srcStride[2]*height);
  998. }
  999. else
  1000. {
  1001. int y;
  1002. for(y=0; y<height; y++)
  1003. {
  1004. memcpy(&(dst[1][y*dstStride[1]]), &(src[1][y*srcStride[1]]), width);
  1005. memcpy(&(dst[2][y*dstStride[2]]), &(src[2][y*srcStride[2]]), width);
  1006. }
  1007. }
  1008. }