alphablend.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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(SwsContext *c, const uint8_t *src[],
  22. int srcStride[], int srcSliceY, int srcSliceH,
  23. uint8_t *dst[], 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, y;
  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 (y = srcSliceY >> y_subsample; y < AV_CEIL_RSHIFT(srcSliceH, y_subsample); y++) {
  50. if (x_subsample || y_subsample) {
  51. int alpha;
  52. unsigned u;
  53. if (sixteen_bits) {
  54. ptrdiff_t alpha_step = srcStride[plane_count] >> 1;
  55. const uint16_t *s = (const uint16_t *)(src[plane ] + srcStride[plane ] * y);
  56. const uint16_t *a = (const uint16_t *)(src[plane_count] + (srcStride[plane_count] * y << y_subsample));
  57. uint16_t *d = ( uint16_t *)(dst[plane ] + dstStride[plane ] * y);
  58. if ((!isBE(c->srcFormat)) == !HAVE_BIGENDIAN) {
  59. for (x = 0; x < w; x++) {
  60. if (y_subsample) {
  61. alpha = (a[2*x] + a[2*x + 1] + 2 +
  62. a[2*x + alpha_step] + a[2*x + alpha_step + 1]) >> 2;
  63. } else
  64. alpha = (a[2*x] + a[2*x + 1]) >> 1;
  65. u = s[x]*alpha + target_table[((x^y)>>5)&1][plane]*(max-alpha) + off;
  66. d[x] = av_clip((u + (u >> shift)) >> shift, 0, max);
  67. }
  68. } else {
  69. for (x = 0; x < w; x++) {
  70. if (y_subsample) {
  71. alpha = (av_bswap16(a[2*x]) + av_bswap16(a[2*x + 1]) + 2 +
  72. av_bswap16(a[2*x + alpha_step]) + av_bswap16(a[2*x + alpha_step + 1])) >> 2;
  73. } else
  74. alpha = (av_bswap16(a[2*x]) + av_bswap16(a[2*x + 1])) >> 1;
  75. u = av_bswap16(s[x])*alpha + target_table[((x^y)>>5)&1][plane]*(max-alpha) + off;
  76. d[x] = av_clip((u + (u >> shift)) >> shift, 0, max);
  77. }
  78. }
  79. } else {
  80. ptrdiff_t alpha_step = srcStride[plane_count];
  81. const uint8_t *s = src[plane ] + srcStride[plane] * y;
  82. const uint8_t *a = src[plane_count] + (srcStride[plane_count] * y << y_subsample);
  83. uint8_t *d = dst[plane ] + dstStride[plane] * y;
  84. for (x = 0; x < w; x++) {
  85. if (y_subsample) {
  86. alpha = (a[2*x] + a[2*x + 1] + 2 +
  87. a[2*x + alpha_step] + a[2*x + alpha_step + 1]) >> 2;
  88. } else
  89. alpha = (a[2*x] + a[2*x + 1]) >> 1;
  90. u = s[x]*alpha + target_table[((x^y)>>5)&1][plane]*(255-alpha) + 128;
  91. d[x] = (257*u) >> 16;
  92. }
  93. }
  94. } else {
  95. if (sixteen_bits) {
  96. const uint16_t *s = (const uint16_t *)(src[plane ] + srcStride[plane ] * y);
  97. const uint16_t *a = (const uint16_t *)(src[plane_count] + srcStride[plane_count] * y);
  98. uint16_t *d = ( uint16_t *)(dst[plane ] + dstStride[plane ] * y);
  99. if ((!isBE(c->srcFormat)) == !HAVE_BIGENDIAN) {
  100. for (x = 0; x < w; x++) {
  101. unsigned u = s[x]*a[x] + target_table[((x^y)>>5)&1][plane]*(max-a[x]) + off;
  102. d[x] = av_clip((u + (u >> shift)) >> shift, 0, max);
  103. }
  104. } else {
  105. for (x = 0; x < w; x++) {
  106. unsigned aswap =av_bswap16(a[x]);
  107. unsigned u = av_bswap16(s[x])*aswap + target_table[((x^y)>>5)&1][plane]*(max-aswap) + off;
  108. d[x] = av_clip((u + (u >> shift)) >> shift, 0, max);
  109. }
  110. }
  111. } else {
  112. const uint8_t *s = src[plane ] + srcStride[plane] * y;
  113. const uint8_t *a = src[plane_count] + srcStride[plane_count] * y;
  114. uint8_t *d = dst[plane ] + dstStride[plane] * y;
  115. for (x = 0; x < w; x++) {
  116. unsigned u = s[x]*a[x] + target_table[((x^y)>>5)&1][plane]*(255-a[x]) + 128;
  117. d[x] = (257*u) >> 16;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. } else {
  124. int alpha_pos = desc->comp[plane_count].offset;
  125. int w = c->srcW;
  126. for (y = srcSliceY; y < srcSliceH; y++) {
  127. if (sixteen_bits) {
  128. const uint16_t *s = (const uint16_t *)(src[0] + srcStride[0] * y + 2*!alpha_pos);
  129. const uint16_t *a = (const uint16_t *)(src[0] + srcStride[0] * y + alpha_pos);
  130. uint16_t *d = ( uint16_t *)(dst[0] + dstStride[0] * y);
  131. if ((!isBE(c->srcFormat)) == !HAVE_BIGENDIAN) {
  132. for (x = 0; x < w; x++) {
  133. for (plane = 0; plane < plane_count; plane++) {
  134. int x_index = (plane_count + 1) * x;
  135. unsigned u = s[x_index + plane]*a[x_index] + target_table[((x^y)>>5)&1][plane]*(max-a[x_index]) + off;
  136. d[plane_count*x + plane] = av_clip((u + (u >> shift)) >> shift, 0, max);
  137. }
  138. }
  139. } else {
  140. for (x = 0; x < w; x++) {
  141. for (plane = 0; plane < plane_count; plane++) {
  142. int x_index = (plane_count + 1) * x;
  143. unsigned aswap =av_bswap16(a[x_index]);
  144. unsigned u = av_bswap16(s[x_index + plane])*aswap + target_table[((x^y)>>5)&1][plane]*(max-aswap) + off;
  145. d[plane_count*x + plane] = av_clip((u + (u >> shift)) >> shift, 0, max);
  146. }
  147. }
  148. }
  149. } else {
  150. const uint8_t *s = src[0] + srcStride[0] * y + !alpha_pos;
  151. const uint8_t *a = src[0] + srcStride[0] * y + alpha_pos;
  152. uint8_t *d = dst[0] + dstStride[0] * y;
  153. for (x = 0; x < w; x++) {
  154. for (plane = 0; plane < plane_count; plane++) {
  155. int x_index = (plane_count + 1) * x;
  156. unsigned u = s[x_index + plane]*a[x_index] + target_table[((x^y)>>5)&1][plane]*(255-a[x_index]) + 128;
  157. d[plane_count*x + plane] = (257*u) >> 16;
  158. }
  159. }
  160. }
  161. }
  162. }
  163. return 0;
  164. }