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.

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