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.

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