pipeline.c 13 KB

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