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.

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