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.

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