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
25 * https://arxiv.org/abs/1609.05158
31 #include "libavutil/opt.h"
32 #include "libavformat/avio.h"
33 #include "libswscale/swscale.h"
34 #include "dnn_interface.h"
36 typedef enum {SRCNN, ESPCN} SRModel;
38 typedef struct SRContext {
43 DNNBackendType backend_type;
44 DNNModule* dnn_module;
46 DNNData input, output;
48 struct SwsContext* sws_context;
52 #define OFFSET(x) offsetof(SRContext, x)
53 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
54 static const AVOption sr_options[] = {
55 { "model", "specifies what DNN model to use", OFFSET(model_type), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, 1, FLAGS, "model_type" },
56 { "srcnn", "Super-Resolution Convolutional Neural Network model (scale factor should be specified for custom SRCNN model)", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "model_type" },
57 { "espcn", "Efficient Sub-Pixel Convolutional Neural Network model", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "model_type" },
58 { "dnn_backend", "DNN backend used for model execution", OFFSET(backend_type), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, 1, FLAGS, "backend" },
59 { "native", "native backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "backend" },
60 #if (CONFIG_LIBTENSORFLOW == 1)
61 { "tensorflow", "tensorflow backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "backend" },
63 {"scale_factor", "scale factor for SRCNN model", OFFSET(scale_factor), AV_OPT_TYPE_INT, { .i64 = 2 }, 2, 4, FLAGS},
64 { "model_filename", "path to model file specifying network architecture and its parameters", OFFSET(model_filename), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
68 AVFILTER_DEFINE_CLASS(sr);
70 static av_cold int init(AVFilterContext* context)
72 SRContext* sr_context = context->priv;
74 sr_context->dnn_module = ff_get_dnn_module(sr_context->backend_type);
75 if (!sr_context->dnn_module){
76 av_log(context, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
77 return AVERROR(ENOMEM);
79 if (!sr_context->model_filename){
80 av_log(context, AV_LOG_VERBOSE, "model file for network was not specified, using default network for x2 upsampling\n");
81 sr_context->scale_factor = 2;
82 switch (sr_context->model_type){
84 sr_context->model = (sr_context->dnn_module->load_default_model)(DNN_SRCNN);
87 sr_context->model = (sr_context->dnn_module->load_default_model)(DNN_ESPCN);
91 sr_context->model = (sr_context->dnn_module->load_model)(sr_context->model_filename);
93 if (!sr_context->model){
94 av_log(context, AV_LOG_ERROR, "could not load DNN model\n");
101 static int query_formats(AVFilterContext* context)
103 const enum AVPixelFormat pixel_formats[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
104 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
106 AVFilterFormats* formats_list;
108 formats_list = ff_make_format_list(pixel_formats);
110 av_log(context, AV_LOG_ERROR, "could not create formats list\n");
111 return AVERROR(ENOMEM);
113 return ff_set_common_formats(context, formats_list);
116 static int config_props(AVFilterLink* inlink)
118 AVFilterContext* context = inlink->dst;
119 SRContext* sr_context = context->priv;
120 AVFilterLink* outlink = context->outputs[0];
121 DNNReturnType result;
122 int sws_src_h, sws_src_w, sws_dst_h, sws_dst_w;
124 switch (sr_context->model_type){
126 sr_context->input.width = inlink->w * sr_context->scale_factor;
127 sr_context->input.height = inlink->h * sr_context->scale_factor;
130 sr_context->input.width = inlink->w;
131 sr_context->input.height = inlink->h;
133 sr_context->input.channels = 1;
135 result = (sr_context->model->set_input_output)(sr_context->model->model, &sr_context->input, &sr_context->output);
136 if (result != DNN_SUCCESS){
137 av_log(context, AV_LOG_ERROR, "could not set input and output for the model\n");
141 outlink->h = sr_context->output.height;
142 outlink->w = sr_context->output.width;
143 switch (sr_context->model_type){
145 sr_context->sws_context = sws_getContext(inlink->w, inlink->h, inlink->format,
146 outlink->w, outlink->h, outlink->format, SWS_BICUBIC, NULL, NULL, NULL);
147 if (!sr_context->sws_context){
148 av_log(context, AV_LOG_ERROR, "could not create SwsContext\n");
149 return AVERROR(ENOMEM);
151 sr_context->sws_slice_h = inlink->h;
154 if (inlink->format == AV_PIX_FMT_GRAY8){
155 sr_context->sws_context = NULL;
158 sws_src_h = sr_context->input.height;
159 sws_src_w = sr_context->input.width;
160 sws_dst_h = sr_context->output.height;
161 sws_dst_w = sr_context->output.width;
163 switch (inlink->format){
164 case AV_PIX_FMT_YUV420P:
165 sws_src_h = (sws_src_h >> 1) + (sws_src_h % 2 != 0 ? 1 : 0);
166 sws_src_w = (sws_src_w >> 1) + (sws_src_w % 2 != 0 ? 1 : 0);
167 sws_dst_h = (sws_dst_h >> 1) + (sws_dst_h % 2 != 0 ? 1 : 0);
168 sws_dst_w = (sws_dst_w >> 1) + (sws_dst_w % 2 != 0 ? 1 : 0);
170 case AV_PIX_FMT_YUV422P:
171 sws_src_w = (sws_src_w >> 1) + (sws_src_w % 2 != 0 ? 1 : 0);
172 sws_dst_w = (sws_dst_w >> 1) + (sws_dst_w % 2 != 0 ? 1 : 0);
174 case AV_PIX_FMT_YUV444P:
176 case AV_PIX_FMT_YUV410P:
177 sws_src_h = (sws_src_h >> 2) + (sws_src_h % 4 != 0 ? 1 : 0);
178 sws_src_w = (sws_src_w >> 2) + (sws_src_w % 4 != 0 ? 1 : 0);
179 sws_dst_h = (sws_dst_h >> 2) + (sws_dst_h % 4 != 0 ? 1 : 0);
180 sws_dst_w = (sws_dst_w >> 2) + (sws_dst_w % 4 != 0 ? 1 : 0);
182 case AV_PIX_FMT_YUV411P:
183 sws_src_w = (sws_src_w >> 2) + (sws_src_w % 4 != 0 ? 1 : 0);
184 sws_dst_w = (sws_dst_w >> 2) + (sws_dst_w % 4 != 0 ? 1 : 0);
187 av_log(context, AV_LOG_ERROR, "could not create SwsContext for input pixel format");
190 sr_context->sws_context = sws_getContext(sws_src_w, sws_src_h, AV_PIX_FMT_GRAY8,
191 sws_dst_w, sws_dst_h, AV_PIX_FMT_GRAY8, SWS_BICUBIC, NULL, NULL, NULL);
192 if (!sr_context->sws_context){
193 av_log(context, AV_LOG_ERROR, "could not create SwsContext\n");
194 return AVERROR(ENOMEM);
196 sr_context->sws_slice_h = sws_src_h;
204 typedef struct ThreadData{
206 int data_linesize, height, width;
209 static int uint8_to_float(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
211 SRContext* sr_context = context->priv;
212 const ThreadData* td = arg;
213 const int slice_start = (td->height * jobnr ) / nb_jobs;
214 const int slice_end = (td->height * (jobnr + 1)) / nb_jobs;
215 const uint8_t* src = td->data + slice_start * td->data_linesize;
216 float* dst = sr_context->input.data + slice_start * td->width;
219 for (y = slice_start; y < slice_end; ++y){
220 for (x = 0; x < td->width; ++x){
221 dst[x] = (float)src[x] / 255.0f;
223 src += td->data_linesize;
230 static int float_to_uint8(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
232 SRContext* sr_context = context->priv;
233 const ThreadData* td = arg;
234 const int slice_start = (td->height * jobnr ) / nb_jobs;
235 const int slice_end = (td->height * (jobnr + 1)) / nb_jobs;
236 const float* src = sr_context->output.data + slice_start * td->width;
237 uint8_t* dst = td->data + slice_start * td->data_linesize;
240 for (y = slice_start; y < slice_end; ++y){
241 for (x = 0; x < td->width; ++x){
242 dst[x] = (uint8_t)(255.0f * FFMIN(src[x], 1.0f));
245 dst += td->data_linesize;
251 static int filter_frame(AVFilterLink* inlink, AVFrame* in)
253 AVFilterContext* context = inlink->dst;
254 SRContext* sr_context = context->priv;
255 AVFilterLink* outlink = context->outputs[0];
256 AVFrame* out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
259 DNNReturnType dnn_result;
262 av_log(context, AV_LOG_ERROR, "could not allocate memory for output frame\n");
264 return AVERROR(ENOMEM);
266 av_frame_copy_props(out, in);
267 out->height = sr_context->output.height;
268 out->width = sr_context->output.width;
269 switch (sr_context->model_type){
271 sws_scale(sr_context->sws_context, (const uint8_t **)in->data, in->linesize,
272 0, sr_context->sws_slice_h, out->data, out->linesize);
273 td.data = out->data[0];
274 td.data_linesize = out->linesize[0];
275 td.height = out->height;
276 td.width = out->width;
279 if (sr_context->sws_context){
280 sws_scale(sr_context->sws_context, (const uint8_t **)(in->data + 1), in->linesize + 1,
281 0, sr_context->sws_slice_h, out->data + 1, out->linesize + 1);
282 sws_scale(sr_context->sws_context, (const uint8_t **)(in->data + 2), in->linesize + 2,
283 0, sr_context->sws_slice_h, out->data + 2, out->linesize + 2);
285 td.data = in->data[0];
286 td.data_linesize = in->linesize[0];
287 td.height = in->height;
288 td.width = in->width;
291 nb_threads = ff_filter_get_nb_threads(context);
292 context->internal->execute(context, uint8_to_float, &td, NULL, FFMIN(td.height, nb_threads));
295 dnn_result = (sr_context->dnn_module->execute_model)(sr_context->model);
296 if (dnn_result != DNN_SUCCESS){
297 av_log(context, AV_LOG_ERROR, "failed to execute loaded model\n");
301 td.data = out->data[0];
302 td.data_linesize = out->linesize[0];
303 td.height = out->height;
304 td.width = out->width;
305 context->internal->execute(context, float_to_uint8, &td, NULL, FFMIN(td.height, nb_threads));
307 return ff_filter_frame(outlink, out);
310 static av_cold void uninit(AVFilterContext* context)
312 SRContext* sr_context = context->priv;
314 if (sr_context->dnn_module){
315 (sr_context->dnn_module->free_model)(&sr_context->model);
316 av_freep(&sr_context->dnn_module);
319 if (sr_context->sws_context){
320 sws_freeContext(sr_context->sws_context);
324 static const AVFilterPad sr_inputs[] = {
327 .type = AVMEDIA_TYPE_VIDEO,
328 .config_props = config_props,
329 .filter_frame = filter_frame,
334 static const AVFilterPad sr_outputs[] = {
337 .type = AVMEDIA_TYPE_VIDEO,
342 AVFilter ff_vf_sr = {
344 .description = NULL_IF_CONFIG_SMALL("Apply DNN-based image super resolution to the input."),
345 .priv_size = sizeof(SRContext),
348 .query_formats = query_formats,
350 .outputs = sr_outputs,
351 .priv_class = &sr_class,
352 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,