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.

2977 lines
101KB

  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, BGR24, BGR16, BGR15, RGB32, 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. #include <inttypes.h>
  53. #include <string.h>
  54. #include <math.h>
  55. #include <stdio.h>
  56. #include <unistd.h>
  57. #include "config.h"
  58. #include <assert.h>
  59. #ifdef 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. #include "swscale.h"
  66. #include "swscale_internal.h"
  67. #include "rgb2rgb.h"
  68. #include "libavutil/x86_cpu.h"
  69. #include "libavutil/bswap.h"
  70. #include "libavcodec/opt.h"
  71. #undef MOVNTQ
  72. #undef PAVGB
  73. //#undef HAVE_MMX2
  74. //#define HAVE_3DNOW
  75. //#undef HAVE_MMX
  76. //#undef ARCH_X86
  77. //#define WORDS_BIGENDIAN
  78. #define DITHER1XBPP
  79. #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
  80. #define RET 0xC3 //near return opcode for X86
  81. #ifdef M_PI
  82. #define PI M_PI
  83. #else
  84. #define PI 3.14159265358979323846
  85. #endif
  86. #define isSupportedIn(x) ( \
  87. (x)==PIX_FMT_YUV420P \
  88. || (x)==PIX_FMT_YUVA420P \
  89. || (x)==PIX_FMT_YUYV422 \
  90. || (x)==PIX_FMT_UYVY422 \
  91. || (x)==PIX_FMT_RGB32 \
  92. || (x)==PIX_FMT_BGR24 \
  93. || (x)==PIX_FMT_BGR565 \
  94. || (x)==PIX_FMT_BGR555 \
  95. || (x)==PIX_FMT_BGR32 \
  96. || (x)==PIX_FMT_RGB24 \
  97. || (x)==PIX_FMT_RGB565 \
  98. || (x)==PIX_FMT_RGB555 \
  99. || (x)==PIX_FMT_GRAY8 \
  100. || (x)==PIX_FMT_YUV410P \
  101. || (x)==PIX_FMT_GRAY16BE \
  102. || (x)==PIX_FMT_GRAY16LE \
  103. || (x)==PIX_FMT_YUV444P \
  104. || (x)==PIX_FMT_YUV422P \
  105. || (x)==PIX_FMT_YUV411P \
  106. || (x)==PIX_FMT_PAL8 \
  107. || (x)==PIX_FMT_BGR8 \
  108. || (x)==PIX_FMT_RGB8 \
  109. || (x)==PIX_FMT_BGR4_BYTE \
  110. || (x)==PIX_FMT_RGB4_BYTE \
  111. || (x)==PIX_FMT_YUV440P \
  112. )
  113. #define isSupportedOut(x) ( \
  114. (x)==PIX_FMT_YUV420P \
  115. || (x)==PIX_FMT_YUYV422 \
  116. || (x)==PIX_FMT_UYVY422 \
  117. || (x)==PIX_FMT_YUV444P \
  118. || (x)==PIX_FMT_YUV422P \
  119. || (x)==PIX_FMT_YUV411P \
  120. || isRGB(x) \
  121. || isBGR(x) \
  122. || (x)==PIX_FMT_NV12 \
  123. || (x)==PIX_FMT_NV21 \
  124. || (x)==PIX_FMT_GRAY16BE \
  125. || (x)==PIX_FMT_GRAY16LE \
  126. || (x)==PIX_FMT_GRAY8 \
  127. || (x)==PIX_FMT_YUV410P \
  128. )
  129. #define isPacked(x) ( \
  130. (x)==PIX_FMT_PAL8 \
  131. || (x)==PIX_FMT_YUYV422 \
  132. || (x)==PIX_FMT_UYVY422 \
  133. || isRGB(x) \
  134. || isBGR(x) \
  135. )
  136. #define RGB2YUV_SHIFT 16
  137. #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
  138. #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
  139. #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  140. #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
  141. #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
  142. #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
  143. #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
  144. #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  145. #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
  146. extern const int32_t Inverse_Table_6_9[8][4];
  147. /*
  148. NOTES
  149. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  150. TODO
  151. more intelligent misalignment avoidance for the horizontal scaler
  152. write special vertical cubic upscale version
  153. Optimize C code (yv12 / minmax)
  154. add support for packed pixel yuv input & output
  155. add support for Y8 output
  156. optimize bgr24 & bgr32
  157. add BGR4 output support
  158. write special BGR->BGR scaler
  159. */
  160. #if defined(ARCH_X86) && defined (CONFIG_GPL)
  161. DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
  162. DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
  163. DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
  164. DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
  165. DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
  166. DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
  167. DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
  168. DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
  169. static volatile uint64_t attribute_used __attribute__((aligned(8))) b5Dither;
  170. static volatile uint64_t attribute_used __attribute__((aligned(8))) g5Dither;
  171. static volatile uint64_t attribute_used __attribute__((aligned(8))) g6Dither;
  172. static volatile uint64_t attribute_used __attribute__((aligned(8))) r5Dither;
  173. const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
  174. 0x0103010301030103LL,
  175. 0x0200020002000200LL,};
  176. const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
  177. 0x0602060206020602LL,
  178. 0x0004000400040004LL,};
  179. DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
  180. DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
  181. DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
  182. DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
  183. DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
  184. DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
  185. DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
  186. DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
  187. DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
  188. #ifdef FAST_BGR2YV12
  189. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
  190. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
  191. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
  192. #else
  193. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
  194. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
  195. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
  196. #endif /* FAST_BGR2YV12 */
  197. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
  198. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
  199. DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
  200. #endif /* defined(ARCH_X86) */
  201. // clipping helper table for C implementations:
  202. static unsigned char clip_table[768];
  203. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
  204. extern const uint8_t dither_2x2_4[2][8];
  205. extern const uint8_t dither_2x2_8[2][8];
  206. extern const uint8_t dither_8x8_32[8][8];
  207. extern const uint8_t dither_8x8_73[8][8];
  208. extern const uint8_t dither_8x8_220[8][8];
  209. static const char * sws_context_to_name(void * ptr) {
  210. return "swscaler";
  211. }
  212. #define OFFSET(x) offsetof(SwsContext, x)
  213. #define DEFAULT 0
  214. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  215. static const AVOption options[] = {
  216. { "sws_flags", "scaler/cpu flags", OFFSET(flags), FF_OPT_TYPE_FLAGS, DEFAULT, 0, UINT_MAX, VE, "sws_flags" },
  217. { "fast_bilinear", "fast bilinear", 0, FF_OPT_TYPE_CONST, SWS_FAST_BILINEAR, INT_MIN, INT_MAX, VE, "sws_flags" },
  218. { "bilinear", "bilinear", 0, FF_OPT_TYPE_CONST, SWS_BILINEAR, INT_MIN, INT_MAX, VE, "sws_flags" },
  219. { "bicubic", "bicubic", 0, FF_OPT_TYPE_CONST, SWS_BICUBIC, INT_MIN, INT_MAX, VE, "sws_flags" },
  220. { "experimental", "experimental", 0, FF_OPT_TYPE_CONST, SWS_X, INT_MIN, INT_MAX, VE, "sws_flags" },
  221. { "neighbor", "nearest neighbor", 0, FF_OPT_TYPE_CONST, SWS_POINT, INT_MIN, INT_MAX, VE, "sws_flags" },
  222. { "area", "averaging area", 0, FF_OPT_TYPE_CONST, SWS_AREA, INT_MIN, INT_MAX, VE, "sws_flags" },
  223. { "bicublin", "luma bicubic, chroma bilinear", 0, FF_OPT_TYPE_CONST, SWS_BICUBLIN, INT_MIN, INT_MAX, VE, "sws_flags" },
  224. { "gauss", "gaussian", 0, FF_OPT_TYPE_CONST, SWS_GAUSS, INT_MIN, INT_MAX, VE, "sws_flags" },
  225. { "sinc", "sinc", 0, FF_OPT_TYPE_CONST, SWS_SINC, INT_MIN, INT_MAX, VE, "sws_flags" },
  226. { "lanczos", "lanczos", 0, FF_OPT_TYPE_CONST, SWS_LANCZOS, INT_MIN, INT_MAX, VE, "sws_flags" },
  227. { "spline", "natural bicubic spline", 0, FF_OPT_TYPE_CONST, SWS_SPLINE, INT_MIN, INT_MAX, VE, "sws_flags" },
  228. { "print_info", "print info", 0, FF_OPT_TYPE_CONST, SWS_PRINT_INFO, INT_MIN, INT_MAX, VE, "sws_flags" },
  229. { "accurate_rnd", "accurate rounding", 0, FF_OPT_TYPE_CONST, SWS_ACCURATE_RND, INT_MIN, INT_MAX, VE, "sws_flags" },
  230. { "mmx", "MMX SIMD acceleration", 0, FF_OPT_TYPE_CONST, SWS_CPU_CAPS_MMX, INT_MIN, INT_MAX, VE, "sws_flags" },
  231. { "mmx2", "MMX2 SIMD acceleration", 0, FF_OPT_TYPE_CONST, SWS_CPU_CAPS_MMX2, INT_MIN, INT_MAX, VE, "sws_flags" },
  232. { "3dnow", "3DNOW SIMD acceleration", 0, FF_OPT_TYPE_CONST, SWS_CPU_CAPS_3DNOW, INT_MIN, INT_MAX, VE, "sws_flags" },
  233. { "altivec", "AltiVec SIMD acceleration", 0, FF_OPT_TYPE_CONST, SWS_CPU_CAPS_ALTIVEC, INT_MIN, INT_MAX, VE, "sws_flags" },
  234. { "bfin", "Blackfin SIMD acceleration", 0, FF_OPT_TYPE_CONST, SWS_CPU_CAPS_BFIN, INT_MIN, INT_MAX, VE, "sws_flags" },
  235. { "full_chroma_int", "full chroma interpolation", 0 , FF_OPT_TYPE_CONST, SWS_FULL_CHR_H_INT, INT_MIN, INT_MAX, VE, "sws_flags" },
  236. { "full_chroma_inp", "full chroma input", 0 , FF_OPT_TYPE_CONST, SWS_FULL_CHR_H_INP, INT_MIN, INT_MAX, VE, "sws_flags" },
  237. { NULL }
  238. };
  239. #undef VE
  240. #undef DEFAULT
  241. static const AVClass sws_context_class = { "SWScaler", sws_context_to_name, options };
  242. const char *sws_format_name(enum PixelFormat format)
  243. {
  244. switch (format) {
  245. case PIX_FMT_YUV420P:
  246. return "yuv420p";
  247. case PIX_FMT_YUVA420P:
  248. return "yuva420p";
  249. case PIX_FMT_YUYV422:
  250. return "yuyv422";
  251. case PIX_FMT_RGB24:
  252. return "rgb24";
  253. case PIX_FMT_BGR24:
  254. return "bgr24";
  255. case PIX_FMT_YUV422P:
  256. return "yuv422p";
  257. case PIX_FMT_YUV444P:
  258. return "yuv444p";
  259. case PIX_FMT_RGB32:
  260. return "rgb32";
  261. case PIX_FMT_YUV410P:
  262. return "yuv410p";
  263. case PIX_FMT_YUV411P:
  264. return "yuv411p";
  265. case PIX_FMT_RGB565:
  266. return "rgb565";
  267. case PIX_FMT_RGB555:
  268. return "rgb555";
  269. case PIX_FMT_GRAY16BE:
  270. return "gray16be";
  271. case PIX_FMT_GRAY16LE:
  272. return "gray16le";
  273. case PIX_FMT_GRAY8:
  274. return "gray8";
  275. case PIX_FMT_MONOWHITE:
  276. return "mono white";
  277. case PIX_FMT_MONOBLACK:
  278. return "mono black";
  279. case PIX_FMT_PAL8:
  280. return "Palette";
  281. case PIX_FMT_YUVJ420P:
  282. return "yuvj420p";
  283. case PIX_FMT_YUVJ422P:
  284. return "yuvj422p";
  285. case PIX_FMT_YUVJ444P:
  286. return "yuvj444p";
  287. case PIX_FMT_XVMC_MPEG2_MC:
  288. return "xvmc_mpeg2_mc";
  289. case PIX_FMT_XVMC_MPEG2_IDCT:
  290. return "xvmc_mpeg2_idct";
  291. case PIX_FMT_UYVY422:
  292. return "uyvy422";
  293. case PIX_FMT_UYYVYY411:
  294. return "uyyvyy411";
  295. case PIX_FMT_RGB32_1:
  296. return "rgb32x";
  297. case PIX_FMT_BGR32_1:
  298. return "bgr32x";
  299. case PIX_FMT_BGR32:
  300. return "bgr32";
  301. case PIX_FMT_BGR565:
  302. return "bgr565";
  303. case PIX_FMT_BGR555:
  304. return "bgr555";
  305. case PIX_FMT_BGR8:
  306. return "bgr8";
  307. case PIX_FMT_BGR4:
  308. return "bgr4";
  309. case PIX_FMT_BGR4_BYTE:
  310. return "bgr4 byte";
  311. case PIX_FMT_RGB8:
  312. return "rgb8";
  313. case PIX_FMT_RGB4:
  314. return "rgb4";
  315. case PIX_FMT_RGB4_BYTE:
  316. return "rgb4 byte";
  317. case PIX_FMT_NV12:
  318. return "nv12";
  319. case PIX_FMT_NV21:
  320. return "nv21";
  321. case PIX_FMT_YUV440P:
  322. return "yuv440p";
  323. default:
  324. return "Unknown format";
  325. }
  326. }
  327. static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  328. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  329. uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
  330. {
  331. //FIXME Optimize (just quickly writen not opti..)
  332. int i;
  333. for (i=0; i<dstW; i++)
  334. {
  335. int val=1<<18;
  336. int j;
  337. for (j=0; j<lumFilterSize; j++)
  338. val += lumSrc[j][i] * lumFilter[j];
  339. dest[i]= av_clip_uint8(val>>19);
  340. }
  341. if (uDest)
  342. for (i=0; i<chrDstW; i++)
  343. {
  344. int u=1<<18;
  345. int v=1<<18;
  346. int j;
  347. for (j=0; j<chrFilterSize; j++)
  348. {
  349. u += chrSrc[j][i] * chrFilter[j];
  350. v += chrSrc[j][i + VOFW] * chrFilter[j];
  351. }
  352. uDest[i]= av_clip_uint8(u>>19);
  353. vDest[i]= av_clip_uint8(v>>19);
  354. }
  355. }
  356. static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  357. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  358. uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
  359. {
  360. //FIXME Optimize (just quickly writen not opti..)
  361. int i;
  362. for (i=0; i<dstW; i++)
  363. {
  364. int val=1<<18;
  365. int j;
  366. for (j=0; j<lumFilterSize; j++)
  367. val += lumSrc[j][i] * lumFilter[j];
  368. dest[i]= av_clip_uint8(val>>19);
  369. }
  370. if (!uDest)
  371. return;
  372. if (dstFormat == PIX_FMT_NV12)
  373. for (i=0; i<chrDstW; i++)
  374. {
  375. int u=1<<18;
  376. int v=1<<18;
  377. int j;
  378. for (j=0; j<chrFilterSize; j++)
  379. {
  380. u += chrSrc[j][i] * chrFilter[j];
  381. v += chrSrc[j][i + VOFW] * chrFilter[j];
  382. }
  383. uDest[2*i]= av_clip_uint8(u>>19);
  384. uDest[2*i+1]= av_clip_uint8(v>>19);
  385. }
  386. else
  387. for (i=0; i<chrDstW; i++)
  388. {
  389. int u=1<<18;
  390. int v=1<<18;
  391. int j;
  392. for (j=0; j<chrFilterSize; j++)
  393. {
  394. u += chrSrc[j][i] * chrFilter[j];
  395. v += chrSrc[j][i + VOFW] * chrFilter[j];
  396. }
  397. uDest[2*i]= av_clip_uint8(v>>19);
  398. uDest[2*i+1]= av_clip_uint8(u>>19);
  399. }
  400. }
  401. #define YSCALE_YUV_2_PACKEDX_C(type) \
  402. for (i=0; i<(dstW>>1); i++){\
  403. int j;\
  404. int Y1 = 1<<18;\
  405. int Y2 = 1<<18;\
  406. int U = 1<<18;\
  407. int V = 1<<18;\
  408. type av_unused *r, *b, *g;\
  409. const int i2= 2*i;\
  410. \
  411. for (j=0; j<lumFilterSize; j++)\
  412. {\
  413. Y1 += lumSrc[j][i2] * lumFilter[j];\
  414. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  415. }\
  416. for (j=0; j<chrFilterSize; j++)\
  417. {\
  418. U += chrSrc[j][i] * chrFilter[j];\
  419. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  420. }\
  421. Y1>>=19;\
  422. Y2>>=19;\
  423. U >>=19;\
  424. V >>=19;\
  425. if ((Y1|Y2|U|V)&256)\
  426. {\
  427. if (Y1>255) Y1=255; \
  428. else if (Y1<0)Y1=0; \
  429. if (Y2>255) Y2=255; \
  430. else if (Y2<0)Y2=0; \
  431. if (U>255) U=255; \
  432. else if (U<0) U=0; \
  433. if (V>255) V=255; \
  434. else if (V<0) V=0; \
  435. }
  436. #define YSCALE_YUV_2_RGBX_C(type) \
  437. YSCALE_YUV_2_PACKEDX_C(type) \
  438. r = (type *)c->table_rV[V]; \
  439. g = (type *)(c->table_gU[U] + c->table_gV[V]); \
  440. b = (type *)c->table_bU[U]; \
  441. #define YSCALE_YUV_2_PACKED2_C \
  442. for (i=0; i<(dstW>>1); i++){ \
  443. const int i2= 2*i; \
  444. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
  445. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
  446. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
  447. int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
  448. #define YSCALE_YUV_2_RGB2_C(type) \
  449. YSCALE_YUV_2_PACKED2_C\
  450. type *r, *b, *g;\
  451. r = (type *)c->table_rV[V];\
  452. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  453. b = (type *)c->table_bU[U];\
  454. #define YSCALE_YUV_2_PACKED1_C \
  455. for (i=0; i<(dstW>>1); i++){\
  456. const int i2= 2*i;\
  457. int Y1= buf0[i2 ]>>7;\
  458. int Y2= buf0[i2+1]>>7;\
  459. int U= (uvbuf1[i ])>>7;\
  460. int V= (uvbuf1[i+VOFW])>>7;\
  461. #define YSCALE_YUV_2_RGB1_C(type) \
  462. YSCALE_YUV_2_PACKED1_C\
  463. type *r, *b, *g;\
  464. r = (type *)c->table_rV[V];\
  465. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  466. b = (type *)c->table_bU[U];\
  467. #define YSCALE_YUV_2_PACKED1B_C \
  468. for (i=0; i<(dstW>>1); i++){\
  469. const int i2= 2*i;\
  470. int Y1= buf0[i2 ]>>7;\
  471. int Y2= buf0[i2+1]>>7;\
  472. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  473. int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
  474. #define YSCALE_YUV_2_RGB1B_C(type) \
  475. YSCALE_YUV_2_PACKED1B_C\
  476. type *r, *b, *g;\
  477. r = (type *)c->table_rV[V];\
  478. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  479. b = (type *)c->table_bU[U];\
  480. #define YSCALE_YUV_2_ANYRGB_C(func, func2)\
  481. switch(c->dstFormat)\
  482. {\
  483. case PIX_FMT_RGB32:\
  484. case PIX_FMT_BGR32:\
  485. func(uint32_t)\
  486. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  487. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  488. } \
  489. break;\
  490. case PIX_FMT_RGB24:\
  491. func(uint8_t)\
  492. ((uint8_t*)dest)[0]= r[Y1];\
  493. ((uint8_t*)dest)[1]= g[Y1];\
  494. ((uint8_t*)dest)[2]= b[Y1];\
  495. ((uint8_t*)dest)[3]= r[Y2];\
  496. ((uint8_t*)dest)[4]= g[Y2];\
  497. ((uint8_t*)dest)[5]= b[Y2];\
  498. dest+=6;\
  499. }\
  500. break;\
  501. case PIX_FMT_BGR24:\
  502. func(uint8_t)\
  503. ((uint8_t*)dest)[0]= b[Y1];\
  504. ((uint8_t*)dest)[1]= g[Y1];\
  505. ((uint8_t*)dest)[2]= r[Y1];\
  506. ((uint8_t*)dest)[3]= b[Y2];\
  507. ((uint8_t*)dest)[4]= g[Y2];\
  508. ((uint8_t*)dest)[5]= r[Y2];\
  509. dest+=6;\
  510. }\
  511. break;\
  512. case PIX_FMT_RGB565:\
  513. case PIX_FMT_BGR565:\
  514. {\
  515. const int dr1= dither_2x2_8[y&1 ][0];\
  516. const int dg1= dither_2x2_4[y&1 ][0];\
  517. const int db1= dither_2x2_8[(y&1)^1][0];\
  518. const int dr2= dither_2x2_8[y&1 ][1];\
  519. const int dg2= dither_2x2_4[y&1 ][1];\
  520. const int db2= dither_2x2_8[(y&1)^1][1];\
  521. func(uint16_t)\
  522. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  523. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  524. }\
  525. }\
  526. break;\
  527. case PIX_FMT_RGB555:\
  528. case PIX_FMT_BGR555:\
  529. {\
  530. const int dr1= dither_2x2_8[y&1 ][0];\
  531. const int dg1= dither_2x2_8[y&1 ][1];\
  532. const int db1= dither_2x2_8[(y&1)^1][0];\
  533. const int dr2= dither_2x2_8[y&1 ][1];\
  534. const int dg2= dither_2x2_8[y&1 ][0];\
  535. const int db2= dither_2x2_8[(y&1)^1][1];\
  536. func(uint16_t)\
  537. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  538. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  539. }\
  540. }\
  541. break;\
  542. case PIX_FMT_RGB8:\
  543. case PIX_FMT_BGR8:\
  544. {\
  545. const uint8_t * const d64= dither_8x8_73[y&7];\
  546. const uint8_t * const d32= dither_8x8_32[y&7];\
  547. func(uint8_t)\
  548. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  549. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  550. }\
  551. }\
  552. break;\
  553. case PIX_FMT_RGB4:\
  554. case PIX_FMT_BGR4:\
  555. {\
  556. const uint8_t * const d64= dither_8x8_73 [y&7];\
  557. const uint8_t * const d128=dither_8x8_220[y&7];\
  558. func(uint8_t)\
  559. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  560. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  561. }\
  562. }\
  563. break;\
  564. case PIX_FMT_RGB4_BYTE:\
  565. case PIX_FMT_BGR4_BYTE:\
  566. {\
  567. const uint8_t * const d64= dither_8x8_73 [y&7];\
  568. const uint8_t * const d128=dither_8x8_220[y&7];\
  569. func(uint8_t)\
  570. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  571. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  572. }\
  573. }\
  574. break;\
  575. case PIX_FMT_MONOBLACK:\
  576. {\
  577. const uint8_t * const d128=dither_8x8_220[y&7];\
  578. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  579. for (i=0; i<dstW-7; i+=8){\
  580. int acc;\
  581. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  582. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  583. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  584. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  585. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  586. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  587. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  588. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  589. ((uint8_t*)dest)[0]= acc;\
  590. dest++;\
  591. }\
  592. \
  593. /*\
  594. ((uint8_t*)dest)-= dstW>>4;\
  595. {\
  596. int acc=0;\
  597. int left=0;\
  598. static int top[1024];\
  599. static int last_new[1024][1024];\
  600. static int last_in3[1024][1024];\
  601. static int drift[1024][1024];\
  602. int topLeft=0;\
  603. int shift=0;\
  604. int count=0;\
  605. const uint8_t * const d128=dither_8x8_220[y&7];\
  606. int error_new=0;\
  607. int error_in3=0;\
  608. int f=0;\
  609. \
  610. for (i=dstW>>1; i<dstW; i++){\
  611. int in= ((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19);\
  612. int in2 = (76309 * (in - 16) + 32768) >> 16;\
  613. int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
  614. int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
  615. + (last_new[y][i] - in3)*f/256;\
  616. int new= old> 128 ? 255 : 0;\
  617. \
  618. error_new+= FFABS(last_new[y][i] - new);\
  619. error_in3+= FFABS(last_in3[y][i] - in3);\
  620. f= error_new - error_in3*4;\
  621. if (f<0) f=0;\
  622. if (f>256) f=256;\
  623. \
  624. topLeft= top[i];\
  625. left= top[i]= old - new;\
  626. last_new[y][i]= new;\
  627. last_in3[y][i]= in3;\
  628. \
  629. acc+= acc + (new&1);\
  630. if ((i&7)==6){\
  631. ((uint8_t*)dest)[0]= acc;\
  632. ((uint8_t*)dest)++;\
  633. }\
  634. }\
  635. }\
  636. */\
  637. }\
  638. break;\
  639. case PIX_FMT_YUYV422:\
  640. func2\
  641. ((uint8_t*)dest)[2*i2+0]= Y1;\
  642. ((uint8_t*)dest)[2*i2+1]= U;\
  643. ((uint8_t*)dest)[2*i2+2]= Y2;\
  644. ((uint8_t*)dest)[2*i2+3]= V;\
  645. } \
  646. break;\
  647. case PIX_FMT_UYVY422:\
  648. func2\
  649. ((uint8_t*)dest)[2*i2+0]= U;\
  650. ((uint8_t*)dest)[2*i2+1]= Y1;\
  651. ((uint8_t*)dest)[2*i2+2]= V;\
  652. ((uint8_t*)dest)[2*i2+3]= Y2;\
  653. } \
  654. break;\
  655. }\
  656. static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  657. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  658. uint8_t *dest, int dstW, int y)
  659. {
  660. int i;
  661. switch(c->dstFormat)
  662. {
  663. case PIX_FMT_BGR32:
  664. case PIX_FMT_RGB32:
  665. YSCALE_YUV_2_RGBX_C(uint32_t)
  666. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];
  667. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];
  668. }
  669. break;
  670. case PIX_FMT_RGB24:
  671. YSCALE_YUV_2_RGBX_C(uint8_t)
  672. ((uint8_t*)dest)[0]= r[Y1];
  673. ((uint8_t*)dest)[1]= g[Y1];
  674. ((uint8_t*)dest)[2]= b[Y1];
  675. ((uint8_t*)dest)[3]= r[Y2];
  676. ((uint8_t*)dest)[4]= g[Y2];
  677. ((uint8_t*)dest)[5]= b[Y2];
  678. dest+=6;
  679. }
  680. break;
  681. case PIX_FMT_BGR24:
  682. YSCALE_YUV_2_RGBX_C(uint8_t)
  683. ((uint8_t*)dest)[0]= b[Y1];
  684. ((uint8_t*)dest)[1]= g[Y1];
  685. ((uint8_t*)dest)[2]= r[Y1];
  686. ((uint8_t*)dest)[3]= b[Y2];
  687. ((uint8_t*)dest)[4]= g[Y2];
  688. ((uint8_t*)dest)[5]= r[Y2];
  689. dest+=6;
  690. }
  691. break;
  692. case PIX_FMT_RGB565:
  693. case PIX_FMT_BGR565:
  694. {
  695. const int dr1= dither_2x2_8[y&1 ][0];
  696. const int dg1= dither_2x2_4[y&1 ][0];
  697. const int db1= dither_2x2_8[(y&1)^1][0];
  698. const int dr2= dither_2x2_8[y&1 ][1];
  699. const int dg2= dither_2x2_4[y&1 ][1];
  700. const int db2= dither_2x2_8[(y&1)^1][1];
  701. YSCALE_YUV_2_RGBX_C(uint16_t)
  702. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  703. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  704. }
  705. }
  706. break;
  707. case PIX_FMT_RGB555:
  708. case PIX_FMT_BGR555:
  709. {
  710. const int dr1= dither_2x2_8[y&1 ][0];
  711. const int dg1= dither_2x2_8[y&1 ][1];
  712. const int db1= dither_2x2_8[(y&1)^1][0];
  713. const int dr2= dither_2x2_8[y&1 ][1];
  714. const int dg2= dither_2x2_8[y&1 ][0];
  715. const int db2= dither_2x2_8[(y&1)^1][1];
  716. YSCALE_YUV_2_RGBX_C(uint16_t)
  717. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  718. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  719. }
  720. }
  721. break;
  722. case PIX_FMT_RGB8:
  723. case PIX_FMT_BGR8:
  724. {
  725. const uint8_t * const d64= dither_8x8_73[y&7];
  726. const uint8_t * const d32= dither_8x8_32[y&7];
  727. YSCALE_YUV_2_RGBX_C(uint8_t)
  728. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];
  729. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];
  730. }
  731. }
  732. break;
  733. case PIX_FMT_RGB4:
  734. case PIX_FMT_BGR4:
  735. {
  736. const uint8_t * const d64= dither_8x8_73 [y&7];
  737. const uint8_t * const d128=dither_8x8_220[y&7];
  738. YSCALE_YUV_2_RGBX_C(uint8_t)
  739. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]
  740. +((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);
  741. }
  742. }
  743. break;
  744. case PIX_FMT_RGB4_BYTE:
  745. case PIX_FMT_BGR4_BYTE:
  746. {
  747. const uint8_t * const d64= dither_8x8_73 [y&7];
  748. const uint8_t * const d128=dither_8x8_220[y&7];
  749. YSCALE_YUV_2_RGBX_C(uint8_t)
  750. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];
  751. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];
  752. }
  753. }
  754. break;
  755. case PIX_FMT_MONOBLACK:
  756. {
  757. const uint8_t * const d128=dither_8x8_220[y&7];
  758. uint8_t *g= c->table_gU[128] + c->table_gV[128];
  759. int acc=0;
  760. for (i=0; i<dstW-1; i+=2){
  761. int j;
  762. int Y1=1<<18;
  763. int Y2=1<<18;
  764. for (j=0; j<lumFilterSize; j++)
  765. {
  766. Y1 += lumSrc[j][i] * lumFilter[j];
  767. Y2 += lumSrc[j][i+1] * lumFilter[j];
  768. }
  769. Y1>>=19;
  770. Y2>>=19;
  771. if ((Y1|Y2)&256)
  772. {
  773. if (Y1>255) Y1=255;
  774. else if (Y1<0)Y1=0;
  775. if (Y2>255) Y2=255;
  776. else if (Y2<0)Y2=0;
  777. }
  778. acc+= acc + g[Y1+d128[(i+0)&7]];
  779. acc+= acc + g[Y2+d128[(i+1)&7]];
  780. if ((i&7)==6){
  781. ((uint8_t*)dest)[0]= acc;
  782. dest++;
  783. }
  784. }
  785. }
  786. break;
  787. case PIX_FMT_YUYV422:
  788. YSCALE_YUV_2_PACKEDX_C(void)
  789. ((uint8_t*)dest)[2*i2+0]= Y1;
  790. ((uint8_t*)dest)[2*i2+1]= U;
  791. ((uint8_t*)dest)[2*i2+2]= Y2;
  792. ((uint8_t*)dest)[2*i2+3]= V;
  793. }
  794. break;
  795. case PIX_FMT_UYVY422:
  796. YSCALE_YUV_2_PACKEDX_C(void)
  797. ((uint8_t*)dest)[2*i2+0]= U;
  798. ((uint8_t*)dest)[2*i2+1]= Y1;
  799. ((uint8_t*)dest)[2*i2+2]= V;
  800. ((uint8_t*)dest)[2*i2+3]= Y2;
  801. }
  802. break;
  803. }
  804. }
  805. //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  806. //Plain C versions
  807. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT) || !defined(CONFIG_GPL)
  808. #define COMPILE_C
  809. #endif
  810. #ifdef ARCH_POWERPC
  811. #if (defined (HAVE_ALTIVEC) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
  812. #define COMPILE_ALTIVEC
  813. #endif //HAVE_ALTIVEC
  814. #endif //ARCH_POWERPC
  815. #if defined(ARCH_X86)
  816. #if ((defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
  817. #define COMPILE_MMX
  818. #endif
  819. #if (defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
  820. #define COMPILE_MMX2
  821. #endif
  822. #if ((defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
  823. #define COMPILE_3DNOW
  824. #endif
  825. #endif //ARCH_X86 || ARCH_X86_64
  826. #undef HAVE_MMX
  827. #undef HAVE_MMX2
  828. #undef HAVE_3DNOW
  829. #ifdef COMPILE_C
  830. #undef HAVE_MMX
  831. #undef HAVE_MMX2
  832. #undef HAVE_3DNOW
  833. #undef HAVE_ALTIVEC
  834. #define RENAME(a) a ## _C
  835. #include "swscale_template.c"
  836. #endif
  837. #ifdef COMPILE_ALTIVEC
  838. #undef RENAME
  839. #define HAVE_ALTIVEC
  840. #define RENAME(a) a ## _altivec
  841. #include "swscale_template.c"
  842. #endif
  843. #if defined(ARCH_X86)
  844. //X86 versions
  845. /*
  846. #undef RENAME
  847. #undef HAVE_MMX
  848. #undef HAVE_MMX2
  849. #undef HAVE_3DNOW
  850. #define ARCH_X86
  851. #define RENAME(a) a ## _X86
  852. #include "swscale_template.c"
  853. */
  854. //MMX versions
  855. #ifdef COMPILE_MMX
  856. #undef RENAME
  857. #define HAVE_MMX
  858. #undef HAVE_MMX2
  859. #undef HAVE_3DNOW
  860. #define RENAME(a) a ## _MMX
  861. #include "swscale_template.c"
  862. #endif
  863. //MMX2 versions
  864. #ifdef COMPILE_MMX2
  865. #undef RENAME
  866. #define HAVE_MMX
  867. #define HAVE_MMX2
  868. #undef HAVE_3DNOW
  869. #define RENAME(a) a ## _MMX2
  870. #include "swscale_template.c"
  871. #endif
  872. //3DNOW versions
  873. #ifdef COMPILE_3DNOW
  874. #undef RENAME
  875. #define HAVE_MMX
  876. #undef HAVE_MMX2
  877. #define HAVE_3DNOW
  878. #define RENAME(a) a ## _3DNow
  879. #include "swscale_template.c"
  880. #endif
  881. #endif //ARCH_X86 || ARCH_X86_64
  882. // minor note: the HAVE_xyz is messed up after that line so don't use it
  883. static double getSplineCoeff(double a, double b, double c, double d, double dist)
  884. {
  885. // printf("%f %f %f %f %f\n", a,b,c,d,dist);
  886. if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
  887. else return getSplineCoeff( 0.0,
  888. b+ 2.0*c + 3.0*d,
  889. c + 3.0*d,
  890. -b- 3.0*c - 6.0*d,
  891. dist-1.0);
  892. }
  893. static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  894. int srcW, int dstW, int filterAlign, int one, int flags,
  895. SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
  896. {
  897. int i;
  898. int filterSize;
  899. int filter2Size;
  900. int minFilterSize;
  901. double *filter=NULL;
  902. double *filter2=NULL;
  903. #if defined(ARCH_X86)
  904. if (flags & SWS_CPU_CAPS_MMX)
  905. asm volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
  906. #endif
  907. // Note the +1 is for the MMXscaler which reads over the end
  908. *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
  909. if (FFABS(xInc - 0x10000) <10) // unscaled
  910. {
  911. int i;
  912. filterSize= 1;
  913. filter= av_malloc(dstW*sizeof(double)*filterSize);
  914. for (i=0; i<dstW*filterSize; i++) filter[i]=0;
  915. for (i=0; i<dstW; i++)
  916. {
  917. filter[i*filterSize]=1;
  918. (*filterPos)[i]=i;
  919. }
  920. }
  921. else if (flags&SWS_POINT) // lame looking point sampling mode
  922. {
  923. int i;
  924. int xDstInSrc;
  925. filterSize= 1;
  926. filter= av_malloc(dstW*sizeof(double)*filterSize);
  927. xDstInSrc= xInc/2 - 0x8000;
  928. for (i=0; i<dstW; i++)
  929. {
  930. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  931. (*filterPos)[i]= xx;
  932. filter[i]= 1.0;
  933. xDstInSrc+= xInc;
  934. }
  935. }
  936. else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
  937. {
  938. int i;
  939. int xDstInSrc;
  940. if (flags&SWS_BICUBIC) filterSize= 4;
  941. else if (flags&SWS_X ) filterSize= 4;
  942. else filterSize= 2; // SWS_BILINEAR / SWS_AREA
  943. filter= av_malloc(dstW*sizeof(double)*filterSize);
  944. xDstInSrc= xInc/2 - 0x8000;
  945. for (i=0; i<dstW; i++)
  946. {
  947. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  948. int j;
  949. (*filterPos)[i]= xx;
  950. //Bilinear upscale / linear interpolate / Area averaging
  951. for (j=0; j<filterSize; j++)
  952. {
  953. double d= FFABS((xx<<16) - xDstInSrc)/(double)(1<<16);
  954. double coeff= 1.0 - d;
  955. if (coeff<0) coeff=0;
  956. filter[i*filterSize + j]= coeff;
  957. xx++;
  958. }
  959. xDstInSrc+= xInc;
  960. }
  961. }
  962. else
  963. {
  964. double xDstInSrc;
  965. double sizeFactor, filterSizeInSrc;
  966. const double xInc1= (double)xInc / (double)(1<<16);
  967. if (flags&SWS_BICUBIC) sizeFactor= 4.0;
  968. else if (flags&SWS_X) sizeFactor= 8.0;
  969. else if (flags&SWS_AREA) sizeFactor= 1.0; //downscale only, for upscale it is bilinear
  970. else if (flags&SWS_GAUSS) sizeFactor= 8.0; // infinite ;)
  971. else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? 2.0*param[0] : 6.0;
  972. else if (flags&SWS_SINC) sizeFactor= 20.0; // infinite ;)
  973. else if (flags&SWS_SPLINE) sizeFactor= 20.0; // infinite ;)
  974. else if (flags&SWS_BILINEAR) sizeFactor= 2.0;
  975. else {
  976. sizeFactor= 0.0; //GCC warning killer
  977. assert(0);
  978. }
  979. if (xInc1 <= 1.0) filterSizeInSrc= sizeFactor; // upscale
  980. else filterSizeInSrc= sizeFactor*srcW / (double)dstW;
  981. filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
  982. if (filterSize > srcW-2) filterSize=srcW-2;
  983. filter= av_malloc(dstW*sizeof(double)*filterSize);
  984. xDstInSrc= xInc1 / 2.0 - 0.5;
  985. for (i=0; i<dstW; i++)
  986. {
  987. int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
  988. int j;
  989. (*filterPos)[i]= xx;
  990. for (j=0; j<filterSize; j++)
  991. {
  992. double d= FFABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
  993. double coeff;
  994. if (flags & SWS_BICUBIC)
  995. {
  996. double B= param[0] != SWS_PARAM_DEFAULT ? param[0] : 0.0;
  997. double C= param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6;
  998. if (d<1.0)
  999. coeff = (12-9*B-6*C)*d*d*d + (-18+12*B+6*C)*d*d + 6-2*B;
  1000. else if (d<2.0)
  1001. coeff = (-B-6*C)*d*d*d + (6*B+30*C)*d*d + (-12*B-48*C)*d +8*B+24*C;
  1002. else
  1003. coeff=0.0;
  1004. }
  1005. /* else if (flags & SWS_X)
  1006. {
  1007. double p= param ? param*0.01 : 0.3;
  1008. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  1009. coeff*= pow(2.0, - p*d*d);
  1010. }*/
  1011. else if (flags & SWS_X)
  1012. {
  1013. double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
  1014. if (d<1.0)
  1015. coeff = cos(d*PI);
  1016. else
  1017. coeff=-1.0;
  1018. if (coeff<0.0) coeff= -pow(-coeff, A);
  1019. else coeff= pow( coeff, A);
  1020. coeff= coeff*0.5 + 0.5;
  1021. }
  1022. else if (flags & SWS_AREA)
  1023. {
  1024. double srcPixelSize= 1.0/xInc1;
  1025. if (d + srcPixelSize/2 < 0.5) coeff= 1.0;
  1026. else if (d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
  1027. else coeff=0.0;
  1028. }
  1029. else if (flags & SWS_GAUSS)
  1030. {
  1031. double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  1032. coeff = pow(2.0, - p*d*d);
  1033. }
  1034. else if (flags & SWS_SINC)
  1035. {
  1036. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  1037. }
  1038. else if (flags & SWS_LANCZOS)
  1039. {
  1040. double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  1041. coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
  1042. if (d>p) coeff=0;
  1043. }
  1044. else if (flags & SWS_BILINEAR)
  1045. {
  1046. coeff= 1.0 - d;
  1047. if (coeff<0) coeff=0;
  1048. }
  1049. else if (flags & SWS_SPLINE)
  1050. {
  1051. double p=-2.196152422706632;
  1052. coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
  1053. }
  1054. else {
  1055. coeff= 0.0; //GCC warning killer
  1056. assert(0);
  1057. }
  1058. filter[i*filterSize + j]= coeff;
  1059. xx++;
  1060. }
  1061. xDstInSrc+= xInc1;
  1062. }
  1063. }
  1064. /* apply src & dst Filter to filter -> filter2
  1065. av_free(filter);
  1066. */
  1067. assert(filterSize>0);
  1068. filter2Size= filterSize;
  1069. if (srcFilter) filter2Size+= srcFilter->length - 1;
  1070. if (dstFilter) filter2Size+= dstFilter->length - 1;
  1071. assert(filter2Size>0);
  1072. filter2= av_malloc(filter2Size*dstW*sizeof(double));
  1073. for (i=0; i<dstW; i++)
  1074. {
  1075. int j;
  1076. SwsVector scaleFilter;
  1077. SwsVector *outVec;
  1078. scaleFilter.coeff= filter + i*filterSize;
  1079. scaleFilter.length= filterSize;
  1080. if (srcFilter) outVec= sws_getConvVec(srcFilter, &scaleFilter);
  1081. else outVec= &scaleFilter;
  1082. assert(outVec->length == filter2Size);
  1083. //FIXME dstFilter
  1084. for (j=0; j<outVec->length; j++)
  1085. {
  1086. filter2[i*filter2Size + j]= outVec->coeff[j];
  1087. }
  1088. (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
  1089. if (outVec != &scaleFilter) sws_freeVec(outVec);
  1090. }
  1091. av_free(filter); filter=NULL;
  1092. /* try to reduce the filter-size (step1 find size and shift left) */
  1093. // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
  1094. minFilterSize= 0;
  1095. for (i=dstW-1; i>=0; i--)
  1096. {
  1097. int min= filter2Size;
  1098. int j;
  1099. double cutOff=0.0;
  1100. /* get rid off near zero elements on the left by shifting left */
  1101. for (j=0; j<filter2Size; j++)
  1102. {
  1103. int k;
  1104. cutOff += FFABS(filter2[i*filter2Size]);
  1105. if (cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1106. /* preserve monotonicity because the core can't handle the filter otherwise */
  1107. if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
  1108. // Move filter coeffs left
  1109. for (k=1; k<filter2Size; k++)
  1110. filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
  1111. filter2[i*filter2Size + k - 1]= 0.0;
  1112. (*filterPos)[i]++;
  1113. }
  1114. cutOff=0.0;
  1115. /* count near zeros on the right */
  1116. for (j=filter2Size-1; j>0; j--)
  1117. {
  1118. cutOff += FFABS(filter2[i*filter2Size + j]);
  1119. if (cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1120. min--;
  1121. }
  1122. if (min>minFilterSize) minFilterSize= min;
  1123. }
  1124. if (flags & SWS_CPU_CAPS_ALTIVEC) {
  1125. // we can handle the special case 4,
  1126. // so we don't want to go to the full 8
  1127. if (minFilterSize < 5)
  1128. filterAlign = 4;
  1129. // we really don't want to waste our time
  1130. // doing useless computation, so fall-back on
  1131. // the scalar C code for very small filter.
  1132. // vectorizing is worth it only if you have
  1133. // decent-sized vector.
  1134. if (minFilterSize < 3)
  1135. filterAlign = 1;
  1136. }
  1137. if (flags & SWS_CPU_CAPS_MMX) {
  1138. // special case for unscaled vertical filtering
  1139. if (minFilterSize == 1 && filterAlign == 2)
  1140. filterAlign= 1;
  1141. }
  1142. assert(minFilterSize > 0);
  1143. filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
  1144. assert(filterSize > 0);
  1145. filter= av_malloc(filterSize*dstW*sizeof(double));
  1146. if (filterSize >= MAX_FILTER_SIZE)
  1147. return -1;
  1148. *outFilterSize= filterSize;
  1149. if (flags&SWS_PRINT_INFO)
  1150. av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
  1151. /* try to reduce the filter-size (step2 reduce it) */
  1152. for (i=0; i<dstW; i++)
  1153. {
  1154. int j;
  1155. for (j=0; j<filterSize; j++)
  1156. {
  1157. if (j>=filter2Size) filter[i*filterSize + j]= 0.0;
  1158. else filter[i*filterSize + j]= filter2[i*filter2Size + j];
  1159. }
  1160. }
  1161. av_free(filter2); filter2=NULL;
  1162. //FIXME try to align filterpos if possible
  1163. //fix borders
  1164. for (i=0; i<dstW; i++)
  1165. {
  1166. int j;
  1167. if ((*filterPos)[i] < 0)
  1168. {
  1169. // Move filter coeffs left to compensate for filterPos
  1170. for (j=1; j<filterSize; j++)
  1171. {
  1172. int left= FFMAX(j + (*filterPos)[i], 0);
  1173. filter[i*filterSize + left] += filter[i*filterSize + j];
  1174. filter[i*filterSize + j]=0;
  1175. }
  1176. (*filterPos)[i]= 0;
  1177. }
  1178. if ((*filterPos)[i] + filterSize > srcW)
  1179. {
  1180. int shift= (*filterPos)[i] + filterSize - srcW;
  1181. // Move filter coeffs right to compensate for filterPos
  1182. for (j=filterSize-2; j>=0; j--)
  1183. {
  1184. int right= FFMIN(j + shift, filterSize-1);
  1185. filter[i*filterSize +right] += filter[i*filterSize +j];
  1186. filter[i*filterSize +j]=0;
  1187. }
  1188. (*filterPos)[i]= srcW - filterSize;
  1189. }
  1190. }
  1191. // Note the +1 is for the MMXscaler which reads over the end
  1192. /* align at 16 for AltiVec (needed by hScale_altivec_real) */
  1193. *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
  1194. /* Normalize & Store in outFilter */
  1195. for (i=0; i<dstW; i++)
  1196. {
  1197. int j;
  1198. double error=0;
  1199. double sum=0;
  1200. double scale= one;
  1201. for (j=0; j<filterSize; j++)
  1202. {
  1203. sum+= filter[i*filterSize + j];
  1204. }
  1205. scale/= sum;
  1206. for (j=0; j<*outFilterSize; j++)
  1207. {
  1208. double v= filter[i*filterSize + j]*scale + error;
  1209. int intV= floor(v + 0.5);
  1210. (*outFilter)[i*(*outFilterSize) + j]= intV;
  1211. error = v - intV;
  1212. }
  1213. }
  1214. (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
  1215. for (i=0; i<*outFilterSize; i++)
  1216. {
  1217. int j= dstW*(*outFilterSize);
  1218. (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
  1219. }
  1220. av_free(filter);
  1221. return 0;
  1222. }
  1223. #ifdef COMPILE_MMX2
  1224. static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
  1225. {
  1226. uint8_t *fragmentA;
  1227. long imm8OfPShufW1A;
  1228. long imm8OfPShufW2A;
  1229. long fragmentLengthA;
  1230. uint8_t *fragmentB;
  1231. long imm8OfPShufW1B;
  1232. long imm8OfPShufW2B;
  1233. long fragmentLengthB;
  1234. int fragmentPos;
  1235. int xpos, i;
  1236. // create an optimized horizontal scaling routine
  1237. //code fragment
  1238. asm volatile(
  1239. "jmp 9f \n\t"
  1240. // Begin
  1241. "0: \n\t"
  1242. "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
  1243. "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
  1244. "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
  1245. "punpcklbw %%mm7, %%mm1 \n\t"
  1246. "punpcklbw %%mm7, %%mm0 \n\t"
  1247. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  1248. "1: \n\t"
  1249. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1250. "2: \n\t"
  1251. "psubw %%mm1, %%mm0 \n\t"
  1252. "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
  1253. "pmullw %%mm3, %%mm0 \n\t"
  1254. "psllw $7, %%mm1 \n\t"
  1255. "paddw %%mm1, %%mm0 \n\t"
  1256. "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
  1257. "add $8, %%"REG_a" \n\t"
  1258. // End
  1259. "9: \n\t"
  1260. // "int $3 \n\t"
  1261. "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
  1262. "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
  1263. "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
  1264. "dec %1 \n\t"
  1265. "dec %2 \n\t"
  1266. "sub %0, %1 \n\t"
  1267. "sub %0, %2 \n\t"
  1268. "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
  1269. "sub %0, %3 \n\t"
  1270. :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  1271. "=r" (fragmentLengthA)
  1272. );
  1273. asm volatile(
  1274. "jmp 9f \n\t"
  1275. // Begin
  1276. "0: \n\t"
  1277. "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
  1278. "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
  1279. "punpcklbw %%mm7, %%mm0 \n\t"
  1280. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  1281. "1: \n\t"
  1282. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1283. "2: \n\t"
  1284. "psubw %%mm1, %%mm0 \n\t"
  1285. "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
  1286. "pmullw %%mm3, %%mm0 \n\t"
  1287. "psllw $7, %%mm1 \n\t"
  1288. "paddw %%mm1, %%mm0 \n\t"
  1289. "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
  1290. "add $8, %%"REG_a" \n\t"
  1291. // End
  1292. "9: \n\t"
  1293. // "int $3 \n\t"
  1294. "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
  1295. "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
  1296. "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
  1297. "dec %1 \n\t"
  1298. "dec %2 \n\t"
  1299. "sub %0, %1 \n\t"
  1300. "sub %0, %2 \n\t"
  1301. "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
  1302. "sub %0, %3 \n\t"
  1303. :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  1304. "=r" (fragmentLengthB)
  1305. );
  1306. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  1307. fragmentPos=0;
  1308. for (i=0; i<dstW/numSplits; i++)
  1309. {
  1310. int xx=xpos>>16;
  1311. if ((i&3) == 0)
  1312. {
  1313. int a=0;
  1314. int b=((xpos+xInc)>>16) - xx;
  1315. int c=((xpos+xInc*2)>>16) - xx;
  1316. int d=((xpos+xInc*3)>>16) - xx;
  1317. filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
  1318. filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
  1319. filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
  1320. filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
  1321. filterPos[i/2]= xx;
  1322. if (d+1<4)
  1323. {
  1324. int maxShift= 3-(d+1);
  1325. int shift=0;
  1326. memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
  1327. funnyCode[fragmentPos + imm8OfPShufW1B]=
  1328. (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
  1329. funnyCode[fragmentPos + imm8OfPShufW2B]=
  1330. a | (b<<2) | (c<<4) | (d<<6);
  1331. if (i+3>=dstW) shift=maxShift; //avoid overread
  1332. else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
  1333. if (shift && i>=shift)
  1334. {
  1335. funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
  1336. funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
  1337. filterPos[i/2]-=shift;
  1338. }
  1339. fragmentPos+= fragmentLengthB;
  1340. }
  1341. else
  1342. {
  1343. int maxShift= 3-d;
  1344. int shift=0;
  1345. memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
  1346. funnyCode[fragmentPos + imm8OfPShufW1A]=
  1347. funnyCode[fragmentPos + imm8OfPShufW2A]=
  1348. a | (b<<2) | (c<<4) | (d<<6);
  1349. if (i+4>=dstW) shift=maxShift; //avoid overread
  1350. else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
  1351. if (shift && i>=shift)
  1352. {
  1353. funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
  1354. funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
  1355. filterPos[i/2]-=shift;
  1356. }
  1357. fragmentPos+= fragmentLengthA;
  1358. }
  1359. funnyCode[fragmentPos]= RET;
  1360. }
  1361. xpos+=xInc;
  1362. }
  1363. filterPos[i/2]= xpos>>16; // needed to jump to the next part
  1364. }
  1365. #endif /* COMPILE_MMX2 */
  1366. static void globalInit(void){
  1367. // generating tables:
  1368. int i;
  1369. for (i=0; i<768; i++){
  1370. int c= av_clip_uint8(i-256);
  1371. clip_table[i]=c;
  1372. }
  1373. }
  1374. static SwsFunc getSwsFunc(int flags){
  1375. #if defined(RUNTIME_CPUDETECT) && defined (CONFIG_GPL)
  1376. #if defined(ARCH_X86)
  1377. // ordered per speed fastest first
  1378. if (flags & SWS_CPU_CAPS_MMX2)
  1379. return swScale_MMX2;
  1380. else if (flags & SWS_CPU_CAPS_3DNOW)
  1381. return swScale_3DNow;
  1382. else if (flags & SWS_CPU_CAPS_MMX)
  1383. return swScale_MMX;
  1384. else
  1385. return swScale_C;
  1386. #else
  1387. #ifdef ARCH_POWERPC
  1388. if (flags & SWS_CPU_CAPS_ALTIVEC)
  1389. return swScale_altivec;
  1390. else
  1391. return swScale_C;
  1392. #endif
  1393. return swScale_C;
  1394. #endif /* defined(ARCH_X86) */
  1395. #else //RUNTIME_CPUDETECT
  1396. #ifdef HAVE_MMX2
  1397. return swScale_MMX2;
  1398. #elif defined (HAVE_3DNOW)
  1399. return swScale_3DNow;
  1400. #elif defined (HAVE_MMX)
  1401. return swScale_MMX;
  1402. #elif defined (HAVE_ALTIVEC)
  1403. return swScale_altivec;
  1404. #else
  1405. return swScale_C;
  1406. #endif
  1407. #endif //!RUNTIME_CPUDETECT
  1408. }
  1409. static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1410. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1411. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1412. /* Copy Y plane */
  1413. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1414. memcpy(dst, src[0], srcSliceH*dstStride[0]);
  1415. else
  1416. {
  1417. int i;
  1418. uint8_t *srcPtr= src[0];
  1419. uint8_t *dstPtr= dst;
  1420. for (i=0; i<srcSliceH; i++)
  1421. {
  1422. memcpy(dstPtr, srcPtr, c->srcW);
  1423. srcPtr+= srcStride[0];
  1424. dstPtr+= dstStride[0];
  1425. }
  1426. }
  1427. dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1428. if (c->dstFormat == PIX_FMT_NV12)
  1429. interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
  1430. else
  1431. interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
  1432. return srcSliceH;
  1433. }
  1434. static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1435. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1436. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1437. yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1438. return srcSliceH;
  1439. }
  1440. static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1441. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1442. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1443. yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1444. return srcSliceH;
  1445. }
  1446. /* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
  1447. static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1448. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1449. const int srcFormat= c->srcFormat;
  1450. const int dstFormat= c->dstFormat;
  1451. const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
  1452. const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
  1453. const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  1454. const int dstId= fmt_depth(dstFormat) >> 2;
  1455. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1456. /* BGR -> BGR */
  1457. if ( (isBGR(srcFormat) && isBGR(dstFormat))
  1458. || (isRGB(srcFormat) && isRGB(dstFormat))){
  1459. switch(srcId | (dstId<<4)){
  1460. case 0x34: conv= rgb16to15; break;
  1461. case 0x36: conv= rgb24to15; break;
  1462. case 0x38: conv= rgb32to15; break;
  1463. case 0x43: conv= rgb15to16; break;
  1464. case 0x46: conv= rgb24to16; break;
  1465. case 0x48: conv= rgb32to16; break;
  1466. case 0x63: conv= rgb15to24; break;
  1467. case 0x64: conv= rgb16to24; break;
  1468. case 0x68: conv= rgb32to24; break;
  1469. case 0x83: conv= rgb15to32; break;
  1470. case 0x84: conv= rgb16to32; break;
  1471. case 0x86: conv= rgb24to32; break;
  1472. default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1473. sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
  1474. }
  1475. }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
  1476. || (isRGB(srcFormat) && isBGR(dstFormat))){
  1477. switch(srcId | (dstId<<4)){
  1478. case 0x33: conv= rgb15tobgr15; break;
  1479. case 0x34: conv= rgb16tobgr15; break;
  1480. case 0x36: conv= rgb24tobgr15; break;
  1481. case 0x38: conv= rgb32tobgr15; break;
  1482. case 0x43: conv= rgb15tobgr16; break;
  1483. case 0x44: conv= rgb16tobgr16; break;
  1484. case 0x46: conv= rgb24tobgr16; break;
  1485. case 0x48: conv= rgb32tobgr16; break;
  1486. case 0x63: conv= rgb15tobgr24; break;
  1487. case 0x64: conv= rgb16tobgr24; break;
  1488. case 0x66: conv= rgb24tobgr24; break;
  1489. case 0x68: conv= rgb32tobgr24; break;
  1490. case 0x83: conv= rgb15tobgr32; break;
  1491. case 0x84: conv= rgb16tobgr32; break;
  1492. case 0x86: conv= rgb24tobgr32; break;
  1493. case 0x88: conv= rgb32tobgr32; break;
  1494. default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1495. sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
  1496. }
  1497. }else{
  1498. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1499. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1500. }
  1501. if(conv)
  1502. {
  1503. if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
  1504. conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1505. else
  1506. {
  1507. int i;
  1508. uint8_t *srcPtr= src[0];
  1509. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1510. for (i=0; i<srcSliceH; i++)
  1511. {
  1512. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1513. srcPtr+= srcStride[0];
  1514. dstPtr+= dstStride[0];
  1515. }
  1516. }
  1517. }
  1518. return srcSliceH;
  1519. }
  1520. static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1521. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1522. rgb24toyv12(
  1523. src[0],
  1524. dst[0]+ srcSliceY *dstStride[0],
  1525. dst[1]+(srcSliceY>>1)*dstStride[1],
  1526. dst[2]+(srcSliceY>>1)*dstStride[2],
  1527. c->srcW, srcSliceH,
  1528. dstStride[0], dstStride[1], srcStride[0]);
  1529. return srcSliceH;
  1530. }
  1531. static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1532. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1533. int i;
  1534. /* copy Y */
  1535. if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
  1536. memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
  1537. else{
  1538. uint8_t *srcPtr= src[0];
  1539. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1540. for (i=0; i<srcSliceH; i++)
  1541. {
  1542. memcpy(dstPtr, srcPtr, c->srcW);
  1543. srcPtr+= srcStride[0];
  1544. dstPtr+= dstStride[0];
  1545. }
  1546. }
  1547. if (c->dstFormat==PIX_FMT_YUV420P){
  1548. planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
  1549. planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
  1550. }else{
  1551. planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
  1552. planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
  1553. }
  1554. return srcSliceH;
  1555. }
  1556. /* unscaled copy like stuff (assumes nearly identical formats) */
  1557. static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1558. int srcSliceH, uint8_t* dst[], int dstStride[])
  1559. {
  1560. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1561. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1562. else
  1563. {
  1564. int i;
  1565. uint8_t *srcPtr= src[0];
  1566. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1567. int length=0;
  1568. /* universal length finder */
  1569. while(length+c->srcW <= FFABS(dstStride[0])
  1570. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  1571. assert(length!=0);
  1572. for (i=0; i<srcSliceH; i++)
  1573. {
  1574. memcpy(dstPtr, srcPtr, length);
  1575. srcPtr+= srcStride[0];
  1576. dstPtr+= dstStride[0];
  1577. }
  1578. }
  1579. return srcSliceH;
  1580. }
  1581. static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1582. int srcSliceH, uint8_t* dst[], int dstStride[])
  1583. {
  1584. int plane;
  1585. for (plane=0; plane<3; plane++)
  1586. {
  1587. int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1588. int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1589. int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1590. if ((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
  1591. {
  1592. if (!isGray(c->dstFormat))
  1593. memset(dst[plane], 128, dstStride[plane]*height);
  1594. }
  1595. else
  1596. {
  1597. if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
  1598. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1599. else
  1600. {
  1601. int i;
  1602. uint8_t *srcPtr= src[plane];
  1603. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1604. for (i=0; i<height; i++)
  1605. {
  1606. memcpy(dstPtr, srcPtr, length);
  1607. srcPtr+= srcStride[plane];
  1608. dstPtr+= dstStride[plane];
  1609. }
  1610. }
  1611. }
  1612. }
  1613. return srcSliceH;
  1614. }
  1615. static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1616. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1617. int length= c->srcW;
  1618. int y= srcSliceY;
  1619. int height= srcSliceH;
  1620. int i, j;
  1621. uint8_t *srcPtr= src[0];
  1622. uint8_t *dstPtr= dst[0] + dstStride[0]*y;
  1623. if (!isGray(c->dstFormat)){
  1624. int height= -((-srcSliceH)>>c->chrDstVSubSample);
  1625. memset(dst[1], 128, dstStride[1]*height);
  1626. memset(dst[2], 128, dstStride[2]*height);
  1627. }
  1628. if (c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
  1629. for (i=0; i<height; i++)
  1630. {
  1631. for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
  1632. srcPtr+= srcStride[0];
  1633. dstPtr+= dstStride[0];
  1634. }
  1635. return srcSliceH;
  1636. }
  1637. static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1638. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1639. int length= c->srcW;
  1640. int y= srcSliceY;
  1641. int height= srcSliceH;
  1642. int i, j;
  1643. uint8_t *srcPtr= src[0];
  1644. uint8_t *dstPtr= dst[0] + dstStride[0]*y;
  1645. for (i=0; i<height; i++)
  1646. {
  1647. for (j=0; j<length; j++)
  1648. {
  1649. dstPtr[j<<1] = srcPtr[j];
  1650. dstPtr[(j<<1)+1] = srcPtr[j];
  1651. }
  1652. srcPtr+= srcStride[0];
  1653. dstPtr+= dstStride[0];
  1654. }
  1655. return srcSliceH;
  1656. }
  1657. static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1658. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1659. int length= c->srcW;
  1660. int y= srcSliceY;
  1661. int height= srcSliceH;
  1662. int i, j;
  1663. uint16_t *srcPtr= (uint16_t*)src[0];
  1664. uint16_t *dstPtr= (uint16_t*)(dst[0] + dstStride[0]*y/2);
  1665. for (i=0; i<height; i++)
  1666. {
  1667. for (j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
  1668. srcPtr+= srcStride[0]/2;
  1669. dstPtr+= dstStride[0]/2;
  1670. }
  1671. return srcSliceH;
  1672. }
  1673. static void getSubSampleFactors(int *h, int *v, int format){
  1674. switch(format){
  1675. case PIX_FMT_UYVY422:
  1676. case PIX_FMT_YUYV422:
  1677. *h=1;
  1678. *v=0;
  1679. break;
  1680. case PIX_FMT_YUV420P:
  1681. case PIX_FMT_YUVA420P:
  1682. case PIX_FMT_GRAY16BE:
  1683. case PIX_FMT_GRAY16LE:
  1684. case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
  1685. case PIX_FMT_NV12:
  1686. case PIX_FMT_NV21:
  1687. *h=1;
  1688. *v=1;
  1689. break;
  1690. case PIX_FMT_YUV440P:
  1691. *h=0;
  1692. *v=1;
  1693. break;
  1694. case PIX_FMT_YUV410P:
  1695. *h=2;
  1696. *v=2;
  1697. break;
  1698. case PIX_FMT_YUV444P:
  1699. *h=0;
  1700. *v=0;
  1701. break;
  1702. case PIX_FMT_YUV422P:
  1703. *h=1;
  1704. *v=0;
  1705. break;
  1706. case PIX_FMT_YUV411P:
  1707. *h=2;
  1708. *v=0;
  1709. break;
  1710. default:
  1711. *h=0;
  1712. *v=0;
  1713. break;
  1714. }
  1715. }
  1716. static uint16_t roundToInt16(int64_t f){
  1717. int r= (f + (1<<15))>>16;
  1718. if (r<-0x7FFF) return 0x8000;
  1719. else if (r> 0x7FFF) return 0x7FFF;
  1720. else return r;
  1721. }
  1722. /**
  1723. * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
  1724. * @param fullRange if 1 then the luma range is 0..255 if 0 it is 16..235
  1725. * @return -1 if not supported
  1726. */
  1727. int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
  1728. int64_t crv = inv_table[0];
  1729. int64_t cbu = inv_table[1];
  1730. int64_t cgu = -inv_table[2];
  1731. int64_t cgv = -inv_table[3];
  1732. int64_t cy = 1<<16;
  1733. int64_t oy = 0;
  1734. if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  1735. memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
  1736. memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
  1737. c->brightness= brightness;
  1738. c->contrast = contrast;
  1739. c->saturation= saturation;
  1740. c->srcRange = srcRange;
  1741. c->dstRange = dstRange;
  1742. c->uOffset= 0x0400040004000400LL;
  1743. c->vOffset= 0x0400040004000400LL;
  1744. if (!srcRange){
  1745. cy= (cy*255) / 219;
  1746. oy= 16<<16;
  1747. }else{
  1748. crv= (crv*224) / 255;
  1749. cbu= (cbu*224) / 255;
  1750. cgu= (cgu*224) / 255;
  1751. cgv= (cgv*224) / 255;
  1752. }
  1753. cy = (cy *contrast )>>16;
  1754. crv= (crv*contrast * saturation)>>32;
  1755. cbu= (cbu*contrast * saturation)>>32;
  1756. cgu= (cgu*contrast * saturation)>>32;
  1757. cgv= (cgv*contrast * saturation)>>32;
  1758. oy -= 256*brightness;
  1759. c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
  1760. c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
  1761. c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
  1762. c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
  1763. c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
  1764. c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
  1765. yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
  1766. //FIXME factorize
  1767. #ifdef COMPILE_ALTIVEC
  1768. if (c->flags & SWS_CPU_CAPS_ALTIVEC)
  1769. yuv2rgb_altivec_init_tables (c, inv_table, brightness, contrast, saturation);
  1770. #endif
  1771. return 0;
  1772. }
  1773. /**
  1774. * @return -1 if not supported
  1775. */
  1776. int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
  1777. if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  1778. *inv_table = c->srcColorspaceTable;
  1779. *table = c->dstColorspaceTable;
  1780. *srcRange = c->srcRange;
  1781. *dstRange = c->dstRange;
  1782. *brightness= c->brightness;
  1783. *contrast = c->contrast;
  1784. *saturation= c->saturation;
  1785. return 0;
  1786. }
  1787. static int handle_jpeg(int *format)
  1788. {
  1789. switch (*format) {
  1790. case PIX_FMT_YUVJ420P:
  1791. *format = PIX_FMT_YUV420P;
  1792. return 1;
  1793. case PIX_FMT_YUVJ422P:
  1794. *format = PIX_FMT_YUV422P;
  1795. return 1;
  1796. case PIX_FMT_YUVJ444P:
  1797. *format = PIX_FMT_YUV444P;
  1798. return 1;
  1799. case PIX_FMT_YUVJ440P:
  1800. *format = PIX_FMT_YUV440P;
  1801. return 1;
  1802. default:
  1803. return 0;
  1804. }
  1805. }
  1806. SwsContext *sws_getContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
  1807. SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
  1808. SwsContext *c;
  1809. int i;
  1810. int usesVFilter, usesHFilter;
  1811. int unscaled, needsDither;
  1812. int srcRange, dstRange;
  1813. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  1814. #if defined(ARCH_X86)
  1815. if (flags & SWS_CPU_CAPS_MMX)
  1816. asm volatile("emms\n\t"::: "memory");
  1817. #endif
  1818. #if !defined(RUNTIME_CPUDETECT) || !defined (CONFIG_GPL) //ensure that the flags match the compiled variant if cpudetect is off
  1819. flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
  1820. #ifdef HAVE_MMX2
  1821. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1822. #elif defined (HAVE_3DNOW)
  1823. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1824. #elif defined (HAVE_MMX)
  1825. flags |= SWS_CPU_CAPS_MMX;
  1826. #elif defined (HAVE_ALTIVEC)
  1827. flags |= SWS_CPU_CAPS_ALTIVEC;
  1828. #elif defined (ARCH_BFIN)
  1829. flags |= SWS_CPU_CAPS_BFIN;
  1830. #endif
  1831. #endif /* RUNTIME_CPUDETECT */
  1832. if (clip_table[512] != 255) globalInit();
  1833. if (!rgb15to16) sws_rgb2rgb_init(flags);
  1834. unscaled = (srcW == dstW && srcH == dstH);
  1835. needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
  1836. && (fmt_depth(dstFormat))<24
  1837. && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
  1838. srcRange = handle_jpeg(&srcFormat);
  1839. dstRange = handle_jpeg(&dstFormat);
  1840. if (!isSupportedIn(srcFormat))
  1841. {
  1842. av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
  1843. return NULL;
  1844. }
  1845. if (!isSupportedOut(dstFormat))
  1846. {
  1847. av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
  1848. return NULL;
  1849. }
  1850. /* sanity check */
  1851. 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
  1852. {
  1853. av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  1854. srcW, srcH, dstW, dstH);
  1855. return NULL;
  1856. }
  1857. if(srcW > VOFW || dstW > VOFW){
  1858. av_log(NULL, AV_LOG_ERROR, "swScaler: Compile time max width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
  1859. return NULL;
  1860. }
  1861. if (!dstFilter) dstFilter= &dummyFilter;
  1862. if (!srcFilter) srcFilter= &dummyFilter;
  1863. c= av_mallocz(sizeof(SwsContext));
  1864. c->av_class = &sws_context_class;
  1865. c->srcW= srcW;
  1866. c->srcH= srcH;
  1867. c->dstW= dstW;
  1868. c->dstH= dstH;
  1869. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  1870. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  1871. c->flags= flags;
  1872. c->dstFormat= dstFormat;
  1873. c->srcFormat= srcFormat;
  1874. c->vRounder= 4* 0x0001000100010001ULL;
  1875. usesHFilter= usesVFilter= 0;
  1876. if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
  1877. if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
  1878. if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
  1879. if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
  1880. if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
  1881. if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
  1882. if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
  1883. if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
  1884. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  1885. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  1886. // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
  1887. if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  1888. // drop some chroma lines if the user wants it
  1889. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  1890. c->chrSrcVSubSample+= c->vChrDrop;
  1891. // drop every 2. pixel for chroma calculation unless user wants full chroma
  1892. if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
  1893. && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
  1894. && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
  1895. && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE)
  1896. c->chrSrcHSubSample=1;
  1897. if (param){
  1898. c->param[0] = param[0];
  1899. c->param[1] = param[1];
  1900. }else{
  1901. c->param[0] =
  1902. c->param[1] = SWS_PARAM_DEFAULT;
  1903. }
  1904. c->chrIntHSubSample= c->chrDstHSubSample;
  1905. c->chrIntVSubSample= c->chrSrcVSubSample;
  1906. // Note the -((-x)>>y) is so that we always round toward +inf.
  1907. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  1908. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  1909. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  1910. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  1911. sws_setColorspaceDetails(c, Inverse_Table_6_9[SWS_CS_DEFAULT], srcRange, Inverse_Table_6_9[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
  1912. /* unscaled special Cases */
  1913. if (unscaled && !usesHFilter && !usesVFilter)
  1914. {
  1915. /* yv12_to_nv12 */
  1916. if (srcFormat == PIX_FMT_YUV420P && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
  1917. {
  1918. c->swScale= PlanarToNV12Wrapper;
  1919. }
  1920. #ifdef CONFIG_GPL
  1921. /* yuv2bgr */
  1922. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P) && (isBGR(dstFormat) || isRGB(dstFormat)))
  1923. {
  1924. c->swScale= yuv2rgb_get_func_ptr(c);
  1925. }
  1926. #endif
  1927. if (srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P)
  1928. {
  1929. c->swScale= yvu9toyv12Wrapper;
  1930. }
  1931. /* bgr24toYV12 */
  1932. if (srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P)
  1933. c->swScale= bgr24toyv12Wrapper;
  1934. /* rgb/bgr -> rgb/bgr (no dither needed forms) */
  1935. if ( (isBGR(srcFormat) || isRGB(srcFormat))
  1936. && (isBGR(dstFormat) || isRGB(dstFormat))
  1937. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  1938. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  1939. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  1940. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  1941. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  1942. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  1943. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  1944. && !needsDither)
  1945. c->swScale= rgb2rgbWrapper;
  1946. /* LQ converters if -sws 0 or -sws 4*/
  1947. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
  1948. /* rgb/bgr -> rgb/bgr (dither needed forms) */
  1949. if ( (isBGR(srcFormat) || isRGB(srcFormat))
  1950. && (isBGR(dstFormat) || isRGB(dstFormat))
  1951. && needsDither)
  1952. c->swScale= rgb2rgbWrapper;
  1953. /* yv12_to_yuy2 */
  1954. if (srcFormat == PIX_FMT_YUV420P &&
  1955. (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422))
  1956. {
  1957. if (dstFormat == PIX_FMT_YUYV422)
  1958. c->swScale= PlanarToYuy2Wrapper;
  1959. else
  1960. c->swScale= PlanarToUyvyWrapper;
  1961. }
  1962. }
  1963. #ifdef COMPILE_ALTIVEC
  1964. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  1965. ((srcFormat == PIX_FMT_YUV420P &&
  1966. (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422)))) {
  1967. // unscaled YV12 -> packed YUV, we want speed
  1968. if (dstFormat == PIX_FMT_YUYV422)
  1969. c->swScale= yv12toyuy2_unscaled_altivec;
  1970. else
  1971. c->swScale= yv12touyvy_unscaled_altivec;
  1972. }
  1973. #endif
  1974. /* simple copy */
  1975. if ( srcFormat == dstFormat
  1976. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1977. || (isPlanarYUV(dstFormat) && isGray(srcFormat)))
  1978. {
  1979. if (isPacked(c->srcFormat))
  1980. c->swScale= packedCopy;
  1981. else /* Planar YUV or gray */
  1982. c->swScale= planarCopy;
  1983. }
  1984. /* gray16{le,be} conversions */
  1985. if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
  1986. {
  1987. c->swScale= gray16togray;
  1988. }
  1989. if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
  1990. {
  1991. c->swScale= graytogray16;
  1992. }
  1993. if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
  1994. {
  1995. c->swScale= gray16swap;
  1996. }
  1997. #ifdef ARCH_BFIN
  1998. if (flags & SWS_CPU_CAPS_BFIN)
  1999. ff_bfin_get_unscaled_swscale (c);
  2000. #endif
  2001. if (c->swScale){
  2002. if (flags&SWS_PRINT_INFO)
  2003. av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
  2004. sws_format_name(srcFormat), sws_format_name(dstFormat));
  2005. return c;
  2006. }
  2007. }
  2008. if (flags & SWS_CPU_CAPS_MMX2)
  2009. {
  2010. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  2011. if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
  2012. {
  2013. if (flags&SWS_PRINT_INFO)
  2014. av_log(c, AV_LOG_INFO, "output Width is not a multiple of 32 -> no MMX2 scaler\n");
  2015. }
  2016. if (usesHFilter) c->canMMX2BeUsed=0;
  2017. }
  2018. else
  2019. c->canMMX2BeUsed=0;
  2020. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  2021. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  2022. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  2023. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  2024. // n-2 is the last chrominance sample available
  2025. // this is not perfect, but no one should notice the difference, the more correct variant
  2026. // would be like the vertical one, but that would require some special code for the
  2027. // first and last pixel
  2028. if (flags&SWS_FAST_BILINEAR)
  2029. {
  2030. if (c->canMMX2BeUsed)
  2031. {
  2032. c->lumXInc+= 20;
  2033. c->chrXInc+= 20;
  2034. }
  2035. //we don't use the x86asm scaler if mmx is available
  2036. else if (flags & SWS_CPU_CAPS_MMX)
  2037. {
  2038. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  2039. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  2040. }
  2041. }
  2042. /* precalculate horizontal scaler filter coefficients */
  2043. {
  2044. const int filterAlign=
  2045. (flags & SWS_CPU_CAPS_MMX) ? 4 :
  2046. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  2047. 1;
  2048. initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  2049. srcW , dstW, filterAlign, 1<<14,
  2050. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  2051. srcFilter->lumH, dstFilter->lumH, c->param);
  2052. initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  2053. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  2054. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  2055. srcFilter->chrH, dstFilter->chrH, c->param);
  2056. #define MAX_FUNNY_CODE_SIZE 10000
  2057. #if defined(COMPILE_MMX2)
  2058. // can't downscale !!!
  2059. if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
  2060. {
  2061. #ifdef MAP_ANONYMOUS
  2062. c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  2063. c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  2064. #else
  2065. c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
  2066. c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
  2067. #endif
  2068. c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
  2069. c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
  2070. c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
  2071. c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
  2072. initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
  2073. initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
  2074. }
  2075. #endif /* defined(COMPILE_MMX2) */
  2076. } // Init Horizontal stuff
  2077. /* precalculate vertical scaler filter coefficients */
  2078. {
  2079. const int filterAlign=
  2080. (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
  2081. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  2082. 1;
  2083. initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  2084. srcH , dstH, filterAlign, (1<<12)-4,
  2085. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  2086. srcFilter->lumV, dstFilter->lumV, c->param);
  2087. initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  2088. c->chrSrcH, c->chrDstH, filterAlign, (1<<12)-4,
  2089. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  2090. srcFilter->chrV, dstFilter->chrV, c->param);
  2091. #ifdef HAVE_ALTIVEC
  2092. c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
  2093. c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
  2094. for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
  2095. int j;
  2096. short *p = (short *)&c->vYCoeffsBank[i];
  2097. for (j=0;j<8;j++)
  2098. p[j] = c->vLumFilter[i];
  2099. }
  2100. for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
  2101. int j;
  2102. short *p = (short *)&c->vCCoeffsBank[i];
  2103. for (j=0;j<8;j++)
  2104. p[j] = c->vChrFilter[i];
  2105. }
  2106. #endif
  2107. }
  2108. // Calculate Buffer Sizes so that they won't run out while handling these damn slices
  2109. c->vLumBufSize= c->vLumFilterSize;
  2110. c->vChrBufSize= c->vChrFilterSize;
  2111. for (i=0; i<dstH; i++)
  2112. {
  2113. int chrI= i*c->chrDstH / dstH;
  2114. int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  2115. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  2116. nextSlice>>= c->chrSrcVSubSample;
  2117. nextSlice<<= c->chrSrcVSubSample;
  2118. if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  2119. c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
  2120. if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  2121. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  2122. }
  2123. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  2124. c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
  2125. c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
  2126. //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)
  2127. /* align at 16 bytes for AltiVec */
  2128. for (i=0; i<c->vLumBufSize; i++)
  2129. c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
  2130. for (i=0; i<c->vChrBufSize; i++)
  2131. c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
  2132. //try to avoid drawing green stuff between the right end and the stride end
  2133. for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
  2134. assert(2*VOFW == VOF);
  2135. assert(c->chrDstH <= dstH);
  2136. if (flags&SWS_PRINT_INFO)
  2137. {
  2138. #ifdef DITHER1XBPP
  2139. const char *dither= " dithered";
  2140. #else
  2141. const char *dither= "";
  2142. #endif
  2143. if (flags&SWS_FAST_BILINEAR)
  2144. av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
  2145. else if (flags&SWS_BILINEAR)
  2146. av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
  2147. else if (flags&SWS_BICUBIC)
  2148. av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
  2149. else if (flags&SWS_X)
  2150. av_log(c, AV_LOG_INFO, "Experimental scaler, ");
  2151. else if (flags&SWS_POINT)
  2152. av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
  2153. else if (flags&SWS_AREA)
  2154. av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
  2155. else if (flags&SWS_BICUBLIN)
  2156. av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
  2157. else if (flags&SWS_GAUSS)
  2158. av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
  2159. else if (flags&SWS_SINC)
  2160. av_log(c, AV_LOG_INFO, "Sinc scaler, ");
  2161. else if (flags&SWS_LANCZOS)
  2162. av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
  2163. else if (flags&SWS_SPLINE)
  2164. av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
  2165. else
  2166. av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
  2167. if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
  2168. av_log(c, AV_LOG_INFO, "from %s to%s %s ",
  2169. sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
  2170. else
  2171. av_log(c, AV_LOG_INFO, "from %s to %s ",
  2172. sws_format_name(srcFormat), sws_format_name(dstFormat));
  2173. if (flags & SWS_CPU_CAPS_MMX2)
  2174. av_log(c, AV_LOG_INFO, "using MMX2\n");
  2175. else if (flags & SWS_CPU_CAPS_3DNOW)
  2176. av_log(c, AV_LOG_INFO, "using 3DNOW\n");
  2177. else if (flags & SWS_CPU_CAPS_MMX)
  2178. av_log(c, AV_LOG_INFO, "using MMX\n");
  2179. else if (flags & SWS_CPU_CAPS_ALTIVEC)
  2180. av_log(c, AV_LOG_INFO, "using AltiVec\n");
  2181. else
  2182. av_log(c, AV_LOG_INFO, "using C\n");
  2183. }
  2184. if (flags & SWS_PRINT_INFO)
  2185. {
  2186. if (flags & SWS_CPU_CAPS_MMX)
  2187. {
  2188. if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  2189. av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  2190. else
  2191. {
  2192. if (c->hLumFilterSize==4)
  2193. av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
  2194. else if (c->hLumFilterSize==8)
  2195. av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
  2196. else
  2197. av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
  2198. if (c->hChrFilterSize==4)
  2199. av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
  2200. else if (c->hChrFilterSize==8)
  2201. av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
  2202. else
  2203. av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
  2204. }
  2205. }
  2206. else
  2207. {
  2208. #if defined(ARCH_X86)
  2209. av_log(c, AV_LOG_VERBOSE, "using X86-Asm scaler for horizontal scaling\n");
  2210. #else
  2211. if (flags & SWS_FAST_BILINEAR)
  2212. av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
  2213. else
  2214. av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
  2215. #endif
  2216. }
  2217. if (isPlanarYUV(dstFormat))
  2218. {
  2219. if (c->vLumFilterSize==1)
  2220. av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2221. else
  2222. av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2223. }
  2224. else
  2225. {
  2226. if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
  2227. av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  2228. " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2229. else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
  2230. av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2231. else
  2232. av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2233. }
  2234. if (dstFormat==PIX_FMT_BGR24)
  2235. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 Converter\n",
  2236. (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
  2237. else if (dstFormat==PIX_FMT_RGB32)
  2238. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2239. else if (dstFormat==PIX_FMT_BGR565)
  2240. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2241. else if (dstFormat==PIX_FMT_BGR555)
  2242. av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2243. av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  2244. }
  2245. if (flags & SWS_PRINT_INFO)
  2246. {
  2247. av_log(c, AV_LOG_DEBUG, "Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2248. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  2249. av_log(c, AV_LOG_DEBUG, "Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2250. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  2251. }
  2252. c->swScale= getSwsFunc(flags);
  2253. return c;
  2254. }
  2255. /**
  2256. * swscale wrapper, so we don't need to export the SwsContext.
  2257. * assumes planar YUV to be in YUV order instead of YVU
  2258. */
  2259. int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  2260. int srcSliceH, uint8_t* dst[], int dstStride[]){
  2261. int i;
  2262. uint8_t* src2[4]= {src[0], src[1], src[2]};
  2263. uint32_t pal[256];
  2264. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  2265. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  2266. return 0;
  2267. }
  2268. if (c->sliceDir == 0) {
  2269. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  2270. }
  2271. if (c->srcFormat == PIX_FMT_PAL8){
  2272. for (i=0; i<256; i++){
  2273. int p= ((uint32_t*)(src[1]))[i];
  2274. int r= (p>>16)&0xFF;
  2275. int g= (p>> 8)&0xFF;
  2276. int b= p &0xFF;
  2277. int y= av_clip_uint8(((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16 );
  2278. int u= av_clip_uint8(((RU*r + GU*g + BU*b)>>RGB2YUV_SHIFT) + 128);
  2279. int v= av_clip_uint8(((RV*r + GV*g + BV*b)>>RGB2YUV_SHIFT) + 128);
  2280. pal[i]= y + (u<<8) + (v<<16);
  2281. }
  2282. src2[1]= (uint8_t*)pal;
  2283. }
  2284. // copy strides, so they can safely be modified
  2285. if (c->sliceDir == 1) {
  2286. // slices go from top to bottom
  2287. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2]};
  2288. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2]};
  2289. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
  2290. } else {
  2291. // slices go from bottom to top => we flip the image internally
  2292. uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
  2293. dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
  2294. dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2]};
  2295. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2]};
  2296. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2]};
  2297. src2[0] += (srcSliceH-1)*srcStride[0];
  2298. if (c->srcFormat != PIX_FMT_PAL8)
  2299. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  2300. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  2301. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  2302. }
  2303. }
  2304. /**
  2305. * swscale wrapper, so we don't need to export the SwsContext
  2306. */
  2307. int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  2308. int srcSliceH, uint8_t* dst[], int dstStride[]){
  2309. return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
  2310. }
  2311. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  2312. float lumaSharpen, float chromaSharpen,
  2313. float chromaHShift, float chromaVShift,
  2314. int verbose)
  2315. {
  2316. SwsFilter *filter= av_malloc(sizeof(SwsFilter));
  2317. if (lumaGBlur!=0.0){
  2318. filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
  2319. filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
  2320. }else{
  2321. filter->lumH= sws_getIdentityVec();
  2322. filter->lumV= sws_getIdentityVec();
  2323. }
  2324. if (chromaGBlur!=0.0){
  2325. filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
  2326. filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
  2327. }else{
  2328. filter->chrH= sws_getIdentityVec();
  2329. filter->chrV= sws_getIdentityVec();
  2330. }
  2331. if (chromaSharpen!=0.0){
  2332. SwsVector *id= sws_getIdentityVec();
  2333. sws_scaleVec(filter->chrH, -chromaSharpen);
  2334. sws_scaleVec(filter->chrV, -chromaSharpen);
  2335. sws_addVec(filter->chrH, id);
  2336. sws_addVec(filter->chrV, id);
  2337. sws_freeVec(id);
  2338. }
  2339. if (lumaSharpen!=0.0){
  2340. SwsVector *id= sws_getIdentityVec();
  2341. sws_scaleVec(filter->lumH, -lumaSharpen);
  2342. sws_scaleVec(filter->lumV, -lumaSharpen);
  2343. sws_addVec(filter->lumH, id);
  2344. sws_addVec(filter->lumV, id);
  2345. sws_freeVec(id);
  2346. }
  2347. if (chromaHShift != 0.0)
  2348. sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
  2349. if (chromaVShift != 0.0)
  2350. sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
  2351. sws_normalizeVec(filter->chrH, 1.0);
  2352. sws_normalizeVec(filter->chrV, 1.0);
  2353. sws_normalizeVec(filter->lumH, 1.0);
  2354. sws_normalizeVec(filter->lumV, 1.0);
  2355. if (verbose) sws_printVec(filter->chrH);
  2356. if (verbose) sws_printVec(filter->lumH);
  2357. return filter;
  2358. }
  2359. /**
  2360. * returns a normalized gaussian curve used to filter stuff
  2361. * quality=3 is high quality, lowwer is lowwer quality
  2362. */
  2363. SwsVector *sws_getGaussianVec(double variance, double quality){
  2364. const int length= (int)(variance*quality + 0.5) | 1;
  2365. int i;
  2366. double *coeff= av_malloc(length*sizeof(double));
  2367. double middle= (length-1)*0.5;
  2368. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2369. vec->coeff= coeff;
  2370. vec->length= length;
  2371. for (i=0; i<length; i++)
  2372. {
  2373. double dist= i-middle;
  2374. coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
  2375. }
  2376. sws_normalizeVec(vec, 1.0);
  2377. return vec;
  2378. }
  2379. SwsVector *sws_getConstVec(double c, int length){
  2380. int i;
  2381. double *coeff= av_malloc(length*sizeof(double));
  2382. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2383. vec->coeff= coeff;
  2384. vec->length= length;
  2385. for (i=0; i<length; i++)
  2386. coeff[i]= c;
  2387. return vec;
  2388. }
  2389. SwsVector *sws_getIdentityVec(void){
  2390. return sws_getConstVec(1.0, 1);
  2391. }
  2392. double sws_dcVec(SwsVector *a){
  2393. int i;
  2394. double sum=0;
  2395. for (i=0; i<a->length; i++)
  2396. sum+= a->coeff[i];
  2397. return sum;
  2398. }
  2399. void sws_scaleVec(SwsVector *a, double scalar){
  2400. int i;
  2401. for (i=0; i<a->length; i++)
  2402. a->coeff[i]*= scalar;
  2403. }
  2404. void sws_normalizeVec(SwsVector *a, double height){
  2405. sws_scaleVec(a, height/sws_dcVec(a));
  2406. }
  2407. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
  2408. int length= a->length + b->length - 1;
  2409. double *coeff= av_malloc(length*sizeof(double));
  2410. int i, j;
  2411. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2412. vec->coeff= coeff;
  2413. vec->length= length;
  2414. for (i=0; i<length; i++) coeff[i]= 0.0;
  2415. for (i=0; i<a->length; i++)
  2416. {
  2417. for (j=0; j<b->length; j++)
  2418. {
  2419. coeff[i+j]+= a->coeff[i]*b->coeff[j];
  2420. }
  2421. }
  2422. return vec;
  2423. }
  2424. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
  2425. int length= FFMAX(a->length, b->length);
  2426. double *coeff= av_malloc(length*sizeof(double));
  2427. int i;
  2428. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2429. vec->coeff= coeff;
  2430. vec->length= length;
  2431. for (i=0; i<length; i++) coeff[i]= 0.0;
  2432. for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2433. for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  2434. return vec;
  2435. }
  2436. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
  2437. int length= FFMAX(a->length, b->length);
  2438. double *coeff= av_malloc(length*sizeof(double));
  2439. int i;
  2440. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2441. vec->coeff= coeff;
  2442. vec->length= length;
  2443. for (i=0; i<length; i++) coeff[i]= 0.0;
  2444. for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2445. for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  2446. return vec;
  2447. }
  2448. /* shift left / or right if "shift" is negative */
  2449. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
  2450. int length= a->length + FFABS(shift)*2;
  2451. double *coeff= av_malloc(length*sizeof(double));
  2452. int i;
  2453. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2454. vec->coeff= coeff;
  2455. vec->length= length;
  2456. for (i=0; i<length; i++) coeff[i]= 0.0;
  2457. for (i=0; i<a->length; i++)
  2458. {
  2459. coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  2460. }
  2461. return vec;
  2462. }
  2463. void sws_shiftVec(SwsVector *a, int shift){
  2464. SwsVector *shifted= sws_getShiftedVec(a, shift);
  2465. av_free(a->coeff);
  2466. a->coeff= shifted->coeff;
  2467. a->length= shifted->length;
  2468. av_free(shifted);
  2469. }
  2470. void sws_addVec(SwsVector *a, SwsVector *b){
  2471. SwsVector *sum= sws_sumVec(a, b);
  2472. av_free(a->coeff);
  2473. a->coeff= sum->coeff;
  2474. a->length= sum->length;
  2475. av_free(sum);
  2476. }
  2477. void sws_subVec(SwsVector *a, SwsVector *b){
  2478. SwsVector *diff= sws_diffVec(a, b);
  2479. av_free(a->coeff);
  2480. a->coeff= diff->coeff;
  2481. a->length= diff->length;
  2482. av_free(diff);
  2483. }
  2484. void sws_convVec(SwsVector *a, SwsVector *b){
  2485. SwsVector *conv= sws_getConvVec(a, b);
  2486. av_free(a->coeff);
  2487. a->coeff= conv->coeff;
  2488. a->length= conv->length;
  2489. av_free(conv);
  2490. }
  2491. SwsVector *sws_cloneVec(SwsVector *a){
  2492. double *coeff= av_malloc(a->length*sizeof(double));
  2493. int i;
  2494. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2495. vec->coeff= coeff;
  2496. vec->length= a->length;
  2497. for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
  2498. return vec;
  2499. }
  2500. void sws_printVec(SwsVector *a){
  2501. int i;
  2502. double max=0;
  2503. double min=0;
  2504. double range;
  2505. for (i=0; i<a->length; i++)
  2506. if (a->coeff[i]>max) max= a->coeff[i];
  2507. for (i=0; i<a->length; i++)
  2508. if (a->coeff[i]<min) min= a->coeff[i];
  2509. range= max - min;
  2510. for (i=0; i<a->length; i++)
  2511. {
  2512. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  2513. av_log(NULL, AV_LOG_DEBUG, "%1.3f ", a->coeff[i]);
  2514. for (;x>0; x--) av_log(NULL, AV_LOG_DEBUG, " ");
  2515. av_log(NULL, AV_LOG_DEBUG, "|\n");
  2516. }
  2517. }
  2518. void sws_freeVec(SwsVector *a){
  2519. if (!a) return;
  2520. av_free(a->coeff);
  2521. a->coeff=NULL;
  2522. a->length=0;
  2523. av_free(a);
  2524. }
  2525. void sws_freeFilter(SwsFilter *filter){
  2526. if (!filter) return;
  2527. if (filter->lumH) sws_freeVec(filter->lumH);
  2528. if (filter->lumV) sws_freeVec(filter->lumV);
  2529. if (filter->chrH) sws_freeVec(filter->chrH);
  2530. if (filter->chrV) sws_freeVec(filter->chrV);
  2531. av_free(filter);
  2532. }
  2533. void sws_freeContext(SwsContext *c){
  2534. int i;
  2535. if (!c) return;
  2536. if (c->lumPixBuf)
  2537. {
  2538. for (i=0; i<c->vLumBufSize; i++)
  2539. {
  2540. av_free(c->lumPixBuf[i]);
  2541. c->lumPixBuf[i]=NULL;
  2542. }
  2543. av_free(c->lumPixBuf);
  2544. c->lumPixBuf=NULL;
  2545. }
  2546. if (c->chrPixBuf)
  2547. {
  2548. for (i=0; i<c->vChrBufSize; i++)
  2549. {
  2550. av_free(c->chrPixBuf[i]);
  2551. c->chrPixBuf[i]=NULL;
  2552. }
  2553. av_free(c->chrPixBuf);
  2554. c->chrPixBuf=NULL;
  2555. }
  2556. av_free(c->vLumFilter);
  2557. c->vLumFilter = NULL;
  2558. av_free(c->vChrFilter);
  2559. c->vChrFilter = NULL;
  2560. av_free(c->hLumFilter);
  2561. c->hLumFilter = NULL;
  2562. av_free(c->hChrFilter);
  2563. c->hChrFilter = NULL;
  2564. #ifdef HAVE_ALTIVEC
  2565. av_free(c->vYCoeffsBank);
  2566. c->vYCoeffsBank = NULL;
  2567. av_free(c->vCCoeffsBank);
  2568. c->vCCoeffsBank = NULL;
  2569. #endif
  2570. av_free(c->vLumFilterPos);
  2571. c->vLumFilterPos = NULL;
  2572. av_free(c->vChrFilterPos);
  2573. c->vChrFilterPos = NULL;
  2574. av_free(c->hLumFilterPos);
  2575. c->hLumFilterPos = NULL;
  2576. av_free(c->hChrFilterPos);
  2577. c->hChrFilterPos = NULL;
  2578. #if defined(ARCH_X86) && defined(CONFIG_GPL)
  2579. #ifdef MAP_ANONYMOUS
  2580. if (c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
  2581. if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
  2582. #else
  2583. av_free(c->funnyYCode);
  2584. av_free(c->funnyUVCode);
  2585. #endif
  2586. c->funnyYCode=NULL;
  2587. c->funnyUVCode=NULL;
  2588. #endif /* defined(ARCH_X86) */
  2589. av_free(c->lumMmx2Filter);
  2590. c->lumMmx2Filter=NULL;
  2591. av_free(c->chrMmx2Filter);
  2592. c->chrMmx2Filter=NULL;
  2593. av_free(c->lumMmx2FilterPos);
  2594. c->lumMmx2FilterPos=NULL;
  2595. av_free(c->chrMmx2FilterPos);
  2596. c->chrMmx2FilterPos=NULL;
  2597. av_free(c->yuvTable);
  2598. c->yuvTable=NULL;
  2599. av_free(c);
  2600. }
  2601. /**
  2602. * Checks if context is valid or reallocs a new one instead.
  2603. * If context is NULL, just calls sws_getContext() to get a new one.
  2604. * Otherwise, checks if the parameters are the same already saved in context.
  2605. * If that is the case, returns the current context.
  2606. * Otherwise, frees context and gets a new one.
  2607. *
  2608. * Be warned that srcFilter, dstFilter are not checked, they are
  2609. * asumed to remain valid.
  2610. */
  2611. struct SwsContext *sws_getCachedContext(struct SwsContext *context,
  2612. int srcW, int srcH, int srcFormat,
  2613. int dstW, int dstH, int dstFormat, int flags,
  2614. SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
  2615. {
  2616. static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
  2617. if (!param)
  2618. param = default_param;
  2619. if (context) {
  2620. if (context->srcW != srcW || context->srcH != srcH ||
  2621. context->srcFormat != srcFormat ||
  2622. context->dstW != dstW || context->dstH != dstH ||
  2623. context->dstFormat != dstFormat || context->flags != flags ||
  2624. context->param[0] != param[0] || context->param[1] != param[1])
  2625. {
  2626. sws_freeContext(context);
  2627. context = NULL;
  2628. }
  2629. }
  2630. if (!context) {
  2631. return sws_getContext(srcW, srcH, srcFormat,
  2632. dstW, dstH, dstFormat, flags,
  2633. srcFilter, dstFilter, param);
  2634. }
  2635. return context;
  2636. }