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.

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