utils.c 58 KB

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