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.

2935 lines
98KB

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