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.

2056 lines
72KB

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