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.

2898 lines
98KB

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