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
56KB

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