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.

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