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.

436 lines
16KB

  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
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License 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. #ifndef SWSCALE_SWSCALE_INTERNAL_H
  21. #define SWSCALE_SWSCALE_INTERNAL_H
  22. #include "config.h"
  23. #if HAVE_ALTIVEC_H
  24. #include <altivec.h>
  25. #endif
  26. #include "libavutil/avutil.h"
  27. #define STR(s) AV_TOSTRING(s) //AV_STRINGIFY is too long
  28. #define MAX_FILTER_SIZE 256
  29. #if ARCH_X86
  30. #define VOFW 5120
  31. #else
  32. #define VOFW 2048 // faster on PPC and not tested on others
  33. #endif
  34. #define VOF (VOFW*2)
  35. #if HAVE_BIGENDIAN
  36. #define ALT32_CORR (-1)
  37. #else
  38. #define ALT32_CORR 1
  39. #endif
  40. #if ARCH_X86_64
  41. # define APCK_PTR2 8
  42. # define APCK_COEF 16
  43. # define APCK_SIZE 24
  44. #else
  45. # define APCK_PTR2 4
  46. # define APCK_COEF 8
  47. # define APCK_SIZE 16
  48. #endif
  49. struct SwsContext;
  50. typedef int (*SwsFunc)(struct SwsContext *context, const uint8_t* src[],
  51. int srcStride[], int srcSliceY, int srcSliceH,
  52. uint8_t* dst[], int dstStride[]);
  53. /* This struct should be aligned on at least a 32-byte boundary. */
  54. typedef struct SwsContext {
  55. /**
  56. * info on struct for av_log
  57. */
  58. const AVClass *av_class;
  59. /**
  60. * Note that src, dst, srcStride, dstStride will be copied in the
  61. * sws_scale() wrapper so they can be freely modified here.
  62. */
  63. SwsFunc swScale;
  64. int srcW; ///< Width of source luma/alpha planes.
  65. int srcH; ///< Height of source luma/alpha planes.
  66. int dstH; ///< Height of destination luma/alpha planes.
  67. int chrSrcW; ///< Width of source chroma planes.
  68. int chrSrcH; ///< Height of source chroma planes.
  69. int chrDstW; ///< Width of destination chroma planes.
  70. int chrDstH; ///< Height of destination chroma planes.
  71. int lumXInc, chrXInc;
  72. int lumYInc, chrYInc;
  73. enum PixelFormat dstFormat; ///< Destination pixel format.
  74. enum PixelFormat srcFormat; ///< Source pixel format.
  75. int chrSrcHSubSample, chrSrcVSubSample;
  76. int chrDstHSubSample, chrDstVSubSample;
  77. int vChrDrop;
  78. int sliceDir;
  79. double param[2]; ///< Input parameters for scaling algorithms that need them.
  80. uint32_t pal_yuv[256];
  81. uint32_t pal_rgb[256];
  82. int16_t **lumPixBuf;
  83. int16_t **chrPixBuf;
  84. int16_t **alpPixBuf;
  85. int16_t *hLumFilter;
  86. int16_t *hLumFilterPos;
  87. int16_t *hChrFilter;
  88. int16_t *hChrFilterPos;
  89. int16_t *vLumFilter;
  90. int16_t *vLumFilterPos;
  91. int16_t *vChrFilter;
  92. int16_t *vChrFilterPos;
  93. uint8_t formatConvBuffer[VOF]; //FIXME dynamic allocation, but we have to change a lot of code for this to be useful
  94. int hLumFilterSize;
  95. int hChrFilterSize;
  96. int vLumFilterSize;
  97. int vChrFilterSize;
  98. int vLumBufSize;
  99. int vChrBufSize;
  100. int lumMmx2FilterCodeSize; ///< Runtime-generated MMX2 horizontal fast bilinear scaler code size for luma/alpha planes.
  101. int chrMmx2FilterCodeSize; ///< Runtime-generated MMX2 horizontal fast bilinear scaler code size for chroma planes.
  102. uint8_t *lumMmx2FilterCode; ///< Runtime-generated MMX2 horizontal fast bilinear scaler code for luma/alpha planes.
  103. uint8_t *chrMmx2FilterCode; ///< Runtime-generated MMX2 horizontal fast bilinear scaler code for chroma planes.
  104. int canMMX2BeUsed;
  105. int lastInLumBuf;
  106. int lastInChrBuf;
  107. int lumBufIndex;
  108. int chrBufIndex;
  109. int dstY; ///< Last destination vertical line output from last slice.
  110. int flags; ///< Flags passed by the user to select scaler algorithm, optimizations, subsampling, etc...
  111. void * yuvTable; // pointer to the yuv->rgb table start so it can be freed()
  112. uint8_t * table_rV[256];
  113. uint8_t * table_gU[256];
  114. int table_gV[256];
  115. uint8_t * table_bU[256];
  116. //Colorspace stuff
  117. int contrast, brightness, saturation; // for sws_getColorspaceDetails
  118. int srcColorspaceTable[4];
  119. int dstColorspaceTable[4];
  120. int srcRange; ///< 0 = MPG YUV range, 1 = JPG YUV range (source image).
  121. int dstRange; ///< 0 = MPG YUV range, 1 = JPG YUV range (destination image).
  122. int yuv2rgb_y_offset;
  123. int yuv2rgb_y_coeff;
  124. int yuv2rgb_v2r_coeff;
  125. int yuv2rgb_v2g_coeff;
  126. int yuv2rgb_u2g_coeff;
  127. int yuv2rgb_u2b_coeff;
  128. #define RED_DITHER "0*8"
  129. #define GREEN_DITHER "1*8"
  130. #define BLUE_DITHER "2*8"
  131. #define Y_COEFF "3*8"
  132. #define VR_COEFF "4*8"
  133. #define UB_COEFF "5*8"
  134. #define VG_COEFF "6*8"
  135. #define UG_COEFF "7*8"
  136. #define Y_OFFSET "8*8"
  137. #define U_OFFSET "9*8"
  138. #define V_OFFSET "10*8"
  139. #define LUM_MMX_FILTER_OFFSET "11*8"
  140. #define CHR_MMX_FILTER_OFFSET "11*8+4*4*256"
  141. #define DSTW_OFFSET "11*8+4*4*256*2" //do not change, it is hardcoded in the ASM
  142. #define ESP_OFFSET "11*8+4*4*256*2+8"
  143. #define VROUNDER_OFFSET "11*8+4*4*256*2+16"
  144. #define U_TEMP "11*8+4*4*256*2+24"
  145. #define V_TEMP "11*8+4*4*256*2+32"
  146. #define Y_TEMP "11*8+4*4*256*2+40"
  147. #define ALP_MMX_FILTER_OFFSET "11*8+4*4*256*2+48"
  148. DECLARE_ALIGNED(8, uint64_t, redDither);
  149. DECLARE_ALIGNED(8, uint64_t, greenDither);
  150. DECLARE_ALIGNED(8, uint64_t, blueDither);
  151. DECLARE_ALIGNED(8, uint64_t, yCoeff);
  152. DECLARE_ALIGNED(8, uint64_t, vrCoeff);
  153. DECLARE_ALIGNED(8, uint64_t, ubCoeff);
  154. DECLARE_ALIGNED(8, uint64_t, vgCoeff);
  155. DECLARE_ALIGNED(8, uint64_t, ugCoeff);
  156. DECLARE_ALIGNED(8, uint64_t, yOffset);
  157. DECLARE_ALIGNED(8, uint64_t, uOffset);
  158. DECLARE_ALIGNED(8, uint64_t, vOffset);
  159. int32_t lumMmxFilter[4*MAX_FILTER_SIZE];
  160. int32_t chrMmxFilter[4*MAX_FILTER_SIZE];
  161. int dstW;
  162. DECLARE_ALIGNED(8, uint64_t, esp);
  163. DECLARE_ALIGNED(8, uint64_t, vRounder);
  164. DECLARE_ALIGNED(8, uint64_t, u_temp);
  165. DECLARE_ALIGNED(8, uint64_t, v_temp);
  166. DECLARE_ALIGNED(8, uint64_t, y_temp);
  167. int32_t alpMmxFilter[4*MAX_FILTER_SIZE];
  168. #if HAVE_ALTIVEC
  169. vector signed short CY;
  170. vector signed short CRV;
  171. vector signed short CBU;
  172. vector signed short CGU;
  173. vector signed short CGV;
  174. vector signed short OY;
  175. vector unsigned short CSHIFT;
  176. vector signed short *vYCoeffsBank, *vCCoeffsBank;
  177. #endif
  178. #if ARCH_BFIN
  179. DECLARE_ALIGNED(4, uint32_t, oy);
  180. DECLARE_ALIGNED(4, uint32_t, oc);
  181. DECLARE_ALIGNED(4, uint32_t, zero);
  182. DECLARE_ALIGNED(4, uint32_t, cy);
  183. DECLARE_ALIGNED(4, uint32_t, crv);
  184. DECLARE_ALIGNED(4, uint32_t, rmask);
  185. DECLARE_ALIGNED(4, uint32_t, cbu);
  186. DECLARE_ALIGNED(4, uint32_t, bmask);
  187. DECLARE_ALIGNED(4, uint32_t, cgu);
  188. DECLARE_ALIGNED(4, uint32_t, cgv);
  189. DECLARE_ALIGNED(4, uint32_t, gmask);
  190. #endif
  191. #if HAVE_VIS
  192. DECLARE_ALIGNED(8, uint64_t, sparc_coeffs[10]);
  193. #endif
  194. /* function pointers for swScale() */
  195. void (*yuv2nv12X )(struct SwsContext *c,
  196. const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  197. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  198. uint8_t *dest, uint8_t *uDest,
  199. int dstW, int chrDstW, int dstFormat);
  200. void (*yuv2yuv1 )(struct SwsContext *c,
  201. const int16_t *lumSrc, const int16_t *chrSrc, const int16_t *alpSrc,
  202. uint8_t *dest,
  203. uint8_t *uDest, uint8_t *vDest, uint8_t *aDest,
  204. long dstW, long chrDstW);
  205. void (*yuv2yuvX )(struct SwsContext *c,
  206. const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  207. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  208. const int16_t **alpSrc,
  209. uint8_t *dest,
  210. uint8_t *uDest, uint8_t *vDest, uint8_t *aDest,
  211. long dstW, long chrDstW);
  212. void (*yuv2packed1)(struct SwsContext *c,
  213. const uint16_t *buf0,
  214. const uint16_t *uvbuf0, const uint16_t *uvbuf1,
  215. const uint16_t *abuf0,
  216. uint8_t *dest,
  217. int dstW, int uvalpha, int dstFormat, int flags, int y);
  218. void (*yuv2packed2)(struct SwsContext *c,
  219. const uint16_t *buf0, const uint16_t *buf1,
  220. const uint16_t *uvbuf0, const uint16_t *uvbuf1,
  221. const uint16_t *abuf0, const uint16_t *abuf1,
  222. uint8_t *dest,
  223. int dstW, int yalpha, int uvalpha, int y);
  224. void (*yuv2packedX)(struct SwsContext *c,
  225. const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  226. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  227. const int16_t **alpSrc, uint8_t *dest,
  228. long dstW, long dstY);
  229. void (*lumToYV12)(uint8_t *dst, const uint8_t *src,
  230. long width, uint32_t *pal); ///< Unscaled conversion of luma plane to YV12 for horizontal scaler.
  231. void (*alpToYV12)(uint8_t *dst, const uint8_t *src,
  232. long width, uint32_t *pal); ///< Unscaled conversion of alpha plane to YV12 for horizontal scaler.
  233. void (*chrToYV12)(uint8_t *dstU, uint8_t *dstV,
  234. const uint8_t *src1, const uint8_t *src2,
  235. long width, uint32_t *pal); ///< Unscaled conversion of chroma planes to YV12 for horizontal scaler.
  236. void (*hyscale_fast)(struct SwsContext *c,
  237. int16_t *dst, long dstWidth,
  238. const uint8_t *src, int srcW, int xInc);
  239. void (*hcscale_fast)(struct SwsContext *c,
  240. int16_t *dst, long dstWidth,
  241. const uint8_t *src1, const uint8_t *src2,
  242. int srcW, int xInc);
  243. void (*hScale)(int16_t *dst, int dstW, const uint8_t *src, int srcW,
  244. int xInc, const int16_t *filter, const int16_t *filterPos,
  245. long filterSize);
  246. void (*lumConvertRange)(uint16_t *dst, int width); ///< Color range conversion function for luma plane if needed.
  247. void (*chrConvertRange)(uint16_t *dst, int width); ///< Color range conversion function for chroma planes if needed.
  248. int lumSrcOffset; ///< Offset given to luma src pointers passed to horizontal input functions.
  249. int chrSrcOffset; ///< Offset given to chroma src pointers passed to horizontal input functions.
  250. int alpSrcOffset; ///< Offset given to alpha src pointers passed to horizontal input functions.
  251. int needs_hcscale; ///< Set if there are chroma planes to be converted.
  252. } SwsContext;
  253. //FIXME check init (where 0)
  254. SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c);
  255. int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
  256. int fullRange, int brightness,
  257. int contrast, int saturation);
  258. void ff_yuv2rgb_init_tables_altivec(SwsContext *c, const int inv_table[4],
  259. int brightness, int contrast, int saturation);
  260. SwsFunc ff_yuv2rgb_init_mmx(SwsContext *c);
  261. SwsFunc ff_yuv2rgb_init_vis(SwsContext *c);
  262. SwsFunc ff_yuv2rgb_init_mlib(SwsContext *c);
  263. SwsFunc ff_yuv2rgb_init_altivec(SwsContext *c);
  264. SwsFunc ff_yuv2rgb_get_func_ptr_bfin(SwsContext *c);
  265. void ff_bfin_get_unscaled_swscale(SwsContext *c);
  266. void ff_yuv2packedX_altivec(SwsContext *c,
  267. const int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  268. const int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  269. uint8_t *dest, int dstW, int dstY);
  270. const char *sws_format_name(enum PixelFormat format);
  271. //FIXME replace this with something faster
  272. #define is16BPS(x) ( \
  273. (x)==PIX_FMT_GRAY16BE \
  274. || (x)==PIX_FMT_GRAY16LE \
  275. || (x)==PIX_FMT_RGB48BE \
  276. || (x)==PIX_FMT_RGB48LE \
  277. || (x)==PIX_FMT_YUV420P16LE \
  278. || (x)==PIX_FMT_YUV422P16LE \
  279. || (x)==PIX_FMT_YUV444P16LE \
  280. || (x)==PIX_FMT_YUV420P16BE \
  281. || (x)==PIX_FMT_YUV422P16BE \
  282. || (x)==PIX_FMT_YUV444P16BE \
  283. )
  284. #define isBE(x) ((x)&1)
  285. #define isPlanar8YUV(x) ( \
  286. (x)==PIX_FMT_YUV410P \
  287. || (x)==PIX_FMT_YUV420P \
  288. || (x)==PIX_FMT_YUVA420P \
  289. || (x)==PIX_FMT_YUV411P \
  290. || (x)==PIX_FMT_YUV422P \
  291. || (x)==PIX_FMT_YUV444P \
  292. || (x)==PIX_FMT_YUV440P \
  293. || (x)==PIX_FMT_NV12 \
  294. || (x)==PIX_FMT_NV21 \
  295. )
  296. #define isPlanarYUV(x) ( \
  297. isPlanar8YUV(x) \
  298. || (x)==PIX_FMT_YUV420P16LE \
  299. || (x)==PIX_FMT_YUV422P16LE \
  300. || (x)==PIX_FMT_YUV444P16LE \
  301. || (x)==PIX_FMT_YUV420P16BE \
  302. || (x)==PIX_FMT_YUV422P16BE \
  303. || (x)==PIX_FMT_YUV444P16BE \
  304. )
  305. #define isYUV(x) ( \
  306. (x)==PIX_FMT_UYVY422 \
  307. || (x)==PIX_FMT_YUYV422 \
  308. || isPlanarYUV(x) \
  309. )
  310. #define isGray(x) ( \
  311. (x)==PIX_FMT_GRAY8 \
  312. || (x)==PIX_FMT_GRAY16BE \
  313. || (x)==PIX_FMT_GRAY16LE \
  314. )
  315. #define isGray16(x) ( \
  316. (x)==PIX_FMT_GRAY16BE \
  317. || (x)==PIX_FMT_GRAY16LE \
  318. )
  319. #define isRGB(x) ( \
  320. (x)==PIX_FMT_RGB48BE \
  321. || (x)==PIX_FMT_RGB48LE \
  322. || (x)==PIX_FMT_RGB32 \
  323. || (x)==PIX_FMT_RGB32_1 \
  324. || (x)==PIX_FMT_RGB24 \
  325. || (x)==PIX_FMT_RGB565 \
  326. || (x)==PIX_FMT_RGB555 \
  327. || (x)==PIX_FMT_RGB8 \
  328. || (x)==PIX_FMT_RGB4 \
  329. || (x)==PIX_FMT_RGB4_BYTE \
  330. || (x)==PIX_FMT_MONOBLACK \
  331. || (x)==PIX_FMT_MONOWHITE \
  332. )
  333. #define isBGR(x) ( \
  334. (x)==PIX_FMT_BGR32 \
  335. || (x)==PIX_FMT_BGR32_1 \
  336. || (x)==PIX_FMT_BGR24 \
  337. || (x)==PIX_FMT_BGR565 \
  338. || (x)==PIX_FMT_BGR555 \
  339. || (x)==PIX_FMT_BGR8 \
  340. || (x)==PIX_FMT_BGR4 \
  341. || (x)==PIX_FMT_BGR4_BYTE \
  342. || (x)==PIX_FMT_MONOBLACK \
  343. || (x)==PIX_FMT_MONOWHITE \
  344. )
  345. #define isALPHA(x) ( \
  346. (x)==PIX_FMT_BGR32 \
  347. || (x)==PIX_FMT_BGR32_1 \
  348. || (x)==PIX_FMT_RGB32 \
  349. || (x)==PIX_FMT_RGB32_1 \
  350. || (x)==PIX_FMT_YUVA420P \
  351. )
  352. static inline int fmt_depth(enum PixelFormat fmt)
  353. {
  354. switch(fmt) {
  355. case PIX_FMT_RGB48BE:
  356. case PIX_FMT_RGB48LE:
  357. return 48;
  358. case PIX_FMT_BGRA:
  359. case PIX_FMT_ABGR:
  360. case PIX_FMT_RGBA:
  361. case PIX_FMT_ARGB:
  362. return 32;
  363. case PIX_FMT_BGR24:
  364. case PIX_FMT_RGB24:
  365. return 24;
  366. case PIX_FMT_BGR565:
  367. case PIX_FMT_RGB565:
  368. case PIX_FMT_GRAY16BE:
  369. case PIX_FMT_GRAY16LE:
  370. return 16;
  371. case PIX_FMT_BGR555:
  372. case PIX_FMT_RGB555:
  373. return 15;
  374. case PIX_FMT_BGR8:
  375. case PIX_FMT_RGB8:
  376. return 8;
  377. case PIX_FMT_BGR4:
  378. case PIX_FMT_RGB4:
  379. case PIX_FMT_BGR4_BYTE:
  380. case PIX_FMT_RGB4_BYTE:
  381. return 4;
  382. case PIX_FMT_MONOBLACK:
  383. case PIX_FMT_MONOWHITE:
  384. return 1;
  385. default:
  386. return 0;
  387. }
  388. }
  389. extern const uint64_t ff_dither4[2];
  390. extern const uint64_t ff_dither8[2];
  391. extern const AVClass sws_context_class;
  392. #endif /* SWSCALE_SWSCALE_INTERNAL_H */