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.

887 lines
26KB

  1. /*
  2. Copyright (C) 2001 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. implement everything in C at least (done at the moment but ...)
  46. unroll stuff if instructions depend too much on the prior one
  47. we use 8x8 blocks for the horizontal filters, opendivx seems to use 8x4?
  48. move YScale thing to the end instead of fixing QP
  49. write a faster and higher quality deblocking filter :)
  50. make the mainloop more flexible (variable number of blocks at once
  51. (the if/else stuff per block is slowing things down)
  52. compare the quality & speed of all filters
  53. split this huge file
  54. border remover
  55. optimize c versions
  56. try to unroll inner for(x=0 ... loop to avoid these damn if(x ... checks
  57. smart blur
  58. commandline option for the deblock / dering thresholds
  59. put fastmemcpy back
  60. dont use #ifdef ARCH_X86 for the asm stuff ... cross compilers? (note cpudetect uses ARCH_X86)
  61. ...
  62. */
  63. //Changelog: use the CVS log
  64. #include "../config.h"
  65. #include <inttypes.h>
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69. #ifdef HAVE_MALLOC_H
  70. #include <malloc.h>
  71. #endif
  72. //#undef HAVE_MMX2
  73. //#define HAVE_3DNOW
  74. //#undef HAVE_MMX
  75. //#undef ARCH_X86
  76. //#define DEBUG_BRIGHTNESS
  77. //#include "../libvo/fastmemcpy.h"
  78. #include "postprocess.h"
  79. #include "../cpudetect.h"
  80. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  81. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  82. #define ABS(a) ((a) > 0 ? (a) : (-(a)))
  83. #define SIGN(a) ((a) > 0 ? 1 : -1)
  84. #define GET_MODE_BUFFER_SIZE 500
  85. #define OPTIONS_ARRAY_SIZE 10
  86. #ifdef ARCH_X86
  87. #define CAN_COMPILE_X86_ASM
  88. #endif
  89. #ifdef CAN_COMPILE_X86_ASM
  90. static volatile uint64_t __attribute__((aligned(8))) packedYOffset= 0x0000000000000000LL;
  91. static volatile uint64_t __attribute__((aligned(8))) packedYScale= 0x0100010001000100LL;
  92. static uint64_t __attribute__((aligned(8))) w05= 0x0005000500050005LL;
  93. static uint64_t __attribute__((aligned(8))) w20= 0x0020002000200020LL;
  94. static uint64_t __attribute__((aligned(8))) w1400= 0x1400140014001400LL;
  95. static uint64_t __attribute__((aligned(8))) bm00000001= 0x00000000000000FFLL;
  96. static uint64_t __attribute__((aligned(8))) bm00010000= 0x000000FF00000000LL;
  97. static uint64_t __attribute__((aligned(8))) bm00001000= 0x00000000FF000000LL;
  98. static uint64_t __attribute__((aligned(8))) bm10000000= 0xFF00000000000000LL;
  99. static uint64_t __attribute__((aligned(8))) bm10000001= 0xFF000000000000FFLL;
  100. static uint64_t __attribute__((aligned(8))) bm11000011= 0xFFFF00000000FFFFLL;
  101. static uint64_t __attribute__((aligned(8))) bm00000011= 0x000000000000FFFFLL;
  102. static uint64_t __attribute__((aligned(8))) bm11111110= 0xFFFFFFFFFFFFFF00LL;
  103. static uint64_t __attribute__((aligned(8))) bm11000000= 0xFFFF000000000000LL;
  104. static uint64_t __attribute__((aligned(8))) bm00011000= 0x000000FFFF000000LL;
  105. static uint64_t __attribute__((aligned(8))) bm00110011= 0x0000FFFF0000FFFFLL;
  106. static uint64_t __attribute__((aligned(8))) bm11001100= 0xFFFF0000FFFF0000LL;
  107. static uint64_t __attribute__((aligned(8))) b00= 0x0000000000000000LL;
  108. static uint64_t __attribute__((aligned(8))) b01= 0x0101010101010101LL;
  109. static uint64_t __attribute__((aligned(8))) b02= 0x0202020202020202LL;
  110. static uint64_t __attribute__((aligned(8))) b0F= 0x0F0F0F0F0F0F0F0FLL;
  111. static uint64_t __attribute__((aligned(8))) b04= 0x0404040404040404LL;
  112. static uint64_t __attribute__((aligned(8))) b08= 0x0808080808080808LL;
  113. static uint64_t __attribute__((aligned(8))) bFF= 0xFFFFFFFFFFFFFFFFLL;
  114. static uint64_t __attribute__((aligned(8))) b20= 0x2020202020202020LL;
  115. static uint64_t __attribute__((aligned(8))) b80= 0x8080808080808080LL;
  116. static uint64_t __attribute__((aligned(8))) b7E= 0x7E7E7E7E7E7E7E7ELL;
  117. static uint64_t __attribute__((aligned(8))) b7C= 0x7C7C7C7C7C7C7C7CLL;
  118. static uint64_t __attribute__((aligned(8))) b3F= 0x3F3F3F3F3F3F3F3FLL;
  119. static uint64_t __attribute__((aligned(8))) temp0=0;
  120. static uint64_t __attribute__((aligned(8))) temp1=0;
  121. static uint64_t __attribute__((aligned(8))) temp2=0;
  122. static uint64_t __attribute__((aligned(8))) temp3=0;
  123. static uint64_t __attribute__((aligned(8))) temp4=0;
  124. static uint64_t __attribute__((aligned(8))) temp5=0;
  125. static uint64_t __attribute__((aligned(8))) pQPb=0;
  126. static uint64_t __attribute__((aligned(8))) pQPb2=0;
  127. static uint8_t __attribute__((aligned(8))) tempBlocks[8*16*2]; //used for the horizontal code
  128. static uint32_t __attribute__((aligned(4))) maxTmpNoise[4];
  129. #else
  130. static uint64_t packedYOffset= 0x0000000000000000LL;
  131. static uint64_t packedYScale= 0x0100010001000100LL;
  132. #endif
  133. int hFlatnessThreshold= 56 - 16;
  134. int vFlatnessThreshold= 56 - 16;
  135. int deringThreshold= 20;
  136. //amount of "black" u r willing to loose to get a brightness corrected picture
  137. double maxClippedThreshold= 0.01;
  138. int maxAllowedY=234;
  139. int minAllowedY=16;
  140. static struct PPFilter filters[]=
  141. {
  142. {"hb", "hdeblock", 1, 1, 3, H_DEBLOCK},
  143. {"vb", "vdeblock", 1, 2, 4, V_DEBLOCK},
  144. {"vr", "rkvdeblock", 1, 2, 4, H_RK1_FILTER},
  145. {"h1", "x1hdeblock", 1, 1, 3, H_X1_FILTER},
  146. {"v1", "x1vdeblock", 1, 2, 4, V_X1_FILTER},
  147. {"dr", "dering", 1, 5, 6, DERING},
  148. {"al", "autolevels", 0, 1, 2, LEVEL_FIX},
  149. {"lb", "linblenddeint", 0, 1, 6, LINEAR_BLEND_DEINT_FILTER},
  150. {"li", "linipoldeint", 0, 1, 6, LINEAR_IPOL_DEINT_FILTER},
  151. {"ci", "cubicipoldeint", 0, 1, 6, CUBIC_IPOL_DEINT_FILTER},
  152. {"md", "mediandeint", 0, 1, 6, MEDIAN_DEINT_FILTER},
  153. {"tn", "tmpnoise", 1, 7, 8, TEMP_NOISE_FILTER},
  154. {NULL, NULL,0,0,0,0} //End Marker
  155. };
  156. static char *replaceTable[]=
  157. {
  158. "default", "hdeblock:a,vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400",
  159. "de", "hdeblock:a,vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400",
  160. "fast", "x1hdeblock:a,x1vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400",
  161. "fa", "x1hdeblock:a,x1vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400",
  162. NULL //End Marker
  163. };
  164. #ifdef CAN_COMPILE_X86_ASM
  165. static inline void unusedVariableWarningFixer()
  166. {
  167. if(
  168. packedYOffset + packedYScale + w05 + w20 + w1400 + bm00000001 + bm00010000
  169. + bm00001000 + bm10000000 + bm10000001 + bm11000011 + bm00000011 + bm11111110
  170. + bm11000000 + bm00011000 + bm00110011 + bm11001100 + b00 + b01 + b02 + b0F
  171. + bFF + b20 + b04+ b08 + pQPb2 + b80 + b7E + b7C + b3F + temp0 + temp1 + temp2 + temp3 + temp4
  172. + temp5 + pQPb== 0) b00=0;
  173. }
  174. #endif
  175. #ifdef TIMING
  176. static inline long long rdtsc()
  177. {
  178. long long l;
  179. asm volatile( "rdtsc\n\t"
  180. : "=A" (l)
  181. );
  182. // printf("%d\n", int(l/1000));
  183. return l;
  184. }
  185. #endif
  186. #ifdef CAN_COMPILE_X86_ASM
  187. static inline void prefetchnta(void *p)
  188. {
  189. asm volatile( "prefetchnta (%0)\n\t"
  190. : : "r" (p)
  191. );
  192. }
  193. static inline void prefetcht0(void *p)
  194. {
  195. asm volatile( "prefetcht0 (%0)\n\t"
  196. : : "r" (p)
  197. );
  198. }
  199. static inline void prefetcht1(void *p)
  200. {
  201. asm volatile( "prefetcht1 (%0)\n\t"
  202. : : "r" (p)
  203. );
  204. }
  205. static inline void prefetcht2(void *p)
  206. {
  207. asm volatile( "prefetcht2 (%0)\n\t"
  208. : : "r" (p)
  209. );
  210. }
  211. #endif
  212. // The horizontal Functions exist only in C cuz the MMX code is faster with vertical filters and transposing
  213. /**
  214. * Check if the given 8x8 Block is mostly "flat"
  215. */
  216. static inline int isHorizDC(uint8_t src[], int stride)
  217. {
  218. int numEq= 0;
  219. int y;
  220. for(y=0; y<BLOCK_SIZE; y++)
  221. {
  222. if(((src[0] - src[1] + 1) & 0xFFFF) < 3) numEq++;
  223. if(((src[1] - src[2] + 1) & 0xFFFF) < 3) numEq++;
  224. if(((src[2] - src[3] + 1) & 0xFFFF) < 3) numEq++;
  225. if(((src[3] - src[4] + 1) & 0xFFFF) < 3) numEq++;
  226. if(((src[4] - src[5] + 1) & 0xFFFF) < 3) numEq++;
  227. if(((src[5] - src[6] + 1) & 0xFFFF) < 3) numEq++;
  228. if(((src[6] - src[7] + 1) & 0xFFFF) < 3) numEq++;
  229. src+= stride;
  230. }
  231. return numEq > hFlatnessThreshold;
  232. }
  233. static inline int isHorizMinMaxOk(uint8_t src[], int stride, int QP)
  234. {
  235. if(abs(src[0] - src[7]) > 2*QP) return 0;
  236. return 1;
  237. }
  238. static inline void doHorizDefFilter(uint8_t dst[], int stride, int QP)
  239. {
  240. int y;
  241. for(y=0; y<BLOCK_SIZE; y++)
  242. {
  243. const int middleEnergy= 5*(dst[4] - dst[5]) + 2*(dst[2] - dst[5]);
  244. if(ABS(middleEnergy) < 8*QP)
  245. {
  246. const int q=(dst[3] - dst[4])/2;
  247. const int leftEnergy= 5*(dst[2] - dst[1]) + 2*(dst[0] - dst[3]);
  248. const int rightEnergy= 5*(dst[6] - dst[5]) + 2*(dst[4] - dst[7]);
  249. int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
  250. d= MAX(d, 0);
  251. d= (5*d + 32) >> 6;
  252. d*= SIGN(-middleEnergy);
  253. if(q>0)
  254. {
  255. d= d<0 ? 0 : d;
  256. d= d>q ? q : d;
  257. }
  258. else
  259. {
  260. d= d>0 ? 0 : d;
  261. d= d<q ? q : d;
  262. }
  263. dst[3]-= d;
  264. dst[4]+= d;
  265. }
  266. dst+= stride;
  267. }
  268. }
  269. /**
  270. * Do a horizontal low pass filter on the 10x8 block (dst points to middle 8x8 Block)
  271. * using the 9-Tap Filter (1,1,2,2,4,2,2,1,1)/16 (C version)
  272. */
  273. static inline void doHorizLowPass(uint8_t dst[], int stride, int QP)
  274. {
  275. int y;
  276. for(y=0; y<BLOCK_SIZE; y++)
  277. {
  278. const int first= ABS(dst[-1] - dst[0]) < QP ? dst[-1] : dst[0];
  279. const int last= ABS(dst[8] - dst[7]) < QP ? dst[8] : dst[7];
  280. int sums[9];
  281. sums[0] = first + dst[0];
  282. sums[1] = dst[0] + dst[1];
  283. sums[2] = dst[1] + dst[2];
  284. sums[3] = dst[2] + dst[3];
  285. sums[4] = dst[3] + dst[4];
  286. sums[5] = dst[4] + dst[5];
  287. sums[6] = dst[5] + dst[6];
  288. sums[7] = dst[6] + dst[7];
  289. sums[8] = dst[7] + last;
  290. dst[0]= ((sums[0]<<2) + ((first + sums[2])<<1) + sums[4] + 8)>>4;
  291. dst[1]= ((dst[1]<<2) + ((first + sums[0] + sums[3])<<1) + sums[5] + 8)>>4;
  292. dst[2]= ((dst[2]<<2) + ((first + sums[1] + sums[4])<<1) + sums[6] + 8)>>4;
  293. dst[3]= ((dst[3]<<2) + ((sums[2] + sums[5])<<1) + sums[0] + sums[7] + 8)>>4;
  294. dst[4]= ((dst[4]<<2) + ((sums[3] + sums[6])<<1) + sums[1] + sums[8] + 8)>>4;
  295. dst[5]= ((dst[5]<<2) + ((last + sums[7] + sums[4])<<1) + sums[2] + 8)>>4;
  296. dst[6]= (((last + dst[6])<<2) + ((dst[7] + sums[5])<<1) + sums[3] + 8)>>4;
  297. dst[7]= ((sums[8]<<2) + ((last + sums[6])<<1) + sums[4] + 8)>>4;
  298. dst+= stride;
  299. }
  300. }
  301. /**
  302. * Experimental Filter 1 (Horizontal)
  303. * will not damage linear gradients
  304. * Flat blocks should look like they where passed through the (1,1,2,2,4,2,2,1,1) 9-Tap filter
  305. * can only smooth blocks at the expected locations (it cant smooth them if they did move)
  306. * MMX2 version does correct clipping C version doesnt
  307. * not identical with the vertical one
  308. */
  309. static inline void horizX1Filter(uint8_t *src, int stride, int QP)
  310. {
  311. int y;
  312. static uint64_t *lut= NULL;
  313. if(lut==NULL)
  314. {
  315. int i;
  316. lut= (uint64_t*)memalign(8, 256*8);
  317. for(i=0; i<256; i++)
  318. {
  319. int v= i < 128 ? 2*i : 2*(i-256);
  320. /*
  321. //Simulate 112242211 9-Tap filter
  322. uint64_t a= (v/16) & 0xFF;
  323. uint64_t b= (v/8) & 0xFF;
  324. uint64_t c= (v/4) & 0xFF;
  325. uint64_t d= (3*v/8) & 0xFF;
  326. */
  327. //Simulate piecewise linear interpolation
  328. uint64_t a= (v/16) & 0xFF;
  329. uint64_t b= (v*3/16) & 0xFF;
  330. uint64_t c= (v*5/16) & 0xFF;
  331. uint64_t d= (7*v/16) & 0xFF;
  332. uint64_t A= (0x100 - a)&0xFF;
  333. uint64_t B= (0x100 - b)&0xFF;
  334. uint64_t C= (0x100 - c)&0xFF;
  335. uint64_t D= (0x100 - c)&0xFF;
  336. lut[i] = (a<<56) | (b<<48) | (c<<40) | (d<<32) |
  337. (D<<24) | (C<<16) | (B<<8) | (A);
  338. //lut[i] = (v<<32) | (v<<24);
  339. }
  340. }
  341. for(y=0; y<BLOCK_SIZE; y++)
  342. {
  343. int a= src[1] - src[2];
  344. int b= src[3] - src[4];
  345. int c= src[5] - src[6];
  346. int d= MAX(ABS(b) - (ABS(a) + ABS(c))/2, 0);
  347. if(d < QP)
  348. {
  349. int v = d * SIGN(-b);
  350. src[1] +=v/8;
  351. src[2] +=v/4;
  352. src[3] +=3*v/8;
  353. src[4] -=3*v/8;
  354. src[5] -=v/4;
  355. src[6] -=v/8;
  356. }
  357. src+=stride;
  358. }
  359. }
  360. //Note: we have C, MMX, MMX2, 3DNOW version there is no 3DNOW+MMX2 one
  361. //Plain C versions
  362. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  363. #define COMPILE_C
  364. #endif
  365. #ifdef CAN_COMPILE_X86_ASM
  366. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  367. #define COMPILE_MMX
  368. #endif
  369. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  370. #define COMPILE_MMX2
  371. #endif
  372. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  373. #define COMPILE_3DNOW
  374. #endif
  375. #endif //CAN_COMPILE_X86_ASM
  376. #undef HAVE_MMX
  377. #undef HAVE_MMX2
  378. #undef HAVE_3DNOW
  379. #undef ARCH_X86
  380. #ifdef COMPILE_C
  381. #undef HAVE_MMX
  382. #undef HAVE_MMX2
  383. #undef HAVE_3DNOW
  384. #undef ARCH_X86
  385. #define RENAME(a) a ## _C
  386. #include "postprocess_template.c"
  387. #endif
  388. //MMX versions
  389. #ifdef COMPILE_MMX
  390. #undef RENAME
  391. #define HAVE_MMX
  392. #undef HAVE_MMX2
  393. #undef HAVE_3DNOW
  394. #define ARCH_X86
  395. #define RENAME(a) a ## _MMX
  396. #include "postprocess_template.c"
  397. #endif
  398. //MMX2 versions
  399. #ifdef COMPILE_MMX2
  400. #undef RENAME
  401. #define HAVE_MMX
  402. #define HAVE_MMX2
  403. #undef HAVE_3DNOW
  404. #define ARCH_X86
  405. #define RENAME(a) a ## _MMX2
  406. #include "postprocess_template.c"
  407. #endif
  408. //3DNOW versions
  409. #ifdef COMPILE_3DNOW
  410. #undef RENAME
  411. #define HAVE_MMX
  412. #undef HAVE_MMX2
  413. #define HAVE_3DNOW
  414. #define ARCH_X86
  415. #define RENAME(a) a ## _3DNow
  416. #include "postprocess_template.c"
  417. #endif
  418. // minor note: the HAVE_xyz is messed up after that line so dont use it
  419. static inline void postProcess(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
  420. QP_STORE_T QPs[], int QPStride, int isColor, struct PPMode *ppMode)
  421. {
  422. // useing ifs here as they are faster than function pointers allthough the
  423. // difference wouldnt be messureable here but its much better because
  424. // someone might exchange the cpu whithout restarting mplayer ;)
  425. #ifdef RUNTIME_CPUDETECT
  426. #ifdef CAN_COMPILE_X86_ASM
  427. // ordered per speed fasterst first
  428. if(gCpuCaps.hasMMX2)
  429. postProcess_MMX2(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, ppMode);
  430. else if(gCpuCaps.has3DNow)
  431. postProcess_3DNow(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, ppMode);
  432. else if(gCpuCaps.hasMMX)
  433. postProcess_MMX(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, ppMode);
  434. else
  435. postProcess_C(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, ppMode);
  436. #else
  437. postProcess_C(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, ppMode);
  438. #endif
  439. #else //RUNTIME_CPUDETECT
  440. #ifdef HAVE_MMX2
  441. postProcess_MMX2(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, ppMode);
  442. #elif defined (HAVE_3DNOW)
  443. postProcess_3DNow(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, ppMode);
  444. #elif defined (HAVE_MMX)
  445. postProcess_MMX(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, ppMode);
  446. #else
  447. postProcess_C(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, ppMode);
  448. #endif
  449. #endif //!RUNTIME_CPUDETECT
  450. }
  451. #ifdef HAVE_ODIVX_POSTPROCESS
  452. #include "../opendivx/postprocess.h"
  453. int use_old_pp=0;
  454. #endif
  455. //static void postProcess(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
  456. // QP_STORE_T QPs[], int QPStride, int isColor, struct PPMode *ppMode);
  457. /* -pp Command line Help
  458. NOTE/FIXME: put this at an appropriate place (--help, html docs, man mplayer)?
  459. -pp <filterName>[:<option>[:<option>...]][,[-]<filterName>[:<option>...]]...
  460. long form example:
  461. -pp vdeblock:autoq,hdeblock:autoq,linblenddeint -pp default,-vdeblock
  462. short form example:
  463. -pp vb:a,hb:a,lb -pp de,-vb
  464. more examples:
  465. -pp tn:64:128:256
  466. Filters Options
  467. short long name short long option Description
  468. * * a autoq cpu power dependant enabler
  469. c chrom chrominance filtring enabled
  470. y nochrom chrominance filtring disabled
  471. hb hdeblock horizontal deblocking filter
  472. vb vdeblock vertical deblocking filter
  473. vr rkvdeblock
  474. h1 x1hdeblock Experimental horizontal deblock filter 1
  475. v1 x1vdeblock Experimental vertical deblock filter 1
  476. dr dering not implemented yet
  477. al autolevels automatic brightness / contrast fixer
  478. f fullyrange stretch luminance range to (0..255)
  479. lb linblenddeint linear blend deinterlacer
  480. li linipoldeint linear interpolating deinterlacer
  481. ci cubicipoldeint cubic interpolating deinterlacer
  482. md mediandeint median deinterlacer
  483. de default hdeblock:a,vdeblock:a,dering:a,autolevels
  484. fa fast x1hdeblock:a,x1vdeblock:a,dering:a,autolevels
  485. tn tmpnoise (3 Thresholds) Temporal Noise Reducer
  486. */
  487. /**
  488. * returns a PPMode struct which will have a non 0 error variable if an error occured
  489. * name is the string after "-pp" on the command line
  490. * quality is a number from 0 to GET_PP_QUALITY_MAX
  491. */
  492. struct PPMode getPPModeByNameAndQuality(char *name, int quality)
  493. {
  494. char temp[GET_MODE_BUFFER_SIZE];
  495. char *p= temp;
  496. char *filterDelimiters= ",";
  497. char *optionDelimiters= ":";
  498. struct PPMode ppMode= {0,0,0,0,0,0,{150,200,400}};
  499. char *filterToken;
  500. strncpy(temp, name, GET_MODE_BUFFER_SIZE);
  501. printf("%s\n", name);
  502. for(;;){
  503. char *filterName;
  504. int q= 1000000; //GET_PP_QUALITY_MAX;
  505. int chrom=-1;
  506. char *option;
  507. char *options[OPTIONS_ARRAY_SIZE];
  508. int i;
  509. int filterNameOk=0;
  510. int numOfUnknownOptions=0;
  511. int enable=1; //does the user want us to enabled or disabled the filter
  512. filterToken= strtok(p, filterDelimiters);
  513. if(filterToken == NULL) break;
  514. p+= strlen(filterToken) + 1; // p points to next filterToken
  515. filterName= strtok(filterToken, optionDelimiters);
  516. printf("%s::%s\n", filterToken, filterName);
  517. if(*filterName == '-')
  518. {
  519. enable=0;
  520. filterName++;
  521. }
  522. for(;;){ //for all options
  523. option= strtok(NULL, optionDelimiters);
  524. if(option == NULL) break;
  525. printf("%s\n", option);
  526. if(!strcmp("autoq", option) || !strcmp("a", option)) q= quality;
  527. else if(!strcmp("nochrom", option) || !strcmp("y", option)) chrom=0;
  528. else if(!strcmp("chrom", option) || !strcmp("c", option)) chrom=1;
  529. else
  530. {
  531. options[numOfUnknownOptions] = option;
  532. numOfUnknownOptions++;
  533. }
  534. if(numOfUnknownOptions >= OPTIONS_ARRAY_SIZE-1) break;
  535. }
  536. options[numOfUnknownOptions] = NULL;
  537. /* replace stuff from the replace Table */
  538. for(i=0; replaceTable[2*i]!=NULL; i++)
  539. {
  540. if(!strcmp(replaceTable[2*i], filterName))
  541. {
  542. int newlen= strlen(replaceTable[2*i + 1]);
  543. int plen;
  544. int spaceLeft;
  545. if(p==NULL) p= temp, *p=0; //last filter
  546. else p--, *p=','; //not last filter
  547. plen= strlen(p);
  548. spaceLeft= (int)p - (int)temp + plen;
  549. if(spaceLeft + newlen >= GET_MODE_BUFFER_SIZE)
  550. {
  551. ppMode.error++;
  552. break;
  553. }
  554. memmove(p + newlen, p, plen+1);
  555. memcpy(p, replaceTable[2*i + 1], newlen);
  556. filterNameOk=1;
  557. }
  558. }
  559. for(i=0; filters[i].shortName!=NULL; i++)
  560. {
  561. // printf("Compareing %s, %s, %s\n", filters[i].shortName,filters[i].longName, filterName);
  562. if( !strcmp(filters[i].longName, filterName)
  563. || !strcmp(filters[i].shortName, filterName))
  564. {
  565. ppMode.lumMode &= ~filters[i].mask;
  566. ppMode.chromMode &= ~filters[i].mask;
  567. filterNameOk=1;
  568. if(!enable) break; // user wants to disable it
  569. if(q >= filters[i].minLumQuality)
  570. ppMode.lumMode|= filters[i].mask;
  571. if(chrom==1 || (chrom==-1 && filters[i].chromDefault))
  572. if(q >= filters[i].minChromQuality)
  573. ppMode.chromMode|= filters[i].mask;
  574. if(filters[i].mask == LEVEL_FIX)
  575. {
  576. int o;
  577. ppMode.minAllowedY= 16;
  578. ppMode.maxAllowedY= 234;
  579. for(o=0; options[o]!=NULL; o++)
  580. if( !strcmp(options[o],"fullyrange")
  581. ||!strcmp(options[o],"f"))
  582. {
  583. ppMode.minAllowedY= 0;
  584. ppMode.maxAllowedY= 255;
  585. numOfUnknownOptions--;
  586. }
  587. }
  588. else if(filters[i].mask == TEMP_NOISE_FILTER)
  589. {
  590. int o;
  591. int numOfNoises=0;
  592. ppMode.maxTmpNoise[0]= 150;
  593. ppMode.maxTmpNoise[1]= 200;
  594. ppMode.maxTmpNoise[2]= 400;
  595. for(o=0; options[o]!=NULL; o++)
  596. {
  597. char *tail;
  598. ppMode.maxTmpNoise[numOfNoises]=
  599. strtol(options[o], &tail, 0);
  600. if(tail!=options[o])
  601. {
  602. numOfNoises++;
  603. numOfUnknownOptions--;
  604. if(numOfNoises >= 3) break;
  605. }
  606. }
  607. }
  608. }
  609. }
  610. if(!filterNameOk) ppMode.error++;
  611. ppMode.error += numOfUnknownOptions;
  612. }
  613. #ifdef HAVE_ODIVX_POSTPROCESS
  614. if(ppMode.lumMode & H_DEBLOCK) ppMode.oldMode |= PP_DEBLOCK_Y_H;
  615. if(ppMode.lumMode & V_DEBLOCK) ppMode.oldMode |= PP_DEBLOCK_Y_V;
  616. if(ppMode.chromMode & H_DEBLOCK) ppMode.oldMode |= PP_DEBLOCK_C_H;
  617. if(ppMode.chromMode & V_DEBLOCK) ppMode.oldMode |= PP_DEBLOCK_C_V;
  618. if(ppMode.lumMode & DERING) ppMode.oldMode |= PP_DERING_Y;
  619. if(ppMode.chromMode & DERING) ppMode.oldMode |= PP_DERING_C;
  620. #endif
  621. return ppMode;
  622. }
  623. /**
  624. * Obsolete, dont use it, use postprocess2() instead
  625. */
  626. void postprocess(unsigned char * src[], int src_stride,
  627. unsigned char * dst[], int dst_stride,
  628. int horizontal_size, int vertical_size,
  629. QP_STORE_T *QP_store, int QP_stride,
  630. int mode)
  631. {
  632. struct PPMode ppMode;
  633. static QP_STORE_T zeroArray[2048/8];
  634. /*
  635. static int qual=0;
  636. ppMode= getPPModeByNameAndQuality("fast,default,-hdeblock,-vdeblock,tmpnoise:150:200:300", qual);
  637. printf("OK\n");
  638. qual++;
  639. qual%=7;
  640. printf("\n%X %X %X %X :%d: %d %d %d\n", ppMode.lumMode, ppMode.chromMode, ppMode.oldMode, ppMode.error,
  641. qual, ppMode.maxTmpNoise[0], ppMode.maxTmpNoise[1], ppMode.maxTmpNoise[2]);
  642. postprocess2(src, src_stride, dst, dst_stride,
  643. horizontal_size, vertical_size, QP_store, QP_stride, &ppMode);
  644. return;
  645. */
  646. if(QP_store==NULL)
  647. {
  648. QP_store= zeroArray;
  649. QP_stride= 0;
  650. }
  651. ppMode.lumMode= mode;
  652. mode= ((mode&0xFF)>>4) | (mode&0xFFFFFF00);
  653. ppMode.chromMode= mode;
  654. ppMode.maxTmpNoise[0]= 700;
  655. ppMode.maxTmpNoise[1]= 1500;
  656. ppMode.maxTmpNoise[2]= 3000;
  657. #ifdef HAVE_ODIVX_POSTPROCESS
  658. // Note: I could make this shit outside of this file, but it would mean one
  659. // more function call...
  660. if(use_old_pp){
  661. odivx_postprocess(src,src_stride,dst,dst_stride,horizontal_size,vertical_size,QP_store,QP_stride,mode);
  662. return;
  663. }
  664. #endif
  665. postProcess(src[0], src_stride, dst[0], dst_stride,
  666. horizontal_size, vertical_size, QP_store, QP_stride, 0, &ppMode);
  667. horizontal_size >>= 1;
  668. vertical_size >>= 1;
  669. src_stride >>= 1;
  670. dst_stride >>= 1;
  671. if(ppMode.chromMode)
  672. {
  673. postProcess(src[1], src_stride, dst[1], dst_stride,
  674. horizontal_size, vertical_size, QP_store, QP_stride, 1, &ppMode);
  675. postProcess(src[2], src_stride, dst[2], dst_stride,
  676. horizontal_size, vertical_size, QP_store, QP_stride, 2, &ppMode);
  677. }
  678. else if(src_stride == dst_stride)
  679. {
  680. memcpy(dst[1], src[1], src_stride*vertical_size);
  681. memcpy(dst[2], src[2], src_stride*vertical_size);
  682. }
  683. else
  684. {
  685. int y;
  686. for(y=0; y<vertical_size; y++)
  687. {
  688. memcpy(&(dst[1][y*dst_stride]), &(src[1][y*src_stride]), horizontal_size);
  689. memcpy(&(dst[2][y*dst_stride]), &(src[2][y*src_stride]), horizontal_size);
  690. }
  691. }
  692. #if 0
  693. memset(dst[1], 128, dst_stride*vertical_size);
  694. memset(dst[2], 128, dst_stride*vertical_size);
  695. #endif
  696. }
  697. void postprocess2(unsigned char * src[], int src_stride,
  698. unsigned char * dst[], int dst_stride,
  699. int horizontal_size, int vertical_size,
  700. QP_STORE_T *QP_store, int QP_stride,
  701. struct PPMode *mode)
  702. {
  703. static QP_STORE_T zeroArray[2048/8];
  704. if(QP_store==NULL)
  705. {
  706. QP_store= zeroArray;
  707. QP_stride= 0;
  708. }
  709. #ifdef HAVE_ODIVX_POSTPROCESS
  710. // Note: I could make this shit outside of this file, but it would mean one
  711. // more function call...
  712. if(use_old_pp){
  713. odivx_postprocess(src,src_stride,dst,dst_stride,horizontal_size,vertical_size,QP_store,QP_stride,
  714. mode->oldMode);
  715. return;
  716. }
  717. #endif
  718. postProcess(src[0], src_stride, dst[0], dst_stride,
  719. horizontal_size, vertical_size, QP_store, QP_stride, 0, mode);
  720. horizontal_size >>= 1;
  721. vertical_size >>= 1;
  722. src_stride >>= 1;
  723. dst_stride >>= 1;
  724. if(mode->chromMode)
  725. {
  726. postProcess(src[1], src_stride, dst[1], dst_stride,
  727. horizontal_size, vertical_size, QP_store, QP_stride, 1, mode);
  728. postProcess(src[2], src_stride, dst[2], dst_stride,
  729. horizontal_size, vertical_size, QP_store, QP_stride, 2, mode);
  730. }
  731. else if(src_stride == dst_stride)
  732. {
  733. memcpy(dst[1], src[1], src_stride*vertical_size);
  734. memcpy(dst[2], src[2], src_stride*vertical_size);
  735. }
  736. else
  737. {
  738. int y;
  739. for(y=0; y<vertical_size; y++)
  740. {
  741. memcpy(&(dst[1][y*dst_stride]), &(src[1][y*src_stride]), horizontal_size);
  742. memcpy(&(dst[2][y*dst_stride]), &(src[2][y*src_stride]), horizontal_size);
  743. }
  744. }
  745. }
  746. /**
  747. * gets the mode flags for a given quality (larger values mean slower but better postprocessing)
  748. * 0 <= quality <= 6
  749. */
  750. int getPpModeForQuality(int quality){
  751. int modes[1+GET_PP_QUALITY_MAX]= {
  752. 0,
  753. #if 1
  754. // horizontal filters first
  755. LUM_H_DEBLOCK,
  756. LUM_H_DEBLOCK | LUM_V_DEBLOCK,
  757. LUM_H_DEBLOCK | LUM_V_DEBLOCK | CHROM_H_DEBLOCK,
  758. LUM_H_DEBLOCK | LUM_V_DEBLOCK | CHROM_H_DEBLOCK | CHROM_V_DEBLOCK,
  759. LUM_H_DEBLOCK | LUM_V_DEBLOCK | CHROM_H_DEBLOCK | CHROM_V_DEBLOCK | LUM_DERING,
  760. LUM_H_DEBLOCK | LUM_V_DEBLOCK | CHROM_H_DEBLOCK | CHROM_V_DEBLOCK | LUM_DERING | CHROM_DERING
  761. #else
  762. // vertical filters first
  763. LUM_V_DEBLOCK,
  764. LUM_V_DEBLOCK | LUM_H_DEBLOCK,
  765. LUM_V_DEBLOCK | LUM_H_DEBLOCK | CHROM_V_DEBLOCK,
  766. LUM_V_DEBLOCK | LUM_H_DEBLOCK | CHROM_V_DEBLOCK | CHROM_H_DEBLOCK,
  767. LUM_V_DEBLOCK | LUM_H_DEBLOCK | CHROM_V_DEBLOCK | CHROM_H_DEBLOCK | LUM_DERING,
  768. LUM_V_DEBLOCK | LUM_H_DEBLOCK | CHROM_V_DEBLOCK | CHROM_H_DEBLOCK | LUM_DERING | CHROM_DERING
  769. #endif
  770. };
  771. #ifdef HAVE_ODIVX_POSTPROCESS
  772. int odivx_modes[1+GET_PP_QUALITY_MAX]= {
  773. 0,
  774. PP_DEBLOCK_Y_H,
  775. PP_DEBLOCK_Y_H|PP_DEBLOCK_Y_V,
  776. PP_DEBLOCK_Y_H|PP_DEBLOCK_Y_V|PP_DEBLOCK_C_H,
  777. PP_DEBLOCK_Y_H|PP_DEBLOCK_Y_V|PP_DEBLOCK_C_H|PP_DEBLOCK_C_V,
  778. PP_DEBLOCK_Y_H|PP_DEBLOCK_Y_V|PP_DEBLOCK_C_H|PP_DEBLOCK_C_V|PP_DERING_Y,
  779. PP_DEBLOCK_Y_H|PP_DEBLOCK_Y_V|PP_DEBLOCK_C_H|PP_DEBLOCK_C_V|PP_DERING_Y|PP_DERING_C
  780. };
  781. if(use_old_pp) return odivx_modes[quality];
  782. #endif
  783. return modes[quality];
  784. }