]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_derain.c
avfilter/dnn: get the data type of network output from dnn execution result
[ffmpeg] / libavfilter / vf_derain.c
1 /*
2  * Copyright (c) 2019 Xuewei Meng
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
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
25  */
26
27 #include "libavformat/avio.h"
28 #include "libavutil/opt.h"
29 #include "avfilter.h"
30 #include "dnn_interface.h"
31 #include "formats.h"
32 #include "internal.h"
33
34 typedef struct DRContext {
35     const AVClass *class;
36
37     int                filter_type;
38     char              *model_filename;
39     DNNBackendType     backend_type;
40     DNNModule         *dnn_module;
41     DNNModel          *model;
42     DNNData            input;
43     DNNData            output;
44 } DRContext;
45
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" },
57 #endif
58     { "model",       "path to model file",          OFFSET(model_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
59     { NULL }
60 };
61
62 AVFILTER_DEFINE_CLASS(derain);
63
64 static int query_formats(AVFilterContext *ctx)
65 {
66     AVFilterFormats *formats;
67     const enum AVPixelFormat pixel_fmts[] = {
68         AV_PIX_FMT_RGB24,
69         AV_PIX_FMT_NONE
70     };
71
72     formats = ff_make_format_list(pixel_fmts);
73
74     return ff_set_common_formats(ctx, formats);
75 }
76
77 static int config_inputs(AVFilterLink *inlink)
78 {
79     AVFilterContext *ctx          = inlink->dst;
80     DRContext *dr_context         = ctx->priv;
81     const char *model_output_name = "y";
82     DNNReturnType result;
83
84     dr_context->input.width    = inlink->w;
85     dr_context->input.height   = inlink->h;
86     dr_context->input.channels = 3;
87
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");
91         return AVERROR(EIO);
92     }
93
94     return 0;
95 }
96
97 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
98 {
99     AVFilterContext *ctx  = inlink->dst;
100     AVFilterLink *outlink = ctx->outputs[0];
101     DRContext *dr_context = ctx->priv;
102     DNNReturnType dnn_result;
103     int pad_size;
104
105     AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
106     if (!out) {
107         av_log(ctx, AV_LOG_ERROR, "could not allocate memory for output frame\n");
108         av_frame_free(&in);
109         return AVERROR(ENOMEM);
110     }
111
112     av_frame_copy_props(out, in);
113
114     for (int i = 0; i < in->height; i++){
115         for(int j = 0; j < in->width * 3; j++){
116             int k = i * in->linesize[0] + j;
117             int t = i * in->width * 3 + j;
118             ((float *)dr_context->input.data)[t] = in->data[0][k] / 255.0;
119         }
120     }
121
122     dnn_result = (dr_context->dnn_module->execute_model)(dr_context->model, &dr_context->output, 1);
123     if (dnn_result != DNN_SUCCESS){
124         av_log(ctx, AV_LOG_ERROR, "failed to execute model\n");
125         return AVERROR(EIO);
126     }
127
128     out->height = dr_context->output.height;
129     out->width  = dr_context->output.width;
130     outlink->h  = dr_context->output.height;
131     outlink->w  = dr_context->output.width;
132     pad_size    = (in->height - out->height) >> 1;
133
134     for (int i = 0; i < out->height; i++){
135         for(int j = 0; j < out->width * 3; j++){
136             int k = i * out->linesize[0] + j;
137             int t = i * out->width * 3 + j;
138
139             int t_in =  (i + pad_size) * in->width * 3 + j + pad_size * 3;
140             out->data[0][k] = CLIP((int)((((float *)dr_context->input.data)[t_in] - ((float *)dr_context->output.data)[t]) * 255), 0, 255);
141         }
142     }
143
144     av_frame_free(&in);
145
146     return ff_filter_frame(outlink, out);
147 }
148
149 static av_cold int init(AVFilterContext *ctx)
150 {
151     DRContext *dr_context = ctx->priv;
152
153     dr_context->input.dt = DNN_FLOAT;
154     dr_context->dnn_module = ff_get_dnn_module(dr_context->backend_type);
155     if (!dr_context->dnn_module) {
156         av_log(ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
157         return AVERROR(ENOMEM);
158     }
159     if (!dr_context->model_filename) {
160         av_log(ctx, AV_LOG_ERROR, "model file for network is not specified\n");
161         return AVERROR(EINVAL);
162     }
163     if (!dr_context->dnn_module->load_model) {
164         av_log(ctx, AV_LOG_ERROR, "load_model for network is not specified\n");
165         return AVERROR(EINVAL);
166     }
167
168     dr_context->model = (dr_context->dnn_module->load_model)(dr_context->model_filename);
169     if (!dr_context->model) {
170         av_log(ctx, AV_LOG_ERROR, "could not load DNN model\n");
171         return AVERROR(EINVAL);
172     }
173
174     return 0;
175 }
176
177 static av_cold void uninit(AVFilterContext *ctx)
178 {
179     DRContext *dr_context = ctx->priv;
180
181     if (dr_context->dnn_module) {
182         (dr_context->dnn_module->free_model)(&dr_context->model);
183         av_freep(&dr_context->dnn_module);
184     }
185 }
186
187 static const AVFilterPad derain_inputs[] = {
188     {
189         .name         = "default",
190         .type         = AVMEDIA_TYPE_VIDEO,
191         .config_props = config_inputs,
192         .filter_frame = filter_frame,
193     },
194     { NULL }
195 };
196
197 static const AVFilterPad derain_outputs[] = {
198     {
199         .name = "default",
200         .type = AVMEDIA_TYPE_VIDEO,
201     },
202     { NULL }
203 };
204
205 AVFilter ff_vf_derain = {
206     .name          = "derain",
207     .description   = NULL_IF_CONFIG_SMALL("Apply derain filter to the input."),
208     .priv_size     = sizeof(DRContext),
209     .init          = init,
210     .uninit        = uninit,
211     .query_formats = query_formats,
212     .inputs        = derain_inputs,
213     .outputs       = derain_outputs,
214     .priv_class    = &derain_class,
215     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
216 };