X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fvf_normalize.c;h=3a2127ade32c007ac9c51959c1ba48dc3805b461;hb=a04ad248a05e7b613abe09b3bb067f555108d794;hp=5c1fe98c60c235a17bd7f6de23c7639c486dbe43;hpb=5c363d3e595a9e5b7c42897b7aab91b91b154ac1;p=ffmpeg diff --git a/libavfilter/vf_normalize.c b/libavfilter/vf_normalize.c index 5c1fe98c60c..3a2127ade32 100644 --- a/libavfilter/vf_normalize.c +++ b/libavfilter/vf_normalize.c @@ -73,13 +73,26 @@ */ #include "libavutil/imgutils.h" +#include "libavutil/intreadwrite.h" #include "libavutil/opt.h" #include "libavutil/pixdesc.h" #include "avfilter.h" +#include "drawutils.h" #include "formats.h" #include "internal.h" #include "video.h" +typedef struct NormalizeHistory { + uint16_t *history; // History entries. + uint64_t history_sum; // Sum of history entries. +} NormalizeHistory; + +typedef struct NormalizeLocal { + uint16_t in; // Original input byte value for this frame. + float smoothed; // Smoothed input value [0,255]. + float out; // Output value [0,255] +} NormalizeLocal; + typedef struct NormalizeContext { const AVClass *class; @@ -90,67 +103,203 @@ typedef struct NormalizeContext { float independence; float strength; - int co[4]; // Offsets to R,G,B,A bytes respectively in each pixel + uint8_t co[4]; // Offsets to R,G,B,A bytes respectively in each pixel + int depth; + int sblackpt[4]; + int swhitept[4]; int num_components; // Number of components in the pixel format + int step; int history_len; // Number of frames to average; based on smoothing factor int frame_num; // Increments on each frame, starting from 0. // Per-extremum, per-channel history, for temporal smoothing. - struct { - uint8_t *history; // History entries. - uint32_t history_sum; // Sum of history entries. - } min[3], max[3]; // Min and max for each channel in {R,G,B}. - uint8_t *history_mem; // Single allocation for above history entries + NormalizeHistory min[3], max[3]; // Min and max for each channel in {R,G,B}. + uint16_t *history_mem; // Single allocation for above history entries + + uint16_t lut[3][65536]; // Lookup table + void (*find_min_max)(struct NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]); + void (*process)(struct NormalizeContext *s, AVFrame *in, AVFrame *out); } NormalizeContext; #define OFFSET(x) offsetof(NormalizeContext, x) #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM +#define FLAGSR AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM static const AVOption normalize_options[] = { - { "blackpt", "output color to which darkest input color is mapped", OFFSET(blackpt), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS }, - { "whitept", "output color to which brightest input color is mapped", OFFSET(whitept), AV_OPT_TYPE_COLOR, { .str = "white" }, CHAR_MIN, CHAR_MAX, FLAGS }, + { "blackpt", "output color to which darkest input color is mapped", OFFSET(blackpt), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGSR }, + { "whitept", "output color to which brightest input color is mapped", OFFSET(whitept), AV_OPT_TYPE_COLOR, { .str = "white" }, 0, 0, FLAGSR }, { "smoothing", "amount of temporal smoothing of the input range, to reduce flicker", OFFSET(smoothing), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX/8, FLAGS }, - { "independence", "proportion of independent to linked channel normalization", OFFSET(independence), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 1.0, FLAGS }, - { "strength", "strength of filter, from no effect to full normalization", OFFSET(strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 1.0, FLAGS }, + { "independence", "proportion of independent to linked channel normalization", OFFSET(independence), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 1.0, FLAGSR }, + { "strength", "strength of filter, from no effect to full normalization", OFFSET(strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 1.0, FLAGSR }, { NULL } }; AVFILTER_DEFINE_CLASS(normalize); +static void find_min_max(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]) +{ + for (int c = 0; c < 3; c++) + min[c].in = max[c].in = in->data[0][s->co[c]]; + for (int y = 0; y < in->height; y++) { + uint8_t *inp = in->data[0] + y * in->linesize[0]; + for (int x = 0; x < in->width; x++) { + for (int c = 0; c < 3; c++) { + min[c].in = FFMIN(min[c].in, inp[s->co[c]]); + max[c].in = FFMAX(max[c].in, inp[s->co[c]]); + } + inp += s->step; + } + } +} + +static void process(NormalizeContext *s, AVFrame *in, AVFrame *out) +{ + for (int y = 0; y < in->height; y++) { + uint8_t *inp = in->data[0] + y * in->linesize[0]; + uint8_t *outp = out->data[0] + y * out->linesize[0]; + for (int x = 0; x < in->width; x++) { + for (int c = 0; c < 3; c++) + outp[s->co[c]] = s->lut[c][inp[s->co[c]]]; + if (s->num_components == 4) + // Copy alpha as-is. + outp[s->co[3]] = inp[s->co[3]]; + inp += s->step; + outp += s->step; + } + } +} + +static void find_min_max_planar(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]) +{ + min[0].in = max[0].in = in->data[2][0]; + min[1].in = max[1].in = in->data[0][0]; + min[2].in = max[2].in = in->data[1][0]; + for (int y = 0; y < in->height; y++) { + uint8_t *inrp = in->data[2] + y * in->linesize[2]; + uint8_t *ingp = in->data[0] + y * in->linesize[0]; + uint8_t *inbp = in->data[1] + y * in->linesize[1]; + for (int x = 0; x < in->width; x++) { + min[0].in = FFMIN(min[0].in, inrp[x]); + max[0].in = FFMAX(max[0].in, inrp[x]); + min[1].in = FFMIN(min[1].in, ingp[x]); + max[1].in = FFMAX(max[1].in, ingp[x]); + min[2].in = FFMIN(min[2].in, inbp[x]); + max[2].in = FFMAX(max[2].in, inbp[x]); + } + } +} + +static void process_planar(NormalizeContext *s, AVFrame *in, AVFrame *out) +{ + for (int y = 0; y < in->height; y++) { + uint8_t *inrp = in->data[2] + y * in->linesize[2]; + uint8_t *ingp = in->data[0] + y * in->linesize[0]; + uint8_t *inbp = in->data[1] + y * in->linesize[1]; + uint8_t *inap = in->data[3] + y * in->linesize[3]; + uint8_t *outrp = out->data[2] + y * out->linesize[2]; + uint8_t *outgp = out->data[0] + y * out->linesize[0]; + uint8_t *outbp = out->data[1] + y * out->linesize[1]; + uint8_t *outap = out->data[3] + y * out->linesize[3]; + for (int x = 0; x < in->width; x++) { + outrp[x] = s->lut[0][inrp[x]]; + outgp[x] = s->lut[1][ingp[x]]; + outbp[x] = s->lut[2][inbp[x]]; + if (s->num_components == 4) + outap[x] = inap[x]; + } + } +} + +static void find_min_max_16(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]) +{ + for (int c = 0; c < 3; c++) + min[c].in = max[c].in = AV_RN16(in->data[0] + 2 * s->co[c]); + for (int y = 0; y < in->height; y++) { + uint16_t *inp = (uint16_t *)(in->data[0] + y * in->linesize[0]); + for (int x = 0; x < in->width; x++) { + for (int c = 0; c < 3; c++) { + min[c].in = FFMIN(min[c].in, inp[s->co[c]]); + max[c].in = FFMAX(max[c].in, inp[s->co[c]]); + } + inp += s->step; + } + } +} + +static void process_16(NormalizeContext *s, AVFrame *in, AVFrame *out) +{ + for (int y = 0; y < in->height; y++) { + uint16_t *inp = (uint16_t *)(in->data[0] + y * in->linesize[0]); + uint16_t *outp = (uint16_t *)(out->data[0] + y * out->linesize[0]); + for (int x = 0; x < in->width; x++) { + for (int c = 0; c < 3; c++) + outp[s->co[c]] = s->lut[c][inp[s->co[c]]]; + if (s->num_components == 4) + // Copy alpha as-is. + outp[s->co[3]] = inp[s->co[3]]; + inp += s->step; + outp += s->step; + } + } +} + +static void find_min_max_planar_16(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]) +{ + min[0].in = max[0].in = AV_RN16(in->data[2]); + min[1].in = max[1].in = AV_RN16(in->data[0]); + min[2].in = max[2].in = AV_RN16(in->data[1]); + for (int y = 0; y < in->height; y++) { + uint16_t *inrp = (uint16_t *)(in->data[2] + y * in->linesize[2]); + uint16_t *ingp = (uint16_t *)(in->data[0] + y * in->linesize[0]); + uint16_t *inbp = (uint16_t *)(in->data[1] + y * in->linesize[1]); + for (int x = 0; x < in->width; x++) { + min[0].in = FFMIN(min[0].in, inrp[x]); + max[0].in = FFMAX(max[0].in, inrp[x]); + min[1].in = FFMIN(min[1].in, ingp[x]); + max[1].in = FFMAX(max[1].in, ingp[x]); + min[2].in = FFMIN(min[2].in, inbp[x]); + max[2].in = FFMAX(max[2].in, inbp[x]); + } + } +} + +static void process_planar_16(NormalizeContext *s, AVFrame *in, AVFrame *out) +{ + for (int y = 0; y < in->height; y++) { + uint16_t *inrp = (uint16_t *)(in->data[2] + y * in->linesize[2]); + uint16_t *ingp = (uint16_t *)(in->data[0] + y * in->linesize[0]); + uint16_t *inbp = (uint16_t *)(in->data[1] + y * in->linesize[1]); + uint16_t *inap = (uint16_t *)(in->data[3] + y * in->linesize[3]); + uint16_t *outrp = (uint16_t *)(out->data[2] + y * out->linesize[2]); + uint16_t *outgp = (uint16_t *)(out->data[0] + y * out->linesize[0]); + uint16_t *outbp = (uint16_t *)(out->data[1] + y * out->linesize[1]); + uint16_t *outap = (uint16_t *)(out->data[3] + y * out->linesize[3]); + for (int x = 0; x < in->width; x++) { + outrp[x] = s->lut[0][inrp[x]]; + outgp[x] = s->lut[1][ingp[x]]; + outbp[x] = s->lut[2][inbp[x]]; + if (s->num_components == 4) + outap[x] = inap[x]; + } + } +} + // This function is the main guts of the filter. Normalizes the input frame // into the output frame. The frames are known to have the same dimensions // and pixel format. static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) { // Per-extremum, per-channel local variables. - struct { - uint8_t in; // Original input byte value for this frame. - float smoothed; // Smoothed input value [0,255]. - float out; // Output value [0,255]. - } min[3], max[3]; // Min and max for each channel in {R,G,B}. + NormalizeLocal min[3], max[3]; // Min and max for each channel in {R,G,B}. float rgb_min_smoothed; // Min input range for linked normalization float rgb_max_smoothed; // Max input range for linked normalization - uint8_t lut[3][256]; // Lookup table - int x, y, c; + int c; // First, scan the input frame to find, for each channel, the minimum // (min.in) and maximum (max.in) values present in the channel. - for (c = 0; c < 3; c++) - min[c].in = max[c].in = in->data[0][s->co[c]]; - for (y = 0; y < in->height; y++) { - uint8_t *inp = in->data[0] + y * in->linesize[0]; - uint8_t *outp = out->data[0] + y * out->linesize[0]; - for (x = 0; x < in->width; x++) { - for (c = 0; c < 3; c++) { - min[c].in = FFMIN(min[c].in, inp[s->co[c]]); - max[c].in = FFMAX(max[c].in, inp[s->co[c]]); - } - inp += s->num_components; - outp += s->num_components; - } - } + s->find_min_max(s, in, min, max); // Next, for each channel, push min.in and max.in into their respective // histories, to determine the min.smoothed and max.smoothed for this frame. @@ -198,9 +347,9 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) // Calculate the output range [min.out,max.out] as a ratio of the full- // strength output range [blackpt,whitept] and the original input range // [min.in,max.in], based on the user-specified filter strength. - min[c].out = (s->blackpt[c] * s->strength) + min[c].out = (s->sblackpt[c] * s->strength) + (min[c].in * (1.0f - s->strength)); - max[c].out = (s->whitept[c] * s->strength) + max[c].out = (s->swhitept[c] * s->strength) + (max[c].in * (1.0f - s->strength)); // Now, build a lookup table which linearly maps the adjusted input range @@ -211,7 +360,7 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) if (min[c].smoothed == max[c].smoothed) { // There is no dynamic range to expand. No mapping for this channel. for (in_val = min[c].in; in_val <= max[c].in; in_val++) - lut[c][in_val] = min[c].out; + s->lut[c][in_val] = min[c].out; } else { // We must set lookup values for all values in the original input // range [min.in,max.in]. Since the original input range may be @@ -220,27 +369,14 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) float scale = (max[c].out - min[c].out) / (max[c].smoothed - min[c].smoothed); for (in_val = min[c].in; in_val <= max[c].in; in_val++) { int out_val = (in_val - min[c].smoothed) * scale + min[c].out + 0.5f; - out_val = FFMAX(out_val, 0); - out_val = FFMIN(out_val, 255); - lut[c][in_val] = out_val; + out_val = av_clip_uintp2_c(out_val, s->depth); + s->lut[c][in_val] = out_val; } } } // Finally, process the pixels of the input frame using the lookup tables. - for (y = 0; y < in->height; y++) { - uint8_t *inp = in->data[0] + y * in->linesize[0]; - uint8_t *outp = out->data[0] + y * out->linesize[0]; - for (x = 0; x < in->width; x++) { - for (c = 0; c < 3; c++) - outp[s->co[c]] = lut[c][inp[s->co[c]]]; - if (s->num_components == 4) - // Copy alpha as-is. - outp[s->co[3]] = inp[s->co[3]]; - inp += s->num_components; - outp += s->num_components; - } - } + s->process(s, in, out); s->frame_num++; } @@ -265,6 +401,11 @@ static int query_formats(AVFilterContext *ctx) AV_PIX_FMT_RGB0, AV_PIX_FMT_0BGR, AV_PIX_FMT_BGR0, + AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48, + AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64, + AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, + AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, + AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_NONE }; // According to filter_design.txt, using ff_set_common_formats() this way @@ -284,11 +425,13 @@ static int config_input(AVFilterLink *inlink) NormalizeContext *s = inlink->dst->priv; // Store offsets to R,G,B,A bytes respectively in each pixel const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); - int c; + int c, planar, scale; - for (c = 0; c < 4; ++c) - s->co[c] = desc->comp[c].offset; + ff_fill_rgba_map(s->co, inlink->format); + s->depth = desc->comp[0].depth; + scale = 1 << (s->depth - 8); s->num_components = desc->nb_components; + s->step = av_get_padded_bits_per_pixel(desc) >> (3 + (s->depth > 8)); // Convert smoothing value to history_len (a count of frames to average, // must be at least 1). Currently this is a direct assignment, but the // smoothing value was originally envisaged as a number of seconds. In @@ -298,14 +441,27 @@ static int config_input(AVFilterLink *inlink) // Allocate the history buffers -- there are 6 -- one for each extrema. // s->smoothing is limited to INT_MAX/8, so that (s->history_len * 6) // can't overflow on 32bit causing a too-small allocation. - s->history_mem = av_malloc(s->history_len * 6); + s->history_mem = av_malloc(s->history_len * 6 * sizeof(*s->history_mem)); if (s->history_mem == NULL) return AVERROR(ENOMEM); for (c = 0; c < 3; c++) { s->min[c].history = s->history_mem + (c*2) * s->history_len; s->max[c].history = s->history_mem + (c*2+1) * s->history_len; + s->sblackpt[c] = scale * s->blackpt[c] + (s->blackpt[c] & (1 << (s->depth - 8))); + s->swhitept[c] = scale * s->whitept[c] + (s->whitept[c] & (1 << (s->depth - 8))); } + + planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR; + + if (s->depth <= 8) { + s->find_min_max = planar ? find_min_max_planar : find_min_max; + s->process = planar? process_planar : process; + } else { + s->find_min_max = planar ? find_min_max_planar_16 : find_min_max_16; + s->process = planar? process_planar_16 : process_16; + } + return 0; } @@ -374,7 +530,7 @@ static const AVFilterPad outputs[] = { { NULL } }; -AVFilter ff_vf_normalize = { +const AVFilter ff_vf_normalize = { .name = "normalize", .description = NULL_IF_CONFIG_SMALL("Normalize RGB video."), .priv_size = sizeof(NormalizeContext), @@ -383,4 +539,6 @@ AVFilter ff_vf_normalize = { .query_formats = query_formats, .inputs = inputs, .outputs = outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, + .process_command = ff_filter_process_command, };