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.

1583 lines
58KB

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