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.

2949 lines
111KB

  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/cpu.h"
  61. #include "libavutil/avutil.h"
  62. #include "libavutil/mathematics.h"
  63. #include "libavutil/bswap.h"
  64. #include "libavutil/pixdesc.h"
  65. #define RGB2YUV_SHIFT 15
  66. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  67. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  68. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  69. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  70. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  71. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  72. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  73. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  74. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  75. /*
  76. NOTES
  77. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  78. TODO
  79. more intelligent misalignment avoidance for the horizontal scaler
  80. write special vertical cubic upscale version
  81. optimize C code (YV12 / minmax)
  82. add support for packed pixel YUV input & output
  83. add support for Y8 output
  84. optimize BGR24 & BGR32
  85. add BGR4 output support
  86. write special BGR->BGR scaler
  87. */
  88. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
  89. { 1, 3, 1, 3, 1, 3, 1, 3, },
  90. { 2, 0, 2, 0, 2, 0, 2, 0, },
  91. };
  92. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
  93. { 6, 2, 6, 2, 6, 2, 6, 2, },
  94. { 0, 4, 0, 4, 0, 4, 0, 4, },
  95. };
  96. DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
  97. { 8, 4, 11, 7, 8, 4, 11, 7, },
  98. { 2, 14, 1, 13, 2, 14, 1, 13, },
  99. { 10, 6, 9, 5, 10, 6, 9, 5, },
  100. { 0, 12, 3, 15, 0, 12, 3, 15, },
  101. };
  102. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
  103. { 17, 9, 23, 15, 16, 8, 22, 14, },
  104. { 5, 29, 3, 27, 4, 28, 2, 26, },
  105. { 21, 13, 19, 11, 20, 12, 18, 10, },
  106. { 0, 24, 6, 30, 1, 25, 7, 31, },
  107. { 16, 8, 22, 14, 17, 9, 23, 15, },
  108. { 4, 28, 2, 26, 5, 29, 3, 27, },
  109. { 20, 12, 18, 10, 21, 13, 19, 11, },
  110. { 1, 25, 7, 31, 0, 24, 6, 30, },
  111. };
  112. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
  113. { 0, 55, 14, 68, 3, 58, 17, 72, },
  114. { 37, 18, 50, 32, 40, 22, 54, 35, },
  115. { 9, 64, 5, 59, 13, 67, 8, 63, },
  116. { 46, 27, 41, 23, 49, 31, 44, 26, },
  117. { 2, 57, 16, 71, 1, 56, 15, 70, },
  118. { 39, 21, 52, 34, 38, 19, 51, 33, },
  119. { 11, 66, 7, 62, 10, 65, 6, 60, },
  120. { 48, 30, 43, 25, 47, 29, 42, 24, },
  121. };
  122. #if 1
  123. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  124. {117, 62, 158, 103, 113, 58, 155, 100, },
  125. { 34, 199, 21, 186, 31, 196, 17, 182, },
  126. {144, 89, 131, 76, 141, 86, 127, 72, },
  127. { 0, 165, 41, 206, 10, 175, 52, 217, },
  128. {110, 55, 151, 96, 120, 65, 162, 107, },
  129. { 28, 193, 14, 179, 38, 203, 24, 189, },
  130. {138, 83, 124, 69, 148, 93, 134, 79, },
  131. { 7, 172, 48, 213, 3, 168, 45, 210, },
  132. };
  133. #elif 1
  134. // tries to correct a gamma of 1.5
  135. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  136. { 0, 143, 18, 200, 2, 156, 25, 215, },
  137. { 78, 28, 125, 64, 89, 36, 138, 74, },
  138. { 10, 180, 3, 161, 16, 195, 8, 175, },
  139. {109, 51, 93, 38, 121, 60, 105, 47, },
  140. { 1, 152, 23, 210, 0, 147, 20, 205, },
  141. { 85, 33, 134, 71, 81, 30, 130, 67, },
  142. { 14, 190, 6, 171, 12, 185, 5, 166, },
  143. {117, 57, 101, 44, 113, 54, 97, 41, },
  144. };
  145. #elif 1
  146. // tries to correct a gamma of 2.0
  147. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  148. { 0, 124, 8, 193, 0, 140, 12, 213, },
  149. { 55, 14, 104, 42, 66, 19, 119, 52, },
  150. { 3, 168, 1, 145, 6, 187, 3, 162, },
  151. { 86, 31, 70, 21, 99, 39, 82, 28, },
  152. { 0, 134, 11, 206, 0, 129, 9, 200, },
  153. { 62, 17, 114, 48, 58, 16, 109, 45, },
  154. { 5, 181, 2, 157, 4, 175, 1, 151, },
  155. { 95, 36, 78, 26, 90, 34, 74, 24, },
  156. };
  157. #else
  158. // tries to correct a gamma of 2.5
  159. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  160. { 0, 107, 3, 187, 0, 125, 6, 212, },
  161. { 39, 7, 86, 28, 49, 11, 102, 36, },
  162. { 1, 158, 0, 131, 3, 180, 1, 151, },
  163. { 68, 19, 52, 12, 81, 25, 64, 17, },
  164. { 0, 119, 5, 203, 0, 113, 4, 195, },
  165. { 45, 9, 96, 33, 42, 8, 91, 30, },
  166. { 2, 172, 1, 144, 2, 165, 0, 137, },
  167. { 77, 23, 60, 15, 72, 21, 56, 14, },
  168. };
  169. #endif
  170. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_128)[8][8] = {
  171. { 36, 68, 60, 92, 34, 66, 58, 90,},
  172. { 100, 4,124, 28, 98, 2,122, 26,},
  173. { 52, 84, 44, 76, 50, 82, 42, 74,},
  174. { 116, 20,108, 12,114, 18,106, 10,},
  175. { 32, 64, 56, 88, 38, 70, 62, 94,},
  176. { 96, 0,120, 24,102, 6,126, 30,},
  177. { 48, 80, 40, 72, 54, 86, 46, 78,},
  178. { 112, 16,104, 8,118, 22,110, 14,},
  179. };
  180. DECLARE_ALIGNED(8, const uint8_t, ff_sws_pb_64)[8] =
  181. { 64, 64, 64, 64, 64, 64, 64, 64 };
  182. DECLARE_ALIGNED(8, const uint8_t, dithers)[8][8][8]={
  183. {
  184. { 0, 1, 0, 1, 0, 1, 0, 1,},
  185. { 1, 0, 1, 0, 1, 0, 1, 0,},
  186. { 0, 1, 0, 1, 0, 1, 0, 1,},
  187. { 1, 0, 1, 0, 1, 0, 1, 0,},
  188. { 0, 1, 0, 1, 0, 1, 0, 1,},
  189. { 1, 0, 1, 0, 1, 0, 1, 0,},
  190. { 0, 1, 0, 1, 0, 1, 0, 1,},
  191. { 1, 0, 1, 0, 1, 0, 1, 0,},
  192. },{
  193. { 1, 2, 1, 2, 1, 2, 1, 2,},
  194. { 3, 0, 3, 0, 3, 0, 3, 0,},
  195. { 1, 2, 1, 2, 1, 2, 1, 2,},
  196. { 3, 0, 3, 0, 3, 0, 3, 0,},
  197. { 1, 2, 1, 2, 1, 2, 1, 2,},
  198. { 3, 0, 3, 0, 3, 0, 3, 0,},
  199. { 1, 2, 1, 2, 1, 2, 1, 2,},
  200. { 3, 0, 3, 0, 3, 0, 3, 0,},
  201. },{
  202. { 2, 4, 3, 5, 2, 4, 3, 5,},
  203. { 6, 0, 7, 1, 6, 0, 7, 1,},
  204. { 3, 5, 2, 4, 3, 5, 2, 4,},
  205. { 7, 1, 6, 0, 7, 1, 6, 0,},
  206. { 2, 4, 3, 5, 2, 4, 3, 5,},
  207. { 6, 0, 7, 1, 6, 0, 7, 1,},
  208. { 3, 5, 2, 4, 3, 5, 2, 4,},
  209. { 7, 1, 6, 0, 7, 1, 6, 0,},
  210. },{
  211. { 4, 8, 7, 11, 4, 8, 7, 11,},
  212. { 12, 0, 15, 3, 12, 0, 15, 3,},
  213. { 6, 10, 5, 9, 6, 10, 5, 9,},
  214. { 14, 2, 13, 1, 14, 2, 13, 1,},
  215. { 4, 8, 7, 11, 4, 8, 7, 11,},
  216. { 12, 0, 15, 3, 12, 0, 15, 3,},
  217. { 6, 10, 5, 9, 6, 10, 5, 9,},
  218. { 14, 2, 13, 1, 14, 2, 13, 1,},
  219. },{
  220. { 9, 17, 15, 23, 8, 16, 14, 22,},
  221. { 25, 1, 31, 7, 24, 0, 30, 6,},
  222. { 13, 21, 11, 19, 12, 20, 10, 18,},
  223. { 29, 5, 27, 3, 28, 4, 26, 2,},
  224. { 8, 16, 14, 22, 9, 17, 15, 23,},
  225. { 24, 0, 30, 6, 25, 1, 31, 7,},
  226. { 12, 20, 10, 18, 13, 21, 11, 19,},
  227. { 28, 4, 26, 2, 29, 5, 27, 3,},
  228. },{
  229. { 18, 34, 30, 46, 17, 33, 29, 45,},
  230. { 50, 2, 62, 14, 49, 1, 61, 13,},
  231. { 26, 42, 22, 38, 25, 41, 21, 37,},
  232. { 58, 10, 54, 6, 57, 9, 53, 5,},
  233. { 16, 32, 28, 44, 19, 35, 31, 47,},
  234. { 48, 0, 60, 12, 51, 3, 63, 15,},
  235. { 24, 40, 20, 36, 27, 43, 23, 39,},
  236. { 56, 8, 52, 4, 59, 11, 55, 7,},
  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. { 36, 68, 60, 92, 34, 66, 58, 90,},
  248. { 100, 4,124, 28, 98, 2,122, 26,},
  249. { 52, 84, 44, 76, 50, 82, 42, 74,},
  250. { 116, 20,108, 12,114, 18,106, 10,},
  251. { 32, 64, 56, 88, 38, 70, 62, 94,},
  252. { 96, 0,120, 24,102, 6,126, 30,},
  253. { 48, 80, 40, 72, 54, 86, 46, 78,},
  254. { 112, 16,104, 8,118, 22,110, 14,},
  255. }};
  256. static const uint8_t flat64[8]={64,64,64,64,64,64,64,64};
  257. const uint16_t dither_scale[15][16]={
  258. { 2, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,},
  259. { 2, 3, 7, 7, 13, 13, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,},
  260. { 3, 3, 4, 15, 15, 29, 57, 57, 57, 113, 113, 113, 113, 113, 113, 113,},
  261. { 3, 4, 4, 5, 31, 31, 61, 121, 241, 241, 241, 241, 481, 481, 481, 481,},
  262. { 3, 4, 5, 5, 6, 63, 63, 125, 249, 497, 993, 993, 993, 993, 993, 1985,},
  263. { 3, 5, 6, 6, 6, 7, 127, 127, 253, 505, 1009, 2017, 4033, 4033, 4033, 4033,},
  264. { 3, 5, 6, 7, 7, 7, 8, 255, 255, 509, 1017, 2033, 4065, 8129,16257,16257,},
  265. { 3, 5, 6, 8, 8, 8, 8, 9, 511, 511, 1021, 2041, 4081, 8161,16321,32641,},
  266. { 3, 5, 7, 8, 9, 9, 9, 9, 10, 1023, 1023, 2045, 4089, 8177,16353,32705,},
  267. { 3, 5, 7, 8, 10, 10, 10, 10, 10, 11, 2047, 2047, 4093, 8185,16369,32737,},
  268. { 3, 5, 7, 8, 10, 11, 11, 11, 11, 11, 12, 4095, 4095, 8189,16377,32753,},
  269. { 3, 5, 7, 9, 10, 12, 12, 12, 12, 12, 12, 13, 8191, 8191,16381,32761,},
  270. { 3, 5, 7, 9, 10, 12, 13, 13, 13, 13, 13, 13, 14,16383,16383,32765,},
  271. { 3, 5, 7, 9, 10, 12, 14, 14, 14, 14, 14, 14, 14, 15,32767,32767,},
  272. { 3, 5, 7, 9, 11, 12, 14, 15, 15, 15, 15, 15, 15, 15, 16,65535,},
  273. };
  274. #define output_pixel(pos, val, bias, signedness) \
  275. if (big_endian) { \
  276. AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  277. } else { \
  278. AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  279. }
  280. static av_always_inline void
  281. yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
  282. int big_endian, int output_bits)
  283. {
  284. int i;
  285. int shift = 19 - output_bits;
  286. for (i = 0; i < dstW; i++) {
  287. int val = src[i] + (1 << (shift - 1));
  288. output_pixel(&dest[i], val, 0, uint);
  289. }
  290. }
  291. static av_always_inline void
  292. yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
  293. const int32_t **src, uint16_t *dest, int dstW,
  294. int big_endian, int output_bits)
  295. {
  296. int i;
  297. int shift = 15 + 16 - output_bits;
  298. for (i = 0; i < dstW; i++) {
  299. int val = 1 << (30-output_bits);
  300. int j;
  301. /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
  302. * filters (or anything with negative coeffs, the range can be slightly
  303. * wider in both directions. To account for this overflow, we subtract
  304. * a constant so it always fits in the signed range (assuming a
  305. * reasonable filterSize), and re-add that at the end. */
  306. val -= 0x40000000;
  307. for (j = 0; j < filterSize; j++)
  308. val += src[j][i] * filter[j];
  309. output_pixel(&dest[i], val, 0x8000, int);
  310. }
  311. }
  312. #undef output_pixel
  313. #define output_pixel(pos, val) \
  314. if (big_endian) { \
  315. AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  316. } else { \
  317. AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  318. }
  319. static av_always_inline void
  320. yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
  321. int big_endian, int output_bits)
  322. {
  323. int i;
  324. int shift = 15 - output_bits;
  325. for (i = 0; i < dstW; i++) {
  326. int val = src[i] + (1 << (shift - 1));
  327. output_pixel(&dest[i], val);
  328. }
  329. }
  330. static av_always_inline void
  331. yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
  332. const int16_t **src, uint16_t *dest, int dstW,
  333. int big_endian, int output_bits)
  334. {
  335. int i;
  336. int shift = 11 + 16 - output_bits;
  337. for (i = 0; i < dstW; i++) {
  338. int val = 1 << (26-output_bits);
  339. int j;
  340. for (j = 0; j < filterSize; j++)
  341. val += src[j][i] * filter[j];
  342. output_pixel(&dest[i], val);
  343. }
  344. }
  345. #undef output_pixel
  346. #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
  347. static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
  348. uint8_t *dest, int dstW, \
  349. const uint8_t *dither, int offset)\
  350. { \
  351. yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
  352. (uint16_t *) dest, dstW, is_be, bits); \
  353. }\
  354. static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
  355. const int16_t **src, uint8_t *dest, int dstW, \
  356. const uint8_t *dither, int offset)\
  357. { \
  358. yuv2planeX_## template_size ## _c_template(filter, \
  359. filterSize, (const typeX_t **) src, \
  360. (uint16_t *) dest, dstW, is_be, bits); \
  361. }
  362. yuv2NBPS( 9, BE, 1, 10, int16_t);
  363. yuv2NBPS( 9, LE, 0, 10, int16_t);
  364. yuv2NBPS(10, BE, 1, 10, int16_t);
  365. yuv2NBPS(10, LE, 0, 10, int16_t);
  366. yuv2NBPS(16, BE, 1, 16, int32_t);
  367. yuv2NBPS(16, LE, 0, 16, int32_t);
  368. static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
  369. const int16_t **src, uint8_t *dest, int dstW,
  370. const uint8_t *dither, int offset)
  371. {
  372. int i;
  373. for (i=0; i<dstW; i++) {
  374. int val = dither[(i + offset) & 7] << 12;
  375. int j;
  376. for (j=0; j<filterSize; j++)
  377. val += src[j][i] * filter[j];
  378. dest[i]= av_clip_uint8(val>>19);
  379. }
  380. }
  381. static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
  382. const uint8_t *dither, int offset)
  383. {
  384. int i;
  385. for (i=0; i<dstW; i++) {
  386. int val = (src[i] + dither[(i + offset) & 7]) >> 7;
  387. dest[i]= av_clip_uint8(val);
  388. }
  389. }
  390. static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
  391. const int16_t **chrUSrc, const int16_t **chrVSrc,
  392. uint8_t *dest, int chrDstW)
  393. {
  394. enum PixelFormat dstFormat = c->dstFormat;
  395. const uint8_t *chrDither = c->chrDither8;
  396. int i;
  397. if (dstFormat == PIX_FMT_NV12)
  398. for (i=0; i<chrDstW; i++) {
  399. int u = chrDither[i & 7] << 12;
  400. int v = chrDither[(i + 3) & 7] << 12;
  401. int j;
  402. for (j=0; j<chrFilterSize; j++) {
  403. u += chrUSrc[j][i] * chrFilter[j];
  404. v += chrVSrc[j][i] * chrFilter[j];
  405. }
  406. dest[2*i]= av_clip_uint8(u>>19);
  407. dest[2*i+1]= av_clip_uint8(v>>19);
  408. }
  409. else
  410. for (i=0; i<chrDstW; i++) {
  411. int u = chrDither[i & 7] << 12;
  412. int v = chrDither[(i + 3) & 7] << 12;
  413. int j;
  414. for (j=0; j<chrFilterSize; j++) {
  415. u += chrUSrc[j][i] * chrFilter[j];
  416. v += chrVSrc[j][i] * chrFilter[j];
  417. }
  418. dest[2*i]= av_clip_uint8(v>>19);
  419. dest[2*i+1]= av_clip_uint8(u>>19);
  420. }
  421. }
  422. #define output_pixel(pos, val) \
  423. if (target == PIX_FMT_GRAY16BE) { \
  424. AV_WB16(pos, val); \
  425. } else { \
  426. AV_WL16(pos, val); \
  427. }
  428. static av_always_inline void
  429. yuv2gray16_X_c_template(SwsContext *c, const int16_t *lumFilter,
  430. const int32_t **lumSrc, int lumFilterSize,
  431. const int16_t *chrFilter, const int32_t **chrUSrc,
  432. const int32_t **chrVSrc, int chrFilterSize,
  433. const int32_t **alpSrc, uint16_t *dest, int dstW,
  434. int y, enum PixelFormat target)
  435. {
  436. int i;
  437. for (i = 0; i < (dstW >> 1); i++) {
  438. int j;
  439. int Y1 = 1 << 14;
  440. int Y2 = 1 << 14;
  441. for (j = 0; j < lumFilterSize; j++) {
  442. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  443. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  444. }
  445. Y1 >>= 15;
  446. Y2 >>= 15;
  447. if ((Y1 | Y2) & 0x10000) {
  448. Y1 = av_clip_uint16(Y1);
  449. Y2 = av_clip_uint16(Y2);
  450. }
  451. output_pixel(&dest[i * 2 + 0], Y1);
  452. output_pixel(&dest[i * 2 + 1], Y2);
  453. }
  454. }
  455. static av_always_inline void
  456. yuv2gray16_2_c_template(SwsContext *c, const int32_t *buf[2],
  457. const int32_t *ubuf[2], const int32_t *vbuf[2],
  458. const int32_t *abuf[2], uint16_t *dest, int dstW,
  459. int yalpha, int uvalpha, int y,
  460. enum PixelFormat target)
  461. {
  462. int yalpha1 = 4095 - yalpha;
  463. int i;
  464. const int32_t *buf0 = buf[0], *buf1 = buf[1];
  465. for (i = 0; i < (dstW >> 1); i++) {
  466. int Y1 = (buf0[i * 2 ] * yalpha1 + buf1[i * 2 ] * yalpha) >> 15;
  467. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 15;
  468. output_pixel(&dest[i * 2 + 0], Y1);
  469. output_pixel(&dest[i * 2 + 1], Y2);
  470. }
  471. }
  472. static av_always_inline void
  473. yuv2gray16_1_c_template(SwsContext *c, const int32_t *buf0,
  474. const int32_t *ubuf[2], const int32_t *vbuf[2],
  475. const int32_t *abuf0, uint16_t *dest, int dstW,
  476. int uvalpha, int y, enum PixelFormat target)
  477. {
  478. int i;
  479. for (i = 0; i < (dstW >> 1); i++) {
  480. int Y1 = (buf0[i * 2 ]+4)>>3;
  481. int Y2 = (buf0[i * 2 + 1]+4)>>3;
  482. output_pixel(&dest[i * 2 + 0], Y1);
  483. output_pixel(&dest[i * 2 + 1], Y2);
  484. }
  485. }
  486. #undef output_pixel
  487. #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
  488. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  489. const int16_t **_lumSrc, int lumFilterSize, \
  490. const int16_t *chrFilter, const int16_t **_chrUSrc, \
  491. const int16_t **_chrVSrc, int chrFilterSize, \
  492. const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
  493. int y) \
  494. { \
  495. const int32_t **lumSrc = (const int32_t **) _lumSrc, \
  496. **chrUSrc = (const int32_t **) _chrUSrc, \
  497. **chrVSrc = (const int32_t **) _chrVSrc, \
  498. **alpSrc = (const int32_t **) _alpSrc; \
  499. uint16_t *dest = (uint16_t *) _dest; \
  500. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  501. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  502. alpSrc, dest, dstW, y, fmt); \
  503. } \
  504. \
  505. static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
  506. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  507. const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
  508. int yalpha, int uvalpha, int y) \
  509. { \
  510. const int32_t **buf = (const int32_t **) _buf, \
  511. **ubuf = (const int32_t **) _ubuf, \
  512. **vbuf = (const int32_t **) _vbuf, \
  513. **abuf = (const int32_t **) _abuf; \
  514. uint16_t *dest = (uint16_t *) _dest; \
  515. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  516. dest, dstW, yalpha, uvalpha, y, fmt); \
  517. } \
  518. \
  519. static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
  520. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  521. const int16_t *_abuf0, uint8_t *_dest, int dstW, \
  522. int uvalpha, int y) \
  523. { \
  524. const int32_t *buf0 = (const int32_t *) _buf0, \
  525. **ubuf = (const int32_t **) _ubuf, \
  526. **vbuf = (const int32_t **) _vbuf, \
  527. *abuf0 = (const int32_t *) _abuf0; \
  528. uint16_t *dest = (uint16_t *) _dest; \
  529. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  530. dstW, uvalpha, y, fmt); \
  531. }
  532. YUV2PACKED16WRAPPER(yuv2gray16,, LE, PIX_FMT_GRAY16LE);
  533. YUV2PACKED16WRAPPER(yuv2gray16,, BE, PIX_FMT_GRAY16BE);
  534. #define output_pixel(pos, acc) \
  535. if (target == PIX_FMT_MONOBLACK) { \
  536. pos = acc; \
  537. } else { \
  538. pos = ~acc; \
  539. }
  540. static av_always_inline void
  541. yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
  542. const int16_t **lumSrc, int lumFilterSize,
  543. const int16_t *chrFilter, const int16_t **chrUSrc,
  544. const int16_t **chrVSrc, int chrFilterSize,
  545. const int16_t **alpSrc, uint8_t *dest, int dstW,
  546. int y, enum PixelFormat target)
  547. {
  548. const uint8_t * const d128=dither_8x8_220[y&7];
  549. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  550. int i;
  551. int acc = 0;
  552. for (i = 0; i < dstW - 1; i += 2) {
  553. int j;
  554. int Y1 = 1 << 18;
  555. int Y2 = 1 << 18;
  556. for (j = 0; j < lumFilterSize; j++) {
  557. Y1 += lumSrc[j][i] * lumFilter[j];
  558. Y2 += lumSrc[j][i+1] * lumFilter[j];
  559. }
  560. Y1 >>= 19;
  561. Y2 >>= 19;
  562. if ((Y1 | Y2) & 0x100) {
  563. Y1 = av_clip_uint8(Y1);
  564. Y2 = av_clip_uint8(Y2);
  565. }
  566. acc += acc + g[Y1 + d128[(i + 0) & 7]];
  567. acc += acc + g[Y2 + d128[(i + 1) & 7]];
  568. if ((i & 7) == 6) {
  569. output_pixel(*dest++, acc);
  570. }
  571. }
  572. }
  573. static av_always_inline void
  574. yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
  575. const int16_t *ubuf[2], const int16_t *vbuf[2],
  576. const int16_t *abuf[2], uint8_t *dest, int dstW,
  577. int yalpha, int uvalpha, int y,
  578. enum PixelFormat target)
  579. {
  580. const int16_t *buf0 = buf[0], *buf1 = buf[1];
  581. const uint8_t * const d128 = dither_8x8_220[y & 7];
  582. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  583. int yalpha1 = 4095 - yalpha;
  584. int i;
  585. for (i = 0; i < dstW - 7; i += 8) {
  586. int acc = g[((buf0[i ] * yalpha1 + buf1[i ] * yalpha) >> 19) + d128[0]];
  587. acc += acc + g[((buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19) + d128[1]];
  588. acc += acc + g[((buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19) + d128[2]];
  589. acc += acc + g[((buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19) + d128[3]];
  590. acc += acc + g[((buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19) + d128[4]];
  591. acc += acc + g[((buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19) + d128[5]];
  592. acc += acc + g[((buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19) + d128[6]];
  593. acc += acc + g[((buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19) + d128[7]];
  594. output_pixel(*dest++, acc);
  595. }
  596. }
  597. static av_always_inline void
  598. yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
  599. const int16_t *ubuf[2], const int16_t *vbuf[2],
  600. const int16_t *abuf0, uint8_t *dest, int dstW,
  601. int uvalpha, int y, enum PixelFormat target)
  602. {
  603. const uint8_t * const d128 = dither_8x8_220[y & 7];
  604. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  605. int i;
  606. for (i = 0; i < dstW - 7; i += 8) {
  607. int acc = g[(buf0[i ] >> 7) + d128[0]];
  608. acc += acc + g[(buf0[i + 1] >> 7) + d128[1]];
  609. acc += acc + g[(buf0[i + 2] >> 7) + d128[2]];
  610. acc += acc + g[(buf0[i + 3] >> 7) + d128[3]];
  611. acc += acc + g[(buf0[i + 4] >> 7) + d128[4]];
  612. acc += acc + g[(buf0[i + 5] >> 7) + d128[5]];
  613. acc += acc + g[(buf0[i + 6] >> 7) + d128[6]];
  614. acc += acc + g[(buf0[i + 7] >> 7) + d128[7]];
  615. output_pixel(*dest++, acc);
  616. }
  617. }
  618. #undef output_pixel
  619. #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
  620. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  621. const int16_t **lumSrc, int lumFilterSize, \
  622. const int16_t *chrFilter, const int16_t **chrUSrc, \
  623. const int16_t **chrVSrc, int chrFilterSize, \
  624. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  625. int y) \
  626. { \
  627. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  628. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  629. alpSrc, dest, dstW, y, fmt); \
  630. } \
  631. \
  632. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  633. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  634. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  635. int yalpha, int uvalpha, int y) \
  636. { \
  637. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  638. dest, dstW, yalpha, uvalpha, y, fmt); \
  639. } \
  640. \
  641. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  642. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  643. const int16_t *abuf0, uint8_t *dest, int dstW, \
  644. int uvalpha, int y) \
  645. { \
  646. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
  647. abuf0, dest, dstW, uvalpha, \
  648. y, fmt); \
  649. }
  650. YUV2PACKEDWRAPPER(yuv2mono,, white, PIX_FMT_MONOWHITE);
  651. YUV2PACKEDWRAPPER(yuv2mono,, black, PIX_FMT_MONOBLACK);
  652. #define output_pixels(pos, Y1, U, Y2, V) \
  653. if (target == PIX_FMT_YUYV422) { \
  654. dest[pos + 0] = Y1; \
  655. dest[pos + 1] = U; \
  656. dest[pos + 2] = Y2; \
  657. dest[pos + 3] = V; \
  658. } else { \
  659. dest[pos + 0] = U; \
  660. dest[pos + 1] = Y1; \
  661. dest[pos + 2] = V; \
  662. dest[pos + 3] = Y2; \
  663. }
  664. static av_always_inline void
  665. yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
  666. const int16_t **lumSrc, int lumFilterSize,
  667. const int16_t *chrFilter, const int16_t **chrUSrc,
  668. const int16_t **chrVSrc, int chrFilterSize,
  669. const int16_t **alpSrc, uint8_t *dest, int dstW,
  670. int y, enum PixelFormat target)
  671. {
  672. int i;
  673. for (i = 0; i < (dstW >> 1); i++) {
  674. int j;
  675. int Y1 = 1 << 18;
  676. int Y2 = 1 << 18;
  677. int U = 1 << 18;
  678. int V = 1 << 18;
  679. for (j = 0; j < lumFilterSize; j++) {
  680. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  681. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  682. }
  683. for (j = 0; j < chrFilterSize; j++) {
  684. U += chrUSrc[j][i] * chrFilter[j];
  685. V += chrVSrc[j][i] * chrFilter[j];
  686. }
  687. Y1 >>= 19;
  688. Y2 >>= 19;
  689. U >>= 19;
  690. V >>= 19;
  691. if ((Y1 | Y2 | U | V) & 0x100) {
  692. Y1 = av_clip_uint8(Y1);
  693. Y2 = av_clip_uint8(Y2);
  694. U = av_clip_uint8(U);
  695. V = av_clip_uint8(V);
  696. }
  697. output_pixels(4*i, Y1, U, Y2, V);
  698. }
  699. }
  700. static av_always_inline void
  701. yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
  702. const int16_t *ubuf[2], const int16_t *vbuf[2],
  703. const int16_t *abuf[2], uint8_t *dest, int dstW,
  704. int yalpha, int uvalpha, int y,
  705. enum PixelFormat target)
  706. {
  707. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  708. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  709. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  710. int yalpha1 = 4095 - yalpha;
  711. int uvalpha1 = 4095 - uvalpha;
  712. int i;
  713. for (i = 0; i < (dstW >> 1); i++) {
  714. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  715. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  716. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  717. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  718. output_pixels(i * 4, Y1, U, Y2, V);
  719. }
  720. }
  721. static av_always_inline void
  722. yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
  723. const int16_t *ubuf[2], const int16_t *vbuf[2],
  724. const int16_t *abuf0, uint8_t *dest, int dstW,
  725. int uvalpha, int y, enum PixelFormat target)
  726. {
  727. const int16_t *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  728. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  729. int i;
  730. if (uvalpha < 2048) {
  731. for (i = 0; i < (dstW >> 1); i++) {
  732. int Y1 = buf0[i * 2] >> 7;
  733. int Y2 = buf0[i * 2 + 1] >> 7;
  734. int U = ubuf1[i] >> 7;
  735. int V = vbuf1[i] >> 7;
  736. output_pixels(i * 4, Y1, U, Y2, V);
  737. }
  738. } else {
  739. for (i = 0; i < (dstW >> 1); i++) {
  740. int Y1 = buf0[i * 2] >> 7;
  741. int Y2 = buf0[i * 2 + 1] >> 7;
  742. int U = (ubuf0[i] + ubuf1[i]) >> 8;
  743. int V = (vbuf0[i] + vbuf1[i]) >> 8;
  744. output_pixels(i * 4, Y1, U, Y2, V);
  745. }
  746. }
  747. }
  748. #undef output_pixels
  749. YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, PIX_FMT_YUYV422);
  750. YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, PIX_FMT_UYVY422);
  751. #define R_B ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? R : B)
  752. #define B_R ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? B : R)
  753. #define output_pixel(pos, val) \
  754. if (isBE(target)) { \
  755. AV_WB16(pos, val); \
  756. } else { \
  757. AV_WL16(pos, val); \
  758. }
  759. static av_always_inline void
  760. yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
  761. const int32_t **lumSrc, int lumFilterSize,
  762. const int16_t *chrFilter, const int32_t **chrUSrc,
  763. const int32_t **chrVSrc, int chrFilterSize,
  764. const int32_t **alpSrc, uint16_t *dest, int dstW,
  765. int y, enum PixelFormat target)
  766. {
  767. int i;
  768. for (i = 0; i < (dstW >> 1); i++) {
  769. int j;
  770. int Y1 = 0;
  771. int Y2 = 0;
  772. int U = -128 << 23; // 19
  773. int V = -128 << 23;
  774. int R, G, B;
  775. for (j = 0; j < lumFilterSize; j++) {
  776. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  777. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  778. }
  779. for (j = 0; j < chrFilterSize; j++) {
  780. U += chrUSrc[j][i] * chrFilter[j];
  781. V += chrVSrc[j][i] * chrFilter[j];
  782. }
  783. // 8bit: 12+15=27; 16-bit: 12+19=31
  784. Y1 >>= 14; // 10
  785. Y2 >>= 14;
  786. U >>= 14;
  787. V >>= 14;
  788. // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
  789. Y1 -= c->yuv2rgb_y_offset;
  790. Y2 -= c->yuv2rgb_y_offset;
  791. Y1 *= c->yuv2rgb_y_coeff;
  792. Y2 *= c->yuv2rgb_y_coeff;
  793. Y1 += 1 << 13; // 21
  794. Y2 += 1 << 13;
  795. // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
  796. R = V * c->yuv2rgb_v2r_coeff;
  797. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  798. B = U * c->yuv2rgb_u2b_coeff;
  799. // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
  800. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  801. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  802. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  803. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  804. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  805. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  806. dest += 6;
  807. }
  808. }
  809. static av_always_inline void
  810. yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
  811. const int32_t *ubuf[2], const int32_t *vbuf[2],
  812. const int32_t *abuf[2], uint16_t *dest, int dstW,
  813. int yalpha, int uvalpha, int y,
  814. enum PixelFormat target)
  815. {
  816. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  817. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  818. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  819. int yalpha1 = 4095 - yalpha;
  820. int uvalpha1 = 4095 - uvalpha;
  821. int i;
  822. for (i = 0; i < (dstW >> 1); i++) {
  823. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
  824. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
  825. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
  826. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
  827. int R, G, B;
  828. Y1 -= c->yuv2rgb_y_offset;
  829. Y2 -= c->yuv2rgb_y_offset;
  830. Y1 *= c->yuv2rgb_y_coeff;
  831. Y2 *= c->yuv2rgb_y_coeff;
  832. Y1 += 1 << 13;
  833. Y2 += 1 << 13;
  834. R = V * c->yuv2rgb_v2r_coeff;
  835. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  836. B = U * c->yuv2rgb_u2b_coeff;
  837. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  838. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  839. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  840. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  841. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  842. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  843. dest += 6;
  844. }
  845. }
  846. static av_always_inline void
  847. yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
  848. const int32_t *ubuf[2], const int32_t *vbuf[2],
  849. const int32_t *abuf0, uint16_t *dest, int dstW,
  850. int uvalpha, int y, enum PixelFormat target)
  851. {
  852. const int32_t *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  853. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  854. int i;
  855. if (uvalpha < 2048) {
  856. for (i = 0; i < (dstW >> 1); i++) {
  857. int Y1 = (buf0[i * 2] ) >> 2;
  858. int Y2 = (buf0[i * 2 + 1]) >> 2;
  859. int U = (ubuf0[i] + (-128 << 11)) >> 2;
  860. int V = (vbuf0[i] + (-128 << 11)) >> 2;
  861. int R, G, B;
  862. Y1 -= c->yuv2rgb_y_offset;
  863. Y2 -= c->yuv2rgb_y_offset;
  864. Y1 *= c->yuv2rgb_y_coeff;
  865. Y2 *= c->yuv2rgb_y_coeff;
  866. Y1 += 1 << 13;
  867. Y2 += 1 << 13;
  868. R = V * c->yuv2rgb_v2r_coeff;
  869. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  870. B = U * c->yuv2rgb_u2b_coeff;
  871. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  872. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  873. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  874. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  875. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  876. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  877. dest += 6;
  878. }
  879. } else {
  880. for (i = 0; i < (dstW >> 1); i++) {
  881. int Y1 = (buf0[i * 2] ) >> 2;
  882. int Y2 = (buf0[i * 2 + 1]) >> 2;
  883. int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
  884. int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
  885. int R, G, B;
  886. Y1 -= c->yuv2rgb_y_offset;
  887. Y2 -= c->yuv2rgb_y_offset;
  888. Y1 *= c->yuv2rgb_y_coeff;
  889. Y2 *= c->yuv2rgb_y_coeff;
  890. Y1 += 1 << 13;
  891. Y2 += 1 << 13;
  892. R = V * c->yuv2rgb_v2r_coeff;
  893. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  894. B = U * c->yuv2rgb_u2b_coeff;
  895. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  896. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  897. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  898. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  899. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  900. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  901. dest += 6;
  902. }
  903. }
  904. }
  905. #undef output_pixel
  906. #undef r_b
  907. #undef b_r
  908. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, PIX_FMT_RGB48BE);
  909. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, PIX_FMT_RGB48LE);
  910. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, PIX_FMT_BGR48BE);
  911. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, PIX_FMT_BGR48LE);
  912. static av_always_inline void
  913. yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
  914. int U, int V, int A1, int A2,
  915. const void *_r, const void *_g, const void *_b, int y,
  916. enum PixelFormat target, int hasAlpha)
  917. {
  918. if (target == PIX_FMT_ARGB || target == PIX_FMT_RGBA ||
  919. target == PIX_FMT_ABGR || target == PIX_FMT_BGRA) {
  920. uint32_t *dest = (uint32_t *) _dest;
  921. const uint32_t *r = (const uint32_t *) _r;
  922. const uint32_t *g = (const uint32_t *) _g;
  923. const uint32_t *b = (const uint32_t *) _b;
  924. #if CONFIG_SMALL
  925. int sh = hasAlpha ? ((target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24) : 0;
  926. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
  927. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
  928. #else
  929. if (hasAlpha) {
  930. int sh = (target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24;
  931. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
  932. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
  933. } else {
  934. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
  935. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
  936. }
  937. #endif
  938. } else if (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) {
  939. uint8_t *dest = (uint8_t *) _dest;
  940. const uint8_t *r = (const uint8_t *) _r;
  941. const uint8_t *g = (const uint8_t *) _g;
  942. const uint8_t *b = (const uint8_t *) _b;
  943. #define r_b ((target == PIX_FMT_RGB24) ? r : b)
  944. #define b_r ((target == PIX_FMT_RGB24) ? b : r)
  945. dest[i * 6 + 0] = r_b[Y1];
  946. dest[i * 6 + 1] = g[Y1];
  947. dest[i * 6 + 2] = b_r[Y1];
  948. dest[i * 6 + 3] = r_b[Y2];
  949. dest[i * 6 + 4] = g[Y2];
  950. dest[i * 6 + 5] = b_r[Y2];
  951. #undef r_b
  952. #undef b_r
  953. } else if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565 ||
  954. target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555 ||
  955. target == PIX_FMT_RGB444 || target == PIX_FMT_BGR444) {
  956. uint16_t *dest = (uint16_t *) _dest;
  957. const uint16_t *r = (const uint16_t *) _r;
  958. const uint16_t *g = (const uint16_t *) _g;
  959. const uint16_t *b = (const uint16_t *) _b;
  960. int dr1, dg1, db1, dr2, dg2, db2;
  961. if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565) {
  962. dr1 = dither_2x2_8[ y & 1 ][0];
  963. dg1 = dither_2x2_4[ y & 1 ][0];
  964. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  965. dr2 = dither_2x2_8[ y & 1 ][1];
  966. dg2 = dither_2x2_4[ y & 1 ][1];
  967. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  968. } else if (target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555) {
  969. dr1 = dither_2x2_8[ y & 1 ][0];
  970. dg1 = dither_2x2_8[ y & 1 ][1];
  971. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  972. dr2 = dither_2x2_8[ y & 1 ][1];
  973. dg2 = dither_2x2_8[ y & 1 ][0];
  974. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  975. } else {
  976. dr1 = dither_4x4_16[ y & 3 ][0];
  977. dg1 = dither_4x4_16[ y & 3 ][1];
  978. db1 = dither_4x4_16[(y & 3) ^ 3][0];
  979. dr2 = dither_4x4_16[ y & 3 ][1];
  980. dg2 = dither_4x4_16[ y & 3 ][0];
  981. db2 = dither_4x4_16[(y & 3) ^ 3][1];
  982. }
  983. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  984. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  985. } else /* 8/4-bit */ {
  986. uint8_t *dest = (uint8_t *) _dest;
  987. const uint8_t *r = (const uint8_t *) _r;
  988. const uint8_t *g = (const uint8_t *) _g;
  989. const uint8_t *b = (const uint8_t *) _b;
  990. int dr1, dg1, db1, dr2, dg2, db2;
  991. if (target == PIX_FMT_RGB8 || target == PIX_FMT_BGR8) {
  992. const uint8_t * const d64 = dither_8x8_73[y & 7];
  993. const uint8_t * const d32 = dither_8x8_32[y & 7];
  994. dr1 = dg1 = d32[(i * 2 + 0) & 7];
  995. db1 = d64[(i * 2 + 0) & 7];
  996. dr2 = dg2 = d32[(i * 2 + 1) & 7];
  997. db2 = d64[(i * 2 + 1) & 7];
  998. } else {
  999. const uint8_t * const d64 = dither_8x8_73 [y & 7];
  1000. const uint8_t * const d128 = dither_8x8_220[y & 7];
  1001. dr1 = db1 = d128[(i * 2 + 0) & 7];
  1002. dg1 = d64[(i * 2 + 0) & 7];
  1003. dr2 = db2 = d128[(i * 2 + 1) & 7];
  1004. dg2 = d64[(i * 2 + 1) & 7];
  1005. }
  1006. if (target == PIX_FMT_RGB4 || target == PIX_FMT_BGR4) {
  1007. dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
  1008. ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
  1009. } else {
  1010. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1011. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1012. }
  1013. }
  1014. }
  1015. static av_always_inline void
  1016. yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1017. const int16_t **lumSrc, int lumFilterSize,
  1018. const int16_t *chrFilter, const int16_t **chrUSrc,
  1019. const int16_t **chrVSrc, int chrFilterSize,
  1020. const int16_t **alpSrc, uint8_t *dest, int dstW,
  1021. int y, enum PixelFormat target, int hasAlpha)
  1022. {
  1023. int i;
  1024. for (i = 0; i < (dstW >> 1); i++) {
  1025. int j;
  1026. int Y1 = 1 << 18;
  1027. int Y2 = 1 << 18;
  1028. int U = 1 << 18;
  1029. int V = 1 << 18;
  1030. int av_unused A1, A2;
  1031. const void *r, *g, *b;
  1032. for (j = 0; j < lumFilterSize; j++) {
  1033. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  1034. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  1035. }
  1036. for (j = 0; j < chrFilterSize; j++) {
  1037. U += chrUSrc[j][i] * chrFilter[j];
  1038. V += chrVSrc[j][i] * chrFilter[j];
  1039. }
  1040. Y1 >>= 19;
  1041. Y2 >>= 19;
  1042. U >>= 19;
  1043. V >>= 19;
  1044. if ((Y1 | Y2 | U | V) & 0x100) {
  1045. Y1 = av_clip_uint8(Y1);
  1046. Y2 = av_clip_uint8(Y2);
  1047. U = av_clip_uint8(U);
  1048. V = av_clip_uint8(V);
  1049. }
  1050. if (hasAlpha) {
  1051. A1 = 1 << 18;
  1052. A2 = 1 << 18;
  1053. for (j = 0; j < lumFilterSize; j++) {
  1054. A1 += alpSrc[j][i * 2 ] * lumFilter[j];
  1055. A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
  1056. }
  1057. A1 >>= 19;
  1058. A2 >>= 19;
  1059. if ((A1 | A2) & 0x100) {
  1060. A1 = av_clip_uint8(A1);
  1061. A2 = av_clip_uint8(A2);
  1062. }
  1063. }
  1064. /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/
  1065. r = c->table_rV[V];
  1066. g = (c->table_gU[U] + c->table_gV[V]);
  1067. b = c->table_bU[U];
  1068. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1069. r, g, b, y, target, hasAlpha);
  1070. }
  1071. }
  1072. static av_always_inline void
  1073. yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
  1074. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1075. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1076. int yalpha, int uvalpha, int y,
  1077. enum PixelFormat target, int hasAlpha)
  1078. {
  1079. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1080. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1081. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1082. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1083. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1084. int yalpha1 = 4095 - yalpha;
  1085. int uvalpha1 = 4095 - uvalpha;
  1086. int i;
  1087. for (i = 0; i < (dstW >> 1); i++) {
  1088. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  1089. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  1090. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  1091. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  1092. int A1, A2;
  1093. const void *r = c->table_rV[V],
  1094. *g = (c->table_gU[U] + c->table_gV[V]),
  1095. *b = c->table_bU[U];
  1096. if (hasAlpha) {
  1097. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
  1098. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
  1099. }
  1100. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1101. r, g, b, y, target, hasAlpha);
  1102. }
  1103. }
  1104. static av_always_inline void
  1105. yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
  1106. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1107. const int16_t *abuf0, uint8_t *dest, int dstW,
  1108. int uvalpha, int y, enum PixelFormat target,
  1109. int hasAlpha)
  1110. {
  1111. const int16_t *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1112. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  1113. int i;
  1114. if (uvalpha < 2048) {
  1115. for (i = 0; i < (dstW >> 1); i++) {
  1116. int Y1 = buf0[i * 2] >> 7;
  1117. int Y2 = buf0[i * 2 + 1] >> 7;
  1118. int U = ubuf1[i] >> 7;
  1119. int V = vbuf1[i] >> 7;
  1120. int A1, A2;
  1121. const void *r = c->table_rV[V],
  1122. *g = (c->table_gU[U] + c->table_gV[V]),
  1123. *b = c->table_bU[U];
  1124. if (hasAlpha) {
  1125. A1 = abuf0[i * 2 ] >> 7;
  1126. A2 = abuf0[i * 2 + 1] >> 7;
  1127. }
  1128. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1129. r, g, b, y, target, hasAlpha);
  1130. }
  1131. } else {
  1132. for (i = 0; i < (dstW >> 1); i++) {
  1133. int Y1 = buf0[i * 2] >> 7;
  1134. int Y2 = buf0[i * 2 + 1] >> 7;
  1135. int U = (ubuf0[i] + ubuf1[i]) >> 8;
  1136. int V = (vbuf0[i] + vbuf1[i]) >> 8;
  1137. int A1, A2;
  1138. const void *r = c->table_rV[V],
  1139. *g = (c->table_gU[U] + c->table_gV[V]),
  1140. *b = c->table_bU[U];
  1141. if (hasAlpha) {
  1142. A1 = abuf0[i * 2 ] >> 7;
  1143. A2 = abuf0[i * 2 + 1] >> 7;
  1144. }
  1145. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1146. r, g, b, y, target, hasAlpha);
  1147. }
  1148. }
  1149. }
  1150. #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1151. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  1152. const int16_t **lumSrc, int lumFilterSize, \
  1153. const int16_t *chrFilter, const int16_t **chrUSrc, \
  1154. const int16_t **chrVSrc, int chrFilterSize, \
  1155. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  1156. int y) \
  1157. { \
  1158. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  1159. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  1160. alpSrc, dest, dstW, y, fmt, hasAlpha); \
  1161. }
  1162. #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
  1163. YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1164. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  1165. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1166. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  1167. int yalpha, int uvalpha, int y) \
  1168. { \
  1169. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  1170. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
  1171. } \
  1172. \
  1173. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  1174. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1175. const int16_t *abuf0, uint8_t *dest, int dstW, \
  1176. int uvalpha, int y) \
  1177. { \
  1178. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  1179. dstW, uvalpha, y, fmt, hasAlpha); \
  1180. }
  1181. #if CONFIG_SMALL
  1182. YUV2RGBWRAPPER(yuv2rgb,, 32_1, PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1183. YUV2RGBWRAPPER(yuv2rgb,, 32, PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1184. #else
  1185. #if CONFIG_SWSCALE_ALPHA
  1186. YUV2RGBWRAPPER(yuv2rgb,, a32_1, PIX_FMT_RGB32_1, 1);
  1187. YUV2RGBWRAPPER(yuv2rgb,, a32, PIX_FMT_RGB32, 1);
  1188. #endif
  1189. YUV2RGBWRAPPER(yuv2rgb,, x32_1, PIX_FMT_RGB32_1, 0);
  1190. YUV2RGBWRAPPER(yuv2rgb,, x32, PIX_FMT_RGB32, 0);
  1191. #endif
  1192. YUV2RGBWRAPPER(yuv2, rgb, rgb24, PIX_FMT_RGB24, 0);
  1193. YUV2RGBWRAPPER(yuv2, rgb, bgr24, PIX_FMT_BGR24, 0);
  1194. YUV2RGBWRAPPER(yuv2rgb,, 16, PIX_FMT_RGB565, 0);
  1195. YUV2RGBWRAPPER(yuv2rgb,, 15, PIX_FMT_RGB555, 0);
  1196. YUV2RGBWRAPPER(yuv2rgb,, 12, PIX_FMT_RGB444, 0);
  1197. YUV2RGBWRAPPER(yuv2rgb,, 8, PIX_FMT_RGB8, 0);
  1198. YUV2RGBWRAPPER(yuv2rgb,, 4, PIX_FMT_RGB4, 0);
  1199. YUV2RGBWRAPPER(yuv2rgb,, 4b, PIX_FMT_RGB4_BYTE, 0);
  1200. static av_always_inline void
  1201. yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1202. const int16_t **lumSrc, int lumFilterSize,
  1203. const int16_t *chrFilter, const int16_t **chrUSrc,
  1204. const int16_t **chrVSrc, int chrFilterSize,
  1205. const int16_t **alpSrc, uint8_t *dest,
  1206. int dstW, int y, enum PixelFormat target, int hasAlpha)
  1207. {
  1208. int i;
  1209. int step = (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) ? 3 : 4;
  1210. for (i = 0; i < dstW; i++) {
  1211. int j;
  1212. int Y = 1<<9;
  1213. int U = (1<<9)-(128 << 19);
  1214. int V = (1<<9)-(128 << 19);
  1215. int av_unused A;
  1216. int R, G, B;
  1217. for (j = 0; j < lumFilterSize; j++) {
  1218. Y += lumSrc[j][i] * lumFilter[j];
  1219. }
  1220. for (j = 0; j < chrFilterSize; j++) {
  1221. U += chrUSrc[j][i] * chrFilter[j];
  1222. V += chrVSrc[j][i] * chrFilter[j];
  1223. }
  1224. Y >>= 10;
  1225. U >>= 10;
  1226. V >>= 10;
  1227. if (hasAlpha) {
  1228. A = 1 << 18;
  1229. for (j = 0; j < lumFilterSize; j++) {
  1230. A += alpSrc[j][i] * lumFilter[j];
  1231. }
  1232. A >>= 19;
  1233. if (A & 0x100)
  1234. A = av_clip_uint8(A);
  1235. }
  1236. Y -= c->yuv2rgb_y_offset;
  1237. Y *= c->yuv2rgb_y_coeff;
  1238. Y += 1 << 21;
  1239. R = Y + V*c->yuv2rgb_v2r_coeff;
  1240. G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
  1241. B = Y + U*c->yuv2rgb_u2b_coeff;
  1242. if ((R | G | B) & 0xC0000000) {
  1243. R = av_clip_uintp2(R, 30);
  1244. G = av_clip_uintp2(G, 30);
  1245. B = av_clip_uintp2(B, 30);
  1246. }
  1247. switch(target) {
  1248. case PIX_FMT_ARGB:
  1249. dest[0] = hasAlpha ? A : 255;
  1250. dest[1] = R >> 22;
  1251. dest[2] = G >> 22;
  1252. dest[3] = B >> 22;
  1253. break;
  1254. case PIX_FMT_RGB24:
  1255. dest[0] = R >> 22;
  1256. dest[1] = G >> 22;
  1257. dest[2] = B >> 22;
  1258. break;
  1259. case PIX_FMT_RGBA:
  1260. dest[0] = R >> 22;
  1261. dest[1] = G >> 22;
  1262. dest[2] = B >> 22;
  1263. dest[3] = hasAlpha ? A : 255;
  1264. break;
  1265. case PIX_FMT_ABGR:
  1266. dest[0] = hasAlpha ? A : 255;
  1267. dest[1] = B >> 22;
  1268. dest[2] = G >> 22;
  1269. dest[3] = R >> 22;
  1270. break;
  1271. case PIX_FMT_BGR24:
  1272. dest[0] = B >> 22;
  1273. dest[1] = G >> 22;
  1274. dest[2] = R >> 22;
  1275. break;
  1276. case PIX_FMT_BGRA:
  1277. dest[0] = B >> 22;
  1278. dest[1] = G >> 22;
  1279. dest[2] = R >> 22;
  1280. dest[3] = hasAlpha ? A : 255;
  1281. break;
  1282. }
  1283. dest += step;
  1284. }
  1285. }
  1286. #if CONFIG_SMALL
  1287. YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1288. YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1289. YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1290. YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1291. #else
  1292. #if CONFIG_SWSCALE_ALPHA
  1293. YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, 1);
  1294. YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, 1);
  1295. YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, 1);
  1296. YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, 1);
  1297. #endif
  1298. YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, PIX_FMT_BGRA, 0);
  1299. YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, PIX_FMT_ABGR, 0);
  1300. YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, PIX_FMT_RGBA, 0);
  1301. YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, PIX_FMT_ARGB, 0);
  1302. #endif
  1303. YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full, PIX_FMT_BGR24, 0);
  1304. YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full, PIX_FMT_RGB24, 0);
  1305. static av_always_inline void fillPlane(uint8_t* plane, int stride,
  1306. int width, int height,
  1307. int y, uint8_t val)
  1308. {
  1309. int i;
  1310. uint8_t *ptr = plane + stride*y;
  1311. for (i=0; i<height; i++) {
  1312. memset(ptr, val, width);
  1313. ptr += stride;
  1314. }
  1315. }
  1316. #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
  1317. #define r ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? b_r : r_b)
  1318. #define b ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? r_b : b_r)
  1319. static av_always_inline void
  1320. rgb48ToY_c_template(uint16_t *dst, const uint16_t *src, int width,
  1321. enum PixelFormat origin)
  1322. {
  1323. int i;
  1324. for (i = 0; i < width; i++) {
  1325. unsigned int r_b = input_pixel(&src[i*3+0]);
  1326. unsigned int g = input_pixel(&src[i*3+1]);
  1327. unsigned int b_r = input_pixel(&src[i*3+2]);
  1328. dst[i] = (RY*r + GY*g + BY*b + (0x2001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1329. }
  1330. }
  1331. static av_always_inline void
  1332. rgb48ToUV_c_template(uint16_t *dstU, uint16_t *dstV,
  1333. const uint16_t *src1, const uint16_t *src2,
  1334. int width, enum PixelFormat origin)
  1335. {
  1336. int i;
  1337. assert(src1==src2);
  1338. for (i = 0; i < width; i++) {
  1339. int r_b = input_pixel(&src1[i*3+0]);
  1340. int g = input_pixel(&src1[i*3+1]);
  1341. int b_r = input_pixel(&src1[i*3+2]);
  1342. dstU[i] = (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1343. dstV[i] = (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1344. }
  1345. }
  1346. static av_always_inline void
  1347. rgb48ToUV_half_c_template(uint16_t *dstU, uint16_t *dstV,
  1348. const uint16_t *src1, const uint16_t *src2,
  1349. int width, enum PixelFormat origin)
  1350. {
  1351. int i;
  1352. assert(src1==src2);
  1353. for (i = 0; i < width; i++) {
  1354. int r_b = (input_pixel(&src1[6 * i + 0]) + input_pixel(&src1[6 * i + 3]) + 1) >> 1;
  1355. int g = (input_pixel(&src1[6 * i + 1]) + input_pixel(&src1[6 * i + 4]) + 1) >> 1;
  1356. int b_r = (input_pixel(&src1[6 * i + 2]) + input_pixel(&src1[6 * i + 5]) + 1) >> 1;
  1357. dstU[i]= (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1358. dstV[i]= (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1359. }
  1360. }
  1361. #undef r
  1362. #undef b
  1363. #undef input_pixel
  1364. #define rgb48funcs(pattern, BE_LE, origin) \
  1365. static void pattern ## 48 ## BE_LE ## ToY_c(uint8_t *_dst, const uint8_t *_src, const uint8_t *unused0, const uint8_t *unused1,\
  1366. int width, uint32_t *unused) \
  1367. { \
  1368. const uint16_t *src = (const uint16_t *) _src; \
  1369. uint16_t *dst = (uint16_t *) _dst; \
  1370. rgb48ToY_c_template(dst, src, width, origin); \
  1371. } \
  1372. \
  1373. static void pattern ## 48 ## BE_LE ## ToUV_c(uint8_t *_dstU, uint8_t *_dstV, \
  1374. const uint8_t *unused0, const uint8_t *_src1, const uint8_t *_src2, \
  1375. int width, uint32_t *unused) \
  1376. { \
  1377. const uint16_t *src1 = (const uint16_t *) _src1, \
  1378. *src2 = (const uint16_t *) _src2; \
  1379. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
  1380. rgb48ToUV_c_template(dstU, dstV, src1, src2, width, origin); \
  1381. } \
  1382. \
  1383. static void pattern ## 48 ## BE_LE ## ToUV_half_c(uint8_t *_dstU, uint8_t *_dstV, \
  1384. const uint8_t *unused0, const uint8_t *_src1, const uint8_t *_src2, \
  1385. int width, uint32_t *unused) \
  1386. { \
  1387. const uint16_t *src1 = (const uint16_t *) _src1, \
  1388. *src2 = (const uint16_t *) _src2; \
  1389. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
  1390. rgb48ToUV_half_c_template(dstU, dstV, src1, src2, width, origin); \
  1391. }
  1392. rgb48funcs(rgb, LE, PIX_FMT_RGB48LE);
  1393. rgb48funcs(rgb, BE, PIX_FMT_RGB48BE);
  1394. rgb48funcs(bgr, LE, PIX_FMT_BGR48LE);
  1395. rgb48funcs(bgr, BE, PIX_FMT_BGR48BE);
  1396. #define input_pixel(i) ((origin == PIX_FMT_RGBA || origin == PIX_FMT_BGRA || \
  1397. origin == PIX_FMT_ARGB || origin == PIX_FMT_ABGR) ? AV_RN32A(&src[(i)*4]) : \
  1398. (isBE(origin) ? AV_RB16(&src[(i)*2]) : AV_RL16(&src[(i)*2])))
  1399. static av_always_inline void
  1400. rgb16_32ToY_c_template(int16_t *dst, const uint8_t *src,
  1401. int width, enum PixelFormat origin,
  1402. int shr, int shg, int shb, int shp,
  1403. int maskr, int maskg, int maskb,
  1404. int rsh, int gsh, int bsh, int S)
  1405. {
  1406. const int ry = RY << rsh, gy = GY << gsh, by = BY << bsh,
  1407. rnd = (32<<((S)-1)) + (1<<(S-7));
  1408. int i;
  1409. for (i = 0; i < width; i++) {
  1410. int px = input_pixel(i) >> shp;
  1411. int b = (px & maskb) >> shb;
  1412. int g = (px & maskg) >> shg;
  1413. int r = (px & maskr) >> shr;
  1414. dst[i] = (ry * r + gy * g + by * b + rnd) >> ((S)-6);
  1415. }
  1416. }
  1417. static av_always_inline void
  1418. rgb16_32ToUV_c_template(int16_t *dstU, int16_t *dstV,
  1419. const uint8_t *src, int width,
  1420. enum PixelFormat origin,
  1421. int shr, int shg, int shb, int shp,
  1422. int maskr, int maskg, int maskb,
  1423. int rsh, int gsh, int bsh, int S)
  1424. {
  1425. const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
  1426. rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
  1427. rnd = (256<<((S)-1)) + (1<<(S-7));
  1428. int i;
  1429. for (i = 0; i < width; i++) {
  1430. int px = input_pixel(i) >> shp;
  1431. int b = (px & maskb) >> shb;
  1432. int g = (px & maskg) >> shg;
  1433. int r = (px & maskr) >> shr;
  1434. dstU[i] = (ru * r + gu * g + bu * b + rnd) >> ((S)-6);
  1435. dstV[i] = (rv * r + gv * g + bv * b + rnd) >> ((S)-6);
  1436. }
  1437. }
  1438. static av_always_inline void
  1439. rgb16_32ToUV_half_c_template(int16_t *dstU, int16_t *dstV,
  1440. const uint8_t *src, int width,
  1441. enum PixelFormat origin,
  1442. int shr, int shg, int shb, int shp,
  1443. int maskr, int maskg, int maskb,
  1444. int rsh, int gsh, int bsh, int S)
  1445. {
  1446. const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
  1447. rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
  1448. rnd = (256U<<(S)) + (1<<(S-6)), maskgx = ~(maskr | maskb);
  1449. int i;
  1450. maskr |= maskr << 1; maskb |= maskb << 1; maskg |= maskg << 1;
  1451. for (i = 0; i < width; i++) {
  1452. int px0 = input_pixel(2 * i + 0) >> shp;
  1453. int px1 = input_pixel(2 * i + 1) >> shp;
  1454. int b, r, g = (px0 & maskgx) + (px1 & maskgx);
  1455. int rb = px0 + px1 - g;
  1456. b = (rb & maskb) >> shb;
  1457. if (shp || origin == PIX_FMT_BGR565LE || origin == PIX_FMT_BGR565BE ||
  1458. origin == PIX_FMT_RGB565LE || origin == PIX_FMT_RGB565BE) {
  1459. g >>= shg;
  1460. } else {
  1461. g = (g & maskg) >> shg;
  1462. }
  1463. r = (rb & maskr) >> shr;
  1464. dstU[i] = (ru * r + gu * g + bu * b + (unsigned)rnd) >> ((S)-6+1);
  1465. dstV[i] = (rv * r + gv * g + bv * b + (unsigned)rnd) >> ((S)-6+1);
  1466. }
  1467. }
  1468. #undef input_pixel
  1469. #define rgb16_32_wrapper(fmt, name, shr, shg, shb, shp, maskr, \
  1470. maskg, maskb, rsh, gsh, bsh, S) \
  1471. static void name ## ToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, \
  1472. int width, uint32_t *unused) \
  1473. { \
  1474. rgb16_32ToY_c_template((int16_t*)dst, src, width, fmt, \
  1475. shr, shg, shb, shp, \
  1476. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1477. } \
  1478. \
  1479. static void name ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
  1480. const uint8_t *unused0, const uint8_t *src, const uint8_t *dummy, \
  1481. int width, uint32_t *unused) \
  1482. { \
  1483. rgb16_32ToUV_c_template((int16_t*)dstU, (int16_t*)dstV, src, width, fmt, \
  1484. shr, shg, shb, shp, \
  1485. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1486. } \
  1487. \
  1488. static void name ## ToUV_half_c(uint8_t *dstU, uint8_t *dstV, \
  1489. const uint8_t *unused0, const uint8_t *src, const uint8_t *dummy, \
  1490. int width, uint32_t *unused) \
  1491. { \
  1492. rgb16_32ToUV_half_c_template((int16_t*)dstU, (int16_t*)dstV, src, width, fmt, \
  1493. shr, shg, shb, shp, \
  1494. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1495. }
  1496. rgb16_32_wrapper(PIX_FMT_BGR32, bgr32, 16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8);
  1497. rgb16_32_wrapper(PIX_FMT_BGR32_1, bgr321, 16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8);
  1498. rgb16_32_wrapper(PIX_FMT_RGB32, rgb32, 0, 0, 16, 0, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8);
  1499. rgb16_32_wrapper(PIX_FMT_RGB32_1, rgb321, 0, 0, 16, 8, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8);
  1500. rgb16_32_wrapper(PIX_FMT_BGR565LE, bgr16le, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8);
  1501. rgb16_32_wrapper(PIX_FMT_BGR555LE, bgr15le, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7);
  1502. rgb16_32_wrapper(PIX_FMT_RGB565LE, rgb16le, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8);
  1503. rgb16_32_wrapper(PIX_FMT_RGB555LE, rgb15le, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7);
  1504. rgb16_32_wrapper(PIX_FMT_BGR565BE, bgr16be, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8);
  1505. rgb16_32_wrapper(PIX_FMT_BGR555BE, bgr15be, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7);
  1506. rgb16_32_wrapper(PIX_FMT_RGB565BE, rgb16be, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8);
  1507. rgb16_32_wrapper(PIX_FMT_RGB555BE, rgb15be, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7);
  1508. static void gbr24pToY_c(uint16_t *dst, const uint8_t *gsrc, const uint8_t *bsrc, const uint8_t *rsrc,
  1509. int width, uint32_t *unused)
  1510. {
  1511. int i;
  1512. for (i = 0; i < width; i++) {
  1513. unsigned int g = gsrc[i];
  1514. unsigned int b = bsrc[i];
  1515. unsigned int r = rsrc[i];
  1516. dst[i] = (RY*r + GY*g + BY*b + (0x801<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
  1517. }
  1518. }
  1519. static void gbr24pToUV_c(uint16_t *dstU, uint16_t *dstV,
  1520. const uint8_t *gsrc, const uint8_t *bsrc, const uint8_t *rsrc,
  1521. int width, enum PixelFormat origin)
  1522. {
  1523. int i;
  1524. for (i = 0; i < width; i++) {
  1525. unsigned int g = gsrc[i];
  1526. unsigned int b = bsrc[i];
  1527. unsigned int r = rsrc[i];
  1528. dstU[i] = (RU*r + GU*g + BU*b + (0x4001<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
  1529. dstV[i] = (RV*r + GV*g + BV*b + (0x4001<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
  1530. }
  1531. }
  1532. static void gbr24pToUV_half_c(uint16_t *dstU, uint16_t *dstV,
  1533. const uint8_t *gsrc, const uint8_t *bsrc, const uint8_t *rsrc,
  1534. int width, enum PixelFormat origin)
  1535. {
  1536. int i;
  1537. for (i = 0; i < width; i++) {
  1538. unsigned int g = gsrc[2*i] + gsrc[2*i+1];
  1539. unsigned int b = bsrc[2*i] + bsrc[2*i+1];
  1540. unsigned int r = rsrc[2*i] + rsrc[2*i+1];
  1541. dstU[i] = (RU*r + GU*g + BU*b + (0x4001<<(RGB2YUV_SHIFT-6))) >> (RGB2YUV_SHIFT-6+1);
  1542. dstV[i] = (RV*r + GV*g + BV*b + (0x4001<<(RGB2YUV_SHIFT-6))) >> (RGB2YUV_SHIFT-6+1);
  1543. }
  1544. }
  1545. static void abgrToA_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
  1546. {
  1547. int i;
  1548. for (i=0; i<width; i++) {
  1549. dst[i]= src[4*i]<<6;
  1550. }
  1551. }
  1552. static void rgbaToA_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
  1553. {
  1554. int i;
  1555. for (i=0; i<width; i++) {
  1556. dst[i]= src[4*i+3]<<6;
  1557. }
  1558. }
  1559. static void palToA_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *pal)
  1560. {
  1561. int i;
  1562. for (i=0; i<width; i++) {
  1563. int d= src[i];
  1564. dst[i]= (pal[d] >> 24)<<6;
  1565. }
  1566. }
  1567. static void palToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, long width, uint32_t *pal)
  1568. {
  1569. int i;
  1570. for (i=0; i<width; i++) {
  1571. int d= src[i];
  1572. dst[i]= (pal[d] & 0xFF)<<6;
  1573. }
  1574. }
  1575. static void palToUV_c(uint16_t *dstU, int16_t *dstV,
  1576. const uint8_t *unused0, const uint8_t *src1, const uint8_t *src2,
  1577. int width, uint32_t *pal)
  1578. {
  1579. int i;
  1580. assert(src1 == src2);
  1581. for (i=0; i<width; i++) {
  1582. int p= pal[src1[i]];
  1583. dstU[i]= (uint8_t)(p>> 8)<<6;
  1584. dstV[i]= (uint8_t)(p>>16)<<6;
  1585. }
  1586. }
  1587. static void monowhite2Y_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
  1588. {
  1589. int i, j;
  1590. for (i=0; i<width/8; i++) {
  1591. int d= ~src[i];
  1592. for(j=0; j<8; j++)
  1593. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1594. }
  1595. if(width&7){
  1596. int d= ~src[i];
  1597. for(j=0; j<(width&7); j++)
  1598. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1599. }
  1600. }
  1601. static void monoblack2Y_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
  1602. {
  1603. int i, j;
  1604. for (i=0; i<width/8; i++) {
  1605. int d= src[i];
  1606. for(j=0; j<8; j++)
  1607. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1608. }
  1609. if(width&7){
  1610. int d= src[i];
  1611. for(j=0; j<(width&7); j++)
  1612. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1613. }
  1614. }
  1615. //FIXME yuy2* can read up to 7 samples too much
  1616. static void yuy2ToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
  1617. uint32_t *unused)
  1618. {
  1619. int i;
  1620. for (i=0; i<width; i++)
  1621. dst[i]= src[2*i];
  1622. }
  1623. static void yuy2ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *unused0, const uint8_t *src1,
  1624. const uint8_t *src2, int width, uint32_t *unused)
  1625. {
  1626. int i;
  1627. for (i=0; i<width; i++) {
  1628. dstU[i]= src1[4*i + 1];
  1629. dstV[i]= src1[4*i + 3];
  1630. }
  1631. assert(src1 == src2);
  1632. }
  1633. static void bswap16Y_c(uint8_t *_dst, const uint8_t *_src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
  1634. {
  1635. int i;
  1636. const uint16_t *src = (const uint16_t *) _src;
  1637. uint16_t *dst = (uint16_t *) _dst;
  1638. for (i=0; i<width; i++) {
  1639. dst[i] = av_bswap16(src[i]);
  1640. }
  1641. }
  1642. static void bswap16UV_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *unused0, const uint8_t *_src1,
  1643. const uint8_t *_src2, int width, uint32_t *unused)
  1644. {
  1645. int i;
  1646. const uint16_t *src1 = (const uint16_t *) _src1,
  1647. *src2 = (const uint16_t *) _src2;
  1648. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV;
  1649. for (i=0; i<width; i++) {
  1650. dstU[i] = av_bswap16(src1[i]);
  1651. dstV[i] = av_bswap16(src2[i]);
  1652. }
  1653. }
  1654. /* This is almost identical to the previous, end exists only because
  1655. * yuy2ToY/UV)(dst, src+1, ...) would have 100% unaligned accesses. */
  1656. static void uyvyToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
  1657. uint32_t *unused)
  1658. {
  1659. int i;
  1660. for (i=0; i<width; i++)
  1661. dst[i]= src[2*i+1];
  1662. }
  1663. static void uyvyToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *unused0, const uint8_t *src1,
  1664. const uint8_t *src2, int width, uint32_t *unused)
  1665. {
  1666. int i;
  1667. for (i=0; i<width; i++) {
  1668. dstU[i]= src1[4*i + 0];
  1669. dstV[i]= src1[4*i + 2];
  1670. }
  1671. assert(src1 == src2);
  1672. }
  1673. static av_always_inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
  1674. const uint8_t *src, int width)
  1675. {
  1676. int i;
  1677. for (i = 0; i < width; i++) {
  1678. dst1[i] = src[2*i+0];
  1679. dst2[i] = src[2*i+1];
  1680. }
  1681. }
  1682. static void nv12ToUV_c(uint8_t *dstU, uint8_t *dstV,
  1683. const uint8_t *unused0, const uint8_t *src1, const uint8_t *src2,
  1684. int width, uint32_t *unused)
  1685. {
  1686. nvXXtoUV_c(dstU, dstV, src1, width);
  1687. }
  1688. static void nv21ToUV_c(uint8_t *dstU, uint8_t *dstV,
  1689. const uint8_t *unused0, const uint8_t *src1, const uint8_t *src2,
  1690. int width, uint32_t *unused)
  1691. {
  1692. nvXXtoUV_c(dstV, dstU, src1, width);
  1693. }
  1694. #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
  1695. static void bgr24ToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2,
  1696. int width, uint32_t *unused)
  1697. {
  1698. int i;
  1699. for (i=0; i<width; i++) {
  1700. int b= src[i*3+0];
  1701. int g= src[i*3+1];
  1702. int r= src[i*3+2];
  1703. dst[i]= ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
  1704. }
  1705. }
  1706. static void bgr24ToUV_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
  1707. const uint8_t *src2, int width, uint32_t *unused)
  1708. {
  1709. int i;
  1710. for (i=0; i<width; i++) {
  1711. int b= src1[3*i + 0];
  1712. int g= src1[3*i + 1];
  1713. int r= src1[3*i + 2];
  1714. dstU[i]= (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1715. dstV[i]= (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1716. }
  1717. assert(src1 == src2);
  1718. }
  1719. static void bgr24ToUV_half_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
  1720. const uint8_t *src2, int width, uint32_t *unused)
  1721. {
  1722. int i;
  1723. for (i=0; i<width; i++) {
  1724. int b= src1[6*i + 0] + src1[6*i + 3];
  1725. int g= src1[6*i + 1] + src1[6*i + 4];
  1726. int r= src1[6*i + 2] + src1[6*i + 5];
  1727. dstU[i]= (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1728. dstV[i]= (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1729. }
  1730. assert(src1 == src2);
  1731. }
  1732. static void rgb24ToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
  1733. uint32_t *unused)
  1734. {
  1735. int i;
  1736. for (i=0; i<width; i++) {
  1737. int r= src[i*3+0];
  1738. int g= src[i*3+1];
  1739. int b= src[i*3+2];
  1740. dst[i]= ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
  1741. }
  1742. }
  1743. static void rgb24ToUV_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
  1744. const uint8_t *src2, int width, uint32_t *unused)
  1745. {
  1746. int i;
  1747. assert(src1==src2);
  1748. for (i=0; i<width; i++) {
  1749. int r= src1[3*i + 0];
  1750. int g= src1[3*i + 1];
  1751. int b= src1[3*i + 2];
  1752. dstU[i]= (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1753. dstV[i]= (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1754. }
  1755. }
  1756. static void rgb24ToUV_half_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
  1757. const uint8_t *src2, int width, uint32_t *unused)
  1758. {
  1759. int i;
  1760. assert(src1==src2);
  1761. for (i=0; i<width; i++) {
  1762. int r= src1[6*i + 0] + src1[6*i + 3];
  1763. int g= src1[6*i + 1] + src1[6*i + 4];
  1764. int b= src1[6*i + 2] + src1[6*i + 5];
  1765. dstU[i]= (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1766. dstV[i]= (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1767. }
  1768. }
  1769. static void hScale16To19_c(SwsContext *c, int16_t *_dst, int dstW, const uint8_t *_src,
  1770. const int16_t *filter,
  1771. const int16_t *filterPos, int filterSize)
  1772. {
  1773. int i;
  1774. int32_t *dst = (int32_t *) _dst;
  1775. const uint16_t *src = (const uint16_t *) _src;
  1776. int bits = av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  1777. int sh = bits - 4;
  1778. if((isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8) && av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1<15)
  1779. sh= 9;
  1780. for (i = 0; i < dstW; i++) {
  1781. int j;
  1782. int srcPos = filterPos[i];
  1783. int val = 0;
  1784. for (j = 0; j < filterSize; j++) {
  1785. val += src[srcPos + j] * filter[filterSize * i + j];
  1786. }
  1787. // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
  1788. dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
  1789. }
  1790. }
  1791. static void hScale16To15_c(SwsContext *c, int16_t *dst, int dstW, const uint8_t *_src,
  1792. const int16_t *filter,
  1793. const int16_t *filterPos, int filterSize)
  1794. {
  1795. int i;
  1796. const uint16_t *src = (const uint16_t *) _src;
  1797. int sh = av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  1798. if(sh<15)
  1799. sh= isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8 ? 13 : av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  1800. for (i = 0; i < dstW; i++) {
  1801. int j;
  1802. int srcPos = filterPos[i];
  1803. int val = 0;
  1804. for (j = 0; j < filterSize; j++) {
  1805. val += src[srcPos + j] * filter[filterSize * i + j];
  1806. }
  1807. // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit
  1808. dst[i] = FFMIN(val >> sh, (1 << 15) - 1);
  1809. }
  1810. }
  1811. // bilinear / bicubic scaling
  1812. static void hScale8To15_c(SwsContext *c, int16_t *dst, int dstW, const uint8_t *src,
  1813. const int16_t *filter, const int16_t *filterPos,
  1814. int filterSize)
  1815. {
  1816. int i;
  1817. for (i=0; i<dstW; i++) {
  1818. int j;
  1819. int srcPos= filterPos[i];
  1820. int val=0;
  1821. for (j=0; j<filterSize; j++) {
  1822. val += ((int)src[srcPos + j])*filter[filterSize*i + j];
  1823. }
  1824. //filter += hFilterSize;
  1825. dst[i] = FFMIN(val>>7, (1<<15)-1); // the cubic equation does overflow ...
  1826. //dst[i] = val>>7;
  1827. }
  1828. }
  1829. static void hScale8To19_c(SwsContext *c, int16_t *_dst, int dstW, const uint8_t *src,
  1830. const int16_t *filter, const int16_t *filterPos,
  1831. int filterSize)
  1832. {
  1833. int i;
  1834. int32_t *dst = (int32_t *) _dst;
  1835. for (i=0; i<dstW; i++) {
  1836. int j;
  1837. int srcPos= filterPos[i];
  1838. int val=0;
  1839. for (j=0; j<filterSize; j++) {
  1840. val += ((int)src[srcPos + j])*filter[filterSize*i + j];
  1841. }
  1842. //filter += hFilterSize;
  1843. dst[i] = FFMIN(val>>3, (1<<19)-1); // the cubic equation does overflow ...
  1844. //dst[i] = val>>7;
  1845. }
  1846. }
  1847. //FIXME all pal and rgb srcFormats could do this convertion as well
  1848. //FIXME all scalers more complex than bilinear could do half of this transform
  1849. static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  1850. {
  1851. int i;
  1852. for (i = 0; i < width; i++) {
  1853. dstU[i] = (FFMIN(dstU[i],30775)*4663 - 9289992)>>12; //-264
  1854. dstV[i] = (FFMIN(dstV[i],30775)*4663 - 9289992)>>12; //-264
  1855. }
  1856. }
  1857. static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  1858. {
  1859. int i;
  1860. for (i = 0; i < width; i++) {
  1861. dstU[i] = (dstU[i]*1799 + 4081085)>>11; //1469
  1862. dstV[i] = (dstV[i]*1799 + 4081085)>>11; //1469
  1863. }
  1864. }
  1865. static void lumRangeToJpeg_c(int16_t *dst, int width)
  1866. {
  1867. int i;
  1868. for (i = 0; i < width; i++)
  1869. dst[i] = (FFMIN(dst[i],30189)*19077 - 39057361)>>14;
  1870. }
  1871. static void lumRangeFromJpeg_c(int16_t *dst, int width)
  1872. {
  1873. int i;
  1874. for (i = 0; i < width; i++)
  1875. dst[i] = (dst[i]*14071 + 33561947)>>14;
  1876. }
  1877. static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  1878. {
  1879. int i;
  1880. int32_t *dstU = (int32_t *) _dstU;
  1881. int32_t *dstV = (int32_t *) _dstV;
  1882. for (i = 0; i < width; i++) {
  1883. dstU[i] = (FFMIN(dstU[i],30775<<4)*4663 - (9289992<<4))>>12; //-264
  1884. dstV[i] = (FFMIN(dstV[i],30775<<4)*4663 - (9289992<<4))>>12; //-264
  1885. }
  1886. }
  1887. static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  1888. {
  1889. int i;
  1890. int32_t *dstU = (int32_t *) _dstU;
  1891. int32_t *dstV = (int32_t *) _dstV;
  1892. for (i = 0; i < width; i++) {
  1893. dstU[i] = (dstU[i]*1799 + (4081085<<4))>>11; //1469
  1894. dstV[i] = (dstV[i]*1799 + (4081085<<4))>>11; //1469
  1895. }
  1896. }
  1897. static void lumRangeToJpeg16_c(int16_t *_dst, int width)
  1898. {
  1899. int i;
  1900. int32_t *dst = (int32_t *) _dst;
  1901. for (i = 0; i < width; i++)
  1902. dst[i] = (FFMIN(dst[i],30189<<4)*4769 - (39057361<<2))>>12;
  1903. }
  1904. static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
  1905. {
  1906. int i;
  1907. int32_t *dst = (int32_t *) _dst;
  1908. for (i = 0; i < width; i++)
  1909. dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
  1910. }
  1911. static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  1912. const uint8_t *src, int srcW, int xInc)
  1913. {
  1914. int i;
  1915. unsigned int xpos=0;
  1916. for (i=0;i<dstWidth;i++) {
  1917. register unsigned int xx=xpos>>16;
  1918. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1919. dst[i]= (src[xx]<<7) + (src[xx+1] - src[xx])*xalpha;
  1920. xpos+=xInc;
  1921. }
  1922. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
  1923. dst[i] = src[srcW-1]*128;
  1924. }
  1925. // *** horizontal scale Y line to temp buffer
  1926. static av_always_inline void hyscale(SwsContext *c, int16_t *dst, int dstWidth,
  1927. const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  1928. int srcW, int xInc,
  1929. const int16_t *hLumFilter,
  1930. const int16_t *hLumFilterPos, int hLumFilterSize,
  1931. uint8_t *formatConvBuffer,
  1932. uint32_t *pal, int isAlpha)
  1933. {
  1934. void (*toYV12)(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *, int, uint32_t *) = isAlpha ? c->alpToYV12 : c->lumToYV12;
  1935. void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
  1936. if (toYV12) {
  1937. toYV12(formatConvBuffer, src, src2, src3, srcW, pal);
  1938. src= formatConvBuffer;
  1939. }
  1940. if (!c->hyscale_fast) {
  1941. c->hyScale(c, dst, dstWidth, src, hLumFilter, hLumFilterPos, hLumFilterSize);
  1942. } else { // fast bilinear upscale / crap downscale
  1943. c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
  1944. }
  1945. if (convertRange)
  1946. convertRange(dst, dstWidth);
  1947. }
  1948. static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  1949. int dstWidth, const uint8_t *src1,
  1950. const uint8_t *src2, int srcW, int xInc)
  1951. {
  1952. int i;
  1953. unsigned int xpos=0;
  1954. for (i=0;i<dstWidth;i++) {
  1955. register unsigned int xx=xpos>>16;
  1956. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1957. dst1[i]=(src1[xx]*(xalpha^127)+src1[xx+1]*xalpha);
  1958. dst2[i]=(src2[xx]*(xalpha^127)+src2[xx+1]*xalpha);
  1959. xpos+=xInc;
  1960. }
  1961. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
  1962. dst1[i] = src1[srcW-1]*128;
  1963. dst2[i] = src2[srcW-1]*128;
  1964. }
  1965. }
  1966. static av_always_inline void hcscale(SwsContext *c, int16_t *dst1, int16_t *dst2, int dstWidth,
  1967. const uint8_t *src0, const uint8_t *src1, const uint8_t *src2,
  1968. int srcW, int xInc, const int16_t *hChrFilter,
  1969. const int16_t *hChrFilterPos, int hChrFilterSize,
  1970. uint8_t *formatConvBuffer, uint32_t *pal)
  1971. {
  1972. if (c->chrToYV12) {
  1973. uint8_t *buf2 = formatConvBuffer + FFALIGN(srcW*2+78, 16);
  1974. c->chrToYV12(formatConvBuffer, buf2, src0, src1, src2, srcW, pal);
  1975. src1= formatConvBuffer;
  1976. src2= buf2;
  1977. }
  1978. if (!c->hcscale_fast) {
  1979. c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
  1980. c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
  1981. } else { // fast bilinear upscale / crap downscale
  1982. c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
  1983. }
  1984. if (c->chrConvertRange)
  1985. c->chrConvertRange(dst1, dst2, dstWidth);
  1986. }
  1987. static av_always_inline void
  1988. find_c_packed_planar_out_funcs(SwsContext *c,
  1989. yuv2planar1_fn *yuv2plane1, yuv2planarX_fn *yuv2planeX,
  1990. yuv2interleavedX_fn *yuv2nv12cX,
  1991. yuv2packed1_fn *yuv2packed1, yuv2packed2_fn *yuv2packed2,
  1992. yuv2packedX_fn *yuv2packedX)
  1993. {
  1994. enum PixelFormat dstFormat = c->dstFormat;
  1995. if (is16BPS(dstFormat)) {
  1996. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
  1997. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
  1998. } else if (is9_OR_10BPS(dstFormat)) {
  1999. if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
  2000. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
  2001. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
  2002. } else {
  2003. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
  2004. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
  2005. }
  2006. } else {
  2007. *yuv2plane1 = yuv2plane1_8_c;
  2008. *yuv2planeX = yuv2planeX_8_c;
  2009. if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)
  2010. *yuv2nv12cX = yuv2nv12cX_c;
  2011. }
  2012. if(c->flags & SWS_FULL_CHR_H_INT) {
  2013. switch (dstFormat) {
  2014. case PIX_FMT_RGBA:
  2015. #if CONFIG_SMALL
  2016. *yuv2packedX = yuv2rgba32_full_X_c;
  2017. #else
  2018. #if CONFIG_SWSCALE_ALPHA
  2019. if (c->alpPixBuf) {
  2020. *yuv2packedX = yuv2rgba32_full_X_c;
  2021. } else
  2022. #endif /* CONFIG_SWSCALE_ALPHA */
  2023. {
  2024. *yuv2packedX = yuv2rgbx32_full_X_c;
  2025. }
  2026. #endif /* !CONFIG_SMALL */
  2027. break;
  2028. case PIX_FMT_ARGB:
  2029. #if CONFIG_SMALL
  2030. *yuv2packedX = yuv2argb32_full_X_c;
  2031. #else
  2032. #if CONFIG_SWSCALE_ALPHA
  2033. if (c->alpPixBuf) {
  2034. *yuv2packedX = yuv2argb32_full_X_c;
  2035. } else
  2036. #endif /* CONFIG_SWSCALE_ALPHA */
  2037. {
  2038. *yuv2packedX = yuv2xrgb32_full_X_c;
  2039. }
  2040. #endif /* !CONFIG_SMALL */
  2041. break;
  2042. case PIX_FMT_BGRA:
  2043. #if CONFIG_SMALL
  2044. *yuv2packedX = yuv2bgra32_full_X_c;
  2045. #else
  2046. #if CONFIG_SWSCALE_ALPHA
  2047. if (c->alpPixBuf) {
  2048. *yuv2packedX = yuv2bgra32_full_X_c;
  2049. } else
  2050. #endif /* CONFIG_SWSCALE_ALPHA */
  2051. {
  2052. *yuv2packedX = yuv2bgrx32_full_X_c;
  2053. }
  2054. #endif /* !CONFIG_SMALL */
  2055. break;
  2056. case PIX_FMT_ABGR:
  2057. #if CONFIG_SMALL
  2058. *yuv2packedX = yuv2abgr32_full_X_c;
  2059. #else
  2060. #if CONFIG_SWSCALE_ALPHA
  2061. if (c->alpPixBuf) {
  2062. *yuv2packedX = yuv2abgr32_full_X_c;
  2063. } else
  2064. #endif /* CONFIG_SWSCALE_ALPHA */
  2065. {
  2066. *yuv2packedX = yuv2xbgr32_full_X_c;
  2067. }
  2068. #endif /* !CONFIG_SMALL */
  2069. break;
  2070. case PIX_FMT_RGB24:
  2071. *yuv2packedX = yuv2rgb24_full_X_c;
  2072. break;
  2073. case PIX_FMT_BGR24:
  2074. *yuv2packedX = yuv2bgr24_full_X_c;
  2075. break;
  2076. }
  2077. if(!*yuv2packedX)
  2078. goto YUV_PACKED;
  2079. } else {
  2080. YUV_PACKED:
  2081. switch (dstFormat) {
  2082. case PIX_FMT_GRAY16BE:
  2083. *yuv2packed1 = yuv2gray16BE_1_c;
  2084. *yuv2packed2 = yuv2gray16BE_2_c;
  2085. *yuv2packedX = yuv2gray16BE_X_c;
  2086. break;
  2087. case PIX_FMT_GRAY16LE:
  2088. *yuv2packed1 = yuv2gray16LE_1_c;
  2089. *yuv2packed2 = yuv2gray16LE_2_c;
  2090. *yuv2packedX = yuv2gray16LE_X_c;
  2091. break;
  2092. case PIX_FMT_MONOWHITE:
  2093. *yuv2packed1 = yuv2monowhite_1_c;
  2094. *yuv2packed2 = yuv2monowhite_2_c;
  2095. *yuv2packedX = yuv2monowhite_X_c;
  2096. break;
  2097. case PIX_FMT_MONOBLACK:
  2098. *yuv2packed1 = yuv2monoblack_1_c;
  2099. *yuv2packed2 = yuv2monoblack_2_c;
  2100. *yuv2packedX = yuv2monoblack_X_c;
  2101. break;
  2102. case PIX_FMT_YUYV422:
  2103. *yuv2packed1 = yuv2yuyv422_1_c;
  2104. *yuv2packed2 = yuv2yuyv422_2_c;
  2105. *yuv2packedX = yuv2yuyv422_X_c;
  2106. break;
  2107. case PIX_FMT_UYVY422:
  2108. *yuv2packed1 = yuv2uyvy422_1_c;
  2109. *yuv2packed2 = yuv2uyvy422_2_c;
  2110. *yuv2packedX = yuv2uyvy422_X_c;
  2111. break;
  2112. case PIX_FMT_RGB48LE:
  2113. *yuv2packed1 = yuv2rgb48le_1_c;
  2114. *yuv2packed2 = yuv2rgb48le_2_c;
  2115. *yuv2packedX = yuv2rgb48le_X_c;
  2116. break;
  2117. case PIX_FMT_RGB48BE:
  2118. *yuv2packed1 = yuv2rgb48be_1_c;
  2119. *yuv2packed2 = yuv2rgb48be_2_c;
  2120. *yuv2packedX = yuv2rgb48be_X_c;
  2121. break;
  2122. case PIX_FMT_BGR48LE:
  2123. *yuv2packed1 = yuv2bgr48le_1_c;
  2124. *yuv2packed2 = yuv2bgr48le_2_c;
  2125. *yuv2packedX = yuv2bgr48le_X_c;
  2126. break;
  2127. case PIX_FMT_BGR48BE:
  2128. *yuv2packed1 = yuv2bgr48be_1_c;
  2129. *yuv2packed2 = yuv2bgr48be_2_c;
  2130. *yuv2packedX = yuv2bgr48be_X_c;
  2131. break;
  2132. case PIX_FMT_RGB32:
  2133. case PIX_FMT_BGR32:
  2134. #if CONFIG_SMALL
  2135. *yuv2packed1 = yuv2rgb32_1_c;
  2136. *yuv2packed2 = yuv2rgb32_2_c;
  2137. *yuv2packedX = yuv2rgb32_X_c;
  2138. #else
  2139. #if CONFIG_SWSCALE_ALPHA
  2140. if (c->alpPixBuf) {
  2141. *yuv2packed1 = yuv2rgba32_1_c;
  2142. *yuv2packed2 = yuv2rgba32_2_c;
  2143. *yuv2packedX = yuv2rgba32_X_c;
  2144. } else
  2145. #endif /* CONFIG_SWSCALE_ALPHA */
  2146. {
  2147. *yuv2packed1 = yuv2rgbx32_1_c;
  2148. *yuv2packed2 = yuv2rgbx32_2_c;
  2149. *yuv2packedX = yuv2rgbx32_X_c;
  2150. }
  2151. #endif /* !CONFIG_SMALL */
  2152. break;
  2153. case PIX_FMT_RGB32_1:
  2154. case PIX_FMT_BGR32_1:
  2155. #if CONFIG_SMALL
  2156. *yuv2packed1 = yuv2rgb32_1_1_c;
  2157. *yuv2packed2 = yuv2rgb32_1_2_c;
  2158. *yuv2packedX = yuv2rgb32_1_X_c;
  2159. #else
  2160. #if CONFIG_SWSCALE_ALPHA
  2161. if (c->alpPixBuf) {
  2162. *yuv2packed1 = yuv2rgba32_1_1_c;
  2163. *yuv2packed2 = yuv2rgba32_1_2_c;
  2164. *yuv2packedX = yuv2rgba32_1_X_c;
  2165. } else
  2166. #endif /* CONFIG_SWSCALE_ALPHA */
  2167. {
  2168. *yuv2packed1 = yuv2rgbx32_1_1_c;
  2169. *yuv2packed2 = yuv2rgbx32_1_2_c;
  2170. *yuv2packedX = yuv2rgbx32_1_X_c;
  2171. }
  2172. #endif /* !CONFIG_SMALL */
  2173. break;
  2174. case PIX_FMT_RGB24:
  2175. *yuv2packed1 = yuv2rgb24_1_c;
  2176. *yuv2packed2 = yuv2rgb24_2_c;
  2177. *yuv2packedX = yuv2rgb24_X_c;
  2178. break;
  2179. case PIX_FMT_BGR24:
  2180. *yuv2packed1 = yuv2bgr24_1_c;
  2181. *yuv2packed2 = yuv2bgr24_2_c;
  2182. *yuv2packedX = yuv2bgr24_X_c;
  2183. break;
  2184. case PIX_FMT_RGB565LE:
  2185. case PIX_FMT_RGB565BE:
  2186. case PIX_FMT_BGR565LE:
  2187. case PIX_FMT_BGR565BE:
  2188. *yuv2packed1 = yuv2rgb16_1_c;
  2189. *yuv2packed2 = yuv2rgb16_2_c;
  2190. *yuv2packedX = yuv2rgb16_X_c;
  2191. break;
  2192. case PIX_FMT_RGB555LE:
  2193. case PIX_FMT_RGB555BE:
  2194. case PIX_FMT_BGR555LE:
  2195. case PIX_FMT_BGR555BE:
  2196. *yuv2packed1 = yuv2rgb15_1_c;
  2197. *yuv2packed2 = yuv2rgb15_2_c;
  2198. *yuv2packedX = yuv2rgb15_X_c;
  2199. break;
  2200. case PIX_FMT_RGB444LE:
  2201. case PIX_FMT_RGB444BE:
  2202. case PIX_FMT_BGR444LE:
  2203. case PIX_FMT_BGR444BE:
  2204. *yuv2packed1 = yuv2rgb12_1_c;
  2205. *yuv2packed2 = yuv2rgb12_2_c;
  2206. *yuv2packedX = yuv2rgb12_X_c;
  2207. break;
  2208. case PIX_FMT_RGB8:
  2209. case PIX_FMT_BGR8:
  2210. *yuv2packed1 = yuv2rgb8_1_c;
  2211. *yuv2packed2 = yuv2rgb8_2_c;
  2212. *yuv2packedX = yuv2rgb8_X_c;
  2213. break;
  2214. case PIX_FMT_RGB4:
  2215. case PIX_FMT_BGR4:
  2216. *yuv2packed1 = yuv2rgb4_1_c;
  2217. *yuv2packed2 = yuv2rgb4_2_c;
  2218. *yuv2packedX = yuv2rgb4_X_c;
  2219. break;
  2220. case PIX_FMT_RGB4_BYTE:
  2221. case PIX_FMT_BGR4_BYTE:
  2222. *yuv2packed1 = yuv2rgb4b_1_c;
  2223. *yuv2packed2 = yuv2rgb4b_2_c;
  2224. *yuv2packedX = yuv2rgb4b_X_c;
  2225. break;
  2226. }
  2227. }
  2228. }
  2229. #define DEBUG_SWSCALE_BUFFERS 0
  2230. #define DEBUG_BUFFERS(...) if (DEBUG_SWSCALE_BUFFERS) av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  2231. static int swScale(SwsContext *c, const uint8_t* src[],
  2232. int srcStride[], int srcSliceY,
  2233. int srcSliceH, uint8_t* dst[], int dstStride[])
  2234. {
  2235. /* load a few things into local vars to make the code more readable? and faster */
  2236. const int srcW= c->srcW;
  2237. const int dstW= c->dstW;
  2238. const int dstH= c->dstH;
  2239. const int chrDstW= c->chrDstW;
  2240. const int chrSrcW= c->chrSrcW;
  2241. const int lumXInc= c->lumXInc;
  2242. const int chrXInc= c->chrXInc;
  2243. const enum PixelFormat dstFormat= c->dstFormat;
  2244. const int flags= c->flags;
  2245. int16_t *vLumFilterPos= c->vLumFilterPos;
  2246. int16_t *vChrFilterPos= c->vChrFilterPos;
  2247. int16_t *hLumFilterPos= c->hLumFilterPos;
  2248. int16_t *hChrFilterPos= c->hChrFilterPos;
  2249. int16_t *vLumFilter= c->vLumFilter;
  2250. int16_t *vChrFilter= c->vChrFilter;
  2251. int16_t *hLumFilter= c->hLumFilter;
  2252. int16_t *hChrFilter= c->hChrFilter;
  2253. int32_t *lumMmxFilter= c->lumMmxFilter;
  2254. int32_t *chrMmxFilter= c->chrMmxFilter;
  2255. int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
  2256. const int vLumFilterSize= c->vLumFilterSize;
  2257. const int vChrFilterSize= c->vChrFilterSize;
  2258. const int hLumFilterSize= c->hLumFilterSize;
  2259. const int hChrFilterSize= c->hChrFilterSize;
  2260. int16_t **lumPixBuf= c->lumPixBuf;
  2261. int16_t **chrUPixBuf= c->chrUPixBuf;
  2262. int16_t **chrVPixBuf= c->chrVPixBuf;
  2263. int16_t **alpPixBuf= c->alpPixBuf;
  2264. const int vLumBufSize= c->vLumBufSize;
  2265. const int vChrBufSize= c->vChrBufSize;
  2266. uint8_t *formatConvBuffer= c->formatConvBuffer;
  2267. const int chrSrcSliceY= srcSliceY >> c->chrSrcVSubSample;
  2268. const int chrSrcSliceH= -((-srcSliceH) >> c->chrSrcVSubSample);
  2269. int lastDstY;
  2270. uint32_t *pal=c->pal_yuv;
  2271. int should_dither= isNBPS(c->srcFormat) || is16BPS(c->srcFormat);
  2272. yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
  2273. yuv2planarX_fn yuv2planeX = c->yuv2planeX;
  2274. yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
  2275. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  2276. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  2277. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  2278. /* vars which will change and which we need to store back in the context */
  2279. int dstY= c->dstY;
  2280. int lumBufIndex= c->lumBufIndex;
  2281. int chrBufIndex= c->chrBufIndex;
  2282. int lastInLumBuf= c->lastInLumBuf;
  2283. int lastInChrBuf= c->lastInChrBuf;
  2284. if (isPacked(c->srcFormat)) {
  2285. src[0]=
  2286. src[1]=
  2287. src[2]=
  2288. src[3]= src[0];
  2289. srcStride[0]=
  2290. srcStride[1]=
  2291. srcStride[2]=
  2292. srcStride[3]= srcStride[0];
  2293. }
  2294. srcStride[1]<<= c->vChrDrop;
  2295. srcStride[2]<<= c->vChrDrop;
  2296. DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  2297. src[0], srcStride[0], src[1], srcStride[1], src[2], srcStride[2], src[3], srcStride[3],
  2298. dst[0], dstStride[0], dst[1], dstStride[1], dst[2], dstStride[2], dst[3], dstStride[3]);
  2299. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  2300. srcSliceY, srcSliceH, dstY, dstH);
  2301. DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
  2302. vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
  2303. if (dstStride[0]%8 !=0 || dstStride[1]%8 !=0 || dstStride[2]%8 !=0 || dstStride[3]%8 != 0) {
  2304. static int warnedAlready=0; //FIXME move this into the context perhaps
  2305. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  2306. av_log(c, AV_LOG_WARNING, "Warning: dstStride is not aligned!\n"
  2307. " ->cannot do aligned memory accesses anymore\n");
  2308. warnedAlready=1;
  2309. }
  2310. }
  2311. /* Note the user might start scaling the picture in the middle so this
  2312. will not get executed. This is not really intended but works
  2313. currently, so people might do it. */
  2314. if (srcSliceY ==0) {
  2315. lumBufIndex=-1;
  2316. chrBufIndex=-1;
  2317. dstY=0;
  2318. lastInLumBuf= -1;
  2319. lastInChrBuf= -1;
  2320. }
  2321. if (!should_dither) {
  2322. c->chrDither8 = c->lumDither8 = ff_sws_pb_64;
  2323. }
  2324. lastDstY= dstY;
  2325. for (;dstY < dstH; dstY++) {
  2326. const int chrDstY= dstY>>c->chrDstVSubSample;
  2327. uint8_t *dest[4] = {
  2328. dst[0] + dstStride[0] * dstY,
  2329. dst[1] + dstStride[1] * chrDstY,
  2330. dst[2] + dstStride[2] * chrDstY,
  2331. (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
  2332. };
  2333. const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
  2334. const int firstLumSrcY2= vLumFilterPos[FFMIN(dstY | ((1<<c->chrDstVSubSample) - 1), dstH-1)];
  2335. const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
  2336. int lastLumSrcY= firstLumSrcY + vLumFilterSize -1; // Last line needed as input
  2337. int lastLumSrcY2=firstLumSrcY2+ vLumFilterSize -1; // Last line needed as input
  2338. int lastChrSrcY= firstChrSrcY + vChrFilterSize -1; // Last line needed as input
  2339. int enough_lines;
  2340. //handle holes (FAST_BILINEAR & weird filters)
  2341. if (firstLumSrcY > lastInLumBuf) lastInLumBuf= firstLumSrcY-1;
  2342. if (firstChrSrcY > lastInChrBuf) lastInChrBuf= firstChrSrcY-1;
  2343. assert(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  2344. assert(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  2345. DEBUG_BUFFERS("dstY: %d\n", dstY);
  2346. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  2347. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  2348. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  2349. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  2350. // Do we have enough lines in this slice to output the dstY line
  2351. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH && lastChrSrcY < -((-srcSliceY - srcSliceH)>>c->chrSrcVSubSample);
  2352. if (!enough_lines) {
  2353. lastLumSrcY = srcSliceY + srcSliceH - 1;
  2354. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  2355. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  2356. lastLumSrcY, lastChrSrcY);
  2357. }
  2358. //Do horizontal scaling
  2359. while(lastInLumBuf < lastLumSrcY) {
  2360. const uint8_t *src1= src[0]+(lastInLumBuf + 1 - srcSliceY)*srcStride[0];
  2361. const uint8_t *src2= src[1]+(lastInLumBuf + 1 - srcSliceY)*srcStride[1];
  2362. const uint8_t *src3= src[2]+(lastInLumBuf + 1 - srcSliceY)*srcStride[2];
  2363. const uint8_t *src4= src[3]+(lastInLumBuf + 1 - srcSliceY)*srcStride[3];
  2364. lumBufIndex++;
  2365. assert(lumBufIndex < 2*vLumBufSize);
  2366. assert(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  2367. assert(lastInLumBuf + 1 - srcSliceY >= 0);
  2368. hyscale(c, lumPixBuf[ lumBufIndex ], dstW, src1, src2, src3, srcW, lumXInc,
  2369. hLumFilter, hLumFilterPos, hLumFilterSize,
  2370. formatConvBuffer,
  2371. pal, 0);
  2372. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  2373. hyscale(c, alpPixBuf[ lumBufIndex ], dstW, src4, NULL, NULL, srcW,
  2374. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  2375. formatConvBuffer,
  2376. pal, 1);
  2377. lastInLumBuf++;
  2378. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  2379. lumBufIndex, lastInLumBuf);
  2380. }
  2381. while(lastInChrBuf < lastChrSrcY) {
  2382. const uint8_t *src0= src[0]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[0];
  2383. const uint8_t *src1= src[1]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[1];
  2384. const uint8_t *src2= src[2]+(lastInChrBuf + 1 - chrSrcSliceY)*srcStride[2];
  2385. chrBufIndex++;
  2386. assert(chrBufIndex < 2*vChrBufSize);
  2387. assert(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  2388. assert(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  2389. //FIXME replace parameters through context struct (some at least)
  2390. if (c->needs_hcscale)
  2391. hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  2392. chrDstW, src0, src1, src2, chrSrcW, chrXInc,
  2393. hChrFilter, hChrFilterPos, hChrFilterSize,
  2394. formatConvBuffer, pal);
  2395. lastInChrBuf++;
  2396. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  2397. chrBufIndex, lastInChrBuf);
  2398. }
  2399. //wrap buf index around to stay inside the ring buffer
  2400. if (lumBufIndex >= vLumBufSize) lumBufIndex-= vLumBufSize;
  2401. if (chrBufIndex >= vChrBufSize) chrBufIndex-= vChrBufSize;
  2402. if (!enough_lines)
  2403. break; //we can't output a dstY line so let's try with the next slice
  2404. #if HAVE_MMX
  2405. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex, lastInLumBuf, lastInChrBuf);
  2406. #endif
  2407. if (should_dither) {
  2408. c->chrDither8 = dither_8x8_128[chrDstY & 7];
  2409. c->lumDither8 = dither_8x8_128[dstY & 7];
  2410. }
  2411. if (dstY >= dstH-2) {
  2412. // hmm looks like we can't use MMX here without overwriting this array's tail
  2413. find_c_packed_planar_out_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
  2414. &yuv2packed1, &yuv2packed2, &yuv2packedX);
  2415. }
  2416. {
  2417. const int16_t **lumSrcPtr= (const int16_t **) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  2418. const int16_t **chrUSrcPtr= (const int16_t **) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  2419. const int16_t **chrVSrcPtr= (const int16_t **) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  2420. const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  2421. if (isPlanarYUV(dstFormat) || dstFormat==PIX_FMT_GRAY8) { //YV12 like
  2422. const int chrSkipMask= (1<<c->chrDstVSubSample)-1;
  2423. if (vLumFilterSize == 1) {
  2424. yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
  2425. } else {
  2426. yuv2planeX(vLumFilter + dstY * vLumFilterSize, vLumFilterSize,
  2427. lumSrcPtr, dest[0], dstW, c->lumDither8, 0);
  2428. }
  2429. if (!((dstY&chrSkipMask) || isGray(dstFormat))) {
  2430. if (yuv2nv12cX) {
  2431. yuv2nv12cX(c, vChrFilter + chrDstY * vChrFilterSize, vChrFilterSize, chrUSrcPtr, chrVSrcPtr, dest[1], chrDstW);
  2432. } else if (vChrFilterSize == 1) {
  2433. yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
  2434. yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
  2435. } else {
  2436. yuv2planeX(vChrFilter + chrDstY * vChrFilterSize, vChrFilterSize,
  2437. chrUSrcPtr, dest[1], chrDstW, c->chrDither8, 0);
  2438. yuv2planeX(vChrFilter + chrDstY * vChrFilterSize, vChrFilterSize,
  2439. chrVSrcPtr, dest[2], chrDstW, c->chrDither8, 3);
  2440. }
  2441. }
  2442. if (CONFIG_SWSCALE_ALPHA && alpPixBuf){
  2443. if (vLumFilterSize == 1) {
  2444. yuv2plane1(alpSrcPtr[0], dest[3], dstW, c->lumDither8, 0);
  2445. } else {
  2446. yuv2planeX(vLumFilter + dstY * vLumFilterSize, vLumFilterSize,
  2447. alpSrcPtr, dest[3], dstW, c->lumDither8, 0);
  2448. }
  2449. }
  2450. } else {
  2451. assert(lumSrcPtr + vLumFilterSize - 1 < lumPixBuf + vLumBufSize*2);
  2452. assert(chrUSrcPtr + vChrFilterSize - 1 < chrUPixBuf + vChrBufSize*2);
  2453. if (c->yuv2packed1 && vLumFilterSize == 1 && vChrFilterSize == 2) { //unscaled RGB
  2454. int chrAlpha = vChrFilter[2 * dstY + 1];
  2455. yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  2456. alpPixBuf ? *alpSrcPtr : NULL,
  2457. dest[0], dstW, chrAlpha, dstY);
  2458. } else if (c->yuv2packed2 && vLumFilterSize == 2 && vChrFilterSize == 2) { //bilinear upscale RGB
  2459. int lumAlpha = vLumFilter[2 * dstY + 1];
  2460. int chrAlpha = vChrFilter[2 * dstY + 1];
  2461. lumMmxFilter[2] =
  2462. lumMmxFilter[3] = vLumFilter[2 * dstY ] * 0x10001;
  2463. chrMmxFilter[2] =
  2464. chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
  2465. yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  2466. alpPixBuf ? alpSrcPtr : NULL,
  2467. dest[0], dstW, lumAlpha, chrAlpha, dstY);
  2468. } else { //general RGB
  2469. yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
  2470. lumSrcPtr, vLumFilterSize,
  2471. vChrFilter + dstY * vChrFilterSize,
  2472. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  2473. alpSrcPtr, dest[0], dstW, dstY);
  2474. }
  2475. }
  2476. }
  2477. }
  2478. if ((dstFormat == PIX_FMT_YUVA420P) && !alpPixBuf)
  2479. fillPlane(dst[3], dstStride[3], dstW, dstY-lastDstY, lastDstY, 255);
  2480. #if HAVE_MMX2
  2481. if (av_get_cpu_flags() & AV_CPU_FLAG_MMX2)
  2482. __asm__ volatile("sfence":::"memory");
  2483. #endif
  2484. emms_c();
  2485. /* store changed local vars back in the context */
  2486. c->dstY= dstY;
  2487. c->lumBufIndex= lumBufIndex;
  2488. c->chrBufIndex= chrBufIndex;
  2489. c->lastInLumBuf= lastInLumBuf;
  2490. c->lastInChrBuf= lastInChrBuf;
  2491. return dstY - lastDstY;
  2492. }
  2493. static av_cold void sws_init_swScale_c(SwsContext *c)
  2494. {
  2495. enum PixelFormat srcFormat = c->srcFormat;
  2496. find_c_packed_planar_out_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
  2497. &c->yuv2nv12cX, &c->yuv2packed1, &c->yuv2packed2,
  2498. &c->yuv2packedX);
  2499. c->chrToYV12 = NULL;
  2500. switch(srcFormat) {
  2501. case PIX_FMT_YUYV422 : c->chrToYV12 = yuy2ToUV_c; break;
  2502. case PIX_FMT_UYVY422 : c->chrToYV12 = uyvyToUV_c; break;
  2503. case PIX_FMT_NV12 : c->chrToYV12 = nv12ToUV_c; break;
  2504. case PIX_FMT_NV21 : c->chrToYV12 = nv21ToUV_c; break;
  2505. case PIX_FMT_RGB8 :
  2506. case PIX_FMT_BGR8 :
  2507. case PIX_FMT_PAL8 :
  2508. case PIX_FMT_BGR4_BYTE:
  2509. case PIX_FMT_RGB4_BYTE: c->chrToYV12 = palToUV_c; break;
  2510. #if HAVE_BIGENDIAN
  2511. case PIX_FMT_YUV444P9LE:
  2512. case PIX_FMT_YUV422P9LE:
  2513. case PIX_FMT_YUV420P9LE:
  2514. case PIX_FMT_YUV422P10LE:
  2515. case PIX_FMT_YUV420P10LE:
  2516. case PIX_FMT_YUV444P10LE:
  2517. case PIX_FMT_YUV420P16LE:
  2518. case PIX_FMT_YUV422P16LE:
  2519. case PIX_FMT_YUV444P16LE: c->chrToYV12 = bswap16UV_c; break;
  2520. #else
  2521. case PIX_FMT_YUV444P9BE:
  2522. case PIX_FMT_YUV422P9BE:
  2523. case PIX_FMT_YUV420P9BE:
  2524. case PIX_FMT_YUV444P10BE:
  2525. case PIX_FMT_YUV422P10BE:
  2526. case PIX_FMT_YUV420P10BE:
  2527. case PIX_FMT_YUV420P16BE:
  2528. case PIX_FMT_YUV422P16BE:
  2529. case PIX_FMT_YUV444P16BE: c->chrToYV12 = bswap16UV_c; break;
  2530. #endif
  2531. }
  2532. if (c->chrSrcHSubSample) {
  2533. switch(srcFormat) {
  2534. case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_half_c; break;
  2535. case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_half_c; break;
  2536. case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_half_c; break;
  2537. case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_half_c; break;
  2538. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_half_c; break;
  2539. case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_half_c; break;
  2540. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_half_c; break;
  2541. case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_half_c; break;
  2542. case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_half_c; break;
  2543. case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_half_c; break;
  2544. case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_half_c; break;
  2545. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_half_c; break;
  2546. case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_half_c; break;
  2547. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_half_c; break;
  2548. case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_half_c; break;
  2549. case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_half_c; break;
  2550. case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_half_c; break;
  2551. case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_half_c; break;
  2552. case PIX_FMT_GBR24P : c->chrToYV12 = gbr24pToUV_half_c; break;
  2553. }
  2554. } else {
  2555. switch(srcFormat) {
  2556. case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_c; break;
  2557. case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_c; break;
  2558. case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_c; break;
  2559. case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_c; break;
  2560. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_c; break;
  2561. case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_c; break;
  2562. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_c; break;
  2563. case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_c; break;
  2564. case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_c; break;
  2565. case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_c; break;
  2566. case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_c; break;
  2567. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_c; break;
  2568. case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_c; break;
  2569. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_c; break;
  2570. case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_c; break;
  2571. case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_c; break;
  2572. case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_c; break;
  2573. case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_c; break;
  2574. case PIX_FMT_GBR24P : c->chrToYV12 = gbr24pToUV_c; break;
  2575. }
  2576. }
  2577. c->lumToYV12 = NULL;
  2578. c->alpToYV12 = NULL;
  2579. switch (srcFormat) {
  2580. #if HAVE_BIGENDIAN
  2581. case PIX_FMT_YUV444P9LE:
  2582. case PIX_FMT_YUV422P9LE:
  2583. case PIX_FMT_YUV420P9LE:
  2584. case PIX_FMT_YUV422P10LE:
  2585. case PIX_FMT_YUV420P10LE:
  2586. case PIX_FMT_YUV444P10LE:
  2587. case PIX_FMT_YUV420P16LE:
  2588. case PIX_FMT_YUV422P16LE:
  2589. case PIX_FMT_YUV444P16LE:
  2590. case PIX_FMT_GRAY16LE: c->lumToYV12 = bswap16Y_c; break;
  2591. #else
  2592. case PIX_FMT_YUV444P9BE:
  2593. case PIX_FMT_YUV422P9BE:
  2594. case PIX_FMT_YUV420P9BE:
  2595. case PIX_FMT_YUV444P10BE:
  2596. case PIX_FMT_YUV422P10BE:
  2597. case PIX_FMT_YUV420P10BE:
  2598. case PIX_FMT_YUV420P16BE:
  2599. case PIX_FMT_YUV422P16BE:
  2600. case PIX_FMT_YUV444P16BE:
  2601. case PIX_FMT_GRAY16BE: c->lumToYV12 = bswap16Y_c; break;
  2602. #endif
  2603. case PIX_FMT_YUYV422 :
  2604. case PIX_FMT_Y400A : c->lumToYV12 = yuy2ToY_c; break;
  2605. case PIX_FMT_UYVY422 : c->lumToYV12 = uyvyToY_c; break;
  2606. case PIX_FMT_BGR24 : c->lumToYV12 = bgr24ToY_c; break;
  2607. case PIX_FMT_BGR565LE : c->lumToYV12 = bgr16leToY_c; break;
  2608. case PIX_FMT_BGR565BE : c->lumToYV12 = bgr16beToY_c; break;
  2609. case PIX_FMT_BGR555LE : c->lumToYV12 = bgr15leToY_c; break;
  2610. case PIX_FMT_BGR555BE : c->lumToYV12 = bgr15beToY_c; break;
  2611. case PIX_FMT_RGB24 : c->lumToYV12 = rgb24ToY_c; break;
  2612. case PIX_FMT_RGB565LE : c->lumToYV12 = rgb16leToY_c; break;
  2613. case PIX_FMT_RGB565BE : c->lumToYV12 = rgb16beToY_c; break;
  2614. case PIX_FMT_RGB555LE : c->lumToYV12 = rgb15leToY_c; break;
  2615. case PIX_FMT_RGB555BE : c->lumToYV12 = rgb15beToY_c; break;
  2616. case PIX_FMT_RGB8 :
  2617. case PIX_FMT_BGR8 :
  2618. case PIX_FMT_PAL8 :
  2619. case PIX_FMT_BGR4_BYTE:
  2620. case PIX_FMT_RGB4_BYTE: c->lumToYV12 = palToY_c; break;
  2621. case PIX_FMT_MONOBLACK: c->lumToYV12 = monoblack2Y_c; break;
  2622. case PIX_FMT_MONOWHITE: c->lumToYV12 = monowhite2Y_c; break;
  2623. case PIX_FMT_RGB32 : c->lumToYV12 = bgr32ToY_c; break;
  2624. case PIX_FMT_RGB32_1: c->lumToYV12 = bgr321ToY_c; break;
  2625. case PIX_FMT_BGR32 : c->lumToYV12 = rgb32ToY_c; break;
  2626. case PIX_FMT_BGR32_1: c->lumToYV12 = rgb321ToY_c; break;
  2627. case PIX_FMT_RGB48BE: c->lumToYV12 = rgb48BEToY_c; break;
  2628. case PIX_FMT_RGB48LE: c->lumToYV12 = rgb48LEToY_c; break;
  2629. case PIX_FMT_BGR48BE: c->lumToYV12 = bgr48BEToY_c; break;
  2630. case PIX_FMT_BGR48LE: c->lumToYV12 = bgr48LEToY_c; break;
  2631. case PIX_FMT_GBR24P : c->lumToYV12 = gbr24pToY_c ; break;
  2632. }
  2633. if (c->alpPixBuf) {
  2634. switch (srcFormat) {
  2635. case PIX_FMT_BGRA:
  2636. case PIX_FMT_RGBA: c->alpToYV12 = rgbaToA_c; break;
  2637. case PIX_FMT_ABGR:
  2638. case PIX_FMT_ARGB: c->alpToYV12 = abgrToA_c; break;
  2639. case PIX_FMT_Y400A: c->alpToYV12 = uyvyToY_c; break;
  2640. case PIX_FMT_PAL8 : c->alpToYV12 = palToA_c; break;
  2641. }
  2642. }
  2643. if (c->srcBpc == 8) {
  2644. if (c->dstBpc <= 10) {
  2645. c->hyScale = c->hcScale = hScale8To15_c;
  2646. if (c->flags & SWS_FAST_BILINEAR) {
  2647. c->hyscale_fast = hyscale_fast_c;
  2648. c->hcscale_fast = hcscale_fast_c;
  2649. }
  2650. } else {
  2651. c->hyScale = c->hcScale = hScale8To19_c;
  2652. }
  2653. } else {
  2654. c->hyScale = c->hcScale = c->dstBpc > 10 ? hScale16To19_c : hScale16To15_c;
  2655. }
  2656. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  2657. if (c->dstBpc <= 10) {
  2658. if (c->srcRange) {
  2659. c->lumConvertRange = lumRangeFromJpeg_c;
  2660. c->chrConvertRange = chrRangeFromJpeg_c;
  2661. } else {
  2662. c->lumConvertRange = lumRangeToJpeg_c;
  2663. c->chrConvertRange = chrRangeToJpeg_c;
  2664. }
  2665. } else {
  2666. if (c->srcRange) {
  2667. c->lumConvertRange = lumRangeFromJpeg16_c;
  2668. c->chrConvertRange = chrRangeFromJpeg16_c;
  2669. } else {
  2670. c->lumConvertRange = lumRangeToJpeg16_c;
  2671. c->chrConvertRange = chrRangeToJpeg16_c;
  2672. }
  2673. }
  2674. }
  2675. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  2676. srcFormat == PIX_FMT_MONOBLACK || srcFormat == PIX_FMT_MONOWHITE))
  2677. c->needs_hcscale = 1;
  2678. }
  2679. SwsFunc ff_getSwsFunc(SwsContext *c)
  2680. {
  2681. sws_init_swScale_c(c);
  2682. if (HAVE_MMX)
  2683. ff_sws_init_swScale_mmx(c);
  2684. if (HAVE_ALTIVEC)
  2685. ff_sws_init_swScale_altivec(c);
  2686. return swScale;
  2687. }