1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avfilter/vf_colorchannelmixer: extend pl option functionality

This commit is contained in:
Paul B Mahol 2021-01-27 00:33:56 +01:00
parent 199d9e705a
commit 2ee2e4cd41
2 changed files with 54 additions and 34 deletions

View File

@ -8067,7 +8067,8 @@ Default is @code{1} for @var{aa}, and @code{0} for @var{ar}, @var{ag} and @var{a
Allowed ranges for options are @code{[-2.0, 2.0]}. Allowed ranges for options are @code{[-2.0, 2.0]}.
@item pl @item pl
Preserve lightness when changing colors. Default is disabled. Preserve lightness when changing colors. Allowed range is from @code{[0.0, 1.0]}.
Default is @code{0.0}, thus disabled.
@end table @end table
@subsection Examples @subsection Examples

View File

@ -44,7 +44,7 @@ typedef struct ColorChannelMixerContext {
double br, bg, bb, ba; double br, bg, bb, ba;
double ar, ag, ab, aa; double ar, ag, ab, aa;
double sr, sg, sb; double sr, sg, sb;
int preserve_lightness; double preserve_lightness;
int *lut[4][4]; int *lut[4][4];
@ -75,7 +75,7 @@ static const AVOption colorchannelmixer_options[] = {
{ "ag", "set the green gain for the alpha channel", OFFSET(ag), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS }, { "ag", "set the green gain for the alpha channel", OFFSET(ag), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
{ "ab", "set the blue gain for the alpha channel", OFFSET(ab), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS }, { "ab", "set the blue gain for the alpha channel", OFFSET(ab), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
{ "aa", "set the alpha gain for the alpha channel", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS }, { "aa", "set the alpha gain for the alpha channel", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
{ "pl", "preserve lightness", OFFSET(preserve_lightness), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS }, { "pl", "preserve lightness", OFFSET(preserve_lightness), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
{ NULL } { NULL }
}; };
@ -106,6 +106,11 @@ static int query_formats(AVFilterContext *ctx)
return ff_set_common_formats(ctx, fmts_list); return ff_set_common_formats(ctx, fmts_list);
} }
static float lerpf(float v0, float v1, float f)
{
return v0 + (v1 - v0) * f;
}
static void preservel(float *r, float *g, float *b, float lin, float lout) static void preservel(float *r, float *g, float *b, float lin, float lout)
{ {
*r *= lout / lin; *r *= lout / lin;
@ -120,6 +125,10 @@ static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void
ThreadData *td = arg; ThreadData *td = arg;
AVFrame *in = td->in; AVFrame *in = td->in;
AVFrame *out = td->out; AVFrame *out = td->out;
const float l = s->preserve_lightness;
const float sr = s->sr;
const float sg = s->sg;
const float sb = s->sb;
const int slice_start = (out->height * jobnr) / nb_jobs; const int slice_start = (out->height * jobnr) / nb_jobs;
const int slice_end = (out->height * (jobnr+1)) / 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 *srcg = in->data[0] + slice_start * in->linesize[0];
@ -142,7 +151,7 @@ static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void
float lin; float lin;
if (pl) if (pl)
lin = FFMAX3(rin, gin, bin) / 255.f + FFMIN3(rin, gin, bin) / 255.f; lin = FFMAX3(rin, gin, bin) + FFMIN3(rin, gin, bin);
rout = s->lut[R][R][rin] + rout = s->lut[R][R][rin] +
s->lut[R][G][gin] + s->lut[R][G][gin] +
@ -158,16 +167,16 @@ static av_always_inline int filter_slice_rgba_planar(AVFilterContext *ctx, void
(have_alpha == 1 ? s->lut[B][A][ain] : 0); (have_alpha == 1 ? s->lut[B][A][ain] : 0);
if (pl) { if (pl) {
float frout = rout / (255.f * s->sr); float frout = rout / sr;
float fgout = gout / (255.f * s->sg); float fgout = gout / sg;
float fbout = bout / (255.f * s->sb); float fbout = bout / sb;
float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout); float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
preservel(&frout, &fgout, &fbout, lin, lout); preservel(&frout, &fgout, &fbout, lin, lout);
rout = lrintf(frout * 255.f); rout = lrintf(lerpf(rout, frout, l));
gout = lrintf(fgout * 255.f); gout = lrintf(lerpf(gout, fgout, l));
bout = lrintf(fbout * 255.f); bout = lrintf(lerpf(bout, fbout, l));
} }
dstr[j] = av_clip_uint8(rout); dstr[j] = av_clip_uint8(rout);
@ -202,6 +211,10 @@ static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, voi
ThreadData *td = arg; ThreadData *td = arg;
AVFrame *in = td->in; AVFrame *in = td->in;
AVFrame *out = td->out; AVFrame *out = td->out;
const float l = s->preserve_lightness;
const float sr = s->sr;
const float sg = s->sg;
const float sb = s->sb;
const int slice_start = (out->height * jobnr) / nb_jobs; const int slice_start = (out->height * jobnr) / nb_jobs;
const int slice_end = (out->height * (jobnr+1)) / 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]); const uint16_t *srcg = (const uint16_t *)(in->data[0] + slice_start * in->linesize[0]);
@ -212,8 +225,6 @@ static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, voi
uint16_t *dstb = (uint16_t *)(out->data[1] + slice_start * out->linesize[1]); uint16_t *dstb = (uint16_t *)(out->data[1] + slice_start * out->linesize[1]);
uint16_t *dstr = (uint16_t *)(out->data[2] + slice_start * out->linesize[2]); uint16_t *dstr = (uint16_t *)(out->data[2] + slice_start * out->linesize[2]);
uint16_t *dsta = (uint16_t *)(out->data[3] + slice_start * out->linesize[3]); uint16_t *dsta = (uint16_t *)(out->data[3] + slice_start * out->linesize[3]);
const float scale = 1.f / ((1 << depth) - 1);
const float factor = (1 << depth) - 1;
int i, j; int i, j;
for (i = slice_start; i < slice_end; i++) { for (i = slice_start; i < slice_end; i++) {
@ -226,7 +237,7 @@ static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, voi
float lin; float lin;
if (pl) if (pl)
lin = FFMAX3(rin, gin, bin) * scale + FFMIN3(rin, gin, bin) * scale; lin = FFMAX3(rin, gin, bin) + FFMIN3(rin, gin, bin);
rout = s->lut[R][R][rin] + rout = s->lut[R][R][rin] +
s->lut[R][G][gin] + s->lut[R][G][gin] +
@ -242,16 +253,16 @@ static av_always_inline int filter_slice_rgba16_planar(AVFilterContext *ctx, voi
(have_alpha == 1 ? s->lut[B][A][ain] : 0); (have_alpha == 1 ? s->lut[B][A][ain] : 0);
if (pl) { if (pl) {
float frout = rout / (factor * s->sr); float frout = rout / sr;
float fgout = gout / (factor * s->sg); float fgout = gout / sg;
float fbout = bout / (factor * s->sb); float fbout = bout / sb;
float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout); float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
preservel(&frout, &fgout, &fbout, lin, lout); preservel(&frout, &fgout, &fbout, lin, lout);
rout = lrintf(frout * factor); rout = lrintf(lerpf(rout, frout, l));
gout = lrintf(fgout * factor); gout = lrintf(lerpf(gout, fgout, l));
bout = lrintf(fbout * factor); bout = lrintf(lerpf(bout, fbout, l));
} }
dstr[j] = av_clip_uintp2(rout, depth); dstr[j] = av_clip_uintp2(rout, depth);
@ -386,6 +397,10 @@ static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void
ThreadData *td = arg; ThreadData *td = arg;
AVFrame *in = td->in; AVFrame *in = td->in;
AVFrame *out = td->out; AVFrame *out = td->out;
const float l = s->preserve_lightness;
const float sr = s->sr;
const float sg = s->sg;
const float sb = s->sb;
const int slice_start = (out->height * jobnr) / nb_jobs; const int slice_start = (out->height * jobnr) / nb_jobs;
const int slice_end = (out->height * (jobnr+1)) / nb_jobs; const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
const uint8_t roffset = s->rgba_map[R]; const uint8_t roffset = s->rgba_map[R];
@ -409,7 +424,7 @@ static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void
float lin; float lin;
if (pl) if (pl)
lin = FFMAX3(rin, gin, bin) / 255.f + FFMIN3(rin, gin, bin) / 255.f; lin = FFMAX3(rin, gin, bin) + FFMIN3(rin, gin, bin);
rout = s->lut[R][R][rin] + rout = s->lut[R][R][rin] +
s->lut[R][G][gin] + s->lut[R][G][gin] +
@ -425,16 +440,16 @@ static av_always_inline int filter_slice_rgba_packed(AVFilterContext *ctx, void
(have_alpha == 1 ? s->lut[B][A][ain] : 0); (have_alpha == 1 ? s->lut[B][A][ain] : 0);
if (pl) { if (pl) {
float frout = rout / (255.f * s->sr); float frout = rout / sr;
float fgout = gout / (255.f * s->sg); float fgout = gout / sg;
float fbout = bout / (255.f * s->sb); float fbout = bout / sb;
float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout); float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
preservel(&frout, &fgout, &fbout, lin, lout); preservel(&frout, &fgout, &fbout, lin, lout);
rout = lrintf(frout * 255.f); rout = lrintf(lerpf(rout, frout, l));
gout = lrintf(fgout * 255.f); gout = lrintf(lerpf(gout, fgout, l));
bout = lrintf(fbout * 255.f); bout = lrintf(lerpf(bout, fbout, l));
} }
dst[j + roffset] = av_clip_uint8(rout); dst[j + roffset] = av_clip_uint8(rout);
@ -464,6 +479,10 @@ static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, voi
ThreadData *td = arg; ThreadData *td = arg;
AVFrame *in = td->in; AVFrame *in = td->in;
AVFrame *out = td->out; AVFrame *out = td->out;
const float l = s->preserve_lightness;
const float sr = s->sr;
const float sg = s->sg;
const float sb = s->sb;
const int slice_start = (out->height * jobnr) / nb_jobs; const int slice_start = (out->height * jobnr) / nb_jobs;
const int slice_end = (out->height * (jobnr+1)) / nb_jobs; const int slice_end = (out->height * (jobnr+1)) / nb_jobs;
const uint8_t roffset = s->rgba_map[R]; const uint8_t roffset = s->rgba_map[R];
@ -487,7 +506,7 @@ static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, voi
float lin; float lin;
if (pl) if (pl)
lin = FFMAX3(rin, gin, bin) / 65535.f + FFMIN3(rin, gin, bin) / 65535.f; lin = FFMAX3(rin, gin, bin) + FFMIN3(rin, gin, bin);
rout = s->lut[R][R][rin] + rout = s->lut[R][R][rin] +
s->lut[R][G][gin] + s->lut[R][G][gin] +
@ -503,16 +522,16 @@ static av_always_inline int filter_slice_rgba16_packed(AVFilterContext *ctx, voi
(have_alpha == 1 ? s->lut[B][A][ain] : 0); (have_alpha == 1 ? s->lut[B][A][ain] : 0);
if (pl) { if (pl) {
float frout = rout / (65535.f * s->sr); float frout = rout / sr;
float fgout = gout / (65535.f * s->sg); float fgout = gout / sg;
float fbout = bout / (65535.f * s->sb); float fbout = bout / sb;
float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout); float lout = FFMAX3(frout, fgout, fbout) + FFMIN3(frout, fgout, fbout);
preservel(&frout, &fgout, &fbout, lin, lout); preservel(&frout, &fgout, &fbout, lin, lout);
rout = lrintf(frout * 65535.f); rout = lrintf(lerpf(rout, frout, l));
gout = lrintf(fgout * 65535.f); gout = lrintf(lerpf(gout, fgout, l));
bout = lrintf(fbout * 65535.f); bout = lrintf(lerpf(bout, fbout, l));
} }
dst[j + roffset] = av_clip_uint16(rout); dst[j + roffset] = av_clip_uint16(rout);
@ -720,7 +739,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
AVFilterContext *ctx = inlink->dst; AVFilterContext *ctx = inlink->dst;
ColorChannelMixerContext *s = ctx->priv; ColorChannelMixerContext *s = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0]; AVFilterLink *outlink = ctx->outputs[0];
const int pl = s->preserve_lightness; const int pl = s->preserve_lightness > 0.;
ThreadData td; ThreadData td;
AVFrame *out; AVFrame *out;