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.

1591 lines
57KB

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