utils.c 56 KB

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