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.

1155 lines
44KB

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