utils.c 57 KB

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