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.

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