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.

1964 lines
54KB

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