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.

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