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.

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