yading@10: /* yading@10: * Copyright (c) 2012 Jeremy Tran yading@10: * Copyright (c) 2001 Donald A. Graft yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or modify yading@10: * it under the terms of the GNU General Public License as published by yading@10: * the Free Software Foundation; either version 2 of the License, or yading@10: * (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the yading@10: * GNU General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU General Public License along yading@10: * with FFmpeg; if not, write to the Free Software Foundation, Inc., yading@10: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * Histogram equalization filter, based on the VirtualDub filter by yading@10: * Donald A. Graft . yading@10: * Implements global automatic contrast adjustment by means of yading@10: * histogram equalization. yading@10: */ yading@10: yading@10: #include "libavutil/common.h" yading@10: #include "libavutil/opt.h" yading@10: #include "libavutil/pixdesc.h" yading@10: yading@10: #include "avfilter.h" yading@10: #include "drawutils.h" yading@10: #include "formats.h" yading@10: #include "internal.h" yading@10: #include "video.h" yading@10: yading@10: // #define DEBUG yading@10: yading@10: // Linear Congruential Generator, see "Numerical Recipes" yading@10: #define LCG_A 4096 yading@10: #define LCG_C 150889 yading@10: #define LCG_M 714025 yading@10: #define LCG(x) (((x) * LCG_A + LCG_C) % LCG_M) yading@10: #define LCG_SEED 739187 yading@10: yading@10: enum HisteqAntibanding { yading@10: HISTEQ_ANTIBANDING_NONE = 0, yading@10: HISTEQ_ANTIBANDING_WEAK = 1, yading@10: HISTEQ_ANTIBANDING_STRONG = 2, yading@10: HISTEQ_ANTIBANDING_NB, yading@10: }; yading@10: yading@10: typedef struct { yading@10: const AVClass *class; yading@10: float strength; yading@10: float intensity; yading@10: enum HisteqAntibanding antibanding; yading@10: char* antibanding_str; yading@10: int in_histogram [256]; ///< input histogram yading@10: int out_histogram[256]; ///< output histogram yading@10: int LUT[256]; ///< lookup table derived from histogram[] yading@10: uint8_t rgba_map[4]; ///< components position yading@10: int bpp; ///< bytes per pixel yading@10: } HisteqContext; yading@10: yading@10: #define OFFSET(x) offsetof(HisteqContext, x) yading@10: #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM yading@10: #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit } yading@10: yading@10: static const AVOption histeq_options[] = { yading@10: { "strength", "set the strength", OFFSET(strength), AV_OPT_TYPE_FLOAT, {.dbl=0.2}, 0, 1, FLAGS }, yading@10: { "intensity", "set the intensity", OFFSET(intensity), AV_OPT_TYPE_FLOAT, {.dbl=0.21}, 0, 1, FLAGS }, yading@10: { "antibanding", "set the antibanding level", OFFSET(antibanding), AV_OPT_TYPE_INT, {.i64=HISTEQ_ANTIBANDING_NONE}, 0, HISTEQ_ANTIBANDING_NB-1, FLAGS, "antibanding" }, yading@10: CONST("none", "apply no antibanding", HISTEQ_ANTIBANDING_NONE, "antibanding"), yading@10: CONST("weak", "apply weak antibanding", HISTEQ_ANTIBANDING_WEAK, "antibanding"), yading@10: CONST("strong", "apply strong antibanding", HISTEQ_ANTIBANDING_STRONG, "antibanding"), yading@10: { NULL } yading@10: }; yading@10: yading@10: AVFILTER_DEFINE_CLASS(histeq); yading@10: yading@10: static av_cold int init(AVFilterContext *ctx) yading@10: { yading@10: HisteqContext *histeq = ctx->priv; yading@10: yading@10: av_log(ctx, AV_LOG_VERBOSE, yading@10: "strength:%0.3f intensity:%0.3f antibanding:%d\n", yading@10: histeq->strength, histeq->intensity, histeq->antibanding); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int query_formats(AVFilterContext *ctx) yading@10: { yading@10: static const enum PixelFormat pix_fmts[] = { yading@10: AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, yading@10: AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, yading@10: AV_PIX_FMT_NONE yading@10: }; yading@10: yading@10: ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); yading@10: return 0; yading@10: } yading@10: yading@10: static int config_input(AVFilterLink *inlink) yading@10: { yading@10: AVFilterContext *ctx = inlink->dst; yading@10: HisteqContext *histeq = ctx->priv; yading@10: const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format); yading@10: yading@10: histeq->bpp = av_get_bits_per_pixel(pix_desc) / 8; yading@10: ff_fill_rgba_map(histeq->rgba_map, inlink->format); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: #define R 0 yading@10: #define G 1 yading@10: #define B 2 yading@10: #define A 3 yading@10: yading@10: #define GET_RGB_VALUES(r, g, b, src, map) do { \ yading@10: r = src[x + map[R]]; \ yading@10: g = src[x + map[G]]; \ yading@10: b = src[x + map[B]]; \ yading@10: } while (0) yading@10: yading@10: static int filter_frame(AVFilterLink *inlink, AVFrame *inpic) yading@10: { yading@10: AVFilterContext *ctx = inlink->dst; yading@10: HisteqContext *histeq = ctx->priv; yading@10: AVFilterLink *outlink = ctx->outputs[0]; yading@10: int strength = histeq->strength * 1000; yading@10: int intensity = histeq->intensity * 1000; yading@10: int x, y, i, luthi, lutlo, lut, luma, oluma, m; yading@10: AVFrame *outpic; yading@10: unsigned int r, g, b, jran; yading@10: uint8_t *src, *dst; yading@10: yading@10: outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h); yading@10: if (!outpic) { yading@10: av_frame_free(&inpic); yading@10: return AVERROR(ENOMEM); yading@10: } yading@10: av_frame_copy_props(outpic, inpic); yading@10: yading@10: /* Seed random generator for antibanding. */ yading@10: jran = LCG_SEED; yading@10: yading@10: /* Calculate and store the luminance and calculate the global histogram yading@10: based on the luminance. */ yading@10: memset(histeq->in_histogram, 0, sizeof(histeq->in_histogram)); yading@10: src = inpic->data[0]; yading@10: dst = outpic->data[0]; yading@10: for (y = 0; y < inlink->h; y++) { yading@10: for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) { yading@10: GET_RGB_VALUES(r, g, b, src, histeq->rgba_map); yading@10: luma = (55 * r + 182 * g + 19 * b) >> 8; yading@10: dst[x + histeq->rgba_map[A]] = luma; yading@10: histeq->in_histogram[luma]++; yading@10: } yading@10: src += inpic->linesize[0]; yading@10: dst += outpic->linesize[0]; yading@10: } yading@10: yading@10: #ifdef DEBUG yading@10: for (x = 0; x < 256; x++) yading@10: av_dlog(ctx, "in[%d]: %u\n", x, histeq->in_histogram[x]); yading@10: #endif yading@10: yading@10: /* Calculate the lookup table. */ yading@10: histeq->LUT[0] = histeq->in_histogram[0]; yading@10: /* Accumulate */ yading@10: for (x = 1; x < 256; x++) yading@10: histeq->LUT[x] = histeq->LUT[x-1] + histeq->in_histogram[x]; yading@10: yading@10: /* Normalize */ yading@10: for (x = 0; x < 256; x++) yading@10: histeq->LUT[x] = (histeq->LUT[x] * intensity) / (inlink->h * inlink->w); yading@10: yading@10: /* Adjust the LUT based on the selected strength. This is an alpha yading@10: mix of the calculated LUT and a linear LUT with gain 1. */ yading@10: for (x = 0; x < 256; x++) yading@10: histeq->LUT[x] = (strength * histeq->LUT[x]) / 255 + yading@10: ((255 - strength) * x) / 255; yading@10: yading@10: /* Output the equalized frame. */ yading@10: memset(histeq->out_histogram, 0, sizeof(histeq->out_histogram)); yading@10: yading@10: src = inpic->data[0]; yading@10: dst = outpic->data[0]; yading@10: for (y = 0; y < inlink->h; y++) { yading@10: for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) { yading@10: luma = dst[x + histeq->rgba_map[A]]; yading@10: if (luma == 0) { yading@10: for (i = 0; i < histeq->bpp; ++i) yading@10: dst[x + i] = 0; yading@10: histeq->out_histogram[0]++; yading@10: } else { yading@10: lut = histeq->LUT[luma]; yading@10: if (histeq->antibanding != HISTEQ_ANTIBANDING_NONE) { yading@10: if (luma > 0) { yading@10: lutlo = histeq->antibanding == HISTEQ_ANTIBANDING_WEAK ? yading@10: (histeq->LUT[luma] + histeq->LUT[luma - 1]) / 2 : yading@10: histeq->LUT[luma - 1]; yading@10: } else yading@10: lutlo = lut; yading@10: yading@10: if (luma < 255) { yading@10: luthi = (histeq->antibanding == HISTEQ_ANTIBANDING_WEAK) ? yading@10: (histeq->LUT[luma] + histeq->LUT[luma + 1]) / 2 : yading@10: histeq->LUT[luma + 1]; yading@10: } else yading@10: luthi = lut; yading@10: yading@10: if (lutlo != luthi) { yading@10: jran = LCG(jran); yading@10: lut = lutlo + ((luthi - lutlo + 1) * jran) / LCG_M; yading@10: } yading@10: } yading@10: yading@10: GET_RGB_VALUES(r, g, b, src, histeq->rgba_map); yading@10: if (((m = FFMAX3(r, g, b)) * lut) / luma > 255) { yading@10: r = (r * 255) / m; yading@10: g = (g * 255) / m; yading@10: b = (b * 255) / m; yading@10: } else { yading@10: r = (r * lut) / luma; yading@10: g = (g * lut) / luma; yading@10: b = (b * lut) / luma; yading@10: } yading@10: dst[x + histeq->rgba_map[R]] = r; yading@10: dst[x + histeq->rgba_map[G]] = g; yading@10: dst[x + histeq->rgba_map[B]] = b; yading@10: oluma = av_clip_uint8((55 * r + 182 * g + 19 * b) >> 8); yading@10: histeq->out_histogram[oluma]++; yading@10: } yading@10: } yading@10: src += inpic->linesize[0]; yading@10: dst += outpic->linesize[0]; yading@10: } yading@10: #ifdef DEBUG yading@10: for (x = 0; x < 256; x++) yading@10: av_dlog(ctx, "out[%d]: %u\n", x, histeq->out_histogram[x]); yading@10: #endif yading@10: yading@10: av_frame_free(&inpic); yading@10: return ff_filter_frame(outlink, outpic); yading@10: } yading@10: yading@10: static const AVFilterPad histeq_inputs[] = { yading@10: { yading@10: .name = "default", yading@10: .type = AVMEDIA_TYPE_VIDEO, yading@10: .config_props = config_input, yading@10: .filter_frame = filter_frame, yading@10: }, yading@10: { NULL } yading@10: }; yading@10: yading@10: static const AVFilterPad histeq_outputs[] = { yading@10: { yading@10: .name = "default", yading@10: .type = AVMEDIA_TYPE_VIDEO, yading@10: }, yading@10: { NULL } yading@10: }; yading@10: yading@10: AVFilter avfilter_vf_histeq = { yading@10: .name = "histeq", yading@10: .description = NULL_IF_CONFIG_SMALL("Apply global color histogram equalization."), yading@10: .priv_size = sizeof(HisteqContext), yading@10: .init = init, yading@10: .query_formats = query_formats, yading@10: yading@10: .inputs = histeq_inputs, yading@10: .outputs = histeq_outputs, yading@10: .priv_class = &histeq_class, yading@10: };