f_metadata.c 12 KB

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