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.

915 lines
27KB

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