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.

1752 lines
61KB

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