2013-04-14 22:16:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Paul B Mahol
|
|
|
|
*
|
|
|
|
* 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 "libavutil/opt.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "drawutils.h"
|
|
|
|
#include "formats.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "video.h"
|
|
|
|
|
|
|
|
#define R 0
|
|
|
|
#define G 1
|
|
|
|
#define B 2
|
|
|
|
#define A 3
|
|
|
|
|
2018-05-05 10:50:16 +00:00
|
|
|
typedef struct ThreadData {
|
|
|
|
AVFrame *in, *out;
|
|
|
|
} ThreadData;
|
|
|
|
|
2017-05-12 18:00:49 +00:00
|
|
|
typedef struct Range {
|
2019-11-13 11:56:04 +00:00
|
|
|
float shadows;
|
|
|
|
float midtones;
|
|
|
|
float highlights;
|
2013-04-14 22:16:51 +00:00
|
|
|
} Range;
|
|
|
|
|
2017-05-12 18:00:49 +00:00
|
|
|
typedef struct ColorBalanceContext {
|
2013-04-14 22:16:51 +00:00
|
|
|
const AVClass *class;
|
|
|
|
Range cyan_red;
|
|
|
|
Range magenta_green;
|
|
|
|
Range yellow_blue;
|
2019-11-13 11:26:18 +00:00
|
|
|
int preserve_lightness;
|
2013-04-14 22:16:51 +00:00
|
|
|
|
|
|
|
uint8_t rgba_map[4];
|
2019-11-12 23:22:13 +00:00
|
|
|
int depth;
|
2019-11-13 11:26:18 +00:00
|
|
|
int max;
|
2013-04-14 22:16:51 +00:00
|
|
|
int step;
|
2018-05-04 22:30:21 +00:00
|
|
|
|
2019-11-12 23:22:13 +00:00
|
|
|
int (*color_balance)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
|
2013-04-14 22:16:51 +00:00
|
|
|
} ColorBalanceContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(ColorBalanceContext, x)
|
2019-11-13 11:59:17 +00:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
|
2013-04-14 22:16:51 +00:00
|
|
|
static const AVOption colorbalance_options[] = {
|
2019-11-13 11:56:04 +00:00
|
|
|
{ "rs", "set red shadows", OFFSET(cyan_red.shadows), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
|
|
|
|
{ "gs", "set green shadows", OFFSET(magenta_green.shadows), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
|
|
|
|
{ "bs", "set blue shadows", OFFSET(yellow_blue.shadows), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
|
|
|
|
{ "rm", "set red midtones", OFFSET(cyan_red.midtones), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
|
|
|
|
{ "gm", "set green midtones", OFFSET(magenta_green.midtones), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
|
|
|
|
{ "bm", "set blue midtones", OFFSET(yellow_blue.midtones), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
|
|
|
|
{ "rh", "set red highlights", OFFSET(cyan_red.highlights), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
|
|
|
|
{ "gh", "set green highlights", OFFSET(magenta_green.highlights), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
|
|
|
|
{ "bh", "set blue highlights", OFFSET(yellow_blue.highlights), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
|
|
|
|
{ "pl", "preserve lightness", OFFSET(preserve_lightness), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
|
2013-04-14 22:16:51 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(colorbalance);
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
|
|
|
AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
|
|
|
|
AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
|
|
|
|
AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB,
|
|
|
|
AV_PIX_FMT_0BGR, AV_PIX_FMT_0RGB,
|
|
|
|
AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
|
2018-05-04 22:30:21 +00:00
|
|
|
AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
|
|
|
|
AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
|
2018-05-05 10:22:52 +00:00
|
|
|
AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
|
|
|
|
AV_PIX_FMT_GBRP9,
|
|
|
|
AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
|
|
|
|
AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
|
|
|
|
AV_PIX_FMT_GBRP14,
|
|
|
|
AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
|
2013-04-14 22:16:51 +00:00
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
2021-08-09 23:25:31 +00:00
|
|
|
return ff_set_common_formats_from_list(ctx, pix_fmts);
|
2013-04-14 22:16:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 11:56:04 +00:00
|
|
|
static float get_component(float v, float l,
|
|
|
|
float s, float m, float h)
|
2019-11-12 23:22:13 +00:00
|
|
|
{
|
2019-11-13 11:56:04 +00:00
|
|
|
const float a = 4.f, b = 0.333f, scale = 0.7f;
|
2019-11-12 23:22:13 +00:00
|
|
|
|
2021-09-26 09:16:04 +00:00
|
|
|
s *= av_clipf((b - l) * a + 0.5f, 0.f, 1.f) * scale;
|
|
|
|
m *= av_clipf((l - b) * a + 0.5f, 0.f, 1.f) * av_clipf((1.f - l - b) * a + 0.5f, 0.f, 1.f) * scale;
|
|
|
|
h *= av_clipf((l + b - 1) * a + 0.5f, 0.f, 1.f) * scale;
|
2019-11-12 23:22:13 +00:00
|
|
|
|
|
|
|
v += s;
|
|
|
|
v += m;
|
|
|
|
v += h;
|
|
|
|
|
2021-09-26 09:16:04 +00:00
|
|
|
return av_clipf(v, 0.f, 1.f);
|
2019-11-13 11:26:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 11:56:04 +00:00
|
|
|
static float hfun(float n, float h, float s, float l)
|
2019-11-13 11:26:18 +00:00
|
|
|
{
|
2021-09-26 09:16:04 +00:00
|
|
|
float a = s * FFMIN(l, 1.f - l);
|
2019-11-13 11:56:04 +00:00
|
|
|
float k = fmodf(n + h / 30.f, 12.f);
|
2019-11-13 11:26:18 +00:00
|
|
|
|
2021-09-26 09:16:04 +00:00
|
|
|
return av_clipf(l - a * FFMAX(FFMIN3(k - 3.f, 9.f - k, 1), -1.f), 0.f, 1.f);
|
2019-11-13 11:26:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 11:56:04 +00:00
|
|
|
static void preservel(float *r, float *g, float *b, float l)
|
2019-11-13 11:26:18 +00:00
|
|
|
{
|
2019-11-13 11:56:04 +00:00
|
|
|
float max = FFMAX3(*r, *g, *b);
|
|
|
|
float min = FFMIN3(*r, *g, *b);
|
|
|
|
float h, s;
|
2019-11-13 11:26:18 +00:00
|
|
|
|
2021-09-26 09:16:04 +00:00
|
|
|
l *= 0.5f;
|
2019-11-13 11:26:18 +00:00
|
|
|
|
|
|
|
if (*r == *g && *g == *b) {
|
2021-09-26 09:16:04 +00:00
|
|
|
h = 0.f;
|
2019-11-13 11:26:18 +00:00
|
|
|
} else if (max == *r) {
|
2021-09-26 09:16:04 +00:00
|
|
|
h = 60.f * (0.f + (*g - *b) / (max - min));
|
2019-11-13 11:26:18 +00:00
|
|
|
} else if (max == *g) {
|
2021-09-26 09:16:04 +00:00
|
|
|
h = 60.f * (2.f + (*b - *r) / (max - min));
|
2019-11-13 11:26:18 +00:00
|
|
|
} else if (max == *b) {
|
2021-09-26 09:16:04 +00:00
|
|
|
h = 60.f * (4.f + (*r - *g) / (max - min));
|
2019-11-13 11:26:18 +00:00
|
|
|
} else {
|
2021-09-26 09:16:04 +00:00
|
|
|
h = 0.f;
|
2019-11-13 11:26:18 +00:00
|
|
|
}
|
2021-09-26 09:16:04 +00:00
|
|
|
if (h < 0.f)
|
|
|
|
h += 360.f;
|
2019-11-13 11:26:18 +00:00
|
|
|
|
2021-09-26 09:16:04 +00:00
|
|
|
if (max == 1.f || min == 0.f) {
|
|
|
|
s = 0.f;
|
2019-11-13 11:26:18 +00:00
|
|
|
} else {
|
2021-09-26 09:16:04 +00:00
|
|
|
s = (max - min) / (1.f - (FFABS(2.f * l - 1.f)));
|
2019-11-13 11:26:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 09:16:04 +00:00
|
|
|
*r = hfun(0.f, h, s, l);
|
|
|
|
*g = hfun(8.f, h, s, l);
|
|
|
|
*b = hfun(4.f, h, s, l);
|
2019-11-12 23:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int color_balance8_p(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
2018-05-05 10:22:52 +00:00
|
|
|
{
|
|
|
|
ColorBalanceContext *s = ctx->priv;
|
2018-05-05 10:50:16 +00:00
|
|
|
ThreadData *td = arg;
|
|
|
|
AVFrame *in = td->in;
|
|
|
|
AVFrame *out = td->out;
|
|
|
|
const int slice_start = (out->height * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
|
|
|
|
const uint8_t *srcg = in->data[0] + slice_start * in->linesize[0];
|
|
|
|
const uint8_t *srcb = in->data[1] + slice_start * in->linesize[1];
|
|
|
|
const uint8_t *srcr = in->data[2] + slice_start * in->linesize[2];
|
|
|
|
const uint8_t *srca = in->data[3] + slice_start * in->linesize[3];
|
|
|
|
uint8_t *dstg = out->data[0] + slice_start * out->linesize[0];
|
|
|
|
uint8_t *dstb = out->data[1] + slice_start * out->linesize[1];
|
|
|
|
uint8_t *dstr = out->data[2] + slice_start * out->linesize[2];
|
|
|
|
uint8_t *dsta = out->data[3] + slice_start * out->linesize[3];
|
2019-11-13 11:56:04 +00:00
|
|
|
const float max = s->max;
|
2018-05-05 10:22:52 +00:00
|
|
|
int i, j;
|
|
|
|
|
2018-05-05 10:50:16 +00:00
|
|
|
for (i = slice_start; i < slice_end; i++) {
|
2018-05-05 10:22:52 +00:00
|
|
|
for (j = 0; j < out->width; j++) {
|
2019-11-13 11:56:04 +00:00
|
|
|
float r = srcr[j] / max;
|
|
|
|
float g = srcg[j] / max;
|
|
|
|
float b = srcb[j] / max;
|
|
|
|
const float l = FFMAX3(r, g, b) + FFMIN3(r, g, b);
|
2019-11-13 11:26:18 +00:00
|
|
|
|
|
|
|
r = get_component(r, l, s->cyan_red.shadows, s->cyan_red.midtones, s->cyan_red.highlights);
|
|
|
|
g = get_component(g, l, s->magenta_green.shadows, s->magenta_green.midtones, s->magenta_green.highlights);
|
|
|
|
b = get_component(b, l, s->yellow_blue.shadows, s->yellow_blue.midtones, s->yellow_blue.highlights);
|
|
|
|
|
|
|
|
if (s->preserve_lightness)
|
|
|
|
preservel(&r, &g, &b, l);
|
|
|
|
|
2020-07-01 12:57:58 +00:00
|
|
|
dstr[j] = av_clip_uint8(lrintf(r * max));
|
|
|
|
dstg[j] = av_clip_uint8(lrintf(g * max));
|
|
|
|
dstb[j] = av_clip_uint8(lrintf(b * max));
|
2018-05-05 10:22:52 +00:00
|
|
|
if (in != out && out->linesize[3])
|
|
|
|
dsta[j] = srca[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
srcg += in->linesize[0];
|
|
|
|
srcb += in->linesize[1];
|
|
|
|
srcr += in->linesize[2];
|
|
|
|
srca += in->linesize[3];
|
|
|
|
dstg += out->linesize[0];
|
|
|
|
dstb += out->linesize[1];
|
|
|
|
dstr += out->linesize[2];
|
|
|
|
dsta += out->linesize[3];
|
|
|
|
}
|
2018-05-05 10:50:16 +00:00
|
|
|
|
|
|
|
return 0;
|
2018-05-05 10:22:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 23:22:13 +00:00
|
|
|
static int color_balance16_p(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
2018-05-05 10:22:52 +00:00
|
|
|
{
|
|
|
|
ColorBalanceContext *s = ctx->priv;
|
2018-05-05 10:50:16 +00:00
|
|
|
ThreadData *td = arg;
|
|
|
|
AVFrame *in = td->in;
|
|
|
|
AVFrame *out = td->out;
|
|
|
|
const int slice_start = (out->height * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
|
|
|
|
const uint16_t *srcg = (const uint16_t *)in->data[0] + slice_start * in->linesize[0] / 2;
|
|
|
|
const uint16_t *srcb = (const uint16_t *)in->data[1] + slice_start * in->linesize[1] / 2;
|
|
|
|
const uint16_t *srcr = (const uint16_t *)in->data[2] + slice_start * in->linesize[2] / 2;
|
|
|
|
const uint16_t *srca = (const uint16_t *)in->data[3] + slice_start * in->linesize[3] / 2;
|
|
|
|
uint16_t *dstg = (uint16_t *)out->data[0] + slice_start * out->linesize[0] / 2;
|
|
|
|
uint16_t *dstb = (uint16_t *)out->data[1] + slice_start * out->linesize[1] / 2;
|
|
|
|
uint16_t *dstr = (uint16_t *)out->data[2] + slice_start * out->linesize[2] / 2;
|
|
|
|
uint16_t *dsta = (uint16_t *)out->data[3] + slice_start * out->linesize[3] / 2;
|
2019-11-12 23:22:13 +00:00
|
|
|
const int depth = s->depth;
|
2019-11-13 11:56:04 +00:00
|
|
|
const float max = s->max;
|
2018-05-05 10:22:52 +00:00
|
|
|
int i, j;
|
|
|
|
|
2018-05-05 10:50:16 +00:00
|
|
|
for (i = slice_start; i < slice_end; i++) {
|
2018-05-05 10:22:52 +00:00
|
|
|
for (j = 0; j < out->width; j++) {
|
2019-11-13 11:56:04 +00:00
|
|
|
float r = srcr[j] / max;
|
|
|
|
float g = srcg[j] / max;
|
|
|
|
float b = srcb[j] / max;
|
|
|
|
const float l = (FFMAX3(r, g, b) + FFMIN3(r, g, b));
|
2019-11-13 11:26:18 +00:00
|
|
|
|
|
|
|
r = get_component(r, l, s->cyan_red.shadows, s->cyan_red.midtones, s->cyan_red.highlights);
|
|
|
|
g = get_component(g, l, s->magenta_green.shadows, s->magenta_green.midtones, s->magenta_green.highlights);
|
|
|
|
b = get_component(b, l, s->yellow_blue.shadows, s->yellow_blue.midtones, s->yellow_blue.highlights);
|
|
|
|
|
|
|
|
if (s->preserve_lightness)
|
|
|
|
preservel(&r, &g, &b, l);
|
|
|
|
|
2020-07-01 12:57:58 +00:00
|
|
|
dstr[j] = av_clip_uintp2_c(lrintf(r * max), depth);
|
|
|
|
dstg[j] = av_clip_uintp2_c(lrintf(g * max), depth);
|
|
|
|
dstb[j] = av_clip_uintp2_c(lrintf(b * max), depth);
|
2018-05-05 10:22:52 +00:00
|
|
|
if (in != out && out->linesize[3])
|
|
|
|
dsta[j] = srca[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
srcg += in->linesize[0] / 2;
|
|
|
|
srcb += in->linesize[1] / 2;
|
|
|
|
srcr += in->linesize[2] / 2;
|
|
|
|
srca += in->linesize[3] / 2;
|
|
|
|
dstg += out->linesize[0] / 2;
|
|
|
|
dstb += out->linesize[1] / 2;
|
|
|
|
dstr += out->linesize[2] / 2;
|
|
|
|
dsta += out->linesize[3] / 2;
|
|
|
|
}
|
2018-05-05 10:50:16 +00:00
|
|
|
|
|
|
|
return 0;
|
2018-05-05 10:22:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 23:22:13 +00:00
|
|
|
static int color_balance8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
2018-05-04 22:30:21 +00:00
|
|
|
{
|
|
|
|
ColorBalanceContext *s = ctx->priv;
|
2018-05-05 10:50:16 +00:00
|
|
|
ThreadData *td = arg;
|
|
|
|
AVFrame *in = td->in;
|
|
|
|
AVFrame *out = td->out;
|
2018-05-04 22:30:21 +00:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2018-05-05 10:50:16 +00:00
|
|
|
const int slice_start = (out->height * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
|
|
|
|
const uint8_t *srcrow = in->data[0] + slice_start * in->linesize[0];
|
2018-05-04 22:30:21 +00:00
|
|
|
const uint8_t roffset = s->rgba_map[R];
|
|
|
|
const uint8_t goffset = s->rgba_map[G];
|
|
|
|
const uint8_t boffset = s->rgba_map[B];
|
|
|
|
const uint8_t aoffset = s->rgba_map[A];
|
2019-11-13 11:56:04 +00:00
|
|
|
const float max = s->max;
|
2018-05-04 22:30:21 +00:00
|
|
|
const int step = s->step;
|
|
|
|
uint8_t *dstrow;
|
|
|
|
int i, j;
|
|
|
|
|
2018-05-05 10:50:16 +00:00
|
|
|
dstrow = out->data[0] + slice_start * out->linesize[0];
|
|
|
|
for (i = slice_start; i < slice_end; i++) {
|
2018-05-04 22:30:21 +00:00
|
|
|
const uint8_t *src = srcrow;
|
|
|
|
uint8_t *dst = dstrow;
|
|
|
|
|
|
|
|
for (j = 0; j < outlink->w * step; j += step) {
|
2019-11-13 11:56:04 +00:00
|
|
|
float r = src[j + roffset] / max;
|
|
|
|
float g = src[j + goffset] / max;
|
|
|
|
float b = src[j + boffset] / max;
|
|
|
|
const float l = (FFMAX3(r, g, b) + FFMIN3(r, g, b));
|
2019-11-13 11:26:18 +00:00
|
|
|
|
|
|
|
r = get_component(r, l, s->cyan_red.shadows, s->cyan_red.midtones, s->cyan_red.highlights);
|
|
|
|
g = get_component(g, l, s->magenta_green.shadows, s->magenta_green.midtones, s->magenta_green.highlights);
|
|
|
|
b = get_component(b, l, s->yellow_blue.shadows, s->yellow_blue.midtones, s->yellow_blue.highlights);
|
|
|
|
|
|
|
|
if (s->preserve_lightness)
|
|
|
|
preservel(&r, &g, &b, l);
|
|
|
|
|
2020-07-01 12:57:58 +00:00
|
|
|
dst[j + roffset] = av_clip_uint8(lrintf(r * max));
|
|
|
|
dst[j + goffset] = av_clip_uint8(lrintf(g * max));
|
|
|
|
dst[j + boffset] = av_clip_uint8(lrintf(b * max));
|
2018-05-04 22:30:21 +00:00
|
|
|
if (in != out && step == 4)
|
|
|
|
dst[j + aoffset] = src[j + aoffset];
|
|
|
|
}
|
|
|
|
|
|
|
|
srcrow += in->linesize[0];
|
|
|
|
dstrow += out->linesize[0];
|
|
|
|
}
|
2018-05-05 10:50:16 +00:00
|
|
|
|
|
|
|
return 0;
|
2018-05-04 22:30:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 23:22:13 +00:00
|
|
|
static int color_balance16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
2018-05-04 22:30:21 +00:00
|
|
|
{
|
|
|
|
ColorBalanceContext *s = ctx->priv;
|
2018-05-05 10:50:16 +00:00
|
|
|
ThreadData *td = arg;
|
|
|
|
AVFrame *in = td->in;
|
|
|
|
AVFrame *out = td->out;
|
2018-05-04 22:30:21 +00:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2018-05-05 10:50:16 +00:00
|
|
|
const int slice_start = (out->height * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
|
|
|
|
const uint16_t *srcrow = (const uint16_t *)in->data[0] + slice_start * in->linesize[0] / 2;
|
2018-05-04 22:30:21 +00:00
|
|
|
const uint8_t roffset = s->rgba_map[R];
|
|
|
|
const uint8_t goffset = s->rgba_map[G];
|
|
|
|
const uint8_t boffset = s->rgba_map[B];
|
|
|
|
const uint8_t aoffset = s->rgba_map[A];
|
|
|
|
const int step = s->step / 2;
|
2019-11-12 23:22:13 +00:00
|
|
|
const int depth = s->depth;
|
2019-11-13 11:56:04 +00:00
|
|
|
const float max = s->max;
|
2018-05-04 22:30:21 +00:00
|
|
|
uint16_t *dstrow;
|
|
|
|
int i, j;
|
|
|
|
|
2018-05-05 10:50:16 +00:00
|
|
|
dstrow = (uint16_t *)out->data[0] + slice_start * out->linesize[0] / 2;
|
|
|
|
for (i = slice_start; i < slice_end; i++) {
|
2018-05-04 22:30:21 +00:00
|
|
|
const uint16_t *src = srcrow;
|
|
|
|
uint16_t *dst = dstrow;
|
|
|
|
|
|
|
|
for (j = 0; j < outlink->w * step; j += step) {
|
2019-11-13 11:56:04 +00:00
|
|
|
float r = src[j + roffset] / max;
|
|
|
|
float g = src[j + goffset] / max;
|
|
|
|
float b = src[j + boffset] / max;
|
|
|
|
const float l = (FFMAX3(r, g, b) + FFMIN3(r, g, b));
|
2019-11-13 11:26:18 +00:00
|
|
|
|
|
|
|
r = get_component(r, l, s->cyan_red.shadows, s->cyan_red.midtones, s->cyan_red.highlights);
|
|
|
|
g = get_component(g, l, s->magenta_green.shadows, s->magenta_green.midtones, s->magenta_green.highlights);
|
|
|
|
b = get_component(b, l, s->yellow_blue.shadows, s->yellow_blue.midtones, s->yellow_blue.highlights);
|
|
|
|
|
|
|
|
if (s->preserve_lightness)
|
|
|
|
preservel(&r, &g, &b, l);
|
|
|
|
|
2020-07-01 12:57:58 +00:00
|
|
|
dst[j + roffset] = av_clip_uintp2_c(lrintf(r * max), depth);
|
|
|
|
dst[j + goffset] = av_clip_uintp2_c(lrintf(g * max), depth);
|
|
|
|
dst[j + boffset] = av_clip_uintp2_c(lrintf(b * max), depth);
|
2018-05-04 22:30:21 +00:00
|
|
|
if (in != out && step == 4)
|
|
|
|
dst[j + aoffset] = src[j + aoffset];
|
|
|
|
}
|
|
|
|
|
|
|
|
srcrow += in->linesize[0] / 2;
|
|
|
|
dstrow += out->linesize[0] / 2;
|
|
|
|
}
|
2018-05-05 10:50:16 +00:00
|
|
|
|
|
|
|
return 0;
|
2018-05-04 22:30:21 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 22:16:51 +00:00
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
2015-01-28 14:11:30 +00:00
|
|
|
ColorBalanceContext *s = ctx->priv;
|
2013-04-14 22:16:51 +00:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
|
2018-05-04 22:30:21 +00:00
|
|
|
const int depth = desc->comp[0].depth;
|
2019-11-13 11:26:18 +00:00
|
|
|
const int max = (1 << depth) - 1;
|
2018-05-05 10:22:52 +00:00
|
|
|
const int planar = av_pix_fmt_count_planes(outlink->format) > 1;
|
2019-11-12 23:22:13 +00:00
|
|
|
|
|
|
|
s->depth = depth;
|
2019-11-13 11:26:18 +00:00
|
|
|
s->max = max;
|
2013-04-14 22:16:51 +00:00
|
|
|
|
2019-11-13 11:26:18 +00:00
|
|
|
if (max == 255 && planar) {
|
2019-11-12 23:22:13 +00:00
|
|
|
s->color_balance = color_balance8_p;
|
2018-05-05 10:22:52 +00:00
|
|
|
} else if (planar) {
|
2019-11-12 23:22:13 +00:00
|
|
|
s->color_balance = color_balance16_p;
|
2019-11-13 11:26:18 +00:00
|
|
|
} else if (max == 255) {
|
2019-11-12 23:22:13 +00:00
|
|
|
s->color_balance = color_balance8;
|
2018-05-04 22:30:21 +00:00
|
|
|
} else {
|
2019-11-12 23:22:13 +00:00
|
|
|
s->color_balance = color_balance16;
|
2013-04-14 22:16:51 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 14:11:30 +00:00
|
|
|
ff_fill_rgba_map(s->rgba_map, outlink->format);
|
|
|
|
s->step = av_get_padded_bits_per_pixel(desc) >> 3;
|
2013-04-14 22:16:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2015-01-28 14:11:30 +00:00
|
|
|
ColorBalanceContext *s = ctx->priv;
|
2013-04-14 22:16:51 +00:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2018-05-05 10:50:16 +00:00
|
|
|
ThreadData td;
|
2013-04-14 22:16:51 +00:00
|
|
|
AVFrame *out;
|
|
|
|
|
|
|
|
if (av_frame_is_writable(in)) {
|
|
|
|
out = in;
|
|
|
|
} else {
|
|
|
|
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
av_frame_copy_props(out, in);
|
|
|
|
}
|
|
|
|
|
2018-05-05 10:50:16 +00:00
|
|
|
td.in = in;
|
|
|
|
td.out = out;
|
2021-08-15 19:33:25 +00:00
|
|
|
ff_filter_execute(ctx, s->color_balance, &td, NULL,
|
|
|
|
FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
|
2013-04-14 22:16:51 +00:00
|
|
|
|
|
|
|
if (in != out)
|
|
|
|
av_frame_free(&in);
|
2018-05-04 22:30:21 +00:00
|
|
|
return ff_filter_frame(outlink, out);
|
2013-04-14 22:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad colorbalance_inputs[] = {
|
|
|
|
{
|
2013-05-14 15:27:25 +00:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.filter_frame = filter_frame,
|
2013-04-14 22:16:51 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad colorbalance_outputs[] = {
|
2013-05-14 15:27:25 +00:00
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
2013-04-14 22:16:51 +00:00
|
|
|
};
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_vf_colorbalance = {
|
2013-04-14 22:16:51 +00:00
|
|
|
.name = "colorbalance",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Adjust the color balance."),
|
|
|
|
.priv_size = sizeof(ColorBalanceContext),
|
|
|
|
.priv_class = &colorbalance_class,
|
2021-08-12 11:05:31 +00:00
|
|
|
FILTER_INPUTS(colorbalance_inputs),
|
|
|
|
FILTER_OUTPUTS(colorbalance_outputs),
|
2021-09-27 10:07:35 +00:00
|
|
|
FILTER_QUERY_FUNC(query_formats),
|
2018-05-05 10:50:16 +00:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
|
2019-11-13 11:59:17 +00:00
|
|
|
.process_command = ff_filter_process_command,
|
2013-04-14 22:16:51 +00:00
|
|
|
};
|