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.

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