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.

1771 lines
62KB

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