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.

2862 lines
81KB

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