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.

1524 lines
54KB

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