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.

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