You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

588 lines
17KB

  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. * @file
  22. * send commands filter
  23. */
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/bprint.h"
  26. #include "libavutil/file.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/parseutils.h"
  29. #include "avfilter.h"
  30. #include "internal.h"
  31. #include "avfiltergraph.h"
  32. #include "audio.h"
  33. #include "video.h"
  34. #define COMMAND_FLAG_ENTER 1
  35. #define COMMAND_FLAG_LEAVE 2
  36. static inline char *make_command_flags_str(AVBPrint *pbuf, int flags)
  37. {
  38. static const char * const flag_strings[] = { "enter", "leave" };
  39. int i, is_first = 1;
  40. av_bprint_init(pbuf, 0, AV_BPRINT_SIZE_AUTOMATIC);
  41. for (i = 0; i < FF_ARRAY_ELEMS(flag_strings); i++) {
  42. if (flags & 1<<i) {
  43. if (!is_first)
  44. av_bprint_chars(pbuf, '+', 1);
  45. av_bprintf(pbuf, "%s", flag_strings[i]);
  46. is_first = 0;
  47. }
  48. }
  49. return pbuf->str;
  50. }
  51. typedef struct {
  52. int flags;
  53. char *target, *command, *arg;
  54. int index;
  55. } Command;
  56. typedef struct {
  57. int64_t start_ts; ///< start timestamp expressed as microseconds units
  58. int64_t end_ts; ///< end timestamp expressed as microseconds units
  59. int index; ///< unique index for these interval commands
  60. Command *commands;
  61. int nb_commands;
  62. int enabled; ///< current time detected inside this interval
  63. } Interval;
  64. typedef struct {
  65. const AVClass *class;
  66. Interval *intervals;
  67. int nb_intervals;
  68. char *commands_filename;
  69. char *commands_str;
  70. } SendCmdContext;
  71. #define OFFSET(x) offsetof(SendCmdContext, x)
  72. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  73. static const AVOption options[] = {
  74. { "commands", "set commands", OFFSET(commands_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  75. { "c", "set commands", OFFSET(commands_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  76. { "filename", "set commands file", OFFSET(commands_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  77. { "f", "set commands file", OFFSET(commands_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  78. { NULL }
  79. };
  80. #define SPACES " \f\t\n\r"
  81. static void skip_comments(const char **buf)
  82. {
  83. while (**buf) {
  84. /* skip leading spaces */
  85. *buf += strspn(*buf, SPACES);
  86. if (**buf != '#')
  87. break;
  88. (*buf)++;
  89. /* skip comment until the end of line */
  90. *buf += strcspn(*buf, "\n");
  91. if (**buf)
  92. (*buf)++;
  93. }
  94. }
  95. #define COMMAND_DELIMS " \f\t\n\r,;"
  96. static int parse_command(Command *cmd, int cmd_count, int interval_count,
  97. const char **buf, void *log_ctx)
  98. {
  99. int ret;
  100. memset(cmd, 0, sizeof(Command));
  101. cmd->index = cmd_count;
  102. /* format: [FLAGS] target command arg */
  103. *buf += strspn(*buf, SPACES);
  104. /* parse flags */
  105. if (**buf == '[') {
  106. (*buf)++; /* skip "[" */
  107. while (**buf) {
  108. int len = strcspn(*buf, "|+]");
  109. if (!strncmp(*buf, "enter", strlen("enter"))) cmd->flags |= COMMAND_FLAG_ENTER;
  110. else if (!strncmp(*buf, "leave", strlen("leave"))) cmd->flags |= COMMAND_FLAG_LEAVE;
  111. else {
  112. char flag_buf[64];
  113. av_strlcpy(flag_buf, *buf, sizeof(flag_buf));
  114. av_log(log_ctx, AV_LOG_ERROR,
  115. "Unknown flag '%s' in interval #%d, command #%d\n",
  116. flag_buf, interval_count, cmd_count);
  117. return AVERROR(EINVAL);
  118. }
  119. *buf += len;
  120. if (**buf == ']')
  121. break;
  122. if (!strspn(*buf, "+|")) {
  123. av_log(log_ctx, AV_LOG_ERROR,
  124. "Invalid flags char '%c' in interval #%d, command #%d\n",
  125. **buf, interval_count, cmd_count);
  126. return AVERROR(EINVAL);
  127. }
  128. if (**buf)
  129. (*buf)++;
  130. }
  131. if (**buf != ']') {
  132. av_log(log_ctx, AV_LOG_ERROR,
  133. "Missing flag terminator or extraneous data found at the end of flags "
  134. "in interval #%d, command #%d\n", interval_count, cmd_count);
  135. return AVERROR(EINVAL);
  136. }
  137. (*buf)++; /* skip "]" */
  138. } else {
  139. cmd->flags = COMMAND_FLAG_ENTER;
  140. }
  141. *buf += strspn(*buf, SPACES);
  142. cmd->target = av_get_token(buf, COMMAND_DELIMS);
  143. if (!cmd->target || !cmd->target[0]) {
  144. av_log(log_ctx, AV_LOG_ERROR,
  145. "No target specified in interval #%d, command #%d\n",
  146. interval_count, cmd_count);
  147. ret = AVERROR(EINVAL);
  148. goto fail;
  149. }
  150. *buf += strspn(*buf, SPACES);
  151. cmd->command = av_get_token(buf, COMMAND_DELIMS);
  152. if (!cmd->command || !cmd->command[0]) {
  153. av_log(log_ctx, AV_LOG_ERROR,
  154. "No command specified in interval #%d, command #%d\n",
  155. interval_count, cmd_count);
  156. ret = AVERROR(EINVAL);
  157. goto fail;
  158. }
  159. *buf += strspn(*buf, SPACES);
  160. cmd->arg = av_get_token(buf, COMMAND_DELIMS);
  161. return 1;
  162. fail:
  163. av_freep(&cmd->target);
  164. av_freep(&cmd->command);
  165. av_freep(&cmd->arg);
  166. return ret;
  167. }
  168. static int parse_commands(Command **cmds, int *nb_cmds, int interval_count,
  169. const char **buf, void *log_ctx)
  170. {
  171. int cmd_count = 0;
  172. int ret, n = 0;
  173. AVBPrint pbuf;
  174. *cmds = NULL;
  175. *nb_cmds = 0;
  176. while (**buf) {
  177. Command cmd;
  178. if ((ret = parse_command(&cmd, cmd_count, interval_count, buf, log_ctx)) < 0)
  179. return ret;
  180. cmd_count++;
  181. /* (re)allocate commands array if required */
  182. if (*nb_cmds == n) {
  183. n = FFMAX(16, 2*n); /* first allocation = 16, or double the number */
  184. *cmds = av_realloc_f(*cmds, n, 2*sizeof(Command));
  185. if (!*cmds) {
  186. av_log(log_ctx, AV_LOG_ERROR,
  187. "Could not (re)allocate command array\n");
  188. return AVERROR(ENOMEM);
  189. }
  190. }
  191. (*cmds)[(*nb_cmds)++] = cmd;
  192. *buf += strspn(*buf, SPACES);
  193. if (**buf && **buf != ';' && **buf != ',') {
  194. av_log(log_ctx, AV_LOG_ERROR,
  195. "Missing separator or extraneous data found at the end of "
  196. "interval #%d, in command #%d\n",
  197. interval_count, cmd_count);
  198. av_log(log_ctx, AV_LOG_ERROR,
  199. "Command was parsed as: flags:[%s] target:%s command:%s arg:%s\n",
  200. make_command_flags_str(&pbuf, cmd.flags), cmd.target, cmd.command, cmd.arg);
  201. return AVERROR(EINVAL);
  202. }
  203. if (**buf == ';')
  204. break;
  205. if (**buf == ',')
  206. (*buf)++;
  207. }
  208. return 0;
  209. }
  210. #define DELIMS " \f\t\n\r,;"
  211. static int parse_interval(Interval *interval, int interval_count,
  212. const char **buf, void *log_ctx)
  213. {
  214. char *intervalstr;
  215. int ret;
  216. *buf += strspn(*buf, SPACES);
  217. if (!**buf)
  218. return 0;
  219. /* reset data */
  220. memset(interval, 0, sizeof(Interval));
  221. interval->index = interval_count;
  222. /* format: INTERVAL COMMANDS */
  223. /* parse interval */
  224. intervalstr = av_get_token(buf, DELIMS);
  225. if (intervalstr && intervalstr[0]) {
  226. char *start, *end;
  227. start = av_strtok(intervalstr, "-", &end);
  228. if (!start) {
  229. ret = AVERROR(EINVAL);
  230. av_log(log_ctx, AV_LOG_ERROR,
  231. "Invalid interval specification '%s' in interval #%d\n",
  232. intervalstr, interval_count);
  233. goto end;
  234. }
  235. if ((ret = av_parse_time(&interval->start_ts, start, 1)) < 0) {
  236. av_log(log_ctx, AV_LOG_ERROR,
  237. "Invalid start time specification '%s' in interval #%d\n",
  238. start, interval_count);
  239. goto end;
  240. }
  241. if (end) {
  242. if ((ret = av_parse_time(&interval->end_ts, end, 1)) < 0) {
  243. av_log(log_ctx, AV_LOG_ERROR,
  244. "Invalid end time specification '%s' in interval #%d\n",
  245. end, interval_count);
  246. goto end;
  247. }
  248. } else {
  249. interval->end_ts = INT64_MAX;
  250. }
  251. if (interval->end_ts < interval->start_ts) {
  252. av_log(log_ctx, AV_LOG_ERROR,
  253. "Invalid end time '%s' in interval #%d: "
  254. "cannot be lesser than start time '%s'\n",
  255. end, interval_count, start);
  256. ret = AVERROR(EINVAL);
  257. goto end;
  258. }
  259. } else {
  260. av_log(log_ctx, AV_LOG_ERROR,
  261. "No interval specified for interval #%d\n", interval_count);
  262. ret = AVERROR(EINVAL);
  263. goto end;
  264. }
  265. /* parse commands */
  266. ret = parse_commands(&interval->commands, &interval->nb_commands,
  267. interval_count, buf, log_ctx);
  268. end:
  269. av_free(intervalstr);
  270. return ret;
  271. }
  272. static int parse_intervals(Interval **intervals, int *nb_intervals,
  273. const char *buf, void *log_ctx)
  274. {
  275. int interval_count = 0;
  276. int ret, n = 0;
  277. *intervals = NULL;
  278. *nb_intervals = 0;
  279. if (!buf)
  280. return 0;
  281. while (1) {
  282. Interval interval;
  283. skip_comments(&buf);
  284. if (!(*buf))
  285. break;
  286. if ((ret = parse_interval(&interval, interval_count, &buf, log_ctx)) < 0)
  287. return ret;
  288. buf += strspn(buf, SPACES);
  289. if (*buf) {
  290. if (*buf != ';') {
  291. av_log(log_ctx, AV_LOG_ERROR,
  292. "Missing terminator or extraneous data found at the end of interval #%d\n",
  293. interval_count);
  294. return AVERROR(EINVAL);
  295. }
  296. buf++; /* skip ';' */
  297. }
  298. interval_count++;
  299. /* (re)allocate commands array if required */
  300. if (*nb_intervals == n) {
  301. n = FFMAX(16, 2*n); /* first allocation = 16, or double the number */
  302. *intervals = av_realloc_f(*intervals, n, 2*sizeof(Interval));
  303. if (!*intervals) {
  304. av_log(log_ctx, AV_LOG_ERROR,
  305. "Could not (re)allocate intervals array\n");
  306. return AVERROR(ENOMEM);
  307. }
  308. }
  309. (*intervals)[(*nb_intervals)++] = interval;
  310. }
  311. return 0;
  312. }
  313. static int cmp_intervals(const void *a, const void *b)
  314. {
  315. const Interval *i1 = a;
  316. const Interval *i2 = b;
  317. return 2 * FFDIFFSIGN(i1->start_ts, i2->start_ts) + FFDIFFSIGN(i1->index, i2->index);
  318. }
  319. static av_cold int init(AVFilterContext *ctx)
  320. {
  321. SendCmdContext *s = ctx->priv;
  322. int ret, i, j;
  323. if ((!!s->commands_filename + !!s->commands_str) != 1) {
  324. av_log(ctx, AV_LOG_ERROR,
  325. "One and only one of the filename or commands options must be specified\n");
  326. return AVERROR(EINVAL);
  327. }
  328. if (s->commands_filename) {
  329. uint8_t *file_buf, *buf;
  330. size_t file_bufsize;
  331. ret = av_file_map(s->commands_filename,
  332. &file_buf, &file_bufsize, 0, ctx);
  333. if (ret < 0)
  334. return ret;
  335. /* create a 0-terminated string based on the read file */
  336. buf = av_malloc(file_bufsize + 1);
  337. if (!buf) {
  338. av_file_unmap(file_buf, file_bufsize);
  339. return AVERROR(ENOMEM);
  340. }
  341. memcpy(buf, file_buf, file_bufsize);
  342. buf[file_bufsize] = 0;
  343. av_file_unmap(file_buf, file_bufsize);
  344. s->commands_str = buf;
  345. }
  346. if ((ret = parse_intervals(&s->intervals, &s->nb_intervals,
  347. s->commands_str, ctx)) < 0)
  348. return ret;
  349. if (s->nb_intervals == 0) {
  350. av_log(ctx, AV_LOG_ERROR, "No commands were specified\n");
  351. return AVERROR(EINVAL);
  352. }
  353. qsort(s->intervals, s->nb_intervals, sizeof(Interval), cmp_intervals);
  354. av_log(ctx, AV_LOG_DEBUG, "Parsed commands:\n");
  355. for (i = 0; i < s->nb_intervals; i++) {
  356. AVBPrint pbuf;
  357. Interval *interval = &s->intervals[i];
  358. av_log(ctx, AV_LOG_VERBOSE, "start_time:%f end_time:%f index:%d\n",
  359. (double)interval->start_ts/1000000, (double)interval->end_ts/1000000, interval->index);
  360. for (j = 0; j < interval->nb_commands; j++) {
  361. Command *cmd = &interval->commands[j];
  362. av_log(ctx, AV_LOG_VERBOSE,
  363. " [%s] target:%s command:%s arg:%s index:%d\n",
  364. make_command_flags_str(&pbuf, cmd->flags), cmd->target, cmd->command, cmd->arg, cmd->index);
  365. }
  366. }
  367. return 0;
  368. }
  369. static av_cold void uninit(AVFilterContext *ctx)
  370. {
  371. SendCmdContext *s = ctx->priv;
  372. int i, j;
  373. for (i = 0; i < s->nb_intervals; i++) {
  374. Interval *interval = &s->intervals[i];
  375. for (j = 0; j < interval->nb_commands; j++) {
  376. Command *cmd = &interval->commands[j];
  377. av_freep(&cmd->target);
  378. av_freep(&cmd->command);
  379. av_freep(&cmd->arg);
  380. }
  381. av_freep(&interval->commands);
  382. }
  383. av_freep(&s->intervals);
  384. }
  385. static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
  386. {
  387. AVFilterContext *ctx = inlink->dst;
  388. SendCmdContext *s = ctx->priv;
  389. int64_t ts;
  390. int i, j, ret;
  391. if (ref->pts == AV_NOPTS_VALUE)
  392. goto end;
  393. ts = av_rescale_q(ref->pts, inlink->time_base, AV_TIME_BASE_Q);
  394. #define WITHIN_INTERVAL(ts, start_ts, end_ts) ((ts) >= (start_ts) && (ts) < (end_ts))
  395. for (i = 0; i < s->nb_intervals; i++) {
  396. Interval *interval = &s->intervals[i];
  397. int flags = 0;
  398. if (!interval->enabled && WITHIN_INTERVAL(ts, interval->start_ts, interval->end_ts)) {
  399. flags += COMMAND_FLAG_ENTER;
  400. interval->enabled = 1;
  401. }
  402. if (interval->enabled && !WITHIN_INTERVAL(ts, interval->start_ts, interval->end_ts)) {
  403. flags += COMMAND_FLAG_LEAVE;
  404. interval->enabled = 0;
  405. }
  406. if (flags) {
  407. AVBPrint pbuf;
  408. av_log(ctx, AV_LOG_VERBOSE,
  409. "[%s] interval #%d start_ts:%f end_ts:%f ts:%f\n",
  410. make_command_flags_str(&pbuf, flags), interval->index,
  411. (double)interval->start_ts/1000000, (double)interval->end_ts/1000000,
  412. (double)ts/1000000);
  413. for (j = 0; flags && j < interval->nb_commands; j++) {
  414. Command *cmd = &interval->commands[j];
  415. char buf[1024];
  416. if (cmd->flags & flags) {
  417. av_log(ctx, AV_LOG_VERBOSE,
  418. "Processing command #%d target:%s command:%s arg:%s\n",
  419. cmd->index, cmd->target, cmd->command, cmd->arg);
  420. ret = avfilter_graph_send_command(inlink->graph,
  421. cmd->target, cmd->command, cmd->arg,
  422. buf, sizeof(buf),
  423. AVFILTER_CMD_FLAG_ONE);
  424. av_log(ctx, AV_LOG_VERBOSE,
  425. "Command reply for command #%d: ret:%s res:%s\n",
  426. cmd->index, av_err2str(ret), buf);
  427. }
  428. }
  429. }
  430. }
  431. end:
  432. switch (inlink->type) {
  433. case AVMEDIA_TYPE_VIDEO:
  434. case AVMEDIA_TYPE_AUDIO:
  435. return ff_filter_frame(inlink->dst->outputs[0], ref);
  436. }
  437. return AVERROR(ENOSYS);
  438. }
  439. #if CONFIG_SENDCMD_FILTER
  440. #define sendcmd_options options
  441. AVFILTER_DEFINE_CLASS(sendcmd);
  442. static const AVFilterPad sendcmd_inputs[] = {
  443. {
  444. .name = "default",
  445. .type = AVMEDIA_TYPE_VIDEO,
  446. .filter_frame = filter_frame,
  447. },
  448. { NULL }
  449. };
  450. static const AVFilterPad sendcmd_outputs[] = {
  451. {
  452. .name = "default",
  453. .type = AVMEDIA_TYPE_VIDEO,
  454. },
  455. { NULL }
  456. };
  457. AVFilter ff_vf_sendcmd = {
  458. .name = "sendcmd",
  459. .description = NULL_IF_CONFIG_SMALL("Send commands to filters."),
  460. .init = init,
  461. .uninit = uninit,
  462. .priv_size = sizeof(SendCmdContext),
  463. .inputs = sendcmd_inputs,
  464. .outputs = sendcmd_outputs,
  465. .priv_class = &sendcmd_class,
  466. };
  467. #endif
  468. #if CONFIG_ASENDCMD_FILTER
  469. #define asendcmd_options options
  470. AVFILTER_DEFINE_CLASS(asendcmd);
  471. static const AVFilterPad asendcmd_inputs[] = {
  472. {
  473. .name = "default",
  474. .type = AVMEDIA_TYPE_AUDIO,
  475. .filter_frame = filter_frame,
  476. },
  477. { NULL }
  478. };
  479. static const AVFilterPad asendcmd_outputs[] = {
  480. {
  481. .name = "default",
  482. .type = AVMEDIA_TYPE_AUDIO,
  483. },
  484. { NULL }
  485. };
  486. AVFilter ff_af_asendcmd = {
  487. .name = "asendcmd",
  488. .description = NULL_IF_CONFIG_SMALL("Send commands to filters."),
  489. .init = init,
  490. .uninit = uninit,
  491. .priv_size = sizeof(SendCmdContext),
  492. .inputs = asendcmd_inputs,
  493. .outputs = asendcmd_outputs,
  494. .priv_class = &asendcmd_class,
  495. };
  496. #endif