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.

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