utils.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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 modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * 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. * the C code (not assembly, mmx, ...) of this file can be used
  21. * under the LGPL license too
  22. */
  23. #define _SVID_SOURCE //needed for MAP_ANONYMOUS
  24. #include <inttypes.h>
  25. #include <string.h>
  26. #include <math.h>
  27. #include <stdio.h>
  28. #include "config.h"
  29. #include <assert.h>
  30. #if HAVE_SYS_MMAN_H
  31. #include <sys/mman.h>
  32. #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  33. #define MAP_ANONYMOUS MAP_ANON
  34. #endif
  35. #endif
  36. #if HAVE_VIRTUALALLOC
  37. #define WIN32_LEAN_AND_MEAN
  38. #include <windows.h>
  39. #endif
  40. #include "swscale.h"
  41. #include "swscale_internal.h"
  42. #include "rgb2rgb.h"
  43. #include "libavutil/intreadwrite.h"
  44. #include "libavutil/x86_cpu.h"
  45. #include "libavutil/avutil.h"
  46. #include "libavutil/bswap.h"
  47. #include "libavutil/pixdesc.h"
  48. unsigned swscale_version(void)
  49. {
  50. return LIBSWSCALE_VERSION_INT;
  51. }
  52. const char *swscale_configuration(void)
  53. {
  54. return FFMPEG_CONFIGURATION;
  55. }
  56. const char *swscale_license(void)
  57. {
  58. #define LICENSE_PREFIX "libswscale license: "
  59. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  60. }
  61. #define RET 0xC3 //near return opcode for x86
  62. #define isSupportedIn(x) ( \
  63. (x)==PIX_FMT_YUV420P \
  64. || (x)==PIX_FMT_YUVA420P \
  65. || (x)==PIX_FMT_YUYV422 \
  66. || (x)==PIX_FMT_UYVY422 \
  67. || (x)==PIX_FMT_RGB48BE \
  68. || (x)==PIX_FMT_RGB48LE \
  69. || (x)==PIX_FMT_RGB32 \
  70. || (x)==PIX_FMT_RGB32_1 \
  71. || (x)==PIX_FMT_BGR24 \
  72. || (x)==PIX_FMT_BGR565 \
  73. || (x)==PIX_FMT_BGR555 \
  74. || (x)==PIX_FMT_BGR32 \
  75. || (x)==PIX_FMT_BGR32_1 \
  76. || (x)==PIX_FMT_RGB24 \
  77. || (x)==PIX_FMT_RGB565 \
  78. || (x)==PIX_FMT_RGB555 \
  79. || (x)==PIX_FMT_GRAY8 \
  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*PI)/(d*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) && CONFIG_GPL
  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) && CONFIG_GPL */
  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 uint16_t roundToInt16(int64_t f)
  594. {
  595. int r= (f + (1<<15))>>16;
  596. if (r<-0x7FFF) return 0x8000;
  597. else if (r> 0x7FFF) return 0x7FFF;
  598. else return r;
  599. }
  600. int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
  601. {
  602. int64_t crv = inv_table[0];
  603. int64_t cbu = inv_table[1];
  604. int64_t cgu = -inv_table[2];
  605. int64_t cgv = -inv_table[3];
  606. int64_t cy = 1<<16;
  607. int64_t oy = 0;
  608. memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
  609. memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
  610. c->brightness= brightness;
  611. c->contrast = contrast;
  612. c->saturation= saturation;
  613. c->srcRange = srcRange;
  614. c->dstRange = dstRange;
  615. if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  616. c->uOffset= 0x0400040004000400LL;
  617. c->vOffset= 0x0400040004000400LL;
  618. if (!srcRange) {
  619. cy= (cy*255) / 219;
  620. oy= 16<<16;
  621. } else {
  622. crv= (crv*224) / 255;
  623. cbu= (cbu*224) / 255;
  624. cgu= (cgu*224) / 255;
  625. cgv= (cgv*224) / 255;
  626. }
  627. cy = (cy *contrast )>>16;
  628. crv= (crv*contrast * saturation)>>32;
  629. cbu= (cbu*contrast * saturation)>>32;
  630. cgu= (cgu*contrast * saturation)>>32;
  631. cgv= (cgv*contrast * saturation)>>32;
  632. oy -= 256*brightness;
  633. c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
  634. c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
  635. c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
  636. c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
  637. c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
  638. c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
  639. c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
  640. c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
  641. c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
  642. c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
  643. c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
  644. c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
  645. ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
  646. //FIXME factorize
  647. #if ARCH_PPC && (HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT)
  648. if (c->flags & SWS_CPU_CAPS_ALTIVEC)
  649. ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
  650. #endif
  651. return 0;
  652. }
  653. int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
  654. {
  655. if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  656. *inv_table = c->srcColorspaceTable;
  657. *table = c->dstColorspaceTable;
  658. *srcRange = c->srcRange;
  659. *dstRange = c->dstRange;
  660. *brightness= c->brightness;
  661. *contrast = c->contrast;
  662. *saturation= c->saturation;
  663. return 0;
  664. }
  665. static int handle_jpeg(enum PixelFormat *format)
  666. {
  667. switch (*format) {
  668. case PIX_FMT_YUVJ420P:
  669. *format = PIX_FMT_YUV420P;
  670. return 1;
  671. case PIX_FMT_YUVJ422P:
  672. *format = PIX_FMT_YUV422P;
  673. return 1;
  674. case PIX_FMT_YUVJ444P:
  675. *format = PIX_FMT_YUV444P;
  676. return 1;
  677. case PIX_FMT_YUVJ440P:
  678. *format = PIX_FMT_YUV440P;
  679. return 1;
  680. default:
  681. return 0;
  682. }
  683. }
  684. SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
  685. int dstW, int dstH, enum PixelFormat dstFormat, int flags,
  686. SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  687. {
  688. SwsContext *c;
  689. int i;
  690. int usesVFilter, usesHFilter;
  691. int unscaled;
  692. int srcRange, dstRange;
  693. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  694. #if ARCH_X86
  695. if (flags & SWS_CPU_CAPS_MMX)
  696. __asm__ volatile("emms\n\t"::: "memory");
  697. #endif
  698. #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
  699. flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
  700. flags |= ff_hardcodedcpuflags();
  701. #endif /* CONFIG_RUNTIME_CPUDETECT */
  702. if (!rgb15to16) sws_rgb2rgb_init(flags);
  703. unscaled = (srcW == dstW && srcH == dstH);
  704. srcRange = handle_jpeg(&srcFormat);
  705. dstRange = handle_jpeg(&dstFormat);
  706. if (!isSupportedIn(srcFormat)) {
  707. av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
  708. return NULL;
  709. }
  710. if (!isSupportedOut(dstFormat)) {
  711. av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
  712. return NULL;
  713. }
  714. i= flags & ( SWS_POINT
  715. |SWS_AREA
  716. |SWS_BILINEAR
  717. |SWS_FAST_BILINEAR
  718. |SWS_BICUBIC
  719. |SWS_X
  720. |SWS_GAUSS
  721. |SWS_LANCZOS
  722. |SWS_SINC
  723. |SWS_SPLINE
  724. |SWS_BICUBLIN);
  725. if(!i || (i & (i-1))) {
  726. av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
  727. return NULL;
  728. }
  729. /* sanity check */
  730. 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
  731. av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  732. srcW, srcH, dstW, dstH);
  733. return NULL;
  734. }
  735. if(srcW > VOFW || dstW > VOFW) {
  736. av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
  737. return NULL;
  738. }
  739. if (!dstFilter) dstFilter= &dummyFilter;
  740. if (!srcFilter) srcFilter= &dummyFilter;
  741. FF_ALLOCZ_OR_GOTO(NULL, c, sizeof(SwsContext), fail);
  742. c->av_class = &sws_context_class;
  743. c->srcW= srcW;
  744. c->srcH= srcH;
  745. c->dstW= dstW;
  746. c->dstH= dstH;
  747. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  748. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  749. c->flags= flags;
  750. c->dstFormat= dstFormat;
  751. c->srcFormat= srcFormat;
  752. c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
  753. c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
  754. c->vRounder= 4* 0x0001000100010001ULL;
  755. usesVFilter = (srcFilter->lumV && srcFilter->lumV->length>1) ||
  756. (srcFilter->chrV && srcFilter->chrV->length>1) ||
  757. (dstFilter->lumV && dstFilter->lumV->length>1) ||
  758. (dstFilter->chrV && dstFilter->chrV->length>1);
  759. usesHFilter = (srcFilter->lumH && srcFilter->lumH->length>1) ||
  760. (srcFilter->chrH && srcFilter->chrH->length>1) ||
  761. (dstFilter->lumH && dstFilter->lumH->length>1) ||
  762. (dstFilter->chrH && dstFilter->chrH->length>1);
  763. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  764. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  765. // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
  766. if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  767. // drop some chroma lines if the user wants it
  768. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  769. c->chrSrcVSubSample+= c->vChrDrop;
  770. // drop every other pixel for chroma calculation unless user wants full chroma
  771. if (isAnyRGB(srcFormat) && !(flags&SWS_FULL_CHR_H_INP)
  772. && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
  773. && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
  774. && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
  775. && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  776. c->chrSrcHSubSample=1;
  777. if (param) {
  778. c->param[0] = param[0];
  779. c->param[1] = param[1];
  780. } else {
  781. c->param[0] =
  782. c->param[1] = SWS_PARAM_DEFAULT;
  783. }
  784. // Note the -((-x)>>y) is so that we always round toward +inf.
  785. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  786. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  787. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  788. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  789. sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
  790. /* unscaled special cases */
  791. if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isAnyRGB(dstFormat))) {
  792. ff_get_unscaled_swscale(c);
  793. if (c->swScale) {
  794. if (flags&SWS_PRINT_INFO)
  795. av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
  796. sws_format_name(srcFormat), sws_format_name(dstFormat));
  797. return c;
  798. }
  799. }
  800. if (flags & SWS_CPU_CAPS_MMX2) {
  801. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  802. if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
  803. if (flags&SWS_PRINT_INFO)
  804. av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
  805. }
  806. if (usesHFilter) c->canMMX2BeUsed=0;
  807. }
  808. else
  809. c->canMMX2BeUsed=0;
  810. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  811. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  812. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  813. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  814. // n-2 is the last chrominance sample available
  815. // this is not perfect, but no one should notice the difference, the more correct variant
  816. // would be like the vertical one, but that would require some special code for the
  817. // first and last pixel
  818. if (flags&SWS_FAST_BILINEAR) {
  819. if (c->canMMX2BeUsed) {
  820. c->lumXInc+= 20;
  821. c->chrXInc+= 20;
  822. }
  823. //we don't use the x86 asm scaler if MMX is available
  824. else if (flags & SWS_CPU_CAPS_MMX) {
  825. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  826. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  827. }
  828. }
  829. /* precalculate horizontal scaler filter coefficients */
  830. {
  831. #if ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
  832. // can't downscale !!!
  833. if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
  834. c->lumMmx2FilterCodeSize = initMMX2HScaler( dstW, c->lumXInc, NULL, NULL, NULL, 8);
  835. c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
  836. #ifdef MAP_ANONYMOUS
  837. c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  838. c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  839. #elif HAVE_VIRTUALALLOC
  840. c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  841. c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  842. #else
  843. c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
  844. c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
  845. #endif
  846. if (!c->lumMmx2FilterCode || !c->chrMmx2FilterCode)
  847. goto fail;
  848. FF_ALLOCZ_OR_GOTO(c, c->hLumFilter , (dstW /8+8)*sizeof(int16_t), fail);
  849. FF_ALLOCZ_OR_GOTO(c, c->hChrFilter , (c->chrDstW /4+8)*sizeof(int16_t), fail);
  850. FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW /2/8+8)*sizeof(int32_t), fail);
  851. FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
  852. initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->hLumFilter, c->hLumFilterPos, 8);
  853. initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->hChrFilter, c->hChrFilterPos, 4);
  854. #ifdef MAP_ANONYMOUS
  855. mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  856. mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  857. #endif
  858. } else
  859. #endif /* ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL */
  860. {
  861. const int filterAlign=
  862. (flags & SWS_CPU_CAPS_MMX) ? 4 :
  863. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  864. 1;
  865. if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  866. srcW , dstW, filterAlign, 1<<14,
  867. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  868. srcFilter->lumH, dstFilter->lumH, c->param) < 0)
  869. goto fail;
  870. if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  871. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  872. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  873. srcFilter->chrH, dstFilter->chrH, c->param) < 0)
  874. goto fail;
  875. }
  876. } // initialize horizontal stuff
  877. /* precalculate vertical scaler filter coefficients */
  878. {
  879. const int filterAlign=
  880. (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
  881. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  882. 1;
  883. if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  884. srcH , dstH, filterAlign, (1<<12),
  885. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  886. srcFilter->lumV, dstFilter->lumV, c->param) < 0)
  887. goto fail;
  888. if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  889. c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
  890. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  891. srcFilter->chrV, dstFilter->chrV, c->param) < 0)
  892. goto fail;
  893. #if ARCH_PPC && (HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT)
  894. FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
  895. FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
  896. for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
  897. int j;
  898. short *p = (short *)&c->vYCoeffsBank[i];
  899. for (j=0;j<8;j++)
  900. p[j] = c->vLumFilter[i];
  901. }
  902. for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
  903. int j;
  904. short *p = (short *)&c->vCCoeffsBank[i];
  905. for (j=0;j<8;j++)
  906. p[j] = c->vChrFilter[i];
  907. }
  908. #endif
  909. }
  910. // calculate buffer sizes so that they won't run out while handling these damn slices
  911. c->vLumBufSize= c->vLumFilterSize;
  912. c->vChrBufSize= c->vChrFilterSize;
  913. for (i=0; i<dstH; i++) {
  914. int chrI= i*c->chrDstH / dstH;
  915. int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  916. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  917. nextSlice>>= c->chrSrcVSubSample;
  918. nextSlice<<= c->chrSrcVSubSample;
  919. if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  920. c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
  921. if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  922. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  923. }
  924. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  925. // allocate several megabytes to handle all possible cases)
  926. FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
  927. FF_ALLOC_OR_GOTO(c, c->chrPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
  928. if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
  929. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
  930. //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)
  931. /* align at 16 bytes for AltiVec */
  932. for (i=0; i<c->vLumBufSize; i++) {
  933. FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], VOF+1, fail);
  934. c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
  935. }
  936. for (i=0; i<c->vChrBufSize; i++) {
  937. FF_ALLOC_OR_GOTO(c, c->chrPixBuf[i+c->vChrBufSize], (VOF+1)*2, fail);
  938. c->chrPixBuf[i] = c->chrPixBuf[i+c->vChrBufSize];
  939. }
  940. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  941. for (i=0; i<c->vLumBufSize; i++) {
  942. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], VOF+1, fail);
  943. c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
  944. }
  945. //try to avoid drawing green stuff between the right end and the stride end
  946. for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
  947. assert(2*VOFW == VOF);
  948. assert(c->chrDstH <= dstH);
  949. if (flags&SWS_PRINT_INFO) {
  950. if (flags&SWS_FAST_BILINEAR)
  951. av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
  952. else if (flags&SWS_BILINEAR)
  953. av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
  954. else if (flags&SWS_BICUBIC)
  955. av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
  956. else if (flags&SWS_X)
  957. av_log(c, AV_LOG_INFO, "Experimental scaler, ");
  958. else if (flags&SWS_POINT)
  959. av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
  960. else if (flags&SWS_AREA)
  961. av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
  962. else if (flags&SWS_BICUBLIN)
  963. av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
  964. else if (flags&SWS_GAUSS)
  965. av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
  966. else if (flags&SWS_SINC)
  967. av_log(c, AV_LOG_INFO, "Sinc scaler, ");
  968. else if (flags&SWS_LANCZOS)
  969. av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
  970. else if (flags&SWS_SPLINE)
  971. av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
  972. else
  973. av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
  974. av_log(c, AV_LOG_INFO, "from %s to %s%s ",
  975. sws_format_name(srcFormat),
  976. #ifdef DITHER1XBPP
  977. dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ? "dithered " : "",
  978. #else
  979. "",
  980. #endif
  981. sws_format_name(dstFormat));
  982. if (flags & SWS_CPU_CAPS_MMX2)
  983. av_log(c, AV_LOG_INFO, "using MMX2\n");
  984. else if (flags & SWS_CPU_CAPS_3DNOW)
  985. av_log(c, AV_LOG_INFO, "using 3DNOW\n");
  986. else if (flags & SWS_CPU_CAPS_MMX)
  987. av_log(c, AV_LOG_INFO, "using MMX\n");
  988. else if (flags & SWS_CPU_CAPS_ALTIVEC)
  989. av_log(c, AV_LOG_INFO, "using AltiVec\n");
  990. else
  991. av_log(c, AV_LOG_INFO, "using C\n");
  992. if (flags & SWS_CPU_CAPS_MMX) {
  993. if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  994. av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  995. else {
  996. if (c->hLumFilterSize==4)
  997. av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
  998. else if (c->hLumFilterSize==8)
  999. av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
  1000. else
  1001. av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
  1002. if (c->hChrFilterSize==4)
  1003. av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
  1004. else if (c->hChrFilterSize==8)
  1005. av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
  1006. else
  1007. av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
  1008. }
  1009. } else {
  1010. #if ARCH_X86
  1011. av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
  1012. #else
  1013. if (flags & SWS_FAST_BILINEAR)
  1014. av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
  1015. else
  1016. av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
  1017. #endif
  1018. }
  1019. if (isPlanarYUV(dstFormat)) {
  1020. if (c->vLumFilterSize==1)
  1021. av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1022. else
  1023. av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1024. } else {
  1025. if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
  1026. av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  1027. " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1028. else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
  1029. av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1030. else
  1031. av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1032. }
  1033. if (dstFormat==PIX_FMT_BGR24)
  1034. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
  1035. (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
  1036. else if (dstFormat==PIX_FMT_RGB32)
  1037. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1038. else if (dstFormat==PIX_FMT_BGR565)
  1039. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1040. else if (dstFormat==PIX_FMT_BGR555)
  1041. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1042. av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  1043. av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1044. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  1045. av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1046. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  1047. }
  1048. c->swScale= ff_getSwsFunc(c);
  1049. return c;
  1050. fail:
  1051. sws_freeContext(c);
  1052. return NULL;
  1053. }
  1054. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  1055. float lumaSharpen, float chromaSharpen,
  1056. float chromaHShift, float chromaVShift,
  1057. int verbose)
  1058. {
  1059. SwsFilter *filter= av_malloc(sizeof(SwsFilter));
  1060. if (!filter)
  1061. return NULL;
  1062. if (lumaGBlur!=0.0) {
  1063. filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
  1064. filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
  1065. } else {
  1066. filter->lumH= sws_getIdentityVec();
  1067. filter->lumV= sws_getIdentityVec();
  1068. }
  1069. if (chromaGBlur!=0.0) {
  1070. filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
  1071. filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
  1072. } else {
  1073. filter->chrH= sws_getIdentityVec();
  1074. filter->chrV= sws_getIdentityVec();
  1075. }
  1076. if (chromaSharpen!=0.0) {
  1077. SwsVector *id= sws_getIdentityVec();
  1078. sws_scaleVec(filter->chrH, -chromaSharpen);
  1079. sws_scaleVec(filter->chrV, -chromaSharpen);
  1080. sws_addVec(filter->chrH, id);
  1081. sws_addVec(filter->chrV, id);
  1082. sws_freeVec(id);
  1083. }
  1084. if (lumaSharpen!=0.0) {
  1085. SwsVector *id= sws_getIdentityVec();
  1086. sws_scaleVec(filter->lumH, -lumaSharpen);
  1087. sws_scaleVec(filter->lumV, -lumaSharpen);
  1088. sws_addVec(filter->lumH, id);
  1089. sws_addVec(filter->lumV, id);
  1090. sws_freeVec(id);
  1091. }
  1092. if (chromaHShift != 0.0)
  1093. sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
  1094. if (chromaVShift != 0.0)
  1095. sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
  1096. sws_normalizeVec(filter->chrH, 1.0);
  1097. sws_normalizeVec(filter->chrV, 1.0);
  1098. sws_normalizeVec(filter->lumH, 1.0);
  1099. sws_normalizeVec(filter->lumV, 1.0);
  1100. if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
  1101. if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
  1102. return filter;
  1103. }
  1104. SwsVector *sws_allocVec(int length)
  1105. {
  1106. SwsVector *vec = av_malloc(sizeof(SwsVector));
  1107. if (!vec)
  1108. return NULL;
  1109. vec->length = length;
  1110. vec->coeff = av_malloc(sizeof(double) * length);
  1111. if (!vec->coeff)
  1112. av_freep(&vec);
  1113. return vec;
  1114. }
  1115. SwsVector *sws_getGaussianVec(double variance, double quality)
  1116. {
  1117. const int length= (int)(variance*quality + 0.5) | 1;
  1118. int i;
  1119. double middle= (length-1)*0.5;
  1120. SwsVector *vec= sws_allocVec(length);
  1121. if (!vec)
  1122. return NULL;
  1123. for (i=0; i<length; i++) {
  1124. double dist= i-middle;
  1125. vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*M_PI);
  1126. }
  1127. sws_normalizeVec(vec, 1.0);
  1128. return vec;
  1129. }
  1130. SwsVector *sws_getConstVec(double c, int length)
  1131. {
  1132. int i;
  1133. SwsVector *vec= sws_allocVec(length);
  1134. if (!vec)
  1135. return NULL;
  1136. for (i=0; i<length; i++)
  1137. vec->coeff[i]= c;
  1138. return vec;
  1139. }
  1140. SwsVector *sws_getIdentityVec(void)
  1141. {
  1142. return sws_getConstVec(1.0, 1);
  1143. }
  1144. static double sws_dcVec(SwsVector *a)
  1145. {
  1146. int i;
  1147. double sum=0;
  1148. for (i=0; i<a->length; i++)
  1149. sum+= a->coeff[i];
  1150. return sum;
  1151. }
  1152. void sws_scaleVec(SwsVector *a, double scalar)
  1153. {
  1154. int i;
  1155. for (i=0; i<a->length; i++)
  1156. a->coeff[i]*= scalar;
  1157. }
  1158. void sws_normalizeVec(SwsVector *a, double height)
  1159. {
  1160. sws_scaleVec(a, height/sws_dcVec(a));
  1161. }
  1162. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
  1163. {
  1164. int length= a->length + b->length - 1;
  1165. int i, j;
  1166. SwsVector *vec= sws_getConstVec(0.0, length);
  1167. if (!vec)
  1168. return NULL;
  1169. for (i=0; i<a->length; i++) {
  1170. for (j=0; j<b->length; j++) {
  1171. vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
  1172. }
  1173. }
  1174. return vec;
  1175. }
  1176. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
  1177. {
  1178. int length= FFMAX(a->length, b->length);
  1179. int i;
  1180. SwsVector *vec= sws_getConstVec(0.0, length);
  1181. if (!vec)
  1182. return NULL;
  1183. for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1184. for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  1185. return vec;
  1186. }
  1187. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
  1188. {
  1189. int length= FFMAX(a->length, b->length);
  1190. int i;
  1191. SwsVector *vec= sws_getConstVec(0.0, length);
  1192. if (!vec)
  1193. return NULL;
  1194. for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1195. for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  1196. return vec;
  1197. }
  1198. /* shift left / or right if "shift" is negative */
  1199. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
  1200. {
  1201. int length= a->length + FFABS(shift)*2;
  1202. int i;
  1203. SwsVector *vec= sws_getConstVec(0.0, length);
  1204. if (!vec)
  1205. return NULL;
  1206. for (i=0; i<a->length; i++) {
  1207. vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  1208. }
  1209. return vec;
  1210. }
  1211. void sws_shiftVec(SwsVector *a, int shift)
  1212. {
  1213. SwsVector *shifted= sws_getShiftedVec(a, shift);
  1214. av_free(a->coeff);
  1215. a->coeff= shifted->coeff;
  1216. a->length= shifted->length;
  1217. av_free(shifted);
  1218. }
  1219. void sws_addVec(SwsVector *a, SwsVector *b)
  1220. {
  1221. SwsVector *sum= sws_sumVec(a, b);
  1222. av_free(a->coeff);
  1223. a->coeff= sum->coeff;
  1224. a->length= sum->length;
  1225. av_free(sum);
  1226. }
  1227. void sws_subVec(SwsVector *a, SwsVector *b)
  1228. {
  1229. SwsVector *diff= sws_diffVec(a, b);
  1230. av_free(a->coeff);
  1231. a->coeff= diff->coeff;
  1232. a->length= diff->length;
  1233. av_free(diff);
  1234. }
  1235. void sws_convVec(SwsVector *a, SwsVector *b)
  1236. {
  1237. SwsVector *conv= sws_getConvVec(a, b);
  1238. av_free(a->coeff);
  1239. a->coeff= conv->coeff;
  1240. a->length= conv->length;
  1241. av_free(conv);
  1242. }
  1243. SwsVector *sws_cloneVec(SwsVector *a)
  1244. {
  1245. int i;
  1246. SwsVector *vec= sws_allocVec(a->length);
  1247. if (!vec)
  1248. return NULL;
  1249. for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
  1250. return vec;
  1251. }
  1252. void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
  1253. {
  1254. int i;
  1255. double max=0;
  1256. double min=0;
  1257. double range;
  1258. for (i=0; i<a->length; i++)
  1259. if (a->coeff[i]>max) max= a->coeff[i];
  1260. for (i=0; i<a->length; i++)
  1261. if (a->coeff[i]<min) min= a->coeff[i];
  1262. range= max - min;
  1263. for (i=0; i<a->length; i++) {
  1264. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  1265. av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
  1266. for (;x>0; x--) av_log(log_ctx, log_level, " ");
  1267. av_log(log_ctx, log_level, "|\n");
  1268. }
  1269. }
  1270. #if LIBSWSCALE_VERSION_MAJOR < 1
  1271. void sws_printVec(SwsVector *a)
  1272. {
  1273. sws_printVec2(a, NULL, AV_LOG_DEBUG);
  1274. }
  1275. #endif
  1276. void sws_freeVec(SwsVector *a)
  1277. {
  1278. if (!a) return;
  1279. av_freep(&a->coeff);
  1280. a->length=0;
  1281. av_free(a);
  1282. }
  1283. void sws_freeFilter(SwsFilter *filter)
  1284. {
  1285. if (!filter) return;
  1286. if (filter->lumH) sws_freeVec(filter->lumH);
  1287. if (filter->lumV) sws_freeVec(filter->lumV);
  1288. if (filter->chrH) sws_freeVec(filter->chrH);
  1289. if (filter->chrV) sws_freeVec(filter->chrV);
  1290. av_free(filter);
  1291. }
  1292. void sws_freeContext(SwsContext *c)
  1293. {
  1294. int i;
  1295. if (!c) return;
  1296. if (c->lumPixBuf) {
  1297. for (i=0; i<c->vLumBufSize; i++)
  1298. av_freep(&c->lumPixBuf[i]);
  1299. av_freep(&c->lumPixBuf);
  1300. }
  1301. if (c->chrPixBuf) {
  1302. for (i=0; i<c->vChrBufSize; i++)
  1303. av_freep(&c->chrPixBuf[i]);
  1304. av_freep(&c->chrPixBuf);
  1305. }
  1306. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  1307. for (i=0; i<c->vLumBufSize; i++)
  1308. av_freep(&c->alpPixBuf[i]);
  1309. av_freep(&c->alpPixBuf);
  1310. }
  1311. av_freep(&c->vLumFilter);
  1312. av_freep(&c->vChrFilter);
  1313. av_freep(&c->hLumFilter);
  1314. av_freep(&c->hChrFilter);
  1315. #if ARCH_PPC && (HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT)
  1316. av_freep(&c->vYCoeffsBank);
  1317. av_freep(&c->vCCoeffsBank);
  1318. #endif
  1319. av_freep(&c->vLumFilterPos);
  1320. av_freep(&c->vChrFilterPos);
  1321. av_freep(&c->hLumFilterPos);
  1322. av_freep(&c->hChrFilterPos);
  1323. #if ARCH_X86 && CONFIG_GPL
  1324. #ifdef MAP_ANONYMOUS
  1325. if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
  1326. if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
  1327. #elif HAVE_VIRTUALALLOC
  1328. if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, 0, MEM_RELEASE);
  1329. if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, 0, MEM_RELEASE);
  1330. #else
  1331. av_free(c->lumMmx2FilterCode);
  1332. av_free(c->chrMmx2FilterCode);
  1333. #endif
  1334. c->lumMmx2FilterCode=NULL;
  1335. c->chrMmx2FilterCode=NULL;
  1336. #endif /* ARCH_X86 && CONFIG_GPL */
  1337. av_freep(&c->yuvTable);
  1338. av_free(c);
  1339. }
  1340. struct SwsContext *sws_getCachedContext(struct SwsContext *context,
  1341. int srcW, int srcH, enum PixelFormat srcFormat,
  1342. int dstW, int dstH, enum PixelFormat dstFormat, int flags,
  1343. SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  1344. {
  1345. static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
  1346. if (!param)
  1347. param = default_param;
  1348. if (context &&
  1349. (context->srcW != srcW ||
  1350. context->srcH != srcH ||
  1351. context->srcFormat != srcFormat ||
  1352. context->dstW != dstW ||
  1353. context->dstH != dstH ||
  1354. context->dstFormat != dstFormat ||
  1355. context->flags != flags ||
  1356. context->param[0] != param[0] ||
  1357. context->param[1] != param[1])) {
  1358. sws_freeContext(context);
  1359. context = NULL;
  1360. }
  1361. if (!context) {
  1362. return sws_getContext(srcW, srcH, srcFormat,
  1363. dstW, dstH, dstFormat, flags,
  1364. srcFilter, dstFilter, param);
  1365. }
  1366. return context;
  1367. }