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.

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