]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_srcnn.c
Adds TensorFlow backend for dnn inference module.
[ffmpeg] / libavfilter / vf_srcnn.c
1 /*
2  * Copyright (c) 2018 Sergey Lavrushkin
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 super-resolution using deep convolutional networks.
24  * https://arxiv.org/abs/1501.00092
25  */
26
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "libavutil/opt.h"
31 #include "libavformat/avio.h"
32 #include "dnn_interface.h"
33
34 typedef struct SRCNNContext {
35     const AVClass *class;
36
37     char* model_filename;
38     float* input_output_buf;
39     DNNModule* dnn_module;
40     DNNModel* model;
41     DNNData input_output;
42 } SRCNNContext;
43
44 #define OFFSET(x) offsetof(SRCNNContext, x)
45 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
46 static const AVOption srcnn_options[] = {
47     { "model_filename", "path to model file specifying network architecture and its parameters", OFFSET(model_filename), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
48     { NULL }
49 };
50
51 AVFILTER_DEFINE_CLASS(srcnn);
52
53 static av_cold int init(AVFilterContext* context)
54 {
55     SRCNNContext* srcnn_context = context->priv;
56
57     srcnn_context->dnn_module = ff_get_dnn_module(DNN_TF);
58     if (!srcnn_context->dnn_module){
59         srcnn_context->dnn_module = ff_get_dnn_module(DNN_NATIVE);
60         if (!srcnn_context->dnn_module){
61             av_log(context, AV_LOG_ERROR, "could not create dnn module\n");
62             return AVERROR(ENOMEM);
63         }
64         else{
65             av_log(context, AV_LOG_INFO, "using native backend for DNN inference\n");
66         }
67     }
68     else{
69         av_log(context, AV_LOG_INFO, "using tensorflow backend for DNN inference\n");
70     }
71     if (!srcnn_context->model_filename){
72         av_log(context, AV_LOG_INFO, "model file for network was not specified, using default network for x2 upsampling\n");
73         srcnn_context->model = (srcnn_context->dnn_module->load_default_model)(DNN_SRCNN);
74     }
75     else{
76         srcnn_context->model = (srcnn_context->dnn_module->load_model)(srcnn_context->model_filename);
77     }
78     if (!srcnn_context->model){
79         av_log(context, AV_LOG_ERROR, "could not load dnn model\n");
80         return AVERROR(EIO);
81     }
82
83     return 0;
84 }
85
86 static int query_formats(AVFilterContext* context)
87 {
88     const enum AVPixelFormat pixel_formats[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
89                                                 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
90                                                 AV_PIX_FMT_NONE};
91     AVFilterFormats* formats_list;
92
93     formats_list = ff_make_format_list(pixel_formats);
94     if (!formats_list){
95         av_log(context, AV_LOG_ERROR, "could not create formats list\n");
96         return AVERROR(ENOMEM);
97     }
98     return ff_set_common_formats(context, formats_list);
99 }
100
101 static int config_props(AVFilterLink* inlink)
102 {
103     AVFilterContext* context = inlink->dst;
104     SRCNNContext* srcnn_context = context->priv;
105     DNNReturnType result;
106
107     srcnn_context->input_output_buf = av_malloc(inlink->h * inlink->w * sizeof(float));
108     if (!srcnn_context->input_output_buf){
109         av_log(context, AV_LOG_ERROR, "could not allocate memory for input/output buffer\n");
110         return AVERROR(ENOMEM);
111     }
112
113     srcnn_context->input_output.data = srcnn_context->input_output_buf;
114     srcnn_context->input_output.width = inlink->w;
115     srcnn_context->input_output.height = inlink->h;
116     srcnn_context->input_output.channels = 1;
117
118     result = (srcnn_context->model->set_input_output)(srcnn_context->model->model, &srcnn_context->input_output, &srcnn_context->input_output);
119     if (result != DNN_SUCCESS){
120         av_log(context, AV_LOG_ERROR, "could not set input and output for the model\n");
121         return AVERROR(EIO);
122     }
123     else{
124         return 0;
125     }
126 }
127
128 typedef struct ThreadData{
129     uint8_t* out;
130     int out_linesize, height, width;
131 } ThreadData;
132
133 static int uint8_to_float(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
134 {
135     SRCNNContext* srcnn_context = context->priv;
136     const ThreadData* td = arg;
137     const int slice_start = (td->height *  jobnr     ) / nb_jobs;
138     const int slice_end   = (td->height * (jobnr + 1)) / nb_jobs;
139     const uint8_t* src = td->out + slice_start * td->out_linesize;
140     float* dst = srcnn_context->input_output_buf + slice_start * td->width;
141     int y, x;
142
143     for (y = slice_start; y < slice_end; ++y){
144         for (x = 0; x < td->width; ++x){
145             dst[x] = (float)src[x] / 255.0f;
146         }
147         src += td->out_linesize;
148         dst += td->width;
149     }
150
151     return 0;
152 }
153
154 static int float_to_uint8(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
155 {
156     SRCNNContext* srcnn_context = context->priv;
157     const ThreadData* td = arg;
158     const int slice_start = (td->height *  jobnr     ) / nb_jobs;
159     const int slice_end   = (td->height * (jobnr + 1)) / nb_jobs;
160     const float* src = srcnn_context->input_output_buf + slice_start * td->width;
161     uint8_t* dst = td->out + slice_start * td->out_linesize;
162     int y, x;
163
164     for (y = slice_start; y < slice_end; ++y){
165         for (x = 0; x < td->width; ++x){
166             dst[x] = (uint8_t)(255.0f * FFMIN(src[x], 1.0f));
167         }
168         src += td->width;
169         dst += td->out_linesize;
170     }
171
172     return 0;
173 }
174
175 static int filter_frame(AVFilterLink* inlink, AVFrame* in)
176 {
177     AVFilterContext* context = inlink->dst;
178     SRCNNContext* srcnn_context = context->priv;
179     AVFilterLink* outlink = context->outputs[0];
180     AVFrame* out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
181     ThreadData td;
182     int nb_threads;
183     DNNReturnType dnn_result;
184
185     if (!out){
186         av_log(context, AV_LOG_ERROR, "could not allocate memory for output frame\n");
187         av_frame_free(&in);
188         return AVERROR(ENOMEM);
189     }
190     av_frame_copy_props(out, in);
191     av_frame_copy(out, in);
192     av_frame_free(&in);
193     td.out = out->data[0];
194     td.out_linesize = out->linesize[0];
195     td.height = out->height;
196     td.width = out->width;
197
198     nb_threads = ff_filter_get_nb_threads(context);
199     context->internal->execute(context, uint8_to_float, &td, NULL, FFMIN(td.height, nb_threads));
200
201     dnn_result = (srcnn_context->dnn_module->execute_model)(srcnn_context->model);
202     if (dnn_result != DNN_SUCCESS){
203         av_log(context, AV_LOG_ERROR, "failed to execute loaded model\n");
204         return AVERROR(EIO);
205     }
206
207     context->internal->execute(context, float_to_uint8, &td, NULL, FFMIN(td.height, nb_threads));
208
209     return ff_filter_frame(outlink, out);
210 }
211
212 static av_cold void uninit(AVFilterContext* context)
213 {
214     SRCNNContext* srcnn_context = context->priv;
215
216     if (srcnn_context->dnn_module){
217         (srcnn_context->dnn_module->free_model)(&srcnn_context->model);
218         av_freep(&srcnn_context->dnn_module);
219     }
220     av_freep(&srcnn_context->input_output_buf);
221 }
222
223 static const AVFilterPad srcnn_inputs[] = {
224     {
225         .name         = "default",
226         .type         = AVMEDIA_TYPE_VIDEO,
227         .config_props = config_props,
228         .filter_frame = filter_frame,
229     },
230     { NULL }
231 };
232
233 static const AVFilterPad srcnn_outputs[] = {
234     {
235         .name = "default",
236         .type = AVMEDIA_TYPE_VIDEO,
237     },
238     { NULL }
239 };
240
241 AVFilter ff_vf_srcnn = {
242     .name          = "srcnn",
243     .description   = NULL_IF_CONFIG_SMALL("Apply super resolution convolutional neural network to the input. Use bicubic upsamping with corresponding scaling factor before."),
244     .priv_size     = sizeof(SRCNNContext),
245     .init          = init,
246     .uninit        = uninit,
247     .query_formats = query_formats,
248     .inputs        = srcnn_inputs,
249     .outputs       = srcnn_outputs,
250     .priv_class    = &srcnn_class,
251     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
252 };
253