FFmpeg
vf_sr.c
Go to the documentation of this file.
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;
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 
62 
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[] = {
73 };
74 
75 static int config_output(AVFilterLink *outlink)
76 {
77  AVFilterContext *context = outlink->src;
78  SRContext *ctx = context->priv;
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 != DNN_SUCCESS) {
86  av_log(ctx, AV_LOG_ERROR, "could not get output from the model\n");
87  return AVERROR(EIO);
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){
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,
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,
112  }
113 
114  return 0;
115 }
116 
118 {
119  DNNAsyncStatusType async_state = 0;
121  SRContext *ctx = context->priv;
122  AVFilterLink *outlink = context->outputs[0];
123  AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
124  DNNReturnType 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  }
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 != DNN_SUCCESS){
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 AVERROR(EIO);
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 
163  av_frame_free(&in);
164  return ff_filter_frame(outlink, out);
165 }
166 
168 {
169  SRContext *sr_context = context->priv;
170 
171  ff_dnn_uninit(&sr_context->dnnctx);
172  sws_freeContext(sr_context->sws_uv_scale);
173  sws_freeContext(sr_context->sws_pre_scale);
174 }
175 
176 static const AVFilterPad sr_inputs[] = {
177  {
178  .name = "default",
179  .type = AVMEDIA_TYPE_VIDEO,
180  .filter_frame = filter_frame,
181  },
182 };
183 
184 static const AVFilterPad sr_outputs[] = {
185  {
186  .name = "default",
187  .config_props = config_output,
188  .type = AVMEDIA_TYPE_VIDEO,
189  },
190 };
191 
193  .name = "sr",
194  .description = NULL_IF_CONFIG_SMALL("Apply DNN-based image super resolution to the input."),
195  .priv_size = sizeof(SRContext),
196  .init = init,
197  .uninit = uninit,
201  .priv_class = &sr_class,
202 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:98
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
SRContext::dnnctx
DnnContext dnnctx
Definition: vf_sr.c:39
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_dnn_get_output
DNNReturnType ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height)
Definition: dnn_filter_common.c:114
sr_outputs
static const AVFilterPad sr_outputs[]
Definition: vf_sr.c:184
out
FILE * out
Definition: movenc.c:54
sr_options
static const AVOption sr_options[]
Definition: vf_sr.c:48
SRContext::sws_pre_scale
struct SwsContext * sws_pre_scale
Definition: vf_sr.c:43
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2660
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:171
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
pixdesc.h
AVOption
AVOption.
Definition: opt.h:247
sws_scale
int attribute_align_arg sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
swscale wrapper, so we don't need to export the SwsContext.
Definition: swscale.c:1204
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
SRContext
Definition: af_acrusher.c:35
dnn_filter_common.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:338
formats.h
uninit
static av_cold void uninit(AVFilterContext *context)
Definition: vf_sr.c:167
DnnContext
Definition: dnn_filter_common.h:29
DNN_SUCCESS
@ DNN_SUCCESS
Definition: dnn_interface.h:33
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
init
static av_cold int init(AVFilterContext *context)
Definition: vf_sr.c:63
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:51
FLAGS
#define FLAGS
Definition: vf_sr.c:47
DNNReturnType
DNNReturnType
Definition: dnn_interface.h:33
ff_vf_sr
const AVFilter ff_vf_sr
Definition: vf_sr.c:192
ff_dnn_get_result
DNNAsyncStatusType ff_dnn_get_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame)
Definition: dnn_filter_common.c:147
ctx
AVFormatContext * ctx
Definition: movenc.c:48
pixel_formats
static enum AVPixelFormat pixel_formats[]
Definition: vf_sr.c:69
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(sr)
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
context
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Definition: writing_filters.txt:91
OFFSET
#define OFFSET(x)
Definition: vf_sr.c:46
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:537
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
SRContext::sws_uv_scale
struct SwsContext * sws_uv_scale
Definition: vf_sr.c:41
SRContext::sws_uv_height
int sws_uv_height
Definition: vf_sr.c:42
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
DAST_SUCCESS
@ DAST_SUCCESS
Definition: dnn_interface.h:49
sws_getContext
struct SwsContext * sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
Definition: utils.c:2052
avio.h
internal.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_sr.c:117
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:165
SRContext::scale_factor
int scale_factor
Definition: vf_sr.c:40
AVFrame::height
int height
Definition: frame.h:389
sws_freeContext
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:2383
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
ff_dnn_execute_model
DNNReturnType ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame)
Definition: dnn_filter_common.c:120
sr_inputs
static const AVFilterPad sr_inputs[]
Definition: vf_sr.c:176
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
ff_dnn_init
int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
Definition: dnn_filter_common.c:54
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:362
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_dnn_uninit
void ff_dnn_uninit(DnnContext *ctx)
Definition: dnn_filter_common.c:157
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
DAST_NOT_READY
@ DAST_NOT_READY
Definition: dnn_interface.h:48
SwsContext
Definition: swscale_internal.h:300
DNNAsyncStatusType
DNNAsyncStatusType
Definition: dnn_interface.h:45
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
DFT_PROCESS_FRAME
@ DFT_PROCESS_FRAME
Definition: dnn_interface.h:54
SWS_BICUBIC
#define SWS_BICUBIC
Definition: swscale.h:61
swscale.h
config_output
static int config_output(AVFilterLink *outlink)
Definition: vf_sr.c:75