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.

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