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.

2215 lines
82KB

  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. #undef output_pixel
  168. #define output_pixel(pos, val) \
  169. if (big_endian) { \
  170. AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  171. } else { \
  172. AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  173. }
  174. static av_always_inline void
  175. yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
  176. int big_endian, int output_bits)
  177. {
  178. int i;
  179. int shift = 15 - output_bits;
  180. for (i = 0; i < dstW; i++) {
  181. int val = src[i] + (1 << (shift - 1));
  182. output_pixel(&dest[i], val);
  183. }
  184. }
  185. static av_always_inline void
  186. yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
  187. const int16_t **src, uint16_t *dest, int dstW,
  188. int big_endian, int output_bits)
  189. {
  190. int i;
  191. int shift = 11 + 16 - output_bits;
  192. for (i = 0; i < dstW; i++) {
  193. int val = 1 << (shift - 1);
  194. int j;
  195. for (j = 0; j < filterSize; j++)
  196. val += src[j][i] * filter[j];
  197. output_pixel(&dest[i], val);
  198. }
  199. }
  200. #undef output_pixel
  201. #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
  202. static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
  203. uint8_t *dest, int dstW, \
  204. const uint8_t *dither, int offset)\
  205. { \
  206. yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
  207. (uint16_t *) dest, dstW, is_be, bits); \
  208. }\
  209. static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
  210. const int16_t **src, uint8_t *dest, int dstW, \
  211. const uint8_t *dither, int offset)\
  212. { \
  213. yuv2planeX_## template_size ## _c_template(filter, \
  214. filterSize, (const typeX_t **) src, \
  215. (uint16_t *) dest, dstW, is_be, bits); \
  216. }
  217. yuv2NBPS( 9, BE, 1, 10, int16_t)
  218. yuv2NBPS( 9, LE, 0, 10, int16_t)
  219. yuv2NBPS(10, BE, 1, 10, int16_t)
  220. yuv2NBPS(10, LE, 0, 10, int16_t)
  221. yuv2NBPS(12, BE, 1, 10, int16_t)
  222. yuv2NBPS(12, LE, 0, 10, int16_t)
  223. yuv2NBPS(14, BE, 1, 10, int16_t)
  224. yuv2NBPS(14, LE, 0, 10, int16_t)
  225. yuv2NBPS(16, BE, 1, 16, int32_t)
  226. yuv2NBPS(16, LE, 0, 16, int32_t)
  227. static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
  228. const int16_t **src, uint8_t *dest, int dstW,
  229. const uint8_t *dither, int offset)
  230. {
  231. int i;
  232. for (i=0; i<dstW; i++) {
  233. int val = dither[(i + offset) & 7] << 12;
  234. int j;
  235. for (j=0; j<filterSize; j++)
  236. val += src[j][i] * filter[j];
  237. dest[i]= av_clip_uint8(val>>19);
  238. }
  239. }
  240. static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
  241. const uint8_t *dither, int offset)
  242. {
  243. int i;
  244. for (i=0; i<dstW; i++) {
  245. int val = (src[i] + dither[(i + offset) & 7]) >> 7;
  246. dest[i]= av_clip_uint8(val);
  247. }
  248. }
  249. static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
  250. const int16_t **chrUSrc, const int16_t **chrVSrc,
  251. uint8_t *dest, int chrDstW)
  252. {
  253. enum AVPixelFormat dstFormat = c->dstFormat;
  254. const uint8_t *chrDither = c->chrDither8;
  255. int i;
  256. if (dstFormat == AV_PIX_FMT_NV12)
  257. for (i=0; i<chrDstW; i++) {
  258. int u = chrDither[i & 7] << 12;
  259. int v = chrDither[(i + 3) & 7] << 12;
  260. int j;
  261. for (j=0; j<chrFilterSize; j++) {
  262. u += chrUSrc[j][i] * chrFilter[j];
  263. v += chrVSrc[j][i] * chrFilter[j];
  264. }
  265. dest[2*i]= av_clip_uint8(u>>19);
  266. dest[2*i+1]= av_clip_uint8(v>>19);
  267. }
  268. else
  269. for (i=0; i<chrDstW; i++) {
  270. int u = chrDither[i & 7] << 12;
  271. int v = chrDither[(i + 3) & 7] << 12;
  272. int j;
  273. for (j=0; j<chrFilterSize; j++) {
  274. u += chrUSrc[j][i] * chrFilter[j];
  275. v += chrVSrc[j][i] * chrFilter[j];
  276. }
  277. dest[2*i]= av_clip_uint8(v>>19);
  278. dest[2*i+1]= av_clip_uint8(u>>19);
  279. }
  280. }
  281. #define accumulate_bit(acc, val) \
  282. acc <<= 1; \
  283. acc |= (val) >= (128 + 110)
  284. #define output_pixel(pos, acc) \
  285. if (target == AV_PIX_FMT_MONOBLACK) { \
  286. pos = acc; \
  287. } else { \
  288. pos = ~acc; \
  289. }
  290. static av_always_inline void
  291. yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
  292. const int16_t **lumSrc, int lumFilterSize,
  293. const int16_t *chrFilter, const int16_t **chrUSrc,
  294. const int16_t **chrVSrc, int chrFilterSize,
  295. const int16_t **alpSrc, uint8_t *dest, int dstW,
  296. int y, enum AVPixelFormat target)
  297. {
  298. const uint8_t * const d128 = ff_dither_8x8_220[y&7];
  299. int i;
  300. unsigned acc = 0;
  301. int err = 0;
  302. for (i = 0; i < dstW; i += 2) {
  303. int j;
  304. int Y1 = 1 << 18;
  305. int Y2 = 1 << 18;
  306. for (j = 0; j < lumFilterSize; j++) {
  307. Y1 += lumSrc[j][i] * lumFilter[j];
  308. Y2 += lumSrc[j][i+1] * lumFilter[j];
  309. }
  310. Y1 >>= 19;
  311. Y2 >>= 19;
  312. if ((Y1 | Y2) & 0x100) {
  313. Y1 = av_clip_uint8(Y1);
  314. Y2 = av_clip_uint8(Y2);
  315. }
  316. if (c->dither == SWS_DITHER_ED) {
  317. 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;
  318. c->dither_error[0][i] = err;
  319. acc = 2*acc + (Y1 >= 128);
  320. Y1 -= 220*(acc&1);
  321. 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);
  322. c->dither_error[0][i+1] = Y1;
  323. acc = 2*acc + (err >= 128);
  324. err -= 220*(acc&1);
  325. } else {
  326. accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
  327. accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
  328. }
  329. if ((i & 7) == 6) {
  330. output_pixel(*dest++, acc);
  331. }
  332. }
  333. c->dither_error[0][i] = err;
  334. if (i & 6) {
  335. output_pixel(*dest, acc);
  336. }
  337. }
  338. static av_always_inline void
  339. yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
  340. const int16_t *ubuf[2], const int16_t *vbuf[2],
  341. const int16_t *abuf[2], uint8_t *dest, int dstW,
  342. int yalpha, int uvalpha, int y,
  343. enum AVPixelFormat target)
  344. {
  345. const int16_t *buf0 = buf[0], *buf1 = buf[1];
  346. const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
  347. int yalpha1 = 4096 - yalpha;
  348. int i;
  349. if (c->dither == SWS_DITHER_ED) {
  350. int err = 0;
  351. int acc = 0;
  352. for (i = 0; i < dstW; i +=2) {
  353. int Y;
  354. Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
  355. 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;
  356. c->dither_error[0][i] = err;
  357. acc = 2*acc + (Y >= 128);
  358. Y -= 220*(acc&1);
  359. err = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
  360. 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;
  361. c->dither_error[0][i+1] = Y;
  362. acc = 2*acc + (err >= 128);
  363. err -= 220*(acc&1);
  364. if ((i & 7) == 6)
  365. output_pixel(*dest++, acc);
  366. }
  367. c->dither_error[0][i] = err;
  368. } else {
  369. for (i = 0; i < dstW; i += 8) {
  370. int Y, acc = 0;
  371. Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
  372. accumulate_bit(acc, Y + d128[0]);
  373. Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
  374. accumulate_bit(acc, Y + d128[1]);
  375. Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
  376. accumulate_bit(acc, Y + d128[2]);
  377. Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
  378. accumulate_bit(acc, Y + d128[3]);
  379. Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
  380. accumulate_bit(acc, Y + d128[4]);
  381. Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
  382. accumulate_bit(acc, Y + d128[5]);
  383. Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
  384. accumulate_bit(acc, Y + d128[6]);
  385. Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
  386. accumulate_bit(acc, Y + d128[7]);
  387. output_pixel(*dest++, acc);
  388. }
  389. }
  390. }
  391. static av_always_inline void
  392. yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
  393. const int16_t *ubuf[2], const int16_t *vbuf[2],
  394. const int16_t *abuf0, uint8_t *dest, int dstW,
  395. int uvalpha, int y, enum AVPixelFormat target)
  396. {
  397. const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
  398. int i;
  399. if (c->dither == SWS_DITHER_ED) {
  400. int err = 0;
  401. int acc = 0;
  402. for (i = 0; i < dstW; i +=2) {
  403. int Y;
  404. Y = ((buf0[i + 0] + 64) >> 7);
  405. 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;
  406. c->dither_error[0][i] = err;
  407. acc = 2*acc + (Y >= 128);
  408. Y -= 220*(acc&1);
  409. err = ((buf0[i + 1] + 64) >> 7);
  410. 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;
  411. c->dither_error[0][i+1] = Y;
  412. acc = 2*acc + (err >= 128);
  413. err -= 220*(acc&1);
  414. if ((i & 7) == 6)
  415. output_pixel(*dest++, acc);
  416. }
  417. c->dither_error[0][i] = err;
  418. } else {
  419. for (i = 0; i < dstW; i += 8) {
  420. int acc = 0;
  421. accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
  422. accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
  423. accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
  424. accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
  425. accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
  426. accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
  427. accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
  428. accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
  429. output_pixel(*dest++, acc);
  430. }
  431. }
  432. }
  433. #undef output_pixel
  434. #undef accumulate_bit
  435. #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
  436. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  437. const int16_t **lumSrc, int lumFilterSize, \
  438. const int16_t *chrFilter, const int16_t **chrUSrc, \
  439. const int16_t **chrVSrc, int chrFilterSize, \
  440. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  441. int y) \
  442. { \
  443. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  444. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  445. alpSrc, dest, dstW, y, fmt); \
  446. } \
  447. \
  448. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  449. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  450. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  451. int yalpha, int uvalpha, int y) \
  452. { \
  453. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  454. dest, dstW, yalpha, uvalpha, y, fmt); \
  455. } \
  456. \
  457. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  458. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  459. const int16_t *abuf0, uint8_t *dest, int dstW, \
  460. int uvalpha, int y) \
  461. { \
  462. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
  463. abuf0, dest, dstW, uvalpha, \
  464. y, fmt); \
  465. }
  466. YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
  467. YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
  468. #define output_pixels(pos, Y1, U, Y2, V) \
  469. if (target == AV_PIX_FMT_YUYV422) { \
  470. dest[pos + 0] = Y1; \
  471. dest[pos + 1] = U; \
  472. dest[pos + 2] = Y2; \
  473. dest[pos + 3] = V; \
  474. } else { \
  475. dest[pos + 0] = U; \
  476. dest[pos + 1] = Y1; \
  477. dest[pos + 2] = V; \
  478. dest[pos + 3] = Y2; \
  479. }
  480. static av_always_inline void
  481. yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
  482. const int16_t **lumSrc, int lumFilterSize,
  483. const int16_t *chrFilter, const int16_t **chrUSrc,
  484. const int16_t **chrVSrc, int chrFilterSize,
  485. const int16_t **alpSrc, uint8_t *dest, int dstW,
  486. int y, enum AVPixelFormat target)
  487. {
  488. int i;
  489. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  490. int j;
  491. int Y1 = 1 << 18;
  492. int Y2 = 1 << 18;
  493. int U = 1 << 18;
  494. int V = 1 << 18;
  495. for (j = 0; j < lumFilterSize; j++) {
  496. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  497. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  498. }
  499. for (j = 0; j < chrFilterSize; j++) {
  500. U += chrUSrc[j][i] * chrFilter[j];
  501. V += chrVSrc[j][i] * chrFilter[j];
  502. }
  503. Y1 >>= 19;
  504. Y2 >>= 19;
  505. U >>= 19;
  506. V >>= 19;
  507. if ((Y1 | Y2 | U | V) & 0x100) {
  508. Y1 = av_clip_uint8(Y1);
  509. Y2 = av_clip_uint8(Y2);
  510. U = av_clip_uint8(U);
  511. V = av_clip_uint8(V);
  512. }
  513. output_pixels(4*i, Y1, U, Y2, V);
  514. }
  515. }
  516. static av_always_inline void
  517. yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
  518. const int16_t *ubuf[2], const int16_t *vbuf[2],
  519. const int16_t *abuf[2], uint8_t *dest, int dstW,
  520. int yalpha, int uvalpha, int y,
  521. enum AVPixelFormat target)
  522. {
  523. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  524. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  525. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  526. int yalpha1 = 4096 - yalpha;
  527. int uvalpha1 = 4096 - uvalpha;
  528. int i;
  529. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  530. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  531. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  532. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  533. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  534. if ((Y1 | Y2 | U | V) & 0x100) {
  535. Y1 = av_clip_uint8(Y1);
  536. Y2 = av_clip_uint8(Y2);
  537. U = av_clip_uint8(U);
  538. V = av_clip_uint8(V);
  539. }
  540. output_pixels(i * 4, Y1, U, Y2, V);
  541. }
  542. }
  543. static av_always_inline void
  544. yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
  545. const int16_t *ubuf[2], const int16_t *vbuf[2],
  546. const int16_t *abuf0, uint8_t *dest, int dstW,
  547. int uvalpha, int y, enum AVPixelFormat target)
  548. {
  549. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  550. int i;
  551. if (uvalpha < 2048) {
  552. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  553. int Y1 = (buf0[i * 2 ]+64) >> 7;
  554. int Y2 = (buf0[i * 2 + 1]+64) >> 7;
  555. int U = (ubuf0[i] +64) >> 7;
  556. int V = (vbuf0[i] +64) >> 7;
  557. if ((Y1 | Y2 | U | V) & 0x100) {
  558. Y1 = av_clip_uint8(Y1);
  559. Y2 = av_clip_uint8(Y2);
  560. U = av_clip_uint8(U);
  561. V = av_clip_uint8(V);
  562. }
  563. Y1 = av_clip_uint8(Y1);
  564. Y2 = av_clip_uint8(Y2);
  565. U = av_clip_uint8(U);
  566. V = av_clip_uint8(V);
  567. output_pixels(i * 4, Y1, U, Y2, V);
  568. }
  569. } else {
  570. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  571. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  572. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  573. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  574. int U = (ubuf0[i] + ubuf1[i]+128) >> 8;
  575. int V = (vbuf0[i] + vbuf1[i]+128) >> 8;
  576. if ((Y1 | Y2 | U | V) & 0x100) {
  577. Y1 = av_clip_uint8(Y1);
  578. Y2 = av_clip_uint8(Y2);
  579. U = av_clip_uint8(U);
  580. V = av_clip_uint8(V);
  581. }
  582. Y1 = av_clip_uint8(Y1);
  583. Y2 = av_clip_uint8(Y2);
  584. U = av_clip_uint8(U);
  585. V = av_clip_uint8(V);
  586. output_pixels(i * 4, Y1, U, Y2, V);
  587. }
  588. }
  589. }
  590. #undef output_pixels
  591. YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
  592. YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
  593. #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)
  594. #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)
  595. #define output_pixel(pos, val) \
  596. if (isBE(target)) { \
  597. AV_WB16(pos, val); \
  598. } else { \
  599. AV_WL16(pos, val); \
  600. }
  601. static av_always_inline void
  602. yuv2rgba64_X_c_template(SwsContext *c, const int16_t *lumFilter,
  603. const int32_t **lumSrc, int lumFilterSize,
  604. const int16_t *chrFilter, const int32_t **chrUSrc,
  605. const int32_t **chrVSrc, int chrFilterSize,
  606. const int32_t **alpSrc, uint16_t *dest, int dstW,
  607. int y, enum AVPixelFormat target, int hasAlpha)
  608. {
  609. int i;
  610. int A1 = 0xffff<<14, A2 = 0xffff<<14;
  611. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  612. int j;
  613. int Y1 = -0x40000000;
  614. int Y2 = -0x40000000;
  615. int U = -128 << 23; // 19
  616. int V = -128 << 23;
  617. int R, G, B;
  618. for (j = 0; j < lumFilterSize; j++) {
  619. Y1 += lumSrc[j][i * 2] * (unsigned)lumFilter[j];
  620. Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
  621. }
  622. for (j = 0; j < chrFilterSize; j++) {;
  623. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  624. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  625. }
  626. if (hasAlpha) {
  627. A1 = -0x40000000;
  628. A2 = -0x40000000;
  629. for (j = 0; j < lumFilterSize; j++) {
  630. A1 += alpSrc[j][i * 2] * (unsigned)lumFilter[j];
  631. A2 += alpSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
  632. }
  633. A1 >>= 1;
  634. A1 += 0x20002000;
  635. A2 >>= 1;
  636. A2 += 0x20002000;
  637. }
  638. // 8bit: 12+15=27; 16-bit: 12+19=31
  639. Y1 >>= 14; // 10
  640. Y1 += 0x10000;
  641. Y2 >>= 14;
  642. Y2 += 0x10000;
  643. U >>= 14;
  644. V >>= 14;
  645. // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
  646. Y1 -= c->yuv2rgb_y_offset;
  647. Y2 -= c->yuv2rgb_y_offset;
  648. Y1 *= c->yuv2rgb_y_coeff;
  649. Y2 *= c->yuv2rgb_y_coeff;
  650. Y1 += 1 << 13; // 21
  651. Y2 += 1 << 13;
  652. // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
  653. R = V * c->yuv2rgb_v2r_coeff;
  654. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  655. B = U * c->yuv2rgb_u2b_coeff;
  656. // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
  657. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  658. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  659. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  660. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  661. output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
  662. output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
  663. output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
  664. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  665. dest += 8;
  666. }
  667. }
  668. static av_always_inline void
  669. yuv2rgba64_2_c_template(SwsContext *c, const int32_t *buf[2],
  670. const int32_t *ubuf[2], const int32_t *vbuf[2],
  671. const int32_t *abuf[2], uint16_t *dest, int dstW,
  672. int yalpha, int uvalpha, int y,
  673. enum AVPixelFormat target, int hasAlpha)
  674. {
  675. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  676. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  677. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  678. *abuf0 = hasAlpha ? abuf[0] : NULL,
  679. *abuf1 = hasAlpha ? abuf[1] : NULL;
  680. int yalpha1 = 4096 - yalpha;
  681. int uvalpha1 = 4096 - uvalpha;
  682. int i;
  683. int A1 = 0xffff<<14, A2 = 0xffff<<14;
  684. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  685. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
  686. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
  687. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
  688. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
  689. int R, G, B;
  690. Y1 -= c->yuv2rgb_y_offset;
  691. Y2 -= c->yuv2rgb_y_offset;
  692. Y1 *= c->yuv2rgb_y_coeff;
  693. Y2 *= c->yuv2rgb_y_coeff;
  694. Y1 += 1 << 13;
  695. Y2 += 1 << 13;
  696. R = V * c->yuv2rgb_v2r_coeff;
  697. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  698. B = U * c->yuv2rgb_u2b_coeff;
  699. if (hasAlpha) {
  700. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 1;
  701. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 1;
  702. A1 += 1 << 13;
  703. A2 += 1 << 13;
  704. }
  705. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  706. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  707. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  708. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  709. output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
  710. output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
  711. output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
  712. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  713. dest += 8;
  714. }
  715. }
  716. static av_always_inline void
  717. yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0,
  718. const int32_t *ubuf[2], const int32_t *vbuf[2],
  719. const int32_t *abuf0, uint16_t *dest, int dstW,
  720. int uvalpha, int y, enum AVPixelFormat target, int hasAlpha)
  721. {
  722. const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  723. int i;
  724. int A1 = 0xffff<<14, A2= 0xffff<<14;
  725. if (uvalpha < 2048) {
  726. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  727. int Y1 = (buf0[i * 2] ) >> 2;
  728. int Y2 = (buf0[i * 2 + 1]) >> 2;
  729. int U = (ubuf0[i] + (-128 << 11)) >> 2;
  730. int V = (vbuf0[i] + (-128 << 11)) >> 2;
  731. int R, G, B;
  732. Y1 -= c->yuv2rgb_y_offset;
  733. Y2 -= c->yuv2rgb_y_offset;
  734. Y1 *= c->yuv2rgb_y_coeff;
  735. Y2 *= c->yuv2rgb_y_coeff;
  736. Y1 += 1 << 13;
  737. Y2 += 1 << 13;
  738. if (hasAlpha) {
  739. A1 = abuf0[i * 2 ] << 11;
  740. A2 = abuf0[i * 2 + 1] << 11;
  741. A1 += 1 << 13;
  742. A2 += 1 << 13;
  743. }
  744. R = V * c->yuv2rgb_v2r_coeff;
  745. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  746. B = U * c->yuv2rgb_u2b_coeff;
  747. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  748. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  749. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  750. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  751. output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
  752. output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
  753. output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
  754. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  755. dest += 8;
  756. }
  757. } else {
  758. const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  759. int A1 = 0xffff<<14, A2 = 0xffff<<14;
  760. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  761. int Y1 = (buf0[i * 2] ) >> 2;
  762. int Y2 = (buf0[i * 2 + 1]) >> 2;
  763. int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
  764. int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
  765. int R, G, B;
  766. Y1 -= c->yuv2rgb_y_offset;
  767. Y2 -= c->yuv2rgb_y_offset;
  768. Y1 *= c->yuv2rgb_y_coeff;
  769. Y2 *= c->yuv2rgb_y_coeff;
  770. Y1 += 1 << 13;
  771. Y2 += 1 << 13;
  772. if (hasAlpha) {
  773. A1 = abuf0[i * 2 ] << 11;
  774. A2 = abuf0[i * 2 + 1] << 11;
  775. A1 += 1 << 13;
  776. A2 += 1 << 13;
  777. }
  778. R = V * c->yuv2rgb_v2r_coeff;
  779. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  780. B = U * c->yuv2rgb_u2b_coeff;
  781. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  782. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  783. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  784. output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
  785. output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
  786. output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
  787. output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
  788. output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
  789. dest += 8;
  790. }
  791. }
  792. }
  793. static av_always_inline void
  794. yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
  795. const int32_t **lumSrc, int lumFilterSize,
  796. const int16_t *chrFilter, const int32_t **chrUSrc,
  797. const int32_t **chrVSrc, int chrFilterSize,
  798. const int32_t **alpSrc, uint16_t *dest, int dstW,
  799. int y, enum AVPixelFormat target, int hasAlpha)
  800. {
  801. int i;
  802. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  803. int j;
  804. int Y1 = -0x40000000;
  805. int Y2 = -0x40000000;
  806. int U = -128 << 23; // 19
  807. int V = -128 << 23;
  808. int R, G, B;
  809. for (j = 0; j < lumFilterSize; j++) {
  810. Y1 += lumSrc[j][i * 2] * (unsigned)lumFilter[j];
  811. Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
  812. }
  813. for (j = 0; j < chrFilterSize; j++) {;
  814. U += chrUSrc[j][i] * (unsigned)chrFilter[j];
  815. V += chrVSrc[j][i] * (unsigned)chrFilter[j];
  816. }
  817. // 8bit: 12+15=27; 16-bit: 12+19=31
  818. Y1 >>= 14; // 10
  819. Y1 += 0x10000;
  820. Y2 >>= 14;
  821. Y2 += 0x10000;
  822. U >>= 14;
  823. V >>= 14;
  824. // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
  825. Y1 -= c->yuv2rgb_y_offset;
  826. Y2 -= c->yuv2rgb_y_offset;
  827. Y1 *= c->yuv2rgb_y_coeff;
  828. Y2 *= c->yuv2rgb_y_coeff;
  829. Y1 += 1 << 13; // 21
  830. Y2 += 1 << 13;
  831. // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
  832. R = V * c->yuv2rgb_v2r_coeff;
  833. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  834. B = U * c->yuv2rgb_u2b_coeff;
  835. // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
  836. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  837. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  838. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  839. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  840. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  841. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  842. dest += 6;
  843. }
  844. }
  845. static av_always_inline void
  846. yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
  847. const int32_t *ubuf[2], const int32_t *vbuf[2],
  848. const int32_t *abuf[2], uint16_t *dest, int dstW,
  849. int yalpha, int uvalpha, int y,
  850. enum AVPixelFormat target, int hasAlpha)
  851. {
  852. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  853. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  854. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  855. int yalpha1 = 4096 - yalpha;
  856. int uvalpha1 = 4096 - uvalpha;
  857. int i;
  858. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  859. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
  860. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
  861. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
  862. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
  863. int R, G, B;
  864. Y1 -= c->yuv2rgb_y_offset;
  865. Y2 -= c->yuv2rgb_y_offset;
  866. Y1 *= c->yuv2rgb_y_coeff;
  867. Y2 *= c->yuv2rgb_y_coeff;
  868. Y1 += 1 << 13;
  869. Y2 += 1 << 13;
  870. R = V * c->yuv2rgb_v2r_coeff;
  871. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  872. B = U * c->yuv2rgb_u2b_coeff;
  873. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  874. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  875. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  876. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  877. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  878. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  879. dest += 6;
  880. }
  881. }
  882. static av_always_inline void
  883. yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
  884. const int32_t *ubuf[2], const int32_t *vbuf[2],
  885. const int32_t *abuf0, uint16_t *dest, int dstW,
  886. int uvalpha, int y, enum AVPixelFormat target, int hasAlpha)
  887. {
  888. const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  889. int i;
  890. if (uvalpha < 2048) {
  891. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  892. int Y1 = (buf0[i * 2] ) >> 2;
  893. int Y2 = (buf0[i * 2 + 1]) >> 2;
  894. int U = (ubuf0[i] + (-128 << 11)) >> 2;
  895. int V = (vbuf0[i] + (-128 << 11)) >> 2;
  896. int R, G, B;
  897. Y1 -= c->yuv2rgb_y_offset;
  898. Y2 -= c->yuv2rgb_y_offset;
  899. Y1 *= c->yuv2rgb_y_coeff;
  900. Y2 *= c->yuv2rgb_y_coeff;
  901. Y1 += 1 << 13;
  902. Y2 += 1 << 13;
  903. R = V * c->yuv2rgb_v2r_coeff;
  904. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  905. B = U * c->yuv2rgb_u2b_coeff;
  906. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  907. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  908. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  909. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  910. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  911. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  912. dest += 6;
  913. }
  914. } else {
  915. const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  916. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  917. int Y1 = (buf0[i * 2] ) >> 2;
  918. int Y2 = (buf0[i * 2 + 1]) >> 2;
  919. int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
  920. int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
  921. int R, G, B;
  922. Y1 -= c->yuv2rgb_y_offset;
  923. Y2 -= c->yuv2rgb_y_offset;
  924. Y1 *= c->yuv2rgb_y_coeff;
  925. Y2 *= c->yuv2rgb_y_coeff;
  926. Y1 += 1 << 13;
  927. Y2 += 1 << 13;
  928. R = V * c->yuv2rgb_v2r_coeff;
  929. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  930. B = U * c->yuv2rgb_u2b_coeff;
  931. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  932. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  933. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  934. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  935. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  936. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  937. dest += 6;
  938. }
  939. }
  940. }
  941. #undef output_pixel
  942. #undef r_b
  943. #undef b_r
  944. #define YUV2PACKED16WRAPPER(name, base, ext, fmt, hasAlpha) \
  945. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  946. const int16_t **_lumSrc, int lumFilterSize, \
  947. const int16_t *chrFilter, const int16_t **_chrUSrc, \
  948. const int16_t **_chrVSrc, int chrFilterSize, \
  949. const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
  950. int y) \
  951. { \
  952. const int32_t **lumSrc = (const int32_t **) _lumSrc, \
  953. **chrUSrc = (const int32_t **) _chrUSrc, \
  954. **chrVSrc = (const int32_t **) _chrVSrc, \
  955. **alpSrc = (const int32_t **) _alpSrc; \
  956. uint16_t *dest = (uint16_t *) _dest; \
  957. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  958. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  959. alpSrc, dest, dstW, y, fmt, hasAlpha); \
  960. } \
  961. \
  962. static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
  963. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  964. const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
  965. int yalpha, int uvalpha, int y) \
  966. { \
  967. const int32_t **buf = (const int32_t **) _buf, \
  968. **ubuf = (const int32_t **) _ubuf, \
  969. **vbuf = (const int32_t **) _vbuf, \
  970. **abuf = (const int32_t **) _abuf; \
  971. uint16_t *dest = (uint16_t *) _dest; \
  972. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  973. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
  974. } \
  975. \
  976. static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
  977. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  978. const int16_t *_abuf0, uint8_t *_dest, int dstW, \
  979. int uvalpha, int y) \
  980. { \
  981. const int32_t *buf0 = (const int32_t *) _buf0, \
  982. **ubuf = (const int32_t **) _ubuf, \
  983. **vbuf = (const int32_t **) _vbuf, \
  984. *abuf0 = (const int32_t *) _abuf0; \
  985. uint16_t *dest = (uint16_t *) _dest; \
  986. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  987. dstW, uvalpha, y, fmt, hasAlpha); \
  988. }
  989. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, AV_PIX_FMT_RGB48BE, 0)
  990. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, AV_PIX_FMT_RGB48LE, 0)
  991. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, AV_PIX_FMT_BGR48BE, 0)
  992. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, AV_PIX_FMT_BGR48LE, 0)
  993. YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64be, AV_PIX_FMT_RGBA64BE, 1)
  994. YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64le, AV_PIX_FMT_RGBA64LE, 1)
  995. YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64be, AV_PIX_FMT_RGBA64BE, 0)
  996. YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64le, AV_PIX_FMT_RGBA64LE, 0)
  997. YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64be, AV_PIX_FMT_BGRA64BE, 1)
  998. YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64le, AV_PIX_FMT_BGRA64LE, 1)
  999. YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64be, AV_PIX_FMT_BGRA64BE, 0)
  1000. YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64le, AV_PIX_FMT_BGRA64LE, 0)
  1001. /*
  1002. * Write out 2 RGB pixels in the target pixel format. This function takes a
  1003. * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
  1004. * things like endianness conversion and shifting. The caller takes care of
  1005. * setting the correct offset in these tables from the chroma (U/V) values.
  1006. * This function then uses the luminance (Y1/Y2) values to write out the
  1007. * correct RGB values into the destination buffer.
  1008. */
  1009. static av_always_inline void
  1010. yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
  1011. unsigned A1, unsigned A2,
  1012. const void *_r, const void *_g, const void *_b, int y,
  1013. enum AVPixelFormat target, int hasAlpha)
  1014. {
  1015. if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
  1016. target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
  1017. uint32_t *dest = (uint32_t *) _dest;
  1018. const uint32_t *r = (const uint32_t *) _r;
  1019. const uint32_t *g = (const uint32_t *) _g;
  1020. const uint32_t *b = (const uint32_t *) _b;
  1021. #if CONFIG_SMALL
  1022. int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
  1023. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
  1024. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
  1025. #else
  1026. if (hasAlpha) {
  1027. int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
  1028. av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0);
  1029. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
  1030. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
  1031. } else {
  1032. #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
  1033. int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
  1034. av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0xFF);
  1035. #endif
  1036. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
  1037. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
  1038. }
  1039. #endif
  1040. } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
  1041. uint8_t *dest = (uint8_t *) _dest;
  1042. const uint8_t *r = (const uint8_t *) _r;
  1043. const uint8_t *g = (const uint8_t *) _g;
  1044. const uint8_t *b = (const uint8_t *) _b;
  1045. #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
  1046. #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
  1047. dest[i * 6 + 0] = r_b[Y1];
  1048. dest[i * 6 + 1] = g[Y1];
  1049. dest[i * 6 + 2] = b_r[Y1];
  1050. dest[i * 6 + 3] = r_b[Y2];
  1051. dest[i * 6 + 4] = g[Y2];
  1052. dest[i * 6 + 5] = b_r[Y2];
  1053. #undef r_b
  1054. #undef b_r
  1055. } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
  1056. target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
  1057. target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
  1058. uint16_t *dest = (uint16_t *) _dest;
  1059. const uint16_t *r = (const uint16_t *) _r;
  1060. const uint16_t *g = (const uint16_t *) _g;
  1061. const uint16_t *b = (const uint16_t *) _b;
  1062. int dr1, dg1, db1, dr2, dg2, db2;
  1063. if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
  1064. dr1 = ff_dither_2x2_8[ y & 1 ][0];
  1065. dg1 = ff_dither_2x2_4[ y & 1 ][0];
  1066. db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
  1067. dr2 = ff_dither_2x2_8[ y & 1 ][1];
  1068. dg2 = ff_dither_2x2_4[ y & 1 ][1];
  1069. db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
  1070. } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
  1071. dr1 = ff_dither_2x2_8[ y & 1 ][0];
  1072. dg1 = ff_dither_2x2_8[ y & 1 ][1];
  1073. db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
  1074. dr2 = ff_dither_2x2_8[ y & 1 ][1];
  1075. dg2 = ff_dither_2x2_8[ y & 1 ][0];
  1076. db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
  1077. } else {
  1078. dr1 = ff_dither_4x4_16[ y & 3 ][0];
  1079. dg1 = ff_dither_4x4_16[ y & 3 ][1];
  1080. db1 = ff_dither_4x4_16[(y & 3) ^ 3][0];
  1081. dr2 = ff_dither_4x4_16[ y & 3 ][1];
  1082. dg2 = ff_dither_4x4_16[ y & 3 ][0];
  1083. db2 = ff_dither_4x4_16[(y & 3) ^ 3][1];
  1084. }
  1085. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1086. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1087. } else /* 8/4-bit */ {
  1088. uint8_t *dest = (uint8_t *) _dest;
  1089. const uint8_t *r = (const uint8_t *) _r;
  1090. const uint8_t *g = (const uint8_t *) _g;
  1091. const uint8_t *b = (const uint8_t *) _b;
  1092. int dr1, dg1, db1, dr2, dg2, db2;
  1093. if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
  1094. const uint8_t * const d64 = ff_dither_8x8_73[y & 7];
  1095. const uint8_t * const d32 = ff_dither_8x8_32[y & 7];
  1096. dr1 = dg1 = d32[(i * 2 + 0) & 7];
  1097. db1 = d64[(i * 2 + 0) & 7];
  1098. dr2 = dg2 = d32[(i * 2 + 1) & 7];
  1099. db2 = d64[(i * 2 + 1) & 7];
  1100. } else {
  1101. const uint8_t * const d64 = ff_dither_8x8_73 [y & 7];
  1102. const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
  1103. dr1 = db1 = d128[(i * 2 + 0) & 7];
  1104. dg1 = d64[(i * 2 + 0) & 7];
  1105. dr2 = db2 = d128[(i * 2 + 1) & 7];
  1106. dg2 = d64[(i * 2 + 1) & 7];
  1107. }
  1108. if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
  1109. dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
  1110. ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
  1111. } else {
  1112. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1113. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1114. }
  1115. }
  1116. }
  1117. static av_always_inline void
  1118. yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1119. const int16_t **lumSrc, int lumFilterSize,
  1120. const int16_t *chrFilter, const int16_t **chrUSrc,
  1121. const int16_t **chrVSrc, int chrFilterSize,
  1122. const int16_t **alpSrc, uint8_t *dest, int dstW,
  1123. int y, enum AVPixelFormat target, int hasAlpha)
  1124. {
  1125. int i;
  1126. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1127. int j, A1, A2;
  1128. int Y1 = 1 << 18;
  1129. int Y2 = 1 << 18;
  1130. int U = 1 << 18;
  1131. int V = 1 << 18;
  1132. const void *r, *g, *b;
  1133. for (j = 0; j < lumFilterSize; j++) {
  1134. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  1135. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  1136. }
  1137. for (j = 0; j < chrFilterSize; j++) {
  1138. U += chrUSrc[j][i] * chrFilter[j];
  1139. V += chrVSrc[j][i] * chrFilter[j];
  1140. }
  1141. Y1 >>= 19;
  1142. Y2 >>= 19;
  1143. U >>= 19;
  1144. V >>= 19;
  1145. if (hasAlpha) {
  1146. A1 = 1 << 18;
  1147. A2 = 1 << 18;
  1148. for (j = 0; j < lumFilterSize; j++) {
  1149. A1 += alpSrc[j][i * 2 ] * lumFilter[j];
  1150. A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
  1151. }
  1152. A1 >>= 19;
  1153. A2 >>= 19;
  1154. if ((A1 | A2) & 0x100) {
  1155. A1 = av_clip_uint8(A1);
  1156. A2 = av_clip_uint8(A2);
  1157. }
  1158. }
  1159. r = c->table_rV[V + YUVRGB_TABLE_HEADROOM];
  1160. g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
  1161. b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1162. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1163. r, g, b, y, target, hasAlpha);
  1164. }
  1165. }
  1166. static av_always_inline void
  1167. yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
  1168. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1169. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1170. int yalpha, int uvalpha, int y,
  1171. enum AVPixelFormat target, int hasAlpha)
  1172. {
  1173. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1174. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1175. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1176. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1177. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1178. int yalpha1 = 4096 - yalpha;
  1179. int uvalpha1 = 4096 - uvalpha;
  1180. int i;
  1181. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1182. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  1183. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  1184. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  1185. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  1186. int A1, A2;
  1187. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1188. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1189. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1190. if (hasAlpha) {
  1191. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
  1192. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
  1193. A1 = av_clip_uint8(A1);
  1194. A2 = av_clip_uint8(A2);
  1195. }
  1196. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1197. r, g, b, y, target, hasAlpha);
  1198. }
  1199. }
  1200. static av_always_inline void
  1201. yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
  1202. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1203. const int16_t *abuf0, uint8_t *dest, int dstW,
  1204. int uvalpha, int y, enum AVPixelFormat target,
  1205. int hasAlpha)
  1206. {
  1207. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1208. int i;
  1209. if (uvalpha < 2048) {
  1210. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1211. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  1212. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  1213. int U = (ubuf0[i] + 64) >> 7;
  1214. int V = (vbuf0[i] + 64) >> 7;
  1215. int A1, A2;
  1216. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1217. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1218. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1219. if (hasAlpha) {
  1220. A1 = abuf0[i * 2 ] * 255 + 16384 >> 15;
  1221. A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
  1222. A1 = av_clip_uint8(A1);
  1223. A2 = av_clip_uint8(A2);
  1224. }
  1225. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1226. r, g, b, y, target, hasAlpha);
  1227. }
  1228. } else {
  1229. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1230. for (i = 0; i < ((dstW + 1) >> 1); i++) {
  1231. int Y1 = (buf0[i * 2 ] + 64) >> 7;
  1232. int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
  1233. int U = (ubuf0[i] + ubuf1[i] + 128) >> 8;
  1234. int V = (vbuf0[i] + vbuf1[i] + 128) >> 8;
  1235. int A1, A2;
  1236. const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
  1237. *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
  1238. *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
  1239. if (hasAlpha) {
  1240. A1 = (abuf0[i * 2 ] + 64) >> 7;
  1241. A2 = (abuf0[i * 2 + 1] + 64) >> 7;
  1242. A1 = av_clip_uint8(A1);
  1243. A2 = av_clip_uint8(A2);
  1244. }
  1245. yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1246. r, g, b, y, target, hasAlpha);
  1247. }
  1248. }
  1249. }
  1250. #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1251. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  1252. const int16_t **lumSrc, int lumFilterSize, \
  1253. const int16_t *chrFilter, const int16_t **chrUSrc, \
  1254. const int16_t **chrVSrc, int chrFilterSize, \
  1255. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  1256. int y) \
  1257. { \
  1258. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  1259. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  1260. alpSrc, dest, dstW, y, fmt, hasAlpha); \
  1261. }
  1262. #define YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
  1263. YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1264. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  1265. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1266. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  1267. int yalpha, int uvalpha, int y) \
  1268. { \
  1269. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  1270. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
  1271. }
  1272. #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
  1273. YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
  1274. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  1275. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1276. const int16_t *abuf0, uint8_t *dest, int dstW, \
  1277. int uvalpha, int y) \
  1278. { \
  1279. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  1280. dstW, uvalpha, y, fmt, hasAlpha); \
  1281. }
  1282. #if CONFIG_SMALL
  1283. YUV2RGBWRAPPER(yuv2rgb,, 32_1, AV_PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1284. YUV2RGBWRAPPER(yuv2rgb,, 32, AV_PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1285. #else
  1286. #if CONFIG_SWSCALE_ALPHA
  1287. YUV2RGBWRAPPER(yuv2rgb,, a32_1, AV_PIX_FMT_RGB32_1, 1)
  1288. YUV2RGBWRAPPER(yuv2rgb,, a32, AV_PIX_FMT_RGB32, 1)
  1289. #endif
  1290. YUV2RGBWRAPPER(yuv2rgb,, x32_1, AV_PIX_FMT_RGB32_1, 0)
  1291. YUV2RGBWRAPPER(yuv2rgb,, x32, AV_PIX_FMT_RGB32, 0)
  1292. #endif
  1293. YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0)
  1294. YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0)
  1295. YUV2RGBWRAPPER(yuv2rgb,, 16, AV_PIX_FMT_RGB565, 0)
  1296. YUV2RGBWRAPPER(yuv2rgb,, 15, AV_PIX_FMT_RGB555, 0)
  1297. YUV2RGBWRAPPER(yuv2rgb,, 12, AV_PIX_FMT_RGB444, 0)
  1298. YUV2RGBWRAPPER(yuv2rgb,, 8, AV_PIX_FMT_RGB8, 0)
  1299. YUV2RGBWRAPPER(yuv2rgb,, 4, AV_PIX_FMT_RGB4, 0)
  1300. YUV2RGBWRAPPER(yuv2rgb,, 4b, AV_PIX_FMT_RGB4_BYTE, 0)
  1301. static av_always_inline void yuv2rgb_write_full(SwsContext *c,
  1302. uint8_t *dest, int i, int Y, int A, int U, int V,
  1303. int y, enum AVPixelFormat target, int hasAlpha, int err[4])
  1304. {
  1305. int R, G, B;
  1306. int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
  1307. Y -= c->yuv2rgb_y_offset;
  1308. Y *= c->yuv2rgb_y_coeff;
  1309. Y += 1 << 21;
  1310. R = Y + V*c->yuv2rgb_v2r_coeff;
  1311. G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
  1312. B = Y + U*c->yuv2rgb_u2b_coeff;
  1313. if ((R | G | B) & 0xC0000000) {
  1314. R = av_clip_uintp2(R, 30);
  1315. G = av_clip_uintp2(G, 30);
  1316. B = av_clip_uintp2(B, 30);
  1317. }
  1318. switch(target) {
  1319. case AV_PIX_FMT_ARGB:
  1320. dest[0] = hasAlpha ? A : 255;
  1321. dest[1] = R >> 22;
  1322. dest[2] = G >> 22;
  1323. dest[3] = B >> 22;
  1324. break;
  1325. case AV_PIX_FMT_RGB24:
  1326. dest[0] = R >> 22;
  1327. dest[1] = G >> 22;
  1328. dest[2] = B >> 22;
  1329. break;
  1330. case AV_PIX_FMT_RGBA:
  1331. dest[0] = R >> 22;
  1332. dest[1] = G >> 22;
  1333. dest[2] = B >> 22;
  1334. dest[3] = hasAlpha ? A : 255;
  1335. break;
  1336. case AV_PIX_FMT_ABGR:
  1337. dest[0] = hasAlpha ? A : 255;
  1338. dest[1] = B >> 22;
  1339. dest[2] = G >> 22;
  1340. dest[3] = R >> 22;
  1341. break;
  1342. case AV_PIX_FMT_BGR24:
  1343. dest[0] = B >> 22;
  1344. dest[1] = G >> 22;
  1345. dest[2] = R >> 22;
  1346. break;
  1347. case AV_PIX_FMT_BGRA:
  1348. dest[0] = B >> 22;
  1349. dest[1] = G >> 22;
  1350. dest[2] = R >> 22;
  1351. dest[3] = hasAlpha ? A : 255;
  1352. break;
  1353. case AV_PIX_FMT_BGR4_BYTE:
  1354. case AV_PIX_FMT_RGB4_BYTE:
  1355. case AV_PIX_FMT_BGR8:
  1356. case AV_PIX_FMT_RGB8:
  1357. {
  1358. int r,g,b;
  1359. switch (c->dither) {
  1360. default:
  1361. case SWS_DITHER_AUTO:
  1362. case SWS_DITHER_ED:
  1363. R >>= 22;
  1364. G >>= 22;
  1365. B >>= 22;
  1366. 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;
  1367. 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;
  1368. 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;
  1369. c->dither_error[0][i] = err[0];
  1370. c->dither_error[1][i] = err[1];
  1371. c->dither_error[2][i] = err[2];
  1372. r = R >> (isrgb8 ? 5 : 7);
  1373. g = G >> (isrgb8 ? 5 : 6);
  1374. b = B >> (isrgb8 ? 6 : 7);
  1375. r = av_clip(r, 0, isrgb8 ? 7 : 1);
  1376. g = av_clip(g, 0, isrgb8 ? 7 : 3);
  1377. b = av_clip(b, 0, isrgb8 ? 3 : 1);
  1378. err[0] = R - r*(isrgb8 ? 36 : 255);
  1379. err[1] = G - g*(isrgb8 ? 36 : 85);
  1380. err[2] = B - b*(isrgb8 ? 85 : 255);
  1381. break;
  1382. case SWS_DITHER_A_DITHER:
  1383. if (isrgb8) {
  1384. /* see http://pippin.gimp.org/a_dither/ for details/origin */
  1385. #define A_DITHER(u,v) (((((u)+((v)*236))*119)&0xff))
  1386. r = (((R >> 19) + A_DITHER(i,y) -96)>>8);
  1387. g = (((G >> 19) + A_DITHER(i + 17,y) - 96)>>8);
  1388. b = (((B >> 20) + A_DITHER(i + 17*2,y) -96)>>8);
  1389. r = av_clip(r, 0, 7);
  1390. g = av_clip(g, 0, 7);
  1391. b = av_clip(b, 0, 3);
  1392. } else {
  1393. r = (((R >> 21) + A_DITHER(i,y)-256)>>8);
  1394. g = (((G >> 19) + A_DITHER(i + 17,y)-256)>>8);
  1395. b = (((B >> 21) + A_DITHER(i + 17*2,y)-256)>>8);
  1396. r = av_clip(r, 0, 1);
  1397. g = av_clip(g, 0, 3);
  1398. b = av_clip(b, 0, 1);
  1399. }
  1400. break;
  1401. case SWS_DITHER_X_DITHER:
  1402. if (isrgb8) {
  1403. /* see http://pippin.gimp.org/a_dither/ for details/origin */
  1404. #define X_DITHER(u,v) (((((u)^((v)*237))*181)&0x1ff)/2)
  1405. r = (((R >> 19) + X_DITHER(i,y) - 96)>>8);
  1406. g = (((G >> 19) + X_DITHER(i + 17,y) - 96)>>8);
  1407. b = (((B >> 20) + X_DITHER(i + 17*2,y) - 96)>>8);
  1408. r = av_clip(r, 0, 7);
  1409. g = av_clip(g, 0, 7);
  1410. b = av_clip(b, 0, 3);
  1411. } else {
  1412. r = (((R >> 21) + X_DITHER(i,y)-256)>>8);
  1413. g = (((G >> 19) + X_DITHER(i + 17,y)-256)>>8);
  1414. b = (((B >> 21) + X_DITHER(i + 17*2,y)-256)>>8);
  1415. r = av_clip(r, 0, 1);
  1416. g = av_clip(g, 0, 3);
  1417. b = av_clip(b, 0, 1);
  1418. }
  1419. break;
  1420. }
  1421. if(target == AV_PIX_FMT_BGR4_BYTE) {
  1422. dest[0] = r + 2*g + 8*b;
  1423. } else if(target == AV_PIX_FMT_RGB4_BYTE) {
  1424. dest[0] = b + 2*g + 8*r;
  1425. } else if(target == AV_PIX_FMT_BGR8) {
  1426. dest[0] = r + 8*g + 64*b;
  1427. } else if(target == AV_PIX_FMT_RGB8) {
  1428. dest[0] = b + 4*g + 32*r;
  1429. } else
  1430. av_assert2(0);
  1431. break;}
  1432. }
  1433. }
  1434. static av_always_inline void
  1435. yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1436. const int16_t **lumSrc, int lumFilterSize,
  1437. const int16_t *chrFilter, const int16_t **chrUSrc,
  1438. const int16_t **chrVSrc, int chrFilterSize,
  1439. const int16_t **alpSrc, uint8_t *dest,
  1440. int dstW, int y, enum AVPixelFormat target, int hasAlpha)
  1441. {
  1442. int i;
  1443. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1444. int err[4] = {0};
  1445. int A = 0; //init to silence warning
  1446. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1447. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1448. step = 1;
  1449. for (i = 0; i < dstW; i++) {
  1450. int j;
  1451. int Y = 1<<9;
  1452. int U = (1<<9)-(128 << 19);
  1453. int V = (1<<9)-(128 << 19);
  1454. for (j = 0; j < lumFilterSize; j++) {
  1455. Y += lumSrc[j][i] * lumFilter[j];
  1456. }
  1457. for (j = 0; j < chrFilterSize; j++) {
  1458. U += chrUSrc[j][i] * chrFilter[j];
  1459. V += chrVSrc[j][i] * chrFilter[j];
  1460. }
  1461. Y >>= 10;
  1462. U >>= 10;
  1463. V >>= 10;
  1464. if (hasAlpha) {
  1465. A = 1 << 18;
  1466. for (j = 0; j < lumFilterSize; j++) {
  1467. A += alpSrc[j][i] * lumFilter[j];
  1468. }
  1469. A >>= 19;
  1470. if (A & 0x100)
  1471. A = av_clip_uint8(A);
  1472. }
  1473. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1474. dest += step;
  1475. }
  1476. c->dither_error[0][i] = err[0];
  1477. c->dither_error[1][i] = err[1];
  1478. c->dither_error[2][i] = err[2];
  1479. }
  1480. static av_always_inline void
  1481. yuv2rgb_full_2_c_template(SwsContext *c, const int16_t *buf[2],
  1482. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1483. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1484. int yalpha, int uvalpha, int y,
  1485. enum AVPixelFormat target, int hasAlpha)
  1486. {
  1487. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1488. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1489. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1490. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1491. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1492. int yalpha1 = 4096 - yalpha;
  1493. int uvalpha1 = 4096 - uvalpha;
  1494. int i;
  1495. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1496. int err[4] = {0};
  1497. int A = 0; // init to silcene warning
  1498. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1499. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1500. step = 1;
  1501. for (i = 0; i < dstW; i++) {
  1502. int Y = ( buf0[i] * yalpha1 + buf1[i] * yalpha ) >> 10; //FIXME rounding
  1503. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha-(128 << 19)) >> 10;
  1504. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha-(128 << 19)) >> 10;
  1505. if (hasAlpha) {
  1506. A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha + (1<<18)) >> 19;
  1507. if (A & 0x100)
  1508. A = av_clip_uint8(A);
  1509. }
  1510. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1511. dest += step;
  1512. }
  1513. c->dither_error[0][i] = err[0];
  1514. c->dither_error[1][i] = err[1];
  1515. c->dither_error[2][i] = err[2];
  1516. }
  1517. static av_always_inline void
  1518. yuv2rgb_full_1_c_template(SwsContext *c, const int16_t *buf0,
  1519. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1520. const int16_t *abuf0, uint8_t *dest, int dstW,
  1521. int uvalpha, int y, enum AVPixelFormat target,
  1522. int hasAlpha)
  1523. {
  1524. const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
  1525. int i;
  1526. int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
  1527. int err[4] = {0};
  1528. if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
  1529. || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
  1530. step = 1;
  1531. if (uvalpha < 2048) {
  1532. int A = 0; //init to silence warning
  1533. for (i = 0; i < dstW; i++) {
  1534. int Y = buf0[i] << 2;
  1535. int U = (ubuf0[i] - (128<<7)) << 2;
  1536. int V = (vbuf0[i] - (128<<7)) << 2;
  1537. if (hasAlpha) {
  1538. A = (abuf0[i] + 64) >> 7;
  1539. if (A & 0x100)
  1540. A = av_clip_uint8(A);
  1541. }
  1542. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1543. dest += step;
  1544. }
  1545. } else {
  1546. const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
  1547. int A = 0; //init to silence warning
  1548. for (i = 0; i < dstW; i++) {
  1549. int Y = buf0[i] << 2;
  1550. int U = (ubuf0[i] + ubuf1[i] - (128<<8)) << 1;
  1551. int V = (vbuf0[i] + vbuf1[i] - (128<<8)) << 1;
  1552. if (hasAlpha) {
  1553. A = (abuf0[i] + 64) >> 7;
  1554. if (A & 0x100)
  1555. A = av_clip_uint8(A);
  1556. }
  1557. yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
  1558. dest += step;
  1559. }
  1560. }
  1561. c->dither_error[0][i] = err[0];
  1562. c->dither_error[1][i] = err[1];
  1563. c->dither_error[2][i] = err[2];
  1564. }
  1565. #if CONFIG_SMALL
  1566. YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1567. YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1568. YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1569. YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1570. #else
  1571. #if CONFIG_SWSCALE_ALPHA
  1572. YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, 1)
  1573. YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, 1)
  1574. YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, 1)
  1575. YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, 1)
  1576. #endif
  1577. YUV2RGBWRAPPER(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA, 0)
  1578. YUV2RGBWRAPPER(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR, 0)
  1579. YUV2RGBWRAPPER(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA, 0)
  1580. YUV2RGBWRAPPER(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB, 0)
  1581. #endif
  1582. YUV2RGBWRAPPER(yuv2, rgb_full, bgr24_full, AV_PIX_FMT_BGR24, 0)
  1583. YUV2RGBWRAPPER(yuv2, rgb_full, rgb24_full, AV_PIX_FMT_RGB24, 0)
  1584. YUV2RGBWRAPPER(yuv2, rgb_full, bgr4_byte_full, AV_PIX_FMT_BGR4_BYTE, 0)
  1585. YUV2RGBWRAPPER(yuv2, rgb_full, rgb4_byte_full, AV_PIX_FMT_RGB4_BYTE, 0)
  1586. YUV2RGBWRAPPER(yuv2, rgb_full, bgr8_full, AV_PIX_FMT_BGR8, 0)
  1587. YUV2RGBWRAPPER(yuv2, rgb_full, rgb8_full, AV_PIX_FMT_RGB8, 0)
  1588. static void
  1589. yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
  1590. const int16_t **lumSrc, int lumFilterSize,
  1591. const int16_t *chrFilter, const int16_t **chrUSrc,
  1592. const int16_t **chrVSrc, int chrFilterSize,
  1593. const int16_t **alpSrc, uint8_t **dest,
  1594. int dstW, int y)
  1595. {
  1596. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  1597. int i;
  1598. int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;
  1599. uint16_t **dest16 = (uint16_t**)dest;
  1600. int SH = 22 + 7 - desc->comp[0].depth_minus1;
  1601. int A = 0; // init to silence warning
  1602. for (i = 0; i < dstW; i++) {
  1603. int j;
  1604. int Y = 1 << 9;
  1605. int U = (1 << 9) - (128 << 19);
  1606. int V = (1 << 9) - (128 << 19);
  1607. int R, G, B;
  1608. for (j = 0; j < lumFilterSize; j++)
  1609. Y += lumSrc[j][i] * lumFilter[j];
  1610. for (j = 0; j < chrFilterSize; j++) {
  1611. U += chrUSrc[j][i] * chrFilter[j];
  1612. V += chrVSrc[j][i] * chrFilter[j];
  1613. }
  1614. Y >>= 10;
  1615. U >>= 10;
  1616. V >>= 10;
  1617. if (hasAlpha) {
  1618. A = 1 << 18;
  1619. for (j = 0; j < lumFilterSize; j++)
  1620. A += alpSrc[j][i] * lumFilter[j];
  1621. A >>= 19;
  1622. if (A & 0x100)
  1623. A = av_clip_uint8(A);
  1624. }
  1625. Y -= c->yuv2rgb_y_offset;
  1626. Y *= c->yuv2rgb_y_coeff;
  1627. Y += 1 << 21;
  1628. R = Y + V * c->yuv2rgb_v2r_coeff;
  1629. G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  1630. B = Y + U * c->yuv2rgb_u2b_coeff;
  1631. if ((R | G | B) & 0xC0000000) {
  1632. R = av_clip_uintp2(R, 30);
  1633. G = av_clip_uintp2(G, 30);
  1634. B = av_clip_uintp2(B, 30);
  1635. }
  1636. if (SH != 22) {
  1637. dest16[0][i] = G >> SH;
  1638. dest16[1][i] = B >> SH;
  1639. dest16[2][i] = R >> SH;
  1640. if (hasAlpha)
  1641. dest16[3][i] = A;
  1642. } else {
  1643. dest[0][i] = G >> 22;
  1644. dest[1][i] = B >> 22;
  1645. dest[2][i] = R >> 22;
  1646. if (hasAlpha)
  1647. dest[3][i] = A;
  1648. }
  1649. }
  1650. if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
  1651. for (i = 0; i < dstW; i++) {
  1652. dest16[0][i] = av_bswap16(dest16[0][i]);
  1653. dest16[1][i] = av_bswap16(dest16[1][i]);
  1654. dest16[2][i] = av_bswap16(dest16[2][i]);
  1655. if (hasAlpha)
  1656. dest16[3][i] = av_bswap16(dest16[3][i]);
  1657. }
  1658. }
  1659. }
  1660. av_cold void ff_sws_init_output_funcs(SwsContext *c,
  1661. yuv2planar1_fn *yuv2plane1,
  1662. yuv2planarX_fn *yuv2planeX,
  1663. yuv2interleavedX_fn *yuv2nv12cX,
  1664. yuv2packed1_fn *yuv2packed1,
  1665. yuv2packed2_fn *yuv2packed2,
  1666. yuv2packedX_fn *yuv2packedX,
  1667. yuv2anyX_fn *yuv2anyX)
  1668. {
  1669. enum AVPixelFormat dstFormat = c->dstFormat;
  1670. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  1671. if (is16BPS(dstFormat)) {
  1672. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
  1673. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
  1674. } else if (is9_OR_10BPS(dstFormat)) {
  1675. if (desc->comp[0].depth_minus1 == 8) {
  1676. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
  1677. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
  1678. } else if (desc->comp[0].depth_minus1 == 9) {
  1679. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
  1680. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
  1681. } else if (desc->comp[0].depth_minus1 == 11) {
  1682. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c : yuv2planeX_12LE_c;
  1683. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c : yuv2plane1_12LE_c;
  1684. } else if (desc->comp[0].depth_minus1 == 13) {
  1685. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c : yuv2planeX_14LE_c;
  1686. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c : yuv2plane1_14LE_c;
  1687. } else
  1688. av_assert0(0);
  1689. } else {
  1690. *yuv2plane1 = yuv2plane1_8_c;
  1691. *yuv2planeX = yuv2planeX_8_c;
  1692. if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
  1693. *yuv2nv12cX = yuv2nv12cX_c;
  1694. }
  1695. if(c->flags & SWS_FULL_CHR_H_INT) {
  1696. switch (dstFormat) {
  1697. case AV_PIX_FMT_RGBA:
  1698. #if CONFIG_SMALL
  1699. *yuv2packedX = yuv2rgba32_full_X_c;
  1700. *yuv2packed2 = yuv2rgba32_full_2_c;
  1701. *yuv2packed1 = yuv2rgba32_full_1_c;
  1702. #else
  1703. #if CONFIG_SWSCALE_ALPHA
  1704. if (c->alpPixBuf) {
  1705. *yuv2packedX = yuv2rgba32_full_X_c;
  1706. *yuv2packed2 = yuv2rgba32_full_2_c;
  1707. *yuv2packed1 = yuv2rgba32_full_1_c;
  1708. } else
  1709. #endif /* CONFIG_SWSCALE_ALPHA */
  1710. {
  1711. *yuv2packedX = yuv2rgbx32_full_X_c;
  1712. *yuv2packed2 = yuv2rgbx32_full_2_c;
  1713. *yuv2packed1 = yuv2rgbx32_full_1_c;
  1714. }
  1715. #endif /* !CONFIG_SMALL */
  1716. break;
  1717. case AV_PIX_FMT_ARGB:
  1718. #if CONFIG_SMALL
  1719. *yuv2packedX = yuv2argb32_full_X_c;
  1720. *yuv2packed2 = yuv2argb32_full_2_c;
  1721. *yuv2packed1 = yuv2argb32_full_1_c;
  1722. #else
  1723. #if CONFIG_SWSCALE_ALPHA
  1724. if (c->alpPixBuf) {
  1725. *yuv2packedX = yuv2argb32_full_X_c;
  1726. *yuv2packed2 = yuv2argb32_full_2_c;
  1727. *yuv2packed1 = yuv2argb32_full_1_c;
  1728. } else
  1729. #endif /* CONFIG_SWSCALE_ALPHA */
  1730. {
  1731. *yuv2packedX = yuv2xrgb32_full_X_c;
  1732. *yuv2packed2 = yuv2xrgb32_full_2_c;
  1733. *yuv2packed1 = yuv2xrgb32_full_1_c;
  1734. }
  1735. #endif /* !CONFIG_SMALL */
  1736. break;
  1737. case AV_PIX_FMT_BGRA:
  1738. #if CONFIG_SMALL
  1739. *yuv2packedX = yuv2bgra32_full_X_c;
  1740. *yuv2packed2 = yuv2bgra32_full_2_c;
  1741. *yuv2packed1 = yuv2bgra32_full_1_c;
  1742. #else
  1743. #if CONFIG_SWSCALE_ALPHA
  1744. if (c->alpPixBuf) {
  1745. *yuv2packedX = yuv2bgra32_full_X_c;
  1746. *yuv2packed2 = yuv2bgra32_full_2_c;
  1747. *yuv2packed1 = yuv2bgra32_full_1_c;
  1748. } else
  1749. #endif /* CONFIG_SWSCALE_ALPHA */
  1750. {
  1751. *yuv2packedX = yuv2bgrx32_full_X_c;
  1752. *yuv2packed2 = yuv2bgrx32_full_2_c;
  1753. *yuv2packed1 = yuv2bgrx32_full_1_c;
  1754. }
  1755. #endif /* !CONFIG_SMALL */
  1756. break;
  1757. case AV_PIX_FMT_ABGR:
  1758. #if CONFIG_SMALL
  1759. *yuv2packedX = yuv2abgr32_full_X_c;
  1760. *yuv2packed2 = yuv2abgr32_full_2_c;
  1761. *yuv2packed1 = yuv2abgr32_full_1_c;
  1762. #else
  1763. #if CONFIG_SWSCALE_ALPHA
  1764. if (c->alpPixBuf) {
  1765. *yuv2packedX = yuv2abgr32_full_X_c;
  1766. *yuv2packed2 = yuv2abgr32_full_2_c;
  1767. *yuv2packed1 = yuv2abgr32_full_1_c;
  1768. } else
  1769. #endif /* CONFIG_SWSCALE_ALPHA */
  1770. {
  1771. *yuv2packedX = yuv2xbgr32_full_X_c;
  1772. *yuv2packed2 = yuv2xbgr32_full_2_c;
  1773. *yuv2packed1 = yuv2xbgr32_full_1_c;
  1774. }
  1775. #endif /* !CONFIG_SMALL */
  1776. break;
  1777. case AV_PIX_FMT_RGB24:
  1778. *yuv2packedX = yuv2rgb24_full_X_c;
  1779. *yuv2packed2 = yuv2rgb24_full_2_c;
  1780. *yuv2packed1 = yuv2rgb24_full_1_c;
  1781. break;
  1782. case AV_PIX_FMT_BGR24:
  1783. *yuv2packedX = yuv2bgr24_full_X_c;
  1784. *yuv2packed2 = yuv2bgr24_full_2_c;
  1785. *yuv2packed1 = yuv2bgr24_full_1_c;
  1786. break;
  1787. case AV_PIX_FMT_BGR4_BYTE:
  1788. *yuv2packedX = yuv2bgr4_byte_full_X_c;
  1789. *yuv2packed2 = yuv2bgr4_byte_full_2_c;
  1790. *yuv2packed1 = yuv2bgr4_byte_full_1_c;
  1791. break;
  1792. case AV_PIX_FMT_RGB4_BYTE:
  1793. *yuv2packedX = yuv2rgb4_byte_full_X_c;
  1794. *yuv2packed2 = yuv2rgb4_byte_full_2_c;
  1795. *yuv2packed1 = yuv2rgb4_byte_full_1_c;
  1796. break;
  1797. case AV_PIX_FMT_BGR8:
  1798. *yuv2packedX = yuv2bgr8_full_X_c;
  1799. *yuv2packed2 = yuv2bgr8_full_2_c;
  1800. *yuv2packed1 = yuv2bgr8_full_1_c;
  1801. break;
  1802. case AV_PIX_FMT_RGB8:
  1803. *yuv2packedX = yuv2rgb8_full_X_c;
  1804. *yuv2packed2 = yuv2rgb8_full_2_c;
  1805. *yuv2packed1 = yuv2rgb8_full_1_c;
  1806. break;
  1807. case AV_PIX_FMT_GBRP:
  1808. case AV_PIX_FMT_GBRP9BE:
  1809. case AV_PIX_FMT_GBRP9LE:
  1810. case AV_PIX_FMT_GBRP10BE:
  1811. case AV_PIX_FMT_GBRP10LE:
  1812. case AV_PIX_FMT_GBRP12BE:
  1813. case AV_PIX_FMT_GBRP12LE:
  1814. case AV_PIX_FMT_GBRP14BE:
  1815. case AV_PIX_FMT_GBRP14LE:
  1816. case AV_PIX_FMT_GBRP16BE:
  1817. case AV_PIX_FMT_GBRP16LE:
  1818. case AV_PIX_FMT_GBRAP:
  1819. *yuv2anyX = yuv2gbrp_full_X_c;
  1820. break;
  1821. }
  1822. if (!*yuv2packedX && !*yuv2anyX)
  1823. goto YUV_PACKED;
  1824. } else {
  1825. YUV_PACKED:
  1826. switch (dstFormat) {
  1827. case AV_PIX_FMT_RGBA64LE:
  1828. #if CONFIG_SWSCALE_ALPHA
  1829. if (c->alpPixBuf) {
  1830. *yuv2packed1 = yuv2rgba64le_1_c;
  1831. *yuv2packed2 = yuv2rgba64le_2_c;
  1832. *yuv2packedX = yuv2rgba64le_X_c;
  1833. } else
  1834. #endif /* CONFIG_SWSCALE_ALPHA */
  1835. {
  1836. *yuv2packed1 = yuv2rgbx64le_1_c;
  1837. *yuv2packed2 = yuv2rgbx64le_2_c;
  1838. *yuv2packedX = yuv2rgbx64le_X_c;
  1839. }
  1840. break;
  1841. case AV_PIX_FMT_RGBA64BE:
  1842. #if CONFIG_SWSCALE_ALPHA
  1843. if (c->alpPixBuf) {
  1844. *yuv2packed1 = yuv2rgba64be_1_c;
  1845. *yuv2packed2 = yuv2rgba64be_2_c;
  1846. *yuv2packedX = yuv2rgba64be_X_c;
  1847. } else
  1848. #endif /* CONFIG_SWSCALE_ALPHA */
  1849. {
  1850. *yuv2packed1 = yuv2rgbx64be_1_c;
  1851. *yuv2packed2 = yuv2rgbx64be_2_c;
  1852. *yuv2packedX = yuv2rgbx64be_X_c;
  1853. }
  1854. break;
  1855. case AV_PIX_FMT_BGRA64LE:
  1856. #if CONFIG_SWSCALE_ALPHA
  1857. if (c->alpPixBuf) {
  1858. *yuv2packed1 = yuv2bgra64le_1_c;
  1859. *yuv2packed2 = yuv2bgra64le_2_c;
  1860. *yuv2packedX = yuv2bgra64le_X_c;
  1861. } else
  1862. #endif /* CONFIG_SWSCALE_ALPHA */
  1863. {
  1864. *yuv2packed1 = yuv2bgrx64le_1_c;
  1865. *yuv2packed2 = yuv2bgrx64le_2_c;
  1866. *yuv2packedX = yuv2bgrx64le_X_c;
  1867. }
  1868. break;
  1869. case AV_PIX_FMT_BGRA64BE:
  1870. #if CONFIG_SWSCALE_ALPHA
  1871. if (c->alpPixBuf) {
  1872. *yuv2packed1 = yuv2bgra64be_1_c;
  1873. *yuv2packed2 = yuv2bgra64be_2_c;
  1874. *yuv2packedX = yuv2bgra64be_X_c;
  1875. } else
  1876. #endif /* CONFIG_SWSCALE_ALPHA */
  1877. {
  1878. *yuv2packed1 = yuv2bgrx64be_1_c;
  1879. *yuv2packed2 = yuv2bgrx64be_2_c;
  1880. *yuv2packedX = yuv2bgrx64be_X_c;
  1881. }
  1882. break;
  1883. case AV_PIX_FMT_RGB48LE:
  1884. *yuv2packed1 = yuv2rgb48le_1_c;
  1885. *yuv2packed2 = yuv2rgb48le_2_c;
  1886. *yuv2packedX = yuv2rgb48le_X_c;
  1887. break;
  1888. case AV_PIX_FMT_RGB48BE:
  1889. *yuv2packed1 = yuv2rgb48be_1_c;
  1890. *yuv2packed2 = yuv2rgb48be_2_c;
  1891. *yuv2packedX = yuv2rgb48be_X_c;
  1892. break;
  1893. case AV_PIX_FMT_BGR48LE:
  1894. *yuv2packed1 = yuv2bgr48le_1_c;
  1895. *yuv2packed2 = yuv2bgr48le_2_c;
  1896. *yuv2packedX = yuv2bgr48le_X_c;
  1897. break;
  1898. case AV_PIX_FMT_BGR48BE:
  1899. *yuv2packed1 = yuv2bgr48be_1_c;
  1900. *yuv2packed2 = yuv2bgr48be_2_c;
  1901. *yuv2packedX = yuv2bgr48be_X_c;
  1902. break;
  1903. case AV_PIX_FMT_RGB32:
  1904. case AV_PIX_FMT_BGR32:
  1905. #if CONFIG_SMALL
  1906. *yuv2packed1 = yuv2rgb32_1_c;
  1907. *yuv2packed2 = yuv2rgb32_2_c;
  1908. *yuv2packedX = yuv2rgb32_X_c;
  1909. #else
  1910. #if CONFIG_SWSCALE_ALPHA
  1911. if (c->alpPixBuf) {
  1912. *yuv2packed1 = yuv2rgba32_1_c;
  1913. *yuv2packed2 = yuv2rgba32_2_c;
  1914. *yuv2packedX = yuv2rgba32_X_c;
  1915. } else
  1916. #endif /* CONFIG_SWSCALE_ALPHA */
  1917. {
  1918. *yuv2packed1 = yuv2rgbx32_1_c;
  1919. *yuv2packed2 = yuv2rgbx32_2_c;
  1920. *yuv2packedX = yuv2rgbx32_X_c;
  1921. }
  1922. #endif /* !CONFIG_SMALL */
  1923. break;
  1924. case AV_PIX_FMT_RGB32_1:
  1925. case AV_PIX_FMT_BGR32_1:
  1926. #if CONFIG_SMALL
  1927. *yuv2packed1 = yuv2rgb32_1_1_c;
  1928. *yuv2packed2 = yuv2rgb32_1_2_c;
  1929. *yuv2packedX = yuv2rgb32_1_X_c;
  1930. #else
  1931. #if CONFIG_SWSCALE_ALPHA
  1932. if (c->alpPixBuf) {
  1933. *yuv2packed1 = yuv2rgba32_1_1_c;
  1934. *yuv2packed2 = yuv2rgba32_1_2_c;
  1935. *yuv2packedX = yuv2rgba32_1_X_c;
  1936. } else
  1937. #endif /* CONFIG_SWSCALE_ALPHA */
  1938. {
  1939. *yuv2packed1 = yuv2rgbx32_1_1_c;
  1940. *yuv2packed2 = yuv2rgbx32_1_2_c;
  1941. *yuv2packedX = yuv2rgbx32_1_X_c;
  1942. }
  1943. #endif /* !CONFIG_SMALL */
  1944. break;
  1945. case AV_PIX_FMT_RGB24:
  1946. *yuv2packed1 = yuv2rgb24_1_c;
  1947. *yuv2packed2 = yuv2rgb24_2_c;
  1948. *yuv2packedX = yuv2rgb24_X_c;
  1949. break;
  1950. case AV_PIX_FMT_BGR24:
  1951. *yuv2packed1 = yuv2bgr24_1_c;
  1952. *yuv2packed2 = yuv2bgr24_2_c;
  1953. *yuv2packedX = yuv2bgr24_X_c;
  1954. break;
  1955. case AV_PIX_FMT_RGB565LE:
  1956. case AV_PIX_FMT_RGB565BE:
  1957. case AV_PIX_FMT_BGR565LE:
  1958. case AV_PIX_FMT_BGR565BE:
  1959. *yuv2packed1 = yuv2rgb16_1_c;
  1960. *yuv2packed2 = yuv2rgb16_2_c;
  1961. *yuv2packedX = yuv2rgb16_X_c;
  1962. break;
  1963. case AV_PIX_FMT_RGB555LE:
  1964. case AV_PIX_FMT_RGB555BE:
  1965. case AV_PIX_FMT_BGR555LE:
  1966. case AV_PIX_FMT_BGR555BE:
  1967. *yuv2packed1 = yuv2rgb15_1_c;
  1968. *yuv2packed2 = yuv2rgb15_2_c;
  1969. *yuv2packedX = yuv2rgb15_X_c;
  1970. break;
  1971. case AV_PIX_FMT_RGB444LE:
  1972. case AV_PIX_FMT_RGB444BE:
  1973. case AV_PIX_FMT_BGR444LE:
  1974. case AV_PIX_FMT_BGR444BE:
  1975. *yuv2packed1 = yuv2rgb12_1_c;
  1976. *yuv2packed2 = yuv2rgb12_2_c;
  1977. *yuv2packedX = yuv2rgb12_X_c;
  1978. break;
  1979. case AV_PIX_FMT_RGB8:
  1980. case AV_PIX_FMT_BGR8:
  1981. *yuv2packed1 = yuv2rgb8_1_c;
  1982. *yuv2packed2 = yuv2rgb8_2_c;
  1983. *yuv2packedX = yuv2rgb8_X_c;
  1984. break;
  1985. case AV_PIX_FMT_RGB4:
  1986. case AV_PIX_FMT_BGR4:
  1987. *yuv2packed1 = yuv2rgb4_1_c;
  1988. *yuv2packed2 = yuv2rgb4_2_c;
  1989. *yuv2packedX = yuv2rgb4_X_c;
  1990. break;
  1991. case AV_PIX_FMT_RGB4_BYTE:
  1992. case AV_PIX_FMT_BGR4_BYTE:
  1993. *yuv2packed1 = yuv2rgb4b_1_c;
  1994. *yuv2packed2 = yuv2rgb4b_2_c;
  1995. *yuv2packedX = yuv2rgb4b_X_c;
  1996. break;
  1997. }
  1998. }
  1999. switch (dstFormat) {
  2000. case AV_PIX_FMT_MONOWHITE:
  2001. *yuv2packed1 = yuv2monowhite_1_c;
  2002. *yuv2packed2 = yuv2monowhite_2_c;
  2003. *yuv2packedX = yuv2monowhite_X_c;
  2004. break;
  2005. case AV_PIX_FMT_MONOBLACK:
  2006. *yuv2packed1 = yuv2monoblack_1_c;
  2007. *yuv2packed2 = yuv2monoblack_2_c;
  2008. *yuv2packedX = yuv2monoblack_X_c;
  2009. break;
  2010. case AV_PIX_FMT_YUYV422:
  2011. *yuv2packed1 = yuv2yuyv422_1_c;
  2012. *yuv2packed2 = yuv2yuyv422_2_c;
  2013. *yuv2packedX = yuv2yuyv422_X_c;
  2014. break;
  2015. case AV_PIX_FMT_UYVY422:
  2016. *yuv2packed1 = yuv2uyvy422_1_c;
  2017. *yuv2packed2 = yuv2uyvy422_2_c;
  2018. *yuv2packedX = yuv2uyvy422_X_c;
  2019. break;
  2020. }
  2021. }