pipeline.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. uint16_t source_index = 0, sink_index = 0;
  96. for (int j = 0; j < topology.num_pads; j++) {
  97. if (pads[j].id == links[i].source_id) {
  98. source_entity = pads[j].entity_id;
  99. source_index = pads[j].index;
  100. } else if (pads[j].id == links[i].sink_id) {
  101. sink_entity = pads[j].entity_id;
  102. sink_index = pads[j].index;
  103. }
  104. }
  105. setup_link(camera, source_entity, sink_entity, source_index, sink_index, 0);
  106. }
  107. }
  108. return 0;
  109. }
  110. int
  111. libmegapixels_open(libmegapixels_camera *camera)
  112. {
  113. if (camera->media_fd != 0) {
  114. log_error("Camera already opened\n");
  115. return -1;
  116. }
  117. if (camera->sensor_fd != 0) {
  118. log_error("Sensor already opened\n");
  119. return -1;
  120. }
  121. if (camera->video_fd != 0) {
  122. log_error("Bridge already opened\n");
  123. return -1;
  124. }
  125. if (camera->media_path) {
  126. camera->media_fd = open(camera->media_path, O_RDWR);
  127. if (camera->media_fd < 0) {
  128. log_error("Could not open %s: %s\n", camera->media_path, strerror(errno));
  129. return -1;
  130. }
  131. }
  132. if (camera->sensor_path) {
  133. camera->sensor_fd = open(camera->sensor_path, O_RDWR);
  134. if (camera->sensor_fd < 0) {
  135. log_error("Could not open %s: %s\n", camera->sensor_path, strerror(errno));
  136. return -1;
  137. }
  138. }
  139. camera->video_fd = open(camera->video_path, O_RDWR);
  140. if (camera->video_fd < 0) {
  141. log_error("Could not open %s: %s\n", camera->video_path, strerror(errno));
  142. return -1;
  143. }
  144. // If this is an UVC camera the sensor _is_ the video device
  145. if (camera->sensor_fd == 0) {
  146. camera->sensor_fd = camera->video_fd;
  147. }
  148. if (camera->media_fd > 0) {
  149. int ret = load_entity_ids(camera);
  150. if (ret < 0) {
  151. return ret;
  152. }
  153. }
  154. return 0;
  155. }
  156. void
  157. libmegapixels_close(libmegapixels_camera *camera)
  158. {
  159. int uvc = 0;
  160. if (camera->sensor_fd != 0 && camera->sensor_fd == camera->video_fd) {
  161. uvc = 1;
  162. }
  163. if (camera->media_fd != 0) {
  164. close(camera->media_fd);
  165. camera->media_fd = 0;
  166. }
  167. if (camera->sensor_fd != 0) {
  168. close(camera->sensor_fd);
  169. camera->sensor_fd = 0;
  170. if (uvc) {
  171. camera->video_fd = 0;
  172. }
  173. }
  174. if (camera->video_fd != 0) {
  175. close(camera->video_fd);
  176. camera->video_fd = 0;
  177. }
  178. for (int i = 0; i < camera->num_handles; i++) {
  179. if (camera->handles[i]->fd != 0) {
  180. close(camera->handles[i]->fd);
  181. camera->handles[i]->fd = 0;
  182. }
  183. }
  184. }
  185. unsigned int
  186. libmegapixels_select_mode(libmegapixels_camera *camera, libmegapixels_mode *mode, struct v4l2_format *format)
  187. {
  188. assert(camera->video_fd != 0);
  189. assert(camera->sensor_fd != 0);
  190. char modename[32] = {0};
  191. mode_snprintf(modename, 31, mode);
  192. log_debug("Setting '%s' mode to [%s]\n", camera->name, modename);
  193. for (int i = 0; i < mode->num_cmds; i++) {
  194. libmegapixels_cmd *cmd = mode->cmds[i];
  195. struct v4l2_subdev_format subdev_fmt = {};
  196. struct v4l2_subdev_crop subdev_crop = {};
  197. struct v4l2_subdev_frame_interval subdev_ival = {};
  198. int found = 0;
  199. libmegapixels_subdev *sd;
  200. switch (cmd->type) {
  201. case LIBMEGAPIXELS_CMD_LINK:
  202. log_debug(" Link %s:%d -> %s:%d\n", cmd->entity_from, cmd->pad_from, cmd->entity_to, cmd->pad_to);
  203. if (setup_link(camera, cmd->entity_from_id, cmd->entity_to_id, cmd->pad_from, cmd->pad_to, 1) != 0) {
  204. log_error("Could not link %d -> %d [%s -> %s] \n", cmd->entity_from_id, cmd->entity_to_id,
  205. cmd->entity_from,
  206. cmd->entity_to);
  207. }
  208. break;
  209. case LIBMEGAPIXELS_CMD_MODE:
  210. log_debug(" Mode %s:%d [%dx%d %s]\n", cmd->entity_from, cmd->pad_from, cmd->width, cmd->height,
  211. libmegapixels_format_name(cmd->format));
  212. found = 0;
  213. for (int h = 0; h < camera->num_handles; h++) {
  214. if (camera->handles[h]->entity_id == cmd->entity_from_id) {
  215. sd = camera->handles[h];
  216. found++;
  217. }
  218. }
  219. if (found != 1) {
  220. log_error("Could not find handle for entity\n");
  221. break;
  222. }
  223. if (sd->fd == 0) {
  224. sd->fd = open(sd->path, O_RDWR);
  225. if (sd->fd < 0) {
  226. log_error("Could not open %s\n", sd->path);
  227. break;
  228. }
  229. }
  230. subdev_fmt.pad = cmd->pad_from;
  231. subdev_fmt.which = V4L2_SUBDEV_FORMAT_TRY;
  232. subdev_fmt.format.width = cmd->width;
  233. subdev_fmt.format.height = cmd->height;
  234. subdev_fmt.format.code = libmegapixels_format_to_media_busfmt(cmd->format);
  235. subdev_fmt.format.field = V4L2_FIELD_ANY;
  236. if (xioctl(sd->fd, VIDIOC_SUBDEV_S_FMT, &subdev_fmt) == -1) {
  237. log_error("Could not try mode on %s:%d: %s\n", cmd->entity_from, cmd->pad_from, strerror(errno));
  238. }
  239. if (subdev_fmt.format.width != cmd->width || subdev_fmt.format.height != cmd->height) {
  240. log_error("Driver rejected resolution try: %dx%d, changed to %dx%d\n", cmd->width, cmd->height,
  241. subdev_fmt.format.width, subdev_fmt.format.height);
  242. // PinePhone Pro currently has a bug where the try returns width 800, height 600, for rkisp1_isp:2, but the non-try succeeds
  243. if (
  244. strcmp(cmd->entity_from, "rkisp1_isp") == 0 && cmd->pad_from == 2
  245. ) {
  246. subdev_fmt.format.width = cmd->width;
  247. subdev_fmt.format.height = cmd->height;
  248. } else {
  249. break;
  250. }
  251. }
  252. if (subdev_fmt.format.code != libmegapixels_format_to_media_busfmt(cmd->format)) {
  253. log_error("Driver rejected pixfmt try: 0x%x\n", subdev_fmt.format.code);
  254. break;
  255. }
  256. subdev_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  257. if (xioctl(sd->fd, VIDIOC_SUBDEV_S_FMT, &subdev_fmt) == -1) {
  258. log_error("Could not set mode on %s:%d: %s\n", cmd->entity_from, cmd->pad_from, strerror(errno));
  259. }
  260. // There seem to be drivers that don't reject resolutions in V4L2_SUBDEV_FORMAT_TRY mode but
  261. // do silently change the resolution when doing the V4L2_SUBDEV_FORMAT_ACTIVE bit. So check
  262. // again since V4L2 is the wild west.
  263. if (subdev_fmt.format.width != cmd->width || subdev_fmt.format.height != cmd->height) {
  264. log_error("Driver rejected resolution %dx%d, changed to %dx%d\n", cmd->width, cmd->height,
  265. subdev_fmt.format.width, subdev_fmt.format.height);
  266. break;
  267. }
  268. if (subdev_fmt.format.code != libmegapixels_format_to_media_busfmt(cmd->format)) {
  269. log_error("Driver rejected pixfmt: 0x%x\n", subdev_fmt.format.code);
  270. break;
  271. }
  272. break;
  273. case LIBMEGAPIXELS_CMD_INTERVAL:
  274. subdev_ival.pad = cmd->pad_from;
  275. struct v4l2_fract ival;
  276. ival.numerator = 1;
  277. ival.denominator = cmd->rate;
  278. subdev_ival.interval = ival;
  279. log_debug(" Rate %s:%d [%d]\n", cmd->entity_from, cmd->pad_from, cmd->rate);
  280. found = 0;
  281. for (int h = 0; h < camera->num_handles; h++) {
  282. if (camera->handles[h]->entity_id == cmd->entity_from_id) {
  283. sd = camera->handles[h];
  284. found++;
  285. }
  286. }
  287. if (found != 1) {
  288. log_error("Could not find handle for entity\n");
  289. break;
  290. }
  291. if (sd->fd == 0) {
  292. sd->fd = open(sd->path, O_RDWR);
  293. if (sd->fd < 0) {
  294. log_error("Could not open %s\n", sd->path);
  295. break;
  296. }
  297. }
  298. if (xioctl(sd->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &subdev_ival) == -1) {
  299. log_error("Could not set rate on %s:%d: %s\n", cmd->entity_from, cmd->pad_from, strerror(errno));
  300. }
  301. break;
  302. case LIBMEGAPIXELS_CMD_CROP:
  303. subdev_crop.pad = cmd->pad_from;
  304. subdev_crop.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  305. subdev_crop.rect.top = cmd->top;
  306. subdev_crop.rect.left = cmd->left;
  307. subdev_crop.rect.width = cmd->width;
  308. subdev_crop.rect.height = cmd->height;
  309. log_debug(" Crop %s:%d [%dx%d+%d+%d]\n", cmd->entity_from, cmd->pad_from, cmd->width, cmd->height,
  310. cmd->top, cmd->left);
  311. found = 0;
  312. for (int h = 0; h < camera->num_handles; h++) {
  313. if (camera->handles[h]->entity_id == cmd->entity_from_id) {
  314. sd = camera->handles[h];
  315. found++;
  316. }
  317. }
  318. if (found != 1) {
  319. log_error("Could not find handle for entity\n");
  320. break;
  321. }
  322. if (sd->fd == 0) {
  323. sd->fd = open(sd->path, O_RDWR);
  324. if (sd->fd < 0) {
  325. log_error("Could not open %s\n", sd->path);
  326. break;
  327. }
  328. }
  329. if (xioctl(sd->fd, VIDIOC_SUBDEV_S_CROP, &subdev_crop) == -1) {
  330. log_error("Could not set crop on entity: %s\n", strerror(errno));
  331. }
  332. break;
  333. }
  334. }
  335. struct v4l2_capability cap;
  336. if (xioctl(camera->video_fd, VIDIOC_QUERYCAP, &cap) == -1) {
  337. log_error("Could not query %s: %s\n", camera->video_path, strerror(errno));
  338. return 0;
  339. }
  340. if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
  341. format->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  342. format->fmt.pix_mp.width = mode->width;
  343. format->fmt.pix_mp.height = mode->height;
  344. format->fmt.pix_mp.pixelformat = mode->v4l_pixfmt;
  345. format->fmt.pix_mp.field = V4L2_FIELD_ANY;
  346. } else {
  347. format->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  348. format->fmt.pix.width = mode->width;
  349. format->fmt.pix.height = mode->height;
  350. format->fmt.pix.pixelformat = mode->v4l_pixfmt;
  351. format->fmt.pix.field = V4L2_FIELD_ANY;
  352. }
  353. if (xioctl(camera->video_fd, VIDIOC_S_FMT, format) == -1) {
  354. log_error("Could not set mode on %s: %s\n", camera->video_path, strerror(errno));
  355. return 0;
  356. }
  357. if (xioctl(camera->video_fd, VIDIOC_G_FMT, format) == -1) {
  358. log_error("Could not get mode of %s: %s\n", camera->video_path, strerror(errno));
  359. return 0;
  360. }
  361. if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
  362. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  363. log_error("Capture driver changed capture type\n");
  364. return 0;
  365. }
  366. if (format->fmt.pix_mp.pixelformat != mode->v4l_pixfmt) {
  367. log_error("Capture driver changed pixfmt to %c%c%c%c\n",
  368. format->fmt.pix_mp.pixelformat & 0xff,
  369. format->fmt.pix_mp.pixelformat >> 8 & 0xff,
  370. format->fmt.pix_mp.pixelformat >> 16 & 0xff,
  371. format->fmt.pix_mp.pixelformat >> 24 & 0xff);
  372. return 0;
  373. }
  374. } else {
  375. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  376. log_error("Capture driver changed capture type\n");
  377. return 0;
  378. }
  379. if (format->fmt.pix.pixelformat != mode->v4l_pixfmt) {
  380. log_error("Capture driver changed pixfmt to %c%c%c%c\n",
  381. format->fmt.pix.pixelformat & 0xff,
  382. format->fmt.pix.pixelformat >> 8 & 0xff,
  383. format->fmt.pix.pixelformat >> 16 & 0xff,
  384. format->fmt.pix.pixelformat >> 24 & 0xff);
  385. return 0;
  386. }
  387. }
  388. camera->current_mode = mode;
  389. return 1;
  390. }