summaryrefslogtreecommitdiff
path: root/libavutil/opt.c
diff options
context:
space:
mode:
authorStefano Sabatini <stefasab@gmail.com>2013-12-16 14:55:54 +0100
committerStefano Sabatini <stefasab@gmail.com>2013-12-26 11:35:27 +0100
commit1575a96b3af5f5a769b043ff2342b6d17f954498 (patch)
tree7c9b4465702fb47291ec7d953f3b4c4b0fcadc0e /libavutil/opt.c
parente2b54464c6a9de5d6b9ad4307696b0215d5e05a4 (diff)
lavu/opt: factorize setting of format values from string
Simplify incoming patch.
Diffstat (limited to 'libavutil/opt.c')
-rw-r--r--libavutil/opt.c70
1 files changed, 38 insertions, 32 deletions
diff --git a/libavutil/opt.c b/libavutil/opt.c
index 723b443a16..b625756760 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -295,6 +295,42 @@ static int set_string_color(void *obj, const AVOption *o, const char *val, uint8
return 0;
}
+static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
+ int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
+{
+ int fmt;
+
+ if (!val || !strcmp(val, "none")) {
+ fmt = -1;
+ } else {
+ fmt = get_fmt(val);
+ if (fmt == -1) {
+ char *tail;
+ fmt = strtol(val, &tail, 0);
+ if (*tail || (unsigned)fmt >= fmt_nb) {
+ av_log(obj, AV_LOG_ERROR,
+ "Unable to parse option value \"%s\" as %s\n", val, desc);
+ return AVERROR(EINVAL);
+ }
+ }
+ }
+
+ *(int *)dst = fmt;
+ return 0;
+}
+
+static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
+{
+ return set_string_fmt(obj, o, val, dst,
+ AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
+}
+
+static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
+{
+ return set_string_fmt(obj, o, val, dst,
+ AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
+}
+
#if FF_API_OLD_AVOPTIONS
int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
{
@@ -331,38 +367,8 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
case AV_OPT_TYPE_IMAGE_SIZE: return set_string_image_size(obj, o, val, dst);
case AV_OPT_TYPE_VIDEO_RATE: return set_string_video_rate(obj, o, val, dst);
- case AV_OPT_TYPE_PIXEL_FMT:
- if (!val || !strcmp(val, "none")) {
- ret = AV_PIX_FMT_NONE;
- } else {
- ret = av_get_pix_fmt(val);
- if (ret == AV_PIX_FMT_NONE) {
- char *tail;
- ret = strtol(val, &tail, 0);
- if (*tail || (unsigned)ret >= AV_PIX_FMT_NB) {
- av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as pixel format\n", val);
- return AVERROR(EINVAL);
- }
- }
- }
- *(enum AVPixelFormat *)dst = ret;
- return 0;
- case AV_OPT_TYPE_SAMPLE_FMT:
- if (!val || !strcmp(val, "none")) {
- ret = AV_SAMPLE_FMT_NONE;
- } else {
- ret = av_get_sample_fmt(val);
- if (ret == AV_SAMPLE_FMT_NONE) {
- char *tail;
- ret = strtol(val, &tail, 0);
- if (*tail || (unsigned)ret >= AV_SAMPLE_FMT_NB) {
- av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as sample format\n", val);
- return AVERROR(EINVAL);
- }
- }
- }
- *(enum AVSampleFormat *)dst = ret;
- return 0;
+ case AV_OPT_TYPE_PIXEL_FMT: return set_string_pixel_fmt(obj, o, val, dst);
+ case AV_OPT_TYPE_SAMPLE_FMT: return set_string_sample_fmt(obj, o, val, dst);
case AV_OPT_TYPE_DURATION:
if (!val) {
*(int64_t *)dst = 0;