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.

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