[FFmpeg-devel] [PATCH] lavu/opt: add av_opt_ accessors for pixel/format/image size options

Stefano Sabatini stefasab at gmail.com
Wed Nov 7 23:58:07 CET 2012


On date Tuesday 2012-11-06 13:31:53 +0100, Michael Niedermayer encoded:
> On Tue, Nov 06, 2012 at 01:09:38AM +0100, Stefano Sabatini wrote:
> > The interface is implemented against the style of the other options
> > accessors. Possibly simplify programmatic setting of options.
> > 
> > TODO: bump minor, update Changelog
> > ---
> >  libavutil/opt.c |  105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
> >  libavutil/opt.h |    8 ++++
> >  2 files changed, 113 insertions(+), 0 deletions(-)
> > 
> > diff --git a/libavutil/opt.c b/libavutil/opt.c
> > index 05ae864..e36f8f6 100644
> > --- a/libavutil/opt.c
> > +++ b/libavutil/opt.c
> > @@ -25,6 +25,7 @@
> >   * @author Michael Niedermayer <michaelni at gmx.at>
> >   */
> >  
> > +#include "avassert.h"
> >  #include "avutil.h"
> >  #include "avstring.h"
> >  #include "common.h"
> > @@ -71,6 +72,8 @@ static int read_number(const AVOption *o, void *dst, double *num, int *den, int6
> >  {
> >      switch (o->type) {
> >      case AV_OPT_TYPE_FLAGS:     *intnum = *(unsigned int*)dst;return 0;
> > +    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_INT64:     *intnum = *(int64_t     *)dst;return 0;
> >      case AV_OPT_TYPE_FLOAT:     *num    = *(float       *)dst;return 0;
> > @@ -93,6 +96,8 @@ static int write_number(void *obj, const AVOption *o, void *dst, double num, int
> >  
> >      switch (o->type) {
> >      case AV_OPT_TYPE_FLAGS:
> > +    case AV_OPT_TYPE_PIXEL_FMT:
> > +    case AV_OPT_TYPE_SAMPLE_FMT:
> >      case AV_OPT_TYPE_INT:   *(int       *)dst= llrint(num/den)*intnum; break;
> >      case AV_OPT_TYPE_INT64: *(int64_t   *)dst= llrint(num/den)*intnum; break;
> >      case AV_OPT_TYPE_FLOAT: *(float     *)dst= num*intnum/den;         break;
> > @@ -402,6 +407,70 @@ 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)
> > +{
> > +    void *target_obj;
> > +    const AVOption *o = av_opt_find2(obj, name, NULL, 0,
> > +                                     search_flags, &target_obj);
> > +    int max;
> > +    const char *typedesc;
> > +    switch (type) {
> > +    case AV_OPT_TYPE_PIXEL_FMT : typedesc = "pixel" ; max = AV_PIX_FMT_NB   -1; break;
> > +    case AV_OPT_TYPE_SAMPLE_FMT: typedesc = "sample"; max = AV_SAMPLE_FMT_NB-1; break;
> > +    default: av_assert0(type == AV_OPT_TYPE_SAMPLE_FMT || type == AV_OPT_TYPE_SAMPLE_FMT);
> > +    }
> > +
> > +    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, typedesc);
> > +        return AVERROR(EINVAL);
> > +    }
> > +
> > +    if (fmt < -1 || fmt > max) {
> > +        av_log(obj, AV_LOG_ERROR,
> > +               "Value %d for parameter '%s' out of range [%d - %d]\n",
> > +               fmt, name, -1, 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);
> > +}
> > +
> > +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);
> > +}
> 

> This looks largely redundant with av_opt_set_int()
> the max/min is also checked already through the max/min fields in
> AVOptions

Possible advantages of this approach: more sensible error messages, no
need to set boilerplate min/max values in the option table.

> 
> 
> > +
> >  #if FF_API_OLD_AVOPTIONS
> >  /**
> >   *
> > @@ -596,6 +665,42 @@ 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 of 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;
> > +}
> > +
> 
> > +int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
> > +{
> > +    int64_t i64;
> > +    int ret = av_opt_get_int(obj, name, search_flags, &i64);
> > +    if (ret >= 0)
> > +        *out_fmt = i64;
> > +    return ret;
> > +}
> > +
> > +int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
> > +{
> > +    int64_t i64;
> > +    int ret = av_opt_get_int(obj, name, search_flags, &i64);
> > +    if (ret >= 0)
> > +        *out_fmt = i64;
> > +    return ret;
> > +}
> 

> Iam not sure a public function pair for each type added is a good
> idea.

This is basically following what is already done in the defied
interface.
 
> But if we go this route, at least the code should check the types,
> i would not want to have a flags or pixel format read as
> AVSampleFormat without warning

Fixed.
-- 
FFmpeg = Faithless and Fanciful Minimal Puristic Exuberant Gangster
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0004-lavu-opt-add-av_opt_-accessors-for-pixel-format-imag.patch
Type: text/x-diff
Size: 6978 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20121107/d54b586e/attachment.bin>


More information about the ffmpeg-devel mailing list