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.

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