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.

3612 lines
126KB

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