2 * Copyright (c) 2012 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
23 * send commands filter
26 #include "libavutil/avstring.h"
27 #include "libavutil/bprint.h"
28 #include "libavutil/file.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/parseutils.h"
33 #include "avfiltergraph.h"
37 #define COMMAND_FLAG_ENTER 1
38 #define COMMAND_FLAG_LEAVE 2
40 static inline char *make_command_flags_str(AVBPrint
*pbuf
, int flags
)
42 static const char * const flag_strings
[] = { "enter", "leave" };
45 av_bprint_init(pbuf
, 0, AV_BPRINT_SIZE_AUTOMATIC
);
46 for (i
= 0; i
< FF_ARRAY_ELEMS(flag_strings
); i
++) {
49 av_bprint_chars(pbuf
, '+', 1);
50 av_bprintf(pbuf
, "%s", flag_strings
[i
]);
60 char *target
, *command
, *arg
;
65 int64_t start_ts
; ///< start timestamp expressed as microseconds units
66 int64_t end_ts
; ///< end timestamp expressed as microseconds units
67 int index
; ///< unique index for these interval commands
70 int enabled
; ///< current time detected inside this interval
78 char *commands_filename
;
82 #define OFFSET(x) offsetof(SendCmdContext, x)
83 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
84 static const AVOption options
[] = {
85 { "commands", "set commands", OFFSET(commands_str
), AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, FLAGS
},
86 { "c", "set commands", OFFSET(commands_str
), AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, FLAGS
},
87 { "filename", "set commands file", OFFSET(commands_filename
), AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, FLAGS
},
88 { "f", "set commands file", OFFSET(commands_filename
), AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, FLAGS
},
92 #define SPACES " \f\t\n\r"
94 static void skip_comments(const char **buf
)
97 /* skip leading spaces */
98 *buf
+= strspn(*buf
, SPACES
);
104 /* skip comment until the end of line */
105 *buf
+= strcspn(*buf
, "\n");
111 #define COMMAND_DELIMS " \f\t\n\r,;"
113 static int parse_command(Command
*cmd
, int cmd_count
, int interval_count
,
114 const char **buf
, void *log_ctx
)
118 memset(cmd
, 0, sizeof(Command
));
119 cmd
->index
= cmd_count
;
121 /* format: [FLAGS] target command arg */
122 *buf
+= strspn(*buf
, SPACES
);
126 (*buf
)++; /* skip "[" */
129 int len
= strcspn(*buf
, "|+]");
131 if (!strncmp(*buf
, "enter", strlen("enter"))) cmd
->flags
|= COMMAND_FLAG_ENTER
;
132 else if (!strncmp(*buf
, "leave", strlen("leave"))) cmd
->flags
|= COMMAND_FLAG_LEAVE
;
135 av_strlcpy(flag_buf
, *buf
, sizeof(flag_buf
));
136 av_log(log_ctx
, AV_LOG_ERROR
,
137 "Unknown flag '%s' in interval #%d, command #%d\n",
138 flag_buf
, interval_count
, cmd_count
);
139 return AVERROR(EINVAL
);
144 if (!strspn(*buf
, "+|")) {
145 av_log(log_ctx
, AV_LOG_ERROR
,
146 "Invalid flags char '%c' in interval #%d, command #%d\n",
147 **buf
, interval_count
, cmd_count
);
148 return AVERROR(EINVAL
);
155 av_log(log_ctx
, AV_LOG_ERROR
,
156 "Missing flag terminator or extraneous data found at the end of flags "
157 "in interval #%d, command #%d\n", interval_count
, cmd_count
);
158 return AVERROR(EINVAL
);
160 (*buf
)++; /* skip "]" */
162 cmd
->flags
= COMMAND_FLAG_ENTER
;
165 *buf
+= strspn(*buf
, SPACES
);
166 cmd
->target
= av_get_token(buf
, COMMAND_DELIMS
);
167 if (!cmd
->target
|| !cmd
->target
[0]) {
168 av_log(log_ctx
, AV_LOG_ERROR
,
169 "No target specified in interval #%d, command #%d\n",
170 interval_count
, cmd_count
);
171 ret
= AVERROR(EINVAL
);
175 *buf
+= strspn(*buf
, SPACES
);
176 cmd
->command
= av_get_token(buf
, COMMAND_DELIMS
);
177 if (!cmd
->command
|| !cmd
->command
[0]) {
178 av_log(log_ctx
, AV_LOG_ERROR
,
179 "No command specified in interval #%d, command #%d\n",
180 interval_count
, cmd_count
);
181 ret
= AVERROR(EINVAL
);
185 *buf
+= strspn(*buf
, SPACES
);
186 cmd
->arg
= av_get_token(buf
, COMMAND_DELIMS
);
191 av_freep(&cmd
->target
);
192 av_freep(&cmd
->command
);
197 static int parse_commands(Command
**cmds
, int *nb_cmds
, int interval_count
,
198 const char **buf
, void *log_ctx
)
210 if ((ret
= parse_command(&cmd
, cmd_count
, interval_count
, buf
, log_ctx
)) < 0)
214 /* (re)allocate commands array if required */
216 n
= FFMAX(16, 2*n
); /* first allocation = 16, or double the number */
217 *cmds
= av_realloc_f(*cmds
, n
, 2*sizeof(Command
));
219 av_log(log_ctx
, AV_LOG_ERROR
,
220 "Could not (re)allocate command array\n");
221 return AVERROR(ENOMEM
);
225 (*cmds
)[(*nb_cmds
)++] = cmd
;
227 *buf
+= strspn(*buf
, SPACES
);
228 if (**buf
&& **buf
!= ';' && **buf
!= ',') {
229 av_log(log_ctx
, AV_LOG_ERROR
,
230 "Missing separator or extraneous data found at the end of "
231 "interval #%d, in command #%d\n",
232 interval_count
, cmd_count
);
233 av_log(log_ctx
, AV_LOG_ERROR
,
234 "Command was parsed as: flags:[%s] target:%s command:%s arg:%s\n",
235 make_command_flags_str(&pbuf
, cmd
.flags
), cmd
.target
, cmd
.command
, cmd
.arg
);
236 return AVERROR(EINVAL
);
247 #define DELIMS " \f\t\n\r,;"
249 static int parse_interval(Interval
*interval
, int interval_count
,
250 const char **buf
, void *log_ctx
)
255 *buf
+= strspn(*buf
, SPACES
);
260 memset(interval
, 0, sizeof(Interval
));
261 interval
->index
= interval_count
;
263 /* format: INTERVAL COMMANDS */
266 intervalstr
= av_get_token(buf
, DELIMS
);
267 if (intervalstr
&& intervalstr
[0]) {
270 start
= av_strtok(intervalstr
, "-", &end
);
271 if ((ret
= av_parse_time(&interval
->start_ts
, start
, 1)) < 0) {
272 av_log(log_ctx
, AV_LOG_ERROR
,
273 "Invalid start time specification '%s' in interval #%d\n",
274 start
, interval_count
);
279 if ((ret
= av_parse_time(&interval
->end_ts
, end
, 1)) < 0) {
280 av_log(log_ctx
, AV_LOG_ERROR
,
281 "Invalid end time specification '%s' in interval #%d\n",
282 end
, interval_count
);
286 interval
->end_ts
= INT64_MAX
;
288 if (interval
->end_ts
< interval
->start_ts
) {
289 av_log(log_ctx
, AV_LOG_ERROR
,
290 "Invalid end time '%s' in interval #%d: "
291 "cannot be lesser than start time '%s'\n",
292 end
, interval_count
, start
);
293 ret
= AVERROR(EINVAL
);
297 av_log(log_ctx
, AV_LOG_ERROR
,
298 "No interval specified for interval #%d\n", interval_count
);
299 ret
= AVERROR(EINVAL
);
304 ret
= parse_commands(&interval
->commands
, &interval
->nb_commands
,
305 interval_count
, buf
, log_ctx
);
308 av_free(intervalstr
);
312 static int parse_intervals(Interval
**intervals
, int *nb_intervals
,
313 const char *buf
, void *log_ctx
)
315 int interval_count
= 0;
328 if ((ret
= parse_interval(&interval
, interval_count
, &buf
, log_ctx
)) < 0)
331 buf
+= strspn(buf
, SPACES
);
334 av_log(log_ctx
, AV_LOG_ERROR
,
335 "Missing terminator or extraneous data found at the end of interval #%d\n",
337 return AVERROR(EINVAL
);
339 buf
++; /* skip ';' */
343 /* (re)allocate commands array if required */
344 if (*nb_intervals
== n
) {
345 n
= FFMAX(16, 2*n
); /* first allocation = 16, or double the number */
346 *intervals
= av_realloc_f(*intervals
, n
, 2*sizeof(Interval
));
348 av_log(log_ctx
, AV_LOG_ERROR
,
349 "Could not (re)allocate intervals array\n");
350 return AVERROR(ENOMEM
);
354 (*intervals
)[(*nb_intervals
)++] = interval
;
360 static int cmp_intervals(const void *a
, const void *b
)
362 const Interval
*i1
= a
;
363 const Interval
*i2
= b
;
364 int64_t ts_diff
= i1
->start_ts
- i2
->start_ts
;
367 ret
= ts_diff
> 0 ? 1 : ts_diff
< 0 ? -1 : 0;
368 return ret
== 0 ? i1
->index
- i2
->index
: ret
;
371 static av_cold
int init(AVFilterContext
*ctx
)
373 SendCmdContext
*sendcmd
= ctx
->priv
;
376 if (sendcmd
->commands_filename
&& sendcmd
->commands_str
) {
377 av_log(ctx
, AV_LOG_ERROR
,
378 "Only one of the filename or commands options must be specified\n");
379 return AVERROR(EINVAL
);
382 if (sendcmd
->commands_filename
) {
383 uint8_t *file_buf
, *buf
;
385 ret
= av_file_map(sendcmd
->commands_filename
,
386 &file_buf
, &file_bufsize
, 0, ctx
);
390 /* create a 0-terminated string based on the read file */
391 buf
= av_malloc(file_bufsize
+ 1);
393 av_file_unmap(file_buf
, file_bufsize
);
394 return AVERROR(ENOMEM
);
396 memcpy(buf
, file_buf
, file_bufsize
);
397 buf
[file_bufsize
] = 0;
398 av_file_unmap(file_buf
, file_bufsize
);
399 sendcmd
->commands_str
= buf
;
402 if ((ret
= parse_intervals(&sendcmd
->intervals
, &sendcmd
->nb_intervals
,
403 sendcmd
->commands_str
, ctx
)) < 0)
406 qsort(sendcmd
->intervals
, sendcmd
->nb_intervals
, sizeof(Interval
), cmp_intervals
);
408 av_log(ctx
, AV_LOG_DEBUG
, "Parsed commands:\n");
409 for (i
= 0; i
< sendcmd
->nb_intervals
; i
++) {
411 Interval
*interval
= &sendcmd
->intervals
[i
];
412 av_log(ctx
, AV_LOG_VERBOSE
, "start_time:%f end_time:%f index:%d\n",
413 (double)interval
->start_ts
/1000000, (double)interval
->end_ts
/1000000, interval
->index
);
414 for (j
= 0; j
< interval
->nb_commands
; j
++) {
415 Command
*cmd
= &interval
->commands
[j
];
416 av_log(ctx
, AV_LOG_VERBOSE
,
417 " [%s] target:%s command:%s arg:%s index:%d\n",
418 make_command_flags_str(&pbuf
, cmd
->flags
), cmd
->target
, cmd
->command
, cmd
->arg
, cmd
->index
);
425 static av_cold
void uninit(AVFilterContext
*ctx
)
427 SendCmdContext
*sendcmd
= ctx
->priv
;
430 for (i
= 0; i
< sendcmd
->nb_intervals
; i
++) {
431 Interval
*interval
= &sendcmd
->intervals
[i
];
432 for (j
= 0; j
< interval
->nb_commands
; j
++) {
433 Command
*cmd
= &interval
->commands
[j
];
434 av_free(cmd
->target
);
435 av_free(cmd
->command
);
438 av_free(interval
->commands
);
440 av_freep(&sendcmd
->intervals
);
443 static int filter_frame(AVFilterLink
*inlink
, AVFrame
*ref
)
445 AVFilterContext
*ctx
= inlink
->dst
;
446 SendCmdContext
*sendcmd
= ctx
->priv
;
450 if (ref
->pts
== AV_NOPTS_VALUE
)
453 ts
= av_rescale_q(ref
->pts
, inlink
->time_base
, AV_TIME_BASE_Q
);
455 #define WITHIN_INTERVAL(ts, start_ts, end_ts) ((ts) >= (start_ts) && (ts) < (end_ts))
457 for (i
= 0; i
< sendcmd
->nb_intervals
; i
++) {
458 Interval
*interval
= &sendcmd
->intervals
[i
];
461 if (!interval
->enabled
&& WITHIN_INTERVAL(ts
, interval
->start_ts
, interval
->end_ts
)) {
462 flags
+= COMMAND_FLAG_ENTER
;
463 interval
->enabled
= 1;
465 if (interval
->enabled
&& !WITHIN_INTERVAL(ts
, interval
->start_ts
, interval
->end_ts
)) {
466 flags
+= COMMAND_FLAG_LEAVE
;
467 interval
->enabled
= 0;
472 av_log(ctx
, AV_LOG_VERBOSE
,
473 "[%s] interval #%d start_ts:%f end_ts:%f ts:%f\n",
474 make_command_flags_str(&pbuf
, flags
), interval
->index
,
475 (double)interval
->start_ts
/1000000, (double)interval
->end_ts
/1000000,
478 for (j
= 0; flags
&& j
< interval
->nb_commands
; j
++) {
479 Command
*cmd
= &interval
->commands
[j
];
482 if (cmd
->flags
& flags
) {
483 av_log(ctx
, AV_LOG_VERBOSE
,
484 "Processing command #%d target:%s command:%s arg:%s\n",
485 cmd
->index
, cmd
->target
, cmd
->command
, cmd
->arg
);
486 ret
= avfilter_graph_send_command(inlink
->graph
,
487 cmd
->target
, cmd
->command
, cmd
->arg
,
489 AVFILTER_CMD_FLAG_ONE
);
490 av_log(ctx
, AV_LOG_VERBOSE
,
491 "Command reply for command #%d: ret:%s res:%s\n",
492 cmd
->index
, av_err2str(ret
), buf
);
499 switch (inlink
->type
) {
500 case AVMEDIA_TYPE_VIDEO
:
501 case AVMEDIA_TYPE_AUDIO
:
502 return ff_filter_frame(inlink
->dst
->outputs
[0], ref
);
505 return AVERROR(ENOSYS
);
508 #if CONFIG_SENDCMD_FILTER
510 #define sendcmd_options options
511 AVFILTER_DEFINE_CLASS(sendcmd
);
513 static const AVFilterPad sendcmd_inputs
[] = {
516 .type
= AVMEDIA_TYPE_VIDEO
,
517 .filter_frame
= filter_frame
,
522 static const AVFilterPad sendcmd_outputs
[] = {
525 .type
= AVMEDIA_TYPE_VIDEO
,
530 AVFilter ff_vf_sendcmd
= {
532 .description
= NULL_IF_CONFIG_SMALL("Send commands to filters."),
535 .priv_size
= sizeof(SendCmdContext
),
536 .inputs
= sendcmd_inputs
,
537 .outputs
= sendcmd_outputs
,
538 .priv_class
= &sendcmd_class
,
543 #if CONFIG_ASENDCMD_FILTER
545 #define asendcmd_options options
546 AVFILTER_DEFINE_CLASS(asendcmd
);
548 static const AVFilterPad asendcmd_inputs
[] = {
551 .type
= AVMEDIA_TYPE_AUDIO
,
552 .filter_frame
= filter_frame
,
557 static const AVFilterPad asendcmd_outputs
[] = {
560 .type
= AVMEDIA_TYPE_AUDIO
,
565 AVFilter ff_af_asendcmd
= {
567 .description
= NULL_IF_CONFIG_SMALL("Send commands to filters."),
570 .priv_size
= sizeof(SendCmdContext
),
571 .inputs
= asendcmd_inputs
,
572 .outputs
= asendcmd_outputs
,
573 .priv_class
= &asendcmd_class
,