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.

2118 lines
77KB

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