1 /* 2 * uvc_h264.h - Definitions of the UVC H.264 Payload specification Version 1.0 3 * 4 * Copyright (c) 2011 USB Implementers Forum, Inc. 5 * 6 * Modification into glib-like header by : 7 * Copyright (C) 2012 Cisco Systems, Inc. 8 * Author: Youness Alaoui <youness.alaoui@collabora.co.uk> 9 * 10 * 11 * Permission is hereby granted, free of charge, to any person obtaining a copy 12 * of this software and associated documentation files (the "Software"), to deal 13 * in the Software without restriction, including without limitation the rights 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 * copies of the Software, and to permit persons to whom the Software is 16 * furnished to do so, subject to the following conditions: 17 * The above copyright notice and this permission notice shall be included in 18 * all copies or substantial portions of the Software. 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 * THE SOFTWARE. 26 */ 27 28 #ifndef _UVC_H264_H_ 29 #define _UVC_H264_H_ 30 31 /* Header File for the little-endian platform */ 32 33 #include <glib.h> 34 #include <glib-object.h> 35 36 #include <gst/video/video.h> 37 #include <linux/uvcvideo.h> 38 #include <linux/usb/video.h> 39 #include <sys/ioctl.h> 40 #include <string.h> 41 42 #include <gudev/gudev.h> 43 #include <libusb.h> 44 45 #include <gst/gst.h> 46 #include <libusb.h> 47 48 #define USB_VIDEO_CONTROL 1 49 #define USB_VIDEO_CONTROL_INTERFACE 0x24 50 #define USB_VIDEO_CONTROL_XU_TYPE 0x06 51 #ifndef LIBUSB_CLASS_VIDEO 52 #define LIBUSB_CLASS_VIDEO 0x0e 53 #endif 54 55 /* bmHints defines */ 56 57 #define UVC_H264_BMHINTS_RESOLUTION (0x0001) 58 #define UVC_H264_BMHINTS_PROFILE (0x0002) 59 #define UVC_H264_BMHINTS_RATECONTROL (0x0004) 60 #define UVC_H264_BMHINTS_USAGE (0x0008) 61 #define UVC_H264_BMHINTS_SLICEMODE (0x0010) 62 #define UVC_H264_BMHINTS_SLICEUNITS (0x0020) 63 #define UVC_H264_BMHINTS_MVCVIEW (0x0040) 64 #define UVC_H264_BMHINTS_TEMPORAL (0x0080) 65 #define UVC_H264_BMHINTS_SNR (0x0100) 66 #define UVC_H264_BMHINTS_SPATIAL (0x0200) 67 #define UVC_H264_BMHINTS_SPATIAL_RATIO (0x0400) 68 #define UVC_H264_BMHINTS_FRAME_INTERVAL (0x0800) 69 #define UVC_H264_BMHINTS_LEAKY_BKT_SIZE (0x1000) 70 #define UVC_H264_BMHINTS_BITRATE (0x2000) 71 #define UVC_H264_BMHINTS_ENTROPY (0x4000) 72 #define UVC_H264_BMHINTS_IFRAMEPERIOD (0x8000) 73 74 75 #define UVC_H264_QP_STEPS_I_FRAME_TYPE (0x01) 76 #define UVC_H264_QP_STEPS_P_FRAME_TYPE (0x02) 77 #define UVC_H264_QP_STEPS_B_FRAME_TYPE (0x04) 78 #define UVC_H264_QP_STEPS_ALL_FRAME_TYPES (UVC_H264_QP_STEPS_I_FRAME_TYPE | \ 79 UVC_H264_QP_STEPS_P_FRAME_TYPE | UVC_H264_QP_STEPS_B_FRAME_TYPE) 80 81 /* wSliceMode defines */ 82 83 typedef enum 84 { 85 UVC_H264_SLICEMODE_IGNORED = 0x0000, 86 UVC_H264_SLICEMODE_BITSPERSLICE = 0x0001, 87 UVC_H264_SLICEMODE_MBSPERSLICE = 0x0002, 88 UVC_H264_SLICEMODE_SLICEPERFRAME = 0x0003 89 } UvcH264SliceMode; 90 91 #define UVC_H264_SLICEMODE_TYPE (uvc_h264_slicemode_get_type()) 92 93 GType uvc_h264_slicemode_get_type (void); 94 95 /* bUsageType defines */ 96 97 typedef enum { 98 UVC_H264_USAGETYPE_REALTIME = 0x01, 99 UVC_H264_USAGETYPE_BROADCAST = 0x02, 100 UVC_H264_USAGETYPE_STORAGE = 0x03, 101 UVC_H264_USAGETYPE_UCCONFIG_0 = 0x04, 102 UVC_H264_USAGETYPE_UCCONFIG_1 = 0x05, 103 UVC_H264_USAGETYPE_UCCONFIG_2Q = 0x06, 104 UVC_H264_USAGETYPE_UCCONFIG_2S = 0x07, 105 UVC_H264_USAGETYPE_UCCONFIG_3 = 0x08, 106 } UvcH264UsageType; 107 108 #define UVC_H264_USAGETYPE_TYPE (uvc_h264_usagetype_get_type()) 109 110 GType uvc_h264_usagetype_get_type (void); 111 112 /* bRateControlMode defines */ 113 114 typedef enum { 115 UVC_H264_RATECONTROL_CBR = 0x01, 116 UVC_H264_RATECONTROL_VBR = 0x02, 117 UVC_H264_RATECONTROL_CONST_QP = 0x03, 118 } UvcH264RateControl; 119 120 #define UVC_H264_RATECONTROL_FIXED_FRM_FLG (0x10) 121 122 #define UVC_H264_RATECONTROL_TYPE (uvc_h264_ratecontrol_get_type()) 123 124 GType uvc_h264_ratecontrol_get_type (void); 125 126 /* bStreamFormat defines */ 127 128 typedef enum { 129 UVC_H264_STREAMFORMAT_ANNEXB = 0x00, 130 UVC_H264_STREAMFORMAT_NAL = 0x01, 131 } UvcH264StreamFormat; 132 133 #define UVC_H264_STREAMFORMAT_TYPE (uvc_h264_streamformat_get_type()) 134 135 GType uvc_h264_streamformat_get_type (void); 136 137 /* bEntropyCABAC defines */ 138 139 typedef enum { 140 UVC_H264_ENTROPY_CAVLC = 0x00, 141 UVC_H264_ENTROPY_CABAC = 0x01, 142 } UvcH264Entropy; 143 144 #define UVC_H264_ENTROPY_TYPE (uvc_h264_entropy_get_type()) 145 146 GType uvc_h264_entropy_get_type (void); 147 148 /* bProfile defines */ 149 #define UVC_H264_PROFILE_CONSTRAINED_BASELINE 0x4240 150 #define UVC_H264_PROFILE_BASELINE 0x4200 151 #define UVC_H264_PROFILE_MAIN 0x4D00 152 #define UVC_H264_PROFILE_HIGH 0x6400 153 154 /* bTimingstamp defines */ 155 156 #define UVC_H264_TIMESTAMP_SEI_DISABLE (0x00) 157 #define UVC_H264_TIMESTAMP_SEI_ENABLE (0x01) 158 159 /* bPreviewFlipped defines */ 160 161 #define UVC_H264_PREFLIPPED_DISABLE (0x00) 162 #define UVC_H264_PREFLIPPED_HORIZONTAL (0x01) 163 164 /* wPicType defines */ 165 #define UVC_H264_PICTYPE_I_FRAME (0x00) 166 #define UVC_H264_PICTYPE_IDR (0x01) 167 #define UVC_H264_PICTYPE_IDR_WITH_PPS_SPS (0x02) 168 169 170 /* wLayerID Macro */ 171 172 /* wLayerID 173 |------------+------------+------------+----------------+------------| 174 | Reserved | StreamID | QualityID | DependencyID | TemporalID | 175 | (3 bits) | (3 bits) | (3 bits) | (4 bits) | (3 bits) | 176 |------------+------------+------------+----------------+------------| 177 |15 13|12 10|9 7|6 3|2 0| 178 |------------+------------+------------+----------------+------------| 179 */ 180 181 #define xLayerID(stream_id, quality_id, dependency_id, temporal_id) \ 182 ((((stream_id) & 7) << 10) | \ 183 (((quality_id) & 7) << 7) | \ 184 (((dependency_id) & 15) << 3) | \ 185 ((temporal_id) & 7)) 186 187 /* id extraction from wLayerID */ 188 189 #define xStream_id(layer_id) (((layer_id) >> 10) & 7) 190 #define xQuality_id(layer_id) (((layer_id) >> 7) & 7) 191 #define xDependency_id(layer_id) (((layer_id) >> 3) & 15) 192 #define xTemporal_id(layer_id) ((layer_id)&7) 193 194 /* UVC H.264 control selectors */ 195 196 typedef enum _uvcx_control_selector_t 197 { 198 UVCX_VIDEO_CONFIG_PROBE = 0x01, 199 UVCX_VIDEO_CONFIG_COMMIT = 0x02, 200 UVCX_RATE_CONTROL_MODE = 0x03, 201 UVCX_TEMPORAL_SCALE_MODE = 0x04, 202 UVCX_SPATIAL_SCALE_MODE = 0x05, 203 UVCX_SNR_SCALE_MODE = 0x06, 204 UVCX_LTR_BUFFER_SIZE_CONTROL = 0x07, 205 UVCX_LTR_PICTURE_CONTROL = 0x08, 206 UVCX_PICTURE_TYPE_CONTROL = 0x09, 207 UVCX_VERSION = 0x0A, 208 UVCX_ENCODER_RESET = 0x0B, 209 UVCX_FRAMERATE_CONFIG = 0x0C, 210 UVCX_VIDEO_ADVANCE_CONFIG = 0x0D, 211 UVCX_BITRATE_LAYERS = 0x0E, 212 UVCX_QP_STEPS_LAYERS = 0x0F, 213 } uvcx_control_selector_t; 214 215 216 typedef struct _uvcx_video_config_probe_commit_t 217 { 218 guint32 dwFrameInterval; 219 guint32 dwBitRate; 220 guint16 bmHints; 221 guint16 wConfigurationIndex; 222 guint16 wWidth; 223 guint16 wHeight; 224 guint16 wSliceUnits; 225 guint16 wSliceMode; 226 guint16 wProfile; 227 guint16 wIFramePeriod; 228 guint16 wEstimatedVideoDelay; 229 guint16 wEstimatedMaxConfigDelay; 230 guint8 bUsageType; 231 guint8 bRateControlMode; 232 guint8 bTemporalScaleMode; 233 guint8 bSpatialScaleMode; 234 guint8 bSNRScaleMode; 235 guint8 bStreamMuxOption; 236 guint8 bStreamFormat; 237 guint8 bEntropyCABAC; 238 guint8 bTimestamp; 239 guint8 bNumOfReorderFrames; 240 guint8 bPreviewFlipped; 241 guint8 bView; 242 guint8 bReserved1; 243 guint8 bReserved2; 244 guint8 bStreamID; 245 guint8 bSpatialLayerRatio; 246 guint16 wLeakyBucketSize; 247 } __attribute__((packed)) uvcx_video_config_probe_commit_t; 248 249 250 typedef struct _uvcx_rate_control_mode_t 251 { 252 guint16 wLayerID; 253 guint8 bRateControlMode; 254 } __attribute__((packed)) uvcx_rate_control_mode_t; 255 256 257 typedef struct _uvcx_temporal_scale_mode_t 258 { 259 guint16 wLayerID; 260 guint8 bTemporalScaleMode; 261 } __attribute__((packed)) uvcx_temporal_scale_mode_t; 262 263 264 typedef struct _uvcx_spatial_scale_mode_t 265 { 266 guint16 wLayerID; 267 guint8 bSpatialScaleMode; 268 } __attribute__((packed)) uvcx_spatial_scale_mode_t; 269 270 271 typedef struct _uvcx_snr_scale_mode_t 272 { 273 guint16 wLayerID; 274 guint8 bSNRScaleMode; 275 guint8 bMGSSublayerMode; 276 } __attribute__((packed)) uvcx_snr_scale_mode_t; 277 278 279 typedef struct _uvcx_ltr_buffer_size_control_t 280 { 281 guint16 wLayerID; 282 guint8 bLTRBufferSize; 283 guint8 bLTREncoderControl; 284 } __attribute__((packed)) uvcx_ltr_buffer_size_control_t; 285 286 typedef struct _uvcx_ltr_picture_control 287 { 288 guint16 wLayerID; 289 guint8 bPutAtPositionInLTRBuffer; 290 guint8 bEncodeUsingLTR; 291 } __attribute__((packed)) uvcx_ltr_picture_control; 292 293 294 typedef struct _uvcx_picture_type_control_t 295 { 296 guint16 wLayerID; 297 guint16 wPicType; 298 } __attribute__((packed)) uvcx_picture_type_control_t; 299 300 301 typedef struct _uvcx_version_t 302 { 303 guint16 wVersion; 304 } __attribute__((packed)) uvcx_version_t; 305 306 307 typedef struct _uvcx_encoder_reset 308 { 309 guint16 wLayerID; 310 } __attribute__((packed)) uvcx_encoder_reset; 311 312 313 typedef struct _uvcx_framerate_config_t 314 { 315 guint16 wLayerID; 316 guint32 dwFrameInterval; 317 } __attribute__((packed)) uvcx_framerate_config_t; 318 319 320 typedef struct _uvcx_video_advance_config_t 321 { 322 guint16 wLayerID; 323 guint32 dwMb_max; 324 guint8 blevel_idc; 325 guint8 bReserved; 326 } __attribute__((packed)) uvcx_video_advance_config_t; 327 328 329 typedef struct _uvcx_bitrate_layers_t 330 { 331 guint16 wLayerID; 332 guint32 dwPeakBitrate; 333 guint32 dwAverageBitrate; 334 } __attribute__((packed)) uvcx_bitrate_layers_t; 335 336 337 typedef struct _uvcx_qp_steps_layers_t 338 { 339 guint16 wLayerID; 340 guint8 bFrameType; 341 guint8 bMinQp; 342 guint8 bMaxQp; 343 } __attribute__((packed)) uvcx_qp_steps_layers_t; 344 345 typedef struct 346 { 347 int8_t bLength; 348 int8_t bDescriptorType; 349 int8_t bDescriptorSubType; 350 int8_t bUnitID; 351 uint8_t guidExtensionCode[16]; 352 } __attribute__ ((__packed__)) xu_descriptor; 353 354 #define GUID_FORMAT "02X%02X%02X%02X-%02X%02X%02X%02X-"\ 355 "%02X%02X%02X%02X-%02X%02X%02X%02X" 356 #define GUID_ARGS(guid) guid[0], guid[1], guid[2], guid[3], \ 357 guid[4], guid[5], guid[6], guid[7], \ 358 guid[8], guid[9], guid[10], guid[11], \ 359 guid[12], guid[13], guid[14], guid[15] 360 361 guint8 xu_get_id (GstObject *self, const gchar *devname, libusb_context **usb_ctx); 362 363 #ifdef _WIN32 364 // GUID of the UVC H.264 extension unit: {A29E7641-DE04-47E3-8B2B-F4341AFF003B} 365 DEFINE_GUID(GUID_UVCX_H264_XU, 0xA29E7641, 0xDE04, 0x47E3, 0x8B, 0x2B, 0xF4, 0x34, 0x1A, 0xFF, 0x00, 0x3B); 366 #else 367 #define GUID_UVCX_H264_XU \ 368 {0x41, 0x76, 0x9e, 0xa2, 0x04, 0xde, 0xe3, 0x47, 0x8b, 0x2b, 0xF4, 0x34, 0x1A, 0xFF, 0x00, 0x3B} 369 #endif 370 371 #endif /*_UVC_H264_H_*/ 372