camera.c 30 KB

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