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