[FFmpeg-devel] [PATCH 5/8] lavd: add device capabilities API

Michael Niedermayer michaelni at gmx.at
Thu Apr 3 16:56:47 CEST 2014


On Sat, Mar 29, 2014 at 10:13:40AM +0100, Lukasz Marek wrote:
> On 25 March 2014 00:39, Lukasz Marek <lukasz.m.luki2 at gmail.com> wrote:
[...]
> This patch also can be merged if there is no more remarks. I attached it
> again, but there should be no changes.

>  libavdevice/avdevice.c |   97 +++++++++++++++++++++++++++++++++++++++++++++++++
>  libavdevice/avdevice.h |   79 +++++++++++++++++++++++++++++++++++++++
>  libavformat/avformat.h |   36 ++++++++++++++++++
>  3 files changed, 212 insertions(+)
> 70d45a7e5ba10d822295db0cdc56f818541d8a93  0001-lavd-add-device-capabilities-API.patch
> From dede45d8dd9e4ed1531ca7023e65de220a8ecd98 Mon Sep 17 00:00:00 2001
> From: Lukasz Marek <lukasz.m.luki at gmail.com>
> Date: Sat, 22 Feb 2014 23:33:00 +0100
> Subject: [PATCH] lavd: add device capabilities API
> 
> TODO: bump minors, update APIchages
> 
> Signed-off-by: Lukasz Marek <lukasz.m.luki at gmail.com>
> ---
>  libavdevice/avdevice.c |   97 ++++++++++++++++++++++++++++++++++++++++++++++++
>  libavdevice/avdevice.h |   79 +++++++++++++++++++++++++++++++++++++++
>  libavformat/avformat.h |   36 ++++++++++++++++++
>  3 files changed, 212 insertions(+)
> 
> diff --git a/libavdevice/avdevice.c b/libavdevice/avdevice.c
> index ea14c7a..e1eb69c 100644
> --- a/libavdevice/avdevice.c
> +++ b/libavdevice/avdevice.c
> @@ -17,9 +17,46 @@
>   */
>  
>  #include "libavutil/avassert.h"
> +#include "libavcodec/avcodec.h"
>  #include "avdevice.h"
>  #include "config.h"
>  
> +#define E AV_OPT_FLAG_ENCODING_PARAM
> +#define D AV_OPT_FLAG_DECODING_PARAM
> +#define A AV_OPT_FLAG_AUDIO_PARAM
> +#define V AV_OPT_FLAG_VIDEO_PARAM
> +#define OFFSET(x) offsetof(AVDeviceCapabilitiesQuery, x)
> +
> +const AVOption av_device_capabilities[] = {
> +    { "device_name", "device name", OFFSET(device_name), AV_OPT_TYPE_STRING,
> +        {.str = NULL}, 0, 0, E|D|A|V },
> +    { "codec", "codec", OFFSET(codec), AV_OPT_TYPE_INT,
> +        {.i64 = AV_CODEC_ID_NONE}, AV_CODEC_ID_NONE, INT_MAX, E|D|A|V },
> +    { "format", "format", OFFSET(format), AV_OPT_TYPE_INT,
> +        {.i64 = -1}, -1, INT_MAX, E|D|A|V },
> +
> +    { "sample_rate", "sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT,
> +        {.i64 = -1}, -1, INT_MAX, E|D|A },
> +    { "channels", "channels", OFFSET(channels), AV_OPT_TYPE_INT,
> +        {.i64 = -1}, -1, INT_MAX, E|D|A },
> +    { "channel_layout", "channel layout", OFFSET(channel_layout), AV_OPT_TYPE_INT64,
> +        {.i64 = -1}, -1, INT_MAX, E|D|A },
> +
> +    { "window_size", "window size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE,
> +        {.str = NULL}, -1, INT_MAX, E|D|V },
> +    { "frame_size", "frame size", OFFSET(frame_width), AV_OPT_TYPE_IMAGE_SIZE,
> +        {.str = NULL}, -1, INT_MAX, E|D|V },
> +    { "fps", "fps", OFFSET(fps), AV_OPT_TYPE_RATIONAL,
> +        {.dbl = -1}, -1, INT_MAX, E|D|V },
> +    { NULL }
> +};
> +
> +#undef E
> +#undef D
> +#undef A
> +#undef V
> +#undef OFFSET
> +
>  unsigned avdevice_version(void)
>  {
>      av_assert0(LIBAVDEVICE_VERSION_MICRO >= 100);
> @@ -99,6 +136,66 @@ int avdevice_dev_to_app_control_message(struct AVFormatContext *s, enum AVDevToA
>      return s->control_message_cb(s, type, data, data_size);
>  }
>  
> +int avdevice_capabilities_create(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s,
> +                                 AVDictionary **device_options)
> +{
> +    int ret;
> +    av_assert0(s && caps);
> +    av_assert0(s->iformat || s->oformat);
> +    if ((s->oformat && !s->oformat->create_device_capabilities) ||
> +        (s->iformat && !s->iformat->create_device_capabilities))
> +        return AVERROR(ENOSYS);
> +    *caps = av_mallocz(sizeof(**caps));
> +    if (!(*caps))
> +        return AVERROR(ENOMEM);
> +    (*caps)->device_context = s;
> +    if (device_options && ((ret = av_opt_set_dict(s->priv_data, device_options)) < 0))
> +        goto fail;
> +    if (s->iformat) {
> +        if ((ret = s->iformat->create_device_capabilities(s, *caps)) < 0)
> +            goto fail;
> +    } else {
> +        if ((ret = s->oformat->create_device_capabilities(s, *caps)) < 0)
> +            goto fail;
> +    }
> +    av_opt_set_defaults(*caps);
> +    return 0;
> +  fail:
> +    av_freep(caps);
> +    return ret;
> +}
> +
> +int avdevice_capabilities_apply(AVDeviceCapabilitiesQuery *caps, AVFormatContext *s,
> +                               enum AVDeviceCapabilitiesApplyStrategy strategy)
> +{
> +    av_assert0(s);
> +    av_assert0(s->iformat || s->oformat);
> +    if (s->iformat) {
> +        if (!s->iformat->apply_device_capabilities)
> +            return AVERROR(ENOSYS);
> +        return s->iformat->apply_device_capabilities(s, caps, strategy);
> +    }
> +    if (!s->oformat->apply_device_capabilities)
> +        return AVERROR(ENOSYS);
> +    return s->oformat->apply_device_capabilities(s, caps, strategy);
> +}
> +
> +void avdevice_capabilities_free(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s)
> +{
> +    if (!s || !caps || !(*caps))
> +        return;
> +    av_assert0(s->iformat || s->oformat);
> +    if (s->iformat) {
> +        if (s->iformat->free_device_capabilities)
> +            s->iformat->free_device_capabilities(s, *caps);
> +    } else {
> +        if (s->oformat->free_device_capabilities)
> +            s->oformat->free_device_capabilities(s, *caps);
> +    }

> +    av_free((*caps)->device_name);

should use av_freep() for saftey


> +    av_freep(caps);
> +}
> +
>  int avdevice_list_devices(AVFormatContext *s, AVDeviceInfoList **device_list)
>  {
>      int ret;
> diff --git a/libavdevice/avdevice.h b/libavdevice/avdevice.h
> index 8b78067..571244f 100644
> --- a/libavdevice/avdevice.h
> +++ b/libavdevice/avdevice.h
> @@ -43,6 +43,9 @@
>   * @}
>   */
>  
> +#include "libavutil/log.h"
> +#include "libavutil/opt.h"
> +#include "libavutil/dict.h"
>  #include "libavformat/avformat.h"
>  
>  /**
> @@ -228,6 +231,82 @@ int avdevice_dev_to_app_control_message(struct AVFormatContext *s,
>                                          void *data, size_t data_size);
>  
>  /**
> + * Structure describes device settings.
> + *
> + * It is used by devices in conjuntion with av_device_capabilities AVOption table
> + * to to implement capabilities probing API based on AVOption API.

typo "to to"



> + */
> +typedef struct AVDeviceCapabilitiesQuery {
> +    const AVClass *class;
> +    char *device_name;
> +    AVFormatContext *device_context;
> +    enum AVCodecID codec;
> +    int format;                          /**< AVSampleFormat or AVPixelFormat */
> +    int sample_rate;
> +    int channels;
> +    int64_t channel_layout;
> +    int window_width;
> +    int window_height;
> +    int frame_width;
> +    int frame_height;
> +    AVRational fps;
> +} AVDeviceCapabilitiesQuery;
> +
> +extern const AVOption av_device_capabilities[];
> +
> +enum AVDeviceCapabilitiesApplyStrategy {
> +    AVDEVICE_STRATEGY_APPLY_IF_VALID,                /**< apply settings when valid */
> +    AVDEVICE_STRATEGY_FIX_TO_THE_NEAREST,            /**< adjust structure only to the nearest valid value */
> +    AVDEVICE_STRATEGY_FIX_TO_THE_BEST,               /**< adjust structure only to the best valid value */
> +    AVDEVICE_STRATEGY_FIX_TO_THE_NEAREST_AND_APPLY,  /**< adjust values to the nearest valid value and apply */
> +    AVDEVICE_STRATEGY_FIX_TO_THE_BEST_AND_APPLY      /**< adjust values to the best valid value and apply */
> +};
> +
> +/**
> + * Initialize capabilities probing API based on AVOption API.
> + *
> + * avdevice_capabilities_free() must be called when query capabilities API is
> + * not used anymore.
> + *

> + * @note: It is not allowed to free device context before calling avdevice_capabilities_free().

why ?


> + *
> + * @param caps           device configuration

caps is a pointer to a pointer to AVDeviceCapabilitiesQuery
what should the user set that to ?
NULL ?
some pointer that points to a NULL pointer ?


> + * @param s              device context

> + * @param device_options device options

same question ?
is this use for inputing or outputing values ?

these things should be documented
also what are device options?
its important to explain this as its a free form key-value list where
the user otherwise would probably have no clue what it is


> + * @return >= 0 on success, negative otherwise.
> + */
> +int avdevice_capabilities_create(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s,
> +                                 AVDictionary **device_options);
> +
> +/**
> + * Apply set parameters to device context and release data allocated
> + * by avdevice_init_device_capabilities().
> + *
> + * All set capabilities are validated and tested. When configuration is not
> + * valid then adjustment takes place according to provided strategy.
> + * When it is required by provided strategy, set capabilities are applied into
> + * device context. Mapping between capablities and device settings are device-specific.
> + * In particular output device may not apply all parameters to the context,
> + * but use stream properties when avformat_write_header() is called.
> + *
> + * @param caps     device configuration
> + * @param s        device context
> + * @param strategy apply strategy
> + * @return 0 when configuration is not applied, 1 when configuration is applied,
> + *         negative on error.
> + */
> +int avdevice_capabilities_apply(AVDeviceCapabilitiesQuery *caps, AVFormatContext *s,
> +                                enum AVDeviceCapabilitiesApplyStrategy strategy);

also can you explain in which cases this api improves over the
existing situation where parameters are set in AVFormatContext
and the devices private context via avoptions?


I mean now one could set sample_rate, fps, ... via AVOptions into
some input devices context (and with an AVDeviceCapabilitiesApplyStrategy
field the device could on init/open choose the closest supported

and with this API one create a seperate context fills the wanted
parameters in there and applies it to the same effect

I must be missing something as i dont see what advantage this
additional step has


[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Observe your enemies, for they first find out your faults. -- Antisthenes
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20140403/d5c81a4b/attachment.asc>


More information about the ffmpeg-devel mailing list