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.

3599 lines
125KB

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