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.

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