utils.c 55 KB

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