utils.c 58 KB

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