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