Skip to content

Instantly share code, notes, and snippets.

@rcombs

rcombs/stdin Secret

Created September 21, 2015 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcombs/070535a3cdee287d5bcf to your computer and use it in GitHub Desktop.
Save rcombs/070535a3cdee287d5bcf to your computer and use it in GitHub Desktop.
commit 6b386d47c2078a730fabcd5bcfb3567a06cfc5b9
Author: Rodger Combs <rodger.combs@gmail.com>
Date: Mon Sep 21 04:16:58 2015 -0500
lavu/opt: add flag to return NULL when applicable in av_opt_get
diff --git a/libavutil/opt.c b/libavutil/opt.c
index ce80de0..4e6425d 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -666,12 +666,20 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
case AV_OPT_TYPE_STRING:
- if (*(uint8_t**)dst)
+ if (*(uint8_t**)dst) {
*out_val = av_strdup(*(uint8_t**)dst);
- else
+ } else if (search_flags & AV_OPT_SEARCH_ALLOW_NULL) {
+ *out_val = NULL;
+ return 0;
+ } else {
*out_val = av_strdup("");
+ }
return *out_val ? 0 : AVERROR(ENOMEM);
case AV_OPT_TYPE_BINARY:
+ if (!*(uint8_t**)dst && (search_flags & AV_OPT_SEARCH_ALLOW_NULL)) {
+ *out_val = NULL;
+ return 0;
+ }
len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
if ((uint64_t)len*2 + 1 > INT_MAX)
return AVERROR(EINVAL);
diff --git a/libavutil/opt.h b/libavutil/opt.h
index ef90e9c..94bc16e 100644
--- a/libavutil/opt.h
+++ b/libavutil/opt.h
@@ -564,6 +564,12 @@ int av_opt_eval_q (void *obj, const AVOption *o, const char *val, AVRational
#define AV_OPT_SEARCH_FAKE_OBJ (1 << 1)
/**
+ * In av_opt_get, return NULL if the option has a pointer type and is set to NULL,
+ * rather than returning an empty string.
+ */
+#define AV_OPT_SEARCH_ALLOW_NULL (1 << 2)
+
+/**
* Allows av_opt_query_ranges and av_opt_query_ranges_default to return more than
* one component for certain option types.
* @see AVOptionRanges for details.
@@ -722,6 +728,10 @@ int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, in
*/
/**
* @note the returned string will be av_malloc()ed and must be av_free()ed by the caller
+ *
+ * @note if AV_OPT_SEARCH_ALLOW_NULL is set in search_flags in av_opt_get, and the option
+ * has AV_OPT_TYPE_STRING or AV_OPT_TYPE_BINARY and is set to NULL, *out_val will be set
+ * to NULL instead of an allocated empty string.
*/
int av_opt_get (void *obj, const char *name, int search_flags, uint8_t **out_val);
int av_opt_get_int (void *obj, const char *name, int search_flags, int64_t *out_val);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment