2011-02-20 22:42:17 +00:00
|
|
|
/*
|
2012-05-22 19:36:30 +00:00
|
|
|
* Copyright 2011 Stefano Sabatini <stefano.sabatini-lala poste it>
|
|
|
|
* Copyright 2012 Nicolas George <nicolas.george normalesup org>
|
|
|
|
*
|
2011-02-20 22:42:17 +00:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2012-08-06 13:49:32 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-06-12 21:40:09 +00:00
|
|
|
#include "libavutil/avassert.h"
|
2011-02-20 22:42:17 +00:00
|
|
|
#include "libavutil/avutil.h"
|
|
|
|
#include "libavutil/colorspace.h"
|
2016-02-15 16:07:33 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/mem.h"
|
2011-02-20 22:42:17 +00:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "drawutils.h"
|
2012-06-16 09:47:46 +00:00
|
|
|
#include "formats.h"
|
2011-02-20 22:42:17 +00:00
|
|
|
|
|
|
|
enum { RED = 0, GREEN, BLUE, ALPHA };
|
|
|
|
|
2012-10-08 18:54:00 +00:00
|
|
|
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
|
2011-02-20 22:42:17 +00:00
|
|
|
{
|
|
|
|
switch (pix_fmt) {
|
2012-10-08 18:54:00 +00:00
|
|
|
case AV_PIX_FMT_0RGB:
|
2012-10-06 10:10:34 +00:00
|
|
|
case AV_PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
|
2012-10-08 18:54:00 +00:00
|
|
|
case AV_PIX_FMT_0BGR:
|
2012-10-06 10:10:34 +00:00
|
|
|
case AV_PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
|
2013-05-08 18:49:55 +00:00
|
|
|
case AV_PIX_FMT_RGB48LE:
|
|
|
|
case AV_PIX_FMT_RGB48BE:
|
|
|
|
case AV_PIX_FMT_RGBA64BE:
|
|
|
|
case AV_PIX_FMT_RGBA64LE:
|
2012-10-08 18:54:00 +00:00
|
|
|
case AV_PIX_FMT_RGB0:
|
2012-10-06 10:10:34 +00:00
|
|
|
case AV_PIX_FMT_RGBA:
|
|
|
|
case AV_PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
|
2013-05-08 18:49:55 +00:00
|
|
|
case AV_PIX_FMT_BGR48LE:
|
|
|
|
case AV_PIX_FMT_BGR48BE:
|
|
|
|
case AV_PIX_FMT_BGRA64BE:
|
|
|
|
case AV_PIX_FMT_BGRA64LE:
|
2012-10-06 10:10:34 +00:00
|
|
|
case AV_PIX_FMT_BGRA:
|
2012-10-08 18:54:00 +00:00
|
|
|
case AV_PIX_FMT_BGR0:
|
2012-10-06 10:10:34 +00:00
|
|
|
case AV_PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
|
2016-02-24 22:21:14 +00:00
|
|
|
case AV_PIX_FMT_GBRP9LE:
|
2016-03-03 10:15:30 +00:00
|
|
|
case AV_PIX_FMT_GBRP9BE:
|
2016-02-24 22:21:14 +00:00
|
|
|
case AV_PIX_FMT_GBRP10LE:
|
2016-03-03 10:15:30 +00:00
|
|
|
case AV_PIX_FMT_GBRP10BE:
|
2016-02-24 22:21:14 +00:00
|
|
|
case AV_PIX_FMT_GBRP12LE:
|
2016-03-03 10:15:30 +00:00
|
|
|
case AV_PIX_FMT_GBRP12BE:
|
2016-02-24 22:21:14 +00:00
|
|
|
case AV_PIX_FMT_GBRP14LE:
|
2016-03-03 10:15:30 +00:00
|
|
|
case AV_PIX_FMT_GBRP14BE:
|
|
|
|
case AV_PIX_FMT_GBRP16LE:
|
|
|
|
case AV_PIX_FMT_GBRP16BE:
|
2013-05-08 18:49:55 +00:00
|
|
|
case AV_PIX_FMT_GBRAP:
|
2017-08-07 10:50:13 +00:00
|
|
|
case AV_PIX_FMT_GBRAP10LE:
|
|
|
|
case AV_PIX_FMT_GBRAP10BE:
|
2016-03-03 10:15:30 +00:00
|
|
|
case AV_PIX_FMT_GBRAP12LE:
|
|
|
|
case AV_PIX_FMT_GBRAP12BE:
|
|
|
|
case AV_PIX_FMT_GBRAP16LE:
|
|
|
|
case AV_PIX_FMT_GBRAP16BE:
|
2021-02-07 20:41:04 +00:00
|
|
|
case AV_PIX_FMT_GBRPF32LE:
|
|
|
|
case AV_PIX_FMT_GBRPF32BE:
|
|
|
|
case AV_PIX_FMT_GBRAPF32LE:
|
|
|
|
case AV_PIX_FMT_GBRAPF32BE:
|
2013-05-08 18:49:55 +00:00
|
|
|
case AV_PIX_FMT_GBRP: rgba_map[GREEN] = 0; rgba_map[BLUE ] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
|
2011-10-24 17:58:48 +00:00
|
|
|
default: /* unsupported */
|
|
|
|
return AVERROR(EINVAL);
|
2011-02-20 22:42:17 +00:00
|
|
|
}
|
2011-10-24 17:58:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-08 18:54:00 +00:00
|
|
|
int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
|
2012-02-23 10:07:01 +00:00
|
|
|
{
|
2012-10-12 15:04:58 +00:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
|
2012-02-23 10:07:01 +00:00
|
|
|
const AVComponentDescriptor *c;
|
|
|
|
unsigned i, nb_planes = 0;
|
|
|
|
int pixelstep[MAX_PLANES] = { 0 };
|
2018-05-02 11:10:10 +00:00
|
|
|
int full_range = 0;
|
2012-02-23 10:07:01 +00:00
|
|
|
|
2015-06-12 22:06:28 +00:00
|
|
|
if (!desc || !desc->name)
|
2012-03-28 13:26:45 +00:00
|
|
|
return AVERROR(EINVAL);
|
2021-04-13 23:46:26 +00:00
|
|
|
if (desc->flags & ~(AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_ALPHA))
|
2012-02-23 10:07:01 +00:00
|
|
|
return AVERROR(ENOSYS);
|
2018-03-06 23:36:21 +00:00
|
|
|
if (format == AV_PIX_FMT_P010LE || format == AV_PIX_FMT_P010BE || format == AV_PIX_FMT_P016LE || format == AV_PIX_FMT_P016BE)
|
2016-08-31 09:24:03 +00:00
|
|
|
return AVERROR(ENOSYS);
|
2018-05-02 11:10:10 +00:00
|
|
|
if (format == AV_PIX_FMT_YUVJ420P || format == AV_PIX_FMT_YUVJ422P || format == AV_PIX_FMT_YUVJ444P ||
|
|
|
|
format == AV_PIX_FMT_YUVJ411P || format == AV_PIX_FMT_YUVJ440P)
|
|
|
|
full_range = 1;
|
2012-02-23 10:07:01 +00:00
|
|
|
for (i = 0; i < desc->nb_components; i++) {
|
|
|
|
c = &desc->comp[i];
|
2016-02-15 16:07:33 +00:00
|
|
|
/* for now, only 8-16 bits formats */
|
|
|
|
if (c->depth < 8 || c->depth > 16)
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
if (desc->flags & AV_PIX_FMT_FLAG_BE)
|
2012-02-23 10:07:01 +00:00
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
if (c->plane >= MAX_PLANES)
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
/* strange interleaving */
|
|
|
|
if (pixelstep[c->plane] != 0 &&
|
2015-09-08 15:10:48 +00:00
|
|
|
pixelstep[c->plane] != c->step)
|
2012-02-23 10:07:01 +00:00
|
|
|
return AVERROR(ENOSYS);
|
2016-02-15 16:07:33 +00:00
|
|
|
if (pixelstep[c->plane] == 6 &&
|
|
|
|
c->depth == 16)
|
|
|
|
return AVERROR(ENOSYS);
|
2015-09-08 15:10:48 +00:00
|
|
|
pixelstep[c->plane] = c->step;
|
2012-03-28 13:26:45 +00:00
|
|
|
if (pixelstep[c->plane] >= 8)
|
|
|
|
return AVERROR(ENOSYS);
|
2012-02-23 10:07:01 +00:00
|
|
|
nb_planes = FFMAX(nb_planes, c->plane + 1);
|
|
|
|
}
|
|
|
|
memset(draw, 0, sizeof(*draw));
|
|
|
|
draw->desc = desc;
|
|
|
|
draw->format = format;
|
|
|
|
draw->nb_planes = nb_planes;
|
2016-09-13 23:37:13 +00:00
|
|
|
draw->flags = flags;
|
2018-05-02 11:10:10 +00:00
|
|
|
draw->full_range = full_range;
|
2012-02-23 10:07:01 +00:00
|
|
|
memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
|
2013-07-14 17:59:25 +00:00
|
|
|
draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
|
|
|
|
draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
|
2016-09-13 23:37:13 +00:00
|
|
|
for (i = 0; i < (desc->nb_components - !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(flags & FF_DRAW_PROCESS_ALPHA))); i++)
|
2012-03-28 13:26:45 +00:00
|
|
|
draw->comp_mask[desc->comp[i].plane] |=
|
2015-09-08 15:10:48 +00:00
|
|
|
1 << desc->comp[i].offset;
|
2012-02-23 10:07:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-01 21:52:25 +00:00
|
|
|
void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
|
2012-02-23 10:07:01 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
uint8_t rgba_map[4];
|
|
|
|
|
2012-03-28 13:26:45 +00:00
|
|
|
if (rgba != color->rgba)
|
|
|
|
memcpy(color->rgba, rgba, sizeof(color->rgba));
|
2013-07-08 15:17:33 +00:00
|
|
|
if ((draw->desc->flags & AV_PIX_FMT_FLAG_RGB) &&
|
2012-02-23 10:07:01 +00:00
|
|
|
ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
|
2013-07-08 15:17:33 +00:00
|
|
|
if (draw->nb_planes == 1) {
|
2016-02-15 16:07:33 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2015-10-27 06:24:30 +00:00
|
|
|
color->comp[0].u8[rgba_map[i]] = rgba[i];
|
2016-02-15 16:07:33 +00:00
|
|
|
if (draw->desc->comp[rgba_map[i]].depth > 8) {
|
|
|
|
color->comp[0].u16[rgba_map[i]] = color->comp[0].u8[rgba_map[i]] << 8;
|
|
|
|
}
|
|
|
|
}
|
2013-07-08 15:17:33 +00:00
|
|
|
} else {
|
2016-02-15 16:07:33 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2013-07-08 15:17:33 +00:00
|
|
|
color->comp[rgba_map[i]].u8[0] = rgba[i];
|
2016-02-15 16:07:33 +00:00
|
|
|
if (draw->desc->comp[rgba_map[i]].depth > 8)
|
|
|
|
color->comp[rgba_map[i]].u16[0] = color->comp[rgba_map[i]].u8[0] << (draw->desc->comp[rgba_map[i]].depth - 8);
|
|
|
|
}
|
2013-07-08 15:17:33 +00:00
|
|
|
}
|
2016-05-10 01:52:06 +00:00
|
|
|
} else if (draw->nb_planes >= 2) {
|
2012-02-23 10:07:01 +00:00
|
|
|
/* assume YUV */
|
2016-05-10 01:52:06 +00:00
|
|
|
const AVPixFmtDescriptor *desc = draw->desc;
|
2018-05-02 11:10:10 +00:00
|
|
|
color->comp[desc->comp[0].plane].u8[desc->comp[0].offset] = draw->full_range ? RGB_TO_Y_JPEG(rgba[0], rgba[1], rgba[2]) : RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
|
|
|
|
color->comp[desc->comp[1].plane].u8[desc->comp[1].offset] = draw->full_range ? RGB_TO_U_JPEG(rgba[0], rgba[1], rgba[2]) : RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
|
|
|
|
color->comp[desc->comp[2].plane].u8[desc->comp[2].offset] = draw->full_range ? RGB_TO_V_JPEG(rgba[0], rgba[1], rgba[2]) : RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
|
2012-02-23 10:07:01 +00:00
|
|
|
color->comp[3].u8[0] = rgba[3];
|
2016-05-10 01:52:06 +00:00
|
|
|
#define EXPAND(compn) \
|
|
|
|
if (desc->comp[compn].depth > 8) \
|
|
|
|
color->comp[desc->comp[compn].plane].u16[desc->comp[compn].offset] = \
|
2016-08-31 09:25:25 +00:00
|
|
|
color->comp[desc->comp[compn].plane].u8[desc->comp[compn].offset] << \
|
|
|
|
(draw->desc->comp[compn].depth + draw->desc->comp[compn].shift - 8)
|
2016-05-10 01:52:06 +00:00
|
|
|
EXPAND(3);
|
|
|
|
EXPAND(2);
|
|
|
|
EXPAND(1);
|
|
|
|
EXPAND(0);
|
2017-08-07 16:39:29 +00:00
|
|
|
} else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A ||
|
|
|
|
draw->format == AV_PIX_FMT_GRAY16LE || draw->format == AV_PIX_FMT_YA16LE ||
|
|
|
|
draw->format == AV_PIX_FMT_GRAY9LE ||
|
|
|
|
draw->format == AV_PIX_FMT_GRAY10LE ||
|
2018-05-05 19:50:22 +00:00
|
|
|
draw->format == AV_PIX_FMT_GRAY12LE ||
|
|
|
|
draw->format == AV_PIX_FMT_GRAY14LE) {
|
2017-08-07 16:39:29 +00:00
|
|
|
const AVPixFmtDescriptor *desc = draw->desc;
|
2016-02-15 16:07:33 +00:00
|
|
|
color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
|
2017-08-07 16:39:29 +00:00
|
|
|
EXPAND(0);
|
2016-02-15 16:07:33 +00:00
|
|
|
color->comp[1].u8[0] = rgba[3];
|
2017-08-07 16:39:29 +00:00
|
|
|
EXPAND(1);
|
2012-02-23 10:07:01 +00:00
|
|
|
} else {
|
|
|
|
av_log(NULL, AV_LOG_WARNING,
|
|
|
|
"Color conversion not implemented for %s\n", draw->desc->name);
|
|
|
|
memset(color, 128, sizeof(*color));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
|
|
|
|
int plane, int x, int y)
|
|
|
|
{
|
|
|
|
return data[plane] +
|
|
|
|
(y >> draw->vsub[plane]) * linesize[plane] +
|
|
|
|
(x >> draw->hsub[plane]) * draw->pixelstep[plane];
|
|
|
|
}
|
|
|
|
|
|
|
|
void ff_copy_rectangle2(FFDrawContext *draw,
|
|
|
|
uint8_t *dst[], int dst_linesize[],
|
|
|
|
uint8_t *src[], int src_linesize[],
|
|
|
|
int dst_x, int dst_y, int src_x, int src_y,
|
|
|
|
int w, int h)
|
|
|
|
{
|
|
|
|
int plane, y, wp, hp;
|
|
|
|
uint8_t *p, *q;
|
|
|
|
|
|
|
|
for (plane = 0; plane < draw->nb_planes; plane++) {
|
|
|
|
p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
|
|
|
|
q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
|
2016-01-27 15:19:38 +00:00
|
|
|
wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane];
|
|
|
|
hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
|
2012-02-23 10:07:01 +00:00
|
|
|
for (y = 0; y < hp; y++) {
|
|
|
|
memcpy(q, p, wp);
|
|
|
|
p += src_linesize[plane];
|
|
|
|
q += dst_linesize[plane];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
|
|
|
|
uint8_t *dst[], int dst_linesize[],
|
|
|
|
int dst_x, int dst_y, int w, int h)
|
|
|
|
{
|
|
|
|
int plane, x, y, wp, hp;
|
|
|
|
uint8_t *p0, *p;
|
2016-02-24 22:11:54 +00:00
|
|
|
FFDrawColor color_tmp = *color;
|
2012-02-23 10:07:01 +00:00
|
|
|
|
|
|
|
for (plane = 0; plane < draw->nb_planes; plane++) {
|
|
|
|
p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
|
2016-01-27 15:19:38 +00:00
|
|
|
wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
|
|
|
|
hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
|
2012-02-23 10:07:01 +00:00
|
|
|
if (!hp)
|
|
|
|
return;
|
|
|
|
p = p0;
|
2016-02-24 22:11:54 +00:00
|
|
|
|
|
|
|
if (HAVE_BIGENDIAN && draw->desc->comp[0].depth > 8) {
|
|
|
|
for (x = 0; 2*x < draw->pixelstep[plane]; x++)
|
|
|
|
color_tmp.comp[plane].u16[x] = av_bswap16(color_tmp.comp[plane].u16[x]);
|
|
|
|
}
|
|
|
|
|
2012-02-23 10:07:01 +00:00
|
|
|
/* copy first line from color */
|
|
|
|
for (x = 0; x < wp; x++) {
|
2016-02-24 22:11:54 +00:00
|
|
|
memcpy(p, color_tmp.comp[plane].u8, draw->pixelstep[plane]);
|
2012-02-23 10:07:01 +00:00
|
|
|
p += draw->pixelstep[plane];
|
|
|
|
}
|
|
|
|
wp *= draw->pixelstep[plane];
|
|
|
|
/* copy next lines from first line */
|
|
|
|
p = p0 + dst_linesize[plane];
|
|
|
|
for (y = 1; y < hp; y++) {
|
|
|
|
memcpy(p, p0, wp);
|
|
|
|
p += dst_linesize[plane];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:26:45 +00:00
|
|
|
/**
|
|
|
|
* Clip interval [x; x+w[ within [0; wmax[.
|
|
|
|
* The resulting w may be negative if the final interval is empty.
|
|
|
|
* dx, if not null, return the difference between in and out value of x.
|
|
|
|
*/
|
|
|
|
static void clip_interval(int wmax, int *x, int *w, int *dx)
|
|
|
|
{
|
|
|
|
if (dx)
|
|
|
|
*dx = 0;
|
|
|
|
if (*x < 0) {
|
|
|
|
if (dx)
|
|
|
|
*dx = -*x;
|
|
|
|
*w += *x;
|
|
|
|
*x = 0;
|
|
|
|
}
|
|
|
|
if (*x + *w > wmax)
|
|
|
|
*w = wmax - *x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decompose w pixels starting at x
|
|
|
|
* into start + (w starting at x) + end
|
|
|
|
* with x and w aligned on multiples of 1<<sub.
|
|
|
|
*/
|
|
|
|
static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
|
|
|
|
{
|
|
|
|
int mask = (1 << sub) - 1;
|
|
|
|
|
|
|
|
*start = (-*x) & mask;
|
|
|
|
*x += *start;
|
|
|
|
*start = FFMIN(*start, *w);
|
|
|
|
*w -= *start;
|
|
|
|
*end = *w & mask;
|
|
|
|
*w >>= sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int component_used(FFDrawContext *draw, int plane, int comp)
|
|
|
|
{
|
|
|
|
return (draw->comp_mask[plane] >> comp) & 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If alpha is in the [ 0 ; 0x1010101 ] range,
|
|
|
|
then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
|
|
|
|
and >> 24 gives a correct rounding. */
|
|
|
|
static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
|
|
|
|
int dx, int w, unsigned hsub, int left, int right)
|
|
|
|
{
|
|
|
|
unsigned asrc = alpha * src;
|
|
|
|
unsigned tau = 0x1010101 - alpha;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
if (left) {
|
|
|
|
unsigned suba = (left * alpha) >> hsub;
|
|
|
|
*dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
|
|
|
|
dst += dx;
|
|
|
|
}
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
*dst = (*dst * tau + asrc) >> 24;
|
|
|
|
dst += dx;
|
|
|
|
}
|
|
|
|
if (right) {
|
|
|
|
unsigned suba = (right * alpha) >> hsub;
|
|
|
|
*dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 16:07:33 +00:00
|
|
|
static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
|
|
|
|
int dx, int w, unsigned hsub, int left, int right)
|
|
|
|
{
|
|
|
|
unsigned asrc = alpha * src;
|
|
|
|
unsigned tau = 0x10001 - alpha;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
if (left) {
|
|
|
|
unsigned suba = (left * alpha) >> hsub;
|
|
|
|
uint16_t value = AV_RL16(dst);
|
|
|
|
AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
|
|
|
|
dst += dx;
|
|
|
|
}
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
uint16_t value = AV_RL16(dst);
|
|
|
|
AV_WL16(dst, (value * tau + asrc) >> 16);
|
|
|
|
dst += dx;
|
|
|
|
}
|
|
|
|
if (right) {
|
|
|
|
unsigned suba = (right * alpha) >> hsub;
|
|
|
|
uint16_t value = AV_RL16(dst);
|
|
|
|
AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:26:45 +00:00
|
|
|
void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
|
|
|
|
uint8_t *dst[], int dst_linesize[],
|
|
|
|
int dst_w, int dst_h,
|
|
|
|
int x0, int y0, int w, int h)
|
|
|
|
{
|
|
|
|
unsigned alpha, nb_planes, nb_comp, plane, comp;
|
|
|
|
int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
|
|
|
|
uint8_t *p0, *p;
|
|
|
|
|
|
|
|
/* TODO optimize if alpha = 0xFF */
|
|
|
|
clip_interval(dst_w, &x0, &w, NULL);
|
|
|
|
clip_interval(dst_h, &y0, &h, NULL);
|
|
|
|
if (w <= 0 || h <= 0 || !color->rgba[3])
|
|
|
|
return;
|
2016-02-15 16:07:33 +00:00
|
|
|
if (draw->desc->comp[0].depth <= 8) {
|
|
|
|
/* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
|
|
|
|
alpha = 0x10203 * color->rgba[3] + 0x2;
|
|
|
|
} else {
|
|
|
|
/* 0x101 * alpha is in the [ 2 ; 0x1001] range */
|
|
|
|
alpha = 0x101 * color->rgba[3] + 0x2;
|
|
|
|
}
|
2016-09-13 23:37:13 +00:00
|
|
|
nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA));
|
2016-08-09 10:22:15 +00:00
|
|
|
nb_planes += !nb_planes;
|
2012-03-28 13:26:45 +00:00
|
|
|
for (plane = 0; plane < nb_planes; plane++) {
|
|
|
|
nb_comp = draw->pixelstep[plane];
|
|
|
|
p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
|
|
|
|
w_sub = w;
|
|
|
|
h_sub = h;
|
|
|
|
x_sub = x0;
|
|
|
|
y_sub = y0;
|
|
|
|
subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
|
|
|
|
subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
|
|
|
|
for (comp = 0; comp < nb_comp; comp++) {
|
2016-02-15 16:07:33 +00:00
|
|
|
const int depth = draw->desc->comp[comp].depth;
|
|
|
|
|
2012-03-28 13:26:45 +00:00
|
|
|
if (!component_used(draw, plane, comp))
|
|
|
|
continue;
|
|
|
|
p = p0 + comp;
|
|
|
|
if (top) {
|
2016-02-15 16:07:33 +00:00
|
|
|
if (depth <= 8) {
|
|
|
|
blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
|
|
|
|
draw->pixelstep[plane], w_sub,
|
|
|
|
draw->hsub[plane], left, right);
|
|
|
|
} else {
|
|
|
|
blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
|
|
|
|
draw->pixelstep[plane], w_sub,
|
|
|
|
draw->hsub[plane], left, right);
|
|
|
|
}
|
2012-03-28 13:26:45 +00:00
|
|
|
p += dst_linesize[plane];
|
|
|
|
}
|
2016-02-15 16:07:33 +00:00
|
|
|
if (depth <= 8) {
|
|
|
|
for (y = 0; y < h_sub; y++) {
|
|
|
|
blend_line(p, color->comp[plane].u8[comp], alpha,
|
|
|
|
draw->pixelstep[plane], w_sub,
|
|
|
|
draw->hsub[plane], left, right);
|
|
|
|
p += dst_linesize[plane];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (y = 0; y < h_sub; y++) {
|
|
|
|
blend_line16(p, color->comp[plane].u16[comp], alpha,
|
|
|
|
draw->pixelstep[plane], w_sub,
|
|
|
|
draw->hsub[plane], left, right);
|
|
|
|
p += dst_linesize[plane];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bottom) {
|
|
|
|
if (depth <= 8) {
|
|
|
|
blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
|
|
|
|
draw->pixelstep[plane], w_sub,
|
|
|
|
draw->hsub[plane], left, right);
|
|
|
|
} else {
|
|
|
|
blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
|
|
|
|
draw->pixelstep[plane], w_sub,
|
|
|
|
draw->hsub[plane], left, right);
|
|
|
|
}
|
2012-03-28 13:26:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 16:07:33 +00:00
|
|
|
static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
|
|
|
|
const uint8_t *mask, int mask_linesize, int l2depth,
|
|
|
|
unsigned w, unsigned h, unsigned shift, unsigned xm0)
|
|
|
|
{
|
|
|
|
unsigned xm, x, y, t = 0;
|
|
|
|
unsigned xmshf = 3 - l2depth;
|
|
|
|
unsigned xmmod = 7 >> l2depth;
|
|
|
|
unsigned mbits = (1 << (1 << l2depth)) - 1;
|
|
|
|
unsigned mmult = 255 / mbits;
|
|
|
|
uint16_t value = AV_RL16(dst);
|
|
|
|
|
|
|
|
for (y = 0; y < h; y++) {
|
|
|
|
xm = xm0;
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
|
|
|
|
* mmult;
|
|
|
|
xm++;
|
|
|
|
}
|
|
|
|
mask += mask_linesize;
|
|
|
|
}
|
|
|
|
alpha = (t >> shift) * alpha;
|
|
|
|
AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:26:45 +00:00
|
|
|
static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
|
2015-10-25 10:27:15 +00:00
|
|
|
const uint8_t *mask, int mask_linesize, int l2depth,
|
2012-03-28 13:26:45 +00:00
|
|
|
unsigned w, unsigned h, unsigned shift, unsigned xm0)
|
|
|
|
{
|
|
|
|
unsigned xm, x, y, t = 0;
|
|
|
|
unsigned xmshf = 3 - l2depth;
|
|
|
|
unsigned xmmod = 7 >> l2depth;
|
|
|
|
unsigned mbits = (1 << (1 << l2depth)) - 1;
|
|
|
|
unsigned mmult = 255 / mbits;
|
|
|
|
|
|
|
|
for (y = 0; y < h; y++) {
|
|
|
|
xm = xm0;
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
|
|
|
|
* mmult;
|
|
|
|
xm++;
|
|
|
|
}
|
|
|
|
mask += mask_linesize;
|
|
|
|
}
|
|
|
|
alpha = (t >> shift) * alpha;
|
|
|
|
*dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
|
|
|
|
}
|
|
|
|
|
2016-02-15 16:07:33 +00:00
|
|
|
static void blend_line_hv16(uint8_t *dst, int dst_delta,
|
|
|
|
unsigned src, unsigned alpha,
|
|
|
|
const uint8_t *mask, int mask_linesize, int l2depth, int w,
|
|
|
|
unsigned hsub, unsigned vsub,
|
|
|
|
int xm, int left, int right, int hband)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
|
|
|
|
if (left) {
|
|
|
|
blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
|
|
|
|
left, hband, hsub + vsub, xm);
|
|
|
|
dst += dst_delta;
|
|
|
|
xm += left;
|
|
|
|
}
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
|
|
|
|
1 << hsub, hband, hsub + vsub, xm);
|
|
|
|
dst += dst_delta;
|
|
|
|
xm += 1 << hsub;
|
|
|
|
}
|
|
|
|
if (right)
|
|
|
|
blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
|
|
|
|
right, hband, hsub + vsub, xm);
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:26:45 +00:00
|
|
|
static void blend_line_hv(uint8_t *dst, int dst_delta,
|
|
|
|
unsigned src, unsigned alpha,
|
2015-10-25 10:27:15 +00:00
|
|
|
const uint8_t *mask, int mask_linesize, int l2depth, int w,
|
2012-03-28 13:26:45 +00:00
|
|
|
unsigned hsub, unsigned vsub,
|
|
|
|
int xm, int left, int right, int hband)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
|
|
|
|
if (left) {
|
|
|
|
blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
|
|
|
|
left, hband, hsub + vsub, xm);
|
|
|
|
dst += dst_delta;
|
|
|
|
xm += left;
|
|
|
|
}
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
|
|
|
|
1 << hsub, hband, hsub + vsub, xm);
|
|
|
|
dst += dst_delta;
|
|
|
|
xm += 1 << hsub;
|
|
|
|
}
|
|
|
|
if (right)
|
|
|
|
blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
|
|
|
|
right, hband, hsub + vsub, xm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
|
|
|
|
uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
|
2015-10-25 10:27:15 +00:00
|
|
|
const uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
|
2012-03-28 13:26:45 +00:00
|
|
|
int l2depth, unsigned endianness, int x0, int y0)
|
|
|
|
{
|
|
|
|
unsigned alpha, nb_planes, nb_comp, plane, comp;
|
|
|
|
int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
|
2015-10-25 10:27:15 +00:00
|
|
|
uint8_t *p0, *p;
|
|
|
|
const uint8_t *m;
|
2012-03-28 13:26:45 +00:00
|
|
|
|
|
|
|
clip_interval(dst_w, &x0, &mask_w, &xm0);
|
|
|
|
clip_interval(dst_h, &y0, &mask_h, &ym0);
|
|
|
|
mask += ym0 * mask_linesize;
|
|
|
|
if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
|
|
|
|
return;
|
2016-02-15 16:07:33 +00:00
|
|
|
if (draw->desc->comp[0].depth <= 8) {
|
|
|
|
/* alpha is in the [ 0 ; 0x10203 ] range,
|
|
|
|
alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
|
|
|
|
alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
|
|
|
|
} else {
|
|
|
|
alpha = (0x101 * color->rgba[3] + 0x2) >> 8;
|
|
|
|
}
|
2016-09-13 23:37:13 +00:00
|
|
|
nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA));
|
2016-08-09 10:22:15 +00:00
|
|
|
nb_planes += !nb_planes;
|
2012-03-28 13:26:45 +00:00
|
|
|
for (plane = 0; plane < nb_planes; plane++) {
|
|
|
|
nb_comp = draw->pixelstep[plane];
|
|
|
|
p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
|
|
|
|
w_sub = mask_w;
|
|
|
|
h_sub = mask_h;
|
|
|
|
x_sub = x0;
|
|
|
|
y_sub = y0;
|
|
|
|
subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
|
|
|
|
subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
|
|
|
|
for (comp = 0; comp < nb_comp; comp++) {
|
2016-02-15 16:07:33 +00:00
|
|
|
const int depth = draw->desc->comp[comp].depth;
|
|
|
|
|
2012-03-28 13:26:45 +00:00
|
|
|
if (!component_used(draw, plane, comp))
|
|
|
|
continue;
|
|
|
|
p = p0 + comp;
|
|
|
|
m = mask;
|
|
|
|
if (top) {
|
2016-02-15 16:07:33 +00:00
|
|
|
if (depth <= 8) {
|
|
|
|
blend_line_hv(p, draw->pixelstep[plane],
|
|
|
|
color->comp[plane].u8[comp], alpha,
|
|
|
|
m, mask_linesize, l2depth, w_sub,
|
|
|
|
draw->hsub[plane], draw->vsub[plane],
|
|
|
|
xm0, left, right, top);
|
|
|
|
} else {
|
|
|
|
blend_line_hv16(p, draw->pixelstep[plane],
|
|
|
|
color->comp[plane].u16[comp], alpha,
|
|
|
|
m, mask_linesize, l2depth, w_sub,
|
|
|
|
draw->hsub[plane], draw->vsub[plane],
|
|
|
|
xm0, left, right, top);
|
|
|
|
}
|
2012-03-28 13:26:45 +00:00
|
|
|
p += dst_linesize[plane];
|
|
|
|
m += top * mask_linesize;
|
|
|
|
}
|
2016-02-15 16:07:33 +00:00
|
|
|
if (depth <= 8) {
|
|
|
|
for (y = 0; y < h_sub; y++) {
|
|
|
|
blend_line_hv(p, draw->pixelstep[plane],
|
|
|
|
color->comp[plane].u8[comp], alpha,
|
|
|
|
m, mask_linesize, l2depth, w_sub,
|
|
|
|
draw->hsub[plane], draw->vsub[plane],
|
|
|
|
xm0, left, right, 1 << draw->vsub[plane]);
|
|
|
|
p += dst_linesize[plane];
|
|
|
|
m += mask_linesize << draw->vsub[plane];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (y = 0; y < h_sub; y++) {
|
|
|
|
blend_line_hv16(p, draw->pixelstep[plane],
|
|
|
|
color->comp[plane].u16[comp], alpha,
|
|
|
|
m, mask_linesize, l2depth, w_sub,
|
|
|
|
draw->hsub[plane], draw->vsub[plane],
|
|
|
|
xm0, left, right, 1 << draw->vsub[plane]);
|
|
|
|
p += dst_linesize[plane];
|
|
|
|
m += mask_linesize << draw->vsub[plane];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bottom) {
|
|
|
|
if (depth <= 8) {
|
|
|
|
blend_line_hv(p, draw->pixelstep[plane],
|
|
|
|
color->comp[plane].u8[comp], alpha,
|
|
|
|
m, mask_linesize, l2depth, w_sub,
|
|
|
|
draw->hsub[plane], draw->vsub[plane],
|
|
|
|
xm0, left, right, bottom);
|
|
|
|
} else {
|
|
|
|
blend_line_hv16(p, draw->pixelstep[plane],
|
|
|
|
color->comp[plane].u16[comp], alpha,
|
|
|
|
m, mask_linesize, l2depth, w_sub,
|
|
|
|
draw->hsub[plane], draw->vsub[plane],
|
|
|
|
xm0, left, right, bottom);
|
|
|
|
}
|
2012-03-28 13:26:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 10:07:01 +00:00
|
|
|
int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
|
|
|
|
int value)
|
|
|
|
{
|
|
|
|
unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
|
|
|
|
|
|
|
|
if (!shift)
|
|
|
|
return value;
|
|
|
|
if (round_dir >= 0)
|
|
|
|
value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
|
|
|
|
return (value >> shift) << shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
|
|
|
|
{
|
2014-05-27 15:43:03 +00:00
|
|
|
enum AVPixelFormat i;
|
2012-02-23 10:07:01 +00:00
|
|
|
FFDrawContext draw;
|
2014-05-27 15:43:03 +00:00
|
|
|
AVFilterFormats *fmts = NULL;
|
2015-10-05 03:39:25 +00:00
|
|
|
int ret;
|
2012-02-23 10:07:01 +00:00
|
|
|
|
2014-05-27 15:43:03 +00:00
|
|
|
for (i = 0; av_pix_fmt_desc_get(i); i++)
|
2015-10-05 03:39:25 +00:00
|
|
|
if (ff_draw_init(&draw, i, flags) >= 0 &&
|
|
|
|
(ret = ff_add_format(&fmts, i)) < 0)
|
|
|
|
return NULL;
|
2014-05-27 15:43:03 +00:00
|
|
|
return fmts;
|
2012-02-23 10:07:01 +00:00
|
|
|
}
|