camera.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. #include "camera.h"
  2. #include <assert.h>
  3. #include <errno.h>
  4. #include <glib.h>
  5. #include <stdio.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/mman.h>
  8. #include <unistd.h>
  9. #define MAX_VIDEO_BUFFERS 20
  10. static const char *pixel_format_names[MP_PIXEL_FMT_MAX] = {
  11. "unsupported", "BGGR8", "GBRG8", "GRBG8", "RGGB8", "BGGR10P",
  12. "GBRG10P", "GRBG10P", "RGGB10P", "UYVY", "YUYV",
  13. };
  14. const char *
  15. mp_pixel_format_to_str(uint32_t pixel_format)
  16. {
  17. g_return_val_if_fail(pixel_format < MP_PIXEL_FMT_MAX, "INVALID");
  18. return pixel_format_names[pixel_format];
  19. }
  20. MPPixelFormat
  21. mp_pixel_format_from_str(const char *name)
  22. {
  23. for (MPPixelFormat i = 0; i < MP_PIXEL_FMT_MAX; ++i) {
  24. if (strcasecmp(pixel_format_names[i], name) == 0) {
  25. return i;
  26. }
  27. }
  28. g_return_val_if_reached(MP_PIXEL_FMT_UNSUPPORTED);
  29. }
  30. static const uint32_t pixel_format_v4l_pixel_formats[MP_PIXEL_FMT_MAX] = {
  31. 0,
  32. V4L2_PIX_FMT_SBGGR8,
  33. V4L2_PIX_FMT_SGBRG8,
  34. V4L2_PIX_FMT_SGRBG8,
  35. V4L2_PIX_FMT_SRGGB8,
  36. V4L2_PIX_FMT_SBGGR10P,
  37. V4L2_PIX_FMT_SGBRG10P,
  38. V4L2_PIX_FMT_SGRBG10P,
  39. V4L2_PIX_FMT_SRGGB10P,
  40. V4L2_PIX_FMT_UYVY,
  41. V4L2_PIX_FMT_YUYV,
  42. };
  43. uint32_t
  44. mp_pixel_format_to_v4l_pixel_format(MPPixelFormat pixel_format)
  45. {
  46. g_return_val_if_fail(pixel_format < MP_PIXEL_FMT_MAX, 0);
  47. return pixel_format_v4l_pixel_formats[pixel_format];
  48. }
  49. MPPixelFormat
  50. mp_pixel_format_from_v4l_pixel_format(uint32_t v4l_pixel_format)
  51. {
  52. for (MPPixelFormat i = 0; i < MP_PIXEL_FMT_MAX; ++i) {
  53. if (pixel_format_v4l_pixel_formats[i] == v4l_pixel_format) {
  54. return i;
  55. }
  56. }
  57. return MP_PIXEL_FMT_UNSUPPORTED;
  58. }
  59. static const uint32_t pixel_format_v4l_bus_codes[MP_PIXEL_FMT_MAX] = {
  60. 0,
  61. MEDIA_BUS_FMT_SBGGR8_1X8,
  62. MEDIA_BUS_FMT_SGBRG8_1X8,
  63. MEDIA_BUS_FMT_SGRBG8_1X8,
  64. MEDIA_BUS_FMT_SRGGB8_1X8,
  65. MEDIA_BUS_FMT_SBGGR10_1X10,
  66. MEDIA_BUS_FMT_SGBRG10_1X10,
  67. MEDIA_BUS_FMT_SGRBG10_1X10,
  68. MEDIA_BUS_FMT_SRGGB10_1X10,
  69. MEDIA_BUS_FMT_UYVY8_2X8,
  70. MEDIA_BUS_FMT_YUYV8_2X8,
  71. };
  72. uint32_t
  73. mp_pixel_format_to_v4l_bus_code(MPPixelFormat pixel_format)
  74. {
  75. g_return_val_if_fail(pixel_format < MP_PIXEL_FMT_MAX, 0);
  76. return pixel_format_v4l_bus_codes[pixel_format];
  77. }
  78. MPPixelFormat
  79. mp_pixel_format_from_v4l_bus_code(uint32_t v4l_bus_code)
  80. {
  81. for (MPPixelFormat i = 0; i < MP_PIXEL_FMT_MAX; ++i) {
  82. if (pixel_format_v4l_bus_codes[i] == v4l_bus_code) {
  83. return i;
  84. }
  85. }
  86. return MP_PIXEL_FMT_UNSUPPORTED;
  87. }
  88. uint32_t
  89. mp_pixel_format_bits_per_pixel(MPPixelFormat pixel_format)
  90. {
  91. g_return_val_if_fail(pixel_format < MP_PIXEL_FMT_MAX, 0);
  92. switch (pixel_format) {
  93. case MP_PIXEL_FMT_BGGR8:
  94. case MP_PIXEL_FMT_GBRG8:
  95. case MP_PIXEL_FMT_GRBG8:
  96. case MP_PIXEL_FMT_RGGB8:
  97. return 8;
  98. case MP_PIXEL_FMT_BGGR10P:
  99. case MP_PIXEL_FMT_GBRG10P:
  100. case MP_PIXEL_FMT_GRBG10P:
  101. case MP_PIXEL_FMT_RGGB10P:
  102. return 10;
  103. case MP_PIXEL_FMT_UYVY:
  104. case MP_PIXEL_FMT_YUYV:
  105. return 16;
  106. default:
  107. return 0;
  108. }
  109. }
  110. uint32_t
  111. mp_pixel_format_width_to_bytes(MPPixelFormat pixel_format, uint32_t width)
  112. {
  113. uint32_t bits_per_pixel = mp_pixel_format_bits_per_pixel(pixel_format);
  114. uint64_t bits_per_width = width * (uint64_t)bits_per_pixel;
  115. uint64_t remainder = bits_per_width % 8;
  116. if (remainder == 0)
  117. return bits_per_width / 8;
  118. return (bits_per_width + 8 - remainder) / 8;
  119. }
  120. uint32_t
  121. mp_pixel_format_width_to_colors(MPPixelFormat pixel_format, uint32_t width)
  122. {
  123. g_return_val_if_fail(pixel_format < MP_PIXEL_FMT_MAX, 0);
  124. switch (pixel_format) {
  125. case MP_PIXEL_FMT_BGGR8:
  126. case MP_PIXEL_FMT_GBRG8:
  127. case MP_PIXEL_FMT_GRBG8:
  128. case MP_PIXEL_FMT_RGGB8:
  129. return width / 2;
  130. case MP_PIXEL_FMT_BGGR10P:
  131. case MP_PIXEL_FMT_GBRG10P:
  132. case MP_PIXEL_FMT_GRBG10P:
  133. case MP_PIXEL_FMT_RGGB10P:
  134. return width / 2 * 5;
  135. case MP_PIXEL_FMT_UYVY:
  136. case MP_PIXEL_FMT_YUYV:
  137. return width;
  138. default:
  139. return 0;
  140. }
  141. }
  142. uint32_t
  143. mp_pixel_format_height_to_colors(MPPixelFormat pixel_format, uint32_t height)
  144. {
  145. g_return_val_if_fail(pixel_format < MP_PIXEL_FMT_MAX, 0);
  146. switch (pixel_format) {
  147. case MP_PIXEL_FMT_BGGR8:
  148. case MP_PIXEL_FMT_GBRG8:
  149. case MP_PIXEL_FMT_GRBG8:
  150. case MP_PIXEL_FMT_RGGB8:
  151. case MP_PIXEL_FMT_BGGR10P:
  152. case MP_PIXEL_FMT_GBRG10P:
  153. case MP_PIXEL_FMT_GRBG10P:
  154. case MP_PIXEL_FMT_RGGB10P:
  155. return height / 2;
  156. case MP_PIXEL_FMT_UYVY:
  157. case MP_PIXEL_FMT_YUYV:
  158. return height;
  159. default:
  160. return 0;
  161. }
  162. }
  163. bool
  164. mp_camera_mode_is_equivalent(const MPCameraMode *m1, const MPCameraMode *m2)
  165. {
  166. return m1->pixel_format == m2->pixel_format &&
  167. m1->frame_interval.numerator == m2->frame_interval.numerator &&
  168. m1->frame_interval.denominator == m2->frame_interval.denominator &&
  169. m1->width == m2->width && m1->height == m2->height;
  170. }
  171. static void
  172. errno_printerr(const char *s)
  173. {
  174. g_printerr("MPCamera: %s error %d, %s\n", s, errno, strerror(errno));
  175. }
  176. static int
  177. xioctl(int fd, int request, void *arg)
  178. {
  179. int r;
  180. do {
  181. r = ioctl(fd, request, arg);
  182. } while (r == -1 && errno == EINTR);
  183. return r;
  184. }
  185. struct video_buffer {
  186. uint32_t length;
  187. uint8_t *data;
  188. int fd;
  189. };
  190. struct _MPCamera {
  191. int video_fd;
  192. int subdev_fd;
  193. bool has_set_mode;
  194. MPCameraMode current_mode;
  195. struct video_buffer buffers[MAX_VIDEO_BUFFERS];
  196. uint32_t num_buffers;
  197. bool use_mplane;
  198. };
  199. MPCamera *
  200. mp_camera_new(int video_fd, int subdev_fd)
  201. {
  202. g_return_val_if_fail(video_fd != -1, NULL);
  203. // Query capabilities
  204. struct v4l2_capability cap;
  205. if (xioctl(video_fd, VIDIOC_QUERYCAP, &cap) == -1) {
  206. return NULL;
  207. }
  208. // Check whether this is a video capture device
  209. bool use_mplane;
  210. if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
  211. use_mplane = true;
  212. printf("!!\n");
  213. } else if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
  214. use_mplane = false;
  215. } else {
  216. return NULL;
  217. }
  218. MPCamera *camera = malloc(sizeof(MPCamera));
  219. camera->video_fd = video_fd;
  220. camera->subdev_fd = subdev_fd;
  221. camera->has_set_mode = false;
  222. camera->num_buffers = 0;
  223. camera->use_mplane = use_mplane;
  224. return camera;
  225. }
  226. void
  227. mp_camera_free(MPCamera *camera)
  228. {
  229. g_warn_if_fail(camera->num_buffers == 0);
  230. if (camera->num_buffers != 0) {
  231. mp_camera_stop_capture(camera);
  232. }
  233. free(camera);
  234. }
  235. bool
  236. mp_camera_is_subdev(MPCamera *camera)
  237. {
  238. return camera->subdev_fd != -1;
  239. }
  240. int
  241. mp_camera_get_video_fd(MPCamera *camera)
  242. {
  243. return camera->video_fd;
  244. }
  245. int
  246. mp_camera_get_subdev_fd(MPCamera *camera)
  247. {
  248. return camera->subdev_fd;
  249. }
  250. static bool
  251. camera_mode_impl(MPCamera *camera, int request, MPCameraMode *mode)
  252. {
  253. uint32_t pixfmt = mp_pixel_format_from_v4l_pixel_format(mode->pixel_format);
  254. struct v4l2_format fmt = {};
  255. if (camera->use_mplane) {
  256. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  257. fmt.fmt.pix_mp.width = mode->width;
  258. fmt.fmt.pix_mp.height = mode->height;
  259. fmt.fmt.pix_mp.pixelformat = pixfmt;
  260. fmt.fmt.pix_mp.field = V4L2_FIELD_ANY;
  261. } else {
  262. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  263. fmt.fmt.pix.width = mode->width;
  264. fmt.fmt.pix.height = mode->height;
  265. fmt.fmt.pix.pixelformat = pixfmt;
  266. fmt.fmt.pix.field = V4L2_FIELD_ANY;
  267. }
  268. if (xioctl(camera->video_fd, request, &fmt) == -1) {
  269. return false;
  270. }
  271. if (camera->use_mplane) {
  272. mode->width = fmt.fmt.pix_mp.width;
  273. mode->height = fmt.fmt.pix_mp.height;
  274. mode->pixel_format = mp_pixel_format_from_v4l_pixel_format(
  275. fmt.fmt.pix_mp.pixelformat);
  276. } else {
  277. mode->width = fmt.fmt.pix.width;
  278. mode->height = fmt.fmt.pix.height;
  279. mode->pixel_format = mp_pixel_format_from_v4l_pixel_format(
  280. fmt.fmt.pix.pixelformat);
  281. }
  282. return true;
  283. }
  284. bool
  285. mp_camera_try_mode(MPCamera *camera, MPCameraMode *mode)
  286. {
  287. if (!camera_mode_impl(camera, VIDIOC_TRY_FMT, mode)) {
  288. errno_printerr("VIDIOC_S_FMT");
  289. return false;
  290. }
  291. return true;
  292. }
  293. const MPCameraMode *
  294. mp_camera_get_mode(const MPCamera *camera)
  295. {
  296. return &camera->current_mode;
  297. }
  298. bool
  299. mp_camera_set_mode(MPCamera *camera, MPCameraMode *mode)
  300. {
  301. // Set the mode in the subdev the camera is one
  302. if (mp_camera_is_subdev(camera)) {
  303. struct v4l2_subdev_frame_interval interval = {};
  304. interval.pad = 0;
  305. interval.interval = mode->frame_interval;
  306. if (xioctl(camera->subdev_fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL,
  307. &interval) == -1) {
  308. errno_printerr("VIDIOC_SUBDEV_S_FRAME_INTERVAL");
  309. return false;
  310. }
  311. bool did_set_frame_rate = interval.interval.numerator ==
  312. mode->frame_interval.numerator &&
  313. interval.interval.denominator ==
  314. mode->frame_interval.denominator;
  315. struct v4l2_subdev_format fmt = {};
  316. fmt.pad = 0;
  317. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  318. fmt.format.width = mode->width;
  319. fmt.format.height = mode->height;
  320. fmt.format.code =
  321. mp_pixel_format_to_v4l_bus_code(mode->pixel_format);
  322. fmt.format.field = V4L2_FIELD_ANY;
  323. if (xioctl(camera->subdev_fd, VIDIOC_SUBDEV_S_FMT, &fmt) == -1) {
  324. errno_printerr("VIDIOC_SUBDEV_S_FMT");
  325. return false;
  326. }
  327. // Some drivers like ov5640 don't allow you to set the frame format with
  328. // too high a frame-rate, but that means the frame-rate won't be set
  329. // after the format change. So we need to try again here if we didn't
  330. // succeed before. Ideally we'd be able to set both at once.
  331. if (!did_set_frame_rate) {
  332. interval.interval = mode->frame_interval;
  333. if (xioctl(camera->subdev_fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL,
  334. &interval) == -1) {
  335. errno_printerr("VIDIOC_SUBDEV_S_FRAME_INTERVAL");
  336. }
  337. }
  338. // Update the mode
  339. mode->pixel_format =
  340. mp_pixel_format_from_v4l_bus_code(fmt.format.code);
  341. mode->frame_interval = interval.interval;
  342. mode->width = fmt.format.width;
  343. mode->height = fmt.format.height;
  344. }
  345. // Set the mode for the video device
  346. {
  347. if (!camera_mode_impl(camera, VIDIOC_S_FMT, mode)) {
  348. errno_printerr("VIDIOC_S_FMT");
  349. return false;
  350. }
  351. }
  352. camera->has_set_mode = true;
  353. camera->current_mode = *mode;
  354. return true;
  355. }
  356. bool
  357. mp_camera_start_capture(MPCamera *camera)
  358. {
  359. g_return_val_if_fail(camera->has_set_mode, false);
  360. g_return_val_if_fail(camera->num_buffers == 0, false);
  361. enum v4l2_buf_type buftype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  362. if (camera->use_mplane) {
  363. buftype = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  364. }
  365. // Start by requesting buffers
  366. struct v4l2_requestbuffers req = {};
  367. req.count = MAX_VIDEO_BUFFERS;
  368. req.type = buftype;
  369. req.memory = V4L2_MEMORY_MMAP;
  370. if (xioctl(camera->video_fd, VIDIOC_REQBUFS, &req) == -1) {
  371. errno_printerr("VIDIOC_REQBUFS");
  372. return false;
  373. }
  374. if (req.count < 2) {
  375. g_printerr(
  376. "Insufficient buffer memory. Only %d buffers available.\n",
  377. req.count);
  378. goto error;
  379. }
  380. for (uint32_t i = 0; i < req.count; ++i) {
  381. // Query each buffer and mmap it
  382. struct v4l2_buffer buf = {
  383. .type = buftype,
  384. .memory = V4L2_MEMORY_MMAP,
  385. .index = i,
  386. };
  387. struct v4l2_plane planes[1];
  388. if (camera->use_mplane) {
  389. buf.m.planes = planes;
  390. buf.length = 1;
  391. }
  392. if (xioctl(camera->video_fd, VIDIOC_QUERYBUF, &buf) == -1) {
  393. errno_printerr("VIDIOC_QUERYBUF");
  394. break;
  395. }
  396. if (camera->use_mplane) {
  397. camera->buffers[i].length = planes[0].length;
  398. camera->buffers[i].data =
  399. mmap(NULL, planes[0].length, PROT_READ, MAP_SHARED,
  400. camera->video_fd, planes[0].m.mem_offset);
  401. } else {
  402. camera->buffers[i].length = buf.length;
  403. camera->buffers[i].data =
  404. mmap(NULL, buf.length, PROT_READ, MAP_SHARED,
  405. camera->video_fd, buf.m.offset);
  406. }
  407. if (camera->buffers[i].data == MAP_FAILED) {
  408. errno_printerr("mmap");
  409. break;
  410. }
  411. struct v4l2_exportbuffer expbuf = {
  412. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  413. .index = i,
  414. };
  415. if (xioctl(camera->video_fd, VIDIOC_EXPBUF, &expbuf) == -1) {
  416. errno_printerr("VIDIOC_EXPBUF");
  417. break;
  418. }
  419. camera->buffers[i].fd = expbuf.fd;
  420. ++camera->num_buffers;
  421. }
  422. if (camera->num_buffers != req.count) {
  423. g_printerr("Unable to map all buffers\n");
  424. goto error;
  425. }
  426. for (uint32_t i = 0; i < camera->num_buffers; ++i) {
  427. struct v4l2_buffer buf = {
  428. .type = buftype,
  429. .memory = V4L2_MEMORY_MMAP,
  430. .index = i,
  431. };
  432. struct v4l2_plane planes[1];
  433. if (camera->use_mplane) {
  434. buf.m.planes = planes;
  435. buf.length = 1;
  436. }
  437. // Queue the buffer for capture
  438. if (xioctl(camera->video_fd, VIDIOC_QBUF, &buf) == -1) {
  439. errno_printerr("VIDIOC_QBUF");
  440. goto error;
  441. }
  442. }
  443. // Start capture
  444. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  445. if (xioctl(camera->video_fd, VIDIOC_STREAMON, &type) == -1) {
  446. errno_printerr("VIDIOC_STREAMON");
  447. goto error;
  448. }
  449. return true;
  450. error:
  451. // Unmap any mapped buffers
  452. assert(camera->num_buffers <= MAX_VIDEO_BUFFERS);
  453. for (uint32_t i = 0; i < camera->num_buffers; ++i) {
  454. if (munmap(camera->buffers[i].data, camera->buffers[i].length) ==
  455. -1) {
  456. errno_printerr("munmap");
  457. }
  458. if (close(camera->buffers[i].fd) == -1) {
  459. errno_printerr("close");
  460. }
  461. }
  462. // Reset allocated buffers
  463. {
  464. struct v4l2_requestbuffers req = {};
  465. req.count = 0;
  466. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  467. req.memory = V4L2_MEMORY_MMAP;
  468. if (xioctl(camera->video_fd, VIDIOC_REQBUFS, &req) == -1) {
  469. errno_printerr("VIDIOC_REQBUFS");
  470. }
  471. }
  472. return false;
  473. }
  474. bool
  475. mp_camera_stop_capture(MPCamera *camera)
  476. {
  477. g_return_val_if_fail(camera->num_buffers > 0, false);
  478. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  479. if (xioctl(camera->video_fd, VIDIOC_STREAMOFF, &type) == -1) {
  480. errno_printerr("VIDIOC_STREAMOFF");
  481. }
  482. assert(camera->num_buffers <= MAX_VIDEO_BUFFERS);
  483. for (int i = 0; i < camera->num_buffers; ++i) {
  484. if (munmap(camera->buffers[i].data, camera->buffers[i].length) ==
  485. -1) {
  486. errno_printerr("munmap");
  487. }
  488. if (close(camera->buffers[i].fd) == -1) {
  489. errno_printerr("close");
  490. }
  491. }
  492. camera->num_buffers = 0;
  493. struct v4l2_requestbuffers req = {};
  494. req.count = 0;
  495. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  496. req.memory = V4L2_MEMORY_MMAP;
  497. if (xioctl(camera->video_fd, VIDIOC_REQBUFS, &req) == -1) {
  498. errno_printerr("VIDIOC_REQBUFS");
  499. }
  500. return true;
  501. }
  502. bool
  503. mp_camera_is_capturing(MPCamera *camera)
  504. {
  505. return camera->num_buffers > 0;
  506. }
  507. bool
  508. mp_camera_capture_buffer(MPCamera *camera, MPBuffer *buffer)
  509. {
  510. struct v4l2_buffer buf = {};
  511. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  512. buf.memory = V4L2_MEMORY_MMAP;
  513. struct v4l2_plane planes[1];
  514. if (camera->use_mplane) {
  515. buf.m.planes = planes;
  516. buf.length = 1;
  517. }
  518. if (xioctl(camera->video_fd, VIDIOC_DQBUF, &buf) == -1) {
  519. switch (errno) {
  520. case EAGAIN:
  521. return true;
  522. case EIO:
  523. /* Could ignore EIO, see spec. */
  524. /* fallthrough */
  525. default:
  526. errno_printerr("VIDIOC_DQBUF");
  527. exit(1);
  528. return false;
  529. }
  530. }
  531. uint32_t pixel_format = camera->current_mode.pixel_format;
  532. uint32_t width = camera->current_mode.width;
  533. uint32_t height = camera->current_mode.height;
  534. uint32_t bytesused;
  535. if (camera->use_mplane) {
  536. bytesused = planes[0].bytesused;
  537. } else {
  538. bytesused = buf.bytesused;
  539. }
  540. assert(bytesused ==
  541. mp_pixel_format_width_to_bytes(pixel_format, width) * height);
  542. assert(bytesused == camera->buffers[buf.index].length);
  543. buffer->index = buf.index;
  544. buffer->data = camera->buffers[buf.index].data;
  545. buffer->fd = camera->buffers[buf.index].fd;
  546. return true;
  547. }
  548. bool mp_camera_release_buffer(MPCamera *camera, uint32_t buffer_index)
  549. {
  550. struct v4l2_buffer buf = {};
  551. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  552. buf.memory = V4L2_MEMORY_MMAP;
  553. buf.index = buffer_index;
  554. if (xioctl(camera->video_fd, VIDIOC_QBUF, &buf) == -1) {
  555. errno_printerr("VIDIOC_QBUF");
  556. return false;
  557. }
  558. return true;
  559. }
  560. struct _MPCameraModeList {
  561. MPCameraMode mode;
  562. MPCameraModeList *next;
  563. };
  564. static MPCameraModeList *
  565. get_subdev_modes(MPCamera *camera, bool (*check)(MPCamera *, MPCameraMode *))
  566. {
  567. MPCameraModeList *item = NULL;
  568. for (uint32_t fmt_index = 0;; ++fmt_index) {
  569. struct v4l2_subdev_mbus_code_enum fmt = {};
  570. fmt.index = fmt_index;
  571. fmt.pad = 0;
  572. fmt.which = V4L2_SUBDEV_FORMAT_TRY;
  573. if (xioctl(camera->subdev_fd, VIDIOC_SUBDEV_ENUM_MBUS_CODE, &fmt) ==
  574. -1) {
  575. if (errno != EINVAL) {
  576. errno_printerr("VIDIOC_SUBDEV_ENUM_MBUS_CODE");
  577. }
  578. break;
  579. }
  580. // Skip unsupported formats
  581. uint32_t format = mp_pixel_format_from_v4l_bus_code(fmt.code);
  582. if (format == MP_PIXEL_FMT_UNSUPPORTED) {
  583. continue;
  584. }
  585. for (uint32_t frame_index = 0;; ++frame_index) {
  586. struct v4l2_subdev_frame_size_enum frame = {};
  587. frame.index = frame_index;
  588. frame.pad = 0;
  589. frame.code = fmt.code;
  590. frame.which = V4L2_SUBDEV_FORMAT_TRY;
  591. if (xioctl(camera->subdev_fd, VIDIOC_SUBDEV_ENUM_FRAME_SIZE,
  592. &frame) == -1) {
  593. if (errno != EINVAL) {
  594. errno_printerr(
  595. "VIDIOC_SUBDEV_ENUM_FRAME_SIZE");
  596. }
  597. break;
  598. }
  599. // TODO: Handle other types
  600. if (frame.min_width != frame.max_width ||
  601. frame.min_height != frame.max_height) {
  602. break;
  603. }
  604. for (uint32_t interval_index = 0;; ++interval_index) {
  605. struct v4l2_subdev_frame_interval_enum interval = {};
  606. interval.index = interval_index;
  607. interval.pad = 0;
  608. interval.code = fmt.code;
  609. interval.width = frame.max_width;
  610. interval.height = frame.max_height;
  611. interval.which = V4L2_SUBDEV_FORMAT_TRY;
  612. if (xioctl(camera->subdev_fd,
  613. VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL,
  614. &interval) == -1) {
  615. if (errno != EINVAL) {
  616. errno_printerr(
  617. "VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL");
  618. }
  619. break;
  620. }
  621. MPCameraMode mode = {
  622. .pixel_format = format,
  623. .frame_interval = interval.interval,
  624. .width = frame.max_width,
  625. .height = frame.max_height,
  626. };
  627. if (!check(camera, &mode)) {
  628. continue;
  629. }
  630. MPCameraModeList *new_item =
  631. malloc(sizeof(MPCameraModeList));
  632. new_item->mode = mode;
  633. new_item->next = item;
  634. item = new_item;
  635. }
  636. }
  637. }
  638. return item;
  639. }
  640. static MPCameraModeList *
  641. get_video_modes(MPCamera *camera, bool (*check)(MPCamera *, MPCameraMode *))
  642. {
  643. MPCameraModeList *item = NULL;
  644. for (uint32_t fmt_index = 0;; ++fmt_index) {
  645. struct v4l2_fmtdesc fmt = {};
  646. fmt.index = fmt_index;
  647. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  648. if (xioctl(camera->video_fd, VIDIOC_ENUM_FMT, &fmt) == -1) {
  649. if (errno != EINVAL) {
  650. errno_printerr("VIDIOC_ENUM_FMT");
  651. }
  652. break;
  653. }
  654. // Skip unsupported formats
  655. uint32_t format =
  656. mp_pixel_format_from_v4l_pixel_format(fmt.pixelformat);
  657. if (format == MP_PIXEL_FMT_UNSUPPORTED) {
  658. continue;
  659. }
  660. for (uint32_t frame_index = 0;; ++frame_index) {
  661. struct v4l2_frmsizeenum frame = {};
  662. frame.index = frame_index;
  663. frame.pixel_format = fmt.pixelformat;
  664. if (xioctl(camera->video_fd, VIDIOC_ENUM_FRAMESIZES,
  665. &frame) == -1) {
  666. if (errno != EINVAL) {
  667. errno_printerr("VIDIOC_ENUM_FRAMESIZES");
  668. }
  669. break;
  670. }
  671. // TODO: Handle other types
  672. if (frame.type != V4L2_FRMSIZE_TYPE_DISCRETE) {
  673. break;
  674. }
  675. for (uint32_t interval_index = 0;; ++interval_index) {
  676. struct v4l2_frmivalenum interval = {};
  677. interval.index = interval_index;
  678. interval.pixel_format = fmt.pixelformat;
  679. interval.width = frame.discrete.width;
  680. interval.height = frame.discrete.height;
  681. if (xioctl(camera->video_fd,
  682. VIDIOC_ENUM_FRAMEINTERVALS,
  683. &interval) == -1) {
  684. if (errno != EINVAL) {
  685. errno_printerr(
  686. "VIDIOC_ENUM_FRAMESIZES");
  687. }
  688. break;
  689. }
  690. // TODO: Handle other types
  691. if (interval.type != V4L2_FRMIVAL_TYPE_DISCRETE) {
  692. break;
  693. }
  694. MPCameraMode mode = {
  695. .pixel_format = format,
  696. .frame_interval = interval.discrete,
  697. .width = frame.discrete.width,
  698. .height = frame.discrete.height,
  699. };
  700. if (!check(camera, &mode)) {
  701. continue;
  702. }
  703. MPCameraModeList *new_item =
  704. malloc(sizeof(MPCameraModeList));
  705. new_item->mode = mode;
  706. new_item->next = item;
  707. item = new_item;
  708. }
  709. }
  710. }
  711. return item;
  712. }
  713. static bool
  714. all_modes(MPCamera *camera, MPCameraMode *mode)
  715. {
  716. return true;
  717. }
  718. static bool
  719. available_modes(MPCamera *camera, MPCameraMode *mode)
  720. {
  721. MPCameraMode attempt = *mode;
  722. return mp_camera_try_mode(camera, &attempt) &&
  723. mp_camera_mode_is_equivalent(mode, &attempt);
  724. }
  725. MPCameraModeList *
  726. mp_camera_list_supported_modes(MPCamera *camera)
  727. {
  728. if (mp_camera_is_subdev(camera)) {
  729. return get_subdev_modes(camera, all_modes);
  730. } else {
  731. return get_video_modes(camera, all_modes);
  732. }
  733. }
  734. MPCameraModeList *
  735. mp_camera_list_available_modes(MPCamera *camera)
  736. {
  737. if (mp_camera_is_subdev(camera)) {
  738. return get_subdev_modes(camera, available_modes);
  739. } else {
  740. return get_video_modes(camera, available_modes);
  741. }
  742. }
  743. MPCameraMode *
  744. mp_camera_mode_list_get(MPCameraModeList *list)
  745. {
  746. g_return_val_if_fail(list, NULL);
  747. return &list->mode;
  748. }
  749. MPCameraModeList *
  750. mp_camera_mode_list_next(MPCameraModeList *list)
  751. {
  752. g_return_val_if_fail(list, NULL);
  753. return list->next;
  754. }
  755. void
  756. mp_camera_mode_list_free(MPCameraModeList *list)
  757. {
  758. while (list) {
  759. MPCameraModeList *tmp = list;
  760. list = tmp->next;
  761. free(tmp);
  762. }
  763. }
  764. struct int_str_pair {
  765. uint32_t value;
  766. const char *str;
  767. };
  768. struct int_str_pair control_id_names[] = {
  769. { V4L2_CID_BRIGHTNESS, "BRIGHTNESS" },
  770. { V4L2_CID_CONTRAST, "CONTRAST" },
  771. { V4L2_CID_SATURATION, "SATURATION" },
  772. { V4L2_CID_HUE, "HUE" },
  773. { V4L2_CID_AUDIO_VOLUME, "AUDIO_VOLUME" },
  774. { V4L2_CID_AUDIO_BALANCE, "AUDIO_BALANCE" },
  775. { V4L2_CID_AUDIO_BASS, "AUDIO_BASS" },
  776. { V4L2_CID_AUDIO_TREBLE, "AUDIO_TREBLE" },
  777. { V4L2_CID_AUDIO_MUTE, "AUDIO_MUTE" },
  778. { V4L2_CID_AUDIO_LOUDNESS, "AUDIO_LOUDNESS" },
  779. { V4L2_CID_BLACK_LEVEL, "BLACK_LEVEL" },
  780. { V4L2_CID_AUTO_WHITE_BALANCE, "AUTO_WHITE_BALANCE" },
  781. { V4L2_CID_DO_WHITE_BALANCE, "DO_WHITE_BALANCE" },
  782. { V4L2_CID_RED_BALANCE, "RED_BALANCE" },
  783. { V4L2_CID_BLUE_BALANCE, "BLUE_BALANCE" },
  784. { V4L2_CID_GAMMA, "GAMMA" },
  785. { V4L2_CID_WHITENESS, "WHITENESS" },
  786. { V4L2_CID_EXPOSURE, "EXPOSURE" },
  787. { V4L2_CID_AUTOGAIN, "AUTOGAIN" },
  788. { V4L2_CID_GAIN, "GAIN" },
  789. { V4L2_CID_HFLIP, "HFLIP" },
  790. { V4L2_CID_VFLIP, "VFLIP" },
  791. { V4L2_CID_POWER_LINE_FREQUENCY, "POWER_LINE_FREQUENCY" },
  792. { V4L2_CID_HUE_AUTO, "HUE_AUTO" },
  793. { V4L2_CID_WHITE_BALANCE_TEMPERATURE, "WHITE_BALANCE_TEMPERATURE" },
  794. { V4L2_CID_SHARPNESS, "SHARPNESS" },
  795. { V4L2_CID_BACKLIGHT_COMPENSATION, "BACKLIGHT_COMPENSATION" },
  796. { V4L2_CID_CHROMA_AGC, "CHROMA_AGC" },
  797. { V4L2_CID_COLOR_KILLER, "COLOR_KILLER" },
  798. { V4L2_CID_COLORFX, "COLORFX" },
  799. { V4L2_CID_AUTOBRIGHTNESS, "AUTOBRIGHTNESS" },
  800. { V4L2_CID_BAND_STOP_FILTER, "BAND_STOP_FILTER" },
  801. { V4L2_CID_ROTATE, "ROTATE" },
  802. { V4L2_CID_BG_COLOR, "BG_COLOR" },
  803. { V4L2_CID_CHROMA_GAIN, "CHROMA_GAIN" },
  804. { V4L2_CID_ILLUMINATORS_1, "ILLUMINATORS_1" },
  805. { V4L2_CID_ILLUMINATORS_2, "ILLUMINATORS_2" },
  806. { V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, "MIN_BUFFERS_FOR_CAPTURE" },
  807. { V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, "MIN_BUFFERS_FOR_OUTPUT" },
  808. { V4L2_CID_ALPHA_COMPONENT, "ALPHA_COMPONENT" },
  809. { V4L2_CID_COLORFX_CBCR, "COLORFX_CBCR" },
  810. { V4L2_CID_LASTP1, "LASTP1" },
  811. { V4L2_CID_USER_MEYE_BASE, "USER_MEYE_BASE" },
  812. { V4L2_CID_USER_BTTV_BASE, "USER_BTTV_BASE" },
  813. { V4L2_CID_USER_S2255_BASE, "USER_S2255_BASE" },
  814. { V4L2_CID_USER_SI476X_BASE, "USER_SI476X_BASE" },
  815. { V4L2_CID_USER_TI_VPE_BASE, "USER_TI_VPE_BASE" },
  816. { V4L2_CID_USER_SAA7134_BASE, "USER_SAA7134_BASE" },
  817. { V4L2_CID_USER_ADV7180_BASE, "USER_ADV7180_BASE" },
  818. { V4L2_CID_USER_TC358743_BASE, "USER_TC358743_BASE" },
  819. { V4L2_CID_USER_MAX217X_BASE, "USER_MAX217X_BASE" },
  820. { V4L2_CID_USER_IMX_BASE, "USER_IMX_BASE" },
  821. // { V4L2_CID_USER_ATMEL_ISC_BASE, "USER_ATMEL_ISC_BASE" },
  822. { V4L2_CID_CAMERA_CLASS_BASE, "CAMERA_CLASS_BASE" },
  823. { V4L2_CID_CAMERA_CLASS, "CAMERA_CLASS" },
  824. { V4L2_CID_EXPOSURE_AUTO, "EXPOSURE_AUTO" },
  825. { V4L2_CID_EXPOSURE_ABSOLUTE, "EXPOSURE_ABSOLUTE" },
  826. { V4L2_CID_EXPOSURE_AUTO_PRIORITY, "EXPOSURE_AUTO_PRIORITY" },
  827. { V4L2_CID_PAN_RELATIVE, "PAN_RELATIVE" },
  828. { V4L2_CID_TILT_RELATIVE, "TILT_RELATIVE" },
  829. { V4L2_CID_PAN_RESET, "PAN_RESET" },
  830. { V4L2_CID_TILT_RESET, "TILT_RESET" },
  831. { V4L2_CID_PAN_ABSOLUTE, "PAN_ABSOLUTE" },
  832. { V4L2_CID_TILT_ABSOLUTE, "TILT_ABSOLUTE" },
  833. { V4L2_CID_FOCUS_ABSOLUTE, "FOCUS_ABSOLUTE" },
  834. { V4L2_CID_FOCUS_RELATIVE, "FOCUS_RELATIVE" },
  835. { V4L2_CID_FOCUS_AUTO, "FOCUS_AUTO" },
  836. { V4L2_CID_ZOOM_ABSOLUTE, "ZOOM_ABSOLUTE" },
  837. { V4L2_CID_ZOOM_RELATIVE, "ZOOM_RELATIVE" },
  838. { V4L2_CID_ZOOM_CONTINUOUS, "ZOOM_CONTINUOUS" },
  839. { V4L2_CID_PRIVACY, "PRIVACY" },
  840. { V4L2_CID_IRIS_ABSOLUTE, "IRIS_ABSOLUTE" },
  841. { V4L2_CID_IRIS_RELATIVE, "IRIS_RELATIVE" },
  842. { V4L2_CID_AUTO_EXPOSURE_BIAS, "AUTO_EXPOSURE_BIAS" },
  843. { V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE, "AUTO_N_PRESET_WHITE_BALANCE" },
  844. { V4L2_CID_WIDE_DYNAMIC_RANGE, "WIDE_DYNAMIC_RANGE" },
  845. { V4L2_CID_IMAGE_STABILIZATION, "IMAGE_STABILIZATION" },
  846. { V4L2_CID_ISO_SENSITIVITY, "ISO_SENSITIVITY" },
  847. { V4L2_CID_ISO_SENSITIVITY_AUTO, "ISO_SENSITIVITY_AUTO" },
  848. { V4L2_CID_EXPOSURE_METERING, "EXPOSURE_METERING" },
  849. { V4L2_CID_SCENE_MODE, "SCENE_MODE" },
  850. { V4L2_CID_3A_LOCK, "3A_LOCK" },
  851. { V4L2_CID_AUTO_FOCUS_START, "AUTO_FOCUS_START" },
  852. { V4L2_CID_AUTO_FOCUS_STOP, "AUTO_FOCUS_STOP" },
  853. { V4L2_CID_AUTO_FOCUS_STATUS, "AUTO_FOCUS_STATUS" },
  854. { V4L2_CID_AUTO_FOCUS_RANGE, "AUTO_FOCUS_RANGE" },
  855. { V4L2_CID_PAN_SPEED, "PAN_SPEED" },
  856. { V4L2_CID_TILT_SPEED, "TILT_SPEED" },
  857. // { V4L2_CID_CAMERA_ORIENTATION, "CAMERA_ORIENTATION" },
  858. // { V4L2_CID_CAMERA_SENSOR_ROTATION, "CAMERA_SENSOR_ROTATION" },
  859. { V4L2_CID_FLASH_LED_MODE, "FLASH_LED_MODE" },
  860. { V4L2_CID_FLASH_STROBE_SOURCE, "FLASH_STROBE_SOURCE" },
  861. { V4L2_CID_FLASH_STROBE, "FLASH_STROBE" },
  862. { V4L2_CID_FLASH_STROBE_STOP, "FLASH_STROBE_STOP" },
  863. { V4L2_CID_FLASH_STROBE_STATUS, "FLASH_STROBE_STATUS" },
  864. { V4L2_CID_FLASH_TIMEOUT, "FLASH_TIMEOUT" },
  865. { V4L2_CID_FLASH_INTENSITY, "FLASH_INTENSITY" },
  866. { V4L2_CID_FLASH_TORCH_INTENSITY, "FLASH_TORCH_INTENSITY" },
  867. { V4L2_CID_FLASH_INDICATOR_INTENSITY, "FLASH_INDICATOR_INTENSITY" },
  868. { V4L2_CID_FLASH_FAULT, "FLASH_FAULT" },
  869. { V4L2_CID_FLASH_CHARGE, "FLASH_CHARGE" },
  870. { V4L2_CID_FLASH_READY, "FLASH_READY" },
  871. };
  872. const char *
  873. mp_control_id_to_str(uint32_t id)
  874. {
  875. size_t size = sizeof(control_id_names) / sizeof(*control_id_names);
  876. for (size_t i = 0; i < size; ++i) {
  877. if (control_id_names[i].value == id) {
  878. return control_id_names[i].str;
  879. }
  880. }
  881. return "UNKNOWN";
  882. }
  883. struct int_str_pair control_type_names[] = {
  884. { V4L2_CTRL_TYPE_INTEGER, "INTEGER" },
  885. { V4L2_CTRL_TYPE_BOOLEAN, "BOOLEAN" },
  886. { V4L2_CTRL_TYPE_MENU, "MENU" },
  887. { V4L2_CTRL_TYPE_INTEGER_MENU, "INTEGER_MENU" },
  888. { V4L2_CTRL_TYPE_BITMASK, "BITMASK" },
  889. { V4L2_CTRL_TYPE_BUTTON, "BUTTON" },
  890. { V4L2_CTRL_TYPE_INTEGER64, "INTEGER64" },
  891. { V4L2_CTRL_TYPE_STRING, "STRING" },
  892. { V4L2_CTRL_TYPE_CTRL_CLASS, "CTRL_CLASS" },
  893. { V4L2_CTRL_TYPE_U8, "U8" },
  894. { V4L2_CTRL_TYPE_U16, "U16" },
  895. { V4L2_CTRL_TYPE_U32, "U32" },
  896. // { V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS, "MPEG2_SLICE_PARAMS" },
  897. // { V4L2_CTRL_TYPE_MPEG2_QUANTIZATION, "MPEG2_QUANTIZATION" },
  898. // { V4L2_CTRL_TYPE_AREA, "AREA" },
  899. // { V4L2_CTRL_TYPE_H264_SPS, "H264_SPS" },
  900. // { V4L2_CTRL_TYPE_H264_PPS, "H264_PPS" },
  901. // { V4L2_CTRL_TYPE_H264_SCALING_MATRIX, "H264_SCALING_MATRIX" },
  902. // { V4L2_CTRL_TYPE_H264_SLICE_PARAMS, "H264_SLICE_PARAMS" },
  903. // { V4L2_CTRL_TYPE_H264_DECODE_PARAMS, "H264_DECODE_PARAMS" },
  904. // { V4L2_CTRL_TYPE_HEVC_SPS, "HEVC_SPS" },
  905. // { V4L2_CTRL_TYPE_HEVC_PPS, "HEVC_PPS" },
  906. // { V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS, "HEVC_SLICE_PARAMS" },
  907. };
  908. const char *
  909. mp_control_type_to_str(uint32_t type)
  910. {
  911. size_t size = sizeof(control_type_names) / sizeof(*control_type_names);
  912. for (size_t i = 0; i < size; ++i) {
  913. if (control_type_names[i].value == type) {
  914. return control_type_names[i].str;
  915. }
  916. }
  917. return "UNKNOWN";
  918. }
  919. struct _MPControlList {
  920. MPControl control;
  921. MPControlList *next;
  922. };
  923. static int
  924. control_fd(MPCamera *camera)
  925. {
  926. if (camera->subdev_fd != -1) {
  927. return camera->subdev_fd;
  928. }
  929. return camera->video_fd;
  930. }
  931. MPControlList *
  932. mp_camera_list_controls(MPCamera *camera)
  933. {
  934. MPControlList *item = NULL;
  935. struct v4l2_query_ext_ctrl ctrl = {};
  936. ctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
  937. while (true) {
  938. if (xioctl(control_fd(camera), VIDIOC_QUERY_EXT_CTRL, &ctrl) == -1) {
  939. if (errno != EINVAL) {
  940. errno_printerr("VIDIOC_QUERY_EXT_CTRL");
  941. }
  942. break;
  943. }
  944. MPControl control = {
  945. .id = ctrl.id,
  946. .type = ctrl.type,
  947. .name = {},
  948. .min = ctrl.minimum,
  949. .max = ctrl.maximum,
  950. .step = ctrl.step,
  951. .default_value = ctrl.default_value,
  952. .flags = ctrl.flags,
  953. .element_size = ctrl.elem_size,
  954. .element_count = ctrl.elems,
  955. .dimensions_count = ctrl.nr_of_dims,
  956. .dimensions = {},
  957. };
  958. strcpy(control.name, ctrl.name);
  959. memcpy(control.dimensions, ctrl.dims,
  960. sizeof(uint32_t) * V4L2_CTRL_MAX_DIMS);
  961. MPControlList *new_item = malloc(sizeof(MPControlList));
  962. new_item->control = control;
  963. new_item->next = item;
  964. item = new_item;
  965. ctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
  966. }
  967. return item;
  968. }
  969. MPControl *
  970. mp_control_list_get(MPControlList *list)
  971. {
  972. g_return_val_if_fail(list, NULL);
  973. return &list->control;
  974. }
  975. MPControlList *
  976. mp_control_list_next(MPControlList *list)
  977. {
  978. g_return_val_if_fail(list, NULL);
  979. return list->next;
  980. }
  981. void
  982. mp_control_list_free(MPControlList *list)
  983. {
  984. while (list) {
  985. MPControlList *tmp = list;
  986. list = tmp->next;
  987. free(tmp);
  988. }
  989. }
  990. bool
  991. mp_camera_query_control(MPCamera *camera, uint32_t id, MPControl *control)
  992. {
  993. struct v4l2_query_ext_ctrl ctrl = {};
  994. ctrl.id = id;
  995. if (xioctl(control_fd(camera), VIDIOC_QUERY_EXT_CTRL, &ctrl) == -1) {
  996. if (errno != EINVAL) {
  997. errno_printerr("VIDIOC_QUERY_EXT_CTRL");
  998. }
  999. return false;
  1000. }
  1001. if (control) {
  1002. control->id = ctrl.id;
  1003. control->type = ctrl.type;
  1004. strcpy(control->name, ctrl.name);
  1005. control->min = ctrl.minimum;
  1006. control->max = ctrl.maximum;
  1007. control->step = ctrl.step;
  1008. control->default_value = ctrl.default_value;
  1009. control->flags = ctrl.flags;
  1010. control->element_size = ctrl.elem_size;
  1011. control->element_count = ctrl.elems;
  1012. control->dimensions_count = ctrl.nr_of_dims;
  1013. memcpy(control->dimensions, ctrl.dims,
  1014. sizeof(uint32_t) * V4L2_CTRL_MAX_DIMS);
  1015. }
  1016. return true;
  1017. }
  1018. static bool
  1019. control_impl_int32(MPCamera *camera, uint32_t id, int request, int32_t *value)
  1020. {
  1021. struct v4l2_ext_control ctrl = {};
  1022. ctrl.id = id;
  1023. ctrl.value = *value;
  1024. struct v4l2_ext_controls ctrls = {
  1025. .ctrl_class = 0,
  1026. .which = V4L2_CTRL_WHICH_CUR_VAL,
  1027. .count = 1,
  1028. .controls = &ctrl,
  1029. };
  1030. if (xioctl(control_fd(camera), request, &ctrls) == -1) {
  1031. return false;
  1032. }
  1033. *value = ctrl.value;
  1034. return true;
  1035. }
  1036. bool
  1037. mp_camera_control_try_int32(MPCamera *camera, uint32_t id, int32_t *v)
  1038. {
  1039. return control_impl_int32(camera, id, VIDIOC_TRY_EXT_CTRLS, v);
  1040. }
  1041. bool
  1042. mp_camera_control_set_int32(MPCamera *camera, uint32_t id, int32_t v)
  1043. {
  1044. return control_impl_int32(camera, id, VIDIOC_S_EXT_CTRLS, &v);
  1045. }
  1046. int32_t
  1047. mp_camera_control_get_int32(MPCamera *camera, uint32_t id)
  1048. {
  1049. int32_t v = 0;
  1050. control_impl_int32(camera, id, VIDIOC_G_EXT_CTRLS, &v);
  1051. return v;
  1052. }
  1053. bool
  1054. mp_camera_control_try_boolean(MPCamera *camera, uint32_t id, bool *v)
  1055. {
  1056. int32_t value = *v;
  1057. bool s = control_impl_int32(camera, id, VIDIOC_TRY_EXT_CTRLS, &value);
  1058. *v = value;
  1059. return s;
  1060. }
  1061. bool
  1062. mp_camera_control_set_bool(MPCamera *camera, uint32_t id, bool v)
  1063. {
  1064. int32_t value = v;
  1065. return control_impl_int32(camera, id, VIDIOC_S_EXT_CTRLS, &value);
  1066. }
  1067. bool
  1068. mp_camera_control_get_bool(MPCamera *camera, uint32_t id)
  1069. {
  1070. int32_t v = false;
  1071. control_impl_int32(camera, id, VIDIOC_G_EXT_CTRLS, &v);
  1072. return v;
  1073. }