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.

1579 lines
57KB

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