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.

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