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.

1558 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. const static 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] = { 0 , 1 },
  126. [PIX_FMT_RGB444BE] = { 0 , 1 },
  127. [PIX_FMT_BGR444LE] = { 0 , 1 },
  128. [PIX_FMT_BGR444BE] = { 0 , 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\n");
  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)) c->chrDstHSubSample=1;
  745. // drop some chroma lines if the user wants it
  746. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  747. c->chrSrcVSubSample+= c->vChrDrop;
  748. // drop every other pixel for chroma calculation unless user wants full chroma
  749. if (isAnyRGB(srcFormat) && !(flags&SWS_FULL_CHR_H_INP)
  750. && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
  751. && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
  752. && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
  753. && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&SWS_FAST_BILINEAR)))
  754. c->chrSrcHSubSample=1;
  755. // Note the -((-x)>>y) is so that we always round toward +inf.
  756. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  757. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  758. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  759. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  760. /* unscaled special cases */
  761. if (unscaled && !usesHFilter && !usesVFilter && (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
  762. ff_get_unscaled_swscale(c);
  763. if (c->swScale) {
  764. if (flags&SWS_PRINT_INFO)
  765. av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
  766. av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
  767. return 0;
  768. }
  769. }
  770. c->srcBpc = 1 + av_pix_fmt_descriptors[srcFormat].comp[0].depth_minus1;
  771. if (c->srcBpc < 8)
  772. c->srcBpc = 8;
  773. c->dstBpc = 1 + av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1;
  774. if (c->dstBpc < 8)
  775. c->dstBpc = 8;
  776. if (isAnyRGB(srcFormat) || srcFormat == PIX_FMT_PAL8)
  777. c->srcBpc = 16;
  778. if (c->dstBpc == 16)
  779. dst_stride <<= 1;
  780. FF_ALLOC_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
  781. if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2 && c->srcBpc == 8 && c->dstBpc <= 10) {
  782. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  783. if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
  784. if (flags&SWS_PRINT_INFO)
  785. av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
  786. }
  787. if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat)) c->canMMX2BeUsed=0;
  788. }
  789. else
  790. c->canMMX2BeUsed=0;
  791. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  792. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  793. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  794. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  795. // n-2 is the last chrominance sample available
  796. // this is not perfect, but no one should notice the difference, the more correct variant
  797. // would be like the vertical one, but that would require some special code for the
  798. // first and last pixel
  799. if (flags&SWS_FAST_BILINEAR) {
  800. if (c->canMMX2BeUsed) {
  801. c->lumXInc+= 20;
  802. c->chrXInc+= 20;
  803. }
  804. //we don't use the x86 asm scaler if MMX is available
  805. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX && c->dstBpc <= 10) {
  806. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  807. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  808. }
  809. }
  810. /* precalculate horizontal scaler filter coefficients */
  811. {
  812. #if HAVE_MMX2
  813. // can't downscale !!!
  814. if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
  815. c->lumMmx2FilterCodeSize = initMMX2HScaler( dstW, c->lumXInc, NULL, NULL, NULL, 8);
  816. c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
  817. #ifdef MAP_ANONYMOUS
  818. c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  819. c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  820. #elif HAVE_VIRTUALALLOC
  821. c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  822. c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  823. #else
  824. c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
  825. c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
  826. #endif
  827. #ifdef MAP_ANONYMOUS
  828. if (c->lumMmx2FilterCode == MAP_FAILED || c->chrMmx2FilterCode == MAP_FAILED)
  829. #else
  830. if (!c->lumMmx2FilterCode || !c->chrMmx2FilterCode)
  831. #endif
  832. {
  833. av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
  834. return AVERROR(ENOMEM);
  835. }
  836. FF_ALLOCZ_OR_GOTO(c, c->hLumFilter , (dstW /8+8)*sizeof(int16_t), fail);
  837. FF_ALLOCZ_OR_GOTO(c, c->hChrFilter , (c->chrDstW /4+8)*sizeof(int16_t), fail);
  838. FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW /2/8+8)*sizeof(int32_t), fail);
  839. FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
  840. initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
  841. initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
  842. #ifdef MAP_ANONYMOUS
  843. mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  844. mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  845. #endif
  846. } else
  847. #endif /* HAVE_MMX2 */
  848. {
  849. const int filterAlign=
  850. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 4 :
  851. (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
  852. 1;
  853. if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  854. srcW , dstW, filterAlign, 1<<14,
  855. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags, cpu_flags,
  856. srcFilter->lumH, dstFilter->lumH, c->param) < 0)
  857. goto fail;
  858. if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  859. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  860. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, cpu_flags,
  861. srcFilter->chrH, dstFilter->chrH, c->param) < 0)
  862. goto fail;
  863. }
  864. } // initialize horizontal stuff
  865. /* precalculate vertical scaler filter coefficients */
  866. {
  867. const int filterAlign=
  868. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 2 :
  869. (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
  870. 1;
  871. if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  872. srcH , dstH, filterAlign, (1<<12),
  873. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags, cpu_flags,
  874. srcFilter->lumV, dstFilter->lumV, c->param) < 0)
  875. goto fail;
  876. if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  877. c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
  878. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, cpu_flags,
  879. srcFilter->chrV, dstFilter->chrV, c->param) < 0)
  880. goto fail;
  881. #if HAVE_ALTIVEC
  882. FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
  883. FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
  884. for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
  885. int j;
  886. short *p = (short *)&c->vYCoeffsBank[i];
  887. for (j=0;j<8;j++)
  888. p[j] = c->vLumFilter[i];
  889. }
  890. for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
  891. int j;
  892. short *p = (short *)&c->vCCoeffsBank[i];
  893. for (j=0;j<8;j++)
  894. p[j] = c->vChrFilter[i];
  895. }
  896. #endif
  897. }
  898. // calculate buffer sizes so that they won't run out while handling these damn slices
  899. c->vLumBufSize= c->vLumFilterSize;
  900. c->vChrBufSize= c->vChrFilterSize;
  901. for (i=0; i<dstH; i++) {
  902. int chrI= (int64_t)i*c->chrDstH / dstH;
  903. int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  904. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  905. nextSlice>>= c->chrSrcVSubSample;
  906. nextSlice<<= c->chrSrcVSubSample;
  907. if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  908. c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
  909. if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  910. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  911. }
  912. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  913. // allocate several megabytes to handle all possible cases)
  914. FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
  915. FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
  916. FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
  917. if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
  918. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
  919. //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)
  920. /* align at 16 bytes for AltiVec */
  921. for (i=0; i<c->vLumBufSize; i++) {
  922. FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], dst_stride+16, fail);
  923. c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
  924. }
  925. // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
  926. c->uv_off = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
  927. c->uv_offx2 = dst_stride + 16;
  928. for (i=0; i<c->vChrBufSize; i++) {
  929. FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i+c->vChrBufSize], dst_stride*2+32, fail);
  930. c->chrUPixBuf[i] = c->chrUPixBuf[i+c->vChrBufSize];
  931. c->chrVPixBuf[i] = c->chrVPixBuf[i+c->vChrBufSize] = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
  932. }
  933. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  934. for (i=0; i<c->vLumBufSize; i++) {
  935. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], dst_stride+16, fail);
  936. c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
  937. }
  938. //try to avoid drawing green stuff between the right end and the stride end
  939. for (i=0; i<c->vChrBufSize; i++)
  940. if(av_pix_fmt_descriptors[c->dstFormat].comp[0].depth_minus1 == 15){
  941. av_assert0(c->dstBpc > 10);
  942. for(j=0; j<dst_stride/2+1; j++)
  943. ((int32_t*)(c->chrUPixBuf[i]))[j] = 1<<18;
  944. } else
  945. for(j=0; j<dst_stride+1; j++)
  946. ((int16_t*)(c->chrUPixBuf[i]))[j] = 1<<14;
  947. assert(c->chrDstH <= dstH);
  948. if (flags&SWS_PRINT_INFO) {
  949. if (flags&SWS_FAST_BILINEAR) av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
  950. else if (flags&SWS_BILINEAR) av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
  951. else if (flags&SWS_BICUBIC) av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
  952. else if (flags&SWS_X) av_log(c, AV_LOG_INFO, "Experimental scaler, ");
  953. else if (flags&SWS_POINT) av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
  954. else if (flags&SWS_AREA) av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
  955. else if (flags&SWS_BICUBLIN) av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
  956. else if (flags&SWS_GAUSS) av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
  957. else if (flags&SWS_SINC) av_log(c, AV_LOG_INFO, "Sinc scaler, ");
  958. else if (flags&SWS_LANCZOS) av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
  959. else if (flags&SWS_SPLINE) av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
  960. else av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
  961. av_log(c, AV_LOG_INFO, "from %s to %s%s ",
  962. av_get_pix_fmt_name(srcFormat),
  963. #ifdef DITHER1XBPP
  964. dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ||
  965. dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
  966. dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE ? "dithered " : "",
  967. #else
  968. "",
  969. #endif
  970. av_get_pix_fmt_name(dstFormat));
  971. if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2) av_log(c, AV_LOG_INFO, "using MMX2\n");
  972. else if (HAVE_AMD3DNOW && cpu_flags & AV_CPU_FLAG_3DNOW) av_log(c, AV_LOG_INFO, "using 3DNOW\n");
  973. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) av_log(c, AV_LOG_INFO, "using MMX\n");
  974. else if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) av_log(c, AV_LOG_INFO, "using AltiVec\n");
  975. else av_log(c, AV_LOG_INFO, "using C\n");
  976. av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  977. av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  978. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  979. av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  980. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  981. }
  982. c->swScale= ff_getSwsFunc(c);
  983. return 0;
  984. fail: //FIXME replace things by appropriate error codes
  985. return -1;
  986. }
  987. #if FF_API_SWS_GETCONTEXT
  988. SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
  989. int dstW, int dstH, enum PixelFormat dstFormat, int flags,
  990. SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  991. {
  992. SwsContext *c;
  993. if(!(c=sws_alloc_context()))
  994. return NULL;
  995. c->flags= flags;
  996. c->srcW= srcW;
  997. c->srcH= srcH;
  998. c->dstW= dstW;
  999. c->dstH= dstH;
  1000. c->srcRange = handle_jpeg(&srcFormat);
  1001. c->dstRange = handle_jpeg(&dstFormat);
  1002. c->srcFormat= srcFormat;
  1003. c->dstFormat= dstFormat;
  1004. if (param) {
  1005. c->param[0] = param[0];
  1006. c->param[1] = param[1];
  1007. }
  1008. 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);
  1009. if(sws_init_context(c, srcFilter, dstFilter) < 0){
  1010. sws_freeContext(c);
  1011. return NULL;
  1012. }
  1013. return c;
  1014. }
  1015. #endif
  1016. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  1017. float lumaSharpen, float chromaSharpen,
  1018. float chromaHShift, float chromaVShift,
  1019. int verbose)
  1020. {
  1021. SwsFilter *filter= av_malloc(sizeof(SwsFilter));
  1022. if (!filter)
  1023. return NULL;
  1024. if (lumaGBlur!=0.0) {
  1025. filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
  1026. filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
  1027. } else {
  1028. filter->lumH= sws_getIdentityVec();
  1029. filter->lumV= sws_getIdentityVec();
  1030. }
  1031. if (chromaGBlur!=0.0) {
  1032. filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
  1033. filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
  1034. } else {
  1035. filter->chrH= sws_getIdentityVec();
  1036. filter->chrV= sws_getIdentityVec();
  1037. }
  1038. if (chromaSharpen!=0.0) {
  1039. SwsVector *id= sws_getIdentityVec();
  1040. sws_scaleVec(filter->chrH, -chromaSharpen);
  1041. sws_scaleVec(filter->chrV, -chromaSharpen);
  1042. sws_addVec(filter->chrH, id);
  1043. sws_addVec(filter->chrV, id);
  1044. sws_freeVec(id);
  1045. }
  1046. if (lumaSharpen!=0.0) {
  1047. SwsVector *id= sws_getIdentityVec();
  1048. sws_scaleVec(filter->lumH, -lumaSharpen);
  1049. sws_scaleVec(filter->lumV, -lumaSharpen);
  1050. sws_addVec(filter->lumH, id);
  1051. sws_addVec(filter->lumV, id);
  1052. sws_freeVec(id);
  1053. }
  1054. if (chromaHShift != 0.0)
  1055. sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
  1056. if (chromaVShift != 0.0)
  1057. sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
  1058. sws_normalizeVec(filter->chrH, 1.0);
  1059. sws_normalizeVec(filter->chrV, 1.0);
  1060. sws_normalizeVec(filter->lumH, 1.0);
  1061. sws_normalizeVec(filter->lumV, 1.0);
  1062. if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
  1063. if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
  1064. return filter;
  1065. }
  1066. SwsVector *sws_allocVec(int length)
  1067. {
  1068. SwsVector *vec = av_malloc(sizeof(SwsVector));
  1069. if (!vec)
  1070. return NULL;
  1071. vec->length = length;
  1072. vec->coeff = av_malloc(sizeof(double) * length);
  1073. if (!vec->coeff)
  1074. av_freep(&vec);
  1075. return vec;
  1076. }
  1077. SwsVector *sws_getGaussianVec(double variance, double quality)
  1078. {
  1079. const int length= (int)(variance*quality + 0.5) | 1;
  1080. int i;
  1081. double middle= (length-1)*0.5;
  1082. SwsVector *vec= sws_allocVec(length);
  1083. if (!vec)
  1084. return NULL;
  1085. for (i=0; i<length; i++) {
  1086. double dist= i-middle;
  1087. vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*M_PI);
  1088. }
  1089. sws_normalizeVec(vec, 1.0);
  1090. return vec;
  1091. }
  1092. SwsVector *sws_getConstVec(double c, int length)
  1093. {
  1094. int i;
  1095. SwsVector *vec= sws_allocVec(length);
  1096. if (!vec)
  1097. return NULL;
  1098. for (i=0; i<length; i++)
  1099. vec->coeff[i]= c;
  1100. return vec;
  1101. }
  1102. SwsVector *sws_getIdentityVec(void)
  1103. {
  1104. return sws_getConstVec(1.0, 1);
  1105. }
  1106. static double sws_dcVec(SwsVector *a)
  1107. {
  1108. int i;
  1109. double sum=0;
  1110. for (i=0; i<a->length; i++)
  1111. sum+= a->coeff[i];
  1112. return sum;
  1113. }
  1114. void sws_scaleVec(SwsVector *a, double scalar)
  1115. {
  1116. int i;
  1117. for (i=0; i<a->length; i++)
  1118. a->coeff[i]*= scalar;
  1119. }
  1120. void sws_normalizeVec(SwsVector *a, double height)
  1121. {
  1122. sws_scaleVec(a, height/sws_dcVec(a));
  1123. }
  1124. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
  1125. {
  1126. int length= a->length + b->length - 1;
  1127. int i, j;
  1128. SwsVector *vec= sws_getConstVec(0.0, length);
  1129. if (!vec)
  1130. return NULL;
  1131. for (i=0; i<a->length; i++) {
  1132. for (j=0; j<b->length; j++) {
  1133. vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
  1134. }
  1135. }
  1136. return vec;
  1137. }
  1138. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
  1139. {
  1140. int length= FFMAX(a->length, b->length);
  1141. int i;
  1142. SwsVector *vec= sws_getConstVec(0.0, length);
  1143. if (!vec)
  1144. return NULL;
  1145. for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1146. for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  1147. return vec;
  1148. }
  1149. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
  1150. {
  1151. int length= FFMAX(a->length, b->length);
  1152. int i;
  1153. SwsVector *vec= sws_getConstVec(0.0, length);
  1154. if (!vec)
  1155. return NULL;
  1156. for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1157. for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  1158. return vec;
  1159. }
  1160. /* shift left / or right if "shift" is negative */
  1161. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
  1162. {
  1163. int length= a->length + FFABS(shift)*2;
  1164. int i;
  1165. SwsVector *vec= sws_getConstVec(0.0, length);
  1166. if (!vec)
  1167. return NULL;
  1168. for (i=0; i<a->length; i++) {
  1169. vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  1170. }
  1171. return vec;
  1172. }
  1173. void sws_shiftVec(SwsVector *a, int shift)
  1174. {
  1175. SwsVector *shifted= sws_getShiftedVec(a, shift);
  1176. av_free(a->coeff);
  1177. a->coeff= shifted->coeff;
  1178. a->length= shifted->length;
  1179. av_free(shifted);
  1180. }
  1181. void sws_addVec(SwsVector *a, SwsVector *b)
  1182. {
  1183. SwsVector *sum= sws_sumVec(a, b);
  1184. av_free(a->coeff);
  1185. a->coeff= sum->coeff;
  1186. a->length= sum->length;
  1187. av_free(sum);
  1188. }
  1189. void sws_subVec(SwsVector *a, SwsVector *b)
  1190. {
  1191. SwsVector *diff= sws_diffVec(a, b);
  1192. av_free(a->coeff);
  1193. a->coeff= diff->coeff;
  1194. a->length= diff->length;
  1195. av_free(diff);
  1196. }
  1197. void sws_convVec(SwsVector *a, SwsVector *b)
  1198. {
  1199. SwsVector *conv= sws_getConvVec(a, b);
  1200. av_free(a->coeff);
  1201. a->coeff= conv->coeff;
  1202. a->length= conv->length;
  1203. av_free(conv);
  1204. }
  1205. SwsVector *sws_cloneVec(SwsVector *a)
  1206. {
  1207. int i;
  1208. SwsVector *vec= sws_allocVec(a->length);
  1209. if (!vec)
  1210. return NULL;
  1211. for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
  1212. return vec;
  1213. }
  1214. void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
  1215. {
  1216. int i;
  1217. double max=0;
  1218. double min=0;
  1219. double range;
  1220. for (i=0; i<a->length; i++)
  1221. if (a->coeff[i]>max) max= a->coeff[i];
  1222. for (i=0; i<a->length; i++)
  1223. if (a->coeff[i]<min) min= a->coeff[i];
  1224. range= max - min;
  1225. for (i=0; i<a->length; i++) {
  1226. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  1227. av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
  1228. for (;x>0; x--) av_log(log_ctx, log_level, " ");
  1229. av_log(log_ctx, log_level, "|\n");
  1230. }
  1231. }
  1232. void sws_freeVec(SwsVector *a)
  1233. {
  1234. if (!a) return;
  1235. av_freep(&a->coeff);
  1236. a->length=0;
  1237. av_free(a);
  1238. }
  1239. void sws_freeFilter(SwsFilter *filter)
  1240. {
  1241. if (!filter) return;
  1242. if (filter->lumH) sws_freeVec(filter->lumH);
  1243. if (filter->lumV) sws_freeVec(filter->lumV);
  1244. if (filter->chrH) sws_freeVec(filter->chrH);
  1245. if (filter->chrV) sws_freeVec(filter->chrV);
  1246. av_free(filter);
  1247. }
  1248. void sws_freeContext(SwsContext *c)
  1249. {
  1250. int i;
  1251. if (!c) return;
  1252. if (c->lumPixBuf) {
  1253. for (i=0; i<c->vLumBufSize; i++)
  1254. av_freep(&c->lumPixBuf[i]);
  1255. av_freep(&c->lumPixBuf);
  1256. }
  1257. if (c->chrUPixBuf) {
  1258. for (i=0; i<c->vChrBufSize; i++)
  1259. av_freep(&c->chrUPixBuf[i]);
  1260. av_freep(&c->chrUPixBuf);
  1261. av_freep(&c->chrVPixBuf);
  1262. }
  1263. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  1264. for (i=0; i<c->vLumBufSize; i++)
  1265. av_freep(&c->alpPixBuf[i]);
  1266. av_freep(&c->alpPixBuf);
  1267. }
  1268. av_freep(&c->vLumFilter);
  1269. av_freep(&c->vChrFilter);
  1270. av_freep(&c->hLumFilter);
  1271. av_freep(&c->hChrFilter);
  1272. #if HAVE_ALTIVEC
  1273. av_freep(&c->vYCoeffsBank);
  1274. av_freep(&c->vCCoeffsBank);
  1275. #endif
  1276. av_freep(&c->vLumFilterPos);
  1277. av_freep(&c->vChrFilterPos);
  1278. av_freep(&c->hLumFilterPos);
  1279. av_freep(&c->hChrFilterPos);
  1280. #if HAVE_MMX
  1281. #ifdef MAP_ANONYMOUS
  1282. if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
  1283. if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
  1284. #elif HAVE_VIRTUALALLOC
  1285. if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, 0, MEM_RELEASE);
  1286. if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, 0, MEM_RELEASE);
  1287. #else
  1288. av_free(c->lumMmx2FilterCode);
  1289. av_free(c->chrMmx2FilterCode);
  1290. #endif
  1291. c->lumMmx2FilterCode=NULL;
  1292. c->chrMmx2FilterCode=NULL;
  1293. #endif /* HAVE_MMX */
  1294. av_freep(&c->yuvTable);
  1295. av_freep(&c->formatConvBuffer);
  1296. av_free(c);
  1297. }
  1298. struct SwsContext *sws_getCachedContext(struct SwsContext *context,
  1299. int srcW, int srcH, enum PixelFormat srcFormat,
  1300. int dstW, int dstH, enum PixelFormat dstFormat, int flags,
  1301. SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  1302. {
  1303. static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
  1304. if (!param)
  1305. param = default_param;
  1306. if (context &&
  1307. (context->srcW != srcW ||
  1308. context->srcH != srcH ||
  1309. context->srcFormat != srcFormat ||
  1310. context->dstW != dstW ||
  1311. context->dstH != dstH ||
  1312. context->dstFormat != dstFormat ||
  1313. context->flags != flags ||
  1314. context->param[0] != param[0] ||
  1315. context->param[1] != param[1])) {
  1316. sws_freeContext(context);
  1317. context = NULL;
  1318. }
  1319. if (!context) {
  1320. if (!(context = sws_alloc_context()))
  1321. return NULL;
  1322. context->srcW = srcW;
  1323. context->srcH = srcH;
  1324. context->srcRange = handle_jpeg(&srcFormat);
  1325. context->srcFormat = srcFormat;
  1326. context->dstW = dstW;
  1327. context->dstH = dstH;
  1328. context->dstRange = handle_jpeg(&dstFormat);
  1329. context->dstFormat = dstFormat;
  1330. context->flags = flags;
  1331. context->param[0] = param[0];
  1332. context->param[1] = param[1];
  1333. 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);
  1334. if (sws_init_context(context, srcFilter, dstFilter) < 0) {
  1335. sws_freeContext(context);
  1336. return NULL;
  1337. }
  1338. }
  1339. return context;
  1340. }