lavu/opt: add av_opt_ accessors for pixel/format/image size options

The interface is implemented against the style of the other options
accessors. Possibly simplify programmatic setting of options.
This commit is contained in:
Stefano Sabatini 2012-11-04 15:32:56 +01:00
parent fdd71cf04c
commit c70ec631c9
4 changed files with 120 additions and 1 deletions

View File

@ -15,6 +15,15 @@ libavutil: 2012-10-22
API changes, most recent first:
2012-11-25 - xxxxxxx - lavu 52.9.100 - opt.h
Add the following convenience functions to opt.h:
av_opt_get_image_size
av_opt_get_pixel_fmt
av_opt_get_sample_fmt
av_opt_set_image_size
av_opt_set_pixel_fmt
av_opt_set_sample_fmt
2012-11-17 - xxxxxxx - lavu 52.8.100 - bprint.h
Add av_bprint_strftime().

View File

@ -402,6 +402,62 @@ int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int
return 0;
}
int av_opt_set_image_size(void *obj, const char *name, int w, int h, 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_IMAGE_SIZE) {
av_log(obj, AV_LOG_ERROR,
"The value set by option '%s' is not an image size.\n", o->name);
return AVERROR(EINVAL);
}
if (w<0 || h<0) {
av_log(obj, AV_LOG_ERROR,
"Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
return AVERROR(EINVAL);
}
*(int *)(((uint8_t *)target_obj) + o->offset) = w;
*(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
return 0;
}
static int set_format(void *obj, const char *name, int fmt, int search_flags,
enum AVOptionType type, const char *desc, int max)
{
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 != type) {
av_log(obj, AV_LOG_ERROR,
"The value set by option '%s' is not a %s format", name, desc);
return AVERROR(EINVAL);
}
if (fmt < -1 || fmt > max) {
av_log(obj, AV_LOG_ERROR,
"Value %d for parameter '%s' out of %s format range [0 - %d]\n",
fmt, name, desc, max);
return AVERROR(ERANGE);
}
*(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
return 0;
}
int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
{
return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB-1);
}
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
{
return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB-1);
}
#if FF_API_OLD_AVOPTIONS
/**
*
@ -596,6 +652,52 @@ int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_
return 0;
}
int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
{
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_IMAGE_SIZE) {
av_log(obj, AV_LOG_ERROR,
"The value for option '%s' is not an image size.\n", name);
return AVERROR(EINVAL);
}
dst = ((uint8_t*)target_obj) + o->offset;
if (w_out) *w_out = *(int *)dst;
if (h_out) *h_out = *((int *)dst+1);
return 0;
}
static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
enum AVOptionType type, const char *desc)
{
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 != type) {
av_log(obj, AV_LOG_ERROR,
"The value for option '%s' is not a %s format.\n", desc, name);
return AVERROR(EINVAL);
}
dst = ((uint8_t*)target_obj) + o->offset;
*out_fmt = *(int *)dst;
return 0;
}
int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
{
return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
}
int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
{
return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
}
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);

View File

@ -31,6 +31,8 @@
#include "avutil.h"
#include "dict.h"
#include "log.h"
#include "pixfmt.h"
#include "samplefmt.h"
/**
* @defgroup avoptions AVOptions
@ -629,6 +631,9 @@ int av_opt_set_int (void *obj, const char *name, int64_t val, int search_f
int av_opt_set_double(void *obj, const char *name, double val, int search_flags);
int av_opt_set_q (void *obj, const char *name, AVRational val, int search_flags);
int av_opt_set_bin (void *obj, const char *name, const uint8_t *val, int size, int search_flags);
int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags);
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);
/**
* @}
*/
@ -652,6 +657,9 @@ int av_opt_get (void *obj, const char *name, int search_flags, uint8_t *
int av_opt_get_int (void *obj, const char *name, int search_flags, int64_t *out_val);
int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val);
int av_opt_get_q (void *obj, const char *name, int search_flags, AVRational *out_val);
int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out);
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);
/**
* @}
*/

View File

@ -75,7 +75,7 @@
*/
#define LIBAVUTIL_VERSION_MAJOR 52
#define LIBAVUTIL_VERSION_MINOR 8
#define LIBAVUTIL_VERSION_MINOR 9
#define LIBAVUTIL_VERSION_MICRO 100
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \