utils.c 55 KB

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