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.

879 lines
24KB

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