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.

464 lines
20KB

  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; ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in source image.
  76. int chrSrcVSubSample; ///< Binary logarithm of vertical subsampling factor between luma/alpha and chroma planes in source image.
  77. int chrDstHSubSample; ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in destination image.
  78. int chrDstVSubSample; ///< Binary logarithm of vertical subsampling factor between luma/alpha and chroma planes in destination image.
  79. int vChrDrop; ///< Binary logarithm of extra vertical subsampling factor in source image chroma planes specified by user.
  80. int sliceDir; ///< Direction that slices are fed to the scaler (1 = top-to-bottom, -1 = bottom-to-top).
  81. double param[2]; ///< Input parameters for scaling algorithms that need them.
  82. uint32_t pal_yuv[256];
  83. uint32_t pal_rgb[256];
  84. /**
  85. * @name Scaled horizontal lines ring buffer.
  86. * The horizontal scaler keeps just enough scaled lines in a ring buffer
  87. * so they may be passed to the vertical scaler. The pointers to the
  88. * allocated buffers for each line are duplicated in sequence in the ring
  89. * buffer to simplify indexing and avoid wrapping around between lines
  90. * inside the vertical scaler code. The wrapping is done before the
  91. * vertical scaler is called.
  92. */
  93. //@{
  94. int16_t **lumPixBuf; ///< Ring buffer for scaled horizontal luma plane lines to be fed to the vertical scaler.
  95. int16_t **chrPixBuf; ///< Ring buffer for scaled horizontal chroma plane lines to be fed to the vertical scaler.
  96. int16_t **alpPixBuf; ///< Ring buffer for scaled horizontal alpha plane lines to be fed to the vertical scaler.
  97. int vLumBufSize; ///< Number of vertical luma/alpha lines allocated in the ring buffer.
  98. int vChrBufSize; ///< Number of vertical chroma lines allocated in the ring buffer.
  99. int lastInLumBuf; ///< Last scaled horizontal luma/alpha line from source in the ring buffer.
  100. int lastInChrBuf; ///< Last scaled horizontal chroma line from source in the ring buffer.
  101. int lumBufIndex; ///< Index in ring buffer of the last scaled horizontal luma/alpha line from source.
  102. int chrBufIndex; ///< Index in ring buffer of the last scaled horizontal chroma line from source.
  103. //@}
  104. uint8_t formatConvBuffer[VOF]; //FIXME dynamic allocation, but we have to change a lot of code for this to be useful
  105. /**
  106. * @name Horizontal and vertical filters.
  107. * To better understand the following fields, here is a pseudo-code of
  108. * their usage in filtering a horizontal line:
  109. * @code
  110. * for (i = 0; i < width; i++) {
  111. * dst[i] = 0;
  112. * for (j = 0; j < filterSize; j++)
  113. * dst[i] += src[ filterPos[i] + j ] * filter[ filterSize * i + j ];
  114. * dst[i] >>= FRAC_BITS; // The actual implementation is fixed-point.
  115. * }
  116. * @endcode
  117. */
  118. //@{
  119. int16_t *hLumFilter; ///< Array of horizontal filter coefficients for luma/alpha planes.
  120. int16_t *hChrFilter; ///< Array of horizontal filter coefficients for chroma planes.
  121. int16_t *vLumFilter; ///< Array of vertical filter coefficients for luma/alpha planes.
  122. int16_t *vChrFilter; ///< Array of vertical filter coefficients for chroma planes.
  123. int16_t *hLumFilterPos; ///< Array of horizontal filter starting positions for each dst[i] for luma/alpha planes.
  124. int16_t *hChrFilterPos; ///< Array of horizontal filter starting positions for each dst[i] for chroma planes.
  125. int16_t *vLumFilterPos; ///< Array of vertical filter starting positions for each dst[i] for luma/alpha planes.
  126. int16_t *vChrFilterPos; ///< Array of vertical filter starting positions for each dst[i] for chroma planes.
  127. int hLumFilterSize; ///< Horizontal filter size for luma/alpha pixels.
  128. int hChrFilterSize; ///< Horizontal filter size for chroma pixels.
  129. int vLumFilterSize; ///< Vertical filter size for luma/alpha pixels.
  130. int vChrFilterSize; ///< Vertical filter size for chroma pixels.
  131. //@}
  132. int lumMmx2FilterCodeSize; ///< Runtime-generated MMX2 horizontal fast bilinear scaler code size for luma/alpha planes.
  133. int chrMmx2FilterCodeSize; ///< Runtime-generated MMX2 horizontal fast bilinear scaler code size for chroma planes.
  134. uint8_t *lumMmx2FilterCode; ///< Runtime-generated MMX2 horizontal fast bilinear scaler code for luma/alpha planes.
  135. uint8_t *chrMmx2FilterCode; ///< Runtime-generated MMX2 horizontal fast bilinear scaler code for chroma planes.
  136. int canMMX2BeUsed;
  137. int dstY; ///< Last destination vertical line output from last slice.
  138. int flags; ///< Flags passed by the user to select scaler algorithm, optimizations, subsampling, etc...
  139. void * yuvTable; // pointer to the yuv->rgb table start so it can be freed()
  140. uint8_t * table_rV[256];
  141. uint8_t * table_gU[256];
  142. int table_gV[256];
  143. uint8_t * table_bU[256];
  144. //Colorspace stuff
  145. int contrast, brightness, saturation; // for sws_getColorspaceDetails
  146. int srcColorspaceTable[4];
  147. int dstColorspaceTable[4];
  148. int srcRange; ///< 0 = MPG YUV range, 1 = JPG YUV range (source image).
  149. int dstRange; ///< 0 = MPG YUV range, 1 = JPG YUV range (destination image).
  150. int yuv2rgb_y_offset;
  151. int yuv2rgb_y_coeff;
  152. int yuv2rgb_v2r_coeff;
  153. int yuv2rgb_v2g_coeff;
  154. int yuv2rgb_u2g_coeff;
  155. int yuv2rgb_u2b_coeff;
  156. #define RED_DITHER "0*8"
  157. #define GREEN_DITHER "1*8"
  158. #define BLUE_DITHER "2*8"
  159. #define Y_COEFF "3*8"
  160. #define VR_COEFF "4*8"
  161. #define UB_COEFF "5*8"
  162. #define VG_COEFF "6*8"
  163. #define UG_COEFF "7*8"
  164. #define Y_OFFSET "8*8"
  165. #define U_OFFSET "9*8"
  166. #define V_OFFSET "10*8"
  167. #define LUM_MMX_FILTER_OFFSET "11*8"
  168. #define CHR_MMX_FILTER_OFFSET "11*8+4*4*256"
  169. #define DSTW_OFFSET "11*8+4*4*256*2" //do not change, it is hardcoded in the ASM
  170. #define ESP_OFFSET "11*8+4*4*256*2+8"
  171. #define VROUNDER_OFFSET "11*8+4*4*256*2+16"
  172. #define U_TEMP "11*8+4*4*256*2+24"
  173. #define V_TEMP "11*8+4*4*256*2+32"
  174. #define Y_TEMP "11*8+4*4*256*2+40"
  175. #define ALP_MMX_FILTER_OFFSET "11*8+4*4*256*2+48"
  176. DECLARE_ALIGNED(8, uint64_t, redDither);
  177. DECLARE_ALIGNED(8, uint64_t, greenDither);
  178. DECLARE_ALIGNED(8, uint64_t, blueDither);
  179. DECLARE_ALIGNED(8, uint64_t, yCoeff);
  180. DECLARE_ALIGNED(8, uint64_t, vrCoeff);
  181. DECLARE_ALIGNED(8, uint64_t, ubCoeff);
  182. DECLARE_ALIGNED(8, uint64_t, vgCoeff);
  183. DECLARE_ALIGNED(8, uint64_t, ugCoeff);
  184. DECLARE_ALIGNED(8, uint64_t, yOffset);
  185. DECLARE_ALIGNED(8, uint64_t, uOffset);
  186. DECLARE_ALIGNED(8, uint64_t, vOffset);
  187. int32_t lumMmxFilter[4*MAX_FILTER_SIZE];
  188. int32_t chrMmxFilter[4*MAX_FILTER_SIZE];
  189. int dstW; ///< Width of destination luma/alpha planes.
  190. DECLARE_ALIGNED(8, uint64_t, esp);
  191. DECLARE_ALIGNED(8, uint64_t, vRounder);
  192. DECLARE_ALIGNED(8, uint64_t, u_temp);
  193. DECLARE_ALIGNED(8, uint64_t, v_temp);
  194. DECLARE_ALIGNED(8, uint64_t, y_temp);
  195. int32_t alpMmxFilter[4*MAX_FILTER_SIZE];
  196. #if HAVE_ALTIVEC
  197. vector signed short CY;
  198. vector signed short CRV;
  199. vector signed short CBU;
  200. vector signed short CGU;
  201. vector signed short CGV;
  202. vector signed short OY;
  203. vector unsigned short CSHIFT;
  204. vector signed short *vYCoeffsBank, *vCCoeffsBank;
  205. #endif
  206. #if ARCH_BFIN
  207. DECLARE_ALIGNED(4, uint32_t, oy);
  208. DECLARE_ALIGNED(4, uint32_t, oc);
  209. DECLARE_ALIGNED(4, uint32_t, zero);
  210. DECLARE_ALIGNED(4, uint32_t, cy);
  211. DECLARE_ALIGNED(4, uint32_t, crv);
  212. DECLARE_ALIGNED(4, uint32_t, rmask);
  213. DECLARE_ALIGNED(4, uint32_t, cbu);
  214. DECLARE_ALIGNED(4, uint32_t, bmask);
  215. DECLARE_ALIGNED(4, uint32_t, cgu);
  216. DECLARE_ALIGNED(4, uint32_t, cgv);
  217. DECLARE_ALIGNED(4, uint32_t, gmask);
  218. #endif
  219. #if HAVE_VIS
  220. DECLARE_ALIGNED(8, uint64_t, sparc_coeffs[10]);
  221. #endif
  222. /* function pointers for swScale() */
  223. void (*yuv2nv12X )(struct SwsContext *c,
  224. const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  225. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  226. uint8_t *dest, uint8_t *uDest,
  227. int dstW, int chrDstW, int dstFormat);
  228. void (*yuv2yuv1 )(struct SwsContext *c,
  229. const int16_t *lumSrc, const int16_t *chrSrc, const int16_t *alpSrc,
  230. uint8_t *dest,
  231. uint8_t *uDest, uint8_t *vDest, uint8_t *aDest,
  232. long dstW, long chrDstW);
  233. void (*yuv2yuvX )(struct SwsContext *c,
  234. const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  235. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  236. const int16_t **alpSrc,
  237. uint8_t *dest,
  238. uint8_t *uDest, uint8_t *vDest, uint8_t *aDest,
  239. long dstW, long chrDstW);
  240. void (*yuv2packed1)(struct SwsContext *c,
  241. const uint16_t *buf0,
  242. const uint16_t *uvbuf0, const uint16_t *uvbuf1,
  243. const uint16_t *abuf0,
  244. uint8_t *dest,
  245. int dstW, int uvalpha, int dstFormat, int flags, int y);
  246. void (*yuv2packed2)(struct SwsContext *c,
  247. const uint16_t *buf0, const uint16_t *buf1,
  248. const uint16_t *uvbuf0, const uint16_t *uvbuf1,
  249. const uint16_t *abuf0, const uint16_t *abuf1,
  250. uint8_t *dest,
  251. int dstW, int yalpha, int uvalpha, int y);
  252. void (*yuv2packedX)(struct SwsContext *c,
  253. const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  254. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  255. const int16_t **alpSrc, uint8_t *dest,
  256. long dstW, long dstY);
  257. void (*lumToYV12)(uint8_t *dst, const uint8_t *src,
  258. long width, uint32_t *pal); ///< Unscaled conversion of luma plane to YV12 for horizontal scaler.
  259. void (*alpToYV12)(uint8_t *dst, const uint8_t *src,
  260. long width, uint32_t *pal); ///< Unscaled conversion of alpha plane to YV12 for horizontal scaler.
  261. void (*chrToYV12)(uint8_t *dstU, uint8_t *dstV,
  262. const uint8_t *src1, const uint8_t *src2,
  263. long width, uint32_t *pal); ///< Unscaled conversion of chroma planes to YV12 for horizontal scaler.
  264. void (*hyscale_fast)(struct SwsContext *c,
  265. int16_t *dst, long dstWidth,
  266. const uint8_t *src, int srcW, int xInc);
  267. void (*hcscale_fast)(struct SwsContext *c,
  268. int16_t *dst, long dstWidth,
  269. const uint8_t *src1, const uint8_t *src2,
  270. int srcW, int xInc);
  271. void (*hScale)(int16_t *dst, int dstW, const uint8_t *src, int srcW,
  272. int xInc, const int16_t *filter, const int16_t *filterPos,
  273. long filterSize);
  274. void (*lumConvertRange)(uint16_t *dst, int width); ///< Color range conversion function for luma plane if needed.
  275. void (*chrConvertRange)(uint16_t *dst, int width); ///< Color range conversion function for chroma planes if needed.
  276. int lumSrcOffset; ///< Offset given to luma src pointers passed to horizontal input functions.
  277. int chrSrcOffset; ///< Offset given to chroma src pointers passed to horizontal input functions.
  278. int alpSrcOffset; ///< Offset given to alpha src pointers passed to horizontal input functions.
  279. int needs_hcscale; ///< Set if there are chroma planes to be converted.
  280. } SwsContext;
  281. //FIXME check init (where 0)
  282. SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c);
  283. int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
  284. int fullRange, int brightness,
  285. int contrast, int saturation);
  286. void ff_yuv2rgb_init_tables_altivec(SwsContext *c, const int inv_table[4],
  287. int brightness, int contrast, int saturation);
  288. SwsFunc ff_yuv2rgb_init_mmx(SwsContext *c);
  289. SwsFunc ff_yuv2rgb_init_vis(SwsContext *c);
  290. SwsFunc ff_yuv2rgb_init_mlib(SwsContext *c);
  291. SwsFunc ff_yuv2rgb_init_altivec(SwsContext *c);
  292. SwsFunc ff_yuv2rgb_get_func_ptr_bfin(SwsContext *c);
  293. void ff_bfin_get_unscaled_swscale(SwsContext *c);
  294. void ff_yuv2packedX_altivec(SwsContext *c,
  295. const int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  296. const int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  297. uint8_t *dest, int dstW, int dstY);
  298. const char *sws_format_name(enum PixelFormat format);
  299. //FIXME replace this with something faster
  300. #define is16BPS(x) ( \
  301. (x)==PIX_FMT_GRAY16BE \
  302. || (x)==PIX_FMT_GRAY16LE \
  303. || (x)==PIX_FMT_RGB48BE \
  304. || (x)==PIX_FMT_RGB48LE \
  305. || (x)==PIX_FMT_YUV420P16LE \
  306. || (x)==PIX_FMT_YUV422P16LE \
  307. || (x)==PIX_FMT_YUV444P16LE \
  308. || (x)==PIX_FMT_YUV420P16BE \
  309. || (x)==PIX_FMT_YUV422P16BE \
  310. || (x)==PIX_FMT_YUV444P16BE \
  311. )
  312. #define isBE(x) ((x)&1)
  313. #define isPlanar8YUV(x) ( \
  314. (x)==PIX_FMT_YUV410P \
  315. || (x)==PIX_FMT_YUV420P \
  316. || (x)==PIX_FMT_YUVA420P \
  317. || (x)==PIX_FMT_YUV411P \
  318. || (x)==PIX_FMT_YUV422P \
  319. || (x)==PIX_FMT_YUV444P \
  320. || (x)==PIX_FMT_YUV440P \
  321. || (x)==PIX_FMT_NV12 \
  322. || (x)==PIX_FMT_NV21 \
  323. )
  324. #define isPlanarYUV(x) ( \
  325. isPlanar8YUV(x) \
  326. || (x)==PIX_FMT_YUV420P16LE \
  327. || (x)==PIX_FMT_YUV422P16LE \
  328. || (x)==PIX_FMT_YUV444P16LE \
  329. || (x)==PIX_FMT_YUV420P16BE \
  330. || (x)==PIX_FMT_YUV422P16BE \
  331. || (x)==PIX_FMT_YUV444P16BE \
  332. )
  333. #define isYUV(x) ( \
  334. (x)==PIX_FMT_UYVY422 \
  335. || (x)==PIX_FMT_YUYV422 \
  336. || isPlanarYUV(x) \
  337. )
  338. #define isGray(x) ( \
  339. (x)==PIX_FMT_GRAY8 \
  340. || (x)==PIX_FMT_GRAY16BE \
  341. || (x)==PIX_FMT_GRAY16LE \
  342. )
  343. #define isGray16(x) ( \
  344. (x)==PIX_FMT_GRAY16BE \
  345. || (x)==PIX_FMT_GRAY16LE \
  346. )
  347. #define isRGB(x) ( \
  348. (x)==PIX_FMT_RGB48BE \
  349. || (x)==PIX_FMT_RGB48LE \
  350. || (x)==PIX_FMT_RGB32 \
  351. || (x)==PIX_FMT_RGB32_1 \
  352. || (x)==PIX_FMT_RGB24 \
  353. || (x)==PIX_FMT_RGB565 \
  354. || (x)==PIX_FMT_RGB555 \
  355. || (x)==PIX_FMT_RGB8 \
  356. || (x)==PIX_FMT_RGB4 \
  357. || (x)==PIX_FMT_RGB4_BYTE \
  358. || (x)==PIX_FMT_MONOBLACK \
  359. || (x)==PIX_FMT_MONOWHITE \
  360. )
  361. #define isBGR(x) ( \
  362. (x)==PIX_FMT_BGR32 \
  363. || (x)==PIX_FMT_BGR32_1 \
  364. || (x)==PIX_FMT_BGR24 \
  365. || (x)==PIX_FMT_BGR565 \
  366. || (x)==PIX_FMT_BGR555 \
  367. || (x)==PIX_FMT_BGR8 \
  368. || (x)==PIX_FMT_BGR4 \
  369. || (x)==PIX_FMT_BGR4_BYTE \
  370. || (x)==PIX_FMT_MONOBLACK \
  371. || (x)==PIX_FMT_MONOWHITE \
  372. )
  373. #define isALPHA(x) ( \
  374. (x)==PIX_FMT_BGR32 \
  375. || (x)==PIX_FMT_BGR32_1 \
  376. || (x)==PIX_FMT_RGB32 \
  377. || (x)==PIX_FMT_RGB32_1 \
  378. || (x)==PIX_FMT_YUVA420P \
  379. )
  380. static inline int fmt_depth(enum PixelFormat fmt)
  381. {
  382. switch(fmt) {
  383. case PIX_FMT_RGB48BE:
  384. case PIX_FMT_RGB48LE:
  385. return 48;
  386. case PIX_FMT_BGRA:
  387. case PIX_FMT_ABGR:
  388. case PIX_FMT_RGBA:
  389. case PIX_FMT_ARGB:
  390. return 32;
  391. case PIX_FMT_BGR24:
  392. case PIX_FMT_RGB24:
  393. return 24;
  394. case PIX_FMT_BGR565:
  395. case PIX_FMT_RGB565:
  396. case PIX_FMT_GRAY16BE:
  397. case PIX_FMT_GRAY16LE:
  398. return 16;
  399. case PIX_FMT_BGR555:
  400. case PIX_FMT_RGB555:
  401. return 15;
  402. case PIX_FMT_BGR8:
  403. case PIX_FMT_RGB8:
  404. return 8;
  405. case PIX_FMT_BGR4:
  406. case PIX_FMT_RGB4:
  407. case PIX_FMT_BGR4_BYTE:
  408. case PIX_FMT_RGB4_BYTE:
  409. return 4;
  410. case PIX_FMT_MONOBLACK:
  411. case PIX_FMT_MONOWHITE:
  412. return 1;
  413. default:
  414. return 0;
  415. }
  416. }
  417. extern const uint64_t ff_dither4[2];
  418. extern const uint64_t ff_dither8[2];
  419. extern const AVClass sws_context_class;
  420. #endif /* SWSCALE_SWSCALE_INTERNAL_H */