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.

2753 lines
78KB

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