00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00035 #include "libavutil/imgutils.h"
00036 #include "libavutil/common.h"
00037 #include "libavutil/cpu.h"
00038 #include "libavutil/pixdesc.h"
00039 #include "avfilter.h"
00040 #include "formats.h"
00041 #include "gradfun.h"
00042 #include "internal.h"
00043 #include "video.h"
00044
00045 DECLARE_ALIGNED(16, static const uint16_t, dither)[8][8] = {
00046 {0x00,0x60,0x18,0x78,0x06,0x66,0x1E,0x7E},
00047 {0x40,0x20,0x58,0x38,0x46,0x26,0x5E,0x3E},
00048 {0x10,0x70,0x08,0x68,0x16,0x76,0x0E,0x6E},
00049 {0x50,0x30,0x48,0x28,0x56,0x36,0x4E,0x2E},
00050 {0x04,0x64,0x1C,0x7C,0x02,0x62,0x1A,0x7A},
00051 {0x44,0x24,0x5C,0x3C,0x42,0x22,0x5A,0x3A},
00052 {0x14,0x74,0x0C,0x6C,0x12,0x72,0x0A,0x6A},
00053 {0x54,0x34,0x4C,0x2C,0x52,0x32,0x4A,0x2A},
00054 };
00055
00056 void ff_gradfun_filter_line_c(uint8_t *dst, const uint8_t *src, const uint16_t *dc, int width, int thresh, const uint16_t *dithers)
00057 {
00058 int x;
00059 for (x = 0; x < width; x++, dc += x & 1) {
00060 int pix = src[x] << 7;
00061 int delta = dc[0] - pix;
00062 int m = abs(delta) * thresh >> 16;
00063 m = FFMAX(0, 127 - m);
00064 m = m * m * delta >> 14;
00065 pix += m + dithers[x & 7];
00066 dst[x] = av_clip_uint8(pix >> 7);
00067 }
00068 }
00069
00070 void ff_gradfun_blur_line_c(uint16_t *dc, uint16_t *buf, const uint16_t *buf1, const uint8_t *src, int src_linesize, int width)
00071 {
00072 int x, v, old;
00073 for (x = 0; x < width; x++) {
00074 v = buf1[x] + src[2 * x] + src[2 * x + 1] + src[2 * x + src_linesize] + src[2 * x + 1 + src_linesize];
00075 old = buf[x];
00076 buf[x] = v;
00077 dc[x] = v - old;
00078 }
00079 }
00080
00081 static void filter(GradFunContext *ctx, uint8_t *dst, const uint8_t *src, int width, int height, int dst_linesize, int src_linesize, int r)
00082 {
00083 int bstride = FFALIGN(width, 16) / 2;
00084 int y;
00085 uint32_t dc_factor = (1 << 21) / (r * r);
00086 uint16_t *dc = ctx->buf + 16;
00087 uint16_t *buf = ctx->buf + bstride + 32;
00088 int thresh = ctx->thresh;
00089
00090 memset(dc, 0, (bstride + 16) * sizeof(*buf));
00091 for (y = 0; y < r; y++)
00092 ctx->blur_line(dc, buf + y * bstride, buf + (y - 1) * bstride, src + 2 * y * src_linesize, src_linesize, width / 2);
00093 for (;;) {
00094 if (y < height - r) {
00095 int mod = ((y + r) / 2) % r;
00096 uint16_t *buf0 = buf + mod * bstride;
00097 uint16_t *buf1 = buf + (mod ? mod - 1 : r - 1) * bstride;
00098 int x, v;
00099 ctx->blur_line(dc, buf0, buf1, src + (y + r) * src_linesize, src_linesize, width / 2);
00100 for (x = v = 0; x < r; x++)
00101 v += dc[x];
00102 for (; x < width / 2; x++) {
00103 v += dc[x] - dc[x-r];
00104 dc[x-r] = v * dc_factor >> 16;
00105 }
00106 for (; x < (width + r + 1) / 2; x++)
00107 dc[x-r] = v * dc_factor >> 16;
00108 for (x = -r / 2; x < 0; x++)
00109 dc[x] = dc[0];
00110 }
00111 if (y == r) {
00112 for (y = 0; y < r; y++)
00113 ctx->filter_line(dst + y * dst_linesize, src + y * src_linesize, dc - r / 2, width, thresh, dither[y & 7]);
00114 }
00115 ctx->filter_line(dst + y * dst_linesize, src + y * src_linesize, dc - r / 2, width, thresh, dither[y & 7]);
00116 if (++y >= height) break;
00117 ctx->filter_line(dst + y * dst_linesize, src + y * src_linesize, dc - r / 2, width, thresh, dither[y & 7]);
00118 if (++y >= height) break;
00119 }
00120 }
00121
00122 static av_cold int init(AVFilterContext *ctx, const char *args)
00123 {
00124 GradFunContext *gf = ctx->priv;
00125 float thresh = 1.2;
00126 int radius = 16;
00127
00128 if (args)
00129 sscanf(args, "%f:%d", &thresh, &radius);
00130
00131 thresh = av_clipf(thresh, 0.51, 255);
00132 gf->thresh = (1 << 15) / thresh;
00133 gf->radius = av_clip((radius + 1) & ~1, 4, 32);
00134
00135 gf->blur_line = ff_gradfun_blur_line_c;
00136 gf->filter_line = ff_gradfun_filter_line_c;
00137
00138 if (HAVE_MMX)
00139 ff_gradfun_init_x86(gf);
00140
00141 av_log(ctx, AV_LOG_VERBOSE, "threshold:%.2f radius:%d\n", thresh, gf->radius);
00142
00143 return 0;
00144 }
00145
00146 static av_cold void uninit(AVFilterContext *ctx)
00147 {
00148 GradFunContext *gf = ctx->priv;
00149 av_freep(&gf->buf);
00150 }
00151
00152 static int query_formats(AVFilterContext *ctx)
00153 {
00154 static const enum PixelFormat pix_fmts[] = {
00155 PIX_FMT_YUV410P, PIX_FMT_YUV420P,
00156 PIX_FMT_GRAY8, PIX_FMT_NV12,
00157 PIX_FMT_NV21, PIX_FMT_YUV444P,
00158 PIX_FMT_YUV422P, PIX_FMT_YUV411P,
00159 PIX_FMT_NONE
00160 };
00161
00162 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00163
00164 return 0;
00165 }
00166
00167 static int config_input(AVFilterLink *inlink)
00168 {
00169 GradFunContext *gf = inlink->dst->priv;
00170 int hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
00171 int vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
00172
00173 gf->buf = av_mallocz((FFALIGN(inlink->w, 16) * (gf->radius + 1) / 2 + 32) * sizeof(uint16_t));
00174 if (!gf->buf)
00175 return AVERROR(ENOMEM);
00176
00177 gf->chroma_w = -((-inlink->w) >> hsub);
00178 gf->chroma_h = -((-inlink->h) >> vsub);
00179 gf->chroma_r = av_clip(((((gf->radius >> hsub) + (gf->radius >> vsub)) / 2 ) + 1) & ~1, 4, 32);
00180
00181 return 0;
00182 }
00183
00184 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00185 {
00186 return 0;
00187 }
00188
00189 static int end_frame(AVFilterLink *inlink)
00190 {
00191 GradFunContext *gf = inlink->dst->priv;
00192 AVFilterBufferRef *inpic = inlink->cur_buf;
00193 AVFilterLink *outlink = inlink->dst->outputs[0];
00194 AVFilterBufferRef *outpic = outlink->out_buf;
00195 int p, ret;
00196
00197 for (p = 0; p < 4 && inpic->data[p]; p++) {
00198 int w = inlink->w;
00199 int h = inlink->h;
00200 int r = gf->radius;
00201 if (p) {
00202 w = gf->chroma_w;
00203 h = gf->chroma_h;
00204 r = gf->chroma_r;
00205 }
00206
00207 if (FFMIN(w, h) > 2 * r)
00208 filter(gf, outpic->data[p], inpic->data[p], w, h, outpic->linesize[p], inpic->linesize[p], r);
00209 else if (outpic->data[p] != inpic->data[p])
00210 av_image_copy_plane(outpic->data[p], outpic->linesize[p], inpic->data[p], inpic->linesize[p], w, h);
00211 }
00212
00213 if ((ret = ff_draw_slice(outlink, 0, inlink->h, 1)) < 0 ||
00214 (ret = ff_end_frame(outlink)) < 0)
00215 return ret;
00216 return 0;
00217 }
00218
00219 AVFilter avfilter_vf_gradfun = {
00220 .name = "gradfun",
00221 .description = NULL_IF_CONFIG_SMALL("Debands video quickly using gradients."),
00222 .priv_size = sizeof(GradFunContext),
00223 .init = init,
00224 .uninit = uninit,
00225 .query_formats = query_formats,
00226
00227 .inputs = (const AVFilterPad[]) {{ .name = "default",
00228 .type = AVMEDIA_TYPE_VIDEO,
00229 .config_props = config_input,
00230 .start_frame = ff_inplace_start_frame,
00231 .draw_slice = null_draw_slice,
00232 .end_frame = end_frame,
00233 .min_perms = AV_PERM_READ, },
00234 { .name = NULL}},
00235 .outputs = (const AVFilterPad[]) {{ .name = "default",
00236 .type = AVMEDIA_TYPE_VIDEO, },
00237 { .name = NULL}},
00238 };