8 #include <stdio.h> // FILE
9 #include <libusb-1.0/libusb.h>
10 #include <libuvc/libuvc_config.h>
12 /** UVC error types, based on libusb errors
15 typedef enum uvc_error {
16 /** Success (no error) */
18 /** Input/output error */
20 /** Invalid parameter */
21 UVC_ERROR_INVALID_PARAM = -2,
23 UVC_ERROR_ACCESS = -3,
25 UVC_ERROR_NO_DEVICE = -4,
26 /** Entity not found */
27 UVC_ERROR_NOT_FOUND = -5,
30 /** Operation timed out */
31 UVC_ERROR_TIMEOUT = -7,
33 UVC_ERROR_OVERFLOW = -8,
36 /** System call interrupted */
37 UVC_ERROR_INTERRUPTED = -10,
38 /** Insufficient memory */
39 UVC_ERROR_NO_MEM = -11,
40 /** Operation not supported */
41 UVC_ERROR_NOT_SUPPORTED = -12,
42 /** Device is not UVC-compliant */
43 UVC_ERROR_INVALID_DEVICE = -50,
44 /** Mode not supported */
45 UVC_ERROR_INVALID_MODE = -51,
46 /** Resource has a callback (can't use polling and async) */
47 UVC_ERROR_CALLBACK_EXISTS = -52,
48 /** Undefined error */
52 /** Color coding of stream, transport-independent
55 enum uvc_frame_format {
56 UVC_FRAME_FORMAT_UNKNOWN = 0,
57 /** Any supported format */
58 UVC_FRAME_FORMAT_ANY = 0,
59 UVC_FRAME_FORMAT_UNCOMPRESSED,
60 UVC_FRAME_FORMAT_COMPRESSED,
61 /** YUYV/YUV2/YUV422: YUV encoding with one luminance value per pixel and
62 * one UV (chrominance) pair for every two pixels.
64 UVC_FRAME_FORMAT_YUYV,
65 UVC_FRAME_FORMAT_UYVY,
69 /** Motion-JPEG (or JPEG) encoded images */
70 UVC_FRAME_FORMAT_MJPEG,
71 UVC_FRAME_FORMAT_GRAY8,
72 /** Number of formats understood */
73 UVC_FRAME_FORMAT_COUNT,
76 /* UVC_COLOR_FORMAT_* have been replaced with UVC_FRAME_FORMAT_*. Please use
77 * UVC_FRAME_FORMAT_* instead of using these. */
78 #define UVC_COLOR_FORMAT_UNKNOWN UVC_FRAME_FORMAT_UNKNOWN
79 #define UVC_COLOR_FORMAT_UNCOMPRESSED UVC_FRAME_FORMAT_UNCOMPRESSED
80 #define UVC_COLOR_FORMAT_COMPRESSED UVC_FRAME_FORMAT_COMPRESSED
81 #define UVC_COLOR_FORMAT_YUYV UVC_FRAME_FORMAT_YUYV
82 #define UVC_COLOR_FORMAT_UYVY UVC_FRAME_FORMAT_UYVY
83 #define UVC_COLOR_FORMAT_RGB UVC_FRAME_FORMAT_RGB
84 #define UVC_COLOR_FORMAT_BGR UVC_FRAME_FORMAT_BGR
85 #define UVC_COLOR_FORMAT_MJPEG UVC_FRAME_FORMAT_MJPEG
86 #define UVC_COLOR_FORMAT_GRAY8 UVC_FRAME_FORMAT_GRAY8
88 /** UVC request code (A.8) */
90 UVC_RC_UNDEFINED = 0x00,
101 enum uvc_device_power_mode {
102 UVC_VC_VIDEO_POWER_MODE_FULL = 0x000b,
103 UVC_VC_VIDEO_POWER_MODE_DEVICE_DEPENDENT = 0x001b,
106 /** Camera terminal control selector (A.9.4) */
107 enum uvc_ct_ctrl_selector {
108 UVC_CT_CONTROL_UNDEFINED = 0x00,
109 UVC_CT_SCANNING_MODE_CONTROL = 0x01,
110 UVC_CT_AE_MODE_CONTROL = 0x02,
111 UVC_CT_AE_PRIORITY_CONTROL = 0x03,
112 UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL = 0x04,
113 UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL = 0x05,
114 UVC_CT_FOCUS_ABSOLUTE_CONTROL = 0x06,
115 UVC_CT_FOCUS_RELATIVE_CONTROL = 0x07,
116 UVC_CT_FOCUS_AUTO_CONTROL = 0x08,
117 UVC_CT_IRIS_ABSOLUTE_CONTROL = 0x09,
118 UVC_CT_IRIS_RELATIVE_CONTROL = 0x0a,
119 UVC_CT_ZOOM_ABSOLUTE_CONTROL = 0x0b,
120 UVC_CT_ZOOM_RELATIVE_CONTROL = 0x0c,
121 UVC_CT_PANTILT_ABSOLUTE_CONTROL = 0x0d,
122 UVC_CT_PANTILT_RELATIVE_CONTROL = 0x0e,
123 UVC_CT_ROLL_ABSOLUTE_CONTROL = 0x0f,
124 UVC_CT_ROLL_RELATIVE_CONTROL = 0x10,
125 UVC_CT_PRIVACY_CONTROL = 0x11
128 /** Processing unit control selector (A.9.5) */
129 enum uvc_pu_ctrl_selector {
130 UVC_PU_CONTROL_UNDEFINED = 0x00,
131 UVC_PU_BACKLIGHT_COMPENSATION_CONTROL = 0x01,
132 UVC_PU_BRIGHTNESS_CONTROL = 0x02,
133 UVC_PU_CONTRAST_CONTROL = 0x03,
134 UVC_PU_GAIN_CONTROL = 0x04,
135 UVC_PU_POWER_LINE_FREQUENCY_CONTROL = 0x05,
136 UVC_PU_HUE_CONTROL = 0x06,
137 UVC_PU_SATURATION_CONTROL = 0x07,
138 UVC_PU_SHARPNESS_CONTROL = 0x08,
139 UVC_PU_GAMMA_CONTROL = 0x09,
140 UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL = 0x0a,
141 UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL = 0x0b,
142 UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL = 0x0c,
143 UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL = 0x0d,
144 UVC_PU_DIGITAL_MULTIPLIER_CONTROL = 0x0e,
145 UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL = 0x0f,
146 UVC_PU_HUE_AUTO_CONTROL = 0x10,
147 UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL = 0x11,
148 UVC_PU_ANALOG_LOCK_STATUS_CONTROL = 0x12
151 /** USB terminal type (B.1) */
153 UVC_TT_VENDOR_SPECIFIC = 0x0100,
154 UVC_TT_STREAMING = 0x0101
157 /** Input terminal type (B.2) */
159 UVC_ITT_VENDOR_SPECIFIC = 0x0200,
160 UVC_ITT_CAMERA = 0x0201,
161 UVC_ITT_MEDIA_TRANSPORT_INPUT = 0x0202
164 /** Output terminal type (B.3) */
166 UVC_OTT_VENDOR_SPECIFIC = 0x0300,
167 UVC_OTT_DISPLAY = 0x0301,
168 UVC_OTT_MEDIA_TRANSPORT_OUTPUT = 0x0302
171 /** External terminal type (B.4) */
173 UVC_EXTERNAL_VENDOR_SPECIFIC = 0x0400,
174 UVC_COMPOSITE_CONNECTOR = 0x0401,
175 UVC_SVIDEO_CONNECTOR = 0x0402,
176 UVC_COMPONENT_CONNECTOR = 0x0403
179 /** Context, equivalent to libusb's contexts.
181 * May either own a libusb context or use one that's already made.
183 * Always create these with uvc_get_context.
186 typedef struct uvc_context uvc_context_t;
190 * Get this from uvc_get_device_list() or uvc_find_device().
193 typedef struct uvc_device uvc_device_t;
195 /** Handle on an open UVC device.
197 * Get one of these from uvc_open(). Once you uvc_close()
198 * it, it's no longer valid.
200 struct uvc_device_handle;
201 typedef struct uvc_device_handle uvc_device_handle_t;
203 /** Handle on an open UVC stream.
205 * Get one of these from uvc_stream_open*().
206 * Once you uvc_stream_close() it, it will no longer be valid.
208 struct uvc_stream_handle;
209 typedef struct uvc_stream_handle uvc_stream_handle_t;
211 /** Representation of the interface that brings data into the UVC device */
212 typedef struct uvc_input_terminal {
213 struct uvc_input_terminal *prev, *next;
214 /** Index of the terminal within the device */
216 /** Type of terminal (e.g., camera) */
217 enum uvc_it_type wTerminalType;
218 uint16_t wObjectiveFocalLengthMin;
219 uint16_t wObjectiveFocalLengthMax;
220 uint16_t wOcularFocalLength;
221 /** Camera controls (meaning of bits given in {uvc_ct_ctrl_selector}) */
223 } uvc_input_terminal_t;
225 typedef struct uvc_output_terminal {
226 struct uvc_output_terminal *prev, *next;
228 } uvc_output_terminal_t;
230 /** Represents post-capture processing functions */
231 typedef struct uvc_processing_unit {
232 struct uvc_processing_unit *prev, *next;
233 /** Index of the processing unit within the device */
235 /** Index of the terminal from which the device accepts images */
237 /** Processing controls (meaning of bits given in {uvc_pu_ctrl_selector}) */
239 } uvc_processing_unit_t;
241 /** Custom processing or camera-control functions */
242 typedef struct uvc_extension_unit {
243 struct uvc_extension_unit *prev, *next;
244 /** Index of the extension unit within the device */
246 /** GUID identifying the extension unit */
247 uint8_t guidExtensionCode[16];
248 /** Bitmap of available controls (manufacturer-dependent) */
250 } uvc_extension_unit_t;
252 enum uvc_status_class {
253 UVC_STATUS_CLASS_CONTROL = 0x10,
254 UVC_STATUS_CLASS_CONTROL_CAMERA = 0x11,
255 UVC_STATUS_CLASS_CONTROL_PROCESSING = 0x12,
258 enum uvc_status_attribute {
259 UVC_STATUS_ATTRIBUTE_VALUE_CHANGE = 0x00,
260 UVC_STATUS_ATTRIBUTE_INFO_CHANGE = 0x01,
261 UVC_STATUS_ATTRIBUTE_FAILURE_CHANGE = 0x02,
262 UVC_STATUS_ATTRIBUTE_UNKNOWN = 0xff
265 /** A callback function to accept status updates
268 typedef void(uvc_status_callback_t)(enum uvc_status_class status_class,
271 enum uvc_status_attribute status_attribute,
272 void *data, size_t data_len,
275 /** Structure representing a UVC device descriptor.
277 * (This isn't a standard structure.)
279 typedef struct uvc_device_descriptor {
284 /** UVC compliance level, e.g. 0x0100 (1.0), 0x0110 */
286 /** Serial number (null if unavailable) */
287 const char *serialNumber;
288 /** Device-reported manufacturer name (or null) */
289 const char *manufacturer;
290 /** Device-reporter product name (or null) */
292 } uvc_device_descriptor_t;
294 /** An image frame received from the UVC device
297 typedef struct uvc_frame {
298 /** Image data for this frame */
300 /** Size of image data buffer */
302 /** Width of image in pixels */
304 /** Height of image in pixels */
306 /** Pixel data format */
307 enum uvc_frame_format frame_format;
308 /** Number of bytes per horizontal line (undefined for compressed format) */
310 /** Frame number (may skip, but is strictly monotonically increasing) */
312 /** Estimate of system time when the device started capturing the image */
313 struct timeval capture_time;
314 /** Handle on the device that produced the image.
315 * @warning You must not call any uvc_* functions during a callback. */
316 uvc_device_handle_t *source;
317 /** Is the data buffer owned by the library?
318 * If 1, the data buffer can be arbitrarily reallocated by frame conversion
320 * If 0, the data buffer will not be reallocated or freed by the library.
321 * Set this field to zero if you are supplying the buffer.
323 uint8_t library_owns_data;
326 /** A callback function to handle incoming assembled UVC frames
329 typedef void(uvc_frame_callback_t)(struct uvc_frame *frame, void *user_ptr);
331 /** Streaming mode, includes all information needed to select stream
334 typedef struct uvc_stream_ctrl {
336 uint8_t bFormatIndex;
338 uint32_t dwFrameInterval;
339 uint16_t wKeyFrameRate;
340 uint16_t wPFrameRate;
341 uint16_t wCompQuality;
342 uint16_t wCompWindowSize;
344 uint32_t dwMaxVideoFrameSize;
345 uint32_t dwMaxPayloadTransferSize;
346 /** @todo add UVC 1.1 parameters */
347 uint8_t bInterfaceNumber;
350 uvc_error_t uvc_init(uvc_context_t **ctx, struct libusb_context *usb_ctx);
351 void uvc_exit(uvc_context_t *ctx);
353 uvc_error_t uvc_get_device_list(
355 uvc_device_t ***list);
356 void uvc_free_device_list(uvc_device_t **list, uint8_t unref_devices);
358 uvc_error_t uvc_get_device_descriptor(
360 uvc_device_descriptor_t **desc);
361 void uvc_free_device_descriptor(
362 uvc_device_descriptor_t *desc);
364 uint8_t uvc_get_bus_number(uvc_device_t *dev);
365 uint8_t uvc_get_device_address(uvc_device_t *dev);
367 uvc_error_t uvc_find_device(
370 int vid, int pid, const char *sn);
372 uvc_error_t uvc_open(
374 uvc_device_handle_t **devh);
375 void uvc_close(uvc_device_handle_t *devh);
377 uvc_device_t *uvc_get_device(uvc_device_handle_t *devh);
378 libusb_device_handle *uvc_get_libusb_handle(uvc_device_handle_t *devh);
380 void uvc_ref_device(uvc_device_t *dev);
381 void uvc_unref_device(uvc_device_t *dev);
383 void uvc_set_status_callback(uvc_device_handle_t *devh,
384 uvc_status_callback_t cb,
387 const uvc_input_terminal_t *uvc_get_input_terminals(uvc_device_handle_t *devh);
388 const uvc_output_terminal_t *uvc_get_output_terminals(uvc_device_handle_t *devh);
389 const uvc_processing_unit_t *uvc_get_processing_units(uvc_device_handle_t *devh);
390 const uvc_extension_unit_t *uvc_get_extension_units(uvc_device_handle_t *devh);
392 uvc_error_t uvc_get_stream_ctrl_format_size(
393 uvc_device_handle_t *devh,
394 uvc_stream_ctrl_t *ctrl,
395 enum uvc_frame_format format,
396 int width, int height,
400 uvc_error_t uvc_probe_stream_ctrl(
401 uvc_device_handle_t *devh,
402 uvc_stream_ctrl_t *ctrl);
404 uvc_error_t uvc_start_streaming(
405 uvc_device_handle_t *devh,
406 uvc_stream_ctrl_t *ctrl,
407 uvc_frame_callback_t *cb,
409 uint8_t isochronous);
411 uvc_error_t uvc_start_iso_streaming(
412 uvc_device_handle_t *devh,
413 uvc_stream_ctrl_t *ctrl,
414 uvc_frame_callback_t *cb,
417 void uvc_stop_streaming(uvc_device_handle_t *devh);
419 uvc_error_t uvc_stream_open_ctrl(uvc_device_handle_t *devh, uvc_stream_handle_t **strmh, uvc_stream_ctrl_t *ctrl);
420 uvc_error_t uvc_stream_ctrl(uvc_stream_handle_t *strmh, uvc_stream_ctrl_t *ctrl);
421 uvc_error_t uvc_stream_start(uvc_stream_handle_t *strmh,
422 uvc_frame_callback_t *cb,
424 uint8_t isochronous);
425 uvc_error_t uvc_stream_start_iso(uvc_stream_handle_t *strmh,
426 uvc_frame_callback_t *cb,
428 uvc_error_t uvc_stream_get_frame(
429 uvc_stream_handle_t *strmh,
433 uvc_error_t uvc_stream_stop(uvc_stream_handle_t *strmh);
434 void uvc_stream_close(uvc_stream_handle_t *strmh);
436 uvc_error_t uvc_get_power_mode(uvc_device_handle_t *devh, enum uvc_device_power_mode *mode, enum uvc_req_code req_code);
437 uvc_error_t uvc_set_power_mode(uvc_device_handle_t *devh, enum uvc_device_power_mode mode);
438 uvc_error_t uvc_get_ae_mode(uvc_device_handle_t *devh, int *mode, enum uvc_req_code req_code);
439 uvc_error_t uvc_set_ae_mode(uvc_device_handle_t *devh, int mode);
440 uvc_error_t uvc_get_ae_priority(uvc_device_handle_t *devh, uint8_t *priority, enum uvc_req_code req_code);
441 uvc_error_t uvc_set_ae_priority(uvc_device_handle_t *devh, uint8_t priority);
442 uvc_error_t uvc_get_exposure_abs(uvc_device_handle_t *devh, int *time, enum uvc_req_code req_code);
443 uvc_error_t uvc_set_exposure_abs(uvc_device_handle_t *devh, int time);
444 uvc_error_t uvc_get_exposure_rel(uvc_device_handle_t *devh, int *step, enum uvc_req_code req_code);
445 uvc_error_t uvc_set_exposure_rel(uvc_device_handle_t *devh, int step);
446 uvc_error_t uvc_get_scanning_mode(uvc_device_handle_t *devh, int *step, enum uvc_req_code req_code);
447 uvc_error_t uvc_set_scanning_mode(uvc_device_handle_t *devh, int mode);
448 uvc_error_t uvc_get_focus_abs(uvc_device_handle_t *devh, short *focus, enum uvc_req_code req_code);
449 uvc_error_t uvc_set_focus_abs(uvc_device_handle_t *devh, short focus);
450 uvc_error_t uvc_get_pantilt_abs(uvc_device_handle_t *devh, int *pan, int *tilt, enum uvc_req_code req_code);
451 uvc_error_t uvc_set_pantilt_abs(uvc_device_handle_t *devh, int pan, int tilt);
453 int uvc_get_ctrl_len(uvc_device_handle_t *devh, uint8_t unit, uint8_t ctrl);
454 int uvc_get_ctrl(uvc_device_handle_t *devh, uint8_t unit, uint8_t ctrl, void *data, int len, enum uvc_req_code req_code);
455 int uvc_set_ctrl(uvc_device_handle_t *devh, uint8_t unit, uint8_t ctrl, void *data, int len);
457 void uvc_perror(uvc_error_t err, const char *msg);
458 const char* uvc_strerror(uvc_error_t err);
459 void uvc_print_diag(uvc_device_handle_t *devh, FILE *stream);
460 void uvc_print_stream_ctrl(uvc_stream_ctrl_t *ctrl, FILE *stream);
462 uvc_frame_t *uvc_allocate_frame(size_t data_bytes);
463 void uvc_free_frame(uvc_frame_t *frame);
465 uvc_error_t uvc_duplicate_frame(uvc_frame_t *in, uvc_frame_t *out);
467 uvc_error_t uvc_yuyv2rgb(uvc_frame_t *in, uvc_frame_t *out);
468 uvc_error_t uvc_uyvy2rgb(uvc_frame_t *in, uvc_frame_t *out);
469 uvc_error_t uvc_any2rgb(uvc_frame_t *in, uvc_frame_t *out);
471 uvc_error_t uvc_yuyv2bgr(uvc_frame_t *in, uvc_frame_t *out);
472 uvc_error_t uvc_uyvy2bgr(uvc_frame_t *in, uvc_frame_t *out);
473 uvc_error_t uvc_any2bgr(uvc_frame_t *in, uvc_frame_t *out);
475 #ifdef LIBUVC_HAS_JPEG
476 uvc_error_t uvc_mjpeg2rgb(uvc_frame_t *in, uvc_frame_t *out);
483 #endif // !def(LIBUVC_H)