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.

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