Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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