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.

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