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.

1577 lines
57KB

  1. /*
  2. * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #define _SVID_SOURCE //needed for MAP_ANONYMOUS
  21. #define _DARWIN_C_SOURCE // needed for MAP_ANON
  22. #include <inttypes.h>
  23. #include <string.h>
  24. #include <math.h>
  25. #include <stdio.h>
  26. #include "config.h"
  27. #include <assert.h>
  28. #if HAVE_SYS_MMAN_H
  29. #include <sys/mman.h>
  30. #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  31. #define MAP_ANONYMOUS MAP_ANON
  32. #endif
  33. #endif
  34. #if HAVE_VIRTUALALLOC
  35. #define WIN32_LEAN_AND_MEAN
  36. #include <windows.h>
  37. #endif
  38. #include "swscale.h"
  39. #include "swscale_internal.h"
  40. #include "rgb2rgb.h"
  41. #include "libavutil/intreadwrite.h"
  42. #include "libavutil/x86_cpu.h"
  43. #include "libavutil/cpu.h"
  44. #include "libavutil/avutil.h"
  45. #include "libavutil/bswap.h"
  46. #include "libavutil/opt.h"
  47. #include "libavutil/pixdesc.h"
  48. unsigned swscale_version(void)
  49. {
  50. return LIBSWSCALE_VERSION_INT;
  51. }
  52. const char *swscale_configuration(void)
  53. {
  54. return FFMPEG_CONFIGURATION;
  55. }
  56. const char *swscale_license(void)
  57. {
  58. #define LICENSE_PREFIX "libswscale license: "
  59. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  60. }
  61. #define RET 0xC3 //near return opcode for x86
  62. #define isSupportedIn(x) ( \
  63. (x)==PIX_FMT_YUV420P \
  64. || (x)==PIX_FMT_YUVA420P \
  65. || (x)==PIX_FMT_YUYV422 \
  66. || (x)==PIX_FMT_UYVY422 \
  67. || (x)==PIX_FMT_RGB48BE \
  68. || (x)==PIX_FMT_RGB48LE \
  69. || (x)==PIX_FMT_RGB32 \
  70. || (x)==PIX_FMT_RGB32_1 \
  71. || (x)==PIX_FMT_BGR48BE \
  72. || (x)==PIX_FMT_BGR48LE \
  73. || (x)==PIX_FMT_BGR24 \
  74. || (x)==PIX_FMT_BGR565 \
  75. || (x)==PIX_FMT_BGR555 \
  76. || (x)==PIX_FMT_BGR32 \
  77. || (x)==PIX_FMT_BGR32_1 \
  78. || (x)==PIX_FMT_RGB24 \
  79. || (x)==PIX_FMT_RGB565 \
  80. || (x)==PIX_FMT_RGB555 \
  81. || (x)==PIX_FMT_GRAY8 \
  82. || (x)==PIX_FMT_GRAY8A \
  83. || (x)==PIX_FMT_YUV410P \
  84. || (x)==PIX_FMT_YUV440P \
  85. || (x)==PIX_FMT_NV12 \
  86. || (x)==PIX_FMT_NV21 \
  87. || (x)==PIX_FMT_GRAY16BE \
  88. || (x)==PIX_FMT_GRAY16LE \
  89. || (x)==PIX_FMT_YUV444P \
  90. || (x)==PIX_FMT_YUV422P \
  91. || (x)==PIX_FMT_YUV411P \
  92. || (x)==PIX_FMT_YUVJ420P \
  93. || (x)==PIX_FMT_YUVJ422P \
  94. || (x)==PIX_FMT_YUVJ440P \
  95. || (x)==PIX_FMT_YUVJ444P \
  96. || (x)==PIX_FMT_PAL8 \
  97. || (x)==PIX_FMT_BGR8 \
  98. || (x)==PIX_FMT_RGB8 \
  99. || (x)==PIX_FMT_BGR4_BYTE \
  100. || (x)==PIX_FMT_RGB4_BYTE \
  101. || (x)==PIX_FMT_YUV440P \
  102. || (x)==PIX_FMT_MONOWHITE \
  103. || (x)==PIX_FMT_MONOBLACK \
  104. || (x)==PIX_FMT_YUV420P9LE \
  105. || (x)==PIX_FMT_YUV420P10LE \
  106. || (x)==PIX_FMT_YUV420P16LE \
  107. || (x)==PIX_FMT_YUV422P16LE \
  108. || (x)==PIX_FMT_YUV444P16LE \
  109. || (x)==PIX_FMT_YUV420P9BE \
  110. || (x)==PIX_FMT_YUV420P10BE \
  111. || (x)==PIX_FMT_YUV420P16BE \
  112. || (x)==PIX_FMT_YUV422P16BE \
  113. || (x)==PIX_FMT_YUV444P16BE \
  114. || (x)==PIX_FMT_YUV422P10 \
  115. )
  116. int sws_isSupportedInput(enum PixelFormat pix_fmt)
  117. {
  118. return isSupportedIn(pix_fmt);
  119. }
  120. #define isSupportedOut(x) ( \
  121. (x)==PIX_FMT_YUV420P \
  122. || (x)==PIX_FMT_YUVA420P \
  123. || (x)==PIX_FMT_YUYV422 \
  124. || (x)==PIX_FMT_UYVY422 \
  125. || (x)==PIX_FMT_YUV444P \
  126. || (x)==PIX_FMT_YUV422P \
  127. || (x)==PIX_FMT_YUV411P \
  128. || (x)==PIX_FMT_YUVJ420P \
  129. || (x)==PIX_FMT_YUVJ422P \
  130. || (x)==PIX_FMT_YUVJ440P \
  131. || (x)==PIX_FMT_YUVJ444P \
  132. || isAnyRGB(x) \
  133. || (x)==PIX_FMT_NV12 \
  134. || (x)==PIX_FMT_NV21 \
  135. || (x)==PIX_FMT_GRAY16BE \
  136. || (x)==PIX_FMT_GRAY16LE \
  137. || (x)==PIX_FMT_GRAY8 \
  138. || (x)==PIX_FMT_YUV410P \
  139. || (x)==PIX_FMT_YUV440P \
  140. || (x)==PIX_FMT_YUV422P10 \
  141. || (x)==PIX_FMT_YUV420P9LE \
  142. || (x)==PIX_FMT_YUV420P10LE \
  143. || (x)==PIX_FMT_YUV420P16LE \
  144. || (x)==PIX_FMT_YUV422P16LE \
  145. || (x)==PIX_FMT_YUV444P16LE \
  146. || (x)==PIX_FMT_YUV420P9BE \
  147. || (x)==PIX_FMT_YUV420P10BE \
  148. || (x)==PIX_FMT_YUV420P16BE \
  149. || (x)==PIX_FMT_YUV422P16BE \
  150. || (x)==PIX_FMT_YUV444P16BE \
  151. )
  152. int sws_isSupportedOutput(enum PixelFormat pix_fmt)
  153. {
  154. return isSupportedOut(pix_fmt);
  155. }
  156. extern const int32_t ff_yuv2rgb_coeffs[8][4];
  157. const char *sws_format_name(enum PixelFormat format)
  158. {
  159. if ((unsigned)format < PIX_FMT_NB && av_pix_fmt_descriptors[format].name)
  160. return av_pix_fmt_descriptors[format].name;
  161. else
  162. return "Unknown format";
  163. }
  164. static double getSplineCoeff(double a, double b, double c, double d, double dist)
  165. {
  166. // printf("%f %f %f %f %f\n", a,b,c,d,dist);
  167. if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
  168. else return getSplineCoeff( 0.0,
  169. b+ 2.0*c + 3.0*d,
  170. c + 3.0*d,
  171. -b- 3.0*c - 6.0*d,
  172. dist-1.0);
  173. }
  174. static int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  175. int srcW, int dstW, int filterAlign, int one, int flags, int cpu_flags,
  176. SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
  177. {
  178. int i;
  179. int filterSize;
  180. int filter2Size;
  181. int minFilterSize;
  182. int64_t *filter=NULL;
  183. int64_t *filter2=NULL;
  184. const int64_t fone= 1LL<<54;
  185. int ret= -1;
  186. emms_c(); //FIXME this should not be required but it IS (even for non-MMX versions)
  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 (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_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 (HAVE_MMX && cpu_flags & AV_CPU_FLAG_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 HAVE_MMX2
  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 /* HAVE_MMX2 */
  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. int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
  604. {
  605. memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
  606. memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
  607. c->brightness= brightness;
  608. c->contrast = contrast;
  609. c->saturation= saturation;
  610. c->srcRange = srcRange;
  611. c->dstRange = dstRange;
  612. if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  613. c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->dstFormat]);
  614. c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->srcFormat]);
  615. ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
  616. //FIXME factorize
  617. if (HAVE_ALTIVEC && av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)
  618. ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
  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. SwsContext *sws_alloc_context(void)
  644. {
  645. SwsContext *c= av_mallocz(sizeof(SwsContext));
  646. c->av_class = &sws_context_class;
  647. av_opt_set_defaults(c);
  648. return c;
  649. }
  650. int sws_init_context(SwsContext *c, SwsFilter *srcFilter, SwsFilter *dstFilter)
  651. {
  652. int i;
  653. int usesVFilter, usesHFilter;
  654. int unscaled;
  655. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  656. int srcW= c->srcW;
  657. int srcH= c->srcH;
  658. int dstW= c->dstW;
  659. int dstH= c->dstH;
  660. int flags, cpu_flags;
  661. enum PixelFormat srcFormat= c->srcFormat;
  662. enum PixelFormat dstFormat= c->dstFormat;
  663. cpu_flags = av_get_cpu_flags();
  664. flags = c->flags;
  665. emms_c();
  666. if (!rgb15to16) sws_rgb2rgb_init();
  667. unscaled = (srcW == dstW && srcH == dstH);
  668. if (!isSupportedIn(srcFormat)) {
  669. av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
  670. return AVERROR(EINVAL);
  671. }
  672. if (!isSupportedOut(dstFormat)) {
  673. av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
  674. return AVERROR(EINVAL);
  675. }
  676. i= flags & ( SWS_POINT
  677. |SWS_AREA
  678. |SWS_BILINEAR
  679. |SWS_FAST_BILINEAR
  680. |SWS_BICUBIC
  681. |SWS_X
  682. |SWS_GAUSS
  683. |SWS_LANCZOS
  684. |SWS_SINC
  685. |SWS_SPLINE
  686. |SWS_BICUBLIN);
  687. if(!i || (i & (i-1))) {
  688. av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
  689. return AVERROR(EINVAL);
  690. }
  691. /* sanity check */
  692. 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
  693. av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  694. srcW, srcH, dstW, dstH);
  695. return AVERROR(EINVAL);
  696. }
  697. if(srcW > VOFW || dstW > VOFW) {
  698. av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
  699. return AVERROR(EINVAL);
  700. }
  701. if (!dstFilter) dstFilter= &dummyFilter;
  702. if (!srcFilter) srcFilter= &dummyFilter;
  703. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  704. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  705. c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
  706. c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
  707. c->vRounder= 4* 0x0001000100010001ULL;
  708. usesVFilter = (srcFilter->lumV && srcFilter->lumV->length>1) ||
  709. (srcFilter->chrV && srcFilter->chrV->length>1) ||
  710. (dstFilter->lumV && dstFilter->lumV->length>1) ||
  711. (dstFilter->chrV && dstFilter->chrV->length>1);
  712. usesHFilter = (srcFilter->lumH && srcFilter->lumH->length>1) ||
  713. (srcFilter->chrH && srcFilter->chrH->length>1) ||
  714. (dstFilter->lumH && dstFilter->lumH->length>1) ||
  715. (dstFilter->chrH && dstFilter->chrH->length>1);
  716. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  717. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  718. // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
  719. if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  720. // drop some chroma lines if the user wants it
  721. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  722. c->chrSrcVSubSample+= c->vChrDrop;
  723. // drop every other pixel for chroma calculation unless user wants full chroma
  724. if (isAnyRGB(srcFormat) && !(flags&SWS_FULL_CHR_H_INP)
  725. && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
  726. && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
  727. && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
  728. && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&SWS_FAST_BILINEAR)))
  729. c->chrSrcHSubSample=1;
  730. // Note the -((-x)>>y) is so that we always round toward +inf.
  731. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  732. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  733. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  734. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  735. /* unscaled special cases */
  736. if (unscaled && !usesHFilter && !usesVFilter && (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
  737. ff_get_unscaled_swscale(c);
  738. if (c->swScale) {
  739. if (flags&SWS_PRINT_INFO)
  740. av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
  741. sws_format_name(srcFormat), sws_format_name(dstFormat));
  742. return 0;
  743. }
  744. }
  745. if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2) {
  746. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  747. if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
  748. if (flags&SWS_PRINT_INFO)
  749. av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
  750. }
  751. if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat)) c->canMMX2BeUsed=0;
  752. }
  753. else
  754. c->canMMX2BeUsed=0;
  755. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  756. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  757. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  758. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  759. // n-2 is the last chrominance sample available
  760. // this is not perfect, but no one should notice the difference, the more correct variant
  761. // would be like the vertical one, but that would require some special code for the
  762. // first and last pixel
  763. if (flags&SWS_FAST_BILINEAR) {
  764. if (c->canMMX2BeUsed) {
  765. c->lumXInc+= 20;
  766. c->chrXInc+= 20;
  767. }
  768. //we don't use the x86 asm scaler if MMX is available
  769. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
  770. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  771. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  772. }
  773. }
  774. /* precalculate horizontal scaler filter coefficients */
  775. {
  776. #if HAVE_MMX2
  777. // can't downscale !!!
  778. if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
  779. c->lumMmx2FilterCodeSize = initMMX2HScaler( dstW, c->lumXInc, NULL, NULL, NULL, 8);
  780. c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
  781. #ifdef MAP_ANONYMOUS
  782. c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  783. c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  784. #elif HAVE_VIRTUALALLOC
  785. c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  786. c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  787. #else
  788. c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
  789. c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
  790. #endif
  791. #ifdef MAP_ANONYMOUS
  792. if (c->lumMmx2FilterCode == MAP_FAILED || c->chrMmx2FilterCode == MAP_FAILED)
  793. #else
  794. if (!c->lumMmx2FilterCode || !c->chrMmx2FilterCode)
  795. #endif
  796. return AVERROR(ENOMEM);
  797. FF_ALLOCZ_OR_GOTO(c, c->hLumFilter , (dstW /8+8)*sizeof(int16_t), fail);
  798. FF_ALLOCZ_OR_GOTO(c, c->hChrFilter , (c->chrDstW /4+8)*sizeof(int16_t), fail);
  799. FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW /2/8+8)*sizeof(int32_t), fail);
  800. FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
  801. initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->hLumFilter, c->hLumFilterPos, 8);
  802. initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->hChrFilter, c->hChrFilterPos, 4);
  803. #ifdef MAP_ANONYMOUS
  804. mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  805. mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  806. #endif
  807. } else
  808. #endif /* HAVE_MMX2 */
  809. {
  810. const int filterAlign=
  811. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 4 :
  812. (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
  813. 1;
  814. if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  815. srcW , dstW, filterAlign, 1<<14,
  816. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags, cpu_flags,
  817. srcFilter->lumH, dstFilter->lumH, c->param) < 0)
  818. goto fail;
  819. if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  820. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  821. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, cpu_flags,
  822. srcFilter->chrH, dstFilter->chrH, c->param) < 0)
  823. goto fail;
  824. }
  825. } // initialize horizontal stuff
  826. /* precalculate vertical scaler filter coefficients */
  827. {
  828. const int filterAlign=
  829. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
  830. (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
  831. 1;
  832. if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  833. srcH , dstH, filterAlign, (1<<12),
  834. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags, cpu_flags,
  835. srcFilter->lumV, dstFilter->lumV, c->param) < 0)
  836. goto fail;
  837. if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  838. c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
  839. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, cpu_flags,
  840. srcFilter->chrV, dstFilter->chrV, c->param) < 0)
  841. goto fail;
  842. #if HAVE_ALTIVEC
  843. FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
  844. FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
  845. for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
  846. int j;
  847. short *p = (short *)&c->vYCoeffsBank[i];
  848. for (j=0;j<8;j++)
  849. p[j] = c->vLumFilter[i];
  850. }
  851. for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
  852. int j;
  853. short *p = (short *)&c->vCCoeffsBank[i];
  854. for (j=0;j<8;j++)
  855. p[j] = c->vChrFilter[i];
  856. }
  857. #endif
  858. }
  859. // calculate buffer sizes so that they won't run out while handling these damn slices
  860. c->vLumBufSize= c->vLumFilterSize;
  861. c->vChrBufSize= c->vChrFilterSize;
  862. for (i=0; i<dstH; i++) {
  863. int chrI= (int64_t)i*c->chrDstH / dstH;
  864. int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  865. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  866. nextSlice>>= c->chrSrcVSubSample;
  867. nextSlice<<= c->chrSrcVSubSample;
  868. if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  869. c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
  870. if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  871. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  872. }
  873. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  874. // allocate several megabytes to handle all possible cases)
  875. FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
  876. FF_ALLOC_OR_GOTO(c, c->chrPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
  877. if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
  878. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
  879. //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)
  880. /* align at 16 bytes for AltiVec */
  881. for (i=0; i<c->vLumBufSize; i++) {
  882. FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], VOF+1, fail);
  883. c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
  884. }
  885. for (i=0; i<c->vChrBufSize; i++) {
  886. FF_ALLOC_OR_GOTO(c, c->chrPixBuf[i+c->vChrBufSize], (VOF+1)*2, fail);
  887. c->chrPixBuf[i] = c->chrPixBuf[i+c->vChrBufSize];
  888. }
  889. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  890. for (i=0; i<c->vLumBufSize; i++) {
  891. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], VOF+1, fail);
  892. c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
  893. }
  894. //try to avoid drawing green stuff between the right end and the stride end
  895. for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
  896. assert(2*VOFW == VOF);
  897. assert(c->chrDstH <= dstH);
  898. if (flags&SWS_PRINT_INFO) {
  899. if (flags&SWS_FAST_BILINEAR) av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
  900. else if (flags&SWS_BILINEAR) av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
  901. else if (flags&SWS_BICUBIC) av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
  902. else if (flags&SWS_X) av_log(c, AV_LOG_INFO, "Experimental scaler, ");
  903. else if (flags&SWS_POINT) av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
  904. else if (flags&SWS_AREA) av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
  905. else if (flags&SWS_BICUBLIN) av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
  906. else if (flags&SWS_GAUSS) av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
  907. else if (flags&SWS_SINC) av_log(c, AV_LOG_INFO, "Sinc scaler, ");
  908. else if (flags&SWS_LANCZOS) av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
  909. else if (flags&SWS_SPLINE) av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
  910. else av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
  911. av_log(c, AV_LOG_INFO, "from %s to %s%s ",
  912. sws_format_name(srcFormat),
  913. #ifdef DITHER1XBPP
  914. dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ||
  915. dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
  916. dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE ? "dithered " : "",
  917. #else
  918. "",
  919. #endif
  920. sws_format_name(dstFormat));
  921. if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2) av_log(c, AV_LOG_INFO, "using MMX2\n");
  922. else if (HAVE_AMD3DNOW && cpu_flags & AV_CPU_FLAG_3DNOW) av_log(c, AV_LOG_INFO, "using 3DNOW\n");
  923. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) av_log(c, AV_LOG_INFO, "using MMX\n");
  924. else if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) av_log(c, AV_LOG_INFO, "using AltiVec\n");
  925. else av_log(c, AV_LOG_INFO, "using C\n");
  926. if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
  927. if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  928. av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  929. else {
  930. if (c->hLumFilterSize==4)
  931. av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
  932. else if (c->hLumFilterSize==8)
  933. av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
  934. else
  935. av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
  936. if (c->hChrFilterSize==4)
  937. av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
  938. else if (c->hChrFilterSize==8)
  939. av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
  940. else
  941. av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
  942. }
  943. } else {
  944. #if HAVE_MMX
  945. av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
  946. #else
  947. if (flags & SWS_FAST_BILINEAR)
  948. av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
  949. else
  950. av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
  951. #endif
  952. }
  953. if (isPlanarYUV(dstFormat)) {
  954. if (c->vLumFilterSize==1)
  955. av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n",
  956. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? "MMX" : "C");
  957. else
  958. av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n",
  959. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? "MMX" : "C");
  960. } else {
  961. if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
  962. av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  963. " 2-tap scaler for vertical chrominance scaling (BGR)\n",
  964. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? "MMX" : "C");
  965. else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
  966. av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n",
  967. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? "MMX" : "C");
  968. else
  969. av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n",
  970. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? "MMX" : "C");
  971. }
  972. if (dstFormat==PIX_FMT_BGR24)
  973. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
  974. (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2) ? "MMX2" :
  975. ((HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? "MMX" : "C"));
  976. else if (dstFormat==PIX_FMT_RGB32)
  977. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n",
  978. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? "MMX" : "C");
  979. else if (dstFormat==PIX_FMT_BGR565)
  980. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n",
  981. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? "MMX" : "C");
  982. else if (dstFormat==PIX_FMT_BGR555)
  983. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n",
  984. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? "MMX" : "C");
  985. else if (dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
  986. dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE)
  987. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR12 converter\n",
  988. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? "MMX" : "C");
  989. av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  990. av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  991. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  992. av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  993. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  994. }
  995. c->swScale= ff_getSwsFunc(c);
  996. return 0;
  997. fail: //FIXME replace things by appropriate error codes
  998. return -1;
  999. }
  1000. #if FF_API_SWS_GETCONTEXT
  1001. SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
  1002. int dstW, int dstH, enum PixelFormat dstFormat, int flags,
  1003. SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  1004. {
  1005. SwsContext *c;
  1006. if(!(c=sws_alloc_context()))
  1007. return NULL;
  1008. c->flags= flags;
  1009. c->srcW= srcW;
  1010. c->srcH= srcH;
  1011. c->dstW= dstW;
  1012. c->dstH= dstH;
  1013. c->srcRange = handle_jpeg(&srcFormat);
  1014. c->dstRange = handle_jpeg(&dstFormat);
  1015. c->srcFormat= srcFormat;
  1016. c->dstFormat= dstFormat;
  1017. if (param) {
  1018. c->param[0] = param[0];
  1019. c->param[1] = param[1];
  1020. }
  1021. 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);
  1022. if(sws_init_context(c, srcFilter, dstFilter) < 0){
  1023. sws_freeContext(c);
  1024. return NULL;
  1025. }
  1026. return c;
  1027. }
  1028. #endif
  1029. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  1030. float lumaSharpen, float chromaSharpen,
  1031. float chromaHShift, float chromaVShift,
  1032. int verbose)
  1033. {
  1034. SwsFilter *filter= av_malloc(sizeof(SwsFilter));
  1035. if (!filter)
  1036. return NULL;
  1037. if (lumaGBlur!=0.0) {
  1038. filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
  1039. filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
  1040. } else {
  1041. filter->lumH= sws_getIdentityVec();
  1042. filter->lumV= sws_getIdentityVec();
  1043. }
  1044. if (chromaGBlur!=0.0) {
  1045. filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
  1046. filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
  1047. } else {
  1048. filter->chrH= sws_getIdentityVec();
  1049. filter->chrV= sws_getIdentityVec();
  1050. }
  1051. if (chromaSharpen!=0.0) {
  1052. SwsVector *id= sws_getIdentityVec();
  1053. sws_scaleVec(filter->chrH, -chromaSharpen);
  1054. sws_scaleVec(filter->chrV, -chromaSharpen);
  1055. sws_addVec(filter->chrH, id);
  1056. sws_addVec(filter->chrV, id);
  1057. sws_freeVec(id);
  1058. }
  1059. if (lumaSharpen!=0.0) {
  1060. SwsVector *id= sws_getIdentityVec();
  1061. sws_scaleVec(filter->lumH, -lumaSharpen);
  1062. sws_scaleVec(filter->lumV, -lumaSharpen);
  1063. sws_addVec(filter->lumH, id);
  1064. sws_addVec(filter->lumV, id);
  1065. sws_freeVec(id);
  1066. }
  1067. if (chromaHShift != 0.0)
  1068. sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
  1069. if (chromaVShift != 0.0)
  1070. sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
  1071. sws_normalizeVec(filter->chrH, 1.0);
  1072. sws_normalizeVec(filter->chrV, 1.0);
  1073. sws_normalizeVec(filter->lumH, 1.0);
  1074. sws_normalizeVec(filter->lumV, 1.0);
  1075. if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
  1076. if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
  1077. return filter;
  1078. }
  1079. SwsVector *sws_allocVec(int length)
  1080. {
  1081. SwsVector *vec = av_malloc(sizeof(SwsVector));
  1082. if (!vec)
  1083. return NULL;
  1084. vec->length = length;
  1085. vec->coeff = av_malloc(sizeof(double) * length);
  1086. if (!vec->coeff)
  1087. av_freep(&vec);
  1088. return vec;
  1089. }
  1090. SwsVector *sws_getGaussianVec(double variance, double quality)
  1091. {
  1092. const int length= (int)(variance*quality + 0.5) | 1;
  1093. int i;
  1094. double middle= (length-1)*0.5;
  1095. SwsVector *vec= sws_allocVec(length);
  1096. if (!vec)
  1097. return NULL;
  1098. for (i=0; i<length; i++) {
  1099. double dist= i-middle;
  1100. vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*M_PI);
  1101. }
  1102. sws_normalizeVec(vec, 1.0);
  1103. return vec;
  1104. }
  1105. SwsVector *sws_getConstVec(double c, int length)
  1106. {
  1107. int i;
  1108. SwsVector *vec= sws_allocVec(length);
  1109. if (!vec)
  1110. return NULL;
  1111. for (i=0; i<length; i++)
  1112. vec->coeff[i]= c;
  1113. return vec;
  1114. }
  1115. SwsVector *sws_getIdentityVec(void)
  1116. {
  1117. return sws_getConstVec(1.0, 1);
  1118. }
  1119. static double sws_dcVec(SwsVector *a)
  1120. {
  1121. int i;
  1122. double sum=0;
  1123. for (i=0; i<a->length; i++)
  1124. sum+= a->coeff[i];
  1125. return sum;
  1126. }
  1127. void sws_scaleVec(SwsVector *a, double scalar)
  1128. {
  1129. int i;
  1130. for (i=0; i<a->length; i++)
  1131. a->coeff[i]*= scalar;
  1132. }
  1133. void sws_normalizeVec(SwsVector *a, double height)
  1134. {
  1135. sws_scaleVec(a, height/sws_dcVec(a));
  1136. }
  1137. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
  1138. {
  1139. int length= a->length + b->length - 1;
  1140. int i, j;
  1141. SwsVector *vec= sws_getConstVec(0.0, length);
  1142. if (!vec)
  1143. return NULL;
  1144. for (i=0; i<a->length; i++) {
  1145. for (j=0; j<b->length; j++) {
  1146. vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
  1147. }
  1148. }
  1149. return vec;
  1150. }
  1151. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
  1152. {
  1153. int length= FFMAX(a->length, b->length);
  1154. int i;
  1155. SwsVector *vec= sws_getConstVec(0.0, length);
  1156. if (!vec)
  1157. return NULL;
  1158. for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1159. for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  1160. return vec;
  1161. }
  1162. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
  1163. {
  1164. int length= FFMAX(a->length, b->length);
  1165. int i;
  1166. SwsVector *vec= sws_getConstVec(0.0, length);
  1167. if (!vec)
  1168. return NULL;
  1169. for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1170. for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  1171. return vec;
  1172. }
  1173. /* shift left / or right if "shift" is negative */
  1174. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
  1175. {
  1176. int length= a->length + FFABS(shift)*2;
  1177. int i;
  1178. SwsVector *vec= sws_getConstVec(0.0, length);
  1179. if (!vec)
  1180. return NULL;
  1181. for (i=0; i<a->length; i++) {
  1182. vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  1183. }
  1184. return vec;
  1185. }
  1186. void sws_shiftVec(SwsVector *a, int shift)
  1187. {
  1188. SwsVector *shifted= sws_getShiftedVec(a, shift);
  1189. av_free(a->coeff);
  1190. a->coeff= shifted->coeff;
  1191. a->length= shifted->length;
  1192. av_free(shifted);
  1193. }
  1194. void sws_addVec(SwsVector *a, SwsVector *b)
  1195. {
  1196. SwsVector *sum= sws_sumVec(a, b);
  1197. av_free(a->coeff);
  1198. a->coeff= sum->coeff;
  1199. a->length= sum->length;
  1200. av_free(sum);
  1201. }
  1202. void sws_subVec(SwsVector *a, SwsVector *b)
  1203. {
  1204. SwsVector *diff= sws_diffVec(a, b);
  1205. av_free(a->coeff);
  1206. a->coeff= diff->coeff;
  1207. a->length= diff->length;
  1208. av_free(diff);
  1209. }
  1210. void sws_convVec(SwsVector *a, SwsVector *b)
  1211. {
  1212. SwsVector *conv= sws_getConvVec(a, b);
  1213. av_free(a->coeff);
  1214. a->coeff= conv->coeff;
  1215. a->length= conv->length;
  1216. av_free(conv);
  1217. }
  1218. SwsVector *sws_cloneVec(SwsVector *a)
  1219. {
  1220. int i;
  1221. SwsVector *vec= sws_allocVec(a->length);
  1222. if (!vec)
  1223. return NULL;
  1224. for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
  1225. return vec;
  1226. }
  1227. void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
  1228. {
  1229. int i;
  1230. double max=0;
  1231. double min=0;
  1232. double range;
  1233. for (i=0; i<a->length; i++)
  1234. if (a->coeff[i]>max) max= a->coeff[i];
  1235. for (i=0; i<a->length; i++)
  1236. if (a->coeff[i]<min) min= a->coeff[i];
  1237. range= max - min;
  1238. for (i=0; i<a->length; i++) {
  1239. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  1240. av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
  1241. for (;x>0; x--) av_log(log_ctx, log_level, " ");
  1242. av_log(log_ctx, log_level, "|\n");
  1243. }
  1244. }
  1245. #if LIBSWSCALE_VERSION_MAJOR < 1
  1246. void sws_printVec(SwsVector *a)
  1247. {
  1248. sws_printVec2(a, NULL, AV_LOG_DEBUG);
  1249. }
  1250. #endif
  1251. void sws_freeVec(SwsVector *a)
  1252. {
  1253. if (!a) return;
  1254. av_freep(&a->coeff);
  1255. a->length=0;
  1256. av_free(a);
  1257. }
  1258. void sws_freeFilter(SwsFilter *filter)
  1259. {
  1260. if (!filter) return;
  1261. if (filter->lumH) sws_freeVec(filter->lumH);
  1262. if (filter->lumV) sws_freeVec(filter->lumV);
  1263. if (filter->chrH) sws_freeVec(filter->chrH);
  1264. if (filter->chrV) sws_freeVec(filter->chrV);
  1265. av_free(filter);
  1266. }
  1267. void sws_freeContext(SwsContext *c)
  1268. {
  1269. int i;
  1270. if (!c) return;
  1271. if (c->lumPixBuf) {
  1272. for (i=0; i<c->vLumBufSize; i++)
  1273. av_freep(&c->lumPixBuf[i]);
  1274. av_freep(&c->lumPixBuf);
  1275. }
  1276. if (c->chrPixBuf) {
  1277. for (i=0; i<c->vChrBufSize; i++)
  1278. av_freep(&c->chrPixBuf[i]);
  1279. av_freep(&c->chrPixBuf);
  1280. }
  1281. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  1282. for (i=0; i<c->vLumBufSize; i++)
  1283. av_freep(&c->alpPixBuf[i]);
  1284. av_freep(&c->alpPixBuf);
  1285. }
  1286. av_freep(&c->vLumFilter);
  1287. av_freep(&c->vChrFilter);
  1288. av_freep(&c->hLumFilter);
  1289. av_freep(&c->hChrFilter);
  1290. #if HAVE_ALTIVEC
  1291. av_freep(&c->vYCoeffsBank);
  1292. av_freep(&c->vCCoeffsBank);
  1293. #endif
  1294. av_freep(&c->vLumFilterPos);
  1295. av_freep(&c->vChrFilterPos);
  1296. av_freep(&c->hLumFilterPos);
  1297. av_freep(&c->hChrFilterPos);
  1298. #if HAVE_MMX
  1299. #ifdef MAP_ANONYMOUS
  1300. if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
  1301. if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
  1302. #elif HAVE_VIRTUALALLOC
  1303. if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, 0, MEM_RELEASE);
  1304. if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, 0, MEM_RELEASE);
  1305. #else
  1306. av_free(c->lumMmx2FilterCode);
  1307. av_free(c->chrMmx2FilterCode);
  1308. #endif
  1309. c->lumMmx2FilterCode=NULL;
  1310. c->chrMmx2FilterCode=NULL;
  1311. #endif /* HAVE_MMX */
  1312. av_freep(&c->yuvTable);
  1313. av_free(c);
  1314. }
  1315. struct SwsContext *sws_getCachedContext(struct SwsContext *context,
  1316. int srcW, int srcH, enum PixelFormat srcFormat,
  1317. int dstW, int dstH, enum PixelFormat dstFormat, int flags,
  1318. SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  1319. {
  1320. static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
  1321. if (!param)
  1322. param = default_param;
  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. }