utils.c 54 KB

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