]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_sendcmd.c
lavfi: deprecate avfilter_link_set_closed().
[ffmpeg] / libavfilter / f_sendcmd.c
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 "libavutil/avstring.h"
27 #include "libavutil/bprint.h"
28 #include "libavutil/file.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/parseutils.h"
31 #include "avfilter.h"
32 #include "internal.h"
33 #include "avfiltergraph.h"
34 #include "audio.h"
35 #include "video.h"
36
37 #define COMMAND_FLAG_ENTER 1
38 #define COMMAND_FLAG_LEAVE 2
39
40 static inline char *make_command_flags_str(AVBPrint *pbuf, int flags)
41 {
42     static const char * const flag_strings[] = { "enter", "leave" };
43     int i, is_first = 1;
44
45     av_bprint_init(pbuf, 0, AV_BPRINT_SIZE_AUTOMATIC);
46     for (i = 0; i < FF_ARRAY_ELEMS(flag_strings); i++) {
47         if (flags & 1<<i) {
48             if (!is_first)
49                 av_bprint_chars(pbuf, '+', 1);
50             av_bprintf(pbuf, "%s", flag_strings[i]);
51             is_first = 0;
52         }
53     }
54
55     return pbuf->str;
56 }
57
58 typedef struct {
59     int flags;
60     char *target, *command, *arg;
61     int index;
62 } Command;
63
64 typedef struct {
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
68     Command *commands;
69     int   nb_commands;
70     int enabled;               ///< current time detected inside this interval
71 } Interval;
72
73 typedef struct {
74     const AVClass *class;
75     Interval *intervals;
76     int   nb_intervals;
77
78     char *commands_filename;
79     char *commands_str;
80 } SendCmdContext;
81
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 },
89     { NULL }
90 };
91
92 #define SPACES " \f\t\n\r"
93
94 static void skip_comments(const char **buf)
95 {
96     while (**buf) {
97         /* skip leading spaces */
98         *buf += strspn(*buf, SPACES);
99         if (**buf != '#')
100             break;
101
102         (*buf)++;
103
104         /* skip comment until the end of line */
105         *buf += strcspn(*buf, "\n");
106         if (**buf)
107             (*buf)++;
108     }
109 }
110
111 #define COMMAND_DELIMS " \f\t\n\r,;"
112
113 static int parse_command(Command *cmd, int cmd_count, int interval_count,
114                          const char **buf, void *log_ctx)
115 {
116     int ret;
117
118     memset(cmd, 0, sizeof(Command));
119     cmd->index = cmd_count;
120
121     /* format: [FLAGS] target command arg */
122     *buf += strspn(*buf, SPACES);
123
124     /* parse flags */
125     if (**buf == '[') {
126         (*buf)++; /* skip "[" */
127
128         while (**buf) {
129             int len = strcspn(*buf, "|+]");
130
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;
133             else {
134                 char flag_buf[64];
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);
140             }
141             *buf += len;
142             if (**buf == ']')
143                 break;
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);
149             }
150             if (**buf)
151                 (*buf)++;
152         }
153
154         if (**buf != ']') {
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);
159         }
160         (*buf)++; /* skip "]" */
161     } else {
162         cmd->flags = COMMAND_FLAG_ENTER;
163     }
164
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);
172         goto fail;
173     }
174
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);
182         goto fail;
183     }
184
185     *buf += strspn(*buf, SPACES);
186     cmd->arg = av_get_token(buf, COMMAND_DELIMS);
187
188     return 1;
189
190 fail:
191     av_freep(&cmd->target);
192     av_freep(&cmd->command);
193     av_freep(&cmd->arg);
194     return ret;
195 }
196
197 static int parse_commands(Command **cmds, int *nb_cmds, int interval_count,
198                           const char **buf, void *log_ctx)
199 {
200     int cmd_count = 0;
201     int ret, n = 0;
202     AVBPrint pbuf;
203
204     *cmds = NULL;
205     *nb_cmds = 0;
206
207     while (**buf) {
208         Command cmd;
209
210         if ((ret = parse_command(&cmd, cmd_count, interval_count, buf, log_ctx)) < 0)
211             return ret;
212         cmd_count++;
213
214         /* (re)allocate commands array if required */
215         if (*nb_cmds == n) {
216             n = FFMAX(16, 2*n); /* first allocation = 16, or double the number */
217             *cmds = av_realloc_f(*cmds, n, 2*sizeof(Command));
218             if (!*cmds) {
219                 av_log(log_ctx, AV_LOG_ERROR,
220                        "Could not (re)allocate command array\n");
221                 return AVERROR(ENOMEM);
222             }
223         }
224
225         (*cmds)[(*nb_cmds)++] = cmd;
226
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);
237         }
238         if (**buf == ';')
239             break;
240         if (**buf == ',')
241             (*buf)++;
242     }
243
244     return 0;
245 }
246
247 #define DELIMS " \f\t\n\r,;"
248
249 static int parse_interval(Interval *interval, int interval_count,
250                           const char **buf, void *log_ctx)
251 {
252     char *intervalstr;
253     int ret;
254
255     *buf += strspn(*buf, SPACES);
256     if (!**buf)
257         return 0;
258
259     /* reset data */
260     memset(interval, 0, sizeof(Interval));
261     interval->index = interval_count;
262
263     /* format: INTERVAL COMMANDS */
264
265     /* parse interval */
266     intervalstr = av_get_token(buf, DELIMS);
267     if (intervalstr && intervalstr[0]) {
268         char *start, *end;
269
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);
275             goto end;
276         }
277
278         if (end) {
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);
283                 goto end;
284             }
285         } else {
286             interval->end_ts = INT64_MAX;
287         }
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);
294             goto end;
295         }
296     } else {
297         av_log(log_ctx, AV_LOG_ERROR,
298                "No interval specified for interval #%d\n", interval_count);
299         ret = AVERROR(EINVAL);
300         goto end;
301     }
302
303     /* parse commands */
304     ret = parse_commands(&interval->commands, &interval->nb_commands,
305                          interval_count, buf, log_ctx);
306
307 end:
308     av_free(intervalstr);
309     return ret;
310 }
311
312 static int parse_intervals(Interval **intervals, int *nb_intervals,
313                            const char *buf, void *log_ctx)
314 {
315     int interval_count = 0;
316     int ret, n = 0;
317
318     *intervals = NULL;
319     *nb_intervals = 0;
320
321     if (!buf)
322         return 0;
323
324     while (1) {
325         Interval interval;
326
327         skip_comments(&buf);
328         if (!(*buf))
329             break;
330
331         if ((ret = parse_interval(&interval, interval_count, &buf, log_ctx)) < 0)
332             return ret;
333
334         buf += strspn(buf, SPACES);
335         if (*buf) {
336             if (*buf != ';') {
337                 av_log(log_ctx, AV_LOG_ERROR,
338                        "Missing terminator or extraneous data found at the end of interval #%d\n",
339                        interval_count);
340                 return AVERROR(EINVAL);
341             }
342             buf++; /* skip ';' */
343         }
344         interval_count++;
345
346         /* (re)allocate commands array if required */
347         if (*nb_intervals == n) {
348             n = FFMAX(16, 2*n); /* first allocation = 16, or double the number */
349             *intervals = av_realloc_f(*intervals, n, 2*sizeof(Interval));
350             if (!*intervals) {
351                 av_log(log_ctx, AV_LOG_ERROR,
352                        "Could not (re)allocate intervals array\n");
353                 return AVERROR(ENOMEM);
354             }
355         }
356
357         (*intervals)[(*nb_intervals)++] = interval;
358     }
359
360     return 0;
361 }
362
363 static int cmp_intervals(const void *a, const void *b)
364 {
365     const Interval *i1 = a;
366     const Interval *i2 = b;
367     return 2 * FFDIFFSIGN(i1->start_ts, i2->start_ts) + FFDIFFSIGN(i1->index, i2->index);
368 }
369
370 static av_cold int init(AVFilterContext *ctx)
371 {
372     SendCmdContext *s = ctx->priv;
373     int ret, i, j;
374
375     if ((!!s->commands_filename + !!s->commands_str) != 1) {
376         av_log(ctx, AV_LOG_ERROR,
377                "One and only one of the filename or commands options must be specified\n");
378         return AVERROR(EINVAL);
379     }
380
381     if (s->commands_filename) {
382         uint8_t *file_buf, *buf;
383         size_t file_bufsize;
384         ret = av_file_map(s->commands_filename,
385                           &file_buf, &file_bufsize, 0, ctx);
386         if (ret < 0)
387             return ret;
388
389         /* create a 0-terminated string based on the read file */
390         buf = av_malloc(file_bufsize + 1);
391         if (!buf) {
392             av_file_unmap(file_buf, file_bufsize);
393             return AVERROR(ENOMEM);
394         }
395         memcpy(buf, file_buf, file_bufsize);
396         buf[file_bufsize] = 0;
397         av_file_unmap(file_buf, file_bufsize);
398         s->commands_str = buf;
399     }
400
401     if ((ret = parse_intervals(&s->intervals, &s->nb_intervals,
402                                s->commands_str, ctx)) < 0)
403         return ret;
404
405     if (s->nb_intervals == 0) {
406         av_log(ctx, AV_LOG_ERROR, "No commands were specified\n");
407         return AVERROR(EINVAL);
408     }
409
410     qsort(s->intervals, s->nb_intervals, sizeof(Interval), cmp_intervals);
411
412     av_log(ctx, AV_LOG_DEBUG, "Parsed commands:\n");
413     for (i = 0; i < s->nb_intervals; i++) {
414         AVBPrint pbuf;
415         Interval *interval = &s->intervals[i];
416         av_log(ctx, AV_LOG_VERBOSE, "start_time:%f end_time:%f index:%d\n",
417                (double)interval->start_ts/1000000, (double)interval->end_ts/1000000, interval->index);
418         for (j = 0; j < interval->nb_commands; j++) {
419             Command *cmd = &interval->commands[j];
420             av_log(ctx, AV_LOG_VERBOSE,
421                    "    [%s] target:%s command:%s arg:%s index:%d\n",
422                    make_command_flags_str(&pbuf, cmd->flags), cmd->target, cmd->command, cmd->arg, cmd->index);
423         }
424     }
425
426     return 0;
427 }
428
429 static av_cold void uninit(AVFilterContext *ctx)
430 {
431     SendCmdContext *s = ctx->priv;
432     int i, j;
433
434     for (i = 0; i < s->nb_intervals; i++) {
435         Interval *interval = &s->intervals[i];
436         for (j = 0; j < interval->nb_commands; j++) {
437             Command *cmd = &interval->commands[j];
438             av_freep(&cmd->target);
439             av_freep(&cmd->command);
440             av_freep(&cmd->arg);
441         }
442         av_freep(&interval->commands);
443     }
444     av_freep(&s->intervals);
445 }
446
447 static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
448 {
449     AVFilterContext *ctx = inlink->dst;
450     SendCmdContext *s = ctx->priv;
451     int64_t ts;
452     int i, j, ret;
453
454     if (ref->pts == AV_NOPTS_VALUE)
455         goto end;
456
457     ts = av_rescale_q(ref->pts, inlink->time_base, AV_TIME_BASE_Q);
458
459 #define WITHIN_INTERVAL(ts, start_ts, end_ts) ((ts) >= (start_ts) && (ts) < (end_ts))
460
461     for (i = 0; i < s->nb_intervals; i++) {
462         Interval *interval = &s->intervals[i];
463         int flags = 0;
464
465         if (!interval->enabled && WITHIN_INTERVAL(ts, interval->start_ts, interval->end_ts)) {
466             flags += COMMAND_FLAG_ENTER;
467             interval->enabled = 1;
468         }
469         if (interval->enabled && !WITHIN_INTERVAL(ts, interval->start_ts, interval->end_ts)) {
470             flags += COMMAND_FLAG_LEAVE;
471             interval->enabled = 0;
472         }
473
474         if (flags) {
475             AVBPrint pbuf;
476             av_log(ctx, AV_LOG_VERBOSE,
477                    "[%s] interval #%d start_ts:%f end_ts:%f ts:%f\n",
478                    make_command_flags_str(&pbuf, flags), interval->index,
479                    (double)interval->start_ts/1000000, (double)interval->end_ts/1000000,
480                    (double)ts/1000000);
481
482             for (j = 0; flags && j < interval->nb_commands; j++) {
483                 Command *cmd = &interval->commands[j];
484                 char buf[1024];
485
486                 if (cmd->flags & flags) {
487                     av_log(ctx, AV_LOG_VERBOSE,
488                            "Processing command #%d target:%s command:%s arg:%s\n",
489                            cmd->index, cmd->target, cmd->command, cmd->arg);
490                     ret = avfilter_graph_send_command(inlink->graph,
491                                                       cmd->target, cmd->command, cmd->arg,
492                                                       buf, sizeof(buf),
493                                                       AVFILTER_CMD_FLAG_ONE);
494                     av_log(ctx, AV_LOG_VERBOSE,
495                            "Command reply for command #%d: ret:%s res:%s\n",
496                            cmd->index, av_err2str(ret), buf);
497                 }
498             }
499         }
500     }
501
502 end:
503     switch (inlink->type) {
504     case AVMEDIA_TYPE_VIDEO:
505     case AVMEDIA_TYPE_AUDIO:
506         return ff_filter_frame(inlink->dst->outputs[0], ref);
507     }
508
509     return AVERROR(ENOSYS);
510 }
511
512 #if CONFIG_SENDCMD_FILTER
513
514 #define sendcmd_options options
515 AVFILTER_DEFINE_CLASS(sendcmd);
516
517 static const AVFilterPad sendcmd_inputs[] = {
518     {
519         .name         = "default",
520         .type         = AVMEDIA_TYPE_VIDEO,
521         .filter_frame = filter_frame,
522     },
523     { NULL }
524 };
525
526 static const AVFilterPad sendcmd_outputs[] = {
527     {
528         .name = "default",
529         .type = AVMEDIA_TYPE_VIDEO,
530     },
531     { NULL }
532 };
533
534 AVFilter ff_vf_sendcmd = {
535     .name        = "sendcmd",
536     .description = NULL_IF_CONFIG_SMALL("Send commands to filters."),
537     .init        = init,
538     .uninit      = uninit,
539     .priv_size   = sizeof(SendCmdContext),
540     .inputs      = sendcmd_inputs,
541     .outputs     = sendcmd_outputs,
542     .priv_class  = &sendcmd_class,
543 };
544
545 #endif
546
547 #if CONFIG_ASENDCMD_FILTER
548
549 #define asendcmd_options options
550 AVFILTER_DEFINE_CLASS(asendcmd);
551
552 static const AVFilterPad asendcmd_inputs[] = {
553     {
554         .name         = "default",
555         .type         = AVMEDIA_TYPE_AUDIO,
556         .filter_frame = filter_frame,
557     },
558     { NULL }
559 };
560
561 static const AVFilterPad asendcmd_outputs[] = {
562     {
563         .name = "default",
564         .type = AVMEDIA_TYPE_AUDIO,
565     },
566     { NULL }
567 };
568
569 AVFilter ff_af_asendcmd = {
570     .name        = "asendcmd",
571     .description = NULL_IF_CONFIG_SMALL("Send commands to filters."),
572     .init        = init,
573     .uninit      = uninit,
574     .priv_size   = sizeof(SendCmdContext),
575     .inputs      = asendcmd_inputs,
576     .outputs     = asendcmd_outputs,
577     .priv_class  = &asendcmd_class,
578 };
579
580 #endif