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.

2120 lines
74KB

  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. /*
  21. supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
  22. supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  23. {BGR,RGB}{1,4,8,15,16} support dithering
  24. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  25. YV12 -> {BGR,RGB}{1,4,8,12,15,16,24,32}
  26. x -> x
  27. YUV9 -> YV12
  28. YUV9/YV12 -> Y800
  29. Y800 -> YUV9/YV12
  30. BGR24 -> BGR32 & RGB24 -> RGB32
  31. BGR32 -> BGR24 & RGB32 -> RGB24
  32. BGR15 -> BGR16
  33. */
  34. /*
  35. tested special converters (most are tested actually, but I did not write it down ...)
  36. YV12 -> BGR12/BGR16
  37. YV12 -> YV12
  38. BGR15 -> BGR16
  39. BGR16 -> BGR16
  40. YVU9 -> YV12
  41. untested special converters
  42. YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
  43. YV12/I420 -> YV12/I420
  44. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  45. BGR24 -> BGR32 & RGB24 -> RGB32
  46. BGR32 -> BGR24 & RGB32 -> RGB24
  47. BGR24 -> YV12
  48. */
  49. #include <inttypes.h>
  50. #include <string.h>
  51. #include <math.h>
  52. #include <stdio.h>
  53. #include "config.h"
  54. #include <assert.h>
  55. #include "swscale.h"
  56. #include "swscale_internal.h"
  57. #include "rgb2rgb.h"
  58. #include "libavutil/intreadwrite.h"
  59. #include "libavutil/x86_cpu.h"
  60. #include "libavutil/cpu.h"
  61. #include "libavutil/avutil.h"
  62. #include "libavutil/mathematics.h"
  63. #include "libavutil/bswap.h"
  64. #include "libavutil/pixdesc.h"
  65. #undef MOVNTQ
  66. #undef PAVGB
  67. //#undef HAVE_MMX2
  68. //#define HAVE_AMD3DNOW
  69. //#undef HAVE_MMX
  70. //#undef ARCH_X86
  71. #define DITHER1XBPP
  72. #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
  73. #define isPacked(x) ( \
  74. (x)==PIX_FMT_PAL8 \
  75. || (x)==PIX_FMT_YUYV422 \
  76. || (x)==PIX_FMT_UYVY422 \
  77. || (x)==PIX_FMT_Y400A \
  78. || isAnyRGB(x) \
  79. )
  80. #define RGB2YUV_SHIFT 15
  81. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  82. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  83. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  84. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  85. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  86. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  87. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  88. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  89. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  90. static const double rgb2yuv_table[8][9]={
  91. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
  92. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
  93. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  94. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  95. {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
  96. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  97. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
  98. {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
  99. };
  100. /*
  101. NOTES
  102. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  103. TODO
  104. more intelligent misalignment avoidance for the horizontal scaler
  105. write special vertical cubic upscale version
  106. optimize C code (YV12 / minmax)
  107. add support for packed pixel YUV input & output
  108. add support for Y8 output
  109. optimize BGR24 & BGR32
  110. add BGR4 output support
  111. write special BGR->BGR scaler
  112. */
  113. #if ARCH_X86
  114. DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
  115. DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
  116. DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
  117. DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
  118. DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
  119. DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
  120. DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
  121. DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
  122. const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
  123. 0x0103010301030103LL,
  124. 0x0200020002000200LL,};
  125. const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
  126. 0x0602060206020602LL,
  127. 0x0004000400040004LL,};
  128. DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
  129. DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
  130. DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
  131. DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
  132. DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
  133. DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
  134. DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
  135. DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
  136. DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
  137. #ifdef FAST_BGR2YV12
  138. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
  139. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
  140. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
  141. #else
  142. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
  143. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
  144. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
  145. #endif /* FAST_BGR2YV12 */
  146. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
  147. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
  148. DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
  149. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
  150. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
  151. DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
  152. DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
  153. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
  154. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV)[2][4] = {
  155. {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
  156. {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
  157. };
  158. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
  159. #endif /* ARCH_X86 */
  160. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
  161. { 1, 3, 1, 3, 1, 3, 1, 3, },
  162. { 2, 0, 2, 0, 2, 0, 2, 0, },
  163. };
  164. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
  165. { 6, 2, 6, 2, 6, 2, 6, 2, },
  166. { 0, 4, 0, 4, 0, 4, 0, 4, },
  167. };
  168. DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
  169. { 8, 4, 11, 7, 8, 4, 11, 7, },
  170. { 2, 14, 1, 13, 2, 14, 1, 13, },
  171. { 10, 6, 9, 5, 10, 6, 9, 5, },
  172. { 0, 12, 3, 15, 0, 12, 3, 15, },
  173. };
  174. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
  175. { 17, 9, 23, 15, 16, 8, 22, 14, },
  176. { 5, 29, 3, 27, 4, 28, 2, 26, },
  177. { 21, 13, 19, 11, 20, 12, 18, 10, },
  178. { 0, 24, 6, 30, 1, 25, 7, 31, },
  179. { 16, 8, 22, 14, 17, 9, 23, 15, },
  180. { 4, 28, 2, 26, 5, 29, 3, 27, },
  181. { 20, 12, 18, 10, 21, 13, 19, 11, },
  182. { 1, 25, 7, 31, 0, 24, 6, 30, },
  183. };
  184. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
  185. { 0, 55, 14, 68, 3, 58, 17, 72, },
  186. { 37, 18, 50, 32, 40, 22, 54, 35, },
  187. { 9, 64, 5, 59, 13, 67, 8, 63, },
  188. { 46, 27, 41, 23, 49, 31, 44, 26, },
  189. { 2, 57, 16, 71, 1, 56, 15, 70, },
  190. { 39, 21, 52, 34, 38, 19, 51, 33, },
  191. { 11, 66, 7, 62, 10, 65, 6, 60, },
  192. { 48, 30, 43, 25, 47, 29, 42, 24, },
  193. };
  194. #if 1
  195. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  196. {117, 62, 158, 103, 113, 58, 155, 100, },
  197. { 34, 199, 21, 186, 31, 196, 17, 182, },
  198. {144, 89, 131, 76, 141, 86, 127, 72, },
  199. { 0, 165, 41, 206, 10, 175, 52, 217, },
  200. {110, 55, 151, 96, 120, 65, 162, 107, },
  201. { 28, 193, 14, 179, 38, 203, 24, 189, },
  202. {138, 83, 124, 69, 148, 93, 134, 79, },
  203. { 7, 172, 48, 213, 3, 168, 45, 210, },
  204. };
  205. #elif 1
  206. // tries to correct a gamma of 1.5
  207. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  208. { 0, 143, 18, 200, 2, 156, 25, 215, },
  209. { 78, 28, 125, 64, 89, 36, 138, 74, },
  210. { 10, 180, 3, 161, 16, 195, 8, 175, },
  211. {109, 51, 93, 38, 121, 60, 105, 47, },
  212. { 1, 152, 23, 210, 0, 147, 20, 205, },
  213. { 85, 33, 134, 71, 81, 30, 130, 67, },
  214. { 14, 190, 6, 171, 12, 185, 5, 166, },
  215. {117, 57, 101, 44, 113, 54, 97, 41, },
  216. };
  217. #elif 1
  218. // tries to correct a gamma of 2.0
  219. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  220. { 0, 124, 8, 193, 0, 140, 12, 213, },
  221. { 55, 14, 104, 42, 66, 19, 119, 52, },
  222. { 3, 168, 1, 145, 6, 187, 3, 162, },
  223. { 86, 31, 70, 21, 99, 39, 82, 28, },
  224. { 0, 134, 11, 206, 0, 129, 9, 200, },
  225. { 62, 17, 114, 48, 58, 16, 109, 45, },
  226. { 5, 181, 2, 157, 4, 175, 1, 151, },
  227. { 95, 36, 78, 26, 90, 34, 74, 24, },
  228. };
  229. #else
  230. // tries to correct a gamma of 2.5
  231. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  232. { 0, 107, 3, 187, 0, 125, 6, 212, },
  233. { 39, 7, 86, 28, 49, 11, 102, 36, },
  234. { 1, 158, 0, 131, 3, 180, 1, 151, },
  235. { 68, 19, 52, 12, 81, 25, 64, 17, },
  236. { 0, 119, 5, 203, 0, 113, 4, 195, },
  237. { 45, 9, 96, 33, 42, 8, 91, 30, },
  238. { 2, 172, 1, 144, 2, 165, 0, 137, },
  239. { 77, 23, 60, 15, 72, 21, 56, 14, },
  240. };
  241. #endif
  242. static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  243. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  244. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
  245. int dstW, int chrDstW, int big_endian)
  246. {
  247. //FIXME Optimize (just quickly written not optimized..)
  248. int i;
  249. for (i = 0; i < dstW; i++) {
  250. int val = 1 << 10;
  251. int j;
  252. for (j = 0; j < lumFilterSize; j++)
  253. val += lumSrc[j][i] * lumFilter[j];
  254. if (big_endian) {
  255. AV_WB16(&dest[i], av_clip_uint16(val >> 11));
  256. } else {
  257. AV_WL16(&dest[i], av_clip_uint16(val >> 11));
  258. }
  259. }
  260. if (uDest) {
  261. for (i = 0; i < chrDstW; i++) {
  262. int u = 1 << 10;
  263. int v = 1 << 10;
  264. int j;
  265. for (j = 0; j < chrFilterSize; j++) {
  266. u += chrSrc[j][i ] * chrFilter[j];
  267. v += chrSrc[j][i + VOFW] * chrFilter[j];
  268. }
  269. if (big_endian) {
  270. AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
  271. AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
  272. } else {
  273. AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
  274. AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
  275. }
  276. }
  277. }
  278. if (CONFIG_SWSCALE_ALPHA && aDest) {
  279. for (i = 0; i < dstW; i++) {
  280. int val = 1 << 10;
  281. int j;
  282. for (j = 0; j < lumFilterSize; j++)
  283. val += alpSrc[j][i] * lumFilter[j];
  284. if (big_endian) {
  285. AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
  286. } else {
  287. AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
  288. }
  289. }
  290. }
  291. }
  292. static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  293. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  294. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
  295. enum PixelFormat dstFormat)
  296. {
  297. if (isBE(dstFormat)) {
  298. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
  299. chrFilter, chrSrc, chrFilterSize,
  300. alpSrc,
  301. dest, uDest, vDest, aDest,
  302. dstW, chrDstW, 1);
  303. } else {
  304. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
  305. chrFilter, chrSrc, chrFilterSize,
  306. alpSrc,
  307. dest, uDest, vDest, aDest,
  308. dstW, chrDstW, 0);
  309. }
  310. }
  311. static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  312. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  313. const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
  314. {
  315. //FIXME Optimize (just quickly written not optimized..)
  316. int i;
  317. for (i=0; i<dstW; i++) {
  318. int val=1<<18;
  319. int j;
  320. for (j=0; j<lumFilterSize; j++)
  321. val += lumSrc[j][i] * lumFilter[j];
  322. dest[i]= av_clip_uint8(val>>19);
  323. }
  324. if (uDest)
  325. for (i=0; i<chrDstW; i++) {
  326. int u=1<<18;
  327. int v=1<<18;
  328. int j;
  329. for (j=0; j<chrFilterSize; j++) {
  330. u += chrSrc[j][i] * chrFilter[j];
  331. v += chrSrc[j][i + VOFW] * chrFilter[j];
  332. }
  333. uDest[i]= av_clip_uint8(u>>19);
  334. vDest[i]= av_clip_uint8(v>>19);
  335. }
  336. if (CONFIG_SWSCALE_ALPHA && aDest)
  337. for (i=0; i<dstW; i++) {
  338. int val=1<<18;
  339. int j;
  340. for (j=0; j<lumFilterSize; j++)
  341. val += alpSrc[j][i] * lumFilter[j];
  342. aDest[i]= av_clip_uint8(val>>19);
  343. }
  344. }
  345. static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  346. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  347. uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
  348. {
  349. //FIXME Optimize (just quickly written not optimized..)
  350. int i;
  351. for (i=0; i<dstW; i++) {
  352. int val=1<<18;
  353. int j;
  354. for (j=0; j<lumFilterSize; j++)
  355. val += lumSrc[j][i] * lumFilter[j];
  356. dest[i]= av_clip_uint8(val>>19);
  357. }
  358. if (!uDest)
  359. return;
  360. if (dstFormat == PIX_FMT_NV12)
  361. for (i=0; i<chrDstW; i++) {
  362. int u=1<<18;
  363. int v=1<<18;
  364. int j;
  365. for (j=0; j<chrFilterSize; j++) {
  366. u += chrSrc[j][i] * chrFilter[j];
  367. v += chrSrc[j][i + VOFW] * chrFilter[j];
  368. }
  369. uDest[2*i]= av_clip_uint8(u>>19);
  370. uDest[2*i+1]= av_clip_uint8(v>>19);
  371. }
  372. else
  373. for (i=0; i<chrDstW; i++) {
  374. int u=1<<18;
  375. int v=1<<18;
  376. int j;
  377. for (j=0; j<chrFilterSize; j++) {
  378. u += chrSrc[j][i] * chrFilter[j];
  379. v += chrSrc[j][i + VOFW] * chrFilter[j];
  380. }
  381. uDest[2*i]= av_clip_uint8(v>>19);
  382. uDest[2*i+1]= av_clip_uint8(u>>19);
  383. }
  384. }
  385. #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
  386. for (i=0; i<(dstW>>1); i++) {\
  387. int j;\
  388. int Y1 = 1<<18;\
  389. int Y2 = 1<<18;\
  390. int U = 1<<18;\
  391. int V = 1<<18;\
  392. int av_unused A1, A2;\
  393. type av_unused *r, *b, *g;\
  394. const int i2= 2*i;\
  395. \
  396. for (j=0; j<lumFilterSize; j++) {\
  397. Y1 += lumSrc[j][i2] * lumFilter[j];\
  398. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  399. }\
  400. for (j=0; j<chrFilterSize; j++) {\
  401. U += chrSrc[j][i] * chrFilter[j];\
  402. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  403. }\
  404. Y1>>=19;\
  405. Y2>>=19;\
  406. U >>=19;\
  407. V >>=19;\
  408. if (alpha) {\
  409. A1 = 1<<18;\
  410. A2 = 1<<18;\
  411. for (j=0; j<lumFilterSize; j++) {\
  412. A1 += alpSrc[j][i2 ] * lumFilter[j];\
  413. A2 += alpSrc[j][i2+1] * lumFilter[j];\
  414. }\
  415. A1>>=19;\
  416. A2>>=19;\
  417. }
  418. #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
  419. YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
  420. if ((Y1|Y2|U|V)&256) {\
  421. if (Y1>255) Y1=255; \
  422. else if (Y1<0)Y1=0; \
  423. if (Y2>255) Y2=255; \
  424. else if (Y2<0)Y2=0; \
  425. if (U>255) U=255; \
  426. else if (U<0) U=0; \
  427. if (V>255) V=255; \
  428. else if (V<0) V=0; \
  429. }\
  430. if (alpha && ((A1|A2)&256)) {\
  431. A1=av_clip_uint8(A1);\
  432. A2=av_clip_uint8(A2);\
  433. }
  434. #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
  435. for (i=0; i<dstW; i++) {\
  436. int j;\
  437. int Y = 0;\
  438. int U = -128<<19;\
  439. int V = -128<<19;\
  440. int av_unused A;\
  441. int R,G,B;\
  442. \
  443. for (j=0; j<lumFilterSize; j++) {\
  444. Y += lumSrc[j][i ] * lumFilter[j];\
  445. }\
  446. for (j=0; j<chrFilterSize; j++) {\
  447. U += chrSrc[j][i ] * chrFilter[j];\
  448. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  449. }\
  450. Y >>=10;\
  451. U >>=10;\
  452. V >>=10;\
  453. if (alpha) {\
  454. A = rnd;\
  455. for (j=0; j<lumFilterSize; j++)\
  456. A += alpSrc[j][i ] * lumFilter[j];\
  457. A >>=19;\
  458. if (A&256)\
  459. A = av_clip_uint8(A);\
  460. }
  461. #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
  462. YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
  463. Y-= c->yuv2rgb_y_offset;\
  464. Y*= c->yuv2rgb_y_coeff;\
  465. Y+= rnd;\
  466. R= Y + V*c->yuv2rgb_v2r_coeff;\
  467. G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
  468. B= Y + U*c->yuv2rgb_u2b_coeff;\
  469. if ((R|G|B)&(0xC0000000)) {\
  470. if (R>=(256<<22)) R=(256<<22)-1; \
  471. else if (R<0)R=0; \
  472. if (G>=(256<<22)) G=(256<<22)-1; \
  473. else if (G<0)G=0; \
  474. if (B>=(256<<22)) B=(256<<22)-1; \
  475. else if (B<0)B=0; \
  476. }
  477. #define YSCALE_YUV_2_GRAY16_C \
  478. for (i=0; i<(dstW>>1); i++) {\
  479. int j;\
  480. int Y1 = 1<<18;\
  481. int Y2 = 1<<18;\
  482. int U = 1<<18;\
  483. int V = 1<<18;\
  484. \
  485. const int i2= 2*i;\
  486. \
  487. for (j=0; j<lumFilterSize; j++) {\
  488. Y1 += lumSrc[j][i2] * lumFilter[j];\
  489. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  490. }\
  491. Y1>>=11;\
  492. Y2>>=11;\
  493. if ((Y1|Y2|U|V)&65536) {\
  494. if (Y1>65535) Y1=65535; \
  495. else if (Y1<0)Y1=0; \
  496. if (Y2>65535) Y2=65535; \
  497. else if (Y2<0)Y2=0; \
  498. }
  499. #define YSCALE_YUV_2_RGBX_C(type,alpha) \
  500. YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
  501. r = (type *)c->table_rV[V]; \
  502. g = (type *)(c->table_gU[U] + c->table_gV[V]); \
  503. b = (type *)c->table_bU[U];
  504. #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
  505. for (i=0; i<(dstW>>1); i++) { \
  506. const int i2= 2*i; \
  507. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
  508. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
  509. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
  510. int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
  511. type av_unused *r, *b, *g; \
  512. int av_unused A1, A2; \
  513. if (alpha) {\
  514. A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
  515. A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
  516. }
  517. #define YSCALE_YUV_2_GRAY16_2_C \
  518. for (i=0; i<(dstW>>1); i++) { \
  519. const int i2= 2*i; \
  520. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
  521. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
  522. #define YSCALE_YUV_2_RGB2_C(type,alpha) \
  523. YSCALE_YUV_2_PACKED2_C(type,alpha)\
  524. r = (type *)c->table_rV[V];\
  525. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  526. b = (type *)c->table_bU[U];
  527. #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
  528. for (i=0; i<(dstW>>1); i++) {\
  529. const int i2= 2*i;\
  530. int Y1= buf0[i2 ]>>7;\
  531. int Y2= buf0[i2+1]>>7;\
  532. int U= (uvbuf1[i ])>>7;\
  533. int V= (uvbuf1[i+VOFW])>>7;\
  534. type av_unused *r, *b, *g;\
  535. int av_unused A1, A2;\
  536. if (alpha) {\
  537. A1= abuf0[i2 ]>>7;\
  538. A2= abuf0[i2+1]>>7;\
  539. }
  540. #define YSCALE_YUV_2_GRAY16_1_C \
  541. for (i=0; i<(dstW>>1); i++) {\
  542. const int i2= 2*i;\
  543. int Y1= buf0[i2 ]<<1;\
  544. int Y2= buf0[i2+1]<<1;
  545. #define YSCALE_YUV_2_RGB1_C(type,alpha) \
  546. YSCALE_YUV_2_PACKED1_C(type,alpha)\
  547. r = (type *)c->table_rV[V];\
  548. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  549. b = (type *)c->table_bU[U];
  550. #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
  551. for (i=0; i<(dstW>>1); i++) {\
  552. const int i2= 2*i;\
  553. int Y1= buf0[i2 ]>>7;\
  554. int Y2= buf0[i2+1]>>7;\
  555. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  556. int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
  557. type av_unused *r, *b, *g;\
  558. int av_unused A1, A2;\
  559. if (alpha) {\
  560. A1= abuf0[i2 ]>>7;\
  561. A2= abuf0[i2+1]>>7;\
  562. }
  563. #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
  564. YSCALE_YUV_2_PACKED1B_C(type,alpha)\
  565. r = (type *)c->table_rV[V];\
  566. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  567. b = (type *)c->table_bU[U];
  568. #define YSCALE_YUV_2_MONO2_C \
  569. const uint8_t * const d128=dither_8x8_220[y&7];\
  570. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  571. for (i=0; i<dstW-7; i+=8) {\
  572. int acc;\
  573. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  574. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  575. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  576. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  577. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  578. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  579. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  580. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  581. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  582. dest++;\
  583. }
  584. #define YSCALE_YUV_2_MONOX_C \
  585. const uint8_t * const d128=dither_8x8_220[y&7];\
  586. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  587. int acc=0;\
  588. for (i=0; i<dstW-1; i+=2) {\
  589. int j;\
  590. int Y1=1<<18;\
  591. int Y2=1<<18;\
  592. \
  593. for (j=0; j<lumFilterSize; j++) {\
  594. Y1 += lumSrc[j][i] * lumFilter[j];\
  595. Y2 += lumSrc[j][i+1] * lumFilter[j];\
  596. }\
  597. Y1>>=19;\
  598. Y2>>=19;\
  599. if ((Y1|Y2)&256) {\
  600. if (Y1>255) Y1=255;\
  601. else if (Y1<0)Y1=0;\
  602. if (Y2>255) Y2=255;\
  603. else if (Y2<0)Y2=0;\
  604. }\
  605. acc+= acc + g[Y1+d128[(i+0)&7]];\
  606. acc+= acc + g[Y2+d128[(i+1)&7]];\
  607. if ((i&7)==6) {\
  608. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  609. dest++;\
  610. }\
  611. }
  612. #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
  613. switch(c->dstFormat) {\
  614. case PIX_FMT_RGB48BE:\
  615. case PIX_FMT_RGB48LE:\
  616. func(uint8_t,0)\
  617. ((uint8_t*)dest)[ 0]= r[Y1];\
  618. ((uint8_t*)dest)[ 1]= r[Y1];\
  619. ((uint8_t*)dest)[ 2]= g[Y1];\
  620. ((uint8_t*)dest)[ 3]= g[Y1];\
  621. ((uint8_t*)dest)[ 4]= b[Y1];\
  622. ((uint8_t*)dest)[ 5]= b[Y1];\
  623. ((uint8_t*)dest)[ 6]= r[Y2];\
  624. ((uint8_t*)dest)[ 7]= r[Y2];\
  625. ((uint8_t*)dest)[ 8]= g[Y2];\
  626. ((uint8_t*)dest)[ 9]= g[Y2];\
  627. ((uint8_t*)dest)[10]= b[Y2];\
  628. ((uint8_t*)dest)[11]= b[Y2];\
  629. dest+=12;\
  630. }\
  631. break;\
  632. case PIX_FMT_BGR48BE:\
  633. case PIX_FMT_BGR48LE:\
  634. func(uint8_t,0)\
  635. ((uint8_t*)dest)[ 0] = ((uint8_t*)dest)[ 1] = b[Y1];\
  636. ((uint8_t*)dest)[ 2] = ((uint8_t*)dest)[ 3] = g[Y1];\
  637. ((uint8_t*)dest)[ 4] = ((uint8_t*)dest)[ 5] = r[Y1];\
  638. ((uint8_t*)dest)[ 6] = ((uint8_t*)dest)[ 7] = b[Y2];\
  639. ((uint8_t*)dest)[ 8] = ((uint8_t*)dest)[ 9] = g[Y2];\
  640. ((uint8_t*)dest)[10] = ((uint8_t*)dest)[11] = r[Y2];\
  641. dest+=12;\
  642. }\
  643. break;\
  644. case PIX_FMT_RGBA:\
  645. case PIX_FMT_BGRA:\
  646. if (CONFIG_SMALL) {\
  647. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  648. func(uint32_t,needAlpha)\
  649. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
  650. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
  651. }\
  652. } else {\
  653. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  654. func(uint32_t,1)\
  655. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
  656. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
  657. }\
  658. } else {\
  659. func(uint32_t,0)\
  660. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  661. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  662. }\
  663. }\
  664. }\
  665. break;\
  666. case PIX_FMT_ARGB:\
  667. case PIX_FMT_ABGR:\
  668. if (CONFIG_SMALL) {\
  669. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  670. func(uint32_t,needAlpha)\
  671. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
  672. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
  673. }\
  674. } else {\
  675. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  676. func(uint32_t,1)\
  677. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
  678. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
  679. }\
  680. } else {\
  681. func(uint32_t,0)\
  682. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  683. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  684. }\
  685. }\
  686. } \
  687. break;\
  688. case PIX_FMT_RGB24:\
  689. func(uint8_t,0)\
  690. ((uint8_t*)dest)[0]= r[Y1];\
  691. ((uint8_t*)dest)[1]= g[Y1];\
  692. ((uint8_t*)dest)[2]= b[Y1];\
  693. ((uint8_t*)dest)[3]= r[Y2];\
  694. ((uint8_t*)dest)[4]= g[Y2];\
  695. ((uint8_t*)dest)[5]= b[Y2];\
  696. dest+=6;\
  697. }\
  698. break;\
  699. case PIX_FMT_BGR24:\
  700. func(uint8_t,0)\
  701. ((uint8_t*)dest)[0]= b[Y1];\
  702. ((uint8_t*)dest)[1]= g[Y1];\
  703. ((uint8_t*)dest)[2]= r[Y1];\
  704. ((uint8_t*)dest)[3]= b[Y2];\
  705. ((uint8_t*)dest)[4]= g[Y2];\
  706. ((uint8_t*)dest)[5]= r[Y2];\
  707. dest+=6;\
  708. }\
  709. break;\
  710. case PIX_FMT_RGB565BE:\
  711. case PIX_FMT_RGB565LE:\
  712. case PIX_FMT_BGR565BE:\
  713. case PIX_FMT_BGR565LE:\
  714. {\
  715. const int dr1= dither_2x2_8[y&1 ][0];\
  716. const int dg1= dither_2x2_4[y&1 ][0];\
  717. const int db1= dither_2x2_8[(y&1)^1][0];\
  718. const int dr2= dither_2x2_8[y&1 ][1];\
  719. const int dg2= dither_2x2_4[y&1 ][1];\
  720. const int db2= dither_2x2_8[(y&1)^1][1];\
  721. func(uint16_t,0)\
  722. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  723. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  724. }\
  725. }\
  726. break;\
  727. case PIX_FMT_RGB555BE:\
  728. case PIX_FMT_RGB555LE:\
  729. case PIX_FMT_BGR555BE:\
  730. case PIX_FMT_BGR555LE:\
  731. {\
  732. const int dr1= dither_2x2_8[y&1 ][0];\
  733. const int dg1= dither_2x2_8[y&1 ][1];\
  734. const int db1= dither_2x2_8[(y&1)^1][0];\
  735. const int dr2= dither_2x2_8[y&1 ][1];\
  736. const int dg2= dither_2x2_8[y&1 ][0];\
  737. const int db2= dither_2x2_8[(y&1)^1][1];\
  738. func(uint16_t,0)\
  739. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  740. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  741. }\
  742. }\
  743. break;\
  744. case PIX_FMT_RGB444BE:\
  745. case PIX_FMT_RGB444LE:\
  746. case PIX_FMT_BGR444BE:\
  747. case PIX_FMT_BGR444LE:\
  748. {\
  749. const int dr1= dither_4x4_16[y&3 ][0];\
  750. const int dg1= dither_4x4_16[y&3 ][1];\
  751. const int db1= dither_4x4_16[(y&3)^3][0];\
  752. const int dr2= dither_4x4_16[y&3 ][1];\
  753. const int dg2= dither_4x4_16[y&3 ][0];\
  754. const int db2= dither_4x4_16[(y&3)^3][1];\
  755. func(uint16_t,0)\
  756. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  757. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  758. }\
  759. }\
  760. break;\
  761. case PIX_FMT_RGB8:\
  762. case PIX_FMT_BGR8:\
  763. {\
  764. const uint8_t * const d64= dither_8x8_73[y&7];\
  765. const uint8_t * const d32= dither_8x8_32[y&7];\
  766. func(uint8_t,0)\
  767. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  768. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  769. }\
  770. }\
  771. break;\
  772. case PIX_FMT_RGB4:\
  773. case PIX_FMT_BGR4:\
  774. {\
  775. const uint8_t * const d64= dither_8x8_73 [y&7];\
  776. const uint8_t * const d128=dither_8x8_220[y&7];\
  777. func(uint8_t,0)\
  778. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  779. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  780. }\
  781. }\
  782. break;\
  783. case PIX_FMT_RGB4_BYTE:\
  784. case PIX_FMT_BGR4_BYTE:\
  785. {\
  786. const uint8_t * const d64= dither_8x8_73 [y&7];\
  787. const uint8_t * const d128=dither_8x8_220[y&7];\
  788. func(uint8_t,0)\
  789. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  790. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  791. }\
  792. }\
  793. break;\
  794. case PIX_FMT_MONOBLACK:\
  795. case PIX_FMT_MONOWHITE:\
  796. {\
  797. func_monoblack\
  798. }\
  799. break;\
  800. case PIX_FMT_YUYV422:\
  801. func2\
  802. ((uint8_t*)dest)[2*i2+0]= Y1;\
  803. ((uint8_t*)dest)[2*i2+1]= U;\
  804. ((uint8_t*)dest)[2*i2+2]= Y2;\
  805. ((uint8_t*)dest)[2*i2+3]= V;\
  806. } \
  807. break;\
  808. case PIX_FMT_UYVY422:\
  809. func2\
  810. ((uint8_t*)dest)[2*i2+0]= U;\
  811. ((uint8_t*)dest)[2*i2+1]= Y1;\
  812. ((uint8_t*)dest)[2*i2+2]= V;\
  813. ((uint8_t*)dest)[2*i2+3]= Y2;\
  814. } \
  815. break;\
  816. case PIX_FMT_GRAY16BE:\
  817. func_g16\
  818. ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
  819. ((uint8_t*)dest)[2*i2+1]= Y1;\
  820. ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
  821. ((uint8_t*)dest)[2*i2+3]= Y2;\
  822. } \
  823. break;\
  824. case PIX_FMT_GRAY16LE:\
  825. func_g16\
  826. ((uint8_t*)dest)[2*i2+0]= Y1;\
  827. ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
  828. ((uint8_t*)dest)[2*i2+2]= Y2;\
  829. ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
  830. } \
  831. break;\
  832. }
  833. static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  834. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  835. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  836. {
  837. int i;
  838. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
  839. }
  840. static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  841. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  842. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  843. {
  844. int i;
  845. int step= c->dstFormatBpp/8;
  846. int aidx= 3;
  847. switch(c->dstFormat) {
  848. case PIX_FMT_ARGB:
  849. dest++;
  850. aidx= 0;
  851. case PIX_FMT_RGB24:
  852. aidx--;
  853. case PIX_FMT_RGBA:
  854. if (CONFIG_SMALL) {
  855. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  856. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  857. dest[aidx]= needAlpha ? A : 255;
  858. dest[0]= R>>22;
  859. dest[1]= G>>22;
  860. dest[2]= B>>22;
  861. dest+= step;
  862. }
  863. } else {
  864. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  865. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  866. dest[aidx]= A;
  867. dest[0]= R>>22;
  868. dest[1]= G>>22;
  869. dest[2]= B>>22;
  870. dest+= step;
  871. }
  872. } else {
  873. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  874. dest[aidx]= 255;
  875. dest[0]= R>>22;
  876. dest[1]= G>>22;
  877. dest[2]= B>>22;
  878. dest+= step;
  879. }
  880. }
  881. }
  882. break;
  883. case PIX_FMT_ABGR:
  884. dest++;
  885. aidx= 0;
  886. case PIX_FMT_BGR24:
  887. aidx--;
  888. case PIX_FMT_BGRA:
  889. if (CONFIG_SMALL) {
  890. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  891. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  892. dest[aidx]= needAlpha ? A : 255;
  893. dest[0]= B>>22;
  894. dest[1]= G>>22;
  895. dest[2]= R>>22;
  896. dest+= step;
  897. }
  898. } else {
  899. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  900. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  901. dest[aidx]= A;
  902. dest[0]= B>>22;
  903. dest[1]= G>>22;
  904. dest[2]= R>>22;
  905. dest+= step;
  906. }
  907. } else {
  908. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  909. dest[aidx]= 255;
  910. dest[0]= B>>22;
  911. dest[1]= G>>22;
  912. dest[2]= R>>22;
  913. dest+= step;
  914. }
  915. }
  916. }
  917. break;
  918. default:
  919. assert(0);
  920. }
  921. }
  922. static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
  923. {
  924. int i;
  925. uint8_t *ptr = plane + stride*y;
  926. for (i=0; i<height; i++) {
  927. memset(ptr, val, width);
  928. ptr += stride;
  929. }
  930. }
  931. static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
  932. uint32_t *unused)
  933. {
  934. int i;
  935. for (i = 0; i < width; i++) {
  936. int r = src[i*6+0];
  937. int g = src[i*6+2];
  938. int b = src[i*6+4];
  939. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  940. }
  941. }
  942. static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
  943. const uint8_t *src1, const uint8_t *src2,
  944. long width, uint32_t *unused)
  945. {
  946. int i;
  947. assert(src1==src2);
  948. for (i = 0; i < width; i++) {
  949. int r = src1[6*i + 0];
  950. int g = src1[6*i + 2];
  951. int b = src1[6*i + 4];
  952. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  953. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  954. }
  955. }
  956. static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  957. const uint8_t *src1, const uint8_t *src2,
  958. long width, uint32_t *unused)
  959. {
  960. int i;
  961. assert(src1==src2);
  962. for (i = 0; i < width; i++) {
  963. int r= src1[12*i + 0] + src1[12*i + 6];
  964. int g= src1[12*i + 2] + src1[12*i + 8];
  965. int b= src1[12*i + 4] + src1[12*i + 10];
  966. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  967. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  968. }
  969. }
  970. static inline void bgr48ToY(uint8_t *dst, const uint8_t *src, long width,
  971. uint32_t *unused)
  972. {
  973. int i;
  974. for (i = 0; i < width; i++) {
  975. int b = src[i*6+0];
  976. int g = src[i*6+2];
  977. int r = src[i*6+4];
  978. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  979. }
  980. }
  981. static inline void bgr48ToUV(uint8_t *dstU, uint8_t *dstV,
  982. const uint8_t *src1, const uint8_t *src2,
  983. long width, uint32_t *unused)
  984. {
  985. int i;
  986. for (i = 0; i < width; i++) {
  987. int b = src1[6*i + 0];
  988. int g = src1[6*i + 2];
  989. int r = src1[6*i + 4];
  990. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  991. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  992. }
  993. }
  994. static inline void bgr48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  995. const uint8_t *src1, const uint8_t *src2,
  996. long width, uint32_t *unused)
  997. {
  998. int i;
  999. for (i = 0; i < width; i++) {
  1000. int b= src1[12*i + 0] + src1[12*i + 6];
  1001. int g= src1[12*i + 2] + src1[12*i + 8];
  1002. int r= src1[12*i + 4] + src1[12*i + 10];
  1003. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  1004. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  1005. }
  1006. }
  1007. #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
  1008. static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
  1009. {\
  1010. int i;\
  1011. for (i=0; i<width; i++) {\
  1012. int b= (((const type*)src)[i]>>shb)&maskb;\
  1013. int g= (((const type*)src)[i]>>shg)&maskg;\
  1014. int r= (((const type*)src)[i]>>shr)&maskr;\
  1015. \
  1016. dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
  1017. }\
  1018. }
  1019. BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  1020. BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
  1021. BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  1022. BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
  1023. BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
  1024. BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
  1025. BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
  1026. BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
  1027. static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1028. {
  1029. int i;
  1030. for (i=0; i<width; i++) {
  1031. dst[i]= src[4*i];
  1032. }
  1033. }
  1034. #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
  1035. static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  1036. {\
  1037. int i;\
  1038. for (i=0; i<width; i++) {\
  1039. int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
  1040. int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
  1041. int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
  1042. \
  1043. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
  1044. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
  1045. }\
  1046. }\
  1047. static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  1048. {\
  1049. int i;\
  1050. for (i=0; i<width; i++) {\
  1051. int pix0= ((const type*)src)[2*i+0]>>shp;\
  1052. int pix1= ((const type*)src)[2*i+1]>>shp;\
  1053. int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
  1054. int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
  1055. int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
  1056. g&= maskg|(2*maskg);\
  1057. \
  1058. g>>=shg;\
  1059. \
  1060. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
  1061. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
  1062. }\
  1063. }
  1064. BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1065. BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1066. BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1067. BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  1068. BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
  1069. BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
  1070. BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
  1071. BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
  1072. static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
  1073. {
  1074. int i;
  1075. for (i=0; i<width; i++) {
  1076. int d= src[i];
  1077. dst[i]= pal[d] & 0xFF;
  1078. }
  1079. }
  1080. static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
  1081. const uint8_t *src1, const uint8_t *src2,
  1082. long width, uint32_t *pal)
  1083. {
  1084. int i;
  1085. assert(src1 == src2);
  1086. for (i=0; i<width; i++) {
  1087. int p= pal[src1[i]];
  1088. dstU[i]= p>>8;
  1089. dstV[i]= p>>16;
  1090. }
  1091. }
  1092. static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1093. {
  1094. int i, j;
  1095. for (i=0; i<width/8; i++) {
  1096. int d= ~src[i];
  1097. for(j=0; j<8; j++)
  1098. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1099. }
  1100. }
  1101. static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1102. {
  1103. int i, j;
  1104. for (i=0; i<width/8; i++) {
  1105. int d= src[i];
  1106. for(j=0; j<8; j++)
  1107. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1108. }
  1109. }
  1110. //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
  1111. //Plain C versions
  1112. #if CONFIG_RUNTIME_CPUDETECT
  1113. # define COMPILE_C 1
  1114. # if ARCH_X86
  1115. # define COMPILE_MMX 1
  1116. # define COMPILE_MMX2 1
  1117. # define COMPILE_3DNOW 1
  1118. # elif ARCH_PPC
  1119. # define COMPILE_ALTIVEC HAVE_ALTIVEC
  1120. # endif
  1121. #else /* CONFIG_RUNTIME_CPUDETECT */
  1122. # if ARCH_X86
  1123. # if HAVE_MMX2
  1124. # define COMPILE_MMX2 1
  1125. # elif HAVE_AMD3DNOW
  1126. # define COMPILE_3DNOW 1
  1127. # elif HAVE_MMX
  1128. # define COMPILE_MMX 1
  1129. # else
  1130. # define COMPILE_C 1
  1131. # endif
  1132. # elif ARCH_PPC && HAVE_ALTIVEC
  1133. # define COMPILE_ALTIVEC 1
  1134. # else
  1135. # define COMPILE_C 1
  1136. # endif
  1137. #endif
  1138. #ifndef COMPILE_C
  1139. # define COMPILE_C 0
  1140. #endif
  1141. #ifndef COMPILE_MMX
  1142. # define COMPILE_MMX 0
  1143. #endif
  1144. #ifndef COMPILE_MMX2
  1145. # define COMPILE_MMX2 0
  1146. #endif
  1147. #ifndef COMPILE_3DNOW
  1148. # define COMPILE_3DNOW 0
  1149. #endif
  1150. #ifndef COMPILE_ALTIVEC
  1151. # define COMPILE_ALTIVEC 0
  1152. #endif
  1153. #define COMPILE_TEMPLATE_MMX 0
  1154. #define COMPILE_TEMPLATE_MMX2 0
  1155. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1156. #define COMPILE_TEMPLATE_ALTIVEC 0
  1157. #if COMPILE_C
  1158. #define RENAME(a) a ## _C
  1159. #include "swscale_template.c"
  1160. #endif
  1161. #if COMPILE_ALTIVEC
  1162. #undef RENAME
  1163. #undef COMPILE_TEMPLATE_ALTIVEC
  1164. #define COMPILE_TEMPLATE_ALTIVEC 1
  1165. #define RENAME(a) a ## _altivec
  1166. #include "swscale_template.c"
  1167. #endif
  1168. #if ARCH_X86
  1169. //MMX versions
  1170. #if COMPILE_MMX
  1171. #undef RENAME
  1172. #undef COMPILE_TEMPLATE_MMX
  1173. #undef COMPILE_TEMPLATE_MMX2
  1174. #undef COMPILE_TEMPLATE_AMD3DNOW
  1175. #define COMPILE_TEMPLATE_MMX 1
  1176. #define COMPILE_TEMPLATE_MMX2 0
  1177. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1178. #define RENAME(a) a ## _MMX
  1179. #include "swscale_template.c"
  1180. #endif
  1181. //MMX2 versions
  1182. #if COMPILE_MMX2
  1183. #undef RENAME
  1184. #undef COMPILE_TEMPLATE_MMX
  1185. #undef COMPILE_TEMPLATE_MMX2
  1186. #undef COMPILE_TEMPLATE_AMD3DNOW
  1187. #define COMPILE_TEMPLATE_MMX 1
  1188. #define COMPILE_TEMPLATE_MMX2 1
  1189. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1190. #define RENAME(a) a ## _MMX2
  1191. #include "swscale_template.c"
  1192. #endif
  1193. //3DNOW versions
  1194. #if COMPILE_3DNOW
  1195. #undef RENAME
  1196. #undef COMPILE_TEMPLATE_MMX
  1197. #undef COMPILE_TEMPLATE_MMX2
  1198. #undef COMPILE_TEMPLATE_AMD3DNOW
  1199. #define COMPILE_TEMPLATE_MMX 1
  1200. #define COMPILE_TEMPLATE_MMX2 0
  1201. #define COMPILE_TEMPLATE_AMD3DNOW 1
  1202. #define RENAME(a) a ## _3DNow
  1203. #include "swscale_template.c"
  1204. #endif
  1205. #endif //ARCH_X86
  1206. SwsFunc ff_getSwsFunc(SwsContext *c)
  1207. {
  1208. #if CONFIG_RUNTIME_CPUDETECT
  1209. int flags = c->flags;
  1210. int cpuflags = av_get_cpu_flags();
  1211. flags |= (cpuflags & AV_CPU_FLAG_MMX ? SWS_CPU_CAPS_MMX : 0);
  1212. flags |= (cpuflags & AV_CPU_FLAG_MMX2 ? SWS_CPU_CAPS_MMX2 : 0);
  1213. flags |= (cpuflags & AV_CPU_FLAG_3DNOW ? SWS_CPU_CAPS_3DNOW : 0);
  1214. #if ARCH_X86
  1215. // ordered per speed fastest first
  1216. if (flags & SWS_CPU_CAPS_MMX2) {
  1217. sws_init_swScale_MMX2(c);
  1218. return swScale_MMX2;
  1219. } else if (flags & SWS_CPU_CAPS_3DNOW) {
  1220. sws_init_swScale_3DNow(c);
  1221. return swScale_3DNow;
  1222. } else if (flags & SWS_CPU_CAPS_MMX) {
  1223. sws_init_swScale_MMX(c);
  1224. return swScale_MMX;
  1225. } else {
  1226. sws_init_swScale_C(c);
  1227. return swScale_C;
  1228. }
  1229. #else
  1230. #if COMPILE_ALTIVEC
  1231. if (flags & SWS_CPU_CAPS_ALTIVEC) {
  1232. sws_init_swScale_altivec(c);
  1233. return swScale_altivec;
  1234. } else {
  1235. sws_init_swScale_C(c);
  1236. return swScale_C;
  1237. }
  1238. #endif
  1239. sws_init_swScale_C(c);
  1240. return swScale_C;
  1241. #endif /* ARCH_X86 */
  1242. #else //CONFIG_RUNTIME_CPUDETECT
  1243. #if COMPILE_TEMPLATE_MMX2
  1244. sws_init_swScale_MMX2(c);
  1245. return swScale_MMX2;
  1246. #elif COMPILE_TEMPLATE_AMD3DNOW
  1247. sws_init_swScale_3DNow(c);
  1248. return swScale_3DNow;
  1249. #elif COMPILE_TEMPLATE_MMX
  1250. sws_init_swScale_MMX(c);
  1251. return swScale_MMX;
  1252. #elif COMPILE_TEMPLATE_ALTIVEC
  1253. sws_init_swScale_altivec(c);
  1254. return swScale_altivec;
  1255. #else
  1256. sws_init_swScale_C(c);
  1257. return swScale_C;
  1258. #endif
  1259. #endif //!CONFIG_RUNTIME_CPUDETECT
  1260. }
  1261. static void copyPlane(const uint8_t *src, int srcStride,
  1262. int srcSliceY, int srcSliceH, int width,
  1263. uint8_t *dst, int dstStride)
  1264. {
  1265. dst += dstStride * srcSliceY;
  1266. if (dstStride == srcStride && srcStride > 0) {
  1267. memcpy(dst, src, srcSliceH * dstStride);
  1268. } else {
  1269. int i;
  1270. for (i=0; i<srcSliceH; i++) {
  1271. memcpy(dst, src, width);
  1272. src += srcStride;
  1273. dst += dstStride;
  1274. }
  1275. }
  1276. }
  1277. static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1278. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1279. {
  1280. uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1281. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  1282. dstParam[0], dstStride[0]);
  1283. if (c->dstFormat == PIX_FMT_NV12)
  1284. interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
  1285. else
  1286. interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
  1287. return srcSliceH;
  1288. }
  1289. static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1290. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1291. {
  1292. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1293. yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1294. return srcSliceH;
  1295. }
  1296. static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1297. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1298. {
  1299. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1300. yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1301. return srcSliceH;
  1302. }
  1303. static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1304. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1305. {
  1306. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1307. yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1308. return srcSliceH;
  1309. }
  1310. static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1311. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1312. {
  1313. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1314. yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1315. return srcSliceH;
  1316. }
  1317. static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1318. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1319. {
  1320. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1321. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1322. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1323. yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1324. if (dstParam[3])
  1325. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1326. return srcSliceH;
  1327. }
  1328. static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1329. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1330. {
  1331. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1332. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1333. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1334. yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1335. return srcSliceH;
  1336. }
  1337. static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1338. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1339. {
  1340. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1341. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1342. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1343. uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1344. if (dstParam[3])
  1345. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1346. return srcSliceH;
  1347. }
  1348. static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1349. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1350. {
  1351. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1352. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1353. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1354. uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1355. return srcSliceH;
  1356. }
  1357. static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1358. {
  1359. long i;
  1360. for (i=0; i<num_pixels; i++)
  1361. ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
  1362. }
  1363. static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1364. {
  1365. long i;
  1366. for (i=0; i<num_pixels; i++)
  1367. ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
  1368. }
  1369. static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1370. {
  1371. long i;
  1372. for (i=0; i<num_pixels; i++) {
  1373. //FIXME slow?
  1374. dst[0]= palette[src[i<<1]*4+0];
  1375. dst[1]= palette[src[i<<1]*4+1];
  1376. dst[2]= palette[src[i<<1]*4+2];
  1377. dst+= 3;
  1378. }
  1379. }
  1380. static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1381. int srcSliceH, uint8_t* dst[], int dstStride[])
  1382. {
  1383. const enum PixelFormat srcFormat= c->srcFormat;
  1384. const enum PixelFormat dstFormat= c->dstFormat;
  1385. void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
  1386. const uint8_t *palette)=NULL;
  1387. int i;
  1388. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1389. const uint8_t *srcPtr= src[0];
  1390. if (srcFormat == PIX_FMT_Y400A) {
  1391. switch (dstFormat) {
  1392. case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
  1393. case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
  1394. case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
  1395. case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
  1396. case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
  1397. case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
  1398. }
  1399. } else if (usePal(srcFormat)) {
  1400. switch (dstFormat) {
  1401. case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
  1402. case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
  1403. case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
  1404. case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
  1405. case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
  1406. case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
  1407. }
  1408. }
  1409. if (!conv)
  1410. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1411. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1412. else {
  1413. for (i=0; i<srcSliceH; i++) {
  1414. conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
  1415. srcPtr+= srcStride[0];
  1416. dstPtr+= dstStride[0];
  1417. }
  1418. }
  1419. return srcSliceH;
  1420. }
  1421. #define isRGBA32(x) ( \
  1422. (x) == PIX_FMT_ARGB \
  1423. || (x) == PIX_FMT_RGBA \
  1424. || (x) == PIX_FMT_BGRA \
  1425. || (x) == PIX_FMT_ABGR \
  1426. )
  1427. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  1428. static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1429. int srcSliceH, uint8_t* dst[], int dstStride[])
  1430. {
  1431. const enum PixelFormat srcFormat= c->srcFormat;
  1432. const enum PixelFormat dstFormat= c->dstFormat;
  1433. const int srcBpp= (c->srcFormatBpp + 7) >> 3;
  1434. const int dstBpp= (c->dstFormatBpp + 7) >> 3;
  1435. const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  1436. const int dstId= c->dstFormatBpp >> 2;
  1437. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1438. #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
  1439. if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
  1440. if ( CONV_IS(ABGR, RGBA)
  1441. || CONV_IS(ARGB, BGRA)
  1442. || CONV_IS(BGRA, ARGB)
  1443. || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
  1444. else if (CONV_IS(ABGR, ARGB)
  1445. || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
  1446. else if (CONV_IS(ABGR, BGRA)
  1447. || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
  1448. else if (CONV_IS(BGRA, RGBA)
  1449. || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
  1450. else if (CONV_IS(BGRA, ABGR)
  1451. || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
  1452. } else
  1453. /* BGR -> BGR */
  1454. if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
  1455. || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
  1456. switch(srcId | (dstId<<4)) {
  1457. case 0x34: conv= rgb16to15; break;
  1458. case 0x36: conv= rgb24to15; break;
  1459. case 0x38: conv= rgb32to15; break;
  1460. case 0x43: conv= rgb15to16; break;
  1461. case 0x46: conv= rgb24to16; break;
  1462. case 0x48: conv= rgb32to16; break;
  1463. case 0x63: conv= rgb15to24; break;
  1464. case 0x64: conv= rgb16to24; break;
  1465. case 0x68: conv= rgb32to24; break;
  1466. case 0x83: conv= rgb15to32; break;
  1467. case 0x84: conv= rgb16to32; break;
  1468. case 0x86: conv= rgb24to32; break;
  1469. }
  1470. } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
  1471. || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
  1472. switch(srcId | (dstId<<4)) {
  1473. case 0x33: conv= rgb15tobgr15; break;
  1474. case 0x34: conv= rgb16tobgr15; break;
  1475. case 0x36: conv= rgb24tobgr15; break;
  1476. case 0x38: conv= rgb32tobgr15; break;
  1477. case 0x43: conv= rgb15tobgr16; break;
  1478. case 0x44: conv= rgb16tobgr16; break;
  1479. case 0x46: conv= rgb24tobgr16; break;
  1480. case 0x48: conv= rgb32tobgr16; break;
  1481. case 0x63: conv= rgb15tobgr24; break;
  1482. case 0x64: conv= rgb16tobgr24; break;
  1483. case 0x66: conv= rgb24tobgr24; break;
  1484. case 0x68: conv= rgb32tobgr24; break;
  1485. case 0x83: conv= rgb15tobgr32; break;
  1486. case 0x84: conv= rgb16tobgr32; break;
  1487. case 0x86: conv= rgb24tobgr32; break;
  1488. }
  1489. }
  1490. if (!conv) {
  1491. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1492. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1493. } else {
  1494. const uint8_t *srcPtr= src[0];
  1495. uint8_t *dstPtr= dst[0];
  1496. if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
  1497. srcPtr += ALT32_CORR;
  1498. if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
  1499. dstPtr += ALT32_CORR;
  1500. if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
  1501. conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1502. else {
  1503. int i;
  1504. dstPtr += dstStride[0]*srcSliceY;
  1505. for (i=0; i<srcSliceH; i++) {
  1506. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1507. srcPtr+= srcStride[0];
  1508. dstPtr+= dstStride[0];
  1509. }
  1510. }
  1511. }
  1512. return srcSliceH;
  1513. }
  1514. static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1515. int srcSliceH, uint8_t* dst[], int dstStride[])
  1516. {
  1517. rgb24toyv12(
  1518. src[0],
  1519. dst[0]+ srcSliceY *dstStride[0],
  1520. dst[1]+(srcSliceY>>1)*dstStride[1],
  1521. dst[2]+(srcSliceY>>1)*dstStride[2],
  1522. c->srcW, srcSliceH,
  1523. dstStride[0], dstStride[1], srcStride[0]);
  1524. if (dst[3])
  1525. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1526. return srcSliceH;
  1527. }
  1528. static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1529. int srcSliceH, uint8_t* dst[], int dstStride[])
  1530. {
  1531. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  1532. dst[0], dstStride[0]);
  1533. planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1534. srcSliceH >> 2, srcStride[1], dstStride[1]);
  1535. planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1536. srcSliceH >> 2, srcStride[2], dstStride[2]);
  1537. if (dst[3])
  1538. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1539. return srcSliceH;
  1540. }
  1541. /* unscaled copy like stuff (assumes nearly identical formats) */
  1542. static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1543. int srcSliceH, uint8_t* dst[], int dstStride[])
  1544. {
  1545. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1546. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1547. else {
  1548. int i;
  1549. const uint8_t *srcPtr= src[0];
  1550. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1551. int length=0;
  1552. /* universal length finder */
  1553. while(length+c->srcW <= FFABS(dstStride[0])
  1554. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  1555. assert(length!=0);
  1556. for (i=0; i<srcSliceH; i++) {
  1557. memcpy(dstPtr, srcPtr, length);
  1558. srcPtr+= srcStride[0];
  1559. dstPtr+= dstStride[0];
  1560. }
  1561. }
  1562. return srcSliceH;
  1563. }
  1564. static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1565. int srcSliceH, uint8_t* dst[], int dstStride[])
  1566. {
  1567. int plane, i, j;
  1568. for (plane=0; plane<4; plane++) {
  1569. int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1570. int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1571. int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1572. const uint8_t *srcPtr= src[plane];
  1573. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1574. if (!dst[plane]) continue;
  1575. // ignore palette for GRAY8
  1576. if (plane == 1 && !dst[2]) continue;
  1577. if (!src[plane] || (plane == 1 && !src[2])) {
  1578. if(is16BPS(c->dstFormat))
  1579. length*=2;
  1580. fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
  1581. } else {
  1582. if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
  1583. if (!isBE(c->srcFormat)) srcPtr++;
  1584. for (i=0; i<height; i++) {
  1585. for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
  1586. srcPtr+= srcStride[plane];
  1587. dstPtr+= dstStride[plane];
  1588. }
  1589. } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
  1590. for (i=0; i<height; i++) {
  1591. for (j=0; j<length; j++) {
  1592. dstPtr[ j<<1 ] = srcPtr[j];
  1593. dstPtr[(j<<1)+1] = srcPtr[j];
  1594. }
  1595. srcPtr+= srcStride[plane];
  1596. dstPtr+= dstStride[plane];
  1597. }
  1598. } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
  1599. && isBE(c->srcFormat) != isBE(c->dstFormat)) {
  1600. for (i=0; i<height; i++) {
  1601. for (j=0; j<length; j++)
  1602. ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
  1603. srcPtr+= srcStride[plane];
  1604. dstPtr+= dstStride[plane];
  1605. }
  1606. } else if (dstStride[plane] == srcStride[plane] &&
  1607. srcStride[plane] > 0 && srcStride[plane] == length) {
  1608. memcpy(dst[plane] + dstStride[plane]*y, src[plane],
  1609. height*dstStride[plane]);
  1610. } else {
  1611. if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  1612. length*=2;
  1613. for (i=0; i<height; i++) {
  1614. memcpy(dstPtr, srcPtr, length);
  1615. srcPtr+= srcStride[plane];
  1616. dstPtr+= dstStride[plane];
  1617. }
  1618. }
  1619. }
  1620. }
  1621. return srcSliceH;
  1622. }
  1623. int ff_hardcodedcpuflags(void)
  1624. {
  1625. int flags = 0;
  1626. #if COMPILE_TEMPLATE_MMX2
  1627. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1628. #elif COMPILE_TEMPLATE_AMD3DNOW
  1629. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1630. #elif COMPILE_TEMPLATE_MMX
  1631. flags |= SWS_CPU_CAPS_MMX;
  1632. #elif COMPILE_TEMPLATE_ALTIVEC
  1633. flags |= SWS_CPU_CAPS_ALTIVEC;
  1634. #elif ARCH_BFIN
  1635. flags |= SWS_CPU_CAPS_BFIN;
  1636. #endif
  1637. return flags;
  1638. }
  1639. void ff_get_unscaled_swscale(SwsContext *c)
  1640. {
  1641. const enum PixelFormat srcFormat = c->srcFormat;
  1642. const enum PixelFormat dstFormat = c->dstFormat;
  1643. const int flags = c->flags;
  1644. const int dstH = c->dstH;
  1645. int needsDither;
  1646. needsDither= isAnyRGB(dstFormat)
  1647. && c->dstFormatBpp < 24
  1648. && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
  1649. /* yv12_to_nv12 */
  1650. if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
  1651. c->swScale= planarToNv12Wrapper;
  1652. }
  1653. /* yuv2bgr */
  1654. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
  1655. && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
  1656. c->swScale= ff_yuv2rgb_get_func_ptr(c);
  1657. }
  1658. if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
  1659. c->swScale= yvu9ToYv12Wrapper;
  1660. }
  1661. /* bgr24toYV12 */
  1662. if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
  1663. c->swScale= bgr24ToYv12Wrapper;
  1664. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  1665. if ( isAnyRGB(srcFormat)
  1666. && isAnyRGB(dstFormat)
  1667. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  1668. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  1669. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  1670. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  1671. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  1672. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  1673. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  1674. && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
  1675. && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
  1676. && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
  1677. && srcFormat != PIX_FMT_BGR48LE && dstFormat != PIX_FMT_BGR48LE
  1678. && srcFormat != PIX_FMT_BGR48BE && dstFormat != PIX_FMT_BGR48BE
  1679. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  1680. c->swScale= rgbToRgbWrapper;
  1681. if ((usePal(srcFormat) && (
  1682. dstFormat == PIX_FMT_RGB32 ||
  1683. dstFormat == PIX_FMT_RGB32_1 ||
  1684. dstFormat == PIX_FMT_RGB24 ||
  1685. dstFormat == PIX_FMT_BGR32 ||
  1686. dstFormat == PIX_FMT_BGR32_1 ||
  1687. dstFormat == PIX_FMT_BGR24)))
  1688. c->swScale= palToRgbWrapper;
  1689. if (srcFormat == PIX_FMT_YUV422P) {
  1690. if (dstFormat == PIX_FMT_YUYV422)
  1691. c->swScale= yuv422pToYuy2Wrapper;
  1692. else if (dstFormat == PIX_FMT_UYVY422)
  1693. c->swScale= yuv422pToUyvyWrapper;
  1694. }
  1695. /* LQ converters if -sws 0 or -sws 4*/
  1696. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  1697. /* yv12_to_yuy2 */
  1698. if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
  1699. if (dstFormat == PIX_FMT_YUYV422)
  1700. c->swScale= planarToYuy2Wrapper;
  1701. else if (dstFormat == PIX_FMT_UYVY422)
  1702. c->swScale= planarToUyvyWrapper;
  1703. }
  1704. }
  1705. if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1706. c->swScale= yuyvToYuv420Wrapper;
  1707. if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1708. c->swScale= uyvyToYuv420Wrapper;
  1709. if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
  1710. c->swScale= yuyvToYuv422Wrapper;
  1711. if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
  1712. c->swScale= uyvyToYuv422Wrapper;
  1713. #if COMPILE_ALTIVEC
  1714. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  1715. !(c->flags & SWS_BITEXACT) &&
  1716. srcFormat == PIX_FMT_YUV420P) {
  1717. // unscaled YV12 -> packed YUV, we want speed
  1718. if (dstFormat == PIX_FMT_YUYV422)
  1719. c->swScale= yv12toyuy2_unscaled_altivec;
  1720. else if (dstFormat == PIX_FMT_UYVY422)
  1721. c->swScale= yv12touyvy_unscaled_altivec;
  1722. }
  1723. #endif
  1724. /* simple copy */
  1725. if ( srcFormat == dstFormat
  1726. || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
  1727. || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
  1728. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1729. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1730. || (isGray(dstFormat) && isGray(srcFormat))
  1731. || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
  1732. && c->chrDstHSubSample == c->chrSrcHSubSample
  1733. && c->chrDstVSubSample == c->chrSrcVSubSample
  1734. && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
  1735. && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
  1736. {
  1737. if (isPacked(c->srcFormat))
  1738. c->swScale= packedCopyWrapper;
  1739. else /* Planar YUV or gray */
  1740. c->swScale= planarCopyWrapper;
  1741. }
  1742. #if ARCH_BFIN
  1743. if (flags & SWS_CPU_CAPS_BFIN)
  1744. ff_bfin_get_unscaled_swscale (c);
  1745. #endif
  1746. }
  1747. static void reset_ptr(const uint8_t* src[], int format)
  1748. {
  1749. if(!isALPHA(format))
  1750. src[3]=NULL;
  1751. if(!isPlanarYUV(format)) {
  1752. src[3]=src[2]=NULL;
  1753. if (!usePal(format))
  1754. src[1]= NULL;
  1755. }
  1756. }
  1757. static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
  1758. const int linesizes[4])
  1759. {
  1760. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  1761. int i;
  1762. for (i = 0; i < 4; i++) {
  1763. int plane = desc->comp[i].plane;
  1764. if (!data[plane] || !linesizes[plane])
  1765. return 0;
  1766. }
  1767. return 1;
  1768. }
  1769. /**
  1770. * swscale wrapper, so we don't need to export the SwsContext.
  1771. * Assumes planar YUV to be in YUV order instead of YVU.
  1772. */
  1773. int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
  1774. int srcSliceH, uint8_t* const dst[], const int dstStride[])
  1775. {
  1776. int i;
  1777. const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
  1778. uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
  1779. // do not mess up sliceDir if we have a "trailing" 0-size slice
  1780. if (srcSliceH == 0)
  1781. return 0;
  1782. if (!check_image_pointers(src, c->srcFormat, srcStride)) {
  1783. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  1784. return 0;
  1785. }
  1786. if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
  1787. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  1788. return 0;
  1789. }
  1790. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  1791. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  1792. return 0;
  1793. }
  1794. if (c->sliceDir == 0) {
  1795. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  1796. }
  1797. if (usePal(c->srcFormat)) {
  1798. for (i=0; i<256; i++) {
  1799. int p, r, g, b,y,u,v;
  1800. if(c->srcFormat == PIX_FMT_PAL8) {
  1801. p=((const uint32_t*)(src[1]))[i];
  1802. r= (p>>16)&0xFF;
  1803. g= (p>> 8)&0xFF;
  1804. b= p &0xFF;
  1805. } else if(c->srcFormat == PIX_FMT_RGB8) {
  1806. r= (i>>5 )*36;
  1807. g= ((i>>2)&7)*36;
  1808. b= (i&3 )*85;
  1809. } else if(c->srcFormat == PIX_FMT_BGR8) {
  1810. b= (i>>6 )*85;
  1811. g= ((i>>3)&7)*36;
  1812. r= (i&7 )*36;
  1813. } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
  1814. r= (i>>3 )*255;
  1815. g= ((i>>1)&3)*85;
  1816. b= (i&1 )*255;
  1817. } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
  1818. r = g = b = i;
  1819. } else {
  1820. assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
  1821. b= (i>>3 )*255;
  1822. g= ((i>>1)&3)*85;
  1823. r= (i&1 )*255;
  1824. }
  1825. y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1826. u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1827. v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1828. c->pal_yuv[i]= y + (u<<8) + (v<<16);
  1829. switch(c->dstFormat) {
  1830. case PIX_FMT_BGR32:
  1831. #if !HAVE_BIGENDIAN
  1832. case PIX_FMT_RGB24:
  1833. #endif
  1834. c->pal_rgb[i]= r + (g<<8) + (b<<16);
  1835. break;
  1836. case PIX_FMT_BGR32_1:
  1837. #if HAVE_BIGENDIAN
  1838. case PIX_FMT_BGR24:
  1839. #endif
  1840. c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
  1841. break;
  1842. case PIX_FMT_RGB32_1:
  1843. #if HAVE_BIGENDIAN
  1844. case PIX_FMT_RGB24:
  1845. #endif
  1846. c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
  1847. break;
  1848. case PIX_FMT_RGB32:
  1849. #if !HAVE_BIGENDIAN
  1850. case PIX_FMT_BGR24:
  1851. #endif
  1852. default:
  1853. c->pal_rgb[i]= b + (g<<8) + (r<<16);
  1854. }
  1855. }
  1856. }
  1857. // copy strides, so they can safely be modified
  1858. if (c->sliceDir == 1) {
  1859. // slices go from top to bottom
  1860. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
  1861. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
  1862. reset_ptr(src2, c->srcFormat);
  1863. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1864. /* reset slice direction at end of frame */
  1865. if (srcSliceY + srcSliceH == c->srcH)
  1866. c->sliceDir = 0;
  1867. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
  1868. } else {
  1869. // slices go from bottom to top => we flip the image internally
  1870. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
  1871. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
  1872. src2[0] += (srcSliceH-1)*srcStride[0];
  1873. if (!usePal(c->srcFormat))
  1874. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  1875. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  1876. src2[3] += (srcSliceH-1)*srcStride[3];
  1877. dst2[0] += ( c->dstH -1)*dstStride[0];
  1878. dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
  1879. dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
  1880. dst2[3] += ( c->dstH -1)*dstStride[3];
  1881. reset_ptr(src2, c->srcFormat);
  1882. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1883. /* reset slice direction at end of frame */
  1884. if (!srcSliceY)
  1885. c->sliceDir = 0;
  1886. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  1887. }
  1888. }
  1889. #if LIBSWSCALE_VERSION_MAJOR < 1
  1890. int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
  1891. int srcSliceH, uint8_t* dst[], int dstStride[])
  1892. {
  1893. return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
  1894. }
  1895. #endif
  1896. /* Convert the palette to the same packed 32-bit format as the palette */
  1897. void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1898. {
  1899. long i;
  1900. for (i=0; i<num_pixels; i++)
  1901. ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
  1902. }
  1903. /* Palette format: ABCD -> dst format: ABC */
  1904. void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  1905. {
  1906. long i;
  1907. for (i=0; i<num_pixels; i++) {
  1908. //FIXME slow?
  1909. dst[0]= palette[src[i]*4+0];
  1910. dst[1]= palette[src[i]*4+1];
  1911. dst[2]= palette[src[i]*4+2];
  1912. dst+= 3;
  1913. }
  1914. }