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.

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