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