]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_derain.c
dnn: move output name from DNNModel.set_input_output to DNNModule.execute_model
[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     DNNReturnType result;
82
83     dr_context->input.width    = inlink->w;
84     dr_context->input.height   = inlink->h;
85     dr_context->input.channels = 3;
86
87     result = (dr_context->model->set_input)(dr_context->model->model, &dr_context->input, "x");
88     if (result != DNN_SUCCESS) {
89         av_log(ctx, AV_LOG_ERROR, "could not set input and output for the model\n");
90         return AVERROR(EIO);
91     }
92
93     return 0;
94 }
95
96 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
97 {
98     AVFilterContext *ctx  = inlink->dst;
99     AVFilterLink *outlink = ctx->outputs[0];
100     DRContext *dr_context = ctx->priv;
101     DNNReturnType dnn_result;
102     const char *model_output_name = "y";
103
104     AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
105     if (!out) {
106         av_log(ctx, AV_LOG_ERROR, "could not allocate memory for output frame\n");
107         av_frame_free(&in);
108         return AVERROR(ENOMEM);
109     }
110
111     av_frame_copy_props(out, in);
112
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;
118         }
119     }
120
121     dnn_result = (dr_context->dnn_module->execute_model)(dr_context->model, &dr_context->output, &model_output_name, 1);
122     if (dnn_result != DNN_SUCCESS){
123         av_log(ctx, AV_LOG_ERROR, "failed to execute model\n");
124         return AVERROR(EIO);
125     }
126
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;
131
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);
137         }
138     }
139
140     av_frame_free(&in);
141
142     return ff_filter_frame(outlink, out);
143 }
144
145 static av_cold int init(AVFilterContext *ctx)
146 {
147     DRContext *dr_context = ctx->priv;
148
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);
154     }
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);
158     }
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);
162     }
163
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);
168     }
169
170     return 0;
171 }
172
173 static av_cold void uninit(AVFilterContext *ctx)
174 {
175     DRContext *dr_context = ctx->priv;
176
177     if (dr_context->dnn_module) {
178         (dr_context->dnn_module->free_model)(&dr_context->model);
179         av_freep(&dr_context->dnn_module);
180     }
181 }
182
183 static const AVFilterPad derain_inputs[] = {
184     {
185         .name         = "default",
186         .type         = AVMEDIA_TYPE_VIDEO,
187         .config_props = config_inputs,
188         .filter_frame = filter_frame,
189     },
190     { NULL }
191 };
192
193 static const AVFilterPad derain_outputs[] = {
194     {
195         .name = "default",
196         .type = AVMEDIA_TYPE_VIDEO,
197     },
198     { NULL }
199 };
200
201 AVFilter ff_vf_derain = {
202     .name          = "derain",
203     .description   = NULL_IF_CONFIG_SMALL("Apply derain filter to the input."),
204     .priv_size     = sizeof(DRContext),
205     .init          = init,
206     .uninit        = uninit,
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,
212 };