utils.c 57 KB

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