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.

2008 lines
55KB

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