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

Michael Niedermayer michaelni at gmx.at
Fri Apr 4 19:28:36 CEST 2014


On Fri, Apr 04, 2014 at 03:35:54PM +0200, Lukasz Marek wrote:
> Hi,
> 
> Updated patch attached.
> I moved locally apply function to separate commit and attached patch
> doesn't contain it.
> I thought it may be helpful for input devices, but it is not required by
> this functionality so it may be discussed later.
> Maybe this apply function may be renamed to something like adjust caps or
> something, but I will think about it later.
> 
> I also removed device_name from const AVOption av_device_capabilities, as
> it is also redundant with device options or input/output name.

>  doc/APIchanges         |    4 +
>  libavdevice/avdevice.c |   77 ++++++++++++++++++++++++++++++
>  libavdevice/avdevice.h |  125 +++++++++++++++++++++++++++++++++++++++++++++++++
>  libavformat/avformat.h |   23 +++++++++
>  4 files changed, 229 insertions(+)
> 38e814f82e96f3302f0cf67bf78c06b2b9eaf61f  0001-lavd-add-device-capabilities-API.patch
> From a4bd09c5f3ffe7d4c7cdf2d87fb40a33abc49466 Mon Sep 17 00:00:00 2001
> From: Lukasz Marek <lukasz.m.luki at gmail.com>
> Date: Thu, 3 Apr 2014 21:11:19 +0200
> Subject: [PATCH] lavd: add device capabilities API
> 
> Provides API to query device capabilities.
> Each device must implement callbacks to benefit from this API.
> 
> Signed-off-by: Lukasz Marek <lukasz.m.luki at gmail.com>
> ---
>  doc/APIchanges         |    4 ++
>  libavdevice/avdevice.c |   77 +++++++++++++++++++++++++++++
>  libavdevice/avdevice.h |  125 ++++++++++++++++++++++++++++++++++++++++++++++++
>  libavformat/avformat.h |   23 +++++++++
>  4 files changed, 229 insertions(+)
> 
> diff --git a/doc/APIchanges b/doc/APIchanges
> index 237d39e..f27360d 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -15,6 +15,10 @@ libavutil:     2012-10-22
>  
>  API changes, most recent first:
>  
> +2014-04-xx - xxxxxxx - lavd 55.12.100 - avdevice.h
> +  Add avdevice_capabilities_create() function.
> +  Add avdevice_capabilities_free() function.
> +
>  2014-04-xx - xxxxxxx - lavu 53.09.0 - log.h
>    Add AV_LOG(c) macro to have 256 color debug messages.
>  
> diff --git a/libavdevice/avdevice.c b/libavdevice/avdevice.c
> index ea14c7a..f4cd2f3 100644
> --- a/libavdevice/avdevice.c
> +++ b/libavdevice/avdevice.c
> @@ -17,9 +17,42 @@
>   */
>  
>  #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[] = {
> +    { "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 +132,50 @@ 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 (((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;
> +}
> +
> +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_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..b1b675c 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,128 @@ int avdevice_dev_to_app_control_message(struct AVFormatContext *s,
>                                          void *data, size_t data_size);
>  
>  /**
> + * Following API allows user to probe device capabilities (supported codecs,
> + * pixel formats, sample formats, resolutions, channel counts, etc).
> + * It is build on top op AVOption API.
> + * Queried capabilities allows to set up converters of video or audio
> + * parameters that fit to the device.
> + *
> + * List of capablities that can be queried:
> + *  - Capabilities valid for both audio and video devices:
> + *    - codec:          supported audio/video codecs.
> + *                      type: AV_OPT_TYPE_INT (AVCodecID value)
> + *    - format:         supported pixel/sample formats.
> + *                      type: AV_OPT_TYPE_INT (AVPixelFormat or AVSampleFormat value)
> + *  - Capabilities valid for audio devices:
> + *    - sample_rate:    supported sample rates.
> + *                      type: AV_OPT_TYPE_INT
> + *    - channels:       supported number of channels.
> + *                      type: AV_OPT_TYPE_INT
> + *    - channel_layout: supported channel layouts.
> + *                      type: AV_OPT_TYPE_INT64
> + *  - Capabilities valid for audio devices:
> + *    - window_size:    supported window sizes (describes size of the window size presented to the user).
> + *                      type: AV_OPT_TYPE_IMAGE_SIZE
> + *    - frame_size:     supported frame sizes (describes size of provided video frames).
> + *                      type: AV_OPT_TYPE_IMAGE_SIZE
> + *    - fps:            supported fps values
> + *                      type: AV_OPT_TYPE_RATIONAL
> + *
> + * Value of the capability may be set by user using av_opt_set() function
> + * and AVDeviceCapabilitiesQuery object. Following queries will
> + * limit results to the values matching already set capabilities.
> + * For example, setting a codec may impact number of formats or fps values
> + * returned during next query. Setting invalid value may limit results to zero.
> + *
> + * Example of the usage basing on opengl output device:
> + *

> + * @code
> + *  AVFormatContext *oc = NULL;
> + *  AVDeviceCapabilitiesQuery *caps = NULL;
> + *  AVOptionRanges *ranges;
> + *  int ret;
> + *
> + *  if ((ret = avformat_alloc_output_context2(&oc, NULL, "opengl", NULL)) < 0)
> + *      goto fail;


> + *  if (avdevice_capabilities_create(&caps, oc, NULL) < 0)
> + *      goto fail;

can this be called multiple times ?
if not, why is a free function needed ?
caps could just be kept track of and freed in avformat_free_context()


> + *
> + *  //query codecs
> + *  if (av_opt_query_ranges(&ranges, caps, "codec", AV_OPT_MULTI_COMPONENT_RANGE)) < 0)
> + *      goto fail;
> + *  //pick codec here and set it
> + *  av_opt_set(caps, "codec", AV_CODEC_ID_RAWVIDEO, 0);
> + *
> + *  //query format
> + *  if (av_opt_query_ranges(&ranges, caps, "format", AV_OPT_MULTI_COMPONENT_RANGE)) < 0)
> + *      goto fail;
> + *  //pick format here and set it
> + *  av_opt_set(caps, "format", AV_PIX_FMT_YUV420P, 0);
> + *
> + *  //query and set more capabilities
> + *
> + * fail:
> + *  //clean up code
> + *  avdevice_capabilities_free(&query, oc);
> + *  avformat_free_context(oc);

naively i would have thought it could be done like this:

if ((ret = avformat_alloc_output_context2(&oc, NULL, "opengl", NULL)) < 0)
    goto fail;

if (av_opt_query_ranges(&ranges, oc, "codec", AV_OPT_MULTI_COMPONENT_RANGE)) < 0)
    goto fail;
av_opt_set(caps, "codec", AV_CODEC_ID_RAWVIDEO, 0);

if (av_opt_query_ranges(&ranges, oc, "format", AV_OPT_MULTI_COMPONENT_RANGE)) < 0)
    goto fail;
av_opt_set(caps, "format", AV_PIX_FMT_YUV420P, 0);

but maybe this has some issue, can you explain why a seperate
"external" struct wth alloc and apply functions is needed ?

Iam not agaist it at all if its neeedd, i am just trying to keep the
API simple if its not
and the difference here would be 2-3 function calls

[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Opposition brings concord. Out of discord comes the fairest harmony.
-- Heraclitus
-------------- 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/20140404/479d1c0a/attachment.asc>


More information about the ffmpeg-devel mailing list