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.

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