2 * Copyright (c) 2018 Sergey Lavrushkin
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 super-resolution using deep convolutional networks.
24 * https://arxiv.org/abs/1501.00092
30 #include "libavutil/opt.h"
31 #include "libavformat/avio.h"
32 #include "dnn_interface.h"
34 typedef struct SRCNNContext {
38 float* input_output_buf;
39 DNNModule* dnn_module;
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 },
51 AVFILTER_DEFINE_CLASS(srcnn);
53 static av_cold int init(AVFilterContext* context)
55 SRCNNContext* srcnn_context = context->priv;
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);
65 av_log(context, AV_LOG_INFO, "using native backend for DNN inference\n");
69 av_log(context, AV_LOG_INFO, "using tensorflow backend for DNN inference\n");
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);
76 srcnn_context->model = (srcnn_context->dnn_module->load_model)(srcnn_context->model_filename);
78 if (!srcnn_context->model){
79 av_log(context, AV_LOG_ERROR, "could not load dnn model\n");
86 static int query_formats(AVFilterContext* context)
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,
91 AVFilterFormats* formats_list;
93 formats_list = ff_make_format_list(pixel_formats);
95 av_log(context, AV_LOG_ERROR, "could not create formats list\n");
96 return AVERROR(ENOMEM);
98 return ff_set_common_formats(context, formats_list);
101 static int config_props(AVFilterLink* inlink)
103 AVFilterContext* context = inlink->dst;
104 SRCNNContext* srcnn_context = context->priv;
105 DNNReturnType result;
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);
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;
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");
128 typedef struct ThreadData{
130 int out_linesize, height, width;
133 static int uint8_to_float(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
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;
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;
147 src += td->out_linesize;
154 static int float_to_uint8(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
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;
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));
169 dst += td->out_linesize;
175 static int filter_frame(AVFilterLink* inlink, AVFrame* in)
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);
183 DNNReturnType dnn_result;
186 av_log(context, AV_LOG_ERROR, "could not allocate memory for output frame\n");
188 return AVERROR(ENOMEM);
190 av_frame_copy_props(out, in);
191 av_frame_copy(out, in);
193 td.out = out->data[0];
194 td.out_linesize = out->linesize[0];
195 td.height = out->height;
196 td.width = out->width;
198 nb_threads = ff_filter_get_nb_threads(context);
199 context->internal->execute(context, uint8_to_float, &td, NULL, FFMIN(td.height, nb_threads));
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");
207 context->internal->execute(context, float_to_uint8, &td, NULL, FFMIN(td.height, nb_threads));
209 return ff_filter_frame(outlink, out);
212 static av_cold void uninit(AVFilterContext* context)
214 SRCNNContext* srcnn_context = context->priv;
216 if (srcnn_context->dnn_module){
217 (srcnn_context->dnn_module->free_model)(&srcnn_context->model);
218 av_freep(&srcnn_context->dnn_module);
220 av_freep(&srcnn_context->input_output_buf);
223 static const AVFilterPad srcnn_inputs[] = {
226 .type = AVMEDIA_TYPE_VIDEO,
227 .config_props = config_props,
228 .filter_frame = filter_frame,
233 static const AVFilterPad srcnn_outputs[] = {
236 .type = AVMEDIA_TYPE_VIDEO,
241 AVFilter ff_vf_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),
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,