camera.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. return false;
  528. }
  529. }
  530. uint32_t pixel_format = camera->current_mode.pixel_format;
  531. uint32_t width = camera->current_mode.width;
  532. uint32_t height = camera->current_mode.height;
  533. uint32_t bytesused;
  534. if (camera->use_mplane) {
  535. bytesused = planes[0].bytesused;
  536. } else {
  537. bytesused = buf.bytesused;
  538. }
  539. assert(bytesused ==
  540. mp_pixel_format_width_to_bytes(pixel_format, width) * height);
  541. assert(bytesused == camera->buffers[buf.index].length);
  542. buffer->index = buf.index;
  543. buffer->data = camera->buffers[buf.index].data;
  544. buffer->fd = camera->buffers[buf.index].fd;
  545. return true;
  546. }
  547. bool mp_camera_release_buffer(MPCamera *camera, uint32_t buffer_index)
  548. {
  549. struct v4l2_buffer buf = {};
  550. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  551. buf.memory = V4L2_MEMORY_MMAP;
  552. buf.index = buffer_index;
  553. if (xioctl(camera->video_fd, VIDIOC_QBUF, &buf) == -1) {
  554. errno_printerr("VIDIOC_QBUF");
  555. return false;
  556. }
  557. return true;
  558. }
  559. struct _MPCameraModeList {
  560. MPCameraMode mode;
  561. MPCameraModeList *next;
  562. };
  563. static MPCameraModeList *
  564. get_subdev_modes(MPCamera *camera, bool (*check)(MPCamera *, MPCameraMode *))
  565. {
  566. MPCameraModeList *item = NULL;
  567. for (uint32_t fmt_index = 0;; ++fmt_index) {
  568. struct v4l2_subdev_mbus_code_enum fmt = {};
  569. fmt.index = fmt_index;
  570. fmt.pad = 0;
  571. fmt.which = V4L2_SUBDEV_FORMAT_TRY;
  572. if (xioctl(camera->subdev_fd, VIDIOC_SUBDEV_ENUM_MBUS_CODE, &fmt) ==
  573. -1) {
  574. if (errno != EINVAL) {
  575. errno_printerr("VIDIOC_SUBDEV_ENUM_MBUS_CODE");
  576. }
  577. break;
  578. }
  579. // Skip unsupported formats
  580. uint32_t format = mp_pixel_format_from_v4l_bus_code(fmt.code);
  581. if (format == MP_PIXEL_FMT_UNSUPPORTED) {
  582. continue;
  583. }
  584. for (uint32_t frame_index = 0;; ++frame_index) {
  585. struct v4l2_subdev_frame_size_enum frame = {};
  586. frame.index = frame_index;
  587. frame.pad = 0;
  588. frame.code = fmt.code;
  589. frame.which = V4L2_SUBDEV_FORMAT_TRY;
  590. if (xioctl(camera->subdev_fd, VIDIOC_SUBDEV_ENUM_FRAME_SIZE,
  591. &frame) == -1) {
  592. if (errno != EINVAL) {
  593. errno_printerr(
  594. "VIDIOC_SUBDEV_ENUM_FRAME_SIZE");
  595. }
  596. break;
  597. }
  598. // TODO: Handle other types
  599. if (frame.min_width != frame.max_width ||
  600. frame.min_height != frame.max_height) {
  601. break;
  602. }
  603. for (uint32_t interval_index = 0;; ++interval_index) {
  604. struct v4l2_subdev_frame_interval_enum interval = {};
  605. interval.index = interval_index;
  606. interval.pad = 0;
  607. interval.code = fmt.code;
  608. interval.width = frame.max_width;
  609. interval.height = frame.max_height;
  610. interval.which = V4L2_SUBDEV_FORMAT_TRY;
  611. if (xioctl(camera->subdev_fd,
  612. VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL,
  613. &interval) == -1) {
  614. if (errno != EINVAL) {
  615. errno_printerr(
  616. "VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL");
  617. }
  618. break;
  619. }
  620. MPCameraMode mode = {
  621. .pixel_format = format,
  622. .frame_interval = interval.interval,
  623. .width = frame.max_width,
  624. .height = frame.max_height,
  625. };
  626. if (!check(camera, &mode)) {
  627. continue;
  628. }
  629. MPCameraModeList *new_item =
  630. malloc(sizeof(MPCameraModeList));
  631. new_item->mode = mode;
  632. new_item->next = item;
  633. item = new_item;
  634. }
  635. }
  636. }
  637. return item;
  638. }
  639. static MPCameraModeList *
  640. get_video_modes(MPCamera *camera, bool (*check)(MPCamera *, MPCameraMode *))
  641. {
  642. MPCameraModeList *item = NULL;
  643. for (uint32_t fmt_index = 0;; ++fmt_index) {
  644. struct v4l2_fmtdesc fmt = {};
  645. fmt.index = fmt_index;
  646. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  647. if (xioctl(camera->video_fd, VIDIOC_ENUM_FMT, &fmt) == -1) {
  648. if (errno != EINVAL) {
  649. errno_printerr("VIDIOC_ENUM_FMT");
  650. }
  651. break;
  652. }
  653. // Skip unsupported formats
  654. uint32_t format =
  655. mp_pixel_format_from_v4l_pixel_format(fmt.pixelformat);
  656. if (format == MP_PIXEL_FMT_UNSUPPORTED) {
  657. continue;
  658. }
  659. for (uint32_t frame_index = 0;; ++frame_index) {
  660. struct v4l2_frmsizeenum frame = {};
  661. frame.index = frame_index;
  662. frame.pixel_format = fmt.pixelformat;
  663. if (xioctl(camera->video_fd, VIDIOC_ENUM_FRAMESIZES,
  664. &frame) == -1) {
  665. if (errno != EINVAL) {
  666. errno_printerr("VIDIOC_ENUM_FRAMESIZES");
  667. }
  668. break;
  669. }
  670. // TODO: Handle other types
  671. if (frame.type != V4L2_FRMSIZE_TYPE_DISCRETE) {
  672. break;
  673. }
  674. for (uint32_t interval_index = 0;; ++interval_index) {
  675. struct v4l2_frmivalenum interval = {};
  676. interval.index = interval_index;
  677. interval.pixel_format = fmt.pixelformat;
  678. interval.width = frame.discrete.width;
  679. interval.height = frame.discrete.height;
  680. if (xioctl(camera->video_fd,
  681. VIDIOC_ENUM_FRAMEINTERVALS,
  682. &interval) == -1) {
  683. if (errno != EINVAL) {
  684. errno_printerr(
  685. "VIDIOC_ENUM_FRAMESIZES");
  686. }
  687. break;
  688. }
  689. // TODO: Handle other types
  690. if (interval.type != V4L2_FRMIVAL_TYPE_DISCRETE) {
  691. break;
  692. }
  693. MPCameraMode mode = {
  694. .pixel_format = format,
  695. .frame_interval = interval.discrete,
  696. .width = frame.discrete.width,
  697. .height = frame.discrete.height,
  698. };
  699. if (!check(camera, &mode)) {
  700. continue;
  701. }
  702. MPCameraModeList *new_item =
  703. malloc(sizeof(MPCameraModeList));
  704. new_item->mode = mode;
  705. new_item->next = item;
  706. item = new_item;
  707. }
  708. }
  709. }
  710. return item;
  711. }
  712. static bool
  713. all_modes(MPCamera *camera, MPCameraMode *mode)
  714. {
  715. return true;
  716. }
  717. static bool
  718. available_modes(MPCamera *camera, MPCameraMode *mode)
  719. {
  720. MPCameraMode attempt = *mode;
  721. return mp_camera_try_mode(camera, &attempt) &&
  722. mp_camera_mode_is_equivalent(mode, &attempt);
  723. }
  724. MPCameraModeList *
  725. mp_camera_list_supported_modes(MPCamera *camera)
  726. {
  727. if (mp_camera_is_subdev(camera)) {
  728. return get_subdev_modes(camera, all_modes);
  729. } else {
  730. return get_video_modes(camera, all_modes);
  731. }
  732. }
  733. MPCameraModeList *
  734. mp_camera_list_available_modes(MPCamera *camera)
  735. {
  736. if (mp_camera_is_subdev(camera)) {
  737. return get_subdev_modes(camera, available_modes);
  738. } else {
  739. return get_video_modes(camera, available_modes);
  740. }
  741. }
  742. MPCameraMode *
  743. mp_camera_mode_list_get(MPCameraModeList *list)
  744. {
  745. g_return_val_if_fail(list, NULL);
  746. return &list->mode;
  747. }
  748. MPCameraModeList *
  749. mp_camera_mode_list_next(MPCameraModeList *list)
  750. {
  751. g_return_val_if_fail(list, NULL);
  752. return list->next;
  753. }
  754. void
  755. mp_camera_mode_list_free(MPCameraModeList *list)
  756. {
  757. while (list) {
  758. MPCameraModeList *tmp = list;
  759. list = tmp->next;
  760. free(tmp);
  761. }
  762. }
  763. struct int_str_pair {
  764. uint32_t value;
  765. const char *str;
  766. };
  767. struct int_str_pair control_id_names[] = {
  768. { V4L2_CID_BRIGHTNESS, "BRIGHTNESS" },
  769. { V4L2_CID_CONTRAST, "CONTRAST" },
  770. { V4L2_CID_SATURATION, "SATURATION" },
  771. { V4L2_CID_HUE, "HUE" },
  772. { V4L2_CID_AUDIO_VOLUME, "AUDIO_VOLUME" },
  773. { V4L2_CID_AUDIO_BALANCE, "AUDIO_BALANCE" },
  774. { V4L2_CID_AUDIO_BASS, "AUDIO_BASS" },
  775. { V4L2_CID_AUDIO_TREBLE, "AUDIO_TREBLE" },
  776. { V4L2_CID_AUDIO_MUTE, "AUDIO_MUTE" },
  777. { V4L2_CID_AUDIO_LOUDNESS, "AUDIO_LOUDNESS" },
  778. { V4L2_CID_BLACK_LEVEL, "BLACK_LEVEL" },
  779. { V4L2_CID_AUTO_WHITE_BALANCE, "AUTO_WHITE_BALANCE" },
  780. { V4L2_CID_DO_WHITE_BALANCE, "DO_WHITE_BALANCE" },
  781. { V4L2_CID_RED_BALANCE, "RED_BALANCE" },
  782. { V4L2_CID_BLUE_BALANCE, "BLUE_BALANCE" },
  783. { V4L2_CID_GAMMA, "GAMMA" },
  784. { V4L2_CID_WHITENESS, "WHITENESS" },
  785. { V4L2_CID_EXPOSURE, "EXPOSURE" },
  786. { V4L2_CID_AUTOGAIN, "AUTOGAIN" },
  787. { V4L2_CID_GAIN, "GAIN" },
  788. { V4L2_CID_HFLIP, "HFLIP" },
  789. { V4L2_CID_VFLIP, "VFLIP" },
  790. { V4L2_CID_POWER_LINE_FREQUENCY, "POWER_LINE_FREQUENCY" },
  791. { V4L2_CID_HUE_AUTO, "HUE_AUTO" },
  792. { V4L2_CID_WHITE_BALANCE_TEMPERATURE, "WHITE_BALANCE_TEMPERATURE" },
  793. { V4L2_CID_SHARPNESS, "SHARPNESS" },
  794. { V4L2_CID_BACKLIGHT_COMPENSATION, "BACKLIGHT_COMPENSATION" },
  795. { V4L2_CID_CHROMA_AGC, "CHROMA_AGC" },
  796. { V4L2_CID_COLOR_KILLER, "COLOR_KILLER" },
  797. { V4L2_CID_COLORFX, "COLORFX" },
  798. { V4L2_CID_AUTOBRIGHTNESS, "AUTOBRIGHTNESS" },
  799. { V4L2_CID_BAND_STOP_FILTER, "BAND_STOP_FILTER" },
  800. { V4L2_CID_ROTATE, "ROTATE" },
  801. { V4L2_CID_BG_COLOR, "BG_COLOR" },
  802. { V4L2_CID_CHROMA_GAIN, "CHROMA_GAIN" },
  803. { V4L2_CID_ILLUMINATORS_1, "ILLUMINATORS_1" },
  804. { V4L2_CID_ILLUMINATORS_2, "ILLUMINATORS_2" },
  805. { V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, "MIN_BUFFERS_FOR_CAPTURE" },
  806. { V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, "MIN_BUFFERS_FOR_OUTPUT" },
  807. { V4L2_CID_ALPHA_COMPONENT, "ALPHA_COMPONENT" },
  808. { V4L2_CID_COLORFX_CBCR, "COLORFX_CBCR" },
  809. { V4L2_CID_LASTP1, "LASTP1" },
  810. { V4L2_CID_USER_MEYE_BASE, "USER_MEYE_BASE" },
  811. { V4L2_CID_USER_BTTV_BASE, "USER_BTTV_BASE" },
  812. { V4L2_CID_USER_S2255_BASE, "USER_S2255_BASE" },
  813. { V4L2_CID_USER_SI476X_BASE, "USER_SI476X_BASE" },
  814. { V4L2_CID_USER_TI_VPE_BASE, "USER_TI_VPE_BASE" },
  815. { V4L2_CID_USER_SAA7134_BASE, "USER_SAA7134_BASE" },
  816. { V4L2_CID_USER_ADV7180_BASE, "USER_ADV7180_BASE" },
  817. { V4L2_CID_USER_TC358743_BASE, "USER_TC358743_BASE" },
  818. { V4L2_CID_USER_MAX217X_BASE, "USER_MAX217X_BASE" },
  819. { V4L2_CID_USER_IMX_BASE, "USER_IMX_BASE" },
  820. // { V4L2_CID_USER_ATMEL_ISC_BASE, "USER_ATMEL_ISC_BASE" },
  821. { V4L2_CID_CAMERA_CLASS_BASE, "CAMERA_CLASS_BASE" },
  822. { V4L2_CID_CAMERA_CLASS, "CAMERA_CLASS" },
  823. { V4L2_CID_EXPOSURE_AUTO, "EXPOSURE_AUTO" },
  824. { V4L2_CID_EXPOSURE_ABSOLUTE, "EXPOSURE_ABSOLUTE" },
  825. { V4L2_CID_EXPOSURE_AUTO_PRIORITY, "EXPOSURE_AUTO_PRIORITY" },
  826. { V4L2_CID_PAN_RELATIVE, "PAN_RELATIVE" },
  827. { V4L2_CID_TILT_RELATIVE, "TILT_RELATIVE" },
  828. { V4L2_CID_PAN_RESET, "PAN_RESET" },
  829. { V4L2_CID_TILT_RESET, "TILT_RESET" },
  830. { V4L2_CID_PAN_ABSOLUTE, "PAN_ABSOLUTE" },
  831. { V4L2_CID_TILT_ABSOLUTE, "TILT_ABSOLUTE" },
  832. { V4L2_CID_FOCUS_ABSOLUTE, "FOCUS_ABSOLUTE" },
  833. { V4L2_CID_FOCUS_RELATIVE, "FOCUS_RELATIVE" },
  834. { V4L2_CID_FOCUS_AUTO, "FOCUS_AUTO" },
  835. { V4L2_CID_ZOOM_ABSOLUTE, "ZOOM_ABSOLUTE" },
  836. { V4L2_CID_ZOOM_RELATIVE, "ZOOM_RELATIVE" },
  837. { V4L2_CID_ZOOM_CONTINUOUS, "ZOOM_CONTINUOUS" },
  838. { V4L2_CID_PRIVACY, "PRIVACY" },
  839. { V4L2_CID_IRIS_ABSOLUTE, "IRIS_ABSOLUTE" },
  840. { V4L2_CID_IRIS_RELATIVE, "IRIS_RELATIVE" },
  841. { V4L2_CID_AUTO_EXPOSURE_BIAS, "AUTO_EXPOSURE_BIAS" },
  842. { V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE, "AUTO_N_PRESET_WHITE_BALANCE" },
  843. { V4L2_CID_WIDE_DYNAMIC_RANGE, "WIDE_DYNAMIC_RANGE" },
  844. { V4L2_CID_IMAGE_STABILIZATION, "IMAGE_STABILIZATION" },
  845. { V4L2_CID_ISO_SENSITIVITY, "ISO_SENSITIVITY" },
  846. { V4L2_CID_ISO_SENSITIVITY_AUTO, "ISO_SENSITIVITY_AUTO" },
  847. { V4L2_CID_EXPOSURE_METERING, "EXPOSURE_METERING" },
  848. { V4L2_CID_SCENE_MODE, "SCENE_MODE" },
  849. { V4L2_CID_3A_LOCK, "3A_LOCK" },
  850. { V4L2_CID_AUTO_FOCUS_START, "AUTO_FOCUS_START" },
  851. { V4L2_CID_AUTO_FOCUS_STOP, "AUTO_FOCUS_STOP" },
  852. { V4L2_CID_AUTO_FOCUS_STATUS, "AUTO_FOCUS_STATUS" },
  853. { V4L2_CID_AUTO_FOCUS_RANGE, "AUTO_FOCUS_RANGE" },
  854. { V4L2_CID_PAN_SPEED, "PAN_SPEED" },
  855. { V4L2_CID_TILT_SPEED, "TILT_SPEED" },
  856. // { V4L2_CID_CAMERA_ORIENTATION, "CAMERA_ORIENTATION" },
  857. // { V4L2_CID_CAMERA_SENSOR_ROTATION, "CAMERA_SENSOR_ROTATION" },
  858. { V4L2_CID_FLASH_LED_MODE, "FLASH_LED_MODE" },
  859. { V4L2_CID_FLASH_STROBE_SOURCE, "FLASH_STROBE_SOURCE" },
  860. { V4L2_CID_FLASH_STROBE, "FLASH_STROBE" },
  861. { V4L2_CID_FLASH_STROBE_STOP, "FLASH_STROBE_STOP" },
  862. { V4L2_CID_FLASH_STROBE_STATUS, "FLASH_STROBE_STATUS" },
  863. { V4L2_CID_FLASH_TIMEOUT, "FLASH_TIMEOUT" },
  864. { V4L2_CID_FLASH_INTENSITY, "FLASH_INTENSITY" },
  865. { V4L2_CID_FLASH_TORCH_INTENSITY, "FLASH_TORCH_INTENSITY" },
  866. { V4L2_CID_FLASH_INDICATOR_INTENSITY, "FLASH_INDICATOR_INTENSITY" },
  867. { V4L2_CID_FLASH_FAULT, "FLASH_FAULT" },
  868. { V4L2_CID_FLASH_CHARGE, "FLASH_CHARGE" },
  869. { V4L2_CID_FLASH_READY, "FLASH_READY" },
  870. };
  871. const char *
  872. mp_control_id_to_str(uint32_t id)
  873. {
  874. size_t size = sizeof(control_id_names) / sizeof(*control_id_names);
  875. for (size_t i = 0; i < size; ++i) {
  876. if (control_id_names[i].value == id) {
  877. return control_id_names[i].str;
  878. }
  879. }
  880. return "UNKNOWN";
  881. }
  882. struct int_str_pair control_type_names[] = {
  883. { V4L2_CTRL_TYPE_INTEGER, "INTEGER" },
  884. { V4L2_CTRL_TYPE_BOOLEAN, "BOOLEAN" },
  885. { V4L2_CTRL_TYPE_MENU, "MENU" },
  886. { V4L2_CTRL_TYPE_INTEGER_MENU, "INTEGER_MENU" },
  887. { V4L2_CTRL_TYPE_BITMASK, "BITMASK" },
  888. { V4L2_CTRL_TYPE_BUTTON, "BUTTON" },
  889. { V4L2_CTRL_TYPE_INTEGER64, "INTEGER64" },
  890. { V4L2_CTRL_TYPE_STRING, "STRING" },
  891. { V4L2_CTRL_TYPE_CTRL_CLASS, "CTRL_CLASS" },
  892. { V4L2_CTRL_TYPE_U8, "U8" },
  893. { V4L2_CTRL_TYPE_U16, "U16" },
  894. { V4L2_CTRL_TYPE_U32, "U32" },
  895. // { V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS, "MPEG2_SLICE_PARAMS" },
  896. // { V4L2_CTRL_TYPE_MPEG2_QUANTIZATION, "MPEG2_QUANTIZATION" },
  897. // { V4L2_CTRL_TYPE_AREA, "AREA" },
  898. // { V4L2_CTRL_TYPE_H264_SPS, "H264_SPS" },
  899. // { V4L2_CTRL_TYPE_H264_PPS, "H264_PPS" },
  900. // { V4L2_CTRL_TYPE_H264_SCALING_MATRIX, "H264_SCALING_MATRIX" },
  901. // { V4L2_CTRL_TYPE_H264_SLICE_PARAMS, "H264_SLICE_PARAMS" },
  902. // { V4L2_CTRL_TYPE_H264_DECODE_PARAMS, "H264_DECODE_PARAMS" },
  903. // { V4L2_CTRL_TYPE_HEVC_SPS, "HEVC_SPS" },
  904. // { V4L2_CTRL_TYPE_HEVC_PPS, "HEVC_PPS" },
  905. // { V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS, "HEVC_SLICE_PARAMS" },
  906. };
  907. const char *
  908. mp_control_type_to_str(uint32_t type)
  909. {
  910. size_t size = sizeof(control_type_names) / sizeof(*control_type_names);
  911. for (size_t i = 0; i < size; ++i) {
  912. if (control_type_names[i].value == type) {
  913. return control_type_names[i].str;
  914. }
  915. }
  916. return "UNKNOWN";
  917. }
  918. struct _MPControlList {
  919. MPControl control;
  920. MPControlList *next;
  921. };
  922. static int
  923. control_fd(MPCamera *camera)
  924. {
  925. if (camera->subdev_fd != -1) {
  926. return camera->subdev_fd;
  927. }
  928. return camera->video_fd;
  929. }
  930. MPControlList *
  931. mp_camera_list_controls(MPCamera *camera)
  932. {
  933. MPControlList *item = NULL;
  934. struct v4l2_query_ext_ctrl ctrl = {};
  935. ctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
  936. while (true) {
  937. if (xioctl(control_fd(camera), VIDIOC_QUERY_EXT_CTRL, &ctrl) == -1) {
  938. if (errno != EINVAL) {
  939. errno_printerr("VIDIOC_QUERY_EXT_CTRL");
  940. }
  941. break;
  942. }
  943. MPControl control = {
  944. .id = ctrl.id,
  945. .type = ctrl.type,
  946. .name = {},
  947. .min = ctrl.minimum,
  948. .max = ctrl.maximum,
  949. .step = ctrl.step,
  950. .default_value = ctrl.default_value,
  951. .flags = ctrl.flags,
  952. .element_size = ctrl.elem_size,
  953. .element_count = ctrl.elems,
  954. .dimensions_count = ctrl.nr_of_dims,
  955. .dimensions = {},
  956. };
  957. strcpy(control.name, ctrl.name);
  958. memcpy(control.dimensions, ctrl.dims,
  959. sizeof(uint32_t) * V4L2_CTRL_MAX_DIMS);
  960. MPControlList *new_item = malloc(sizeof(MPControlList));
  961. new_item->control = control;
  962. new_item->next = item;
  963. item = new_item;
  964. ctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
  965. }
  966. return item;
  967. }
  968. MPControl *
  969. mp_control_list_get(MPControlList *list)
  970. {
  971. g_return_val_if_fail(list, NULL);
  972. return &list->control;
  973. }
  974. MPControlList *
  975. mp_control_list_next(MPControlList *list)
  976. {
  977. g_return_val_if_fail(list, NULL);
  978. return list->next;
  979. }
  980. void
  981. mp_control_list_free(MPControlList *list)
  982. {
  983. while (list) {
  984. MPControlList *tmp = list;
  985. list = tmp->next;
  986. free(tmp);
  987. }
  988. }
  989. bool
  990. mp_camera_query_control(MPCamera *camera, uint32_t id, MPControl *control)
  991. {
  992. struct v4l2_query_ext_ctrl ctrl = {};
  993. ctrl.id = id;
  994. if (xioctl(control_fd(camera), VIDIOC_QUERY_EXT_CTRL, &ctrl) == -1) {
  995. if (errno != EINVAL) {
  996. errno_printerr("VIDIOC_QUERY_EXT_CTRL");
  997. }
  998. return false;
  999. }
  1000. if (control) {
  1001. control->id = ctrl.id;
  1002. control->type = ctrl.type;
  1003. strcpy(control->name, ctrl.name);
  1004. control->min = ctrl.minimum;
  1005. control->max = ctrl.maximum;
  1006. control->step = ctrl.step;
  1007. control->default_value = ctrl.default_value;
  1008. control->flags = ctrl.flags;
  1009. control->element_size = ctrl.elem_size;
  1010. control->element_count = ctrl.elems;
  1011. control->dimensions_count = ctrl.nr_of_dims;
  1012. memcpy(control->dimensions, ctrl.dims,
  1013. sizeof(uint32_t) * V4L2_CTRL_MAX_DIMS);
  1014. }
  1015. return true;
  1016. }
  1017. static bool
  1018. control_impl_int32(MPCamera *camera, uint32_t id, int request, int32_t *value)
  1019. {
  1020. struct v4l2_ext_control ctrl = {};
  1021. ctrl.id = id;
  1022. ctrl.value = *value;
  1023. struct v4l2_ext_controls ctrls = {
  1024. .ctrl_class = 0,
  1025. .which = V4L2_CTRL_WHICH_CUR_VAL,
  1026. .count = 1,
  1027. .controls = &ctrl,
  1028. };
  1029. if (xioctl(control_fd(camera), request, &ctrls) == -1) {
  1030. return false;
  1031. }
  1032. *value = ctrl.value;
  1033. return true;
  1034. }
  1035. bool
  1036. mp_camera_control_try_int32(MPCamera *camera, uint32_t id, int32_t *v)
  1037. {
  1038. return control_impl_int32(camera, id, VIDIOC_TRY_EXT_CTRLS, v);
  1039. }
  1040. bool
  1041. mp_camera_control_set_int32(MPCamera *camera, uint32_t id, int32_t v)
  1042. {
  1043. return control_impl_int32(camera, id, VIDIOC_S_EXT_CTRLS, &v);
  1044. }
  1045. int32_t
  1046. mp_camera_control_get_int32(MPCamera *camera, uint32_t id)
  1047. {
  1048. int32_t v = 0;
  1049. control_impl_int32(camera, id, VIDIOC_G_EXT_CTRLS, &v);
  1050. return v;
  1051. }
  1052. bool
  1053. mp_camera_control_try_boolean(MPCamera *camera, uint32_t id, bool *v)
  1054. {
  1055. int32_t value = *v;
  1056. bool s = control_impl_int32(camera, id, VIDIOC_TRY_EXT_CTRLS, &value);
  1057. *v = value;
  1058. return s;
  1059. }
  1060. bool
  1061. mp_camera_control_set_bool(MPCamera *camera, uint32_t id, bool v)
  1062. {
  1063. int32_t value = v;
  1064. return control_impl_int32(camera, id, VIDIOC_S_EXT_CTRLS, &value);
  1065. }
  1066. bool
  1067. mp_camera_control_get_bool(MPCamera *camera, uint32_t id)
  1068. {
  1069. int32_t v = false;
  1070. control_impl_int32(camera, id, VIDIOC_G_EXT_CTRLS, &v);
  1071. return v;
  1072. }