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.

1534 lines
55KB

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