2015-08-06 14:36:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Michael Niedermayer <michaelni@gmx.at>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "swscale_internal.h"
|
|
|
|
|
swscale: rename SwsContext to SwsInternal
And preserve the public SwsContext as separate name. The motivation here
is that I want to turn SwsContext into a public struct, while keeping the
internal implementation hidden. Additionally, I also want to be able to
use multiple internal implementations, e.g. for GPU devices.
This commit does not include any functional changes. For the most part, it is
a simple rename. The only complications arise from the public facing API
functions, which preserve their current type (and hence require an additional
unwrapping step internally), and the checkasm test framework, which directly
accesses SwsInternal.
For consistency, the affected functions that need to maintain a distionction
have generally been changed to refer to the SwsContext as *sws, and the
SwsInternal as *c.
In an upcoming commit, I will provide a backing definition for the public
SwsContext, and update `sws_internal()` to dereference the internal struct
instead of merely casting it.
Sponsored-by: Sovereign Tech Fund
Signed-off-by: Niklas Haas <git@haasn.dev>
2024-10-09 17:44:33 +00:00
|
|
|
int ff_sws_alphablendaway(SwsInternal *c, const uint8_t *const src[],
|
2024-10-02 13:37:10 +00:00
|
|
|
const int srcStride[], int srcSliceY, int srcSliceH,
|
|
|
|
uint8_t *const dst[], const int dstStride[])
|
2015-08-06 14:36:05 +00:00
|
|
|
{
|
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
|
|
|
|
int nb_components = desc->nb_components;
|
2021-09-30 17:00:56 +00:00
|
|
|
int plane, x, ysrc;
|
2015-08-06 14:36:05 +00:00
|
|
|
int plane_count = isGray(c->srcFormat) ? 1 : 3;
|
2015-09-08 15:10:48 +00:00
|
|
|
int sixteen_bits = desc->comp[0].depth >= 9;
|
|
|
|
unsigned off = 1<<(desc->comp[0].depth - 1);
|
|
|
|
unsigned shift = desc->comp[0].depth;
|
2015-08-06 14:36:05 +00:00
|
|
|
unsigned max = (1<<shift) - 1;
|
2015-08-09 15:11:53 +00:00
|
|
|
int target_table[2][3];
|
2015-08-09 14:09:30 +00:00
|
|
|
|
2015-08-09 15:11:53 +00:00
|
|
|
for (plane = 0; plane < plane_count; plane++) {
|
|
|
|
int a = 0, b = 0;
|
|
|
|
if (c->alphablend == SWS_ALPHA_BLEND_CHECKERBOARD) {
|
2015-09-08 15:10:48 +00:00
|
|
|
a = (1<<(desc->comp[0].depth - 1))/2;
|
|
|
|
b = 3*(1<<(desc->comp[0].depth-1))/2;
|
2015-08-09 15:11:53 +00:00
|
|
|
}
|
2015-09-08 15:10:48 +00:00
|
|
|
target_table[0][plane] = plane && !(desc->flags & AV_PIX_FMT_FLAG_RGB) ? 1<<(desc->comp[0].depth - 1) : a;
|
|
|
|
target_table[1][plane] = plane && !(desc->flags & AV_PIX_FMT_FLAG_RGB) ? 1<<(desc->comp[0].depth - 1) : b;
|
2015-08-09 15:11:53 +00:00
|
|
|
}
|
2015-08-06 14:36:05 +00:00
|
|
|
|
|
|
|
av_assert0(plane_count == nb_components - 1);
|
|
|
|
if (desc->flags & AV_PIX_FMT_FLAG_PLANAR) {
|
|
|
|
for (plane = 0; plane < plane_count; plane++) {
|
|
|
|
int w = plane ? c->chrSrcW : c->srcW;
|
2015-08-09 15:48:58 +00:00
|
|
|
int x_subsample = plane ? desc->log2_chroma_w: 0;
|
2015-08-06 14:36:05 +00:00
|
|
|
int y_subsample = plane ? desc->log2_chroma_h: 0;
|
2021-09-30 17:00:56 +00:00
|
|
|
for (ysrc = 0; ysrc < AV_CEIL_RSHIFT(srcSliceH, y_subsample); ysrc++) {
|
|
|
|
int y = ysrc + (srcSliceY >> y_subsample);
|
2015-08-09 15:48:58 +00:00
|
|
|
if (x_subsample || y_subsample) {
|
|
|
|
int alpha;
|
|
|
|
unsigned u;
|
|
|
|
if (sixteen_bits) {
|
|
|
|
ptrdiff_t alpha_step = srcStride[plane_count] >> 1;
|
2021-09-30 17:00:56 +00:00
|
|
|
const uint16_t *s = (const uint16_t *)(src[plane ] + srcStride[plane ] * ysrc);
|
|
|
|
const uint16_t *a = (const uint16_t *)(src[plane_count] + (srcStride[plane_count] * ysrc << y_subsample));
|
2015-08-21 10:14:08 +00:00
|
|
|
uint16_t *d = ( uint16_t *)(dst[plane ] + dstStride[plane ] * y);
|
2015-08-09 15:48:58 +00:00
|
|
|
if ((!isBE(c->srcFormat)) == !HAVE_BIGENDIAN) {
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
if (y_subsample) {
|
|
|
|
alpha = (a[2*x] + a[2*x + 1] + 2 +
|
|
|
|
a[2*x + alpha_step] + a[2*x + alpha_step + 1]) >> 2;
|
|
|
|
} else
|
|
|
|
alpha = (a[2*x] + a[2*x + 1]) >> 1;
|
|
|
|
u = s[x]*alpha + target_table[((x^y)>>5)&1][plane]*(max-alpha) + off;
|
|
|
|
d[x] = av_clip((u + (u >> shift)) >> shift, 0, max);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
if (y_subsample) {
|
|
|
|
alpha = (av_bswap16(a[2*x]) + av_bswap16(a[2*x + 1]) + 2 +
|
|
|
|
av_bswap16(a[2*x + alpha_step]) + av_bswap16(a[2*x + alpha_step + 1])) >> 2;
|
|
|
|
} else
|
|
|
|
alpha = (av_bswap16(a[2*x]) + av_bswap16(a[2*x + 1])) >> 1;
|
|
|
|
u = av_bswap16(s[x])*alpha + target_table[((x^y)>>5)&1][plane]*(max-alpha) + off;
|
|
|
|
d[x] = av_clip((u + (u >> shift)) >> shift, 0, max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ptrdiff_t alpha_step = srcStride[plane_count];
|
2021-09-30 17:00:56 +00:00
|
|
|
const uint8_t *s = src[plane ] + srcStride[plane] * ysrc;
|
|
|
|
const uint8_t *a = src[plane_count] + (srcStride[plane_count] * ysrc << y_subsample);
|
2015-08-09 15:48:58 +00:00
|
|
|
uint8_t *d = dst[plane ] + dstStride[plane] * y;
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
if (y_subsample) {
|
|
|
|
alpha = (a[2*x] + a[2*x + 1] + 2 +
|
|
|
|
a[2*x + alpha_step] + a[2*x + alpha_step + 1]) >> 2;
|
|
|
|
} else
|
|
|
|
alpha = (a[2*x] + a[2*x + 1]) >> 1;
|
|
|
|
u = s[x]*alpha + target_table[((x^y)>>5)&1][plane]*(255-alpha) + 128;
|
|
|
|
d[x] = (257*u) >> 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-08-06 14:36:05 +00:00
|
|
|
if (sixteen_bits) {
|
2021-09-30 17:00:56 +00:00
|
|
|
const uint16_t *s = (const uint16_t *)(src[plane ] + srcStride[plane ] * ysrc);
|
|
|
|
const uint16_t *a = (const uint16_t *)(src[plane_count] + srcStride[plane_count] * ysrc);
|
2015-08-21 10:14:08 +00:00
|
|
|
uint16_t *d = ( uint16_t *)(dst[plane ] + dstStride[plane ] * y);
|
2015-08-09 13:59:12 +00:00
|
|
|
if ((!isBE(c->srcFormat)) == !HAVE_BIGENDIAN) {
|
2015-08-06 14:36:05 +00:00
|
|
|
for (x = 0; x < w; x++) {
|
2015-08-09 15:11:53 +00:00
|
|
|
unsigned u = s[x]*a[x] + target_table[((x^y)>>5)&1][plane]*(max-a[x]) + off;
|
2015-08-06 14:36:05 +00:00
|
|
|
d[x] = av_clip((u + (u >> shift)) >> shift, 0, max);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
unsigned aswap =av_bswap16(a[x]);
|
2015-08-09 15:11:53 +00:00
|
|
|
unsigned u = av_bswap16(s[x])*aswap + target_table[((x^y)>>5)&1][plane]*(max-aswap) + off;
|
2015-08-06 14:36:05 +00:00
|
|
|
d[x] = av_clip((u + (u >> shift)) >> shift, 0, max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-30 17:00:56 +00:00
|
|
|
const uint8_t *s = src[plane ] + srcStride[plane] * ysrc;
|
|
|
|
const uint8_t *a = src[plane_count] + srcStride[plane_count] * ysrc;
|
2015-08-06 14:36:05 +00:00
|
|
|
uint8_t *d = dst[plane ] + dstStride[plane] * y;
|
|
|
|
for (x = 0; x < w; x++) {
|
2015-08-09 15:11:53 +00:00
|
|
|
unsigned u = s[x]*a[x] + target_table[((x^y)>>5)&1][plane]*(255-a[x]) + 128;
|
2015-08-06 14:36:05 +00:00
|
|
|
d[x] = (257*u) >> 16;
|
|
|
|
}
|
|
|
|
}
|
2015-08-09 15:48:58 +00:00
|
|
|
}
|
2015-08-06 14:36:05 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-08 09:24:26 +00:00
|
|
|
} else {
|
2015-09-08 15:10:48 +00:00
|
|
|
int alpha_pos = desc->comp[plane_count].offset;
|
2015-08-08 09:24:26 +00:00
|
|
|
int w = c->srcW;
|
2021-09-30 17:00:56 +00:00
|
|
|
for (ysrc = 0; ysrc < srcSliceH; ysrc++) {
|
|
|
|
int y = ysrc + srcSliceY;
|
2015-08-08 09:24:26 +00:00
|
|
|
if (sixteen_bits) {
|
2021-09-30 17:00:56 +00:00
|
|
|
const uint16_t *s = (const uint16_t *)(src[0] + srcStride[0] * ysrc + 2*!alpha_pos);
|
|
|
|
const uint16_t *a = (const uint16_t *)(src[0] + srcStride[0] * ysrc + alpha_pos);
|
2015-08-22 02:34:16 +00:00
|
|
|
uint16_t *d = ( uint16_t *)(dst[0] + dstStride[0] * y);
|
2015-08-08 09:24:26 +00:00
|
|
|
if ((!isBE(c->srcFormat)) == !HAVE_BIGENDIAN) {
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
for (plane = 0; plane < plane_count; plane++) {
|
|
|
|
int x_index = (plane_count + 1) * x;
|
2015-08-09 15:11:53 +00:00
|
|
|
unsigned u = s[x_index + plane]*a[x_index] + target_table[((x^y)>>5)&1][plane]*(max-a[x_index]) + off;
|
2015-08-08 09:24:26 +00:00
|
|
|
d[plane_count*x + plane] = av_clip((u + (u >> shift)) >> shift, 0, max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
for (plane = 0; plane < plane_count; plane++) {
|
|
|
|
int x_index = (plane_count + 1) * x;
|
|
|
|
unsigned aswap =av_bswap16(a[x_index]);
|
2015-08-09 15:11:53 +00:00
|
|
|
unsigned u = av_bswap16(s[x_index + plane])*aswap + target_table[((x^y)>>5)&1][plane]*(max-aswap) + off;
|
2015-08-08 09:24:26 +00:00
|
|
|
d[plane_count*x + plane] = av_clip((u + (u >> shift)) >> shift, 0, max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-30 17:00:56 +00:00
|
|
|
const uint8_t *s = src[0] + srcStride[0] * ysrc + !alpha_pos;
|
|
|
|
const uint8_t *a = src[0] + srcStride[0] * ysrc + alpha_pos;
|
2015-08-08 09:24:26 +00:00
|
|
|
uint8_t *d = dst[0] + dstStride[0] * y;
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
for (plane = 0; plane < plane_count; plane++) {
|
|
|
|
int x_index = (plane_count + 1) * x;
|
2015-08-09 15:11:53 +00:00
|
|
|
unsigned u = s[x_index + plane]*a[x_index] + target_table[((x^y)>>5)&1][plane]*(255-a[x_index]) + 128;
|
2015-08-08 09:24:26 +00:00
|
|
|
d[plane_count*x + plane] = (257*u) >> 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-06 14:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|