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.

853 lines
24KB

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