utils.c 57 KB

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