summaryrefslogtreecommitdiffstats
path: root/common/av_common.c
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-08-02 03:12:09 +0200
committerwm4 <wm4@nowhere>2014-08-02 03:12:33 +0200
commitd68a759fa4dea2701087039c58961757eb246b4f (patch)
tree07efabb3a1bf1bdcc0b80f6150f996aacf293ca6 /common/av_common.c
parent0c6c2da8bcdb0f1523c0f10bc117c41057875e34 (diff)
downloadmpv-d68a759fa4dea2701087039c58961757eb246b4f.tar.bz2
mpv-d68a759fa4dea2701087039c58961757eb246b4f.tar.xz
Improve setting AVOptions
Use OPT_KEYVALUELIST() for all places where AVOptions are directly set from mpv command line options. This allows escaping values, better diagnostics (also no more "pal"), and somehow reduces code size. Remove the old crappy option parser (av_opts.c).
Diffstat (limited to 'common/av_common.c')
-rw-r--r--common/av_common.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/common/av_common.c b/common/av_common.c
index d090ccfc50..e0a5da7726 100644
--- a/common/av_common.c
+++ b/common/av_common.c
@@ -20,6 +20,8 @@
#include <libavutil/common.h>
#include <libavutil/log.h>
#include <libavutil/dict.h>
+#include <libavutil/opt.h>
+#include <libavutil/error.h>
#include <libavcodec/avcodec.h>
#include "common/common.h"
@@ -189,3 +191,37 @@ void mp_set_avdict(AVDictionary **dict, char **kv)
for (int n = 0; kv && kv[n * 2]; n++)
av_dict_set(dict, kv[n * 2 + 0], kv[n * 2 + 1], 0);
}
+
+// For use with libav* APIs that take AVDictionaries of options.
+// Print options remaining in the dict as unset.
+void mp_avdict_print_unset(struct mp_log *log, int msgl, AVDictionary *dict)
+{
+ AVDictionaryEntry *t = NULL;
+ while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX)))
+ mp_msg(log, msgl, "Could not set AVOption %s='%s'\n", t->key, t->value);
+}
+
+// kv is in the format as by OPT_KEYVALUELIST(): kv[0]=key0, kv[1]=val0, ...
+// Set these options on given avobj (using av_opt_set..., meaning avobj must
+// point to a struct that has AVClass as first member).
+// Options which fail to set (error or not found) are printed to log.
+// Returns: >=0 success, <0 failed to set an option
+int mp_set_avopts(struct mp_log *log, void *avobj, char **kv)
+{
+ int success = 0;
+ for (int n = 0; kv && kv[n * 2]; n++) {
+ char *k = kv[n * 2 + 0];
+ char *v = kv[n * 2 + 1];
+ int r = av_opt_set(avobj, k, v, AV_OPT_SEARCH_CHILDREN);
+ if (r == AVERROR_OPTION_NOT_FOUND) {
+ mp_err(log, "AVOption '%s' not found.\n", k);
+ success = -1;
+ } else if (r < 0) {
+ char errstr[80];
+ av_strerror(r, errstr, sizeof(errstr));
+ mp_err(log, "Could not set AVOption %s='%s' (%s)\n", k, v, errstr);
+ success = -1;
+ }
+ }
+ return success;
+}