FFmpeg
Loading...
Searching...
No Matches
vf_derain.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Xuewei Meng
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 derain filter using deep convolutional networks.
24 * http://openaccess.thecvf.com/content_ECCV_2018/html/Xia_Li_Recurrent_Squeeze-and-Excitation_Context_ECCV_2018_paper.html
25 */
26
27#include "libavutil/opt.h"
28#include "avfilter.h"
29#include "dnn_filter_common.h"
30#include "filters.h"
31#include "video.h"
32
33typedef struct DRContext {
34 const AVClass *class;
37} DRContext;
38
39#define OFFSET(x) offsetof(DRContext, x)
40#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
41static const AVOption derain_options[] = {
42 { "filter_type", "filter type(derain/dehaze)", OFFSET(filter_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS, .unit = "type" },
43 { "derain", "derain filter flag", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, .unit = "type" },
44 { "dehaze", "dehaze filter flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, .unit = "type" },
45 { "dnn_backend", "DNN backend", OFFSET(dnnctx.backend_type), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, .unit = "backend" },
46#if (CONFIG_LIBTENSORFLOW == 1)
47 { "tensorflow", "tensorflow backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, .unit = "backend" },
48#endif
49 { NULL }
50};
51
53
54static int filter_frame(AVFilterLink *inlink, AVFrame *in)
55{
56 DNNAsyncStatusType async_state = 0;
57 AVFilterContext *ctx = inlink->dst;
58 AVFilterLink *outlink = ctx->outputs[0];
59 DRContext *dr_context = ctx->priv;
60 int dnn_result;
61 AVFrame *out;
62
63 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
64 if (!out) {
65 av_log(ctx, AV_LOG_ERROR, "could not allocate memory for output frame\n");
66 av_frame_free(&in);
67 return AVERROR(ENOMEM);
68 }
70
71 dnn_result = ff_dnn_execute_model(&dr_context->dnnctx, in, out);
72 if (dnn_result != 0){
73 av_log(ctx, AV_LOG_ERROR, "failed to execute model\n");
74 av_frame_free(&in);
75 return dnn_result;
76 }
77 do {
78 async_state = ff_dnn_get_result(&dr_context->dnnctx, &in, &out);
79 } while (async_state == DAST_NOT_READY);
80
81 if (async_state != DAST_SUCCESS)
82 return AVERROR(EINVAL);
83
84 av_frame_free(&in);
85
86 return ff_filter_frame(outlink, out);
87}
88
90{
91 DRContext *dr_context = ctx->priv;
92 return ff_dnn_init(&dr_context->dnnctx, DFT_PROCESS_FRAME, ctx);
93}
94
96{
97 DRContext *dr_context = ctx->priv;
98 ff_dnn_uninit(&dr_context->dnnctx);
99}
100
101static const AVFilterPad derain_inputs[] = {
102 {
103 .name = "default",
104 .type = AVMEDIA_TYPE_VIDEO,
105 .filter_frame = filter_frame,
106 },
107};
108
110 .p.name = "derain",
111 .p.description = NULL_IF_CONFIG_SMALL("Apply derain filter to the input."),
112 .p.priv_class = &derain_class,
114 .priv_size = sizeof(DRContext),
116 .init = init,
117 .uninit = uninit,
121};
const FFFilter ff_vf_derain
Definition vf_derain.c:109
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
void ff_dnn_uninit(DnnContext *ctx)
DNNAsyncStatusType ff_dnn_get_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame)
int ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame)
int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
int ff_dnn_filter_init_child_class(AVFilterContext *filter)
common functions for the dnn based filters
#define AVFILTER_DNN_DEFINE_CLASS(fname, backend_mask)
DNNAsyncStatusType
@ DAST_NOT_READY
@ DAST_SUCCESS
@ DNN_TF
@ DFT_PROCESS_FRAME
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition filters.h:254
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
AVOptions.
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
static av_cold int preinit(AVBitStreamFilterContext *ctx)
Definition source.c:137
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
int filter_type
Definition vf_derain.c:36
DnnContext dnnctx
Definition vf_derain.c:35
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition vf_derain.c:54
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_derain.c:95
#define OFFSET(x)
Definition vf_derain.c:39
static const AVFilterPad derain_inputs[]
Definition vf_derain.c:101
static const AVOption derain_options[]
Definition vf_derain.c:41
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89