uvc-parser.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #include "uvc-parser.h"
5 
6 //#define UVC_AE_MODE_D0_MANUAL ( 1 << 0 )
7 //#define UVC_AE_MODE_D1_AUTO ( 1 << 1 )
8 //#define UVC_AE_MODE_D2_SP ( 1 << 2 )
9 //#define UVC_AE_MODE_D3_AP ( 1 << 3 )
10 //
11 //#define SWAP_UINT32(x) (((x) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | ((x) << 24))
12 
13 // Data structures for Backend-Frontend queue:
14 struct frame;
15 // We keep no more then 2 frames in between frontend and backend
17 
18 namespace librealsense
19 {
20  namespace platform
21  {
22 
24  _usb_device(usb_device), _info(info)
25  {
27  }
28 
30  {
31 
32  }
33 
34  void uvc_parser::parse_video_stream_input_header(const std::vector<uint8_t>& block)
35  {
36  _endpoint_address = block[6] & 0x8f;
37  _terminal_link = block[8];
38  }
39 
41  {
42  format.bDescriptorSubtype = (uvc_vs_desc_subtype) block[2];
43  format.bFormatIndex = block[3];
44  //format->bmCapabilities = block[4];
45  //format->bmFlags = block[5];
46  memcpy(format.guidFormat, &block[5], 16);
47  format.bBitsPerPixel = block[21];
48  format.bDefaultFrameIndex = block[22];
49  format.bAspectRatioX = block[23];
50  format.bAspectRatioY = block[24];
51  format.bmInterlaceFlags = block[25];
52  format.bCopyProtect = block[26];
53  }
54 
55  void uvc_parser::parse_video_stream_format_mjpeg(const std::vector<uint8_t>& block, uvc_format_desc_t& format)
56  {
57  format.bDescriptorSubtype = (uvc_vs_desc_subtype) block[2];
58  format.bFormatIndex = block[3];
59  memcpy(format.fourccFormat, "MJPG", 4);
60  format.bmFlags = block[5];
61  format.bBitsPerPixel = 0;
62  format.bDefaultFrameIndex = block[6];
63  format.bAspectRatioX = block[7];
64  format.bAspectRatioY = block[8];
65  format.bmInterlaceFlags = block[9];
66  format.bCopyProtect = block[10];
67  }
68 
69  void uvc_parser::parse_video_stream_frame_format(const std::vector<uint8_t>& block, uvc_format_desc_t& format)
70  {
71  format.bDescriptorSubtype = (uvc_vs_desc_subtype) block[2];
72  format.bFormatIndex = block[3];
73  format.bNumFrameDescriptors = block[4];
74  memcpy(format.guidFormat, &block[5], 16);
75  format.bBitsPerPixel = block[21];
76  format.bDefaultFrameIndex = block[22];
77  format.bAspectRatioX = block[23];
78  format.bAspectRatioY = block[24];
79  format.bmInterlaceFlags = block[25];
80  format.bCopyProtect = block[26];
81  format.bVariableSize = block[27];
82  }
83 
85  {
87 
88  frame.bDescriptorSubtype = (uvc_vs_desc_subtype) block[2];
89  frame.bFrameIndex = block[3];
90  frame.bmCapabilities = block[4];
91  frame.wWidth = block[5] + (block[6] << 8);
92  frame.wHeight = block[7] + (block[8] << 8);
93  frame.dwMinBitRate = as<int>(block, 9);
94  frame.dwMaxBitRate = as<int>(block, 13);
95  frame.dwMaxVideoFrameBufferSize = as<int>(block, 17);
96  frame.dwDefaultFrameInterval = as<int>(block, 21);
97  frame.bFrameIntervalType = block[25];
98 
99  if (block[25] == 0) {
100  frame.dwMinFrameInterval = as<int>(block, 26);
101  frame.dwMaxFrameInterval = as<int>(block, 30);
102  frame.dwFrameIntervalStep = as<int>(block, 34);
103  } else {
104  frame.intervals = std::vector<uint32_t>((block[25] + 1));
105 
106  for (int i = 0; i < block[25]; ++i)
107  {
108  frame.intervals[i] = as<int>(block, 26 + (i * 4));
109  }
110 // frame.intervals[block[25]] = 0;
111  }
112 
113 
114  format.frame_descs.push_back(frame);
115  }
116 
117  void uvc_parser::parse_video_stream_frame_frame(const std::vector<uint8_t>& block, uvc_format_desc_t& format)
118  {
120 
121  frame.bDescriptorSubtype = (uvc_vs_desc_subtype) block[2];
122  frame.bFrameIndex = block[3];
123  frame.bmCapabilities = block[4];
124  frame.wWidth = block[5] + (block[6] << 8);
125  frame.wHeight = block[7] + (block[8] << 8);
126  frame.dwMinBitRate = as<int>(block, 9);
127  frame.dwMaxBitRate = as<int>(block, 13);
128  frame.dwDefaultFrameInterval = as<int>(block, 17);
129  frame.bFrameIntervalType = block[21];
130  frame.dwBytesPerLine = as<int>(block, 22);
131 
132  if (block[21] == 0) {
133  frame.dwMinFrameInterval = as<int>(block, 26);
134  frame.dwMaxFrameInterval = as<int>(block, 30);
135  frame.dwFrameIntervalStep = as<int>(block, 34);
136  } else {
137  frame.intervals = std::vector<uint32_t>((block[21] + 1));
138  for (int i = 0; i < block[21]; ++i)
139  {
140  frame.intervals[i] = as<int>(block, 26 + (i * 4));
141  }
142 // frame.intervals[block[21]] = 0;
143  }
144  format.frame_descs.push_back(frame);
145  }
146 
147  void uvc_parser::parse_video_stream_format(int interface_number, int index)
148  {
149  auto descs = _usb_device->get_descriptors();
150  auto block = descs[index].data;
152  auto descriptor_subtype = block[2];
153 
154  switch (descriptor_subtype) {
157  _formats[interface_number].push_back(format);
158  break;
159  case UVC_VS_FORMAT_MJPEG:
161  _formats[interface_number].push_back(format);
162  break;
165  _formats[interface_number].push_back(format);
166  break;
167  case UVC_VS_COLORFORMAT:
168  break;
169 
170  default:
171  break;
172  }
173  }
174 
175  void uvc_parser::parse_video_stream_frame(int interface_number, int index)
176  {
177  auto descs = _usb_device->get_descriptors();
178  auto block = descs[index].data;
179  auto descriptor_subtype = block[2];
180  auto& format = _formats.at(interface_number).back();
181 
182  switch (descriptor_subtype) {
184  case UVC_VS_FRAME_MJPEG:
186  break;
189  break;
190 
191  default:
192  break;
193  }
194  }
195 
196  void uvc_parser::scan_streaming(int interface_number)
197  {
198  auto descs = _usb_device->get_descriptors();
199  for(int i = 0; i < descs.size(); i++)
200  {
201  auto d = descs[i];
202  if(d.data[1] != USB_DT_INTERFACE)
203  continue;
204  if(d.data[2] != interface_number)
205  continue;
206 
207  for(i++ ; i < descs.size(); i++)
208  {
209  d = descs[i];
210  if(d.data[1] == USB_DT_INTERFACE)
211  break;
212  auto descriptor_subtype = d.data[2];
213 
214  switch (descriptor_subtype) {
215  case UVC_VS_INPUT_HEADER:
217  break;
219  case UVC_VS_FORMAT_MJPEG:
221  parse_video_stream_format(interface_number, i);
222  break;
224  case UVC_VS_FRAME_MJPEG:
226  parse_video_stream_frame(interface_number, i);
227  break;
228  case UVC_VS_COLORFORMAT:
229  break;
230 
231  default:
233  break;
234  }
235 
236 // parse_video_stream(interface_number, i);
237  }
238  break;
239  }
240  }
241 
242  void uvc_parser::parse_video_control_header(const std::vector<uint8_t>& block)
243  {
244  _bcd_uvc = as<uint16_t>(block, 3);
245 
246  switch (_bcd_uvc) {
247  case 0x0100:
248  case 0x010a:
249  _clock_frequency = as<int>(block, 7);
250  break;
251  case 0x0110:
252  case 0x0150:
253  _clock_frequency = 0;
254  break;
255  default:
256  return throw std::runtime_error("parse_video_control_header failed to parse bcdUVC");
257  }
258 
259  for (int j = 12; j < block.size(); ++j) {
260  auto intf = block[j];
261  scan_streaming(intf);
262  }
263  }
264 
265  void uvc_parser::parse_video_control_input_terminal(const std::vector<uint8_t>& block)
266  {
267  /* only supporting camera-type input terminals */
268  if (as<uint16_t>(block, 4) != UVC_ITT_CAMERA) {
269  return;
270  }
271 
272  _input_terminal.bTerminalID = block[3];
273  _input_terminal.wTerminalType = (uvc_it_type) as<uint16_t>(block, 4);
274  _input_terminal.wObjectiveFocalLengthMin = as<uint16_t>(block, 8);
275  _input_terminal.wObjectiveFocalLengthMax = as<uint16_t>(block, 10);
276  _input_terminal.wOcularFocalLength = as<uint16_t>(block, 12);
277 
278  for (size_t i = 14 + block[14]; i >= 15; --i)
280  }
281 
282  void uvc_parser::parse_video_control_selector_unit(const std::vector<uint8_t>& block)
283  {
284  _selector_unit.bUnitID = block[3];
285  }
286 
287  void uvc_parser::parse_video_control_processing_unit(const std::vector<uint8_t>& block)
288  {
289  _processing_unit.bUnitID = block[3];
290  _processing_unit.bSourceID = block[4];
291 
292  for (size_t i = 7 + block[7]; i >= 8; --i)
294  }
295 
296  void uvc_parser::parse_video_control_extension_unit(const std::vector<uint8_t>& block)
297  {
298  const uint8_t *start_of_controls;
299  int size_of_controls, num_in_pins;
300 
301  _extension_unit.bUnitID = block[3];
302  memcpy(_extension_unit.guidExtensionCode, &block[4], 16);
303 
304  num_in_pins = block[21];
305  size_of_controls = block[22 + num_in_pins];
306  start_of_controls = &block[23 + num_in_pins];
307 
308  for (int i = size_of_controls - 1; i >= 0; --i)
309  _extension_unit.bmControls = start_of_controls[i] + (_extension_unit.bmControls << 8);
310  }
311 
312  void uvc_parser::parse_video_control(const std::vector<uint8_t>& block)
313  {
314  auto descriptor_subtype = block[2];
315 
316  if (block[1] != 36)
317  return;
318 
319  switch (descriptor_subtype) {
320  case UVC_VC_HEADER:
322  break;
325  break;
327  break;
330  break;
333  break;
336  break;
337  default:
338  break;
339  }
340  }
341 
342  void get_block_range(const std::vector<usb_descriptor>& descs, int mi, int& begin, int& end)
343  {
344  begin = -1;
345  end = -1;
346 
347  for (int i = 0; i < descs.size(); i++)
348  {
349  auto d = descs[i];
350  if(d.data[1] != USB_DT_INTERFACE)
351  continue;
352  if (d.data[2] == mi)
353  {
354  begin = i;
355  continue;
356  }
357  if (begin != -1)
358  {
359  end = i;
360  return;
361  }
362  }
363  }
364 
365  void uvc_parser::scan_control(int interface_number)
366  {
367  auto descs = _usb_device->get_descriptors();
368 
369  int begin, end;
370  get_block_range(descs, _info.mi, begin, end);
371  for(int i = begin; i < end; i++)
372  {
373  auto d = descs[i];
374  parse_video_control(d.data);
375  }
376  }
377  }
378 }
uint16_t wOcularFocalLength
Definition: uvc-types.h:269
uint16_t wObjectiveFocalLengthMin
Definition: uvc-types.h:267
GLuint GLuint end
enum uvc_vs_desc_subtype bDescriptorSubtype
Definition: uvc-types.h:379
enum uvc_it_type wTerminalType
Definition: uvc-types.h:266
const uvc_device_info _info
Definition: uvc-parser.h:58
uint8_t guidExtensionCode[16]
Definition: uvc-types.h:296
void parse_video_stream_format_mjpeg(const std::vector< uint8_t > &block, uvc_format_desc_t &format)
Definition: uvc-parser.cpp:55
uvc_extension_unit_t _extension_unit
Definition: uvc-parser.h:70
uvc_vs_desc_subtype
Definition: uvc-types.h:105
void parse_video_stream_input_header(const std::vector< uint8_t > &block)
Definition: uvc-parser.cpp:34
std::map< int, std::vector< uvc_format_desc_t > > _formats
Definition: uvc-parser.h:72
#define USB_DT_INTERFACE
Definition: usb-types.h:15
librealsense::small_heap< frame, 10 > frames_archive
Definition: uvc-parser.cpp:14
void parse_video_control_extension_unit(const std::vector< uint8_t > &block)
Definition: uvc-parser.cpp:296
d
Definition: rmse.py:171
void scan_streaming(int interface_number)
Definition: uvc-parser.cpp:196
unsigned char uint8_t
Definition: stdint.h:78
uvc_input_terminal_t _input_terminal
Definition: uvc-parser.h:67
uvc_processing_unit_t _processing_unit
Definition: uvc-parser.h:69
uint16_t wObjectiveFocalLengthMax
Definition: uvc-types.h:268
GLuint index
uvc_selector_unit_t _selector_unit
Definition: uvc-parser.h:68
def info(name, value, persistent=False)
Definition: test.py:301
not_this_one begin(...)
uint8_t bBitsPerPixel
Definition: uvc-types.h:391
uint8_t bFormatIndex
Definition: uvc-types.h:381
uint8_t bDefaultFrameIndex
Definition: uvc-types.h:396
void parse_video_control_processing_unit(const std::vector< uint8_t > &block)
Definition: uvc-parser.cpp:287
GLint GLint GLsizei GLint GLenum format
uint8_t bAspectRatioY
Definition: uvc-types.h:398
void parse_video_control(const std::vector< uint8_t > &block)
Definition: uvc-parser.cpp:312
GLint j
uint8_t bCopyProtect
Definition: uvc-types.h:400
uint8_t guidFormat[16]
Definition: uvc-types.h:385
void parse_video_control_input_terminal(const std::vector< uint8_t > &block)
Definition: uvc-parser.cpp:265
uint8_t fourccFormat[4]
Definition: uvc-types.h:386
void parse_video_control_selector_unit(const std::vector< uint8_t > &block)
Definition: uvc-parser.cpp:282
uint64_t bmControls
Definition: uvc-types.h:298
void parse_video_stream_frame_format(const std::vector< uint8_t > &block, uvc_format_desc_t &format)
Definition: uvc-parser.cpp:69
uint64_t bmControls
Definition: uvc-types.h:282
void parse_video_stream_frame_frame(const std::vector< uint8_t > &block, uvc_format_desc_t &format)
Definition: uvc-parser.cpp:117
void parse_video_stream_format(int interface_number, int index)
Definition: uvc-parser.cpp:147
uint8_t bAspectRatioX
Definition: uvc-types.h:397
void parse_video_stream_frame_uncompressed(const std::vector< uint8_t > &block, uvc_format_desc_t &format)
Definition: uvc-parser.cpp:84
void get_block_range(const std::vector< usb_descriptor > &descs, int mi, int &begin, int &end)
Definition: uvc-parser.cpp:342
uvc_it_type
Definition: uvc-types.h:200
uint64_t bmControls
Definition: uvc-types.h:271
uint8_t bNumFrameDescriptors
Definition: uvc-types.h:382
uint8_t bmFlags
Definition: uvc-types.h:393
void parse_video_stream_format_uncompressed(const std::vector< uint8_t > &block, uvc_format_desc_t &format)
Definition: uvc-parser.cpp:40
int i
uint8_t bmInterlaceFlags
Definition: uvc-types.h:399
void scan_control(int interface_number)
Definition: uvc-parser.cpp:365
void parse_video_stream_frame(int interface_number, int index)
Definition: uvc-parser.cpp:175
uint8_t bVariableSize
Definition: uvc-types.h:401
std::vector< uvc_frame_desc_t > frame_descs
Definition: uvc-types.h:403
std::shared_ptr< usb_device > rs_usb_device
Definition: usb-device.h:29
void parse_video_control_header(const std::vector< uint8_t > &block)
Definition: uvc-parser.cpp:242
uvc_parser(const rs_usb_device &usb_device, const uvc_device_info &info)
Definition: uvc-parser.cpp:23
uint8_t bTerminalID
Definition: uvc-types.h:264


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:13