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.

2229 lines
79KB

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