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.

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