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.

3064 lines
102KB

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