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.

1017 lines
29KB

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