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.

1536 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. static const 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] = { 1 , 1 },
  117. [PIX_FMT_RGB444BE] = { 1 , 1 },
  118. [PIX_FMT_BGR444LE] = { 1 , 1 },
  119. [PIX_FMT_BGR444BE] = { 1 , 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], int is_horizontal)
  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. filterSize = av_clip(filterSize, 1, 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. if (is_horizontal) {
  410. for (i = 0; i < dstW; i++) {
  411. int j;
  412. if ((*filterPos)[i] < 0) {
  413. // move filter coefficients left to compensate for filterPos
  414. for (j = 1; j < filterSize; j++) {
  415. int left = FFMAX(j + (*filterPos)[i], 0);
  416. filter[i * filterSize + left] += filter[i * filterSize + j];
  417. filter[i * filterSize + j ] = 0;
  418. }
  419. (*filterPos)[i] = 0;
  420. }
  421. if ((*filterPos)[i] + filterSize > srcW) {
  422. int shift = (*filterPos)[i] + filterSize - srcW;
  423. // move filter coefficients right to compensate for filterPos
  424. for (j = filterSize - 2; j >= 0; j--) {
  425. int right = FFMIN(j + shift, filterSize - 1);
  426. filter[i * filterSize + right] += filter[i * filterSize + j];
  427. filter[i * filterSize + j ] = 0;
  428. }
  429. (*filterPos)[i] = srcW - filterSize;
  430. }
  431. }
  432. }
  433. // Note the +1 is for the MMX scaler which reads over the end
  434. /* align at 16 for AltiVec (needed by hScale_altivec_real) */
  435. FF_ALLOCZ_OR_GOTO(NULL, *outFilter, *outFilterSize*(dstW+3)*sizeof(int16_t), fail);
  436. /* normalize & store in outFilter */
  437. for (i=0; i<dstW; i++) {
  438. int j;
  439. int64_t error=0;
  440. int64_t sum=0;
  441. for (j=0; j<filterSize; j++) {
  442. sum+= filter[i*filterSize + j];
  443. }
  444. sum= (sum + one/2)/ one;
  445. for (j=0; j<*outFilterSize; j++) {
  446. int64_t v= filter[i*filterSize + j] + error;
  447. int intV= ROUNDED_DIV(v, sum);
  448. (*outFilter)[i*(*outFilterSize) + j]= intV;
  449. error= v - intV*sum;
  450. }
  451. }
  452. (*filterPos)[dstW+0] =
  453. (*filterPos)[dstW+1] =
  454. (*filterPos)[dstW+2] = (*filterPos)[dstW-1]; // the MMX/SSE scaler will read over the end
  455. for (i=0; i<*outFilterSize; i++) {
  456. int k= (dstW - 1) * (*outFilterSize) + i;
  457. (*outFilter)[k + 1 * (*outFilterSize)] =
  458. (*outFilter)[k + 2 * (*outFilterSize)] =
  459. (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
  460. }
  461. ret=0;
  462. fail:
  463. av_free(filter);
  464. av_free(filter2);
  465. return ret;
  466. }
  467. #if HAVE_MMX2
  468. static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
  469. {
  470. uint8_t *fragmentA;
  471. x86_reg imm8OfPShufW1A;
  472. x86_reg imm8OfPShufW2A;
  473. x86_reg fragmentLengthA;
  474. uint8_t *fragmentB;
  475. x86_reg imm8OfPShufW1B;
  476. x86_reg imm8OfPShufW2B;
  477. x86_reg fragmentLengthB;
  478. int fragmentPos;
  479. int xpos, i;
  480. // create an optimized horizontal scaling routine
  481. /* This scaler is made of runtime-generated MMX2 code using specially
  482. * tuned pshufw instructions. For every four output pixels, if four
  483. * input pixels are enough for the fast bilinear scaling, then a chunk
  484. * of fragmentB is used. If five input pixels are needed, then a chunk
  485. * of fragmentA is used.
  486. */
  487. //code fragment
  488. __asm__ volatile(
  489. "jmp 9f \n\t"
  490. // Begin
  491. "0: \n\t"
  492. "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
  493. "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
  494. "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
  495. "punpcklbw %%mm7, %%mm1 \n\t"
  496. "punpcklbw %%mm7, %%mm0 \n\t"
  497. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  498. "1: \n\t"
  499. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  500. "2: \n\t"
  501. "psubw %%mm1, %%mm0 \n\t"
  502. "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
  503. "pmullw %%mm3, %%mm0 \n\t"
  504. "psllw $7, %%mm1 \n\t"
  505. "paddw %%mm1, %%mm0 \n\t"
  506. "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
  507. "add $8, %%"REG_a" \n\t"
  508. // End
  509. "9: \n\t"
  510. // "int $3 \n\t"
  511. "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
  512. "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
  513. "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
  514. "dec %1 \n\t"
  515. "dec %2 \n\t"
  516. "sub %0, %1 \n\t"
  517. "sub %0, %2 \n\t"
  518. "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
  519. "sub %0, %3 \n\t"
  520. :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  521. "=r" (fragmentLengthA)
  522. );
  523. __asm__ volatile(
  524. "jmp 9f \n\t"
  525. // Begin
  526. "0: \n\t"
  527. "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
  528. "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
  529. "punpcklbw %%mm7, %%mm0 \n\t"
  530. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  531. "1: \n\t"
  532. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  533. "2: \n\t"
  534. "psubw %%mm1, %%mm0 \n\t"
  535. "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
  536. "pmullw %%mm3, %%mm0 \n\t"
  537. "psllw $7, %%mm1 \n\t"
  538. "paddw %%mm1, %%mm0 \n\t"
  539. "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
  540. "add $8, %%"REG_a" \n\t"
  541. // End
  542. "9: \n\t"
  543. // "int $3 \n\t"
  544. "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
  545. "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
  546. "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
  547. "dec %1 \n\t"
  548. "dec %2 \n\t"
  549. "sub %0, %1 \n\t"
  550. "sub %0, %2 \n\t"
  551. "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
  552. "sub %0, %3 \n\t"
  553. :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  554. "=r" (fragmentLengthB)
  555. );
  556. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  557. fragmentPos=0;
  558. for (i=0; i<dstW/numSplits; i++) {
  559. int xx=xpos>>16;
  560. if ((i&3) == 0) {
  561. int a=0;
  562. int b=((xpos+xInc)>>16) - xx;
  563. int c=((xpos+xInc*2)>>16) - xx;
  564. int d=((xpos+xInc*3)>>16) - xx;
  565. int inc = (d+1<4);
  566. uint8_t *fragment = (d+1<4) ? fragmentB : fragmentA;
  567. x86_reg imm8OfPShufW1 = (d+1<4) ? imm8OfPShufW1B : imm8OfPShufW1A;
  568. x86_reg imm8OfPShufW2 = (d+1<4) ? imm8OfPShufW2B : imm8OfPShufW2A;
  569. x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
  570. int maxShift= 3-(d+inc);
  571. int shift=0;
  572. if (filterCode) {
  573. filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
  574. filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
  575. filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
  576. filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
  577. filterPos[i/2]= xx;
  578. memcpy(filterCode + fragmentPos, fragment, fragmentLength);
  579. filterCode[fragmentPos + imm8OfPShufW1]=
  580. (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
  581. filterCode[fragmentPos + imm8OfPShufW2]=
  582. a | (b<<2) | (c<<4) | (d<<6);
  583. if (i+4-inc>=dstW) shift=maxShift; //avoid overread
  584. else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
  585. if (shift && i>=shift) {
  586. filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
  587. filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
  588. filterPos[i/2]-=shift;
  589. }
  590. }
  591. fragmentPos+= fragmentLength;
  592. if (filterCode)
  593. filterCode[fragmentPos]= RET;
  594. }
  595. xpos+=xInc;
  596. }
  597. if (filterCode)
  598. filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
  599. return fragmentPos + 1;
  600. }
  601. #endif /* HAVE_MMX2 */
  602. static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
  603. {
  604. *h = av_pix_fmt_descriptors[format].log2_chroma_w;
  605. *v = av_pix_fmt_descriptors[format].log2_chroma_h;
  606. }
  607. int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
  608. int srcRange, const int table[4], int dstRange,
  609. int brightness, int contrast, int saturation)
  610. {
  611. memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
  612. memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
  613. c->brightness= brightness;
  614. c->contrast = contrast;
  615. c->saturation= saturation;
  616. c->srcRange = srcRange;
  617. c->dstRange = dstRange;
  618. if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  619. c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->dstFormat]);
  620. c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->srcFormat]);
  621. ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
  622. //FIXME factorize
  623. if (HAVE_ALTIVEC && av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)
  624. ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
  625. return 0;
  626. }
  627. int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
  628. int *srcRange, int **table, int *dstRange,
  629. int *brightness, int *contrast, int *saturation)
  630. {
  631. if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  632. *inv_table = c->srcColorspaceTable;
  633. *table = c->dstColorspaceTable;
  634. *srcRange = c->srcRange;
  635. *dstRange = c->dstRange;
  636. *brightness= c->brightness;
  637. *contrast = c->contrast;
  638. *saturation= c->saturation;
  639. return 0;
  640. }
  641. static int handle_jpeg(enum PixelFormat *format)
  642. {
  643. switch (*format) {
  644. case PIX_FMT_YUVJ420P: *format = PIX_FMT_YUV420P; return 1;
  645. case PIX_FMT_YUVJ422P: *format = PIX_FMT_YUV422P; return 1;
  646. case PIX_FMT_YUVJ444P: *format = PIX_FMT_YUV444P; return 1;
  647. case PIX_FMT_YUVJ440P: *format = PIX_FMT_YUV440P; return 1;
  648. default: return 0;
  649. }
  650. }
  651. SwsContext *sws_alloc_context(void)
  652. {
  653. SwsContext *c= av_mallocz(sizeof(SwsContext));
  654. c->av_class = &sws_context_class;
  655. av_opt_set_defaults(c);
  656. return c;
  657. }
  658. int sws_init_context(SwsContext *c, SwsFilter *srcFilter, SwsFilter *dstFilter)
  659. {
  660. int i;
  661. int usesVFilter, usesHFilter;
  662. int unscaled;
  663. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  664. int srcW= c->srcW;
  665. int srcH= c->srcH;
  666. int dstW= c->dstW;
  667. int dstH= c->dstH;
  668. int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 16, 16), dst_stride_px = dst_stride >> 1;
  669. int flags, cpu_flags;
  670. enum PixelFormat srcFormat= c->srcFormat;
  671. enum PixelFormat dstFormat= c->dstFormat;
  672. cpu_flags = av_get_cpu_flags();
  673. flags = c->flags;
  674. emms_c();
  675. if (!rgb15to16) sws_rgb2rgb_init();
  676. unscaled = (srcW == dstW && srcH == dstH);
  677. if (!sws_isSupportedInput(srcFormat)) {
  678. av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n", sws_format_name(srcFormat));
  679. return AVERROR(EINVAL);
  680. }
  681. if (!sws_isSupportedOutput(dstFormat)) {
  682. av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n", sws_format_name(dstFormat));
  683. return AVERROR(EINVAL);
  684. }
  685. i= flags & ( SWS_POINT
  686. |SWS_AREA
  687. |SWS_BILINEAR
  688. |SWS_FAST_BILINEAR
  689. |SWS_BICUBIC
  690. |SWS_X
  691. |SWS_GAUSS
  692. |SWS_LANCZOS
  693. |SWS_SINC
  694. |SWS_SPLINE
  695. |SWS_BICUBLIN);
  696. if(!i || (i & (i-1))) {
  697. av_log(c, AV_LOG_ERROR, "Exactly one scaler algorithm must be chosen\n");
  698. return AVERROR(EINVAL);
  699. }
  700. /* sanity check */
  701. 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
  702. av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
  703. srcW, srcH, dstW, dstH);
  704. return AVERROR(EINVAL);
  705. }
  706. if (!dstFilter) dstFilter= &dummyFilter;
  707. if (!srcFilter) srcFilter= &dummyFilter;
  708. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  709. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  710. c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
  711. c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
  712. c->vRounder= 4* 0x0001000100010001ULL;
  713. usesVFilter = (srcFilter->lumV && srcFilter->lumV->length>1) ||
  714. (srcFilter->chrV && srcFilter->chrV->length>1) ||
  715. (dstFilter->lumV && dstFilter->lumV->length>1) ||
  716. (dstFilter->chrV && dstFilter->chrV->length>1);
  717. usesHFilter = (srcFilter->lumH && srcFilter->lumH->length>1) ||
  718. (srcFilter->chrH && srcFilter->chrH->length>1) ||
  719. (dstFilter->lumH && dstFilter->lumH->length>1) ||
  720. (dstFilter->chrH && dstFilter->chrH->length>1);
  721. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  722. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  723. // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
  724. if (flags & SWS_FULL_CHR_H_INT &&
  725. isAnyRGB(dstFormat) &&
  726. dstFormat != PIX_FMT_RGBA &&
  727. dstFormat != PIX_FMT_ARGB &&
  728. dstFormat != PIX_FMT_BGRA &&
  729. dstFormat != PIX_FMT_ABGR &&
  730. dstFormat != PIX_FMT_RGB24 &&
  731. dstFormat != PIX_FMT_BGR24) {
  732. av_log(c, AV_LOG_ERROR,
  733. "full chroma interpolation for destination format '%s' not yet implemented\n",
  734. sws_format_name(dstFormat));
  735. flags &= ~SWS_FULL_CHR_H_INT;
  736. c->flags = flags;
  737. }
  738. if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  739. // drop some chroma lines if the user wants it
  740. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  741. c->chrSrcVSubSample+= c->vChrDrop;
  742. // drop every other pixel for chroma calculation unless user wants full chroma
  743. if (isAnyRGB(srcFormat) && !(flags&SWS_FULL_CHR_H_INP)
  744. && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
  745. && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
  746. && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
  747. && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&SWS_FAST_BILINEAR)))
  748. c->chrSrcHSubSample=1;
  749. // Note the -((-x)>>y) is so that we always round toward +inf.
  750. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  751. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  752. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  753. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  754. /* unscaled special cases */
  755. if (unscaled && !usesHFilter && !usesVFilter && (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
  756. ff_get_unscaled_swscale(c);
  757. if (c->swScale) {
  758. if (flags&SWS_PRINT_INFO)
  759. av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
  760. sws_format_name(srcFormat), sws_format_name(dstFormat));
  761. return 0;
  762. }
  763. }
  764. c->srcBpc = 1 + av_pix_fmt_descriptors[srcFormat].comp[0].depth_minus1;
  765. if (c->srcBpc < 8)
  766. c->srcBpc = 8;
  767. c->dstBpc = 1 + av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1;
  768. if (c->dstBpc < 8)
  769. c->dstBpc = 8;
  770. if (c->dstBpc == 16)
  771. dst_stride <<= 1;
  772. FF_ALLOC_OR_GOTO(c, c->formatConvBuffer,
  773. (FFALIGN(srcW, 16) * 2 * FFALIGN(c->srcBpc, 8) >> 3) + 16,
  774. fail);
  775. if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2 && c->srcBpc == 8 && c->dstBpc <= 10) {
  776. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  777. if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
  778. if (flags&SWS_PRINT_INFO)
  779. av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
  780. }
  781. if (usesHFilter) c->canMMX2BeUsed=0;
  782. }
  783. else
  784. c->canMMX2BeUsed=0;
  785. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  786. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  787. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  788. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  789. // n-2 is the last chrominance sample available
  790. // this is not perfect, but no one should notice the difference, the more correct variant
  791. // would be like the vertical one, but that would require some special code for the
  792. // first and last pixel
  793. if (flags&SWS_FAST_BILINEAR) {
  794. if (c->canMMX2BeUsed) {
  795. c->lumXInc+= 20;
  796. c->chrXInc+= 20;
  797. }
  798. //we don't use the x86 asm scaler if MMX is available
  799. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
  800. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  801. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  802. }
  803. }
  804. /* precalculate horizontal scaler filter coefficients */
  805. {
  806. #if HAVE_MMX2
  807. // can't downscale !!!
  808. if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
  809. c->lumMmx2FilterCodeSize = initMMX2HScaler( dstW, c->lumXInc, NULL, NULL, NULL, 8);
  810. c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
  811. #ifdef MAP_ANONYMOUS
  812. c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  813. c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  814. #elif HAVE_VIRTUALALLOC
  815. c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  816. c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  817. #else
  818. c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
  819. c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
  820. #endif
  821. if (!c->lumMmx2FilterCode || !c->chrMmx2FilterCode)
  822. return AVERROR(ENOMEM);
  823. FF_ALLOCZ_OR_GOTO(c, c->hLumFilter , (dstW /8+8)*sizeof(int16_t), fail);
  824. FF_ALLOCZ_OR_GOTO(c, c->hChrFilter , (c->chrDstW /4+8)*sizeof(int16_t), fail);
  825. FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW /2/8+8)*sizeof(int32_t), fail);
  826. FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
  827. initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->hLumFilter, c->hLumFilterPos, 8);
  828. initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->hChrFilter, c->hChrFilterPos, 4);
  829. #ifdef MAP_ANONYMOUS
  830. mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  831. mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  832. #endif
  833. } else
  834. #endif /* HAVE_MMX2 */
  835. {
  836. const int filterAlign=
  837. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 4 :
  838. (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
  839. 1;
  840. if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  841. srcW , dstW, filterAlign, 1<<14,
  842. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags, cpu_flags,
  843. srcFilter->lumH, dstFilter->lumH, c->param, 1) < 0)
  844. goto fail;
  845. if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  846. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  847. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, cpu_flags,
  848. srcFilter->chrH, dstFilter->chrH, c->param, 1) < 0)
  849. goto fail;
  850. }
  851. } // initialize horizontal stuff
  852. /* precalculate vertical scaler filter coefficients */
  853. {
  854. const int filterAlign=
  855. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 2 :
  856. (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
  857. 1;
  858. if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  859. srcH , dstH, filterAlign, (1<<12),
  860. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags, cpu_flags,
  861. srcFilter->lumV, dstFilter->lumV, c->param, 0) < 0)
  862. goto fail;
  863. if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  864. c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
  865. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, cpu_flags,
  866. srcFilter->chrV, dstFilter->chrV, c->param, 0) < 0)
  867. goto fail;
  868. #if HAVE_ALTIVEC
  869. FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
  870. FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
  871. for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
  872. int j;
  873. short *p = (short *)&c->vYCoeffsBank[i];
  874. for (j=0;j<8;j++)
  875. p[j] = c->vLumFilter[i];
  876. }
  877. for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
  878. int j;
  879. short *p = (short *)&c->vCCoeffsBank[i];
  880. for (j=0;j<8;j++)
  881. p[j] = c->vChrFilter[i];
  882. }
  883. #endif
  884. }
  885. // calculate buffer sizes so that they won't run out while handling these damn slices
  886. c->vLumBufSize= c->vLumFilterSize;
  887. c->vChrBufSize= c->vChrFilterSize;
  888. for (i=0; i<dstH; i++) {
  889. int chrI= i*c->chrDstH / dstH;
  890. int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  891. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  892. nextSlice>>= c->chrSrcVSubSample;
  893. nextSlice<<= c->chrSrcVSubSample;
  894. if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  895. c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
  896. if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  897. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  898. }
  899. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  900. // allocate several megabytes to handle all possible cases)
  901. FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*3*sizeof(int16_t*), fail);
  902. FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize*3*sizeof(int16_t*), fail);
  903. FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize*3*sizeof(int16_t*), fail);
  904. if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
  905. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*3*sizeof(int16_t*), fail);
  906. //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)
  907. /* align at 16 bytes for AltiVec */
  908. for (i=0; i<c->vLumBufSize; i++) {
  909. FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], dst_stride+16, fail);
  910. c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
  911. }
  912. // 64 / (c->dstBpc & ~7) is the same as 16 / sizeof(scaling_intermediate)
  913. c->uv_off_px = dst_stride_px + 64 / (c->dstBpc &~ 7);
  914. c->uv_off_byte = dst_stride + 16;
  915. for (i=0; i<c->vChrBufSize; i++) {
  916. FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i+c->vChrBufSize], dst_stride*2+32, fail);
  917. c->chrUPixBuf[i] = c->chrUPixBuf[i+c->vChrBufSize];
  918. c->chrVPixBuf[i] = c->chrVPixBuf[i+c->vChrBufSize] = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
  919. }
  920. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  921. for (i=0; i<c->vLumBufSize; i++) {
  922. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], dst_stride+16, fail);
  923. c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
  924. }
  925. //try to avoid drawing green stuff between the right end and the stride end
  926. for (i=0; i<c->vChrBufSize; i++)
  927. memset(c->chrUPixBuf[i], 64, dst_stride*2+1);
  928. assert(c->chrDstH <= dstH);
  929. if (flags&SWS_PRINT_INFO) {
  930. if (flags&SWS_FAST_BILINEAR) av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
  931. else if (flags&SWS_BILINEAR) av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
  932. else if (flags&SWS_BICUBIC) av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
  933. else if (flags&SWS_X) av_log(c, AV_LOG_INFO, "Experimental scaler, ");
  934. else if (flags&SWS_POINT) av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
  935. else if (flags&SWS_AREA) av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
  936. else if (flags&SWS_BICUBLIN) av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
  937. else if (flags&SWS_GAUSS) av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
  938. else if (flags&SWS_SINC) av_log(c, AV_LOG_INFO, "Sinc scaler, ");
  939. else if (flags&SWS_LANCZOS) av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
  940. else if (flags&SWS_SPLINE) av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
  941. else av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
  942. av_log(c, AV_LOG_INFO, "from %s to %s%s ",
  943. sws_format_name(srcFormat),
  944. #ifdef DITHER1XBPP
  945. dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ||
  946. dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
  947. dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE ? "dithered " : "",
  948. #else
  949. "",
  950. #endif
  951. sws_format_name(dstFormat));
  952. if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2) av_log(c, AV_LOG_INFO, "using MMX2\n");
  953. else if (HAVE_AMD3DNOW && cpu_flags & AV_CPU_FLAG_3DNOW) av_log(c, AV_LOG_INFO, "using 3DNOW\n");
  954. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) av_log(c, AV_LOG_INFO, "using MMX\n");
  955. else if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) av_log(c, AV_LOG_INFO, "using AltiVec\n");
  956. else av_log(c, AV_LOG_INFO, "using C\n");
  957. av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  958. av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  959. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  960. av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  961. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  962. }
  963. c->swScale= ff_getSwsFunc(c);
  964. return 0;
  965. fail: //FIXME replace things by appropriate error codes
  966. return -1;
  967. }
  968. #if FF_API_SWS_GETCONTEXT
  969. SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
  970. int dstW, int dstH, enum PixelFormat dstFormat, int flags,
  971. SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  972. {
  973. SwsContext *c;
  974. if(!(c=sws_alloc_context()))
  975. return NULL;
  976. c->flags= flags;
  977. c->srcW= srcW;
  978. c->srcH= srcH;
  979. c->dstW= dstW;
  980. c->dstH= dstH;
  981. c->srcRange = handle_jpeg(&srcFormat);
  982. c->dstRange = handle_jpeg(&dstFormat);
  983. c->srcFormat= srcFormat;
  984. c->dstFormat= dstFormat;
  985. if (param) {
  986. c->param[0] = param[0];
  987. c->param[1] = param[1];
  988. }
  989. 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);
  990. if(sws_init_context(c, srcFilter, dstFilter) < 0){
  991. sws_freeContext(c);
  992. return NULL;
  993. }
  994. return c;
  995. }
  996. #endif
  997. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  998. float lumaSharpen, float chromaSharpen,
  999. float chromaHShift, float chromaVShift,
  1000. int verbose)
  1001. {
  1002. SwsFilter *filter= av_malloc(sizeof(SwsFilter));
  1003. if (!filter)
  1004. return NULL;
  1005. if (lumaGBlur!=0.0) {
  1006. filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
  1007. filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
  1008. } else {
  1009. filter->lumH= sws_getIdentityVec();
  1010. filter->lumV= sws_getIdentityVec();
  1011. }
  1012. if (chromaGBlur!=0.0) {
  1013. filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
  1014. filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
  1015. } else {
  1016. filter->chrH= sws_getIdentityVec();
  1017. filter->chrV= sws_getIdentityVec();
  1018. }
  1019. if (chromaSharpen!=0.0) {
  1020. SwsVector *id= sws_getIdentityVec();
  1021. sws_scaleVec(filter->chrH, -chromaSharpen);
  1022. sws_scaleVec(filter->chrV, -chromaSharpen);
  1023. sws_addVec(filter->chrH, id);
  1024. sws_addVec(filter->chrV, id);
  1025. sws_freeVec(id);
  1026. }
  1027. if (lumaSharpen!=0.0) {
  1028. SwsVector *id= sws_getIdentityVec();
  1029. sws_scaleVec(filter->lumH, -lumaSharpen);
  1030. sws_scaleVec(filter->lumV, -lumaSharpen);
  1031. sws_addVec(filter->lumH, id);
  1032. sws_addVec(filter->lumV, id);
  1033. sws_freeVec(id);
  1034. }
  1035. if (chromaHShift != 0.0)
  1036. sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
  1037. if (chromaVShift != 0.0)
  1038. sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
  1039. sws_normalizeVec(filter->chrH, 1.0);
  1040. sws_normalizeVec(filter->chrV, 1.0);
  1041. sws_normalizeVec(filter->lumH, 1.0);
  1042. sws_normalizeVec(filter->lumV, 1.0);
  1043. if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
  1044. if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
  1045. return filter;
  1046. }
  1047. SwsVector *sws_allocVec(int length)
  1048. {
  1049. SwsVector *vec = av_malloc(sizeof(SwsVector));
  1050. if (!vec)
  1051. return NULL;
  1052. vec->length = length;
  1053. vec->coeff = av_malloc(sizeof(double) * length);
  1054. if (!vec->coeff)
  1055. av_freep(&vec);
  1056. return vec;
  1057. }
  1058. SwsVector *sws_getGaussianVec(double variance, double quality)
  1059. {
  1060. const int length= (int)(variance*quality + 0.5) | 1;
  1061. int i;
  1062. double middle= (length-1)*0.5;
  1063. SwsVector *vec= sws_allocVec(length);
  1064. if (!vec)
  1065. return NULL;
  1066. for (i=0; i<length; i++) {
  1067. double dist= i-middle;
  1068. vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*M_PI);
  1069. }
  1070. sws_normalizeVec(vec, 1.0);
  1071. return vec;
  1072. }
  1073. SwsVector *sws_getConstVec(double c, int length)
  1074. {
  1075. int i;
  1076. SwsVector *vec= sws_allocVec(length);
  1077. if (!vec)
  1078. return NULL;
  1079. for (i=0; i<length; i++)
  1080. vec->coeff[i]= c;
  1081. return vec;
  1082. }
  1083. SwsVector *sws_getIdentityVec(void)
  1084. {
  1085. return sws_getConstVec(1.0, 1);
  1086. }
  1087. static double sws_dcVec(SwsVector *a)
  1088. {
  1089. int i;
  1090. double sum=0;
  1091. for (i=0; i<a->length; i++)
  1092. sum+= a->coeff[i];
  1093. return sum;
  1094. }
  1095. void sws_scaleVec(SwsVector *a, double scalar)
  1096. {
  1097. int i;
  1098. for (i=0; i<a->length; i++)
  1099. a->coeff[i]*= scalar;
  1100. }
  1101. void sws_normalizeVec(SwsVector *a, double height)
  1102. {
  1103. sws_scaleVec(a, height/sws_dcVec(a));
  1104. }
  1105. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
  1106. {
  1107. int length= a->length + b->length - 1;
  1108. int i, j;
  1109. SwsVector *vec= sws_getConstVec(0.0, length);
  1110. if (!vec)
  1111. return NULL;
  1112. for (i=0; i<a->length; i++) {
  1113. for (j=0; j<b->length; j++) {
  1114. vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
  1115. }
  1116. }
  1117. return vec;
  1118. }
  1119. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
  1120. {
  1121. int length= FFMAX(a->length, b->length);
  1122. int i;
  1123. SwsVector *vec= sws_getConstVec(0.0, length);
  1124. if (!vec)
  1125. return NULL;
  1126. for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1127. for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  1128. return vec;
  1129. }
  1130. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
  1131. {
  1132. int length= FFMAX(a->length, b->length);
  1133. int i;
  1134. SwsVector *vec= sws_getConstVec(0.0, length);
  1135. if (!vec)
  1136. return NULL;
  1137. for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1138. for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  1139. return vec;
  1140. }
  1141. /* shift left / or right if "shift" is negative */
  1142. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
  1143. {
  1144. int length= a->length + FFABS(shift)*2;
  1145. int i;
  1146. SwsVector *vec= sws_getConstVec(0.0, length);
  1147. if (!vec)
  1148. return NULL;
  1149. for (i=0; i<a->length; i++) {
  1150. vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  1151. }
  1152. return vec;
  1153. }
  1154. void sws_shiftVec(SwsVector *a, int shift)
  1155. {
  1156. SwsVector *shifted= sws_getShiftedVec(a, shift);
  1157. av_free(a->coeff);
  1158. a->coeff= shifted->coeff;
  1159. a->length= shifted->length;
  1160. av_free(shifted);
  1161. }
  1162. void sws_addVec(SwsVector *a, SwsVector *b)
  1163. {
  1164. SwsVector *sum= sws_sumVec(a, b);
  1165. av_free(a->coeff);
  1166. a->coeff= sum->coeff;
  1167. a->length= sum->length;
  1168. av_free(sum);
  1169. }
  1170. void sws_subVec(SwsVector *a, SwsVector *b)
  1171. {
  1172. SwsVector *diff= sws_diffVec(a, b);
  1173. av_free(a->coeff);
  1174. a->coeff= diff->coeff;
  1175. a->length= diff->length;
  1176. av_free(diff);
  1177. }
  1178. void sws_convVec(SwsVector *a, SwsVector *b)
  1179. {
  1180. SwsVector *conv= sws_getConvVec(a, b);
  1181. av_free(a->coeff);
  1182. a->coeff= conv->coeff;
  1183. a->length= conv->length;
  1184. av_free(conv);
  1185. }
  1186. SwsVector *sws_cloneVec(SwsVector *a)
  1187. {
  1188. int i;
  1189. SwsVector *vec= sws_allocVec(a->length);
  1190. if (!vec)
  1191. return NULL;
  1192. for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
  1193. return vec;
  1194. }
  1195. void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
  1196. {
  1197. int i;
  1198. double max=0;
  1199. double min=0;
  1200. double range;
  1201. for (i=0; i<a->length; i++)
  1202. if (a->coeff[i]>max) max= a->coeff[i];
  1203. for (i=0; i<a->length; i++)
  1204. if (a->coeff[i]<min) min= a->coeff[i];
  1205. range= max - min;
  1206. for (i=0; i<a->length; i++) {
  1207. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  1208. av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
  1209. for (;x>0; x--) av_log(log_ctx, log_level, " ");
  1210. av_log(log_ctx, log_level, "|\n");
  1211. }
  1212. }
  1213. void sws_freeVec(SwsVector *a)
  1214. {
  1215. if (!a) return;
  1216. av_freep(&a->coeff);
  1217. a->length=0;
  1218. av_free(a);
  1219. }
  1220. void sws_freeFilter(SwsFilter *filter)
  1221. {
  1222. if (!filter) return;
  1223. if (filter->lumH) sws_freeVec(filter->lumH);
  1224. if (filter->lumV) sws_freeVec(filter->lumV);
  1225. if (filter->chrH) sws_freeVec(filter->chrH);
  1226. if (filter->chrV) sws_freeVec(filter->chrV);
  1227. av_free(filter);
  1228. }
  1229. void sws_freeContext(SwsContext *c)
  1230. {
  1231. int i;
  1232. if (!c) return;
  1233. if (c->lumPixBuf) {
  1234. for (i=0; i<c->vLumBufSize; i++)
  1235. av_freep(&c->lumPixBuf[i]);
  1236. av_freep(&c->lumPixBuf);
  1237. }
  1238. if (c->chrUPixBuf) {
  1239. for (i=0; i<c->vChrBufSize; i++)
  1240. av_freep(&c->chrUPixBuf[i]);
  1241. av_freep(&c->chrUPixBuf);
  1242. av_freep(&c->chrVPixBuf);
  1243. }
  1244. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  1245. for (i=0; i<c->vLumBufSize; i++)
  1246. av_freep(&c->alpPixBuf[i]);
  1247. av_freep(&c->alpPixBuf);
  1248. }
  1249. av_freep(&c->vLumFilter);
  1250. av_freep(&c->vChrFilter);
  1251. av_freep(&c->hLumFilter);
  1252. av_freep(&c->hChrFilter);
  1253. #if HAVE_ALTIVEC
  1254. av_freep(&c->vYCoeffsBank);
  1255. av_freep(&c->vCCoeffsBank);
  1256. #endif
  1257. av_freep(&c->vLumFilterPos);
  1258. av_freep(&c->vChrFilterPos);
  1259. av_freep(&c->hLumFilterPos);
  1260. av_freep(&c->hChrFilterPos);
  1261. #if HAVE_MMX
  1262. #ifdef MAP_ANONYMOUS
  1263. if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
  1264. if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
  1265. #elif HAVE_VIRTUALALLOC
  1266. if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, 0, MEM_RELEASE);
  1267. if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, 0, MEM_RELEASE);
  1268. #else
  1269. av_free(c->lumMmx2FilterCode);
  1270. av_free(c->chrMmx2FilterCode);
  1271. #endif
  1272. c->lumMmx2FilterCode=NULL;
  1273. c->chrMmx2FilterCode=NULL;
  1274. #endif /* HAVE_MMX */
  1275. av_freep(&c->yuvTable);
  1276. av_free(c->formatConvBuffer);
  1277. av_free(c);
  1278. }
  1279. struct SwsContext *sws_getCachedContext(struct SwsContext *context,
  1280. int srcW, int srcH, enum PixelFormat srcFormat,
  1281. int dstW, int dstH, enum PixelFormat dstFormat, int flags,
  1282. SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  1283. {
  1284. static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
  1285. if (!param)
  1286. param = default_param;
  1287. if (context &&
  1288. (context->srcW != srcW ||
  1289. context->srcH != srcH ||
  1290. context->srcFormat != srcFormat ||
  1291. context->dstW != dstW ||
  1292. context->dstH != dstH ||
  1293. context->dstFormat != dstFormat ||
  1294. context->flags != flags ||
  1295. context->param[0] != param[0] ||
  1296. context->param[1] != param[1])) {
  1297. sws_freeContext(context);
  1298. context = NULL;
  1299. }
  1300. if (!context) {
  1301. if (!(context = sws_alloc_context()))
  1302. return NULL;
  1303. context->srcW = srcW;
  1304. context->srcH = srcH;
  1305. context->srcRange = handle_jpeg(&srcFormat);
  1306. context->srcFormat = srcFormat;
  1307. context->dstW = dstW;
  1308. context->dstH = dstH;
  1309. context->dstRange = handle_jpeg(&dstFormat);
  1310. context->dstFormat = dstFormat;
  1311. context->flags = flags;
  1312. context->param[0] = param[0];
  1313. context->param[1] = param[1];
  1314. 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);
  1315. if (sws_init_context(context, srcFilter, dstFilter) < 0) {
  1316. sws_freeContext(context);
  1317. return NULL;
  1318. }
  1319. }
  1320. return context;
  1321. }