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.

1561 lines
57KB

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