f_sendcmd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. static const AVOption sendcmd_options[]= {
  73. { "commands", "set commands", OFFSET(commands_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  74. { "c", "set commands", OFFSET(commands_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  75. { "filename", "set commands file", OFFSET(commands_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  76. { "f", "set commands file", OFFSET(commands_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  77. {NULL},
  78. };
  79. AVFILTER_DEFINE_CLASS(sendcmd);
  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 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 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 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 ((ret = av_parse_time(&interval->start_ts, start, 1)) < 0) {
  229. av_log(log_ctx, AV_LOG_ERROR,
  230. "Invalid start time specification '%s' in interval #%d\n",
  231. start, interval_count);
  232. goto end;
  233. }
  234. if (end) {
  235. if ((ret = av_parse_time(&interval->end_ts, end, 1)) < 0) {
  236. av_log(log_ctx, AV_LOG_ERROR,
  237. "Invalid end time specification '%s' in interval #%d\n",
  238. end, interval_count);
  239. goto end;
  240. }
  241. } else {
  242. interval->end_ts = INT64_MAX;
  243. }
  244. if (interval->end_ts < interval->start_ts) {
  245. av_log(log_ctx, AV_LOG_ERROR,
  246. "Invalid end time '%s' in interval #%d: "
  247. "cannot be lesser than start time '%s'\n",
  248. end, interval_count, start);
  249. ret = AVERROR(EINVAL);
  250. goto end;
  251. }
  252. } else {
  253. av_log(log_ctx, AV_LOG_ERROR,
  254. "No interval specified for interval #%d\n", interval_count);
  255. ret = AVERROR(EINVAL);
  256. goto end;
  257. }
  258. /* parse commands */
  259. ret = parse_commands(&interval->commands, &interval->nb_commands,
  260. interval_count, buf, log_ctx);
  261. end:
  262. av_free(intervalstr);
  263. return ret;
  264. }
  265. static int parse_intervals(Interval **intervals, int *nb_intervals,
  266. const char *buf, void *log_ctx)
  267. {
  268. int interval_count = 0;
  269. int ret, n = 0;
  270. *intervals = NULL;
  271. *nb_intervals = 0;
  272. while (1) {
  273. Interval interval;
  274. skip_comments(&buf);
  275. if (!(*buf))
  276. break;
  277. if ((ret = parse_interval(&interval, interval_count, &buf, log_ctx)) < 0)
  278. return ret;
  279. buf += strspn(buf, SPACES);
  280. if (*buf) {
  281. if (*buf != ';') {
  282. av_log(log_ctx, AV_LOG_ERROR,
  283. "Missing terminator or extraneous data found at the end of interval #%d\n",
  284. interval_count);
  285. return AVERROR(EINVAL);
  286. }
  287. buf++; /* skip ';' */
  288. }
  289. interval_count++;
  290. /* (re)allocate commands array if required */
  291. if (*nb_intervals == n) {
  292. n = FFMAX(16, 2*n); /* first allocation = 16, or double the number */
  293. *intervals = av_realloc_f(*intervals, n, 2*sizeof(Interval));
  294. if (!*intervals) {
  295. av_log(log_ctx, AV_LOG_ERROR,
  296. "Could not (re)allocate intervals array\n");
  297. return AVERROR(ENOMEM);
  298. }
  299. }
  300. (*intervals)[(*nb_intervals)++] = interval;
  301. }
  302. return 0;
  303. }
  304. static int cmp_intervals(const void *a, const void *b)
  305. {
  306. const Interval *i1 = a;
  307. const Interval *i2 = b;
  308. int64_t ts_diff = i1->start_ts - i2->start_ts;
  309. int ret;
  310. ret = ts_diff > 0 ? 1 : ts_diff < 0 ? -1 : 0;
  311. return ret == 0 ? i1->index - i2->index : ret;
  312. }
  313. static av_cold int init(AVFilterContext *ctx, const char *args)
  314. {
  315. SendCmdContext *sendcmd = ctx->priv;
  316. int ret, i, j;
  317. char *buf;
  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;
  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. return AVERROR(ENOMEM);
  338. memcpy(buf, file_buf, file_bufsize);
  339. buf[file_bufsize] = 0;
  340. av_file_unmap(file_buf, file_bufsize);
  341. sendcmd->commands_str = buf;
  342. }
  343. if ((ret = parse_intervals(&sendcmd->intervals, &sendcmd->nb_intervals,
  344. sendcmd->commands_str, ctx)) < 0)
  345. return ret;
  346. qsort(sendcmd->intervals, sendcmd->nb_intervals, sizeof(Interval), cmp_intervals);
  347. av_log(ctx, AV_LOG_DEBUG, "Parsed commands:\n");
  348. for (i = 0; i < sendcmd->nb_intervals; i++) {
  349. AVBPrint pbuf;
  350. Interval *interval = &sendcmd->intervals[i];
  351. av_log(ctx, AV_LOG_VERBOSE, "start_time:%f end_time:%f index:%d\n",
  352. (double)interval->start_ts/1000000, (double)interval->end_ts/1000000, interval->index);
  353. for (j = 0; j < interval->nb_commands; j++) {
  354. Command *cmd = &interval->commands[j];
  355. av_log(ctx, AV_LOG_VERBOSE,
  356. " [%s] target:%s command:%s arg:%s index:%d\n",
  357. make_command_flags_str(&pbuf, cmd->flags), cmd->target, cmd->command, cmd->arg, cmd->index);
  358. }
  359. }
  360. return 0;
  361. }
  362. static void av_cold uninit(AVFilterContext *ctx)
  363. {
  364. SendCmdContext *sendcmd = ctx->priv;
  365. int i, j;
  366. av_opt_free(sendcmd);
  367. for (i = 0; i < sendcmd->nb_intervals; i++) {
  368. Interval *interval = &sendcmd->intervals[i];
  369. for (j = 0; j < interval->nb_commands; j++) {
  370. Command *cmd = &interval->commands[j];
  371. av_free(cmd->target);
  372. av_free(cmd->command);
  373. av_free(cmd->arg);
  374. }
  375. av_free(interval->commands);
  376. }
  377. av_freep(&sendcmd->intervals);
  378. }
  379. static int process_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
  380. {
  381. AVFilterContext *ctx = inlink->dst;
  382. SendCmdContext *sendcmd = ctx->priv;
  383. int64_t ts;
  384. int i, j, ret;
  385. if (ref->pts == AV_NOPTS_VALUE)
  386. goto end;
  387. ts = av_rescale_q(ref->pts, inlink->time_base, AV_TIME_BASE_Q);
  388. #define WITHIN_INTERVAL(ts, start_ts, end_ts) ((ts) >= (start_ts) && (ts) < (end_ts))
  389. for (i = 0; i < sendcmd->nb_intervals; i++) {
  390. Interval *interval = &sendcmd->intervals[i];
  391. int flags = 0;
  392. if (!interval->enabled && WITHIN_INTERVAL(ts, interval->start_ts, interval->end_ts)) {
  393. flags += COMMAND_FLAG_ENTER;
  394. interval->enabled = 1;
  395. }
  396. if (interval->enabled && !WITHIN_INTERVAL(ts, interval->start_ts, interval->end_ts)) {
  397. flags += COMMAND_FLAG_LEAVE;
  398. interval->enabled = 0;
  399. }
  400. if (flags) {
  401. AVBPrint pbuf;
  402. av_log(ctx, AV_LOG_VERBOSE,
  403. "[%s] interval #%d start_ts:%f end_ts:%f ts:%f\n",
  404. make_command_flags_str(&pbuf, flags), interval->index,
  405. (double)interval->start_ts/1000000, (double)interval->end_ts/1000000,
  406. (double)ts/1000000);
  407. for (j = 0; flags && j < interval->nb_commands; j++) {
  408. Command *cmd = &interval->commands[j];
  409. char buf[1024];
  410. if (cmd->flags & flags) {
  411. av_log(ctx, AV_LOG_VERBOSE,
  412. "Processing command #%d target:%s command:%s arg:%s\n",
  413. cmd->index, cmd->target, cmd->command, cmd->arg);
  414. ret = avfilter_graph_send_command(inlink->graph,
  415. cmd->target, cmd->command, cmd->arg,
  416. buf, sizeof(buf),
  417. AVFILTER_CMD_FLAG_ONE);
  418. av_log(ctx, AV_LOG_VERBOSE,
  419. "Command reply for command #%d: ret:%s res:%s\n",
  420. cmd->index, av_err2str(ret), buf);
  421. }
  422. }
  423. }
  424. }
  425. end:
  426. /* give the reference away, do not store in cur_buf */
  427. inlink->cur_buf = NULL;
  428. switch (inlink->type) {
  429. case AVMEDIA_TYPE_VIDEO: return ff_start_frame (inlink->dst->outputs[0], ref);
  430. case AVMEDIA_TYPE_AUDIO: return ff_filter_samples(inlink->dst->outputs[0], ref);
  431. }
  432. return AVERROR(ENOSYS);
  433. }
  434. #if CONFIG_SENDCMD_FILTER
  435. AVFilter avfilter_vf_sendcmd = {
  436. .name = "sendcmd",
  437. .description = NULL_IF_CONFIG_SMALL("Send commands to filters."),
  438. .init = init,
  439. .uninit = uninit,
  440. .priv_size = sizeof(SendCmdContext),
  441. .inputs = (const AVFilterPad[]) {
  442. {
  443. .name = "default",
  444. .type = AVMEDIA_TYPE_VIDEO,
  445. .get_video_buffer = ff_null_get_video_buffer,
  446. .start_frame = process_frame,
  447. .end_frame = ff_null_end_frame,
  448. },
  449. { .name = NULL }
  450. },
  451. .outputs = (const AVFilterPad[]) {
  452. {
  453. .name = "default",
  454. .type = AVMEDIA_TYPE_VIDEO,
  455. },
  456. { .name = NULL }
  457. },
  458. };
  459. #endif
  460. #if CONFIG_ASENDCMD_FILTER
  461. AVFilter avfilter_af_asendcmd = {
  462. .name = "asendcmd",
  463. .description = NULL_IF_CONFIG_SMALL("Send commands to filters."),
  464. .init = init,
  465. .uninit = uninit,
  466. .priv_size = sizeof(SendCmdContext),
  467. .inputs = (const AVFilterPad[]) {
  468. {
  469. .name = "default",
  470. .type = AVMEDIA_TYPE_AUDIO,
  471. .get_audio_buffer = ff_null_get_audio_buffer,
  472. .filter_samples = process_frame,
  473. },
  474. { .name = NULL }
  475. },
  476. .outputs = (const AVFilterPad[]) {
  477. {
  478. .name = "default",
  479. .type = AVMEDIA_TYPE_AUDIO,
  480. },
  481. { .name = NULL }
  482. },
  483. };
  484. #endif