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.

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