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.

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