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.

2567 lines
73KB

  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, YVU9/IF09
  17. supported output formats: YV12, I420/IYUV, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  18. {BGR,RGB}{1,4,8,15,16} support dithering
  19. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  20. YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
  21. x -> x
  22. YUV9 -> YV12
  23. YUV9/YV12 -> Y800
  24. Y800 -> YUV9/YV12
  25. BGR24 -> BGR32 & RGB24 -> RGB32
  26. BGR32 -> BGR24 & RGB32 -> RGB24
  27. BGR15 -> BGR16
  28. */
  29. /*
  30. tested special converters (most are tested actually but i didnt write it down ...)
  31. YV12 -> BGR16
  32. YV12 -> YV12
  33. BGR15 -> BGR16
  34. BGR16 -> BGR16
  35. YVU9 -> YV12
  36. untested special converters
  37. YV12/I420 -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
  38. YV12/I420 -> YV12/I420
  39. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  40. BGR24 -> BGR32 & RGB24 -> RGB32
  41. BGR32 -> BGR24 & RGB32 -> RGB24
  42. BGR24 -> YV12
  43. */
  44. #include <inttypes.h>
  45. #include <string.h>
  46. #include <math.h>
  47. #include <stdio.h>
  48. #include "../config.h"
  49. #include "../mangle.h"
  50. #include <assert.h>
  51. #ifdef HAVE_MALLOC_H
  52. #include <malloc.h>
  53. #else
  54. #include <stdlib.h>
  55. #endif
  56. #include "swscale.h"
  57. #include "../cpudetect.h"
  58. #include "../bswap.h"
  59. #include "../libvo/img_format.h"
  60. #include "rgb2rgb.h"
  61. #include "../libvo/fastmemcpy.h"
  62. #include "../mp_msg.h"
  63. #define MSG_WARN(args...) mp_msg(MSGT_SWS,MSGL_WARN, ##args )
  64. #define MSG_FATAL(args...) mp_msg(MSGT_SWS,MSGL_FATAL, ##args )
  65. #define MSG_ERR(args...) mp_msg(MSGT_SWS,MSGL_ERR, ##args )
  66. #define MSG_V(args...) mp_msg(MSGT_SWS,MSGL_V, ##args )
  67. #define MSG_DBG2(args...) mp_msg(MSGT_SWS,MSGL_DBG2, ##args )
  68. #define MSG_INFO(args...) mp_msg(MSGT_SWS,MSGL_INFO, ##args )
  69. #undef MOVNTQ
  70. #undef PAVGB
  71. //#undef HAVE_MMX2
  72. //#define HAVE_3DNOW
  73. //#undef HAVE_MMX
  74. //#undef ARCH_X86
  75. //#define WORDS_BIGENDIAN
  76. #define DITHER1XBPP
  77. #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
  78. #define RET 0xC3 //near return opcode for X86
  79. #ifdef MP_DEBUG
  80. #define ASSERT(x) assert(x);
  81. #else
  82. #define ASSERT(x) ;
  83. #endif
  84. #ifdef M_PI
  85. #define PI M_PI
  86. #else
  87. #define PI 3.14159265358979323846
  88. #endif
  89. //FIXME replace this with something faster
  90. #define isPlanarYUV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YVU9)
  91. #define isYUV(x) ((x)==IMGFMT_YUY2 || isPlanarYUV(x))
  92. #define isGray(x) ((x)==IMGFMT_Y800)
  93. #define isRGB(x) (((x)&IMGFMT_RGB_MASK)==IMGFMT_RGB)
  94. #define isBGR(x) (((x)&IMGFMT_BGR_MASK)==IMGFMT_BGR)
  95. #define isSupportedIn(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YUY2 \
  96. || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
  97. || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
  98. || (x)==IMGFMT_Y800 || (x)==IMGFMT_YVU9)
  99. #define isSupportedOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 \
  100. || isRGB(x) || isBGR(x)\
  101. || (x)==IMGFMT_Y800 || (x)==IMGFMT_YVU9)
  102. #define isPacked(x) ((x)==IMGFMT_YUY2 || isRGB(x) || isBGR(x))
  103. #define RGB2YUV_SHIFT 16
  104. #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
  105. #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
  106. #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  107. #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
  108. #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
  109. #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
  110. #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
  111. #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  112. #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
  113. extern int verbose; // defined in mplayer.c
  114. /*
  115. NOTES
  116. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  117. TODO
  118. more intelligent missalignment avoidance for the horizontal scaler
  119. write special vertical cubic upscale version
  120. Optimize C code (yv12 / minmax)
  121. add support for packed pixel yuv input & output
  122. add support for Y8 output
  123. optimize bgr24 & bgr32
  124. add BGR4 output support
  125. write special BGR->BGR scaler
  126. deglobalize yuv2rgb*.c
  127. */
  128. #define ABS(a) ((a) > 0 ? (a) : (-(a)))
  129. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  130. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  131. #ifdef ARCH_X86
  132. #define CAN_COMPILE_X86_ASM
  133. #endif
  134. #ifdef CAN_COMPILE_X86_ASM
  135. static uint64_t __attribute__((aligned(8))) yCoeff= 0x2568256825682568LL;
  136. static uint64_t __attribute__((aligned(8))) vrCoeff= 0x3343334333433343LL;
  137. static uint64_t __attribute__((aligned(8))) ubCoeff= 0x40cf40cf40cf40cfLL;
  138. static uint64_t __attribute__((aligned(8))) vgCoeff= 0xE5E2E5E2E5E2E5E2LL;
  139. static uint64_t __attribute__((aligned(8))) ugCoeff= 0xF36EF36EF36EF36ELL;
  140. static uint64_t __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
  141. static uint64_t __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
  142. static uint64_t __attribute__((aligned(8))) w400= 0x0400040004000400LL;
  143. static uint64_t __attribute__((aligned(8))) w80= 0x0080008000800080LL;
  144. static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
  145. static uint64_t __attribute__((aligned(8))) w02= 0x0002000200020002LL;
  146. static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
  147. static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
  148. static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
  149. static uint64_t __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
  150. static volatile uint64_t __attribute__((aligned(8))) b5Dither;
  151. static volatile uint64_t __attribute__((aligned(8))) g5Dither;
  152. static volatile uint64_t __attribute__((aligned(8))) g6Dither;
  153. static volatile uint64_t __attribute__((aligned(8))) r5Dither;
  154. static uint64_t __attribute__((aligned(8))) dither4[2]={
  155. 0x0103010301030103LL,
  156. 0x0200020002000200LL,};
  157. static uint64_t __attribute__((aligned(8))) dither8[2]={
  158. 0x0602060206020602LL,
  159. 0x0004000400040004LL,};
  160. static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
  161. static uint64_t __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
  162. static uint64_t __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
  163. static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
  164. static uint64_t __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
  165. static uint64_t __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
  166. static uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
  167. static uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
  168. static uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
  169. #ifdef FAST_BGR2YV12
  170. static const uint64_t bgr2YCoeff __attribute__((aligned(8))) = 0x000000210041000DULL;
  171. static const uint64_t bgr2UCoeff __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
  172. static const uint64_t bgr2VCoeff __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
  173. #else
  174. static const uint64_t bgr2YCoeff __attribute__((aligned(8))) = 0x000020E540830C8BULL;
  175. static const uint64_t bgr2UCoeff __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
  176. static const uint64_t bgr2VCoeff __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
  177. #endif
  178. static const uint64_t bgr2YOffset __attribute__((aligned(8))) = 0x1010101010101010ULL;
  179. static const uint64_t bgr2UVOffset __attribute__((aligned(8)))= 0x8080808080808080ULL;
  180. static const uint64_t w1111 __attribute__((aligned(8))) = 0x0001000100010001ULL;
  181. #endif
  182. // clipping helper table for C implementations:
  183. static unsigned char clip_table[768];
  184. //global sws_flags from the command line
  185. int sws_flags=2;
  186. //global srcFilter
  187. SwsFilter src_filter= {NULL, NULL, NULL, NULL};
  188. float sws_lum_gblur= 0.0;
  189. float sws_chr_gblur= 0.0;
  190. int sws_chr_vshift= 0;
  191. int sws_chr_hshift= 0;
  192. float sws_chr_sharpen= 0.0;
  193. float sws_lum_sharpen= 0.0;
  194. /* cpuCaps combined from cpudetect and whats actually compiled in
  195. (if there is no support for something compiled in it wont appear here) */
  196. static CpuCaps cpuCaps;
  197. void (*swScale)(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
  198. int srcSliceH, uint8_t* dst[], int dstStride[])=NULL;
  199. static SwsVector *getConvVec(SwsVector *a, SwsVector *b);
  200. static inline void orderYUV(int format, uint8_t * sortedP[], int sortedStride[], uint8_t * p[], int stride[]);
  201. void *yuv2rgb_c_init (unsigned bpp, int mode, void *table_rV[256], void *table_gU[256], int table_gV[256], void *table_bU[256]);
  202. extern const uint8_t dither_2x2_4[2][8];
  203. extern const uint8_t dither_2x2_8[2][8];
  204. extern const uint8_t dither_8x8_32[8][8];
  205. extern const uint8_t dither_8x8_73[8][8];
  206. extern const uint8_t dither_8x8_220[8][8];
  207. #ifdef CAN_COMPILE_X86_ASM
  208. void in_asm_used_var_warning_killer()
  209. {
  210. volatile int i= yCoeff+vrCoeff+ubCoeff+vgCoeff+ugCoeff+bF8+bFC+w400+w80+w10+
  211. bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+
  212. M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
  213. if(i) i=0;
  214. }
  215. #endif
  216. static int testFormat[]={
  217. IMGFMT_YVU9,
  218. IMGFMT_YV12,
  219. //IMGFMT_IYUV,
  220. IMGFMT_I420,
  221. IMGFMT_BGR15,
  222. IMGFMT_BGR16,
  223. IMGFMT_BGR24,
  224. IMGFMT_BGR32,
  225. IMGFMT_RGB24,
  226. IMGFMT_RGB32,
  227. //IMGFMT_Y8,
  228. IMGFMT_Y800,
  229. //IMGFMT_YUY2,
  230. 0
  231. };
  232. static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h){
  233. int x,y;
  234. uint64_t ssd=0;
  235. for(y=0; y<h; y++){
  236. for(x=0; x<w; x++){
  237. int d= src1[x + y*stride1] - src2[x + y*stride2];
  238. ssd+= d*d;
  239. }
  240. }
  241. return ssd;
  242. }
  243. // test by ref -> src -> dst -> out & compare out against ref
  244. // ref & out are YV12
  245. static void doTest(uint8_t *ref[3], int refStride[3], int w, int h, int srcFormat, int dstFormat,
  246. int srcW, int srcH, int dstW, int dstH, int flags){
  247. uint8_t *src[3];
  248. uint8_t *dst[3];
  249. uint8_t *out[3];
  250. int srcStride[3], dstStride[3];
  251. int i;
  252. uint64_t ssdY, ssdU, ssdV;
  253. SwsContext *srcContext, *dstContext, *outContext;
  254. for(i=0; i<3; i++){
  255. // avoid stride % bpp != 0
  256. if(srcFormat==IMGFMT_RGB24 || srcFormat==IMGFMT_BGR24)
  257. srcStride[i]= srcW*3;
  258. else
  259. srcStride[i]= srcW*4;
  260. if(dstFormat==IMGFMT_RGB24 || dstFormat==IMGFMT_BGR24)
  261. dstStride[i]= dstW*3;
  262. else
  263. dstStride[i]= dstW*4;
  264. src[i]= malloc(srcStride[i]*srcH);
  265. dst[i]= malloc(dstStride[i]*dstH);
  266. out[i]= malloc(refStride[i]*h);
  267. }
  268. srcContext= getSwsContext(w, h, IMGFMT_YV12, srcW, srcH, srcFormat, flags, NULL, NULL);
  269. dstContext= getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL);
  270. outContext= getSwsContext(dstW, dstH, dstFormat, w, h, IMGFMT_YV12, flags, NULL, NULL);
  271. if(srcContext==NULL ||dstContext==NULL ||outContext==NULL){
  272. printf("Failed allocating swsContext\n");
  273. goto end;
  274. }
  275. // printf("test %X %X %X -> %X %X %X\n", (int)ref[0], (int)ref[1], (int)ref[2],
  276. // (int)src[0], (int)src[1], (int)src[2]);
  277. srcContext->swScale(srcContext, ref, refStride, 0, h , src, srcStride);
  278. dstContext->swScale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
  279. outContext->swScale(outContext, dst, dstStride, 0, dstH, out, refStride);
  280. ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
  281. ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
  282. ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);
  283. if(isGray(srcFormat) || isGray(dstFormat)) ssdU=ssdV=0; //FIXME check that output is really gray
  284. ssdY/= w*h;
  285. ssdU/= w*h/4;
  286. ssdV/= w*h/4;
  287. if(ssdY>100 || ssdU>50 || ssdV>50){
  288. printf(" %s %dx%d -> %s %4dx%4d flags=%2d SSD=%5lld,%5lld,%5lld\n",
  289. vo_format_name(srcFormat), srcW, srcH,
  290. vo_format_name(dstFormat), dstW, dstH,
  291. flags,
  292. ssdY, ssdU, ssdV);
  293. }
  294. end:
  295. freeSwsContext(srcContext);
  296. freeSwsContext(dstContext);
  297. freeSwsContext(outContext);
  298. for(i=0; i<3; i++){
  299. free(src[i]);
  300. free(dst[i]);
  301. free(out[i]);
  302. }
  303. }
  304. static void selfTest(uint8_t *src[3], int stride[3], int w, int h){
  305. int srcFormat, dstFormat, srcFormatIndex, dstFormatIndex;
  306. int srcW, srcH, dstW, dstH;
  307. int flags;
  308. for(srcFormatIndex=0; ;srcFormatIndex++){
  309. srcFormat= testFormat[srcFormatIndex];
  310. if(!srcFormat) break;
  311. for(dstFormatIndex=0; ;dstFormatIndex++){
  312. dstFormat= testFormat[dstFormatIndex];
  313. if(!dstFormat) break;
  314. if(!isSupportedOut(dstFormat)) continue;
  315. printf("%s -> %s\n",
  316. vo_format_name(srcFormat),
  317. vo_format_name(dstFormat));
  318. srcW= w+w/3;
  319. srcH= h+h/3;
  320. for(dstW=w; dstW<w*2; dstW+= dstW/3){
  321. for(dstH=h; dstH<h*2; dstH+= dstH/3){
  322. for(flags=1; flags<33; flags*=2)
  323. doTest(src, stride, w, h, srcFormat, dstFormat,
  324. srcW, srcH, dstW, dstH, flags);
  325. }
  326. }
  327. }
  328. }
  329. }
  330. static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  331. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  332. uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
  333. {
  334. //FIXME Optimize (just quickly writen not opti..)
  335. int i;
  336. for(i=0; i<dstW; i++)
  337. {
  338. int val=0;
  339. int j;
  340. for(j=0; j<lumFilterSize; j++)
  341. val += lumSrc[j][i] * lumFilter[j];
  342. dest[i]= MIN(MAX(val>>19, 0), 255);
  343. }
  344. if(uDest != NULL)
  345. for(i=0; i<chrDstW; i++)
  346. {
  347. int u=0;
  348. int v=0;
  349. int j;
  350. for(j=0; j<chrFilterSize; j++)
  351. {
  352. u += chrSrc[j][i] * chrFilter[j];
  353. v += chrSrc[j][i + 2048] * chrFilter[j];
  354. }
  355. uDest[i]= MIN(MAX(u>>19, 0), 255);
  356. vDest[i]= MIN(MAX(v>>19, 0), 255);
  357. }
  358. }
  359. #define YSCALE_YUV_2_RGBX_C(type) \
  360. for(i=0; i<(dstW>>1); i++){\
  361. int j;\
  362. int Y1=0;\
  363. int Y2=0;\
  364. int U=0;\
  365. int V=0;\
  366. type *r, *b, *g;\
  367. const int i2= 2*i;\
  368. \
  369. for(j=0; j<lumFilterSize; j++)\
  370. {\
  371. Y1 += lumSrc[j][i2] * lumFilter[j];\
  372. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  373. }\
  374. for(j=0; j<chrFilterSize; j++)\
  375. {\
  376. U += chrSrc[j][i] * chrFilter[j];\
  377. V += chrSrc[j][i+2048] * chrFilter[j];\
  378. }\
  379. Y1>>=19;\
  380. Y2>>=19;\
  381. U >>=19;\
  382. V >>=19;\
  383. if((Y1|Y2|U|V)&256)\
  384. {\
  385. if(Y1>255) Y1=255;\
  386. else if(Y1<0)Y1=0;\
  387. if(Y2>255) Y2=255;\
  388. else if(Y2<0)Y2=0;\
  389. if(U>255) U=255;\
  390. else if(U<0) U=0;\
  391. if(V>255) V=255;\
  392. else if(V<0) V=0;\
  393. }\
  394. r = c->table_rV[V];\
  395. g = c->table_gU[U] + c->table_gV[V];\
  396. b = c->table_bU[U];\
  397. #define YSCALE_YUV_2_RGB2_C(type) \
  398. for(i=0; i<(dstW>>1); i++){\
  399. const int i2= 2*i;\
  400. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19;\
  401. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;\
  402. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19;\
  403. int V= (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;\
  404. type *r, *b, *g;\
  405. r = c->table_rV[V];\
  406. g = c->table_gU[U] + c->table_gV[V];\
  407. b = c->table_bU[U];\
  408. #define YSCALE_YUV_2_RGB1_C(type) \
  409. for(i=0; i<(dstW>>1); i++){\
  410. const int i2= 2*i;\
  411. int Y1= buf0[i2 ]>>7;\
  412. int Y2= buf0[i2+1]>>7;\
  413. int U= (uvbuf1[i ])>>7;\
  414. int V= (uvbuf1[i+2048])>>7;\
  415. type *r, *b, *g;\
  416. r = c->table_rV[V];\
  417. g = c->table_gU[U] + c->table_gV[V];\
  418. b = c->table_bU[U];\
  419. #define YSCALE_YUV_2_RGB1B_C(type) \
  420. for(i=0; i<(dstW>>1); i++){\
  421. const int i2= 2*i;\
  422. int Y1= buf0[i2 ]>>7;\
  423. int Y2= buf0[i2+1]>>7;\
  424. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  425. int V= (uvbuf0[i+2048] + uvbuf1[i+2048])>>8;\
  426. type *r, *b, *g;\
  427. r = c->table_rV[V];\
  428. g = c->table_gU[U] + c->table_gV[V];\
  429. b = c->table_bU[U];\
  430. #define YSCALE_YUV_2_ANYRGB_C(func)\
  431. switch(c->dstFormat)\
  432. {\
  433. case IMGFMT_BGR32:\
  434. case IMGFMT_RGB32:\
  435. func(uint32_t)\
  436. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  437. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  438. } \
  439. break;\
  440. case IMGFMT_RGB24:\
  441. func(uint8_t)\
  442. ((uint8_t*)dest)[0]= r[Y1];\
  443. ((uint8_t*)dest)[1]= g[Y1];\
  444. ((uint8_t*)dest)[2]= b[Y1];\
  445. ((uint8_t*)dest)[3]= r[Y2];\
  446. ((uint8_t*)dest)[4]= g[Y2];\
  447. ((uint8_t*)dest)[5]= b[Y2];\
  448. ((uint8_t*)dest)+=6;\
  449. }\
  450. break;\
  451. case IMGFMT_BGR24:\
  452. func(uint8_t)\
  453. ((uint8_t*)dest)[0]= b[Y1];\
  454. ((uint8_t*)dest)[1]= g[Y1];\
  455. ((uint8_t*)dest)[2]= r[Y1];\
  456. ((uint8_t*)dest)[3]= b[Y2];\
  457. ((uint8_t*)dest)[4]= g[Y2];\
  458. ((uint8_t*)dest)[5]= r[Y2];\
  459. ((uint8_t*)dest)+=6;\
  460. }\
  461. break;\
  462. case IMGFMT_RGB16:\
  463. case IMGFMT_BGR16:\
  464. {\
  465. const int dr1= dither_2x2_8[y&1 ][0];\
  466. const int dg1= dither_2x2_4[y&1 ][0];\
  467. const int db1= dither_2x2_8[(y&1)^1][0];\
  468. const int dr2= dither_2x2_8[y&1 ][1];\
  469. const int dg2= dither_2x2_4[y&1 ][1];\
  470. const int db2= dither_2x2_8[(y&1)^1][1];\
  471. func(uint16_t)\
  472. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  473. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  474. }\
  475. }\
  476. break;\
  477. case IMGFMT_RGB15:\
  478. case IMGFMT_BGR15:\
  479. {\
  480. const int dr1= dither_2x2_8[y&1 ][0];\
  481. const int dg1= dither_2x2_8[y&1 ][1];\
  482. const int db1= dither_2x2_8[(y&1)^1][0];\
  483. const int dr2= dither_2x2_8[y&1 ][1];\
  484. const int dg2= dither_2x2_8[y&1 ][0];\
  485. const int db2= dither_2x2_8[(y&1)^1][1];\
  486. func(uint16_t)\
  487. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  488. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  489. }\
  490. }\
  491. break;\
  492. case IMGFMT_RGB8:\
  493. case IMGFMT_BGR8:\
  494. {\
  495. const uint8_t * const d64= dither_8x8_73[y&7];\
  496. const uint8_t * const d32= dither_8x8_32[y&7];\
  497. func(uint8_t)\
  498. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  499. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  500. }\
  501. }\
  502. break;\
  503. case IMGFMT_RGB4:\
  504. case IMGFMT_BGR4:\
  505. {\
  506. const uint8_t * const d64= dither_8x8_73 [y&7];\
  507. const uint8_t * const d128=dither_8x8_220[y&7];\
  508. func(uint8_t)\
  509. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  510. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  511. }\
  512. }\
  513. break;\
  514. case IMGFMT_RGB1:\
  515. case IMGFMT_BGR1:\
  516. {\
  517. const uint8_t * const d128=dither_8x8_220[y&7];\
  518. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  519. for(i=0; i<dstW-7; i+=8){\
  520. int acc;\
  521. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  522. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  523. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  524. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  525. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  526. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  527. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  528. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  529. ((uint8_t*)dest)[0]= acc;\
  530. ((uint8_t*)dest)++;\
  531. }\
  532. \
  533. /*\
  534. ((uint8_t*)dest)-= dstW>>4;\
  535. {\
  536. int acc=0;\
  537. int left=0;\
  538. static int top[1024];\
  539. static int last_new[1024][1024];\
  540. static int last_in3[1024][1024];\
  541. static int drift[1024][1024];\
  542. int topLeft=0;\
  543. int shift=0;\
  544. int count=0;\
  545. const uint8_t * const d128=dither_8x8_220[y&7];\
  546. int error_new=0;\
  547. int error_in3=0;\
  548. int f=0;\
  549. \
  550. for(i=dstW>>1; i<dstW; i++){\
  551. int in= ((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19);\
  552. int in2 = (76309 * (in - 16) + 32768) >> 16;\
  553. int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
  554. int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
  555. + (last_new[y][i] - in3)*f/256;\
  556. int new= old> 128 ? 255 : 0;\
  557. \
  558. error_new+= ABS(last_new[y][i] - new);\
  559. error_in3+= ABS(last_in3[y][i] - in3);\
  560. f= error_new - error_in3*4;\
  561. if(f<0) f=0;\
  562. if(f>256) f=256;\
  563. \
  564. topLeft= top[i];\
  565. left= top[i]= old - new;\
  566. last_new[y][i]= new;\
  567. last_in3[y][i]= in3;\
  568. \
  569. acc+= acc + (new&1);\
  570. if((i&7)==6){\
  571. ((uint8_t*)dest)[0]= acc;\
  572. ((uint8_t*)dest)++;\
  573. }\
  574. }\
  575. }\
  576. */\
  577. }\
  578. break;\
  579. }\
  580. static inline void yuv2rgbXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  581. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  582. uint8_t *dest, int dstW, int y)
  583. {
  584. int i;
  585. switch(c->dstFormat)
  586. {
  587. case IMGFMT_RGB32:
  588. case IMGFMT_BGR32:
  589. YSCALE_YUV_2_RGBX_C(uint32_t)
  590. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];
  591. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];
  592. }
  593. break;
  594. case IMGFMT_RGB24:
  595. YSCALE_YUV_2_RGBX_C(uint8_t)
  596. ((uint8_t*)dest)[0]= r[Y1];
  597. ((uint8_t*)dest)[1]= g[Y1];
  598. ((uint8_t*)dest)[2]= b[Y1];
  599. ((uint8_t*)dest)[3]= r[Y2];
  600. ((uint8_t*)dest)[4]= g[Y2];
  601. ((uint8_t*)dest)[5]= b[Y2];
  602. ((uint8_t*)dest)+=6;
  603. }
  604. break;
  605. case IMGFMT_BGR24:
  606. YSCALE_YUV_2_RGBX_C(uint8_t)
  607. ((uint8_t*)dest)[0]= b[Y1];
  608. ((uint8_t*)dest)[1]= g[Y1];
  609. ((uint8_t*)dest)[2]= r[Y1];
  610. ((uint8_t*)dest)[3]= b[Y2];
  611. ((uint8_t*)dest)[4]= g[Y2];
  612. ((uint8_t*)dest)[5]= r[Y2];
  613. ((uint8_t*)dest)+=6;
  614. }
  615. break;
  616. case IMGFMT_RGB16:
  617. case IMGFMT_BGR16:
  618. {
  619. const int dr1= dither_2x2_8[y&1 ][0];
  620. const int dg1= dither_2x2_4[y&1 ][0];
  621. const int db1= dither_2x2_8[(y&1)^1][0];
  622. const int dr2= dither_2x2_8[y&1 ][1];
  623. const int dg2= dither_2x2_4[y&1 ][1];
  624. const int db2= dither_2x2_8[(y&1)^1][1];
  625. YSCALE_YUV_2_RGBX_C(uint16_t)
  626. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  627. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  628. }
  629. }
  630. break;
  631. case IMGFMT_RGB15:
  632. case IMGFMT_BGR15:
  633. {
  634. const int dr1= dither_2x2_8[y&1 ][0];
  635. const int dg1= dither_2x2_8[y&1 ][1];
  636. const int db1= dither_2x2_8[(y&1)^1][0];
  637. const int dr2= dither_2x2_8[y&1 ][1];
  638. const int dg2= dither_2x2_8[y&1 ][0];
  639. const int db2= dither_2x2_8[(y&1)^1][1];
  640. YSCALE_YUV_2_RGBX_C(uint16_t)
  641. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  642. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  643. }
  644. }
  645. break;
  646. case IMGFMT_RGB8:
  647. case IMGFMT_BGR8:
  648. {
  649. const uint8_t * const d64= dither_8x8_73[y&7];
  650. const uint8_t * const d32= dither_8x8_32[y&7];
  651. YSCALE_YUV_2_RGBX_C(uint8_t)
  652. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];
  653. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];
  654. }
  655. }
  656. break;
  657. case IMGFMT_RGB4:
  658. case IMGFMT_BGR4:
  659. {
  660. const uint8_t * const d64= dither_8x8_73 [y&7];
  661. const uint8_t * const d128=dither_8x8_220[y&7];
  662. YSCALE_YUV_2_RGBX_C(uint8_t)
  663. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];
  664. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];
  665. }
  666. }
  667. break;
  668. case IMGFMT_RGB1:
  669. case IMGFMT_BGR1:
  670. {
  671. const uint8_t * const d128=dither_8x8_220[y&7];
  672. uint8_t *g= c->table_gU[128] + c->table_gV[128];
  673. int acc=0;
  674. for(i=0; i<dstW-1; i+=2){
  675. int j;
  676. int Y1=0;
  677. int Y2=0;
  678. for(j=0; j<lumFilterSize; j++)
  679. {
  680. Y1 += lumSrc[j][i] * lumFilter[j];
  681. Y2 += lumSrc[j][i+1] * lumFilter[j];
  682. }
  683. Y1>>=19;
  684. Y2>>=19;
  685. if((Y1|Y2)&256)
  686. {
  687. if(Y1>255) Y1=255;
  688. else if(Y1<0)Y1=0;
  689. if(Y2>255) Y2=255;
  690. else if(Y2<0)Y2=0;
  691. }
  692. acc+= acc + g[Y1+d128[(i+0)&7]];
  693. acc+= acc + g[Y2+d128[(i+1)&7]];
  694. if((i&7)==6){
  695. ((uint8_t*)dest)[0]= acc;
  696. ((uint8_t*)dest)++;
  697. }
  698. }
  699. }
  700. break;
  701. }
  702. }
  703. //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  704. //Plain C versions
  705. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  706. #define COMPILE_C
  707. #endif
  708. #ifdef CAN_COMPILE_X86_ASM
  709. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  710. #define COMPILE_MMX
  711. #endif
  712. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  713. #define COMPILE_MMX2
  714. #endif
  715. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  716. #define COMPILE_3DNOW
  717. #endif
  718. #endif //CAN_COMPILE_X86_ASM
  719. #undef HAVE_MMX
  720. #undef HAVE_MMX2
  721. #undef HAVE_3DNOW
  722. #ifdef COMPILE_C
  723. #undef HAVE_MMX
  724. #undef HAVE_MMX2
  725. #undef HAVE_3DNOW
  726. #define RENAME(a) a ## _C
  727. #include "swscale_template.c"
  728. #endif
  729. #ifdef CAN_COMPILE_X86_ASM
  730. //X86 versions
  731. /*
  732. #undef RENAME
  733. #undef HAVE_MMX
  734. #undef HAVE_MMX2
  735. #undef HAVE_3DNOW
  736. #define ARCH_X86
  737. #define RENAME(a) a ## _X86
  738. #include "swscale_template.c"
  739. */
  740. //MMX versions
  741. #ifdef COMPILE_MMX
  742. #undef RENAME
  743. #define HAVE_MMX
  744. #undef HAVE_MMX2
  745. #undef HAVE_3DNOW
  746. #define RENAME(a) a ## _MMX
  747. #include "swscale_template.c"
  748. #endif
  749. //MMX2 versions
  750. #ifdef COMPILE_MMX2
  751. #undef RENAME
  752. #define HAVE_MMX
  753. #define HAVE_MMX2
  754. #undef HAVE_3DNOW
  755. #define RENAME(a) a ## _MMX2
  756. #include "swscale_template.c"
  757. #endif
  758. //3DNOW versions
  759. #ifdef COMPILE_3DNOW
  760. #undef RENAME
  761. #define HAVE_MMX
  762. #undef HAVE_MMX2
  763. #define HAVE_3DNOW
  764. #define RENAME(a) a ## _3DNow
  765. #include "swscale_template.c"
  766. #endif
  767. #endif //CAN_COMPILE_X86_ASM
  768. // minor note: the HAVE_xyz is messed up after that line so dont use it
  769. // old global scaler, dont use for new code
  770. // will use sws_flags from the command line
  771. void SwScale_YV12slice(unsigned char* src[], int srcStride[], int srcSliceY ,
  772. int srcSliceH, uint8_t* dst[], int dstStride, int dstbpp,
  773. int srcW, int srcH, int dstW, int dstH){
  774. static SwsContext *context=NULL;
  775. int dstFormat;
  776. int dstStride3[3]= {dstStride, dstStride>>1, dstStride>>1};
  777. switch(dstbpp)
  778. {
  779. case 8 : dstFormat= IMGFMT_Y8; break;
  780. case 12: dstFormat= IMGFMT_YV12; break;
  781. case 15: dstFormat= IMGFMT_BGR15; break;
  782. case 16: dstFormat= IMGFMT_BGR16; break;
  783. case 24: dstFormat= IMGFMT_BGR24; break;
  784. case 32: dstFormat= IMGFMT_BGR32; break;
  785. default: return;
  786. }
  787. if(!context) context=getSwsContextFromCmdLine(srcW, srcH, IMGFMT_YV12, dstW, dstH, dstFormat);
  788. context->swScale(context, src, srcStride, srcSliceY, srcSliceH, dst, dstStride3);
  789. }
  790. void swsGetFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam)
  791. {
  792. static int firstTime=1;
  793. *flags=0;
  794. #ifdef ARCH_X86
  795. if(gCpuCaps.hasMMX)
  796. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  797. #endif
  798. if(firstTime)
  799. {
  800. firstTime=0;
  801. *flags= SWS_PRINT_INFO;
  802. }
  803. else if(verbose>1) *flags= SWS_PRINT_INFO;
  804. if(src_filter.lumH) freeVec(src_filter.lumH);
  805. if(src_filter.lumV) freeVec(src_filter.lumV);
  806. if(src_filter.chrH) freeVec(src_filter.chrH);
  807. if(src_filter.chrV) freeVec(src_filter.chrV);
  808. if(sws_lum_gblur!=0.0){
  809. src_filter.lumH= getGaussianVec(sws_lum_gblur, 3.0);
  810. src_filter.lumV= getGaussianVec(sws_lum_gblur, 3.0);
  811. }else{
  812. src_filter.lumH= getIdentityVec();
  813. src_filter.lumV= getIdentityVec();
  814. }
  815. if(sws_chr_gblur!=0.0){
  816. src_filter.chrH= getGaussianVec(sws_chr_gblur, 3.0);
  817. src_filter.chrV= getGaussianVec(sws_chr_gblur, 3.0);
  818. }else{
  819. src_filter.chrH= getIdentityVec();
  820. src_filter.chrV= getIdentityVec();
  821. }
  822. if(sws_chr_sharpen!=0.0){
  823. SwsVector *g= getConstVec(-1.0, 3);
  824. SwsVector *id= getConstVec(10.0/sws_chr_sharpen, 1);
  825. g->coeff[1]=2.0;
  826. addVec(id, g);
  827. convVec(src_filter.chrH, id);
  828. convVec(src_filter.chrV, id);
  829. freeVec(g);
  830. freeVec(id);
  831. }
  832. if(sws_lum_sharpen!=0.0){
  833. SwsVector *g= getConstVec(-1.0, 3);
  834. SwsVector *id= getConstVec(10.0/sws_lum_sharpen, 1);
  835. g->coeff[1]=2.0;
  836. addVec(id, g);
  837. convVec(src_filter.lumH, id);
  838. convVec(src_filter.lumV, id);
  839. freeVec(g);
  840. freeVec(id);
  841. }
  842. if(sws_chr_hshift)
  843. shiftVec(src_filter.chrH, sws_chr_hshift);
  844. if(sws_chr_vshift)
  845. shiftVec(src_filter.chrV, sws_chr_vshift);
  846. normalizeVec(src_filter.chrH, 1.0);
  847. normalizeVec(src_filter.chrV, 1.0);
  848. normalizeVec(src_filter.lumH, 1.0);
  849. normalizeVec(src_filter.lumV, 1.0);
  850. if(verbose > 1) printVec(src_filter.chrH);
  851. if(verbose > 1) printVec(src_filter.lumH);
  852. switch(sws_flags)
  853. {
  854. case 0: *flags|= SWS_FAST_BILINEAR; break;
  855. case 1: *flags|= SWS_BILINEAR; break;
  856. case 2: *flags|= SWS_BICUBIC; break;
  857. case 3: *flags|= SWS_X; break;
  858. case 4: *flags|= SWS_POINT; break;
  859. case 5: *flags|= SWS_AREA; break;
  860. case 6: *flags|= SWS_BICUBLIN; break;
  861. default:*flags|= SWS_BILINEAR; break;
  862. }
  863. *srcFilterParam= &src_filter;
  864. *dstFilterParam= NULL;
  865. }
  866. // will use sws_flags & src_filter (from cmd line)
  867. SwsContext *getSwsContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
  868. {
  869. int flags;
  870. SwsFilter *dstFilterParam, *srcFilterParam;
  871. swsGetFlagsAndFilterFromCmdLine(&flags, &srcFilterParam, &dstFilterParam);
  872. return getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, srcFilterParam, dstFilterParam);
  873. }
  874. static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  875. int srcW, int dstW, int filterAlign, int one, int flags,
  876. SwsVector *srcFilter, SwsVector *dstFilter)
  877. {
  878. int i;
  879. int filterSize;
  880. int filter2Size;
  881. int minFilterSize;
  882. double *filter=NULL;
  883. double *filter2=NULL;
  884. #ifdef ARCH_X86
  885. if(gCpuCaps.hasMMX)
  886. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  887. #endif
  888. // Note the +1 is for the MMXscaler which reads over the end
  889. *filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));
  890. if(ABS(xInc - 0x10000) <10) // unscaled
  891. {
  892. int i;
  893. filterSize= 1;
  894. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  895. for(i=0; i<dstW*filterSize; i++) filter[i]=0;
  896. for(i=0; i<dstW; i++)
  897. {
  898. filter[i*filterSize]=1;
  899. (*filterPos)[i]=i;
  900. }
  901. }
  902. else if(flags&SWS_POINT) // lame looking point sampling mode
  903. {
  904. int i;
  905. int xDstInSrc;
  906. filterSize= 1;
  907. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  908. xDstInSrc= xInc/2 - 0x8000;
  909. for(i=0; i<dstW; i++)
  910. {
  911. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  912. (*filterPos)[i]= xx;
  913. filter[i]= 1.0;
  914. xDstInSrc+= xInc;
  915. }
  916. }
  917. else if(xInc <= (1<<16) || (flags&SWS_FAST_BILINEAR)) // upscale
  918. {
  919. int i;
  920. int xDstInSrc;
  921. if (flags&SWS_BICUBIC) filterSize= 4;
  922. else if(flags&SWS_X ) filterSize= 4;
  923. else filterSize= 2; // SWS_BILINEAR / SWS_AREA
  924. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  925. xDstInSrc= xInc/2 - 0x8000;
  926. for(i=0; i<dstW; i++)
  927. {
  928. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  929. int j;
  930. (*filterPos)[i]= xx;
  931. if((flags & SWS_BICUBIC) || (flags & SWS_X))
  932. {
  933. double d= ABS(((xx+1)<<16) - xDstInSrc)/(double)(1<<16);
  934. double y1,y2,y3,y4;
  935. double A= -0.6;
  936. if(flags & SWS_BICUBIC){
  937. // Equation is from VirtualDub
  938. y1 = ( + A*d - 2.0*A*d*d + A*d*d*d);
  939. y2 = (+ 1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  940. y3 = ( - A*d + (2.0*A+3.0)*d*d - (A+2.0)*d*d*d);
  941. y4 = ( + A*d*d - A*d*d*d);
  942. }else{
  943. // cubic interpolation (derived it myself)
  944. y1 = ( -2.0*d + 3.0*d*d - 1.0*d*d*d)/6.0;
  945. y2 = (6.0 -3.0*d - 6.0*d*d + 3.0*d*d*d)/6.0;
  946. y3 = ( +6.0*d + 3.0*d*d - 3.0*d*d*d)/6.0;
  947. y4 = ( -1.0*d + 1.0*d*d*d)/6.0;
  948. }
  949. filter[i*filterSize + 0]= y1;
  950. filter[i*filterSize + 1]= y2;
  951. filter[i*filterSize + 2]= y3;
  952. filter[i*filterSize + 3]= y4;
  953. }
  954. else
  955. {
  956. //Bilinear upscale / linear interpolate / Area averaging
  957. for(j=0; j<filterSize; j++)
  958. {
  959. double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
  960. double coeff= 1.0 - d;
  961. if(coeff<0) coeff=0;
  962. filter[i*filterSize + j]= coeff;
  963. xx++;
  964. }
  965. }
  966. xDstInSrc+= xInc;
  967. }
  968. }
  969. else // downscale
  970. {
  971. int xDstInSrc;
  972. ASSERT(dstW <= srcW)
  973. if(flags&SWS_BICUBIC) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
  974. else if(flags&SWS_X) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
  975. else if(flags&SWS_AREA) filterSize= (int)ceil(1 + 1.0*srcW / (double)dstW);
  976. else /* BILINEAR */ filterSize= (int)ceil(1 + 2.0*srcW / (double)dstW);
  977. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  978. xDstInSrc= xInc/2 - 0x8000;
  979. for(i=0; i<dstW; i++)
  980. {
  981. int xx= (int)((double)xDstInSrc/(double)(1<<16) - (filterSize-1)*0.5 + 0.5);
  982. int j;
  983. (*filterPos)[i]= xx;
  984. for(j=0; j<filterSize; j++)
  985. {
  986. double d= ABS((xx<<16) - xDstInSrc)/(double)xInc;
  987. double coeff;
  988. if((flags & SWS_BICUBIC) || (flags & SWS_X))
  989. {
  990. double A= -0.75;
  991. // d*=2;
  992. // Equation is from VirtualDub
  993. if(d<1.0)
  994. coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  995. else if(d<2.0)
  996. coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
  997. else
  998. coeff=0.0;
  999. }
  1000. else if(flags & SWS_AREA)
  1001. {
  1002. double srcPixelSize= (1<<16)/(double)xInc;
  1003. if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
  1004. else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
  1005. else coeff=0.0;
  1006. }
  1007. else
  1008. {
  1009. coeff= 1.0 - d;
  1010. if(coeff<0) coeff=0;
  1011. }
  1012. filter[i*filterSize + j]= coeff;
  1013. xx++;
  1014. }
  1015. xDstInSrc+= xInc;
  1016. }
  1017. }
  1018. /* apply src & dst Filter to filter -> filter2
  1019. free(filter);
  1020. */
  1021. ASSERT(filterSize>0)
  1022. filter2Size= filterSize;
  1023. if(srcFilter) filter2Size+= srcFilter->length - 1;
  1024. if(dstFilter) filter2Size+= dstFilter->length - 1;
  1025. ASSERT(filter2Size>0)
  1026. filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
  1027. for(i=0; i<dstW; i++)
  1028. {
  1029. int j;
  1030. SwsVector scaleFilter;
  1031. SwsVector *outVec;
  1032. scaleFilter.coeff= filter + i*filterSize;
  1033. scaleFilter.length= filterSize;
  1034. if(srcFilter) outVec= getConvVec(srcFilter, &scaleFilter);
  1035. else outVec= &scaleFilter;
  1036. ASSERT(outVec->length == filter2Size)
  1037. //FIXME dstFilter
  1038. for(j=0; j<outVec->length; j++)
  1039. {
  1040. filter2[i*filter2Size + j]= outVec->coeff[j];
  1041. }
  1042. (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
  1043. if(outVec != &scaleFilter) freeVec(outVec);
  1044. }
  1045. free(filter); filter=NULL;
  1046. /* try to reduce the filter-size (step1 find size and shift left) */
  1047. // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
  1048. minFilterSize= 0;
  1049. for(i=dstW-1; i>=0; i--)
  1050. {
  1051. int min= filter2Size;
  1052. int j;
  1053. double cutOff=0.0;
  1054. /* get rid off near zero elements on the left by shifting left */
  1055. for(j=0; j<filter2Size; j++)
  1056. {
  1057. int k;
  1058. cutOff += ABS(filter2[i*filter2Size]);
  1059. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1060. /* preserve Monotonicity because the core cant handle the filter otherwise */
  1061. if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
  1062. // Move filter coeffs left
  1063. for(k=1; k<filter2Size; k++)
  1064. filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
  1065. filter2[i*filter2Size + k - 1]= 0.0;
  1066. (*filterPos)[i]++;
  1067. }
  1068. cutOff=0.0;
  1069. /* count near zeros on the right */
  1070. for(j=filter2Size-1; j>0; j--)
  1071. {
  1072. cutOff += ABS(filter2[i*filter2Size + j]);
  1073. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1074. min--;
  1075. }
  1076. if(min>minFilterSize) minFilterSize= min;
  1077. }
  1078. ASSERT(minFilterSize > 0)
  1079. filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
  1080. ASSERT(filterSize > 0)
  1081. filter= (double*)memalign(8, filterSize*dstW*sizeof(double));
  1082. *outFilterSize= filterSize;
  1083. if(flags&SWS_PRINT_INFO)
  1084. MSG_INFO("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
  1085. /* try to reduce the filter-size (step2 reduce it) */
  1086. for(i=0; i<dstW; i++)
  1087. {
  1088. int j;
  1089. for(j=0; j<filterSize; j++)
  1090. {
  1091. if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
  1092. else filter[i*filterSize + j]= filter2[i*filter2Size + j];
  1093. }
  1094. }
  1095. free(filter2); filter2=NULL;
  1096. //FIXME try to align filterpos if possible
  1097. //fix borders
  1098. for(i=0; i<dstW; i++)
  1099. {
  1100. int j;
  1101. if((*filterPos)[i] < 0)
  1102. {
  1103. // Move filter coeffs left to compensate for filterPos
  1104. for(j=1; j<filterSize; j++)
  1105. {
  1106. int left= MAX(j + (*filterPos)[i], 0);
  1107. filter[i*filterSize + left] += filter[i*filterSize + j];
  1108. filter[i*filterSize + j]=0;
  1109. }
  1110. (*filterPos)[i]= 0;
  1111. }
  1112. if((*filterPos)[i] + filterSize > srcW)
  1113. {
  1114. int shift= (*filterPos)[i] + filterSize - srcW;
  1115. // Move filter coeffs right to compensate for filterPos
  1116. for(j=filterSize-2; j>=0; j--)
  1117. {
  1118. int right= MIN(j + shift, filterSize-1);
  1119. filter[i*filterSize +right] += filter[i*filterSize +j];
  1120. filter[i*filterSize +j]=0;
  1121. }
  1122. (*filterPos)[i]= srcW - filterSize;
  1123. }
  1124. }
  1125. // Note the +1 is for the MMXscaler which reads over the end
  1126. *outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));
  1127. memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
  1128. /* Normalize & Store in outFilter */
  1129. for(i=0; i<dstW; i++)
  1130. {
  1131. int j;
  1132. double sum=0;
  1133. double scale= one;
  1134. for(j=0; j<filterSize; j++)
  1135. {
  1136. sum+= filter[i*filterSize + j];
  1137. }
  1138. scale/= sum;
  1139. for(j=0; j<filterSize; j++)
  1140. {
  1141. (*outFilter)[i*(*outFilterSize) + j]= (int)(filter[i*filterSize + j]*scale);
  1142. }
  1143. }
  1144. (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
  1145. for(i=0; i<*outFilterSize; i++)
  1146. {
  1147. int j= dstW*(*outFilterSize);
  1148. (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
  1149. }
  1150. free(filter);
  1151. }
  1152. #ifdef ARCH_X86
  1153. static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
  1154. {
  1155. uint8_t *fragmentA;
  1156. int imm8OfPShufW1A;
  1157. int imm8OfPShufW2A;
  1158. int fragmentLengthA;
  1159. uint8_t *fragmentB;
  1160. int imm8OfPShufW1B;
  1161. int imm8OfPShufW2B;
  1162. int fragmentLengthB;
  1163. int fragmentPos;
  1164. int xpos, i;
  1165. // create an optimized horizontal scaling routine
  1166. //code fragment
  1167. asm volatile(
  1168. "jmp 9f \n\t"
  1169. // Begin
  1170. "0: \n\t"
  1171. "movq (%%edx, %%eax), %%mm3 \n\t"
  1172. "movd (%%ecx, %%esi), %%mm0 \n\t"
  1173. "movd 1(%%ecx, %%esi), %%mm1 \n\t"
  1174. "punpcklbw %%mm7, %%mm1 \n\t"
  1175. "punpcklbw %%mm7, %%mm0 \n\t"
  1176. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  1177. "1: \n\t"
  1178. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1179. "2: \n\t"
  1180. "psubw %%mm1, %%mm0 \n\t"
  1181. "movl 8(%%ebx, %%eax), %%esi \n\t"
  1182. "pmullw %%mm3, %%mm0 \n\t"
  1183. "psllw $7, %%mm1 \n\t"
  1184. "paddw %%mm1, %%mm0 \n\t"
  1185. "movq %%mm0, (%%edi, %%eax) \n\t"
  1186. "addl $8, %%eax \n\t"
  1187. // End
  1188. "9: \n\t"
  1189. // "int $3\n\t"
  1190. "leal 0b, %0 \n\t"
  1191. "leal 1b, %1 \n\t"
  1192. "leal 2b, %2 \n\t"
  1193. "decl %1 \n\t"
  1194. "decl %2 \n\t"
  1195. "subl %0, %1 \n\t"
  1196. "subl %0, %2 \n\t"
  1197. "leal 9b, %3 \n\t"
  1198. "subl %0, %3 \n\t"
  1199. :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  1200. "=r" (fragmentLengthA)
  1201. );
  1202. asm volatile(
  1203. "jmp 9f \n\t"
  1204. // Begin
  1205. "0: \n\t"
  1206. "movq (%%edx, %%eax), %%mm3 \n\t"
  1207. "movd (%%ecx, %%esi), %%mm0 \n\t"
  1208. "punpcklbw %%mm7, %%mm0 \n\t"
  1209. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  1210. "1: \n\t"
  1211. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1212. "2: \n\t"
  1213. "psubw %%mm1, %%mm0 \n\t"
  1214. "movl 8(%%ebx, %%eax), %%esi \n\t"
  1215. "pmullw %%mm3, %%mm0 \n\t"
  1216. "psllw $7, %%mm1 \n\t"
  1217. "paddw %%mm1, %%mm0 \n\t"
  1218. "movq %%mm0, (%%edi, %%eax) \n\t"
  1219. "addl $8, %%eax \n\t"
  1220. // End
  1221. "9: \n\t"
  1222. // "int $3\n\t"
  1223. "leal 0b, %0 \n\t"
  1224. "leal 1b, %1 \n\t"
  1225. "leal 2b, %2 \n\t"
  1226. "decl %1 \n\t"
  1227. "decl %2 \n\t"
  1228. "subl %0, %1 \n\t"
  1229. "subl %0, %2 \n\t"
  1230. "leal 9b, %3 \n\t"
  1231. "subl %0, %3 \n\t"
  1232. :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  1233. "=r" (fragmentLengthB)
  1234. );
  1235. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  1236. fragmentPos=0;
  1237. for(i=0; i<dstW/numSplits; i++)
  1238. {
  1239. int xx=xpos>>16;
  1240. if((i&3) == 0)
  1241. {
  1242. int a=0;
  1243. int b=((xpos+xInc)>>16) - xx;
  1244. int c=((xpos+xInc*2)>>16) - xx;
  1245. int d=((xpos+xInc*3)>>16) - xx;
  1246. filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
  1247. filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
  1248. filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
  1249. filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
  1250. filterPos[i/2]= xx;
  1251. if(d+1<4)
  1252. {
  1253. int maxShift= 3-(d+1);
  1254. int shift=0;
  1255. memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
  1256. funnyCode[fragmentPos + imm8OfPShufW1B]=
  1257. (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
  1258. funnyCode[fragmentPos + imm8OfPShufW2B]=
  1259. a | (b<<2) | (c<<4) | (d<<6);
  1260. if(i+3>=dstW) shift=maxShift; //avoid overread
  1261. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
  1262. if(shift && i>=shift)
  1263. {
  1264. funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
  1265. funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
  1266. filterPos[i/2]-=shift;
  1267. }
  1268. fragmentPos+= fragmentLengthB;
  1269. }
  1270. else
  1271. {
  1272. int maxShift= 3-d;
  1273. int shift=0;
  1274. memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
  1275. funnyCode[fragmentPos + imm8OfPShufW1A]=
  1276. funnyCode[fragmentPos + imm8OfPShufW2A]=
  1277. a | (b<<2) | (c<<4) | (d<<6);
  1278. if(i+4>=dstW) shift=maxShift; //avoid overread
  1279. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
  1280. if(shift && i>=shift)
  1281. {
  1282. funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
  1283. funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
  1284. filterPos[i/2]-=shift;
  1285. }
  1286. fragmentPos+= fragmentLengthA;
  1287. }
  1288. funnyCode[fragmentPos]= RET;
  1289. }
  1290. xpos+=xInc;
  1291. }
  1292. filterPos[i/2]= xpos>>16; // needed to jump to the next part
  1293. }
  1294. #endif // ARCH_X86
  1295. //FIXME remove
  1296. void SwScale_Init(){
  1297. }
  1298. static void globalInit(){
  1299. // generating tables:
  1300. int i;
  1301. for(i=0; i<768; i++){
  1302. int c= MIN(MAX(i-256, 0), 255);
  1303. clip_table[i]=c;
  1304. }
  1305. cpuCaps= gCpuCaps;
  1306. #ifdef RUNTIME_CPUDETECT
  1307. #ifdef CAN_COMPILE_X86_ASM
  1308. // ordered per speed fasterst first
  1309. if(gCpuCaps.hasMMX2)
  1310. swScale= swScale_MMX2;
  1311. else if(gCpuCaps.has3DNow)
  1312. swScale= swScale_3DNow;
  1313. else if(gCpuCaps.hasMMX)
  1314. swScale= swScale_MMX;
  1315. else
  1316. swScale= swScale_C;
  1317. #else
  1318. swScale= swScale_C;
  1319. cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
  1320. #endif
  1321. #else //RUNTIME_CPUDETECT
  1322. #ifdef HAVE_MMX2
  1323. swScale= swScale_MMX2;
  1324. cpuCaps.has3DNow = 0;
  1325. #elif defined (HAVE_3DNOW)
  1326. swScale= swScale_3DNow;
  1327. cpuCaps.hasMMX2 = 0;
  1328. #elif defined (HAVE_MMX)
  1329. swScale= swScale_MMX;
  1330. cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
  1331. #else
  1332. swScale= swScale_C;
  1333. cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
  1334. #endif
  1335. #endif //!RUNTIME_CPUDETECT
  1336. }
  1337. static void PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1338. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1339. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1340. /* Copy Y plane */
  1341. if(dstStride[0]==srcStride[0])
  1342. memcpy(dst, src[0], srcSliceH*dstStride[0]);
  1343. else
  1344. {
  1345. int i;
  1346. uint8_t *srcPtr= src[0];
  1347. uint8_t *dstPtr= dst;
  1348. for(i=0; i<srcSliceH; i++)
  1349. {
  1350. memcpy(dstPtr, srcPtr, srcStride[0]);
  1351. srcPtr+= srcStride[0];
  1352. dstPtr+= dstStride[0];
  1353. }
  1354. }
  1355. dst = dstParam[1] + dstStride[1]*srcSliceY;
  1356. if(c->srcFormat==IMGFMT_YV12)
  1357. interleaveBytes( src[1],src[2],dst,c->srcW,srcSliceH,srcStride[1],srcStride[2],dstStride[0] );
  1358. else /* I420 & IYUV */
  1359. interleaveBytes( src[2],src[1],dst,c->srcW,srcSliceH,srcStride[2],srcStride[1],dstStride[0] );
  1360. }
  1361. /* Warper functions for yuv2bgr */
  1362. static void planarYuvToBgr(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1363. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1364. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1365. if(c->srcFormat==IMGFMT_YV12)
  1366. yuv2rgb( dst,src[0],src[1],src[2],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
  1367. else /* I420 & IYUV */
  1368. yuv2rgb( dst,src[0],src[2],src[1],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
  1369. }
  1370. static void PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1371. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1372. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1373. if(c->srcFormat==IMGFMT_YV12)
  1374. yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1375. else /* I420 & IYUV */
  1376. yv12toyuy2( src[0],src[2],src[1],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1377. }
  1378. /* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
  1379. static void rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1380. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1381. const int srcFormat= c->srcFormat;
  1382. const int dstFormat= c->dstFormat;
  1383. const int srcBpp= ((srcFormat&0xFF) + 7)>>3;
  1384. const int dstBpp= ((dstFormat&0xFF) + 7)>>3;
  1385. const int srcId= (srcFormat&0xFF)>>2; // 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8
  1386. const int dstId= (dstFormat&0xFF)>>2;
  1387. void (*conv)(const uint8_t *src, uint8_t *dst, unsigned src_size)=NULL;
  1388. /* BGR -> BGR */
  1389. if(isBGR(srcFormat) && isBGR(dstFormat)){
  1390. switch(srcId | (dstId<<4)){
  1391. case 0x34: conv= rgb16to15; break;
  1392. case 0x36: conv= rgb24to15; break;
  1393. case 0x38: conv= rgb32to15; break;
  1394. case 0x43: conv= rgb15to16; break;
  1395. case 0x46: conv= rgb24to16; break;
  1396. case 0x48: conv= rgb32to16; break;
  1397. case 0x63: conv= rgb15to24; break;
  1398. case 0x64: conv= rgb16to24; break;
  1399. case 0x68: conv= rgb32to24; break;
  1400. case 0x83: conv= rgb15to32; break;
  1401. case 0x84: conv= rgb16to32; break;
  1402. case 0x86: conv= rgb24to32; break;
  1403. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1404. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1405. }
  1406. }else if(isBGR(srcFormat) && isRGB(dstFormat)){
  1407. switch(srcId | (dstId<<4)){
  1408. case 0x33: conv= rgb15tobgr15; break;
  1409. case 0x34: conv= rgb16tobgr15; break;
  1410. case 0x36: conv= rgb24tobgr15; break;
  1411. case 0x38: conv= rgb32tobgr15; break;
  1412. case 0x43: conv= rgb15tobgr16; break;
  1413. case 0x44: conv= rgb16tobgr16; break;
  1414. case 0x46: conv= rgb24tobgr16; break;
  1415. case 0x48: conv= rgb32tobgr16; break;
  1416. case 0x63: conv= rgb15tobgr24; break;
  1417. case 0x64: conv= rgb16tobgr24; break;
  1418. case 0x66: conv= rgb24tobgr24; break;
  1419. case 0x68: conv= rgb32tobgr24; break;
  1420. case 0x83: conv= rgb15tobgr32; break;
  1421. case 0x84: conv= rgb16tobgr32; break;
  1422. case 0x86: conv= rgb24tobgr32; break;
  1423. case 0x88: conv= rgb32tobgr32; break;
  1424. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1425. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1426. }
  1427. }else if(isRGB(srcFormat) && isRGB(dstFormat)){
  1428. switch(srcId | (dstId<<4)){
  1429. case 0x34: conv= rgb16to15; break;
  1430. case 0x36: conv= rgb24to15; break;
  1431. case 0x38: conv= rgb32to15; break;
  1432. case 0x43: conv= rgb15to16; break;
  1433. case 0x46: conv= rgb24to16; break;
  1434. case 0x48: conv= rgb32to16; break;
  1435. case 0x63: conv= rgb15to24; break;
  1436. case 0x64: conv= rgb16to24; break;
  1437. case 0x68: conv= rgb32to24; break;
  1438. case 0x83: conv= rgb15to32; break;
  1439. case 0x84: conv= rgb16to32; break;
  1440. case 0x86: conv= rgb24to32; break;
  1441. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1442. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1443. }
  1444. }else if(isRGB(srcFormat) && isBGR(dstFormat)){
  1445. switch(srcId | (dstId<<4)){
  1446. case 0x33: conv= rgb15tobgr15; break;
  1447. case 0x34: conv= rgb16tobgr15; break;
  1448. case 0x36: conv= rgb24tobgr15; break;
  1449. case 0x38: conv= rgb32tobgr15; break;
  1450. case 0x43: conv= rgb15tobgr16; break;
  1451. case 0x44: conv= rgb16tobgr16; break;
  1452. case 0x46: conv= rgb24tobgr16; break;
  1453. case 0x48: conv= rgb32tobgr16; break;
  1454. case 0x63: conv= rgb15tobgr24; break;
  1455. case 0x64: conv= rgb16tobgr24; break;
  1456. case 0x66: conv= rgb24tobgr24; break;
  1457. case 0x68: conv= rgb32tobgr24; break;
  1458. case 0x83: conv= rgb15tobgr32; break;
  1459. case 0x84: conv= rgb16tobgr32; break;
  1460. case 0x86: conv= rgb24tobgr32; break;
  1461. case 0x88: conv= rgb32tobgr32; break;
  1462. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1463. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1464. }
  1465. }
  1466. if(dstStride[0]*srcBpp == srcStride[0]*dstBpp)
  1467. conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1468. else
  1469. {
  1470. int i;
  1471. uint8_t *srcPtr= src[0];
  1472. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1473. for(i=0; i<srcSliceH; i++)
  1474. {
  1475. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1476. srcPtr+= srcStride[0];
  1477. dstPtr+= dstStride[0];
  1478. }
  1479. }
  1480. }
  1481. static void bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1482. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1483. rgb24toyv12(
  1484. src[0],
  1485. dst[0]+ srcSliceY *dstStride[0],
  1486. dst[1]+(srcSliceY>>1)*dstStride[1],
  1487. dst[2]+(srcSliceY>>1)*dstStride[2],
  1488. c->srcW, srcSliceH,
  1489. dstStride[0], dstStride[1], srcStride[0]);
  1490. }
  1491. static void yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1492. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1493. int i;
  1494. /* copy Y */
  1495. if(srcStride[0]==dstStride[0])
  1496. memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
  1497. else{
  1498. uint8_t *srcPtr= src[0];
  1499. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1500. for(i=0; i<srcSliceH; i++)
  1501. {
  1502. memcpy(dstPtr, srcPtr, c->srcW);
  1503. srcPtr+= srcStride[0];
  1504. dstPtr+= dstStride[0];
  1505. }
  1506. }
  1507. if(c->dstFormat==IMGFMT_YV12){
  1508. planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
  1509. planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
  1510. }else{
  1511. planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
  1512. planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
  1513. }
  1514. }
  1515. /**
  1516. * bring pointers in YUV order instead of YVU
  1517. */
  1518. static inline void orderYUV(int format, uint8_t * sortedP[], int sortedStride[], uint8_t * p[], int stride[]){
  1519. if(format == IMGFMT_YV12 || format == IMGFMT_YVU9){
  1520. sortedP[0]= p[0];
  1521. sortedP[1]= p[1];
  1522. sortedP[2]= p[2];
  1523. sortedStride[0]= stride[0];
  1524. sortedStride[1]= stride[1];
  1525. sortedStride[2]= stride[2];
  1526. }
  1527. else if(isPacked(format) || isGray(format))
  1528. {
  1529. sortedP[0]= p[0];
  1530. sortedP[1]=
  1531. sortedP[2]= NULL;
  1532. sortedStride[0]= stride[0];
  1533. sortedStride[1]=
  1534. sortedStride[2]= 0;
  1535. }
  1536. else if(format == IMGFMT_I420)
  1537. {
  1538. sortedP[0]= p[0];
  1539. sortedP[1]= p[2];
  1540. sortedP[2]= p[1];
  1541. sortedStride[0]= stride[0];
  1542. sortedStride[1]= stride[2];
  1543. sortedStride[2]= stride[1];
  1544. }else{
  1545. MSG_ERR("internal error in orderYUV\n");
  1546. }
  1547. }
  1548. /* unscaled copy like stuff (assumes nearly identical formats) */
  1549. static void simpleCopy(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
  1550. int srcSliceH, uint8_t* dstParam[], int dstStrideParam[]){
  1551. int srcStride[3];
  1552. int dstStride[3];
  1553. uint8_t *src[3];
  1554. uint8_t *dst[3];
  1555. orderYUV(c->srcFormat, src, srcStride, srcParam, srcStrideParam);
  1556. orderYUV(c->dstFormat, dst, dstStride, dstParam, dstStrideParam);
  1557. if(isPacked(c->srcFormat))
  1558. {
  1559. if(dstStride[0]==srcStride[0])
  1560. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1561. else
  1562. {
  1563. int i;
  1564. uint8_t *srcPtr= src[0];
  1565. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1566. int length=0;
  1567. /* universal length finder */
  1568. while(length+c->srcW <= ABS(dstStride[0])
  1569. && length+c->srcW <= ABS(srcStride[0])) length+= c->srcW;
  1570. ASSERT(length!=0);
  1571. for(i=0; i<srcSliceH; i++)
  1572. {
  1573. memcpy(dstPtr, srcPtr, length);
  1574. srcPtr+= srcStride[0];
  1575. dstPtr+= dstStride[0];
  1576. }
  1577. }
  1578. }
  1579. else
  1580. { /* Planar YUV or gray */
  1581. int plane;
  1582. for(plane=0; plane<3; plane++)
  1583. {
  1584. int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1585. int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1586. int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1587. if((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
  1588. {
  1589. if(!isGray(c->dstFormat))
  1590. memset(dst[plane], 128, dstStride[plane]*height);
  1591. }
  1592. else
  1593. {
  1594. if(dstStride[plane]==srcStride[plane])
  1595. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1596. else
  1597. {
  1598. int i;
  1599. uint8_t *srcPtr= src[plane];
  1600. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1601. for(i=0; i<height; i++)
  1602. {
  1603. memcpy(dstPtr, srcPtr, length);
  1604. srcPtr+= srcStride[plane];
  1605. dstPtr+= dstStride[plane];
  1606. }
  1607. }
  1608. }
  1609. }
  1610. }
  1611. }
  1612. static int remove_dup_fourcc(int fourcc)
  1613. {
  1614. switch(fourcc)
  1615. {
  1616. case IMGFMT_IYUV: return IMGFMT_I420;
  1617. case IMGFMT_Y8 : return IMGFMT_Y800;
  1618. case IMGFMT_IF09: return IMGFMT_YVU9;
  1619. default: return fourcc;
  1620. }
  1621. }
  1622. static void getSubSampleFactors(int *h, int *v, int format){
  1623. switch(format){
  1624. case IMGFMT_YUY2:
  1625. *h=1;
  1626. *v=0;
  1627. break;
  1628. case IMGFMT_YV12:
  1629. case IMGFMT_I420:
  1630. case IMGFMT_Y800: //FIXME remove after different subsamplings are fully implemented
  1631. *h=1;
  1632. *v=1;
  1633. break;
  1634. case IMGFMT_YVU9:
  1635. *h=2;
  1636. *v=2;
  1637. break;
  1638. default:
  1639. *h=0;
  1640. *v=0;
  1641. break;
  1642. }
  1643. }
  1644. SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
  1645. SwsFilter *srcFilter, SwsFilter *dstFilter){
  1646. SwsContext *c;
  1647. int i;
  1648. int usesFilter;
  1649. int unscaled, needsDither;
  1650. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  1651. #ifdef ARCH_X86
  1652. if(gCpuCaps.hasMMX)
  1653. asm volatile("emms\n\t"::: "memory");
  1654. #endif
  1655. if(swScale==NULL) globalInit();
  1656. //srcFormat= IMGFMT_Y800;
  1657. //dstFormat= IMGFMT_Y800;
  1658. /* avoid dupplicate Formats, so we dont need to check to much */
  1659. srcFormat = remove_dup_fourcc(srcFormat);
  1660. dstFormat = remove_dup_fourcc(dstFormat);
  1661. unscaled = (srcW == dstW && srcH == dstH);
  1662. needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
  1663. && (dstFormat&0xFF)<24
  1664. && ((dstFormat&0xFF)<(srcFormat&0xFF) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
  1665. if(!isSupportedIn(srcFormat))
  1666. {
  1667. MSG_ERR("swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
  1668. return NULL;
  1669. }
  1670. if(!isSupportedOut(dstFormat))
  1671. {
  1672. MSG_ERR("swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
  1673. return NULL;
  1674. }
  1675. /* sanity check */
  1676. 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
  1677. {
  1678. MSG_ERR("swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  1679. srcW, srcH, dstW, dstH);
  1680. return NULL;
  1681. }
  1682. if(!dstFilter) dstFilter= &dummyFilter;
  1683. if(!srcFilter) srcFilter= &dummyFilter;
  1684. c= memalign(64, sizeof(SwsContext));
  1685. memset(c, 0, sizeof(SwsContext));
  1686. c->srcW= srcW;
  1687. c->srcH= srcH;
  1688. c->dstW= dstW;
  1689. c->dstH= dstH;
  1690. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  1691. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  1692. c->flags= flags;
  1693. c->dstFormat= dstFormat;
  1694. c->srcFormat= srcFormat;
  1695. usesFilter=0;
  1696. if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesFilter=1;
  1697. if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesFilter=1;
  1698. if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesFilter=1;
  1699. if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesFilter=1;
  1700. if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesFilter=1;
  1701. if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesFilter=1;
  1702. if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesFilter=1;
  1703. if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesFilter=1;
  1704. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  1705. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  1706. // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
  1707. if((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  1708. // drop some chroma lines if the user wants it
  1709. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  1710. c->chrSrcVSubSample+= c->vChrDrop;
  1711. // drop every 2. pixel for chroma calculation unless user wants full chroma
  1712. if((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP))
  1713. c->chrSrcHSubSample=1;
  1714. c->chrIntHSubSample= c->chrDstHSubSample;
  1715. c->chrIntVSubSample= c->chrSrcVSubSample;
  1716. // note the -((-x)>>y) is so that we allways round toward +inf
  1717. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  1718. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  1719. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  1720. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  1721. if(isBGR(dstFormat))
  1722. c->yuvTable= yuv2rgb_c_init(dstFormat & 0xFF, MODE_RGB, c->table_rV, c->table_gU, c->table_gV, c->table_bU);
  1723. if(isRGB(dstFormat))
  1724. c->yuvTable= yuv2rgb_c_init(dstFormat & 0xFF, MODE_BGR, c->table_rV, c->table_gU, c->table_gV, c->table_bU);
  1725. /* unscaled special Cases */
  1726. if(unscaled && !usesFilter)
  1727. {
  1728. /* yv12_to_nv12 */
  1729. if((srcFormat == IMGFMT_YV12||srcFormat==IMGFMT_I420)&&dstFormat == IMGFMT_NV12)
  1730. {
  1731. c->swScale= PlanarToNV12Wrapper;
  1732. if(flags&SWS_PRINT_INFO)
  1733. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1734. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1735. return c;
  1736. }
  1737. /* yv12_to_yuy2 */
  1738. if((srcFormat == IMGFMT_YV12||srcFormat==IMGFMT_I420)&&dstFormat == IMGFMT_YUY2)
  1739. {
  1740. c->swScale= PlanarToYuy2Wrapper;
  1741. if(flags&SWS_PRINT_INFO)
  1742. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1743. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1744. return c;
  1745. }
  1746. /* yuv2bgr */
  1747. if((srcFormat==IMGFMT_YV12 || srcFormat==IMGFMT_I420) && isBGR(dstFormat))
  1748. {
  1749. // FIXME multiple yuv2rgb converters wont work that way cuz that thing is full of globals&statics
  1750. //FIXME rgb vs. bgr ?
  1751. #ifdef WORDS_BIGENDIAN
  1752. if(dstFormat==IMGFMT_BGR32)
  1753. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_BGR);
  1754. else
  1755. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
  1756. #else
  1757. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
  1758. #endif
  1759. c->swScale= planarYuvToBgr;
  1760. if(flags&SWS_PRINT_INFO)
  1761. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1762. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1763. return c;
  1764. }
  1765. /* simple copy */
  1766. if( srcFormat == dstFormat
  1767. || (srcFormat==IMGFMT_YV12 && dstFormat==IMGFMT_I420)
  1768. || (srcFormat==IMGFMT_I420 && dstFormat==IMGFMT_YV12)
  1769. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1770. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1771. )
  1772. {
  1773. c->swScale= simpleCopy;
  1774. if(flags&SWS_PRINT_INFO)
  1775. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1776. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1777. return c;
  1778. }
  1779. if( srcFormat==IMGFMT_YVU9 && (dstFormat==IMGFMT_YV12 || dstFormat==IMGFMT_I420) )
  1780. {
  1781. c->swScale= yvu9toyv12Wrapper;
  1782. if(flags&SWS_PRINT_INFO)
  1783. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1784. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1785. return c;
  1786. }
  1787. /* bgr24toYV12 */
  1788. if(srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_YV12)
  1789. c->swScale= bgr24toyv12Wrapper;
  1790. /* rgb/bgr -> rgb/bgr (no dither needed forms) */
  1791. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1792. && (isBGR(dstFormat) || isRGB(dstFormat))
  1793. && !needsDither)
  1794. c->swScale= rgb2rgbWrapper;
  1795. /* LQ converters if -sws 0 or -sws 4*/
  1796. if(c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
  1797. /* rgb/bgr -> rgb/bgr (dither needed forms) */
  1798. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1799. && (isBGR(dstFormat) || isRGB(dstFormat))
  1800. && needsDither)
  1801. c->swScale= rgb2rgbWrapper;
  1802. }
  1803. if(c->swScale){
  1804. if(flags&SWS_PRINT_INFO)
  1805. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1806. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1807. return c;
  1808. }
  1809. }
  1810. if(cpuCaps.hasMMX2)
  1811. {
  1812. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  1813. if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
  1814. {
  1815. if(flags&SWS_PRINT_INFO)
  1816. MSG_INFO("SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
  1817. }
  1818. }
  1819. else
  1820. c->canMMX2BeUsed=0;
  1821. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  1822. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  1823. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  1824. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  1825. // n-2 is the last chrominance sample available
  1826. // this is not perfect, but noone shuld notice the difference, the more correct variant
  1827. // would be like the vertical one, but that would require some special code for the
  1828. // first and last pixel
  1829. if(flags&SWS_FAST_BILINEAR)
  1830. {
  1831. if(c->canMMX2BeUsed)
  1832. {
  1833. c->lumXInc+= 20;
  1834. c->chrXInc+= 20;
  1835. }
  1836. //we dont use the x86asm scaler if mmx is available
  1837. else if(cpuCaps.hasMMX)
  1838. {
  1839. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  1840. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  1841. }
  1842. }
  1843. /* precalculate horizontal scaler filter coefficients */
  1844. {
  1845. const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
  1846. initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  1847. srcW , dstW, filterAlign, 1<<14,
  1848. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1849. srcFilter->lumH, dstFilter->lumH);
  1850. initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  1851. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  1852. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1853. srcFilter->chrH, dstFilter->chrH);
  1854. #ifdef ARCH_X86
  1855. // cant downscale !!!
  1856. if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
  1857. {
  1858. c->lumMmx2Filter = (int16_t*)memalign(8, (dstW /8+8)*sizeof(int16_t));
  1859. c->chrMmx2Filter = (int16_t*)memalign(8, (c->chrDstW /4+8)*sizeof(int16_t));
  1860. c->lumMmx2FilterPos= (int32_t*)memalign(8, (dstW /2/8+8)*sizeof(int32_t));
  1861. c->chrMmx2FilterPos= (int32_t*)memalign(8, (c->chrDstW/2/4+8)*sizeof(int32_t));
  1862. initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
  1863. initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
  1864. }
  1865. #endif
  1866. } // Init Horizontal stuff
  1867. /* precalculate vertical scaler filter coefficients */
  1868. initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  1869. srcH , dstH, 1, (1<<12)-4,
  1870. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1871. srcFilter->lumV, dstFilter->lumV);
  1872. initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  1873. c->chrSrcH, c->chrDstH, 1, (1<<12)-4,
  1874. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1875. srcFilter->chrV, dstFilter->chrV);
  1876. // Calculate Buffer Sizes so that they wont run out while handling these damn slices
  1877. c->vLumBufSize= c->vLumFilterSize;
  1878. c->vChrBufSize= c->vChrFilterSize;
  1879. for(i=0; i<dstH; i++)
  1880. {
  1881. int chrI= i*c->chrDstH / dstH;
  1882. int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  1883. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  1884. nextSlice&= ~3; // Slices start at boundaries which are divisable through 4
  1885. if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  1886. c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
  1887. if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  1888. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  1889. }
  1890. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  1891. c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
  1892. c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
  1893. //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)
  1894. for(i=0; i<c->vLumBufSize; i++)
  1895. c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
  1896. for(i=0; i<c->vChrBufSize; i++)
  1897. c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
  1898. //try to avoid drawing green stuff between the right end and the stride end
  1899. for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
  1900. for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
  1901. ASSERT(c->chrDstH <= dstH)
  1902. // pack filter data for mmx code
  1903. if(cpuCaps.hasMMX)
  1904. {
  1905. c->lumMmxFilter= (int16_t*)memalign(8, c->vLumFilterSize* dstH*4*sizeof(int16_t));
  1906. c->chrMmxFilter= (int16_t*)memalign(8, c->vChrFilterSize*c->chrDstH*4*sizeof(int16_t));
  1907. for(i=0; i<c->vLumFilterSize*dstH; i++)
  1908. c->lumMmxFilter[4*i]=c->lumMmxFilter[4*i+1]=c->lumMmxFilter[4*i+2]=c->lumMmxFilter[4*i+3]=
  1909. c->vLumFilter[i];
  1910. for(i=0; i<c->vChrFilterSize*c->chrDstH; i++)
  1911. c->chrMmxFilter[4*i]=c->chrMmxFilter[4*i+1]=c->chrMmxFilter[4*i+2]=c->chrMmxFilter[4*i+3]=
  1912. c->vChrFilter[i];
  1913. }
  1914. if(flags&SWS_PRINT_INFO)
  1915. {
  1916. #ifdef DITHER1XBPP
  1917. char *dither= " dithered";
  1918. #else
  1919. char *dither= "";
  1920. #endif
  1921. if(flags&SWS_FAST_BILINEAR)
  1922. MSG_INFO("\nSwScaler: FAST_BILINEAR scaler, ");
  1923. else if(flags&SWS_BILINEAR)
  1924. MSG_INFO("\nSwScaler: BILINEAR scaler, ");
  1925. else if(flags&SWS_BICUBIC)
  1926. MSG_INFO("\nSwScaler: BICUBIC scaler, ");
  1927. else if(flags&SWS_X)
  1928. MSG_INFO("\nSwScaler: Experimental scaler, ");
  1929. else if(flags&SWS_POINT)
  1930. MSG_INFO("\nSwScaler: Nearest Neighbor / POINT scaler, ");
  1931. else if(flags&SWS_AREA)
  1932. MSG_INFO("\nSwScaler: Area Averageing scaler, ");
  1933. else if(flags&SWS_BICUBLIN)
  1934. MSG_INFO("\nSwScaler: luma BICUBIC / chroma BILINEAR, ");
  1935. else
  1936. MSG_INFO("\nSwScaler: ehh flags invalid?! ");
  1937. if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
  1938. MSG_INFO("from %s to%s %s ",
  1939. vo_format_name(srcFormat), dither, vo_format_name(dstFormat));
  1940. else
  1941. MSG_INFO("from %s to %s ",
  1942. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1943. if(cpuCaps.hasMMX2)
  1944. MSG_INFO("using MMX2\n");
  1945. else if(cpuCaps.has3DNow)
  1946. MSG_INFO("using 3DNOW\n");
  1947. else if(cpuCaps.hasMMX)
  1948. MSG_INFO("using MMX\n");
  1949. else
  1950. MSG_INFO("using C\n");
  1951. }
  1952. if((flags & SWS_PRINT_INFO) && verbose)
  1953. {
  1954. if(cpuCaps.hasMMX)
  1955. {
  1956. if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  1957. MSG_V("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  1958. else
  1959. {
  1960. if(c->hLumFilterSize==4)
  1961. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
  1962. else if(c->hLumFilterSize==8)
  1963. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
  1964. else
  1965. MSG_V("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
  1966. if(c->hChrFilterSize==4)
  1967. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
  1968. else if(c->hChrFilterSize==8)
  1969. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
  1970. else
  1971. MSG_V("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
  1972. }
  1973. }
  1974. else
  1975. {
  1976. #ifdef ARCH_X86
  1977. MSG_V("SwScaler: using X86-Asm scaler for horizontal scaling\n");
  1978. #else
  1979. if(flags & SWS_FAST_BILINEAR)
  1980. MSG_V("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
  1981. else
  1982. MSG_V("SwScaler: using C scaler for horizontal scaling\n");
  1983. #endif
  1984. }
  1985. if(isPlanarYUV(dstFormat))
  1986. {
  1987. if(c->vLumFilterSize==1)
  1988. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
  1989. else
  1990. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
  1991. }
  1992. else
  1993. {
  1994. if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
  1995. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  1996. "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
  1997. else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
  1998. MSG_V("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
  1999. else
  2000. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
  2001. }
  2002. if(dstFormat==IMGFMT_BGR24)
  2003. MSG_V("SwScaler: using %s YV12->BGR24 Converter\n",
  2004. cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
  2005. else if(dstFormat==IMGFMT_BGR32)
  2006. MSG_V("SwScaler: using %s YV12->BGR32 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  2007. else if(dstFormat==IMGFMT_BGR16)
  2008. MSG_V("SwScaler: using %s YV12->BGR16 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  2009. else if(dstFormat==IMGFMT_BGR15)
  2010. MSG_V("SwScaler: using %s YV12->BGR15 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  2011. MSG_V("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  2012. }
  2013. if((flags & SWS_PRINT_INFO) && verbose>1)
  2014. {
  2015. MSG_DBG2("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2016. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  2017. MSG_DBG2("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2018. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  2019. }
  2020. c->swScale= swScale;
  2021. return c;
  2022. }
  2023. /**
  2024. * returns a normalized gaussian curve used to filter stuff
  2025. * quality=3 is high quality, lowwer is lowwer quality
  2026. */
  2027. SwsVector *getGaussianVec(double variance, double quality){
  2028. const int length= (int)(variance*quality + 0.5) | 1;
  2029. int i;
  2030. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2031. double middle= (length-1)*0.5;
  2032. SwsVector *vec= malloc(sizeof(SwsVector));
  2033. vec->coeff= coeff;
  2034. vec->length= length;
  2035. for(i=0; i<length; i++)
  2036. {
  2037. double dist= i-middle;
  2038. coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
  2039. }
  2040. normalizeVec(vec, 1.0);
  2041. return vec;
  2042. }
  2043. SwsVector *getConstVec(double c, int length){
  2044. int i;
  2045. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2046. SwsVector *vec= malloc(sizeof(SwsVector));
  2047. vec->coeff= coeff;
  2048. vec->length= length;
  2049. for(i=0; i<length; i++)
  2050. coeff[i]= c;
  2051. return vec;
  2052. }
  2053. SwsVector *getIdentityVec(void){
  2054. double *coeff= memalign(sizeof(double), sizeof(double));
  2055. SwsVector *vec= malloc(sizeof(SwsVector));
  2056. coeff[0]= 1.0;
  2057. vec->coeff= coeff;
  2058. vec->length= 1;
  2059. return vec;
  2060. }
  2061. void normalizeVec(SwsVector *a, double height){
  2062. int i;
  2063. double sum=0;
  2064. double inv;
  2065. for(i=0; i<a->length; i++)
  2066. sum+= a->coeff[i];
  2067. inv= height/sum;
  2068. for(i=0; i<a->length; i++)
  2069. a->coeff[i]*= height;
  2070. }
  2071. void scaleVec(SwsVector *a, double scalar){
  2072. int i;
  2073. for(i=0; i<a->length; i++)
  2074. a->coeff[i]*= scalar;
  2075. }
  2076. static SwsVector *getConvVec(SwsVector *a, SwsVector *b){
  2077. int length= a->length + b->length - 1;
  2078. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2079. int i, j;
  2080. SwsVector *vec= malloc(sizeof(SwsVector));
  2081. vec->coeff= coeff;
  2082. vec->length= length;
  2083. for(i=0; i<length; i++) coeff[i]= 0.0;
  2084. for(i=0; i<a->length; i++)
  2085. {
  2086. for(j=0; j<b->length; j++)
  2087. {
  2088. coeff[i+j]+= a->coeff[i]*b->coeff[j];
  2089. }
  2090. }
  2091. return vec;
  2092. }
  2093. static SwsVector *sumVec(SwsVector *a, SwsVector *b){
  2094. int length= MAX(a->length, b->length);
  2095. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2096. int i;
  2097. SwsVector *vec= malloc(sizeof(SwsVector));
  2098. vec->coeff= coeff;
  2099. vec->length= length;
  2100. for(i=0; i<length; i++) coeff[i]= 0.0;
  2101. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2102. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  2103. return vec;
  2104. }
  2105. static SwsVector *diffVec(SwsVector *a, SwsVector *b){
  2106. int length= MAX(a->length, b->length);
  2107. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2108. int i;
  2109. SwsVector *vec= malloc(sizeof(SwsVector));
  2110. vec->coeff= coeff;
  2111. vec->length= length;
  2112. for(i=0; i<length; i++) coeff[i]= 0.0;
  2113. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2114. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  2115. return vec;
  2116. }
  2117. /* shift left / or right if "shift" is negative */
  2118. static SwsVector *getShiftedVec(SwsVector *a, int shift){
  2119. int length= a->length + ABS(shift)*2;
  2120. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2121. int i;
  2122. SwsVector *vec= malloc(sizeof(SwsVector));
  2123. vec->coeff= coeff;
  2124. vec->length= length;
  2125. for(i=0; i<length; i++) coeff[i]= 0.0;
  2126. for(i=0; i<a->length; i++)
  2127. {
  2128. coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  2129. }
  2130. return vec;
  2131. }
  2132. void shiftVec(SwsVector *a, int shift){
  2133. SwsVector *shifted= getShiftedVec(a, shift);
  2134. free(a->coeff);
  2135. a->coeff= shifted->coeff;
  2136. a->length= shifted->length;
  2137. free(shifted);
  2138. }
  2139. void addVec(SwsVector *a, SwsVector *b){
  2140. SwsVector *sum= sumVec(a, b);
  2141. free(a->coeff);
  2142. a->coeff= sum->coeff;
  2143. a->length= sum->length;
  2144. free(sum);
  2145. }
  2146. void subVec(SwsVector *a, SwsVector *b){
  2147. SwsVector *diff= diffVec(a, b);
  2148. free(a->coeff);
  2149. a->coeff= diff->coeff;
  2150. a->length= diff->length;
  2151. free(diff);
  2152. }
  2153. void convVec(SwsVector *a, SwsVector *b){
  2154. SwsVector *conv= getConvVec(a, b);
  2155. free(a->coeff);
  2156. a->coeff= conv->coeff;
  2157. a->length= conv->length;
  2158. free(conv);
  2159. }
  2160. SwsVector *cloneVec(SwsVector *a){
  2161. double *coeff= memalign(sizeof(double), a->length*sizeof(double));
  2162. int i;
  2163. SwsVector *vec= malloc(sizeof(SwsVector));
  2164. vec->coeff= coeff;
  2165. vec->length= a->length;
  2166. for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
  2167. return vec;
  2168. }
  2169. void printVec(SwsVector *a){
  2170. int i;
  2171. double max=0;
  2172. double min=0;
  2173. double range;
  2174. for(i=0; i<a->length; i++)
  2175. if(a->coeff[i]>max) max= a->coeff[i];
  2176. for(i=0; i<a->length; i++)
  2177. if(a->coeff[i]<min) min= a->coeff[i];
  2178. range= max - min;
  2179. for(i=0; i<a->length; i++)
  2180. {
  2181. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  2182. MSG_DBG2("%1.3f ", a->coeff[i]);
  2183. for(;x>0; x--) MSG_DBG2(" ");
  2184. MSG_DBG2("|\n");
  2185. }
  2186. }
  2187. void freeVec(SwsVector *a){
  2188. if(!a) return;
  2189. if(a->coeff) free(a->coeff);
  2190. a->coeff=NULL;
  2191. a->length=0;
  2192. free(a);
  2193. }
  2194. void freeSwsContext(SwsContext *c){
  2195. int i;
  2196. if(!c) return;
  2197. if(c->lumPixBuf)
  2198. {
  2199. for(i=0; i<c->vLumBufSize; i++)
  2200. {
  2201. if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
  2202. c->lumPixBuf[i]=NULL;
  2203. }
  2204. free(c->lumPixBuf);
  2205. c->lumPixBuf=NULL;
  2206. }
  2207. if(c->chrPixBuf)
  2208. {
  2209. for(i=0; i<c->vChrBufSize; i++)
  2210. {
  2211. if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
  2212. c->chrPixBuf[i]=NULL;
  2213. }
  2214. free(c->chrPixBuf);
  2215. c->chrPixBuf=NULL;
  2216. }
  2217. if(c->vLumFilter) free(c->vLumFilter);
  2218. c->vLumFilter = NULL;
  2219. if(c->vChrFilter) free(c->vChrFilter);
  2220. c->vChrFilter = NULL;
  2221. if(c->hLumFilter) free(c->hLumFilter);
  2222. c->hLumFilter = NULL;
  2223. if(c->hChrFilter) free(c->hChrFilter);
  2224. c->hChrFilter = NULL;
  2225. if(c->vLumFilterPos) free(c->vLumFilterPos);
  2226. c->vLumFilterPos = NULL;
  2227. if(c->vChrFilterPos) free(c->vChrFilterPos);
  2228. c->vChrFilterPos = NULL;
  2229. if(c->hLumFilterPos) free(c->hLumFilterPos);
  2230. c->hLumFilterPos = NULL;
  2231. if(c->hChrFilterPos) free(c->hChrFilterPos);
  2232. c->hChrFilterPos = NULL;
  2233. if(c->lumMmxFilter) free(c->lumMmxFilter);
  2234. c->lumMmxFilter = NULL;
  2235. if(c->chrMmxFilter) free(c->chrMmxFilter);
  2236. c->chrMmxFilter = NULL;
  2237. if(c->lumMmx2Filter) free(c->lumMmx2Filter);
  2238. c->lumMmx2Filter=NULL;
  2239. if(c->chrMmx2Filter) free(c->chrMmx2Filter);
  2240. c->chrMmx2Filter=NULL;
  2241. if(c->lumMmx2FilterPos) free(c->lumMmx2FilterPos);
  2242. c->lumMmx2FilterPos=NULL;
  2243. if(c->chrMmx2FilterPos) free(c->chrMmx2FilterPos);
  2244. c->chrMmx2FilterPos=NULL;
  2245. if(c->yuvTable) free(c->yuvTable);
  2246. c->yuvTable=NULL;
  2247. free(c);
  2248. }