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.

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