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.

1073 lines
38KB

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