diff options
author | Stefano Sabatini <stefasab@gmail.com> | 2013-10-04 12:07:55 +0200 |
---|---|---|
committer | Stefano Sabatini <stefasab@gmail.com> | 2013-10-17 18:09:29 +0200 |
commit | 8696e51bafdc08baa8fe74fc4bc95904f4ad5654 (patch) | |
tree | e99d3cc749e221e1efaca25d60fd6b69275c7cf9 /libavutil/opt.c | |
parent | d96e377c394cc4664a91bfd2c24bbb80f42ea8de (diff) | |
download | ffmpeg-8696e51bafdc08baa8fe74fc4bc95904f4ad5654.tar.gz |
lavu/opt: add AV_OPT_TYPE_CHANNEL_LAYOUT and handler functions
The new type is compatible with AV_OPT_TYPE_INT64, but allows to specify
channel layouts using the format accepted by av_get_channel_layout().
Diffstat (limited to 'libavutil/opt.c')
-rw-r--r-- | libavutil/opt.c | 74 |
1 files changed, 73 insertions, 1 deletions
diff --git a/libavutil/opt.c b/libavutil/opt.c index d282af2f29..5dd84e4201 100644 --- a/libavutil/opt.c +++ b/libavutil/opt.c @@ -27,6 +27,7 @@ #include "avutil.h" #include "avstring.h" +#include "channel_layout.h" #include "common.h" #include "opt.h" #include "eval.h" @@ -77,6 +78,7 @@ static int read_number(const AVOption *o, void *dst, double *num, int *den, int6 case AV_OPT_TYPE_PIXEL_FMT: case AV_OPT_TYPE_SAMPLE_FMT: case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0; + case AV_OPT_TYPE_CHANNEL_LAYOUT: case AV_OPT_TYPE_DURATION: case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0; case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0; @@ -103,6 +105,7 @@ static int write_number(void *obj, const AVOption *o, void *dst, double num, int case AV_OPT_TYPE_SAMPLE_FMT: case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break; case AV_OPT_TYPE_DURATION: + case AV_OPT_TYPE_CHANNEL_LAYOUT: case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break; case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break; case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break; @@ -259,7 +262,8 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags) if (!val && (o->type != AV_OPT_TYPE_STRING && o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT && o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE && - o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR)) + o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR && + o->type != AV_OPT_TYPE_CHANNEL_LAYOUT)) return AVERROR(EINVAL); dst = ((uint8_t*)target_obj) + o->offset; @@ -341,6 +345,24 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags) av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val); return ret; } + break; + case AV_OPT_TYPE_CHANNEL_LAYOUT: + if (!val || !strcmp(val, "none")) { + *(int64_t *)dst = 0; + } else { +#if FF_API_GET_CHANNEL_LAYOUT_COMPAT + int64_t cl = ff_get_channel_layout(val, 0); +#else + int64_t cl = av_get_channel_layout(val); +#endif + if (!cl) { + av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val); + ret = AVERROR(EINVAL); + } + *(int64_t *)dst = cl; + return ret; + } + break; } av_log(obj, AV_LOG_ERROR, "Invalid option type.\n"); @@ -532,6 +554,22 @@ int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB); } +int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags) +{ + void *target_obj; + const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj); + + if (!o || !target_obj) + return AVERROR_OPTION_NOT_FOUND; + if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) { + av_log(obj, AV_LOG_ERROR, + "The value set by option '%s' is not a channel layout.\n", o->name); + return AVERROR(EINVAL); + } + *(int *)(((int64_t *)target_obj) + o->offset) = cl; + return 0; +} + #if FF_API_OLD_AVOPTIONS /** * @@ -630,6 +668,10 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) case AV_OPT_TYPE_COLOR: ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x", ((int *)dst)[0], ((int *)dst)[1], ((int *)dst)[2], ((int *)dst)[3]); break; + case AV_OPT_TYPE_CHANNEL_LAYOUT: + i64 = *(int64_t *)dst; + ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64); + break; default: return AVERROR(EINVAL); } @@ -799,6 +841,23 @@ int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AV return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample"); } +int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl) +{ + void *dst, *target_obj; + const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj); + if (!o || !target_obj) + return AVERROR_OPTION_NOT_FOUND; + if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) { + av_log(obj, AV_LOG_ERROR, + "The value for option '%s' is not a channel layout.\n", name); + return AVERROR(EINVAL); + } + + dst = ((uint8_t*)target_obj) + o->offset; + *cl = *(int64_t *)dst; + return 0; +} + int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name) { const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0); @@ -902,6 +961,9 @@ static void opt_list(void *obj, void *av_log_obj, const char *unit, case AV_OPT_TYPE_COLOR: av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>"); break; + case AV_OPT_TYPE_CHANNEL_LAYOUT: + av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>"); + break; case AV_OPT_TYPE_CONST: default: av_log(av_log_obj, AV_LOG_INFO, "%-12s ", ""); @@ -972,6 +1034,10 @@ static void opt_list(void *obj, void *av_log_obj, const char *unit, case AV_OPT_TYPE_STRING: case AV_OPT_TYPE_VIDEO_RATE: av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str); + break; + case AV_OPT_TYPE_CHANNEL_LAYOUT: + av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64); + break; } av_log(av_log_obj, AV_LOG_INFO, ")"); } @@ -1019,6 +1085,7 @@ void av_opt_set_defaults2(void *s, int mask, int flags) case AV_OPT_TYPE_INT: case AV_OPT_TYPE_INT64: case AV_OPT_TYPE_DURATION: + case AV_OPT_TYPE_CHANNEL_LAYOUT: av_opt_set_int(s, opt->name, opt->default_val.i64, 0); break; case AV_OPT_TYPE_DOUBLE: @@ -1395,6 +1462,7 @@ int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const ch case AV_OPT_TYPE_DOUBLE: case AV_OPT_TYPE_DURATION: case AV_OPT_TYPE_COLOR: + case AV_OPT_TYPE_CHANNEL_LAYOUT: break; case AV_OPT_TYPE_STRING: range->component_min = 0; @@ -1462,6 +1530,7 @@ typedef struct TestContext enum AVSampleFormat sample_fmt; int64_t duration; uint8_t color[4]; + int64_t channel_layout; } TestContext; #define OFFSET(x) offsetof(TestContext, x) @@ -1485,6 +1554,7 @@ static const AVOption test_options[]= { {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 }, {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX}, {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0}, +{"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX}, {NULL}, }; @@ -1546,6 +1616,8 @@ int main(void) "color=blue", "color=0x223300", "color=0x42FF07AA", + "cl=stereo+downmix", + "cl=foo", }; test_ctx.class = &test_class; |