FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_sr.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 0 66 0.0%
Branches: 0 22 0.0%

Line Branch Exec Source
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 * https://arxiv.org/abs/1609.05158
26 */
27
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "internal.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavformat/avio.h"
34 #include "libswscale/swscale.h"
35 #include "dnn_filter_common.h"
36
37 typedef struct SRContext {
38 const AVClass *class;
39 DnnContext dnnctx;
40 int scale_factor;
41 struct SwsContext *sws_uv_scale;
42 int sws_uv_height;
43 struct SwsContext *sws_pre_scale;
44 } SRContext;
45
46 #define OFFSET(x) offsetof(SRContext, x)
47 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
48 static const AVOption sr_options[] = {
49 { "dnn_backend", "DNN backend used for model execution", OFFSET(dnnctx.backend_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS, "backend" },
50 { "native", "native backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "backend" },
51 #if (CONFIG_LIBTENSORFLOW == 1)
52 { "tensorflow", "tensorflow backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "backend" },
53 #endif
54 { "scale_factor", "scale factor for SRCNN model", OFFSET(scale_factor), AV_OPT_TYPE_INT, { .i64 = 2 }, 2, 4, FLAGS },
55 { "model", "path to model file specifying network architecture and its parameters", OFFSET(dnnctx.model_filename), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
56 { "input", "input name of the model", OFFSET(dnnctx.model_inputname), AV_OPT_TYPE_STRING, { .str = "x" }, 0, 0, FLAGS },
57 { "output", "output name of the model", OFFSET(dnnctx.model_outputnames_string), AV_OPT_TYPE_STRING, { .str = "y" }, 0, 0, FLAGS },
58 { NULL }
59 };
60
61 AVFILTER_DEFINE_CLASS(sr);
62
63 static av_cold int init(AVFilterContext *context)
64 {
65 SRContext *sr_context = context->priv;
66 return ff_dnn_init(&sr_context->dnnctx, DFT_PROCESS_FRAME, context);
67 }
68
69 static const enum AVPixelFormat pixel_formats[] = {
70 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
71 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
72 AV_PIX_FMT_NONE
73 };
74
75 static int config_output(AVFilterLink *outlink)
76 {
77 AVFilterContext *context = outlink->src;
78 SRContext *ctx = context->priv;
79 int result;
80 AVFilterLink *inlink = context->inputs[0];
81 int out_width, out_height;
82
83 // have a try run in case that the dnn model resize the frame
84 result = ff_dnn_get_output(&ctx->dnnctx, inlink->w, inlink->h, &out_width, &out_height);
85 if (result != 0) {
86 av_log(ctx, AV_LOG_ERROR, "could not get output from the model\n");
87 return result;
88 }
89
90 if (inlink->w != out_width || inlink->h != out_height) {
91 //espcn
92 outlink->w = out_width;
93 outlink->h = out_height;
94 if (inlink->format != AV_PIX_FMT_GRAY8){
95 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
96 int sws_src_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
97 int sws_src_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
98 int sws_dst_h = AV_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h);
99 int sws_dst_w = AV_CEIL_RSHIFT(outlink->w, desc->log2_chroma_w);
100 ctx->sws_uv_scale = sws_getContext(sws_src_w, sws_src_h, AV_PIX_FMT_GRAY8,
101 sws_dst_w, sws_dst_h, AV_PIX_FMT_GRAY8,
102 SWS_BICUBIC, NULL, NULL, NULL);
103 ctx->sws_uv_height = sws_src_h;
104 }
105 } else {
106 //srcnn
107 outlink->w = out_width * ctx->scale_factor;
108 outlink->h = out_height * ctx->scale_factor;
109 ctx->sws_pre_scale = sws_getContext(inlink->w, inlink->h, inlink->format,
110 outlink->w, outlink->h, outlink->format,
111 SWS_BICUBIC, NULL, NULL, NULL);
112 }
113
114 return 0;
115 }
116
117 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
118 {
119 DNNAsyncStatusType async_state = 0;
120 AVFilterContext *context = inlink->dst;
121 SRContext *ctx = context->priv;
122 AVFilterLink *outlink = context->outputs[0];
123 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
124 int dnn_result;
125
126 if (!out){
127 av_log(context, AV_LOG_ERROR, "could not allocate memory for output frame\n");
128 av_frame_free(&in);
129 return AVERROR(ENOMEM);
130 }
131 av_frame_copy_props(out, in);
132
133 if (ctx->sws_pre_scale) {
134 sws_scale(ctx->sws_pre_scale,
135 (const uint8_t **)in->data, in->linesize, 0, in->height,
136 out->data, out->linesize);
137 dnn_result = ff_dnn_execute_model(&ctx->dnnctx, out, out);
138 } else {
139 dnn_result = ff_dnn_execute_model(&ctx->dnnctx, in, out);
140 }
141
142 if (dnn_result != 0){
143 av_log(ctx, AV_LOG_ERROR, "failed to execute loaded model\n");
144 av_frame_free(&in);
145 av_frame_free(&out);
146 return dnn_result;
147 }
148
149 do {
150 async_state = ff_dnn_get_result(&ctx->dnnctx, &in, &out);
151 } while (async_state == DAST_NOT_READY);
152
153 if (async_state != DAST_SUCCESS)
154 return AVERROR(EINVAL);
155
156 if (ctx->sws_uv_scale) {
157 sws_scale(ctx->sws_uv_scale, (const uint8_t **)(in->data + 1), in->linesize + 1,
158 0, ctx->sws_uv_height, out->data + 1, out->linesize + 1);
159 sws_scale(ctx->sws_uv_scale, (const uint8_t **)(in->data + 2), in->linesize + 2,
160 0, ctx->sws_uv_height, out->data + 2, out->linesize + 2);
161 }
162 if (in != out) {
163 av_frame_free(&in);
164 }
165 return ff_filter_frame(outlink, out);
166 }
167
168 static av_cold void uninit(AVFilterContext *context)
169 {
170 SRContext *sr_context = context->priv;
171
172 ff_dnn_uninit(&sr_context->dnnctx);
173 sws_freeContext(sr_context->sws_uv_scale);
174 sws_freeContext(sr_context->sws_pre_scale);
175 }
176
177 static const AVFilterPad sr_inputs[] = {
178 {
179 .name = "default",
180 .type = AVMEDIA_TYPE_VIDEO,
181 .filter_frame = filter_frame,
182 },
183 };
184
185 static const AVFilterPad sr_outputs[] = {
186 {
187 .name = "default",
188 .config_props = config_output,
189 .type = AVMEDIA_TYPE_VIDEO,
190 },
191 };
192
193 const AVFilter ff_vf_sr = {
194 .name = "sr",
195 .description = NULL_IF_CONFIG_SMALL("Apply DNN-based image super resolution to the input."),
196 .priv_size = sizeof(SRContext),
197 .init = init,
198 .uninit = uninit,
199 FILTER_INPUTS(sr_inputs),
200 FILTER_OUTPUTS(sr_outputs),
201 FILTER_PIXFMTS_ARRAY(pixel_formats),
202 .priv_class = &sr_class,
203 };
204