f_metadata.c 12 KB

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