alphablend.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2015 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "swscale_internal.h"
  21. int ff_sws_alphablendaway(SwsInternal *c, const uint8_t *const src[],
  22. const int srcStride[], int srcSliceY, int srcSliceH,
  23. uint8_t *const dst[], const int dstStride[])
  24. {
  25. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
  26. int nb_components = desc->nb_components;
  27. int plane, x, ysrc;
  28. int plane_count = isGray(c->srcFormat) ? 1 : 3;
  29. int sixteen_bits = desc->comp[0].depth >= 9;
  30. unsigned off = 1<<(desc->comp[0].depth - 1);
  31. unsigned shift = desc->comp[0].depth;
  32. unsigned max = (1<<shift) - 1;
  33. int target_table[2][3];
  34. for (plane = 0; plane < plane_count; plane++) {
  35. int a = 0, b = 0;
  36. if (c->alphablend == SWS_ALPHA_BLEND_CHECKERBOARD) {
  37. a = (1<<(desc->comp[0].depth - 1))/2;
  38. b = 3*(1<<(desc->comp[0].depth-1))/2;
  39. }
  40. target_table[0][plane] = plane && !(desc->flags & AV_PIX_FMT_FLAG_RGB) ? 1<<(desc->comp[0].depth - 1) : a;
  41. target_table[1][plane] = plane && !(desc->flags & AV_PIX_FMT_FLAG_RGB) ? 1<<(desc->comp[0].depth - 1) : b;
  42. }
  43. av_assert0(plane_count == nb_components - 1);
  44. if (desc->flags & AV_PIX_FMT_FLAG_PLANAR) {
  45. for (plane = 0; plane < plane_count; plane++) {
  46. int w = plane ? c->chrSrcW : c->srcW;
  47. int x_subsample = plane ? desc->log2_chroma_w: 0;
  48. int y_subsample = plane ? desc->log2_chroma_h: 0;
  49. for (ysrc = 0; ysrc < AV_CEIL_RSHIFT(srcSliceH, y_subsample); ysrc++) {
  50. int y = ysrc + (srcSliceY >> y_subsample);
  51. if (x_subsample || y_subsample) {
  52. int alpha;
  53. unsigned u;
  54. if (sixteen_bits) {
  55. ptrdiff_t alpha_step = srcStride[plane_count] >> 1;
  56. const uint16_t *s = (const uint16_t *)(src[plane ] + srcStride[plane ] * ysrc);
  57. const uint16_t *a = (const uint16_t *)(src[plane_count] + (srcStride[plane_count] * ysrc << y_subsample));
  58. uint16_t *d = ( uint16_t *)(dst[plane ] + dstStride[plane ] * y);
  59. if ((!isBE(c->srcFormat)) == !HAVE_BIGENDIAN) {
  60. for (x = 0; x < w; x++) {
  61. if (y_subsample) {
  62. alpha = (a[2*x] + a[2*x + 1] + 2 +
  63. a[2*x + alpha_step] + a[2*x + alpha_step + 1]) >> 2;
  64. } else
  65. alpha = (a[2*x] + a[2*x + 1]) >> 1;
  66. u = s[x]*alpha + target_table[((x^y)>>5)&1][plane]*(max-alpha) + off;
  67. d[x] = av_clip((u + (u >> shift)) >> shift, 0, max);
  68. }
  69. } else {
  70. for (x = 0; x < w; x++) {
  71. if (y_subsample) {
  72. alpha = (av_bswap16(a[2*x]) + av_bswap16(a[2*x + 1]) + 2 +
  73. av_bswap16(a[2*x + alpha_step]) + av_bswap16(a[2*x + alpha_step + 1])) >> 2;
  74. } else
  75. alpha = (av_bswap16(a[2*x]) + av_bswap16(a[2*x + 1])) >> 1;
  76. u = av_bswap16(s[x])*alpha + target_table[((x^y)>>5)&1][plane]*(max-alpha) + off;
  77. d[x] = av_clip((u + (u >> shift)) >> shift, 0, max);
  78. }
  79. }
  80. } else {
  81. ptrdiff_t alpha_step = srcStride[plane_count];
  82. const uint8_t *s = src[plane ] + srcStride[plane] * ysrc;
  83. const uint8_t *a = src[plane_count] + (srcStride[plane_count] * ysrc << y_subsample);
  84. uint8_t *d = dst[plane ] + dstStride[plane] * y;
  85. for (x = 0; x < w; x++) {
  86. if (y_subsample) {
  87. alpha = (a[2*x] + a[2*x + 1] + 2 +
  88. a[2*x + alpha_step] + a[2*x + alpha_step + 1]) >> 2;
  89. } else
  90. alpha = (a[2*x] + a[2*x + 1]) >> 1;
  91. u = s[x]*alpha + target_table[((x^y)>>5)&1][plane]*(255-alpha) + 128;
  92. d[x] = (257*u) >> 16;
  93. }
  94. }
  95. } else {
  96. if (sixteen_bits) {
  97. const uint16_t *s = (const uint16_t *)(src[plane ] + srcStride[plane ] * ysrc);
  98. const uint16_t *a = (const uint16_t *)(src[plane_count] + srcStride[plane_count] * ysrc);
  99. uint16_t *d = ( uint16_t *)(dst[plane ] + dstStride[plane ] * y);
  100. if ((!isBE(c->srcFormat)) == !HAVE_BIGENDIAN) {
  101. for (x = 0; x < w; x++) {
  102. unsigned u = s[x]*a[x] + target_table[((x^y)>>5)&1][plane]*(max-a[x]) + off;
  103. d[x] = av_clip((u + (u >> shift)) >> shift, 0, max);
  104. }
  105. } else {
  106. for (x = 0; x < w; x++) {
  107. unsigned aswap =av_bswap16(a[x]);
  108. unsigned u = av_bswap16(s[x])*aswap + target_table[((x^y)>>5)&1][plane]*(max-aswap) + off;
  109. d[x] = av_clip((u + (u >> shift)) >> shift, 0, max);
  110. }
  111. }
  112. } else {
  113. const uint8_t *s = src[plane ] + srcStride[plane] * ysrc;
  114. const uint8_t *a = src[plane_count] + srcStride[plane_count] * ysrc;
  115. uint8_t *d = dst[plane ] + dstStride[plane] * y;
  116. for (x = 0; x < w; x++) {
  117. unsigned u = s[x]*a[x] + target_table[((x^y)>>5)&1][plane]*(255-a[x]) + 128;
  118. d[x] = (257*u) >> 16;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. } else {
  125. int alpha_pos = desc->comp[plane_count].offset;
  126. int w = c->srcW;
  127. for (ysrc = 0; ysrc < srcSliceH; ysrc++) {
  128. int y = ysrc + srcSliceY;
  129. if (sixteen_bits) {
  130. const uint16_t *s = (const uint16_t *)(src[0] + srcStride[0] * ysrc + 2*!alpha_pos);
  131. const uint16_t *a = (const uint16_t *)(src[0] + srcStride[0] * ysrc + alpha_pos);
  132. uint16_t *d = ( uint16_t *)(dst[0] + dstStride[0] * y);
  133. if ((!isBE(c->srcFormat)) == !HAVE_BIGENDIAN) {
  134. for (x = 0; x < w; x++) {
  135. for (plane = 0; plane < plane_count; plane++) {
  136. int x_index = (plane_count + 1) * x;
  137. unsigned u = s[x_index + plane]*a[x_index] + target_table[((x^y)>>5)&1][plane]*(max-a[x_index]) + off;
  138. d[plane_count*x + plane] = av_clip((u + (u >> shift)) >> shift, 0, max);
  139. }
  140. }
  141. } else {
  142. for (x = 0; x < w; x++) {
  143. for (plane = 0; plane < plane_count; plane++) {
  144. int x_index = (plane_count + 1) * x;
  145. unsigned aswap =av_bswap16(a[x_index]);
  146. unsigned u = av_bswap16(s[x_index + plane])*aswap + target_table[((x^y)>>5)&1][plane]*(max-aswap) + off;
  147. d[plane_count*x + plane] = av_clip((u + (u >> shift)) >> shift, 0, max);
  148. }
  149. }
  150. }
  151. } else {
  152. const uint8_t *s = src[0] + srcStride[0] * ysrc + !alpha_pos;
  153. const uint8_t *a = src[0] + srcStride[0] * ysrc + alpha_pos;
  154. uint8_t *d = dst[0] + dstStride[0] * y;
  155. for (x = 0; x < w; x++) {
  156. for (plane = 0; plane < plane_count; plane++) {
  157. int x_index = (plane_count + 1) * x;
  158. unsigned u = s[x_index + plane]*a[x_index] + target_table[((x^y)>>5)&1][plane]*(255-a[x_index]) + 128;
  159. d[plane_count*x + plane] = (257*u) >> 16;
  160. }
  161. }
  162. }
  163. }
  164. }
  165. return 0;
  166. }