From 8696e51bafdc08baa8fe74fc4bc95904f4ad5654 Mon Sep 17 00:00:00 2001 From: Stefano Sabatini Date: Fri, 4 Oct 2013 12:07:55 +0200 Subject: [PATCH] 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(). --- doc/APIchanges | 5 +++ libavutil/opt.c | 74 ++++++++++++++++++++++++++++++++++++++++++++- libavutil/opt.h | 3 ++ libavutil/version.h | 4 +-- 4 files changed, 83 insertions(+), 3 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index f45c663005..c1f103ae2b 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -15,6 +15,11 @@ libavutil: 2012-10-22 API changes, most recent first: +libavutil 52.47.100 +2013-10-17 - xxxxxxx - lavu 52.47.100 - opt.h + Add AV_OPT_TYPE_CHANNEL_LAYOUT and channel layout option handlers + av_opt_get_channel_layout() and av_opt_set_channel_layout(). + 2013-10-xx - xxxxxxx -libswscale 2.5.101 - options.c Change default scaler to bicubic 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 ", ""); break; + case AV_OPT_TYPE_CHANNEL_LAYOUT: + av_log(av_log_obj, AV_LOG_INFO, "%-12s ", ""); + 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; diff --git a/libavutil/opt.h b/libavutil/opt.h index 3d210652d9..bf4c5ddf5c 100644 --- a/libavutil/opt.h +++ b/libavutil/opt.h @@ -233,6 +233,7 @@ enum AVOptionType{ AV_OPT_TYPE_VIDEO_RATE = MKBETAG('V','R','A','T'), ///< offset must point to AVRational AV_OPT_TYPE_DURATION = MKBETAG('D','U','R',' '), AV_OPT_TYPE_COLOR = MKBETAG('C','O','L','R'), + AV_OPT_TYPE_CHANNEL_LAYOUT = MKBETAG('C','H','L','A'), #if FF_API_OLD_AVOPTIONS FF_OPT_TYPE_FLAGS = 0, FF_OPT_TYPE_INT, @@ -657,6 +658,7 @@ int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_ int av_opt_set_pixel_fmt (void *obj, const char *name, enum AVPixelFormat fmt, int search_flags); int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags); int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags); +int av_opt_set_channel_layout(void *obj, const char *name, int64_t ch_layout, int search_flags); /** * Set a binary option to an integer list. @@ -700,6 +702,7 @@ int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_ int av_opt_get_pixel_fmt (void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt); int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt); int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val); +int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *ch_layout); /** * @} */ diff --git a/libavutil/version.h b/libavutil/version.h index 001b612d4e..956187e765 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -75,8 +75,8 @@ */ #define LIBAVUTIL_VERSION_MAJOR 52 -#define LIBAVUTIL_VERSION_MINOR 46 -#define LIBAVUTIL_VERSION_MICRO 101 +#define LIBAVUTIL_VERSION_MINOR 47 +#define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ LIBAVUTIL_VERSION_MINOR, \