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.

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