FFmpeg
Loading...
Searching...
No Matches
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 "filters.h"
30#include "video.h"
31#include "libavutil/opt.h"
32#include "libavutil/pixdesc.h"
33#include "libswscale/swscale.h"
34#include "dnn_filter_common.h"
35
36typedef struct SRContext {
37 const AVClass *class;
43} SRContext;
44
45#define OFFSET(x) offsetof(SRContext, x)
46#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
47static const AVOption sr_options[] = {
48 { "dnn_backend", "DNN backend used for model execution", OFFSET(dnnctx.backend_type), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, .unit = "backend" },
49#if (CONFIG_LIBTENSORFLOW == 1)
50 { "tensorflow", "tensorflow backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, .unit = "backend" },
51#endif
52 { "scale_factor", "scale factor for SRCNN model", OFFSET(scale_factor), AV_OPT_TYPE_INT, { .i64 = 2 }, 2, 4, FLAGS },
53 { NULL }
54};
55
57
58static av_cold int init(AVFilterContext *context)
59{
60 SRContext *sr_context = context->priv;
61 return ff_dnn_init(&sr_context->dnnctx, DFT_PROCESS_FRAME, context);
62}
63
69
70static int config_output(AVFilterLink *outlink)
71{
72 AVFilterContext *context = outlink->src;
73 SRContext *ctx = context->priv;
74 int result;
75 AVFilterLink *inlink = context->inputs[0];
76 int out_width, out_height;
77
78 // have a try run in case that the dnn model resize the frame
79 result = ff_dnn_get_output(&ctx->dnnctx, inlink->w, inlink->h, &out_width, &out_height);
80 if (result != 0) {
81 av_log(ctx, AV_LOG_ERROR, "could not get output from the model\n");
82 return result;
83 }
84
85 if (inlink->w != out_width || inlink->h != out_height) {
86 //espcn
87 outlink->w = out_width;
88 outlink->h = out_height;
89 if (inlink->format != AV_PIX_FMT_GRAY8){
91 int sws_src_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
92 int sws_src_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
93 int sws_dst_h = AV_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h);
94 int sws_dst_w = AV_CEIL_RSHIFT(outlink->w, desc->log2_chroma_w);
95 ctx->sws_uv_scale = sws_getContext(sws_src_w, sws_src_h, AV_PIX_FMT_GRAY8,
96 sws_dst_w, sws_dst_h, AV_PIX_FMT_GRAY8,
98 ctx->sws_uv_height = sws_src_h;
99 }
100 } else {
101 //srcnn
102 outlink->w = out_width * ctx->scale_factor;
103 outlink->h = out_height * ctx->scale_factor;
104 ctx->sws_pre_scale = sws_getContext(inlink->w, inlink->h, inlink->format,
105 outlink->w, outlink->h, outlink->format,
107 }
108
109 return 0;
110}
111
112static int filter_frame(AVFilterLink *inlink, AVFrame *in)
113{
114 DNNAsyncStatusType async_state = 0;
115 AVFilterContext *context = inlink->dst;
116 SRContext *ctx = context->priv;
117 AVFilterLink *outlink = context->outputs[0];
118 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
119 int dnn_result;
120
121 if (!out){
122 av_log(context, AV_LOG_ERROR, "could not allocate memory for output frame\n");
123 av_frame_free(&in);
124 return AVERROR(ENOMEM);
125 }
127
128 if (ctx->sws_pre_scale) {
129 sws_scale(ctx->sws_pre_scale,
130 (const uint8_t **)in->data, in->linesize, 0, in->height,
131 out->data, out->linesize);
132 dnn_result = ff_dnn_execute_model(&ctx->dnnctx, out, out);
133 } else {
134 dnn_result = ff_dnn_execute_model(&ctx->dnnctx, in, out);
135 }
136
137 if (dnn_result != 0){
138 av_log(ctx, AV_LOG_ERROR, "failed to execute loaded model\n");
139 av_frame_free(&in);
141 return dnn_result;
142 }
143
144 do {
145 async_state = ff_dnn_get_result(&ctx->dnnctx, &in, &out);
146 } while (async_state == DAST_NOT_READY);
147
148 if (async_state != DAST_SUCCESS)
149 return AVERROR(EINVAL);
150
151 if (ctx->sws_uv_scale) {
152 sws_scale(ctx->sws_uv_scale, (const uint8_t **)(in->data + 1), in->linesize + 1,
153 0, ctx->sws_uv_height, out->data + 1, out->linesize + 1);
154 sws_scale(ctx->sws_uv_scale, (const uint8_t **)(in->data + 2), in->linesize + 2,
155 0, ctx->sws_uv_height, out->data + 2, out->linesize + 2);
156 }
157 if (in != out) {
158 av_frame_free(&in);
159 }
160 return ff_filter_frame(outlink, out);
161}
162
163static av_cold void uninit(AVFilterContext *context)
164{
165 SRContext *sr_context = context->priv;
166
167 ff_dnn_uninit(&sr_context->dnnctx);
168 sws_freeContext(sr_context->sws_uv_scale);
169 sws_freeContext(sr_context->sws_pre_scale);
170}
171
172static const AVFilterPad sr_inputs[] = {
173 {
174 .name = "default",
175 .type = AVMEDIA_TYPE_VIDEO,
176 .filter_frame = filter_frame,
177 },
178};
179
180static const AVFilterPad sr_outputs[] = {
181 {
182 .name = "default",
183 .config_props = config_output,
184 .type = AVMEDIA_TYPE_VIDEO,
185 },
186};
187
189 .p.name = "sr",
190 .p.description = NULL_IF_CONFIG_SMALL("Apply DNN-based image super resolution to the input."),
191 .p.priv_class = &sr_class,
192 .priv_size = sizeof(SRContext),
194 .init = init,
195 .uninit = uninit,
199};
const FFFilter ff_vf_sr
Definition vf_sr.c:188
static FILE * out
static AVFormatContext * ctx
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 AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#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_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height)
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 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
int attribute_align_arg sws_scale(SwsContext *sws, 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:1626
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:1919
void sws_freeContext(SwsContext *swsContext)
Free the swscaler context swsContext.
Definition utils.c:2250
@ SWS_BICUBIC
2-tap cubic B-spline
Definition swscale.h:199
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#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
const char * desc
Definition libsvtav1.c:83
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
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
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:281
void * priv
private data for use by the filter
Definition avfilter.h:288
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
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
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int height
Definition frame.h:544
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:517
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
DnnContext dnnctx
Definition vf_sr.c:38
int sws_uv_height
Definition vf_sr.c:41
int scale_factor
Definition vf_sr.c:39
struct SwsContext * sws_uv_scale
Definition vf_sr.c:40
struct SwsContext * sws_pre_scale
Definition vf_sr.c:42
Main external API structure.
Definition swscale.h:227
external API header
#define av_log(a,...)
static const AVFilterPad sr_inputs[]
Definition vf_sr.c:172
static av_cold void uninit(AVFilterContext *context)
Definition vf_sr.c:163
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition vf_sr.c:112
static enum AVPixelFormat pixel_formats[]
Definition vf_sr.c:64
#define OFFSET(x)
Definition vf_sr.c:45
static int config_output(AVFilterLink *outlink)
Definition vf_sr.c:70
static const AVOption sr_options[]
Definition vf_sr.c:47
static const AVFilterPad sr_outputs[]
Definition vf_sr.c:180
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