utils.c 61 KB

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