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.

2616 lines
74KB

  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. case 7: *flags|= SWS_GAUSS; break;
  862. case 8: *flags|= SWS_SINC; break;
  863. case 9: *flags|= SWS_LANCZOS; break;
  864. case 10:*flags|= SWS_SPLINE; break;
  865. default:*flags|= SWS_BILINEAR; break;
  866. }
  867. *srcFilterParam= &src_filter;
  868. *dstFilterParam= NULL;
  869. }
  870. // will use sws_flags & src_filter (from cmd line)
  871. SwsContext *getSwsContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
  872. {
  873. int flags;
  874. SwsFilter *dstFilterParam, *srcFilterParam;
  875. swsGetFlagsAndFilterFromCmdLine(&flags, &srcFilterParam, &dstFilterParam);
  876. return getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, srcFilterParam, dstFilterParam);
  877. }
  878. static double getSplineCoeff(double a, double b, double c, double d, double dist)
  879. {
  880. // printf("%f %f %f %f %f\n", a,b,c,d,dist);
  881. if(dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
  882. else return getSplineCoeff( 0.0,
  883. b+ 2.0*c + 3.0*d,
  884. c + 3.0*d,
  885. -b- 3.0*c - 6.0*d,
  886. dist-1.0);
  887. }
  888. static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  889. int srcW, int dstW, int filterAlign, int one, int flags,
  890. SwsVector *srcFilter, SwsVector *dstFilter)
  891. {
  892. int i;
  893. int filterSize;
  894. int filter2Size;
  895. int minFilterSize;
  896. double *filter=NULL;
  897. double *filter2=NULL;
  898. #ifdef ARCH_X86
  899. if(gCpuCaps.hasMMX)
  900. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  901. #endif
  902. // Note the +1 is for the MMXscaler which reads over the end
  903. *filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));
  904. if(ABS(xInc - 0x10000) <10) // unscaled
  905. {
  906. int i;
  907. filterSize= 1;
  908. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  909. for(i=0; i<dstW*filterSize; i++) filter[i]=0;
  910. for(i=0; i<dstW; i++)
  911. {
  912. filter[i*filterSize]=1;
  913. (*filterPos)[i]=i;
  914. }
  915. }
  916. else if(flags&SWS_POINT) // lame looking point sampling mode
  917. {
  918. int i;
  919. int xDstInSrc;
  920. filterSize= 1;
  921. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  922. xDstInSrc= xInc/2 - 0x8000;
  923. for(i=0; i<dstW; i++)
  924. {
  925. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  926. (*filterPos)[i]= xx;
  927. filter[i]= 1.0;
  928. xDstInSrc+= xInc;
  929. }
  930. }
  931. else if((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
  932. {
  933. int i;
  934. int xDstInSrc;
  935. if (flags&SWS_BICUBIC) filterSize= 4;
  936. else if(flags&SWS_X ) filterSize= 4;
  937. else filterSize= 2; // SWS_BILINEAR / SWS_AREA
  938. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  939. xDstInSrc= xInc/2 - 0x8000;
  940. for(i=0; i<dstW; i++)
  941. {
  942. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  943. int j;
  944. (*filterPos)[i]= xx;
  945. //Bilinear upscale / linear interpolate / Area averaging
  946. for(j=0; j<filterSize; j++)
  947. {
  948. double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
  949. double coeff= 1.0 - d;
  950. if(coeff<0) coeff=0;
  951. filter[i*filterSize + j]= coeff;
  952. xx++;
  953. }
  954. xDstInSrc+= xInc;
  955. }
  956. }
  957. else
  958. {
  959. double xDstInSrc;
  960. double sizeFactor, filterSizeInSrc;
  961. const double xInc1= (double)xInc / (double)(1<<16);
  962. int param= (flags&SWS_PARAM_MASK)>>SWS_PARAM_SHIFT;
  963. if (flags&SWS_BICUBIC) sizeFactor= 4.0;
  964. else if(flags&SWS_X) sizeFactor= 8.0;
  965. else if(flags&SWS_AREA) sizeFactor= 1.0; //downscale only, for upscale it is bilinear
  966. else if(flags&SWS_GAUSS) sizeFactor= 8.0; // infinite ;)
  967. else if(flags&SWS_LANCZOS) sizeFactor= param ? 2.0*param : 6.0;
  968. else if(flags&SWS_SINC) sizeFactor= 100.0; // infinite ;)
  969. else if(flags&SWS_SPLINE) sizeFactor= 20.0; // infinite ;)
  970. else if(flags&SWS_BILINEAR) sizeFactor= 2.0;
  971. else ASSERT(0)
  972. if(xInc1 <= 1.0) filterSizeInSrc= sizeFactor; // upscale
  973. else filterSizeInSrc= sizeFactor*srcW / (double)dstW;
  974. filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
  975. if(filterSize > srcW-2) filterSize=srcW-2;
  976. filter= (double*)memalign(16, dstW*sizeof(double)*filterSize);
  977. xDstInSrc= xInc1 / 2.0 - 0.5;
  978. for(i=0; i<dstW; i++)
  979. {
  980. int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
  981. int j;
  982. (*filterPos)[i]= xx;
  983. for(j=0; j<filterSize; j++)
  984. {
  985. double d= ABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
  986. double coeff;
  987. if(flags & SWS_BICUBIC)
  988. {
  989. double A= param ? -param*0.01 : -0.60;
  990. // Equation is from VirtualDub
  991. if(d<1.0)
  992. coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  993. else if(d<2.0)
  994. coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
  995. else
  996. coeff=0.0;
  997. }
  998. /* else if(flags & SWS_X)
  999. {
  1000. double p= param ? param*0.01 : 0.3;
  1001. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  1002. coeff*= pow(2.0, - p*d*d);
  1003. }*/
  1004. else if(flags & SWS_X)
  1005. {
  1006. double A= param ? param*0.1 : 1.0;
  1007. if(d<1.0)
  1008. coeff = cos(d*PI);
  1009. else
  1010. coeff=-1.0;
  1011. if(coeff<0.0) coeff= -pow(-coeff, A);
  1012. else coeff= pow( coeff, A);
  1013. coeff= coeff*0.5 + 0.5;
  1014. }
  1015. else if(flags & SWS_AREA)
  1016. {
  1017. double srcPixelSize= 1.0/xInc1;
  1018. if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
  1019. else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
  1020. else coeff=0.0;
  1021. }
  1022. else if(flags & SWS_GAUSS)
  1023. {
  1024. double p= param ? param*0.1 : 3.0;
  1025. coeff = pow(2.0, - p*d*d);
  1026. }
  1027. else if(flags & SWS_SINC)
  1028. {
  1029. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  1030. }
  1031. else if(flags & SWS_LANCZOS)
  1032. {
  1033. double p= param ? param : 3.0;
  1034. coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
  1035. if(d>p) coeff=0;
  1036. }
  1037. else if(flags & SWS_BILINEAR)
  1038. {
  1039. coeff= 1.0 - d;
  1040. if(coeff<0) coeff=0;
  1041. }
  1042. else if(flags & SWS_SPLINE)
  1043. {
  1044. double p=-2.196152422706632;
  1045. coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
  1046. }
  1047. else ASSERT(0)
  1048. filter[i*filterSize + j]= coeff;
  1049. xx++;
  1050. }
  1051. xDstInSrc+= xInc1;
  1052. }
  1053. }
  1054. /* apply src & dst Filter to filter -> filter2
  1055. free(filter);
  1056. */
  1057. ASSERT(filterSize>0)
  1058. filter2Size= filterSize;
  1059. if(srcFilter) filter2Size+= srcFilter->length - 1;
  1060. if(dstFilter) filter2Size+= dstFilter->length - 1;
  1061. ASSERT(filter2Size>0)
  1062. filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
  1063. for(i=0; i<dstW; i++)
  1064. {
  1065. int j;
  1066. SwsVector scaleFilter;
  1067. SwsVector *outVec;
  1068. scaleFilter.coeff= filter + i*filterSize;
  1069. scaleFilter.length= filterSize;
  1070. if(srcFilter) outVec= getConvVec(srcFilter, &scaleFilter);
  1071. else outVec= &scaleFilter;
  1072. ASSERT(outVec->length == filter2Size)
  1073. //FIXME dstFilter
  1074. for(j=0; j<outVec->length; j++)
  1075. {
  1076. filter2[i*filter2Size + j]= outVec->coeff[j];
  1077. }
  1078. (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
  1079. if(outVec != &scaleFilter) freeVec(outVec);
  1080. }
  1081. free(filter); filter=NULL;
  1082. /* try to reduce the filter-size (step1 find size and shift left) */
  1083. // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
  1084. minFilterSize= 0;
  1085. for(i=dstW-1; i>=0; i--)
  1086. {
  1087. int min= filter2Size;
  1088. int j;
  1089. double cutOff=0.0;
  1090. /* get rid off near zero elements on the left by shifting left */
  1091. for(j=0; j<filter2Size; j++)
  1092. {
  1093. int k;
  1094. cutOff += ABS(filter2[i*filter2Size]);
  1095. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1096. /* preserve Monotonicity because the core cant handle the filter otherwise */
  1097. if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
  1098. // Move filter coeffs left
  1099. for(k=1; k<filter2Size; k++)
  1100. filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
  1101. filter2[i*filter2Size + k - 1]= 0.0;
  1102. (*filterPos)[i]++;
  1103. }
  1104. cutOff=0.0;
  1105. /* count near zeros on the right */
  1106. for(j=filter2Size-1; j>0; j--)
  1107. {
  1108. cutOff += ABS(filter2[i*filter2Size + j]);
  1109. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1110. min--;
  1111. }
  1112. if(min>minFilterSize) minFilterSize= min;
  1113. }
  1114. ASSERT(minFilterSize > 0)
  1115. filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
  1116. ASSERT(filterSize > 0)
  1117. filter= (double*)memalign(8, filterSize*dstW*sizeof(double));
  1118. *outFilterSize= filterSize;
  1119. if(flags&SWS_PRINT_INFO)
  1120. MSG_INFO("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
  1121. /* try to reduce the filter-size (step2 reduce it) */
  1122. for(i=0; i<dstW; i++)
  1123. {
  1124. int j;
  1125. for(j=0; j<filterSize; j++)
  1126. {
  1127. if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
  1128. else filter[i*filterSize + j]= filter2[i*filter2Size + j];
  1129. }
  1130. }
  1131. free(filter2); filter2=NULL;
  1132. //FIXME try to align filterpos if possible
  1133. //fix borders
  1134. for(i=0; i<dstW; i++)
  1135. {
  1136. int j;
  1137. if((*filterPos)[i] < 0)
  1138. {
  1139. // Move filter coeffs left to compensate for filterPos
  1140. for(j=1; j<filterSize; j++)
  1141. {
  1142. int left= MAX(j + (*filterPos)[i], 0);
  1143. filter[i*filterSize + left] += filter[i*filterSize + j];
  1144. filter[i*filterSize + j]=0;
  1145. }
  1146. (*filterPos)[i]= 0;
  1147. }
  1148. if((*filterPos)[i] + filterSize > srcW)
  1149. {
  1150. int shift= (*filterPos)[i] + filterSize - srcW;
  1151. // Move filter coeffs right to compensate for filterPos
  1152. for(j=filterSize-2; j>=0; j--)
  1153. {
  1154. int right= MIN(j + shift, filterSize-1);
  1155. filter[i*filterSize +right] += filter[i*filterSize +j];
  1156. filter[i*filterSize +j]=0;
  1157. }
  1158. (*filterPos)[i]= srcW - filterSize;
  1159. }
  1160. }
  1161. // Note the +1 is for the MMXscaler which reads over the end
  1162. *outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));
  1163. memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
  1164. /* Normalize & Store in outFilter */
  1165. for(i=0; i<dstW; i++)
  1166. {
  1167. int j;
  1168. double sum=0;
  1169. double scale= one;
  1170. for(j=0; j<filterSize; j++)
  1171. {
  1172. sum+= filter[i*filterSize + j];
  1173. }
  1174. scale/= sum;
  1175. for(j=0; j<filterSize; j++)
  1176. {
  1177. (*outFilter)[i*(*outFilterSize) + j]= (int)(filter[i*filterSize + j]*scale);
  1178. }
  1179. }
  1180. (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
  1181. for(i=0; i<*outFilterSize; i++)
  1182. {
  1183. int j= dstW*(*outFilterSize);
  1184. (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
  1185. }
  1186. free(filter);
  1187. }
  1188. #ifdef ARCH_X86
  1189. static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
  1190. {
  1191. uint8_t *fragmentA;
  1192. int imm8OfPShufW1A;
  1193. int imm8OfPShufW2A;
  1194. int fragmentLengthA;
  1195. uint8_t *fragmentB;
  1196. int imm8OfPShufW1B;
  1197. int imm8OfPShufW2B;
  1198. int fragmentLengthB;
  1199. int fragmentPos;
  1200. int xpos, i;
  1201. // create an optimized horizontal scaling routine
  1202. //code fragment
  1203. asm volatile(
  1204. "jmp 9f \n\t"
  1205. // Begin
  1206. "0: \n\t"
  1207. "movq (%%edx, %%eax), %%mm3 \n\t"
  1208. "movd (%%ecx, %%esi), %%mm0 \n\t"
  1209. "movd 1(%%ecx, %%esi), %%mm1 \n\t"
  1210. "punpcklbw %%mm7, %%mm1 \n\t"
  1211. "punpcklbw %%mm7, %%mm0 \n\t"
  1212. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  1213. "1: \n\t"
  1214. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1215. "2: \n\t"
  1216. "psubw %%mm1, %%mm0 \n\t"
  1217. "movl 8(%%ebx, %%eax), %%esi \n\t"
  1218. "pmullw %%mm3, %%mm0 \n\t"
  1219. "psllw $7, %%mm1 \n\t"
  1220. "paddw %%mm1, %%mm0 \n\t"
  1221. "movq %%mm0, (%%edi, %%eax) \n\t"
  1222. "addl $8, %%eax \n\t"
  1223. // End
  1224. "9: \n\t"
  1225. // "int $3\n\t"
  1226. "leal 0b, %0 \n\t"
  1227. "leal 1b, %1 \n\t"
  1228. "leal 2b, %2 \n\t"
  1229. "decl %1 \n\t"
  1230. "decl %2 \n\t"
  1231. "subl %0, %1 \n\t"
  1232. "subl %0, %2 \n\t"
  1233. "leal 9b, %3 \n\t"
  1234. "subl %0, %3 \n\t"
  1235. :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  1236. "=r" (fragmentLengthA)
  1237. );
  1238. asm volatile(
  1239. "jmp 9f \n\t"
  1240. // Begin
  1241. "0: \n\t"
  1242. "movq (%%edx, %%eax), %%mm3 \n\t"
  1243. "movd (%%ecx, %%esi), %%mm0 \n\t"
  1244. "punpcklbw %%mm7, %%mm0 \n\t"
  1245. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  1246. "1: \n\t"
  1247. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1248. "2: \n\t"
  1249. "psubw %%mm1, %%mm0 \n\t"
  1250. "movl 8(%%ebx, %%eax), %%esi \n\t"
  1251. "pmullw %%mm3, %%mm0 \n\t"
  1252. "psllw $7, %%mm1 \n\t"
  1253. "paddw %%mm1, %%mm0 \n\t"
  1254. "movq %%mm0, (%%edi, %%eax) \n\t"
  1255. "addl $8, %%eax \n\t"
  1256. // End
  1257. "9: \n\t"
  1258. // "int $3\n\t"
  1259. "leal 0b, %0 \n\t"
  1260. "leal 1b, %1 \n\t"
  1261. "leal 2b, %2 \n\t"
  1262. "decl %1 \n\t"
  1263. "decl %2 \n\t"
  1264. "subl %0, %1 \n\t"
  1265. "subl %0, %2 \n\t"
  1266. "leal 9b, %3 \n\t"
  1267. "subl %0, %3 \n\t"
  1268. :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  1269. "=r" (fragmentLengthB)
  1270. );
  1271. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  1272. fragmentPos=0;
  1273. for(i=0; i<dstW/numSplits; i++)
  1274. {
  1275. int xx=xpos>>16;
  1276. if((i&3) == 0)
  1277. {
  1278. int a=0;
  1279. int b=((xpos+xInc)>>16) - xx;
  1280. int c=((xpos+xInc*2)>>16) - xx;
  1281. int d=((xpos+xInc*3)>>16) - xx;
  1282. filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
  1283. filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
  1284. filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
  1285. filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
  1286. filterPos[i/2]= xx;
  1287. if(d+1<4)
  1288. {
  1289. int maxShift= 3-(d+1);
  1290. int shift=0;
  1291. memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
  1292. funnyCode[fragmentPos + imm8OfPShufW1B]=
  1293. (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
  1294. funnyCode[fragmentPos + imm8OfPShufW2B]=
  1295. a | (b<<2) | (c<<4) | (d<<6);
  1296. if(i+3>=dstW) shift=maxShift; //avoid overread
  1297. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
  1298. if(shift && i>=shift)
  1299. {
  1300. funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
  1301. funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
  1302. filterPos[i/2]-=shift;
  1303. }
  1304. fragmentPos+= fragmentLengthB;
  1305. }
  1306. else
  1307. {
  1308. int maxShift= 3-d;
  1309. int shift=0;
  1310. memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
  1311. funnyCode[fragmentPos + imm8OfPShufW1A]=
  1312. funnyCode[fragmentPos + imm8OfPShufW2A]=
  1313. a | (b<<2) | (c<<4) | (d<<6);
  1314. if(i+4>=dstW) shift=maxShift; //avoid overread
  1315. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
  1316. if(shift && i>=shift)
  1317. {
  1318. funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
  1319. funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
  1320. filterPos[i/2]-=shift;
  1321. }
  1322. fragmentPos+= fragmentLengthA;
  1323. }
  1324. funnyCode[fragmentPos]= RET;
  1325. }
  1326. xpos+=xInc;
  1327. }
  1328. filterPos[i/2]= xpos>>16; // needed to jump to the next part
  1329. }
  1330. #endif // ARCH_X86
  1331. //FIXME remove
  1332. void SwScale_Init(){
  1333. }
  1334. static void globalInit(){
  1335. // generating tables:
  1336. int i;
  1337. for(i=0; i<768; i++){
  1338. int c= MIN(MAX(i-256, 0), 255);
  1339. clip_table[i]=c;
  1340. }
  1341. cpuCaps= gCpuCaps;
  1342. #ifdef RUNTIME_CPUDETECT
  1343. #ifdef CAN_COMPILE_X86_ASM
  1344. // ordered per speed fasterst first
  1345. if(gCpuCaps.hasMMX2)
  1346. swScale= swScale_MMX2;
  1347. else if(gCpuCaps.has3DNow)
  1348. swScale= swScale_3DNow;
  1349. else if(gCpuCaps.hasMMX)
  1350. swScale= swScale_MMX;
  1351. else
  1352. swScale= swScale_C;
  1353. #else
  1354. swScale= swScale_C;
  1355. cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
  1356. #endif
  1357. #else //RUNTIME_CPUDETECT
  1358. #ifdef HAVE_MMX2
  1359. swScale= swScale_MMX2;
  1360. cpuCaps.has3DNow = 0;
  1361. #elif defined (HAVE_3DNOW)
  1362. swScale= swScale_3DNow;
  1363. cpuCaps.hasMMX2 = 0;
  1364. #elif defined (HAVE_MMX)
  1365. swScale= swScale_MMX;
  1366. cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
  1367. #else
  1368. swScale= swScale_C;
  1369. cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
  1370. #endif
  1371. #endif //!RUNTIME_CPUDETECT
  1372. }
  1373. static void PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1374. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1375. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1376. /* Copy Y plane */
  1377. if(dstStride[0]==srcStride[0])
  1378. memcpy(dst, src[0], srcSliceH*dstStride[0]);
  1379. else
  1380. {
  1381. int i;
  1382. uint8_t *srcPtr= src[0];
  1383. uint8_t *dstPtr= dst;
  1384. for(i=0; i<srcSliceH; i++)
  1385. {
  1386. memcpy(dstPtr, srcPtr, srcStride[0]);
  1387. srcPtr+= srcStride[0];
  1388. dstPtr+= dstStride[0];
  1389. }
  1390. }
  1391. dst = dstParam[1] + dstStride[1]*srcSliceY;
  1392. if(c->srcFormat==IMGFMT_YV12)
  1393. interleaveBytes( src[1],src[2],dst,c->srcW,srcSliceH,srcStride[1],srcStride[2],dstStride[0] );
  1394. else /* I420 & IYUV */
  1395. interleaveBytes( src[2],src[1],dst,c->srcW,srcSliceH,srcStride[2],srcStride[1],dstStride[0] );
  1396. }
  1397. /* Warper functions for yuv2bgr */
  1398. static void planarYuvToBgr(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1399. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1400. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1401. if(c->srcFormat==IMGFMT_YV12)
  1402. yuv2rgb( dst,src[0],src[1],src[2],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
  1403. else /* I420 & IYUV */
  1404. yuv2rgb( dst,src[0],src[2],src[1],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
  1405. }
  1406. static void PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1407. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1408. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1409. if(c->srcFormat==IMGFMT_YV12)
  1410. yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1411. else /* I420 & IYUV */
  1412. yv12toyuy2( src[0],src[2],src[1],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1413. }
  1414. /* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
  1415. static void rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1416. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1417. const int srcFormat= c->srcFormat;
  1418. const int dstFormat= c->dstFormat;
  1419. const int srcBpp= ((srcFormat&0xFF) + 7)>>3;
  1420. const int dstBpp= ((dstFormat&0xFF) + 7)>>3;
  1421. const int srcId= (srcFormat&0xFF)>>2; // 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8
  1422. const int dstId= (dstFormat&0xFF)>>2;
  1423. void (*conv)(const uint8_t *src, uint8_t *dst, unsigned src_size)=NULL;
  1424. /* BGR -> BGR */
  1425. if(isBGR(srcFormat) && isBGR(dstFormat)){
  1426. switch(srcId | (dstId<<4)){
  1427. case 0x34: conv= rgb16to15; break;
  1428. case 0x36: conv= rgb24to15; break;
  1429. case 0x38: conv= rgb32to15; break;
  1430. case 0x43: conv= rgb15to16; break;
  1431. case 0x46: conv= rgb24to16; break;
  1432. case 0x48: conv= rgb32to16; break;
  1433. case 0x63: conv= rgb15to24; break;
  1434. case 0x64: conv= rgb16to24; break;
  1435. case 0x68: conv= rgb32to24; break;
  1436. case 0x83: conv= rgb15to32; break;
  1437. case 0x84: conv= rgb16to32; break;
  1438. case 0x86: conv= rgb24to32; break;
  1439. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1440. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1441. }
  1442. }else if(isBGR(srcFormat) && isRGB(dstFormat)){
  1443. switch(srcId | (dstId<<4)){
  1444. case 0x33: conv= rgb15tobgr15; break;
  1445. case 0x34: conv= rgb16tobgr15; break;
  1446. case 0x36: conv= rgb24tobgr15; break;
  1447. case 0x38: conv= rgb32tobgr15; break;
  1448. case 0x43: conv= rgb15tobgr16; break;
  1449. case 0x44: conv= rgb16tobgr16; break;
  1450. case 0x46: conv= rgb24tobgr16; break;
  1451. case 0x48: conv= rgb32tobgr16; break;
  1452. case 0x63: conv= rgb15tobgr24; break;
  1453. case 0x64: conv= rgb16tobgr24; break;
  1454. case 0x66: conv= rgb24tobgr24; break;
  1455. case 0x68: conv= rgb32tobgr24; break;
  1456. case 0x83: conv= rgb15tobgr32; break;
  1457. case 0x84: conv= rgb16tobgr32; break;
  1458. case 0x86: conv= rgb24tobgr32; break;
  1459. case 0x88: conv= rgb32tobgr32; break;
  1460. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1461. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1462. }
  1463. }else if(isRGB(srcFormat) && isRGB(dstFormat)){
  1464. switch(srcId | (dstId<<4)){
  1465. case 0x34: conv= rgb16to15; break;
  1466. case 0x36: conv= rgb24to15; break;
  1467. case 0x38: conv= rgb32to15; break;
  1468. case 0x43: conv= rgb15to16; break;
  1469. case 0x46: conv= rgb24to16; break;
  1470. case 0x48: conv= rgb32to16; break;
  1471. case 0x63: conv= rgb15to24; break;
  1472. case 0x64: conv= rgb16to24; break;
  1473. case 0x68: conv= rgb32to24; break;
  1474. case 0x83: conv= rgb15to32; break;
  1475. case 0x84: conv= rgb16to32; break;
  1476. case 0x86: conv= rgb24to32; break;
  1477. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1478. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1479. }
  1480. }else if(isRGB(srcFormat) && isBGR(dstFormat)){
  1481. switch(srcId | (dstId<<4)){
  1482. case 0x33: conv= rgb15tobgr15; break;
  1483. case 0x34: conv= rgb16tobgr15; break;
  1484. case 0x36: conv= rgb24tobgr15; break;
  1485. case 0x38: conv= rgb32tobgr15; break;
  1486. case 0x43: conv= rgb15tobgr16; break;
  1487. case 0x44: conv= rgb16tobgr16; break;
  1488. case 0x46: conv= rgb24tobgr16; break;
  1489. case 0x48: conv= rgb32tobgr16; break;
  1490. case 0x63: conv= rgb15tobgr24; break;
  1491. case 0x64: conv= rgb16tobgr24; break;
  1492. case 0x66: conv= rgb24tobgr24; break;
  1493. case 0x68: conv= rgb32tobgr24; break;
  1494. case 0x83: conv= rgb15tobgr32; break;
  1495. case 0x84: conv= rgb16tobgr32; break;
  1496. case 0x86: conv= rgb24tobgr32; break;
  1497. case 0x88: conv= rgb32tobgr32; break;
  1498. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1499. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1500. }
  1501. }
  1502. if(dstStride[0]*srcBpp == srcStride[0]*dstBpp)
  1503. conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1504. else
  1505. {
  1506. int i;
  1507. uint8_t *srcPtr= src[0];
  1508. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1509. for(i=0; i<srcSliceH; i++)
  1510. {
  1511. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1512. srcPtr+= srcStride[0];
  1513. dstPtr+= dstStride[0];
  1514. }
  1515. }
  1516. }
  1517. static void bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1518. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1519. rgb24toyv12(
  1520. src[0],
  1521. dst[0]+ srcSliceY *dstStride[0],
  1522. dst[1]+(srcSliceY>>1)*dstStride[1],
  1523. dst[2]+(srcSliceY>>1)*dstStride[2],
  1524. c->srcW, srcSliceH,
  1525. dstStride[0], dstStride[1], srcStride[0]);
  1526. }
  1527. static void yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1528. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1529. int i;
  1530. /* copy Y */
  1531. if(srcStride[0]==dstStride[0])
  1532. memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
  1533. else{
  1534. uint8_t *srcPtr= src[0];
  1535. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1536. for(i=0; i<srcSliceH; i++)
  1537. {
  1538. memcpy(dstPtr, srcPtr, c->srcW);
  1539. srcPtr+= srcStride[0];
  1540. dstPtr+= dstStride[0];
  1541. }
  1542. }
  1543. if(c->dstFormat==IMGFMT_YV12){
  1544. planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
  1545. planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
  1546. }else{
  1547. planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
  1548. planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
  1549. }
  1550. }
  1551. /**
  1552. * bring pointers in YUV order instead of YVU
  1553. */
  1554. static inline void orderYUV(int format, uint8_t * sortedP[], int sortedStride[], uint8_t * p[], int stride[]){
  1555. if(format == IMGFMT_YV12 || format == IMGFMT_YVU9){
  1556. sortedP[0]= p[0];
  1557. sortedP[1]= p[1];
  1558. sortedP[2]= p[2];
  1559. sortedStride[0]= stride[0];
  1560. sortedStride[1]= stride[1];
  1561. sortedStride[2]= stride[2];
  1562. }
  1563. else if(isPacked(format) || isGray(format))
  1564. {
  1565. sortedP[0]= p[0];
  1566. sortedP[1]=
  1567. sortedP[2]= NULL;
  1568. sortedStride[0]= stride[0];
  1569. sortedStride[1]=
  1570. sortedStride[2]= 0;
  1571. }
  1572. else if(format == IMGFMT_I420)
  1573. {
  1574. sortedP[0]= p[0];
  1575. sortedP[1]= p[2];
  1576. sortedP[2]= p[1];
  1577. sortedStride[0]= stride[0];
  1578. sortedStride[1]= stride[2];
  1579. sortedStride[2]= stride[1];
  1580. }else{
  1581. MSG_ERR("internal error in orderYUV\n");
  1582. }
  1583. }
  1584. /* unscaled copy like stuff (assumes nearly identical formats) */
  1585. static void simpleCopy(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
  1586. int srcSliceH, uint8_t* dstParam[], int dstStrideParam[]){
  1587. int srcStride[3];
  1588. int dstStride[3];
  1589. uint8_t *src[3];
  1590. uint8_t *dst[3];
  1591. orderYUV(c->srcFormat, src, srcStride, srcParam, srcStrideParam);
  1592. orderYUV(c->dstFormat, dst, dstStride, dstParam, dstStrideParam);
  1593. if(isPacked(c->srcFormat))
  1594. {
  1595. if(dstStride[0]==srcStride[0])
  1596. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1597. else
  1598. {
  1599. int i;
  1600. uint8_t *srcPtr= src[0];
  1601. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1602. int length=0;
  1603. /* universal length finder */
  1604. while(length+c->srcW <= ABS(dstStride[0])
  1605. && length+c->srcW <= ABS(srcStride[0])) length+= c->srcW;
  1606. ASSERT(length!=0);
  1607. for(i=0; i<srcSliceH; i++)
  1608. {
  1609. memcpy(dstPtr, srcPtr, length);
  1610. srcPtr+= srcStride[0];
  1611. dstPtr+= dstStride[0];
  1612. }
  1613. }
  1614. }
  1615. else
  1616. { /* Planar YUV or gray */
  1617. int plane;
  1618. for(plane=0; plane<3; plane++)
  1619. {
  1620. int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1621. int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1622. int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1623. if((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
  1624. {
  1625. if(!isGray(c->dstFormat))
  1626. memset(dst[plane], 128, dstStride[plane]*height);
  1627. }
  1628. else
  1629. {
  1630. if(dstStride[plane]==srcStride[plane])
  1631. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1632. else
  1633. {
  1634. int i;
  1635. uint8_t *srcPtr= src[plane];
  1636. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1637. for(i=0; i<height; i++)
  1638. {
  1639. memcpy(dstPtr, srcPtr, length);
  1640. srcPtr+= srcStride[plane];
  1641. dstPtr+= dstStride[plane];
  1642. }
  1643. }
  1644. }
  1645. }
  1646. }
  1647. }
  1648. static int remove_dup_fourcc(int fourcc)
  1649. {
  1650. switch(fourcc)
  1651. {
  1652. case IMGFMT_IYUV: return IMGFMT_I420;
  1653. case IMGFMT_Y8 : return IMGFMT_Y800;
  1654. case IMGFMT_IF09: return IMGFMT_YVU9;
  1655. default: return fourcc;
  1656. }
  1657. }
  1658. static void getSubSampleFactors(int *h, int *v, int format){
  1659. switch(format){
  1660. case IMGFMT_YUY2:
  1661. *h=1;
  1662. *v=0;
  1663. break;
  1664. case IMGFMT_YV12:
  1665. case IMGFMT_I420:
  1666. case IMGFMT_Y800: //FIXME remove after different subsamplings are fully implemented
  1667. *h=1;
  1668. *v=1;
  1669. break;
  1670. case IMGFMT_YVU9:
  1671. *h=2;
  1672. *v=2;
  1673. break;
  1674. default:
  1675. *h=0;
  1676. *v=0;
  1677. break;
  1678. }
  1679. }
  1680. SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
  1681. SwsFilter *srcFilter, SwsFilter *dstFilter){
  1682. SwsContext *c;
  1683. int i;
  1684. int usesFilter;
  1685. int unscaled, needsDither;
  1686. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  1687. #ifdef ARCH_X86
  1688. if(gCpuCaps.hasMMX)
  1689. asm volatile("emms\n\t"::: "memory");
  1690. #endif
  1691. if(swScale==NULL) globalInit();
  1692. //srcFormat= IMGFMT_Y800;
  1693. //dstFormat= IMGFMT_Y800;
  1694. /* avoid dupplicate Formats, so we dont need to check to much */
  1695. srcFormat = remove_dup_fourcc(srcFormat);
  1696. dstFormat = remove_dup_fourcc(dstFormat);
  1697. unscaled = (srcW == dstW && srcH == dstH);
  1698. needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
  1699. && (dstFormat&0xFF)<24
  1700. && ((dstFormat&0xFF)<(srcFormat&0xFF) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
  1701. if(!isSupportedIn(srcFormat))
  1702. {
  1703. MSG_ERR("swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
  1704. return NULL;
  1705. }
  1706. if(!isSupportedOut(dstFormat))
  1707. {
  1708. MSG_ERR("swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
  1709. return NULL;
  1710. }
  1711. /* sanity check */
  1712. 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
  1713. {
  1714. MSG_ERR("swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  1715. srcW, srcH, dstW, dstH);
  1716. return NULL;
  1717. }
  1718. if(!dstFilter) dstFilter= &dummyFilter;
  1719. if(!srcFilter) srcFilter= &dummyFilter;
  1720. c= memalign(64, sizeof(SwsContext));
  1721. memset(c, 0, sizeof(SwsContext));
  1722. c->srcW= srcW;
  1723. c->srcH= srcH;
  1724. c->dstW= dstW;
  1725. c->dstH= dstH;
  1726. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  1727. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  1728. c->flags= flags;
  1729. c->dstFormat= dstFormat;
  1730. c->srcFormat= srcFormat;
  1731. usesFilter=0;
  1732. if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesFilter=1;
  1733. if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesFilter=1;
  1734. if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesFilter=1;
  1735. if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesFilter=1;
  1736. if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesFilter=1;
  1737. if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesFilter=1;
  1738. if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesFilter=1;
  1739. if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesFilter=1;
  1740. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  1741. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  1742. // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
  1743. if((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  1744. // drop some chroma lines if the user wants it
  1745. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  1746. c->chrSrcVSubSample+= c->vChrDrop;
  1747. // drop every 2. pixel for chroma calculation unless user wants full chroma
  1748. if((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP))
  1749. c->chrSrcHSubSample=1;
  1750. c->chrIntHSubSample= c->chrDstHSubSample;
  1751. c->chrIntVSubSample= c->chrSrcVSubSample;
  1752. // note the -((-x)>>y) is so that we allways round toward +inf
  1753. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  1754. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  1755. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  1756. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  1757. if(isBGR(dstFormat))
  1758. c->yuvTable= yuv2rgb_c_init(dstFormat & 0xFF, MODE_RGB, c->table_rV, c->table_gU, c->table_gV, c->table_bU);
  1759. if(isRGB(dstFormat))
  1760. c->yuvTable= yuv2rgb_c_init(dstFormat & 0xFF, MODE_BGR, c->table_rV, c->table_gU, c->table_gV, c->table_bU);
  1761. /* unscaled special Cases */
  1762. if(unscaled && !usesFilter)
  1763. {
  1764. /* yv12_to_nv12 */
  1765. if((srcFormat == IMGFMT_YV12||srcFormat==IMGFMT_I420)&&dstFormat == IMGFMT_NV12)
  1766. {
  1767. c->swScale= PlanarToNV12Wrapper;
  1768. if(flags&SWS_PRINT_INFO)
  1769. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1770. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1771. return c;
  1772. }
  1773. /* yv12_to_yuy2 */
  1774. if((srcFormat == IMGFMT_YV12||srcFormat==IMGFMT_I420)&&dstFormat == IMGFMT_YUY2)
  1775. {
  1776. c->swScale= PlanarToYuy2Wrapper;
  1777. if(flags&SWS_PRINT_INFO)
  1778. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1779. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1780. return c;
  1781. }
  1782. /* yuv2bgr */
  1783. if((srcFormat==IMGFMT_YV12 || srcFormat==IMGFMT_I420) && isBGR(dstFormat))
  1784. {
  1785. // FIXME multiple yuv2rgb converters wont work that way cuz that thing is full of globals&statics
  1786. //FIXME rgb vs. bgr ?
  1787. #ifdef WORDS_BIGENDIAN
  1788. if(dstFormat==IMGFMT_BGR32)
  1789. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_BGR);
  1790. else
  1791. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
  1792. #else
  1793. yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
  1794. #endif
  1795. c->swScale= planarYuvToBgr;
  1796. if(flags&SWS_PRINT_INFO)
  1797. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1798. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1799. return c;
  1800. }
  1801. /* simple copy */
  1802. if( srcFormat == dstFormat
  1803. || (srcFormat==IMGFMT_YV12 && dstFormat==IMGFMT_I420)
  1804. || (srcFormat==IMGFMT_I420 && dstFormat==IMGFMT_YV12)
  1805. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1806. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1807. )
  1808. {
  1809. c->swScale= simpleCopy;
  1810. if(flags&SWS_PRINT_INFO)
  1811. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1812. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1813. return c;
  1814. }
  1815. if( srcFormat==IMGFMT_YVU9 && (dstFormat==IMGFMT_YV12 || dstFormat==IMGFMT_I420) )
  1816. {
  1817. c->swScale= yvu9toyv12Wrapper;
  1818. if(flags&SWS_PRINT_INFO)
  1819. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1820. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1821. return c;
  1822. }
  1823. /* bgr24toYV12 */
  1824. if(srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_YV12)
  1825. c->swScale= bgr24toyv12Wrapper;
  1826. /* rgb/bgr -> rgb/bgr (no dither needed forms) */
  1827. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1828. && (isBGR(dstFormat) || isRGB(dstFormat))
  1829. && !needsDither)
  1830. c->swScale= rgb2rgbWrapper;
  1831. /* LQ converters if -sws 0 or -sws 4*/
  1832. if(c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
  1833. /* rgb/bgr -> rgb/bgr (dither needed forms) */
  1834. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1835. && (isBGR(dstFormat) || isRGB(dstFormat))
  1836. && needsDither)
  1837. c->swScale= rgb2rgbWrapper;
  1838. }
  1839. if(c->swScale){
  1840. if(flags&SWS_PRINT_INFO)
  1841. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1842. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1843. return c;
  1844. }
  1845. }
  1846. if(cpuCaps.hasMMX2)
  1847. {
  1848. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  1849. if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
  1850. {
  1851. if(flags&SWS_PRINT_INFO)
  1852. MSG_INFO("SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
  1853. }
  1854. }
  1855. else
  1856. c->canMMX2BeUsed=0;
  1857. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  1858. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  1859. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  1860. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  1861. // n-2 is the last chrominance sample available
  1862. // this is not perfect, but noone shuld notice the difference, the more correct variant
  1863. // would be like the vertical one, but that would require some special code for the
  1864. // first and last pixel
  1865. if(flags&SWS_FAST_BILINEAR)
  1866. {
  1867. if(c->canMMX2BeUsed)
  1868. {
  1869. c->lumXInc+= 20;
  1870. c->chrXInc+= 20;
  1871. }
  1872. //we dont use the x86asm scaler if mmx is available
  1873. else if(cpuCaps.hasMMX)
  1874. {
  1875. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  1876. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  1877. }
  1878. }
  1879. /* precalculate horizontal scaler filter coefficients */
  1880. {
  1881. const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
  1882. initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  1883. srcW , dstW, filterAlign, 1<<14,
  1884. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1885. srcFilter->lumH, dstFilter->lumH);
  1886. initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  1887. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  1888. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1889. srcFilter->chrH, dstFilter->chrH);
  1890. #ifdef ARCH_X86
  1891. // cant downscale !!!
  1892. if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
  1893. {
  1894. c->lumMmx2Filter = (int16_t*)memalign(8, (dstW /8+8)*sizeof(int16_t));
  1895. c->chrMmx2Filter = (int16_t*)memalign(8, (c->chrDstW /4+8)*sizeof(int16_t));
  1896. c->lumMmx2FilterPos= (int32_t*)memalign(8, (dstW /2/8+8)*sizeof(int32_t));
  1897. c->chrMmx2FilterPos= (int32_t*)memalign(8, (c->chrDstW/2/4+8)*sizeof(int32_t));
  1898. initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
  1899. initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
  1900. }
  1901. #endif
  1902. } // Init Horizontal stuff
  1903. /* precalculate vertical scaler filter coefficients */
  1904. initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  1905. srcH , dstH, 1, (1<<12)-4,
  1906. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1907. srcFilter->lumV, dstFilter->lumV);
  1908. initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  1909. c->chrSrcH, c->chrDstH, 1, (1<<12)-4,
  1910. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1911. srcFilter->chrV, dstFilter->chrV);
  1912. // Calculate Buffer Sizes so that they wont run out while handling these damn slices
  1913. c->vLumBufSize= c->vLumFilterSize;
  1914. c->vChrBufSize= c->vChrFilterSize;
  1915. for(i=0; i<dstH; i++)
  1916. {
  1917. int chrI= i*c->chrDstH / dstH;
  1918. int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  1919. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  1920. nextSlice&= ~3; // Slices start at boundaries which are divisable through 4
  1921. if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  1922. c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
  1923. if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  1924. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  1925. }
  1926. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  1927. c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
  1928. c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
  1929. //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)
  1930. for(i=0; i<c->vLumBufSize; i++)
  1931. c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
  1932. for(i=0; i<c->vChrBufSize; i++)
  1933. c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
  1934. //try to avoid drawing green stuff between the right end and the stride end
  1935. for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
  1936. for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
  1937. ASSERT(c->chrDstH <= dstH)
  1938. // pack filter data for mmx code
  1939. if(cpuCaps.hasMMX)
  1940. {
  1941. c->lumMmxFilter= (int16_t*)memalign(8, c->vLumFilterSize* dstH*4*sizeof(int16_t));
  1942. c->chrMmxFilter= (int16_t*)memalign(8, c->vChrFilterSize*c->chrDstH*4*sizeof(int16_t));
  1943. for(i=0; i<c->vLumFilterSize*dstH; i++)
  1944. c->lumMmxFilter[4*i]=c->lumMmxFilter[4*i+1]=c->lumMmxFilter[4*i+2]=c->lumMmxFilter[4*i+3]=
  1945. c->vLumFilter[i];
  1946. for(i=0; i<c->vChrFilterSize*c->chrDstH; i++)
  1947. c->chrMmxFilter[4*i]=c->chrMmxFilter[4*i+1]=c->chrMmxFilter[4*i+2]=c->chrMmxFilter[4*i+3]=
  1948. c->vChrFilter[i];
  1949. }
  1950. if(flags&SWS_PRINT_INFO)
  1951. {
  1952. #ifdef DITHER1XBPP
  1953. char *dither= " dithered";
  1954. #else
  1955. char *dither= "";
  1956. #endif
  1957. if(flags&SWS_FAST_BILINEAR)
  1958. MSG_INFO("\nSwScaler: FAST_BILINEAR scaler, ");
  1959. else if(flags&SWS_BILINEAR)
  1960. MSG_INFO("\nSwScaler: BILINEAR scaler, ");
  1961. else if(flags&SWS_BICUBIC)
  1962. MSG_INFO("\nSwScaler: BICUBIC scaler, ");
  1963. else if(flags&SWS_X)
  1964. MSG_INFO("\nSwScaler: Experimental scaler, ");
  1965. else if(flags&SWS_POINT)
  1966. MSG_INFO("\nSwScaler: Nearest Neighbor / POINT scaler, ");
  1967. else if(flags&SWS_AREA)
  1968. MSG_INFO("\nSwScaler: Area Averageing scaler, ");
  1969. else if(flags&SWS_BICUBLIN)
  1970. MSG_INFO("\nSwScaler: luma BICUBIC / chroma BILINEAR scaler, ");
  1971. else if(flags&SWS_GAUSS)
  1972. MSG_INFO("\nSwScaler: Gaussian scaler, ");
  1973. else if(flags&SWS_SINC)
  1974. MSG_INFO("\nSwScaler: Sinc scaler, ");
  1975. else if(flags&SWS_LANCZOS)
  1976. MSG_INFO("\nSwScaler: Lanczos scaler, ");
  1977. else if(flags&SWS_SPLINE)
  1978. MSG_INFO("\nSwScaler: Bicubic spline scaler, ");
  1979. else
  1980. MSG_INFO("\nSwScaler: ehh flags invalid?! ");
  1981. if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
  1982. MSG_INFO("from %s to%s %s ",
  1983. vo_format_name(srcFormat), dither, vo_format_name(dstFormat));
  1984. else
  1985. MSG_INFO("from %s to %s ",
  1986. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1987. if(cpuCaps.hasMMX2)
  1988. MSG_INFO("using MMX2\n");
  1989. else if(cpuCaps.has3DNow)
  1990. MSG_INFO("using 3DNOW\n");
  1991. else if(cpuCaps.hasMMX)
  1992. MSG_INFO("using MMX\n");
  1993. else
  1994. MSG_INFO("using C\n");
  1995. }
  1996. if((flags & SWS_PRINT_INFO) && verbose)
  1997. {
  1998. if(cpuCaps.hasMMX)
  1999. {
  2000. if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  2001. MSG_V("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  2002. else
  2003. {
  2004. if(c->hLumFilterSize==4)
  2005. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
  2006. else if(c->hLumFilterSize==8)
  2007. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
  2008. else
  2009. MSG_V("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
  2010. if(c->hChrFilterSize==4)
  2011. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
  2012. else if(c->hChrFilterSize==8)
  2013. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
  2014. else
  2015. MSG_V("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
  2016. }
  2017. }
  2018. else
  2019. {
  2020. #ifdef ARCH_X86
  2021. MSG_V("SwScaler: using X86-Asm scaler for horizontal scaling\n");
  2022. #else
  2023. if(flags & SWS_FAST_BILINEAR)
  2024. MSG_V("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
  2025. else
  2026. MSG_V("SwScaler: using C scaler for horizontal scaling\n");
  2027. #endif
  2028. }
  2029. if(isPlanarYUV(dstFormat))
  2030. {
  2031. if(c->vLumFilterSize==1)
  2032. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
  2033. else
  2034. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
  2035. }
  2036. else
  2037. {
  2038. if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
  2039. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  2040. "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
  2041. else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
  2042. MSG_V("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
  2043. else
  2044. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
  2045. }
  2046. if(dstFormat==IMGFMT_BGR24)
  2047. MSG_V("SwScaler: using %s YV12->BGR24 Converter\n",
  2048. cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
  2049. else if(dstFormat==IMGFMT_BGR32)
  2050. MSG_V("SwScaler: using %s YV12->BGR32 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  2051. else if(dstFormat==IMGFMT_BGR16)
  2052. MSG_V("SwScaler: using %s YV12->BGR16 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  2053. else if(dstFormat==IMGFMT_BGR15)
  2054. MSG_V("SwScaler: using %s YV12->BGR15 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
  2055. MSG_V("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  2056. }
  2057. if((flags & SWS_PRINT_INFO) && verbose>1)
  2058. {
  2059. MSG_DBG2("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2060. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  2061. MSG_DBG2("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2062. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  2063. }
  2064. c->swScale= swScale;
  2065. return c;
  2066. }
  2067. /**
  2068. * returns a normalized gaussian curve used to filter stuff
  2069. * quality=3 is high quality, lowwer is lowwer quality
  2070. */
  2071. SwsVector *getGaussianVec(double variance, double quality){
  2072. const int length= (int)(variance*quality + 0.5) | 1;
  2073. int i;
  2074. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2075. double middle= (length-1)*0.5;
  2076. SwsVector *vec= malloc(sizeof(SwsVector));
  2077. vec->coeff= coeff;
  2078. vec->length= length;
  2079. for(i=0; i<length; i++)
  2080. {
  2081. double dist= i-middle;
  2082. coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
  2083. }
  2084. normalizeVec(vec, 1.0);
  2085. return vec;
  2086. }
  2087. SwsVector *getConstVec(double c, int length){
  2088. int i;
  2089. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2090. SwsVector *vec= malloc(sizeof(SwsVector));
  2091. vec->coeff= coeff;
  2092. vec->length= length;
  2093. for(i=0; i<length; i++)
  2094. coeff[i]= c;
  2095. return vec;
  2096. }
  2097. SwsVector *getIdentityVec(void){
  2098. double *coeff= memalign(sizeof(double), sizeof(double));
  2099. SwsVector *vec= malloc(sizeof(SwsVector));
  2100. coeff[0]= 1.0;
  2101. vec->coeff= coeff;
  2102. vec->length= 1;
  2103. return vec;
  2104. }
  2105. void normalizeVec(SwsVector *a, double height){
  2106. int i;
  2107. double sum=0;
  2108. double inv;
  2109. for(i=0; i<a->length; i++)
  2110. sum+= a->coeff[i];
  2111. inv= height/sum;
  2112. for(i=0; i<a->length; i++)
  2113. a->coeff[i]*= height;
  2114. }
  2115. void scaleVec(SwsVector *a, double scalar){
  2116. int i;
  2117. for(i=0; i<a->length; i++)
  2118. a->coeff[i]*= scalar;
  2119. }
  2120. static SwsVector *getConvVec(SwsVector *a, SwsVector *b){
  2121. int length= a->length + b->length - 1;
  2122. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2123. int i, j;
  2124. SwsVector *vec= malloc(sizeof(SwsVector));
  2125. vec->coeff= coeff;
  2126. vec->length= length;
  2127. for(i=0; i<length; i++) coeff[i]= 0.0;
  2128. for(i=0; i<a->length; i++)
  2129. {
  2130. for(j=0; j<b->length; j++)
  2131. {
  2132. coeff[i+j]+= a->coeff[i]*b->coeff[j];
  2133. }
  2134. }
  2135. return vec;
  2136. }
  2137. static SwsVector *sumVec(SwsVector *a, SwsVector *b){
  2138. int length= MAX(a->length, b->length);
  2139. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2140. int i;
  2141. SwsVector *vec= malloc(sizeof(SwsVector));
  2142. vec->coeff= coeff;
  2143. vec->length= length;
  2144. for(i=0; i<length; i++) coeff[i]= 0.0;
  2145. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2146. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  2147. return vec;
  2148. }
  2149. static SwsVector *diffVec(SwsVector *a, SwsVector *b){
  2150. int length= MAX(a->length, b->length);
  2151. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2152. int i;
  2153. SwsVector *vec= malloc(sizeof(SwsVector));
  2154. vec->coeff= coeff;
  2155. vec->length= length;
  2156. for(i=0; i<length; i++) coeff[i]= 0.0;
  2157. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2158. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  2159. return vec;
  2160. }
  2161. /* shift left / or right if "shift" is negative */
  2162. static SwsVector *getShiftedVec(SwsVector *a, int shift){
  2163. int length= a->length + ABS(shift)*2;
  2164. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2165. int i;
  2166. SwsVector *vec= malloc(sizeof(SwsVector));
  2167. vec->coeff= coeff;
  2168. vec->length= length;
  2169. for(i=0; i<length; i++) coeff[i]= 0.0;
  2170. for(i=0; i<a->length; i++)
  2171. {
  2172. coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  2173. }
  2174. return vec;
  2175. }
  2176. void shiftVec(SwsVector *a, int shift){
  2177. SwsVector *shifted= getShiftedVec(a, shift);
  2178. free(a->coeff);
  2179. a->coeff= shifted->coeff;
  2180. a->length= shifted->length;
  2181. free(shifted);
  2182. }
  2183. void addVec(SwsVector *a, SwsVector *b){
  2184. SwsVector *sum= sumVec(a, b);
  2185. free(a->coeff);
  2186. a->coeff= sum->coeff;
  2187. a->length= sum->length;
  2188. free(sum);
  2189. }
  2190. void subVec(SwsVector *a, SwsVector *b){
  2191. SwsVector *diff= diffVec(a, b);
  2192. free(a->coeff);
  2193. a->coeff= diff->coeff;
  2194. a->length= diff->length;
  2195. free(diff);
  2196. }
  2197. void convVec(SwsVector *a, SwsVector *b){
  2198. SwsVector *conv= getConvVec(a, b);
  2199. free(a->coeff);
  2200. a->coeff= conv->coeff;
  2201. a->length= conv->length;
  2202. free(conv);
  2203. }
  2204. SwsVector *cloneVec(SwsVector *a){
  2205. double *coeff= memalign(sizeof(double), a->length*sizeof(double));
  2206. int i;
  2207. SwsVector *vec= malloc(sizeof(SwsVector));
  2208. vec->coeff= coeff;
  2209. vec->length= a->length;
  2210. for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
  2211. return vec;
  2212. }
  2213. void printVec(SwsVector *a){
  2214. int i;
  2215. double max=0;
  2216. double min=0;
  2217. double range;
  2218. for(i=0; i<a->length; i++)
  2219. if(a->coeff[i]>max) max= a->coeff[i];
  2220. for(i=0; i<a->length; i++)
  2221. if(a->coeff[i]<min) min= a->coeff[i];
  2222. range= max - min;
  2223. for(i=0; i<a->length; i++)
  2224. {
  2225. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  2226. MSG_DBG2("%1.3f ", a->coeff[i]);
  2227. for(;x>0; x--) MSG_DBG2(" ");
  2228. MSG_DBG2("|\n");
  2229. }
  2230. }
  2231. void freeVec(SwsVector *a){
  2232. if(!a) return;
  2233. if(a->coeff) free(a->coeff);
  2234. a->coeff=NULL;
  2235. a->length=0;
  2236. free(a);
  2237. }
  2238. void freeSwsContext(SwsContext *c){
  2239. int i;
  2240. if(!c) return;
  2241. if(c->lumPixBuf)
  2242. {
  2243. for(i=0; i<c->vLumBufSize; i++)
  2244. {
  2245. if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
  2246. c->lumPixBuf[i]=NULL;
  2247. }
  2248. free(c->lumPixBuf);
  2249. c->lumPixBuf=NULL;
  2250. }
  2251. if(c->chrPixBuf)
  2252. {
  2253. for(i=0; i<c->vChrBufSize; i++)
  2254. {
  2255. if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
  2256. c->chrPixBuf[i]=NULL;
  2257. }
  2258. free(c->chrPixBuf);
  2259. c->chrPixBuf=NULL;
  2260. }
  2261. if(c->vLumFilter) free(c->vLumFilter);
  2262. c->vLumFilter = NULL;
  2263. if(c->vChrFilter) free(c->vChrFilter);
  2264. c->vChrFilter = NULL;
  2265. if(c->hLumFilter) free(c->hLumFilter);
  2266. c->hLumFilter = NULL;
  2267. if(c->hChrFilter) free(c->hChrFilter);
  2268. c->hChrFilter = NULL;
  2269. if(c->vLumFilterPos) free(c->vLumFilterPos);
  2270. c->vLumFilterPos = NULL;
  2271. if(c->vChrFilterPos) free(c->vChrFilterPos);
  2272. c->vChrFilterPos = NULL;
  2273. if(c->hLumFilterPos) free(c->hLumFilterPos);
  2274. c->hLumFilterPos = NULL;
  2275. if(c->hChrFilterPos) free(c->hChrFilterPos);
  2276. c->hChrFilterPos = NULL;
  2277. if(c->lumMmxFilter) free(c->lumMmxFilter);
  2278. c->lumMmxFilter = NULL;
  2279. if(c->chrMmxFilter) free(c->chrMmxFilter);
  2280. c->chrMmxFilter = NULL;
  2281. if(c->lumMmx2Filter) free(c->lumMmx2Filter);
  2282. c->lumMmx2Filter=NULL;
  2283. if(c->chrMmx2Filter) free(c->chrMmx2Filter);
  2284. c->chrMmx2Filter=NULL;
  2285. if(c->lumMmx2FilterPos) free(c->lumMmx2FilterPos);
  2286. c->lumMmx2FilterPos=NULL;
  2287. if(c->chrMmx2FilterPos) free(c->chrMmx2FilterPos);
  2288. c->chrMmx2FilterPos=NULL;
  2289. if(c->yuvTable) free(c->yuvTable);
  2290. c->yuvTable=NULL;
  2291. free(c);
  2292. }