| 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 | /** | ||
| 20 | * @file | ||
| 21 | * feedback video filter | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include "libavutil/fifo.h" | ||
| 25 | #include "libavutil/imgutils.h" | ||
| 26 | #include "libavutil/opt.h" | ||
| 27 | #include "libavutil/internal.h" | ||
| 28 | #include "avfilter.h" | ||
| 29 | #include "filters.h" | ||
| 30 | #include "formats.h" | ||
| 31 | #include "video.h" | ||
| 32 | |||
| 33 | typedef struct FeedbackContext { | ||
| 34 | const AVClass *class; | ||
| 35 | |||
| 36 | int x, y; | ||
| 37 | int w, h; | ||
| 38 | |||
| 39 | int max_step[4]; | ||
| 40 | int hsub, vsub; | ||
| 41 | |||
| 42 | AVFrame *feed; | ||
| 43 | |||
| 44 | AVFifo *fifo; | ||
| 45 | } FeedbackContext; | ||
| 46 | |||
| 47 | 235 | static void adjust_pos(AVFilterContext *ctx, FeedbackContext *s) | |
| 48 | { | ||
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 235 times.
|
235 | if (s->x + s->w > ctx->inputs[0]->w) |
| 50 | ✗ | s->x = ctx->inputs[0]->w - s->w; | |
| 51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 235 times.
|
235 | if (s->y + s->h > ctx->inputs[0]->h) |
| 52 | ✗ | s->y = ctx->inputs[0]->h - s->h; | |
| 53 | 235 | } | |
| 54 | |||
| 55 | 8 | static void adjust_parameters(AVFilterContext *ctx, FeedbackContext *s) | |
| 56 | { | ||
| 57 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (s->x >= ctx->inputs[0]->w) |
| 58 | ✗ | s->x = 0; | |
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (s->y >= ctx->inputs[0]->h) |
| 60 | ✗ | s->y = 0; | |
| 61 | |||
| 62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (s->w <= 0) |
| 63 | ✗ | s->w = ctx->inputs[0]->w - s->x; | |
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (s->h <= 0) |
| 65 | ✗ | s->h = ctx->inputs[0]->h - s->y; | |
| 66 | |||
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (s->w > ctx->inputs[0]->w) |
| 68 | ✗ | s->w = ctx->inputs[0]->w; | |
| 69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (s->h > ctx->inputs[0]->h) |
| 70 | ✗ | s->h = ctx->inputs[0]->h; | |
| 71 | |||
| 72 | 8 | adjust_pos(ctx, s); | |
| 73 | 8 | } | |
| 74 | |||
| 75 | 4 | static int config_input(AVFilterLink *inlink) | |
| 76 | { | ||
| 77 | 4 | AVFilterContext *ctx = inlink->dst; | |
| 78 | 4 | const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format); | |
| 79 | 4 | FeedbackContext *s = ctx->priv; | |
| 80 | |||
| 81 | 4 | s->hsub = pix_desc->log2_chroma_w; | |
| 82 | 4 | s->vsub = pix_desc->log2_chroma_h; | |
| 83 | |||
| 84 | 4 | av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc); | |
| 85 | |||
| 86 | 4 | adjust_parameters(ctx, s); | |
| 87 | |||
| 88 | 4 | ctx->inputs[1]->w = s->w; | |
| 89 | 4 | ctx->inputs[1]->h = s->h; | |
| 90 | |||
| 91 | 4 | return 0; | |
| 92 | } | ||
| 93 | |||
| 94 | 4 | static int config_output(AVFilterLink *outlink) | |
| 95 | { | ||
| 96 | 4 | AVFilterContext *ctx = outlink->src; | |
| 97 | 4 | FeedbackContext *s = ctx->priv; | |
| 98 | |||
| 99 | 4 | adjust_parameters(ctx, s); | |
| 100 | |||
| 101 | 4 | ctx->outputs[0]->w = ctx->inputs[0]->w; | |
| 102 | 4 | ctx->outputs[0]->h = ctx->inputs[0]->h; | |
| 103 | 4 | ctx->outputs[1]->w = s->w; | |
| 104 | 4 | ctx->outputs[1]->h = s->h; | |
| 105 | |||
| 106 | 4 | return 0; | |
| 107 | } | ||
| 108 | |||
| 109 | 2 | static int query_formats(const AVFilterContext *ctx, | |
| 110 | AVFilterFormatsConfig **cfg_in, | ||
| 111 | AVFilterFormatsConfig **cfg_out) | ||
| 112 | { | ||
| 113 | 2 | return ff_set_common_formats2(ctx, cfg_in, cfg_out, | |
| 114 | ff_formats_pixdesc_filter(0, AV_PIX_FMT_FLAG_BITSTREAM | | ||
| 115 | AV_PIX_FMT_FLAG_HWACCEL | | ||
| 116 | AV_PIX_FMT_FLAG_PAL)); | ||
| 117 | } | ||
| 118 | |||
| 119 | 227 | static int activate(AVFilterContext *ctx) | |
| 120 | { | ||
| 121 | 227 | FeedbackContext *s = ctx->priv; | |
| 122 | int status, ret; | ||
| 123 | int64_t pts; | ||
| 124 | |||
| 125 | 227 | adjust_pos(ctx, s); | |
| 126 | |||
| 127 |
2/2✓ Branch 0 taken 452 times.
✓ Branch 1 taken 225 times.
|
677 | for (int i = 0; i < ctx->nb_outputs; i++) |
| 128 |
4/4✓ Branch 1 taken 2 times.
✓ Branch 2 taken 450 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 2 times.
|
456 | FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[i], ctx); |
| 129 | |||
| 130 |
1/2✓ Branch 0 taken 225 times.
✗ Branch 1 not taken.
|
225 | if (!s->feed) { |
| 131 | 225 | ret = ff_inlink_consume_frame(ctx->inputs[1], &s->feed); | |
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 225 times.
|
225 | if (ret < 0) |
| 133 | ✗ | return ret; | |
| 134 | } | ||
| 135 | |||
| 136 |
3/4✓ Branch 0 taken 55 times.
✓ Branch 1 taken 170 times.
✓ Branch 3 taken 55 times.
✗ Branch 4 not taken.
|
225 | if (s->feed && av_fifo_can_read(s->fifo)) { |
| 137 | 55 | AVFrame *src = s->feed; | |
| 138 | 55 | AVFrame *dst = NULL; | |
| 139 | |||
| 140 | 55 | av_fifo_read(s->fifo, &dst, 1); | |
| 141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
|
55 | if (!dst) |
| 142 | ✗ | return AVERROR_BUG; | |
| 143 | |||
| 144 |
2/2✓ Branch 1 taken 32 times.
✓ Branch 2 taken 23 times.
|
55 | if (!av_frame_is_writable(dst)) { |
| 145 | 32 | AVFrame *tmp = ff_get_video_buffer(ctx->outputs[0], ctx->outputs[0]->w, ctx->outputs[0]->h); | |
| 146 | |||
| 147 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (!tmp) { |
| 148 | ✗ | av_frame_free(&dst); | |
| 149 | ✗ | return AVERROR(ENOMEM); | |
| 150 | } | ||
| 151 | |||
| 152 | 32 | ret = av_frame_copy(tmp, dst); | |
| 153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (ret < 0) { |
| 154 | ✗ | av_frame_free(&dst); | |
| 155 | ✗ | av_frame_free(&tmp); | |
| 156 | ✗ | return ret; | |
| 157 | } | ||
| 158 | |||
| 159 | 32 | av_frame_copy_props(tmp, dst); | |
| 160 | 32 | av_frame_free(&dst); | |
| 161 | 32 | dst = tmp; | |
| 162 | } | ||
| 163 | |||
| 164 |
2/2✓ Branch 0 taken 5500 times.
✓ Branch 1 taken 55 times.
|
5555 | for (int y = 0; y < src->height; y++) { |
| 165 | 5500 | memmove(dst->data[0] + (s->y + y) * dst->linesize[0] + s->x * s->max_step[0], | |
| 166 | 5500 | src->data[0] + y * src->linesize[0], src->width * s->max_step[0]); | |
| 167 | } | ||
| 168 | |||
| 169 |
2/2✓ Branch 0 taken 110 times.
✓ Branch 1 taken 55 times.
|
165 | for (int i = 1; i < 3; i++) { |
| 170 |
1/2✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
|
110 | if (dst->data[i]) { |
| 171 |
2/2✓ Branch 0 taken 11000 times.
✓ Branch 1 taken 110 times.
|
11110 | for (int y = 0; y < src->height; y++) { |
| 172 | 11000 | memmove(dst->data[i] + ((s->y + y) >> s->vsub) * dst->linesize[i] + (s->x >> s->hsub) * s->max_step[i], | |
| 173 | 11000 | src->data[i] + (y >> s->vsub) * src->linesize[i], (src->width >> s->hsub) * s->max_step[i]); | |
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
|
55 | if (dst->data[3]) { |
| 179 | ✗ | for (int y = 0; y < src->height; y++) { | |
| 180 | ✗ | memmove(dst->data[3] + (s->y + y) * dst->linesize[3] + s->x * s->max_step[3], | |
| 181 | ✗ | src->data[3] + y * src->linesize[3], src->width * s->max_step[3]); | |
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | 55 | ret = ff_filter_frame(ctx->outputs[0], dst); | |
| 186 | 55 | av_frame_free(&s->feed); | |
| 187 | 55 | return ret; | |
| 188 | } | ||
| 189 | |||
| 190 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 170 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
170 | if (!s->feed || ctx->is_disabled) { |
| 191 | 170 | AVFrame *in = NULL; | |
| 192 | |||
| 193 | 170 | ret = ff_inlink_consume_frame(ctx->inputs[0], &in); | |
| 194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 170 times.
|
170 | if (ret < 0) |
| 195 | 56 | return ret; | |
| 196 | |||
| 197 |
3/4✓ Branch 0 taken 56 times.
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
|
170 | if (ret > 0 && ctx->is_disabled) |
| 198 | ✗ | return ff_filter_frame(ctx->outputs[0], in); | |
| 199 | |||
| 200 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 114 times.
|
170 | if (ret > 0) { |
| 201 | AVFrame *frame; | ||
| 202 | |||
| 203 | 56 | ret = av_fifo_write(s->fifo, &in, 1); | |
| 204 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | if (ret < 0) { |
| 205 | ✗ | av_frame_free(&in); | |
| 206 | ✗ | return ret; | |
| 207 | } | ||
| 208 | |||
| 209 | 56 | frame = av_frame_clone(in); | |
| 210 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | if (!frame) |
| 211 | ✗ | return AVERROR(ENOMEM); | |
| 212 | |||
| 213 | 56 | frame->width = s->w; | |
| 214 | 56 | frame->height = s->h; | |
| 215 | |||
| 216 | 56 | frame->data[0] += s->y * frame->linesize[0]; | |
| 217 | 56 | frame->data[0] += s->x * s->max_step[0]; | |
| 218 | |||
| 219 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 56 times.
|
168 | for (int i = 1; i < 3; i ++) { |
| 220 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | if (frame->data[i]) { |
| 221 | 112 | frame->data[i] += (s->y >> s->vsub) * frame->linesize[i]; | |
| 222 | 112 | frame->data[i] += (s->x >> s->hsub) * s->max_step[i]; | |
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | if (frame->data[3]) { |
| 227 | ✗ | frame->data[3] += s->y * frame->linesize[3]; | |
| 228 | ✗ | frame->data[3] += s->x * s->max_step[3]; | |
| 229 | } | ||
| 230 | |||
| 231 | 56 | return ff_filter_frame(ctx->outputs[1], frame); | |
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 112 times.
|
114 | if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) { |
| 236 | 2 | ff_outlink_set_status(ctx->outputs[0], status, pts); | |
| 237 | 2 | ff_outlink_set_status(ctx->outputs[1], status, pts); | |
| 238 | 2 | return 0; | |
| 239 | } | ||
| 240 | |||
| 241 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
|
112 | if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) { |
| 242 | ✗ | ff_outlink_set_status(ctx->outputs[0], status, pts); | |
| 243 | ✗ | ff_outlink_set_status(ctx->outputs[1], status, pts); | |
| 244 | ✗ | return 0; | |
| 245 | } | ||
| 246 | |||
| 247 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
112 | if (!s->feed || ctx->is_disabled) { |
| 248 |
3/4✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 56 times.
|
112 | if (!ctx->is_disabled && ff_outlink_frame_wanted(ctx->outputs[1])) { |
| 249 | 56 | ff_inlink_request_frame(ctx->inputs[0]); | |
| 250 | 56 | return 0; | |
| 251 | } | ||
| 252 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | if (ff_outlink_frame_wanted(ctx->outputs[0])) { |
| 253 | 56 | ff_inlink_request_frame(ctx->inputs[0]); | |
| 254 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | if (!ctx->is_disabled) |
| 255 | 56 | ff_inlink_request_frame(ctx->inputs[1]); | |
| 256 | 56 | return 0; | |
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | ✗ | return FFERROR_NOT_READY; | |
| 261 | } | ||
| 262 | |||
| 263 | 4 | static av_cold int init(AVFilterContext *ctx) | |
| 264 | { | ||
| 265 | 4 | FeedbackContext *s = ctx->priv; | |
| 266 | |||
| 267 | 4 | s->fifo = av_fifo_alloc2(8, sizeof(AVFrame *), AV_FIFO_FLAG_AUTO_GROW); | |
| 268 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!s->fifo) |
| 269 | ✗ | return AVERROR(ENOMEM); | |
| 270 | |||
| 271 | 4 | return 0; | |
| 272 | } | ||
| 273 | |||
| 274 | 4 | static av_cold void uninit(AVFilterContext *ctx) | |
| 275 | { | ||
| 276 | 4 | FeedbackContext *s = ctx->priv; | |
| 277 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (s->fifo) { |
| 278 | 4 | size_t size = av_fifo_can_read(s->fifo); | |
| 279 | |||
| 280 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | for (size_t n = 0; n < size; n++) { |
| 281 | 1 | AVFrame *frame = NULL; | |
| 282 | |||
| 283 | 1 | av_fifo_read(s->fifo, &frame, 1); | |
| 284 | |||
| 285 | 1 | av_frame_free(&frame); | |
| 286 | } | ||
| 287 | |||
| 288 | 4 | av_fifo_freep2(&s->fifo); | |
| 289 | } | ||
| 290 | 4 | } | |
| 291 | |||
| 292 | static const AVFilterPad inputs[] = { | ||
| 293 | { | ||
| 294 | .name = "default", | ||
| 295 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 296 | .config_props = config_input, | ||
| 297 | }, | ||
| 298 | { | ||
| 299 | .name = "feedin", | ||
| 300 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 301 | .config_props = config_input, | ||
| 302 | }, | ||
| 303 | }; | ||
| 304 | |||
| 305 | static const AVFilterPad outputs[] = { | ||
| 306 | { | ||
| 307 | .name = "default", | ||
| 308 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 309 | .config_props = config_output, | ||
| 310 | }, | ||
| 311 | { | ||
| 312 | .name = "feedout", | ||
| 313 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 314 | .config_props = config_output, | ||
| 315 | }, | ||
| 316 | }; | ||
| 317 | |||
| 318 | #define OFFSET(x) offsetof(FeedbackContext, x) | ||
| 319 | #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM) | ||
| 320 | #define TFLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM) | ||
| 321 | |||
| 322 | static const AVOption feedback_options[] = { | ||
| 323 | { "x", "set top left crop position", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, TFLAGS }, | ||
| 324 | { "y", "set top left crop position", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, TFLAGS }, | ||
| 325 | { "w", "set crop size", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, | ||
| 326 | { "h", "set crop size", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, | ||
| 327 | { NULL } | ||
| 328 | }; | ||
| 329 | |||
| 330 | AVFILTER_DEFINE_CLASS(feedback); | ||
| 331 | |||
| 332 | const FFFilter ff_vf_feedback = { | ||
| 333 | .p.name = "feedback", | ||
| 334 | .p.description = NULL_IF_CONFIG_SMALL("Apply feedback video filter."), | ||
| 335 | .p.priv_class = &feedback_class, | ||
| 336 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, | ||
| 337 | .priv_size = sizeof(FeedbackContext), | ||
| 338 | .activate = activate, | ||
| 339 | .init = init, | ||
| 340 | .uninit = uninit, | ||
| 341 | FILTER_INPUTS(inputs), | ||
| 342 | FILTER_OUTPUTS(outputs), | ||
| 343 | FILTER_QUERY_FUNC2(query_formats), | ||
| 344 | .process_command = ff_filter_process_command, | ||
| 345 | }; | ||
| 346 |