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.

993 lines
27KB

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