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.

3509 lines
123KB

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