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.

1619 lines
59KB

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