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.

2791 lines
80KB

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