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.

2021 lines
55KB

  1. /*
  2. Copyright (C) 2001-2002 Michael Niedermayer <michaelni@gmx.at>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /*
  16. supported Input formats: YV12, I420, IYUV, YUY2, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8, Y800
  17. supported output formats: YV12, I420, IYUV, BGR15, BGR16, BGR24, BGR32 (grayscale soon too)
  18. BGR15/16 support dithering
  19. unscaled special converters
  20. YV12/I420/IYUV -> BGR15/BGR16/BGR24/BGR32
  21. YV12/I420/IYUV -> YV12/I420/IYUV
  22. YUY2/BGR15/BGR16/BGR24/BGR32/RGB24/RGB32 -> same format
  23. BGR24 -> BGR32 & RGB24 -> RGB32
  24. BGR32 -> BGR24 & RGB32 -> RGB24
  25. BGR15 -> BGR16
  26. */
  27. /*
  28. tested special converters
  29. YV12/I420 -> BGR16
  30. YV12 -> YV12
  31. BGR15 -> BGR16
  32. BGR16 -> BGR16
  33. untested special converters
  34. YV12/I420 -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
  35. YV12/I420 -> YV12/I420
  36. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  37. BGR24 -> BGR32 & RGB24 -> RGB32
  38. BGR32 -> BGR24 & RGB32 -> RGB24
  39. BGR24 -> YV12
  40. */
  41. #include <inttypes.h>
  42. #include <string.h>
  43. #include <math.h>
  44. #include <stdio.h>
  45. #include "../config.h"
  46. #include "../mangle.h"
  47. #include <assert.h>
  48. #ifdef HAVE_MALLOC_H
  49. #include <malloc.h>
  50. #endif
  51. #include "swscale.h"
  52. #include "../cpudetect.h"
  53. #include "../bswap.h"
  54. #include "../libvo/img_format.h"
  55. #include "rgb2rgb.h"
  56. #include "../libvo/fastmemcpy.h"
  57. #undef MOVNTQ
  58. #undef PAVGB
  59. //#undef HAVE_MMX2
  60. //#define HAVE_3DNOW
  61. //#undef HAVE_MMX
  62. //#undef ARCH_X86
  63. //#define WORDS_BIGENDIAN
  64. #define DITHER1XBPP
  65. #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
  66. #define RET 0xC3 //near return opcode for X86
  67. #ifdef MP_DEBUG
  68. #define ASSERT(x) assert(x);
  69. #else
  70. #define ASSERT(x) ;
  71. #endif
  72. #ifdef M_PI
  73. #define PI M_PI
  74. #else
  75. #define PI 3.14159265358979323846
  76. #endif
  77. //FIXME replace this with something faster
  78. #define isPlanarYUV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
  79. #define isYUV(x) ((x)==IMGFMT_YUY2 || isPlanarYUV(x))
  80. #define isHalfChrV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
  81. #define isHalfChrH(x) ((x)==IMGFMT_YUY2 || (x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
  82. #define isPacked(x) ((x)==IMGFMT_YUY2 || ((x)&IMGFMT_BGR_MASK)==IMGFMT_BGR || ((x)&IMGFMT_RGB_MASK)==IMGFMT_RGB)
  83. #define isGray(x) ((x)==IMGFMT_Y800)
  84. #define isSupportedIn(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YUY2 \
  85. || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
  86. || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
  87. || (x)==IMGFMT_Y800)
  88. #define isSupportedOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 \
  89. || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
  90. #define isBGR(x) ((x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
  91. #define RGB2YUV_SHIFT 16
  92. #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
  93. #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
  94. #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  95. #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
  96. #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
  97. #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
  98. #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
  99. #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  100. #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
  101. extern int verbose; // defined in mplayer.c
  102. /*
  103. NOTES
  104. known BUGS with known cause (no bugreports please!, but patches are welcome :) )
  105. horizontal fast_bilinear MMX2 scaler reads 1-7 samples too much (might cause a sig11)
  106. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  107. TODO
  108. more intelligent missalignment avoidance for the horizontal scaler
  109. write special vertical cubic upscale version
  110. Optimize C code (yv12 / minmax)
  111. add support for packed pixel yuv input & output
  112. add support for Y8 output
  113. optimize bgr24 & bgr32
  114. add BGR4 output support
  115. write special BGR->BGR scaler
  116. deglobalize yuv2rgb*.c
  117. */
  118. #define ABS(a) ((a) > 0 ? (a) : (-(a)))
  119. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  120. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  121. #ifdef ARCH_X86
  122. #define CAN_COMPILE_X86_ASM
  123. #endif
  124. #ifdef CAN_COMPILE_X86_ASM
  125. static uint64_t __attribute__((aligned(8))) yCoeff= 0x2568256825682568LL;
  126. static uint64_t __attribute__((aligned(8))) vrCoeff= 0x3343334333433343LL;
  127. static uint64_t __attribute__((aligned(8))) ubCoeff= 0x40cf40cf40cf40cfLL;
  128. static uint64_t __attribute__((aligned(8))) vgCoeff= 0xE5E2E5E2E5E2E5E2LL;
  129. static uint64_t __attribute__((aligned(8))) ugCoeff= 0xF36EF36EF36EF36ELL;
  130. static uint64_t __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
  131. static uint64_t __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
  132. static uint64_t __attribute__((aligned(8))) w400= 0x0400040004000400LL;
  133. static uint64_t __attribute__((aligned(8))) w80= 0x0080008000800080LL;
  134. static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
  135. static uint64_t __attribute__((aligned(8))) w02= 0x0002000200020002LL;
  136. static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
  137. static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
  138. static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
  139. static uint64_t __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
  140. static volatile uint64_t __attribute__((aligned(8))) b5Dither;
  141. static volatile uint64_t __attribute__((aligned(8))) g5Dither;
  142. static volatile uint64_t __attribute__((aligned(8))) g6Dither;
  143. static volatile uint64_t __attribute__((aligned(8))) r5Dither;
  144. static uint64_t __attribute__((aligned(8))) dither4[2]={
  145. 0x0103010301030103LL,
  146. 0x0200020002000200LL,};
  147. static uint64_t __attribute__((aligned(8))) dither8[2]={
  148. 0x0602060206020602LL,
  149. 0x0004000400040004LL,};
  150. static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
  151. static uint64_t __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
  152. static uint64_t __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
  153. static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
  154. static uint64_t __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
  155. static uint64_t __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
  156. static uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
  157. static uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
  158. static uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
  159. #ifdef FAST_BGR2YV12
  160. static const uint64_t bgr2YCoeff __attribute__((aligned(8))) = 0x000000210041000DULL;
  161. static const uint64_t bgr2UCoeff __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
  162. static const uint64_t bgr2VCoeff __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
  163. #else
  164. static const uint64_t bgr2YCoeff __attribute__((aligned(8))) = 0x000020E540830C8BULL;
  165. static const uint64_t bgr2UCoeff __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
  166. static const uint64_t bgr2VCoeff __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
  167. #endif
  168. static const uint64_t bgr2YOffset __attribute__((aligned(8))) = 0x1010101010101010ULL;
  169. static const uint64_t bgr2UVOffset __attribute__((aligned(8)))= 0x8080808080808080ULL;
  170. static const uint64_t w1111 __attribute__((aligned(8))) = 0x0001000100010001ULL;
  171. // FIXME remove
  172. static uint64_t __attribute__((aligned(8))) asm_yalpha1;
  173. static uint64_t __attribute__((aligned(8))) asm_uvalpha1;
  174. #endif
  175. // clipping helper table for C implementations:
  176. static unsigned char clip_table[768];
  177. static unsigned short clip_table16b[768];
  178. static unsigned short clip_table16g[768];
  179. static unsigned short clip_table16r[768];
  180. static unsigned short clip_table15b[768];
  181. static unsigned short clip_table15g[768];
  182. static unsigned short clip_table15r[768];
  183. // yuv->rgb conversion tables:
  184. static int yuvtab_2568[256];
  185. static int yuvtab_3343[256];
  186. static int yuvtab_0c92[256];
  187. static int yuvtab_1a1e[256];
  188. static int yuvtab_40cf[256];
  189. // Needed for cubic scaler to catch overflows
  190. static int clip_yuvtab_2568[768];
  191. static int clip_yuvtab_3343[768];
  192. static int clip_yuvtab_0c92[768];
  193. static int clip_yuvtab_1a1e[768];
  194. static int clip_yuvtab_40cf[768];
  195. //global sws_flags from the command line
  196. int sws_flags=2;
  197. //global srcFilter
  198. SwsFilter src_filter= {NULL, NULL, NULL, NULL};
  199. float sws_lum_gblur= 0.0;
  200. float sws_chr_gblur= 0.0;
  201. int sws_chr_vshift= 0;
  202. int sws_chr_hshift= 0;
  203. float sws_chr_sharpen= 0.0;
  204. float sws_lum_sharpen= 0.0;
  205. /* cpuCaps combined from cpudetect and whats actually compiled in
  206. (if there is no support for something compiled in it wont appear here) */
  207. static CpuCaps cpuCaps;
  208. void (*swScale)(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
  209. int srcSliceH, uint8_t* dst[], int dstStride[])=NULL;
  210. static SwsVector *getConvVec(SwsVector *a, SwsVector *b);
  211. #ifdef CAN_COMPILE_X86_ASM
  212. void in_asm_used_var_warning_killer()
  213. {
  214. volatile int i= yCoeff+vrCoeff+ubCoeff+vgCoeff+ugCoeff+bF8+bFC+w400+w80+w10+
  215. bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+asm_yalpha1+ asm_uvalpha1+
  216. M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
  217. if(i) i=0;
  218. }
  219. #endif
  220. static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  221. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  222. uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW)
  223. {
  224. //FIXME Optimize (just quickly writen not opti..)
  225. int i;
  226. for(i=0; i<dstW; i++)
  227. {
  228. int val=0;
  229. int j;
  230. for(j=0; j<lumFilterSize; j++)
  231. val += lumSrc[j][i] * lumFilter[j];
  232. dest[i]= MIN(MAX(val>>19, 0), 255);
  233. }
  234. if(uDest != NULL)
  235. for(i=0; i<(dstW>>1); i++)
  236. {
  237. int u=0;
  238. int v=0;
  239. int j;
  240. for(j=0; j<chrFilterSize; j++)
  241. {
  242. u += chrSrc[j][i] * chrFilter[j];
  243. v += chrSrc[j][i + 2048] * chrFilter[j];
  244. }
  245. uDest[i]= MIN(MAX(u>>19, 0), 255);
  246. vDest[i]= MIN(MAX(v>>19, 0), 255);
  247. }
  248. }
  249. static inline void yuv2rgbXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  250. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  251. uint8_t *dest, int dstW, int dstFormat)
  252. {
  253. if(dstFormat==IMGFMT_BGR32)
  254. {
  255. int i;
  256. #ifdef WORDS_BIGENDIAN
  257. dest++;
  258. #endif
  259. for(i=0; i<(dstW>>1); i++){
  260. int j;
  261. int Y1=0;
  262. int Y2=0;
  263. int U=0;
  264. int V=0;
  265. int Cb, Cr, Cg;
  266. for(j=0; j<lumFilterSize; j++)
  267. {
  268. Y1 += lumSrc[j][2*i] * lumFilter[j];
  269. Y2 += lumSrc[j][2*i+1] * lumFilter[j];
  270. }
  271. for(j=0; j<chrFilterSize; j++)
  272. {
  273. U += chrSrc[j][i] * chrFilter[j];
  274. V += chrSrc[j][i+2048] * chrFilter[j];
  275. }
  276. Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
  277. Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
  278. U >>= 19;
  279. V >>= 19;
  280. Cb= clip_yuvtab_40cf[U+ 256];
  281. Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
  282. Cr= clip_yuvtab_3343[V+ 256];
  283. dest[8*i+0]=clip_table[((Y1 + Cb) >>13)];
  284. dest[8*i+1]=clip_table[((Y1 + Cg) >>13)];
  285. dest[8*i+2]=clip_table[((Y1 + Cr) >>13)];
  286. dest[8*i+4]=clip_table[((Y2 + Cb) >>13)];
  287. dest[8*i+5]=clip_table[((Y2 + Cg) >>13)];
  288. dest[8*i+6]=clip_table[((Y2 + Cr) >>13)];
  289. }
  290. }
  291. else if(dstFormat==IMGFMT_BGR24)
  292. {
  293. int i;
  294. for(i=0; i<(dstW>>1); i++){
  295. int j;
  296. int Y1=0;
  297. int Y2=0;
  298. int U=0;
  299. int V=0;
  300. int Cb, Cr, Cg;
  301. for(j=0; j<lumFilterSize; j++)
  302. {
  303. Y1 += lumSrc[j][2*i] * lumFilter[j];
  304. Y2 += lumSrc[j][2*i+1] * lumFilter[j];
  305. }
  306. for(j=0; j<chrFilterSize; j++)
  307. {
  308. U += chrSrc[j][i] * chrFilter[j];
  309. V += chrSrc[j][i+2048] * chrFilter[j];
  310. }
  311. Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
  312. Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
  313. U >>= 19;
  314. V >>= 19;
  315. Cb= clip_yuvtab_40cf[U+ 256];
  316. Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
  317. Cr= clip_yuvtab_3343[V+ 256];
  318. dest[0]=clip_table[((Y1 + Cb) >>13)];
  319. dest[1]=clip_table[((Y1 + Cg) >>13)];
  320. dest[2]=clip_table[((Y1 + Cr) >>13)];
  321. dest[3]=clip_table[((Y2 + Cb) >>13)];
  322. dest[4]=clip_table[((Y2 + Cg) >>13)];
  323. dest[5]=clip_table[((Y2 + Cr) >>13)];
  324. dest+=6;
  325. }
  326. }
  327. else if(dstFormat==IMGFMT_BGR16)
  328. {
  329. int i;
  330. #ifdef DITHER1XBPP
  331. static int ditherb1=1<<14;
  332. static int ditherg1=1<<13;
  333. static int ditherr1=2<<14;
  334. static int ditherb2=3<<14;
  335. static int ditherg2=3<<13;
  336. static int ditherr2=0<<14;
  337. ditherb1 ^= (1^2)<<14;
  338. ditherg1 ^= (1^2)<<13;
  339. ditherr1 ^= (1^2)<<14;
  340. ditherb2 ^= (3^0)<<14;
  341. ditherg2 ^= (3^0)<<13;
  342. ditherr2 ^= (3^0)<<14;
  343. #else
  344. const int ditherb1=0;
  345. const int ditherg1=0;
  346. const int ditherr1=0;
  347. const int ditherb2=0;
  348. const int ditherg2=0;
  349. const int ditherr2=0;
  350. #endif
  351. for(i=0; i<(dstW>>1); i++){
  352. int j;
  353. int Y1=0;
  354. int Y2=0;
  355. int U=0;
  356. int V=0;
  357. int Cb, Cr, Cg;
  358. for(j=0; j<lumFilterSize; j++)
  359. {
  360. Y1 += lumSrc[j][2*i] * lumFilter[j];
  361. Y2 += lumSrc[j][2*i+1] * lumFilter[j];
  362. }
  363. for(j=0; j<chrFilterSize; j++)
  364. {
  365. U += chrSrc[j][i] * chrFilter[j];
  366. V += chrSrc[j][i+2048] * chrFilter[j];
  367. }
  368. Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
  369. Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
  370. U >>= 19;
  371. V >>= 19;
  372. Cb= clip_yuvtab_40cf[U+ 256];
  373. Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
  374. Cr= clip_yuvtab_3343[V+ 256];
  375. ((uint16_t*)dest)[2*i] =
  376. clip_table16b[(Y1 + Cb + ditherb1) >>13] |
  377. clip_table16g[(Y1 + Cg + ditherg1) >>13] |
  378. clip_table16r[(Y1 + Cr + ditherr1) >>13];
  379. ((uint16_t*)dest)[2*i+1] =
  380. clip_table16b[(Y2 + Cb + ditherb2) >>13] |
  381. clip_table16g[(Y2 + Cg + ditherg2) >>13] |
  382. clip_table16r[(Y2 + Cr + ditherr2) >>13];
  383. }
  384. }
  385. else if(dstFormat==IMGFMT_BGR15)
  386. {
  387. int i;
  388. #ifdef DITHER1XBPP
  389. static int ditherb1=1<<14;
  390. static int ditherg1=1<<14;
  391. static int ditherr1=2<<14;
  392. static int ditherb2=3<<14;
  393. static int ditherg2=3<<14;
  394. static int ditherr2=0<<14;
  395. ditherb1 ^= (1^2)<<14;
  396. ditherg1 ^= (1^2)<<14;
  397. ditherr1 ^= (1^2)<<14;
  398. ditherb2 ^= (3^0)<<14;
  399. ditherg2 ^= (3^0)<<14;
  400. ditherr2 ^= (3^0)<<14;
  401. #else
  402. const int ditherb1=0;
  403. const int ditherg1=0;
  404. const int ditherr1=0;
  405. const int ditherb2=0;
  406. const int ditherg2=0;
  407. const int ditherr2=0;
  408. #endif
  409. for(i=0; i<(dstW>>1); i++){
  410. int j;
  411. int Y1=0;
  412. int Y2=0;
  413. int U=0;
  414. int V=0;
  415. int Cb, Cr, Cg;
  416. for(j=0; j<lumFilterSize; j++)
  417. {
  418. Y1 += lumSrc[j][2*i] * lumFilter[j];
  419. Y2 += lumSrc[j][2*i+1] * lumFilter[j];
  420. }
  421. for(j=0; j<chrFilterSize; j++)
  422. {
  423. U += chrSrc[j][i] * chrFilter[j];
  424. V += chrSrc[j][i+2048] * chrFilter[j];
  425. }
  426. Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
  427. Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
  428. U >>= 19;
  429. V >>= 19;
  430. Cb= clip_yuvtab_40cf[U+ 256];
  431. Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
  432. Cr= clip_yuvtab_3343[V+ 256];
  433. ((uint16_t*)dest)[2*i] =
  434. clip_table15b[(Y1 + Cb + ditherb1) >>13] |
  435. clip_table15g[(Y1 + Cg + ditherg1) >>13] |
  436. clip_table15r[(Y1 + Cr + ditherr1) >>13];
  437. ((uint16_t*)dest)[2*i+1] =
  438. clip_table15b[(Y2 + Cb + ditherb2) >>13] |
  439. clip_table15g[(Y2 + Cg + ditherg2) >>13] |
  440. clip_table15r[(Y2 + Cr + ditherr2) >>13];
  441. }
  442. }
  443. }
  444. //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  445. //Plain C versions
  446. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  447. #define COMPILE_C
  448. #endif
  449. #ifdef CAN_COMPILE_X86_ASM
  450. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  451. #define COMPILE_MMX
  452. #endif
  453. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  454. #define COMPILE_MMX2
  455. #endif
  456. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  457. #define COMPILE_3DNOW
  458. #endif
  459. #endif //CAN_COMPILE_X86_ASM
  460. #undef HAVE_MMX
  461. #undef HAVE_MMX2
  462. #undef HAVE_3DNOW
  463. #ifdef COMPILE_C
  464. #undef HAVE_MMX
  465. #undef HAVE_MMX2
  466. #undef HAVE_3DNOW
  467. #define RENAME(a) a ## _C
  468. #include "swscale_template.c"
  469. #endif
  470. #ifdef CAN_COMPILE_X86_ASM
  471. //X86 versions
  472. /*
  473. #undef RENAME
  474. #undef HAVE_MMX
  475. #undef HAVE_MMX2
  476. #undef HAVE_3DNOW
  477. #define ARCH_X86
  478. #define RENAME(a) a ## _X86
  479. #include "swscale_template.c"
  480. */
  481. //MMX versions
  482. #ifdef COMPILE_MMX
  483. #undef RENAME
  484. #define HAVE_MMX
  485. #undef HAVE_MMX2
  486. #undef HAVE_3DNOW
  487. #define RENAME(a) a ## _MMX
  488. #include "swscale_template.c"
  489. #endif
  490. //MMX2 versions
  491. #ifdef COMPILE_MMX2
  492. #undef RENAME
  493. #define HAVE_MMX
  494. #define HAVE_MMX2
  495. #undef HAVE_3DNOW
  496. #define RENAME(a) a ## _MMX2
  497. #include "swscale_template.c"
  498. #endif
  499. //3DNOW versions
  500. #ifdef COMPILE_3DNOW
  501. #undef RENAME
  502. #define HAVE_MMX
  503. #undef HAVE_MMX2
  504. #define HAVE_3DNOW
  505. #define RENAME(a) a ## _3DNow
  506. #include "swscale_template.c"
  507. #endif
  508. #endif //CAN_COMPILE_X86_ASM
  509. // minor note: the HAVE_xyz is messed up after that line so dont use it
  510. // old global scaler, dont use for new code
  511. // will use sws_flags from the command line
  512. void SwScale_YV12slice(unsigned char* src[], int srcStride[], int srcSliceY ,
  513. int srcSliceH, uint8_t* dst[], int dstStride, int dstbpp,
  514. int srcW, int srcH, int dstW, int dstH){
  515. static SwsContext *context=NULL;
  516. int dstFormat;
  517. int dstStride3[3]= {dstStride, dstStride>>1, dstStride>>1};
  518. switch(dstbpp)
  519. {
  520. case 8 : dstFormat= IMGFMT_Y8; break;
  521. case 12: dstFormat= IMGFMT_YV12; break;
  522. case 15: dstFormat= IMGFMT_BGR15; break;
  523. case 16: dstFormat= IMGFMT_BGR16; break;
  524. case 24: dstFormat= IMGFMT_BGR24; break;
  525. case 32: dstFormat= IMGFMT_BGR32; break;
  526. default: return;
  527. }
  528. if(!context) context=getSwsContextFromCmdLine(srcW, srcH, IMGFMT_YV12, dstW, dstH, dstFormat);
  529. context->swScale(context, src, srcStride, srcSliceY, srcSliceH, dst, dstStride3);
  530. }
  531. // will use sws_flags & src_filter (from cmd line)
  532. SwsContext *getSwsContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
  533. {
  534. int flags=0;
  535. static int firstTime=1;
  536. #ifdef ARCH_X86
  537. if(gCpuCaps.hasMMX)
  538. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  539. #endif
  540. if(firstTime)
  541. {
  542. firstTime=0;
  543. flags= SWS_PRINT_INFO;
  544. }
  545. else if(verbose>1) flags= SWS_PRINT_INFO;
  546. if(src_filter.lumH) freeVec(src_filter.lumH);
  547. if(src_filter.lumV) freeVec(src_filter.lumV);
  548. if(src_filter.chrH) freeVec(src_filter.chrH);
  549. if(src_filter.chrV) freeVec(src_filter.chrV);
  550. if(sws_lum_gblur!=0.0){
  551. src_filter.lumH= getGaussianVec(sws_lum_gblur, 3.0);
  552. src_filter.lumV= getGaussianVec(sws_lum_gblur, 3.0);
  553. }else{
  554. src_filter.lumH= getIdentityVec();
  555. src_filter.lumV= getIdentityVec();
  556. }
  557. if(sws_chr_gblur!=0.0){
  558. src_filter.chrH= getGaussianVec(sws_chr_gblur, 3.0);
  559. src_filter.chrV= getGaussianVec(sws_chr_gblur, 3.0);
  560. }else{
  561. src_filter.chrH= getIdentityVec();
  562. src_filter.chrV= getIdentityVec();
  563. }
  564. if(sws_chr_sharpen!=0.0){
  565. SwsVector *g= getConstVec(-1.0, 3);
  566. SwsVector *id= getConstVec(10.0/sws_chr_sharpen, 1);
  567. g->coeff[1]=2.0;
  568. addVec(id, g);
  569. convVec(src_filter.chrH, id);
  570. convVec(src_filter.chrV, id);
  571. freeVec(g);
  572. freeVec(id);
  573. }
  574. if(sws_lum_sharpen!=0.0){
  575. SwsVector *g= getConstVec(-1.0, 3);
  576. SwsVector *id= getConstVec(10.0/sws_lum_sharpen, 1);
  577. g->coeff[1]=2.0;
  578. addVec(id, g);
  579. convVec(src_filter.lumH, id);
  580. convVec(src_filter.lumV, id);
  581. freeVec(g);
  582. freeVec(id);
  583. }
  584. if(sws_chr_hshift)
  585. shiftVec(src_filter.chrH, sws_chr_hshift);
  586. if(sws_chr_vshift)
  587. shiftVec(src_filter.chrV, sws_chr_vshift);
  588. normalizeVec(src_filter.chrH, 1.0);
  589. normalizeVec(src_filter.chrV, 1.0);
  590. normalizeVec(src_filter.lumH, 1.0);
  591. normalizeVec(src_filter.lumV, 1.0);
  592. if(verbose > 1) printVec(src_filter.chrH);
  593. if(verbose > 1) printVec(src_filter.lumH);
  594. switch(sws_flags)
  595. {
  596. case 0: flags|= SWS_FAST_BILINEAR; break;
  597. case 1: flags|= SWS_BILINEAR; break;
  598. case 2: flags|= SWS_BICUBIC; break;
  599. case 3: flags|= SWS_X; break;
  600. case 4: flags|= SWS_POINT; break;
  601. case 5: flags|= SWS_AREA; break;
  602. default:flags|= SWS_BILINEAR; break;
  603. }
  604. return getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, &src_filter, NULL);
  605. }
  606. static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  607. int srcW, int dstW, int filterAlign, int one, int flags,
  608. SwsVector *srcFilter, SwsVector *dstFilter)
  609. {
  610. int i;
  611. int filterSize;
  612. int filter2Size;
  613. int minFilterSize;
  614. double *filter=NULL;
  615. double *filter2=NULL;
  616. #ifdef ARCH_X86
  617. if(gCpuCaps.hasMMX)
  618. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  619. #endif
  620. // Note the +1 is for the MMXscaler which reads over the end
  621. *filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));
  622. if(ABS(xInc - 0x10000) <10) // unscaled
  623. {
  624. int i;
  625. filterSize= 1;
  626. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  627. for(i=0; i<dstW*filterSize; i++) filter[i]=0;
  628. for(i=0; i<dstW; i++)
  629. {
  630. filter[i*filterSize]=1;
  631. (*filterPos)[i]=i;
  632. }
  633. }
  634. else if(flags&SWS_POINT) // lame looking point sampling mode
  635. {
  636. int i;
  637. int xDstInSrc;
  638. filterSize= 1;
  639. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  640. xDstInSrc= xInc/2 - 0x8000;
  641. for(i=0; i<dstW; i++)
  642. {
  643. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  644. (*filterPos)[i]= xx;
  645. filter[i]= 1.0;
  646. xDstInSrc+= xInc;
  647. }
  648. }
  649. else if(xInc <= (1<<16) || (flags&SWS_FAST_BILINEAR)) // upscale
  650. {
  651. int i;
  652. int xDstInSrc;
  653. if (flags&SWS_BICUBIC) filterSize= 4;
  654. else if(flags&SWS_X ) filterSize= 4;
  655. else filterSize= 2; // SWS_BILINEAR / SWS_AREA
  656. // printf("%d %d %d\n", filterSize, srcW, dstW);
  657. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  658. xDstInSrc= xInc/2 - 0x8000;
  659. for(i=0; i<dstW; i++)
  660. {
  661. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  662. int j;
  663. (*filterPos)[i]= xx;
  664. if((flags & SWS_BICUBIC) || (flags & SWS_X))
  665. {
  666. double d= ABS(((xx+1)<<16) - xDstInSrc)/(double)(1<<16);
  667. double y1,y2,y3,y4;
  668. double A= -0.6;
  669. if(flags & SWS_BICUBIC){
  670. // Equation is from VirtualDub
  671. y1 = ( + A*d - 2.0*A*d*d + A*d*d*d);
  672. y2 = (+ 1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  673. y3 = ( - A*d + (2.0*A+3.0)*d*d - (A+2.0)*d*d*d);
  674. y4 = ( + A*d*d - A*d*d*d);
  675. }else{
  676. // cubic interpolation (derived it myself)
  677. y1 = ( -2.0*d + 3.0*d*d - 1.0*d*d*d)/6.0;
  678. y2 = (6.0 -3.0*d - 6.0*d*d + 3.0*d*d*d)/6.0;
  679. y3 = ( +6.0*d + 3.0*d*d - 3.0*d*d*d)/6.0;
  680. y4 = ( -1.0*d + 1.0*d*d*d)/6.0;
  681. }
  682. // printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
  683. filter[i*filterSize + 0]= y1;
  684. filter[i*filterSize + 1]= y2;
  685. filter[i*filterSize + 2]= y3;
  686. filter[i*filterSize + 3]= y4;
  687. // printf("%1.3f %1.3f %1.3f %1.3f %1.3f\n",d , y1, y2, y3, y4);
  688. }
  689. else
  690. {
  691. //Bilinear upscale / linear interpolate / Area averaging
  692. for(j=0; j<filterSize; j++)
  693. {
  694. double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
  695. double coeff= 1.0 - d;
  696. if(coeff<0) coeff=0;
  697. // printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
  698. filter[i*filterSize + j]= coeff;
  699. xx++;
  700. }
  701. }
  702. xDstInSrc+= xInc;
  703. }
  704. }
  705. else // downscale
  706. {
  707. int xDstInSrc;
  708. ASSERT(dstW <= srcW)
  709. if(flags&SWS_BICUBIC) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
  710. else if(flags&SWS_X) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
  711. else if(flags&SWS_AREA) filterSize= (int)ceil(1 + 1.0*srcW / (double)dstW);
  712. else /* BILINEAR */ filterSize= (int)ceil(1 + 2.0*srcW / (double)dstW);
  713. // printf("%d %d %d\n", *filterSize, srcW, dstW);
  714. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  715. xDstInSrc= xInc/2 - 0x8000;
  716. for(i=0; i<dstW; i++)
  717. {
  718. int xx= (int)((double)xDstInSrc/(double)(1<<16) - (filterSize-1)*0.5 + 0.5);
  719. int j;
  720. (*filterPos)[i]= xx;
  721. for(j=0; j<filterSize; j++)
  722. {
  723. double d= ABS((xx<<16) - xDstInSrc)/(double)xInc;
  724. double coeff;
  725. if((flags & SWS_BICUBIC) || (flags & SWS_X))
  726. {
  727. double A= -0.75;
  728. // d*=2;
  729. // Equation is from VirtualDub
  730. if(d<1.0)
  731. coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  732. else if(d<2.0)
  733. coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
  734. else
  735. coeff=0.0;
  736. }
  737. else if(flags & SWS_AREA)
  738. {
  739. double srcPixelSize= (1<<16)/(double)xInc;
  740. if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
  741. else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
  742. else coeff=0.0;
  743. }
  744. else
  745. {
  746. coeff= 1.0 - d;
  747. if(coeff<0) coeff=0;
  748. }
  749. // printf("%1.3f %2.3f %d \n", coeff, d, xDstInSrc);
  750. filter[i*filterSize + j]= coeff;
  751. xx++;
  752. }
  753. xDstInSrc+= xInc;
  754. }
  755. }
  756. /* apply src & dst Filter to filter -> filter2
  757. free(filter);
  758. */
  759. ASSERT(filterSize>0)
  760. filter2Size= filterSize;
  761. if(srcFilter) filter2Size+= srcFilter->length - 1;
  762. if(dstFilter) filter2Size+= dstFilter->length - 1;
  763. ASSERT(filter2Size>0)
  764. filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
  765. for(i=0; i<dstW; i++)
  766. {
  767. int j;
  768. SwsVector scaleFilter;
  769. SwsVector *outVec;
  770. scaleFilter.coeff= filter + i*filterSize;
  771. scaleFilter.length= filterSize;
  772. if(srcFilter) outVec= getConvVec(srcFilter, &scaleFilter);
  773. else outVec= &scaleFilter;
  774. ASSERT(outVec->length == filter2Size)
  775. //FIXME dstFilter
  776. for(j=0; j<outVec->length; j++)
  777. {
  778. filter2[i*filter2Size + j]= outVec->coeff[j];
  779. }
  780. (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
  781. if(outVec != &scaleFilter) freeVec(outVec);
  782. }
  783. free(filter); filter=NULL;
  784. /* try to reduce the filter-size (step1 find size and shift left) */
  785. // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
  786. minFilterSize= 0;
  787. for(i=dstW-1; i>=0; i--)
  788. {
  789. int min= filter2Size;
  790. int j;
  791. double cutOff=0.0;
  792. /* get rid off near zero elements on the left by shifting left */
  793. for(j=0; j<filter2Size; j++)
  794. {
  795. int k;
  796. cutOff += ABS(filter2[i*filter2Size]);
  797. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  798. /* preserve Monotonicity because the core cant handle the filter otherwise */
  799. if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
  800. // Move filter coeffs left
  801. for(k=1; k<filter2Size; k++)
  802. filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
  803. filter2[i*filter2Size + k - 1]= 0.0;
  804. (*filterPos)[i]++;
  805. }
  806. cutOff=0.0;
  807. /* count near zeros on the right */
  808. for(j=filter2Size-1; j>0; j--)
  809. {
  810. cutOff += ABS(filter2[i*filter2Size + j]);
  811. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  812. min--;
  813. }
  814. if(min>minFilterSize) minFilterSize= min;
  815. }
  816. ASSERT(minFilterSize > 0)
  817. filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
  818. ASSERT(filterSize > 0)
  819. filter= (double*)memalign(8, filterSize*dstW*sizeof(double));
  820. *outFilterSize= filterSize;
  821. if((flags&SWS_PRINT_INFO) && verbose)
  822. printf("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
  823. /* try to reduce the filter-size (step2 reduce it) */
  824. for(i=0; i<dstW; i++)
  825. {
  826. int j;
  827. for(j=0; j<filterSize; j++)
  828. {
  829. if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
  830. else filter[i*filterSize + j]= filter2[i*filter2Size + j];
  831. }
  832. }
  833. free(filter2); filter2=NULL;
  834. //FIXME try to align filterpos if possible
  835. //fix borders
  836. for(i=0; i<dstW; i++)
  837. {
  838. int j;
  839. if((*filterPos)[i] < 0)
  840. {
  841. // Move filter coeffs left to compensate for filterPos
  842. for(j=1; j<filterSize; j++)
  843. {
  844. int left= MAX(j + (*filterPos)[i], 0);
  845. filter[i*filterSize + left] += filter[i*filterSize + j];
  846. filter[i*filterSize + j]=0;
  847. }
  848. (*filterPos)[i]= 0;
  849. }
  850. if((*filterPos)[i] + filterSize > srcW)
  851. {
  852. int shift= (*filterPos)[i] + filterSize - srcW;
  853. // Move filter coeffs right to compensate for filterPos
  854. for(j=filterSize-2; j>=0; j--)
  855. {
  856. int right= MIN(j + shift, filterSize-1);
  857. filter[i*filterSize +right] += filter[i*filterSize +j];
  858. filter[i*filterSize +j]=0;
  859. }
  860. (*filterPos)[i]= srcW - filterSize;
  861. }
  862. }
  863. // Note the +1 is for the MMXscaler which reads over the end
  864. *outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));
  865. memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
  866. /* Normalize & Store in outFilter */
  867. for(i=0; i<dstW; i++)
  868. {
  869. int j;
  870. double sum=0;
  871. double scale= one;
  872. for(j=0; j<filterSize; j++)
  873. {
  874. sum+= filter[i*filterSize + j];
  875. }
  876. scale/= sum;
  877. for(j=0; j<filterSize; j++)
  878. {
  879. (*outFilter)[i*(*outFilterSize) + j]= (int)(filter[i*filterSize + j]*scale);
  880. }
  881. }
  882. (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
  883. for(i=0; i<*outFilterSize; i++)
  884. {
  885. int j= dstW*(*outFilterSize);
  886. (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
  887. }
  888. free(filter);
  889. }
  890. #ifdef ARCH_X86
  891. static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode)
  892. {
  893. uint8_t *fragment;
  894. int imm8OfPShufW1;
  895. int imm8OfPShufW2;
  896. int fragmentLength;
  897. int xpos, i;
  898. // create an optimized horizontal scaling routine
  899. //code fragment
  900. asm volatile(
  901. "jmp 9f \n\t"
  902. // Begin
  903. "0: \n\t"
  904. "movq (%%esi), %%mm0 \n\t" //FIXME Alignment
  905. "movq %%mm0, %%mm1 \n\t"
  906. "psrlq $8, %%mm0 \n\t"
  907. "punpcklbw %%mm7, %%mm1 \n\t"
  908. "movq %%mm2, %%mm3 \n\t"
  909. "punpcklbw %%mm7, %%mm0 \n\t"
  910. "addw %%bx, %%cx \n\t" //2*xalpha += (4*lumXInc)&0xFFFF
  911. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  912. "1: \n\t"
  913. "adcl %%edx, %%esi \n\t" //xx+= (4*lumXInc)>>16 + carry
  914. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  915. "2: \n\t"
  916. "psrlw $9, %%mm3 \n\t"
  917. "psubw %%mm1, %%mm0 \n\t"
  918. "pmullw %%mm3, %%mm0 \n\t"
  919. "paddw %%mm6, %%mm2 \n\t" // 2*alpha += xpos&0xFFFF
  920. "psllw $7, %%mm1 \n\t"
  921. "paddw %%mm1, %%mm0 \n\t"
  922. "movq %%mm0, (%%edi, %%eax) \n\t"
  923. "addl $8, %%eax \n\t"
  924. // End
  925. "9: \n\t"
  926. // "int $3\n\t"
  927. "leal 0b, %0 \n\t"
  928. "leal 1b, %1 \n\t"
  929. "leal 2b, %2 \n\t"
  930. "decl %1 \n\t"
  931. "decl %2 \n\t"
  932. "subl %0, %1 \n\t"
  933. "subl %0, %2 \n\t"
  934. "leal 9b, %3 \n\t"
  935. "subl %0, %3 \n\t"
  936. :"=r" (fragment), "=r" (imm8OfPShufW1), "=r" (imm8OfPShufW2),
  937. "=r" (fragmentLength)
  938. );
  939. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  940. for(i=0; i<dstW/8; i++)
  941. {
  942. int xx=xpos>>16;
  943. if((i&3) == 0)
  944. {
  945. int a=0;
  946. int b=((xpos+xInc)>>16) - xx;
  947. int c=((xpos+xInc*2)>>16) - xx;
  948. int d=((xpos+xInc*3)>>16) - xx;
  949. memcpy(funnyCode + fragmentLength*i/4, fragment, fragmentLength);
  950. funnyCode[fragmentLength*i/4 + imm8OfPShufW1]=
  951. funnyCode[fragmentLength*i/4 + imm8OfPShufW2]=
  952. a | (b<<2) | (c<<4) | (d<<6);
  953. // if we dont need to read 8 bytes than dont :), reduces the chance of
  954. // crossing a cache line
  955. if(d<3) funnyCode[fragmentLength*i/4 + 1]= 0x6E;
  956. funnyCode[fragmentLength*(i+4)/4]= RET;
  957. }
  958. xpos+=xInc;
  959. }
  960. }
  961. #endif // ARCH_X86
  962. //FIXME remove
  963. void SwScale_Init(){
  964. }
  965. static void globalInit(){
  966. // generating tables:
  967. int i;
  968. for(i=0; i<768; i++){
  969. int c= MIN(MAX(i-256, 0), 255);
  970. clip_table[i]=c;
  971. yuvtab_2568[c]= clip_yuvtab_2568[i]=(0x2568*(c-16))+(256<<13);
  972. yuvtab_3343[c]= clip_yuvtab_3343[i]=0x3343*(c-128);
  973. yuvtab_0c92[c]= clip_yuvtab_0c92[i]=-0x0c92*(c-128);
  974. yuvtab_1a1e[c]= clip_yuvtab_1a1e[i]=-0x1a1e*(c-128);
  975. yuvtab_40cf[c]= clip_yuvtab_40cf[i]=0x40cf*(c-128);
  976. }
  977. for(i=0; i<768; i++)
  978. {
  979. int v= clip_table[i];
  980. clip_table16b[i]= v>>3;
  981. clip_table16g[i]= (v<<3)&0x07E0;
  982. clip_table16r[i]= (v<<8)&0xF800;
  983. clip_table15b[i]= v>>3;
  984. clip_table15g[i]= (v<<2)&0x03E0;
  985. clip_table15r[i]= (v<<7)&0x7C00;
  986. }
  987. cpuCaps= gCpuCaps;
  988. #ifdef RUNTIME_CPUDETECT
  989. #ifdef CAN_COMPILE_X86_ASM
  990. // ordered per speed fasterst first
  991. if(gCpuCaps.hasMMX2)
  992. swScale= swScale_MMX2;
  993. else if(gCpuCaps.has3DNow)
  994. swScale= swScale_3DNow;
  995. else if(gCpuCaps.hasMMX)
  996. swScale= swScale_MMX;
  997. else
  998. swScale= swScale_C;
  999. #else
  1000. swScale= swScale_C;
  1001. cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
  1002. #endif
  1003. #else //RUNTIME_CPUDETECT
  1004. #ifdef HAVE_MMX2
  1005. swScale= swScale_MMX2;
  1006. cpuCaps.has3DNow = 0;
  1007. #elif defined (HAVE_3DNOW)
  1008. swScale= swScale_3DNow;
  1009. cpuCaps.hasMMX2 = 0;
  1010. #elif defined (HAVE_MMX)
  1011. swScale= swScale_MMX;
  1012. cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
  1013. #else
  1014. swScale= swScale_C;
  1015. cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
  1016. #endif
  1017. #endif //!RUNTIME_CPUDETECT
  1018. }
  1019. /* Warper functions for yuv2bgr */
  1020. static void planarYuvToBgr(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1021. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1022. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1023. if(c->srcFormat==IMGFMT_YV12)
  1024. yuv2rgb( dst,src[0],src[1],src[2],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
  1025. else /* I420 & IYUV */
  1026. yuv2rgb( dst,src[0],src[2],src[1],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
  1027. }
  1028. static void bgr24to32Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1029. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1030. if(dstStride[0]*3==srcStride[0]*4)
  1031. rgb24to32(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1032. else
  1033. {
  1034. int i;
  1035. uint8_t *srcPtr= src[0];
  1036. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1037. for(i=0; i<srcSliceH; i++)
  1038. {
  1039. rgb24to32(srcPtr, dstPtr, c->srcW*3);
  1040. srcPtr+= srcStride[0];
  1041. dstPtr+= dstStride[0];
  1042. }
  1043. }
  1044. }
  1045. static void bgr32to24Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1046. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1047. if(dstStride[0]*4==srcStride[0]*3)
  1048. rgb32to24(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1049. else
  1050. {
  1051. int i;
  1052. uint8_t *srcPtr= src[0];
  1053. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1054. for(i=0; i<srcSliceH; i++)
  1055. {
  1056. rgb32to24(srcPtr, dstPtr, c->srcW<<2);
  1057. srcPtr+= srcStride[0];
  1058. dstPtr+= dstStride[0];
  1059. }
  1060. }
  1061. }
  1062. static void bgr15to16Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1063. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1064. if(dstStride[0]==srcStride[0])
  1065. rgb15to16(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1066. else
  1067. {
  1068. int i;
  1069. uint8_t *srcPtr= src[0];
  1070. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1071. for(i=0; i<srcSliceH; i++)
  1072. {
  1073. rgb15to16(srcPtr, dstPtr, c->srcW<<1);
  1074. srcPtr+= srcStride[0];
  1075. dstPtr+= dstStride[0];
  1076. }
  1077. }
  1078. }
  1079. static void bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1080. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1081. rgb24toyv12(
  1082. src[0],
  1083. dst[0]+ srcSliceY *dstStride[0],
  1084. dst[1]+(srcSliceY>>1)*dstStride[1],
  1085. dst[2]+(srcSliceY>>1)*dstStride[2],
  1086. c->srcW, srcSliceH,
  1087. dstStride[0], dstStride[1], srcStride[0]);
  1088. }
  1089. /* unscaled copy like stuff (assumes nearly identical formats) */
  1090. static void simpleCopy(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
  1091. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1092. int srcStride[3];
  1093. uint8_t *src[3];
  1094. uint8_t *dst[3];
  1095. if(c->srcFormat == IMGFMT_I420){
  1096. src[0]= srcParam[0];
  1097. src[1]= srcParam[2];
  1098. src[2]= srcParam[1];
  1099. srcStride[0]= srcStrideParam[0];
  1100. srcStride[1]= srcStrideParam[2];
  1101. srcStride[2]= srcStrideParam[1];
  1102. }
  1103. else if(c->srcFormat==IMGFMT_YV12){
  1104. src[0]= srcParam[0];
  1105. src[1]= srcParam[1];
  1106. src[2]= srcParam[2];
  1107. srcStride[0]= srcStrideParam[0];
  1108. srcStride[1]= srcStrideParam[1];
  1109. srcStride[2]= srcStrideParam[2];
  1110. }
  1111. else if(isPacked(c->srcFormat) || isGray(c->srcFormat)){
  1112. src[0]= srcParam[0];
  1113. src[1]=
  1114. src[2]= NULL;
  1115. srcStride[0]= srcStrideParam[0];
  1116. srcStride[1]=
  1117. srcStride[2]= 0;
  1118. }
  1119. if(c->dstFormat == IMGFMT_I420){
  1120. dst[0]= dstParam[0];
  1121. dst[1]= dstParam[2];
  1122. dst[2]= dstParam[1];
  1123. }else{
  1124. dst[0]= dstParam[0];
  1125. dst[1]= dstParam[1];
  1126. dst[2]= dstParam[2];
  1127. }
  1128. if(isPacked(c->srcFormat))
  1129. {
  1130. if(dstStride[0]==srcStride[0])
  1131. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1132. else
  1133. {
  1134. int i;
  1135. uint8_t *srcPtr= src[0];
  1136. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1137. int length=0;
  1138. /* universal length finder */
  1139. while(length+c->srcW <= ABS(dstStride[0])
  1140. && length+c->srcW <= ABS(srcStride[0])) length+= c->srcW;
  1141. ASSERT(length!=0);
  1142. for(i=0; i<srcSliceH; i++)
  1143. {
  1144. memcpy(dstPtr, srcPtr, length);
  1145. srcPtr+= srcStride[0];
  1146. dstPtr+= dstStride[0];
  1147. }
  1148. }
  1149. }
  1150. else
  1151. { /* Planar YUV */
  1152. int plane;
  1153. for(plane=0; plane<3; plane++)
  1154. {
  1155. int length= plane==0 ? c->srcW : ((c->srcW+1)>>1);
  1156. int y= plane==0 ? srcSliceY: ((srcSliceY+1)>>1);
  1157. int height= plane==0 ? srcSliceH: ((srcSliceH+1)>>1);
  1158. if(dstStride[plane]==srcStride[plane])
  1159. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1160. else
  1161. {
  1162. int i;
  1163. uint8_t *srcPtr= src[plane];
  1164. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1165. for(i=0; i<height; i++)
  1166. {
  1167. memcpy(dstPtr, srcPtr, length);
  1168. srcPtr+= srcStride[plane];
  1169. dstPtr+= dstStride[plane];
  1170. }
  1171. }
  1172. }
  1173. }
  1174. }
  1175. SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
  1176. SwsFilter *srcFilter, SwsFilter *dstFilter){
  1177. SwsContext *c;
  1178. int i;
  1179. int usesFilter;
  1180. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  1181. #ifdef ARCH_X86
  1182. if(gCpuCaps.hasMMX)
  1183. asm volatile("emms\n\t"::: "memory");
  1184. #endif
  1185. if(swScale==NULL) globalInit();
  1186. /* avoid dupplicate Formats, so we dont need to check to much */
  1187. if(srcFormat==IMGFMT_IYUV) srcFormat=IMGFMT_I420;
  1188. if(srcFormat==IMGFMT_Y8) srcFormat=IMGFMT_Y800;
  1189. if(dstFormat==IMGFMT_Y8) dstFormat=IMGFMT_Y800;
  1190. if(!isSupportedIn(srcFormat))
  1191. {
  1192. fprintf(stderr, "swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
  1193. return NULL;
  1194. }
  1195. if(!isSupportedOut(dstFormat))
  1196. {
  1197. fprintf(stderr, "swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
  1198. return NULL;
  1199. }
  1200. /* sanity check */
  1201. 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
  1202. {
  1203. fprintf(stderr, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  1204. srcW, srcH, dstW, dstH);
  1205. return NULL;
  1206. }
  1207. if(!dstFilter) dstFilter= &dummyFilter;
  1208. if(!srcFilter) srcFilter= &dummyFilter;
  1209. c= memalign(64, sizeof(SwsContext));
  1210. memset(c, 0, sizeof(SwsContext));
  1211. c->srcW= srcW;
  1212. c->srcH= srcH;
  1213. c->dstW= dstW;
  1214. c->dstH= dstH;
  1215. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  1216. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  1217. c->flags= flags;
  1218. c->dstFormat= dstFormat;
  1219. c->srcFormat= srcFormat;
  1220. usesFilter=0;
  1221. if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesFilter=1;
  1222. if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesFilter=1;
  1223. if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesFilter=1;
  1224. if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesFilter=1;
  1225. if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesFilter=1;
  1226. if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesFilter=1;
  1227. if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesFilter=1;
  1228. if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesFilter=1;
  1229. /* unscaled special Cases */
  1230. if(srcW==dstW && srcH==dstH && !usesFilter)
  1231. {
  1232. /* yuv2bgr */
  1233. if(isPlanarYUV(srcFormat) && isBGR(dstFormat))
  1234. {
  1235. // FIXME multiple yuv2rgb converters wont work that way cuz that thing is full of globals&statics
  1236. #ifdef WORDS_BIGENDIAN
  1237. if(dstFormat==IMGFMT_BGR32)
  1238. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_BGR);
  1239. else
  1240. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
  1241. #else
  1242. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
  1243. #endif
  1244. c->swScale= planarYuvToBgr;
  1245. if(flags&SWS_PRINT_INFO)
  1246. printf("SwScaler: using unscaled %s -> %s special converter\n",
  1247. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1248. return c;
  1249. }
  1250. /* simple copy */
  1251. if(srcFormat == dstFormat || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)))
  1252. {
  1253. c->swScale= simpleCopy;
  1254. if(flags&SWS_PRINT_INFO)
  1255. printf("SwScaler: using unscaled %s -> %s special converter\n",
  1256. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1257. return c;
  1258. }
  1259. /* bgr32to24 & rgb32to24*/
  1260. if((srcFormat==IMGFMT_BGR32 && dstFormat==IMGFMT_BGR24)
  1261. ||(srcFormat==IMGFMT_RGB32 && dstFormat==IMGFMT_RGB24))
  1262. {
  1263. c->swScale= bgr32to24Wrapper;
  1264. if(flags&SWS_PRINT_INFO)
  1265. printf("SwScaler: using unscaled %s -> %s special converter\n",
  1266. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1267. return c;
  1268. }
  1269. /* bgr24to32 & rgb24to32*/
  1270. if((srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_BGR32)
  1271. ||(srcFormat==IMGFMT_RGB24 && dstFormat==IMGFMT_RGB32))
  1272. {
  1273. c->swScale= bgr24to32Wrapper;
  1274. if(flags&SWS_PRINT_INFO)
  1275. printf("SwScaler: using unscaled %s -> %s special converter\n",
  1276. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1277. return c;
  1278. }
  1279. /* bgr15to16 */
  1280. if(srcFormat==IMGFMT_BGR15 && dstFormat==IMGFMT_BGR16)
  1281. {
  1282. c->swScale= bgr15to16Wrapper;
  1283. if(flags&SWS_PRINT_INFO)
  1284. printf("SwScaler: using unscaled %s -> %s special converter\n",
  1285. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1286. return c;
  1287. }
  1288. /* bgr24toYV12 */
  1289. if(srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_YV12)
  1290. {
  1291. c->swScale= bgr24toyv12Wrapper;
  1292. if(flags&SWS_PRINT_INFO)
  1293. printf("SwScaler: using unscaled %s -> %s special converter\n",
  1294. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1295. return c;
  1296. }
  1297. }
  1298. if(cpuCaps.hasMMX2)
  1299. {
  1300. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  1301. if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
  1302. {
  1303. if(flags&SWS_PRINT_INFO)
  1304. fprintf(stderr, "SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
  1305. }
  1306. }
  1307. else
  1308. c->canMMX2BeUsed=0;
  1309. /* dont use full vertical UV input/internaly if the source doesnt even have it */
  1310. if(isHalfChrV(srcFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_V);
  1311. /* dont use full horizontal UV input if the source doesnt even have it */
  1312. if(isHalfChrH(srcFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_H_INP);
  1313. /* dont use full horizontal UV internally if the destination doesnt even have it */
  1314. if(isHalfChrH(dstFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_H_INT);
  1315. if(flags&SWS_FULL_CHR_H_INP) c->chrSrcW= srcW;
  1316. else c->chrSrcW= (srcW+1)>>1;
  1317. if(flags&SWS_FULL_CHR_H_INT) c->chrDstW= dstW;
  1318. else c->chrDstW= (dstW+1)>>1;
  1319. if(flags&SWS_FULL_CHR_V) c->chrSrcH= srcH;
  1320. else c->chrSrcH= (srcH+1)>>1;
  1321. if(isHalfChrV(dstFormat)) c->chrDstH= (dstH+1)>>1;
  1322. else c->chrDstH= dstH;
  1323. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  1324. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  1325. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  1326. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  1327. // n-2 is the last chrominance sample available
  1328. // this is not perfect, but noone shuld notice the difference, the more correct variant
  1329. // would be like the vertical one, but that would require some special code for the
  1330. // first and last pixel
  1331. if(flags&SWS_FAST_BILINEAR)
  1332. {
  1333. if(c->canMMX2BeUsed)
  1334. {
  1335. c->lumXInc+= 20;
  1336. c->chrXInc+= 20;
  1337. }
  1338. //we dont use the x86asm scaler if mmx is available
  1339. else if(cpuCaps.hasMMX)
  1340. {
  1341. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  1342. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  1343. }
  1344. }
  1345. /* precalculate horizontal scaler filter coefficients */
  1346. {
  1347. const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
  1348. initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  1349. srcW , dstW, filterAlign, 1<<14, flags,
  1350. srcFilter->lumH, dstFilter->lumH);
  1351. initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  1352. (srcW+1)>>1, c->chrDstW, filterAlign, 1<<14, flags,
  1353. srcFilter->chrH, dstFilter->chrH);
  1354. #ifdef ARCH_X86
  1355. // cant downscale !!!
  1356. if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
  1357. {
  1358. initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode);
  1359. initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode);
  1360. }
  1361. #endif
  1362. } // Init Horizontal stuff
  1363. /* precalculate vertical scaler filter coefficients */
  1364. initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  1365. srcH , dstH, 1, (1<<12)-4, flags,
  1366. srcFilter->lumV, dstFilter->lumV);
  1367. initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  1368. (srcH+1)>>1, c->chrDstH, 1, (1<<12)-4, flags,
  1369. srcFilter->chrV, dstFilter->chrV);
  1370. // Calculate Buffer Sizes so that they wont run out while handling these damn slices
  1371. c->vLumBufSize= c->vLumFilterSize;
  1372. c->vChrBufSize= c->vChrFilterSize;
  1373. for(i=0; i<dstH; i++)
  1374. {
  1375. int chrI= i*c->chrDstH / dstH;
  1376. int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  1377. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<1));
  1378. nextSlice&= ~1; // Slices start at even boundaries
  1379. if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  1380. c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
  1381. if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>1))
  1382. c->vChrBufSize= (nextSlice>>1) - c->vChrFilterPos[chrI];
  1383. }
  1384. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  1385. c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
  1386. c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
  1387. //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)
  1388. for(i=0; i<c->vLumBufSize; i++)
  1389. c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
  1390. for(i=0; i<c->vChrBufSize; i++)
  1391. c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
  1392. //try to avoid drawing green stuff between the right end and the stride end
  1393. for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
  1394. for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
  1395. ASSERT(c->chrDstH <= dstH)
  1396. // pack filter data for mmx code
  1397. if(cpuCaps.hasMMX)
  1398. {
  1399. c->lumMmxFilter= (int16_t*)memalign(8, c->vLumFilterSize* dstH*4*sizeof(int16_t));
  1400. c->chrMmxFilter= (int16_t*)memalign(8, c->vChrFilterSize*c->chrDstH*4*sizeof(int16_t));
  1401. for(i=0; i<c->vLumFilterSize*dstH; i++)
  1402. c->lumMmxFilter[4*i]=c->lumMmxFilter[4*i+1]=c->lumMmxFilter[4*i+2]=c->lumMmxFilter[4*i+3]=
  1403. c->vLumFilter[i];
  1404. for(i=0; i<c->vChrFilterSize*c->chrDstH; i++)
  1405. c->chrMmxFilter[4*i]=c->chrMmxFilter[4*i+1]=c->chrMmxFilter[4*i+2]=c->chrMmxFilter[4*i+3]=
  1406. c->vChrFilter[i];
  1407. }
  1408. if(flags&SWS_PRINT_INFO)
  1409. {
  1410. #ifdef DITHER1XBPP
  1411. char *dither= " dithered";
  1412. #else
  1413. char *dither= "";
  1414. #endif
  1415. if(flags&SWS_FAST_BILINEAR)
  1416. fprintf(stderr, "\nSwScaler: FAST_BILINEAR scaler, ");
  1417. else if(flags&SWS_BILINEAR)
  1418. fprintf(stderr, "\nSwScaler: BILINEAR scaler, ");
  1419. else if(flags&SWS_BICUBIC)
  1420. fprintf(stderr, "\nSwScaler: BICUBIC scaler, ");
  1421. else if(flags&SWS_X)
  1422. fprintf(stderr, "\nSwScaler: Experimental scaler, ");
  1423. else if(flags&SWS_POINT)
  1424. fprintf(stderr, "\nSwScaler: Nearest Neighbor / POINT scaler, ");
  1425. else if(flags&SWS_AREA)
  1426. fprintf(stderr, "\nSwScaler: Area Averageing scaler, ");
  1427. else
  1428. fprintf(stderr, "\nSwScaler: ehh flags invalid?! ");
  1429. if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
  1430. fprintf(stderr, "from %s to%s %s ",
  1431. vo_format_name(srcFormat), dither, vo_format_name(dstFormat));
  1432. else
  1433. fprintf(stderr, "from %s to %s ",
  1434. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1435. if(cpuCaps.hasMMX2)
  1436. fprintf(stderr, "using MMX2\n");
  1437. else if(cpuCaps.has3DNow)
  1438. fprintf(stderr, "using 3DNOW\n");
  1439. else if(cpuCaps.hasMMX)
  1440. fprintf(stderr, "using MMX\n");
  1441. else
  1442. fprintf(stderr, "using C\n");
  1443. }
  1444. if((flags & SWS_PRINT_INFO) && verbose)
  1445. {
  1446. if(cpuCaps.hasMMX)
  1447. {
  1448. if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  1449. printf("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  1450. else
  1451. {
  1452. if(c->hLumFilterSize==4)
  1453. printf("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
  1454. else if(c->hLumFilterSize==8)
  1455. printf("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
  1456. else
  1457. printf("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
  1458. if(c->hChrFilterSize==4)
  1459. printf("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
  1460. else if(c->hChrFilterSize==8)
  1461. printf("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
  1462. else
  1463. printf("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
  1464. }
  1465. }
  1466. else
  1467. {
  1468. #ifdef ARCH_X86
  1469. printf("SwScaler: using X86-Asm scaler for horizontal scaling\n");
  1470. #else
  1471. if(flags & SWS_FAST_BILINEAR)
  1472. printf("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
  1473. else
  1474. printf("SwScaler: using C scaler for horizontal scaling\n");
  1475. #endif
  1476. }
  1477. if(isPlanarYUV(dstFormat))
  1478. {
  1479. if(c->vLumFilterSize==1)
  1480. printf("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
  1481. else
  1482. printf("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
  1483. }
  1484. else
  1485. {
  1486. if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
  1487. printf("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  1488. "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
  1489. else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
  1490. printf("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
  1491. else
  1492. printf("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
  1493. }
  1494. if(dstFormat==IMGFMT_BGR24)
  1495. printf("SwScaler: using %s YV12->BGR24 Converter\n",
  1496. cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
  1497. else if(dstFormat==IMGFMT_BGR32)
  1498. printf("SwScaler: using %s YV12->BGR32 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  1499. else if(dstFormat==IMGFMT_BGR16)
  1500. printf("SwScaler: using %s YV12->BGR16 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  1501. else if(dstFormat==IMGFMT_BGR15)
  1502. printf("SwScaler: using %s YV12->BGR15 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  1503. printf("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  1504. }
  1505. if((flags & SWS_PRINT_INFO) && verbose>1)
  1506. {
  1507. printf("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1508. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  1509. printf("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1510. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  1511. }
  1512. c->swScale= swScale;
  1513. return c;
  1514. }
  1515. /**
  1516. * returns a normalized gaussian curve used to filter stuff
  1517. * quality=3 is high quality, lowwer is lowwer quality
  1518. */
  1519. SwsVector *getGaussianVec(double variance, double quality){
  1520. const int length= (int)(variance*quality + 0.5) | 1;
  1521. int i;
  1522. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1523. double middle= (length-1)*0.5;
  1524. SwsVector *vec= malloc(sizeof(SwsVector));
  1525. vec->coeff= coeff;
  1526. vec->length= length;
  1527. for(i=0; i<length; i++)
  1528. {
  1529. double dist= i-middle;
  1530. coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
  1531. }
  1532. normalizeVec(vec, 1.0);
  1533. return vec;
  1534. }
  1535. SwsVector *getConstVec(double c, int length){
  1536. int i;
  1537. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1538. SwsVector *vec= malloc(sizeof(SwsVector));
  1539. vec->coeff= coeff;
  1540. vec->length= length;
  1541. for(i=0; i<length; i++)
  1542. coeff[i]= c;
  1543. return vec;
  1544. }
  1545. SwsVector *getIdentityVec(void){
  1546. double *coeff= memalign(sizeof(double), sizeof(double));
  1547. SwsVector *vec= malloc(sizeof(SwsVector));
  1548. coeff[0]= 1.0;
  1549. vec->coeff= coeff;
  1550. vec->length= 1;
  1551. return vec;
  1552. }
  1553. void normalizeVec(SwsVector *a, double height){
  1554. int i;
  1555. double sum=0;
  1556. double inv;
  1557. for(i=0; i<a->length; i++)
  1558. sum+= a->coeff[i];
  1559. inv= height/sum;
  1560. for(i=0; i<a->length; i++)
  1561. a->coeff[i]*= height;
  1562. }
  1563. void scaleVec(SwsVector *a, double scalar){
  1564. int i;
  1565. for(i=0; i<a->length; i++)
  1566. a->coeff[i]*= scalar;
  1567. }
  1568. static SwsVector *getConvVec(SwsVector *a, SwsVector *b){
  1569. int length= a->length + b->length - 1;
  1570. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1571. int i, j;
  1572. SwsVector *vec= malloc(sizeof(SwsVector));
  1573. vec->coeff= coeff;
  1574. vec->length= length;
  1575. for(i=0; i<length; i++) coeff[i]= 0.0;
  1576. for(i=0; i<a->length; i++)
  1577. {
  1578. for(j=0; j<b->length; j++)
  1579. {
  1580. coeff[i+j]+= a->coeff[i]*b->coeff[j];
  1581. }
  1582. }
  1583. return vec;
  1584. }
  1585. static SwsVector *sumVec(SwsVector *a, SwsVector *b){
  1586. int length= MAX(a->length, b->length);
  1587. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1588. int i;
  1589. SwsVector *vec= malloc(sizeof(SwsVector));
  1590. vec->coeff= coeff;
  1591. vec->length= length;
  1592. for(i=0; i<length; i++) coeff[i]= 0.0;
  1593. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1594. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  1595. return vec;
  1596. }
  1597. static SwsVector *diffVec(SwsVector *a, SwsVector *b){
  1598. int length= MAX(a->length, b->length);
  1599. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1600. int i;
  1601. SwsVector *vec= malloc(sizeof(SwsVector));
  1602. vec->coeff= coeff;
  1603. vec->length= length;
  1604. for(i=0; i<length; i++) coeff[i]= 0.0;
  1605. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  1606. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  1607. return vec;
  1608. }
  1609. /* shift left / or right if "shift" is negative */
  1610. static SwsVector *getShiftedVec(SwsVector *a, int shift){
  1611. int length= a->length + ABS(shift)*2;
  1612. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1613. int i;
  1614. SwsVector *vec= malloc(sizeof(SwsVector));
  1615. vec->coeff= coeff;
  1616. vec->length= length;
  1617. for(i=0; i<length; i++) coeff[i]= 0.0;
  1618. for(i=0; i<a->length; i++)
  1619. {
  1620. coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  1621. }
  1622. return vec;
  1623. }
  1624. void shiftVec(SwsVector *a, int shift){
  1625. SwsVector *shifted= getShiftedVec(a, shift);
  1626. free(a->coeff);
  1627. a->coeff= shifted->coeff;
  1628. a->length= shifted->length;
  1629. free(shifted);
  1630. }
  1631. void addVec(SwsVector *a, SwsVector *b){
  1632. SwsVector *sum= sumVec(a, b);
  1633. free(a->coeff);
  1634. a->coeff= sum->coeff;
  1635. a->length= sum->length;
  1636. free(sum);
  1637. }
  1638. void subVec(SwsVector *a, SwsVector *b){
  1639. SwsVector *diff= diffVec(a, b);
  1640. free(a->coeff);
  1641. a->coeff= diff->coeff;
  1642. a->length= diff->length;
  1643. free(diff);
  1644. }
  1645. void convVec(SwsVector *a, SwsVector *b){
  1646. SwsVector *conv= getConvVec(a, b);
  1647. free(a->coeff);
  1648. a->coeff= conv->coeff;
  1649. a->length= conv->length;
  1650. free(conv);
  1651. }
  1652. SwsVector *cloneVec(SwsVector *a){
  1653. double *coeff= memalign(sizeof(double), a->length*sizeof(double));
  1654. int i;
  1655. SwsVector *vec= malloc(sizeof(SwsVector));
  1656. vec->coeff= coeff;
  1657. vec->length= a->length;
  1658. for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
  1659. return vec;
  1660. }
  1661. void printVec(SwsVector *a){
  1662. int i;
  1663. double max=0;
  1664. double min=0;
  1665. double range;
  1666. for(i=0; i<a->length; i++)
  1667. if(a->coeff[i]>max) max= a->coeff[i];
  1668. for(i=0; i<a->length; i++)
  1669. if(a->coeff[i]<min) min= a->coeff[i];
  1670. range= max - min;
  1671. for(i=0; i<a->length; i++)
  1672. {
  1673. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  1674. printf("%1.3f ", a->coeff[i]);
  1675. for(;x>0; x--) printf(" ");
  1676. printf("|\n");
  1677. }
  1678. }
  1679. void freeVec(SwsVector *a){
  1680. if(!a) return;
  1681. if(a->coeff) free(a->coeff);
  1682. a->coeff=NULL;
  1683. a->length=0;
  1684. free(a);
  1685. }
  1686. void freeSwsContext(SwsContext *c){
  1687. int i;
  1688. if(!c) return;
  1689. if(c->lumPixBuf)
  1690. {
  1691. for(i=0; i<c->vLumBufSize; i++)
  1692. {
  1693. if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
  1694. c->lumPixBuf[i]=NULL;
  1695. }
  1696. free(c->lumPixBuf);
  1697. c->lumPixBuf=NULL;
  1698. }
  1699. if(c->chrPixBuf)
  1700. {
  1701. for(i=0; i<c->vChrBufSize; i++)
  1702. {
  1703. if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
  1704. c->chrPixBuf[i]=NULL;
  1705. }
  1706. free(c->chrPixBuf);
  1707. c->chrPixBuf=NULL;
  1708. }
  1709. if(c->vLumFilter) free(c->vLumFilter);
  1710. c->vLumFilter = NULL;
  1711. if(c->vChrFilter) free(c->vChrFilter);
  1712. c->vChrFilter = NULL;
  1713. if(c->hLumFilter) free(c->hLumFilter);
  1714. c->hLumFilter = NULL;
  1715. if(c->hChrFilter) free(c->hChrFilter);
  1716. c->hChrFilter = NULL;
  1717. if(c->vLumFilterPos) free(c->vLumFilterPos);
  1718. c->vLumFilterPos = NULL;
  1719. if(c->vChrFilterPos) free(c->vChrFilterPos);
  1720. c->vChrFilterPos = NULL;
  1721. if(c->hLumFilterPos) free(c->hLumFilterPos);
  1722. c->hLumFilterPos = NULL;
  1723. if(c->hChrFilterPos) free(c->hChrFilterPos);
  1724. c->hChrFilterPos = NULL;
  1725. if(c->lumMmxFilter) free(c->lumMmxFilter);
  1726. c->lumMmxFilter = NULL;
  1727. if(c->chrMmxFilter) free(c->chrMmxFilter);
  1728. c->chrMmxFilter = NULL;
  1729. free(c);
  1730. }