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;
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 },
52 AVFILTER_DEFINE_CLASS(srcnn);
54 static av_cold int init(AVFilterContext* context)
56 SRCNNContext* srcnn_context = context->priv;
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);
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);
68 srcnn_context->model = (srcnn_context->dnn_module->load_model)(srcnn_context->model_filename);
70 if (!srcnn_context->model){
71 av_log(context, AV_LOG_ERROR, "could not load dnn model\n");
78 static int query_formats(AVFilterContext* context)
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,
83 AVFilterFormats* formats_list;
85 formats_list = ff_make_format_list(pixel_formats);
87 av_log(context, AV_LOG_ERROR, "could not create formats list\n");
88 return AVERROR(ENOMEM);
90 return ff_set_common_formats(context, formats_list);
93 static int config_props(AVFilterLink* inlink)
95 AVFilterContext* context = inlink->dst;
96 SRCNNContext* srcnn_context = context->priv;
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);
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;
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");
120 typedef struct ThreadData{
122 int out_linesize, height, width;
125 static int uint8_to_float(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
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;
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;
139 src += td->out_linesize;
146 static int float_to_uint8(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
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;
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));
161 dst += td->out_linesize;
167 static int filter_frame(AVFilterLink* inlink, AVFrame* in)
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);
175 DNNReturnType dnn_result;
178 av_log(context, AV_LOG_ERROR, "could not allocate memory for output frame\n");
180 return AVERROR(ENOMEM);
182 av_frame_copy_props(out, in);
183 av_frame_copy(out, in);
185 td.out = out->data[0];
186 td.out_linesize = out->linesize[0];
187 td.height = out->height;
188 td.width = out->width;
190 nb_threads = ff_filter_get_nb_threads(context);
191 context->internal->execute(context, uint8_to_float, &td, NULL, FFMIN(td.height, nb_threads));
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");
199 context->internal->execute(context, float_to_uint8, &td, NULL, FFMIN(td.height, nb_threads));
201 return ff_filter_frame(outlink, out);
204 static av_cold void uninit(AVFilterContext* context)
206 SRCNNContext* srcnn_context = context->priv;
208 if (srcnn_context->dnn_module){
209 (srcnn_context->dnn_module->free_model)(&srcnn_context->model);
210 av_freep(&srcnn_context->dnn_module);
212 av_freep(&srcnn_context->input_output_buf);
215 static const AVFilterPad srcnn_inputs[] = {
218 .type = AVMEDIA_TYPE_VIDEO,
219 .config_props = config_props,
220 .filter_frame = filter_frame,
225 static const AVFilterPad srcnn_outputs[] = {
228 .type = AVMEDIA_TYPE_VIDEO,
233 AVFilter ff_vf_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),
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,