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.

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