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.

919 lines
25KB

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