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.

1581 lines
56KB

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