2 * Copyright (c) 2019 Guo Yejun
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 * implementing a generic image processing filter using deep learning networks.
26 #include "libavformat/avio.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/imgutils.h"
32 #include "dnn_interface.h"
35 #include "libswscale/swscale.h"
37 typedef struct DnnProcessingContext {
41 DNNBackendType backend_type;
42 char *model_inputname;
43 char *model_outputname;
44 char *backend_options;
46 DNNModule *dnn_module;
49 struct SwsContext *sws_uv_scale;
51 } DnnProcessingContext;
53 #define OFFSET(x) offsetof(DnnProcessingContext, x)
54 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
55 static const AVOption dnn_processing_options[] = {
56 { "dnn_backend", "DNN backend", OFFSET(backend_type), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "backend" },
57 { "native", "native backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "backend" },
58 #if (CONFIG_LIBTENSORFLOW == 1)
59 { "tensorflow", "tensorflow backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "backend" },
61 #if (CONFIG_LIBOPENVINO == 1)
62 { "openvino", "openvino backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "backend" },
64 { "model", "path to model file", OFFSET(model_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
65 { "input", "input name of the model", OFFSET(model_inputname), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
66 { "output", "output name of the model", OFFSET(model_outputname), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
67 { "options", "backend options", OFFSET(backend_options), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
71 AVFILTER_DEFINE_CLASS(dnn_processing);
73 static av_cold int init(AVFilterContext *context)
75 DnnProcessingContext *ctx = context->priv;
77 if (!ctx->model_filename) {
78 av_log(ctx, AV_LOG_ERROR, "model file for network is not specified\n");
79 return AVERROR(EINVAL);
81 if (!ctx->model_inputname) {
82 av_log(ctx, AV_LOG_ERROR, "input name of the model network is not specified\n");
83 return AVERROR(EINVAL);
85 if (!ctx->model_outputname) {
86 av_log(ctx, AV_LOG_ERROR, "output name of the model network is not specified\n");
87 return AVERROR(EINVAL);
90 ctx->dnn_module = ff_get_dnn_module(ctx->backend_type);
91 if (!ctx->dnn_module) {
92 av_log(ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
93 return AVERROR(ENOMEM);
95 if (!ctx->dnn_module->load_model) {
96 av_log(ctx, AV_LOG_ERROR, "load_model for network is not specified\n");
97 return AVERROR(EINVAL);
100 ctx->model = (ctx->dnn_module->load_model)(ctx->model_filename, ctx->backend_options, ctx);
102 av_log(ctx, AV_LOG_ERROR, "could not load DNN model\n");
103 return AVERROR(EINVAL);
109 static int query_formats(AVFilterContext *context)
111 static const enum AVPixelFormat pix_fmts[] = {
112 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
113 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAYF32,
114 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
115 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
118 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
119 return ff_set_common_formats(context, fmts_list);
122 #define LOG_FORMAT_CHANNEL_MISMATCH() \
123 av_log(ctx, AV_LOG_ERROR, \
124 "the frame's format %s does not match " \
125 "the model input channel %d\n", \
126 av_get_pix_fmt_name(fmt), \
127 model_input->channels);
129 static int check_modelinput_inlink(const DNNData *model_input, const AVFilterLink *inlink)
131 AVFilterContext *ctx = inlink->dst;
132 enum AVPixelFormat fmt = inlink->format;
134 // the design is to add explicit scale filter before this filter
135 if (model_input->height != -1 && model_input->height != inlink->h) {
136 av_log(ctx, AV_LOG_ERROR, "the model requires frame height %d but got %d\n",
137 model_input->height, inlink->h);
140 if (model_input->width != -1 && model_input->width != inlink->w) {
141 av_log(ctx, AV_LOG_ERROR, "the model requires frame width %d but got %d\n",
142 model_input->width, inlink->w);
145 if (model_input->dt != DNN_FLOAT) {
146 av_log(ctx, AV_LOG_ERROR, "only support dnn models with input data type as float32.\n");
151 case AV_PIX_FMT_RGB24:
152 case AV_PIX_FMT_BGR24:
153 if (model_input->channels != 3) {
154 LOG_FORMAT_CHANNEL_MISMATCH();
158 case AV_PIX_FMT_GRAYF32:
159 case AV_PIX_FMT_YUV420P:
160 case AV_PIX_FMT_YUV422P:
161 case AV_PIX_FMT_YUV444P:
162 case AV_PIX_FMT_YUV410P:
163 case AV_PIX_FMT_YUV411P:
164 if (model_input->channels != 1) {
165 LOG_FORMAT_CHANNEL_MISMATCH();
170 av_log(ctx, AV_LOG_ERROR, "%s not supported.\n", av_get_pix_fmt_name(fmt));
177 static int config_input(AVFilterLink *inlink)
179 AVFilterContext *context = inlink->dst;
180 DnnProcessingContext *ctx = context->priv;
181 DNNReturnType result;
185 result = ctx->model->get_input(ctx->model->model, &model_input, ctx->model_inputname);
186 if (result != DNN_SUCCESS) {
187 av_log(ctx, AV_LOG_ERROR, "could not get input from the model\n");
191 check = check_modelinput_inlink(&model_input, inlink);
199 static av_always_inline int isPlanarYUV(enum AVPixelFormat pix_fmt)
201 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
203 return !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components == 3;
206 static int prepare_uv_scale(AVFilterLink *outlink)
208 AVFilterContext *context = outlink->src;
209 DnnProcessingContext *ctx = context->priv;
210 AVFilterLink *inlink = context->inputs[0];
211 enum AVPixelFormat fmt = inlink->format;
213 if (isPlanarYUV(fmt)) {
214 if (inlink->w != outlink->w || inlink->h != outlink->h) {
215 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
216 int sws_src_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
217 int sws_src_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
218 int sws_dst_h = AV_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h);
219 int sws_dst_w = AV_CEIL_RSHIFT(outlink->w, desc->log2_chroma_w);
220 ctx->sws_uv_scale = sws_getContext(sws_src_w, sws_src_h, AV_PIX_FMT_GRAY8,
221 sws_dst_w, sws_dst_h, AV_PIX_FMT_GRAY8,
222 SWS_BICUBIC, NULL, NULL, NULL);
223 ctx->sws_uv_height = sws_src_h;
230 static int config_output(AVFilterLink *outlink)
232 AVFilterContext *context = outlink->src;
233 DnnProcessingContext *ctx = context->priv;
234 DNNReturnType result;
235 AVFilterLink *inlink = context->inputs[0];
238 AVFrame *fake_in = ff_get_video_buffer(inlink, inlink->w, inlink->h);
240 // have a try run in case that the dnn model resize the frame
241 out = ff_get_video_buffer(inlink, inlink->w, inlink->h);
242 result = (ctx->dnn_module->execute_model)(ctx->model, ctx->model_inputname, fake_in,
243 (const char **)&ctx->model_outputname, 1, out);
244 if (result != DNN_SUCCESS){
245 av_log(ctx, AV_LOG_ERROR, "failed to execute model\n");
249 outlink->w = out->width;
250 outlink->h = out->height;
252 av_frame_free(&fake_in);
254 prepare_uv_scale(outlink);
259 static int copy_uv_planes(DnnProcessingContext *ctx, AVFrame *out, const AVFrame *in)
261 const AVPixFmtDescriptor *desc;
264 if (!ctx->sws_uv_scale) {
265 av_assert0(in->height == out->height && in->width == out->width);
266 desc = av_pix_fmt_desc_get(in->format);
267 uv_height = AV_CEIL_RSHIFT(in->height, desc->log2_chroma_h);
268 for (int i = 1; i < 3; ++i) {
269 int bytewidth = av_image_get_linesize(in->format, in->width, i);
270 av_image_copy_plane(out->data[i], out->linesize[i],
271 in->data[i], in->linesize[i],
272 bytewidth, uv_height);
275 sws_scale(ctx->sws_uv_scale, (const uint8_t **)(in->data + 1), in->linesize + 1,
276 0, ctx->sws_uv_height, out->data + 1, out->linesize + 1);
277 sws_scale(ctx->sws_uv_scale, (const uint8_t **)(in->data + 2), in->linesize + 2,
278 0, ctx->sws_uv_height, out->data + 2, out->linesize + 2);
284 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
286 AVFilterContext *context = inlink->dst;
287 AVFilterLink *outlink = context->outputs[0];
288 DnnProcessingContext *ctx = context->priv;
289 DNNReturnType dnn_result;
292 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
295 return AVERROR(ENOMEM);
297 av_frame_copy_props(out, in);
299 dnn_result = (ctx->dnn_module->execute_model)(ctx->model, ctx->model_inputname, in,
300 (const char **)&ctx->model_outputname, 1, out);
301 if (dnn_result != DNN_SUCCESS){
302 av_log(ctx, AV_LOG_ERROR, "failed to execute model\n");
308 if (isPlanarYUV(in->format))
309 copy_uv_planes(ctx, out, in);
312 return ff_filter_frame(outlink, out);
315 static av_cold void uninit(AVFilterContext *ctx)
317 DnnProcessingContext *context = ctx->priv;
319 sws_freeContext(context->sws_uv_scale);
321 if (context->dnn_module)
322 (context->dnn_module->free_model)(&context->model);
324 av_freep(&context->dnn_module);
327 static const AVFilterPad dnn_processing_inputs[] = {
330 .type = AVMEDIA_TYPE_VIDEO,
331 .config_props = config_input,
332 .filter_frame = filter_frame,
337 static const AVFilterPad dnn_processing_outputs[] = {
340 .type = AVMEDIA_TYPE_VIDEO,
341 .config_props = config_output,
346 AVFilter ff_vf_dnn_processing = {
347 .name = "dnn_processing",
348 .description = NULL_IF_CONFIG_SMALL("Apply DNN processing filter to the input."),
349 .priv_size = sizeof(DnnProcessingContext),
352 .query_formats = query_formats,
353 .inputs = dnn_processing_inputs,
354 .outputs = dnn_processing_outputs,
355 .priv_class = &dnn_processing_class,