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.

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