utils.c 54 KB

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