postprocess.c 37 KB

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