utils.c 62 KB

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