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.

1131 lines
44KB

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