diff options
author | Guo, Yejun <yejun.guo@intel.com> | 2020-08-28 12:51:44 +0800 |
---|---|---|
committer | Guo, Yejun <yejun.guo@intel.com> | 2020-09-21 21:26:56 +0800 |
commit | 2003e32f62d94ba75b59d70632c9f2862b383591 (patch) | |
tree | 55ec60788bc740eb45dbafd613bd8cf50a10417a /libavfilter/vf_derain.c | |
parent | 6918e240d706f7390272976d8b8d502afe426a18 (diff) |
dnn: change dnn interface to replace DNNData* with AVFrame*
Currently, every filter needs to provide code to transfer data from
AVFrame* to model input (DNNData*), and also from model output
(DNNData*) to AVFrame*. Actually, such transfer can be implemented
within DNN module, and so filter can focus on its own business logic.
DNN module also exports the function pointer pre_proc and post_proc
in struct DNNModel, just in case that a filter has its special logic
to transfer data between AVFrame* and DNNData*. The default implementation
within DNN module is used if the filter does not set pre/post_proc.
Diffstat (limited to 'libavfilter/vf_derain.c')
-rw-r--r-- | libavfilter/vf_derain.c | 59 |
1 files changed, 11 insertions, 48 deletions
diff --git a/libavfilter/vf_derain.c b/libavfilter/vf_derain.c index c251d55ee7..a59cd6e941 100644 --- a/libavfilter/vf_derain.c +++ b/libavfilter/vf_derain.c @@ -39,11 +39,8 @@ typedef struct DRContext { DNNBackendType backend_type; DNNModule *dnn_module; DNNModel *model; - DNNData input; - DNNData output; } DRContext; -#define CLIP(x, min, max) (x < min ? min : (x > max ? max : x)) #define OFFSET(x) offsetof(DRContext, x) #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM static const AVOption derain_options[] = { @@ -74,25 +71,6 @@ static int query_formats(AVFilterContext *ctx) return ff_set_common_formats(ctx, formats); } -static int config_inputs(AVFilterLink *inlink) -{ - AVFilterContext *ctx = inlink->dst; - DRContext *dr_context = ctx->priv; - DNNReturnType result; - - dr_context->input.width = inlink->w; - dr_context->input.height = inlink->h; - dr_context->input.channels = 3; - - result = (dr_context->model->set_input)(dr_context->model->model, &dr_context->input, "x"); - if (result != DNN_SUCCESS) { - av_log(ctx, AV_LOG_ERROR, "could not set input and output for the model\n"); - return AVERROR(EIO); - } - - return 0; -} - static int filter_frame(AVFilterLink *inlink, AVFrame *in) { AVFilterContext *ctx = inlink->dst; @@ -100,43 +78,30 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) DRContext *dr_context = ctx->priv; DNNReturnType dnn_result; const char *model_output_name = "y"; + AVFrame *out; - AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + dnn_result = (dr_context->model->set_input)(dr_context->model->model, in, "x"); + if (dnn_result != DNN_SUCCESS) { + av_log(ctx, AV_LOG_ERROR, "could not set input for the model\n"); + av_frame_free(&in); + return AVERROR(EIO); + } + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); if (!out) { av_log(ctx, AV_LOG_ERROR, "could not allocate memory for output frame\n"); av_frame_free(&in); return AVERROR(ENOMEM); } - av_frame_copy_props(out, in); - for (int i = 0; i < in->height; i++){ - for(int j = 0; j < in->width * 3; j++){ - int k = i * in->linesize[0] + j; - int t = i * in->width * 3 + j; - ((float *)dr_context->input.data)[t] = in->data[0][k] / 255.0; - } - } - - dnn_result = (dr_context->dnn_module->execute_model)(dr_context->model, &dr_context->output, &model_output_name, 1); + dnn_result = (dr_context->dnn_module->execute_model)(dr_context->model, &model_output_name, 1, out); if (dnn_result != DNN_SUCCESS){ av_log(ctx, AV_LOG_ERROR, "failed to execute model\n"); + av_frame_free(&in); return AVERROR(EIO); } - out->height = dr_context->output.height; - out->width = dr_context->output.width; - outlink->h = dr_context->output.height; - outlink->w = dr_context->output.width; - - for (int i = 0; i < out->height; i++){ - for(int j = 0; j < out->width * 3; j++){ - int k = i * out->linesize[0] + j; - int t = i * out->width * 3 + j; - out->data[0][k] = CLIP((int)((((float *)dr_context->output.data)[t]) * 255), 0, 255); - } - } - av_frame_free(&in); return ff_filter_frame(outlink, out); @@ -146,7 +111,6 @@ static av_cold int init(AVFilterContext *ctx) { DRContext *dr_context = ctx->priv; - dr_context->input.dt = DNN_FLOAT; dr_context->dnn_module = ff_get_dnn_module(dr_context->backend_type); if (!dr_context->dnn_module) { av_log(ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n"); @@ -184,7 +148,6 @@ static const AVFilterPad derain_inputs[] = { { .name = "default", .type = AVMEDIA_TYPE_VIDEO, - .config_props = config_inputs, .filter_frame = filter_frame, }, { NULL } |