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;
45 DNNModule *dnn_module;
48 // input & output of the model at execution time
52 struct SwsContext *sws_gray8_to_grayf32;
53 struct SwsContext *sws_grayf32_to_gray8;
54 struct SwsContext *sws_uv_scale;
56 } DnnProcessingContext;
58 #define OFFSET(x) offsetof(DnnProcessingContext, x)
59 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
60 static const AVOption dnn_processing_options[] = {
61 { "dnn_backend", "DNN backend", OFFSET(backend_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS, "backend" },
62 { "native", "native backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "backend" },
63 #if (CONFIG_LIBTENSORFLOW == 1)
64 { "tensorflow", "tensorflow backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "backend" },
66 { "model", "path to model file", OFFSET(model_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
67 { "input", "input name of the model", OFFSET(model_inputname), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
68 { "output", "output name of the model", OFFSET(model_outputname), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
72 AVFILTER_DEFINE_CLASS(dnn_processing);
74 static av_cold int init(AVFilterContext *context)
76 DnnProcessingContext *ctx = context->priv;
78 if (!ctx->model_filename) {
79 av_log(ctx, AV_LOG_ERROR, "model file for network is not specified\n");
80 return AVERROR(EINVAL);
82 if (!ctx->model_inputname) {
83 av_log(ctx, AV_LOG_ERROR, "input name of the model network is not specified\n");
84 return AVERROR(EINVAL);
86 if (!ctx->model_outputname) {
87 av_log(ctx, AV_LOG_ERROR, "output name of the model network is not specified\n");
88 return AVERROR(EINVAL);
91 ctx->dnn_module = ff_get_dnn_module(ctx->backend_type);
92 if (!ctx->dnn_module) {
93 av_log(ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
94 return AVERROR(ENOMEM);
96 if (!ctx->dnn_module->load_model) {
97 av_log(ctx, AV_LOG_ERROR, "load_model for network is not specified\n");
98 return AVERROR(EINVAL);
101 ctx->model = (ctx->dnn_module->load_model)(ctx->model_filename);
103 av_log(ctx, AV_LOG_ERROR, "could not load DNN model\n");
104 return AVERROR(EINVAL);
110 static int query_formats(AVFilterContext *context)
112 static const enum AVPixelFormat pix_fmts[] = {
113 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
114 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAYF32,
115 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
116 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
119 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
120 return ff_set_common_formats(context, fmts_list);
123 #define LOG_FORMAT_CHANNEL_MISMATCH() \
124 av_log(ctx, AV_LOG_ERROR, \
125 "the frame's format %s does not match " \
126 "the model input channel %d\n", \
127 av_get_pix_fmt_name(fmt), \
128 model_input->channels);
130 static int check_modelinput_inlink(const DNNData *model_input, const AVFilterLink *inlink)
132 AVFilterContext *ctx = inlink->dst;
133 enum AVPixelFormat fmt = inlink->format;
135 // the design is to add explicit scale filter before this filter
136 if (model_input->height != -1 && model_input->height != inlink->h) {
137 av_log(ctx, AV_LOG_ERROR, "the model requires frame height %d but got %d\n",
138 model_input->height, inlink->h);
141 if (model_input->width != -1 && model_input->width != inlink->w) {
142 av_log(ctx, AV_LOG_ERROR, "the model requires frame width %d but got %d\n",
143 model_input->width, inlink->w);
148 case AV_PIX_FMT_RGB24:
149 case AV_PIX_FMT_BGR24:
150 if (model_input->channels != 3) {
151 LOG_FORMAT_CHANNEL_MISMATCH();
154 if (model_input->dt != DNN_FLOAT && model_input->dt != DNN_UINT8) {
155 av_log(ctx, AV_LOG_ERROR, "only support dnn models with input data type as float32 and uint8.\n");
159 case AV_PIX_FMT_GRAY8:
160 if (model_input->channels != 1) {
161 LOG_FORMAT_CHANNEL_MISMATCH();
164 if (model_input->dt != DNN_UINT8) {
165 av_log(ctx, AV_LOG_ERROR, "only support dnn models with input data type uint8.\n");
169 case AV_PIX_FMT_GRAYF32:
170 case AV_PIX_FMT_YUV420P:
171 case AV_PIX_FMT_YUV422P:
172 case AV_PIX_FMT_YUV444P:
173 case AV_PIX_FMT_YUV410P:
174 case AV_PIX_FMT_YUV411P:
175 if (model_input->channels != 1) {
176 LOG_FORMAT_CHANNEL_MISMATCH();
179 if (model_input->dt != DNN_FLOAT) {
180 av_log(ctx, AV_LOG_ERROR, "only support dnn models with input data type float32.\n");
185 av_log(ctx, AV_LOG_ERROR, "%s not supported.\n", av_get_pix_fmt_name(fmt));
192 static int config_input(AVFilterLink *inlink)
194 AVFilterContext *context = inlink->dst;
195 DnnProcessingContext *ctx = context->priv;
196 DNNReturnType result;
200 result = ctx->model->get_input(ctx->model->model, &model_input, ctx->model_inputname);
201 if (result != DNN_SUCCESS) {
202 av_log(ctx, AV_LOG_ERROR, "could not get input from the model\n");
206 check = check_modelinput_inlink(&model_input, inlink);
211 ctx->input.width = inlink->w;
212 ctx->input.height = inlink->h;
213 ctx->input.channels = model_input.channels;
214 ctx->input.dt = model_input.dt;
216 result = (ctx->model->set_input_output)(ctx->model->model,
217 &ctx->input, ctx->model_inputname,
218 (const char **)&ctx->model_outputname, 1);
219 if (result != DNN_SUCCESS) {
220 av_log(ctx, AV_LOG_ERROR, "could not set input and output for the model\n");
227 static int prepare_sws_context(AVFilterLink *outlink)
229 AVFilterContext *context = outlink->src;
230 DnnProcessingContext *ctx = context->priv;
231 AVFilterLink *inlink = context->inputs[0];
232 enum AVPixelFormat fmt = inlink->format;
233 DNNDataType input_dt = ctx->input.dt;
234 DNNDataType output_dt = ctx->output.dt;
237 case AV_PIX_FMT_RGB24:
238 case AV_PIX_FMT_BGR24:
239 if (input_dt == DNN_FLOAT) {
240 ctx->sws_gray8_to_grayf32 = sws_getContext(inlink->w * 3,
246 0, NULL, NULL, NULL);
248 if (output_dt == DNN_FLOAT) {
249 ctx->sws_grayf32_to_gray8 = sws_getContext(outlink->w * 3,
255 0, NULL, NULL, NULL);
258 case AV_PIX_FMT_YUV420P:
259 case AV_PIX_FMT_YUV422P:
260 case AV_PIX_FMT_YUV444P:
261 case AV_PIX_FMT_YUV410P:
262 case AV_PIX_FMT_YUV411P:
263 av_assert0(input_dt == DNN_FLOAT);
264 av_assert0(output_dt == DNN_FLOAT);
265 ctx->sws_gray8_to_grayf32 = sws_getContext(inlink->w,
271 0, NULL, NULL, NULL);
272 ctx->sws_grayf32_to_gray8 = sws_getContext(outlink->w,
278 0, NULL, NULL, NULL);
280 if (inlink->w != outlink->w || inlink->h != outlink->h) {
281 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
282 int sws_src_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
283 int sws_src_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
284 int sws_dst_h = AV_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h);
285 int sws_dst_w = AV_CEIL_RSHIFT(outlink->w, desc->log2_chroma_w);
286 ctx->sws_uv_scale = sws_getContext(sws_src_w, sws_src_h, AV_PIX_FMT_GRAY8,
287 sws_dst_w, sws_dst_h, AV_PIX_FMT_GRAY8,
288 SWS_BICUBIC, NULL, NULL, NULL);
289 ctx->sws_uv_height = sws_src_h;
300 static int config_output(AVFilterLink *outlink)
302 AVFilterContext *context = outlink->src;
303 DnnProcessingContext *ctx = context->priv;
304 DNNReturnType result;
306 // have a try run in case that the dnn model resize the frame
307 result = (ctx->dnn_module->execute_model)(ctx->model, &ctx->output, 1);
308 if (result != DNN_SUCCESS){
309 av_log(ctx, AV_LOG_ERROR, "failed to execute model\n");
313 outlink->w = ctx->output.width;
314 outlink->h = ctx->output.height;
316 prepare_sws_context(outlink);
321 static int copy_from_frame_to_dnn(DnnProcessingContext *ctx, const AVFrame *frame)
323 int bytewidth = av_image_get_linesize(frame->format, frame->width, 0);
324 DNNData *dnn_input = &ctx->input;
326 switch (frame->format) {
327 case AV_PIX_FMT_RGB24:
328 case AV_PIX_FMT_BGR24:
329 if (dnn_input->dt == DNN_FLOAT) {
330 sws_scale(ctx->sws_gray8_to_grayf32, (const uint8_t **)frame->data, frame->linesize,
331 0, frame->height, (uint8_t * const*)(&dnn_input->data),
332 (const int [4]){frame->linesize[0] * sizeof(float), 0, 0, 0});
334 av_assert0(dnn_input->dt == DNN_UINT8);
335 av_image_copy_plane(dnn_input->data, bytewidth,
336 frame->data[0], frame->linesize[0],
337 bytewidth, frame->height);
340 case AV_PIX_FMT_GRAY8:
341 case AV_PIX_FMT_GRAYF32:
342 av_image_copy_plane(dnn_input->data, bytewidth,
343 frame->data[0], frame->linesize[0],
344 bytewidth, frame->height);
346 case AV_PIX_FMT_YUV420P:
347 case AV_PIX_FMT_YUV422P:
348 case AV_PIX_FMT_YUV444P:
349 case AV_PIX_FMT_YUV410P:
350 case AV_PIX_FMT_YUV411P:
351 sws_scale(ctx->sws_gray8_to_grayf32, (const uint8_t **)frame->data, frame->linesize,
352 0, frame->height, (uint8_t * const*)(&dnn_input->data),
353 (const int [4]){frame->width * sizeof(float), 0, 0, 0});
362 static int copy_from_dnn_to_frame(DnnProcessingContext *ctx, AVFrame *frame)
364 int bytewidth = av_image_get_linesize(frame->format, frame->width, 0);
365 DNNData *dnn_output = &ctx->output;
367 switch (frame->format) {
368 case AV_PIX_FMT_RGB24:
369 case AV_PIX_FMT_BGR24:
370 if (dnn_output->dt == DNN_FLOAT) {
371 sws_scale(ctx->sws_grayf32_to_gray8, (const uint8_t *[4]){(const uint8_t *)dnn_output->data, 0, 0, 0},
372 (const int[4]){frame->linesize[0] * sizeof(float), 0, 0, 0},
373 0, frame->height, (uint8_t * const*)frame->data, frame->linesize);
376 av_assert0(dnn_output->dt == DNN_UINT8);
377 av_image_copy_plane(frame->data[0], frame->linesize[0],
378 dnn_output->data, bytewidth,
379 bytewidth, frame->height);
382 case AV_PIX_FMT_GRAY8:
383 // it is possible that data type of dnn output is float32,
384 // need to add support for such case when needed.
385 av_assert0(dnn_output->dt == DNN_UINT8);
386 av_image_copy_plane(frame->data[0], frame->linesize[0],
387 dnn_output->data, bytewidth,
388 bytewidth, frame->height);
390 case AV_PIX_FMT_GRAYF32:
391 av_assert0(dnn_output->dt == DNN_FLOAT);
392 av_image_copy_plane(frame->data[0], frame->linesize[0],
393 dnn_output->data, bytewidth,
394 bytewidth, frame->height);
396 case AV_PIX_FMT_YUV420P:
397 case AV_PIX_FMT_YUV422P:
398 case AV_PIX_FMT_YUV444P:
399 case AV_PIX_FMT_YUV410P:
400 case AV_PIX_FMT_YUV411P:
401 sws_scale(ctx->sws_grayf32_to_gray8, (const uint8_t *[4]){(const uint8_t *)dnn_output->data, 0, 0, 0},
402 (const int[4]){frame->width * sizeof(float), 0, 0, 0},
403 0, frame->height, (uint8_t * const*)frame->data, frame->linesize);
412 static av_always_inline int isPlanarYUV(enum AVPixelFormat pix_fmt)
414 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
416 return !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components == 3;
419 static int copy_uv_planes(DnnProcessingContext *ctx, AVFrame *out, const AVFrame *in)
421 const AVPixFmtDescriptor *desc;
424 if (!ctx->sws_uv_scale) {
425 av_assert0(in->height == out->height && in->width == out->width);
426 desc = av_pix_fmt_desc_get(in->format);
427 uv_height = AV_CEIL_RSHIFT(in->height, desc->log2_chroma_h);
428 for (int i = 1; i < 3; ++i) {
429 int bytewidth = av_image_get_linesize(in->format, in->width, i);
430 av_image_copy_plane(out->data[i], out->linesize[i],
431 in->data[i], in->linesize[i],
432 bytewidth, uv_height);
435 sws_scale(ctx->sws_uv_scale, (const uint8_t **)(in->data + 1), in->linesize + 1,
436 0, ctx->sws_uv_height, out->data + 1, out->linesize + 1);
437 sws_scale(ctx->sws_uv_scale, (const uint8_t **)(in->data + 2), in->linesize + 2,
438 0, ctx->sws_uv_height, out->data + 2, out->linesize + 2);
444 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
446 AVFilterContext *context = inlink->dst;
447 AVFilterLink *outlink = context->outputs[0];
448 DnnProcessingContext *ctx = context->priv;
449 DNNReturnType dnn_result;
452 copy_from_frame_to_dnn(ctx, in);
454 dnn_result = (ctx->dnn_module->execute_model)(ctx->model, &ctx->output, 1);
455 if (dnn_result != DNN_SUCCESS){
456 av_log(ctx, AV_LOG_ERROR, "failed to execute model\n");
461 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
464 return AVERROR(ENOMEM);
467 av_frame_copy_props(out, in);
468 copy_from_dnn_to_frame(ctx, out);
470 if (isPlanarYUV(in->format))
471 copy_uv_planes(ctx, out, in);
474 return ff_filter_frame(outlink, out);
477 static av_cold void uninit(AVFilterContext *ctx)
479 DnnProcessingContext *context = ctx->priv;
481 sws_freeContext(context->sws_gray8_to_grayf32);
482 sws_freeContext(context->sws_grayf32_to_gray8);
483 sws_freeContext(context->sws_uv_scale);
485 if (context->dnn_module)
486 (context->dnn_module->free_model)(&context->model);
488 av_freep(&context->dnn_module);
491 static const AVFilterPad dnn_processing_inputs[] = {
494 .type = AVMEDIA_TYPE_VIDEO,
495 .config_props = config_input,
496 .filter_frame = filter_frame,
501 static const AVFilterPad dnn_processing_outputs[] = {
504 .type = AVMEDIA_TYPE_VIDEO,
505 .config_props = config_output,
510 AVFilter ff_vf_dnn_processing = {
511 .name = "dnn_processing",
512 .description = NULL_IF_CONFIG_SMALL("Apply DNN processing filter to the input."),
513 .priv_size = sizeof(DnnProcessingContext),
516 .query_formats = query_formats,
517 .inputs = dnn_processing_inputs,
518 .outputs = dnn_processing_outputs,
519 .priv_class = &dnn_processing_class,