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.

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