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
36
37 #define COMMAND_FLAG_ENTER 1
38 #define COMMAND_FLAG_LEAVE 2
39
41 {
42 static const char * const flag_strings[] = { "enter", "leave" };
43 int i, is_first = 1;
44
47 if (flags & 1<<i) {
48 if (!is_first)
51 is_first = 0;
52 }
53 }
54
55 return pbuf->str;
56 }
57
63
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
72
77
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
90 };
91
92 #define SPACES " \f\t\n\r"
93
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
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
133 else {
134 char flag_buf[64];
137 "Unknown flag '%s' in interval #%d, command #%d\n",
138 flag_buf, interval_count, cmd_count);
140 }
142 if (**buf == ']')
143 break;
144 if (!strspn(*buf, "+|")) {
146 "Invalid flags char '%c' in interval #%d, command #%d\n",
147 **buf, interval_count, cmd_count);
149 }
150 if (**buf)
151 (*buf)++;
152 }
153
154 if (**buf != ']') {
156 "Missing flag terminator or extraneous data found at the end of flags "
157 "in interval #%d, command #%d\n", interval_count, cmd_count);
159 }
160 (*buf)++; /* skip "]" */
161 } else {
163 }
164
165 *buf += strspn(*buf,
SPACES);
169 "No target specified in interval #%d, command #%d\n",
170 interval_count, cmd_count);
173 }
174
175 *buf += strspn(*buf,
SPACES);
179 "No command specified in interval #%d, command #%d\n",
180 interval_count, cmd_count);
183 }
184
185 *buf += strspn(*buf,
SPACES);
187
188 return 1;
189
194 return ret;
195 }
196
198 const char **
buf,
void *log_ctx)
199 {
200 int cmd_count = 0;
202 AVBPrint pbuf;
203
205 *nb_cmds = 0;
206
207 while (**buf) {
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 */
218 if (!*cmds) {
220 "Could not (re)allocate command array\n");
222 }
223 }
224
225 (*cmds)[(*nb_cmds)++] = cmd;
226
227 *buf += strspn(*buf,
SPACES);
228 if (**buf && **buf != ';' && **buf != ',') {
230 "Missing separator or extraneous data found at the end of "
231 "interval #%d, in command #%d\n",
232 interval_count, cmd_count);
234 "Command was parsed as: flags:[%s] target:%s command:%s arg:%s\n",
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
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 */
267 if (intervalstr && intervalstr[0]) {
269
270 start =
av_strtok(intervalstr,
"-", &end);
273 "Invalid start time specification '%s' in interval #%d\n",
274 start, interval_count);
276 }
277
278 if (end) {
281 "Invalid end time specification '%s' in interval #%d\n",
282 end, interval_count);
284 }
285 } else {
286 interval->
end_ts = INT64_MAX;
287 }
290 "Invalid end time '%s' in interval #%d: "
291 "cannot be lesser than start time '%s'\n",
292 end, interval_count, start);
295 }
296 } else {
298 "No interval specified for interval #%d\n", interval_count);
301 }
302
303 /* parse commands */
305 interval_count, buf, log_ctx);
306
309 return ret;
310 }
311
313 const char *
buf,
void *log_ctx)
314 {
315 int interval_count = 0;
317
319 *nb_intervals = 0;
320
321 if (!buf)
322 return 0;
323
324 while (1) {
326
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 != ';') {
338 "Missing terminator or extraneous data found at the end of interval #%d\n",
339 interval_count);
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 */
350 if (!*intervals) {
352 "Could not (re)allocate intervals array\n");
354 }
355 }
356
357 (*intervals)[(*nb_intervals)++] = interval;
358 }
359
360 return 0;
361 }
362
364 {
368 }
369
371 {
373 int ret, i, j;
374
377 "One and only one of the filename or commands options must be specified\n");
379 }
380
383 size_t file_bufsize;
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 */
391 if (!buf) {
394 }
395 memcpy(buf, file_buf, file_bufsize);
396 buf[file_bufsize] = 0;
399 }
400
403 return ret;
404
408 }
409
411
414 AVBPrint pbuf;
421 " [%s] target:%s command:%s arg:%s index:%d\n",
423 }
424 }
425
426 return 0;
427 }
428
430 {
432 int i, j;
433
441 }
443 }
445 }
446
448 {
451 int64_t ts;
452 int i, j, ret;
453
456
458
459 #define WITHIN_INTERVAL(ts, start_ts, end_ts) ((ts) >= (start_ts) && (ts) < (end_ts))
460
464
468 }
472 }
473
474 if (flags) {
475 AVBPrint pbuf;
477 "[%s] interval #%d start_ts:%f end_ts:%f ts:%f\n",
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++) {
485
486 if (cmd->
flags & flags) {
488 "Processing command #%d target:%s command:%s arg:%s\n",
492 buf, sizeof(buf),
495 "Command reply for command #%d: ret:%s res:%s\n",
497 }
498 }
499 }
500 }
501
503 switch (inlink->
type) {
507 }
508
510 }
511
512 #if CONFIG_SENDCMD_FILTER
513
514 #define sendcmd_options options
516
518 {
522 },
524 };
525
527 {
530 },
532 };
533
542 .priv_class = &sendcmd_class,
543 };
544
545 #endif
546
547 #if CONFIG_ASENDCMD_FILTER
548
549 #define asendcmd_options options
551
553 {
557 },
559 };
560
562 {
565 },
567 };
568
575 .
inputs = asendcmd_inputs,
577 .priv_class = &asendcmd_class,
578 };
579
580 #endif
void av_bprintf(AVBPrint *buf, const char *fmt,...)
This structure describes decoded (raw) audio or video data.
#define av_realloc_f(p, o, n)
static int cmp_intervals(const void *a, const void *b)
Main libavfilter public API header.
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
int index
unique index for these interval commands
static int parse_commands(Command **cmds, int *nb_cmds, int interval_count, const char **buf, void *log_ctx)
static int parse_interval(Interval *interval, int interval_count, const char **buf, void *log_ctx)
const char * name
Pad name.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static void skip_comments(const char **buf)
static av_cold int end(AVCodecContext *avctx)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
#define AV_LOG_VERBOSE
Detailed information.
static int parse_intervals(Interval **intervals, int *nb_intervals, const char *buf, void *log_ctx)
A filter pad used for either input or output.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
A link between two filters.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
void * priv
private data for use by the filter
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link...
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
static av_cold void uninit(AVFilterContext *ctx)
#define FFDIFFSIGN(x, y)
Comparator.
int enabled
current time detected inside this interval
static int parse_command(Command *cmd, int cmd_count, int interval_count, const char **buf, void *log_ctx)
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define COMMAND_FLAG_LEAVE
#define AVFILTER_CMD_FLAG_ONE
Stop once a filter understood the command (for target=all for example), fast filters are favored auto...
static const AVFilterPad outputs[]
#define FF_ARRAY_ELEMS(a)
static const AVFilterPad inputs[]
#define AV_BPRINT_SIZE_AUTOMATIC
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Describe the class of an AVClass context structure.
static av_cold int init(AVFilterContext *ctx)
#define COMMAND_FLAG_ENTER
const char * name
Filter name.
static char * make_command_flags_str(AVBPrint *pbuf, int flags)
int64_t end_ts
end timestamp expressed as microseconds units
AVFilterLink ** outputs
array of pointers to output links
enum AVMediaType type
filter media type
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
struct AVFilterGraph * graph
Graph the filter belongs to.
int64_t start_ts
start timestamp expressed as microseconds units
int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
Send a command to one or more filter instances.
AVFilterContext * dst
dest filter
#define AVFILTER_DEFINE_CLASS(fname)
#define WITHIN_INTERVAL(ts, start_ts, end_ts)
static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
#define AV_NOPTS_VALUE
Undefined timestamp value.
static const AVOption options[]
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.