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.

1031 lines
28KB

  1. /*
  2. Copyright (C) 2001-2003 Michael Niedermayer (michaelni@gmx.at)
  3. AltiVec optimizations (C) 2004 Romain Dolbeau <romain@dolbeau.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. /**
  17. * @file postprocess.c
  18. * postprocessing.
  19. */
  20. /*
  21. C MMX MMX2 3DNow AltiVec
  22. isVertDC Ec Ec Ec
  23. isVertMinMaxOk Ec Ec Ec
  24. doVertLowPass E e e Ec
  25. doVertDefFilter Ec Ec e e Ec
  26. isHorizDC Ec Ec
  27. isHorizMinMaxOk a E
  28. doHorizLowPass E e e
  29. doHorizDefFilter Ec Ec e e
  30. deRing E e e* Ecp
  31. Vertical RKAlgo1 E a a
  32. Horizontal RKAlgo1 a a
  33. Vertical X1# a E E
  34. Horizontal X1# a E E
  35. LinIpolDeinterlace e E E*
  36. CubicIpolDeinterlace a e e*
  37. LinBlendDeinterlace e E E*
  38. MedianDeinterlace# E Ec Ec
  39. TempDeNoiser# E e e
  40. * i dont have a 3dnow CPU -> its untested, but noone said it doesnt work so it seems to work
  41. # more or less selfinvented filters so the exactness isnt too meaningfull
  42. E = Exact implementation
  43. e = allmost exact implementation (slightly different rounding,...)
  44. a = alternative / approximate impl
  45. c = checked against the other implementations (-vo md5)
  46. p = partially optimized, still some work to do
  47. */
  48. /*
  49. TODO:
  50. reduce the time wasted on the mem transfer
  51. unroll stuff if instructions depend too much on the prior one
  52. move YScale thing to the end instead of fixing QP
  53. write a faster and higher quality deblocking filter :)
  54. make the mainloop more flexible (variable number of blocks at once
  55. (the if/else stuff per block is slowing things down)
  56. compare the quality & speed of all filters
  57. split this huge file
  58. optimize c versions
  59. try to unroll inner for(x=0 ... loop to avoid these damn if(x ... checks
  60. ...
  61. */
  62. //Changelog: use the CVS log
  63. #include "config.h"
  64. #include <inttypes.h>
  65. #include <stdio.h>
  66. #include <stdlib.h>
  67. #include <string.h>
  68. #ifdef HAVE_MALLOC_H
  69. #include <malloc.h>
  70. #endif
  71. //#undef HAVE_MMX2
  72. //#define HAVE_3DNOW
  73. //#undef HAVE_MMX
  74. //#undef ARCH_X86
  75. //#define DEBUG_BRIGHTNESS
  76. #ifdef USE_FASTMEMCPY
  77. #include "fastmemcpy.h"
  78. #endif
  79. #include "postprocess.h"
  80. #include "postprocess_internal.h"
  81. #include "mangle.h" //FIXME should be supressed
  82. #ifndef HAVE_MEMALIGN
  83. #define memalign(a,b) malloc(b)
  84. #endif
  85. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  86. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  87. #define ABS(a) ((a) > 0 ? (a) : (-(a)))
  88. #define SIGN(a) ((a) > 0 ? 1 : -1)
  89. #define GET_MODE_BUFFER_SIZE 500
  90. #define OPTIONS_ARRAY_SIZE 10
  91. #define BLOCK_SIZE 8
  92. #define TEMP_STRIDE 8
  93. //#define NUM_BLOCKS_AT_ONCE 16 //not used yet
  94. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  95. # define attribute_used __attribute__((used))
  96. #else
  97. # define attribute_used
  98. #endif
  99. #ifdef ARCH_X86
  100. static uint64_t __attribute__((aligned(8))) attribute_used w05= 0x0005000500050005LL;
  101. static uint64_t __attribute__((aligned(8))) attribute_used w20= 0x0020002000200020LL;
  102. static uint64_t __attribute__((aligned(8))) attribute_used b00= 0x0000000000000000LL;
  103. static uint64_t __attribute__((aligned(8))) attribute_used b01= 0x0101010101010101LL;
  104. static uint64_t __attribute__((aligned(8))) attribute_used b02= 0x0202020202020202LL;
  105. static uint64_t __attribute__((aligned(8))) attribute_used b08= 0x0808080808080808LL;
  106. static uint64_t __attribute__((aligned(8))) attribute_used b80= 0x8080808080808080LL;
  107. #endif
  108. static uint8_t clip_table[3*256];
  109. static uint8_t * const clip_tab= clip_table + 256;
  110. static const int verbose= 0;
  111. static const int attribute_used deringThreshold= 20;
  112. static struct PPFilter filters[]=
  113. {
  114. {"hb", "hdeblock", 1, 1, 3, H_DEBLOCK},
  115. {"vb", "vdeblock", 1, 2, 4, V_DEBLOCK},
  116. /* {"hr", "rkhdeblock", 1, 1, 3, H_RK1_FILTER},
  117. {"vr", "rkvdeblock", 1, 2, 4, V_RK1_FILTER},*/
  118. {"h1", "x1hdeblock", 1, 1, 3, H_X1_FILTER},
  119. {"v1", "x1vdeblock", 1, 2, 4, V_X1_FILTER},
  120. {"dr", "dering", 1, 5, 6, DERING},
  121. {"al", "autolevels", 0, 1, 2, LEVEL_FIX},
  122. {"lb", "linblenddeint", 1, 1, 4, LINEAR_BLEND_DEINT_FILTER},
  123. {"li", "linipoldeint", 1, 1, 4, LINEAR_IPOL_DEINT_FILTER},
  124. {"ci", "cubicipoldeint", 1, 1, 4, CUBIC_IPOL_DEINT_FILTER},
  125. {"md", "mediandeint", 1, 1, 4, MEDIAN_DEINT_FILTER},
  126. {"fd", "ffmpegdeint", 1, 1, 4, FFMPEG_DEINT_FILTER},
  127. {"l5", "lowpass5", 1, 1, 4, LOWPASS5_DEINT_FILTER},
  128. {"tn", "tmpnoise", 1, 7, 8, TEMP_NOISE_FILTER},
  129. {"fq", "forcequant", 1, 0, 0, FORCE_QUANT},
  130. {NULL, NULL,0,0,0,0} //End Marker
  131. };
  132. static char *replaceTable[]=
  133. {
  134. "default", "hdeblock:a,vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400",
  135. "de", "hdeblock:a,vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400",
  136. "fast", "x1hdeblock:a,x1vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400",
  137. "fa", "x1hdeblock:a,x1vdeblock:a,dering:a,autolevels,tmpnoise:a:150:200:400",
  138. NULL //End Marker
  139. };
  140. #ifdef ARCH_X86
  141. static inline void prefetchnta(void *p)
  142. {
  143. asm volatile( "prefetchnta (%0)\n\t"
  144. : : "r" (p)
  145. );
  146. }
  147. static inline void prefetcht0(void *p)
  148. {
  149. asm volatile( "prefetcht0 (%0)\n\t"
  150. : : "r" (p)
  151. );
  152. }
  153. static inline void prefetcht1(void *p)
  154. {
  155. asm volatile( "prefetcht1 (%0)\n\t"
  156. : : "r" (p)
  157. );
  158. }
  159. static inline void prefetcht2(void *p)
  160. {
  161. asm volatile( "prefetcht2 (%0)\n\t"
  162. : : "r" (p)
  163. );
  164. }
  165. #endif
  166. // The horizontal Functions exist only in C cuz the MMX code is faster with vertical filters and transposing
  167. /**
  168. * Check if the given 8x8 Block is mostly "flat"
  169. */
  170. static inline int isHorizDC_C(uint8_t src[], int stride, PPContext *c)
  171. {
  172. int numEq= 0;
  173. int y;
  174. const int dcOffset= ((c->nonBQP*c->ppMode.baseDcDiff)>>8) + 1;
  175. const int dcThreshold= dcOffset*2 + 1;
  176. for(y=0; y<BLOCK_SIZE; y++)
  177. {
  178. if(((unsigned)(src[0] - src[1] + dcOffset)) < dcThreshold) numEq++;
  179. if(((unsigned)(src[1] - src[2] + dcOffset)) < dcThreshold) numEq++;
  180. if(((unsigned)(src[2] - src[3] + dcOffset)) < dcThreshold) numEq++;
  181. if(((unsigned)(src[3] - src[4] + dcOffset)) < dcThreshold) numEq++;
  182. if(((unsigned)(src[4] - src[5] + dcOffset)) < dcThreshold) numEq++;
  183. if(((unsigned)(src[5] - src[6] + dcOffset)) < dcThreshold) numEq++;
  184. if(((unsigned)(src[6] - src[7] + dcOffset)) < dcThreshold) numEq++;
  185. src+= stride;
  186. }
  187. return numEq > c->ppMode.flatnessThreshold;
  188. }
  189. /**
  190. * Check if the middle 8x8 Block in the given 8x16 block is flat
  191. */
  192. static inline int isVertDC_C(uint8_t src[], int stride, PPContext *c){
  193. int numEq= 0;
  194. int y;
  195. const int dcOffset= ((c->nonBQP*c->ppMode.baseDcDiff)>>8) + 1;
  196. const int dcThreshold= dcOffset*2 + 1;
  197. src+= stride*4; // src points to begin of the 8x8 Block
  198. for(y=0; y<BLOCK_SIZE-1; y++)
  199. {
  200. if(((unsigned)(src[0] - src[0+stride] + dcOffset)) < dcThreshold) numEq++;
  201. if(((unsigned)(src[1] - src[1+stride] + dcOffset)) < dcThreshold) numEq++;
  202. if(((unsigned)(src[2] - src[2+stride] + dcOffset)) < dcThreshold) numEq++;
  203. if(((unsigned)(src[3] - src[3+stride] + dcOffset)) < dcThreshold) numEq++;
  204. if(((unsigned)(src[4] - src[4+stride] + dcOffset)) < dcThreshold) numEq++;
  205. if(((unsigned)(src[5] - src[5+stride] + dcOffset)) < dcThreshold) numEq++;
  206. if(((unsigned)(src[6] - src[6+stride] + dcOffset)) < dcThreshold) numEq++;
  207. if(((unsigned)(src[7] - src[7+stride] + dcOffset)) < dcThreshold) numEq++;
  208. src+= stride;
  209. }
  210. return numEq > c->ppMode.flatnessThreshold;
  211. }
  212. static inline int isHorizMinMaxOk_C(uint8_t src[], int stride, int QP)
  213. {
  214. int i;
  215. #if 1
  216. for(i=0; i<2; i++){
  217. if((unsigned)(src[0] - src[5] + 2*QP) > 4*QP) return 0;
  218. src += stride;
  219. if((unsigned)(src[2] - src[7] + 2*QP) > 4*QP) return 0;
  220. src += stride;
  221. if((unsigned)(src[4] - src[1] + 2*QP) > 4*QP) return 0;
  222. src += stride;
  223. if((unsigned)(src[6] - src[3] + 2*QP) > 4*QP) return 0;
  224. src += stride;
  225. }
  226. #else
  227. for(i=0; i<8; i++){
  228. if((unsigned)(src[0] - src[7] + 2*QP) > 4*QP) return 0;
  229. src += stride;
  230. }
  231. #endif
  232. return 1;
  233. }
  234. static inline int isVertMinMaxOk_C(uint8_t src[], int stride, int QP)
  235. {
  236. #if 1
  237. #if 1
  238. int x;
  239. src+= stride*4;
  240. for(x=0; x<BLOCK_SIZE; x+=4)
  241. {
  242. if((unsigned)(src[ x + 0*stride] - src[ x + 5*stride] + 2*QP) > 4*QP) return 0;
  243. if((unsigned)(src[1+x + 2*stride] - src[1+x + 7*stride] + 2*QP) > 4*QP) return 0;
  244. if((unsigned)(src[2+x + 4*stride] - src[2+x + 1*stride] + 2*QP) > 4*QP) return 0;
  245. if((unsigned)(src[3+x + 6*stride] - src[3+x + 3*stride] + 2*QP) > 4*QP) return 0;
  246. }
  247. #else
  248. int x;
  249. src+= stride*3;
  250. for(x=0; x<BLOCK_SIZE; x++)
  251. {
  252. if((unsigned)(src[x + stride] - src[x + (stride<<3)] + 2*QP) > 4*QP) return 0;
  253. }
  254. #endif
  255. return 1;
  256. #else
  257. int x;
  258. src+= stride*4;
  259. for(x=0; x<BLOCK_SIZE; x++)
  260. {
  261. int min=255;
  262. int max=0;
  263. int y;
  264. for(y=0; y<8; y++){
  265. int v= src[x + y*stride];
  266. if(v>max) max=v;
  267. if(v<min) min=v;
  268. }
  269. if(max-min > 2*QP) return 0;
  270. }
  271. return 1;
  272. #endif
  273. }
  274. static inline int horizClassify_C(uint8_t src[], int stride, PPContext *c){
  275. if( isHorizDC_C(src, stride, c) ){
  276. if( isHorizMinMaxOk_C(src, stride, c->QP) )
  277. return 1;
  278. else
  279. return 0;
  280. }else{
  281. return 2;
  282. }
  283. }
  284. static inline int vertClassify_C(uint8_t src[], int stride, PPContext *c){
  285. if( isVertDC_C(src, stride, c) ){
  286. if( isVertMinMaxOk_C(src, stride, c->QP) )
  287. return 1;
  288. else
  289. return 0;
  290. }else{
  291. return 2;
  292. }
  293. }
  294. static inline void doHorizDefFilter_C(uint8_t dst[], int stride, PPContext *c)
  295. {
  296. int y;
  297. for(y=0; y<BLOCK_SIZE; y++)
  298. {
  299. const int middleEnergy= 5*(dst[4] - dst[3]) + 2*(dst[2] - dst[5]);
  300. if(ABS(middleEnergy) < 8*c->QP)
  301. {
  302. const int q=(dst[3] - dst[4])/2;
  303. const int leftEnergy= 5*(dst[2] - dst[1]) + 2*(dst[0] - dst[3]);
  304. const int rightEnergy= 5*(dst[6] - dst[5]) + 2*(dst[4] - dst[7]);
  305. int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
  306. d= MAX(d, 0);
  307. d= (5*d + 32) >> 6;
  308. d*= SIGN(-middleEnergy);
  309. if(q>0)
  310. {
  311. d= d<0 ? 0 : d;
  312. d= d>q ? q : d;
  313. }
  314. else
  315. {
  316. d= d>0 ? 0 : d;
  317. d= d<q ? q : d;
  318. }
  319. dst[3]-= d;
  320. dst[4]+= d;
  321. }
  322. dst+= stride;
  323. }
  324. }
  325. /**
  326. * Do a horizontal low pass filter on the 10x8 block (dst points to middle 8x8 Block)
  327. * using the 9-Tap Filter (1,1,2,2,4,2,2,1,1)/16 (C version)
  328. */
  329. static inline void doHorizLowPass_C(uint8_t dst[], int stride, PPContext *c)
  330. {
  331. int y;
  332. for(y=0; y<BLOCK_SIZE; y++)
  333. {
  334. const int first= ABS(dst[-1] - dst[0]) < c->QP ? dst[-1] : dst[0];
  335. const int last= ABS(dst[8] - dst[7]) < c->QP ? dst[8] : dst[7];
  336. int sums[9];
  337. sums[0] = first + dst[0];
  338. sums[1] = dst[0] + dst[1];
  339. sums[2] = dst[1] + dst[2];
  340. sums[3] = dst[2] + dst[3];
  341. sums[4] = dst[3] + dst[4];
  342. sums[5] = dst[4] + dst[5];
  343. sums[6] = dst[5] + dst[6];
  344. sums[7] = dst[6] + dst[7];
  345. sums[8] = dst[7] + last;
  346. dst[0]= ((sums[0]<<2) + ((first + sums[2])<<1) + sums[4] + 8)>>4;
  347. dst[1]= ((dst[1]<<2) + ((first + sums[0] + sums[3])<<1) + sums[5] + 8)>>4;
  348. dst[2]= ((dst[2]<<2) + ((first + sums[1] + sums[4])<<1) + sums[6] + 8)>>4;
  349. dst[3]= ((dst[3]<<2) + ((sums[2] + sums[5])<<1) + sums[0] + sums[7] + 8)>>4;
  350. dst[4]= ((dst[4]<<2) + ((sums[3] + sums[6])<<1) + sums[1] + sums[8] + 8)>>4;
  351. dst[5]= ((dst[5]<<2) + ((last + sums[7] + sums[4])<<1) + sums[2] + 8)>>4;
  352. dst[6]= (((last + dst[6])<<2) + ((dst[7] + sums[5])<<1) + sums[3] + 8)>>4;
  353. dst[7]= ((sums[8]<<2) + ((last + sums[6])<<1) + sums[4] + 8)>>4;
  354. dst+= stride;
  355. }
  356. }
  357. /**
  358. * Experimental Filter 1 (Horizontal)
  359. * will not damage linear gradients
  360. * Flat blocks should look like they where passed through the (1,1,2,2,4,2,2,1,1) 9-Tap filter
  361. * can only smooth blocks at the expected locations (it cant smooth them if they did move)
  362. * MMX2 version does correct clipping C version doesnt
  363. * not identical with the vertical one
  364. */
  365. static inline void horizX1Filter(uint8_t *src, int stride, int QP)
  366. {
  367. int y;
  368. static uint64_t *lut= NULL;
  369. if(lut==NULL)
  370. {
  371. int i;
  372. lut= (uint64_t*)memalign(8, 256*8);
  373. for(i=0; i<256; i++)
  374. {
  375. int v= i < 128 ? 2*i : 2*(i-256);
  376. /*
  377. //Simulate 112242211 9-Tap filter
  378. uint64_t a= (v/16) & 0xFF;
  379. uint64_t b= (v/8) & 0xFF;
  380. uint64_t c= (v/4) & 0xFF;
  381. uint64_t d= (3*v/8) & 0xFF;
  382. */
  383. //Simulate piecewise linear interpolation
  384. uint64_t a= (v/16) & 0xFF;
  385. uint64_t b= (v*3/16) & 0xFF;
  386. uint64_t c= (v*5/16) & 0xFF;
  387. uint64_t d= (7*v/16) & 0xFF;
  388. uint64_t A= (0x100 - a)&0xFF;
  389. uint64_t B= (0x100 - b)&0xFF;
  390. uint64_t C= (0x100 - c)&0xFF;
  391. uint64_t D= (0x100 - c)&0xFF;
  392. lut[i] = (a<<56) | (b<<48) | (c<<40) | (d<<32) |
  393. (D<<24) | (C<<16) | (B<<8) | (A);
  394. //lut[i] = (v<<32) | (v<<24);
  395. }
  396. }
  397. for(y=0; y<BLOCK_SIZE; y++)
  398. {
  399. int a= src[1] - src[2];
  400. int b= src[3] - src[4];
  401. int c= src[5] - src[6];
  402. int d= MAX(ABS(b) - (ABS(a) + ABS(c))/2, 0);
  403. if(d < QP)
  404. {
  405. int v = d * SIGN(-b);
  406. src[1] +=v/8;
  407. src[2] +=v/4;
  408. src[3] +=3*v/8;
  409. src[4] -=3*v/8;
  410. src[5] -=v/4;
  411. src[6] -=v/8;
  412. }
  413. src+=stride;
  414. }
  415. }
  416. //Note: we have C, MMX, MMX2, 3DNOW version there is no 3DNOW+MMX2 one
  417. //Plain C versions
  418. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  419. #define COMPILE_C
  420. #endif
  421. #ifdef ARCH_POWERPC
  422. #ifdef HAVE_ALTIVEC
  423. #define COMPILE_ALTIVEC
  424. #ifndef CONFIG_DARWIN
  425. #warning "################################################################################"
  426. #warning "WARNING: No gcc available as of today (2004-05-25) seems to be able to compile properly some of the code under non-Darwin PPC OSes. Some functions result in wrong results, while others simply won't compile (gcc explodes after allocating 1GiB+)."
  427. #warning "################################################################################"
  428. #endif //CONFIG_DARWIN
  429. #endif //HAVE_ALTIVEC
  430. #endif //ARCH_POWERPC
  431. #ifdef ARCH_X86
  432. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  433. #define COMPILE_MMX
  434. #endif
  435. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  436. #define COMPILE_MMX2
  437. #endif
  438. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  439. #define COMPILE_3DNOW
  440. #endif
  441. #endif //ARCH_X86
  442. #undef HAVE_MMX
  443. #undef HAVE_MMX2
  444. #undef HAVE_3DNOW
  445. #undef HAVE_ALTIVEC
  446. #undef ARCH_X86
  447. #ifdef COMPILE_C
  448. #undef HAVE_MMX
  449. #undef HAVE_MMX2
  450. #undef HAVE_3DNOW
  451. #undef ARCH_X86
  452. #define RENAME(a) a ## _C
  453. #include "postprocess_template.c"
  454. #endif
  455. #ifdef ARCH_POWERPC
  456. #ifdef COMPILE_ALTIVEC
  457. #undef RENAME
  458. #define HAVE_ALTIVEC
  459. #define RENAME(a) a ## _altivec
  460. #include "postprocess_altivec_template.c"
  461. #include "postprocess_template.c"
  462. #endif
  463. #endif //ARCH_POWERPC
  464. //MMX versions
  465. #ifdef COMPILE_MMX
  466. #undef RENAME
  467. #define HAVE_MMX
  468. #undef HAVE_MMX2
  469. #undef HAVE_3DNOW
  470. #define ARCH_X86
  471. #define RENAME(a) a ## _MMX
  472. #include "postprocess_template.c"
  473. #endif
  474. //MMX2 versions
  475. #ifdef COMPILE_MMX2
  476. #undef RENAME
  477. #define HAVE_MMX
  478. #define HAVE_MMX2
  479. #undef HAVE_3DNOW
  480. #define ARCH_X86
  481. #define RENAME(a) a ## _MMX2
  482. #include "postprocess_template.c"
  483. #endif
  484. //3DNOW versions
  485. #ifdef COMPILE_3DNOW
  486. #undef RENAME
  487. #define HAVE_MMX
  488. #undef HAVE_MMX2
  489. #define HAVE_3DNOW
  490. #define ARCH_X86
  491. #define RENAME(a) a ## _3DNow
  492. #include "postprocess_template.c"
  493. #endif
  494. // minor note: the HAVE_xyz is messed up after that line so dont use it
  495. static inline void postProcess(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
  496. QP_STORE_T QPs[], int QPStride, int isColor, pp_mode_t *vm, pp_context_t *vc)
  497. {
  498. PPContext *c= (PPContext *)vc;
  499. PPMode *ppMode= (PPMode *)vm;
  500. c->ppMode= *ppMode; //FIXME
  501. // useing ifs here as they are faster than function pointers allthough the
  502. // difference wouldnt be messureable here but its much better because
  503. // someone might exchange the cpu whithout restarting mplayer ;)
  504. #ifdef RUNTIME_CPUDETECT
  505. #ifdef ARCH_X86
  506. // ordered per speed fasterst first
  507. if(c->cpuCaps & PP_CPU_CAPS_MMX2)
  508. postProcess_MMX2(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  509. else if(c->cpuCaps & PP_CPU_CAPS_3DNOW)
  510. postProcess_3DNow(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  511. else if(c->cpuCaps & PP_CPU_CAPS_MMX)
  512. postProcess_MMX(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  513. else
  514. postProcess_C(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  515. #else
  516. #ifdef ARCH_POWERPC
  517. #ifdef HAVE_ALTIVEC
  518. else if(c->cpuCaps & PP_CPU_CAPS_ALTIVEC)
  519. postProcess_altivec(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  520. else
  521. #endif
  522. #endif
  523. postProcess_C(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  524. #endif
  525. #else //RUNTIME_CPUDETECT
  526. #ifdef HAVE_MMX2
  527. postProcess_MMX2(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  528. #elif defined (HAVE_3DNOW)
  529. postProcess_3DNow(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  530. #elif defined (HAVE_MMX)
  531. postProcess_MMX(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  532. #elif defined (HAVE_ALTIVEC)
  533. postProcess_altivec(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  534. #else
  535. postProcess_C(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
  536. #endif
  537. #endif //!RUNTIME_CPUDETECT
  538. }
  539. //static void postProcess(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
  540. // QP_STORE_T QPs[], int QPStride, int isColor, struct PPMode *ppMode);
  541. /* -pp Command line Help
  542. */
  543. char *pp_help=
  544. "<filterName>[:<option>[:<option>...]][[,|/][-]<filterName>[:<option>...]]...\n"
  545. "long form example:\n"
  546. "vdeblock:autoq/hdeblock:autoq/linblenddeint default,-vdeblock\n"
  547. "short form example:\n"
  548. "vb:a/hb:a/lb de,-vb\n"
  549. "more examples:\n"
  550. "tn:64:128:256\n"
  551. "Filters Options\n"
  552. "short long name short long option Description\n"
  553. "* * a autoq CPU power dependent enabler\n"
  554. " c chrom chrominance filtering enabled\n"
  555. " y nochrom chrominance filtering disabled\n"
  556. "hb hdeblock (2 threshold) horizontal deblocking filter\n"
  557. " 1. difference factor: default=32, higher -> more deblocking\n"
  558. " 2. flatness threshold: default=39, lower -> more deblocking\n"
  559. " the h & v deblocking filters share these\n"
  560. " so you can't set different thresholds for h / v\n"
  561. "vb vdeblock (2 threshold) vertical deblocking filter\n"
  562. "h1 x1hdeblock experimental h deblock filter 1\n"
  563. "v1 x1vdeblock experimental v deblock filter 1\n"
  564. "dr dering deringing filter\n"
  565. "al autolevels automatic brightness / contrast\n"
  566. " f fullyrange stretch luminance to (0..255)\n"
  567. "lb linblenddeint linear blend deinterlacer\n"
  568. "li linipoldeint linear interpolating deinterlace\n"
  569. "ci cubicipoldeint cubic interpolating deinterlacer\n"
  570. "md mediandeint median deinterlacer\n"
  571. "fd ffmpegdeint ffmpeg deinterlacer\n"
  572. "de default hb:a,vb:a,dr:a,al\n"
  573. "fa fast h1:a,v1:a,dr:a,al\n"
  574. "tn tmpnoise (3 threshold) temporal noise reducer\n"
  575. " 1. <= 2. <= 3. larger -> stronger filtering\n"
  576. "fq forceQuant <quantizer> force quantizer\n"
  577. ;
  578. pp_mode_t *pp_get_mode_by_name_and_quality(char *name, int quality)
  579. {
  580. char temp[GET_MODE_BUFFER_SIZE];
  581. char *p= temp;
  582. char *filterDelimiters= ",/";
  583. char *optionDelimiters= ":";
  584. struct PPMode *ppMode;
  585. char *filterToken;
  586. ppMode= memalign(8, sizeof(PPMode));
  587. ppMode->lumMode= 0;
  588. ppMode->chromMode= 0;
  589. ppMode->maxTmpNoise[0]= 700;
  590. ppMode->maxTmpNoise[1]= 1500;
  591. ppMode->maxTmpNoise[2]= 3000;
  592. ppMode->maxAllowedY= 234;
  593. ppMode->minAllowedY= 16;
  594. ppMode->baseDcDiff= 256/8;
  595. ppMode->flatnessThreshold= 56-16-1;
  596. ppMode->maxClippedThreshold= 0.01;
  597. ppMode->error=0;
  598. strncpy(temp, name, GET_MODE_BUFFER_SIZE);
  599. if(verbose>1) printf("pp: %s\n", name);
  600. for(;;){
  601. char *filterName;
  602. int q= 1000000; //PP_QUALITY_MAX;
  603. int chrom=-1;
  604. char *option;
  605. char *options[OPTIONS_ARRAY_SIZE];
  606. int i;
  607. int filterNameOk=0;
  608. int numOfUnknownOptions=0;
  609. int enable=1; //does the user want us to enabled or disabled the filter
  610. filterToken= strtok(p, filterDelimiters);
  611. if(filterToken == NULL) break;
  612. p+= strlen(filterToken) + 1; // p points to next filterToken
  613. filterName= strtok(filterToken, optionDelimiters);
  614. if(verbose>1) printf("pp: %s::%s\n", filterToken, filterName);
  615. if(*filterName == '-')
  616. {
  617. enable=0;
  618. filterName++;
  619. }
  620. for(;;){ //for all options
  621. option= strtok(NULL, optionDelimiters);
  622. if(option == NULL) break;
  623. if(verbose>1) printf("pp: option: %s\n", option);
  624. if(!strcmp("autoq", option) || !strcmp("a", option)) q= quality;
  625. else if(!strcmp("nochrom", option) || !strcmp("y", option)) chrom=0;
  626. else if(!strcmp("chrom", option) || !strcmp("c", option)) chrom=1;
  627. else
  628. {
  629. options[numOfUnknownOptions] = option;
  630. numOfUnknownOptions++;
  631. }
  632. if(numOfUnknownOptions >= OPTIONS_ARRAY_SIZE-1) break;
  633. }
  634. options[numOfUnknownOptions] = NULL;
  635. /* replace stuff from the replace Table */
  636. for(i=0; replaceTable[2*i]!=NULL; i++)
  637. {
  638. if(!strcmp(replaceTable[2*i], filterName))
  639. {
  640. int newlen= strlen(replaceTable[2*i + 1]);
  641. int plen;
  642. int spaceLeft;
  643. if(p==NULL) p= temp, *p=0; //last filter
  644. else p--, *p=','; //not last filter
  645. plen= strlen(p);
  646. spaceLeft= p - temp + plen;
  647. if(spaceLeft + newlen >= GET_MODE_BUFFER_SIZE)
  648. {
  649. ppMode->error++;
  650. break;
  651. }
  652. memmove(p + newlen, p, plen+1);
  653. memcpy(p, replaceTable[2*i + 1], newlen);
  654. filterNameOk=1;
  655. }
  656. }
  657. for(i=0; filters[i].shortName!=NULL; i++)
  658. {
  659. // printf("Compareing %s, %s, %s\n", filters[i].shortName,filters[i].longName, filterName);
  660. if( !strcmp(filters[i].longName, filterName)
  661. || !strcmp(filters[i].shortName, filterName))
  662. {
  663. ppMode->lumMode &= ~filters[i].mask;
  664. ppMode->chromMode &= ~filters[i].mask;
  665. filterNameOk=1;
  666. if(!enable) break; // user wants to disable it
  667. if(q >= filters[i].minLumQuality)
  668. ppMode->lumMode|= filters[i].mask;
  669. if(chrom==1 || (chrom==-1 && filters[i].chromDefault))
  670. if(q >= filters[i].minChromQuality)
  671. ppMode->chromMode|= filters[i].mask;
  672. if(filters[i].mask == LEVEL_FIX)
  673. {
  674. int o;
  675. ppMode->minAllowedY= 16;
  676. ppMode->maxAllowedY= 234;
  677. for(o=0; options[o]!=NULL; o++)
  678. {
  679. if( !strcmp(options[o],"fullyrange")
  680. ||!strcmp(options[o],"f"))
  681. {
  682. ppMode->minAllowedY= 0;
  683. ppMode->maxAllowedY= 255;
  684. numOfUnknownOptions--;
  685. }
  686. }
  687. }
  688. else if(filters[i].mask == TEMP_NOISE_FILTER)
  689. {
  690. int o;
  691. int numOfNoises=0;
  692. for(o=0; options[o]!=NULL; o++)
  693. {
  694. char *tail;
  695. ppMode->maxTmpNoise[numOfNoises]=
  696. strtol(options[o], &tail, 0);
  697. if(tail!=options[o])
  698. {
  699. numOfNoises++;
  700. numOfUnknownOptions--;
  701. if(numOfNoises >= 3) break;
  702. }
  703. }
  704. }
  705. else if(filters[i].mask == V_DEBLOCK || filters[i].mask == H_DEBLOCK)
  706. {
  707. int o;
  708. for(o=0; options[o]!=NULL && o<2; o++)
  709. {
  710. char *tail;
  711. int val= strtol(options[o], &tail, 0);
  712. if(tail==options[o]) break;
  713. numOfUnknownOptions--;
  714. if(o==0) ppMode->baseDcDiff= val;
  715. else ppMode->flatnessThreshold= val;
  716. }
  717. }
  718. else if(filters[i].mask == FORCE_QUANT)
  719. {
  720. int o;
  721. ppMode->forcedQuant= 15;
  722. for(o=0; options[o]!=NULL && o<1; o++)
  723. {
  724. char *tail;
  725. int val= strtol(options[o], &tail, 0);
  726. if(tail==options[o]) break;
  727. numOfUnknownOptions--;
  728. ppMode->forcedQuant= val;
  729. }
  730. }
  731. }
  732. }
  733. if(!filterNameOk) ppMode->error++;
  734. ppMode->error += numOfUnknownOptions;
  735. }
  736. if(verbose>1) printf("pp: lumMode=%X, chromMode=%X\n", ppMode->lumMode, ppMode->chromMode);
  737. if(ppMode->error)
  738. {
  739. fprintf(stderr, "%d errors in postprocess string \"%s\"\n", ppMode->error, name);
  740. free(ppMode);
  741. return NULL;
  742. }
  743. return ppMode;
  744. }
  745. void pp_free_mode(pp_mode_t *mode){
  746. if(mode) free(mode);
  747. }
  748. static void reallocAlign(void **p, int alignment, int size){
  749. if(*p) free(*p);
  750. *p= memalign(alignment, size);
  751. memset(*p, 0, size);
  752. }
  753. static void reallocBuffers(PPContext *c, int width, int height, int stride, int qpStride){
  754. int mbWidth = (width+15)>>4;
  755. int mbHeight= (height+15)>>4;
  756. int i;
  757. c->stride= stride;
  758. c->qpStride= qpStride;
  759. reallocAlign((void **)&c->tempDst, 8, stride*24);
  760. reallocAlign((void **)&c->tempSrc, 8, stride*24);
  761. reallocAlign((void **)&c->tempBlocks, 8, 2*16*8);
  762. reallocAlign((void **)&c->yHistogram, 8, 256*sizeof(uint64_t));
  763. for(i=0; i<256; i++)
  764. c->yHistogram[i]= width*height/64*15/256;
  765. for(i=0; i<3; i++)
  766. {
  767. //Note:the +17*1024 is just there so i dont have to worry about r/w over te end
  768. reallocAlign((void **)&c->tempBlured[i], 8, stride*mbHeight*16 + 17*1024);
  769. reallocAlign((void **)&c->tempBluredPast[i], 8, 256*((height+7)&(~7))/2 + 17*1024);//FIXME size
  770. }
  771. reallocAlign((void **)&c->deintTemp, 8, 2*width+32);
  772. reallocAlign((void **)&c->nonBQPTable, 8, qpStride*mbHeight*sizeof(QP_STORE_T));
  773. reallocAlign((void **)&c->stdQPTable, 8, qpStride*mbHeight*sizeof(QP_STORE_T));
  774. reallocAlign((void **)&c->forcedQPTable, 8, mbWidth*sizeof(QP_STORE_T));
  775. }
  776. static void global_init(void){
  777. int i;
  778. memset(clip_table, 0, 256);
  779. for(i=256; i<512; i++)
  780. clip_table[i]= i;
  781. memset(clip_table+512, 0, 256);
  782. }
  783. pp_context_t *pp_get_context(int width, int height, int cpuCaps){
  784. PPContext *c= memalign(32, sizeof(PPContext));
  785. int stride= (width+15)&(~15); //assumed / will realloc if needed
  786. int qpStride= (width+15)/16 + 2; //assumed / will realloc if needed
  787. global_init();
  788. memset(c, 0, sizeof(PPContext));
  789. c->cpuCaps= cpuCaps;
  790. if(cpuCaps&PP_FORMAT){
  791. c->hChromaSubSample= cpuCaps&0x3;
  792. c->vChromaSubSample= (cpuCaps>>4)&0x3;
  793. }else{
  794. c->hChromaSubSample= 1;
  795. c->vChromaSubSample= 1;
  796. }
  797. reallocBuffers(c, width, height, stride, qpStride);
  798. c->frameNum=-1;
  799. return c;
  800. }
  801. void pp_free_context(void *vc){
  802. PPContext *c = (PPContext*)vc;
  803. int i;
  804. for(i=0; i<3; i++) free(c->tempBlured[i]);
  805. for(i=0; i<3; i++) free(c->tempBluredPast[i]);
  806. free(c->tempBlocks);
  807. free(c->yHistogram);
  808. free(c->tempDst);
  809. free(c->tempSrc);
  810. free(c->deintTemp);
  811. free(c->stdQPTable);
  812. free(c->nonBQPTable);
  813. free(c->forcedQPTable);
  814. memset(c, 0, sizeof(PPContext));
  815. free(c);
  816. }
  817. void pp_postprocess(uint8_t * src[3], int srcStride[3],
  818. uint8_t * dst[3], int dstStride[3],
  819. int width, int height,
  820. QP_STORE_T *QP_store, int QPStride,
  821. pp_mode_t *vm, void *vc, int pict_type)
  822. {
  823. int mbWidth = (width+15)>>4;
  824. int mbHeight= (height+15)>>4;
  825. PPMode *mode = (PPMode*)vm;
  826. PPContext *c = (PPContext*)vc;
  827. int minStride= MAX(srcStride[0], dstStride[0]);
  828. if(c->stride < minStride || c->qpStride < QPStride)
  829. reallocBuffers(c, width, height,
  830. MAX(minStride, c->stride),
  831. MAX(c->qpStride, QPStride));
  832. if(QP_store==NULL || (mode->lumMode & FORCE_QUANT))
  833. {
  834. int i;
  835. QP_store= c->forcedQPTable;
  836. QPStride= 0;
  837. if(mode->lumMode & FORCE_QUANT)
  838. for(i=0; i<mbWidth; i++) QP_store[i]= mode->forcedQuant;
  839. else
  840. for(i=0; i<mbWidth; i++) QP_store[i]= 1;
  841. }
  842. //printf("pict_type:%d\n", pict_type);
  843. if(pict_type & PP_PICT_TYPE_QP2){
  844. int i;
  845. const int count= mbHeight * QPStride;
  846. for(i=0; i<(count>>2); i++){
  847. ((uint32_t*)c->stdQPTable)[i] = (((uint32_t*)QP_store)[i]>>1) & 0x7F7F7F7F;
  848. }
  849. for(i<<=2; i<count; i++){
  850. c->stdQPTable[i] = QP_store[i]>>1;
  851. }
  852. QP_store= c->stdQPTable;
  853. }
  854. if(0){
  855. int x,y;
  856. for(y=0; y<mbHeight; y++){
  857. for(x=0; x<mbWidth; x++){
  858. printf("%2d ", QP_store[x + y*QPStride]);
  859. }
  860. printf("\n");
  861. }
  862. printf("\n");
  863. }
  864. if((pict_type&7)!=3)
  865. {
  866. int i;
  867. const int count= mbHeight * QPStride;
  868. for(i=0; i<(count>>2); i++){
  869. ((uint32_t*)c->nonBQPTable)[i] = ((uint32_t*)QP_store)[i] & 0x3F3F3F3F;
  870. }
  871. for(i<<=2; i<count; i++){
  872. c->nonBQPTable[i] = QP_store[i] & 0x3F;
  873. }
  874. }
  875. if(verbose>2)
  876. {
  877. printf("using npp filters 0x%X/0x%X\n", mode->lumMode, mode->chromMode);
  878. }
  879. postProcess(src[0], srcStride[0], dst[0], dstStride[0],
  880. width, height, QP_store, QPStride, 0, mode, c);
  881. width = (width )>>c->hChromaSubSample;
  882. height = (height)>>c->vChromaSubSample;
  883. if(mode->chromMode)
  884. {
  885. postProcess(src[1], srcStride[1], dst[1], dstStride[1],
  886. width, height, QP_store, QPStride, 1, mode, c);
  887. postProcess(src[2], srcStride[2], dst[2], dstStride[2],
  888. width, height, QP_store, QPStride, 2, mode, c);
  889. }
  890. else if(srcStride[1] == dstStride[1] && srcStride[2] == dstStride[2])
  891. {
  892. memcpy(dst[1], src[1], srcStride[1]*height);
  893. memcpy(dst[2], src[2], srcStride[2]*height);
  894. }
  895. else
  896. {
  897. int y;
  898. for(y=0; y<height; y++)
  899. {
  900. memcpy(&(dst[1][y*dstStride[1]]), &(src[1][y*srcStride[1]]), width);
  901. memcpy(&(dst[2][y*dstStride[2]]), &(src[2][y*srcStride[2]]), width);
  902. }
  903. }
  904. }