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.

1768 lines
62KB

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