History

lavfi: addroi filter

This can be used to add region of interest side data to video frames.

Mark Thompson committed 7 years ago

Commit 20fed2f1 parent 33871474 files · +344 −0Browse files

Unified · Split

4 files
  1. doc/filters.texi +73 −0
  2. libavfilter/Makefile +1 −0
  3. libavfilter/allfilters.c +1 −0
  4. libavfilter/vf_addroi.c +269 −0
doc/filters.texi+73−0
@@ -5931,6 +5931,79 @@ build. · 5930 unmodified lines
5931 5931
5932Below is a description of the currently available video filters.5932Below is a description of the currently available video filters.
5933 5933
5934@section addroi
5935
5936Mark a region of interest in a video frame.
5937
5938The frame data is passed through unchanged, but metadata is attached
5939to the frame indicating regions of interest which can affect the
5940behaviour of later encoding. Multiple regions can be marked by
5941applying the filter multiple times.
5942
5943@table @option
5944@item x
5945Region distance in pixels from the left edge of the frame.
5946@item y
5947Region distance in pixels from the top edge of the frame.
5948@item w
5949Region width in pixels.
5950@item h
5951Region height in pixels.
5952
5953The parameters @var{x}, @var{y}, @var{w} and @var{h} are expressions,
5954and may contain the following variables:
5955@table @option
5956@item iw
5957Width of the input frame.
5958@item ih
5959Height of the input frame.
5960@end table
5961
5962@item qoffset
5963Quantisation offset to apply within the region.
5964
5965This must be a real value in the range -1 to +1. A value of zero
5966indicates no quality change. A negative value asks for better quality
5967(less quantisation), while a positive value asks for worse quality
5968(greater quantisation).
5969
5970The range is calibrated so that the extreme values indicate the
5971largest possible offset - if the rest of the frame is encoded with the
5972worst possible quality, an offset of -1 indicates that this region
5973should be encoded with the best possible quality anyway. Intermediate
5974values are then interpolated in some codec-dependent way.
5975
5976For example, in 10-bit H.264 the quantisation parameter varies between
5977-12 and 51. A typical qoffset value of -1/10 therefore indicates that
5978this region should be encoded with a QP around one-tenth of the full
5979range better than the rest of the frame. So, if most of the frame
5980were to be encoded with a QP of around 30, this region would get a QP
5981of around 24 (an offset of approximately -1/10 * (51 - -12) = -6.3).
5982An extreme value of -1 would indicate that this region should be
5983encoded with the best possible quality regardless of the treatment of
5984the rest of the frame - that is, should be encoded at a QP of -12.
5985@item clear
5986If set to true, remove any existing regions of interest marked on the
5987frame before adding the new one.
5988@end table
5989
5990@subsection Examples
5991
5992@itemize
5993@item
5994Mark the centre quarter of the frame as interesting.
5995@example
5996addroi=iw/4:ih/4:iw/2:ih/2:-1/10
5997@end example
5998@item
5999Mark the 100-pixel-wide region on the left edge of the frame as very
6000uninteresting (to be encoded at much lower quality than the rest of
6001the frame).
6002@example
6003addroi=0:0:100:ih:+1/5
6004@end example
6005@end itemize
6006
5934@section alphaextract6007@section alphaextract
5935 6008
5936Extract the alpha component from the input as a grayscale video. This6009Extract the alpha component from the input as a grayscale video. This
libavfilter/Makefile+1−0
@@ -151,6 +151,7 @@ OBJS-$(CONFIG_SINE_FILTER) += asrc_sine.o · 150 unmodified lines
151OBJS-$(CONFIG_ANULLSINK_FILTER) += asink_anullsink.o151OBJS-$(CONFIG_ANULLSINK_FILTER) += asink_anullsink.o
152 152
153# video filters153# video filters
154OBJS-$(CONFIG_ADDROI_FILTER) += vf_addroi.o
154OBJS-$(CONFIG_ALPHAEXTRACT_FILTER) += vf_extractplanes.o155OBJS-$(CONFIG_ALPHAEXTRACT_FILTER) += vf_extractplanes.o
155OBJS-$(CONFIG_ALPHAMERGE_FILTER) += vf_alphamerge.o156OBJS-$(CONFIG_ALPHAMERGE_FILTER) += vf_alphamerge.o
156OBJS-$(CONFIG_AMPLIFY_FILTER) += vf_amplify.o157OBJS-$(CONFIG_AMPLIFY_FILTER) += vf_amplify.o
libavfilter/allfilters.c+1−0
@@ -143,6 +143,7 @@ extern AVFilter ff_asrc_sine; · 142 unmodified lines
143 143
144extern AVFilter ff_asink_anullsink;144extern AVFilter ff_asink_anullsink;
145 145
146extern AVFilter ff_vf_addroi;
146extern AVFilter ff_vf_alphaextract;147extern AVFilter ff_vf_alphaextract;
147extern AVFilter ff_vf_alphamerge;148extern AVFilter ff_vf_alphamerge;
148extern AVFilter ff_vf_amplify;149extern AVFilter ff_vf_amplify;
libavfilter/vf_addroi.c+269−0
@@ -0,0 +1,269 @@
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "libavutil/avassert.h"
20#include "libavutil/eval.h"
21#include "libavutil/opt.h"
22#include "avfilter.h"
23#include "internal.h"
24
25enum {
26 X, Y, W, H,
27 NB_PARAMS,
28};
29static const char *addroi_param_names[] = {
30 "x", "y", "w", "h",
31};
32
33enum {
34 VAR_IW,
35 VAR_IH,
36 NB_VARS,
37};
38static const char *const addroi_var_names[] = {
39 "iw",
40 "ih",
41};
42
43typedef struct AddROIContext {
44 const AVClass *class;
45
46 char *region_str[NB_PARAMS];
47 AVExpr *region_expr[NB_PARAMS];
48
49 int region[NB_PARAMS];
50 AVRational qoffset;
51
52 int clear;
53} AddROIContext;
54
55static int addroi_config_input(AVFilterLink *inlink)
56{
57 AVFilterContext *avctx = inlink->dst;
58 AddROIContext *ctx = avctx->priv;
59 int i;
60 double vars[NB_VARS];
61 double val;
62
63 vars[VAR_IW] = inlink->w;
64 vars[VAR_IH] = inlink->h;
65
66 for (i = 0; i < NB_PARAMS; i++) {
67 int max_value;
68 switch (i) {
69 case X: max_value = inlink->w; break;
70 case Y: max_value = inlink->h; break;
71 case W: max_value = inlink->w - ctx->region[X]; break;
72 case H: max_value = inlink->h - ctx->region[Y]; break;
73 }
74
75 val = av_expr_eval(ctx->region_expr[i], vars, NULL);
76 if (val < 0.0) {
77 av_log(avctx, AV_LOG_WARNING, "Calculated value %g for %s is "
78 "less than zero - using zero instead.\n", val,
79 addroi_param_names[i]);
80 val = 0.0;
81 } else if (val > max_value) {
82 av_log(avctx, AV_LOG_WARNING, "Calculated value %g for %s is "
83 "greater than maximum allowed value %d - "
84 "using %d instead.\n", val, addroi_param_names[i],
85 max_value, max_value);
86 val = max_value;
87 }
88 ctx->region[i] = val;
89 }
90
91 return 0;
92}
93
94static int addroi_filter_frame(AVFilterLink *inlink, AVFrame *frame)
95{
96 AVFilterContext *avctx = inlink->dst;
97 AVFilterLink *outlink = avctx->outputs[0];
98 AddROIContext *ctx = avctx->priv;
99 AVRegionOfInterest *roi;
100 AVFrameSideData *sd;
101 int err;
102
103 if (ctx->clear) {
104 av_frame_remove_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
105 sd = NULL;
106 } else {
107 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
108 }
109 if (sd) {
110 const AVRegionOfInterest *old_roi;
111 uint32_t old_roi_size;
112 AVBufferRef *roi_ref;
113 int nb_roi, i;
114
115 old_roi = (const AVRegionOfInterest*)sd->data;
116 old_roi_size = old_roi->self_size;
117 av_assert0(old_roi_size && sd->size % old_roi_size == 0);
118 nb_roi = sd->size / old_roi_size + 1;
119
120 roi_ref = av_buffer_alloc(sizeof(*roi) * nb_roi);
121 if (!roi_ref) {
122 err = AVERROR(ENOMEM);
123 goto fail;
124 }
125 roi = (AVRegionOfInterest*)roi_ref->data;
126
127 for (i = 0; i < nb_roi - 1; i++) {
128 old_roi = (const AVRegionOfInterest*)
129 (sd->data + old_roi_size * i);
130
131 roi[i] = (AVRegionOfInterest) {
132 .self_size = sizeof(*roi),
133 .top = old_roi->top,
134 .bottom = old_roi->bottom,
135 .left = old_roi->left,
136 .right = old_roi->right,
137 .qoffset = old_roi->qoffset,
138 };
139 }
140
141 roi[nb_roi - 1] = (AVRegionOfInterest) {
142 .self_size = sizeof(*roi),
143 .top = ctx->region[Y],
144 .bottom = ctx->region[Y] + ctx->region[H],
145 .left = ctx->region[X],
146 .right = ctx->region[X] + ctx->region[W],
147 .qoffset = ctx->qoffset,
148 };
149
150 av_frame_remove_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
151
152 sd = av_frame_new_side_data_from_buf(frame,
153 AV_FRAME_DATA_REGIONS_OF_INTEREST,
154 roi_ref);
155 if (!sd) {
156 av_buffer_unref(&roi_ref);
157 err = AVERROR(ENOMEM);
158 goto fail;
159 }
160
161 } else {
162 sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST,
163 sizeof(AVRegionOfInterest));
164 if (!sd) {
165 err = AVERROR(ENOMEM);
166 goto fail;
167 }
168 roi = (AVRegionOfInterest*)sd->data;
169 *roi = (AVRegionOfInterest) {
170 .self_size = sizeof(*roi),
171 .top = ctx->region[Y],
172 .bottom = ctx->region[Y] + ctx->region[H],
173 .left = ctx->region[X],
174 .right = ctx->region[X] + ctx->region[W],
175 .qoffset = ctx->qoffset,
176 };
177 }
178
179 return ff_filter_frame(outlink, frame);
180
181fail:
182 av_frame_free(&frame);
183 return err;
184}
185
186static av_cold int addroi_init(AVFilterContext *avctx)
187{
188 AddROIContext *ctx = avctx->priv;
189 int i, err;
190
191 for (i = 0; i < NB_PARAMS; i++) {
192 err = av_expr_parse(&ctx->region_expr[i], ctx->region_str[i],
193 addroi_var_names, NULL, NULL, NULL, NULL,
194 0, avctx);
195 if (err < 0) {
196 av_log(ctx, AV_LOG_ERROR,
197 "Error parsing %s expression '%s'.\n",
198 addroi_param_names[i], ctx->region_str[i]);
199 return err;
200 }
201 }
202
203 return 0;
204}
205
206static av_cold void addroi_uninit(AVFilterContext *avctx)
207{
208 AddROIContext *ctx = avctx->priv;
209 int i;
210
211 for (i = 0; i < NB_PARAMS; i++) {
212 av_expr_free(ctx->region_expr[i]);
213 ctx->region_expr[i] = NULL;
214 }
215}
216
217#define OFFSET(x) offsetof(AddROIContext, x)
218#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
219static const AVOption addroi_options[] = {
220 { "x", "Region distance from left edge of frame.",
221 OFFSET(region_str[X]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
222 { "y", "Region distance from top edge of frame.",
223 OFFSET(region_str[Y]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
224 { "w", "Region width.",
225 OFFSET(region_str[W]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
226 { "h", "Region height.",
227 OFFSET(region_str[H]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
228
229 { "qoffset", "Quantisation offset to apply in the region.",
230 OFFSET(qoffset), AV_OPT_TYPE_RATIONAL, { .dbl = -0.1 }, -1, +1, FLAGS },
231
232 { "clear", "Remove any existing regions of interest before adding the new one.",
233 OFFSET(clear), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
234
235 { NULL }
236};
237
238AVFILTER_DEFINE_CLASS(addroi);
239
240static const AVFilterPad addroi_inputs[] = {
241 {
242 .name = "default",
243 .type = AVMEDIA_TYPE_VIDEO,
244 .config_props = addroi_config_input,
245 .filter_frame = addroi_filter_frame,
246 },
247 { NULL }
248};
249
250static const AVFilterPad addroi_outputs[] = {
251 {
252 .name = "default",
253 .type = AVMEDIA_TYPE_VIDEO,
254 },
255 { NULL }
256};
257
258AVFilter ff_vf_addroi = {
259 .name = "addroi",
260 .description = NULL_IF_CONFIG_SMALL("Add region of interest to frame."),
261 .init = addroi_init,
262 .uninit = addroi_uninit,
263
264 .priv_size = sizeof(AddROIContext),
265 .priv_class = &addroi_class,
266
267 .inputs = addroi_inputs,
268 .outputs = addroi_outputs,
269};

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t