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.

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