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.

895 lines
25KB

  1. /*
  2. Copyright (C) 2001-2002 Michael Niedermayer (michaelni@gmx.at)
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /*
  16. C MMX MMX2 3DNow
  17. isVertDC Ec Ec
  18. isVertMinMaxOk Ec Ec
  19. doVertLowPass E e e
  20. doVertDefFilter Ec Ec e e
  21. isHorizDC Ec Ec
  22. isHorizMinMaxOk a E
  23. doHorizLowPass E e e
  24. doHorizDefFilter Ec Ec e e
  25. deRing E e e*
  26. Vertical RKAlgo1 E a a
  27. Horizontal RKAlgo1 a a
  28. Vertical X1# a E E
  29. Horizontal X1# a E E
  30. LinIpolDeinterlace e E E*
  31. CubicIpolDeinterlace a e e*
  32. LinBlendDeinterlace e E E*
  33. MedianDeinterlace# Ec Ec
  34. TempDeNoiser# E e e
  35. * i dont have a 3dnow CPU -> its untested, but noone said it doesnt work so it seems to work
  36. # more or less selfinvented filters so the exactness isnt too meaningfull
  37. E = Exact implementation
  38. e = allmost exact implementation (slightly different rounding,...)
  39. a = alternative / approximate impl
  40. c = checked against the other implementations (-vo md5)
  41. */
  42. /*
  43. TODO:
  44. reduce the time wasted on the mem transfer
  45. unroll stuff if instructions depend too much on the prior one
  46. move YScale thing to the end instead of fixing QP
  47. write a faster and higher quality deblocking filter :)
  48. make the mainloop more flexible (variable number of blocks at once
  49. (the if/else stuff per block is slowing things down)
  50. compare the quality & speed of all filters
  51. split this huge file
  52. optimize c versions
  53. try to unroll inner for(x=0 ... loop to avoid these damn if(x ... checks
  54. ...
  55. */
  56. //Changelog: use the CVS log
  57. #include "../config.h"
  58. #include <inttypes.h>
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #ifdef HAVE_MALLOC_H
  63. #include <malloc.h>
  64. #endif
  65. //#undef HAVE_MMX2
  66. //#define HAVE_3DNOW
  67. //#undef HAVE_MMX
  68. //#undef ARCH_X86
  69. //#define DEBUG_BRIGHTNESS
  70. #include "../libvo/fastmemcpy.h"
  71. #include "postprocess.h"
  72. #include "../mangle.h"
  73. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  74. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  75. #define ABS(a) ((a) > 0 ? (a) : (-(a)))
  76. #define SIGN(a) ((a) > 0 ? 1 : -1)
  77. #define GET_MODE_BUFFER_SIZE 500
  78. #define OPTIONS_ARRAY_SIZE 10
  79. #define BLOCK_SIZE 8
  80. #define TEMP_STRIDE 8
  81. //#define NUM_BLOCKS_AT_ONCE 16 //not used yet
  82. #ifdef ARCH_X86
  83. static uint64_t __attribute__((aligned(8))) w05= 0x0005000500050005LL;
  84. static uint64_t __attribute__((aligned(8))) w20= 0x0020002000200020LL;
  85. static uint64_t __attribute__((aligned(8))) b00= 0x0000000000000000LL;
  86. static uint64_t __attribute__((aligned(8))) b01= 0x0101010101010101LL;
  87. static uint64_t __attribute__((aligned(8))) b02= 0x0202020202020202LL;
  88. static uint64_t __attribute__((aligned(8))) b08= 0x0808080808080808LL;
  89. static uint64_t __attribute__((aligned(8))) b80= 0x8080808080808080LL;
  90. #endif
  91. static int verbose= 0;
  92. static const int deringThreshold= 20;
  93. static int cpuCaps=0;
  94. struct PPFilter{
  95. char *shortName;
  96. char *longName;
  97. int chromDefault; // is chrominance filtering on by default if this filter is manually activated
  98. int minLumQuality; // minimum quality to turn luminance filtering on
  99. int minChromQuality; // minimum quality to turn chrominance filtering on
  100. int mask; // Bitmask to turn this filter on
  101. };
  102. typedef struct PPContext{
  103. uint8_t *tempBlocks; //used for the horizontal code
  104. /* we need 64bit here otherwise we´ll going to have a problem
  105. after watching a black picture for 5 hours*/
  106. uint64_t *yHistogram;
  107. uint64_t __attribute__((aligned(8))) packedYOffset;
  108. uint64_t __attribute__((aligned(8))) packedYScale;
  109. /* Temporal noise reducing buffers */
  110. uint8_t *tempBlured[3];
  111. int32_t *tempBluredPast[3];
  112. /* Temporary buffers for handling the last row(s) */
  113. uint8_t *tempDst;
  114. uint8_t *tempSrc;
  115. /* Temporary buffers for handling the last block */
  116. uint8_t *tempDstBlock;
  117. uint8_t *tempSrcBlock;
  118. uint8_t *deintTemp;
  119. uint64_t __attribute__((aligned(8))) pQPb;
  120. uint64_t __attribute__((aligned(8))) pQPb2;
  121. uint64_t __attribute__((aligned(8))) mmxDcOffset[32];
  122. uint64_t __attribute__((aligned(8))) mmxDcThreshold[32];
  123. QP_STORE_T *nonBQPTable;
  124. int QP;
  125. int nonBQP;
  126. int frameNum;
  127. PPMode ppMode;
  128. } PPContext;
  129. static struct PPFilter filters[]=
  130. {
  131. {"hb", "hdeblock", 1, 1, 3, H_DEBLOCK},
  132. {"vb", "vdeblock", 1, 2, 4, V_DEBLOCK},
  133. /* {"hr", "rkhdeblock", 1, 1, 3, H_RK1_FILTER},
  134. {"vr", "rkvdeblock", 1, 2, 4, V_RK1_FILTER},*/
  135. {"h1", "x1hdeblock", 1, 1, 3, H_X1_FILTER},
  136. {"v1", "x1vdeblock", 1, 2, 4, V_X1_FILTER},
  137. {"dr", "dering", 1, 5, 6, DERING},
  138. {"al", "autolevels", 0, 1, 2, LEVEL_FIX},
  139. {"lb", "linblenddeint", 1, 1, 4, LINEAR_BLEND_DEINT_FILTER},
  140. {"li", "linipoldeint", 1, 1, 4, LINEAR_IPOL_DEINT_FILTER},
  141. {"ci", "cubicipoldeint", 1, 1, 4, CUBIC_IPOL_DEINT_FILTER},
  142. {"md", "mediandeint", 1, 1, 4, MEDIAN_DEINT_FILTER},
  143. {"fd", "ffmpegdeint", 1, 1, 4, FFMPEG_DEINT_FILTER},
  144. {"tn", "tmpnoise", 1, 7, 8, TEMP_NOISE_FILTER},
  145. {"fq", "forcequant", 1, 0, 0, FORCE_QUANT},
  146. {NULL, NULL,0,0,0,0} //End Marker
  147. };
  148. static char *replaceTable[]=
  149. {
  150. "default", "hdeblock:a,vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400",
  151. "de", "hdeblock:a,vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400",
  152. "fast", "x1hdeblock:a,x1vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400",
  153. "fa", "x1hdeblock:a,x1vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400",
  154. NULL //End Marker
  155. };
  156. #ifdef ARCH_X86
  157. static inline void unusedVariableWarningFixer()
  158. {
  159. if(w05 + w20 + b00 + b01 + b02 + b08 + b80 == 0) b00=0;
  160. }
  161. #endif
  162. #ifdef ARCH_X86
  163. static inline void prefetchnta(void *p)
  164. {
  165. asm volatile( "prefetchnta (%0)\n\t"
  166. : : "r" (p)
  167. );
  168. }
  169. static inline void prefetcht0(void *p)
  170. {
  171. asm volatile( "prefetcht0 (%0)\n\t"
  172. : : "r" (p)
  173. );
  174. }
  175. static inline void prefetcht1(void *p)
  176. {
  177. asm volatile( "prefetcht1 (%0)\n\t"
  178. : : "r" (p)
  179. );
  180. }
  181. static inline void prefetcht2(void *p)
  182. {
  183. asm volatile( "prefetcht2 (%0)\n\t"
  184. : : "r" (p)
  185. );
  186. }
  187. #endif
  188. int pp_init(int caps){
  189. cpuCaps= caps;
  190. return 0;
  191. }
  192. // The horizontal Functions exist only in C cuz the MMX code is faster with vertical filters and transposing
  193. /**
  194. * Check if the given 8x8 Block is mostly "flat"
  195. */
  196. static inline int isHorizDC(uint8_t src[], int stride, PPContext *c)
  197. {
  198. int numEq= 0;
  199. int y;
  200. const int dcOffset= ((c->QP*c->ppMode.baseDcDiff)>>8) + 1;
  201. const int dcThreshold= dcOffset*2 + 1;
  202. for(y=0; y<BLOCK_SIZE; y++)
  203. {
  204. if(((unsigned)(src[0] - src[1] + dcOffset)) < dcThreshold) numEq++;
  205. if(((unsigned)(src[1] - src[2] + dcOffset)) < dcThreshold) numEq++;
  206. if(((unsigned)(src[2] - src[3] + dcOffset)) < dcThreshold) numEq++;
  207. if(((unsigned)(src[3] - src[4] + dcOffset)) < dcThreshold) numEq++;
  208. if(((unsigned)(src[4] - src[5] + dcOffset)) < dcThreshold) numEq++;
  209. if(((unsigned)(src[5] - src[6] + dcOffset)) < dcThreshold) numEq++;
  210. if(((unsigned)(src[6] - src[7] + dcOffset)) < dcThreshold) numEq++;
  211. src+= stride;
  212. }
  213. return numEq > c->ppMode.flatnessThreshold;
  214. }
  215. /**
  216. * Check if the middle 8x8 Block in the given 8x16 block is flat
  217. */
  218. static inline int isVertDC_C(uint8_t src[], int stride, PPContext *c){
  219. int numEq= 0;
  220. int y;
  221. const int dcOffset= ((c->QP*c->ppMode.baseDcDiff)>>8) + 1;
  222. const int dcThreshold= dcOffset*2 + 1;
  223. src+= stride*4; // src points to begin of the 8x8 Block
  224. for(y=0; y<BLOCK_SIZE-1; y++)
  225. {
  226. if(((unsigned)(src[0] - src[0+stride] + dcOffset)) < dcThreshold) numEq++;
  227. if(((unsigned)(src[1] - src[1+stride] + dcOffset)) < dcThreshold) numEq++;
  228. if(((unsigned)(src[2] - src[2+stride] + dcOffset)) < dcThreshold) numEq++;
  229. if(((unsigned)(src[3] - src[3+stride] + dcOffset)) < dcThreshold) numEq++;
  230. if(((unsigned)(src[4] - src[4+stride] + dcOffset)) < dcThreshold) numEq++;
  231. if(((unsigned)(src[5] - src[5+stride] + dcOffset)) < dcThreshold) numEq++;
  232. if(((unsigned)(src[6] - src[6+stride] + dcOffset)) < dcThreshold) numEq++;
  233. if(((unsigned)(src[7] - src[7+stride] + dcOffset)) < dcThreshold) numEq++;
  234. src+= stride;
  235. }
  236. return numEq > c->ppMode.flatnessThreshold;
  237. }
  238. static inline int isHorizMinMaxOk(uint8_t src[], int stride, int QP)
  239. {
  240. if(abs(src[0] - src[7]) > 2*QP) return 0;
  241. return 1;
  242. }
  243. static inline void doHorizDefFilter(uint8_t dst[], int stride, int QP)
  244. {
  245. int y;
  246. for(y=0; y<BLOCK_SIZE; y++)
  247. {
  248. const int middleEnergy= 5*(dst[4] - dst[5]) + 2*(dst[2] - dst[5]);
  249. if(ABS(middleEnergy) < 8*QP)
  250. {
  251. const int q=(dst[3] - dst[4])/2;
  252. const int leftEnergy= 5*(dst[2] - dst[1]) + 2*(dst[0] - dst[3]);
  253. const int rightEnergy= 5*(dst[6] - dst[5]) + 2*(dst[4] - dst[7]);
  254. int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
  255. d= MAX(d, 0);
  256. d= (5*d + 32) >> 6;
  257. d*= SIGN(-middleEnergy);
  258. if(q>0)
  259. {
  260. d= d<0 ? 0 : d;
  261. d= d>q ? q : d;
  262. }
  263. else
  264. {
  265. d= d>0 ? 0 : d;
  266. d= d<q ? q : d;
  267. }
  268. dst[3]-= d;
  269. dst[4]+= d;
  270. }
  271. dst+= stride;
  272. }
  273. }
  274. /**
  275. * Do a horizontal low pass filter on the 10x8 block (dst points to middle 8x8 Block)
  276. * using the 9-Tap Filter (1,1,2,2,4,2,2,1,1)/16 (C version)
  277. */
  278. static inline void doHorizLowPass(uint8_t dst[], int stride, int QP)
  279. {
  280. int y;
  281. for(y=0; y<BLOCK_SIZE; y++)
  282. {
  283. const int first= ABS(dst[-1] - dst[0]) < QP ? dst[-1] : dst[0];
  284. const int last= ABS(dst[8] - dst[7]) < QP ? dst[8] : dst[7];
  285. int sums[9];
  286. sums[0] = first + dst[0];
  287. sums[1] = dst[0] + dst[1];
  288. sums[2] = dst[1] + dst[2];
  289. sums[3] = dst[2] + dst[3];
  290. sums[4] = dst[3] + dst[4];
  291. sums[5] = dst[4] + dst[5];
  292. sums[6] = dst[5] + dst[6];
  293. sums[7] = dst[6] + dst[7];
  294. sums[8] = dst[7] + last;
  295. dst[0]= ((sums[0]<<2) + ((first + sums[2])<<1) + sums[4] + 8)>>4;
  296. dst[1]= ((dst[1]<<2) + ((first + sums[0] + sums[3])<<1) + sums[5] + 8)>>4;
  297. dst[2]= ((dst[2]<<2) + ((first + sums[1] + sums[4])<<1) + sums[6] + 8)>>4;
  298. dst[3]= ((dst[3]<<2) + ((sums[2] + sums[5])<<1) + sums[0] + sums[7] + 8)>>4;
  299. dst[4]= ((dst[4]<<2) + ((sums[3] + sums[6])<<1) + sums[1] + sums[8] + 8)>>4;
  300. dst[5]= ((dst[5]<<2) + ((last + sums[7] + sums[4])<<1) + sums[2] + 8)>>4;
  301. dst[6]= (((last + dst[6])<<2) + ((dst[7] + sums[5])<<1) + sums[3] + 8)>>4;
  302. dst[7]= ((sums[8]<<2) + ((last + sums[6])<<1) + sums[4] + 8)>>4;
  303. dst+= stride;
  304. }
  305. }
  306. /**
  307. * Experimental Filter 1 (Horizontal)
  308. * will not damage linear gradients
  309. * Flat blocks should look like they where passed through the (1,1,2,2,4,2,2,1,1) 9-Tap filter
  310. * can only smooth blocks at the expected locations (it cant smooth them if they did move)
  311. * MMX2 version does correct clipping C version doesnt
  312. * not identical with the vertical one
  313. */
  314. static inline void horizX1Filter(uint8_t *src, int stride, int QP)
  315. {
  316. int y;
  317. static uint64_t *lut= NULL;
  318. if(lut==NULL)
  319. {
  320. int i;
  321. lut= (uint64_t*)memalign(8, 256*8);
  322. for(i=0; i<256; i++)
  323. {
  324. int v= i < 128 ? 2*i : 2*(i-256);
  325. /*
  326. //Simulate 112242211 9-Tap filter
  327. uint64_t a= (v/16) & 0xFF;
  328. uint64_t b= (v/8) & 0xFF;
  329. uint64_t c= (v/4) & 0xFF;
  330. uint64_t d= (3*v/8) & 0xFF;
  331. */
  332. //Simulate piecewise linear interpolation
  333. uint64_t a= (v/16) & 0xFF;
  334. uint64_t b= (v*3/16) & 0xFF;
  335. uint64_t c= (v*5/16) & 0xFF;
  336. uint64_t d= (7*v/16) & 0xFF;
  337. uint64_t A= (0x100 - a)&0xFF;
  338. uint64_t B= (0x100 - b)&0xFF;
  339. uint64_t C= (0x100 - c)&0xFF;
  340. uint64_t D= (0x100 - c)&0xFF;
  341. lut[i] = (a<<56) | (b<<48) | (c<<40) | (d<<32) |
  342. (D<<24) | (C<<16) | (B<<8) | (A);
  343. //lut[i] = (v<<32) | (v<<24);
  344. }
  345. }
  346. for(y=0; y<BLOCK_SIZE; y++)
  347. {
  348. int a= src[1] - src[2];
  349. int b= src[3] - src[4];
  350. int c= src[5] - src[6];
  351. int d= MAX(ABS(b) - (ABS(a) + ABS(c))/2, 0);
  352. if(d < QP)
  353. {
  354. int v = d * SIGN(-b);
  355. src[1] +=v/8;
  356. src[2] +=v/4;
  357. src[3] +=3*v/8;
  358. src[4] -=3*v/8;
  359. src[5] -=v/4;
  360. src[6] -=v/8;
  361. }
  362. src+=stride;
  363. }
  364. }
  365. //Note: we have C, MMX, MMX2, 3DNOW version there is no 3DNOW+MMX2 one
  366. //Plain C versions
  367. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  368. #define COMPILE_C
  369. #endif
  370. #ifdef ARCH_X86
  371. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  372. #define COMPILE_MMX
  373. #endif
  374. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  375. #define COMPILE_MMX2
  376. #endif
  377. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  378. #define COMPILE_3DNOW
  379. #endif
  380. #endif //ARCH_X86
  381. #undef HAVE_MMX
  382. #undef HAVE_MMX2
  383. #undef HAVE_3DNOW
  384. #undef ARCH_X86
  385. #ifdef COMPILE_C
  386. #undef HAVE_MMX
  387. #undef HAVE_MMX2
  388. #undef HAVE_3DNOW
  389. #undef ARCH_X86
  390. #define RENAME(a) a ## _C
  391. #include "postprocess_template.c"
  392. #endif
  393. //MMX versions
  394. #ifdef COMPILE_MMX
  395. #undef RENAME
  396. #define HAVE_MMX
  397. #undef HAVE_MMX2
  398. #undef HAVE_3DNOW
  399. #define ARCH_X86
  400. #define RENAME(a) a ## _MMX
  401. #include "postprocess_template.c"
  402. #endif
  403. //MMX2 versions
  404. #ifdef COMPILE_MMX2
  405. #undef RENAME
  406. #define HAVE_MMX
  407. #define HAVE_MMX2
  408. #undef HAVE_3DNOW
  409. #define ARCH_X86
  410. #define RENAME(a) a ## _MMX2
  411. #include "postprocess_template.c"
  412. #endif
  413. //3DNOW versions
  414. #ifdef COMPILE_3DNOW
  415. #undef RENAME
  416. #define HAVE_MMX
  417. #undef HAVE_MMX2
  418. #define HAVE_3DNOW
  419. #define ARCH_X86
  420. #define RENAME(a) a ## _3DNow
  421. #include "postprocess_template.c"
  422. #endif
  423. // minor note: the HAVE_xyz is messed up after that line so dont use it
  424. static inline void postProcess(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
  425. QP_STORE_T QPs[], int QPStride, int isColor, PPMode *ppMode, void *vc)
  426. {
  427. PPContext *c= (PPContext *)vc;
  428. c->ppMode= *ppMode; //FIXME
  429. // useing ifs here as they are faster than function pointers allthough the
  430. // difference wouldnt be messureable here but its much better because
  431. // someone might exchange the cpu whithout restarting mplayer ;)
  432. #ifdef RUNTIME_CPUDETECT
  433. #ifdef ARCH_X86
  434. // ordered per speed fasterst first
  435. if(cpuCaps & PP_CPU_CAPS_MMX2)
  436. postProcess_MMX2(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  437. else if(cpuCaps & PP_CPU_CAPS_3DNOW)
  438. postProcess_3DNow(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  439. else if(cpuCaps & PP_CPU_CAPS_MMX)
  440. postProcess_MMX(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  441. else
  442. postProcess_C(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  443. #else
  444. postProcess_C(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  445. #endif
  446. #else //RUNTIME_CPUDETECT
  447. #ifdef HAVE_MMX2
  448. postProcess_MMX2(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  449. #elif defined (HAVE_3DNOW)
  450. postProcess_3DNow(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  451. #elif defined (HAVE_MMX)
  452. postProcess_MMX(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  453. #else
  454. postProcess_C(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  455. #endif
  456. #endif //!RUNTIME_CPUDETECT
  457. }
  458. //static void postProcess(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
  459. // QP_STORE_T QPs[], int QPStride, int isColor, struct PPMode *ppMode);
  460. /* -pp Command line Help
  461. */
  462. char *postproc_help=
  463. "-npp <filterName>[:<option>[:<option>...]][,[-]<filterName>[:<option>...]]...\n"
  464. "long form example:\n"
  465. "-npp vdeblock:autoq,hdeblock:autoq,linblenddeint -npp default,-vdeblock\n"
  466. "short form example:\n"
  467. "-npp vb:a,hb:a,lb -npp de,-vb\n"
  468. "more examples:\n"
  469. "-npp tn:64:128:256\n"
  470. "Filters Options\n"
  471. "short long name short long option Description\n"
  472. "* * a autoq cpu power dependant enabler\n"
  473. " c chrom chrominance filtring enabled\n"
  474. " y nochrom chrominance filtring disabled\n"
  475. "hb hdeblock (2 Threshold) horizontal deblocking filter\n"
  476. " 1. Threshold: default=1, higher -> more deblocking\n"
  477. " 2. Threshold: default=40, lower -> more deblocking\n"
  478. " the h & v deblocking filters share these\n"
  479. " so u cant set different thresholds for h / v\n"
  480. "vb vdeblock (2 Threshold) vertical deblocking filter\n"
  481. "h1 x1hdeblock Experimental h deblock filter 1\n"
  482. "v1 x1vdeblock Experimental v deblock filter 1\n"
  483. "dr dering Deringing filter\n"
  484. "al autolevels automatic brightness / contrast\n"
  485. " f fullyrange stretch luminance to (0..255)\n"
  486. "lb linblenddeint linear blend deinterlacer\n"
  487. "li linipoldeint linear interpolating deinterlace\n"
  488. "ci cubicipoldeint cubic interpolating deinterlacer\n"
  489. "md mediandeint median deinterlacer\n"
  490. "fd ffmpegdeint ffmpeg deinterlacer\n"
  491. "de default hb:a,vb:a,dr:a,al\n"
  492. "fa fast h1:a,v1:a,dr:a,al\n"
  493. "tn tmpnoise (3 Thresholds) Temporal Noise Reducer\n"
  494. " 1. <= 2. <= 3. larger -> stronger filtering\n"
  495. "fq forceQuant <quantizer> Force quantizer\n"
  496. ;
  497. /**
  498. * returns a PPMode struct which will have a non 0 error variable if an error occured
  499. * name is the string after "-pp" on the command line
  500. * quality is a number from 0 to GET_PP_QUALITY_MAX
  501. */
  502. struct PPMode pp_get_mode_by_name_and_quality(char *name, int quality)
  503. {
  504. char temp[GET_MODE_BUFFER_SIZE];
  505. char *p= temp;
  506. char *filterDelimiters= ",/";
  507. char *optionDelimiters= ":";
  508. struct PPMode ppMode;
  509. char *filterToken;
  510. ppMode.lumMode= 0;
  511. ppMode.chromMode= 0;
  512. ppMode.maxTmpNoise[0]= 700;
  513. ppMode.maxTmpNoise[1]= 1500;
  514. ppMode.maxTmpNoise[2]= 3000;
  515. ppMode.maxAllowedY= 234;
  516. ppMode.minAllowedY= 16;
  517. ppMode.baseDcDiff= 256/4;
  518. ppMode.flatnessThreshold=40;
  519. ppMode.flatnessThreshold= 56-16;
  520. ppMode.maxClippedThreshold= 0.01;
  521. ppMode.error=0;
  522. strncpy(temp, name, GET_MODE_BUFFER_SIZE);
  523. if(verbose>1) printf("pp: %s\n", name);
  524. for(;;){
  525. char *filterName;
  526. int q= 1000000; //GET_PP_QUALITY_MAX;
  527. int chrom=-1;
  528. char *option;
  529. char *options[OPTIONS_ARRAY_SIZE];
  530. int i;
  531. int filterNameOk=0;
  532. int numOfUnknownOptions=0;
  533. int enable=1; //does the user want us to enabled or disabled the filter
  534. filterToken= strtok(p, filterDelimiters);
  535. if(filterToken == NULL) break;
  536. p+= strlen(filterToken) + 1; // p points to next filterToken
  537. filterName= strtok(filterToken, optionDelimiters);
  538. if(verbose>1) printf("pp: %s::%s\n", filterToken, filterName);
  539. if(*filterName == '-')
  540. {
  541. enable=0;
  542. filterName++;
  543. }
  544. for(;;){ //for all options
  545. option= strtok(NULL, optionDelimiters);
  546. if(option == NULL) break;
  547. if(verbose>1) printf("pp: option: %s\n", option);
  548. if(!strcmp("autoq", option) || !strcmp("a", option)) q= quality;
  549. else if(!strcmp("nochrom", option) || !strcmp("y", option)) chrom=0;
  550. else if(!strcmp("chrom", option) || !strcmp("c", option)) chrom=1;
  551. else
  552. {
  553. options[numOfUnknownOptions] = option;
  554. numOfUnknownOptions++;
  555. }
  556. if(numOfUnknownOptions >= OPTIONS_ARRAY_SIZE-1) break;
  557. }
  558. options[numOfUnknownOptions] = NULL;
  559. /* replace stuff from the replace Table */
  560. for(i=0; replaceTable[2*i]!=NULL; i++)
  561. {
  562. if(!strcmp(replaceTable[2*i], filterName))
  563. {
  564. int newlen= strlen(replaceTable[2*i + 1]);
  565. int plen;
  566. int spaceLeft;
  567. if(p==NULL) p= temp, *p=0; //last filter
  568. else p--, *p=','; //not last filter
  569. plen= strlen(p);
  570. spaceLeft= p - temp + plen;
  571. if(spaceLeft + newlen >= GET_MODE_BUFFER_SIZE)
  572. {
  573. ppMode.error++;
  574. break;
  575. }
  576. memmove(p + newlen, p, plen+1);
  577. memcpy(p, replaceTable[2*i + 1], newlen);
  578. filterNameOk=1;
  579. }
  580. }
  581. for(i=0; filters[i].shortName!=NULL; i++)
  582. {
  583. // printf("Compareing %s, %s, %s\n", filters[i].shortName,filters[i].longName, filterName);
  584. if( !strcmp(filters[i].longName, filterName)
  585. || !strcmp(filters[i].shortName, filterName))
  586. {
  587. ppMode.lumMode &= ~filters[i].mask;
  588. ppMode.chromMode &= ~filters[i].mask;
  589. filterNameOk=1;
  590. if(!enable) break; // user wants to disable it
  591. if(q >= filters[i].minLumQuality)
  592. ppMode.lumMode|= filters[i].mask;
  593. if(chrom==1 || (chrom==-1 && filters[i].chromDefault))
  594. if(q >= filters[i].minChromQuality)
  595. ppMode.chromMode|= filters[i].mask;
  596. if(filters[i].mask == LEVEL_FIX)
  597. {
  598. int o;
  599. ppMode.minAllowedY= 16;
  600. ppMode.maxAllowedY= 234;
  601. for(o=0; options[o]!=NULL; o++)
  602. {
  603. if( !strcmp(options[o],"fullyrange")
  604. ||!strcmp(options[o],"f"))
  605. {
  606. ppMode.minAllowedY= 0;
  607. ppMode.maxAllowedY= 255;
  608. numOfUnknownOptions--;
  609. }
  610. }
  611. }
  612. else if(filters[i].mask == TEMP_NOISE_FILTER)
  613. {
  614. int o;
  615. int numOfNoises=0;
  616. for(o=0; options[o]!=NULL; o++)
  617. {
  618. char *tail;
  619. ppMode.maxTmpNoise[numOfNoises]=
  620. strtol(options[o], &tail, 0);
  621. if(tail!=options[o])
  622. {
  623. numOfNoises++;
  624. numOfUnknownOptions--;
  625. if(numOfNoises >= 3) break;
  626. }
  627. }
  628. }
  629. else if(filters[i].mask == V_DEBLOCK || filters[i].mask == H_DEBLOCK)
  630. {
  631. int o;
  632. for(o=0; options[o]!=NULL && o<2; o++)
  633. {
  634. char *tail;
  635. int val= strtol(options[o], &tail, 0);
  636. if(tail==options[o]) break;
  637. numOfUnknownOptions--;
  638. if(o==0) ppMode.baseDcDiff= val;
  639. else ppMode.flatnessThreshold= val;
  640. }
  641. }
  642. else if(filters[i].mask == FORCE_QUANT)
  643. {
  644. int o;
  645. ppMode.forcedQuant= 15;
  646. for(o=0; options[o]!=NULL && o<1; o++)
  647. {
  648. char *tail;
  649. int val= strtol(options[o], &tail, 0);
  650. if(tail==options[o]) break;
  651. numOfUnknownOptions--;
  652. ppMode.forcedQuant= val;
  653. }
  654. }
  655. }
  656. }
  657. if(!filterNameOk) ppMode.error++;
  658. ppMode.error += numOfUnknownOptions;
  659. }
  660. if(verbose>1) printf("pp: lumMode=%X, chromMode=%X\n", ppMode.lumMode, ppMode.chromMode);
  661. return ppMode;
  662. }
  663. void *pp_get_context(int width, int height){
  664. PPContext *c= memalign(32, sizeof(PPContext));
  665. int i;
  666. int mbWidth = (width+15)>>4;
  667. int mbHeight= (height+15)>>4;
  668. c->tempBlocks= (uint8_t*)memalign(8, 2*16*8);
  669. c->yHistogram= (uint64_t*)memalign(8, 256*sizeof(uint64_t));
  670. for(i=0; i<256; i++)
  671. c->yHistogram[i]= width*height/64*15/256;
  672. for(i=0; i<3; i++)
  673. {
  674. //Note:the +17*1024 is just there so i dont have to worry about r/w over te end
  675. c->tempBlured[i]= (uint8_t*)memalign(8, ((width+7)&(~7))*2*((height+7)&(~7)) + 17*1024); //FIXME dstStride instead of width
  676. c->tempBluredPast[i]= (uint32_t*)memalign(8, 256*((height+7)&(~7))/2 + 17*1024);
  677. memset(c->tempBlured[i], 0, ((width+7)&(~7))*2*((height+7)&(~7)) + 17*1024);
  678. memset(c->tempBluredPast[i], 0, 256*((height+7)&(~7))/2 + 17*1024);
  679. }
  680. c->tempDst= (uint8_t*)memalign(8, 1024*24);
  681. c->tempSrc= (uint8_t*)memalign(8, 1024*24);
  682. c->tempDstBlock= (uint8_t*)memalign(8, 1024*24);
  683. c->tempSrcBlock= (uint8_t*)memalign(8, 1024*24);
  684. c->deintTemp= (uint8_t*)memalign(8, width+16);
  685. c->nonBQPTable= (QP_STORE_T*)memalign(8, mbWidth*mbHeight*sizeof(QP_STORE_T));
  686. memset(c->nonBQPTable, 0, mbWidth*mbHeight*sizeof(QP_STORE_T));
  687. c->frameNum=-1;
  688. return c;
  689. }
  690. void pp_free_context(void *vc){
  691. PPContext *c = (PPContext*)vc;
  692. int i;
  693. for(i=0; i<3; i++) free(c->tempBlured[i]);
  694. for(i=0; i<3; i++) free(c->tempBluredPast[i]);
  695. free(c->tempBlocks);
  696. free(c->yHistogram);
  697. free(c->tempDst);
  698. free(c->tempSrc);
  699. free(c->tempDstBlock);
  700. free(c->tempSrcBlock);
  701. free(c->deintTemp);
  702. free(c->nonBQPTable);
  703. free(c);
  704. }
  705. void pp_postprocess(uint8_t * src[3], int srcStride[3],
  706. uint8_t * dst[3], int dstStride[3],
  707. int width, int height,
  708. QP_STORE_T *QP_store, int QPStride,
  709. PPMode *mode, void *vc, int pict_type)
  710. {
  711. int mbWidth = (width+15)>>4;
  712. int mbHeight= (height+15)>>4;
  713. QP_STORE_T quantArray[2048/8];
  714. PPContext *c = (PPContext*)vc;
  715. if(QP_store==NULL || (mode->lumMode & FORCE_QUANT))
  716. {
  717. int i;
  718. QP_store= quantArray;
  719. QPStride= 0;
  720. if(mode->lumMode & FORCE_QUANT)
  721. for(i=0; i<2048/8; i++) quantArray[i]= mode->forcedQuant;
  722. else
  723. for(i=0; i<2048/8; i++) quantArray[i]= 1;
  724. }
  725. if(0){
  726. int x,y;
  727. for(y=0; y<mbHeight; y++){
  728. for(x=0; x<mbWidth; x++){
  729. printf("%2d ", QP_store[x + y*QPStride]);
  730. }
  731. printf("\n");
  732. }
  733. printf("\n");
  734. }
  735. //printf("pict_type:%d\n", pict_type);
  736. if(pict_type!=3)
  737. {
  738. int x,y;
  739. for(y=0; y<mbHeight; y++){
  740. for(x=0; x<mbWidth; x++){
  741. int qscale= QP_store[x + y*QPStride];
  742. if(qscale&~31)
  743. qscale=31;
  744. c->nonBQPTable[y*mbWidth + x]= qscale;
  745. }
  746. }
  747. }
  748. if(verbose>2)
  749. {
  750. printf("using npp filters 0x%X/0x%X\n", mode->lumMode, mode->chromMode);
  751. }
  752. postProcess(src[0], srcStride[0], dst[0], dstStride[0],
  753. width, height, QP_store, QPStride, 0, mode, c);
  754. width = (width +1)>>1;
  755. height = (height+1)>>1;
  756. if(mode->chromMode)
  757. {
  758. postProcess(src[1], srcStride[1], dst[1], dstStride[1],
  759. width, height, QP_store, QPStride, 1, mode, c);
  760. postProcess(src[2], srcStride[2], dst[2], dstStride[2],
  761. width, height, QP_store, QPStride, 2, mode, c);
  762. }
  763. else if(srcStride[1] == dstStride[1] && srcStride[2] == dstStride[2])
  764. {
  765. memcpy(dst[1], src[1], srcStride[1]*height);
  766. memcpy(dst[2], src[2], srcStride[2]*height);
  767. }
  768. else
  769. {
  770. int y;
  771. for(y=0; y<height; y++)
  772. {
  773. memcpy(&(dst[1][y*dstStride[1]]), &(src[1][y*srcStride[1]]), width);
  774. memcpy(&(dst[2][y*dstStride[2]]), &(src[2][y*srcStride[2]]), width);
  775. }
  776. }
  777. }