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.

1603 lines
58KB

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