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.

1663 lines
45KB

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