You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1144 lines
44KB

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