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.

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