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.

433 lines
13KB

  1. /*
  2. * Copyright (c) 2016 Paul B Mahol
  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. * filter for manipulating frame metadata
  23. */
  24. #include <float.h>
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/eval.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/timestamp.h"
  31. #include "libavformat/avio.h"
  32. #include "avfilter.h"
  33. #include "audio.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. enum MetadataMode {
  38. METADATA_SELECT,
  39. METADATA_ADD,
  40. METADATA_MODIFY,
  41. METADATA_DELETE,
  42. METADATA_PRINT,
  43. METADATA_NB
  44. };
  45. enum MetadataFunction {
  46. METADATAF_SAME_STR,
  47. METADATAF_STARTS_WITH,
  48. METADATAF_LESS,
  49. METADATAF_EQUAL,
  50. METADATAF_GREATER,
  51. METADATAF_EXPR,
  52. METADATAF_ENDS_WITH,
  53. METADATAF_NB
  54. };
  55. static const char *const var_names[] = {
  56. "VALUE1",
  57. "VALUE2",
  58. NULL
  59. };
  60. enum var_name {
  61. VAR_VALUE1,
  62. VAR_VALUE2,
  63. VAR_VARS_NB
  64. };
  65. typedef struct MetadataContext {
  66. const AVClass *class;
  67. int mode;
  68. char *key;
  69. char *value;
  70. int function;
  71. char *expr_str;
  72. AVExpr *expr;
  73. double var_values[VAR_VARS_NB];
  74. AVIOContext* avio_context;
  75. char *file_str;
  76. int (*compare)(struct MetadataContext *s,
  77. const char *value1, const char *value2);
  78. void (*print)(AVFilterContext *ctx, const char *msg, ...) av_printf_format(2, 3);
  79. int direct; // reduces buffering when printing to user-supplied URL
  80. } MetadataContext;
  81. #define OFFSET(x) offsetof(MetadataContext, x)
  82. #define DEFINE_OPTIONS(filt_name, FLAGS) \
  83. static const AVOption filt_name##_options[] = { \
  84. { "mode", "set a mode of operation", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, METADATA_NB-1, FLAGS, "mode" }, \
  85. { "select", "select frame", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_SELECT }, 0, 0, FLAGS, "mode" }, \
  86. { "add", "add new metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_ADD }, 0, 0, FLAGS, "mode" }, \
  87. { "modify", "modify metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_MODIFY }, 0, 0, FLAGS, "mode" }, \
  88. { "delete", "delete metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_DELETE }, 0, 0, FLAGS, "mode" }, \
  89. { "print", "print metadata", 0, AV_OPT_TYPE_CONST, {.i64 = METADATA_PRINT }, 0, 0, FLAGS, "mode" }, \
  90. { "key", "set metadata key", OFFSET(key), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
  91. { "value", "set metadata value", OFFSET(value), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
  92. { "function", "function for comparing values", OFFSET(function), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, METADATAF_NB-1, FLAGS, "function" }, \
  93. { "same_str", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_SAME_STR }, 0, 3, FLAGS, "function" }, \
  94. { "starts_with", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_STARTS_WITH }, 0, 0, FLAGS, "function" }, \
  95. { "less", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_LESS }, 0, 3, FLAGS, "function" }, \
  96. { "equal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EQUAL }, 0, 3, FLAGS, "function" }, \
  97. { "greater", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_GREATER }, 0, 3, FLAGS, "function" }, \
  98. { "expr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_EXPR }, 0, 3, FLAGS, "function" }, \
  99. { "ends_with", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = METADATAF_ENDS_WITH }, 0, 0, FLAGS, "function" }, \
  100. { "expr", "set expression for expr function", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, FLAGS }, \
  101. { "file", "set file where to print metadata information", OFFSET(file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS }, \
  102. { "direct", "reduce buffering when printing to user-set file or pipe", OFFSET(direct), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS }, \
  103. { NULL } \
  104. }
  105. static int same_str(MetadataContext *s, const char *value1, const char *value2)
  106. {
  107. return !strcmp(value1, value2);
  108. }
  109. static int starts_with(MetadataContext *s, const char *value1, const char *value2)
  110. {
  111. return !strncmp(value1, value2, strlen(value2));
  112. }
  113. static int ends_with(MetadataContext *s, const char *value1, const char *value2)
  114. {
  115. const int len1 = strlen(value1);
  116. const int len2 = strlen(value2);
  117. return !strncmp(value1 + FFMAX(len1 - len2, 0), value2, len2);
  118. }
  119. static int equal(MetadataContext *s, const char *value1, const char *value2)
  120. {
  121. float f1, f2;
  122. if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
  123. return 0;
  124. return fabsf(f1 - f2) < FLT_EPSILON;
  125. }
  126. static int less(MetadataContext *s, const char *value1, const char *value2)
  127. {
  128. float f1, f2;
  129. if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
  130. return 0;
  131. return (f1 - f2) < FLT_EPSILON;
  132. }
  133. static int greater(MetadataContext *s, const char *value1, const char *value2)
  134. {
  135. float f1, f2;
  136. if (sscanf(value1, "%f", &f1) + sscanf(value2, "%f", &f2) != 2)
  137. return 0;
  138. return (f2 - f1) < FLT_EPSILON;
  139. }
  140. static int parse_expr(MetadataContext *s, const char *value1, const char *value2)
  141. {
  142. double f1, f2;
  143. if (sscanf(value1, "%lf", &f1) + sscanf(value2, "%lf", &f2) != 2)
  144. return 0;
  145. s->var_values[VAR_VALUE1] = f1;
  146. s->var_values[VAR_VALUE2] = f2;
  147. return av_expr_eval(s->expr, s->var_values, NULL);
  148. }
  149. static void print_log(AVFilterContext *ctx, const char *msg, ...)
  150. {
  151. va_list argument_list;
  152. va_start(argument_list, msg);
  153. if (msg)
  154. av_vlog(ctx, AV_LOG_INFO, msg, argument_list);
  155. va_end(argument_list);
  156. }
  157. static void print_file(AVFilterContext *ctx, const char *msg, ...)
  158. {
  159. MetadataContext *s = ctx->priv;
  160. va_list argument_list;
  161. va_start(argument_list, msg);
  162. if (msg) {
  163. char buf[128];
  164. vsnprintf(buf, sizeof(buf), msg, argument_list);
  165. avio_write(s->avio_context, buf, av_strnlen(buf, sizeof(buf)));
  166. }
  167. va_end(argument_list);
  168. }
  169. static av_cold int init(AVFilterContext *ctx)
  170. {
  171. MetadataContext *s = ctx->priv;
  172. int ret;
  173. if (!s->key && s->mode != METADATA_PRINT && s->mode != METADATA_DELETE) {
  174. av_log(ctx, AV_LOG_WARNING, "Metadata key must be set\n");
  175. return AVERROR(EINVAL);
  176. }
  177. if ((s->mode == METADATA_MODIFY ||
  178. s->mode == METADATA_ADD) && !s->value) {
  179. av_log(ctx, AV_LOG_WARNING, "Missing metadata value\n");
  180. return AVERROR(EINVAL);
  181. }
  182. switch (s->function) {
  183. case METADATAF_SAME_STR:
  184. s->compare = same_str;
  185. break;
  186. case METADATAF_STARTS_WITH:
  187. s->compare = starts_with;
  188. break;
  189. case METADATAF_ENDS_WITH:
  190. s->compare = ends_with;
  191. break;
  192. case METADATAF_LESS:
  193. s->compare = less;
  194. break;
  195. case METADATAF_EQUAL:
  196. s->compare = equal;
  197. break;
  198. case METADATAF_GREATER:
  199. s->compare = greater;
  200. break;
  201. case METADATAF_EXPR:
  202. s->compare = parse_expr;
  203. break;
  204. default:
  205. av_assert0(0);
  206. };
  207. if (s->function == METADATAF_EXPR) {
  208. if (!s->expr_str) {
  209. av_log(ctx, AV_LOG_WARNING, "expr option not set\n");
  210. return AVERROR(EINVAL);
  211. }
  212. if ((ret = av_expr_parse(&s->expr, s->expr_str,
  213. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  214. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", s->expr_str);
  215. return ret;
  216. }
  217. }
  218. if (s->mode == METADATA_PRINT && s->file_str) {
  219. s->print = print_file;
  220. } else {
  221. s->print = print_log;
  222. }
  223. s->avio_context = NULL;
  224. if (s->file_str) {
  225. if (!strcmp("-", s->file_str)) {
  226. ret = avio_open(&s->avio_context, "pipe:1", AVIO_FLAG_WRITE);
  227. } else {
  228. ret = avio_open(&s->avio_context, s->file_str, AVIO_FLAG_WRITE);
  229. }
  230. if (ret < 0) {
  231. char buf[128];
  232. av_strerror(ret, buf, sizeof(buf));
  233. av_log(ctx, AV_LOG_ERROR, "Could not open %s: %s\n",
  234. s->file_str, buf);
  235. return ret;
  236. }
  237. if (s->direct)
  238. s->avio_context->direct = AVIO_FLAG_DIRECT;
  239. }
  240. return 0;
  241. }
  242. static av_cold void uninit(AVFilterContext *ctx)
  243. {
  244. MetadataContext *s = ctx->priv;
  245. av_expr_free(s->expr);
  246. s->expr = NULL;
  247. if (s->avio_context) {
  248. avio_closep(&s->avio_context);
  249. }
  250. }
  251. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  252. {
  253. AVFilterContext *ctx = inlink->dst;
  254. AVFilterLink *outlink = ctx->outputs[0];
  255. MetadataContext *s = ctx->priv;
  256. AVDictionary **metadata = &frame->metadata;
  257. AVDictionaryEntry *e;
  258. if (!*metadata && s->mode != METADATA_ADD)
  259. return ff_filter_frame(outlink, frame);
  260. e = av_dict_get(*metadata, !s->key ? "" : s->key, NULL,
  261. !s->key ? AV_DICT_IGNORE_SUFFIX: 0);
  262. switch (s->mode) {
  263. case METADATA_SELECT:
  264. if (!s->value && e && e->value) {
  265. return ff_filter_frame(outlink, frame);
  266. } else if (s->value && e && e->value &&
  267. s->compare(s, e->value, s->value)) {
  268. return ff_filter_frame(outlink, frame);
  269. }
  270. break;
  271. case METADATA_ADD:
  272. if (e && e->value) {
  273. ;
  274. } else {
  275. av_dict_set(metadata, s->key, s->value, 0);
  276. }
  277. return ff_filter_frame(outlink, frame);
  278. case METADATA_MODIFY:
  279. if (e && e->value) {
  280. av_dict_set(metadata, s->key, s->value, 0);
  281. }
  282. return ff_filter_frame(outlink, frame);
  283. case METADATA_PRINT:
  284. if (!s->key && e) {
  285. s->print(ctx, "frame:%-4"PRId64" pts:%-7s pts_time:%s\n",
  286. inlink->frame_count_out, av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
  287. s->print(ctx, "%s=%s\n", e->key, e->value);
  288. while ((e = av_dict_get(*metadata, "", e, AV_DICT_IGNORE_SUFFIX)) != NULL) {
  289. s->print(ctx, "%s=%s\n", e->key, e->value);
  290. }
  291. } else if (e && e->value && (!s->value || (e->value && s->compare(s, e->value, s->value)))) {
  292. s->print(ctx, "frame:%-4"PRId64" pts:%-7s pts_time:%s\n",
  293. inlink->frame_count_out, av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
  294. s->print(ctx, "%s=%s\n", s->key, e->value);
  295. }
  296. return ff_filter_frame(outlink, frame);
  297. case METADATA_DELETE:
  298. if (!s->key) {
  299. av_dict_free(metadata);
  300. } else if (e && e->value && (!s->value || s->compare(s, e->value, s->value))) {
  301. av_dict_set(metadata, s->key, NULL, 0);
  302. }
  303. return ff_filter_frame(outlink, frame);
  304. default:
  305. av_assert0(0);
  306. };
  307. av_frame_free(&frame);
  308. return 0;
  309. }
  310. #if CONFIG_AMETADATA_FILTER
  311. DEFINE_OPTIONS(ametadata, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
  312. AVFILTER_DEFINE_CLASS(ametadata);
  313. static const AVFilterPad ainputs[] = {
  314. {
  315. .name = "default",
  316. .type = AVMEDIA_TYPE_AUDIO,
  317. .filter_frame = filter_frame,
  318. },
  319. { NULL }
  320. };
  321. static const AVFilterPad aoutputs[] = {
  322. {
  323. .name = "default",
  324. .type = AVMEDIA_TYPE_AUDIO,
  325. },
  326. { NULL }
  327. };
  328. AVFilter ff_af_ametadata = {
  329. .name = "ametadata",
  330. .description = NULL_IF_CONFIG_SMALL("Manipulate audio frame metadata."),
  331. .priv_size = sizeof(MetadataContext),
  332. .priv_class = &ametadata_class,
  333. .init = init,
  334. .uninit = uninit,
  335. .inputs = ainputs,
  336. .outputs = aoutputs,
  337. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  338. };
  339. #endif /* CONFIG_AMETADATA_FILTER */
  340. #if CONFIG_METADATA_FILTER
  341. DEFINE_OPTIONS(metadata, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
  342. AVFILTER_DEFINE_CLASS(metadata);
  343. static const AVFilterPad inputs[] = {
  344. {
  345. .name = "default",
  346. .type = AVMEDIA_TYPE_VIDEO,
  347. .filter_frame = filter_frame,
  348. },
  349. { NULL }
  350. };
  351. static const AVFilterPad outputs[] = {
  352. {
  353. .name = "default",
  354. .type = AVMEDIA_TYPE_VIDEO,
  355. },
  356. { NULL }
  357. };
  358. AVFilter ff_vf_metadata = {
  359. .name = "metadata",
  360. .description = NULL_IF_CONFIG_SMALL("Manipulate video frame metadata."),
  361. .priv_size = sizeof(MetadataContext),
  362. .priv_class = &metadata_class,
  363. .init = init,
  364. .uninit = uninit,
  365. .inputs = inputs,
  366. .outputs = outputs,
  367. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  368. };
  369. #endif /* CONFIG_METADATA_FILTER */