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.

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