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.

3573 lines
124KB

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