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.

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