FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/f_sendcmd.c
Date: 2023-09-24 13:02:57
Exec Total Coverage
Lines: 0 261 0.0%
Functions: 0 10 0.0%
Branches: 0 152 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012 Stefano Sabatini
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 * send commands filter
24 */
25
26 #include "config_components.h"
27
28 #include "libavutil/avstring.h"
29 #include "libavutil/bprint.h"
30 #include "libavutil/eval.h"
31 #include "libavutil/file.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/parseutils.h"
34 #include "avfilter.h"
35 #include "internal.h"
36 #include "audio.h"
37 #include "video.h"
38
39 #define COMMAND_FLAG_ENTER 1
40 #define COMMAND_FLAG_LEAVE 2
41 #define COMMAND_FLAG_EXPR 4
42
43 static const char *const var_names[] = {
44 "N", /* frame number */
45 "T", /* frame time in seconds */
46 #if FF_API_FRAME_PKT
47 "POS", /* original position in the file of the frame */
48 #endif
49 "PTS", /* frame pts */
50 "TS", /* interval start time in seconds */
51 "TE", /* interval end time in seconds */
52 "TI", /* interval interpolated value: TI = (T - TS) / (TE - TS) */
53 "W", /* width for video frames */
54 "H", /* height for video frames */
55 NULL
56 };
57
58 enum var_name {
59 VAR_N,
60 VAR_T,
61 #if FF_API_FRAME_PKT
62 VAR_POS,
63 #endif
64 VAR_PTS,
65 VAR_TS,
66 VAR_TE,
67 VAR_TI,
68 VAR_W,
69 VAR_H,
70 VAR_VARS_NB
71 };
72
73 static inline char *make_command_flags_str(AVBPrint *pbuf, int flags)
74 {
75 static const char * const flag_strings[] = { "enter", "leave", "expr" };
76 int i, is_first = 1;
77
78 av_bprint_init(pbuf, 0, AV_BPRINT_SIZE_AUTOMATIC);
79 for (i = 0; i < FF_ARRAY_ELEMS(flag_strings); i++) {
80 if (flags & 1<<i) {
81 if (!is_first)
82 av_bprint_chars(pbuf, '+', 1);
83 av_bprintf(pbuf, "%s", flag_strings[i]);
84 is_first = 0;
85 }
86 }
87
88 return pbuf->str;
89 }
90
91 typedef struct Command {
92 int flags;
93 char *target, *command, *arg;
94 int index;
95 } Command;
96
97 typedef struct Interval {
98 int64_t start_ts; ///< start timestamp expressed as microseconds units
99 int64_t end_ts; ///< end timestamp expressed as microseconds units
100 int index; ///< unique index for these interval commands
101 Command *commands;
102 int nb_commands;
103 int enabled; ///< current time detected inside this interval
104 } Interval;
105
106 typedef struct SendCmdContext {
107 const AVClass *class;
108 Interval *intervals;
109 int nb_intervals;
110
111 char *commands_filename;
112 char *commands_str;
113 } SendCmdContext;
114
115 #define OFFSET(x) offsetof(SendCmdContext, x)
116 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
117 static const AVOption options[] = {
118 { "commands", "set commands", OFFSET(commands_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
119 { "c", "set commands", OFFSET(commands_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
120 { "filename", "set commands file", OFFSET(commands_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
121 { "f", "set commands file", OFFSET(commands_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
122 { NULL }
123 };
124
125 #define SPACES " \f\t\n\r"
126
127 static void skip_comments(const char **buf)
128 {
129 while (**buf) {
130 /* skip leading spaces */
131 *buf += strspn(*buf, SPACES);
132 if (**buf != '#')
133 break;
134
135 (*buf)++;
136
137 /* skip comment until the end of line */
138 *buf += strcspn(*buf, "\n");
139 if (**buf)
140 (*buf)++;
141 }
142 }
143
144 #define COMMAND_DELIMS " \f\t\n\r,;"
145
146 static int parse_command(Command *cmd, int cmd_count, int interval_count,
147 const char **buf, void *log_ctx)
148 {
149 int ret;
150
151 memset(cmd, 0, sizeof(Command));
152 cmd->index = cmd_count;
153
154 /* format: [FLAGS] target command arg */
155 *buf += strspn(*buf, SPACES);
156
157 /* parse flags */
158 if (**buf == '[') {
159 (*buf)++; /* skip "[" */
160
161 while (**buf) {
162 int len = strcspn(*buf, "|+]");
163
164 if (!strncmp(*buf, "enter", strlen("enter"))) cmd->flags |= COMMAND_FLAG_ENTER;
165 else if (!strncmp(*buf, "leave", strlen("leave"))) cmd->flags |= COMMAND_FLAG_LEAVE;
166 else if (!strncmp(*buf, "expr", strlen("expr"))) cmd->flags |= COMMAND_FLAG_EXPR;
167 else {
168 char flag_buf[64];
169 av_strlcpy(flag_buf, *buf, sizeof(flag_buf));
170 av_log(log_ctx, AV_LOG_ERROR,
171 "Unknown flag '%s' in interval #%d, command #%d\n",
172 flag_buf, interval_count, cmd_count);
173 return AVERROR(EINVAL);
174 }
175 *buf += len;
176 if (**buf == ']')
177 break;
178 if (!strspn(*buf, "+|")) {
179 av_log(log_ctx, AV_LOG_ERROR,
180 "Invalid flags char '%c' in interval #%d, command #%d\n",
181 **buf, interval_count, cmd_count);
182 return AVERROR(EINVAL);
183 }
184 if (**buf)
185 (*buf)++;
186 }
187
188 if (**buf != ']') {
189 av_log(log_ctx, AV_LOG_ERROR,
190 "Missing flag terminator or extraneous data found at the end of flags "
191 "in interval #%d, command #%d\n", interval_count, cmd_count);
192 return AVERROR(EINVAL);
193 }
194 (*buf)++; /* skip "]" */
195 } else {
196 cmd->flags = COMMAND_FLAG_ENTER;
197 }
198
199 *buf += strspn(*buf, SPACES);
200 cmd->target = av_get_token(buf, COMMAND_DELIMS);
201 if (!cmd->target || !cmd->target[0]) {
202 av_log(log_ctx, AV_LOG_ERROR,
203 "No target specified in interval #%d, command #%d\n",
204 interval_count, cmd_count);
205 ret = AVERROR(EINVAL);
206 goto fail;
207 }
208
209 *buf += strspn(*buf, SPACES);
210 cmd->command = av_get_token(buf, COMMAND_DELIMS);
211 if (!cmd->command || !cmd->command[0]) {
212 av_log(log_ctx, AV_LOG_ERROR,
213 "No command specified in interval #%d, command #%d\n",
214 interval_count, cmd_count);
215 ret = AVERROR(EINVAL);
216 goto fail;
217 }
218
219 *buf += strspn(*buf, SPACES);
220 cmd->arg = av_get_token(buf, COMMAND_DELIMS);
221
222 return 1;
223
224 fail:
225 av_freep(&cmd->target);
226 av_freep(&cmd->command);
227 av_freep(&cmd->arg);
228 return ret;
229 }
230
231 static int parse_commands(Command **cmds, int *nb_cmds, int interval_count,
232 const char **buf, void *log_ctx)
233 {
234 int cmd_count = 0;
235 int ret, n = 0;
236 AVBPrint pbuf;
237
238 *cmds = NULL;
239 *nb_cmds = 0;
240
241 while (**buf) {
242 Command cmd;
243
244 if ((ret = parse_command(&cmd, cmd_count, interval_count, buf, log_ctx)) < 0)
245 return ret;
246 cmd_count++;
247
248 /* (re)allocate commands array if required */
249 if (*nb_cmds == n) {
250 n = FFMAX(16, 2*n); /* first allocation = 16, or double the number */
251 *cmds = av_realloc_f(*cmds, n, 2*sizeof(Command));
252 if (!*cmds) {
253 av_log(log_ctx, AV_LOG_ERROR,
254 "Could not (re)allocate command array\n");
255 return AVERROR(ENOMEM);
256 }
257 }
258
259 (*cmds)[(*nb_cmds)++] = cmd;
260
261 *buf += strspn(*buf, SPACES);
262 if (**buf && **buf != ';' && **buf != ',') {
263 av_log(log_ctx, AV_LOG_ERROR,
264 "Missing separator or extraneous data found at the end of "
265 "interval #%d, in command #%d\n",
266 interval_count, cmd_count);
267 av_log(log_ctx, AV_LOG_ERROR,
268 "Command was parsed as: flags:[%s] target:%s command:%s arg:%s\n",
269 make_command_flags_str(&pbuf, cmd.flags), cmd.target, cmd.command, cmd.arg);
270 return AVERROR(EINVAL);
271 }
272 if (**buf == ';')
273 break;
274 if (**buf == ',')
275 (*buf)++;
276 }
277
278 return 0;
279 }
280
281 #define DELIMS " \f\t\n\r,;"
282
283 static int parse_interval(Interval *interval, int interval_count,
284 const char **buf, void *log_ctx)
285 {
286 char *intervalstr;
287 int ret;
288
289 *buf += strspn(*buf, SPACES);
290 if (!**buf)
291 return 0;
292
293 /* reset data */
294 memset(interval, 0, sizeof(Interval));
295 interval->index = interval_count;
296
297 /* format: INTERVAL COMMANDS */
298
299 /* parse interval */
300 intervalstr = av_get_token(buf, DELIMS);
301 if (intervalstr && intervalstr[0]) {
302 char *start, *end;
303
304 start = av_strtok(intervalstr, "-", &end);
305 if (!start) {
306 ret = AVERROR(EINVAL);
307 av_log(log_ctx, AV_LOG_ERROR,
308 "Invalid interval specification '%s' in interval #%d\n",
309 intervalstr, interval_count);
310 goto end;
311 }
312 if ((ret = av_parse_time(&interval->start_ts, start, 1)) < 0) {
313 av_log(log_ctx, AV_LOG_ERROR,
314 "Invalid start time specification '%s' in interval #%d\n",
315 start, interval_count);
316 goto end;
317 }
318
319 if (end) {
320 if ((ret = av_parse_time(&interval->end_ts, end, 1)) < 0) {
321 av_log(log_ctx, AV_LOG_ERROR,
322 "Invalid end time specification '%s' in interval #%d\n",
323 end, interval_count);
324 goto end;
325 }
326 } else {
327 interval->end_ts = INT64_MAX;
328 }
329 if (interval->end_ts < interval->start_ts) {
330 av_log(log_ctx, AV_LOG_ERROR,
331 "Invalid end time '%s' in interval #%d: "
332 "cannot be lesser than start time '%s'\n",
333 end, interval_count, start);
334 ret = AVERROR(EINVAL);
335 goto end;
336 }
337 } else {
338 av_log(log_ctx, AV_LOG_ERROR,
339 "No interval specified for interval #%d\n", interval_count);
340 ret = AVERROR(EINVAL);
341 goto end;
342 }
343
344 /* parse commands */
345 ret = parse_commands(&interval->commands, &interval->nb_commands,
346 interval_count, buf, log_ctx);
347
348 end:
349 av_free(intervalstr);
350 return ret;
351 }
352
353 static int parse_intervals(Interval **intervals, int *nb_intervals,
354 const char *buf, void *log_ctx)
355 {
356 int interval_count = 0;
357 int ret, n = 0;
358
359 *intervals = NULL;
360 *nb_intervals = 0;
361
362 if (!buf)
363 return 0;
364
365 while (1) {
366 Interval interval;
367
368 skip_comments(&buf);
369 if (!(*buf))
370 break;
371
372 if ((ret = parse_interval(&interval, interval_count, &buf, log_ctx)) < 0)
373 return ret;
374
375 buf += strspn(buf, SPACES);
376 if (*buf) {
377 if (*buf != ';') {
378 av_log(log_ctx, AV_LOG_ERROR,
379 "Missing terminator or extraneous data found at the end of interval #%d\n",
380 interval_count);
381 return AVERROR(EINVAL);
382 }
383 buf++; /* skip ';' */
384 }
385 interval_count++;
386
387 /* (re)allocate commands array if required */
388 if (*nb_intervals == n) {
389 n = FFMAX(16, 2*n); /* first allocation = 16, or double the number */
390 *intervals = av_realloc_f(*intervals, n, 2*sizeof(Interval));
391 if (!*intervals) {
392 av_log(log_ctx, AV_LOG_ERROR,
393 "Could not (re)allocate intervals array\n");
394 return AVERROR(ENOMEM);
395 }
396 }
397
398 (*intervals)[(*nb_intervals)++] = interval;
399 }
400
401 return 0;
402 }
403
404 static int cmp_intervals(const void *a, const void *b)
405 {
406 const Interval *i1 = a;
407 const Interval *i2 = b;
408 return 2 * FFDIFFSIGN(i1->start_ts, i2->start_ts) + FFDIFFSIGN(i1->index, i2->index);
409 }
410
411 static av_cold int init(AVFilterContext *ctx)
412 {
413 SendCmdContext *s = ctx->priv;
414 int ret, i, j;
415
416 if ((!!s->commands_filename + !!s->commands_str) != 1) {
417 av_log(ctx, AV_LOG_ERROR,
418 "One and only one of the filename or commands options must be specified\n");
419 return AVERROR(EINVAL);
420 }
421
422 if (s->commands_filename) {
423 uint8_t *file_buf, *buf;
424 size_t file_bufsize;
425 ret = av_file_map(s->commands_filename,
426 &file_buf, &file_bufsize, 0, ctx);
427 if (ret < 0)
428 return ret;
429
430 /* create a 0-terminated string based on the read file */
431 buf = av_malloc(file_bufsize + 1);
432 if (!buf) {
433 av_file_unmap(file_buf, file_bufsize);
434 return AVERROR(ENOMEM);
435 }
436 memcpy(buf, file_buf, file_bufsize);
437 buf[file_bufsize] = 0;
438 av_file_unmap(file_buf, file_bufsize);
439 s->commands_str = buf;
440 }
441
442 if ((ret = parse_intervals(&s->intervals, &s->nb_intervals,
443 s->commands_str, ctx)) < 0)
444 return ret;
445
446 if (s->nb_intervals == 0) {
447 av_log(ctx, AV_LOG_ERROR, "No commands were specified\n");
448 return AVERROR(EINVAL);
449 }
450
451 qsort(s->intervals, s->nb_intervals, sizeof(Interval), cmp_intervals);
452
453 av_log(ctx, AV_LOG_DEBUG, "Parsed commands:\n");
454 for (i = 0; i < s->nb_intervals; i++) {
455 AVBPrint pbuf;
456 Interval *interval = &s->intervals[i];
457 av_log(ctx, AV_LOG_VERBOSE, "start_time:%f end_time:%f index:%d\n",
458 (double)interval->start_ts/1000000, (double)interval->end_ts/1000000, interval->index);
459 for (j = 0; j < interval->nb_commands; j++) {
460 Command *cmd = &interval->commands[j];
461 av_log(ctx, AV_LOG_VERBOSE,
462 " [%s] target:%s command:%s arg:%s index:%d\n",
463 make_command_flags_str(&pbuf, cmd->flags), cmd->target, cmd->command, cmd->arg, cmd->index);
464 }
465 }
466
467 return 0;
468 }
469
470 static av_cold void uninit(AVFilterContext *ctx)
471 {
472 SendCmdContext *s = ctx->priv;
473 int i, j;
474
475 for (i = 0; i < s->nb_intervals; i++) {
476 Interval *interval = &s->intervals[i];
477 for (j = 0; j < interval->nb_commands; j++) {
478 Command *cmd = &interval->commands[j];
479 av_freep(&cmd->target);
480 av_freep(&cmd->command);
481 av_freep(&cmd->arg);
482 }
483 av_freep(&interval->commands);
484 }
485 av_freep(&s->intervals);
486 }
487
488 static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
489 {
490 AVFilterContext *ctx = inlink->dst;
491 SendCmdContext *s = ctx->priv;
492 int64_t ts;
493 int i, j, ret;
494
495 if (ref->pts == AV_NOPTS_VALUE)
496 goto end;
497
498 ts = av_rescale_q(ref->pts, inlink->time_base, AV_TIME_BASE_Q);
499
500 #define WITHIN_INTERVAL(ts, start_ts, end_ts) ((ts) >= (start_ts) && (ts) < (end_ts))
501
502 for (i = 0; i < s->nb_intervals; i++) {
503 Interval *interval = &s->intervals[i];
504 int flags = 0;
505
506 if (!interval->enabled && WITHIN_INTERVAL(ts, interval->start_ts, interval->end_ts)) {
507 flags += COMMAND_FLAG_ENTER;
508 interval->enabled = 1;
509 }
510 if (interval->enabled && !WITHIN_INTERVAL(ts, interval->start_ts, interval->end_ts)) {
511 flags += COMMAND_FLAG_LEAVE;
512 interval->enabled = 0;
513 }
514 if (interval->enabled)
515 flags += COMMAND_FLAG_EXPR;
516
517 if (flags) {
518 AVBPrint pbuf;
519 av_log(ctx, AV_LOG_VERBOSE,
520 "[%s] interval #%d start_ts:%f end_ts:%f ts:%f\n",
521 make_command_flags_str(&pbuf, flags), interval->index,
522 (double)interval->start_ts/1000000, (double)interval->end_ts/1000000,
523 (double)ts/1000000);
524
525 for (j = 0; flags && j < interval->nb_commands; j++) {
526 Command *cmd = &interval->commands[j];
527 char *cmd_arg = cmd->arg;
528 char buf[1024];
529
530 if (cmd->flags & flags) {
531 if (cmd->flags & COMMAND_FLAG_EXPR) {
532 double var_values[VAR_VARS_NB], res;
533 double start = TS2T(interval->start_ts, AV_TIME_BASE_Q);
534 double end = TS2T(interval->end_ts, AV_TIME_BASE_Q);
535 double current = TS2T(ref->pts, inlink->time_base);
536
537 var_values[VAR_N] = inlink->frame_count_in;
538 #if FF_API_FRAME_PKT
539 FF_DISABLE_DEPRECATION_WARNINGS
540 var_values[VAR_POS] = ref->pkt_pos == -1 ? NAN : ref->pkt_pos;
541 FF_ENABLE_DEPRECATION_WARNINGS
542 #endif
543 var_values[VAR_PTS] = TS2D(ref->pts);
544 var_values[VAR_T] = current;
545 var_values[VAR_TS] = start;
546 var_values[VAR_TE] = end;
547 var_values[VAR_TI] = (current - start) / (end - start);
548 var_values[VAR_W] = ref->width;
549 var_values[VAR_H] = ref->height;
550
551 if ((ret = av_expr_parse_and_eval(&res, cmd->arg, var_names, var_values,
552 NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
553 av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for command argument.\n", cmd->arg);
554 av_frame_free(&ref);
555 return AVERROR(EINVAL);
556 }
557
558 cmd_arg = av_asprintf("%g", res);
559 if (!cmd_arg) {
560 av_frame_free(&ref);
561 return AVERROR(ENOMEM);
562 }
563 }
564 av_log(ctx, AV_LOG_VERBOSE,
565 "Processing command #%d target:%s command:%s arg:%s\n",
566 cmd->index, cmd->target, cmd->command, cmd_arg);
567 ret = avfilter_graph_send_command(inlink->graph,
568 cmd->target, cmd->command, cmd_arg,
569 buf, sizeof(buf),
570 AVFILTER_CMD_FLAG_ONE);
571 av_log(ctx, AV_LOG_VERBOSE,
572 "Command reply for command #%d: ret:%s res:%s\n",
573 cmd->index, av_err2str(ret), buf);
574 if (cmd->flags & COMMAND_FLAG_EXPR)
575 av_freep(&cmd_arg);
576 }
577 }
578 }
579 }
580
581 end:
582 switch (inlink->type) {
583 case AVMEDIA_TYPE_VIDEO:
584 case AVMEDIA_TYPE_AUDIO:
585 return ff_filter_frame(inlink->dst->outputs[0], ref);
586 }
587
588 return AVERROR(ENOSYS);
589 }
590
591 AVFILTER_DEFINE_CLASS_EXT(sendcmd, "(a)sendcmd", options);
592
593 #if CONFIG_SENDCMD_FILTER
594
595 static const AVFilterPad sendcmd_inputs[] = {
596 {
597 .name = "default",
598 .type = AVMEDIA_TYPE_VIDEO,
599 .filter_frame = filter_frame,
600 },
601 };
602
603 const AVFilter ff_vf_sendcmd = {
604 .name = "sendcmd",
605 .description = NULL_IF_CONFIG_SMALL("Send commands to filters."),
606 .init = init,
607 .uninit = uninit,
608 .priv_size = sizeof(SendCmdContext),
609 .flags = AVFILTER_FLAG_METADATA_ONLY,
610 FILTER_INPUTS(sendcmd_inputs),
611 FILTER_OUTPUTS(ff_video_default_filterpad),
612 .priv_class = &sendcmd_class,
613 };
614
615 #endif
616
617 #if CONFIG_ASENDCMD_FILTER
618
619 static const AVFilterPad asendcmd_inputs[] = {
620 {
621 .name = "default",
622 .type = AVMEDIA_TYPE_AUDIO,
623 .filter_frame = filter_frame,
624 },
625 };
626
627 const AVFilter ff_af_asendcmd = {
628 .name = "asendcmd",
629 .description = NULL_IF_CONFIG_SMALL("Send commands to filters."),
630 .priv_class = &sendcmd_class,
631 .init = init,
632 .uninit = uninit,
633 .priv_size = sizeof(SendCmdContext),
634 .flags = AVFILTER_FLAG_METADATA_ONLY,
635 FILTER_INPUTS(asendcmd_inputs),
636 FILTER_OUTPUTS(ff_audio_default_filterpad),
637 };
638
639 #endif
640