pipeline.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include <fcntl.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <linux/media.h>
  5. #include <linux/v4l2-subdev.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include "libmegapixels.h"
  11. #include "log.h"
  12. #include "util.h"
  13. #include "mode.h"
  14. int
  15. setup_link(libmegapixels_camera *camera, uint32_t source_entity_id, uint32_t sink_entity_id,
  16. uint16_t source_index, uint16_t sink_index, int enabled)
  17. {
  18. struct media_link_desc link = {};
  19. link.flags = (enabled > 0) ? MEDIA_LNK_FL_ENABLED : 0;
  20. link.source.entity = source_entity_id;
  21. link.source.index = source_index;
  22. link.sink.entity = sink_entity_id;
  23. link.sink.index = sink_index;
  24. if (xioctl(camera->media_fd, MEDIA_IOC_SETUP_LINK, &link) == -1) {
  25. return -1;
  26. }
  27. return 0;
  28. }
  29. int
  30. load_entity_ids(libmegapixels_camera *camera)
  31. {
  32. // This media device matches on model or driver, scan the entities for the sensor
  33. struct media_v2_topology topology = {0};
  34. if (xioctl(camera->media_fd, MEDIA_IOC_G_TOPOLOGY, &topology) == -1 ||
  35. topology.num_entities == 0) {
  36. close(camera->media_fd);
  37. return -1;
  38. }
  39. struct media_v2_entity *entities = calloc(topology.num_entities, sizeof(struct media_v2_entity));
  40. struct media_v2_interface *interfaces = calloc(topology.num_interfaces, sizeof(struct media_v2_interface));
  41. struct media_v2_pad *pads = calloc(topology.num_pads, sizeof(struct media_v2_pad));
  42. struct media_v2_link *links = calloc(topology.num_links, sizeof(struct media_v2_link));
  43. topology.ptr_entities = (uint64_t) entities;
  44. topology.ptr_interfaces = (uint64_t) interfaces;
  45. topology.ptr_pads = (uint64_t) pads;
  46. topology.ptr_links = (uint64_t) links;
  47. if (xioctl(camera->media_fd, MEDIA_IOC_G_TOPOLOGY, &topology) == -1) {
  48. close(camera->media_fd);
  49. return -1;
  50. }
  51. for (int i = 0; i < camera->num_modes; i++) {
  52. libmegapixels_mode *mode = camera->modes[i];
  53. for (int j = 0; j < mode->num_cmds; j++) {
  54. libmegapixels_cmd *cmd = mode->cmds[j];
  55. if (cmd->type == LIBMEGAPIXELS_CMD_LINK) {
  56. int found_from = 0;
  57. int found_to = 0;
  58. for (int k = 0; k < topology.num_entities; k++) {
  59. if (strncmp(entities[k].name, cmd->entity_from, strlen(cmd->entity_from)) == 0) {
  60. cmd->entity_from_id = entities[k].id;
  61. found_from++;
  62. }
  63. if (strncmp(entities[k].name, cmd->entity_to, strlen(cmd->entity_to)) == 0) {
  64. cmd->entity_to_id = entities[k].id;
  65. found_to++;
  66. }
  67. }
  68. if (found_from != 1) {
  69. log_error("Could not find entity '%s'\n", cmd->entity_from);
  70. return -1;
  71. }
  72. if (found_to != 1) {
  73. log_error("Could not find entity '%s'\n", cmd->entity_to);
  74. return -1;
  75. }
  76. } else if (cmd->type == LIBMEGAPIXELS_CMD_MODE || cmd->type == LIBMEGAPIXELS_CMD_CROP ||
  77. cmd->type == LIBMEGAPIXELS_CMD_INTERVAL) {
  78. int found = 0;
  79. for (int k = 0; k < topology.num_entities; k++) {
  80. if (strncmp(entities[k].name, cmd->entity_from, strlen(cmd->entity_from)) == 0) {
  81. cmd->entity_from_id = entities[k].id;
  82. found++;
  83. break;
  84. }
  85. }
  86. if (found != 1) {
  87. log_error("Could not find entity '%s'\n", cmd->entity_from);
  88. }
  89. }
  90. }
  91. }
  92. for (int i = 0; i < topology.num_links; i++) {
  93. if (links[i].flags & MEDIA_LNK_FL_ENABLED && !(links[i].flags & MEDIA_LNK_FL_IMMUTABLE)) {
  94. uint32_t source_entity = 0, sink_entity = 0;
  95. for (int j = 0; j < topology.num_pads; j++) {
  96. if (pads[j].id == links[i].source_id) {
  97. source_entity = pads[j].entity_id;
  98. } else if (pads[j].id == links[i].sink_id) {
  99. sink_entity = pads[j].entity_id;
  100. }
  101. }
  102. setup_link(camera, source_entity, sink_entity, 0, 0, 0);
  103. }
  104. }
  105. return 0;
  106. }
  107. int
  108. libmegapixels_open(libmegapixels_camera *camera)
  109. {
  110. if (camera->media_fd != 0) {
  111. log_error("Camera already opened\n");
  112. return -1;
  113. }
  114. if (camera->sensor_fd != 0) {
  115. log_error("Sensor already opened\n");
  116. return -1;
  117. }
  118. if (camera->video_fd != 0) {
  119. log_error("Bridge already opened\n");
  120. return -1;
  121. }
  122. if (camera->media_path) {
  123. camera->media_fd = open(camera->media_path, O_RDWR);
  124. if (camera->media_fd < 0) {
  125. log_error("Could not open %s: %s\n", camera->media_path, strerror(errno));
  126. return -1;
  127. }
  128. }
  129. if (camera->sensor_path) {
  130. camera->sensor_fd = open(camera->sensor_path, O_RDWR);
  131. if (camera->sensor_fd < 0) {
  132. log_error("Could not open %s: %s\n", camera->sensor_path, strerror(errno));
  133. return -1;
  134. }
  135. }
  136. camera->video_fd = open(camera->video_path, O_RDWR);
  137. if (camera->video_fd < 0) {
  138. log_error("Could not open %s: %s\n", camera->video_path, strerror(errno));
  139. return -1;
  140. }
  141. // If this is an UVC camera the sensor _is_ the video device
  142. if (camera->sensor_fd == 0) {
  143. camera->sensor_fd = camera->video_fd;
  144. }
  145. if (camera->media_fd > 0) {
  146. int ret = load_entity_ids(camera);
  147. if (ret < 0) {
  148. return ret;
  149. }
  150. }
  151. return 0;
  152. }
  153. void
  154. libmegapixels_close(libmegapixels_camera *camera)
  155. {
  156. int uvc = 0;
  157. if (camera->sensor_fd != 0 && camera->sensor_fd == camera->video_fd) {
  158. uvc = 1;
  159. }
  160. if (camera->media_fd != 0) {
  161. close(camera->media_fd);
  162. camera->media_fd = 0;
  163. }
  164. if (camera->sensor_fd != 0) {
  165. close(camera->sensor_fd);
  166. camera->sensor_fd = 0;
  167. if (uvc) {
  168. camera->video_fd = 0;
  169. }
  170. }
  171. if (camera->video_fd != 0) {
  172. close(camera->video_fd);
  173. camera->video_fd = 0;
  174. }
  175. for (int i = 0; i < camera->num_handles; i++) {
  176. if (camera->handles[i]->fd != 0) {
  177. close(camera->handles[i]->fd);
  178. camera->handles[i]->fd = 0;
  179. }
  180. }
  181. }
  182. unsigned int
  183. libmegapixels_select_mode(libmegapixels_camera *camera, libmegapixels_mode *mode, struct v4l2_format *format)
  184. {
  185. assert(camera->video_fd != 0);
  186. assert(camera->sensor_fd != 0);
  187. char modename[32] = {0};
  188. mode_snprintf(modename, 31, mode);
  189. log_debug("Setting '%s' mode to [%s]\n", camera->name, modename);
  190. for (int i = 0; i < mode->num_cmds; i++) {
  191. libmegapixels_cmd *cmd = mode->cmds[i];
  192. struct v4l2_subdev_format subdev_fmt = {};
  193. struct v4l2_subdev_crop subdev_crop = {};
  194. struct v4l2_subdev_frame_interval subdev_ival = {};
  195. int found = 0;
  196. libmegapixels_subdev *sd;
  197. switch (cmd->type) {
  198. case LIBMEGAPIXELS_CMD_LINK:
  199. log_debug(" Link %s:%d -> %s:%d\n", cmd->entity_from, cmd->pad_from, cmd->entity_to, cmd->pad_to);
  200. if (setup_link(camera, cmd->entity_from_id, cmd->entity_to_id, cmd->pad_from, cmd->pad_to, 1) != 0) {
  201. log_error("Could not link %d -> %d [%s -> %s] \n", cmd->entity_from_id, cmd->entity_to_id,
  202. cmd->entity_from,
  203. cmd->entity_to);
  204. }
  205. break;
  206. case LIBMEGAPIXELS_CMD_MODE:
  207. log_debug(" Mode %s:%d [%dx%d %s]\n", cmd->entity_from, cmd->pad_from, cmd->width, cmd->height,
  208. libmegapixels_format_name(cmd->format));
  209. found = 0;
  210. for (int h = 0; h < camera->num_handles; h++) {
  211. if (camera->handles[h]->entity_id == cmd->entity_from_id) {
  212. sd = camera->handles[h];
  213. found++;
  214. }
  215. }
  216. if (found != 1) {
  217. log_error("Could not find handle for entity\n");
  218. break;
  219. }
  220. if (sd->fd == 0) {
  221. sd->fd = open(sd->path, O_RDWR);
  222. if (sd->fd < 0) {
  223. log_error("Could not open %s\n", sd->path);
  224. break;
  225. }
  226. }
  227. subdev_fmt.pad = cmd->pad_from;
  228. subdev_fmt.which = V4L2_SUBDEV_FORMAT_TRY;
  229. subdev_fmt.format.width = cmd->width;
  230. subdev_fmt.format.height = cmd->height;
  231. subdev_fmt.format.code = libmegapixels_format_to_media_busfmt(cmd->format);
  232. subdev_fmt.format.field = V4L2_FIELD_ANY;
  233. if (xioctl(sd->fd, VIDIOC_SUBDEV_S_FMT, &subdev_fmt) == -1) {
  234. log_error("Could not try mode on %s:%d: %s\n", cmd->entity_from, cmd->pad_from, strerror(errno));
  235. }
  236. if (subdev_fmt.format.width != cmd->width || subdev_fmt.format.height != cmd->height) {
  237. log_error("Driver rejected resolution %dx%d, changed to %dx%d\n", cmd->width, cmd->height,
  238. subdev_fmt.format.width, subdev_fmt.format.height);
  239. break;
  240. }
  241. if (subdev_fmt.format.code != libmegapixels_format_to_media_busfmt(cmd->format)) {
  242. log_error("Driver rejected pixfmt try: %d\n", subdev_fmt.format.code);
  243. break;
  244. }
  245. subdev_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  246. if (xioctl(sd->fd, VIDIOC_SUBDEV_S_FMT, &subdev_fmt) == -1) {
  247. log_error("Could not set mode on %s:%d: %s\n", cmd->entity_from, cmd->pad_from, strerror(errno));
  248. }
  249. // There seem to be drivers that don't reject resolutions in V4L2_SUBDEV_FORMAT_TRY mode but
  250. // do silently change the resolution when doing the V4L2_SUBDEV_FORMAT_ACTIVE bit. So check
  251. // again since V4L2 is the wild west.
  252. if (subdev_fmt.format.width != cmd->width || subdev_fmt.format.height != cmd->height) {
  253. log_error("Driver rejected resolution %dx%d, changed to %dx%d\n", cmd->width, cmd->height,
  254. subdev_fmt.format.width, subdev_fmt.format.height);
  255. break;
  256. }
  257. if (subdev_fmt.format.code != libmegapixels_format_to_media_busfmt(cmd->format)) {
  258. log_error("Driver rejected pixfmt: %d\n", subdev_fmt.format.code);
  259. break;
  260. }
  261. break;
  262. case LIBMEGAPIXELS_CMD_INTERVAL:
  263. subdev_ival.pad = cmd->pad_from;
  264. struct v4l2_fract ival;
  265. ival.numerator = 1;
  266. ival.denominator = cmd->rate;
  267. subdev_ival.interval = ival;
  268. log_debug(" Rate %s:%d [%d]\n", cmd->entity_from, cmd->pad_from, cmd->rate);
  269. found = 0;
  270. for (int h = 0; h < camera->num_handles; h++) {
  271. if (camera->handles[h]->entity_id == cmd->entity_from_id) {
  272. sd = camera->handles[h];
  273. found++;
  274. }
  275. }
  276. if (found != 1) {
  277. log_error("Could not find handle for entity\n");
  278. break;
  279. }
  280. if (sd->fd == 0) {
  281. sd->fd = open(sd->path, O_RDWR);
  282. if (sd->fd < 0) {
  283. log_error("Could not open %s\n", sd->path);
  284. break;
  285. }
  286. }
  287. if (xioctl(sd->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &subdev_ival) == -1) {
  288. log_error("Could not set rate on %s:%d: %s\n", cmd->entity_from, cmd->pad_from, strerror(errno));
  289. }
  290. break;
  291. case LIBMEGAPIXELS_CMD_CROP:
  292. subdev_crop.pad = cmd->pad_from;
  293. subdev_crop.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  294. subdev_crop.rect.top = cmd->top;
  295. subdev_crop.rect.left = cmd->left;
  296. subdev_crop.rect.width = cmd->width;
  297. subdev_crop.rect.height = cmd->height;
  298. log_debug(" Crop %s:%d [%dx%d+%d+%d]\n", cmd->entity_from, cmd->pad_from, cmd->width, cmd->height,
  299. cmd->top, cmd->left);
  300. found = 0;
  301. for (int h = 0; h < camera->num_handles; h++) {
  302. if (camera->handles[h]->entity_id == cmd->entity_from_id) {
  303. sd = camera->handles[h];
  304. found++;
  305. }
  306. }
  307. if (found != 1) {
  308. log_error("Could not find handle for entity\n");
  309. break;
  310. }
  311. if (sd->fd == 0) {
  312. sd->fd = open(sd->path, O_RDWR);
  313. if (sd->fd < 0) {
  314. log_error("Could not open %s\n", sd->path);
  315. break;
  316. }
  317. }
  318. if (xioctl(sd->fd, VIDIOC_SUBDEV_S_CROP, &subdev_crop) == -1) {
  319. log_error("Could not set crop on entity: %s\n", strerror(errno));
  320. }
  321. break;
  322. }
  323. }
  324. struct v4l2_capability cap;
  325. if (xioctl(camera->video_fd, VIDIOC_QUERYCAP, &cap) == -1) {
  326. log_error("Could not query %s: %s\n", camera->video_path, strerror(errno));
  327. return 0;
  328. }
  329. if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
  330. format->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  331. format->fmt.pix_mp.width = mode->width;
  332. format->fmt.pix_mp.height = mode->height;
  333. format->fmt.pix_mp.pixelformat = mode->v4l_pixfmt;
  334. format->fmt.pix_mp.field = V4L2_FIELD_ANY;
  335. } else {
  336. format->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  337. format->fmt.pix.width = mode->width;
  338. format->fmt.pix.height = mode->height;
  339. format->fmt.pix.pixelformat = mode->v4l_pixfmt;
  340. format->fmt.pix.field = V4L2_FIELD_ANY;
  341. }
  342. if (xioctl(camera->video_fd, VIDIOC_S_FMT, format) == -1) {
  343. log_error("Could not set mode on %s: %s\n", camera->video_path, strerror(errno));
  344. return 0;
  345. }
  346. if (xioctl(camera->video_fd, VIDIOC_G_FMT, format) == -1) {
  347. log_error("Could not get mode of %s: %s\n", camera->video_path, strerror(errno));
  348. return 0;
  349. }
  350. if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
  351. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  352. log_error("Capture driver changed capture type\n");
  353. return 0;
  354. }
  355. if (format->fmt.pix_mp.pixelformat != mode->v4l_pixfmt) {
  356. log_error("Capture driver changed pixfmt to %c%c%c%c\n",
  357. format->fmt.pix_mp.pixelformat & 0xff,
  358. format->fmt.pix_mp.pixelformat >> 8 & 0xff,
  359. format->fmt.pix_mp.pixelformat >> 16 & 0xff,
  360. format->fmt.pix_mp.pixelformat >> 24 & 0xff);
  361. return 0;
  362. }
  363. } else {
  364. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  365. log_error("Capture driver changed capture type\n");
  366. return 0;
  367. }
  368. if (format->fmt.pix.pixelformat != mode->v4l_pixfmt) {
  369. log_error("Capture driver changed pixfmt to %c%c%c%c\n",
  370. format->fmt.pix.pixelformat & 0xff,
  371. format->fmt.pix.pixelformat >> 8 & 0xff,
  372. format->fmt.pix.pixelformat >> 16 & 0xff,
  373. format->fmt.pix.pixelformat >> 24 & 0xff);
  374. return 0;
  375. }
  376. }
  377. camera->current_mode = mode;
  378. return 1;
  379. }