Line |
Branch |
Exec |
Source |
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 "filters.h" |
24 |
|
|
#include "video.h" |
25 |
|
|
|
26 |
|
|
enum { |
27 |
|
|
X, Y, W, H, |
28 |
|
|
NB_PARAMS, |
29 |
|
|
}; |
30 |
|
|
static const char addroi_param_names[] = { |
31 |
|
|
'x', 'y', 'w', 'h', |
32 |
|
|
}; |
33 |
|
|
|
34 |
|
|
enum { |
35 |
|
|
VAR_IW, |
36 |
|
|
VAR_IH, |
37 |
|
|
NB_VARS, |
38 |
|
|
}; |
39 |
|
|
static const char *const addroi_var_names[] = { |
40 |
|
|
"iw", |
41 |
|
|
"ih", |
42 |
|
|
}; |
43 |
|
|
|
44 |
|
|
typedef struct AddROIContext { |
45 |
|
|
const AVClass *class; |
46 |
|
|
|
47 |
|
|
char *region_str[NB_PARAMS]; |
48 |
|
|
AVExpr *region_expr[NB_PARAMS]; |
49 |
|
|
|
50 |
|
|
int region[NB_PARAMS]; |
51 |
|
|
AVRational qoffset; |
52 |
|
|
|
53 |
|
|
int clear; |
54 |
|
|
} AddROIContext; |
55 |
|
|
|
56 |
|
✗ |
static int addroi_config_input(AVFilterLink *inlink) |
57 |
|
|
{ |
58 |
|
✗ |
AVFilterContext *avctx = inlink->dst; |
59 |
|
✗ |
AddROIContext *ctx = avctx->priv; |
60 |
|
|
int i; |
61 |
|
|
double vars[NB_VARS]; |
62 |
|
|
double val; |
63 |
|
|
|
64 |
|
✗ |
vars[VAR_IW] = inlink->w; |
65 |
|
✗ |
vars[VAR_IH] = inlink->h; |
66 |
|
|
|
67 |
|
✗ |
for (i = 0; i < NB_PARAMS; i++) { |
68 |
|
|
int max_value; |
69 |
|
✗ |
switch (i) { |
70 |
|
✗ |
case X: max_value = inlink->w; break; |
71 |
|
✗ |
case Y: max_value = inlink->h; break; |
72 |
|
✗ |
case W: max_value = inlink->w - ctx->region[X]; break; |
73 |
|
✗ |
case H: max_value = inlink->h - ctx->region[Y]; break; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
✗ |
val = av_expr_eval(ctx->region_expr[i], vars, NULL); |
77 |
|
✗ |
if (val < 0.0) { |
78 |
|
✗ |
av_log(avctx, AV_LOG_WARNING, "Calculated value %g for %c is " |
79 |
|
|
"less than zero - using zero instead.\n", val, |
80 |
|
✗ |
addroi_param_names[i]); |
81 |
|
✗ |
val = 0.0; |
82 |
|
✗ |
} else if (val > max_value) { |
83 |
|
✗ |
av_log(avctx, AV_LOG_WARNING, "Calculated value %g for %c is " |
84 |
|
|
"greater than maximum allowed value %d - " |
85 |
|
✗ |
"using %d instead.\n", val, addroi_param_names[i], |
86 |
|
|
max_value, max_value); |
87 |
|
✗ |
val = max_value; |
88 |
|
|
} |
89 |
|
✗ |
ctx->region[i] = val; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
✗ |
return 0; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
✗ |
static int addroi_filter_frame(AVFilterLink *inlink, AVFrame *frame) |
96 |
|
|
{ |
97 |
|
✗ |
AVFilterContext *avctx = inlink->dst; |
98 |
|
✗ |
AVFilterLink *outlink = avctx->outputs[0]; |
99 |
|
✗ |
AddROIContext *ctx = avctx->priv; |
100 |
|
|
AVRegionOfInterest *roi; |
101 |
|
|
AVFrameSideData *sd; |
102 |
|
|
int err; |
103 |
|
|
|
104 |
|
✗ |
if (ctx->clear) { |
105 |
|
✗ |
av_frame_remove_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST); |
106 |
|
✗ |
sd = NULL; |
107 |
|
|
} else { |
108 |
|
✗ |
sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST); |
109 |
|
|
} |
110 |
|
✗ |
if (sd) { |
111 |
|
|
const AVRegionOfInterest *old_roi; |
112 |
|
|
uint32_t old_roi_size; |
113 |
|
|
AVBufferRef *roi_ref; |
114 |
|
|
int nb_roi, i; |
115 |
|
|
|
116 |
|
✗ |
old_roi = (const AVRegionOfInterest*)sd->data; |
117 |
|
✗ |
old_roi_size = old_roi->self_size; |
118 |
|
✗ |
av_assert0(old_roi_size && sd->size % old_roi_size == 0); |
119 |
|
✗ |
nb_roi = sd->size / old_roi_size + 1; |
120 |
|
|
|
121 |
|
✗ |
roi_ref = av_buffer_alloc(sizeof(*roi) * nb_roi); |
122 |
|
✗ |
if (!roi_ref) { |
123 |
|
✗ |
err = AVERROR(ENOMEM); |
124 |
|
✗ |
goto fail; |
125 |
|
|
} |
126 |
|
✗ |
roi = (AVRegionOfInterest*)roi_ref->data; |
127 |
|
|
|
128 |
|
✗ |
for (i = 0; i < nb_roi - 1; i++) { |
129 |
|
✗ |
old_roi = (const AVRegionOfInterest*) |
130 |
|
✗ |
(sd->data + old_roi_size * i); |
131 |
|
|
|
132 |
|
✗ |
roi[i] = (AVRegionOfInterest) { |
133 |
|
|
.self_size = sizeof(*roi), |
134 |
|
✗ |
.top = old_roi->top, |
135 |
|
✗ |
.bottom = old_roi->bottom, |
136 |
|
✗ |
.left = old_roi->left, |
137 |
|
✗ |
.right = old_roi->right, |
138 |
|
✗ |
.qoffset = old_roi->qoffset, |
139 |
|
|
}; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
✗ |
roi[nb_roi - 1] = (AVRegionOfInterest) { |
143 |
|
|
.self_size = sizeof(*roi), |
144 |
|
✗ |
.top = ctx->region[Y], |
145 |
|
✗ |
.bottom = ctx->region[Y] + ctx->region[H], |
146 |
|
✗ |
.left = ctx->region[X], |
147 |
|
✗ |
.right = ctx->region[X] + ctx->region[W], |
148 |
|
✗ |
.qoffset = ctx->qoffset, |
149 |
|
|
}; |
150 |
|
|
|
151 |
|
✗ |
av_frame_remove_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST); |
152 |
|
|
|
153 |
|
✗ |
sd = av_frame_new_side_data_from_buf(frame, |
154 |
|
|
AV_FRAME_DATA_REGIONS_OF_INTEREST, |
155 |
|
|
roi_ref); |
156 |
|
✗ |
if (!sd) { |
157 |
|
✗ |
av_buffer_unref(&roi_ref); |
158 |
|
✗ |
err = AVERROR(ENOMEM); |
159 |
|
✗ |
goto fail; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
} else { |
163 |
|
✗ |
sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST, |
164 |
|
|
sizeof(AVRegionOfInterest)); |
165 |
|
✗ |
if (!sd) { |
166 |
|
✗ |
err = AVERROR(ENOMEM); |
167 |
|
✗ |
goto fail; |
168 |
|
|
} |
169 |
|
✗ |
roi = (AVRegionOfInterest*)sd->data; |
170 |
|
✗ |
*roi = (AVRegionOfInterest) { |
171 |
|
|
.self_size = sizeof(*roi), |
172 |
|
✗ |
.top = ctx->region[Y], |
173 |
|
✗ |
.bottom = ctx->region[Y] + ctx->region[H], |
174 |
|
✗ |
.left = ctx->region[X], |
175 |
|
✗ |
.right = ctx->region[X] + ctx->region[W], |
176 |
|
✗ |
.qoffset = ctx->qoffset, |
177 |
|
|
}; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
✗ |
return ff_filter_frame(outlink, frame); |
181 |
|
|
|
182 |
|
✗ |
fail: |
183 |
|
✗ |
av_frame_free(&frame); |
184 |
|
✗ |
return err; |
185 |
|
|
} |
186 |
|
|
|
187 |
|
✗ |
static av_cold int addroi_init(AVFilterContext *avctx) |
188 |
|
|
{ |
189 |
|
✗ |
AddROIContext *ctx = avctx->priv; |
190 |
|
|
int i, err; |
191 |
|
|
|
192 |
|
✗ |
for (i = 0; i < NB_PARAMS; i++) { |
193 |
|
✗ |
err = av_expr_parse(&ctx->region_expr[i], ctx->region_str[i], |
194 |
|
|
addroi_var_names, NULL, NULL, NULL, NULL, |
195 |
|
|
0, avctx); |
196 |
|
✗ |
if (err < 0) { |
197 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, |
198 |
|
|
"Error parsing %c expression '%s'.\n", |
199 |
|
✗ |
addroi_param_names[i], ctx->region_str[i]); |
200 |
|
✗ |
return err; |
201 |
|
|
} |
202 |
|
|
} |
203 |
|
|
|
204 |
|
✗ |
return 0; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
✗ |
static av_cold void addroi_uninit(AVFilterContext *avctx) |
208 |
|
|
{ |
209 |
|
✗ |
AddROIContext *ctx = avctx->priv; |
210 |
|
|
int i; |
211 |
|
|
|
212 |
|
✗ |
for (i = 0; i < NB_PARAMS; i++) { |
213 |
|
✗ |
av_expr_free(ctx->region_expr[i]); |
214 |
|
✗ |
ctx->region_expr[i] = NULL; |
215 |
|
|
} |
216 |
|
✗ |
} |
217 |
|
|
|
218 |
|
|
#define OFFSET(x) offsetof(AddROIContext, x) |
219 |
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
220 |
|
|
static const AVOption addroi_options[] = { |
221 |
|
|
{ "x", "Region distance from left edge of frame.", |
222 |
|
|
OFFSET(region_str[X]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS }, |
223 |
|
|
{ "y", "Region distance from top edge of frame.", |
224 |
|
|
OFFSET(region_str[Y]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS }, |
225 |
|
|
{ "w", "Region width.", |
226 |
|
|
OFFSET(region_str[W]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS }, |
227 |
|
|
{ "h", "Region height.", |
228 |
|
|
OFFSET(region_str[H]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS }, |
229 |
|
|
|
230 |
|
|
{ "qoffset", "Quantisation offset to apply in the region.", |
231 |
|
|
OFFSET(qoffset), AV_OPT_TYPE_RATIONAL, { .dbl = -0.1 }, -1, +1, FLAGS }, |
232 |
|
|
|
233 |
|
|
{ "clear", "Remove any existing regions of interest before adding the new one.", |
234 |
|
|
OFFSET(clear), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, |
235 |
|
|
|
236 |
|
|
{ NULL } |
237 |
|
|
}; |
238 |
|
|
|
239 |
|
|
AVFILTER_DEFINE_CLASS(addroi); |
240 |
|
|
|
241 |
|
|
static const AVFilterPad addroi_inputs[] = { |
242 |
|
|
{ |
243 |
|
|
.name = "default", |
244 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
245 |
|
|
.config_props = addroi_config_input, |
246 |
|
|
.filter_frame = addroi_filter_frame, |
247 |
|
|
}, |
248 |
|
|
}; |
249 |
|
|
|
250 |
|
|
const AVFilter ff_vf_addroi = { |
251 |
|
|
.name = "addroi", |
252 |
|
|
.description = NULL_IF_CONFIG_SMALL("Add region of interest to frame."), |
253 |
|
|
.init = addroi_init, |
254 |
|
|
.uninit = addroi_uninit, |
255 |
|
|
|
256 |
|
|
.priv_size = sizeof(AddROIContext), |
257 |
|
|
.priv_class = &addroi_class, |
258 |
|
|
|
259 |
|
|
.flags = AVFILTER_FLAG_METADATA_ONLY, |
260 |
|
|
|
261 |
|
|
FILTER_INPUTS(addroi_inputs), |
262 |
|
|
FILTER_OUTPUTS(ff_video_default_filterpad), |
263 |
|
|
}; |
264 |
|
|
|