History

lavd: add device capabilities API

Provides API to query device capabilities. Each device must implement callbacks to benefit from this API.

Lukasz Marek committed 12 years ago

Commit 6db42a21 parent 4899ccd6 files · +239 −3Browse files

Unified · Split

6 files
  1. doc/APIchanges +4 −0
  2. libavdevice/avdevice.c +81 −0
  3. libavdevice/avdevice.h +128 −0
  4. libavdevice/version.h +1 −1
  5. libavformat/avformat.h +23 −0
  6. libavformat/version.h +2 −2
doc/APIchanges+4−0
@@ -15,6 +15,10 @@ libavutil: 2012-10-22 · 14 unmodified lines
1515
1616 API changes, most recent first:
1717
18+2014-04-xx - xxxxxxx - lavd 55.12.100 - avdevice.h
19+ Add avdevice_capabilities_create() function.
20+ Add avdevice_capabilities_free() function.
21+
1822 2014-04-xx - xxxxxxx - lavu 53.11.0 - pixfmt.h
1923 Add AV_PIX_FMT_YVYU422 pixel format.
2024
libavdevice/avdevice.c+81−0
@@ -17,9 +17,46 @@ · 16 unmodified lines
1717 */
1818
1919 #include "libavutil/avassert.h"
20+#include "libavutil/samplefmt.h"
21+#include "libavutil/pixfmt.h"
22+#include "libavcodec/avcodec.h"
2023 #include "avdevice.h"
2124 #include "config.h"
2225
26+#define E AV_OPT_FLAG_ENCODING_PARAM
27+#define D AV_OPT_FLAG_DECODING_PARAM
28+#define A AV_OPT_FLAG_AUDIO_PARAM
29+#define V AV_OPT_FLAG_VIDEO_PARAM
30+#define OFFSET(x) offsetof(AVDeviceCapabilitiesQuery, x)
31+
32+const AVOption av_device_capabilities[] = {
33+ { "codec", "codec", OFFSET(codec), AV_OPT_TYPE_INT,
34+ {.i64 = AV_CODEC_ID_NONE}, AV_CODEC_ID_NONE, INT_MAX, E|D|A|V },
35+ { "sample_format", "sample format", OFFSET(sample_format), AV_OPT_TYPE_INT,
36+ {.i64 = AV_SAMPLE_FMT_NONE}, -1, INT_MAX, E|D|A },
37+ { "sample_rate", "sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT,
38+ {.i64 = -1}, -1, INT_MAX, E|D|A },
39+ { "channels", "channels", OFFSET(channels), AV_OPT_TYPE_INT,
40+ {.i64 = -1}, -1, INT_MAX, E|D|A },
41+ { "channel_layout", "channel layout", OFFSET(channel_layout), AV_OPT_TYPE_INT64,
42+ {.i64 = -1}, -1, INT_MAX, E|D|A },
43+ { "pixel_format", "pixel format", OFFSET(pixel_format), AV_OPT_TYPE_INT,
44+ {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX, E|D|V },
45+ { "window_size", "window size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE,
46+ {.str = NULL}, -1, INT_MAX, E|D|V },
47+ { "frame_size", "frame size", OFFSET(frame_width), AV_OPT_TYPE_IMAGE_SIZE,
48+ {.str = NULL}, -1, INT_MAX, E|D|V },
49+ { "fps", "fps", OFFSET(fps), AV_OPT_TYPE_RATIONAL,
50+ {.dbl = -1}, -1, INT_MAX, E|D|V },
51+ { NULL }
52+};
53+
54+#undef E
55+#undef D
56+#undef A
57+#undef V
58+#undef OFFSET
59+
2360 unsigned avdevice_version(void)
2461 {
2562 av_assert0(LIBAVDEVICE_VERSION_MICRO >= 100);
@@ -99,6 +136,50 @@ int avdevice_dev_to_app_control_message(struct AVFormatContext *s, enum AVDevToA · 73 unmodified lines
99136 return s->control_message_cb(s, type, data, data_size);
100137 }
101138
139+int avdevice_capabilities_create(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s,
140+ AVDictionary **device_options)
141+{
142+ int ret;
143+ av_assert0(s && caps);
144+ av_assert0(s->iformat || s->oformat);
145+ if ((s->oformat && !s->oformat->create_device_capabilities) ||
146+ (s->iformat && !s->iformat->create_device_capabilities))
147+ return AVERROR(ENOSYS);
148+ *caps = av_mallocz(sizeof(**caps));
149+ if (!(*caps))
150+ return AVERROR(ENOMEM);
151+ (*caps)->device_context = s;
152+ if (((ret = av_opt_set_dict(s->priv_data, device_options)) < 0))
153+ goto fail;
154+ if (s->iformat) {
155+ if ((ret = s->iformat->create_device_capabilities(s, *caps)) < 0)
156+ goto fail;
157+ } else {
158+ if ((ret = s->oformat->create_device_capabilities(s, *caps)) < 0)
159+ goto fail;
160+ }
161+ av_opt_set_defaults(*caps);
162+ return 0;
163+ fail:
164+ av_freep(caps);
165+ return ret;
166+}
167+
168+void avdevice_capabilities_free(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s)
169+{
170+ if (!s || !caps || !(*caps))
171+ return;
172+ av_assert0(s->iformat || s->oformat);
173+ if (s->iformat) {
174+ if (s->iformat->free_device_capabilities)
175+ s->iformat->free_device_capabilities(s, *caps);
176+ } else {
177+ if (s->oformat->free_device_capabilities)
178+ s->oformat->free_device_capabilities(s, *caps);
179+ }
180+ av_freep(caps);
181+}
182+
102183 int avdevice_list_devices(AVFormatContext *s, AVDeviceInfoList **device_list)
103184 {
104185 int ret;
libavdevice/avdevice.h+128−0
@@ -43,6 +43,9 @@ · 42 unmodified lines
4343 * @}
4444 */
4545
46+#include "libavutil/log.h"
47+#include "libavutil/opt.h"
48+#include "libavutil/dict.h"
4649 #include "libavformat/avformat.h"
4750
4851 /**
@@ -227,6 +230,131 @@ int avdevice_dev_to_app_control_message(struct AVFormatContext *s, · 178 unmodified lines
227230 enum AVDevToAppMessageType type,
228231 void *data, size_t data_size);
229232
233+/**
234+ * Following API allows user to probe device capabilities (supported codecs,
235+ * pixel formats, sample formats, resolutions, channel counts, etc).
236+ * It is build on top op AVOption API.
237+ * Queried capabilities allows to set up converters of video or audio
238+ * parameters that fit to the device.
239+ *
240+ * List of capablities that can be queried:
241+ * - Capabilities valid for both audio and video devices:
242+ * - codec: supported audio/video codecs.
243+ * type: AV_OPT_TYPE_INT (AVCodecID value)
244+ * - Capabilities valid for audio devices:
245+ * - sample_format: supported sample formats.
246+ * type: AV_OPT_TYPE_INT (AVSampleFormat value)
247+ * - sample_rate: supported sample rates.
248+ * type: AV_OPT_TYPE_INT
249+ * - channels: supported number of channels.
250+ * type: AV_OPT_TYPE_INT
251+ * - channel_layout: supported channel layouts.
252+ * type: AV_OPT_TYPE_INT64
253+ * - Capabilities valid for video devices:
254+ * - pixel_format: supported pixel formats.
255+ * type: AV_OPT_TYPE_INT (AVPixelFormat value)
256+ * - window_size: supported window sizes (describes size of the window size presented to the user).
257+ * type: AV_OPT_TYPE_IMAGE_SIZE
258+ * - frame_size: supported frame sizes (describes size of provided video frames).
259+ * type: AV_OPT_TYPE_IMAGE_SIZE
260+ * - fps: supported fps values
261+ * type: AV_OPT_TYPE_RATIONAL
262+ *
263+ * Value of the capability may be set by user using av_opt_set() function
264+ * and AVDeviceCapabilitiesQuery object. Following queries will
265+ * limit results to the values matching already set capabilities.
266+ * For example, setting a codec may impact number of formats or fps values
267+ * returned during next query. Setting invalid value may limit results to zero.
268+ *
269+ * Example of the usage basing on opengl output device:
270+ *
271+ * @code
272+ * AVFormatContext *oc = NULL;
273+ * AVDeviceCapabilitiesQuery *caps = NULL;
274+ * AVOptionRanges *ranges;
275+ * int ret;
276+ *
277+ * if ((ret = avformat_alloc_output_context2(&oc, NULL, "opengl", NULL)) < 0)
278+ * goto fail;
279+ * if (avdevice_capabilities_create(&caps, oc, NULL) < 0)
280+ * goto fail;
281+ *
282+ * //query codecs
283+ * if (av_opt_query_ranges(&ranges, caps, "codec", AV_OPT_MULTI_COMPONENT_RANGE)) < 0)
284+ * goto fail;
285+ * //pick codec here and set it
286+ * av_opt_set(caps, "codec", AV_CODEC_ID_RAWVIDEO, 0);
287+ *
288+ * //query format
289+ * if (av_opt_query_ranges(&ranges, caps, "pixel_format", AV_OPT_MULTI_COMPONENT_RANGE)) < 0)
290+ * goto fail;
291+ * //pick format here and set it
292+ * av_opt_set(caps, "pixel_format", AV_PIX_FMT_YUV420P, 0);
293+ *
294+ * //query and set more capabilities
295+ *
296+ * fail:
297+ * //clean up code
298+ * avdevice_capabilities_free(&query, oc);
299+ * avformat_free_context(oc);
300+ * @endcode
301+ */
302+
303+/**
304+ * Structure describes device capabilites.
305+ *
306+ * It is used by devices in conjuntion with av_device_capabilities AVOption table
307+ * to implement capabilities probing API based on AVOption API. Should not be used directly.
308+ */
309+typedef struct AVDeviceCapabilitiesQuery {
310+ const AVClass *class;
311+ AVFormatContext *device_context;
312+ enum AVCodecID codec;
313+ enum AVSampleFormat sample_format;
314+ enum AVPixelFormat pixel_format;
315+ int sample_rate;
316+ int channels;
317+ int64_t channel_layout;
318+ int window_width;
319+ int window_height;
320+ int frame_width;
321+ int frame_height;
322+ AVRational fps;
323+} AVDeviceCapabilitiesQuery;
324+
325+/**
326+ * AVOption table used by devices to implement device capabilites API. Should not be used by a user.
327+ */
328+extern const AVOption av_device_capabilities[];
329+
330+/**
331+ * Initialize capabilities probing API based on AVOption API.
332+ *
333+ * avdevice_capabilities_free() must be called when query capabilities API is
334+ * not used anymore.
335+ *
336+ * @param[out] caps Device capabilities data. Pointer to a NULL pointer must be passed.
337+ * @param s Context of the device.
338+ * @param device_options An AVDictionary filled with device-private options.
339+ * On return this parameter will be destroyed and replaced with a dict
340+ * containing options that were not found. May be NULL.
341+ * The same options must be passed later to avformat_write_header() for output
342+ * devices or avformat_open_input() for input devices, or at any other place
343+ * that affects device-private options.
344+ *
345+ * @return >= 0 on success, negative otherwise.
346+ */
347+int avdevice_capabilities_create(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s,
348+ AVDictionary **device_options);
349+
350+/**
351+ * Free resources created by avdevice_capabilities_create()
352+ *
353+ * @param caps Device capabilities data to be freed.
354+ * @param s Context of the device.
355+ */
356+void avdevice_capabilities_free(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s);
357+
230358 /**
231359 * Structure describes basic parameters of the device.
232360 */
libavdevice/version.h+1−1
@@ -28,7 +28,7 @@ · 27 unmodified lines
2828 #include "libavutil/version.h"
2929
3030 #define LIBAVDEVICE_VERSION_MAJOR 55
31−#define LIBAVDEVICE_VERSION_MINOR 11
31+#define LIBAVDEVICE_VERSION_MINOR 12
3232 #define LIBAVDEVICE_VERSION_MICRO 100
3333
3434 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \
libavformat/avformat.h+23−0
@@ -262,6 +262,7 @@ · 261 unmodified lines
262262 struct AVFormatContext;
263263
264264 struct AVDeviceInfoList;
265+struct AVDeviceCapabilitiesQuery;
265266
266267 /**
267268 * @defgroup metadata_api Public Metadata API
@@ -531,6 +532,16 @@ typedef struct AVOutputFormat { · 263 unmodified lines
531532 * @see avdevice_list_devices() for more details.
532533 */
533534 int (*get_device_list)(struct AVFormatContext *s, struct AVDeviceInfoList *device_list);
535+ /**
536+ * Initialize device capabilities submodule.
537+ * @see avdevice_capabilities_create() for more details.
538+ */
539+ int (*create_device_capabilities)(struct AVFormatContext *s, struct AVDeviceCapabilitiesQuery *caps);
540+ /**
541+ * Free device capabilities submodule.
542+ * @see avdevice_capabilities_free() for more details.
543+ */
544+ int (*free_device_capabilities)(struct AVFormatContext *s, struct AVDeviceCapabilitiesQuery *caps);
534545 } AVOutputFormat;
535546 /**
536547 * @}
@@ -665,6 +676,18 @@ typedef struct AVInputFormat { · 128 unmodified lines
665676 * @see avdevice_list_devices() for more details.
666677 */
667678 int (*get_device_list)(struct AVFormatContext *s, struct AVDeviceInfoList *device_list);
679+
680+ /**
681+ * Initialize device capabilities submodule.
682+ * @see avdevice_capabilities_create() for more details.
683+ */
684+ int (*create_device_capabilities)(struct AVFormatContext *s, struct AVDeviceCapabilitiesQuery *caps);
685+
686+ /**
687+ * Free device capabilities submodule.
688+ * @see avdevice_capabilities_free() for more details.
689+ */
690+ int (*free_device_capabilities)(struct AVFormatContext *s, struct AVDeviceCapabilitiesQuery *caps);
668691 } AVInputFormat;
669692 /**
670693 * @}
libavformat/version.h+2−2
@@ -30,8 +30,8 @@ · 29 unmodified lines
3030 #include "libavutil/version.h"
3131
3232 #define LIBAVFORMAT_VERSION_MAJOR 55
33−#define LIBAVFORMAT_VERSION_MINOR 36
34−#define LIBAVFORMAT_VERSION_MICRO 103
33+#define LIBAVFORMAT_VERSION_MINOR 37
34+#define LIBAVFORMAT_VERSION_MICRO 100
3535
3636 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
3737 LIBAVFORMAT_VERSION_MINOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t