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.

1930 lines
53KB

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