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.

1545 lines
55KB

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