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.

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