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.

1538 lines
55KB

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