/src/ffmpeg/libswscale/alphablend.c
Line | Count | Source |
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 | | |
21 | | #include "swscale_internal.h" |
22 | | |
23 | | int ff_sws_alphablendaway(SwsInternal *c, const uint8_t *const src[], |
24 | | const int srcStride[], int srcSliceY, int srcSliceH, |
25 | | uint8_t *const dst[], const int dstStride[]) |
26 | 0 | { |
27 | 0 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->opts.src_format); |
28 | 0 | const int lum_w = c->opts.src_w; |
29 | 0 | const int lum_h = c->opts.src_h; |
30 | 0 | int nb_components = desc->nb_components; |
31 | 0 | int plane, x, ysrc; |
32 | 0 | int plane_count = isGray(c->opts.src_format) ? 1 : 3; |
33 | 0 | int sixteen_bits = desc->comp[0].depth >= 9; |
34 | 0 | unsigned off = 1<<(desc->comp[0].depth - 1); |
35 | 0 | unsigned shift = desc->comp[0].depth; |
36 | 0 | unsigned max = (1<<shift) - 1; |
37 | 0 | int target_table[2][3]; |
38 | |
|
39 | 0 | for (plane = 0; plane < plane_count; plane++) { |
40 | 0 | int a = 0, b = 0; |
41 | 0 | if (c->opts.alpha_blend == SWS_ALPHA_BLEND_CHECKERBOARD) { |
42 | 0 | a = (1<<(desc->comp[0].depth - 1))/2; |
43 | 0 | b = 3*(1<<(desc->comp[0].depth-1))/2; |
44 | 0 | } |
45 | 0 | target_table[0][plane] = plane && !(desc->flags & AV_PIX_FMT_FLAG_RGB) ? 1<<(desc->comp[0].depth - 1) : a; |
46 | 0 | target_table[1][plane] = plane && !(desc->flags & AV_PIX_FMT_FLAG_RGB) ? 1<<(desc->comp[0].depth - 1) : b; |
47 | 0 | } |
48 | |
|
49 | 0 | av_assert0(plane_count == nb_components - 1); |
50 | 0 | if (desc->flags & AV_PIX_FMT_FLAG_PLANAR) { |
51 | 0 | for (plane = 0; plane < plane_count; plane++) { |
52 | 0 | int w = plane ? c->chrSrcW : c->opts.src_w; |
53 | 0 | int x_subsample = plane ? desc->log2_chroma_w: 0; |
54 | 0 | int y_subsample = plane ? desc->log2_chroma_h: 0; |
55 | 0 | for (ysrc = 0; ysrc < AV_CEIL_RSHIFT(srcSliceH, y_subsample); ysrc++) { |
56 | 0 | int y = ysrc + (srcSliceY >> y_subsample); |
57 | 0 | int subsample_row = y_subsample && (y << y_subsample) + 1 < lum_h; |
58 | 0 | if (x_subsample || subsample_row) { |
59 | 0 | int alpha; |
60 | 0 | unsigned u; |
61 | 0 | if (sixteen_bits) { |
62 | 0 | ptrdiff_t alpha_step = srcStride[plane_count] >> 1; |
63 | 0 | const uint16_t *s = (const uint16_t *)(src[plane ] + srcStride[plane ] * ysrc); |
64 | 0 | const uint16_t *a = (const uint16_t *)(src[plane_count] + (srcStride[plane_count] * ysrc << y_subsample)); |
65 | 0 | uint16_t *d = ( uint16_t *)(dst[plane ] + dstStride[plane ] * y); |
66 | 0 | if ((!isBE(c->opts.src_format)) == !HAVE_BIGENDIAN) { |
67 | 0 | for (x = 0; x < w; x++) { |
68 | 0 | const int xnext = FFMIN(2*x + 1, lum_w - 1); |
69 | 0 | if (subsample_row) { |
70 | 0 | alpha = (a[2*x] + a[xnext] + 2 + |
71 | 0 | a[2*x + alpha_step] + a[xnext + alpha_step]) >> 2; |
72 | 0 | } else |
73 | 0 | alpha = (a[2*x] + a[xnext]) >> 1; |
74 | 0 | u = s[x]*alpha + target_table[((x^y)>>5)&1][plane]*(max-alpha) + off; |
75 | 0 | d[x] = av_clip((u + (u >> shift)) >> shift, 0, max); |
76 | 0 | } |
77 | 0 | } else { |
78 | 0 | for (x = 0; x < w; x++) { |
79 | 0 | const int xnext = FFMIN(2*x + 1, lum_w - 1); |
80 | 0 | if (subsample_row) { |
81 | 0 | alpha = (av_bswap16(a[2*x]) + av_bswap16(a[xnext]) + 2 + |
82 | 0 | av_bswap16(a[2*x + alpha_step]) + av_bswap16(a[xnext + alpha_step])) >> 2; |
83 | 0 | } else |
84 | 0 | alpha = (av_bswap16(a[2*x]) + av_bswap16(a[xnext])) >> 1; |
85 | 0 | u = av_bswap16(s[x])*alpha + target_table[((x^y)>>5)&1][plane]*(max-alpha) + off; |
86 | 0 | d[x] = av_clip((u + (u >> shift)) >> shift, 0, max); |
87 | 0 | } |
88 | 0 | } |
89 | 0 | } else { |
90 | 0 | ptrdiff_t alpha_step = srcStride[plane_count]; |
91 | 0 | const uint8_t *s = src[plane ] + srcStride[plane] * ysrc; |
92 | 0 | const uint8_t *a = src[plane_count] + (srcStride[plane_count] * ysrc << y_subsample); |
93 | 0 | uint8_t *d = dst[plane ] + dstStride[plane] * y; |
94 | 0 | for (x = 0; x < w; x++) { |
95 | 0 | const int xnext = FFMIN(2*x + 1, lum_w - 1); |
96 | 0 | if (subsample_row) { |
97 | 0 | alpha = (a[2*x] + a[xnext] + 2 + |
98 | 0 | a[2*x + alpha_step] + a[xnext + alpha_step]) >> 2; |
99 | 0 | } else |
100 | 0 | alpha = (a[2*x] + a[xnext]) >> 1; |
101 | 0 | u = s[x]*alpha + target_table[((x^y)>>5)&1][plane]*(255-alpha) + 128; |
102 | 0 | d[x] = (257*u) >> 16; |
103 | 0 | } |
104 | 0 | } |
105 | 0 | } else { |
106 | 0 | if (sixteen_bits) { |
107 | 0 | const uint16_t *s = (const uint16_t *)(src[plane ] + srcStride[plane ] * ysrc); |
108 | 0 | const uint16_t *a = (const uint16_t *)(src[plane_count] + srcStride[plane_count] * ysrc); |
109 | 0 | uint16_t *d = ( uint16_t *)(dst[plane ] + dstStride[plane ] * y); |
110 | 0 | if ((!isBE(c->opts.src_format)) == !HAVE_BIGENDIAN) { |
111 | 0 | for (x = 0; x < w; x++) { |
112 | 0 | unsigned u = s[x]*a[x] + target_table[((x^y)>>5)&1][plane]*(max-a[x]) + off; |
113 | 0 | d[x] = av_clip((u + (u >> shift)) >> shift, 0, max); |
114 | 0 | } |
115 | 0 | } else { |
116 | 0 | for (x = 0; x < w; x++) { |
117 | 0 | unsigned aswap =av_bswap16(a[x]); |
118 | 0 | unsigned u = av_bswap16(s[x])*aswap + target_table[((x^y)>>5)&1][plane]*(max-aswap) + off; |
119 | 0 | d[x] = av_clip((u + (u >> shift)) >> shift, 0, max); |
120 | 0 | } |
121 | 0 | } |
122 | 0 | } else { |
123 | 0 | const uint8_t *s = src[plane ] + srcStride[plane] * ysrc; |
124 | 0 | const uint8_t *a = src[plane_count] + srcStride[plane_count] * ysrc; |
125 | 0 | uint8_t *d = dst[plane ] + dstStride[plane] * y; |
126 | 0 | for (x = 0; x < w; x++) { |
127 | 0 | unsigned u = s[x]*a[x] + target_table[((x^y)>>5)&1][plane]*(255-a[x]) + 128; |
128 | 0 | d[x] = (257*u) >> 16; |
129 | 0 | } |
130 | 0 | } |
131 | 0 | } |
132 | 0 | } |
133 | 0 | } |
134 | 0 | } else { |
135 | 0 | int alpha_pos = desc->comp[plane_count].offset; |
136 | 0 | int w = c->opts.src_w; |
137 | 0 | for (ysrc = 0; ysrc < srcSliceH; ysrc++) { |
138 | 0 | int y = ysrc + srcSliceY; |
139 | 0 | if (sixteen_bits) { |
140 | 0 | const uint16_t *s = (const uint16_t *)(src[0] + srcStride[0] * ysrc + 2*!alpha_pos); |
141 | 0 | const uint16_t *a = (const uint16_t *)(src[0] + srcStride[0] * ysrc + alpha_pos); |
142 | 0 | uint16_t *d = ( uint16_t *)(dst[0] + dstStride[0] * y); |
143 | 0 | if ((!isBE(c->opts.src_format)) == !HAVE_BIGENDIAN) { |
144 | 0 | for (x = 0; x < w; x++) { |
145 | 0 | for (plane = 0; plane < plane_count; plane++) { |
146 | 0 | int x_index = (plane_count + 1) * x; |
147 | 0 | unsigned u = s[x_index + plane]*a[x_index] + target_table[((x^y)>>5)&1][plane]*(max-a[x_index]) + off; |
148 | 0 | d[plane_count*x + plane] = av_clip((u + (u >> shift)) >> shift, 0, max); |
149 | 0 | } |
150 | 0 | } |
151 | 0 | } else { |
152 | 0 | for (x = 0; x < w; x++) { |
153 | 0 | for (plane = 0; plane < plane_count; plane++) { |
154 | 0 | int x_index = (plane_count + 1) * x; |
155 | 0 | unsigned aswap =av_bswap16(a[x_index]); |
156 | 0 | unsigned u = av_bswap16(s[x_index + plane])*aswap + target_table[((x^y)>>5)&1][plane]*(max-aswap) + off; |
157 | 0 | d[plane_count*x + plane] = av_clip((u + (u >> shift)) >> shift, 0, max); |
158 | 0 | } |
159 | 0 | } |
160 | 0 | } |
161 | 0 | } else { |
162 | 0 | const uint8_t *s = src[0] + srcStride[0] * ysrc + !alpha_pos; |
163 | 0 | const uint8_t *a = src[0] + srcStride[0] * ysrc + alpha_pos; |
164 | 0 | uint8_t *d = dst[0] + dstStride[0] * y; |
165 | 0 | for (x = 0; x < w; x++) { |
166 | 0 | for (plane = 0; plane < plane_count; plane++) { |
167 | 0 | int x_index = (plane_count + 1) * x; |
168 | 0 | unsigned u = s[x_index + plane]*a[x_index] + target_table[((x^y)>>5)&1][plane]*(255-a[x_index]) + 128; |
169 | 0 | d[plane_count*x + plane] = (257*u) >> 16; |
170 | 0 | } |
171 | 0 | } |
172 | 0 | } |
173 | 0 | } |
174 | 0 | } |
175 | |
|
176 | 0 | return 0; |
177 | 0 | } |