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.

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