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.

1628 lines
59KB

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