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.

1089 lines
37KB

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