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.

946 lines
28KB

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