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.

3040 lines
109KB

  1. /*
  2. * Copyright (C) 2001-2012 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. #include <math.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/avutil.h"
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/bswap.h"
  28. #include "libavutil/cpu.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/mathematics.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "config.h"
  33. #include "rgb2rgb.h"
  34. #include "swscale.h"
  35. #include "swscale_internal.h"
  36. DECLARE_ALIGNED(8, const uint8_t, ff_dither_2x2_4)[][8] = {
  37. { 1, 3, 1, 3, 1, 3, 1, 3, },
  38. { 2, 0, 2, 0, 2, 0, 2, 0, },
  39. { 1, 3, 1, 3, 1, 3, 1, 3, },
  40. };
  41. DECLARE_ALIGNED(8, const uint8_t, ff_dither_2x2_8)[][8] = {
  42. { 6, 2, 6, 2, 6, 2, 6, 2, },
  43. { 0, 4, 0, 4, 0, 4, 0, 4, },
  44. { 6, 2, 6, 2, 6, 2, 6, 2, },
  45. };
  46. DECLARE_ALIGNED(8, const uint8_t, ff_dither_4x4_16)[][8] = {
  47. { 8, 4, 11, 7, 8, 4, 11, 7, },
  48. { 2, 14, 1, 13, 2, 14, 1, 13, },
  49. { 10, 6, 9, 5, 10, 6, 9, 5, },
  50. { 0, 12, 3, 15, 0, 12, 3, 15, },
  51. { 8, 4, 11, 7, 8, 4, 11, 7, },
  52. };
  53. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_32)[][8] = {
  54. { 17, 9, 23, 15, 16, 8, 22, 14, },
  55. { 5, 29, 3, 27, 4, 28, 2, 26, },
  56. { 21, 13, 19, 11, 20, 12, 18, 10, },
  57. { 0, 24, 6, 30, 1, 25, 7, 31, },
  58. { 16, 8, 22, 14, 17, 9, 23, 15, },
  59. { 4, 28, 2, 26, 5, 29, 3, 27, },
  60. { 20, 12, 18, 10, 21, 13, 19, 11, },
  61. { 1, 25, 7, 31, 0, 24, 6, 30, },
  62. { 17, 9, 23, 15, 16, 8, 22, 14, },
  63. };
  64. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_73)[][8] = {
  65. { 0, 55, 14, 68, 3, 58, 17, 72, },
  66. { 37, 18, 50, 32, 40, 22, 54, 35, },
  67. { 9, 64, 5, 59, 13, 67, 8, 63, },
  68. { 46, 27, 41, 23, 49, 31, 44, 26, },
  69. { 2, 57, 16, 71, 1, 56, 15, 70, },
  70. { 39, 21, 52, 34, 38, 19, 51, 33, },
  71. { 11, 66, 7, 62, 10, 65, 6, 60, },
  72. { 48, 30, 43, 25, 47, 29, 42, 24, },
  73. { 0, 55, 14, 68, 3, 58, 17, 72, },
  74. };
  75. #if 1
  76. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
  77. {117, 62, 158, 103, 113, 58, 155, 100, },
  78. { 34, 199, 21, 186, 31, 196, 17, 182, },
  79. {144, 89, 131, 76, 141, 86, 127, 72, },
  80. { 0, 165, 41, 206, 10, 175, 52, 217, },
  81. {110, 55, 151, 96, 120, 65, 162, 107, },
  82. { 28, 193, 14, 179, 38, 203, 24, 189, },
  83. {138, 83, 124, 69, 148, 93, 134, 79, },
  84. { 7, 172, 48, 213, 3, 168, 45, 210, },
  85. {117, 62, 158, 103, 113, 58, 155, 100, },
  86. };
  87. #elif 1
  88. // tries to correct a gamma of 1.5
  89. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
  90. { 0, 143, 18, 200, 2, 156, 25, 215, },
  91. { 78, 28, 125, 64, 89, 36, 138, 74, },
  92. { 10, 180, 3, 161, 16, 195, 8, 175, },
  93. {109, 51, 93, 38, 121, 60, 105, 47, },
  94. { 1, 152, 23, 210, 0, 147, 20, 205, },
  95. { 85, 33, 134, 71, 81, 30, 130, 67, },
  96. { 14, 190, 6, 171, 12, 185, 5, 166, },
  97. {117, 57, 101, 44, 113, 54, 97, 41, },
  98. { 0, 143, 18, 200, 2, 156, 25, 215, },
  99. };
  100. #elif 1
  101. // tries to correct a gamma of 2.0
  102. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
  103. { 0, 124, 8, 193, 0, 140, 12, 213, },
  104. { 55, 14, 104, 42, 66, 19, 119, 52, },
  105. { 3, 168, 1, 145, 6, 187, 3, 162, },
  106. { 86, 31, 70, 21, 99, 39, 82, 28, },
  107. { 0, 134, 11, 206, 0, 129, 9, 200, },
  108. { 62, 17, 114, 48, 58, 16, 109, 45, },
  109. { 5, 181, 2, 157, 4, 175, 1, 151, },
  110. { 95, 36, 78, 26, 90, 34, 74, 24, },
  111. { 0, 124, 8, 193, 0, 140, 12, 213, },
  112. };
  113. #else
  114. // tries to correct a gamma of 2.5
  115. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
  116. { 0, 107, 3, 187, 0, 125, 6, 212, },
  117. { 39, 7, 86, 28, 49, 11, 102, 36, },
  118. { 1, 158, 0, 131, 3, 180, 1, 151, },
  119. { 68, 19, 52, 12, 81, 25, 64, 17, },
  120. { 0, 119, 5, 203, 0, 113, 4, 195, },
  121. { 45, 9, 96, 33, 42, 8, 91, 30, },
  122. { 2, 172, 1, 144, 2, 165, 0, 137, },
  123. { 77, 23, 60, 15, 72, 21, 56, 14, },
  124. { 0, 107, 3, 187, 0, 125, 6, 212, },
  125. };
  126. #endif
  127. #define output_pixel(pos, val, bias, signedness) \
  128. if (big_endian) { \
  129. AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  130. } else { \
  131. AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  132. }
  133. static av_always_inline void
  134. yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
  135. int big_endian, int output_bits)
  136. {
  137. int i;
  138. int shift = 3;
  139. av_assert0(output_bits == 16);
  140. for (i = 0; i < dstW; i++) {
  141. int val = src[i] + (1 << (shift - 1));
  142. output_pixel(&dest[i], val, 0, uint);
  143. }
  144. }
  145. static av_always_inline void
  146. yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
  147. const int32_t **src, uint16_t *dest, int dstW,
  148. int big_endian, int output_bits)
  149. {
  150. int i;
  151. int shift = 15;
  152. av_assert0(output_bits == 16);
  153. for (i = 0; i < dstW; i++) {
  154. int val = 1 << (shift - 1);
  155. int j;
  156. /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
  157. * filters (or anything with negative coeffs, the range can be slightly
  158. * wider in both directions. To account for this overflow, we subtract
  159. * a constant so it always fits in the signed range (assuming a
  160. * reasonable filterSize), and re-add that at the end. */
  161. val -= 0x40000000;
  162. for (j = 0; j < filterSize; j++)
  163. val += src[j][i] * (unsigned)filter[j];
  164. output_pixel(&dest[i], val, 0x8000, int);
  165. }
  166. }
  167. static void yuv2p016cX_c(enum AVPixelFormat dstFormat, const uint8_t *chrDither,
  168. const int16_t *chrFilter, int chrFilterSize,
  169. const int16_t **chrUSrc, const int16_t **chrVSrc,
  170. uint8_t *dest8, int chrDstW)
  171. {
  172. uint16_t *dest = (uint16_t*)dest8;
  173. const int32_t **uSrc = (const int32_t **)chrUSrc;
  174. const int32_t **vSrc = (const int32_t **)chrVSrc;
  175. int shift = 15;
  176. int big_endian = dstFormat == AV_PIX_FMT_P016BE;
  177. int i, j;
  178. for (i = 0; i < chrDstW; i++) {
  179. int u = 1 << (shift - 1);
  180. int v = 1 << (shift - 1);
  181. /* See yuv2planeX_16_c_template for details. */
  182. u -= 0x40000000;
  183. v -= 0x40000000;
  184. for (j = 0; j < chrFilterSize; j++) {
  185. u += uSrc[j][i] * (unsigned)chrFilter[j];
  186. v += vSrc[j][i] * (unsigned)chrFilter[j];
  187. }
  188. output_pixel(&dest[2*i] , u, 0x8000, int);
  189. output_pixel(&dest[2*i+1], v, 0x8000, int);
  190. }
  191. }
  192. static av_always_inline void
  193. yuv2plane1_float_c_template(const int32_t *src, float *dest, int dstW)
  194. {
  195. static const int big_endian = HAVE_BIGENDIAN;
  196. static const int shift = 3;
  197. static const float float_mult = 1.0f / 65535.0f;
  198. int i, val;
  199. uint16_t val_uint;
  200. for (i = 0; i < dstW; ++i){
  201. val = src[i] + (1 << (shift - 1));
  202. output_pixel(&val_uint, val, 0, uint);
  203. dest[i] = float_mult * (float)val_uint;
  204. }
  205. }
  206. static av_always_inline void
  207. yuv2plane1_float_bswap_c_template(const int32_t *src, uint32_t *dest, int dstW)
  208. {
  209. static const int big_endian = HAVE_BIGENDIAN;
  210. static const int shift = 3;
  211. static const float float_mult = 1.0f / 65535.0f;
  212. int i, val;
  213. uint16_t val_uint;
  214. for (i = 0; i < dstW; ++i){
  215. val = src[i] + (1 << (shift - 1));
  216. output_pixel(&val_uint, val, 0, uint);
  217. dest[i] = av_bswap32(av_float2int(float_mult * (float)val_uint));
  218. }
  219. }
  220. static av_always_inline void
  221. yuv2planeX_float_c_template(const int16_t *filter, int filterSize, const int32_t **src,
  222. float *dest, int dstW)
  223. {
  224. static const int big_endian = HAVE_BIGENDIAN;
  225. static const int shift = 15;
  226. static const float float_mult = 1.0f / 65535.0f;
  227. int i, j, val;
  228. uint16_t val_uint;
  229. for (i = 0; i < dstW; ++i){
  230. val = (1 << (shift - 1)) - 0x40000000;
  231. for (j = 0; j < filterSize; ++j){
  232. val += src[j][i] * (unsigned)filter[j];
  233. }
  234. output_pixel(&val_uint, val, 0x8000, int);
  235. dest[i] = float_mult * (float)val_uint;
  236. }
  237. }
  238. static av_always_inline void
  239. yuv2planeX_float_bswap_c_template(const int16_t *filter, int filterSize, const int32_t **src,
  240. uint32_t *dest, int dstW)
  241. {
  242. static const int big_endian = HAVE_BIGENDIAN;
  243. static const int shift = 15;
  244. static const float float_mult = 1.0f / 65535.0f;
  245. int i, j, val;
  246. uint16_t val_uint;
  247. for (i = 0; i < dstW; ++i){
  248. val = (1 << (shift - 1)) - 0x40000000;
  249. for (j = 0; j < filterSize; ++j){
  250. val += src[j][i] * (unsigned)filter[j];
  251. }
  252. output_pixel(&val_uint, val, 0x8000, int);
  253. dest[i] = av_bswap32(av_float2int(float_mult * (float)val_uint));
  254. }
  255. }
  256. #define yuv2plane1_float(template, dest_type, BE_LE) \
  257. static void yuv2plane1_float ## BE_LE ## _c(const int16_t *src, uint8_t *dest, int dstW, \
  258. const uint8_t *dither, int offset) \
  259. { \
  260. template((const int32_t *)src, (dest_type *)dest, dstW); \
  261. }
  262. #define yuv2planeX_float(template, dest_type, BE_LE) \
  263. static void yuv2planeX_float ## BE_LE ## _c(const int16_t *filter, int filterSize, \
  264. const int16_t **src, uint8_t *dest, int dstW, \
  265. const uint8_t *dither, int offset) \
  266. { \
  267. template(filter, filterSize, (const int32_t **)src, (dest_type *)dest, dstW); \
  268. }
  269. #if HAVE_BIGENDIAN
  270. yuv2plane1_float(yuv2plane1_float_c_template, float, BE)
  271. yuv2plane1_float(yuv2plane1_float_bswap_c_template, uint32_t, LE)
  272. yuv2planeX_float(yuv2planeX_float_c_template, float, BE)
  273. yuv2planeX_float(yuv2planeX_float_bswap_c_template, uint32_t, LE)
  274. #else
  275. yuv2plane1_float(yuv2plane1_float_c_template, float, LE)
  276. yuv2plane1_float(yuv2plane1_float_bswap_c_template, uint32_t, BE)
  277. yuv2planeX_float(yuv2planeX_float_c_template, float, LE)
  278. yuv2planeX_float(yuv2planeX_float_bswap_c_template, uint32_t, BE)
  279. #endif
  280. #undef output_pixel
  281. #define output_pixel(pos, val) \
  282. if (big_endian) { \
  283. AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  284. } else { \
  285. AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  286. }
  287. static av_always_inline void
  288. yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
  289. int big_endian, int output_bits)
  290. {
  291. int i;
  292. int shift = 15 - output_bits;
  293. for (i = 0; i < dstW; i++) {
  294. int val = src[i] + (1 << (shift - 1));
  295. output_pixel(&dest[i], val);
  296. }
  297. }
  298. static av_always_inline void
  299. yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
  300. const int16_t **src, uint16_t *dest, int dstW,
  301. int big_endian, int output_bits)
  302. {
  303. int i;
  304. int shift = 11 + 16 - output_bits;
  305. for (i = 0; i < dstW; i++) {
  306. int val = 1 << (shift - 1);
  307. int j;
  308. for (j = 0; j < filterSize; j++)
  309. val += src[j][i] * filter[j];
  310. output_pixel(&dest[i], val);
  311. }
  312. }
  313. #undef output_pixel
  314. #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
  315. static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
  316. uint8_t *dest, int dstW, \
  317. const uint8_t *dither, int offset)\
  318. { \
  319. yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
  320. (uint16_t *) dest, dstW, is_be, bits); \
  321. }\
  322. static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
  323. const int16_t **src, uint8_t *dest, int dstW, \
  324. const uint8_t *dither, int offset)\
  325. { \
  326. yuv2planeX_## template_size ## _c_template(filter, \
  327. filterSize, (const typeX_t **) src, \
  328. (uint16_t *) dest, dstW, is_be, bits); \
  329. }
  330. yuv2NBPS( 9, BE, 1, 10, int16_t)
  331. yuv2NBPS( 9, LE, 0, 10, int16_t)
  332. yuv2NBPS(10, BE, 1, 10, int16_t)
  333. yuv2NBPS(10, LE, 0, 10, int16_t)
  334. yuv2NBPS(12, BE, 1, 10, int16_t)
  335. yuv2NBPS(12, LE, 0, 10, int16_t)
  336. yuv2NBPS(14, BE, 1, 10, int16_t)
  337. yuv2NBPS(14, LE, 0, 10, int16_t)
  338. yuv2NBPS(16, BE, 1, 16, int32_t)
  339. yuv2NBPS(16, LE, 0, 16, int32_t)
  340. static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
  341. const int16_t **src, uint8_t *dest, int dstW,
  342. const uint8_t *dither, int offset)
  343. {
  344. int i;
  345. for (i=0; i<dstW; i++) {
  346. int val = dither[(i + offset) & 7] << 12;
  347. int j;
  348. for (j=0; j<filterSize; j++)
  349. val += src[j][i] * filter[j];
  350. dest[i]= av_clip_uint8(val>>19);
  351. }
  352. }
  353. static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
  354. const uint8_t *dither, int offset)
  355. {
  356. int i;
  357. for (i=0; i<dstW; i++) {
  358. int val = (src[i] + dither[(i + offset) & 7]) >> 7;
  359. dest[i]= av_clip_uint8(val);
  360. }
  361. }
  362. static void yuv2nv12cX_c(enum AVPixelFormat dstFormat, const uint8_t *chrDither,
  363. const int16_t *chrFilter, int chrFilterSize,
  364. const int16_t **chrUSrc, const int16_t **chrVSrc,
  365. uint8_t *dest, int chrDstW)
  366. {
  367. int i;
  368. if (dstFormat == AV_PIX_FMT_NV12 ||
  369. dstFormat == AV_PIX_FMT_NV24)
  370. for (i=0; i<chrDstW; i++) {
  371. int u = chrDither[i & 7] << 12;
  372. int v = chrDither[(i + 3) & 7] << 12;
  373. int j;
  374. for (j=0; j<chrFilterSize; j++) {
  375. u += chrUSrc[j][i] * chrFilter[j];
  376. v += chrVSrc[j][i] * chrFilter[j];
  377. }
  378. dest[2*i]= av_clip_uint8(u>>19);
  379. dest[2*i+1]= av_clip_uint8(v>>19);
  380. }
  381. else
  382. for (i=0; i<chrDstW; i++) {
  383. int u = chrDither[i & 7] << 12;
  384. int v = chrDither[(i + 3) & 7] << 12;
  385. int j;
  386. for (j=0; j<chrFilterSize; j++) {
  387. u += chrUSrc[j][i] * chrFilter[j];
  388. v += chrVSrc[j][i] * chrFilter[j];
  389. }
  390. dest[2*i]= av_clip_uint8(v>>19);
  391. dest[2*i+1]= av_clip_uint8(u>>19);
  392. }
  393. }
  394. #define output_pixel(pos, val) \
  395. if (big_endian) { \
  396. AV_WB16(pos, av_clip_uintp2(val >> shift, 10) << 6); \
  397. } else { \
  398. AV_WL16(pos, av_clip_uintp2(val >> shift, 10) << 6); \
  399. }
  400. static void yuv2p010l1_c(const int16_t *src,
  401. uint16_t *dest, int dstW,
  402. int big_endian)
  403. {
  404. int i;
  405. int shift = 5;
  406. for (i = 0; i < dstW; i++) {
  407. int val = src[i] + (1 << (shift - 1));
  408. output_pixel(&dest[i], val);
  409. }
  410. }
  411. static void yuv2p010lX_c(const int16_t *filter, int filterSize,
  412. const int16_t **src, uint16_t *dest, int dstW,
  413. int big_endian)
  414. {
  415. int i, j;
  416. int shift = 17;
  417. for (i = 0; i < dstW; i++) {
  418. int val = 1 << (shift - 1);
  419. for (j = 0; j < filterSize; j++)
  420. val += src[j][i] * filter[j];
  421. output_pixel(&dest[i], val);
  422. }
  423. }
  424. static void yuv2p010cX_c(enum AVPixelFormat dstFormat, const uint8_t *chrDither,
  425. const int16_t *chrFilter, int chrFilterSize,
  426. const int16_t **chrUSrc, const int16_t **chrVSrc,
  427. uint8_t *dest8, int chrDstW)
  428. {
  429. uint16_t *dest = (uint16_t*)dest8;
  430. int shift = 17;
  431. int big_endian = dstFormat == AV_PIX_FMT_P010BE;
  432. int i, j;
  433. for (i = 0; i < chrDstW; i++) {
  434. int u = 1 << (shift - 1);
  435. int v = 1 << (shift - 1);
  436. for (j = 0; j < chrFilterSize; j++) {
  437. u += chrUSrc[j][i] * chrFilter[j];
  438. v += chrVSrc[j][i] * chrFilter[j];
  439. }
  440. output_pixel(&dest[2*i] , u);
  441. output_pixel(&dest[2*i+1], v);
  442. }
  443. }
  444. static void yuv2p010l1_LE_c(const int16_t *src,
  445. uint8_t *dest, int dstW,
  446. const uint8_t *dither, int offset)
  447. {
  448. yuv2p010l1_c(src, (uint16_t*)dest, dstW, 0);
  449. }
  450. static void yuv2p010l1_BE_c(const int16_t *src,
  451. uint8_t *dest, int dstW,
  452. const uint8_t *dither, int offset)
  453. {
  454. yuv2p010l1_c(src, (uint16_t*)dest, dstW, 1);
  455. }
  456. static void yuv2p010lX_LE_c(const int16_t *filter, int filterSize,
  457. const int16_t **src, uint8_t *dest, int dstW,
  458. const uint8_t *dither, int offset)
  459. {
  460. yuv2p010lX_c(filter, filterSize, src, (uint16_t*)dest, dstW, 0);
  461. }
  462. static void yuv2p010lX_BE_c(const int16_t *filter, int filterSize,
  463. const int16_t **src, uint8_t *dest, int dstW,
  464. const uint8_t *dither, int offset)
  465. {
  466. yuv2p010lX_c(filter, filterSize, src, (uint16_t*)dest, dstW, 1);
  467. }
  468. #undef output_pixel
  469. #define accumulate_bit(acc, val) \
  470. acc <<= 1; \
  471. acc |= (val) >= 234
  472. #define output_pixel(pos, acc) \
  473. if (target == AV_PIX_FMT_MONOBLACK) { \
  474. pos = acc; \
  475. } else { \
  476. pos = ~acc; \
  477. }
  478. static av_always_inline void
  479. yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
  480. const int16_t **lumSrc, int lumFilterSize,
  481. const int16_t *chrFilter, const int16_t **chrUSrc,
  482. const int16_t **chrVSrc, int chrFilterSize,
  483. const int16_t **alpSrc, uint8_t *dest, int dstW,
  484. int y, enum AVPixelFormat target)
  485. {
  486. const uint8_t * const d128 = ff_dither_8x8_220[y&7];
  487. int i;
  488. unsigned acc = 0;
  489. int err = 0;
  490. for (i = 0; i < dstW; i += 2) {
  491. int j;
  492. int Y1 = 1 << 18;
  493. int Y2 = 1 << 18;
  494. for (j = 0; j < lumFilterSize; j++) {
  495. Y1 += lumSrc[j][i] * lumFilter[j];
  496. Y2 += lumSrc[j][i+1] * lumFilter[j];
  497. }
  498. Y1 >>= 19;
  499. Y2 >>= 19;
  500. if ((Y1 | Y2) & 0x100) {
  501. Y1 = av_clip_uint8(Y1);
  502. Y2 = av_clip_uint8(Y2);
  503. }
  504. if (c->dither == SWS_DITHER_ED) {
  505. Y1 += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
  506. c->dither_error[0][i] = err;
  507. acc = 2*acc + (Y1 >= 128);
  508. Y1 -= 220*(acc&1);
  509. err = Y2 + ((7*Y1 + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4);
  510. c->dither_error[0][i+1] = Y1;
  511. acc = 2*acc + (err >= 128);
  512. err -= 220*(acc&1);
  513. } else {
  514. accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
  515. accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
  516. }
  517. if ((i & 7) == 6) {
  518. output_pixel(*dest++, acc);
  519. }
  520. }
  521. c->dither_error[0][i] = err;
  522. if (i & 6) {
  523. output_pixel(*dest, acc);
  524. }
  525. }
  526. static av_always_inline void
  527. yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
  528. const int16_t *ubuf[2], const int16_t *vbuf[2],
  529. const int16_t *abuf[2], uint8_t *dest, int dstW,
  530. int yalpha, int uvalpha, int y,
  531. enum AVPixelFormat target)
  532. {
  533. const int16_t *buf0 = buf[0], *buf1 = buf[1];
  534. const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
  535. int yalpha1 = 4096 - yalpha;
  536. int i;
  537. av_assert2(yalpha <= 4096U);
  538. if (c->dither == SWS_DITHER_ED) {
  539. int err = 0;
  540. int acc = 0;
  541. for (i = 0; i < dstW; i +=2) {
  542. int Y;
  543. Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
  544. Y += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
  545. c->dither_error[0][i] = err;
  546. acc = 2*acc + (Y >= 128);
  547. Y -= 220*(acc&1);
  548. err = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
  549. err += (7*Y + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4;
  550. c->dither_error[0][i+1] = Y;
  551. acc = 2*acc + (err >= 128);
  552. err -= 220*(acc&1);
  553. if ((i & 7) == 6)
  554. output_pixel(*dest++, acc);
  555. }
  556. c->dither_error[0][i] = err;
  557. } else {
  558. for (i = 0; i < dstW; i += 8) {
  559. int Y, acc = 0;
  560. Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
  561. accumulate_bit(acc, Y + d128[0]);
  562. Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
  563. accumulate_bit(acc, Y + d128[1]);
  564. Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
  565. accumulate_bit(acc, Y + d128[2]);
  566. Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
  567. accumulate_bit(acc, Y + d128[3]);
  568. Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
  569. accumulate_bit(acc, Y + d128[4]);
  570. Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
  571. accumulate_bit(acc, Y + d128[5]);
  572. Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
  573. accumulate_bit(acc, Y + d128[6]);
  574. Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
  575. accumulate_bit(acc, Y + d128[7]);
  576. output_pixel(*dest++, acc);
  577. }
  578. }
  579. }
  580. static av_always_inline void
  581. yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
  582. const int16_t *ubuf[2], const int16_t *vbuf[2],
  583. const int16_t *abuf0, uint8_t *dest, int dstW,
  584. int uvalpha, int y, enum AVPixelFormat target)
  585. {
  586. const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
  587. int i;
  588. if (c->dither == SWS_DITHER_ED) {
  589. int err = 0;
  590. int acc = 0;
  591. for (i = 0; i < dstW; i +=2) {
  592. int Y;
  593. Y = ((buf0[i + 0] + 64) >> 7);
  594. Y += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
  595. c->dither_error[0][i] = err;
  596. acc = 2*acc + (Y >= 128);
  597. Y -= 220*(acc&1);
  598. err = ((buf0[i + 1] + 64) >> 7);
  599. err += (7*Y + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4;
  600. c->dither_error[0][i+1] = Y;
  601. acc = 2*acc + (err >= 128);
  602. err -= 220*(acc&1);
  603. if ((i & 7) == 6)
  604. output_pixel(*dest++, acc);
  605. }
  606. c->dither_error[0][i] = err;
  607. } else {
  608. for (i = 0; i < dstW; i += 8) {
  609. int acc = 0;
  610. accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
  611. accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
  612. accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
  613. accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
  614. accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
  615. accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
  616. accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
  617. accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
  618. output_pixel(*dest++, acc);
  619. }
  620. }
  621. }
  622. #undef output_pixel
  623. #undef accumulate_bit
  624. #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
  625. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  626. const int16_t **lumSrc, int lumFilterSize, \
  627. const int16_t *chrFilter, const int16_t **chrUSrc, \
  628. const int16_t **chrVSrc, int chrFilterSize, \
  629. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  630. int y) \
  631. { \
  632. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  633. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  634. alpSrc, dest, dstW, y, fmt); \
  635. } \
  636. \
  637. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  638. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  639. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  640. int yalpha, int uvalpha, int y) \
  641. { \
  642. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  643. dest, dstW, yalpha, uvalpha, y, fmt); \
  644. } \
  645. \
  646. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  647. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  648. const int16_t *abuf0, uint8_t *dest, int dstW, \
  649. int uvalpha, int y) \
  650. { \
  651. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
  652. abuf0, dest, dstW, uvalpha, \
  653. y, fmt); \
  654. }
  655. YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
  656. YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
  657. #define output_pixels(pos, Y1, U, Y2, V) \
  658. if (target == AV_PIX_FMT_YUYV422) { \
  659. dest[pos + 0] = Y1; \
  660. dest[pos + 1] = U; \
  661. dest[pos + 2] = Y2; \
  662. dest[pos + 3] = V; \
  663. } else if (target == AV_PIX_FMT_YVYU422) { \
  664. dest[pos + 0] = Y1; \
  665. dest[pos + 1] = V; \
  666. dest[pos + 2] = Y2; \
  667. dest[pos + 3] = U; \
  668. } else { /* AV_PIX_FMT_UYVY422 */ \
  669. dest[pos + 0] = U; \
  670. dest[pos + 1] = Y1; \
  671. dest[pos + 2] = V; \
  672. dest[pos + 3] = Y2; \
  673. }
  674. static av_always_inline void
  675. yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
  676. const int16_t **lumSrc, int lumFilterSize,
  677. const int16_t *chrFilter, const int16_t **chrUSrc,
  678. const int16_t **chrVSrc, int chrFilterSize,
  679. const int16_t **alpSrc, uint8_t *dest, int dstW,
  680. int y, enum AVPixelFormat target)
  681. {
  682. int i;
  683. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  684. int j;
  685. int Y1 = 1 << 18;
  686. int Y2 = 1 << 18;
  687. int U = 1 << 18;
  688. int V = 1 << 18;
  689. for (j = 0; j < lumFilterSize; j++) {
  690. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  691. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  692. }
  693. for (j = 0; j < chrFilterSize; j++) {
  694. U += chrUSrc[j][i] * chrFilter[j];
  695. V += chrVSrc[j][i] * chrFilter[j];
  696. }
  697. Y1 >>= 19;
  698. Y2 >>= 19;
  699. U >>= 19;
  700. V >>= 19;
  701. if ((Y1 | Y2 | U | V) & 0x100) {
  702. Y1 = av_clip_uint8(Y1);
  703. Y2 = av_clip_uint8(Y2);
  704. U = av_clip_uint8(U);
  705. V = av_clip_uint8(V);
  706. }
  707. output_pixels(4*i, Y1, U, Y2, V);
  708. }
  709. }
  710. static av_always_inline void
  711. yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
  712. const int16_t *ubuf[2], const int16_t *vbuf[2],
  713. const int16_t *abuf[2], uint8_t *dest, int dstW,
  714. int yalpha, int uvalpha, int y,
  715. enum AVPixelFormat target)
  716. {
  717. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  718. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  719. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  720. int yalpha1 = 4096 - yalpha;
  721. int uvalpha1 = 4096 - uvalpha;
  722. int i;
  723. av_assert2(yalpha <= 4096U);
  724. av_assert2(uvalpha <= 4096U);
  725. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  726. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  727. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  728. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  729. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  730. if ((Y1 | Y2 | U | V) & 0x100) {
  731. Y1 = av_clip_uint8(Y1);
  732. Y2 = av_clip_uint8(Y2);
  733. U = av_clip_uint8(U);
  734. V = av_clip_uint8(V);
  735. }
  736. output_pixels(i * 4, Y1, U, Y2, V);
  737. }
  738. }
  739. static av_always_inline void
  740. yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
  741. const int16_t *ubuf[2], const int16_t *vbuf[2],
  742. const int16_t *abuf0, uint8_t *dest, int dstW,
  743. int uvalpha, int y, enum AVPixelFormat target)
  744. {
  745. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  746. int i;
  747. if (uvalpha < 2048) {
  748. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  749. int Y1 = (buf0[i * 2 ]+64) >> 7;
  750. int Y2 = (buf0[i * 2 + 1]+64) >> 7;
  751. int U = (ubuf0[i] +64) >> 7;
  752. int V = (vbuf0[i] +64) >> 7;
  753. if ((Y1 | Y2 | U | V) & 0x100) {
  754. Y1 = av_clip_uint8(Y1);
  755. Y2 = av_clip_uint8(Y2);
  756. U = av_clip_uint8(U);
  757. V = av_clip_uint8(V);
  758. }
  759. output_pixels(i * 4, Y1, U, Y2, V);
  760. }
  761. } else {
  762. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  763. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  764. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  765. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  766. int U = (ubuf0[i] + ubuf1[i]+128) >> 8;
  767. int V = (vbuf0[i] + vbuf1[i]+128) >> 8;
  768. if ((Y1 | Y2 | U | V) & 0x100) {
  769. Y1 = av_clip_uint8(Y1);
  770. Y2 = av_clip_uint8(Y2);
  771. U = av_clip_uint8(U);
  772. V = av_clip_uint8(V);
  773. }
  774. output_pixels(i * 4, Y1, U, Y2, V);
  775. }
  776. }
  777. }
  778. #undef output_pixels
  779. YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
  780. YUV2PACKEDWRAPPER(yuv2, 422, yvyu422, AV_PIX_FMT_YVYU422)
  781. YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
  782. #define R_B ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE || target == AV_PIX_FMT_RGBA64LE || target == AV_PIX_FMT_RGBA64BE) ? R : B)
  783. #define B_R ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE || target == AV_PIX_FMT_RGBA64LE || target == AV_PIX_FMT_RGBA64BE) ? B : R)
  784. #define output_pixel(pos, val) \
  785. if (isBE(target)) { \
  786. AV_WB16(pos, val); \
  787. } else { \
  788. AV_WL16(pos, val); \
  789. }
  790. static av_always_inline void
  791. yuv2ya16_X_c_template(SwsContext *c, const int16_t *lumFilter,
  792. const int32_t **lumSrc, int lumFilterSize,
  793. const int16_t *chrFilter, const int32_t **unused_chrUSrc,
  794. const int32_t **unused_chrVSrc, int unused_chrFilterSize,
  795. const int32_t **alpSrc, uint16_t *dest, int dstW,
  796. int y, enum AVPixelFormat target, int unused_hasAlpha, int unused_eightbytes)
  797. {
  798. int hasAlpha = !!alpSrc;
  799. int i;
  800. for (i = 0; i < dstW; i++) {
  801. int j;
  802. int Y = -0x40000000;
  803. int A = 0xffff;
  804. for (j = 0; j < lumFilterSize; j++)
  805. Y += lumSrc[j][i] * lumFilter[j];
  806. Y >>= 15;
  807. Y += (1<<3) + 0x8000;
  808. Y = av_clip_uint16(Y);
  809. if (hasAlpha) {
  810. A = -0x40000000 + (1<<14);
  811. for (j = 0; j < lumFilterSize; j++)
  812. A += alpSrc[j][i] * lumFilter[j];
  813. A >>= 15;
  814. A += 0x8000;
  815. A = av_clip_uint16(A);
  816. }
  817. output_pixel(&dest[2 * i ], Y);
  818. output_pixel(&dest[2 * i + 1], A);
  819. }
  820. }
  821. static av_always_inline void
  822. yuv2ya16_2_c_template(SwsContext *c, const int32_t *buf[2],
  823. const int32_t *unused_ubuf[2], const int32_t *unused_vbuf[2],
  824. const int32_t *abuf[2], uint16_t *dest, int dstW,
  825. int yalpha, int unused_uvalpha, int y,
  826. enum AVPixelFormat target, int unused_hasAlpha, int unused_eightbytes)
  827. {
  828. int hasAlpha = abuf && abuf[0] && abuf[1];
  829. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  830. *abuf0 = hasAlpha ? abuf[0] : NULL,
  831. *abuf1 = hasAlpha ? abuf[1] : NULL;
  832. int yalpha1 = 4096 - yalpha;
  833. int i;
  834. av_assert2(yalpha <= 4096U);
  835. for (i = 0; i < dstW; i++) {
  836. int Y = (buf0[i] * yalpha1 + buf1[i] * yalpha) >> 15;
  837. int A;
  838. Y = av_clip_uint16(Y);
  839. if (hasAlpha) {
  840. A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 15;
  841. A = av_clip_uint16(A);
  842. }
  843. output_pixel(&dest[2 * i ], Y);
  844. output_pixel(&dest[2 * i + 1], hasAlpha ? A : 65535);
  845. }
  846. }
  847. static av_always_inline void
  848. yuv2ya16_1_c_template(SwsContext *c, const int32_t *buf0,
  849. const int32_t *unused_ubuf[2], const int32_t *unused_vbuf[2],
  850. const int32_t *abuf0, uint16_t *dest, int dstW,
  851. int unused_uvalpha, int y, enum AVPixelFormat target, int unused_hasAlpha, int unused_eightbytes)
  852. {
  853. int hasAlpha = !!abuf0;
  854. int i;
  855. for (i = 0; i < dstW; i++) {
  856. int Y = buf0[i] >> 3;/* 19 - 16 */
  857. int A;
  858. Y = av_clip_uint16(Y);
  859. if (hasAlpha) {
  860. A = abuf0[i] >> 3;
  861. if (A & 0x100)
  862. A = av_clip_uint16(A);
  863. }
  864. output_pixel(&dest[2 * i ], Y);
  865. output_pixel(&dest[2 * i + 1], hasAlpha ? A : 65535);
  866. }
  867. }
  868. static av_always_inline void
  869. yuv2rgba64_X_c_template(SwsContext *c, const int16_t *lumFilter,
  870. const int32_t **lumSrc, int lumFilterSize,
  871. const int16_t *chrFilter, const int32_t **chrUSrc,
  872. const int32_t **chrVSrc, int chrFilterSize,
  873. const int32_t **alpSrc, uint16_t *dest, int dstW,
  874. int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
  875. {
  876. int i;
  877. int A1 = 0xffff<<14, A2 = 0xffff<<14;
  878. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  879. int j;
  880. int Y1 = -0x40000000;
  881. int Y2 = -0x40000000;
  882. int U = -(128 << 23); // 19
  883. int V = -(128 << 23);
  884. int R, G, B;
  885. for (j = 0; j < lumFilterSize; j++) {
  886. Y1 += lumSrc[j][i * 2] * (unsigned)lumFilter[j];
  887. Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
  888. }
  889. for (j = 0; j < chrFilterSize; j++) {;
  890. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  891. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  892. }
  893. if (hasAlpha) {
  894. A1 = -0x40000000;
  895. A2 = -0x40000000;
  896. for (j = 0; j < lumFilterSize; j++) {
  897. A1 += alpSrc[j][i * 2] * (unsigned)lumFilter[j];
  898. A2 += alpSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
  899. }
  900. A1 >>= 1;
  901. A1 += 0x20002000;
  902. A2 >>= 1;
  903. A2 += 0x20002000;
  904. }
  905. // 8 bits: 12+15=27; 16 bits: 12+19=31
  906. Y1 >>= 14; // 10
  907. Y1 += 0x10000;
  908. Y2 >>= 14;
  909. Y2 += 0x10000;
  910. U >>= 14;
  911. V >>= 14;
  912. // 8 bits: 27 -> 17 bits, 16 bits: 31 - 14 = 17 bits
  913. Y1 -= c->yuv2rgb_y_offset;
  914. Y2 -= c->yuv2rgb_y_offset;
  915. Y1 *= c->yuv2rgb_y_coeff;
  916. Y2 *= c->yuv2rgb_y_coeff;
  917. Y1 += 1 << 13; // 21
  918. Y2 += 1 << 13;
  919. // 8 bits: 17 + 13 bits = 30 bits, 16 bits: 17 + 13 bits = 30 bits
  920. R = V * c->yuv2rgb_v2r_coeff;
  921. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  922. B = U * c->yuv2rgb_u2b_coeff;
  923. // 8 bits: 30 - 22 = 8 bits, 16 bits: 30 bits - 14 = 16 bits
  924. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  925. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  926. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  927. if (eightbytes) {
  928. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  929. output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
  930. output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
  931. output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
  932. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  933. dest += 8;
  934. } else {
  935. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  936. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  937. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  938. dest += 6;
  939. }
  940. }
  941. }
  942. static av_always_inline void
  943. yuv2rgba64_2_c_template(SwsContext *c, const int32_t *buf[2],
  944. const int32_t *ubuf[2], const int32_t *vbuf[2],
  945. const int32_t *abuf[2], uint16_t *dest, int dstW,
  946. int yalpha, int uvalpha, int y,
  947. enum AVPixelFormat target, int hasAlpha, int eightbytes)
  948. {
  949. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  950. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  951. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  952. *abuf0 = hasAlpha ? abuf[0] : NULL,
  953. *abuf1 = hasAlpha ? abuf[1] : NULL;
  954. int yalpha1 = 4096 - yalpha;
  955. int uvalpha1 = 4096 - uvalpha;
  956. int i;
  957. int A1 = 0xffff<<14, A2 = 0xffff<<14;
  958. av_assert2(yalpha <= 4096U);
  959. av_assert2(uvalpha <= 4096U);
  960. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  961. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
  962. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
  963. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha - (128 << 23)) >> 14;
  964. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha - (128 << 23)) >> 14;
  965. int R, G, B;
  966. Y1 -= c->yuv2rgb_y_offset;
  967. Y2 -= c->yuv2rgb_y_offset;
  968. Y1 *= c->yuv2rgb_y_coeff;
  969. Y2 *= c->yuv2rgb_y_coeff;
  970. Y1 += 1 << 13;
  971. Y2 += 1 << 13;
  972. R = V * c->yuv2rgb_v2r_coeff;
  973. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  974. B = U * c->yuv2rgb_u2b_coeff;
  975. if (hasAlpha) {
  976. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 1;
  977. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 1;
  978. A1 += 1 << 13;
  979. A2 += 1 << 13;
  980. }
  981. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  982. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  983. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  984. if (eightbytes) {
  985. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  986. output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
  987. output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
  988. output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
  989. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  990. dest += 8;
  991. } else {
  992. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  993. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  994. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  995. dest += 6;
  996. }
  997. }
  998. }
  999. static av_always_inline void
  1000. yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0,
  1001. const int32_t *ubuf[2], const int32_t *vbuf[2],
  1002. const int32_t *abuf0, uint16_t *dest, int dstW,
  1003. int uvalpha, int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
  1004. {
  1005. const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1006. int i;
  1007. int A1 = 0xffff<<14, A2= 0xffff<<14;
  1008. if (uvalpha < 2048) {
  1009. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1010. int Y1 = (buf0[i * 2] ) >> 2;
  1011. int Y2 = (buf0[i * 2 + 1]) >> 2;
  1012. int U = (ubuf0[i] - (128 << 11)) >> 2;
  1013. int V = (vbuf0[i] - (128 << 11)) >> 2;
  1014. int R, G, B;
  1015. Y1 -= c->yuv2rgb_y_offset;
  1016. Y2 -= c->yuv2rgb_y_offset;
  1017. Y1 *= c->yuv2rgb_y_coeff;
  1018. Y2 *= c->yuv2rgb_y_coeff;
  1019. Y1 += 1 << 13;
  1020. Y2 += 1 << 13;
  1021. if (hasAlpha) {
  1022. A1 = abuf0[i * 2 ] << 11;
  1023. A2 = abuf0[i * 2 + 1] << 11;
  1024. A1 += 1 << 13;
  1025. A2 += 1 << 13;
  1026. }
  1027. R = V * c->yuv2rgb_v2r_coeff;
  1028. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1029. B = U * c->yuv2rgb_u2b_coeff;
  1030. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  1031. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  1032. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  1033. if (eightbytes) {
  1034. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  1035. output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
  1036. output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
  1037. output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
  1038. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  1039. dest += 8;
  1040. } else {
  1041. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  1042. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  1043. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  1044. dest += 6;
  1045. }
  1046. }
  1047. } else {
  1048. const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1049. int A1 = 0xffff<<14, A2 = 0xffff<<14;
  1050. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1051. int Y1 = (buf0[i * 2] ) >> 2;
  1052. int Y2 = (buf0[i * 2 + 1]) >> 2;
  1053. int U = (ubuf0[i] + ubuf1[i] - (128 << 12)) >> 3;
  1054. int V = (vbuf0[i] + vbuf1[i] - (128 << 12)) >> 3;
  1055. int R, G, B;
  1056. Y1 -= c->yuv2rgb_y_offset;
  1057. Y2 -= c->yuv2rgb_y_offset;
  1058. Y1 *= c->yuv2rgb_y_coeff;
  1059. Y2 *= c->yuv2rgb_y_coeff;
  1060. Y1 += 1 << 13;
  1061. Y2 += 1 << 13;
  1062. if (hasAlpha) {
  1063. A1 = abuf0[i * 2 ] << 11;
  1064. A2 = abuf0[i * 2 + 1] << 11;
  1065. A1 += 1 << 13;
  1066. A2 += 1 << 13;
  1067. }
  1068. R = V * c->yuv2rgb_v2r_coeff;
  1069. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1070. B = U * c->yuv2rgb_u2b_coeff;
  1071. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  1072. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  1073. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  1074. if (eightbytes) {
  1075. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  1076. output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
  1077. output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
  1078. output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
  1079. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  1080. dest += 8;
  1081. } else {
  1082. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  1083. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  1084. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  1085. dest += 6;
  1086. }
  1087. }
  1088. }
  1089. }
  1090. static av_always_inline void
  1091. yuv2rgba64_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1092. const int32_t **lumSrc, int lumFilterSize,
  1093. const int16_t *chrFilter, const int32_t **chrUSrc,
  1094. const int32_t **chrVSrc, int chrFilterSize,
  1095. const int32_t **alpSrc, uint16_t *dest, int dstW,
  1096. int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
  1097. {
  1098. int i;
  1099. int A = 0xffff<<14;
  1100. for (i = 0; i < dstW; i++) {
  1101. int j;
  1102. int Y = -0x40000000;
  1103. int U = -(128 << 23); // 19
  1104. int V = -(128 << 23);
  1105. int R, G, B;
  1106. for (j = 0; j < lumFilterSize; j++) {
  1107. Y += lumSrc[j][i] * (unsigned)lumFilter[j];
  1108. }
  1109. for (j = 0; j < chrFilterSize; j++) {;
  1110. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  1111. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  1112. }
  1113. if (hasAlpha) {
  1114. A = -0x40000000;
  1115. for (j = 0; j < lumFilterSize; j++) {
  1116. A += alpSrc[j][i] * (unsigned)lumFilter[j];
  1117. }
  1118. A >>= 1;
  1119. A += 0x20002000;
  1120. }
  1121. // 8bit: 12+15=27; 16-bit: 12+19=31
  1122. Y >>= 14; // 10
  1123. Y += 0x10000;
  1124. U >>= 14;
  1125. V >>= 14;
  1126. // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
  1127. Y -= c->yuv2rgb_y_offset;
  1128. Y *= c->yuv2rgb_y_coeff;
  1129. Y += 1 << 13; // 21
  1130. // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
  1131. R = V * c->yuv2rgb_v2r_coeff;
  1132. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1133. B = U * c->yuv2rgb_u2b_coeff;
  1134. // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
  1135. output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
  1136. output_pixel(&dest[1], av_clip_uintp2( G + Y, 30) >> 14);
  1137. output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
  1138. if (eightbytes) {
  1139. output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
  1140. dest += 4;
  1141. } else {
  1142. dest += 3;
  1143. }
  1144. }
  1145. }
  1146. static av_always_inline void
  1147. yuv2rgba64_full_2_c_template(SwsContext *c, const int32_t *buf[2],
  1148. const int32_t *ubuf[2], const int32_t *vbuf[2],
  1149. const int32_t *abuf[2], uint16_t *dest, int dstW,
  1150. int yalpha, int uvalpha, int y,
  1151. enum AVPixelFormat target, int hasAlpha, int eightbytes)
  1152. {
  1153. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  1154. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1155. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1156. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1157. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1158. int yalpha1 = 4096 - yalpha;
  1159. int uvalpha1 = 4096 - uvalpha;
  1160. int i;
  1161. int A = 0xffff<<14;
  1162. av_assert2(yalpha <= 4096U);
  1163. av_assert2(uvalpha <= 4096U);
  1164. for (i = 0; i < dstW; i++) {
  1165. int Y = (buf0[i] * yalpha1 + buf1[i] * yalpha) >> 14;
  1166. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha - (128 << 23)) >> 14;
  1167. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha - (128 << 23)) >> 14;
  1168. int R, G, B;
  1169. Y -= c->yuv2rgb_y_offset;
  1170. Y *= c->yuv2rgb_y_coeff;
  1171. Y += 1 << 13;
  1172. R = V * c->yuv2rgb_v2r_coeff;
  1173. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1174. B = U * c->yuv2rgb_u2b_coeff;
  1175. if (hasAlpha) {
  1176. A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 1;
  1177. A += 1 << 13;
  1178. }
  1179. output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
  1180. output_pixel(&dest[1], av_clip_uintp2( G + Y, 30) >> 14);
  1181. output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
  1182. if (eightbytes) {
  1183. output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
  1184. dest += 4;
  1185. } else {
  1186. dest += 3;
  1187. }
  1188. }
  1189. }
  1190. static av_always_inline void
  1191. yuv2rgba64_full_1_c_template(SwsContext *c, const int32_t *buf0,
  1192. const int32_t *ubuf[2], const int32_t *vbuf[2],
  1193. const int32_t *abuf0, uint16_t *dest, int dstW,
  1194. int uvalpha, int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
  1195. {
  1196. const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1197. int i;
  1198. int A = 0xffff<<14;
  1199. if (uvalpha < 2048) {
  1200. for (i = 0; i < dstW; i++) {
  1201. int Y = (buf0[i]) >> 2;
  1202. int U = (ubuf0[i] - (128 << 11)) >> 2;
  1203. int V = (vbuf0[i] - (128 << 11)) >> 2;
  1204. int R, G, B;
  1205. Y -= c->yuv2rgb_y_offset;
  1206. Y *= c->yuv2rgb_y_coeff;
  1207. Y += 1 << 13;
  1208. if (hasAlpha) {
  1209. A = abuf0[i] << 11;
  1210. A += 1 << 13;
  1211. }
  1212. R = V * c->yuv2rgb_v2r_coeff;
  1213. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1214. B = U * c->yuv2rgb_u2b_coeff;
  1215. output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
  1216. output_pixel(&dest[1], av_clip_uintp2( G + Y, 30) >> 14);
  1217. output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
  1218. if (eightbytes) {
  1219. output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
  1220. dest += 4;
  1221. } else {
  1222. dest += 3;
  1223. }
  1224. }
  1225. } else {
  1226. const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1227. int A = 0xffff<<14;
  1228. for (i = 0; i < dstW; i++) {
  1229. int Y = (buf0[i] ) >> 2;
  1230. int U = (ubuf0[i] + ubuf1[i] - (128 << 12)) >> 3;
  1231. int V = (vbuf0[i] + vbuf1[i] - (128 << 12)) >> 3;
  1232. int R, G, B;
  1233. Y -= c->yuv2rgb_y_offset;
  1234. Y *= c->yuv2rgb_y_coeff;
  1235. Y += 1 << 13;
  1236. if (hasAlpha) {
  1237. A = abuf0[i] << 11;
  1238. A += 1 << 13;
  1239. }
  1240. R = V * c->yuv2rgb_v2r_coeff;
  1241. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1242. B = U * c->yuv2rgb_u2b_coeff;
  1243. output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
  1244. output_pixel(&dest[1], av_clip_uintp2( G + Y, 30) >> 14);
  1245. output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
  1246. if (eightbytes) {
  1247. output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
  1248. dest += 4;
  1249. } else {
  1250. dest += 3;
  1251. }
  1252. }
  1253. }
  1254. }
  1255. #undef output_pixel
  1256. #undef r_b
  1257. #undef b_r
  1258. #define YUV2PACKED16WRAPPER(name, base, ext, fmt, hasAlpha, eightbytes) \
  1259. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  1260. const int16_t **_lumSrc, int lumFilterSize, \
  1261. const int16_t *chrFilter, const int16_t **_chrUSrc, \
  1262. const int16_t **_chrVSrc, int chrFilterSize, \
  1263. const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
  1264. int y) \
  1265. { \
  1266. const int32_t **lumSrc = (const int32_t **) _lumSrc, \
  1267. **chrUSrc = (const int32_t **) _chrUSrc, \
  1268. **chrVSrc = (const int32_t **) _chrVSrc, \
  1269. **alpSrc = (const int32_t **) _alpSrc; \
  1270. uint16_t *dest = (uint16_t *) _dest; \
  1271. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  1272. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  1273. alpSrc, dest, dstW, y, fmt, hasAlpha, eightbytes); \
  1274. } \
  1275. \
  1276. static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
  1277. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  1278. const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
  1279. int yalpha, int uvalpha, int y) \
  1280. { \
  1281. const int32_t **buf = (const int32_t **) _buf, \
  1282. **ubuf = (const int32_t **) _ubuf, \
  1283. **vbuf = (const int32_t **) _vbuf, \
  1284. **abuf = (const int32_t **) _abuf; \
  1285. uint16_t *dest = (uint16_t *) _dest; \
  1286. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  1287. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha, eightbytes); \
  1288. } \
  1289. \
  1290. static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
  1291. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  1292. const int16_t *_abuf0, uint8_t *_dest, int dstW, \
  1293. int uvalpha, int y) \
  1294. { \
  1295. const int32_t *buf0 = (const int32_t *) _buf0, \
  1296. **ubuf = (const int32_t **) _ubuf, \
  1297. **vbuf = (const int32_t **) _vbuf, \
  1298. *abuf0 = (const int32_t *) _abuf0; \
  1299. uint16_t *dest = (uint16_t *) _dest; \
  1300. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  1301. dstW, uvalpha, y, fmt, hasAlpha, eightbytes); \
  1302. }
  1303. YUV2PACKED16WRAPPER(yuv2, rgba64, rgb48be, AV_PIX_FMT_RGB48BE, 0, 0)
  1304. YUV2PACKED16WRAPPER(yuv2, rgba64, rgb48le, AV_PIX_FMT_RGB48LE, 0, 0)
  1305. YUV2PACKED16WRAPPER(yuv2, rgba64, bgr48be, AV_PIX_FMT_BGR48BE, 0, 0)
  1306. YUV2PACKED16WRAPPER(yuv2, rgba64, bgr48le, AV_PIX_FMT_BGR48LE, 0, 0)
  1307. YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64be, AV_PIX_FMT_RGBA64BE, 1, 1)
  1308. YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64le, AV_PIX_FMT_RGBA64LE, 1, 1)
  1309. YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64be, AV_PIX_FMT_RGBA64BE, 0, 1)
  1310. YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64le, AV_PIX_FMT_RGBA64LE, 0, 1)
  1311. YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64be, AV_PIX_FMT_BGRA64BE, 1, 1)
  1312. YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64le, AV_PIX_FMT_BGRA64LE, 1, 1)
  1313. YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64be, AV_PIX_FMT_BGRA64BE, 0, 1)
  1314. YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64le, AV_PIX_FMT_BGRA64LE, 0, 1)
  1315. YUV2PACKED16WRAPPER(yuv2, ya16, ya16be, AV_PIX_FMT_YA16BE, 1, 0)
  1316. YUV2PACKED16WRAPPER(yuv2, ya16, ya16le, AV_PIX_FMT_YA16LE, 1, 0)
  1317. YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgb48be_full, AV_PIX_FMT_RGB48BE, 0, 0)
  1318. YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgb48le_full, AV_PIX_FMT_RGB48LE, 0, 0)
  1319. YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgr48be_full, AV_PIX_FMT_BGR48BE, 0, 0)
  1320. YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgr48le_full, AV_PIX_FMT_BGR48LE, 0, 0)
  1321. YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgba64be_full, AV_PIX_FMT_RGBA64BE, 1, 1)
  1322. YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgba64le_full, AV_PIX_FMT_RGBA64LE, 1, 1)
  1323. YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgbx64be_full, AV_PIX_FMT_RGBA64BE, 0, 1)
  1324. YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgbx64le_full, AV_PIX_FMT_RGBA64LE, 0, 1)
  1325. YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgra64be_full, AV_PIX_FMT_BGRA64BE, 1, 1)
  1326. YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgra64le_full, AV_PIX_FMT_BGRA64LE, 1, 1)
  1327. YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgrx64be_full, AV_PIX_FMT_BGRA64BE, 0, 1)
  1328. YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgrx64le_full, AV_PIX_FMT_BGRA64LE, 0, 1)
  1329. /*
  1330. * Write out 2 RGB pixels in the target pixel format. This function takes a
  1331. * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
  1332. * things like endianness conversion and shifting. The caller takes care of
  1333. * setting the correct offset in these tables from the chroma (U/V) values.
  1334. * This function then uses the luminance (Y1/Y2) values to write out the
  1335. * correct RGB values into the destination buffer.
  1336. */
  1337. static av_always_inline void
  1338. yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
  1339. unsigned A1, unsigned A2,
  1340. const void *_r, const void *_g, const void *_b, int y,
  1341. enum AVPixelFormat target, int hasAlpha)
  1342. {
  1343. if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
  1344. target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
  1345. uint32_t *dest = (uint32_t *) _dest;
  1346. const uint32_t *r = (const uint32_t *) _r;
  1347. const uint32_t *g = (const uint32_t *) _g;
  1348. const uint32_t *b = (const uint32_t *) _b;
  1349. #if CONFIG_SMALL
  1350. int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
  1351. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
  1352. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
  1353. #else
  1354. if (hasAlpha) {
  1355. int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
  1356. av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0);
  1357. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
  1358. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
  1359. } else {
  1360. #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
  1361. int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
  1362. av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0xFF);
  1363. #endif
  1364. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
  1365. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
  1366. }
  1367. #endif
  1368. } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
  1369. uint8_t *dest = (uint8_t *) _dest;
  1370. const uint8_t *r = (const uint8_t *) _r;
  1371. const uint8_t *g = (const uint8_t *) _g;
  1372. const uint8_t *b = (const uint8_t *) _b;
  1373. #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
  1374. #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
  1375. dest[i * 6 + 0] = r_b[Y1];
  1376. dest[i * 6 + 1] = g[Y1];
  1377. dest[i * 6 + 2] = b_r[Y1];
  1378. dest[i * 6 + 3] = r_b[Y2];
  1379. dest[i * 6 + 4] = g[Y2];
  1380. dest[i * 6 + 5] = b_r[Y2];
  1381. #undef r_b
  1382. #undef b_r
  1383. } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
  1384. target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
  1385. target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
  1386. uint16_t *dest = (uint16_t *) _dest;
  1387. const uint16_t *r = (const uint16_t *) _r;
  1388. const uint16_t *g = (const uint16_t *) _g;
  1389. const uint16_t *b = (const uint16_t *) _b;
  1390. int dr1, dg1, db1, dr2, dg2, db2;
  1391. if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
  1392. dr1 = ff_dither_2x2_8[ y & 1 ][0];
  1393. dg1 = ff_dither_2x2_4[ y & 1 ][0];
  1394. db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
  1395. dr2 = ff_dither_2x2_8[ y & 1 ][1];
  1396. dg2 = ff_dither_2x2_4[ y & 1 ][1];
  1397. db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
  1398. } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
  1399. dr1 = ff_dither_2x2_8[ y & 1 ][0];
  1400. dg1 = ff_dither_2x2_8[ y & 1 ][1];
  1401. db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
  1402. dr2 = ff_dither_2x2_8[ y & 1 ][1];
  1403. dg2 = ff_dither_2x2_8[ y & 1 ][0];
  1404. db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
  1405. } else {
  1406. dr1 = ff_dither_4x4_16[ y & 3 ][0];
  1407. dg1 = ff_dither_4x4_16[ y & 3 ][1];
  1408. db1 = ff_dither_4x4_16[(y & 3) ^ 3][0];
  1409. dr2 = ff_dither_4x4_16[ y & 3 ][1];
  1410. dg2 = ff_dither_4x4_16[ y & 3 ][0];
  1411. db2 = ff_dither_4x4_16[(y & 3) ^ 3][1];
  1412. }
  1413. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1414. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1415. } else if (target == AV_PIX_FMT_X2RGB10) {
  1416. uint32_t *dest = (uint32_t *) _dest;
  1417. const uint32_t *r = (const uint32_t *) _r;
  1418. const uint32_t *g = (const uint32_t *) _g;
  1419. const uint32_t *b = (const uint32_t *) _b;
  1420. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
  1421. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
  1422. } else /* 8/4 bits */ {
  1423. uint8_t *dest = (uint8_t *) _dest;
  1424. const uint8_t *r = (const uint8_t *) _r;
  1425. const uint8_t *g = (const uint8_t *) _g;
  1426. const uint8_t *b = (const uint8_t *) _b;
  1427. int dr1, dg1, db1, dr2, dg2, db2;
  1428. if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
  1429. const uint8_t * const d64 = ff_dither_8x8_73[y & 7];
  1430. const uint8_t * const d32 = ff_dither_8x8_32[y & 7];
  1431. dr1 = dg1 = d32[(i * 2 + 0) & 7];
  1432. db1 = d64[(i * 2 + 0) & 7];
  1433. dr2 = dg2 = d32[(i * 2 + 1) & 7];
  1434. db2 = d64[(i * 2 + 1) & 7];
  1435. } else {
  1436. const uint8_t * const d64 = ff_dither_8x8_73 [y & 7];
  1437. const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
  1438. dr1 = db1 = d128[(i * 2 + 0) & 7];
  1439. dg1 = d64[(i * 2 + 0) & 7];
  1440. dr2 = db2 = d128[(i * 2 + 1) & 7];
  1441. dg2 = d64[(i * 2 + 1) & 7];
  1442. }
  1443. if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
  1444. dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
  1445. ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
  1446. } else {
  1447. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1448. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1449. }
  1450. }
  1451. }
  1452. static av_always_inline void
  1453. yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1454. const int16_t **lumSrc, int lumFilterSize,
  1455. const int16_t *chrFilter, const int16_t **chrUSrc,
  1456. const int16_t **chrVSrc, int chrFilterSize,
  1457. const int16_t **alpSrc, uint8_t *dest, int dstW,
  1458. int y, enum AVPixelFormat target, int hasAlpha)
  1459. {
  1460. int i;
  1461. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1462. int j, A1, A2;
  1463. int Y1 = 1 << 18;
  1464. int Y2 = 1 << 18;
  1465. int U = 1 << 18;
  1466. int V = 1 << 18;
  1467. const void *r, *g, *b;
  1468. for (j = 0; j < lumFilterSize; j++) {
  1469. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  1470. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  1471. }
  1472. for (j = 0; j < chrFilterSize; j++) {
  1473. U += chrUSrc[j][i] * chrFilter[j];
  1474. V += chrVSrc[j][i] * chrFilter[j];
  1475. }
  1476. Y1 >>= 19;
  1477. Y2 >>= 19;
  1478. U >>= 19;
  1479. V >>= 19;
  1480. if (hasAlpha) {
  1481. A1 = 1 << 18;
  1482. A2 = 1 << 18;
  1483. for (j = 0; j < lumFilterSize; j++) {
  1484. A1 += alpSrc[j][i * 2 ] * lumFilter[j];
  1485. A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
  1486. }
  1487. A1 >>= 19;
  1488. A2 >>= 19;
  1489. if ((A1 | A2) & 0x100) {
  1490. A1 = av_clip_uint8(A1);
  1491. A2 = av_clip_uint8(A2);
  1492. }
  1493. }
  1494. r = c->table_rV[V + YUVRGB_TABLE_HEADROOM];
  1495. g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
  1496. b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1497. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1498. r, g, b, y, target, hasAlpha);
  1499. }
  1500. }
  1501. static av_always_inline void
  1502. yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
  1503. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1504. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1505. int yalpha, int uvalpha, int y,
  1506. enum AVPixelFormat target, int hasAlpha)
  1507. {
  1508. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1509. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1510. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1511. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1512. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1513. int yalpha1 = 4096 - yalpha;
  1514. int uvalpha1 = 4096 - uvalpha;
  1515. int i;
  1516. av_assert2(yalpha <= 4096U);
  1517. av_assert2(uvalpha <= 4096U);
  1518. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1519. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  1520. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  1521. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  1522. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  1523. int A1, A2;
  1524. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1525. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1526. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1527. if (hasAlpha) {
  1528. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
  1529. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
  1530. A1 = av_clip_uint8(A1);
  1531. A2 = av_clip_uint8(A2);
  1532. }
  1533. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1534. r, g, b, y, target, hasAlpha);
  1535. }
  1536. }
  1537. static av_always_inline void
  1538. yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
  1539. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1540. const int16_t *abuf0, uint8_t *dest, int dstW,
  1541. int uvalpha, int y, enum AVPixelFormat target,
  1542. int hasAlpha)
  1543. {
  1544. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1545. int i;
  1546. if (uvalpha < 2048) {
  1547. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1548. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  1549. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  1550. int U = (ubuf0[i] + 64) >> 7;
  1551. int V = (vbuf0[i] + 64) >> 7;
  1552. int A1, A2;
  1553. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1554. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1555. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1556. if (hasAlpha) {
  1557. A1 = abuf0[i * 2 ] * 255 + 16384 >> 15;
  1558. A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
  1559. A1 = av_clip_uint8(A1);
  1560. A2 = av_clip_uint8(A2);
  1561. }
  1562. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1563. r, g, b, y, target, hasAlpha);
  1564. }
  1565. } else {
  1566. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1567. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1568. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  1569. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  1570. int U = (ubuf0[i] + ubuf1[i] + 128) >> 8;
  1571. int V = (vbuf0[i] + vbuf1[i] + 128) >> 8;
  1572. int A1, A2;
  1573. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1574. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1575. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1576. if (hasAlpha) {
  1577. A1 = (abuf0[i * 2 ] + 64) >> 7;
  1578. A2 = (abuf0[i * 2 + 1] + 64) >> 7;
  1579. A1 = av_clip_uint8(A1);
  1580. A2 = av_clip_uint8(A2);
  1581. }
  1582. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1583. r, g, b, y, target, hasAlpha);
  1584. }
  1585. }
  1586. }
  1587. #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1588. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  1589. const int16_t **lumSrc, int lumFilterSize, \
  1590. const int16_t *chrFilter, const int16_t **chrUSrc, \
  1591. const int16_t **chrVSrc, int chrFilterSize, \
  1592. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  1593. int y) \
  1594. { \
  1595. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  1596. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  1597. alpSrc, dest, dstW, y, fmt, hasAlpha); \
  1598. }
  1599. #define YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
  1600. YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1601. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  1602. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1603. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  1604. int yalpha, int uvalpha, int y) \
  1605. { \
  1606. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  1607. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
  1608. }
  1609. #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
  1610. YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
  1611. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  1612. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1613. const int16_t *abuf0, uint8_t *dest, int dstW, \
  1614. int uvalpha, int y) \
  1615. { \
  1616. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  1617. dstW, uvalpha, y, fmt, hasAlpha); \
  1618. }
  1619. #if CONFIG_SMALL
  1620. YUV2RGBWRAPPER(yuv2rgb,, 32_1, AV_PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->needAlpha)
  1621. YUV2RGBWRAPPER(yuv2rgb,, 32, AV_PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->needAlpha)
  1622. #else
  1623. #if CONFIG_SWSCALE_ALPHA
  1624. YUV2RGBWRAPPER(yuv2rgb,, a32_1, AV_PIX_FMT_RGB32_1, 1)
  1625. YUV2RGBWRAPPER(yuv2rgb,, a32, AV_PIX_FMT_RGB32, 1)
  1626. #endif
  1627. YUV2RGBWRAPPER(yuv2rgb,, x32_1, AV_PIX_FMT_RGB32_1, 0)
  1628. YUV2RGBWRAPPER(yuv2rgb,, x32, AV_PIX_FMT_RGB32, 0)
  1629. #endif
  1630. YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0)
  1631. YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0)
  1632. YUV2RGBWRAPPER(yuv2rgb,, 16, AV_PIX_FMT_RGB565, 0)
  1633. YUV2RGBWRAPPER(yuv2rgb,, 15, AV_PIX_FMT_RGB555, 0)
  1634. YUV2RGBWRAPPER(yuv2rgb,, 12, AV_PIX_FMT_RGB444, 0)
  1635. YUV2RGBWRAPPER(yuv2rgb,, 8, AV_PIX_FMT_RGB8, 0)
  1636. YUV2RGBWRAPPER(yuv2rgb,, 4, AV_PIX_FMT_RGB4, 0)
  1637. YUV2RGBWRAPPER(yuv2rgb,, 4b, AV_PIX_FMT_RGB4_BYTE, 0)
  1638. YUV2RGBWRAPPER(yuv2, rgb, x2rgb10, AV_PIX_FMT_X2RGB10, 0)
  1639. static av_always_inline void yuv2rgb_write_full(SwsContext *c,
  1640. uint8_t *dest, int i, int Y, int A, int U, int V,
  1641. int y, enum AVPixelFormat target, int hasAlpha, int err[4])
  1642. {
  1643. int R, G, B;
  1644. int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
  1645. Y -= c->yuv2rgb_y_offset;
  1646. Y *= c->yuv2rgb_y_coeff;
  1647. Y += 1 << 21;
  1648. R = (unsigned)Y + V*c->yuv2rgb_v2r_coeff;
  1649. G = (unsigned)Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
  1650. B = (unsigned)Y + U*c->yuv2rgb_u2b_coeff;
  1651. if ((R | G | B) & 0xC0000000) {
  1652. R = av_clip_uintp2(R, 30);
  1653. G = av_clip_uintp2(G, 30);
  1654. B = av_clip_uintp2(B, 30);
  1655. }
  1656. switch(target) {
  1657. case AV_PIX_FMT_ARGB:
  1658. dest[0] = hasAlpha ? A : 255;
  1659. dest[1] = R >> 22;
  1660. dest[2] = G >> 22;
  1661. dest[3] = B >> 22;
  1662. break;
  1663. case AV_PIX_FMT_RGB24:
  1664. dest[0] = R >> 22;
  1665. dest[1] = G >> 22;
  1666. dest[2] = B >> 22;
  1667. break;
  1668. case AV_PIX_FMT_RGBA:
  1669. dest[0] = R >> 22;
  1670. dest[1] = G >> 22;
  1671. dest[2] = B >> 22;
  1672. dest[3] = hasAlpha ? A : 255;
  1673. break;
  1674. case AV_PIX_FMT_ABGR:
  1675. dest[0] = hasAlpha ? A : 255;
  1676. dest[1] = B >> 22;
  1677. dest[2] = G >> 22;
  1678. dest[3] = R >> 22;
  1679. break;
  1680. case AV_PIX_FMT_BGR24:
  1681. dest[0] = B >> 22;
  1682. dest[1] = G >> 22;
  1683. dest[2] = R >> 22;
  1684. break;
  1685. case AV_PIX_FMT_BGRA:
  1686. dest[0] = B >> 22;
  1687. dest[1] = G >> 22;
  1688. dest[2] = R >> 22;
  1689. dest[3] = hasAlpha ? A : 255;
  1690. break;
  1691. case AV_PIX_FMT_BGR4_BYTE:
  1692. case AV_PIX_FMT_RGB4_BYTE:
  1693. case AV_PIX_FMT_BGR8:
  1694. case AV_PIX_FMT_RGB8:
  1695. {
  1696. int r,g,b;
  1697. switch (c->dither) {
  1698. default:
  1699. case SWS_DITHER_AUTO:
  1700. case SWS_DITHER_ED:
  1701. R >>= 22;
  1702. G >>= 22;
  1703. B >>= 22;
  1704. R += (7*err[0] + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2])>>4;
  1705. G += (7*err[1] + 1*c->dither_error[1][i] + 5*c->dither_error[1][i+1] + 3*c->dither_error[1][i+2])>>4;
  1706. B += (7*err[2] + 1*c->dither_error[2][i] + 5*c->dither_error[2][i+1] + 3*c->dither_error[2][i+2])>>4;
  1707. c->dither_error[0][i] = err[0];
  1708. c->dither_error[1][i] = err[1];
  1709. c->dither_error[2][i] = err[2];
  1710. r = R >> (isrgb8 ? 5 : 7);
  1711. g = G >> (isrgb8 ? 5 : 6);
  1712. b = B >> (isrgb8 ? 6 : 7);
  1713. r = av_clip(r, 0, isrgb8 ? 7 : 1);
  1714. g = av_clip(g, 0, isrgb8 ? 7 : 3);
  1715. b = av_clip(b, 0, isrgb8 ? 3 : 1);
  1716. err[0] = R - r*(isrgb8 ? 36 : 255);
  1717. err[1] = G - g*(isrgb8 ? 36 : 85);
  1718. err[2] = B - b*(isrgb8 ? 85 : 255);
  1719. break;
  1720. case SWS_DITHER_A_DITHER:
  1721. if (isrgb8) {
  1722. /* see http://pippin.gimp.org/a_dither/ for details/origin */
  1723. #define A_DITHER(u,v) (((((u)+((v)*236))*119)&0xff))
  1724. r = (((R >> 19) + A_DITHER(i,y) -96)>>8);
  1725. g = (((G >> 19) + A_DITHER(i + 17,y) - 96)>>8);
  1726. b = (((B >> 20) + A_DITHER(i + 17*2,y) -96)>>8);
  1727. r = av_clip_uintp2(r, 3);
  1728. g = av_clip_uintp2(g, 3);
  1729. b = av_clip_uintp2(b, 2);
  1730. } else {
  1731. r = (((R >> 21) + A_DITHER(i,y)-256)>>8);
  1732. g = (((G >> 19) + A_DITHER(i + 17,y)-256)>>8);
  1733. b = (((B >> 21) + A_DITHER(i + 17*2,y)-256)>>8);
  1734. r = av_clip_uintp2(r, 1);
  1735. g = av_clip_uintp2(g, 2);
  1736. b = av_clip_uintp2(b, 1);
  1737. }
  1738. break;
  1739. case SWS_DITHER_X_DITHER:
  1740. if (isrgb8) {
  1741. /* see http://pippin.gimp.org/a_dither/ for details/origin */
  1742. #define X_DITHER(u,v) (((((u)^((v)*237))*181)&0x1ff)/2)
  1743. r = (((R >> 19) + X_DITHER(i,y) - 96)>>8);
  1744. g = (((G >> 19) + X_DITHER(i + 17,y) - 96)>>8);
  1745. b = (((B >> 20) + X_DITHER(i + 17*2,y) - 96)>>8);
  1746. r = av_clip_uintp2(r, 3);
  1747. g = av_clip_uintp2(g, 3);
  1748. b = av_clip_uintp2(b, 2);
  1749. } else {
  1750. r = (((R >> 21) + X_DITHER(i,y)-256)>>8);
  1751. g = (((G >> 19) + X_DITHER(i + 17,y)-256)>>8);
  1752. b = (((B >> 21) + X_DITHER(i + 17*2,y)-256)>>8);
  1753. r = av_clip_uintp2(r, 1);
  1754. g = av_clip_uintp2(g, 2);
  1755. b = av_clip_uintp2(b, 1);
  1756. }
  1757. break;
  1758. }
  1759. if(target == AV_PIX_FMT_BGR4_BYTE) {
  1760. dest[0] = r + 2*g + 8*b;
  1761. } else if(target == AV_PIX_FMT_RGB4_BYTE) {
  1762. dest[0] = b + 2*g + 8*r;
  1763. } else if(target == AV_PIX_FMT_BGR8) {
  1764. dest[0] = r + 8*g + 64*b;
  1765. } else if(target == AV_PIX_FMT_RGB8) {
  1766. dest[0] = b + 4*g + 32*r;
  1767. } else
  1768. av_assert2(0);
  1769. break;}
  1770. }
  1771. }
  1772. static av_always_inline void
  1773. yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1774. const int16_t **lumSrc, int lumFilterSize,
  1775. const int16_t *chrFilter, const int16_t **chrUSrc,
  1776. const int16_t **chrVSrc, int chrFilterSize,
  1777. const int16_t **alpSrc, uint8_t *dest,
  1778. int dstW, int y, enum AVPixelFormat target, int hasAlpha)
  1779. {
  1780. int i;
  1781. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1782. int err[4] = {0};
  1783. int A = 0; //init to silence warning
  1784. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1785. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1786. step = 1;
  1787. for (i = 0; i < dstW; i++) {
  1788. int j;
  1789. int Y = 1<<9;
  1790. int U = (1<<9)-(128 << 19);
  1791. int V = (1<<9)-(128 << 19);
  1792. for (j = 0; j < lumFilterSize; j++) {
  1793. Y += lumSrc[j][i] * lumFilter[j];
  1794. }
  1795. for (j = 0; j < chrFilterSize; j++) {
  1796. U += chrUSrc[j][i] * chrFilter[j];
  1797. V += chrVSrc[j][i] * chrFilter[j];
  1798. }
  1799. Y >>= 10;
  1800. U >>= 10;
  1801. V >>= 10;
  1802. if (hasAlpha) {
  1803. A = 1 << 18;
  1804. for (j = 0; j < lumFilterSize; j++) {
  1805. A += alpSrc[j][i] * lumFilter[j];
  1806. }
  1807. A >>= 19;
  1808. if (A & 0x100)
  1809. A = av_clip_uint8(A);
  1810. }
  1811. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1812. dest += step;
  1813. }
  1814. c->dither_error[0][i] = err[0];
  1815. c->dither_error[1][i] = err[1];
  1816. c->dither_error[2][i] = err[2];
  1817. }
  1818. static av_always_inline void
  1819. yuv2rgb_full_2_c_template(SwsContext *c, const int16_t *buf[2],
  1820. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1821. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1822. int yalpha, int uvalpha, int y,
  1823. enum AVPixelFormat target, int hasAlpha)
  1824. {
  1825. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1826. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1827. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1828. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1829. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1830. int yalpha1 = 4096 - yalpha;
  1831. int uvalpha1 = 4096 - uvalpha;
  1832. int i;
  1833. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1834. int err[4] = {0};
  1835. int A = 0; // init to silcene warning
  1836. av_assert2(yalpha <= 4096U);
  1837. av_assert2(uvalpha <= 4096U);
  1838. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1839. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1840. step = 1;
  1841. for (i = 0; i < dstW; i++) {
  1842. int Y = ( buf0[i] * yalpha1 + buf1[i] * yalpha ) >> 10; //FIXME rounding
  1843. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha-(128 << 19)) >> 10;
  1844. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha-(128 << 19)) >> 10;
  1845. if (hasAlpha) {
  1846. A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha + (1<<18)) >> 19;
  1847. if (A & 0x100)
  1848. A = av_clip_uint8(A);
  1849. }
  1850. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1851. dest += step;
  1852. }
  1853. c->dither_error[0][i] = err[0];
  1854. c->dither_error[1][i] = err[1];
  1855. c->dither_error[2][i] = err[2];
  1856. }
  1857. static av_always_inline void
  1858. yuv2rgb_full_1_c_template(SwsContext *c, const int16_t *buf0,
  1859. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1860. const int16_t *abuf0, uint8_t *dest, int dstW,
  1861. int uvalpha, int y, enum AVPixelFormat target,
  1862. int hasAlpha)
  1863. {
  1864. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1865. int i;
  1866. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1867. int err[4] = {0};
  1868. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1869. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1870. step = 1;
  1871. if (uvalpha < 2048) {
  1872. int A = 0; //init to silence warning
  1873. for (i = 0; i < dstW; i++) {
  1874. int Y = buf0[i] * 4;
  1875. int U = (ubuf0[i] - (128<<7)) * 4;
  1876. int V = (vbuf0[i] - (128<<7)) * 4;
  1877. if (hasAlpha) {
  1878. A = (abuf0[i] + 64) >> 7;
  1879. if (A & 0x100)
  1880. A = av_clip_uint8(A);
  1881. }
  1882. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1883. dest += step;
  1884. }
  1885. } else {
  1886. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1887. int A = 0; //init to silence warning
  1888. for (i = 0; i < dstW; i++) {
  1889. int Y = buf0[i] * 4;
  1890. int U = (ubuf0[i] + ubuf1[i] - (128<<8)) * 2;
  1891. int V = (vbuf0[i] + vbuf1[i] - (128<<8)) * 2;
  1892. if (hasAlpha) {
  1893. A = (abuf0[i] + 64) >> 7;
  1894. if (A & 0x100)
  1895. A = av_clip_uint8(A);
  1896. }
  1897. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1898. dest += step;
  1899. }
  1900. }
  1901. c->dither_error[0][i] = err[0];
  1902. c->dither_error[1][i] = err[1];
  1903. c->dither_error[2][i] = err[2];
  1904. }
  1905. #if CONFIG_SMALL
  1906. YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->needAlpha)
  1907. YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->needAlpha)
  1908. YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->needAlpha)
  1909. YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->needAlpha)
  1910. #else
  1911. #if CONFIG_SWSCALE_ALPHA
  1912. YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, 1)
  1913. YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, 1)
  1914. YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, 1)
  1915. YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, 1)
  1916. #endif
  1917. YUV2RGBWRAPPER(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA, 0)
  1918. YUV2RGBWRAPPER(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR, 0)
  1919. YUV2RGBWRAPPER(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA, 0)
  1920. YUV2RGBWRAPPER(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB, 0)
  1921. #endif
  1922. YUV2RGBWRAPPER(yuv2, rgb_full, bgr24_full, AV_PIX_FMT_BGR24, 0)
  1923. YUV2RGBWRAPPER(yuv2, rgb_full, rgb24_full, AV_PIX_FMT_RGB24, 0)
  1924. YUV2RGBWRAPPER(yuv2, rgb_full, bgr4_byte_full, AV_PIX_FMT_BGR4_BYTE, 0)
  1925. YUV2RGBWRAPPER(yuv2, rgb_full, rgb4_byte_full, AV_PIX_FMT_RGB4_BYTE, 0)
  1926. YUV2RGBWRAPPER(yuv2, rgb_full, bgr8_full, AV_PIX_FMT_BGR8, 0)
  1927. YUV2RGBWRAPPER(yuv2, rgb_full, rgb8_full, AV_PIX_FMT_RGB8, 0)
  1928. static void
  1929. yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
  1930. const int16_t **lumSrc, int lumFilterSize,
  1931. const int16_t *chrFilter, const int16_t **chrUSrc,
  1932. const int16_t **chrVSrc, int chrFilterSize,
  1933. const int16_t **alpSrc, uint8_t **dest,
  1934. int dstW, int y)
  1935. {
  1936. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  1937. int i;
  1938. int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;
  1939. uint16_t **dest16 = (uint16_t**)dest;
  1940. int SH = 22 + 8 - desc->comp[0].depth;
  1941. int A = 0; // init to silence warning
  1942. for (i = 0; i < dstW; i++) {
  1943. int j;
  1944. int Y = 1 << 9;
  1945. int U = (1 << 9) - (128 << 19);
  1946. int V = (1 << 9) - (128 << 19);
  1947. int R, G, B;
  1948. for (j = 0; j < lumFilterSize; j++)
  1949. Y += lumSrc[j][i] * lumFilter[j];
  1950. for (j = 0; j < chrFilterSize; j++) {
  1951. U += chrUSrc[j][i] * chrFilter[j];
  1952. V += chrVSrc[j][i] * chrFilter[j];
  1953. }
  1954. Y >>= 10;
  1955. U >>= 10;
  1956. V >>= 10;
  1957. if (hasAlpha) {
  1958. A = 1 << 18;
  1959. for (j = 0; j < lumFilterSize; j++)
  1960. A += alpSrc[j][i] * lumFilter[j];
  1961. if (A & 0xF8000000)
  1962. A = av_clip_uintp2(A, 27);
  1963. }
  1964. Y -= c->yuv2rgb_y_offset;
  1965. Y *= c->yuv2rgb_y_coeff;
  1966. Y += 1 << (SH-1);
  1967. R = Y + V * c->yuv2rgb_v2r_coeff;
  1968. G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1969. B = Y + U * c->yuv2rgb_u2b_coeff;
  1970. if ((R | G | B) & 0xC0000000) {
  1971. R = av_clip_uintp2(R, 30);
  1972. G = av_clip_uintp2(G, 30);
  1973. B = av_clip_uintp2(B, 30);
  1974. }
  1975. if (SH != 22) {
  1976. dest16[0][i] = G >> SH;
  1977. dest16[1][i] = B >> SH;
  1978. dest16[2][i] = R >> SH;
  1979. if (hasAlpha)
  1980. dest16[3][i] = A >> (SH - 3);
  1981. } else {
  1982. dest[0][i] = G >> 22;
  1983. dest[1][i] = B >> 22;
  1984. dest[2][i] = R >> 22;
  1985. if (hasAlpha)
  1986. dest[3][i] = A >> 19;
  1987. }
  1988. }
  1989. if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
  1990. for (i = 0; i < dstW; i++) {
  1991. dest16[0][i] = av_bswap16(dest16[0][i]);
  1992. dest16[1][i] = av_bswap16(dest16[1][i]);
  1993. dest16[2][i] = av_bswap16(dest16[2][i]);
  1994. if (hasAlpha)
  1995. dest16[3][i] = av_bswap16(dest16[3][i]);
  1996. }
  1997. }
  1998. }
  1999. static void
  2000. yuv2gbrp16_full_X_c(SwsContext *c, const int16_t *lumFilter,
  2001. const int16_t **lumSrcx, int lumFilterSize,
  2002. const int16_t *chrFilter, const int16_t **chrUSrcx,
  2003. const int16_t **chrVSrcx, int chrFilterSize,
  2004. const int16_t **alpSrcx, uint8_t **dest,
  2005. int dstW, int y)
  2006. {
  2007. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  2008. int i;
  2009. int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrcx;
  2010. uint16_t **dest16 = (uint16_t**)dest;
  2011. const int32_t **lumSrc = (const int32_t**)lumSrcx;
  2012. const int32_t **chrUSrc = (const int32_t**)chrUSrcx;
  2013. const int32_t **chrVSrc = (const int32_t**)chrVSrcx;
  2014. const int32_t **alpSrc = (const int32_t**)alpSrcx;
  2015. for (i = 0; i < dstW; i++) {
  2016. int j;
  2017. int Y = -0x40000000;
  2018. int U = -(128 << 23);
  2019. int V = -(128 << 23);
  2020. int R, G, B, A;
  2021. for (j = 0; j < lumFilterSize; j++)
  2022. Y += lumSrc[j][i] * (unsigned)lumFilter[j];
  2023. for (j = 0; j < chrFilterSize; j++) {
  2024. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  2025. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  2026. }
  2027. Y >>= 14;
  2028. Y += 0x10000;
  2029. U >>= 14;
  2030. V >>= 14;
  2031. if (hasAlpha) {
  2032. A = -0x40000000;
  2033. for (j = 0; j < lumFilterSize; j++)
  2034. A += alpSrc[j][i] * (unsigned)lumFilter[j];
  2035. A >>= 1;
  2036. A += 0x20002000;
  2037. }
  2038. Y -= c->yuv2rgb_y_offset;
  2039. Y *= c->yuv2rgb_y_coeff;
  2040. Y += 1 << 13;
  2041. R = V * c->yuv2rgb_v2r_coeff;
  2042. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  2043. B = U * c->yuv2rgb_u2b_coeff;
  2044. R = av_clip_uintp2(Y + R, 30);
  2045. G = av_clip_uintp2(Y + G, 30);
  2046. B = av_clip_uintp2(Y + B, 30);
  2047. dest16[0][i] = G >> 14;
  2048. dest16[1][i] = B >> 14;
  2049. dest16[2][i] = R >> 14;
  2050. if (hasAlpha)
  2051. dest16[3][i] = av_clip_uintp2(A, 30) >> 14;
  2052. }
  2053. if ((!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
  2054. for (i = 0; i < dstW; i++) {
  2055. dest16[0][i] = av_bswap16(dest16[0][i]);
  2056. dest16[1][i] = av_bswap16(dest16[1][i]);
  2057. dest16[2][i] = av_bswap16(dest16[2][i]);
  2058. if (hasAlpha)
  2059. dest16[3][i] = av_bswap16(dest16[3][i]);
  2060. }
  2061. }
  2062. }
  2063. static void
  2064. yuv2gbrpf32_full_X_c(SwsContext *c, const int16_t *lumFilter,
  2065. const int16_t **lumSrcx, int lumFilterSize,
  2066. const int16_t *chrFilter, const int16_t **chrUSrcx,
  2067. const int16_t **chrVSrcx, int chrFilterSize,
  2068. const int16_t **alpSrcx, uint8_t **dest,
  2069. int dstW, int y)
  2070. {
  2071. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  2072. int i;
  2073. int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrcx;
  2074. uint32_t **dest32 = (uint32_t**)dest;
  2075. const int32_t **lumSrc = (const int32_t**)lumSrcx;
  2076. const int32_t **chrUSrc = (const int32_t**)chrUSrcx;
  2077. const int32_t **chrVSrc = (const int32_t**)chrVSrcx;
  2078. const int32_t **alpSrc = (const int32_t**)alpSrcx;
  2079. static const float float_mult = 1.0f / 65535.0f;
  2080. for (i = 0; i < dstW; i++) {
  2081. int j;
  2082. int Y = -0x40000000;
  2083. int U = -(128 << 23);
  2084. int V = -(128 << 23);
  2085. int R, G, B, A;
  2086. for (j = 0; j < lumFilterSize; j++)
  2087. Y += lumSrc[j][i] * (unsigned)lumFilter[j];
  2088. for (j = 0; j < chrFilterSize; j++) {
  2089. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  2090. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  2091. }
  2092. Y >>= 14;
  2093. Y += 0x10000;
  2094. U >>= 14;
  2095. V >>= 14;
  2096. if (hasAlpha) {
  2097. A = -0x40000000;
  2098. for (j = 0; j < lumFilterSize; j++)
  2099. A += alpSrc[j][i] * (unsigned)lumFilter[j];
  2100. A >>= 1;
  2101. A += 0x20002000;
  2102. }
  2103. Y -= c->yuv2rgb_y_offset;
  2104. Y *= c->yuv2rgb_y_coeff;
  2105. Y += 1 << 13;
  2106. R = V * c->yuv2rgb_v2r_coeff;
  2107. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  2108. B = U * c->yuv2rgb_u2b_coeff;
  2109. R = av_clip_uintp2(Y + R, 30);
  2110. G = av_clip_uintp2(Y + G, 30);
  2111. B = av_clip_uintp2(Y + B, 30);
  2112. dest32[0][i] = av_float2int(float_mult * (float)(G >> 14));
  2113. dest32[1][i] = av_float2int(float_mult * (float)(B >> 14));
  2114. dest32[2][i] = av_float2int(float_mult * (float)(R >> 14));
  2115. if (hasAlpha)
  2116. dest32[3][i] = av_float2int(float_mult * (float)(av_clip_uintp2(A, 30) >> 14));
  2117. }
  2118. if ((!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
  2119. for (i = 0; i < dstW; i++) {
  2120. dest32[0][i] = av_bswap32(dest32[0][i]);
  2121. dest32[1][i] = av_bswap32(dest32[1][i]);
  2122. dest32[2][i] = av_bswap32(dest32[2][i]);
  2123. if (hasAlpha)
  2124. dest32[3][i] = av_bswap32(dest32[3][i]);
  2125. }
  2126. }
  2127. }
  2128. static void
  2129. yuv2ya8_1_c(SwsContext *c, const int16_t *buf0,
  2130. const int16_t *ubuf[2], const int16_t *vbuf[2],
  2131. const int16_t *abuf0, uint8_t *dest, int dstW,
  2132. int uvalpha, int y)
  2133. {
  2134. int hasAlpha = !!abuf0;
  2135. int i;
  2136. for (i = 0; i < dstW; i++) {
  2137. int Y = (buf0[i] + 64) >> 7;
  2138. int A;
  2139. Y = av_clip_uint8(Y);
  2140. if (hasAlpha) {
  2141. A = (abuf0[i] + 64) >> 7;
  2142. if (A & 0x100)
  2143. A = av_clip_uint8(A);
  2144. }
  2145. dest[i * 2 ] = Y;
  2146. dest[i * 2 + 1] = hasAlpha ? A : 255;
  2147. }
  2148. }
  2149. static void
  2150. yuv2ya8_2_c(SwsContext *c, const int16_t *buf[2],
  2151. const int16_t *ubuf[2], const int16_t *vbuf[2],
  2152. const int16_t *abuf[2], uint8_t *dest, int dstW,
  2153. int yalpha, int uvalpha, int y)
  2154. {
  2155. int hasAlpha = abuf && abuf[0] && abuf[1];
  2156. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  2157. *abuf0 = hasAlpha ? abuf[0] : NULL,
  2158. *abuf1 = hasAlpha ? abuf[1] : NULL;
  2159. int yalpha1 = 4096 - yalpha;
  2160. int i;
  2161. av_assert2(yalpha <= 4096U);
  2162. for (i = 0; i < dstW; i++) {
  2163. int Y = (buf0[i] * yalpha1 + buf1[i] * yalpha) >> 19;
  2164. int A;
  2165. Y = av_clip_uint8(Y);
  2166. if (hasAlpha) {
  2167. A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 19;
  2168. A = av_clip_uint8(A);
  2169. }
  2170. dest[i * 2 ] = Y;
  2171. dest[i * 2 + 1] = hasAlpha ? A : 255;
  2172. }
  2173. }
  2174. static void
  2175. yuv2ya8_X_c(SwsContext *c, const int16_t *lumFilter,
  2176. const int16_t **lumSrc, int lumFilterSize,
  2177. const int16_t *chrFilter, const int16_t **chrUSrc,
  2178. const int16_t **chrVSrc, int chrFilterSize,
  2179. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  2180. {
  2181. int hasAlpha = !!alpSrc;
  2182. int i;
  2183. for (i = 0; i < dstW; i++) {
  2184. int j;
  2185. int Y = 1 << 18, A = 1 << 18;
  2186. for (j = 0; j < lumFilterSize; j++)
  2187. Y += lumSrc[j][i] * lumFilter[j];
  2188. Y >>= 19;
  2189. if (Y & 0x100)
  2190. Y = av_clip_uint8(Y);
  2191. if (hasAlpha) {
  2192. for (j = 0; j < lumFilterSize; j++)
  2193. A += alpSrc[j][i] * lumFilter[j];
  2194. A >>= 19;
  2195. if (A & 0x100)
  2196. A = av_clip_uint8(A);
  2197. }
  2198. dest[2 * i ] = Y;
  2199. dest[2 * i + 1] = hasAlpha ? A : 255;
  2200. }
  2201. }
  2202. static void
  2203. yuv2ayuv64le_X_c(SwsContext *c, const int16_t *lumFilter,
  2204. const int16_t **_lumSrc, int lumFilterSize,
  2205. const int16_t *chrFilter, const int16_t **_chrUSrc,
  2206. const int16_t **_chrVSrc, int chrFilterSize,
  2207. const int16_t **_alpSrc, uint8_t *dest, int dstW, int y)
  2208. {
  2209. const int32_t **lumSrc = (const int32_t **) _lumSrc,
  2210. **chrUSrc = (const int32_t **) _chrUSrc,
  2211. **chrVSrc = (const int32_t **) _chrVSrc,
  2212. **alpSrc = (const int32_t **) _alpSrc;
  2213. int hasAlpha = !!alpSrc;
  2214. int i;
  2215. for (i = 0; i < dstW; i++) {
  2216. int Y = 1 << 14, U = 1 << 14;
  2217. int V = 1 << 14, A = 1 << 14;
  2218. int j;
  2219. Y -= 0x40000000;
  2220. U -= 0x40000000;
  2221. V -= 0x40000000;
  2222. A -= 0x40000000;
  2223. for (j = 0; j < lumFilterSize; j++)
  2224. Y += lumSrc[j][i] * (unsigned)lumFilter[j];
  2225. for (j = 0; j < chrFilterSize; j++)
  2226. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  2227. for (j = 0; j < chrFilterSize; j++)
  2228. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  2229. if (hasAlpha)
  2230. for (j = 0; j < lumFilterSize; j++)
  2231. A += alpSrc[j][i] * (unsigned)lumFilter[j];
  2232. Y = 0x8000 + av_clip_int16(Y >> 15);
  2233. U = 0x8000 + av_clip_int16(U >> 15);
  2234. V = 0x8000 + av_clip_int16(V >> 15);
  2235. A = 0x8000 + av_clip_int16(A >> 15);
  2236. AV_WL16(dest + 8 * i, hasAlpha ? A : 65535);
  2237. AV_WL16(dest + 8 * i + 2, Y);
  2238. AV_WL16(dest + 8 * i + 4, U);
  2239. AV_WL16(dest + 8 * i + 6, V);
  2240. }
  2241. }
  2242. av_cold void ff_sws_init_output_funcs(SwsContext *c,
  2243. yuv2planar1_fn *yuv2plane1,
  2244. yuv2planarX_fn *yuv2planeX,
  2245. yuv2interleavedX_fn *yuv2nv12cX,
  2246. yuv2packed1_fn *yuv2packed1,
  2247. yuv2packed2_fn *yuv2packed2,
  2248. yuv2packedX_fn *yuv2packedX,
  2249. yuv2anyX_fn *yuv2anyX)
  2250. {
  2251. enum AVPixelFormat dstFormat = c->dstFormat;
  2252. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  2253. if (dstFormat == AV_PIX_FMT_P010LE || dstFormat == AV_PIX_FMT_P010BE) {
  2254. *yuv2plane1 = isBE(dstFormat) ? yuv2p010l1_BE_c : yuv2p010l1_LE_c;
  2255. *yuv2planeX = isBE(dstFormat) ? yuv2p010lX_BE_c : yuv2p010lX_LE_c;
  2256. *yuv2nv12cX = yuv2p010cX_c;
  2257. } else if (is16BPS(dstFormat)) {
  2258. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
  2259. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
  2260. if (dstFormat == AV_PIX_FMT_P016LE || dstFormat == AV_PIX_FMT_P016BE) {
  2261. *yuv2nv12cX = yuv2p016cX_c;
  2262. }
  2263. } else if (isNBPS(dstFormat)) {
  2264. if (desc->comp[0].depth == 9) {
  2265. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
  2266. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
  2267. } else if (desc->comp[0].depth == 10) {
  2268. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
  2269. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
  2270. } else if (desc->comp[0].depth == 12) {
  2271. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c : yuv2planeX_12LE_c;
  2272. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c : yuv2plane1_12LE_c;
  2273. } else if (desc->comp[0].depth == 14) {
  2274. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c : yuv2planeX_14LE_c;
  2275. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c : yuv2plane1_14LE_c;
  2276. } else
  2277. av_assert0(0);
  2278. } else if (dstFormat == AV_PIX_FMT_GRAYF32BE) {
  2279. *yuv2planeX = yuv2planeX_floatBE_c;
  2280. *yuv2plane1 = yuv2plane1_floatBE_c;
  2281. } else if (dstFormat == AV_PIX_FMT_GRAYF32LE) {
  2282. *yuv2planeX = yuv2planeX_floatLE_c;
  2283. *yuv2plane1 = yuv2plane1_floatLE_c;
  2284. } else {
  2285. *yuv2plane1 = yuv2plane1_8_c;
  2286. *yuv2planeX = yuv2planeX_8_c;
  2287. if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21 ||
  2288. dstFormat == AV_PIX_FMT_NV24 || dstFormat == AV_PIX_FMT_NV42)
  2289. *yuv2nv12cX = yuv2nv12cX_c;
  2290. }
  2291. if(c->flags & SWS_FULL_CHR_H_INT) {
  2292. switch (dstFormat) {
  2293. case AV_PIX_FMT_RGBA:
  2294. #if CONFIG_SMALL
  2295. *yuv2packedX = yuv2rgba32_full_X_c;
  2296. *yuv2packed2 = yuv2rgba32_full_2_c;
  2297. *yuv2packed1 = yuv2rgba32_full_1_c;
  2298. #else
  2299. #if CONFIG_SWSCALE_ALPHA
  2300. if (c->needAlpha) {
  2301. *yuv2packedX = yuv2rgba32_full_X_c;
  2302. *yuv2packed2 = yuv2rgba32_full_2_c;
  2303. *yuv2packed1 = yuv2rgba32_full_1_c;
  2304. } else
  2305. #endif /* CONFIG_SWSCALE_ALPHA */
  2306. {
  2307. *yuv2packedX = yuv2rgbx32_full_X_c;
  2308. *yuv2packed2 = yuv2rgbx32_full_2_c;
  2309. *yuv2packed1 = yuv2rgbx32_full_1_c;
  2310. }
  2311. #endif /* !CONFIG_SMALL */
  2312. break;
  2313. case AV_PIX_FMT_ARGB:
  2314. #if CONFIG_SMALL
  2315. *yuv2packedX = yuv2argb32_full_X_c;
  2316. *yuv2packed2 = yuv2argb32_full_2_c;
  2317. *yuv2packed1 = yuv2argb32_full_1_c;
  2318. #else
  2319. #if CONFIG_SWSCALE_ALPHA
  2320. if (c->needAlpha) {
  2321. *yuv2packedX = yuv2argb32_full_X_c;
  2322. *yuv2packed2 = yuv2argb32_full_2_c;
  2323. *yuv2packed1 = yuv2argb32_full_1_c;
  2324. } else
  2325. #endif /* CONFIG_SWSCALE_ALPHA */
  2326. {
  2327. *yuv2packedX = yuv2xrgb32_full_X_c;
  2328. *yuv2packed2 = yuv2xrgb32_full_2_c;
  2329. *yuv2packed1 = yuv2xrgb32_full_1_c;
  2330. }
  2331. #endif /* !CONFIG_SMALL */
  2332. break;
  2333. case AV_PIX_FMT_BGRA:
  2334. #if CONFIG_SMALL
  2335. *yuv2packedX = yuv2bgra32_full_X_c;
  2336. *yuv2packed2 = yuv2bgra32_full_2_c;
  2337. *yuv2packed1 = yuv2bgra32_full_1_c;
  2338. #else
  2339. #if CONFIG_SWSCALE_ALPHA
  2340. if (c->needAlpha) {
  2341. *yuv2packedX = yuv2bgra32_full_X_c;
  2342. *yuv2packed2 = yuv2bgra32_full_2_c;
  2343. *yuv2packed1 = yuv2bgra32_full_1_c;
  2344. } else
  2345. #endif /* CONFIG_SWSCALE_ALPHA */
  2346. {
  2347. *yuv2packedX = yuv2bgrx32_full_X_c;
  2348. *yuv2packed2 = yuv2bgrx32_full_2_c;
  2349. *yuv2packed1 = yuv2bgrx32_full_1_c;
  2350. }
  2351. #endif /* !CONFIG_SMALL */
  2352. break;
  2353. case AV_PIX_FMT_ABGR:
  2354. #if CONFIG_SMALL
  2355. *yuv2packedX = yuv2abgr32_full_X_c;
  2356. *yuv2packed2 = yuv2abgr32_full_2_c;
  2357. *yuv2packed1 = yuv2abgr32_full_1_c;
  2358. #else
  2359. #if CONFIG_SWSCALE_ALPHA
  2360. if (c->needAlpha) {
  2361. *yuv2packedX = yuv2abgr32_full_X_c;
  2362. *yuv2packed2 = yuv2abgr32_full_2_c;
  2363. *yuv2packed1 = yuv2abgr32_full_1_c;
  2364. } else
  2365. #endif /* CONFIG_SWSCALE_ALPHA */
  2366. {
  2367. *yuv2packedX = yuv2xbgr32_full_X_c;
  2368. *yuv2packed2 = yuv2xbgr32_full_2_c;
  2369. *yuv2packed1 = yuv2xbgr32_full_1_c;
  2370. }
  2371. #endif /* !CONFIG_SMALL */
  2372. break;
  2373. case AV_PIX_FMT_RGBA64LE:
  2374. #if CONFIG_SWSCALE_ALPHA
  2375. if (c->needAlpha) {
  2376. *yuv2packedX = yuv2rgba64le_full_X_c;
  2377. *yuv2packed2 = yuv2rgba64le_full_2_c;
  2378. *yuv2packed1 = yuv2rgba64le_full_1_c;
  2379. } else
  2380. #endif /* CONFIG_SWSCALE_ALPHA */
  2381. {
  2382. *yuv2packedX = yuv2rgbx64le_full_X_c;
  2383. *yuv2packed2 = yuv2rgbx64le_full_2_c;
  2384. *yuv2packed1 = yuv2rgbx64le_full_1_c;
  2385. }
  2386. break;
  2387. case AV_PIX_FMT_RGBA64BE:
  2388. #if CONFIG_SWSCALE_ALPHA
  2389. if (c->needAlpha) {
  2390. *yuv2packedX = yuv2rgba64be_full_X_c;
  2391. *yuv2packed2 = yuv2rgba64be_full_2_c;
  2392. *yuv2packed1 = yuv2rgba64be_full_1_c;
  2393. } else
  2394. #endif /* CONFIG_SWSCALE_ALPHA */
  2395. {
  2396. *yuv2packedX = yuv2rgbx64be_full_X_c;
  2397. *yuv2packed2 = yuv2rgbx64be_full_2_c;
  2398. *yuv2packed1 = yuv2rgbx64be_full_1_c;
  2399. }
  2400. break;
  2401. case AV_PIX_FMT_BGRA64LE:
  2402. #if CONFIG_SWSCALE_ALPHA
  2403. if (c->needAlpha) {
  2404. *yuv2packedX = yuv2bgra64le_full_X_c;
  2405. *yuv2packed2 = yuv2bgra64le_full_2_c;
  2406. *yuv2packed1 = yuv2bgra64le_full_1_c;
  2407. } else
  2408. #endif /* CONFIG_SWSCALE_ALPHA */
  2409. {
  2410. *yuv2packedX = yuv2bgrx64le_full_X_c;
  2411. *yuv2packed2 = yuv2bgrx64le_full_2_c;
  2412. *yuv2packed1 = yuv2bgrx64le_full_1_c;
  2413. }
  2414. break;
  2415. case AV_PIX_FMT_BGRA64BE:
  2416. #if CONFIG_SWSCALE_ALPHA
  2417. if (c->needAlpha) {
  2418. *yuv2packedX = yuv2bgra64be_full_X_c;
  2419. *yuv2packed2 = yuv2bgra64be_full_2_c;
  2420. *yuv2packed1 = yuv2bgra64be_full_1_c;
  2421. } else
  2422. #endif /* CONFIG_SWSCALE_ALPHA */
  2423. {
  2424. *yuv2packedX = yuv2bgrx64be_full_X_c;
  2425. *yuv2packed2 = yuv2bgrx64be_full_2_c;
  2426. *yuv2packed1 = yuv2bgrx64be_full_1_c;
  2427. }
  2428. break;
  2429. case AV_PIX_FMT_RGB24:
  2430. *yuv2packedX = yuv2rgb24_full_X_c;
  2431. *yuv2packed2 = yuv2rgb24_full_2_c;
  2432. *yuv2packed1 = yuv2rgb24_full_1_c;
  2433. break;
  2434. case AV_PIX_FMT_BGR24:
  2435. *yuv2packedX = yuv2bgr24_full_X_c;
  2436. *yuv2packed2 = yuv2bgr24_full_2_c;
  2437. *yuv2packed1 = yuv2bgr24_full_1_c;
  2438. break;
  2439. case AV_PIX_FMT_RGB48LE:
  2440. *yuv2packedX = yuv2rgb48le_full_X_c;
  2441. *yuv2packed2 = yuv2rgb48le_full_2_c;
  2442. *yuv2packed1 = yuv2rgb48le_full_1_c;
  2443. break;
  2444. case AV_PIX_FMT_BGR48LE:
  2445. *yuv2packedX = yuv2bgr48le_full_X_c;
  2446. *yuv2packed2 = yuv2bgr48le_full_2_c;
  2447. *yuv2packed1 = yuv2bgr48le_full_1_c;
  2448. break;
  2449. case AV_PIX_FMT_RGB48BE:
  2450. *yuv2packedX = yuv2rgb48be_full_X_c;
  2451. *yuv2packed2 = yuv2rgb48be_full_2_c;
  2452. *yuv2packed1 = yuv2rgb48be_full_1_c;
  2453. break;
  2454. case AV_PIX_FMT_BGR48BE:
  2455. *yuv2packedX = yuv2bgr48be_full_X_c;
  2456. *yuv2packed2 = yuv2bgr48be_full_2_c;
  2457. *yuv2packed1 = yuv2bgr48be_full_1_c;
  2458. break;
  2459. case AV_PIX_FMT_BGR4_BYTE:
  2460. *yuv2packedX = yuv2bgr4_byte_full_X_c;
  2461. *yuv2packed2 = yuv2bgr4_byte_full_2_c;
  2462. *yuv2packed1 = yuv2bgr4_byte_full_1_c;
  2463. break;
  2464. case AV_PIX_FMT_RGB4_BYTE:
  2465. *yuv2packedX = yuv2rgb4_byte_full_X_c;
  2466. *yuv2packed2 = yuv2rgb4_byte_full_2_c;
  2467. *yuv2packed1 = yuv2rgb4_byte_full_1_c;
  2468. break;
  2469. case AV_PIX_FMT_BGR8:
  2470. *yuv2packedX = yuv2bgr8_full_X_c;
  2471. *yuv2packed2 = yuv2bgr8_full_2_c;
  2472. *yuv2packed1 = yuv2bgr8_full_1_c;
  2473. break;
  2474. case AV_PIX_FMT_RGB8:
  2475. *yuv2packedX = yuv2rgb8_full_X_c;
  2476. *yuv2packed2 = yuv2rgb8_full_2_c;
  2477. *yuv2packed1 = yuv2rgb8_full_1_c;
  2478. break;
  2479. case AV_PIX_FMT_GBRP:
  2480. case AV_PIX_FMT_GBRP9BE:
  2481. case AV_PIX_FMT_GBRP9LE:
  2482. case AV_PIX_FMT_GBRP10BE:
  2483. case AV_PIX_FMT_GBRP10LE:
  2484. case AV_PIX_FMT_GBRP12BE:
  2485. case AV_PIX_FMT_GBRP12LE:
  2486. case AV_PIX_FMT_GBRP14BE:
  2487. case AV_PIX_FMT_GBRP14LE:
  2488. case AV_PIX_FMT_GBRAP:
  2489. case AV_PIX_FMT_GBRAP10BE:
  2490. case AV_PIX_FMT_GBRAP10LE:
  2491. case AV_PIX_FMT_GBRAP12BE:
  2492. case AV_PIX_FMT_GBRAP12LE:
  2493. *yuv2anyX = yuv2gbrp_full_X_c;
  2494. break;
  2495. case AV_PIX_FMT_GBRP16BE:
  2496. case AV_PIX_FMT_GBRP16LE:
  2497. case AV_PIX_FMT_GBRAP16BE:
  2498. case AV_PIX_FMT_GBRAP16LE:
  2499. *yuv2anyX = yuv2gbrp16_full_X_c;
  2500. break;
  2501. case AV_PIX_FMT_GBRPF32BE:
  2502. case AV_PIX_FMT_GBRPF32LE:
  2503. case AV_PIX_FMT_GBRAPF32BE:
  2504. case AV_PIX_FMT_GBRAPF32LE:
  2505. *yuv2anyX = yuv2gbrpf32_full_X_c;
  2506. break;
  2507. }
  2508. if (!*yuv2packedX && !*yuv2anyX)
  2509. goto YUV_PACKED;
  2510. } else {
  2511. YUV_PACKED:
  2512. switch (dstFormat) {
  2513. case AV_PIX_FMT_RGBA64LE:
  2514. #if CONFIG_SWSCALE_ALPHA
  2515. if (c->needAlpha) {
  2516. *yuv2packed1 = yuv2rgba64le_1_c;
  2517. *yuv2packed2 = yuv2rgba64le_2_c;
  2518. *yuv2packedX = yuv2rgba64le_X_c;
  2519. } else
  2520. #endif /* CONFIG_SWSCALE_ALPHA */
  2521. {
  2522. *yuv2packed1 = yuv2rgbx64le_1_c;
  2523. *yuv2packed2 = yuv2rgbx64le_2_c;
  2524. *yuv2packedX = yuv2rgbx64le_X_c;
  2525. }
  2526. break;
  2527. case AV_PIX_FMT_RGBA64BE:
  2528. #if CONFIG_SWSCALE_ALPHA
  2529. if (c->needAlpha) {
  2530. *yuv2packed1 = yuv2rgba64be_1_c;
  2531. *yuv2packed2 = yuv2rgba64be_2_c;
  2532. *yuv2packedX = yuv2rgba64be_X_c;
  2533. } else
  2534. #endif /* CONFIG_SWSCALE_ALPHA */
  2535. {
  2536. *yuv2packed1 = yuv2rgbx64be_1_c;
  2537. *yuv2packed2 = yuv2rgbx64be_2_c;
  2538. *yuv2packedX = yuv2rgbx64be_X_c;
  2539. }
  2540. break;
  2541. case AV_PIX_FMT_BGRA64LE:
  2542. #if CONFIG_SWSCALE_ALPHA
  2543. if (c->needAlpha) {
  2544. *yuv2packed1 = yuv2bgra64le_1_c;
  2545. *yuv2packed2 = yuv2bgra64le_2_c;
  2546. *yuv2packedX = yuv2bgra64le_X_c;
  2547. } else
  2548. #endif /* CONFIG_SWSCALE_ALPHA */
  2549. {
  2550. *yuv2packed1 = yuv2bgrx64le_1_c;
  2551. *yuv2packed2 = yuv2bgrx64le_2_c;
  2552. *yuv2packedX = yuv2bgrx64le_X_c;
  2553. }
  2554. break;
  2555. case AV_PIX_FMT_BGRA64BE:
  2556. #if CONFIG_SWSCALE_ALPHA
  2557. if (c->needAlpha) {
  2558. *yuv2packed1 = yuv2bgra64be_1_c;
  2559. *yuv2packed2 = yuv2bgra64be_2_c;
  2560. *yuv2packedX = yuv2bgra64be_X_c;
  2561. } else
  2562. #endif /* CONFIG_SWSCALE_ALPHA */
  2563. {
  2564. *yuv2packed1 = yuv2bgrx64be_1_c;
  2565. *yuv2packed2 = yuv2bgrx64be_2_c;
  2566. *yuv2packedX = yuv2bgrx64be_X_c;
  2567. }
  2568. break;
  2569. case AV_PIX_FMT_RGB48LE:
  2570. *yuv2packed1 = yuv2rgb48le_1_c;
  2571. *yuv2packed2 = yuv2rgb48le_2_c;
  2572. *yuv2packedX = yuv2rgb48le_X_c;
  2573. break;
  2574. case AV_PIX_FMT_RGB48BE:
  2575. *yuv2packed1 = yuv2rgb48be_1_c;
  2576. *yuv2packed2 = yuv2rgb48be_2_c;
  2577. *yuv2packedX = yuv2rgb48be_X_c;
  2578. break;
  2579. case AV_PIX_FMT_BGR48LE:
  2580. *yuv2packed1 = yuv2bgr48le_1_c;
  2581. *yuv2packed2 = yuv2bgr48le_2_c;
  2582. *yuv2packedX = yuv2bgr48le_X_c;
  2583. break;
  2584. case AV_PIX_FMT_BGR48BE:
  2585. *yuv2packed1 = yuv2bgr48be_1_c;
  2586. *yuv2packed2 = yuv2bgr48be_2_c;
  2587. *yuv2packedX = yuv2bgr48be_X_c;
  2588. break;
  2589. case AV_PIX_FMT_RGB32:
  2590. case AV_PIX_FMT_BGR32:
  2591. #if CONFIG_SMALL
  2592. *yuv2packed1 = yuv2rgb32_1_c;
  2593. *yuv2packed2 = yuv2rgb32_2_c;
  2594. *yuv2packedX = yuv2rgb32_X_c;
  2595. #else
  2596. #if CONFIG_SWSCALE_ALPHA
  2597. if (c->needAlpha) {
  2598. *yuv2packed1 = yuv2rgba32_1_c;
  2599. *yuv2packed2 = yuv2rgba32_2_c;
  2600. *yuv2packedX = yuv2rgba32_X_c;
  2601. } else
  2602. #endif /* CONFIG_SWSCALE_ALPHA */
  2603. {
  2604. *yuv2packed1 = yuv2rgbx32_1_c;
  2605. *yuv2packed2 = yuv2rgbx32_2_c;
  2606. *yuv2packedX = yuv2rgbx32_X_c;
  2607. }
  2608. #endif /* !CONFIG_SMALL */
  2609. break;
  2610. case AV_PIX_FMT_RGB32_1:
  2611. case AV_PIX_FMT_BGR32_1:
  2612. #if CONFIG_SMALL
  2613. *yuv2packed1 = yuv2rgb32_1_1_c;
  2614. *yuv2packed2 = yuv2rgb32_1_2_c;
  2615. *yuv2packedX = yuv2rgb32_1_X_c;
  2616. #else
  2617. #if CONFIG_SWSCALE_ALPHA
  2618. if (c->needAlpha) {
  2619. *yuv2packed1 = yuv2rgba32_1_1_c;
  2620. *yuv2packed2 = yuv2rgba32_1_2_c;
  2621. *yuv2packedX = yuv2rgba32_1_X_c;
  2622. } else
  2623. #endif /* CONFIG_SWSCALE_ALPHA */
  2624. {
  2625. *yuv2packed1 = yuv2rgbx32_1_1_c;
  2626. *yuv2packed2 = yuv2rgbx32_1_2_c;
  2627. *yuv2packedX = yuv2rgbx32_1_X_c;
  2628. }
  2629. #endif /* !CONFIG_SMALL */
  2630. break;
  2631. case AV_PIX_FMT_RGB24:
  2632. *yuv2packed1 = yuv2rgb24_1_c;
  2633. *yuv2packed2 = yuv2rgb24_2_c;
  2634. *yuv2packedX = yuv2rgb24_X_c;
  2635. break;
  2636. case AV_PIX_FMT_BGR24:
  2637. *yuv2packed1 = yuv2bgr24_1_c;
  2638. *yuv2packed2 = yuv2bgr24_2_c;
  2639. *yuv2packedX = yuv2bgr24_X_c;
  2640. break;
  2641. case AV_PIX_FMT_RGB565LE:
  2642. case AV_PIX_FMT_RGB565BE:
  2643. case AV_PIX_FMT_BGR565LE:
  2644. case AV_PIX_FMT_BGR565BE:
  2645. *yuv2packed1 = yuv2rgb16_1_c;
  2646. *yuv2packed2 = yuv2rgb16_2_c;
  2647. *yuv2packedX = yuv2rgb16_X_c;
  2648. break;
  2649. case AV_PIX_FMT_RGB555LE:
  2650. case AV_PIX_FMT_RGB555BE:
  2651. case AV_PIX_FMT_BGR555LE:
  2652. case AV_PIX_FMT_BGR555BE:
  2653. *yuv2packed1 = yuv2rgb15_1_c;
  2654. *yuv2packed2 = yuv2rgb15_2_c;
  2655. *yuv2packedX = yuv2rgb15_X_c;
  2656. break;
  2657. case AV_PIX_FMT_RGB444LE:
  2658. case AV_PIX_FMT_RGB444BE:
  2659. case AV_PIX_FMT_BGR444LE:
  2660. case AV_PIX_FMT_BGR444BE:
  2661. *yuv2packed1 = yuv2rgb12_1_c;
  2662. *yuv2packed2 = yuv2rgb12_2_c;
  2663. *yuv2packedX = yuv2rgb12_X_c;
  2664. break;
  2665. case AV_PIX_FMT_RGB8:
  2666. case AV_PIX_FMT_BGR8:
  2667. *yuv2packed1 = yuv2rgb8_1_c;
  2668. *yuv2packed2 = yuv2rgb8_2_c;
  2669. *yuv2packedX = yuv2rgb8_X_c;
  2670. break;
  2671. case AV_PIX_FMT_RGB4:
  2672. case AV_PIX_FMT_BGR4:
  2673. *yuv2packed1 = yuv2rgb4_1_c;
  2674. *yuv2packed2 = yuv2rgb4_2_c;
  2675. *yuv2packedX = yuv2rgb4_X_c;
  2676. break;
  2677. case AV_PIX_FMT_RGB4_BYTE:
  2678. case AV_PIX_FMT_BGR4_BYTE:
  2679. *yuv2packed1 = yuv2rgb4b_1_c;
  2680. *yuv2packed2 = yuv2rgb4b_2_c;
  2681. *yuv2packedX = yuv2rgb4b_X_c;
  2682. break;
  2683. case AV_PIX_FMT_X2RGB10LE:
  2684. case AV_PIX_FMT_X2RGB10BE:
  2685. *yuv2packed1 = yuv2x2rgb10_1_c;
  2686. *yuv2packed2 = yuv2x2rgb10_2_c;
  2687. *yuv2packedX = yuv2x2rgb10_X_c;
  2688. break;
  2689. }
  2690. }
  2691. switch (dstFormat) {
  2692. case AV_PIX_FMT_MONOWHITE:
  2693. *yuv2packed1 = yuv2monowhite_1_c;
  2694. *yuv2packed2 = yuv2monowhite_2_c;
  2695. *yuv2packedX = yuv2monowhite_X_c;
  2696. break;
  2697. case AV_PIX_FMT_MONOBLACK:
  2698. *yuv2packed1 = yuv2monoblack_1_c;
  2699. *yuv2packed2 = yuv2monoblack_2_c;
  2700. *yuv2packedX = yuv2monoblack_X_c;
  2701. break;
  2702. case AV_PIX_FMT_YUYV422:
  2703. *yuv2packed1 = yuv2yuyv422_1_c;
  2704. *yuv2packed2 = yuv2yuyv422_2_c;
  2705. *yuv2packedX = yuv2yuyv422_X_c;
  2706. break;
  2707. case AV_PIX_FMT_YVYU422:
  2708. *yuv2packed1 = yuv2yvyu422_1_c;
  2709. *yuv2packed2 = yuv2yvyu422_2_c;
  2710. *yuv2packedX = yuv2yvyu422_X_c;
  2711. break;
  2712. case AV_PIX_FMT_UYVY422:
  2713. *yuv2packed1 = yuv2uyvy422_1_c;
  2714. *yuv2packed2 = yuv2uyvy422_2_c;
  2715. *yuv2packedX = yuv2uyvy422_X_c;
  2716. break;
  2717. case AV_PIX_FMT_YA8:
  2718. *yuv2packed1 = yuv2ya8_1_c;
  2719. *yuv2packed2 = yuv2ya8_2_c;
  2720. *yuv2packedX = yuv2ya8_X_c;
  2721. break;
  2722. case AV_PIX_FMT_YA16LE:
  2723. *yuv2packed1 = yuv2ya16le_1_c;
  2724. *yuv2packed2 = yuv2ya16le_2_c;
  2725. *yuv2packedX = yuv2ya16le_X_c;
  2726. break;
  2727. case AV_PIX_FMT_YA16BE:
  2728. *yuv2packed1 = yuv2ya16be_1_c;
  2729. *yuv2packed2 = yuv2ya16be_2_c;
  2730. *yuv2packedX = yuv2ya16be_X_c;
  2731. break;
  2732. case AV_PIX_FMT_AYUV64LE:
  2733. *yuv2packedX = yuv2ayuv64le_X_c;
  2734. break;
  2735. }
  2736. }