You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1542 lines
55KB

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