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.

3464 lines
122KB

  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 modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * 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. * the C code (not assembly, mmx, ...) of this file can be used
  21. * under the LGPL license too
  22. */
  23. /*
  24. supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
  25. supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  26. {BGR,RGB}{1,4,8,15,16} support dithering
  27. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  28. YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
  29. x -> x
  30. YUV9 -> YV12
  31. YUV9/YV12 -> Y800
  32. Y800 -> YUV9/YV12
  33. BGR24 -> BGR32 & RGB24 -> RGB32
  34. BGR32 -> BGR24 & RGB32 -> RGB24
  35. BGR15 -> BGR16
  36. */
  37. /*
  38. tested special converters (most are tested actually, but I did not write it down ...)
  39. YV12 -> BGR16
  40. YV12 -> YV12
  41. BGR15 -> BGR16
  42. BGR16 -> BGR16
  43. YVU9 -> YV12
  44. untested special converters
  45. YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
  46. YV12/I420 -> YV12/I420
  47. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  48. BGR24 -> BGR32 & RGB24 -> RGB32
  49. BGR32 -> BGR24 & RGB32 -> RGB24
  50. BGR24 -> YV12
  51. */
  52. #define _SVID_SOURCE //needed for MAP_ANONYMOUS
  53. #include <inttypes.h>
  54. #include <string.h>
  55. #include <math.h>
  56. #include <stdio.h>
  57. #include "config.h"
  58. #include <assert.h>
  59. #if HAVE_SYS_MMAN_H
  60. #include <sys/mman.h>
  61. #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  62. #define MAP_ANONYMOUS MAP_ANON
  63. #endif
  64. #endif
  65. #if HAVE_VIRTUALALLOC
  66. #define WIN32_LEAN_AND_MEAN
  67. #include <windows.h>
  68. #endif
  69. #include "swscale.h"
  70. #include "swscale_internal.h"
  71. #include "rgb2rgb.h"
  72. #include "libavutil/intreadwrite.h"
  73. #include "libavutil/x86_cpu.h"
  74. #include "libavutil/avutil.h"
  75. #include "libavutil/bswap.h"
  76. #include "libavutil/pixdesc.h"
  77. unsigned swscale_version(void)
  78. {
  79. return LIBSWSCALE_VERSION_INT;
  80. }
  81. const char *swscale_configuration(void)
  82. {
  83. return FFMPEG_CONFIGURATION;
  84. }
  85. const char *swscale_license(void)
  86. {
  87. #define LICENSE_PREFIX "libswscale license: "
  88. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  89. }
  90. #undef MOVNTQ
  91. #undef PAVGB
  92. //#undef HAVE_MMX2
  93. //#define HAVE_AMD3DNOW
  94. //#undef HAVE_MMX
  95. //#undef ARCH_X86
  96. #define DITHER1XBPP
  97. #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
  98. #define RET 0xC3 //near return opcode for x86
  99. #ifdef M_PI
  100. #define PI M_PI
  101. #else
  102. #define PI 3.14159265358979323846
  103. #endif
  104. #define isSupportedIn(x) ( \
  105. (x)==PIX_FMT_YUV420P \
  106. || (x)==PIX_FMT_YUVA420P \
  107. || (x)==PIX_FMT_YUYV422 \
  108. || (x)==PIX_FMT_UYVY422 \
  109. || (x)==PIX_FMT_RGB48BE \
  110. || (x)==PIX_FMT_RGB48LE \
  111. || (x)==PIX_FMT_RGB32 \
  112. || (x)==PIX_FMT_RGB32_1 \
  113. || (x)==PIX_FMT_BGR24 \
  114. || (x)==PIX_FMT_BGR565 \
  115. || (x)==PIX_FMT_BGR555 \
  116. || (x)==PIX_FMT_BGR32 \
  117. || (x)==PIX_FMT_BGR32_1 \
  118. || (x)==PIX_FMT_RGB24 \
  119. || (x)==PIX_FMT_RGB565 \
  120. || (x)==PIX_FMT_RGB555 \
  121. || (x)==PIX_FMT_GRAY8 \
  122. || (x)==PIX_FMT_YUV410P \
  123. || (x)==PIX_FMT_YUV440P \
  124. || (x)==PIX_FMT_NV12 \
  125. || (x)==PIX_FMT_NV21 \
  126. || (x)==PIX_FMT_GRAY16BE \
  127. || (x)==PIX_FMT_GRAY16LE \
  128. || (x)==PIX_FMT_YUV444P \
  129. || (x)==PIX_FMT_YUV422P \
  130. || (x)==PIX_FMT_YUV411P \
  131. || (x)==PIX_FMT_PAL8 \
  132. || (x)==PIX_FMT_BGR8 \
  133. || (x)==PIX_FMT_RGB8 \
  134. || (x)==PIX_FMT_BGR4_BYTE \
  135. || (x)==PIX_FMT_RGB4_BYTE \
  136. || (x)==PIX_FMT_YUV440P \
  137. || (x)==PIX_FMT_MONOWHITE \
  138. || (x)==PIX_FMT_MONOBLACK \
  139. || (x)==PIX_FMT_YUV420P16LE \
  140. || (x)==PIX_FMT_YUV422P16LE \
  141. || (x)==PIX_FMT_YUV444P16LE \
  142. || (x)==PIX_FMT_YUV420P16BE \
  143. || (x)==PIX_FMT_YUV422P16BE \
  144. || (x)==PIX_FMT_YUV444P16BE \
  145. )
  146. #define isSupportedOut(x) ( \
  147. (x)==PIX_FMT_YUV420P \
  148. || (x)==PIX_FMT_YUVA420P \
  149. || (x)==PIX_FMT_YUYV422 \
  150. || (x)==PIX_FMT_UYVY422 \
  151. || (x)==PIX_FMT_YUV444P \
  152. || (x)==PIX_FMT_YUV422P \
  153. || (x)==PIX_FMT_YUV411P \
  154. || isRGB(x) \
  155. || isBGR(x) \
  156. || (x)==PIX_FMT_NV12 \
  157. || (x)==PIX_FMT_NV21 \
  158. || (x)==PIX_FMT_GRAY16BE \
  159. || (x)==PIX_FMT_GRAY16LE \
  160. || (x)==PIX_FMT_GRAY8 \
  161. || (x)==PIX_FMT_YUV410P \
  162. || (x)==PIX_FMT_YUV440P \
  163. || (x)==PIX_FMT_YUV420P16LE \
  164. || (x)==PIX_FMT_YUV422P16LE \
  165. || (x)==PIX_FMT_YUV444P16LE \
  166. || (x)==PIX_FMT_YUV420P16BE \
  167. || (x)==PIX_FMT_YUV422P16BE \
  168. || (x)==PIX_FMT_YUV444P16BE \
  169. )
  170. #define isPacked(x) ( \
  171. (x)==PIX_FMT_PAL8 \
  172. || (x)==PIX_FMT_YUYV422 \
  173. || (x)==PIX_FMT_UYVY422 \
  174. || isRGB(x) \
  175. || isBGR(x) \
  176. )
  177. #define usePal(x) (av_pix_fmt_descriptors[x].flags & PIX_FMT_PAL)
  178. #define RGB2YUV_SHIFT 15
  179. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  180. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  181. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  182. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  183. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  184. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  185. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  186. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  187. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  188. extern const int32_t ff_yuv2rgb_coeffs[8][4];
  189. static const double rgb2yuv_table[8][9]={
  190. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
  191. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
  192. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
  193. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
  194. {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
  195. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
  196. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
  197. {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
  198. };
  199. /*
  200. NOTES
  201. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  202. TODO
  203. more intelligent misalignment avoidance for the horizontal scaler
  204. write special vertical cubic upscale version
  205. optimize C code (YV12 / minmax)
  206. add support for packed pixel YUV input & output
  207. add support for Y8 output
  208. optimize BGR24 & BGR32
  209. add BGR4 output support
  210. write special BGR->BGR scaler
  211. */
  212. #if ARCH_X86 && CONFIG_GPL
  213. DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
  214. DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
  215. DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
  216. DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
  217. DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
  218. DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
  219. DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
  220. DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
  221. const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
  222. 0x0103010301030103LL,
  223. 0x0200020002000200LL,};
  224. const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
  225. 0x0602060206020602LL,
  226. 0x0004000400040004LL,};
  227. DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
  228. DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
  229. DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
  230. DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
  231. DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
  232. DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
  233. DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
  234. DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
  235. DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
  236. #ifdef FAST_BGR2YV12
  237. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
  238. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
  239. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
  240. #else
  241. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
  242. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
  243. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
  244. #endif /* FAST_BGR2YV12 */
  245. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
  246. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
  247. DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
  248. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
  249. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
  250. DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
  251. DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
  252. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
  253. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
  254. {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
  255. {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
  256. };
  257. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
  258. #endif /* ARCH_X86 && CONFIG_GPL */
  259. // clipping helper table for C implementations:
  260. static unsigned char clip_table[768];
  261. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
  262. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4[2][8])={
  263. { 1, 3, 1, 3, 1, 3, 1, 3, },
  264. { 2, 0, 2, 0, 2, 0, 2, 0, },
  265. };
  266. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8[2][8])={
  267. { 6, 2, 6, 2, 6, 2, 6, 2, },
  268. { 0, 4, 0, 4, 0, 4, 0, 4, },
  269. };
  270. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32[8][8])={
  271. { 17, 9, 23, 15, 16, 8, 22, 14, },
  272. { 5, 29, 3, 27, 4, 28, 2, 26, },
  273. { 21, 13, 19, 11, 20, 12, 18, 10, },
  274. { 0, 24, 6, 30, 1, 25, 7, 31, },
  275. { 16, 8, 22, 14, 17, 9, 23, 15, },
  276. { 4, 28, 2, 26, 5, 29, 3, 27, },
  277. { 20, 12, 18, 10, 21, 13, 19, 11, },
  278. { 1, 25, 7, 31, 0, 24, 6, 30, },
  279. };
  280. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73[8][8])={
  281. { 0, 55, 14, 68, 3, 58, 17, 72, },
  282. { 37, 18, 50, 32, 40, 22, 54, 35, },
  283. { 9, 64, 5, 59, 13, 67, 8, 63, },
  284. { 46, 27, 41, 23, 49, 31, 44, 26, },
  285. { 2, 57, 16, 71, 1, 56, 15, 70, },
  286. { 39, 21, 52, 34, 38, 19, 51, 33, },
  287. { 11, 66, 7, 62, 10, 65, 6, 60, },
  288. { 48, 30, 43, 25, 47, 29, 42, 24, },
  289. };
  290. #if 1
  291. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
  292. {117, 62, 158, 103, 113, 58, 155, 100, },
  293. { 34, 199, 21, 186, 31, 196, 17, 182, },
  294. {144, 89, 131, 76, 141, 86, 127, 72, },
  295. { 0, 165, 41, 206, 10, 175, 52, 217, },
  296. {110, 55, 151, 96, 120, 65, 162, 107, },
  297. { 28, 193, 14, 179, 38, 203, 24, 189, },
  298. {138, 83, 124, 69, 148, 93, 134, 79, },
  299. { 7, 172, 48, 213, 3, 168, 45, 210, },
  300. };
  301. #elif 1
  302. // tries to correct a gamma of 1.5
  303. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
  304. { 0, 143, 18, 200, 2, 156, 25, 215, },
  305. { 78, 28, 125, 64, 89, 36, 138, 74, },
  306. { 10, 180, 3, 161, 16, 195, 8, 175, },
  307. {109, 51, 93, 38, 121, 60, 105, 47, },
  308. { 1, 152, 23, 210, 0, 147, 20, 205, },
  309. { 85, 33, 134, 71, 81, 30, 130, 67, },
  310. { 14, 190, 6, 171, 12, 185, 5, 166, },
  311. {117, 57, 101, 44, 113, 54, 97, 41, },
  312. };
  313. #elif 1
  314. // tries to correct a gamma of 2.0
  315. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
  316. { 0, 124, 8, 193, 0, 140, 12, 213, },
  317. { 55, 14, 104, 42, 66, 19, 119, 52, },
  318. { 3, 168, 1, 145, 6, 187, 3, 162, },
  319. { 86, 31, 70, 21, 99, 39, 82, 28, },
  320. { 0, 134, 11, 206, 0, 129, 9, 200, },
  321. { 62, 17, 114, 48, 58, 16, 109, 45, },
  322. { 5, 181, 2, 157, 4, 175, 1, 151, },
  323. { 95, 36, 78, 26, 90, 34, 74, 24, },
  324. };
  325. #else
  326. // tries to correct a gamma of 2.5
  327. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
  328. { 0, 107, 3, 187, 0, 125, 6, 212, },
  329. { 39, 7, 86, 28, 49, 11, 102, 36, },
  330. { 1, 158, 0, 131, 3, 180, 1, 151, },
  331. { 68, 19, 52, 12, 81, 25, 64, 17, },
  332. { 0, 119, 5, 203, 0, 113, 4, 195, },
  333. { 45, 9, 96, 33, 42, 8, 91, 30, },
  334. { 2, 172, 1, 144, 2, 165, 0, 137, },
  335. { 77, 23, 60, 15, 72, 21, 56, 14, },
  336. };
  337. #endif
  338. const char *sws_format_name(enum PixelFormat format)
  339. {
  340. if ((unsigned)format < PIX_FMT_NB && av_pix_fmt_descriptors[format].name)
  341. return av_pix_fmt_descriptors[format].name;
  342. else
  343. return "Unknown format";
  344. }
  345. static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  346. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  347. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
  348. int dstW, int chrDstW, int big_endian)
  349. {
  350. //FIXME Optimize (just quickly written not optimized..)
  351. int i;
  352. for (i = 0; i < dstW; i++) {
  353. int val = 1 << 10;
  354. int j;
  355. for (j = 0; j < lumFilterSize; j++)
  356. val += lumSrc[j][i] * lumFilter[j];
  357. if (big_endian) {
  358. AV_WB16(&dest[i], av_clip_uint16(val >> 11));
  359. } else {
  360. AV_WL16(&dest[i], av_clip_uint16(val >> 11));
  361. }
  362. }
  363. if (uDest) {
  364. for (i = 0; i < chrDstW; i++) {
  365. int u = 1 << 10;
  366. int v = 1 << 10;
  367. int j;
  368. for (j = 0; j < chrFilterSize; j++) {
  369. u += chrSrc[j][i ] * chrFilter[j];
  370. v += chrSrc[j][i + VOFW] * chrFilter[j];
  371. }
  372. if (big_endian) {
  373. AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
  374. AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
  375. } else {
  376. AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
  377. AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
  378. }
  379. }
  380. }
  381. if (CONFIG_SWSCALE_ALPHA && aDest) {
  382. for (i = 0; i < dstW; i++) {
  383. int val = 1 << 10;
  384. int j;
  385. for (j = 0; j < lumFilterSize; j++)
  386. val += alpSrc[j][i] * lumFilter[j];
  387. if (big_endian) {
  388. AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
  389. } else {
  390. AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
  391. }
  392. }
  393. }
  394. }
  395. static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  396. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  397. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
  398. enum PixelFormat dstFormat)
  399. {
  400. if (isBE(dstFormat)) {
  401. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
  402. chrFilter, chrSrc, chrFilterSize,
  403. alpSrc,
  404. dest, uDest, vDest, aDest,
  405. dstW, chrDstW, 1);
  406. } else {
  407. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
  408. chrFilter, chrSrc, chrFilterSize,
  409. alpSrc,
  410. dest, uDest, vDest, aDest,
  411. dstW, chrDstW, 0);
  412. }
  413. }
  414. static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  415. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  416. const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
  417. {
  418. //FIXME Optimize (just quickly written not optimized..)
  419. int i;
  420. for (i=0; i<dstW; i++) {
  421. int val=1<<18;
  422. int j;
  423. for (j=0; j<lumFilterSize; j++)
  424. val += lumSrc[j][i] * lumFilter[j];
  425. dest[i]= av_clip_uint8(val>>19);
  426. }
  427. if (uDest)
  428. for (i=0; i<chrDstW; i++) {
  429. int u=1<<18;
  430. int v=1<<18;
  431. int j;
  432. for (j=0; j<chrFilterSize; j++) {
  433. u += chrSrc[j][i] * chrFilter[j];
  434. v += chrSrc[j][i + VOFW] * chrFilter[j];
  435. }
  436. uDest[i]= av_clip_uint8(u>>19);
  437. vDest[i]= av_clip_uint8(v>>19);
  438. }
  439. if (CONFIG_SWSCALE_ALPHA && aDest)
  440. for (i=0; i<dstW; i++) {
  441. int val=1<<18;
  442. int j;
  443. for (j=0; j<lumFilterSize; j++)
  444. val += alpSrc[j][i] * lumFilter[j];
  445. aDest[i]= av_clip_uint8(val>>19);
  446. }
  447. }
  448. static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  449. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  450. uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
  451. {
  452. //FIXME Optimize (just quickly written not optimized..)
  453. int i;
  454. for (i=0; i<dstW; i++) {
  455. int val=1<<18;
  456. int j;
  457. for (j=0; j<lumFilterSize; j++)
  458. val += lumSrc[j][i] * lumFilter[j];
  459. dest[i]= av_clip_uint8(val>>19);
  460. }
  461. if (!uDest)
  462. return;
  463. if (dstFormat == PIX_FMT_NV12)
  464. for (i=0; i<chrDstW; i++) {
  465. int u=1<<18;
  466. int v=1<<18;
  467. int j;
  468. for (j=0; j<chrFilterSize; j++) {
  469. u += chrSrc[j][i] * chrFilter[j];
  470. v += chrSrc[j][i + VOFW] * chrFilter[j];
  471. }
  472. uDest[2*i]= av_clip_uint8(u>>19);
  473. uDest[2*i+1]= av_clip_uint8(v>>19);
  474. }
  475. else
  476. for (i=0; i<chrDstW; i++) {
  477. int u=1<<18;
  478. int v=1<<18;
  479. int j;
  480. for (j=0; j<chrFilterSize; j++) {
  481. u += chrSrc[j][i] * chrFilter[j];
  482. v += chrSrc[j][i + VOFW] * chrFilter[j];
  483. }
  484. uDest[2*i]= av_clip_uint8(v>>19);
  485. uDest[2*i+1]= av_clip_uint8(u>>19);
  486. }
  487. }
  488. #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
  489. for (i=0; i<(dstW>>1); i++) {\
  490. int j;\
  491. int Y1 = 1<<18;\
  492. int Y2 = 1<<18;\
  493. int U = 1<<18;\
  494. int V = 1<<18;\
  495. int av_unused A1, A2;\
  496. type av_unused *r, *b, *g;\
  497. const int i2= 2*i;\
  498. \
  499. for (j=0; j<lumFilterSize; j++) {\
  500. Y1 += lumSrc[j][i2] * lumFilter[j];\
  501. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  502. }\
  503. for (j=0; j<chrFilterSize; j++) {\
  504. U += chrSrc[j][i] * chrFilter[j];\
  505. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  506. }\
  507. Y1>>=19;\
  508. Y2>>=19;\
  509. U >>=19;\
  510. V >>=19;\
  511. if (alpha) {\
  512. A1 = 1<<18;\
  513. A2 = 1<<18;\
  514. for (j=0; j<lumFilterSize; j++) {\
  515. A1 += alpSrc[j][i2 ] * lumFilter[j];\
  516. A2 += alpSrc[j][i2+1] * lumFilter[j];\
  517. }\
  518. A1>>=19;\
  519. A2>>=19;\
  520. }\
  521. #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
  522. YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
  523. if ((Y1|Y2|U|V)&256) {\
  524. if (Y1>255) Y1=255; \
  525. else if (Y1<0)Y1=0; \
  526. if (Y2>255) Y2=255; \
  527. else if (Y2<0)Y2=0; \
  528. if (U>255) U=255; \
  529. else if (U<0) U=0; \
  530. if (V>255) V=255; \
  531. else if (V<0) V=0; \
  532. }\
  533. if (alpha && ((A1|A2)&256)) {\
  534. A1=av_clip_uint8(A1);\
  535. A2=av_clip_uint8(A2);\
  536. }
  537. #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
  538. for (i=0; i<dstW; i++) {\
  539. int j;\
  540. int Y = 0;\
  541. int U = -128<<19;\
  542. int V = -128<<19;\
  543. int av_unused A;\
  544. int R,G,B;\
  545. \
  546. for (j=0; j<lumFilterSize; j++) {\
  547. Y += lumSrc[j][i ] * lumFilter[j];\
  548. }\
  549. for (j=0; j<chrFilterSize; j++) {\
  550. U += chrSrc[j][i ] * chrFilter[j];\
  551. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  552. }\
  553. Y >>=10;\
  554. U >>=10;\
  555. V >>=10;\
  556. if (alpha) {\
  557. A = rnd;\
  558. for (j=0; j<lumFilterSize; j++)\
  559. A += alpSrc[j][i ] * lumFilter[j];\
  560. A >>=19;\
  561. if (A&256)\
  562. A = av_clip_uint8(A);\
  563. }\
  564. #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
  565. YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
  566. Y-= c->yuv2rgb_y_offset;\
  567. Y*= c->yuv2rgb_y_coeff;\
  568. Y+= rnd;\
  569. R= Y + V*c->yuv2rgb_v2r_coeff;\
  570. G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
  571. B= Y + U*c->yuv2rgb_u2b_coeff;\
  572. if ((R|G|B)&(0xC0000000)) {\
  573. if (R>=(256<<22)) R=(256<<22)-1; \
  574. else if (R<0)R=0; \
  575. if (G>=(256<<22)) G=(256<<22)-1; \
  576. else if (G<0)G=0; \
  577. if (B>=(256<<22)) B=(256<<22)-1; \
  578. else if (B<0)B=0; \
  579. }\
  580. #define YSCALE_YUV_2_GRAY16_C \
  581. for (i=0; i<(dstW>>1); i++) {\
  582. int j;\
  583. int Y1 = 1<<18;\
  584. int Y2 = 1<<18;\
  585. int U = 1<<18;\
  586. int V = 1<<18;\
  587. \
  588. const int i2= 2*i;\
  589. \
  590. for (j=0; j<lumFilterSize; j++) {\
  591. Y1 += lumSrc[j][i2] * lumFilter[j];\
  592. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  593. }\
  594. Y1>>=11;\
  595. Y2>>=11;\
  596. if ((Y1|Y2|U|V)&65536) {\
  597. if (Y1>65535) Y1=65535; \
  598. else if (Y1<0)Y1=0; \
  599. if (Y2>65535) Y2=65535; \
  600. else if (Y2<0)Y2=0; \
  601. }
  602. #define YSCALE_YUV_2_RGBX_C(type,alpha) \
  603. YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
  604. r = (type *)c->table_rV[V]; \
  605. g = (type *)(c->table_gU[U] + c->table_gV[V]); \
  606. b = (type *)c->table_bU[U]; \
  607. #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
  608. for (i=0; i<(dstW>>1); i++) { \
  609. const int i2= 2*i; \
  610. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
  611. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
  612. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
  613. int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
  614. type av_unused *r, *b, *g; \
  615. int av_unused A1, A2; \
  616. if (alpha) {\
  617. A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
  618. A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
  619. }\
  620. #define YSCALE_YUV_2_GRAY16_2_C \
  621. for (i=0; i<(dstW>>1); i++) { \
  622. const int i2= 2*i; \
  623. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
  624. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11; \
  625. #define YSCALE_YUV_2_RGB2_C(type,alpha) \
  626. YSCALE_YUV_2_PACKED2_C(type,alpha)\
  627. r = (type *)c->table_rV[V];\
  628. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  629. b = (type *)c->table_bU[U];\
  630. #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
  631. for (i=0; i<(dstW>>1); i++) {\
  632. const int i2= 2*i;\
  633. int Y1= buf0[i2 ]>>7;\
  634. int Y2= buf0[i2+1]>>7;\
  635. int U= (uvbuf1[i ])>>7;\
  636. int V= (uvbuf1[i+VOFW])>>7;\
  637. type av_unused *r, *b, *g;\
  638. int av_unused A1, A2;\
  639. if (alpha) {\
  640. A1= abuf0[i2 ]>>7;\
  641. A2= abuf0[i2+1]>>7;\
  642. }\
  643. #define YSCALE_YUV_2_GRAY16_1_C \
  644. for (i=0; i<(dstW>>1); i++) {\
  645. const int i2= 2*i;\
  646. int Y1= buf0[i2 ]<<1;\
  647. int Y2= buf0[i2+1]<<1;\
  648. #define YSCALE_YUV_2_RGB1_C(type,alpha) \
  649. YSCALE_YUV_2_PACKED1_C(type,alpha)\
  650. r = (type *)c->table_rV[V];\
  651. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  652. b = (type *)c->table_bU[U];\
  653. #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
  654. for (i=0; i<(dstW>>1); i++) {\
  655. const int i2= 2*i;\
  656. int Y1= buf0[i2 ]>>7;\
  657. int Y2= buf0[i2+1]>>7;\
  658. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  659. int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
  660. type av_unused *r, *b, *g;\
  661. int av_unused A1, A2;\
  662. if (alpha) {\
  663. A1= abuf0[i2 ]>>7;\
  664. A2= abuf0[i2+1]>>7;\
  665. }\
  666. #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
  667. YSCALE_YUV_2_PACKED1B_C(type,alpha)\
  668. r = (type *)c->table_rV[V];\
  669. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  670. b = (type *)c->table_bU[U];\
  671. #define YSCALE_YUV_2_MONO2_C \
  672. const uint8_t * const d128=dither_8x8_220[y&7];\
  673. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  674. for (i=0; i<dstW-7; i+=8) {\
  675. int acc;\
  676. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  677. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  678. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  679. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  680. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  681. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  682. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  683. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  684. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  685. dest++;\
  686. }\
  687. #define YSCALE_YUV_2_MONOX_C \
  688. const uint8_t * const d128=dither_8x8_220[y&7];\
  689. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  690. int acc=0;\
  691. for (i=0; i<dstW-1; i+=2) {\
  692. int j;\
  693. int Y1=1<<18;\
  694. int Y2=1<<18;\
  695. \
  696. for (j=0; j<lumFilterSize; j++) {\
  697. Y1 += lumSrc[j][i] * lumFilter[j];\
  698. Y2 += lumSrc[j][i+1] * lumFilter[j];\
  699. }\
  700. Y1>>=19;\
  701. Y2>>=19;\
  702. if ((Y1|Y2)&256) {\
  703. if (Y1>255) Y1=255;\
  704. else if (Y1<0)Y1=0;\
  705. if (Y2>255) Y2=255;\
  706. else if (Y2<0)Y2=0;\
  707. }\
  708. acc+= acc + g[Y1+d128[(i+0)&7]];\
  709. acc+= acc + g[Y2+d128[(i+1)&7]];\
  710. if ((i&7)==6) {\
  711. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  712. dest++;\
  713. }\
  714. }
  715. #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
  716. switch(c->dstFormat) {\
  717. case PIX_FMT_RGB48BE:\
  718. case PIX_FMT_RGB48LE:\
  719. func(uint8_t,0)\
  720. ((uint8_t*)dest)[ 0]= r[Y1];\
  721. ((uint8_t*)dest)[ 1]= r[Y1];\
  722. ((uint8_t*)dest)[ 2]= g[Y1];\
  723. ((uint8_t*)dest)[ 3]= g[Y1];\
  724. ((uint8_t*)dest)[ 4]= b[Y1];\
  725. ((uint8_t*)dest)[ 5]= b[Y1];\
  726. ((uint8_t*)dest)[ 6]= r[Y2];\
  727. ((uint8_t*)dest)[ 7]= r[Y2];\
  728. ((uint8_t*)dest)[ 8]= g[Y2];\
  729. ((uint8_t*)dest)[ 9]= g[Y2];\
  730. ((uint8_t*)dest)[10]= b[Y2];\
  731. ((uint8_t*)dest)[11]= b[Y2];\
  732. dest+=12;\
  733. }\
  734. break;\
  735. case PIX_FMT_RGBA:\
  736. case PIX_FMT_BGRA:\
  737. if (CONFIG_SMALL) {\
  738. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  739. func(uint32_t,needAlpha)\
  740. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
  741. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
  742. }\
  743. } else {\
  744. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  745. func(uint32_t,1)\
  746. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
  747. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
  748. }\
  749. } else {\
  750. func(uint32_t,0)\
  751. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  752. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  753. }\
  754. }\
  755. }\
  756. break;\
  757. case PIX_FMT_ARGB:\
  758. case PIX_FMT_ABGR:\
  759. if (CONFIG_SMALL) {\
  760. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  761. func(uint32_t,needAlpha)\
  762. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
  763. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
  764. }\
  765. } else {\
  766. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  767. func(uint32_t,1)\
  768. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
  769. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
  770. }\
  771. } else {\
  772. func(uint32_t,0)\
  773. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  774. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  775. }\
  776. }\
  777. } \
  778. break;\
  779. case PIX_FMT_RGB24:\
  780. func(uint8_t,0)\
  781. ((uint8_t*)dest)[0]= r[Y1];\
  782. ((uint8_t*)dest)[1]= g[Y1];\
  783. ((uint8_t*)dest)[2]= b[Y1];\
  784. ((uint8_t*)dest)[3]= r[Y2];\
  785. ((uint8_t*)dest)[4]= g[Y2];\
  786. ((uint8_t*)dest)[5]= b[Y2];\
  787. dest+=6;\
  788. }\
  789. break;\
  790. case PIX_FMT_BGR24:\
  791. func(uint8_t,0)\
  792. ((uint8_t*)dest)[0]= b[Y1];\
  793. ((uint8_t*)dest)[1]= g[Y1];\
  794. ((uint8_t*)dest)[2]= r[Y1];\
  795. ((uint8_t*)dest)[3]= b[Y2];\
  796. ((uint8_t*)dest)[4]= g[Y2];\
  797. ((uint8_t*)dest)[5]= r[Y2];\
  798. dest+=6;\
  799. }\
  800. break;\
  801. case PIX_FMT_RGB565:\
  802. case PIX_FMT_BGR565:\
  803. {\
  804. const int dr1= dither_2x2_8[y&1 ][0];\
  805. const int dg1= dither_2x2_4[y&1 ][0];\
  806. const int db1= dither_2x2_8[(y&1)^1][0];\
  807. const int dr2= dither_2x2_8[y&1 ][1];\
  808. const int dg2= dither_2x2_4[y&1 ][1];\
  809. const int db2= dither_2x2_8[(y&1)^1][1];\
  810. func(uint16_t,0)\
  811. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  812. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  813. }\
  814. }\
  815. break;\
  816. case PIX_FMT_RGB555:\
  817. case PIX_FMT_BGR555:\
  818. {\
  819. const int dr1= dither_2x2_8[y&1 ][0];\
  820. const int dg1= dither_2x2_8[y&1 ][1];\
  821. const int db1= dither_2x2_8[(y&1)^1][0];\
  822. const int dr2= dither_2x2_8[y&1 ][1];\
  823. const int dg2= dither_2x2_8[y&1 ][0];\
  824. const int db2= dither_2x2_8[(y&1)^1][1];\
  825. func(uint16_t,0)\
  826. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  827. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  828. }\
  829. }\
  830. break;\
  831. case PIX_FMT_RGB8:\
  832. case PIX_FMT_BGR8:\
  833. {\
  834. const uint8_t * const d64= dither_8x8_73[y&7];\
  835. const uint8_t * const d32= dither_8x8_32[y&7];\
  836. func(uint8_t,0)\
  837. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  838. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  839. }\
  840. }\
  841. break;\
  842. case PIX_FMT_RGB4:\
  843. case PIX_FMT_BGR4:\
  844. {\
  845. const uint8_t * const d64= dither_8x8_73 [y&7];\
  846. const uint8_t * const d128=dither_8x8_220[y&7];\
  847. func(uint8_t,0)\
  848. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  849. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  850. }\
  851. }\
  852. break;\
  853. case PIX_FMT_RGB4_BYTE:\
  854. case PIX_FMT_BGR4_BYTE:\
  855. {\
  856. const uint8_t * const d64= dither_8x8_73 [y&7];\
  857. const uint8_t * const d128=dither_8x8_220[y&7];\
  858. func(uint8_t,0)\
  859. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  860. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  861. }\
  862. }\
  863. break;\
  864. case PIX_FMT_MONOBLACK:\
  865. case PIX_FMT_MONOWHITE:\
  866. {\
  867. func_monoblack\
  868. }\
  869. break;\
  870. case PIX_FMT_YUYV422:\
  871. func2\
  872. ((uint8_t*)dest)[2*i2+0]= Y1;\
  873. ((uint8_t*)dest)[2*i2+1]= U;\
  874. ((uint8_t*)dest)[2*i2+2]= Y2;\
  875. ((uint8_t*)dest)[2*i2+3]= V;\
  876. } \
  877. break;\
  878. case PIX_FMT_UYVY422:\
  879. func2\
  880. ((uint8_t*)dest)[2*i2+0]= U;\
  881. ((uint8_t*)dest)[2*i2+1]= Y1;\
  882. ((uint8_t*)dest)[2*i2+2]= V;\
  883. ((uint8_t*)dest)[2*i2+3]= Y2;\
  884. } \
  885. break;\
  886. case PIX_FMT_GRAY16BE:\
  887. func_g16\
  888. ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
  889. ((uint8_t*)dest)[2*i2+1]= Y1;\
  890. ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
  891. ((uint8_t*)dest)[2*i2+3]= Y2;\
  892. } \
  893. break;\
  894. case PIX_FMT_GRAY16LE:\
  895. func_g16\
  896. ((uint8_t*)dest)[2*i2+0]= Y1;\
  897. ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
  898. ((uint8_t*)dest)[2*i2+2]= Y2;\
  899. ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
  900. } \
  901. break;\
  902. }\
  903. static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  904. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  905. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  906. {
  907. int i;
  908. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
  909. }
  910. static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  911. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  912. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  913. {
  914. int i;
  915. int step= fmt_depth(c->dstFormat)/8;
  916. int aidx= 3;
  917. switch(c->dstFormat) {
  918. case PIX_FMT_ARGB:
  919. dest++;
  920. aidx= 0;
  921. case PIX_FMT_RGB24:
  922. aidx--;
  923. case PIX_FMT_RGBA:
  924. if (CONFIG_SMALL) {
  925. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  926. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  927. dest[aidx]= needAlpha ? A : 255;
  928. dest[0]= R>>22;
  929. dest[1]= G>>22;
  930. dest[2]= B>>22;
  931. dest+= step;
  932. }
  933. } else {
  934. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  935. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  936. dest[aidx]= A;
  937. dest[0]= R>>22;
  938. dest[1]= G>>22;
  939. dest[2]= B>>22;
  940. dest+= step;
  941. }
  942. } else {
  943. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  944. dest[aidx]= 255;
  945. dest[0]= R>>22;
  946. dest[1]= G>>22;
  947. dest[2]= B>>22;
  948. dest+= step;
  949. }
  950. }
  951. }
  952. break;
  953. case PIX_FMT_ABGR:
  954. dest++;
  955. aidx= 0;
  956. case PIX_FMT_BGR24:
  957. aidx--;
  958. case PIX_FMT_BGRA:
  959. if (CONFIG_SMALL) {
  960. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  961. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  962. dest[aidx]= needAlpha ? A : 255;
  963. dest[0]= B>>22;
  964. dest[1]= G>>22;
  965. dest[2]= R>>22;
  966. dest+= step;
  967. }
  968. } else {
  969. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  970. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  971. dest[aidx]= A;
  972. dest[0]= B>>22;
  973. dest[1]= G>>22;
  974. dest[2]= R>>22;
  975. dest+= step;
  976. }
  977. } else {
  978. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  979. dest[aidx]= 255;
  980. dest[0]= B>>22;
  981. dest[1]= G>>22;
  982. dest[2]= R>>22;
  983. dest+= step;
  984. }
  985. }
  986. }
  987. break;
  988. default:
  989. assert(0);
  990. }
  991. }
  992. static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
  993. {
  994. int i;
  995. uint8_t *ptr = plane + stride*y;
  996. for (i=0; i<height; i++) {
  997. memset(ptr, val, width);
  998. ptr += stride;
  999. }
  1000. }
  1001. static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width)
  1002. {
  1003. int i;
  1004. for (i = 0; i < width; i++) {
  1005. int r = src[i*6+0];
  1006. int g = src[i*6+2];
  1007. int b = src[i*6+4];
  1008. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1009. }
  1010. }
  1011. static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
  1012. uint8_t *src1, uint8_t *src2, int width)
  1013. {
  1014. int i;
  1015. assert(src1==src2);
  1016. for (i = 0; i < width; i++) {
  1017. int r = src1[6*i + 0];
  1018. int g = src1[6*i + 2];
  1019. int b = src1[6*i + 4];
  1020. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1021. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1022. }
  1023. }
  1024. static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  1025. uint8_t *src1, uint8_t *src2, int width)
  1026. {
  1027. int i;
  1028. assert(src1==src2);
  1029. for (i = 0; i < width; i++) {
  1030. int r= src1[12*i + 0] + src1[12*i + 6];
  1031. int g= src1[12*i + 2] + src1[12*i + 8];
  1032. int b= src1[12*i + 4] + src1[12*i + 10];
  1033. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  1034. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  1035. }
  1036. }
  1037. #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
  1038. static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
  1039. {\
  1040. int i;\
  1041. for (i=0; i<width; i++) {\
  1042. int b= (((const type*)src)[i]>>shb)&maskb;\
  1043. int g= (((const type*)src)[i]>>shg)&maskg;\
  1044. int r= (((const type*)src)[i]>>shr)&maskr;\
  1045. \
  1046. dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
  1047. }\
  1048. }
  1049. BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  1050. BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  1051. BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
  1052. BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
  1053. BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
  1054. BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
  1055. static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1056. {
  1057. int i;
  1058. for (i=0; i<width; i++) {
  1059. dst[i]= src[4*i];
  1060. }
  1061. }
  1062. #define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
  1063. static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  1064. {\
  1065. int i;\
  1066. for (i=0; i<width; i++) {\
  1067. int b= (((const type*)src)[i]&maskb)>>shb;\
  1068. int g= (((const type*)src)[i]&maskg)>>shg;\
  1069. int r= (((const type*)src)[i]&maskr)>>shr;\
  1070. \
  1071. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
  1072. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
  1073. }\
  1074. }\
  1075. static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  1076. {\
  1077. int i;\
  1078. for (i=0; i<width; i++) {\
  1079. int pix0= ((const type*)src)[2*i+0];\
  1080. int pix1= ((const type*)src)[2*i+1];\
  1081. int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
  1082. int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
  1083. int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
  1084. g&= maskg|(2*maskg);\
  1085. \
  1086. g>>=shg;\
  1087. \
  1088. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
  1089. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
  1090. }\
  1091. }
  1092. BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1093. BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1094. BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
  1095. BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
  1096. BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
  1097. BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
  1098. static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
  1099. {
  1100. int i;
  1101. for (i=0; i<width; i++) {
  1102. int d= src[i];
  1103. dst[i]= pal[d] & 0xFF;
  1104. }
  1105. }
  1106. static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
  1107. const uint8_t *src1, const uint8_t *src2,
  1108. long width, uint32_t *pal)
  1109. {
  1110. int i;
  1111. assert(src1 == src2);
  1112. for (i=0; i<width; i++) {
  1113. int p= pal[src1[i]];
  1114. dstU[i]= p>>8;
  1115. dstV[i]= p>>16;
  1116. }
  1117. }
  1118. static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1119. {
  1120. int i, j;
  1121. for (i=0; i<width/8; i++) {
  1122. int d= ~src[i];
  1123. for(j=0; j<8; j++)
  1124. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1125. }
  1126. }
  1127. static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1128. {
  1129. int i, j;
  1130. for (i=0; i<width/8; i++) {
  1131. int d= src[i];
  1132. for(j=0; j<8; j++)
  1133. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1134. }
  1135. }
  1136. //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
  1137. //Plain C versions
  1138. #if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
  1139. #define COMPILE_C
  1140. #endif
  1141. #if ARCH_PPC
  1142. #if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
  1143. #define COMPILE_ALTIVEC
  1144. #endif
  1145. #endif //ARCH_PPC
  1146. #if ARCH_X86
  1147. #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
  1148. #define COMPILE_MMX
  1149. #endif
  1150. #if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
  1151. #define COMPILE_MMX2
  1152. #endif
  1153. #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
  1154. #define COMPILE_3DNOW
  1155. #endif
  1156. #endif //ARCH_X86
  1157. #define COMPILE_TEMPLATE_MMX 0
  1158. #define COMPILE_TEMPLATE_MMX2 0
  1159. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1160. #define COMPILE_TEMPLATE_ALTIVEC 0
  1161. #ifdef COMPILE_C
  1162. #define RENAME(a) a ## _C
  1163. #include "swscale_template.c"
  1164. #endif
  1165. #ifdef COMPILE_ALTIVEC
  1166. #undef RENAME
  1167. #undef COMPILE_TEMPLATE_ALTIVEC
  1168. #define COMPILE_TEMPLATE_ALTIVEC 1
  1169. #define RENAME(a) a ## _altivec
  1170. #include "swscale_template.c"
  1171. #endif
  1172. #if ARCH_X86
  1173. //MMX versions
  1174. #ifdef COMPILE_MMX
  1175. #undef RENAME
  1176. #undef COMPILE_TEMPLATE_MMX
  1177. #undef COMPILE_TEMPLATE_MMX2
  1178. #undef COMPILE_TEMPLATE_AMD3DNOW
  1179. #define COMPILE_TEMPLATE_MMX 1
  1180. #define COMPILE_TEMPLATE_MMX2 0
  1181. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1182. #define RENAME(a) a ## _MMX
  1183. #include "swscale_template.c"
  1184. #endif
  1185. //MMX2 versions
  1186. #ifdef COMPILE_MMX2
  1187. #undef RENAME
  1188. #undef COMPILE_TEMPLATE_MMX
  1189. #undef COMPILE_TEMPLATE_MMX2
  1190. #undef COMPILE_TEMPLATE_AMD3DNOW
  1191. #define COMPILE_TEMPLATE_MMX 1
  1192. #define COMPILE_TEMPLATE_MMX2 1
  1193. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1194. #define RENAME(a) a ## _MMX2
  1195. #include "swscale_template.c"
  1196. #endif
  1197. //3DNOW versions
  1198. #ifdef COMPILE_3DNOW
  1199. #undef RENAME
  1200. #undef COMPILE_TEMPLATE_MMX
  1201. #undef COMPILE_TEMPLATE_MMX2
  1202. #undef COMPILE_TEMPLATE_AMD3DNOW
  1203. #define COMPILE_TEMPLATE_MMX 1
  1204. #define COMPILE_TEMPLATE_MMX2 0
  1205. #define COMPILE_TEMPLATE_AMD3DNOW 1
  1206. #define RENAME(a) a ## _3DNow
  1207. #include "swscale_template.c"
  1208. #endif
  1209. #endif //ARCH_X86
  1210. static double getSplineCoeff(double a, double b, double c, double d, double dist)
  1211. {
  1212. // printf("%f %f %f %f %f\n", a,b,c,d,dist);
  1213. if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
  1214. else return getSplineCoeff( 0.0,
  1215. b+ 2.0*c + 3.0*d,
  1216. c + 3.0*d,
  1217. -b- 3.0*c - 6.0*d,
  1218. dist-1.0);
  1219. }
  1220. static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  1221. int srcW, int dstW, int filterAlign, int one, int flags,
  1222. SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
  1223. {
  1224. int i;
  1225. int filterSize;
  1226. int filter2Size;
  1227. int minFilterSize;
  1228. int64_t *filter=NULL;
  1229. int64_t *filter2=NULL;
  1230. const int64_t fone= 1LL<<54;
  1231. int ret= -1;
  1232. #if ARCH_X86
  1233. if (flags & SWS_CPU_CAPS_MMX)
  1234. __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
  1235. #endif
  1236. // NOTE: the +1 is for the MMX scaler which reads over the end
  1237. FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW+1)*sizeof(int16_t), fail);
  1238. if (FFABS(xInc - 0x10000) <10) { // unscaled
  1239. int i;
  1240. filterSize= 1;
  1241. FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
  1242. for (i=0; i<dstW; i++) {
  1243. filter[i*filterSize]= fone;
  1244. (*filterPos)[i]=i;
  1245. }
  1246. } else if (flags&SWS_POINT) { // lame looking point sampling mode
  1247. int i;
  1248. int xDstInSrc;
  1249. filterSize= 1;
  1250. FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
  1251. xDstInSrc= xInc/2 - 0x8000;
  1252. for (i=0; i<dstW; i++) {
  1253. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  1254. (*filterPos)[i]= xx;
  1255. filter[i]= fone;
  1256. xDstInSrc+= xInc;
  1257. }
  1258. } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
  1259. int i;
  1260. int xDstInSrc;
  1261. filterSize= 2;
  1262. FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
  1263. xDstInSrc= xInc/2 - 0x8000;
  1264. for (i=0; i<dstW; i++) {
  1265. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  1266. int j;
  1267. (*filterPos)[i]= xx;
  1268. //bilinear upscale / linear interpolate / area averaging
  1269. for (j=0; j<filterSize; j++) {
  1270. int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
  1271. if (coeff<0) coeff=0;
  1272. filter[i*filterSize + j]= coeff;
  1273. xx++;
  1274. }
  1275. xDstInSrc+= xInc;
  1276. }
  1277. } else {
  1278. int xDstInSrc;
  1279. int sizeFactor;
  1280. if (flags&SWS_BICUBIC) sizeFactor= 4;
  1281. else if (flags&SWS_X) sizeFactor= 8;
  1282. else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
  1283. else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
  1284. else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
  1285. else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
  1286. else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
  1287. else if (flags&SWS_BILINEAR) sizeFactor= 2;
  1288. else {
  1289. sizeFactor= 0; //GCC warning killer
  1290. assert(0);
  1291. }
  1292. if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
  1293. else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
  1294. if (filterSize > srcW-2) filterSize=srcW-2;
  1295. FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
  1296. xDstInSrc= xInc - 0x10000;
  1297. for (i=0; i<dstW; i++) {
  1298. int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
  1299. int j;
  1300. (*filterPos)[i]= xx;
  1301. for (j=0; j<filterSize; j++) {
  1302. int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
  1303. double floatd;
  1304. int64_t coeff;
  1305. if (xInc > 1<<16)
  1306. d= d*dstW/srcW;
  1307. floatd= d * (1.0/(1<<30));
  1308. if (flags & SWS_BICUBIC) {
  1309. int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
  1310. int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
  1311. int64_t dd = ( d*d)>>30;
  1312. int64_t ddd= (dd*d)>>30;
  1313. if (d < 1LL<<30)
  1314. coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
  1315. else if (d < 1LL<<31)
  1316. coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
  1317. else
  1318. coeff=0.0;
  1319. coeff *= fone>>(30+24);
  1320. }
  1321. /* else if (flags & SWS_X) {
  1322. double p= param ? param*0.01 : 0.3;
  1323. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  1324. coeff*= pow(2.0, - p*d*d);
  1325. }*/
  1326. else if (flags & SWS_X) {
  1327. double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
  1328. double c;
  1329. if (floatd<1.0)
  1330. c = cos(floatd*PI);
  1331. else
  1332. c=-1.0;
  1333. if (c<0.0) c= -pow(-c, A);
  1334. else c= pow( c, A);
  1335. coeff= (c*0.5 + 0.5)*fone;
  1336. } else if (flags & SWS_AREA) {
  1337. int64_t d2= d - (1<<29);
  1338. if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
  1339. else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
  1340. else coeff=0.0;
  1341. coeff *= fone>>(30+16);
  1342. } else if (flags & SWS_GAUSS) {
  1343. double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  1344. coeff = (pow(2.0, - p*floatd*floatd))*fone;
  1345. } else if (flags & SWS_SINC) {
  1346. coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
  1347. } else if (flags & SWS_LANCZOS) {
  1348. double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  1349. coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
  1350. if (floatd>p) coeff=0;
  1351. } else if (flags & SWS_BILINEAR) {
  1352. coeff= (1<<30) - d;
  1353. if (coeff<0) coeff=0;
  1354. coeff *= fone >> 30;
  1355. } else if (flags & SWS_SPLINE) {
  1356. double p=-2.196152422706632;
  1357. coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
  1358. } else {
  1359. coeff= 0.0; //GCC warning killer
  1360. assert(0);
  1361. }
  1362. filter[i*filterSize + j]= coeff;
  1363. xx++;
  1364. }
  1365. xDstInSrc+= 2*xInc;
  1366. }
  1367. }
  1368. /* apply src & dst Filter to filter -> filter2
  1369. av_free(filter);
  1370. */
  1371. assert(filterSize>0);
  1372. filter2Size= filterSize;
  1373. if (srcFilter) filter2Size+= srcFilter->length - 1;
  1374. if (dstFilter) filter2Size+= dstFilter->length - 1;
  1375. assert(filter2Size>0);
  1376. FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);
  1377. for (i=0; i<dstW; i++) {
  1378. int j, k;
  1379. if(srcFilter) {
  1380. for (k=0; k<srcFilter->length; k++) {
  1381. for (j=0; j<filterSize; j++)
  1382. filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
  1383. }
  1384. } else {
  1385. for (j=0; j<filterSize; j++)
  1386. filter2[i*filter2Size + j]= filter[i*filterSize + j];
  1387. }
  1388. //FIXME dstFilter
  1389. (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
  1390. }
  1391. av_freep(&filter);
  1392. /* try to reduce the filter-size (step1 find size and shift left) */
  1393. // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
  1394. minFilterSize= 0;
  1395. for (i=dstW-1; i>=0; i--) {
  1396. int min= filter2Size;
  1397. int j;
  1398. int64_t cutOff=0.0;
  1399. /* get rid off near zero elements on the left by shifting left */
  1400. for (j=0; j<filter2Size; j++) {
  1401. int k;
  1402. cutOff += FFABS(filter2[i*filter2Size]);
  1403. if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
  1404. /* preserve monotonicity because the core can't handle the filter otherwise */
  1405. if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
  1406. // move filter coefficients left
  1407. for (k=1; k<filter2Size; k++)
  1408. filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
  1409. filter2[i*filter2Size + k - 1]= 0;
  1410. (*filterPos)[i]++;
  1411. }
  1412. cutOff=0;
  1413. /* count near zeros on the right */
  1414. for (j=filter2Size-1; j>0; j--) {
  1415. cutOff += FFABS(filter2[i*filter2Size + j]);
  1416. if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
  1417. min--;
  1418. }
  1419. if (min>minFilterSize) minFilterSize= min;
  1420. }
  1421. if (flags & SWS_CPU_CAPS_ALTIVEC) {
  1422. // we can handle the special case 4,
  1423. // so we don't want to go to the full 8
  1424. if (minFilterSize < 5)
  1425. filterAlign = 4;
  1426. // We really don't want to waste our time
  1427. // doing useless computation, so fall back on
  1428. // the scalar C code for very small filters.
  1429. // Vectorizing is worth it only if you have a
  1430. // decent-sized vector.
  1431. if (minFilterSize < 3)
  1432. filterAlign = 1;
  1433. }
  1434. if (flags & SWS_CPU_CAPS_MMX) {
  1435. // special case for unscaled vertical filtering
  1436. if (minFilterSize == 1 && filterAlign == 2)
  1437. filterAlign= 1;
  1438. }
  1439. assert(minFilterSize > 0);
  1440. filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
  1441. assert(filterSize > 0);
  1442. filter= av_malloc(filterSize*dstW*sizeof(*filter));
  1443. if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
  1444. goto fail;
  1445. *outFilterSize= filterSize;
  1446. if (flags&SWS_PRINT_INFO)
  1447. av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
  1448. /* try to reduce the filter-size (step2 reduce it) */
  1449. for (i=0; i<dstW; i++) {
  1450. int j;
  1451. for (j=0; j<filterSize; j++) {
  1452. if (j>=filter2Size) filter[i*filterSize + j]= 0;
  1453. else filter[i*filterSize + j]= filter2[i*filter2Size + j];
  1454. if((flags & SWS_BITEXACT) && j>=minFilterSize)
  1455. filter[i*filterSize + j]= 0;
  1456. }
  1457. }
  1458. //FIXME try to align filterPos if possible
  1459. //fix borders
  1460. for (i=0; i<dstW; i++) {
  1461. int j;
  1462. if ((*filterPos)[i] < 0) {
  1463. // move filter coefficients left to compensate for filterPos
  1464. for (j=1; j<filterSize; j++) {
  1465. int left= FFMAX(j + (*filterPos)[i], 0);
  1466. filter[i*filterSize + left] += filter[i*filterSize + j];
  1467. filter[i*filterSize + j]=0;
  1468. }
  1469. (*filterPos)[i]= 0;
  1470. }
  1471. if ((*filterPos)[i] + filterSize > srcW) {
  1472. int shift= (*filterPos)[i] + filterSize - srcW;
  1473. // move filter coefficients right to compensate for filterPos
  1474. for (j=filterSize-2; j>=0; j--) {
  1475. int right= FFMIN(j + shift, filterSize-1);
  1476. filter[i*filterSize +right] += filter[i*filterSize +j];
  1477. filter[i*filterSize +j]=0;
  1478. }
  1479. (*filterPos)[i]= srcW - filterSize;
  1480. }
  1481. }
  1482. // Note the +1 is for the MMX scaler which reads over the end
  1483. /* align at 16 for AltiVec (needed by hScale_altivec_real) */
  1484. FF_ALLOCZ_OR_GOTO(NULL, *outFilter, *outFilterSize*(dstW+1)*sizeof(int16_t), fail);
  1485. /* normalize & store in outFilter */
  1486. for (i=0; i<dstW; i++) {
  1487. int j;
  1488. int64_t error=0;
  1489. int64_t sum=0;
  1490. for (j=0; j<filterSize; j++) {
  1491. sum+= filter[i*filterSize + j];
  1492. }
  1493. sum= (sum + one/2)/ one;
  1494. for (j=0; j<*outFilterSize; j++) {
  1495. int64_t v= filter[i*filterSize + j] + error;
  1496. int intV= ROUNDED_DIV(v, sum);
  1497. (*outFilter)[i*(*outFilterSize) + j]= intV;
  1498. error= v - intV*sum;
  1499. }
  1500. }
  1501. (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
  1502. for (i=0; i<*outFilterSize; i++) {
  1503. int j= dstW*(*outFilterSize);
  1504. (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
  1505. }
  1506. ret=0;
  1507. fail:
  1508. av_free(filter);
  1509. av_free(filter2);
  1510. return ret;
  1511. }
  1512. #ifdef COMPILE_MMX2
  1513. static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
  1514. {
  1515. uint8_t *fragmentA;
  1516. x86_reg imm8OfPShufW1A;
  1517. x86_reg imm8OfPShufW2A;
  1518. x86_reg fragmentLengthA;
  1519. uint8_t *fragmentB;
  1520. x86_reg imm8OfPShufW1B;
  1521. x86_reg imm8OfPShufW2B;
  1522. x86_reg fragmentLengthB;
  1523. int fragmentPos;
  1524. int xpos, i;
  1525. // create an optimized horizontal scaling routine
  1526. /* This scaler is made of runtime-generated MMX2 code using specially
  1527. * tuned pshufw instructions. For every four output pixels, if four
  1528. * input pixels are enough for the fast bilinear scaling, then a chunk
  1529. * of fragmentB is used. If five input pixels are needed, then a chunk
  1530. * of fragmentA is used.
  1531. */
  1532. //code fragment
  1533. __asm__ volatile(
  1534. "jmp 9f \n\t"
  1535. // Begin
  1536. "0: \n\t"
  1537. "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
  1538. "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
  1539. "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
  1540. "punpcklbw %%mm7, %%mm1 \n\t"
  1541. "punpcklbw %%mm7, %%mm0 \n\t"
  1542. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  1543. "1: \n\t"
  1544. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1545. "2: \n\t"
  1546. "psubw %%mm1, %%mm0 \n\t"
  1547. "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
  1548. "pmullw %%mm3, %%mm0 \n\t"
  1549. "psllw $7, %%mm1 \n\t"
  1550. "paddw %%mm1, %%mm0 \n\t"
  1551. "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
  1552. "add $8, %%"REG_a" \n\t"
  1553. // End
  1554. "9: \n\t"
  1555. // "int $3 \n\t"
  1556. "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
  1557. "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
  1558. "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
  1559. "dec %1 \n\t"
  1560. "dec %2 \n\t"
  1561. "sub %0, %1 \n\t"
  1562. "sub %0, %2 \n\t"
  1563. "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
  1564. "sub %0, %3 \n\t"
  1565. :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  1566. "=r" (fragmentLengthA)
  1567. );
  1568. __asm__ volatile(
  1569. "jmp 9f \n\t"
  1570. // Begin
  1571. "0: \n\t"
  1572. "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
  1573. "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
  1574. "punpcklbw %%mm7, %%mm0 \n\t"
  1575. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  1576. "1: \n\t"
  1577. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1578. "2: \n\t"
  1579. "psubw %%mm1, %%mm0 \n\t"
  1580. "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
  1581. "pmullw %%mm3, %%mm0 \n\t"
  1582. "psllw $7, %%mm1 \n\t"
  1583. "paddw %%mm1, %%mm0 \n\t"
  1584. "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
  1585. "add $8, %%"REG_a" \n\t"
  1586. // End
  1587. "9: \n\t"
  1588. // "int $3 \n\t"
  1589. "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
  1590. "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
  1591. "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
  1592. "dec %1 \n\t"
  1593. "dec %2 \n\t"
  1594. "sub %0, %1 \n\t"
  1595. "sub %0, %2 \n\t"
  1596. "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
  1597. "sub %0, %3 \n\t"
  1598. :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  1599. "=r" (fragmentLengthB)
  1600. );
  1601. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  1602. fragmentPos=0;
  1603. for (i=0; i<dstW/numSplits; i++) {
  1604. int xx=xpos>>16;
  1605. if ((i&3) == 0) {
  1606. int a=0;
  1607. int b=((xpos+xInc)>>16) - xx;
  1608. int c=((xpos+xInc*2)>>16) - xx;
  1609. int d=((xpos+xInc*3)>>16) - xx;
  1610. int inc = (d+1<4);
  1611. uint8_t *fragment = (d+1<4) ? fragmentB : fragmentA;
  1612. x86_reg imm8OfPShufW1 = (d+1<4) ? imm8OfPShufW1B : imm8OfPShufW1A;
  1613. x86_reg imm8OfPShufW2 = (d+1<4) ? imm8OfPShufW2B : imm8OfPShufW2A;
  1614. x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
  1615. int maxShift= 3-(d+inc);
  1616. int shift=0;
  1617. if (filterCode) {
  1618. filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
  1619. filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
  1620. filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
  1621. filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
  1622. filterPos[i/2]= xx;
  1623. memcpy(filterCode + fragmentPos, fragment, fragmentLength);
  1624. filterCode[fragmentPos + imm8OfPShufW1]=
  1625. (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
  1626. filterCode[fragmentPos + imm8OfPShufW2]=
  1627. a | (b<<2) | (c<<4) | (d<<6);
  1628. if (i+4-inc>=dstW) shift=maxShift; //avoid overread
  1629. else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
  1630. if (shift && i>=shift) {
  1631. filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
  1632. filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
  1633. filterPos[i/2]-=shift;
  1634. }
  1635. }
  1636. fragmentPos+= fragmentLength;
  1637. if (filterCode)
  1638. filterCode[fragmentPos]= RET;
  1639. }
  1640. xpos+=xInc;
  1641. }
  1642. if (filterCode)
  1643. filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
  1644. return fragmentPos + 1;
  1645. }
  1646. #endif /* COMPILE_MMX2 */
  1647. static void globalInit(void)
  1648. {
  1649. // generating tables:
  1650. int i;
  1651. for (i=0; i<768; i++) {
  1652. int c= av_clip_uint8(i-256);
  1653. clip_table[i]=c;
  1654. }
  1655. }
  1656. static SwsFunc getSwsFunc(SwsContext *c)
  1657. {
  1658. #if CONFIG_RUNTIME_CPUDETECT
  1659. int flags = c->flags;
  1660. #if ARCH_X86 && CONFIG_GPL
  1661. // ordered per speed fastest first
  1662. if (flags & SWS_CPU_CAPS_MMX2) {
  1663. sws_init_swScale_MMX2(c);
  1664. return swScale_MMX2;
  1665. } else if (flags & SWS_CPU_CAPS_3DNOW) {
  1666. sws_init_swScale_3DNow(c);
  1667. return swScale_3DNow;
  1668. } else if (flags & SWS_CPU_CAPS_MMX) {
  1669. sws_init_swScale_MMX(c);
  1670. return swScale_MMX;
  1671. } else {
  1672. sws_init_swScale_C(c);
  1673. return swScale_C;
  1674. }
  1675. #else
  1676. #if ARCH_PPC
  1677. if (flags & SWS_CPU_CAPS_ALTIVEC) {
  1678. sws_init_swScale_altivec(c);
  1679. return swScale_altivec;
  1680. } else {
  1681. sws_init_swScale_C(c);
  1682. return swScale_C;
  1683. }
  1684. #endif
  1685. sws_init_swScale_C(c);
  1686. return swScale_C;
  1687. #endif /* ARCH_X86 && CONFIG_GPL */
  1688. #else //CONFIG_RUNTIME_CPUDETECT
  1689. #if COMPILE_TEMPLATE_MMX2
  1690. sws_init_swScale_MMX2(c);
  1691. return swScale_MMX2;
  1692. #elif COMPILE_TEMPLATE_AMD3DNOW
  1693. sws_init_swScale_3DNow(c);
  1694. return swScale_3DNow;
  1695. #elif COMPILE_TEMPLATE_MMX
  1696. sws_init_swScale_MMX(c);
  1697. return swScale_MMX;
  1698. #elif COMPILE_TEMPLATE_ALTIVEC
  1699. sws_init_swScale_altivec(c);
  1700. return swScale_altivec;
  1701. #else
  1702. sws_init_swScale_C(c);
  1703. return swScale_C;
  1704. #endif
  1705. #endif //!CONFIG_RUNTIME_CPUDETECT
  1706. }
  1707. static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1708. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1709. {
  1710. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1711. /* Copy Y plane */
  1712. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1713. memcpy(dst, src[0], srcSliceH*dstStride[0]);
  1714. else {
  1715. int i;
  1716. const uint8_t *srcPtr= src[0];
  1717. uint8_t *dstPtr= dst;
  1718. for (i=0; i<srcSliceH; i++) {
  1719. memcpy(dstPtr, srcPtr, c->srcW);
  1720. srcPtr+= srcStride[0];
  1721. dstPtr+= dstStride[0];
  1722. }
  1723. }
  1724. dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1725. if (c->dstFormat == PIX_FMT_NV12)
  1726. interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
  1727. else
  1728. interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
  1729. return srcSliceH;
  1730. }
  1731. static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1732. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1733. {
  1734. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1735. yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1736. return srcSliceH;
  1737. }
  1738. static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1739. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1740. {
  1741. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1742. yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1743. return srcSliceH;
  1744. }
  1745. static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1746. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1747. {
  1748. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1749. yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1750. return srcSliceH;
  1751. }
  1752. static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1753. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1754. {
  1755. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1756. yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1757. return srcSliceH;
  1758. }
  1759. static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1760. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1761. {
  1762. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1763. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1764. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1765. yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1766. if (dstParam[3])
  1767. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1768. return srcSliceH;
  1769. }
  1770. static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1771. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1772. {
  1773. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1774. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1775. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1776. yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1777. return srcSliceH;
  1778. }
  1779. static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1780. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1781. {
  1782. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1783. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1784. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1785. uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1786. if (dstParam[3])
  1787. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1788. return srcSliceH;
  1789. }
  1790. static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1791. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1792. {
  1793. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1794. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1795. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1796. uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1797. return srcSliceH;
  1798. }
  1799. static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1800. int srcSliceH, uint8_t* dst[], int dstStride[])
  1801. {
  1802. const enum PixelFormat srcFormat= c->srcFormat;
  1803. const enum PixelFormat dstFormat= c->dstFormat;
  1804. void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
  1805. const uint8_t *palette)=NULL;
  1806. int i;
  1807. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1808. uint8_t *srcPtr= src[0];
  1809. if (!usePal(srcFormat))
  1810. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1811. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1812. switch(dstFormat) {
  1813. case PIX_FMT_RGB32 : conv = palette8topacked32; break;
  1814. case PIX_FMT_BGR32 : conv = palette8topacked32; break;
  1815. case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
  1816. case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
  1817. case PIX_FMT_RGB24 : conv = palette8topacked24; break;
  1818. case PIX_FMT_BGR24 : conv = palette8topacked24; break;
  1819. default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1820. sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
  1821. }
  1822. for (i=0; i<srcSliceH; i++) {
  1823. conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
  1824. srcPtr+= srcStride[0];
  1825. dstPtr+= dstStride[0];
  1826. }
  1827. return srcSliceH;
  1828. }
  1829. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  1830. static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1831. int srcSliceH, uint8_t* dst[], int dstStride[])
  1832. {
  1833. const enum PixelFormat srcFormat= c->srcFormat;
  1834. const enum PixelFormat dstFormat= c->dstFormat;
  1835. const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
  1836. const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
  1837. const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  1838. const int dstId= fmt_depth(dstFormat) >> 2;
  1839. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1840. /* BGR -> BGR */
  1841. if ( (isBGR(srcFormat) && isBGR(dstFormat))
  1842. || (isRGB(srcFormat) && isRGB(dstFormat))) {
  1843. switch(srcId | (dstId<<4)) {
  1844. case 0x34: conv= rgb16to15; break;
  1845. case 0x36: conv= rgb24to15; break;
  1846. case 0x38: conv= rgb32to15; break;
  1847. case 0x43: conv= rgb15to16; break;
  1848. case 0x46: conv= rgb24to16; break;
  1849. case 0x48: conv= rgb32to16; break;
  1850. case 0x63: conv= rgb15to24; break;
  1851. case 0x64: conv= rgb16to24; break;
  1852. case 0x68: conv= rgb32to24; break;
  1853. case 0x83: conv= rgb15to32; break;
  1854. case 0x84: conv= rgb16to32; break;
  1855. case 0x86: conv= rgb24to32; break;
  1856. default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1857. sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
  1858. }
  1859. } else if ( (isBGR(srcFormat) && isRGB(dstFormat))
  1860. || (isRGB(srcFormat) && isBGR(dstFormat))) {
  1861. switch(srcId | (dstId<<4)) {
  1862. case 0x33: conv= rgb15tobgr15; break;
  1863. case 0x34: conv= rgb16tobgr15; break;
  1864. case 0x36: conv= rgb24tobgr15; break;
  1865. case 0x38: conv= rgb32tobgr15; break;
  1866. case 0x43: conv= rgb15tobgr16; break;
  1867. case 0x44: conv= rgb16tobgr16; break;
  1868. case 0x46: conv= rgb24tobgr16; break;
  1869. case 0x48: conv= rgb32tobgr16; break;
  1870. case 0x63: conv= rgb15tobgr24; break;
  1871. case 0x64: conv= rgb16tobgr24; break;
  1872. case 0x66: conv= rgb24tobgr24; break;
  1873. case 0x68: conv= rgb32tobgr24; break;
  1874. case 0x83: conv= rgb15tobgr32; break;
  1875. case 0x84: conv= rgb16tobgr32; break;
  1876. case 0x86: conv= rgb24tobgr32; break;
  1877. case 0x88: conv= rgb32tobgr32; break;
  1878. default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1879. sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
  1880. }
  1881. } else {
  1882. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1883. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1884. }
  1885. if(conv) {
  1886. uint8_t *srcPtr= src[0];
  1887. if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
  1888. srcPtr += ALT32_CORR;
  1889. if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
  1890. conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1891. else {
  1892. int i;
  1893. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1894. for (i=0; i<srcSliceH; i++) {
  1895. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1896. srcPtr+= srcStride[0];
  1897. dstPtr+= dstStride[0];
  1898. }
  1899. }
  1900. }
  1901. return srcSliceH;
  1902. }
  1903. static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1904. int srcSliceH, uint8_t* dst[], int dstStride[])
  1905. {
  1906. rgb24toyv12(
  1907. src[0],
  1908. dst[0]+ srcSliceY *dstStride[0],
  1909. dst[1]+(srcSliceY>>1)*dstStride[1],
  1910. dst[2]+(srcSliceY>>1)*dstStride[2],
  1911. c->srcW, srcSliceH,
  1912. dstStride[0], dstStride[1], srcStride[0]);
  1913. if (dst[3])
  1914. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1915. return srcSliceH;
  1916. }
  1917. static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1918. int srcSliceH, uint8_t* dst[], int dstStride[])
  1919. {
  1920. int i;
  1921. /* copy Y */
  1922. if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
  1923. memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
  1924. else {
  1925. uint8_t *srcPtr= src[0];
  1926. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1927. for (i=0; i<srcSliceH; i++) {
  1928. memcpy(dstPtr, srcPtr, c->srcW);
  1929. srcPtr+= srcStride[0];
  1930. dstPtr+= dstStride[0];
  1931. }
  1932. }
  1933. if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P) {
  1934. planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1935. srcSliceH >> 2, srcStride[1], dstStride[1]);
  1936. planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1937. srcSliceH >> 2, srcStride[2], dstStride[2]);
  1938. } else {
  1939. planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1940. srcSliceH >> 2, srcStride[1], dstStride[2]);
  1941. planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1942. srcSliceH >> 2, srcStride[2], dstStride[1]);
  1943. }
  1944. if (dst[3])
  1945. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1946. return srcSliceH;
  1947. }
  1948. /* unscaled copy like stuff (assumes nearly identical formats) */
  1949. static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1950. int srcSliceH, uint8_t* dst[], int dstStride[])
  1951. {
  1952. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1953. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1954. else {
  1955. int i;
  1956. uint8_t *srcPtr= src[0];
  1957. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1958. int length=0;
  1959. /* universal length finder */
  1960. while(length+c->srcW <= FFABS(dstStride[0])
  1961. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  1962. assert(length!=0);
  1963. for (i=0; i<srcSliceH; i++) {
  1964. memcpy(dstPtr, srcPtr, length);
  1965. srcPtr+= srcStride[0];
  1966. dstPtr+= dstStride[0];
  1967. }
  1968. }
  1969. return srcSliceH;
  1970. }
  1971. static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1972. int srcSliceH, uint8_t* dst[], int dstStride[])
  1973. {
  1974. int plane, i, j;
  1975. for (plane=0; plane<4; plane++) {
  1976. int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1977. int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1978. int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1979. uint8_t *srcPtr= src[plane];
  1980. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1981. if (!dst[plane]) continue;
  1982. // ignore palette for GRAY8
  1983. if (plane == 1 && !dst[2]) continue;
  1984. if (!src[plane] || (plane == 1 && !src[2])) {
  1985. if(is16BPS(c->dstFormat))
  1986. length*=2;
  1987. fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
  1988. } else {
  1989. if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
  1990. if (!isBE(c->srcFormat)) srcPtr++;
  1991. for (i=0; i<height; i++) {
  1992. for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
  1993. srcPtr+= srcStride[plane];
  1994. dstPtr+= dstStride[plane];
  1995. }
  1996. } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
  1997. for (i=0; i<height; i++) {
  1998. for (j=0; j<length; j++) {
  1999. dstPtr[ j<<1 ] = srcPtr[j];
  2000. dstPtr[(j<<1)+1] = srcPtr[j];
  2001. }
  2002. srcPtr+= srcStride[plane];
  2003. dstPtr+= dstStride[plane];
  2004. }
  2005. } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
  2006. && isBE(c->srcFormat) != isBE(c->dstFormat)) {
  2007. for (i=0; i<height; i++) {
  2008. for (j=0; j<length; j++)
  2009. ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
  2010. srcPtr+= srcStride[plane];
  2011. dstPtr+= dstStride[plane];
  2012. }
  2013. } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
  2014. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  2015. else {
  2016. if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  2017. length*=2;
  2018. for (i=0; i<height; i++) {
  2019. memcpy(dstPtr, srcPtr, length);
  2020. srcPtr+= srcStride[plane];
  2021. dstPtr+= dstStride[plane];
  2022. }
  2023. }
  2024. }
  2025. }
  2026. return srcSliceH;
  2027. }
  2028. static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
  2029. {
  2030. *h = av_pix_fmt_descriptors[format].log2_chroma_w;
  2031. *v = av_pix_fmt_descriptors[format].log2_chroma_h;
  2032. }
  2033. static uint16_t roundToInt16(int64_t f)
  2034. {
  2035. int r= (f + (1<<15))>>16;
  2036. if (r<-0x7FFF) return 0x8000;
  2037. else if (r> 0x7FFF) return 0x7FFF;
  2038. else return r;
  2039. }
  2040. int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
  2041. {
  2042. int64_t crv = inv_table[0];
  2043. int64_t cbu = inv_table[1];
  2044. int64_t cgu = -inv_table[2];
  2045. int64_t cgv = -inv_table[3];
  2046. int64_t cy = 1<<16;
  2047. int64_t oy = 0;
  2048. memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
  2049. memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
  2050. c->brightness= brightness;
  2051. c->contrast = contrast;
  2052. c->saturation= saturation;
  2053. c->srcRange = srcRange;
  2054. c->dstRange = dstRange;
  2055. if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  2056. c->uOffset= 0x0400040004000400LL;
  2057. c->vOffset= 0x0400040004000400LL;
  2058. if (!srcRange) {
  2059. cy= (cy*255) / 219;
  2060. oy= 16<<16;
  2061. } else {
  2062. crv= (crv*224) / 255;
  2063. cbu= (cbu*224) / 255;
  2064. cgu= (cgu*224) / 255;
  2065. cgv= (cgv*224) / 255;
  2066. }
  2067. cy = (cy *contrast )>>16;
  2068. crv= (crv*contrast * saturation)>>32;
  2069. cbu= (cbu*contrast * saturation)>>32;
  2070. cgu= (cgu*contrast * saturation)>>32;
  2071. cgv= (cgv*contrast * saturation)>>32;
  2072. oy -= 256*brightness;
  2073. c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
  2074. c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
  2075. c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
  2076. c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
  2077. c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
  2078. c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
  2079. c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
  2080. c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
  2081. c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
  2082. c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
  2083. c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
  2084. c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
  2085. ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
  2086. //FIXME factorize
  2087. #ifdef COMPILE_ALTIVEC
  2088. if (c->flags & SWS_CPU_CAPS_ALTIVEC)
  2089. ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
  2090. #endif
  2091. return 0;
  2092. }
  2093. int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
  2094. {
  2095. if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  2096. *inv_table = c->srcColorspaceTable;
  2097. *table = c->dstColorspaceTable;
  2098. *srcRange = c->srcRange;
  2099. *dstRange = c->dstRange;
  2100. *brightness= c->brightness;
  2101. *contrast = c->contrast;
  2102. *saturation= c->saturation;
  2103. return 0;
  2104. }
  2105. static int handle_jpeg(enum PixelFormat *format)
  2106. {
  2107. switch (*format) {
  2108. case PIX_FMT_YUVJ420P:
  2109. *format = PIX_FMT_YUV420P;
  2110. return 1;
  2111. case PIX_FMT_YUVJ422P:
  2112. *format = PIX_FMT_YUV422P;
  2113. return 1;
  2114. case PIX_FMT_YUVJ444P:
  2115. *format = PIX_FMT_YUV444P;
  2116. return 1;
  2117. case PIX_FMT_YUVJ440P:
  2118. *format = PIX_FMT_YUV440P;
  2119. return 1;
  2120. default:
  2121. return 0;
  2122. }
  2123. }
  2124. SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
  2125. SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  2126. {
  2127. SwsContext *c;
  2128. int i;
  2129. int usesVFilter, usesHFilter;
  2130. int unscaled, needsDither;
  2131. int srcRange, dstRange;
  2132. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  2133. #if ARCH_X86
  2134. if (flags & SWS_CPU_CAPS_MMX)
  2135. __asm__ volatile("emms\n\t"::: "memory");
  2136. #endif
  2137. #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
  2138. flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
  2139. #if COMPILE_TEMPLATE_MMX2
  2140. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  2141. #elif COMPILE_TEMPLATE_AMD3DNOW
  2142. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  2143. #elif COMPILE_TEMPLATE_MMX
  2144. flags |= SWS_CPU_CAPS_MMX;
  2145. #elif COMPILE_TEMPLATE_ALTIVEC
  2146. flags |= SWS_CPU_CAPS_ALTIVEC;
  2147. #elif ARCH_BFIN
  2148. flags |= SWS_CPU_CAPS_BFIN;
  2149. #endif
  2150. #endif /* CONFIG_RUNTIME_CPUDETECT */
  2151. if (clip_table[512] != 255) globalInit();
  2152. if (!rgb15to16) sws_rgb2rgb_init(flags);
  2153. unscaled = (srcW == dstW && srcH == dstH);
  2154. needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
  2155. && (fmt_depth(dstFormat))<24
  2156. && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
  2157. srcRange = handle_jpeg(&srcFormat);
  2158. dstRange = handle_jpeg(&dstFormat);
  2159. if (!isSupportedIn(srcFormat)) {
  2160. av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
  2161. return NULL;
  2162. }
  2163. if (!isSupportedOut(dstFormat)) {
  2164. av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
  2165. return NULL;
  2166. }
  2167. i= flags & ( SWS_POINT
  2168. |SWS_AREA
  2169. |SWS_BILINEAR
  2170. |SWS_FAST_BILINEAR
  2171. |SWS_BICUBIC
  2172. |SWS_X
  2173. |SWS_GAUSS
  2174. |SWS_LANCZOS
  2175. |SWS_SINC
  2176. |SWS_SPLINE
  2177. |SWS_BICUBLIN);
  2178. if(!i || (i & (i-1))) {
  2179. av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
  2180. return NULL;
  2181. }
  2182. /* sanity check */
  2183. if (srcW<4 || srcH<1 || dstW<8 || dstH<1) { //FIXME check if these are enough and try to lowwer them after fixing the relevant parts of the code
  2184. av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  2185. srcW, srcH, dstW, dstH);
  2186. return NULL;
  2187. }
  2188. if(srcW > VOFW || dstW > VOFW) {
  2189. av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
  2190. return NULL;
  2191. }
  2192. if (!dstFilter) dstFilter= &dummyFilter;
  2193. if (!srcFilter) srcFilter= &dummyFilter;
  2194. FF_ALLOCZ_OR_GOTO(NULL, c, sizeof(SwsContext), fail);
  2195. c->av_class = &sws_context_class;
  2196. c->srcW= srcW;
  2197. c->srcH= srcH;
  2198. c->dstW= dstW;
  2199. c->dstH= dstH;
  2200. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  2201. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  2202. c->flags= flags;
  2203. c->dstFormat= dstFormat;
  2204. c->srcFormat= srcFormat;
  2205. c->vRounder= 4* 0x0001000100010001ULL;
  2206. usesHFilter= usesVFilter= 0;
  2207. if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
  2208. if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
  2209. if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
  2210. if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
  2211. if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
  2212. if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
  2213. if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
  2214. if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
  2215. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  2216. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  2217. // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
  2218. if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  2219. // drop some chroma lines if the user wants it
  2220. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  2221. c->chrSrcVSubSample+= c->vChrDrop;
  2222. // drop every other pixel for chroma calculation unless user wants full chroma
  2223. if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
  2224. && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
  2225. && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
  2226. && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
  2227. && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  2228. c->chrSrcHSubSample=1;
  2229. if (param) {
  2230. c->param[0] = param[0];
  2231. c->param[1] = param[1];
  2232. } else {
  2233. c->param[0] =
  2234. c->param[1] = SWS_PARAM_DEFAULT;
  2235. }
  2236. // Note the -((-x)>>y) is so that we always round toward +inf.
  2237. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  2238. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  2239. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  2240. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  2241. sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
  2242. /* unscaled special cases */
  2243. if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat))) {
  2244. /* yv12_to_nv12 */
  2245. if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
  2246. c->swScale= PlanarToNV12Wrapper;
  2247. }
  2248. /* yuv2bgr */
  2249. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
  2250. && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
  2251. c->swScale= ff_yuv2rgb_get_func_ptr(c);
  2252. }
  2253. if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
  2254. c->swScale= yvu9toyv12Wrapper;
  2255. }
  2256. /* bgr24toYV12 */
  2257. if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
  2258. c->swScale= bgr24toyv12Wrapper;
  2259. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  2260. if ( (isBGR(srcFormat) || isRGB(srcFormat))
  2261. && (isBGR(dstFormat) || isRGB(dstFormat))
  2262. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  2263. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  2264. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  2265. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  2266. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  2267. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  2268. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  2269. && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
  2270. && dstFormat != PIX_FMT_RGB32_1
  2271. && dstFormat != PIX_FMT_BGR32_1
  2272. && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
  2273. && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
  2274. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  2275. c->swScale= rgb2rgbWrapper;
  2276. if ((usePal(srcFormat) && (
  2277. dstFormat == PIX_FMT_RGB32 ||
  2278. dstFormat == PIX_FMT_RGB32_1 ||
  2279. dstFormat == PIX_FMT_RGB24 ||
  2280. dstFormat == PIX_FMT_BGR32 ||
  2281. dstFormat == PIX_FMT_BGR32_1 ||
  2282. dstFormat == PIX_FMT_BGR24)))
  2283. c->swScale= pal2rgbWrapper;
  2284. if (srcFormat == PIX_FMT_YUV422P) {
  2285. if (dstFormat == PIX_FMT_YUYV422)
  2286. c->swScale= YUV422PToYuy2Wrapper;
  2287. else if (dstFormat == PIX_FMT_UYVY422)
  2288. c->swScale= YUV422PToUyvyWrapper;
  2289. }
  2290. /* LQ converters if -sws 0 or -sws 4*/
  2291. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  2292. /* yv12_to_yuy2 */
  2293. if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
  2294. if (dstFormat == PIX_FMT_YUYV422)
  2295. c->swScale= PlanarToYuy2Wrapper;
  2296. else if (dstFormat == PIX_FMT_UYVY422)
  2297. c->swScale= PlanarToUyvyWrapper;
  2298. }
  2299. }
  2300. if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  2301. c->swScale= YUYV2YUV420Wrapper;
  2302. if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  2303. c->swScale= UYVY2YUV420Wrapper;
  2304. if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
  2305. c->swScale= YUYV2YUV422Wrapper;
  2306. if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
  2307. c->swScale= UYVY2YUV422Wrapper;
  2308. #ifdef COMPILE_ALTIVEC
  2309. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  2310. !(c->flags & SWS_BITEXACT) &&
  2311. srcFormat == PIX_FMT_YUV420P) {
  2312. // unscaled YV12 -> packed YUV, we want speed
  2313. if (dstFormat == PIX_FMT_YUYV422)
  2314. c->swScale= yv12toyuy2_unscaled_altivec;
  2315. else if (dstFormat == PIX_FMT_UYVY422)
  2316. c->swScale= yv12touyvy_unscaled_altivec;
  2317. }
  2318. #endif
  2319. /* simple copy */
  2320. if ( srcFormat == dstFormat
  2321. || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
  2322. || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
  2323. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  2324. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  2325. || (isGray(dstFormat) && isGray(srcFormat))
  2326. || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
  2327. && c->chrDstHSubSample == c->chrSrcHSubSample
  2328. && c->chrDstVSubSample == c->chrSrcVSubSample
  2329. && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
  2330. && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
  2331. {
  2332. if (isPacked(c->srcFormat))
  2333. c->swScale= packedCopy;
  2334. else /* Planar YUV or gray */
  2335. c->swScale= planarCopy;
  2336. }
  2337. #if ARCH_BFIN
  2338. if (flags & SWS_CPU_CAPS_BFIN)
  2339. ff_bfin_get_unscaled_swscale (c);
  2340. #endif
  2341. if (c->swScale) {
  2342. if (flags&SWS_PRINT_INFO)
  2343. av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
  2344. sws_format_name(srcFormat), sws_format_name(dstFormat));
  2345. return c;
  2346. }
  2347. }
  2348. if (flags & SWS_CPU_CAPS_MMX2) {
  2349. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  2350. if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
  2351. if (flags&SWS_PRINT_INFO)
  2352. av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
  2353. }
  2354. if (usesHFilter) c->canMMX2BeUsed=0;
  2355. }
  2356. else
  2357. c->canMMX2BeUsed=0;
  2358. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  2359. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  2360. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  2361. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  2362. // n-2 is the last chrominance sample available
  2363. // this is not perfect, but no one should notice the difference, the more correct variant
  2364. // would be like the vertical one, but that would require some special code for the
  2365. // first and last pixel
  2366. if (flags&SWS_FAST_BILINEAR) {
  2367. if (c->canMMX2BeUsed) {
  2368. c->lumXInc+= 20;
  2369. c->chrXInc+= 20;
  2370. }
  2371. //we don't use the x86 asm scaler if MMX is available
  2372. else if (flags & SWS_CPU_CAPS_MMX) {
  2373. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  2374. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  2375. }
  2376. }
  2377. /* precalculate horizontal scaler filter coefficients */
  2378. {
  2379. const int filterAlign=
  2380. (flags & SWS_CPU_CAPS_MMX) ? 4 :
  2381. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  2382. 1;
  2383. if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  2384. srcW , dstW, filterAlign, 1<<14,
  2385. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  2386. srcFilter->lumH, dstFilter->lumH, c->param) < 0)
  2387. goto fail;
  2388. if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  2389. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  2390. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  2391. srcFilter->chrH, dstFilter->chrH, c->param) < 0)
  2392. goto fail;
  2393. #if defined(COMPILE_MMX2)
  2394. // can't downscale !!!
  2395. if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
  2396. c->lumMmx2FilterCodeSize = initMMX2HScaler( dstW, c->lumXInc, NULL, NULL, NULL, 8);
  2397. c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
  2398. #ifdef MAP_ANONYMOUS
  2399. c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  2400. c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  2401. #elif HAVE_VIRTUALALLOC
  2402. c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  2403. c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  2404. #else
  2405. c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
  2406. c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
  2407. #endif
  2408. FF_ALLOCZ_OR_GOTO(c, c->lumMmx2Filter , (dstW /8+8)*sizeof(int16_t), fail);
  2409. FF_ALLOCZ_OR_GOTO(c, c->chrMmx2Filter , (c->chrDstW /4+8)*sizeof(int16_t), fail);
  2410. FF_ALLOCZ_OR_GOTO(c, c->lumMmx2FilterPos, (dstW /2/8+8)*sizeof(int32_t), fail);
  2411. FF_ALLOCZ_OR_GOTO(c, c->chrMmx2FilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
  2412. initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
  2413. initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
  2414. #ifdef MAP_ANONYMOUS
  2415. mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  2416. mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  2417. #endif
  2418. }
  2419. #endif /* defined(COMPILE_MMX2) */
  2420. } // initialize horizontal stuff
  2421. /* precalculate vertical scaler filter coefficients */
  2422. {
  2423. const int filterAlign=
  2424. (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
  2425. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  2426. 1;
  2427. if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  2428. srcH , dstH, filterAlign, (1<<12),
  2429. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  2430. srcFilter->lumV, dstFilter->lumV, c->param) < 0)
  2431. goto fail;
  2432. if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  2433. c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
  2434. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  2435. srcFilter->chrV, dstFilter->chrV, c->param) < 0)
  2436. goto fail;
  2437. #ifdef COMPILE_ALTIVEC
  2438. FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
  2439. FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
  2440. for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
  2441. int j;
  2442. short *p = (short *)&c->vYCoeffsBank[i];
  2443. for (j=0;j<8;j++)
  2444. p[j] = c->vLumFilter[i];
  2445. }
  2446. for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
  2447. int j;
  2448. short *p = (short *)&c->vCCoeffsBank[i];
  2449. for (j=0;j<8;j++)
  2450. p[j] = c->vChrFilter[i];
  2451. }
  2452. #endif
  2453. }
  2454. // calculate buffer sizes so that they won't run out while handling these damn slices
  2455. c->vLumBufSize= c->vLumFilterSize;
  2456. c->vChrBufSize= c->vChrFilterSize;
  2457. for (i=0; i<dstH; i++) {
  2458. int chrI= i*c->chrDstH / dstH;
  2459. int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  2460. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  2461. nextSlice>>= c->chrSrcVSubSample;
  2462. nextSlice<<= c->chrSrcVSubSample;
  2463. if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  2464. c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
  2465. if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  2466. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  2467. }
  2468. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  2469. // allocate several megabytes to handle all possible cases)
  2470. FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
  2471. FF_ALLOC_OR_GOTO(c, c->chrPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
  2472. if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
  2473. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
  2474. //Note we need at least one pixel more at the end because of the MMX code (just in case someone wanna replace the 4000/8000)
  2475. /* align at 16 bytes for AltiVec */
  2476. for (i=0; i<c->vLumBufSize; i++) {
  2477. FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], VOF+1, fail);
  2478. c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
  2479. }
  2480. for (i=0; i<c->vChrBufSize; i++) {
  2481. FF_ALLOC_OR_GOTO(c, c->chrPixBuf[i+c->vChrBufSize], (VOF+1)*2, fail);
  2482. c->chrPixBuf[i] = c->chrPixBuf[i+c->vChrBufSize];
  2483. }
  2484. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  2485. for (i=0; i<c->vLumBufSize; i++) {
  2486. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], VOF+1, fail);
  2487. c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
  2488. }
  2489. //try to avoid drawing green stuff between the right end and the stride end
  2490. for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
  2491. assert(2*VOFW == VOF);
  2492. assert(c->chrDstH <= dstH);
  2493. if (flags&SWS_PRINT_INFO) {
  2494. #ifdef DITHER1XBPP
  2495. const char *dither= " dithered";
  2496. #else
  2497. const char *dither= "";
  2498. #endif
  2499. if (flags&SWS_FAST_BILINEAR)
  2500. av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
  2501. else if (flags&SWS_BILINEAR)
  2502. av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
  2503. else if (flags&SWS_BICUBIC)
  2504. av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
  2505. else if (flags&SWS_X)
  2506. av_log(c, AV_LOG_INFO, "Experimental scaler, ");
  2507. else if (flags&SWS_POINT)
  2508. av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
  2509. else if (flags&SWS_AREA)
  2510. av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
  2511. else if (flags&SWS_BICUBLIN)
  2512. av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
  2513. else if (flags&SWS_GAUSS)
  2514. av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
  2515. else if (flags&SWS_SINC)
  2516. av_log(c, AV_LOG_INFO, "Sinc scaler, ");
  2517. else if (flags&SWS_LANCZOS)
  2518. av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
  2519. else if (flags&SWS_SPLINE)
  2520. av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
  2521. else
  2522. av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
  2523. if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
  2524. av_log(c, AV_LOG_INFO, "from %s to%s %s ",
  2525. sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
  2526. else
  2527. av_log(c, AV_LOG_INFO, "from %s to %s ",
  2528. sws_format_name(srcFormat), sws_format_name(dstFormat));
  2529. if (flags & SWS_CPU_CAPS_MMX2)
  2530. av_log(c, AV_LOG_INFO, "using MMX2\n");
  2531. else if (flags & SWS_CPU_CAPS_3DNOW)
  2532. av_log(c, AV_LOG_INFO, "using 3DNOW\n");
  2533. else if (flags & SWS_CPU_CAPS_MMX)
  2534. av_log(c, AV_LOG_INFO, "using MMX\n");
  2535. else if (flags & SWS_CPU_CAPS_ALTIVEC)
  2536. av_log(c, AV_LOG_INFO, "using AltiVec\n");
  2537. else
  2538. av_log(c, AV_LOG_INFO, "using C\n");
  2539. }
  2540. if (flags & SWS_PRINT_INFO) {
  2541. if (flags & SWS_CPU_CAPS_MMX) {
  2542. if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  2543. av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  2544. else {
  2545. if (c->hLumFilterSize==4)
  2546. av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
  2547. else if (c->hLumFilterSize==8)
  2548. av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
  2549. else
  2550. av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
  2551. if (c->hChrFilterSize==4)
  2552. av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
  2553. else if (c->hChrFilterSize==8)
  2554. av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
  2555. else
  2556. av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
  2557. }
  2558. } else {
  2559. #if ARCH_X86
  2560. av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
  2561. #else
  2562. if (flags & SWS_FAST_BILINEAR)
  2563. av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
  2564. else
  2565. av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
  2566. #endif
  2567. }
  2568. if (isPlanarYUV(dstFormat)) {
  2569. if (c->vLumFilterSize==1)
  2570. av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2571. else
  2572. av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2573. } else {
  2574. if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
  2575. av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  2576. " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2577. else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
  2578. av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2579. else
  2580. av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2581. }
  2582. if (dstFormat==PIX_FMT_BGR24)
  2583. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
  2584. (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
  2585. else if (dstFormat==PIX_FMT_RGB32)
  2586. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2587. else if (dstFormat==PIX_FMT_BGR565)
  2588. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2589. else if (dstFormat==PIX_FMT_BGR555)
  2590. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2591. av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  2592. }
  2593. if (flags & SWS_PRINT_INFO) {
  2594. av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2595. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  2596. av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2597. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  2598. }
  2599. c->swScale= getSwsFunc(c);
  2600. return c;
  2601. fail:
  2602. sws_freeContext(c);
  2603. return NULL;
  2604. }
  2605. static void reset_ptr(uint8_t* src[], int format)
  2606. {
  2607. if(!isALPHA(format))
  2608. src[3]=NULL;
  2609. if(!isPlanarYUV(format)) {
  2610. src[3]=src[2]=NULL;
  2611. if( format != PIX_FMT_PAL8
  2612. && format != PIX_FMT_RGB8
  2613. && format != PIX_FMT_BGR8
  2614. && format != PIX_FMT_RGB4_BYTE
  2615. && format != PIX_FMT_BGR4_BYTE
  2616. )
  2617. src[1]= NULL;
  2618. }
  2619. }
  2620. /**
  2621. * swscale wrapper, so we don't need to export the SwsContext.
  2622. * Assumes planar YUV to be in YUV order instead of YVU.
  2623. */
  2624. int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  2625. int srcSliceH, uint8_t* dst[], int dstStride[])
  2626. {
  2627. int i;
  2628. uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
  2629. uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
  2630. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  2631. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  2632. return 0;
  2633. }
  2634. if (c->sliceDir == 0) {
  2635. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  2636. }
  2637. if (usePal(c->srcFormat)) {
  2638. for (i=0; i<256; i++) {
  2639. int p, r, g, b,y,u,v;
  2640. if(c->srcFormat == PIX_FMT_PAL8) {
  2641. p=((uint32_t*)(src[1]))[i];
  2642. r= (p>>16)&0xFF;
  2643. g= (p>> 8)&0xFF;
  2644. b= p &0xFF;
  2645. } else if(c->srcFormat == PIX_FMT_RGB8) {
  2646. r= (i>>5 )*36;
  2647. g= ((i>>2)&7)*36;
  2648. b= (i&3 )*85;
  2649. } else if(c->srcFormat == PIX_FMT_BGR8) {
  2650. b= (i>>6 )*85;
  2651. g= ((i>>3)&7)*36;
  2652. r= (i&7 )*36;
  2653. } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
  2654. r= (i>>3 )*255;
  2655. g= ((i>>1)&3)*85;
  2656. b= (i&1 )*255;
  2657. } else {
  2658. assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
  2659. b= (i>>3 )*255;
  2660. g= ((i>>1)&3)*85;
  2661. r= (i&1 )*255;
  2662. }
  2663. y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  2664. u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  2665. v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  2666. c->pal_yuv[i]= y + (u<<8) + (v<<16);
  2667. switch(c->dstFormat) {
  2668. case PIX_FMT_BGR32:
  2669. #if !HAVE_BIGENDIAN
  2670. case PIX_FMT_RGB24:
  2671. #endif
  2672. c->pal_rgb[i]= r + (g<<8) + (b<<16);
  2673. break;
  2674. case PIX_FMT_BGR32_1:
  2675. #if HAVE_BIGENDIAN
  2676. case PIX_FMT_BGR24:
  2677. #endif
  2678. c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
  2679. break;
  2680. case PIX_FMT_RGB32_1:
  2681. #if HAVE_BIGENDIAN
  2682. case PIX_FMT_RGB24:
  2683. #endif
  2684. c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
  2685. break;
  2686. case PIX_FMT_RGB32:
  2687. #if !HAVE_BIGENDIAN
  2688. case PIX_FMT_BGR24:
  2689. #endif
  2690. default:
  2691. c->pal_rgb[i]= b + (g<<8) + (r<<16);
  2692. }
  2693. }
  2694. }
  2695. // copy strides, so they can safely be modified
  2696. if (c->sliceDir == 1) {
  2697. // slices go from top to bottom
  2698. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
  2699. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
  2700. reset_ptr(src2, c->srcFormat);
  2701. reset_ptr(dst2, c->dstFormat);
  2702. /* reset slice direction at end of frame */
  2703. if (srcSliceY + srcSliceH == c->srcH)
  2704. c->sliceDir = 0;
  2705. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
  2706. } else {
  2707. // slices go from bottom to top => we flip the image internally
  2708. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
  2709. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
  2710. src2[0] += (srcSliceH-1)*srcStride[0];
  2711. if (!usePal(c->srcFormat))
  2712. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  2713. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  2714. src2[3] += (srcSliceH-1)*srcStride[3];
  2715. dst2[0] += ( c->dstH -1)*dstStride[0];
  2716. dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
  2717. dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
  2718. dst2[3] += ( c->dstH -1)*dstStride[3];
  2719. reset_ptr(src2, c->srcFormat);
  2720. reset_ptr(dst2, c->dstFormat);
  2721. /* reset slice direction at end of frame */
  2722. if (!srcSliceY)
  2723. c->sliceDir = 0;
  2724. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  2725. }
  2726. }
  2727. #if LIBSWSCALE_VERSION_MAJOR < 1
  2728. int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  2729. int srcSliceH, uint8_t* dst[], int dstStride[])
  2730. {
  2731. return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
  2732. }
  2733. #endif
  2734. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  2735. float lumaSharpen, float chromaSharpen,
  2736. float chromaHShift, float chromaVShift,
  2737. int verbose)
  2738. {
  2739. SwsFilter *filter= av_malloc(sizeof(SwsFilter));
  2740. if (!filter)
  2741. return NULL;
  2742. if (lumaGBlur!=0.0) {
  2743. filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
  2744. filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
  2745. } else {
  2746. filter->lumH= sws_getIdentityVec();
  2747. filter->lumV= sws_getIdentityVec();
  2748. }
  2749. if (chromaGBlur!=0.0) {
  2750. filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
  2751. filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
  2752. } else {
  2753. filter->chrH= sws_getIdentityVec();
  2754. filter->chrV= sws_getIdentityVec();
  2755. }
  2756. if (chromaSharpen!=0.0) {
  2757. SwsVector *id= sws_getIdentityVec();
  2758. sws_scaleVec(filter->chrH, -chromaSharpen);
  2759. sws_scaleVec(filter->chrV, -chromaSharpen);
  2760. sws_addVec(filter->chrH, id);
  2761. sws_addVec(filter->chrV, id);
  2762. sws_freeVec(id);
  2763. }
  2764. if (lumaSharpen!=0.0) {
  2765. SwsVector *id= sws_getIdentityVec();
  2766. sws_scaleVec(filter->lumH, -lumaSharpen);
  2767. sws_scaleVec(filter->lumV, -lumaSharpen);
  2768. sws_addVec(filter->lumH, id);
  2769. sws_addVec(filter->lumV, id);
  2770. sws_freeVec(id);
  2771. }
  2772. if (chromaHShift != 0.0)
  2773. sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
  2774. if (chromaVShift != 0.0)
  2775. sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
  2776. sws_normalizeVec(filter->chrH, 1.0);
  2777. sws_normalizeVec(filter->chrV, 1.0);
  2778. sws_normalizeVec(filter->lumH, 1.0);
  2779. sws_normalizeVec(filter->lumV, 1.0);
  2780. if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
  2781. if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
  2782. return filter;
  2783. }
  2784. SwsVector *sws_allocVec(int length)
  2785. {
  2786. SwsVector *vec = av_malloc(sizeof(SwsVector));
  2787. if (!vec)
  2788. return NULL;
  2789. vec->length = length;
  2790. vec->coeff = av_malloc(sizeof(double) * length);
  2791. if (!vec->coeff)
  2792. av_freep(&vec);
  2793. return vec;
  2794. }
  2795. SwsVector *sws_getGaussianVec(double variance, double quality)
  2796. {
  2797. const int length= (int)(variance*quality + 0.5) | 1;
  2798. int i;
  2799. double middle= (length-1)*0.5;
  2800. SwsVector *vec= sws_allocVec(length);
  2801. if (!vec)
  2802. return NULL;
  2803. for (i=0; i<length; i++) {
  2804. double dist= i-middle;
  2805. vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
  2806. }
  2807. sws_normalizeVec(vec, 1.0);
  2808. return vec;
  2809. }
  2810. SwsVector *sws_getConstVec(double c, int length)
  2811. {
  2812. int i;
  2813. SwsVector *vec= sws_allocVec(length);
  2814. if (!vec)
  2815. return NULL;
  2816. for (i=0; i<length; i++)
  2817. vec->coeff[i]= c;
  2818. return vec;
  2819. }
  2820. SwsVector *sws_getIdentityVec(void)
  2821. {
  2822. return sws_getConstVec(1.0, 1);
  2823. }
  2824. double sws_dcVec(SwsVector *a)
  2825. {
  2826. int i;
  2827. double sum=0;
  2828. for (i=0; i<a->length; i++)
  2829. sum+= a->coeff[i];
  2830. return sum;
  2831. }
  2832. void sws_scaleVec(SwsVector *a, double scalar)
  2833. {
  2834. int i;
  2835. for (i=0; i<a->length; i++)
  2836. a->coeff[i]*= scalar;
  2837. }
  2838. void sws_normalizeVec(SwsVector *a, double height)
  2839. {
  2840. sws_scaleVec(a, height/sws_dcVec(a));
  2841. }
  2842. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
  2843. {
  2844. int length= a->length + b->length - 1;
  2845. int i, j;
  2846. SwsVector *vec= sws_getConstVec(0.0, length);
  2847. if (!vec)
  2848. return NULL;
  2849. for (i=0; i<a->length; i++) {
  2850. for (j=0; j<b->length; j++) {
  2851. vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
  2852. }
  2853. }
  2854. return vec;
  2855. }
  2856. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
  2857. {
  2858. int length= FFMAX(a->length, b->length);
  2859. int i;
  2860. SwsVector *vec= sws_getConstVec(0.0, length);
  2861. if (!vec)
  2862. return NULL;
  2863. for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2864. for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  2865. return vec;
  2866. }
  2867. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
  2868. {
  2869. int length= FFMAX(a->length, b->length);
  2870. int i;
  2871. SwsVector *vec= sws_getConstVec(0.0, length);
  2872. if (!vec)
  2873. return NULL;
  2874. for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2875. for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  2876. return vec;
  2877. }
  2878. /* shift left / or right if "shift" is negative */
  2879. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
  2880. {
  2881. int length= a->length + FFABS(shift)*2;
  2882. int i;
  2883. SwsVector *vec= sws_getConstVec(0.0, length);
  2884. if (!vec)
  2885. return NULL;
  2886. for (i=0; i<a->length; i++) {
  2887. vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  2888. }
  2889. return vec;
  2890. }
  2891. void sws_shiftVec(SwsVector *a, int shift)
  2892. {
  2893. SwsVector *shifted= sws_getShiftedVec(a, shift);
  2894. av_free(a->coeff);
  2895. a->coeff= shifted->coeff;
  2896. a->length= shifted->length;
  2897. av_free(shifted);
  2898. }
  2899. void sws_addVec(SwsVector *a, SwsVector *b)
  2900. {
  2901. SwsVector *sum= sws_sumVec(a, b);
  2902. av_free(a->coeff);
  2903. a->coeff= sum->coeff;
  2904. a->length= sum->length;
  2905. av_free(sum);
  2906. }
  2907. void sws_subVec(SwsVector *a, SwsVector *b)
  2908. {
  2909. SwsVector *diff= sws_diffVec(a, b);
  2910. av_free(a->coeff);
  2911. a->coeff= diff->coeff;
  2912. a->length= diff->length;
  2913. av_free(diff);
  2914. }
  2915. void sws_convVec(SwsVector *a, SwsVector *b)
  2916. {
  2917. SwsVector *conv= sws_getConvVec(a, b);
  2918. av_free(a->coeff);
  2919. a->coeff= conv->coeff;
  2920. a->length= conv->length;
  2921. av_free(conv);
  2922. }
  2923. SwsVector *sws_cloneVec(SwsVector *a)
  2924. {
  2925. int i;
  2926. SwsVector *vec= sws_allocVec(a->length);
  2927. if (!vec)
  2928. return NULL;
  2929. for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
  2930. return vec;
  2931. }
  2932. void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
  2933. {
  2934. int i;
  2935. double max=0;
  2936. double min=0;
  2937. double range;
  2938. for (i=0; i<a->length; i++)
  2939. if (a->coeff[i]>max) max= a->coeff[i];
  2940. for (i=0; i<a->length; i++)
  2941. if (a->coeff[i]<min) min= a->coeff[i];
  2942. range= max - min;
  2943. for (i=0; i<a->length; i++) {
  2944. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  2945. av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
  2946. for (;x>0; x--) av_log(log_ctx, log_level, " ");
  2947. av_log(log_ctx, log_level, "|\n");
  2948. }
  2949. }
  2950. #if LIBSWSCALE_VERSION_MAJOR < 1
  2951. void sws_printVec(SwsVector *a)
  2952. {
  2953. sws_printVec2(a, NULL, AV_LOG_DEBUG);
  2954. }
  2955. #endif
  2956. void sws_freeVec(SwsVector *a)
  2957. {
  2958. if (!a) return;
  2959. av_freep(&a->coeff);
  2960. a->length=0;
  2961. av_free(a);
  2962. }
  2963. void sws_freeFilter(SwsFilter *filter)
  2964. {
  2965. if (!filter) return;
  2966. if (filter->lumH) sws_freeVec(filter->lumH);
  2967. if (filter->lumV) sws_freeVec(filter->lumV);
  2968. if (filter->chrH) sws_freeVec(filter->chrH);
  2969. if (filter->chrV) sws_freeVec(filter->chrV);
  2970. av_free(filter);
  2971. }
  2972. void sws_freeContext(SwsContext *c)
  2973. {
  2974. int i;
  2975. if (!c) return;
  2976. if (c->lumPixBuf) {
  2977. for (i=0; i<c->vLumBufSize; i++)
  2978. av_freep(&c->lumPixBuf[i]);
  2979. av_freep(&c->lumPixBuf);
  2980. }
  2981. if (c->chrPixBuf) {
  2982. for (i=0; i<c->vChrBufSize; i++)
  2983. av_freep(&c->chrPixBuf[i]);
  2984. av_freep(&c->chrPixBuf);
  2985. }
  2986. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  2987. for (i=0; i<c->vLumBufSize; i++)
  2988. av_freep(&c->alpPixBuf[i]);
  2989. av_freep(&c->alpPixBuf);
  2990. }
  2991. av_freep(&c->vLumFilter);
  2992. av_freep(&c->vChrFilter);
  2993. av_freep(&c->hLumFilter);
  2994. av_freep(&c->hChrFilter);
  2995. #ifdef COMPILE_ALTIVEC
  2996. av_freep(&c->vYCoeffsBank);
  2997. av_freep(&c->vCCoeffsBank);
  2998. #endif
  2999. av_freep(&c->vLumFilterPos);
  3000. av_freep(&c->vChrFilterPos);
  3001. av_freep(&c->hLumFilterPos);
  3002. av_freep(&c->hChrFilterPos);
  3003. #if ARCH_X86 && CONFIG_GPL
  3004. #ifdef MAP_ANONYMOUS
  3005. if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
  3006. if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
  3007. #elif HAVE_VIRTUALALLOC
  3008. if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, MEM_RELEASE);
  3009. if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, MEM_RELEASE);
  3010. #else
  3011. av_free(c->lumMmx2FilterCode);
  3012. av_free(c->chrMmx2FilterCode);
  3013. #endif
  3014. c->lumMmx2FilterCode=NULL;
  3015. c->chrMmx2FilterCode=NULL;
  3016. #endif /* ARCH_X86 && CONFIG_GPL */
  3017. av_freep(&c->lumMmx2Filter);
  3018. av_freep(&c->chrMmx2Filter);
  3019. av_freep(&c->lumMmx2FilterPos);
  3020. av_freep(&c->chrMmx2FilterPos);
  3021. av_freep(&c->yuvTable);
  3022. av_free(c);
  3023. }
  3024. struct SwsContext *sws_getCachedContext(struct SwsContext *context,
  3025. int srcW, int srcH, enum PixelFormat srcFormat,
  3026. int dstW, int dstH, enum PixelFormat dstFormat, int flags,
  3027. SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  3028. {
  3029. static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
  3030. if (!param)
  3031. param = default_param;
  3032. if (context) {
  3033. if (context->srcW != srcW || context->srcH != srcH ||
  3034. context->srcFormat != srcFormat ||
  3035. context->dstW != dstW || context->dstH != dstH ||
  3036. context->dstFormat != dstFormat || context->flags != flags ||
  3037. context->param[0] != param[0] || context->param[1] != param[1])
  3038. {
  3039. sws_freeContext(context);
  3040. context = NULL;
  3041. }
  3042. }
  3043. if (!context) {
  3044. return sws_getContext(srcW, srcH, srcFormat,
  3045. dstW, dstH, dstFormat, flags,
  3046. srcFilter, dstFilter, param);
  3047. }
  3048. return context;
  3049. }