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.

3440 lines
122KB

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