2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/avassert.h"
20 #include "libavutil/eval.h"
21 #include "libavutil/opt.h"
29 static const char addroi_param_names[] = {
38 static const char *const addroi_var_names[] = {
43 typedef struct AddROIContext {
46 char *region_str[NB_PARAMS];
47 AVExpr *region_expr[NB_PARAMS];
49 int region[NB_PARAMS];
55 static int addroi_config_input(AVFilterLink *inlink)
57 AVFilterContext *avctx = inlink->dst;
58 AddROIContext *ctx = avctx->priv;
63 vars[VAR_IW] = inlink->w;
64 vars[VAR_IH] = inlink->h;
66 for (i = 0; i < NB_PARAMS; 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;
75 val = av_expr_eval(ctx->region_expr[i], vars, NULL);
77 av_log(avctx, AV_LOG_WARNING, "Calculated value %g for %c is "
78 "less than zero - using zero instead.\n", val,
79 addroi_param_names[i]);
81 } else if (val > max_value) {
82 av_log(avctx, AV_LOG_WARNING, "Calculated value %g for %c is "
83 "greater than maximum allowed value %d - "
84 "using %d instead.\n", val, addroi_param_names[i],
85 max_value, max_value);
94 static int addroi_filter_frame(AVFilterLink *inlink, AVFrame *frame)
96 AVFilterContext *avctx = inlink->dst;
97 AVFilterLink *outlink = avctx->outputs[0];
98 AddROIContext *ctx = avctx->priv;
99 AVRegionOfInterest *roi;
104 av_frame_remove_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
107 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
110 const AVRegionOfInterest *old_roi;
111 uint32_t old_roi_size;
112 AVBufferRef *roi_ref;
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;
120 roi_ref = av_buffer_alloc(sizeof(*roi) * nb_roi);
122 err = AVERROR(ENOMEM);
125 roi = (AVRegionOfInterest*)roi_ref->data;
127 for (i = 0; i < nb_roi - 1; i++) {
128 old_roi = (const AVRegionOfInterest*)
129 (sd->data + old_roi_size * i);
131 roi[i] = (AVRegionOfInterest) {
132 .self_size = sizeof(*roi),
134 .bottom = old_roi->bottom,
135 .left = old_roi->left,
136 .right = old_roi->right,
137 .qoffset = old_roi->qoffset,
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,
150 av_frame_remove_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
152 sd = av_frame_new_side_data_from_buf(frame,
153 AV_FRAME_DATA_REGIONS_OF_INTEREST,
156 av_buffer_unref(&roi_ref);
157 err = AVERROR(ENOMEM);
162 sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST,
163 sizeof(AVRegionOfInterest));
165 err = AVERROR(ENOMEM);
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,
179 return ff_filter_frame(outlink, frame);
182 av_frame_free(&frame);
186 static av_cold int addroi_init(AVFilterContext *avctx)
188 AddROIContext *ctx = avctx->priv;
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,
196 av_log(ctx, AV_LOG_ERROR,
197 "Error parsing %c expression '%s'.\n",
198 addroi_param_names[i], ctx->region_str[i]);
206 static av_cold void addroi_uninit(AVFilterContext *avctx)
208 AddROIContext *ctx = avctx->priv;
211 for (i = 0; i < NB_PARAMS; i++) {
212 av_expr_free(ctx->region_expr[i]);
213 ctx->region_expr[i] = NULL;
217 #define OFFSET(x) offsetof(AddROIContext, x)
218 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
219 static 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 },
229 { "qoffset", "Quantisation offset to apply in the region.",
230 OFFSET(qoffset), AV_OPT_TYPE_RATIONAL, { .dbl = -0.1 }, -1, +1, FLAGS },
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 },
238 AVFILTER_DEFINE_CLASS(addroi);
240 static const AVFilterPad addroi_inputs[] = {
243 .type = AVMEDIA_TYPE_VIDEO,
244 .config_props = addroi_config_input,
245 .filter_frame = addroi_filter_frame,
250 static const AVFilterPad addroi_outputs[] = {
253 .type = AVMEDIA_TYPE_VIDEO,
258 AVFilter ff_vf_addroi = {
260 .description = NULL_IF_CONFIG_SMALL("Add region of interest to frame."),
262 .uninit = addroi_uninit,
264 .priv_size = sizeof(AddROIContext),
265 .priv_class = &addroi_class,
267 .inputs = addroi_inputs,
268 .outputs = addroi_outputs,