swscale_internal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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, 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, srcH, dstH;
  65. int chrSrcW, chrSrcH, chrDstW, chrDstH;
  66. int lumXInc, chrXInc;
  67. int lumYInc, chrYInc;
  68. enum PixelFormat dstFormat, srcFormat; ///< format 4:2:0 type is always YV12
  69. int origDstFormat, origSrcFormat; ///< format
  70. int chrSrcHSubSample, chrSrcVSubSample;
  71. int chrDstHSubSample, chrDstVSubSample;
  72. int vChrDrop;
  73. int sliceDir;
  74. double param[2];
  75. uint32_t pal_yuv[256];
  76. uint32_t pal_rgb[256];
  77. int16_t **lumPixBuf;
  78. int16_t **chrPixBuf;
  79. int16_t **alpPixBuf;
  80. int16_t *hLumFilter;
  81. int16_t *hLumFilterPos;
  82. int16_t *hChrFilter;
  83. int16_t *hChrFilterPos;
  84. int16_t *vLumFilter;
  85. int16_t *vLumFilterPos;
  86. int16_t *vChrFilter;
  87. int16_t *vChrFilterPos;
  88. uint8_t formatConvBuffer[VOF]; //FIXME dynamic allocation, but we have to change a lot of code for this to be useful
  89. int hLumFilterSize;
  90. int hChrFilterSize;
  91. int vLumFilterSize;
  92. int vChrFilterSize;
  93. int vLumBufSize;
  94. int vChrBufSize;
  95. uint8_t *funnyYCode;
  96. uint8_t *funnyUVCode;
  97. int32_t *lumMmx2FilterPos;
  98. int32_t *chrMmx2FilterPos;
  99. int16_t *lumMmx2Filter;
  100. int16_t *chrMmx2Filter;
  101. int canMMX2BeUsed;
  102. int lastInLumBuf;
  103. int lastInChrBuf;
  104. int lumBufIndex;
  105. int chrBufIndex;
  106. int dstY;
  107. int flags;
  108. void * yuvTable; // pointer to the yuv->rgb table start so it can be freed()
  109. uint8_t * table_rV[256];
  110. uint8_t * table_gU[256];
  111. int table_gV[256];
  112. uint8_t * table_bU[256];
  113. //Colorspace stuff
  114. int contrast, brightness, saturation; // for sws_getColorspaceDetails
  115. int srcColorspaceTable[4];
  116. int dstColorspaceTable[4];
  117. int srcRange, dstRange;
  118. int yuv2rgb_y_offset;
  119. int yuv2rgb_y_coeff;
  120. int yuv2rgb_v2r_coeff;
  121. int yuv2rgb_v2g_coeff;
  122. int yuv2rgb_u2g_coeff;
  123. int yuv2rgb_u2b_coeff;
  124. #define RED_DITHER "0*8"
  125. #define GREEN_DITHER "1*8"
  126. #define BLUE_DITHER "2*8"
  127. #define Y_COEFF "3*8"
  128. #define VR_COEFF "4*8"
  129. #define UB_COEFF "5*8"
  130. #define VG_COEFF "6*8"
  131. #define UG_COEFF "7*8"
  132. #define Y_OFFSET "8*8"
  133. #define U_OFFSET "9*8"
  134. #define V_OFFSET "10*8"
  135. #define LUM_MMX_FILTER_OFFSET "11*8"
  136. #define CHR_MMX_FILTER_OFFSET "11*8+4*4*256"
  137. #define DSTW_OFFSET "11*8+4*4*256*2" //do not change, it is hardcoded in the ASM
  138. #define ESP_OFFSET "11*8+4*4*256*2+8"
  139. #define VROUNDER_OFFSET "11*8+4*4*256*2+16"
  140. #define U_TEMP "11*8+4*4*256*2+24"
  141. #define V_TEMP "11*8+4*4*256*2+32"
  142. #define Y_TEMP "11*8+4*4*256*2+40"
  143. #define ALP_MMX_FILTER_OFFSET "11*8+4*4*256*2+48"
  144. DECLARE_ALIGNED(8, uint64_t, redDither);
  145. DECLARE_ALIGNED(8, uint64_t, greenDither);
  146. DECLARE_ALIGNED(8, uint64_t, blueDither);
  147. DECLARE_ALIGNED(8, uint64_t, yCoeff);
  148. DECLARE_ALIGNED(8, uint64_t, vrCoeff);
  149. DECLARE_ALIGNED(8, uint64_t, ubCoeff);
  150. DECLARE_ALIGNED(8, uint64_t, vgCoeff);
  151. DECLARE_ALIGNED(8, uint64_t, ugCoeff);
  152. DECLARE_ALIGNED(8, uint64_t, yOffset);
  153. DECLARE_ALIGNED(8, uint64_t, uOffset);
  154. DECLARE_ALIGNED(8, uint64_t, vOffset);
  155. int32_t lumMmxFilter[4*MAX_FILTER_SIZE];
  156. int32_t chrMmxFilter[4*MAX_FILTER_SIZE];
  157. int dstW;
  158. DECLARE_ALIGNED(8, uint64_t, esp);
  159. DECLARE_ALIGNED(8, uint64_t, vRounder);
  160. DECLARE_ALIGNED(8, uint64_t, u_temp);
  161. DECLARE_ALIGNED(8, uint64_t, v_temp);
  162. DECLARE_ALIGNED(8, uint64_t, y_temp);
  163. int32_t alpMmxFilter[4*MAX_FILTER_SIZE];
  164. #if HAVE_ALTIVEC
  165. vector signed short CY;
  166. vector signed short CRV;
  167. vector signed short CBU;
  168. vector signed short CGU;
  169. vector signed short CGV;
  170. vector signed short OY;
  171. vector unsigned short CSHIFT;
  172. vector signed short *vYCoeffsBank, *vCCoeffsBank;
  173. #endif
  174. #if ARCH_BFIN
  175. DECLARE_ALIGNED(4, uint32_t, oy);
  176. DECLARE_ALIGNED(4, uint32_t, oc);
  177. DECLARE_ALIGNED(4, uint32_t, zero);
  178. DECLARE_ALIGNED(4, uint32_t, cy);
  179. DECLARE_ALIGNED(4, uint32_t, crv);
  180. DECLARE_ALIGNED(4, uint32_t, rmask);
  181. DECLARE_ALIGNED(4, uint32_t, cbu);
  182. DECLARE_ALIGNED(4, uint32_t, bmask);
  183. DECLARE_ALIGNED(4, uint32_t, cgu);
  184. DECLARE_ALIGNED(4, uint32_t, cgv);
  185. DECLARE_ALIGNED(4, uint32_t, gmask);
  186. #endif
  187. #if HAVE_VIS
  188. DECLARE_ALIGNED(8, uint64_t, sparc_coeffs[10]);
  189. #endif
  190. /* function pointers for swScale() */
  191. void (*yuv2nv12X )(struct SwsContext *c,
  192. const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  193. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  194. uint8_t *dest, uint8_t *uDest,
  195. int dstW, int chrDstW, int dstFormat);
  196. void (*yuv2yuv1 )(struct SwsContext *c,
  197. const int16_t *lumSrc, const int16_t *chrSrc, const int16_t *alpSrc,
  198. uint8_t *dest,
  199. uint8_t *uDest, uint8_t *vDest, uint8_t *aDest,
  200. long dstW, long chrDstW);
  201. void (*yuv2yuvX )(struct SwsContext *c,
  202. const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  203. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  204. const int16_t **alpSrc,
  205. uint8_t *dest,
  206. uint8_t *uDest, uint8_t *vDest, uint8_t *aDest,
  207. long dstW, long chrDstW);
  208. void (*yuv2packed1)(struct SwsContext *c,
  209. const uint16_t *buf0,
  210. const uint16_t *uvbuf0, const uint16_t *uvbuf1,
  211. const uint16_t *abuf0,
  212. uint8_t *dest,
  213. int dstW, int uvalpha, int dstFormat, int flags, int y);
  214. void (*yuv2packed2)(struct SwsContext *c,
  215. const uint16_t *buf0, const uint16_t *buf1,
  216. const uint16_t *uvbuf0, const uint16_t *uvbuf1,
  217. const uint16_t *abuf0, const uint16_t *abuf1,
  218. uint8_t *dest,
  219. int dstW, int yalpha, int uvalpha, int y);
  220. void (*yuv2packedX)(struct SwsContext *c,
  221. const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  222. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  223. const int16_t **alpSrc, uint8_t *dest,
  224. long dstW, long dstY);
  225. void (*hyscale_internal)(uint8_t *dst, const uint8_t *src,
  226. long width, uint32_t *pal);
  227. void (*hascale_internal)(uint8_t *dst, const uint8_t *src,
  228. long width, uint32_t *pal);
  229. void (*hcscale_internal)(uint8_t *dstU, uint8_t *dstV,
  230. const uint8_t *src1, const uint8_t *src2,
  231. long width, uint32_t *pal);
  232. void (*hyscale_fast)(struct SwsContext *c,
  233. int16_t *dst, int dstWidth,
  234. const uint8_t *src, int srcW, int xInc);
  235. void (*hcscale_fast)(struct SwsContext *c,
  236. int16_t *dst, int dstWidth,
  237. const uint8_t *src1, const uint8_t *src2,
  238. int srcW, int xInc);
  239. void (*hScale)(int16_t *dst, int dstW, const uint8_t *src, int srcW,
  240. int xInc, const int16_t *filter, const int16_t *filterPos,
  241. long filterSize);
  242. } SwsContext;
  243. //FIXME check init (where 0)
  244. SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c);
  245. int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
  246. int fullRange, int brightness,
  247. int contrast, int saturation);
  248. void ff_yuv2rgb_init_tables_altivec(SwsContext *c, const int inv_table[4],
  249. int brightness, int contrast, int saturation);
  250. SwsFunc ff_yuv2rgb_init_mmx(SwsContext *c);
  251. SwsFunc ff_yuv2rgb_init_vis(SwsContext *c);
  252. SwsFunc ff_yuv2rgb_init_mlib(SwsContext *c);
  253. SwsFunc ff_yuv2rgb_init_altivec(SwsContext *c);
  254. SwsFunc ff_yuv2rgb_get_func_ptr_bfin(SwsContext *c);
  255. void ff_bfin_get_unscaled_swscale(SwsContext *c);
  256. void ff_yuv2packedX_altivec(SwsContext *c,
  257. const int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  258. const int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  259. uint8_t *dest, int dstW, int dstY);
  260. const char *sws_format_name(enum PixelFormat format);
  261. //FIXME replace this with something faster
  262. #define is16BPS(x) ( \
  263. (x)==PIX_FMT_GRAY16BE \
  264. || (x)==PIX_FMT_GRAY16LE \
  265. || (x)==PIX_FMT_RGB48BE \
  266. || (x)==PIX_FMT_RGB48LE \
  267. || (x)==PIX_FMT_YUV420PLE \
  268. || (x)==PIX_FMT_YUV422PLE \
  269. || (x)==PIX_FMT_YUV444PLE \
  270. || (x)==PIX_FMT_YUV420PBE \
  271. || (x)==PIX_FMT_YUV422PBE \
  272. || (x)==PIX_FMT_YUV444PBE \
  273. )
  274. #define isBE(x) ((x)&1)
  275. #define isPlanar8YUV(x) ( \
  276. (x)==PIX_FMT_YUV410P \
  277. || (x)==PIX_FMT_YUV420P \
  278. || (x)==PIX_FMT_YUVA420P \
  279. || (x)==PIX_FMT_YUV411P \
  280. || (x)==PIX_FMT_YUV422P \
  281. || (x)==PIX_FMT_YUV444P \
  282. || (x)==PIX_FMT_YUV440P \
  283. || (x)==PIX_FMT_NV12 \
  284. || (x)==PIX_FMT_NV21 \
  285. )
  286. #define isPlanarYUV(x) ( \
  287. isPlanar8YUV(x) \
  288. || (x)==PIX_FMT_YUV420PLE \
  289. || (x)==PIX_FMT_YUV422PLE \
  290. || (x)==PIX_FMT_YUV444PLE \
  291. || (x)==PIX_FMT_YUV420PBE \
  292. || (x)==PIX_FMT_YUV422PBE \
  293. || (x)==PIX_FMT_YUV444PBE \
  294. )
  295. #define isYUV(x) ( \
  296. (x)==PIX_FMT_UYVY422 \
  297. || (x)==PIX_FMT_YUYV422 \
  298. || isPlanarYUV(x) \
  299. )
  300. #define isGray(x) ( \
  301. (x)==PIX_FMT_GRAY8 \
  302. || (x)==PIX_FMT_GRAY16BE \
  303. || (x)==PIX_FMT_GRAY16LE \
  304. )
  305. #define isGray16(x) ( \
  306. (x)==PIX_FMT_GRAY16BE \
  307. || (x)==PIX_FMT_GRAY16LE \
  308. )
  309. #define isRGB(x) ( \
  310. (x)==PIX_FMT_RGB48BE \
  311. || (x)==PIX_FMT_RGB48LE \
  312. || (x)==PIX_FMT_RGB32 \
  313. || (x)==PIX_FMT_RGB32_1 \
  314. || (x)==PIX_FMT_RGB24 \
  315. || (x)==PIX_FMT_RGB565 \
  316. || (x)==PIX_FMT_RGB555 \
  317. || (x)==PIX_FMT_RGB8 \
  318. || (x)==PIX_FMT_RGB4 \
  319. || (x)==PIX_FMT_RGB4_BYTE \
  320. || (x)==PIX_FMT_MONOBLACK \
  321. || (x)==PIX_FMT_MONOWHITE \
  322. )
  323. #define isBGR(x) ( \
  324. (x)==PIX_FMT_BGR32 \
  325. || (x)==PIX_FMT_BGR32_1 \
  326. || (x)==PIX_FMT_BGR24 \
  327. || (x)==PIX_FMT_BGR565 \
  328. || (x)==PIX_FMT_BGR555 \
  329. || (x)==PIX_FMT_BGR8 \
  330. || (x)==PIX_FMT_BGR4 \
  331. || (x)==PIX_FMT_BGR4_BYTE \
  332. || (x)==PIX_FMT_MONOBLACK \
  333. || (x)==PIX_FMT_MONOWHITE \
  334. )
  335. #define isALPHA(x) ( \
  336. (x)==PIX_FMT_BGR32 \
  337. || (x)==PIX_FMT_BGR32_1 \
  338. || (x)==PIX_FMT_RGB32 \
  339. || (x)==PIX_FMT_RGB32_1 \
  340. || (x)==PIX_FMT_YUVA420P \
  341. )
  342. static inline int fmt_depth(int fmt)
  343. {
  344. switch(fmt) {
  345. case PIX_FMT_RGB48BE:
  346. case PIX_FMT_RGB48LE:
  347. return 48;
  348. case PIX_FMT_BGRA:
  349. case PIX_FMT_ABGR:
  350. case PIX_FMT_RGBA:
  351. case PIX_FMT_ARGB:
  352. return 32;
  353. case PIX_FMT_BGR24:
  354. case PIX_FMT_RGB24:
  355. return 24;
  356. case PIX_FMT_BGR565:
  357. case PIX_FMT_RGB565:
  358. case PIX_FMT_GRAY16BE:
  359. case PIX_FMT_GRAY16LE:
  360. return 16;
  361. case PIX_FMT_BGR555:
  362. case PIX_FMT_RGB555:
  363. return 15;
  364. case PIX_FMT_BGR8:
  365. case PIX_FMT_RGB8:
  366. return 8;
  367. case PIX_FMT_BGR4:
  368. case PIX_FMT_RGB4:
  369. case PIX_FMT_BGR4_BYTE:
  370. case PIX_FMT_RGB4_BYTE:
  371. return 4;
  372. case PIX_FMT_MONOBLACK:
  373. case PIX_FMT_MONOWHITE:
  374. return 1;
  375. default:
  376. return 0;
  377. }
  378. }
  379. extern const uint64_t ff_dither4[2];
  380. extern const uint64_t ff_dither8[2];
  381. extern const AVClass sws_context_class;
  382. #endif /* SWSCALE_SWSCALE_INTERNAL_H */