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.

2113 lines
78KB

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