2 * Copyright (c) 2019 Xuewei Meng
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Filter implementing image derain filter using deep convolutional networks.
24 * http://openaccess.thecvf.com/content_ECCV_2018/html/Xia_Li_Recurrent_Squeeze-and-Excitation_Context_ECCV_2018_paper.html
27 #include "libavformat/avio.h"
28 #include "libavutil/opt.h"
30 #include "dnn_interface.h"
34 typedef struct DRContext {
39 DNNBackendType backend_type;
40 DNNModule *dnn_module;
46 #define CLIP(x, min, max) (x < min ? min : (x > max ? max : x))
47 #define OFFSET(x) offsetof(DRContext, x)
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
49 static const AVOption derain_options[] = {
50 { "filter_type", "filter type(derain/dehaze)", OFFSET(filter_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS, "type" },
51 { "derain", "derain filter flag", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "type" },
52 { "dehaze", "dehaze filter flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "type" },
53 { "dnn_backend", "DNN backend", OFFSET(backend_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS, "backend" },
54 { "native", "native backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "backend" },
55 #if (CONFIG_LIBTENSORFLOW == 1)
56 { "tensorflow", "tensorflow backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "backend" },
58 { "model", "path to model file", OFFSET(model_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
62 AVFILTER_DEFINE_CLASS(derain);
64 static int query_formats(AVFilterContext *ctx)
66 AVFilterFormats *formats;
67 const enum AVPixelFormat pixel_fmts[] = {
72 formats = ff_make_format_list(pixel_fmts);
74 return ff_set_common_formats(ctx, formats);
77 static int config_inputs(AVFilterLink *inlink)
79 AVFilterContext *ctx = inlink->dst;
80 DRContext *dr_context = ctx->priv;
81 const char *model_output_name = "y";
84 dr_context->input.width = inlink->w;
85 dr_context->input.height = inlink->h;
86 dr_context->input.channels = 3;
88 result = (dr_context->model->set_input_output)(dr_context->model->model, &dr_context->input, "x", &model_output_name, 1);
89 if (result != DNN_SUCCESS) {
90 av_log(ctx, AV_LOG_ERROR, "could not set input and output for the model\n");
97 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
99 AVFilterContext *ctx = inlink->dst;
100 AVFilterLink *outlink = ctx->outputs[0];
101 DRContext *dr_context = ctx->priv;
102 DNNReturnType dnn_result;
104 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
106 av_log(ctx, AV_LOG_ERROR, "could not allocate memory for output frame\n");
108 return AVERROR(ENOMEM);
111 av_frame_copy_props(out, in);
113 for (int i = 0; i < in->height; i++){
114 for(int j = 0; j < in->width * 3; j++){
115 int k = i * in->linesize[0] + j;
116 int t = i * in->width * 3 + j;
117 ((float *)dr_context->input.data)[t] = in->data[0][k] / 255.0;
121 dnn_result = (dr_context->dnn_module->execute_model)(dr_context->model, &dr_context->output, 1);
122 if (dnn_result != DNN_SUCCESS){
123 av_log(ctx, AV_LOG_ERROR, "failed to execute model\n");
127 out->height = dr_context->output.height;
128 out->width = dr_context->output.width;
129 outlink->h = dr_context->output.height;
130 outlink->w = dr_context->output.width;
132 for (int i = 0; i < out->height; i++){
133 for(int j = 0; j < out->width * 3; j++){
134 int k = i * out->linesize[0] + j;
135 int t = i * out->width * 3 + j;
136 out->data[0][k] = CLIP((int)((((float *)dr_context->output.data)[t]) * 255), 0, 255);
142 return ff_filter_frame(outlink, out);
145 static av_cold int init(AVFilterContext *ctx)
147 DRContext *dr_context = ctx->priv;
149 dr_context->input.dt = DNN_FLOAT;
150 dr_context->dnn_module = ff_get_dnn_module(dr_context->backend_type);
151 if (!dr_context->dnn_module) {
152 av_log(ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
153 return AVERROR(ENOMEM);
155 if (!dr_context->model_filename) {
156 av_log(ctx, AV_LOG_ERROR, "model file for network is not specified\n");
157 return AVERROR(EINVAL);
159 if (!dr_context->dnn_module->load_model) {
160 av_log(ctx, AV_LOG_ERROR, "load_model for network is not specified\n");
161 return AVERROR(EINVAL);
164 dr_context->model = (dr_context->dnn_module->load_model)(dr_context->model_filename, NULL);
165 if (!dr_context->model) {
166 av_log(ctx, AV_LOG_ERROR, "could not load DNN model\n");
167 return AVERROR(EINVAL);
173 static av_cold void uninit(AVFilterContext *ctx)
175 DRContext *dr_context = ctx->priv;
177 if (dr_context->dnn_module) {
178 (dr_context->dnn_module->free_model)(&dr_context->model);
179 av_freep(&dr_context->dnn_module);
183 static const AVFilterPad derain_inputs[] = {
186 .type = AVMEDIA_TYPE_VIDEO,
187 .config_props = config_inputs,
188 .filter_frame = filter_frame,
193 static const AVFilterPad derain_outputs[] = {
196 .type = AVMEDIA_TYPE_VIDEO,
201 AVFilter ff_vf_derain = {
203 .description = NULL_IF_CONFIG_SMALL("Apply derain filter to the input."),
204 .priv_size = sizeof(DRContext),
207 .query_formats = query_formats,
208 .inputs = derain_inputs,
209 .outputs = derain_outputs,
210 .priv_class = &derain_class,
211 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,