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.

1597 lines
58KB

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