utils.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. /*
  2. * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * the C code (not assembly, mmx, ...) of this file can be used
  21. * under the LGPL license too
  22. */
  23. #define _SVID_SOURCE //needed for MAP_ANONYMOUS
  24. #include <inttypes.h>
  25. #include <string.h>
  26. #include <math.h>
  27. #include <stdio.h>
  28. #include "config.h"
  29. #include <assert.h>
  30. #if HAVE_SYS_MMAN_H
  31. #include <sys/mman.h>
  32. #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  33. #define MAP_ANONYMOUS MAP_ANON
  34. #endif
  35. #endif
  36. #if HAVE_VIRTUALALLOC
  37. #define WIN32_LEAN_AND_MEAN
  38. #include <windows.h>
  39. #endif
  40. #include "swscale.h"
  41. #include "swscale_internal.h"
  42. #include "rgb2rgb.h"
  43. #include "libavutil/intreadwrite.h"
  44. #include "libavutil/x86_cpu.h"
  45. #include "libavutil/avutil.h"
  46. #include "libavutil/bswap.h"
  47. #include "libavutil/pixdesc.h"
  48. unsigned swscale_version(void)
  49. {
  50. return LIBSWSCALE_VERSION_INT;
  51. }
  52. const char *swscale_configuration(void)
  53. {
  54. return FFMPEG_CONFIGURATION;
  55. }
  56. const char *swscale_license(void)
  57. {
  58. #define LICENSE_PREFIX "libswscale license: "
  59. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  60. }
  61. #define RET 0xC3 //near return opcode for x86
  62. #define isSupportedIn(x) ( \
  63. (x)==PIX_FMT_YUV420P \
  64. || (x)==PIX_FMT_YUVA420P \
  65. || (x)==PIX_FMT_YUYV422 \
  66. || (x)==PIX_FMT_UYVY422 \
  67. || (x)==PIX_FMT_RGB48BE \
  68. || (x)==PIX_FMT_RGB48LE \
  69. || (x)==PIX_FMT_RGB32 \
  70. || (x)==PIX_FMT_RGB32_1 \
  71. || (x)==PIX_FMT_BGR24 \
  72. || (x)==PIX_FMT_BGR565 \
  73. || (x)==PIX_FMT_BGR555 \
  74. || (x)==PIX_FMT_BGR32 \
  75. || (x)==PIX_FMT_BGR32_1 \
  76. || (x)==PIX_FMT_RGB24 \
  77. || (x)==PIX_FMT_RGB565 \
  78. || (x)==PIX_FMT_RGB555 \
  79. || (x)==PIX_FMT_GRAY8 \
  80. || (x)==PIX_FMT_YUV410P \
  81. || (x)==PIX_FMT_YUV440P \
  82. || (x)==PIX_FMT_NV12 \
  83. || (x)==PIX_FMT_NV21 \
  84. || (x)==PIX_FMT_GRAY16BE \
  85. || (x)==PIX_FMT_GRAY16LE \
  86. || (x)==PIX_FMT_YUV444P \
  87. || (x)==PIX_FMT_YUV422P \
  88. || (x)==PIX_FMT_YUV411P \
  89. || (x)==PIX_FMT_YUVJ420P \
  90. || (x)==PIX_FMT_YUVJ422P \
  91. || (x)==PIX_FMT_YUVJ440P \
  92. || (x)==PIX_FMT_YUVJ444P \
  93. || (x)==PIX_FMT_PAL8 \
  94. || (x)==PIX_FMT_BGR8 \
  95. || (x)==PIX_FMT_RGB8 \
  96. || (x)==PIX_FMT_BGR4_BYTE \
  97. || (x)==PIX_FMT_RGB4_BYTE \
  98. || (x)==PIX_FMT_YUV440P \
  99. || (x)==PIX_FMT_MONOWHITE \
  100. || (x)==PIX_FMT_MONOBLACK \
  101. || (x)==PIX_FMT_YUV420P16LE \
  102. || (x)==PIX_FMT_YUV422P16LE \
  103. || (x)==PIX_FMT_YUV444P16LE \
  104. || (x)==PIX_FMT_YUV420P16BE \
  105. || (x)==PIX_FMT_YUV422P16BE \
  106. || (x)==PIX_FMT_YUV444P16BE \
  107. )
  108. int sws_isSupportedInput(enum PixelFormat pix_fmt)
  109. {
  110. return isSupportedIn(pix_fmt);
  111. }
  112. #define isSupportedOut(x) ( \
  113. (x)==PIX_FMT_YUV420P \
  114. || (x)==PIX_FMT_YUVA420P \
  115. || (x)==PIX_FMT_YUYV422 \
  116. || (x)==PIX_FMT_UYVY422 \
  117. || (x)==PIX_FMT_YUV444P \
  118. || (x)==PIX_FMT_YUV422P \
  119. || (x)==PIX_FMT_YUV411P \
  120. || (x)==PIX_FMT_YUVJ420P \
  121. || (x)==PIX_FMT_YUVJ422P \
  122. || (x)==PIX_FMT_YUVJ440P \
  123. || (x)==PIX_FMT_YUVJ444P \
  124. || isAnyRGB(x) \
  125. || (x)==PIX_FMT_NV12 \
  126. || (x)==PIX_FMT_NV21 \
  127. || (x)==PIX_FMT_GRAY16BE \
  128. || (x)==PIX_FMT_GRAY16LE \
  129. || (x)==PIX_FMT_GRAY8 \
  130. || (x)==PIX_FMT_YUV410P \
  131. || (x)==PIX_FMT_YUV440P \
  132. || (x)==PIX_FMT_YUV420P16LE \
  133. || (x)==PIX_FMT_YUV422P16LE \
  134. || (x)==PIX_FMT_YUV444P16LE \
  135. || (x)==PIX_FMT_YUV420P16BE \
  136. || (x)==PIX_FMT_YUV422P16BE \
  137. || (x)==PIX_FMT_YUV444P16BE \
  138. )
  139. int sws_isSupportedOutput(enum PixelFormat pix_fmt)
  140. {
  141. return isSupportedOut(pix_fmt);
  142. }
  143. extern const int32_t ff_yuv2rgb_coeffs[8][4];
  144. const char *sws_format_name(enum PixelFormat format)
  145. {
  146. if ((unsigned)format < PIX_FMT_NB && av_pix_fmt_descriptors[format].name)
  147. return av_pix_fmt_descriptors[format].name;
  148. else
  149. return "Unknown format";
  150. }
  151. static double getSplineCoeff(double a, double b, double c, double d, double dist)
  152. {
  153. // printf("%f %f %f %f %f\n", a,b,c,d,dist);
  154. if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
  155. else return getSplineCoeff( 0.0,
  156. b+ 2.0*c + 3.0*d,
  157. c + 3.0*d,
  158. -b- 3.0*c - 6.0*d,
  159. dist-1.0);
  160. }
  161. static int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  162. int srcW, int dstW, int filterAlign, int one, int flags,
  163. SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
  164. {
  165. int i;
  166. int filterSize;
  167. int filter2Size;
  168. int minFilterSize;
  169. int64_t *filter=NULL;
  170. int64_t *filter2=NULL;
  171. const int64_t fone= 1LL<<54;
  172. int ret= -1;
  173. #if ARCH_X86
  174. if (flags & SWS_CPU_CAPS_MMX)
  175. __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
  176. #endif
  177. // NOTE: the +1 is for the MMX scaler which reads over the end
  178. FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW+1)*sizeof(int16_t), fail);
  179. if (FFABS(xInc - 0x10000) <10) { // unscaled
  180. int i;
  181. filterSize= 1;
  182. FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
  183. for (i=0; i<dstW; i++) {
  184. filter[i*filterSize]= fone;
  185. (*filterPos)[i]=i;
  186. }
  187. } else if (flags&SWS_POINT) { // lame looking point sampling mode
  188. int i;
  189. int xDstInSrc;
  190. filterSize= 1;
  191. FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
  192. xDstInSrc= xInc/2 - 0x8000;
  193. for (i=0; i<dstW; i++) {
  194. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  195. (*filterPos)[i]= xx;
  196. filter[i]= fone;
  197. xDstInSrc+= xInc;
  198. }
  199. } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
  200. int i;
  201. int xDstInSrc;
  202. filterSize= 2;
  203. FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
  204. xDstInSrc= xInc/2 - 0x8000;
  205. for (i=0; i<dstW; i++) {
  206. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  207. int j;
  208. (*filterPos)[i]= xx;
  209. //bilinear upscale / linear interpolate / area averaging
  210. for (j=0; j<filterSize; j++) {
  211. int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
  212. if (coeff<0) coeff=0;
  213. filter[i*filterSize + j]= coeff;
  214. xx++;
  215. }
  216. xDstInSrc+= xInc;
  217. }
  218. } else {
  219. int xDstInSrc;
  220. int sizeFactor;
  221. if (flags&SWS_BICUBIC) sizeFactor= 4;
  222. else if (flags&SWS_X) sizeFactor= 8;
  223. else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
  224. else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
  225. else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
  226. else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
  227. else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
  228. else if (flags&SWS_BILINEAR) sizeFactor= 2;
  229. else {
  230. sizeFactor= 0; //GCC warning killer
  231. assert(0);
  232. }
  233. if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
  234. else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
  235. if (filterSize > srcW-2) filterSize=srcW-2;
  236. FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
  237. xDstInSrc= xInc - 0x10000;
  238. for (i=0; i<dstW; i++) {
  239. int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
  240. int j;
  241. (*filterPos)[i]= xx;
  242. for (j=0; j<filterSize; j++) {
  243. int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
  244. double floatd;
  245. int64_t coeff;
  246. if (xInc > 1<<16)
  247. d= d*dstW/srcW;
  248. floatd= d * (1.0/(1<<30));
  249. if (flags & SWS_BICUBIC) {
  250. int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
  251. int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
  252. int64_t dd = ( d*d)>>30;
  253. int64_t ddd= (dd*d)>>30;
  254. if (d < 1LL<<30)
  255. coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
  256. else if (d < 1LL<<31)
  257. coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
  258. else
  259. coeff=0.0;
  260. coeff *= fone>>(30+24);
  261. }
  262. /* else if (flags & SWS_X) {
  263. double p= param ? param*0.01 : 0.3;
  264. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  265. coeff*= pow(2.0, - p*d*d);
  266. }*/
  267. else if (flags & SWS_X) {
  268. double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
  269. double c;
  270. if (floatd<1.0)
  271. c = cos(floatd*M_PI);
  272. else
  273. c=-1.0;
  274. if (c<0.0) c= -pow(-c, A);
  275. else c= pow( c, A);
  276. coeff= (c*0.5 + 0.5)*fone;
  277. } else if (flags & SWS_AREA) {
  278. int64_t d2= d - (1<<29);
  279. if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
  280. else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
  281. else coeff=0.0;
  282. coeff *= fone>>(30+16);
  283. } else if (flags & SWS_GAUSS) {
  284. double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  285. coeff = (pow(2.0, - p*floatd*floatd))*fone;
  286. } else if (flags & SWS_SINC) {
  287. coeff = (d ? sin(floatd*M_PI)/(floatd*M_PI) : 1.0)*fone;
  288. } else if (flags & SWS_LANCZOS) {
  289. double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  290. coeff = (d ? sin(floatd*M_PI)*sin(floatd*M_PI/p)/(floatd*floatd*M_PI*M_PI/p) : 1.0)*fone;
  291. if (floatd>p) coeff=0;
  292. } else if (flags & SWS_BILINEAR) {
  293. coeff= (1<<30) - d;
  294. if (coeff<0) coeff=0;
  295. coeff *= fone >> 30;
  296. } else if (flags & SWS_SPLINE) {
  297. double p=-2.196152422706632;
  298. coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
  299. } else {
  300. coeff= 0.0; //GCC warning killer
  301. assert(0);
  302. }
  303. filter[i*filterSize + j]= coeff;
  304. xx++;
  305. }
  306. xDstInSrc+= 2*xInc;
  307. }
  308. }
  309. /* apply src & dst Filter to filter -> filter2
  310. av_free(filter);
  311. */
  312. assert(filterSize>0);
  313. filter2Size= filterSize;
  314. if (srcFilter) filter2Size+= srcFilter->length - 1;
  315. if (dstFilter) filter2Size+= dstFilter->length - 1;
  316. assert(filter2Size>0);
  317. FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);
  318. for (i=0; i<dstW; i++) {
  319. int j, k;
  320. if(srcFilter) {
  321. for (k=0; k<srcFilter->length; k++) {
  322. for (j=0; j<filterSize; j++)
  323. filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
  324. }
  325. } else {
  326. for (j=0; j<filterSize; j++)
  327. filter2[i*filter2Size + j]= filter[i*filterSize + j];
  328. }
  329. //FIXME dstFilter
  330. (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
  331. }
  332. av_freep(&filter);
  333. /* try to reduce the filter-size (step1 find size and shift left) */
  334. // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
  335. minFilterSize= 0;
  336. for (i=dstW-1; i>=0; i--) {
  337. int min= filter2Size;
  338. int j;
  339. int64_t cutOff=0.0;
  340. /* get rid of near zero elements on the left by shifting left */
  341. for (j=0; j<filter2Size; j++) {
  342. int k;
  343. cutOff += FFABS(filter2[i*filter2Size]);
  344. if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
  345. /* preserve monotonicity because the core can't handle the filter otherwise */
  346. if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
  347. // move filter coefficients left
  348. for (k=1; k<filter2Size; k++)
  349. filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
  350. filter2[i*filter2Size + k - 1]= 0;
  351. (*filterPos)[i]++;
  352. }
  353. cutOff=0;
  354. /* count near zeros on the right */
  355. for (j=filter2Size-1; j>0; j--) {
  356. cutOff += FFABS(filter2[i*filter2Size + j]);
  357. if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
  358. min--;
  359. }
  360. if (min>minFilterSize) minFilterSize= min;
  361. }
  362. if (flags & SWS_CPU_CAPS_ALTIVEC) {
  363. // we can handle the special case 4,
  364. // so we don't want to go to the full 8
  365. if (minFilterSize < 5)
  366. filterAlign = 4;
  367. // We really don't want to waste our time
  368. // doing useless computation, so fall back on
  369. // the scalar C code for very small filters.
  370. // Vectorizing is worth it only if you have a
  371. // decent-sized vector.
  372. if (minFilterSize < 3)
  373. filterAlign = 1;
  374. }
  375. if (flags & SWS_CPU_CAPS_MMX) {
  376. // special case for unscaled vertical filtering
  377. if (minFilterSize == 1 && filterAlign == 2)
  378. filterAlign= 1;
  379. }
  380. assert(minFilterSize > 0);
  381. filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
  382. assert(filterSize > 0);
  383. filter= av_malloc(filterSize*dstW*sizeof(*filter));
  384. if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
  385. goto fail;
  386. *outFilterSize= filterSize;
  387. if (flags&SWS_PRINT_INFO)
  388. av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
  389. /* try to reduce the filter-size (step2 reduce it) */
  390. for (i=0; i<dstW; i++) {
  391. int j;
  392. for (j=0; j<filterSize; j++) {
  393. if (j>=filter2Size) filter[i*filterSize + j]= 0;
  394. else filter[i*filterSize + j]= filter2[i*filter2Size + j];
  395. if((flags & SWS_BITEXACT) && j>=minFilterSize)
  396. filter[i*filterSize + j]= 0;
  397. }
  398. }
  399. //FIXME try to align filterPos if possible
  400. //fix borders
  401. for (i=0; i<dstW; i++) {
  402. int j;
  403. if ((*filterPos)[i] < 0) {
  404. // move filter coefficients left to compensate for filterPos
  405. for (j=1; j<filterSize; j++) {
  406. int left= FFMAX(j + (*filterPos)[i], 0);
  407. filter[i*filterSize + left] += filter[i*filterSize + j];
  408. filter[i*filterSize + j]=0;
  409. }
  410. (*filterPos)[i]= 0;
  411. }
  412. if ((*filterPos)[i] + filterSize > srcW) {
  413. int shift= (*filterPos)[i] + filterSize - srcW;
  414. // move filter coefficients right to compensate for filterPos
  415. for (j=filterSize-2; j>=0; j--) {
  416. int right= FFMIN(j + shift, filterSize-1);
  417. filter[i*filterSize +right] += filter[i*filterSize +j];
  418. filter[i*filterSize +j]=0;
  419. }
  420. (*filterPos)[i]= srcW - filterSize;
  421. }
  422. }
  423. // Note the +1 is for the MMX scaler which reads over the end
  424. /* align at 16 for AltiVec (needed by hScale_altivec_real) */
  425. FF_ALLOCZ_OR_GOTO(NULL, *outFilter, *outFilterSize*(dstW+1)*sizeof(int16_t), fail);
  426. /* normalize & store in outFilter */
  427. for (i=0; i<dstW; i++) {
  428. int j;
  429. int64_t error=0;
  430. int64_t sum=0;
  431. for (j=0; j<filterSize; j++) {
  432. sum+= filter[i*filterSize + j];
  433. }
  434. sum= (sum + one/2)/ one;
  435. for (j=0; j<*outFilterSize; j++) {
  436. int64_t v= filter[i*filterSize + j] + error;
  437. int intV= ROUNDED_DIV(v, sum);
  438. (*outFilter)[i*(*outFilterSize) + j]= intV;
  439. error= v - intV*sum;
  440. }
  441. }
  442. (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
  443. for (i=0; i<*outFilterSize; i++) {
  444. int j= dstW*(*outFilterSize);
  445. (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
  446. }
  447. ret=0;
  448. fail:
  449. av_free(filter);
  450. av_free(filter2);
  451. return ret;
  452. }
  453. #if ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
  454. static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
  455. {
  456. uint8_t *fragmentA;
  457. x86_reg imm8OfPShufW1A;
  458. x86_reg imm8OfPShufW2A;
  459. x86_reg fragmentLengthA;
  460. uint8_t *fragmentB;
  461. x86_reg imm8OfPShufW1B;
  462. x86_reg imm8OfPShufW2B;
  463. x86_reg fragmentLengthB;
  464. int fragmentPos;
  465. int xpos, i;
  466. // create an optimized horizontal scaling routine
  467. /* This scaler is made of runtime-generated MMX2 code using specially
  468. * tuned pshufw instructions. For every four output pixels, if four
  469. * input pixels are enough for the fast bilinear scaling, then a chunk
  470. * of fragmentB is used. If five input pixels are needed, then a chunk
  471. * of fragmentA is used.
  472. */
  473. //code fragment
  474. __asm__ volatile(
  475. "jmp 9f \n\t"
  476. // Begin
  477. "0: \n\t"
  478. "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
  479. "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
  480. "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
  481. "punpcklbw %%mm7, %%mm1 \n\t"
  482. "punpcklbw %%mm7, %%mm0 \n\t"
  483. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  484. "1: \n\t"
  485. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  486. "2: \n\t"
  487. "psubw %%mm1, %%mm0 \n\t"
  488. "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
  489. "pmullw %%mm3, %%mm0 \n\t"
  490. "psllw $7, %%mm1 \n\t"
  491. "paddw %%mm1, %%mm0 \n\t"
  492. "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
  493. "add $8, %%"REG_a" \n\t"
  494. // End
  495. "9: \n\t"
  496. // "int $3 \n\t"
  497. "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
  498. "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
  499. "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
  500. "dec %1 \n\t"
  501. "dec %2 \n\t"
  502. "sub %0, %1 \n\t"
  503. "sub %0, %2 \n\t"
  504. "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
  505. "sub %0, %3 \n\t"
  506. :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  507. "=r" (fragmentLengthA)
  508. );
  509. __asm__ volatile(
  510. "jmp 9f \n\t"
  511. // Begin
  512. "0: \n\t"
  513. "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
  514. "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
  515. "punpcklbw %%mm7, %%mm0 \n\t"
  516. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  517. "1: \n\t"
  518. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  519. "2: \n\t"
  520. "psubw %%mm1, %%mm0 \n\t"
  521. "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
  522. "pmullw %%mm3, %%mm0 \n\t"
  523. "psllw $7, %%mm1 \n\t"
  524. "paddw %%mm1, %%mm0 \n\t"
  525. "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
  526. "add $8, %%"REG_a" \n\t"
  527. // End
  528. "9: \n\t"
  529. // "int $3 \n\t"
  530. "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
  531. "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
  532. "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
  533. "dec %1 \n\t"
  534. "dec %2 \n\t"
  535. "sub %0, %1 \n\t"
  536. "sub %0, %2 \n\t"
  537. "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
  538. "sub %0, %3 \n\t"
  539. :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  540. "=r" (fragmentLengthB)
  541. );
  542. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  543. fragmentPos=0;
  544. for (i=0; i<dstW/numSplits; i++) {
  545. int xx=xpos>>16;
  546. if ((i&3) == 0) {
  547. int a=0;
  548. int b=((xpos+xInc)>>16) - xx;
  549. int c=((xpos+xInc*2)>>16) - xx;
  550. int d=((xpos+xInc*3)>>16) - xx;
  551. int inc = (d+1<4);
  552. uint8_t *fragment = (d+1<4) ? fragmentB : fragmentA;
  553. x86_reg imm8OfPShufW1 = (d+1<4) ? imm8OfPShufW1B : imm8OfPShufW1A;
  554. x86_reg imm8OfPShufW2 = (d+1<4) ? imm8OfPShufW2B : imm8OfPShufW2A;
  555. x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
  556. int maxShift= 3-(d+inc);
  557. int shift=0;
  558. if (filterCode) {
  559. filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
  560. filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
  561. filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
  562. filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
  563. filterPos[i/2]= xx;
  564. memcpy(filterCode + fragmentPos, fragment, fragmentLength);
  565. filterCode[fragmentPos + imm8OfPShufW1]=
  566. (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
  567. filterCode[fragmentPos + imm8OfPShufW2]=
  568. a | (b<<2) | (c<<4) | (d<<6);
  569. if (i+4-inc>=dstW) shift=maxShift; //avoid overread
  570. else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
  571. if (shift && i>=shift) {
  572. filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
  573. filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
  574. filterPos[i/2]-=shift;
  575. }
  576. }
  577. fragmentPos+= fragmentLength;
  578. if (filterCode)
  579. filterCode[fragmentPos]= RET;
  580. }
  581. xpos+=xInc;
  582. }
  583. if (filterCode)
  584. filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
  585. return fragmentPos + 1;
  586. }
  587. #endif /* ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL */
  588. static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
  589. {
  590. *h = av_pix_fmt_descriptors[format].log2_chroma_w;
  591. *v = av_pix_fmt_descriptors[format].log2_chroma_h;
  592. }
  593. static uint16_t roundToInt16(int64_t f)
  594. {
  595. int r= (f + (1<<15))>>16;
  596. if (r<-0x7FFF) return 0x8000;
  597. else if (r> 0x7FFF) return 0x7FFF;
  598. else return r;
  599. }
  600. int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
  601. {
  602. int64_t crv = inv_table[0];
  603. int64_t cbu = inv_table[1];
  604. int64_t cgu = -inv_table[2];
  605. int64_t cgv = -inv_table[3];
  606. int64_t cy = 1<<16;
  607. int64_t oy = 0;
  608. memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
  609. memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
  610. c->brightness= brightness;
  611. c->contrast = contrast;
  612. c->saturation= saturation;
  613. c->srcRange = srcRange;
  614. c->dstRange = dstRange;
  615. if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  616. c->uOffset= 0x0400040004000400LL;
  617. c->vOffset= 0x0400040004000400LL;
  618. if (!srcRange) {
  619. cy= (cy*255) / 219;
  620. oy= 16<<16;
  621. } else {
  622. crv= (crv*224) / 255;
  623. cbu= (cbu*224) / 255;
  624. cgu= (cgu*224) / 255;
  625. cgv= (cgv*224) / 255;
  626. }
  627. cy = (cy *contrast )>>16;
  628. crv= (crv*contrast * saturation)>>32;
  629. cbu= (cbu*contrast * saturation)>>32;
  630. cgu= (cgu*contrast * saturation)>>32;
  631. cgv= (cgv*contrast * saturation)>>32;
  632. oy -= 256*brightness;
  633. c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
  634. c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
  635. c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
  636. c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
  637. c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
  638. c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
  639. c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
  640. c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
  641. c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
  642. c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
  643. c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
  644. c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
  645. ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
  646. //FIXME factorize
  647. #if ARCH_PPC && HAVE_ALTIVEC
  648. if (c->flags & SWS_CPU_CAPS_ALTIVEC)
  649. ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
  650. #endif
  651. return 0;
  652. }
  653. int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
  654. {
  655. if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  656. *inv_table = c->srcColorspaceTable;
  657. *table = c->dstColorspaceTable;
  658. *srcRange = c->srcRange;
  659. *dstRange = c->dstRange;
  660. *brightness= c->brightness;
  661. *contrast = c->contrast;
  662. *saturation= c->saturation;
  663. return 0;
  664. }
  665. static int handle_jpeg(enum PixelFormat *format)
  666. {
  667. switch (*format) {
  668. case PIX_FMT_YUVJ420P:
  669. *format = PIX_FMT_YUV420P;
  670. return 1;
  671. case PIX_FMT_YUVJ422P:
  672. *format = PIX_FMT_YUV422P;
  673. return 1;
  674. case PIX_FMT_YUVJ444P:
  675. *format = PIX_FMT_YUV444P;
  676. return 1;
  677. case PIX_FMT_YUVJ440P:
  678. *format = PIX_FMT_YUV440P;
  679. return 1;
  680. default:
  681. return 0;
  682. }
  683. }
  684. SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
  685. int dstW, int dstH, enum PixelFormat dstFormat, int flags,
  686. SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  687. {
  688. SwsContext *c;
  689. int i;
  690. int usesVFilter, usesHFilter;
  691. int unscaled;
  692. int srcRange, dstRange;
  693. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  694. #if ARCH_X86
  695. if (flags & SWS_CPU_CAPS_MMX)
  696. __asm__ volatile("emms\n\t"::: "memory");
  697. #endif
  698. #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
  699. flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
  700. flags |= ff_hardcodedcpuflags();
  701. #endif /* CONFIG_RUNTIME_CPUDETECT */
  702. if (!rgb15to16) sws_rgb2rgb_init(flags);
  703. unscaled = (srcW == dstW && srcH == dstH);
  704. srcRange = handle_jpeg(&srcFormat);
  705. dstRange = handle_jpeg(&dstFormat);
  706. if (!isSupportedIn(srcFormat)) {
  707. av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
  708. return NULL;
  709. }
  710. if (!isSupportedOut(dstFormat)) {
  711. av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
  712. return NULL;
  713. }
  714. i= flags & ( SWS_POINT
  715. |SWS_AREA
  716. |SWS_BILINEAR
  717. |SWS_FAST_BILINEAR
  718. |SWS_BICUBIC
  719. |SWS_X
  720. |SWS_GAUSS
  721. |SWS_LANCZOS
  722. |SWS_SINC
  723. |SWS_SPLINE
  724. |SWS_BICUBLIN);
  725. if(!i || (i & (i-1))) {
  726. av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
  727. return NULL;
  728. }
  729. /* sanity check */
  730. if (srcW<4 || srcH<1 || dstW<8 || dstH<1) { //FIXME check if these are enough and try to lowwer them after fixing the relevant parts of the code
  731. av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  732. srcW, srcH, dstW, dstH);
  733. return NULL;
  734. }
  735. if(srcW > VOFW || dstW > VOFW) {
  736. av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
  737. return NULL;
  738. }
  739. if (!dstFilter) dstFilter= &dummyFilter;
  740. if (!srcFilter) srcFilter= &dummyFilter;
  741. FF_ALLOCZ_OR_GOTO(NULL, c, sizeof(SwsContext), fail);
  742. c->av_class = &sws_context_class;
  743. c->srcW= srcW;
  744. c->srcH= srcH;
  745. c->dstW= dstW;
  746. c->dstH= dstH;
  747. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  748. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  749. c->flags= flags;
  750. c->dstFormat= dstFormat;
  751. c->srcFormat= srcFormat;
  752. c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
  753. c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
  754. c->vRounder= 4* 0x0001000100010001ULL;
  755. usesVFilter = (srcFilter->lumV && srcFilter->lumV->length>1) ||
  756. (srcFilter->chrV && srcFilter->chrV->length>1) ||
  757. (dstFilter->lumV && dstFilter->lumV->length>1) ||
  758. (dstFilter->chrV && dstFilter->chrV->length>1);
  759. usesHFilter = (srcFilter->lumH && srcFilter->lumH->length>1) ||
  760. (srcFilter->chrH && srcFilter->chrH->length>1) ||
  761. (dstFilter->lumH && dstFilter->lumH->length>1) ||
  762. (dstFilter->chrH && dstFilter->chrH->length>1);
  763. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  764. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  765. // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
  766. if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  767. // drop some chroma lines if the user wants it
  768. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  769. c->chrSrcVSubSample+= c->vChrDrop;
  770. // drop every other pixel for chroma calculation unless user wants full chroma
  771. if (isAnyRGB(srcFormat) && !(flags&SWS_FULL_CHR_H_INP)
  772. && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
  773. && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
  774. && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
  775. && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  776. c->chrSrcHSubSample=1;
  777. if (param) {
  778. c->param[0] = param[0];
  779. c->param[1] = param[1];
  780. } else {
  781. c->param[0] =
  782. c->param[1] = SWS_PARAM_DEFAULT;
  783. }
  784. // Note the -((-x)>>y) is so that we always round toward +inf.
  785. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  786. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  787. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  788. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  789. sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
  790. /* unscaled special cases */
  791. if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isAnyRGB(dstFormat))) {
  792. ff_get_unscaled_swscale(c);
  793. if (c->swScale) {
  794. if (flags&SWS_PRINT_INFO)
  795. av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
  796. sws_format_name(srcFormat), sws_format_name(dstFormat));
  797. return c;
  798. }
  799. }
  800. if (flags & SWS_CPU_CAPS_MMX2) {
  801. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  802. if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
  803. if (flags&SWS_PRINT_INFO)
  804. av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
  805. }
  806. if (usesHFilter) c->canMMX2BeUsed=0;
  807. }
  808. else
  809. c->canMMX2BeUsed=0;
  810. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  811. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  812. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  813. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  814. // n-2 is the last chrominance sample available
  815. // this is not perfect, but no one should notice the difference, the more correct variant
  816. // would be like the vertical one, but that would require some special code for the
  817. // first and last pixel
  818. if (flags&SWS_FAST_BILINEAR) {
  819. if (c->canMMX2BeUsed) {
  820. c->lumXInc+= 20;
  821. c->chrXInc+= 20;
  822. }
  823. //we don't use the x86 asm scaler if MMX is available
  824. else if (flags & SWS_CPU_CAPS_MMX) {
  825. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  826. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  827. }
  828. }
  829. /* precalculate horizontal scaler filter coefficients */
  830. {
  831. #if ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
  832. // can't downscale !!!
  833. if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
  834. c->lumMmx2FilterCodeSize = initMMX2HScaler( dstW, c->lumXInc, NULL, NULL, NULL, 8);
  835. c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
  836. #ifdef MAP_ANONYMOUS
  837. c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  838. c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  839. #elif HAVE_VIRTUALALLOC
  840. c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  841. c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  842. #else
  843. c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
  844. c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
  845. #endif
  846. if (!c->lumMmx2FilterCode || !c->chrMmx2FilterCode)
  847. goto fail;
  848. FF_ALLOCZ_OR_GOTO(c, c->hLumFilter , (dstW /8+8)*sizeof(int16_t), fail);
  849. FF_ALLOCZ_OR_GOTO(c, c->hChrFilter , (c->chrDstW /4+8)*sizeof(int16_t), fail);
  850. FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW /2/8+8)*sizeof(int32_t), fail);
  851. FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
  852. initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->hLumFilter, c->hLumFilterPos, 8);
  853. initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->hChrFilter, c->hChrFilterPos, 4);
  854. #ifdef MAP_ANONYMOUS
  855. mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  856. mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  857. #endif
  858. } else
  859. #endif /* ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL */
  860. {
  861. const int filterAlign=
  862. (flags & SWS_CPU_CAPS_MMX) ? 4 :
  863. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  864. 1;
  865. if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  866. srcW , dstW, filterAlign, 1<<14,
  867. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  868. srcFilter->lumH, dstFilter->lumH, c->param) < 0)
  869. goto fail;
  870. if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  871. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  872. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  873. srcFilter->chrH, dstFilter->chrH, c->param) < 0)
  874. goto fail;
  875. }
  876. } // initialize horizontal stuff
  877. /* precalculate vertical scaler filter coefficients */
  878. {
  879. const int filterAlign=
  880. (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
  881. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  882. 1;
  883. if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  884. srcH , dstH, filterAlign, (1<<12),
  885. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  886. srcFilter->lumV, dstFilter->lumV, c->param) < 0)
  887. goto fail;
  888. if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  889. c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
  890. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  891. srcFilter->chrV, dstFilter->chrV, c->param) < 0)
  892. goto fail;
  893. #if ARCH_PPC && HAVE_ALTIVEC
  894. FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
  895. FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
  896. for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
  897. int j;
  898. short *p = (short *)&c->vYCoeffsBank[i];
  899. for (j=0;j<8;j++)
  900. p[j] = c->vLumFilter[i];
  901. }
  902. for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
  903. int j;
  904. short *p = (short *)&c->vCCoeffsBank[i];
  905. for (j=0;j<8;j++)
  906. p[j] = c->vChrFilter[i];
  907. }
  908. #endif
  909. }
  910. // calculate buffer sizes so that they won't run out while handling these damn slices
  911. c->vLumBufSize= c->vLumFilterSize;
  912. c->vChrBufSize= c->vChrFilterSize;
  913. for (i=0; i<dstH; i++) {
  914. int chrI= i*c->chrDstH / dstH;
  915. int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  916. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  917. nextSlice>>= c->chrSrcVSubSample;
  918. nextSlice<<= c->chrSrcVSubSample;
  919. if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  920. c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
  921. if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  922. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  923. }
  924. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  925. // allocate several megabytes to handle all possible cases)
  926. FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
  927. FF_ALLOC_OR_GOTO(c, c->chrPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
  928. if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
  929. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
  930. //Note we need at least one pixel more at the end because of the MMX code (just in case someone wanna replace the 4000/8000)
  931. /* align at 16 bytes for AltiVec */
  932. for (i=0; i<c->vLumBufSize; i++) {
  933. FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], VOF+1, fail);
  934. c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
  935. }
  936. for (i=0; i<c->vChrBufSize; i++) {
  937. FF_ALLOC_OR_GOTO(c, c->chrPixBuf[i+c->vChrBufSize], (VOF+1)*2, fail);
  938. c->chrPixBuf[i] = c->chrPixBuf[i+c->vChrBufSize];
  939. }
  940. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  941. for (i=0; i<c->vLumBufSize; i++) {
  942. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], VOF+1, fail);
  943. c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
  944. }
  945. //try to avoid drawing green stuff between the right end and the stride end
  946. for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
  947. assert(2*VOFW == VOF);
  948. assert(c->chrDstH <= dstH);
  949. if (flags&SWS_PRINT_INFO) {
  950. if (flags&SWS_FAST_BILINEAR)
  951. av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
  952. else if (flags&SWS_BILINEAR)
  953. av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
  954. else if (flags&SWS_BICUBIC)
  955. av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
  956. else if (flags&SWS_X)
  957. av_log(c, AV_LOG_INFO, "Experimental scaler, ");
  958. else if (flags&SWS_POINT)
  959. av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
  960. else if (flags&SWS_AREA)
  961. av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
  962. else if (flags&SWS_BICUBLIN)
  963. av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
  964. else if (flags&SWS_GAUSS)
  965. av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
  966. else if (flags&SWS_SINC)
  967. av_log(c, AV_LOG_INFO, "Sinc scaler, ");
  968. else if (flags&SWS_LANCZOS)
  969. av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
  970. else if (flags&SWS_SPLINE)
  971. av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
  972. else
  973. av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
  974. av_log(c, AV_LOG_INFO, "from %s to %s%s ",
  975. sws_format_name(srcFormat),
  976. #ifdef DITHER1XBPP
  977. dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ||
  978. dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
  979. dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE ? "dithered " : "",
  980. #else
  981. "",
  982. #endif
  983. sws_format_name(dstFormat));
  984. if (flags & SWS_CPU_CAPS_MMX2)
  985. av_log(c, AV_LOG_INFO, "using MMX2\n");
  986. else if (flags & SWS_CPU_CAPS_3DNOW)
  987. av_log(c, AV_LOG_INFO, "using 3DNOW\n");
  988. else if (flags & SWS_CPU_CAPS_MMX)
  989. av_log(c, AV_LOG_INFO, "using MMX\n");
  990. else if (flags & SWS_CPU_CAPS_ALTIVEC)
  991. av_log(c, AV_LOG_INFO, "using AltiVec\n");
  992. else
  993. av_log(c, AV_LOG_INFO, "using C\n");
  994. if (flags & SWS_CPU_CAPS_MMX) {
  995. if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  996. av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  997. else {
  998. if (c->hLumFilterSize==4)
  999. av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
  1000. else if (c->hLumFilterSize==8)
  1001. av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
  1002. else
  1003. av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
  1004. if (c->hChrFilterSize==4)
  1005. av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
  1006. else if (c->hChrFilterSize==8)
  1007. av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
  1008. else
  1009. av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
  1010. }
  1011. } else {
  1012. #if ARCH_X86
  1013. av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
  1014. #else
  1015. if (flags & SWS_FAST_BILINEAR)
  1016. av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
  1017. else
  1018. av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
  1019. #endif
  1020. }
  1021. if (isPlanarYUV(dstFormat)) {
  1022. if (c->vLumFilterSize==1)
  1023. av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1024. else
  1025. av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1026. } else {
  1027. if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
  1028. av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  1029. " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1030. else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
  1031. av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1032. else
  1033. av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1034. }
  1035. if (dstFormat==PIX_FMT_BGR24)
  1036. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
  1037. (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
  1038. else if (dstFormat==PIX_FMT_RGB32)
  1039. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1040. else if (dstFormat==PIX_FMT_BGR565)
  1041. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1042. else if (dstFormat==PIX_FMT_BGR555)
  1043. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1044. else if (dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
  1045. dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE)
  1046. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR12 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1047. av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  1048. av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1049. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  1050. av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1051. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  1052. }
  1053. c->swScale= ff_getSwsFunc(c);
  1054. return c;
  1055. fail:
  1056. sws_freeContext(c);
  1057. return NULL;
  1058. }
  1059. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  1060. float lumaSharpen, float chromaSharpen,
  1061. float chromaHShift, float chromaVShift,
  1062. int verbose)
  1063. {
  1064. SwsFilter *filter= av_malloc(sizeof(SwsFilter));
  1065. if (!filter)
  1066. return NULL;
  1067. if (lumaGBlur!=0.0) {
  1068. filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
  1069. filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
  1070. } else {
  1071. filter->lumH= sws_getIdentityVec();
  1072. filter->lumV= sws_getIdentityVec();
  1073. }
  1074. if (chromaGBlur!=0.0) {
  1075. filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
  1076. filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
  1077. } else {
  1078. filter->chrH= sws_getIdentityVec();
  1079. filter->chrV= sws_getIdentityVec();
  1080. }
  1081. if (chromaSharpen!=0.0) {
  1082. SwsVector *id= sws_getIdentityVec();
  1083. sws_scaleVec(filter->chrH, -chromaSharpen);
  1084. sws_scaleVec(filter->chrV, -chromaSharpen);
  1085. sws_addVec(filter->chrH, id);
  1086. sws_addVec(filter->chrV, id);
  1087. sws_freeVec(id);
  1088. }
  1089. if (lumaSharpen!=0.0) {
  1090. SwsVector *id= sws_getIdentityVec();
  1091. sws_scaleVec(filter->lumH, -lumaSharpen);
  1092. sws_scaleVec(filter->lumV, -lumaSharpen);
  1093. sws_addVec(filter->lumH, id);
  1094. sws_addVec(filter->lumV, id);
  1095. sws_freeVec(id);
  1096. }
  1097. if (chromaHShift != 0.0)
  1098. sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
  1099. if (chromaVShift != 0.0)
  1100. sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
  1101. sws_normalizeVec(filter->chrH, 1.0);
  1102. sws_normalizeVec(filter->chrV, 1.0);
  1103. sws_normalizeVec(filter->lumH, 1.0);
  1104. sws_normalizeVec(filter->lumV, 1.0);
  1105. if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
  1106. if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
  1107. return filter;
  1108. }
  1109. SwsVector *sws_allocVec(int length)
  1110. {
  1111. SwsVector *vec = av_malloc(sizeof(SwsVector));
  1112. if (!vec)
  1113. return NULL;
  1114. vec->length = length;
  1115. vec->coeff = av_malloc(sizeof(double) * length);
  1116. if (!vec->coeff)
  1117. av_freep(&vec);
  1118. return vec;
  1119. }
  1120. SwsVector *sws_getGaussianVec(double variance, double quality)
  1121. {
  1122. const int length= (int)(variance*quality + 0.5) | 1;
  1123. int i;
  1124. double middle= (length-1)*0.5;
  1125. SwsVector *vec= sws_allocVec(length);
  1126. if (!vec)
  1127. return NULL;
  1128. for (i=0; i<length; i++) {
  1129. double dist= i-middle;
  1130. vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*M_PI);
  1131. }
  1132. sws_normalizeVec(vec, 1.0);
  1133. return vec;
  1134. }
  1135. SwsVector *sws_getConstVec(double c, int length)
  1136. {
  1137. int i;
  1138. SwsVector *vec= sws_allocVec(length);
  1139. if (!vec)
  1140. return NULL;
  1141. for (i=0; i<length; i++)
  1142. vec->coeff[i]= c;
  1143. return vec;
  1144. }
  1145. SwsVector *sws_getIdentityVec(void)
  1146. {
  1147. return sws_getConstVec(1.0, 1);
  1148. }
  1149. static double sws_dcVec(SwsVector *a)
  1150. {
  1151. int i;
  1152. double sum=0;
  1153. for (i=0; i<a->length; i++)
  1154. sum+= a->coeff[i];
  1155. return sum;
  1156. }
  1157. void sws_scaleVec(SwsVector *a, double scalar)
  1158. {
  1159. int i;
  1160. for (i=0; i<a->length; i++)
  1161. a->coeff[i]*= scalar;
  1162. }
  1163. void sws_normalizeVec(SwsVector *a, double height)
  1164. {
  1165. sws_scaleVec(a, height/sws_dcVec(a));
  1166. }
  1167. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
  1168. {
  1169. int length= a->length + b->length - 1;
  1170. int i, j;
  1171. SwsVector *vec= sws_getConstVec(0.0, length);
  1172. if (!vec)
  1173. return NULL;
  1174. for (i=0; i<a->length; i++) {
  1175. for (j=0; j<b->length; j++) {
  1176. vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
  1177. }
  1178. }
  1179. return vec;
  1180. }
  1181. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
  1182. {
  1183. int length= FFMAX(a->length, b->length);
  1184. int i;
  1185. SwsVector *vec= sws_getConstVec(0.0, length);
  1186. if (!vec)
  1187. return NULL;
  1188. for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1189. for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  1190. return vec;
  1191. }
  1192. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
  1193. {
  1194. int length= FFMAX(a->length, b->length);
  1195. int i;
  1196. SwsVector *vec= sws_getConstVec(0.0, length);
  1197. if (!vec)
  1198. return NULL;
  1199. for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1200. for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  1201. return vec;
  1202. }
  1203. /* shift left / or right if "shift" is negative */
  1204. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
  1205. {
  1206. int length= a->length + FFABS(shift)*2;
  1207. int i;
  1208. SwsVector *vec= sws_getConstVec(0.0, length);
  1209. if (!vec)
  1210. return NULL;
  1211. for (i=0; i<a->length; i++) {
  1212. vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  1213. }
  1214. return vec;
  1215. }
  1216. void sws_shiftVec(SwsVector *a, int shift)
  1217. {
  1218. SwsVector *shifted= sws_getShiftedVec(a, shift);
  1219. av_free(a->coeff);
  1220. a->coeff= shifted->coeff;
  1221. a->length= shifted->length;
  1222. av_free(shifted);
  1223. }
  1224. void sws_addVec(SwsVector *a, SwsVector *b)
  1225. {
  1226. SwsVector *sum= sws_sumVec(a, b);
  1227. av_free(a->coeff);
  1228. a->coeff= sum->coeff;
  1229. a->length= sum->length;
  1230. av_free(sum);
  1231. }
  1232. void sws_subVec(SwsVector *a, SwsVector *b)
  1233. {
  1234. SwsVector *diff= sws_diffVec(a, b);
  1235. av_free(a->coeff);
  1236. a->coeff= diff->coeff;
  1237. a->length= diff->length;
  1238. av_free(diff);
  1239. }
  1240. void sws_convVec(SwsVector *a, SwsVector *b)
  1241. {
  1242. SwsVector *conv= sws_getConvVec(a, b);
  1243. av_free(a->coeff);
  1244. a->coeff= conv->coeff;
  1245. a->length= conv->length;
  1246. av_free(conv);
  1247. }
  1248. SwsVector *sws_cloneVec(SwsVector *a)
  1249. {
  1250. int i;
  1251. SwsVector *vec= sws_allocVec(a->length);
  1252. if (!vec)
  1253. return NULL;
  1254. for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
  1255. return vec;
  1256. }
  1257. void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
  1258. {
  1259. int i;
  1260. double max=0;
  1261. double min=0;
  1262. double range;
  1263. for (i=0; i<a->length; i++)
  1264. if (a->coeff[i]>max) max= a->coeff[i];
  1265. for (i=0; i<a->length; i++)
  1266. if (a->coeff[i]<min) min= a->coeff[i];
  1267. range= max - min;
  1268. for (i=0; i<a->length; i++) {
  1269. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  1270. av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
  1271. for (;x>0; x--) av_log(log_ctx, log_level, " ");
  1272. av_log(log_ctx, log_level, "|\n");
  1273. }
  1274. }
  1275. #if LIBSWSCALE_VERSION_MAJOR < 1
  1276. void sws_printVec(SwsVector *a)
  1277. {
  1278. sws_printVec2(a, NULL, AV_LOG_DEBUG);
  1279. }
  1280. #endif
  1281. void sws_freeVec(SwsVector *a)
  1282. {
  1283. if (!a) return;
  1284. av_freep(&a->coeff);
  1285. a->length=0;
  1286. av_free(a);
  1287. }
  1288. void sws_freeFilter(SwsFilter *filter)
  1289. {
  1290. if (!filter) return;
  1291. if (filter->lumH) sws_freeVec(filter->lumH);
  1292. if (filter->lumV) sws_freeVec(filter->lumV);
  1293. if (filter->chrH) sws_freeVec(filter->chrH);
  1294. if (filter->chrV) sws_freeVec(filter->chrV);
  1295. av_free(filter);
  1296. }
  1297. void sws_freeContext(SwsContext *c)
  1298. {
  1299. int i;
  1300. if (!c) return;
  1301. if (c->lumPixBuf) {
  1302. for (i=0; i<c->vLumBufSize; i++)
  1303. av_freep(&c->lumPixBuf[i]);
  1304. av_freep(&c->lumPixBuf);
  1305. }
  1306. if (c->chrPixBuf) {
  1307. for (i=0; i<c->vChrBufSize; i++)
  1308. av_freep(&c->chrPixBuf[i]);
  1309. av_freep(&c->chrPixBuf);
  1310. }
  1311. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  1312. for (i=0; i<c->vLumBufSize; i++)
  1313. av_freep(&c->alpPixBuf[i]);
  1314. av_freep(&c->alpPixBuf);
  1315. }
  1316. av_freep(&c->vLumFilter);
  1317. av_freep(&c->vChrFilter);
  1318. av_freep(&c->hLumFilter);
  1319. av_freep(&c->hChrFilter);
  1320. #if ARCH_PPC && HAVE_ALTIVEC
  1321. av_freep(&c->vYCoeffsBank);
  1322. av_freep(&c->vCCoeffsBank);
  1323. #endif
  1324. av_freep(&c->vLumFilterPos);
  1325. av_freep(&c->vChrFilterPos);
  1326. av_freep(&c->hLumFilterPos);
  1327. av_freep(&c->hChrFilterPos);
  1328. #if ARCH_X86 && CONFIG_GPL
  1329. #ifdef MAP_ANONYMOUS
  1330. if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
  1331. if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
  1332. #elif HAVE_VIRTUALALLOC
  1333. if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, 0, MEM_RELEASE);
  1334. if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, 0, MEM_RELEASE);
  1335. #else
  1336. av_free(c->lumMmx2FilterCode);
  1337. av_free(c->chrMmx2FilterCode);
  1338. #endif
  1339. c->lumMmx2FilterCode=NULL;
  1340. c->chrMmx2FilterCode=NULL;
  1341. #endif /* ARCH_X86 && CONFIG_GPL */
  1342. av_freep(&c->yuvTable);
  1343. av_free(c);
  1344. }
  1345. struct SwsContext *sws_getCachedContext(struct SwsContext *context,
  1346. int srcW, int srcH, enum PixelFormat srcFormat,
  1347. int dstW, int dstH, enum PixelFormat dstFormat, int flags,
  1348. SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  1349. {
  1350. static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
  1351. if (!param)
  1352. param = default_param;
  1353. if (context &&
  1354. (context->srcW != srcW ||
  1355. context->srcH != srcH ||
  1356. context->srcFormat != srcFormat ||
  1357. context->dstW != dstW ||
  1358. context->dstH != dstH ||
  1359. context->dstFormat != dstFormat ||
  1360. context->flags != flags ||
  1361. context->param[0] != param[0] ||
  1362. context->param[1] != param[1])) {
  1363. sws_freeContext(context);
  1364. context = NULL;
  1365. }
  1366. if (!context) {
  1367. return sws_getContext(srcW, srcH, srcFormat,
  1368. dstW, dstH, dstFormat, flags,
  1369. srcFilter, dstFilter, param);
  1370. }
  1371. return context;
  1372. }