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.

2754 lines
78KB

  1. /*
  2. Copyright (C) 2001-2003 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. the C code (not assembly, mmx, ...) of the swscaler which has been written
  15. by Michael Niedermayer can be used under the LGPL license too
  16. */
  17. /*
  18. supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09
  19. supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  20. {BGR,RGB}{1,4,8,15,16} support dithering
  21. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  22. YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
  23. x -> x
  24. YUV9 -> YV12
  25. YUV9/YV12 -> Y800
  26. Y800 -> YUV9/YV12
  27. BGR24 -> BGR32 & RGB24 -> RGB32
  28. BGR32 -> BGR24 & RGB32 -> RGB24
  29. BGR15 -> BGR16
  30. */
  31. /*
  32. tested special converters (most are tested actually but i didnt write it down ...)
  33. YV12 -> BGR16
  34. YV12 -> YV12
  35. BGR15 -> BGR16
  36. BGR16 -> BGR16
  37. YVU9 -> YV12
  38. untested special converters
  39. YV12/I420 -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
  40. YV12/I420 -> YV12/I420
  41. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  42. BGR24 -> BGR32 & RGB24 -> RGB32
  43. BGR32 -> BGR24 & RGB32 -> RGB24
  44. BGR24 -> YV12
  45. */
  46. #include <inttypes.h>
  47. #include <string.h>
  48. #include <math.h>
  49. #include <stdio.h>
  50. #include <unistd.h>
  51. #include "config.h"
  52. #include <assert.h>
  53. #ifdef HAVE_MALLOC_H
  54. #include <malloc.h>
  55. #else
  56. #include <stdlib.h>
  57. #endif
  58. #ifdef HAVE_SYS_MMAN_H
  59. #include <sys/mman.h>
  60. #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  61. #define MAP_ANONYMOUS MAP_ANON
  62. #endif
  63. #endif
  64. #include "swscale.h"
  65. #include "swscale_internal.h"
  66. #include "x86_cpu.h"
  67. #include "bswap.h"
  68. #include "rgb2rgb.h"
  69. #ifdef USE_FASTMEMCPY
  70. #include "libvo/fastmemcpy.h"
  71. #endif
  72. #undef MOVNTQ
  73. #undef PAVGB
  74. //#undef HAVE_MMX2
  75. //#define HAVE_3DNOW
  76. //#undef HAVE_MMX
  77. //#undef ARCH_X86
  78. //#define WORDS_BIGENDIAN
  79. #define DITHER1XBPP
  80. #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
  81. #define RET 0xC3 //near return opcode for X86
  82. #ifdef MP_DEBUG
  83. #define ASSERT(x) assert(x);
  84. #else
  85. #define ASSERT(x) ;
  86. #endif
  87. #ifdef M_PI
  88. #define PI M_PI
  89. #else
  90. #define PI 3.14159265358979323846
  91. #endif
  92. #define isSupportedIn(x) ((x)==PIX_FMT_YUV420P || (x)==PIX_FMT_YUYV422 || (x)==PIX_FMT_UYVY422\
  93. || (x)==PIX_FMT_RGB32|| (x)==PIX_FMT_BGR24|| (x)==PIX_FMT_BGR565|| (x)==PIX_FMT_BGR555\
  94. || (x)==PIX_FMT_BGR32|| (x)==PIX_FMT_RGB24\
  95. || (x)==PIX_FMT_GRAY8 || (x)==PIX_FMT_YUV410P\
  96. || (x)==PIX_FMT_YUV444P || (x)==PIX_FMT_YUV422P || (x)==PIX_FMT_YUV411P)
  97. #define isSupportedOut(x) ((x)==PIX_FMT_YUV420P || (x)==PIX_FMT_YUYV422 || (x)==PIX_FMT_UYVY422\
  98. || (x)==PIX_FMT_YUV444P || (x)==PIX_FMT_YUV422P || (x)==PIX_FMT_YUV411P\
  99. || isRGB(x) || isBGR(x)\
  100. || (x)==PIX_FMT_NV12 || (x)==PIX_FMT_NV21\
  101. || (x)==PIX_FMT_GRAY8 || (x)==PIX_FMT_YUV410P)
  102. #define isPacked(x) ((x)==PIX_FMT_YUYV422 || (x)==PIX_FMT_UYVY422 ||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 const int32_t Inverse_Table_6_9[8][4];
  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. */
  127. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  128. static uint64_t attribute_used __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
  129. static uint64_t attribute_used __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
  130. static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
  131. static uint64_t attribute_used __attribute__((aligned(8))) w02= 0x0002000200020002LL;
  132. static uint64_t attribute_used __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
  133. static uint64_t attribute_used __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
  134. static uint64_t attribute_used __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
  135. static uint64_t attribute_used __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
  136. static volatile uint64_t attribute_used __attribute__((aligned(8))) b5Dither;
  137. static volatile uint64_t attribute_used __attribute__((aligned(8))) g5Dither;
  138. static volatile uint64_t attribute_used __attribute__((aligned(8))) g6Dither;
  139. static volatile uint64_t attribute_used __attribute__((aligned(8))) r5Dither;
  140. static uint64_t __attribute__((aligned(8))) dither4[2]={
  141. 0x0103010301030103LL,
  142. 0x0200020002000200LL,};
  143. static uint64_t __attribute__((aligned(8))) dither8[2]={
  144. 0x0602060206020602LL,
  145. 0x0004000400040004LL,};
  146. static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
  147. static uint64_t attribute_used __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
  148. static uint64_t attribute_used __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
  149. static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
  150. static uint64_t attribute_used __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
  151. static uint64_t attribute_used __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
  152. static uint64_t attribute_used __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
  153. static uint64_t attribute_used __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
  154. static uint64_t attribute_used __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
  155. #ifdef FAST_BGR2YV12
  156. static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
  157. static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
  158. static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
  159. #else
  160. static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
  161. static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
  162. static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
  163. #endif /* FAST_BGR2YV12 */
  164. static const uint64_t bgr2YOffset attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
  165. static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8)))= 0x8080808080808080ULL;
  166. static const uint64_t w1111 attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
  167. #endif /* defined(ARCH_X86) || defined(ARCH_X86_64) */
  168. // clipping helper table for C implementations:
  169. static unsigned char clip_table[768];
  170. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
  171. extern const uint8_t dither_2x2_4[2][8];
  172. extern const uint8_t dither_2x2_8[2][8];
  173. extern const uint8_t dither_8x8_32[8][8];
  174. extern const uint8_t dither_8x8_73[8][8];
  175. extern const uint8_t dither_8x8_220[8][8];
  176. char *sws_format_name(enum PixelFormat format)
  177. {
  178. switch (format) {
  179. case PIX_FMT_YUV420P:
  180. return "yuv420p";
  181. case PIX_FMT_YUYV422:
  182. return "yuyv422";
  183. case PIX_FMT_RGB24:
  184. return "rgb24";
  185. case PIX_FMT_BGR24:
  186. return "bgr24";
  187. case PIX_FMT_YUV422P:
  188. return "yuv422p";
  189. case PIX_FMT_YUV444P:
  190. return "yuv444p";
  191. case PIX_FMT_RGB32:
  192. return "rgb32";
  193. case PIX_FMT_YUV410P:
  194. return "yuv410p";
  195. case PIX_FMT_YUV411P:
  196. return "yuv411p";
  197. case PIX_FMT_RGB565:
  198. return "rgb565";
  199. case PIX_FMT_RGB555:
  200. return "rgb555";
  201. case PIX_FMT_GRAY8:
  202. return "gray8";
  203. case PIX_FMT_MONOWHITE:
  204. return "mono white";
  205. case PIX_FMT_MONOBLACK:
  206. return "mono black";
  207. case PIX_FMT_PAL8:
  208. return "Palette";
  209. case PIX_FMT_YUVJ420P:
  210. return "yuvj420p";
  211. case PIX_FMT_YUVJ422P:
  212. return "yuvj422p";
  213. case PIX_FMT_YUVJ444P:
  214. return "yuvj444p";
  215. case PIX_FMT_XVMC_MPEG2_MC:
  216. return "xvmc_mpeg2_mc";
  217. case PIX_FMT_XVMC_MPEG2_IDCT:
  218. return "xvmc_mpeg2_idct";
  219. case PIX_FMT_UYVY422:
  220. return "uyvy422";
  221. case PIX_FMT_UYYVYY411:
  222. return "uyyvyy411";
  223. case PIX_FMT_RGB32_1:
  224. return "rgb32x";
  225. case PIX_FMT_BGR32_1:
  226. return "bgr32x";
  227. case PIX_FMT_BGR32:
  228. return "bgr32";
  229. case PIX_FMT_BGR565:
  230. return "bgr565";
  231. case PIX_FMT_BGR555:
  232. return "bgr555";
  233. case PIX_FMT_BGR8:
  234. return "bgr8";
  235. case PIX_FMT_BGR4:
  236. return "bgr4";
  237. case PIX_FMT_BGR4_BYTE:
  238. return "bgr4 byte";
  239. case PIX_FMT_RGB8:
  240. return "rgb8";
  241. case PIX_FMT_RGB4:
  242. return "rgb4";
  243. case PIX_FMT_RGB4_BYTE:
  244. return "rgb4 byte";
  245. case PIX_FMT_NV12:
  246. return "nv12";
  247. case PIX_FMT_NV21:
  248. return "nv21";
  249. default:
  250. return "Unknown format";
  251. }
  252. }
  253. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  254. void in_asm_used_var_warning_killer()
  255. {
  256. volatile int i= bF8+bFC+w10+
  257. bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+
  258. M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
  259. if(i) i=0;
  260. }
  261. #endif
  262. static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  263. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  264. uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
  265. {
  266. //FIXME Optimize (just quickly writen not opti..)
  267. int i;
  268. for(i=0; i<dstW; i++)
  269. {
  270. int val=1<<18;
  271. int j;
  272. for(j=0; j<lumFilterSize; j++)
  273. val += lumSrc[j][i] * lumFilter[j];
  274. dest[i]= FFMIN(FFMAX(val>>19, 0), 255);
  275. }
  276. if(uDest != NULL)
  277. for(i=0; i<chrDstW; i++)
  278. {
  279. int u=1<<18;
  280. int v=1<<18;
  281. int j;
  282. for(j=0; j<chrFilterSize; j++)
  283. {
  284. u += chrSrc[j][i] * chrFilter[j];
  285. v += chrSrc[j][i + 2048] * chrFilter[j];
  286. }
  287. uDest[i]= FFMIN(FFMAX(u>>19, 0), 255);
  288. vDest[i]= FFMIN(FFMAX(v>>19, 0), 255);
  289. }
  290. }
  291. static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  292. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  293. uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
  294. {
  295. //FIXME Optimize (just quickly writen not opti..)
  296. int i;
  297. for(i=0; i<dstW; i++)
  298. {
  299. int val=1<<18;
  300. int j;
  301. for(j=0; j<lumFilterSize; j++)
  302. val += lumSrc[j][i] * lumFilter[j];
  303. dest[i]= FFMIN(FFMAX(val>>19, 0), 255);
  304. }
  305. if(uDest == NULL)
  306. return;
  307. if(dstFormat == PIX_FMT_NV12)
  308. for(i=0; i<chrDstW; i++)
  309. {
  310. int u=1<<18;
  311. int v=1<<18;
  312. int j;
  313. for(j=0; j<chrFilterSize; j++)
  314. {
  315. u += chrSrc[j][i] * chrFilter[j];
  316. v += chrSrc[j][i + 2048] * chrFilter[j];
  317. }
  318. uDest[2*i]= FFMIN(FFMAX(u>>19, 0), 255);
  319. uDest[2*i+1]= FFMIN(FFMAX(v>>19, 0), 255);
  320. }
  321. else
  322. for(i=0; i<chrDstW; i++)
  323. {
  324. int u=1<<18;
  325. int v=1<<18;
  326. int j;
  327. for(j=0; j<chrFilterSize; j++)
  328. {
  329. u += chrSrc[j][i] * chrFilter[j];
  330. v += chrSrc[j][i + 2048] * chrFilter[j];
  331. }
  332. uDest[2*i]= FFMIN(FFMAX(v>>19, 0), 255);
  333. uDest[2*i+1]= FFMIN(FFMAX(u>>19, 0), 255);
  334. }
  335. }
  336. #define YSCALE_YUV_2_PACKEDX_C(type) \
  337. for(i=0; i<(dstW>>1); i++){\
  338. int j;\
  339. int Y1=1<<18;\
  340. int Y2=1<<18;\
  341. int U=1<<18;\
  342. int V=1<<18;\
  343. type *r, *b, *g;\
  344. const int i2= 2*i;\
  345. \
  346. for(j=0; j<lumFilterSize; j++)\
  347. {\
  348. Y1 += lumSrc[j][i2] * lumFilter[j];\
  349. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  350. }\
  351. for(j=0; j<chrFilterSize; j++)\
  352. {\
  353. U += chrSrc[j][i] * chrFilter[j];\
  354. V += chrSrc[j][i+2048] * chrFilter[j];\
  355. }\
  356. Y1>>=19;\
  357. Y2>>=19;\
  358. U >>=19;\
  359. V >>=19;\
  360. if((Y1|Y2|U|V)&256)\
  361. {\
  362. if(Y1>255) Y1=255;\
  363. else if(Y1<0)Y1=0;\
  364. if(Y2>255) Y2=255;\
  365. else if(Y2<0)Y2=0;\
  366. if(U>255) U=255;\
  367. else if(U<0) U=0;\
  368. if(V>255) V=255;\
  369. else if(V<0) V=0;\
  370. }
  371. #define YSCALE_YUV_2_RGBX_C(type) \
  372. YSCALE_YUV_2_PACKEDX_C(type)\
  373. r = c->table_rV[V];\
  374. g = c->table_gU[U] + c->table_gV[V];\
  375. b = c->table_bU[U];\
  376. #define YSCALE_YUV_2_PACKED2_C \
  377. for(i=0; i<(dstW>>1); i++){\
  378. const int i2= 2*i;\
  379. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19;\
  380. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;\
  381. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19;\
  382. int V= (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;\
  383. #define YSCALE_YUV_2_RGB2_C(type) \
  384. YSCALE_YUV_2_PACKED2_C\
  385. type *r, *b, *g;\
  386. r = c->table_rV[V];\
  387. g = c->table_gU[U] + c->table_gV[V];\
  388. b = c->table_bU[U];\
  389. #define YSCALE_YUV_2_PACKED1_C \
  390. for(i=0; i<(dstW>>1); i++){\
  391. const int i2= 2*i;\
  392. int Y1= buf0[i2 ]>>7;\
  393. int Y2= buf0[i2+1]>>7;\
  394. int U= (uvbuf1[i ])>>7;\
  395. int V= (uvbuf1[i+2048])>>7;\
  396. #define YSCALE_YUV_2_RGB1_C(type) \
  397. YSCALE_YUV_2_PACKED1_C\
  398. type *r, *b, *g;\
  399. r = c->table_rV[V];\
  400. g = c->table_gU[U] + c->table_gV[V];\
  401. b = c->table_bU[U];\
  402. #define YSCALE_YUV_2_PACKED1B_C \
  403. for(i=0; i<(dstW>>1); i++){\
  404. const int i2= 2*i;\
  405. int Y1= buf0[i2 ]>>7;\
  406. int Y2= buf0[i2+1]>>7;\
  407. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  408. int V= (uvbuf0[i+2048] + uvbuf1[i+2048])>>8;\
  409. #define YSCALE_YUV_2_RGB1B_C(type) \
  410. YSCALE_YUV_2_PACKED1B_C\
  411. type *r, *b, *g;\
  412. r = c->table_rV[V];\
  413. g = c->table_gU[U] + c->table_gV[V];\
  414. b = c->table_bU[U];\
  415. #define YSCALE_YUV_2_ANYRGB_C(func, func2)\
  416. switch(c->dstFormat)\
  417. {\
  418. case PIX_FMT_RGB32:\
  419. case PIX_FMT_BGR32:\
  420. func(uint32_t)\
  421. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  422. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  423. } \
  424. break;\
  425. case PIX_FMT_RGB24:\
  426. func(uint8_t)\
  427. ((uint8_t*)dest)[0]= r[Y1];\
  428. ((uint8_t*)dest)[1]= g[Y1];\
  429. ((uint8_t*)dest)[2]= b[Y1];\
  430. ((uint8_t*)dest)[3]= r[Y2];\
  431. ((uint8_t*)dest)[4]= g[Y2];\
  432. ((uint8_t*)dest)[5]= b[Y2];\
  433. dest+=6;\
  434. }\
  435. break;\
  436. case PIX_FMT_BGR24:\
  437. func(uint8_t)\
  438. ((uint8_t*)dest)[0]= b[Y1];\
  439. ((uint8_t*)dest)[1]= g[Y1];\
  440. ((uint8_t*)dest)[2]= r[Y1];\
  441. ((uint8_t*)dest)[3]= b[Y2];\
  442. ((uint8_t*)dest)[4]= g[Y2];\
  443. ((uint8_t*)dest)[5]= r[Y2];\
  444. dest+=6;\
  445. }\
  446. break;\
  447. case PIX_FMT_RGB565:\
  448. case PIX_FMT_BGR565:\
  449. {\
  450. const int dr1= dither_2x2_8[y&1 ][0];\
  451. const int dg1= dither_2x2_4[y&1 ][0];\
  452. const int db1= dither_2x2_8[(y&1)^1][0];\
  453. const int dr2= dither_2x2_8[y&1 ][1];\
  454. const int dg2= dither_2x2_4[y&1 ][1];\
  455. const int db2= dither_2x2_8[(y&1)^1][1];\
  456. func(uint16_t)\
  457. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  458. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  459. }\
  460. }\
  461. break;\
  462. case PIX_FMT_RGB555:\
  463. case PIX_FMT_BGR555:\
  464. {\
  465. const int dr1= dither_2x2_8[y&1 ][0];\
  466. const int dg1= dither_2x2_8[y&1 ][1];\
  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_8[y&1 ][0];\
  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 PIX_FMT_RGB8:\
  478. case PIX_FMT_BGR8:\
  479. {\
  480. const uint8_t * const d64= dither_8x8_73[y&7];\
  481. const uint8_t * const d32= dither_8x8_32[y&7];\
  482. func(uint8_t)\
  483. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  484. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  485. }\
  486. }\
  487. break;\
  488. case PIX_FMT_RGB4:\
  489. case PIX_FMT_BGR4:\
  490. {\
  491. const uint8_t * const d64= dither_8x8_73 [y&7];\
  492. const uint8_t * const d128=dither_8x8_220[y&7];\
  493. func(uint8_t)\
  494. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  495. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  496. }\
  497. }\
  498. break;\
  499. case PIX_FMT_RGB4_BYTE:\
  500. case PIX_FMT_BGR4_BYTE:\
  501. {\
  502. const uint8_t * const d64= dither_8x8_73 [y&7];\
  503. const uint8_t * const d128=dither_8x8_220[y&7];\
  504. func(uint8_t)\
  505. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  506. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  507. }\
  508. }\
  509. break;\
  510. case PIX_FMT_MONOBLACK:\
  511. {\
  512. const uint8_t * const d128=dither_8x8_220[y&7];\
  513. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  514. for(i=0; i<dstW-7; i+=8){\
  515. int acc;\
  516. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  517. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  518. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  519. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  520. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  521. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  522. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  523. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  524. ((uint8_t*)dest)[0]= acc;\
  525. dest++;\
  526. }\
  527. \
  528. /*\
  529. ((uint8_t*)dest)-= dstW>>4;\
  530. {\
  531. int acc=0;\
  532. int left=0;\
  533. static int top[1024];\
  534. static int last_new[1024][1024];\
  535. static int last_in3[1024][1024];\
  536. static int drift[1024][1024];\
  537. int topLeft=0;\
  538. int shift=0;\
  539. int count=0;\
  540. const uint8_t * const d128=dither_8x8_220[y&7];\
  541. int error_new=0;\
  542. int error_in3=0;\
  543. int f=0;\
  544. \
  545. for(i=dstW>>1; i<dstW; i++){\
  546. int in= ((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19);\
  547. int in2 = (76309 * (in - 16) + 32768) >> 16;\
  548. int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
  549. int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
  550. + (last_new[y][i] - in3)*f/256;\
  551. int new= old> 128 ? 255 : 0;\
  552. \
  553. error_new+= ABS(last_new[y][i] - new);\
  554. error_in3+= ABS(last_in3[y][i] - in3);\
  555. f= error_new - error_in3*4;\
  556. if(f<0) f=0;\
  557. if(f>256) f=256;\
  558. \
  559. topLeft= top[i];\
  560. left= top[i]= old - new;\
  561. last_new[y][i]= new;\
  562. last_in3[y][i]= in3;\
  563. \
  564. acc+= acc + (new&1);\
  565. if((i&7)==6){\
  566. ((uint8_t*)dest)[0]= acc;\
  567. ((uint8_t*)dest)++;\
  568. }\
  569. }\
  570. }\
  571. */\
  572. }\
  573. break;\
  574. case PIX_FMT_YUYV422:\
  575. func2\
  576. ((uint8_t*)dest)[2*i2+0]= Y1;\
  577. ((uint8_t*)dest)[2*i2+1]= U;\
  578. ((uint8_t*)dest)[2*i2+2]= Y2;\
  579. ((uint8_t*)dest)[2*i2+3]= V;\
  580. } \
  581. break;\
  582. case PIX_FMT_UYVY422:\
  583. func2\
  584. ((uint8_t*)dest)[2*i2+0]= U;\
  585. ((uint8_t*)dest)[2*i2+1]= Y1;\
  586. ((uint8_t*)dest)[2*i2+2]= V;\
  587. ((uint8_t*)dest)[2*i2+3]= Y2;\
  588. } \
  589. break;\
  590. }\
  591. static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  592. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  593. uint8_t *dest, int dstW, int y)
  594. {
  595. int i;
  596. switch(c->dstFormat)
  597. {
  598. case PIX_FMT_BGR32:
  599. case PIX_FMT_RGB32:
  600. YSCALE_YUV_2_RGBX_C(uint32_t)
  601. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];
  602. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];
  603. }
  604. break;
  605. case PIX_FMT_RGB24:
  606. YSCALE_YUV_2_RGBX_C(uint8_t)
  607. ((uint8_t*)dest)[0]= r[Y1];
  608. ((uint8_t*)dest)[1]= g[Y1];
  609. ((uint8_t*)dest)[2]= b[Y1];
  610. ((uint8_t*)dest)[3]= r[Y2];
  611. ((uint8_t*)dest)[4]= g[Y2];
  612. ((uint8_t*)dest)[5]= b[Y2];
  613. dest+=6;
  614. }
  615. break;
  616. case PIX_FMT_BGR24:
  617. YSCALE_YUV_2_RGBX_C(uint8_t)
  618. ((uint8_t*)dest)[0]= b[Y1];
  619. ((uint8_t*)dest)[1]= g[Y1];
  620. ((uint8_t*)dest)[2]= r[Y1];
  621. ((uint8_t*)dest)[3]= b[Y2];
  622. ((uint8_t*)dest)[4]= g[Y2];
  623. ((uint8_t*)dest)[5]= r[Y2];
  624. dest+=6;
  625. }
  626. break;
  627. case PIX_FMT_RGB565:
  628. case PIX_FMT_BGR565:
  629. {
  630. const int dr1= dither_2x2_8[y&1 ][0];
  631. const int dg1= dither_2x2_4[y&1 ][0];
  632. const int db1= dither_2x2_8[(y&1)^1][0];
  633. const int dr2= dither_2x2_8[y&1 ][1];
  634. const int dg2= dither_2x2_4[y&1 ][1];
  635. const int db2= dither_2x2_8[(y&1)^1][1];
  636. YSCALE_YUV_2_RGBX_C(uint16_t)
  637. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  638. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  639. }
  640. }
  641. break;
  642. case PIX_FMT_RGB555:
  643. case PIX_FMT_BGR555:
  644. {
  645. const int dr1= dither_2x2_8[y&1 ][0];
  646. const int dg1= dither_2x2_8[y&1 ][1];
  647. const int db1= dither_2x2_8[(y&1)^1][0];
  648. const int dr2= dither_2x2_8[y&1 ][1];
  649. const int dg2= dither_2x2_8[y&1 ][0];
  650. const int db2= dither_2x2_8[(y&1)^1][1];
  651. YSCALE_YUV_2_RGBX_C(uint16_t)
  652. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  653. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  654. }
  655. }
  656. break;
  657. case PIX_FMT_RGB8:
  658. case PIX_FMT_BGR8:
  659. {
  660. const uint8_t * const d64= dither_8x8_73[y&7];
  661. const uint8_t * const d32= dither_8x8_32[y&7];
  662. YSCALE_YUV_2_RGBX_C(uint8_t)
  663. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];
  664. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];
  665. }
  666. }
  667. break;
  668. case PIX_FMT_RGB4:
  669. case PIX_FMT_BGR4:
  670. {
  671. const uint8_t * const d64= dither_8x8_73 [y&7];
  672. const uint8_t * const d128=dither_8x8_220[y&7];
  673. YSCALE_YUV_2_RGBX_C(uint8_t)
  674. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]
  675. +((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);
  676. }
  677. }
  678. break;
  679. case PIX_FMT_RGB4_BYTE:
  680. case PIX_FMT_BGR4_BYTE:
  681. {
  682. const uint8_t * const d64= dither_8x8_73 [y&7];
  683. const uint8_t * const d128=dither_8x8_220[y&7];
  684. YSCALE_YUV_2_RGBX_C(uint8_t)
  685. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];
  686. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];
  687. }
  688. }
  689. break;
  690. case PIX_FMT_MONOBLACK:
  691. {
  692. const uint8_t * const d128=dither_8x8_220[y&7];
  693. uint8_t *g= c->table_gU[128] + c->table_gV[128];
  694. int acc=0;
  695. for(i=0; i<dstW-1; i+=2){
  696. int j;
  697. int Y1=1<<18;
  698. int Y2=1<<18;
  699. for(j=0; j<lumFilterSize; j++)
  700. {
  701. Y1 += lumSrc[j][i] * lumFilter[j];
  702. Y2 += lumSrc[j][i+1] * lumFilter[j];
  703. }
  704. Y1>>=19;
  705. Y2>>=19;
  706. if((Y1|Y2)&256)
  707. {
  708. if(Y1>255) Y1=255;
  709. else if(Y1<0)Y1=0;
  710. if(Y2>255) Y2=255;
  711. else if(Y2<0)Y2=0;
  712. }
  713. acc+= acc + g[Y1+d128[(i+0)&7]];
  714. acc+= acc + g[Y2+d128[(i+1)&7]];
  715. if((i&7)==6){
  716. ((uint8_t*)dest)[0]= acc;
  717. dest++;
  718. }
  719. }
  720. }
  721. break;
  722. case PIX_FMT_YUYV422:
  723. YSCALE_YUV_2_PACKEDX_C(void)
  724. ((uint8_t*)dest)[2*i2+0]= Y1;
  725. ((uint8_t*)dest)[2*i2+1]= U;
  726. ((uint8_t*)dest)[2*i2+2]= Y2;
  727. ((uint8_t*)dest)[2*i2+3]= V;
  728. }
  729. break;
  730. case PIX_FMT_UYVY422:
  731. YSCALE_YUV_2_PACKEDX_C(void)
  732. ((uint8_t*)dest)[2*i2+0]= U;
  733. ((uint8_t*)dest)[2*i2+1]= Y1;
  734. ((uint8_t*)dest)[2*i2+2]= V;
  735. ((uint8_t*)dest)[2*i2+3]= Y2;
  736. }
  737. break;
  738. }
  739. }
  740. //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  741. //Plain C versions
  742. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  743. #define COMPILE_C
  744. #endif
  745. #ifdef ARCH_POWERPC
  746. #if defined (HAVE_ALTIVEC) || defined (RUNTIME_CPUDETECT)
  747. #define COMPILE_ALTIVEC
  748. #endif //HAVE_ALTIVEC
  749. #endif //ARCH_POWERPC
  750. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  751. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  752. #define COMPILE_MMX
  753. #endif
  754. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  755. #define COMPILE_MMX2
  756. #endif
  757. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  758. #define COMPILE_3DNOW
  759. #endif
  760. #endif //ARCH_X86 || ARCH_X86_64
  761. #undef HAVE_MMX
  762. #undef HAVE_MMX2
  763. #undef HAVE_3DNOW
  764. #ifdef COMPILE_C
  765. #undef HAVE_MMX
  766. #undef HAVE_MMX2
  767. #undef HAVE_3DNOW
  768. #undef HAVE_ALTIVEC
  769. #define RENAME(a) a ## _C
  770. #include "swscale_template.c"
  771. #endif
  772. #ifdef ARCH_POWERPC
  773. #ifdef COMPILE_ALTIVEC
  774. #undef RENAME
  775. #define HAVE_ALTIVEC
  776. #define RENAME(a) a ## _altivec
  777. #include "swscale_template.c"
  778. #endif
  779. #endif //ARCH_POWERPC
  780. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  781. //X86 versions
  782. /*
  783. #undef RENAME
  784. #undef HAVE_MMX
  785. #undef HAVE_MMX2
  786. #undef HAVE_3DNOW
  787. #define ARCH_X86
  788. #define RENAME(a) a ## _X86
  789. #include "swscale_template.c"
  790. */
  791. //MMX versions
  792. #ifdef COMPILE_MMX
  793. #undef RENAME
  794. #define HAVE_MMX
  795. #undef HAVE_MMX2
  796. #undef HAVE_3DNOW
  797. #define RENAME(a) a ## _MMX
  798. #include "swscale_template.c"
  799. #endif
  800. //MMX2 versions
  801. #ifdef COMPILE_MMX2
  802. #undef RENAME
  803. #define HAVE_MMX
  804. #define HAVE_MMX2
  805. #undef HAVE_3DNOW
  806. #define RENAME(a) a ## _MMX2
  807. #include "swscale_template.c"
  808. #endif
  809. //3DNOW versions
  810. #ifdef COMPILE_3DNOW
  811. #undef RENAME
  812. #define HAVE_MMX
  813. #undef HAVE_MMX2
  814. #define HAVE_3DNOW
  815. #define RENAME(a) a ## _3DNow
  816. #include "swscale_template.c"
  817. #endif
  818. #endif //ARCH_X86 || ARCH_X86_64
  819. // minor note: the HAVE_xyz is messed up after that line so don't use it
  820. static double getSplineCoeff(double a, double b, double c, double d, double dist)
  821. {
  822. // printf("%f %f %f %f %f\n", a,b,c,d,dist);
  823. if(dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
  824. else return getSplineCoeff( 0.0,
  825. b+ 2.0*c + 3.0*d,
  826. c + 3.0*d,
  827. -b- 3.0*c - 6.0*d,
  828. dist-1.0);
  829. }
  830. static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  831. int srcW, int dstW, int filterAlign, int one, int flags,
  832. SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
  833. {
  834. int i;
  835. int filterSize;
  836. int filter2Size;
  837. int minFilterSize;
  838. double *filter=NULL;
  839. double *filter2=NULL;
  840. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  841. if(flags & SWS_CPU_CAPS_MMX)
  842. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  843. #endif
  844. // Note the +1 is for the MMXscaler which reads over the end
  845. *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
  846. if(ABS(xInc - 0x10000) <10) // unscaled
  847. {
  848. int i;
  849. filterSize= 1;
  850. filter= av_malloc(dstW*sizeof(double)*filterSize);
  851. for(i=0; i<dstW*filterSize; i++) filter[i]=0;
  852. for(i=0; i<dstW; i++)
  853. {
  854. filter[i*filterSize]=1;
  855. (*filterPos)[i]=i;
  856. }
  857. }
  858. else if(flags&SWS_POINT) // lame looking point sampling mode
  859. {
  860. int i;
  861. int xDstInSrc;
  862. filterSize= 1;
  863. filter= av_malloc(dstW*sizeof(double)*filterSize);
  864. xDstInSrc= xInc/2 - 0x8000;
  865. for(i=0; i<dstW; i++)
  866. {
  867. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  868. (*filterPos)[i]= xx;
  869. filter[i]= 1.0;
  870. xDstInSrc+= xInc;
  871. }
  872. }
  873. else if((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
  874. {
  875. int i;
  876. int xDstInSrc;
  877. if (flags&SWS_BICUBIC) filterSize= 4;
  878. else if(flags&SWS_X ) filterSize= 4;
  879. else filterSize= 2; // SWS_BILINEAR / SWS_AREA
  880. filter= av_malloc(dstW*sizeof(double)*filterSize);
  881. xDstInSrc= xInc/2 - 0x8000;
  882. for(i=0; i<dstW; i++)
  883. {
  884. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  885. int j;
  886. (*filterPos)[i]= xx;
  887. //Bilinear upscale / linear interpolate / Area averaging
  888. for(j=0; j<filterSize; j++)
  889. {
  890. double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
  891. double coeff= 1.0 - d;
  892. if(coeff<0) coeff=0;
  893. filter[i*filterSize + j]= coeff;
  894. xx++;
  895. }
  896. xDstInSrc+= xInc;
  897. }
  898. }
  899. else
  900. {
  901. double xDstInSrc;
  902. double sizeFactor, filterSizeInSrc;
  903. const double xInc1= (double)xInc / (double)(1<<16);
  904. if (flags&SWS_BICUBIC) sizeFactor= 4.0;
  905. else if(flags&SWS_X) sizeFactor= 8.0;
  906. else if(flags&SWS_AREA) sizeFactor= 1.0; //downscale only, for upscale it is bilinear
  907. else if(flags&SWS_GAUSS) sizeFactor= 8.0; // infinite ;)
  908. else if(flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? 2.0*param[0] : 6.0;
  909. else if(flags&SWS_SINC) sizeFactor= 20.0; // infinite ;)
  910. else if(flags&SWS_SPLINE) sizeFactor= 20.0; // infinite ;)
  911. else if(flags&SWS_BILINEAR) sizeFactor= 2.0;
  912. else {
  913. sizeFactor= 0.0; //GCC warning killer
  914. ASSERT(0)
  915. }
  916. if(xInc1 <= 1.0) filterSizeInSrc= sizeFactor; // upscale
  917. else filterSizeInSrc= sizeFactor*srcW / (double)dstW;
  918. filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
  919. if(filterSize > srcW-2) filterSize=srcW-2;
  920. filter= av_malloc(dstW*sizeof(double)*filterSize);
  921. xDstInSrc= xInc1 / 2.0 - 0.5;
  922. for(i=0; i<dstW; i++)
  923. {
  924. int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
  925. int j;
  926. (*filterPos)[i]= xx;
  927. for(j=0; j<filterSize; j++)
  928. {
  929. double d= ABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
  930. double coeff;
  931. if(flags & SWS_BICUBIC)
  932. {
  933. double B= param[0] != SWS_PARAM_DEFAULT ? param[0] : 0.0;
  934. double C= param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6;
  935. if(d<1.0)
  936. coeff = (12-9*B-6*C)*d*d*d + (-18+12*B+6*C)*d*d + 6-2*B;
  937. else if(d<2.0)
  938. coeff = (-B-6*C)*d*d*d + (6*B+30*C)*d*d + (-12*B-48*C)*d +8*B+24*C;
  939. else
  940. coeff=0.0;
  941. }
  942. /* else if(flags & SWS_X)
  943. {
  944. double p= param ? param*0.01 : 0.3;
  945. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  946. coeff*= pow(2.0, - p*d*d);
  947. }*/
  948. else if(flags & SWS_X)
  949. {
  950. double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
  951. if(d<1.0)
  952. coeff = cos(d*PI);
  953. else
  954. coeff=-1.0;
  955. if(coeff<0.0) coeff= -pow(-coeff, A);
  956. else coeff= pow( coeff, A);
  957. coeff= coeff*0.5 + 0.5;
  958. }
  959. else if(flags & SWS_AREA)
  960. {
  961. double srcPixelSize= 1.0/xInc1;
  962. if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
  963. else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
  964. else coeff=0.0;
  965. }
  966. else if(flags & SWS_GAUSS)
  967. {
  968. double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  969. coeff = pow(2.0, - p*d*d);
  970. }
  971. else if(flags & SWS_SINC)
  972. {
  973. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  974. }
  975. else if(flags & SWS_LANCZOS)
  976. {
  977. double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  978. coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
  979. if(d>p) coeff=0;
  980. }
  981. else if(flags & SWS_BILINEAR)
  982. {
  983. coeff= 1.0 - d;
  984. if(coeff<0) coeff=0;
  985. }
  986. else if(flags & SWS_SPLINE)
  987. {
  988. double p=-2.196152422706632;
  989. coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
  990. }
  991. else {
  992. coeff= 0.0; //GCC warning killer
  993. ASSERT(0)
  994. }
  995. filter[i*filterSize + j]= coeff;
  996. xx++;
  997. }
  998. xDstInSrc+= xInc1;
  999. }
  1000. }
  1001. /* apply src & dst Filter to filter -> filter2
  1002. av_free(filter);
  1003. */
  1004. ASSERT(filterSize>0)
  1005. filter2Size= filterSize;
  1006. if(srcFilter) filter2Size+= srcFilter->length - 1;
  1007. if(dstFilter) filter2Size+= dstFilter->length - 1;
  1008. ASSERT(filter2Size>0)
  1009. filter2= av_malloc(filter2Size*dstW*sizeof(double));
  1010. for(i=0; i<dstW; i++)
  1011. {
  1012. int j;
  1013. SwsVector scaleFilter;
  1014. SwsVector *outVec;
  1015. scaleFilter.coeff= filter + i*filterSize;
  1016. scaleFilter.length= filterSize;
  1017. if(srcFilter) outVec= sws_getConvVec(srcFilter, &scaleFilter);
  1018. else outVec= &scaleFilter;
  1019. ASSERT(outVec->length == filter2Size)
  1020. //FIXME dstFilter
  1021. for(j=0; j<outVec->length; j++)
  1022. {
  1023. filter2[i*filter2Size + j]= outVec->coeff[j];
  1024. }
  1025. (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
  1026. if(outVec != &scaleFilter) sws_freeVec(outVec);
  1027. }
  1028. av_free(filter); filter=NULL;
  1029. /* try to reduce the filter-size (step1 find size and shift left) */
  1030. // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
  1031. minFilterSize= 0;
  1032. for(i=dstW-1; i>=0; i--)
  1033. {
  1034. int min= filter2Size;
  1035. int j;
  1036. double cutOff=0.0;
  1037. /* get rid off near zero elements on the left by shifting left */
  1038. for(j=0; j<filter2Size; j++)
  1039. {
  1040. int k;
  1041. cutOff += ABS(filter2[i*filter2Size]);
  1042. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1043. /* preserve Monotonicity because the core can't handle the filter otherwise */
  1044. if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
  1045. // Move filter coeffs left
  1046. for(k=1; k<filter2Size; k++)
  1047. filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
  1048. filter2[i*filter2Size + k - 1]= 0.0;
  1049. (*filterPos)[i]++;
  1050. }
  1051. cutOff=0.0;
  1052. /* count near zeros on the right */
  1053. for(j=filter2Size-1; j>0; j--)
  1054. {
  1055. cutOff += ABS(filter2[i*filter2Size + j]);
  1056. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1057. min--;
  1058. }
  1059. if(min>minFilterSize) minFilterSize= min;
  1060. }
  1061. if (flags & SWS_CPU_CAPS_ALTIVEC) {
  1062. // we can handle the special case 4,
  1063. // so we don't want to go to the full 8
  1064. if (minFilterSize < 5)
  1065. filterAlign = 4;
  1066. // we really don't want to waste our time
  1067. // doing useless computation, so fall-back on
  1068. // the scalar C code for very small filter.
  1069. // vectorizing is worth it only if you have
  1070. // decent-sized vector.
  1071. if (minFilterSize < 3)
  1072. filterAlign = 1;
  1073. }
  1074. if (flags & SWS_CPU_CAPS_MMX) {
  1075. // special case for unscaled vertical filtering
  1076. if(minFilterSize == 1 && filterAlign == 2)
  1077. filterAlign= 1;
  1078. }
  1079. ASSERT(minFilterSize > 0)
  1080. filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
  1081. ASSERT(filterSize > 0)
  1082. filter= av_malloc(filterSize*dstW*sizeof(double));
  1083. if(filterSize >= MAX_FILTER_SIZE)
  1084. return -1;
  1085. *outFilterSize= filterSize;
  1086. if(flags&SWS_PRINT_INFO)
  1087. MSG_V("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
  1088. /* try to reduce the filter-size (step2 reduce it) */
  1089. for(i=0; i<dstW; i++)
  1090. {
  1091. int j;
  1092. for(j=0; j<filterSize; j++)
  1093. {
  1094. if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
  1095. else filter[i*filterSize + j]= filter2[i*filter2Size + j];
  1096. }
  1097. }
  1098. av_free(filter2); filter2=NULL;
  1099. //FIXME try to align filterpos if possible
  1100. //fix borders
  1101. for(i=0; i<dstW; i++)
  1102. {
  1103. int j;
  1104. if((*filterPos)[i] < 0)
  1105. {
  1106. // Move filter coeffs left to compensate for filterPos
  1107. for(j=1; j<filterSize; j++)
  1108. {
  1109. int left= FFMAX(j + (*filterPos)[i], 0);
  1110. filter[i*filterSize + left] += filter[i*filterSize + j];
  1111. filter[i*filterSize + j]=0;
  1112. }
  1113. (*filterPos)[i]= 0;
  1114. }
  1115. if((*filterPos)[i] + filterSize > srcW)
  1116. {
  1117. int shift= (*filterPos)[i] + filterSize - srcW;
  1118. // Move filter coeffs right to compensate for filterPos
  1119. for(j=filterSize-2; j>=0; j--)
  1120. {
  1121. int right= FFMIN(j + shift, filterSize-1);
  1122. filter[i*filterSize +right] += filter[i*filterSize +j];
  1123. filter[i*filterSize +j]=0;
  1124. }
  1125. (*filterPos)[i]= srcW - filterSize;
  1126. }
  1127. }
  1128. // Note the +1 is for the MMXscaler which reads over the end
  1129. /* align at 16 for AltiVec (needed by hScale_altivec_real) */
  1130. *outFilter= av_malloc(*outFilterSize*(dstW+1)*sizeof(int16_t));
  1131. memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
  1132. /* Normalize & Store in outFilter */
  1133. for(i=0; i<dstW; i++)
  1134. {
  1135. int j;
  1136. double error=0;
  1137. double sum=0;
  1138. double scale= one;
  1139. for(j=0; j<filterSize; j++)
  1140. {
  1141. sum+= filter[i*filterSize + j];
  1142. }
  1143. scale/= sum;
  1144. for(j=0; j<*outFilterSize; j++)
  1145. {
  1146. double v= filter[i*filterSize + j]*scale + error;
  1147. int intV= floor(v + 0.5);
  1148. (*outFilter)[i*(*outFilterSize) + j]= intV;
  1149. error = v - intV;
  1150. }
  1151. }
  1152. (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
  1153. for(i=0; i<*outFilterSize; i++)
  1154. {
  1155. int j= dstW*(*outFilterSize);
  1156. (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
  1157. }
  1158. av_free(filter);
  1159. return 0;
  1160. }
  1161. #ifdef COMPILE_MMX2
  1162. static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
  1163. {
  1164. uint8_t *fragmentA;
  1165. long imm8OfPShufW1A;
  1166. long imm8OfPShufW2A;
  1167. long fragmentLengthA;
  1168. uint8_t *fragmentB;
  1169. long imm8OfPShufW1B;
  1170. long imm8OfPShufW2B;
  1171. long fragmentLengthB;
  1172. int fragmentPos;
  1173. int xpos, i;
  1174. // create an optimized horizontal scaling routine
  1175. //code fragment
  1176. asm volatile(
  1177. "jmp 9f \n\t"
  1178. // Begin
  1179. "0: \n\t"
  1180. "movq (%%"REG_d", %%"REG_a"), %%mm3\n\t"
  1181. "movd (%%"REG_c", %%"REG_S"), %%mm0\n\t"
  1182. "movd 1(%%"REG_c", %%"REG_S"), %%mm1\n\t"
  1183. "punpcklbw %%mm7, %%mm1 \n\t"
  1184. "punpcklbw %%mm7, %%mm0 \n\t"
  1185. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  1186. "1: \n\t"
  1187. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1188. "2: \n\t"
  1189. "psubw %%mm1, %%mm0 \n\t"
  1190. "movl 8(%%"REG_b", %%"REG_a"), %%esi\n\t"
  1191. "pmullw %%mm3, %%mm0 \n\t"
  1192. "psllw $7, %%mm1 \n\t"
  1193. "paddw %%mm1, %%mm0 \n\t"
  1194. "movq %%mm0, (%%"REG_D", %%"REG_a")\n\t"
  1195. "add $8, %%"REG_a" \n\t"
  1196. // End
  1197. "9: \n\t"
  1198. // "int $3\n\t"
  1199. "lea 0b, %0 \n\t"
  1200. "lea 1b, %1 \n\t"
  1201. "lea 2b, %2 \n\t"
  1202. "dec %1 \n\t"
  1203. "dec %2 \n\t"
  1204. "sub %0, %1 \n\t"
  1205. "sub %0, %2 \n\t"
  1206. "lea 9b, %3 \n\t"
  1207. "sub %0, %3 \n\t"
  1208. :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  1209. "=r" (fragmentLengthA)
  1210. );
  1211. asm volatile(
  1212. "jmp 9f \n\t"
  1213. // Begin
  1214. "0: \n\t"
  1215. "movq (%%"REG_d", %%"REG_a"), %%mm3\n\t"
  1216. "movd (%%"REG_c", %%"REG_S"), %%mm0\n\t"
  1217. "punpcklbw %%mm7, %%mm0 \n\t"
  1218. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  1219. "1: \n\t"
  1220. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1221. "2: \n\t"
  1222. "psubw %%mm1, %%mm0 \n\t"
  1223. "movl 8(%%"REG_b", %%"REG_a"), %%esi\n\t"
  1224. "pmullw %%mm3, %%mm0 \n\t"
  1225. "psllw $7, %%mm1 \n\t"
  1226. "paddw %%mm1, %%mm0 \n\t"
  1227. "movq %%mm0, (%%"REG_D", %%"REG_a")\n\t"
  1228. "add $8, %%"REG_a" \n\t"
  1229. // End
  1230. "9: \n\t"
  1231. // "int $3\n\t"
  1232. "lea 0b, %0 \n\t"
  1233. "lea 1b, %1 \n\t"
  1234. "lea 2b, %2 \n\t"
  1235. "dec %1 \n\t"
  1236. "dec %2 \n\t"
  1237. "sub %0, %1 \n\t"
  1238. "sub %0, %2 \n\t"
  1239. "lea 9b, %3 \n\t"
  1240. "sub %0, %3 \n\t"
  1241. :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  1242. "=r" (fragmentLengthB)
  1243. );
  1244. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  1245. fragmentPos=0;
  1246. for(i=0; i<dstW/numSplits; i++)
  1247. {
  1248. int xx=xpos>>16;
  1249. if((i&3) == 0)
  1250. {
  1251. int a=0;
  1252. int b=((xpos+xInc)>>16) - xx;
  1253. int c=((xpos+xInc*2)>>16) - xx;
  1254. int d=((xpos+xInc*3)>>16) - xx;
  1255. filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
  1256. filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
  1257. filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
  1258. filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
  1259. filterPos[i/2]= xx;
  1260. if(d+1<4)
  1261. {
  1262. int maxShift= 3-(d+1);
  1263. int shift=0;
  1264. memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
  1265. funnyCode[fragmentPos + imm8OfPShufW1B]=
  1266. (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
  1267. funnyCode[fragmentPos + imm8OfPShufW2B]=
  1268. a | (b<<2) | (c<<4) | (d<<6);
  1269. if(i+3>=dstW) shift=maxShift; //avoid overread
  1270. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
  1271. if(shift && i>=shift)
  1272. {
  1273. funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
  1274. funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
  1275. filterPos[i/2]-=shift;
  1276. }
  1277. fragmentPos+= fragmentLengthB;
  1278. }
  1279. else
  1280. {
  1281. int maxShift= 3-d;
  1282. int shift=0;
  1283. memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
  1284. funnyCode[fragmentPos + imm8OfPShufW1A]=
  1285. funnyCode[fragmentPos + imm8OfPShufW2A]=
  1286. a | (b<<2) | (c<<4) | (d<<6);
  1287. if(i+4>=dstW) shift=maxShift; //avoid overread
  1288. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
  1289. if(shift && i>=shift)
  1290. {
  1291. funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
  1292. funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
  1293. filterPos[i/2]-=shift;
  1294. }
  1295. fragmentPos+= fragmentLengthA;
  1296. }
  1297. funnyCode[fragmentPos]= RET;
  1298. }
  1299. xpos+=xInc;
  1300. }
  1301. filterPos[i/2]= xpos>>16; // needed to jump to the next part
  1302. }
  1303. #endif /* COMPILE_MMX2 */
  1304. static void globalInit(void){
  1305. // generating tables:
  1306. int i;
  1307. for(i=0; i<768; i++){
  1308. int c= FFMIN(FFMAX(i-256, 0), 255);
  1309. clip_table[i]=c;
  1310. }
  1311. }
  1312. static SwsFunc getSwsFunc(int flags){
  1313. #ifdef RUNTIME_CPUDETECT
  1314. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  1315. // ordered per speed fasterst first
  1316. if(flags & SWS_CPU_CAPS_MMX2)
  1317. return swScale_MMX2;
  1318. else if(flags & SWS_CPU_CAPS_3DNOW)
  1319. return swScale_3DNow;
  1320. else if(flags & SWS_CPU_CAPS_MMX)
  1321. return swScale_MMX;
  1322. else
  1323. return swScale_C;
  1324. #else
  1325. #ifdef ARCH_POWERPC
  1326. if(flags & SWS_CPU_CAPS_ALTIVEC)
  1327. return swScale_altivec;
  1328. else
  1329. return swScale_C;
  1330. #endif
  1331. return swScale_C;
  1332. #endif /* defined(ARCH_X86) || defined(ARCH_X86_64) */
  1333. #else //RUNTIME_CPUDETECT
  1334. #ifdef HAVE_MMX2
  1335. return swScale_MMX2;
  1336. #elif defined (HAVE_3DNOW)
  1337. return swScale_3DNow;
  1338. #elif defined (HAVE_MMX)
  1339. return swScale_MMX;
  1340. #elif defined (HAVE_ALTIVEC)
  1341. return swScale_altivec;
  1342. #else
  1343. return swScale_C;
  1344. #endif
  1345. #endif //!RUNTIME_CPUDETECT
  1346. }
  1347. static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1348. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1349. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1350. /* Copy Y plane */
  1351. if(dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1352. memcpy(dst, src[0], srcSliceH*dstStride[0]);
  1353. else
  1354. {
  1355. int i;
  1356. uint8_t *srcPtr= src[0];
  1357. uint8_t *dstPtr= dst;
  1358. for(i=0; i<srcSliceH; i++)
  1359. {
  1360. memcpy(dstPtr, srcPtr, c->srcW);
  1361. srcPtr+= srcStride[0];
  1362. dstPtr+= dstStride[0];
  1363. }
  1364. }
  1365. dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1366. if (c->dstFormat == PIX_FMT_NV12)
  1367. interleaveBytes( src[1],src[2],dst,c->srcW/2,srcSliceH/2,srcStride[1],srcStride[2],dstStride[0] );
  1368. else
  1369. interleaveBytes( src[2],src[1],dst,c->srcW/2,srcSliceH/2,srcStride[2],srcStride[1],dstStride[0] );
  1370. return srcSliceH;
  1371. }
  1372. static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1373. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1374. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1375. yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1376. return srcSliceH;
  1377. }
  1378. static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1379. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1380. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1381. yv12touyvy( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1382. return srcSliceH;
  1383. }
  1384. /* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
  1385. static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1386. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1387. const int srcFormat= c->srcFormat;
  1388. const int dstFormat= c->dstFormat;
  1389. const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
  1390. const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
  1391. const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  1392. const int dstId= fmt_depth(dstFormat) >> 2;
  1393. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1394. /* BGR -> BGR */
  1395. if( (isBGR(srcFormat) && isBGR(dstFormat))
  1396. || (isRGB(srcFormat) && isRGB(dstFormat))){
  1397. switch(srcId | (dstId<<4)){
  1398. case 0x34: conv= rgb16to15; break;
  1399. case 0x36: conv= rgb24to15; break;
  1400. case 0x38: conv= rgb32to15; break;
  1401. case 0x43: conv= rgb15to16; break;
  1402. case 0x46: conv= rgb24to16; break;
  1403. case 0x48: conv= rgb32to16; break;
  1404. case 0x63: conv= rgb15to24; break;
  1405. case 0x64: conv= rgb16to24; break;
  1406. case 0x68: conv= rgb32to24; break;
  1407. case 0x83: conv= rgb15to32; break;
  1408. case 0x84: conv= rgb16to32; break;
  1409. case 0x86: conv= rgb24to32; break;
  1410. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1411. sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
  1412. }
  1413. }else if( (isBGR(srcFormat) && isRGB(dstFormat))
  1414. || (isRGB(srcFormat) && isBGR(dstFormat))){
  1415. switch(srcId | (dstId<<4)){
  1416. case 0x33: conv= rgb15tobgr15; break;
  1417. case 0x34: conv= rgb16tobgr15; break;
  1418. case 0x36: conv= rgb24tobgr15; break;
  1419. case 0x38: conv= rgb32tobgr15; break;
  1420. case 0x43: conv= rgb15tobgr16; break;
  1421. case 0x44: conv= rgb16tobgr16; break;
  1422. case 0x46: conv= rgb24tobgr16; break;
  1423. case 0x48: conv= rgb32tobgr16; break;
  1424. case 0x63: conv= rgb15tobgr24; break;
  1425. case 0x64: conv= rgb16tobgr24; break;
  1426. case 0x66: conv= rgb24tobgr24; break;
  1427. case 0x68: conv= rgb32tobgr24; break;
  1428. case 0x83: conv= rgb15tobgr32; break;
  1429. case 0x84: conv= rgb16tobgr32; break;
  1430. case 0x86: conv= rgb24tobgr32; break;
  1431. case 0x88: conv= rgb32tobgr32; break;
  1432. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1433. sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
  1434. }
  1435. }else{
  1436. MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1437. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1438. }
  1439. if(dstStride[0]*srcBpp == srcStride[0]*dstBpp)
  1440. conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1441. else
  1442. {
  1443. int i;
  1444. uint8_t *srcPtr= src[0];
  1445. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1446. for(i=0; i<srcSliceH; i++)
  1447. {
  1448. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1449. srcPtr+= srcStride[0];
  1450. dstPtr+= dstStride[0];
  1451. }
  1452. }
  1453. return srcSliceH;
  1454. }
  1455. static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1456. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1457. rgb24toyv12(
  1458. src[0],
  1459. dst[0]+ srcSliceY *dstStride[0],
  1460. dst[1]+(srcSliceY>>1)*dstStride[1],
  1461. dst[2]+(srcSliceY>>1)*dstStride[2],
  1462. c->srcW, srcSliceH,
  1463. dstStride[0], dstStride[1], srcStride[0]);
  1464. return srcSliceH;
  1465. }
  1466. static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1467. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1468. int i;
  1469. /* copy Y */
  1470. if(srcStride[0]==dstStride[0] && srcStride[0] > 0)
  1471. memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
  1472. else{
  1473. uint8_t *srcPtr= src[0];
  1474. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1475. for(i=0; i<srcSliceH; i++)
  1476. {
  1477. memcpy(dstPtr, srcPtr, c->srcW);
  1478. srcPtr+= srcStride[0];
  1479. dstPtr+= dstStride[0];
  1480. }
  1481. }
  1482. if(c->dstFormat==PIX_FMT_YUV420P){
  1483. planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
  1484. planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
  1485. }else{
  1486. planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
  1487. planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
  1488. }
  1489. return srcSliceH;
  1490. }
  1491. /* unscaled copy like stuff (assumes nearly identical formats) */
  1492. static int simpleCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1493. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1494. if(isPacked(c->srcFormat))
  1495. {
  1496. if(dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1497. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1498. else
  1499. {
  1500. int i;
  1501. uint8_t *srcPtr= src[0];
  1502. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1503. int length=0;
  1504. /* universal length finder */
  1505. while(length+c->srcW <= ABS(dstStride[0])
  1506. && length+c->srcW <= ABS(srcStride[0])) length+= c->srcW;
  1507. ASSERT(length!=0);
  1508. for(i=0; i<srcSliceH; i++)
  1509. {
  1510. memcpy(dstPtr, srcPtr, length);
  1511. srcPtr+= srcStride[0];
  1512. dstPtr+= dstStride[0];
  1513. }
  1514. }
  1515. }
  1516. else
  1517. { /* Planar YUV or gray */
  1518. int plane;
  1519. for(plane=0; plane<3; plane++)
  1520. {
  1521. int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1522. int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1523. int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1524. if((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
  1525. {
  1526. if(!isGray(c->dstFormat))
  1527. memset(dst[plane], 128, dstStride[plane]*height);
  1528. }
  1529. else
  1530. {
  1531. if(dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
  1532. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1533. else
  1534. {
  1535. int i;
  1536. uint8_t *srcPtr= src[plane];
  1537. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1538. for(i=0; i<height; i++)
  1539. {
  1540. memcpy(dstPtr, srcPtr, length);
  1541. srcPtr+= srcStride[plane];
  1542. dstPtr+= dstStride[plane];
  1543. }
  1544. }
  1545. }
  1546. }
  1547. }
  1548. return srcSliceH;
  1549. }
  1550. static void getSubSampleFactors(int *h, int *v, int format){
  1551. switch(format){
  1552. case PIX_FMT_UYVY422:
  1553. case PIX_FMT_YUYV422:
  1554. *h=1;
  1555. *v=0;
  1556. break;
  1557. case PIX_FMT_YUV420P:
  1558. case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
  1559. case PIX_FMT_NV12:
  1560. case PIX_FMT_NV21:
  1561. *h=1;
  1562. *v=1;
  1563. break;
  1564. case PIX_FMT_YUV410P:
  1565. *h=2;
  1566. *v=2;
  1567. break;
  1568. case PIX_FMT_YUV444P:
  1569. *h=0;
  1570. *v=0;
  1571. break;
  1572. case PIX_FMT_YUV422P:
  1573. *h=1;
  1574. *v=0;
  1575. break;
  1576. case PIX_FMT_YUV411P:
  1577. *h=2;
  1578. *v=0;
  1579. break;
  1580. default:
  1581. *h=0;
  1582. *v=0;
  1583. break;
  1584. }
  1585. }
  1586. static uint16_t roundToInt16(int64_t f){
  1587. int r= (f + (1<<15))>>16;
  1588. if(r<-0x7FFF) return 0x8000;
  1589. else if(r> 0x7FFF) return 0x7FFF;
  1590. else return r;
  1591. }
  1592. /**
  1593. * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
  1594. * @param fullRange if 1 then the luma range is 0..255 if 0 its 16..235
  1595. * @return -1 if not supported
  1596. */
  1597. int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
  1598. int64_t crv = inv_table[0];
  1599. int64_t cbu = inv_table[1];
  1600. int64_t cgu = -inv_table[2];
  1601. int64_t cgv = -inv_table[3];
  1602. int64_t cy = 1<<16;
  1603. int64_t oy = 0;
  1604. if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  1605. memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
  1606. memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
  1607. c->brightness= brightness;
  1608. c->contrast = contrast;
  1609. c->saturation= saturation;
  1610. c->srcRange = srcRange;
  1611. c->dstRange = dstRange;
  1612. c->uOffset= 0x0400040004000400LL;
  1613. c->vOffset= 0x0400040004000400LL;
  1614. if(!srcRange){
  1615. cy= (cy*255) / 219;
  1616. oy= 16<<16;
  1617. }
  1618. cy = (cy *contrast )>>16;
  1619. crv= (crv*contrast * saturation)>>32;
  1620. cbu= (cbu*contrast * saturation)>>32;
  1621. cgu= (cgu*contrast * saturation)>>32;
  1622. cgv= (cgv*contrast * saturation)>>32;
  1623. oy -= 256*brightness;
  1624. c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
  1625. c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
  1626. c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
  1627. c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
  1628. c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
  1629. c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
  1630. yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
  1631. //FIXME factorize
  1632. #ifdef COMPILE_ALTIVEC
  1633. if (c->flags & SWS_CPU_CAPS_ALTIVEC)
  1634. yuv2rgb_altivec_init_tables (c, inv_table, brightness, contrast, saturation);
  1635. #endif
  1636. return 0;
  1637. }
  1638. /**
  1639. * @return -1 if not supported
  1640. */
  1641. int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
  1642. if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  1643. *inv_table = c->srcColorspaceTable;
  1644. *table = c->dstColorspaceTable;
  1645. *srcRange = c->srcRange;
  1646. *dstRange = c->dstRange;
  1647. *brightness= c->brightness;
  1648. *contrast = c->contrast;
  1649. *saturation= c->saturation;
  1650. return 0;
  1651. }
  1652. SwsContext *sws_getContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
  1653. SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
  1654. SwsContext *c;
  1655. int i;
  1656. int usesVFilter, usesHFilter;
  1657. int unscaled, needsDither;
  1658. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  1659. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  1660. if(flags & SWS_CPU_CAPS_MMX)
  1661. asm volatile("emms\n\t"::: "memory");
  1662. #endif
  1663. #ifndef RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
  1664. flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC);
  1665. #ifdef HAVE_MMX2
  1666. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1667. #elif defined (HAVE_3DNOW)
  1668. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1669. #elif defined (HAVE_MMX)
  1670. flags |= SWS_CPU_CAPS_MMX;
  1671. #elif defined (HAVE_ALTIVEC)
  1672. flags |= SWS_CPU_CAPS_ALTIVEC;
  1673. #endif
  1674. #endif /* RUNTIME_CPUDETECT */
  1675. if(clip_table[512] != 255) globalInit();
  1676. if(rgb15to16 == NULL) sws_rgb2rgb_init(flags);
  1677. unscaled = (srcW == dstW && srcH == dstH);
  1678. needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
  1679. && (fmt_depth(dstFormat))<24
  1680. && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
  1681. if(!isSupportedIn(srcFormat))
  1682. {
  1683. MSG_ERR("swScaler: %s is not supported as input format\n", sws_format_name(srcFormat));
  1684. return NULL;
  1685. }
  1686. if(!isSupportedOut(dstFormat))
  1687. {
  1688. MSG_ERR("swScaler: %s is not supported as output format\n", sws_format_name(dstFormat));
  1689. return NULL;
  1690. }
  1691. /* sanity check */
  1692. 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
  1693. {
  1694. MSG_ERR("swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  1695. srcW, srcH, dstW, dstH);
  1696. return NULL;
  1697. }
  1698. if(!dstFilter) dstFilter= &dummyFilter;
  1699. if(!srcFilter) srcFilter= &dummyFilter;
  1700. c= av_malloc(sizeof(SwsContext));
  1701. memset(c, 0, sizeof(SwsContext));
  1702. c->srcW= srcW;
  1703. c->srcH= srcH;
  1704. c->dstW= dstW;
  1705. c->dstH= dstH;
  1706. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  1707. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  1708. c->flags= flags;
  1709. c->dstFormat= dstFormat;
  1710. c->srcFormat= srcFormat;
  1711. c->vRounder= 4* 0x0001000100010001ULL;
  1712. usesHFilter= usesVFilter= 0;
  1713. if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesVFilter=1;
  1714. if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesHFilter=1;
  1715. if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesVFilter=1;
  1716. if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesHFilter=1;
  1717. if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesVFilter=1;
  1718. if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesHFilter=1;
  1719. if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesVFilter=1;
  1720. if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesHFilter=1;
  1721. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  1722. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  1723. // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
  1724. if((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  1725. // drop some chroma lines if the user wants it
  1726. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  1727. c->chrSrcVSubSample+= c->vChrDrop;
  1728. // drop every 2. pixel for chroma calculation unless user wants full chroma
  1729. if((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP))
  1730. c->chrSrcHSubSample=1;
  1731. if(param){
  1732. c->param[0] = param[0];
  1733. c->param[1] = param[1];
  1734. }else{
  1735. c->param[0] =
  1736. c->param[1] = SWS_PARAM_DEFAULT;
  1737. }
  1738. c->chrIntHSubSample= c->chrDstHSubSample;
  1739. c->chrIntVSubSample= c->chrSrcVSubSample;
  1740. // note the -((-x)>>y) is so that we allways round toward +inf
  1741. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  1742. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  1743. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  1744. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  1745. sws_setColorspaceDetails(c, Inverse_Table_6_9[SWS_CS_DEFAULT], 0, Inverse_Table_6_9[SWS_CS_DEFAULT] /* FIXME*/, 0, 0, 1<<16, 1<<16);
  1746. /* unscaled special Cases */
  1747. if(unscaled && !usesHFilter && !usesVFilter)
  1748. {
  1749. /* yv12_to_nv12 */
  1750. if(srcFormat == PIX_FMT_YUV420P && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
  1751. {
  1752. c->swScale= PlanarToNV12Wrapper;
  1753. }
  1754. /* yuv2bgr */
  1755. if((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P) && (isBGR(dstFormat) || isRGB(dstFormat)))
  1756. {
  1757. c->swScale= yuv2rgb_get_func_ptr(c);
  1758. }
  1759. if( srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P )
  1760. {
  1761. c->swScale= yvu9toyv12Wrapper;
  1762. }
  1763. /* bgr24toYV12 */
  1764. if(srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P)
  1765. c->swScale= bgr24toyv12Wrapper;
  1766. /* rgb/bgr -> rgb/bgr (no dither needed forms) */
  1767. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1768. && (isBGR(dstFormat) || isRGB(dstFormat))
  1769. && !needsDither)
  1770. c->swScale= rgb2rgbWrapper;
  1771. /* LQ converters if -sws 0 or -sws 4*/
  1772. if(c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
  1773. /* rgb/bgr -> rgb/bgr (dither needed forms) */
  1774. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1775. && (isBGR(dstFormat) || isRGB(dstFormat))
  1776. && needsDither)
  1777. c->swScale= rgb2rgbWrapper;
  1778. /* yv12_to_yuy2 */
  1779. if(srcFormat == PIX_FMT_YUV420P &&
  1780. (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422))
  1781. {
  1782. if (dstFormat == PIX_FMT_YUYV422)
  1783. c->swScale= PlanarToYuy2Wrapper;
  1784. else
  1785. c->swScale= PlanarToUyvyWrapper;
  1786. }
  1787. }
  1788. #ifdef COMPILE_ALTIVEC
  1789. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  1790. ((srcFormat == PIX_FMT_YUV420P &&
  1791. (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422)))) {
  1792. // unscaled YV12 -> packed YUV, we want speed
  1793. if (dstFormat == PIX_FMT_YUYV422)
  1794. c->swScale= yv12toyuy2_unscaled_altivec;
  1795. else
  1796. c->swScale= yv12touyvy_unscaled_altivec;
  1797. }
  1798. #endif
  1799. /* simple copy */
  1800. if( srcFormat == dstFormat
  1801. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1802. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1803. )
  1804. {
  1805. c->swScale= simpleCopy;
  1806. }
  1807. if(c->swScale){
  1808. if(flags&SWS_PRINT_INFO)
  1809. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1810. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1811. return c;
  1812. }
  1813. }
  1814. if(flags & SWS_CPU_CAPS_MMX2)
  1815. {
  1816. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  1817. if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
  1818. {
  1819. if(flags&SWS_PRINT_INFO)
  1820. MSG_INFO("SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
  1821. }
  1822. if(usesHFilter) c->canMMX2BeUsed=0;
  1823. }
  1824. else
  1825. c->canMMX2BeUsed=0;
  1826. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  1827. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  1828. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  1829. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  1830. // n-2 is the last chrominance sample available
  1831. // this is not perfect, but noone shuld notice the difference, the more correct variant
  1832. // would be like the vertical one, but that would require some special code for the
  1833. // first and last pixel
  1834. if(flags&SWS_FAST_BILINEAR)
  1835. {
  1836. if(c->canMMX2BeUsed)
  1837. {
  1838. c->lumXInc+= 20;
  1839. c->chrXInc+= 20;
  1840. }
  1841. //we don't use the x86asm scaler if mmx is available
  1842. else if(flags & SWS_CPU_CAPS_MMX)
  1843. {
  1844. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  1845. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  1846. }
  1847. }
  1848. /* precalculate horizontal scaler filter coefficients */
  1849. {
  1850. const int filterAlign=
  1851. (flags & SWS_CPU_CAPS_MMX) ? 4 :
  1852. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  1853. 1;
  1854. initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  1855. srcW , dstW, filterAlign, 1<<14,
  1856. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1857. srcFilter->lumH, dstFilter->lumH, c->param);
  1858. initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  1859. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  1860. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1861. srcFilter->chrH, dstFilter->chrH, c->param);
  1862. #define MAX_FUNNY_CODE_SIZE 10000
  1863. #if defined(COMPILE_MMX2)
  1864. // can't downscale !!!
  1865. if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
  1866. {
  1867. #ifdef MAP_ANONYMOUS
  1868. c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  1869. c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  1870. #else
  1871. c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
  1872. c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
  1873. #endif
  1874. c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
  1875. c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
  1876. c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
  1877. c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
  1878. initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
  1879. initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
  1880. }
  1881. #endif /* defined(COMPILE_MMX2) */
  1882. } // Init Horizontal stuff
  1883. /* precalculate vertical scaler filter coefficients */
  1884. {
  1885. const int filterAlign=
  1886. (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
  1887. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  1888. 1;
  1889. initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  1890. srcH , dstH, filterAlign, (1<<12)-4,
  1891. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1892. srcFilter->lumV, dstFilter->lumV, c->param);
  1893. initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  1894. c->chrSrcH, c->chrDstH, filterAlign, (1<<12)-4,
  1895. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1896. srcFilter->chrV, dstFilter->chrV, c->param);
  1897. #ifdef HAVE_ALTIVEC
  1898. c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
  1899. c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
  1900. for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
  1901. int j;
  1902. short *p = (short *)&c->vYCoeffsBank[i];
  1903. for (j=0;j<8;j++)
  1904. p[j] = c->vLumFilter[i];
  1905. }
  1906. for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
  1907. int j;
  1908. short *p = (short *)&c->vCCoeffsBank[i];
  1909. for (j=0;j<8;j++)
  1910. p[j] = c->vChrFilter[i];
  1911. }
  1912. #endif
  1913. }
  1914. // Calculate Buffer Sizes so that they won't run out while handling these damn slices
  1915. c->vLumBufSize= c->vLumFilterSize;
  1916. c->vChrBufSize= c->vChrFilterSize;
  1917. for(i=0; i<dstH; i++)
  1918. {
  1919. int chrI= i*c->chrDstH / dstH;
  1920. int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  1921. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  1922. nextSlice>>= c->chrSrcVSubSample;
  1923. nextSlice<<= c->chrSrcVSubSample;
  1924. if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  1925. c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
  1926. if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  1927. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  1928. }
  1929. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  1930. c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
  1931. c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
  1932. //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)
  1933. /* align at 16 bytes for AltiVec */
  1934. for(i=0; i<c->vLumBufSize; i++)
  1935. c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_malloc(4000);
  1936. for(i=0; i<c->vChrBufSize; i++)
  1937. c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc(8000);
  1938. //try to avoid drawing green stuff between the right end and the stride end
  1939. for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
  1940. for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
  1941. ASSERT(c->chrDstH <= dstH)
  1942. if(flags&SWS_PRINT_INFO)
  1943. {
  1944. #ifdef DITHER1XBPP
  1945. char *dither= " dithered";
  1946. #else
  1947. char *dither= "";
  1948. #endif
  1949. if(flags&SWS_FAST_BILINEAR)
  1950. MSG_INFO("\nSwScaler: FAST_BILINEAR scaler, ");
  1951. else if(flags&SWS_BILINEAR)
  1952. MSG_INFO("\nSwScaler: BILINEAR scaler, ");
  1953. else if(flags&SWS_BICUBIC)
  1954. MSG_INFO("\nSwScaler: BICUBIC scaler, ");
  1955. else if(flags&SWS_X)
  1956. MSG_INFO("\nSwScaler: Experimental scaler, ");
  1957. else if(flags&SWS_POINT)
  1958. MSG_INFO("\nSwScaler: Nearest Neighbor / POINT scaler, ");
  1959. else if(flags&SWS_AREA)
  1960. MSG_INFO("\nSwScaler: Area Averageing scaler, ");
  1961. else if(flags&SWS_BICUBLIN)
  1962. MSG_INFO("\nSwScaler: luma BICUBIC / chroma BILINEAR scaler, ");
  1963. else if(flags&SWS_GAUSS)
  1964. MSG_INFO("\nSwScaler: Gaussian scaler, ");
  1965. else if(flags&SWS_SINC)
  1966. MSG_INFO("\nSwScaler: Sinc scaler, ");
  1967. else if(flags&SWS_LANCZOS)
  1968. MSG_INFO("\nSwScaler: Lanczos scaler, ");
  1969. else if(flags&SWS_SPLINE)
  1970. MSG_INFO("\nSwScaler: Bicubic spline scaler, ");
  1971. else
  1972. MSG_INFO("\nSwScaler: ehh flags invalid?! ");
  1973. if(dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
  1974. MSG_INFO("from %s to%s %s ",
  1975. sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
  1976. else
  1977. MSG_INFO("from %s to %s ",
  1978. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1979. if(flags & SWS_CPU_CAPS_MMX2)
  1980. MSG_INFO("using MMX2\n");
  1981. else if(flags & SWS_CPU_CAPS_3DNOW)
  1982. MSG_INFO("using 3DNOW\n");
  1983. else if(flags & SWS_CPU_CAPS_MMX)
  1984. MSG_INFO("using MMX\n");
  1985. else if(flags & SWS_CPU_CAPS_ALTIVEC)
  1986. MSG_INFO("using AltiVec\n");
  1987. else
  1988. MSG_INFO("using C\n");
  1989. }
  1990. if(flags & SWS_PRINT_INFO)
  1991. {
  1992. if(flags & SWS_CPU_CAPS_MMX)
  1993. {
  1994. if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  1995. MSG_V("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  1996. else
  1997. {
  1998. if(c->hLumFilterSize==4)
  1999. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
  2000. else if(c->hLumFilterSize==8)
  2001. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
  2002. else
  2003. MSG_V("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
  2004. if(c->hChrFilterSize==4)
  2005. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
  2006. else if(c->hChrFilterSize==8)
  2007. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
  2008. else
  2009. MSG_V("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
  2010. }
  2011. }
  2012. else
  2013. {
  2014. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  2015. MSG_V("SwScaler: using X86-Asm scaler for horizontal scaling\n");
  2016. #else
  2017. if(flags & SWS_FAST_BILINEAR)
  2018. MSG_V("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
  2019. else
  2020. MSG_V("SwScaler: using C scaler for horizontal scaling\n");
  2021. #endif
  2022. }
  2023. if(isPlanarYUV(dstFormat))
  2024. {
  2025. if(c->vLumFilterSize==1)
  2026. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2027. else
  2028. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2029. }
  2030. else
  2031. {
  2032. if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
  2033. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  2034. "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",(flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2035. else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
  2036. MSG_V("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2037. else
  2038. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2039. }
  2040. if(dstFormat==PIX_FMT_BGR24)
  2041. MSG_V("SwScaler: using %s YV12->BGR24 Converter\n",
  2042. (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
  2043. else if(dstFormat==PIX_FMT_RGB32)
  2044. MSG_V("SwScaler: using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2045. else if(dstFormat==PIX_FMT_BGR565)
  2046. MSG_V("SwScaler: using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2047. else if(dstFormat==PIX_FMT_BGR555)
  2048. MSG_V("SwScaler: using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2049. MSG_V("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  2050. }
  2051. if(flags & SWS_PRINT_INFO)
  2052. {
  2053. MSG_DBG2("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2054. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  2055. MSG_DBG2("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2056. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  2057. }
  2058. c->swScale= getSwsFunc(flags);
  2059. return c;
  2060. }
  2061. /**
  2062. * swscale warper, so we don't need to export the SwsContext.
  2063. * assumes planar YUV to be in YUV order instead of YVU
  2064. */
  2065. int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  2066. int srcSliceH, uint8_t* dst[], int dstStride[]){
  2067. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  2068. MSG_ERR("swScaler: slices start in the middle!\n");
  2069. return 0;
  2070. }
  2071. if (c->sliceDir == 0) {
  2072. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  2073. }
  2074. // copy strides, so they can safely be modified
  2075. if (c->sliceDir == 1) {
  2076. // slices go from top to bottom
  2077. int srcStride2[3]= {srcStride[0], srcStride[1], srcStride[2]};
  2078. int dstStride2[3]= {dstStride[0], dstStride[1], dstStride[2]};
  2079. return c->swScale(c, src, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
  2080. } else {
  2081. // slices go from bottom to top => we flip the image internally
  2082. uint8_t* src2[3]= {src[0] + (srcSliceH-1)*srcStride[0],
  2083. src[1] + ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1],
  2084. src[2] + ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2]
  2085. };
  2086. uint8_t* dst2[3]= {dst[0] + (c->dstH-1)*dstStride[0],
  2087. dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
  2088. dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2]};
  2089. int srcStride2[3]= {-srcStride[0], -srcStride[1], -srcStride[2]};
  2090. int dstStride2[3]= {-dstStride[0], -dstStride[1], -dstStride[2]};
  2091. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  2092. }
  2093. }
  2094. /**
  2095. * swscale warper, so we don't need to export the SwsContext
  2096. */
  2097. int sws_scale(SwsContext *c, uint8_t* srcParam[], int srcStride[], int srcSliceY,
  2098. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  2099. uint8_t *src[3];
  2100. uint8_t *dst[3];
  2101. src[0] = srcParam[0]; src[1] = srcParam[1]; src[2] = srcParam[2];
  2102. dst[0] = dstParam[0]; dst[1] = dstParam[1]; dst[2] = dstParam[2];
  2103. //printf("sws: slice %d %d\n", srcSliceY, srcSliceH);
  2104. return c->swScale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
  2105. }
  2106. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  2107. float lumaSharpen, float chromaSharpen,
  2108. float chromaHShift, float chromaVShift,
  2109. int verbose)
  2110. {
  2111. SwsFilter *filter= av_malloc(sizeof(SwsFilter));
  2112. if(lumaGBlur!=0.0){
  2113. filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
  2114. filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
  2115. }else{
  2116. filter->lumH= sws_getIdentityVec();
  2117. filter->lumV= sws_getIdentityVec();
  2118. }
  2119. if(chromaGBlur!=0.0){
  2120. filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
  2121. filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
  2122. }else{
  2123. filter->chrH= sws_getIdentityVec();
  2124. filter->chrV= sws_getIdentityVec();
  2125. }
  2126. if(chromaSharpen!=0.0){
  2127. SwsVector *id= sws_getIdentityVec();
  2128. sws_scaleVec(filter->chrH, -chromaSharpen);
  2129. sws_scaleVec(filter->chrV, -chromaSharpen);
  2130. sws_addVec(filter->chrH, id);
  2131. sws_addVec(filter->chrV, id);
  2132. sws_freeVec(id);
  2133. }
  2134. if(lumaSharpen!=0.0){
  2135. SwsVector *id= sws_getIdentityVec();
  2136. sws_scaleVec(filter->lumH, -lumaSharpen);
  2137. sws_scaleVec(filter->lumV, -lumaSharpen);
  2138. sws_addVec(filter->lumH, id);
  2139. sws_addVec(filter->lumV, id);
  2140. sws_freeVec(id);
  2141. }
  2142. if(chromaHShift != 0.0)
  2143. sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
  2144. if(chromaVShift != 0.0)
  2145. sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
  2146. sws_normalizeVec(filter->chrH, 1.0);
  2147. sws_normalizeVec(filter->chrV, 1.0);
  2148. sws_normalizeVec(filter->lumH, 1.0);
  2149. sws_normalizeVec(filter->lumV, 1.0);
  2150. if(verbose) sws_printVec(filter->chrH);
  2151. if(verbose) sws_printVec(filter->lumH);
  2152. return filter;
  2153. }
  2154. /**
  2155. * returns a normalized gaussian curve used to filter stuff
  2156. * quality=3 is high quality, lowwer is lowwer quality
  2157. */
  2158. SwsVector *sws_getGaussianVec(double variance, double quality){
  2159. const int length= (int)(variance*quality + 0.5) | 1;
  2160. int i;
  2161. double *coeff= av_malloc(length*sizeof(double));
  2162. double middle= (length-1)*0.5;
  2163. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2164. vec->coeff= coeff;
  2165. vec->length= length;
  2166. for(i=0; i<length; i++)
  2167. {
  2168. double dist= i-middle;
  2169. coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
  2170. }
  2171. sws_normalizeVec(vec, 1.0);
  2172. return vec;
  2173. }
  2174. SwsVector *sws_getConstVec(double c, int length){
  2175. int i;
  2176. double *coeff= av_malloc(length*sizeof(double));
  2177. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2178. vec->coeff= coeff;
  2179. vec->length= length;
  2180. for(i=0; i<length; i++)
  2181. coeff[i]= c;
  2182. return vec;
  2183. }
  2184. SwsVector *sws_getIdentityVec(void){
  2185. return sws_getConstVec(1.0, 1);
  2186. }
  2187. double sws_dcVec(SwsVector *a){
  2188. int i;
  2189. double sum=0;
  2190. for(i=0; i<a->length; i++)
  2191. sum+= a->coeff[i];
  2192. return sum;
  2193. }
  2194. void sws_scaleVec(SwsVector *a, double scalar){
  2195. int i;
  2196. for(i=0; i<a->length; i++)
  2197. a->coeff[i]*= scalar;
  2198. }
  2199. void sws_normalizeVec(SwsVector *a, double height){
  2200. sws_scaleVec(a, height/sws_dcVec(a));
  2201. }
  2202. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
  2203. int length= a->length + b->length - 1;
  2204. double *coeff= av_malloc(length*sizeof(double));
  2205. int i, j;
  2206. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2207. vec->coeff= coeff;
  2208. vec->length= length;
  2209. for(i=0; i<length; i++) coeff[i]= 0.0;
  2210. for(i=0; i<a->length; i++)
  2211. {
  2212. for(j=0; j<b->length; j++)
  2213. {
  2214. coeff[i+j]+= a->coeff[i]*b->coeff[j];
  2215. }
  2216. }
  2217. return vec;
  2218. }
  2219. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
  2220. int length= FFMAX(a->length, b->length);
  2221. double *coeff= av_malloc(length*sizeof(double));
  2222. int i;
  2223. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2224. vec->coeff= coeff;
  2225. vec->length= length;
  2226. for(i=0; i<length; i++) coeff[i]= 0.0;
  2227. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2228. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  2229. return vec;
  2230. }
  2231. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
  2232. int length= FFMAX(a->length, b->length);
  2233. double *coeff= av_malloc(length*sizeof(double));
  2234. int i;
  2235. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2236. vec->coeff= coeff;
  2237. vec->length= length;
  2238. for(i=0; i<length; i++) coeff[i]= 0.0;
  2239. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2240. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  2241. return vec;
  2242. }
  2243. /* shift left / or right if "shift" is negative */
  2244. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
  2245. int length= a->length + ABS(shift)*2;
  2246. double *coeff= av_malloc(length*sizeof(double));
  2247. int i;
  2248. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2249. vec->coeff= coeff;
  2250. vec->length= length;
  2251. for(i=0; i<length; i++) coeff[i]= 0.0;
  2252. for(i=0; i<a->length; i++)
  2253. {
  2254. coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  2255. }
  2256. return vec;
  2257. }
  2258. void sws_shiftVec(SwsVector *a, int shift){
  2259. SwsVector *shifted= sws_getShiftedVec(a, shift);
  2260. av_free(a->coeff);
  2261. a->coeff= shifted->coeff;
  2262. a->length= shifted->length;
  2263. av_free(shifted);
  2264. }
  2265. void sws_addVec(SwsVector *a, SwsVector *b){
  2266. SwsVector *sum= sws_sumVec(a, b);
  2267. av_free(a->coeff);
  2268. a->coeff= sum->coeff;
  2269. a->length= sum->length;
  2270. av_free(sum);
  2271. }
  2272. void sws_subVec(SwsVector *a, SwsVector *b){
  2273. SwsVector *diff= sws_diffVec(a, b);
  2274. av_free(a->coeff);
  2275. a->coeff= diff->coeff;
  2276. a->length= diff->length;
  2277. av_free(diff);
  2278. }
  2279. void sws_convVec(SwsVector *a, SwsVector *b){
  2280. SwsVector *conv= sws_getConvVec(a, b);
  2281. av_free(a->coeff);
  2282. a->coeff= conv->coeff;
  2283. a->length= conv->length;
  2284. av_free(conv);
  2285. }
  2286. SwsVector *sws_cloneVec(SwsVector *a){
  2287. double *coeff= av_malloc(a->length*sizeof(double));
  2288. int i;
  2289. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2290. vec->coeff= coeff;
  2291. vec->length= a->length;
  2292. for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
  2293. return vec;
  2294. }
  2295. void sws_printVec(SwsVector *a){
  2296. int i;
  2297. double max=0;
  2298. double min=0;
  2299. double range;
  2300. for(i=0; i<a->length; i++)
  2301. if(a->coeff[i]>max) max= a->coeff[i];
  2302. for(i=0; i<a->length; i++)
  2303. if(a->coeff[i]<min) min= a->coeff[i];
  2304. range= max - min;
  2305. for(i=0; i<a->length; i++)
  2306. {
  2307. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  2308. MSG_DBG2("%1.3f ", a->coeff[i]);
  2309. for(;x>0; x--) MSG_DBG2(" ");
  2310. MSG_DBG2("|\n");
  2311. }
  2312. }
  2313. void sws_freeVec(SwsVector *a){
  2314. if(!a) return;
  2315. av_free(a->coeff);
  2316. a->coeff=NULL;
  2317. a->length=0;
  2318. av_free(a);
  2319. }
  2320. void sws_freeFilter(SwsFilter *filter){
  2321. if(!filter) return;
  2322. if(filter->lumH) sws_freeVec(filter->lumH);
  2323. if(filter->lumV) sws_freeVec(filter->lumV);
  2324. if(filter->chrH) sws_freeVec(filter->chrH);
  2325. if(filter->chrV) sws_freeVec(filter->chrV);
  2326. av_free(filter);
  2327. }
  2328. void sws_freeContext(SwsContext *c){
  2329. int i;
  2330. if(!c) return;
  2331. if(c->lumPixBuf)
  2332. {
  2333. for(i=0; i<c->vLumBufSize; i++)
  2334. {
  2335. av_free(c->lumPixBuf[i]);
  2336. c->lumPixBuf[i]=NULL;
  2337. }
  2338. av_free(c->lumPixBuf);
  2339. c->lumPixBuf=NULL;
  2340. }
  2341. if(c->chrPixBuf)
  2342. {
  2343. for(i=0; i<c->vChrBufSize; i++)
  2344. {
  2345. av_free(c->chrPixBuf[i]);
  2346. c->chrPixBuf[i]=NULL;
  2347. }
  2348. av_free(c->chrPixBuf);
  2349. c->chrPixBuf=NULL;
  2350. }
  2351. av_free(c->vLumFilter);
  2352. c->vLumFilter = NULL;
  2353. av_free(c->vChrFilter);
  2354. c->vChrFilter = NULL;
  2355. av_free(c->hLumFilter);
  2356. c->hLumFilter = NULL;
  2357. av_free(c->hChrFilter);
  2358. c->hChrFilter = NULL;
  2359. #ifdef HAVE_ALTIVEC
  2360. av_free(c->vYCoeffsBank);
  2361. c->vYCoeffsBank = NULL;
  2362. av_free(c->vCCoeffsBank);
  2363. c->vCCoeffsBank = NULL;
  2364. #endif
  2365. av_free(c->vLumFilterPos);
  2366. c->vLumFilterPos = NULL;
  2367. av_free(c->vChrFilterPos);
  2368. c->vChrFilterPos = NULL;
  2369. av_free(c->hLumFilterPos);
  2370. c->hLumFilterPos = NULL;
  2371. av_free(c->hChrFilterPos);
  2372. c->hChrFilterPos = NULL;
  2373. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  2374. #ifdef MAP_ANONYMOUS
  2375. if(c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
  2376. if(c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
  2377. #else
  2378. av_free(c->funnyYCode);
  2379. av_free(c->funnyUVCode);
  2380. #endif
  2381. c->funnyYCode=NULL;
  2382. c->funnyUVCode=NULL;
  2383. #endif /* defined(ARCH_X86) || defined(ARCH_X86_64) */
  2384. av_free(c->lumMmx2Filter);
  2385. c->lumMmx2Filter=NULL;
  2386. av_free(c->chrMmx2Filter);
  2387. c->chrMmx2Filter=NULL;
  2388. av_free(c->lumMmx2FilterPos);
  2389. c->lumMmx2FilterPos=NULL;
  2390. av_free(c->chrMmx2FilterPos);
  2391. c->chrMmx2FilterPos=NULL;
  2392. av_free(c->yuvTable);
  2393. c->yuvTable=NULL;
  2394. av_free(c);
  2395. }
  2396. /**
  2397. * Checks if context is valid or reallocs a new one instead.
  2398. * If context is NULL, just calls sws_getContext() to get a new one.
  2399. * Otherwise, checks if the parameters are the same already saved in context.
  2400. * If that is the case, returns the current context.
  2401. * Otherwise, frees context and gets a new one.
  2402. *
  2403. * Be warned that srcFilter, dstFilter are not checked, they are
  2404. * asumed to remain valid.
  2405. */
  2406. struct SwsContext *sws_getCachedContext(struct SwsContext *context,
  2407. int srcW, int srcH, int srcFormat,
  2408. int dstW, int dstH, int dstFormat, int flags,
  2409. SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
  2410. {
  2411. if (context != NULL) {
  2412. if ((context->srcW != srcW) || (context->srcH != srcH) ||
  2413. (context->srcFormat != srcFormat) ||
  2414. (context->dstW != dstW) || (context->dstH != dstH) ||
  2415. (context->dstFormat != dstFormat) || (context->flags != flags) ||
  2416. (context->param != param))
  2417. {
  2418. sws_freeContext(context);
  2419. context = NULL;
  2420. }
  2421. }
  2422. if (context == NULL) {
  2423. return sws_getContext(srcW, srcH, srcFormat,
  2424. dstW, dstH, dstFormat, flags,
  2425. srcFilter, dstFilter, param);
  2426. }
  2427. return context;
  2428. }