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.

1589 lines
58KB

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