VideoIn.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. This class has been implemented based on
  3. yavta -- Yet Another V4L2 Test Application written by:
  4. Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <AP_HAL/AP_HAL.h>
  17. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BEBOP
  18. #include "VideoIn.h"
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <linux/videodev2.h>
  22. #include <pthread.h>
  23. #include <sched.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/mman.h>
  29. #include <sys/select.h>
  30. #include <sys/stat.h>
  31. #include <sys/time.h>
  32. #include <time.h>
  33. #include <unistd.h>
  34. extern const AP_HAL::HAL& hal;
  35. using namespace Linux;
  36. bool VideoIn::get_frame(Frame &frame)
  37. {
  38. if (!_streaming) {
  39. if (!_set_streaming(true)) {
  40. AP_HAL::panic("couldn't start streaming\n");
  41. return false;
  42. }
  43. _streaming = true;
  44. }
  45. return _dequeue_frame(frame);
  46. }
  47. void VideoIn::put_frame(Frame &frame)
  48. {
  49. _queue_buffer((uint32_t)frame.buf_index);
  50. }
  51. bool VideoIn::open_device(const char *device_path, uint32_t memtype)
  52. {
  53. struct v4l2_capability cap;
  54. int ret;
  55. _fd = -1;
  56. _buffers = nullptr;
  57. _fd = open(device_path, O_RDWR|O_CLOEXEC);
  58. _memtype = memtype;
  59. if (_fd < 0) {
  60. hal.console->printf("Error opening device %s: %s (%d).\n",
  61. device_path,
  62. strerror(errno), errno);
  63. return false;
  64. }
  65. memset(&cap, 0, sizeof cap);
  66. ret = ioctl(_fd, VIDIOC_QUERYCAP, &cap);
  67. if (ret < 0) {
  68. hal.console->printf("Error querying caps\n");
  69. return false;
  70. }
  71. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  72. hal.console->printf("Error opening device %s: is not a video capture device\n",
  73. device_path);
  74. close(_fd);
  75. _fd = -1;
  76. return false;
  77. }
  78. return true;
  79. }
  80. bool VideoIn::allocate_buffers(uint32_t nbufs)
  81. {
  82. struct v4l2_requestbuffers rb;
  83. struct v4l2_buffer buf;
  84. struct buffer *buffers;
  85. unsigned int i;
  86. int ret;
  87. memset(&rb, 0, sizeof rb);
  88. rb.count = nbufs;
  89. rb.type = (v4l2_buf_type) V4L2_CAP_VIDEO_CAPTURE;
  90. rb.memory = (v4l2_memory) _memtype;
  91. ret = ioctl(_fd, VIDIOC_REQBUFS, &rb);
  92. if (ret < 0) {
  93. printf("Unable to request buffers: %s (%d).\n", strerror(errno), errno);
  94. return ret;
  95. }
  96. buffers = (struct buffer *)calloc(rb.count, sizeof buffers[0]);
  97. if (buffers == nullptr) {
  98. hal.console->printf("Unable to allocate buffers\n");
  99. return false;
  100. }
  101. for (i=0; i < rb.count; i++) {
  102. memset(&buf, 0, sizeof(buf));
  103. buf.index = i;
  104. buf.type = rb.type;
  105. buf.memory = rb.memory;
  106. ret = ioctl(_fd, VIDIOC_QUERYBUF, &buf);
  107. if (ret < 0) {
  108. hal.console->printf("Unable to query buffer %u: %s(%d).\n", i,
  109. strerror(errno), errno);
  110. return false;
  111. }
  112. switch (_memtype) {
  113. case V4L2_MEMORY_MMAP:
  114. buffers[i].mem = mmap(0, buf.length, PROT_READ | PROT_WRITE,
  115. MAP_SHARED, _fd, buf.m.offset);
  116. if (buffers[i].mem == MAP_FAILED) {
  117. hal.console->printf("Unable to map buffer %u: %s (%d)\n", i,
  118. strerror(errno), errno);
  119. return false;
  120. }
  121. buffers[i].size = buf.length;
  122. break;
  123. case V4L2_MEMORY_USERPTR:
  124. ret = posix_memalign(&buffers[i].mem, getpagesize(), buf.length);
  125. if (ret < 0) {
  126. hal.console->printf("Unable to allocate buffer %u (%d)\n", i,
  127. ret);
  128. return false;
  129. }
  130. buffers[i].size = buf.length;
  131. break;
  132. default:
  133. return false;
  134. }
  135. }
  136. _nbufs = rb.count;
  137. _buffers = buffers;
  138. return true;
  139. }
  140. void VideoIn::get_pixel_formats(std::vector<uint32_t> *formats)
  141. {
  142. struct v4l2_fmtdesc fmtdesc;
  143. memset(&fmtdesc, 0, sizeof fmtdesc);
  144. fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  145. while (ioctl(_fd, VIDIOC_ENUM_FMT, &fmtdesc) == 0) {
  146. formats->insert(formats->begin(), fmtdesc.pixelformat);
  147. fmtdesc.index++;
  148. }
  149. }
  150. bool VideoIn::set_format(uint32_t *width, uint32_t *height, uint32_t *format,
  151. uint32_t *bytesperline, uint32_t *sizeimage)
  152. {
  153. struct v4l2_format fmt;
  154. int ret;
  155. memset(&fmt, 0, sizeof fmt);
  156. fmt.type = (v4l2_buf_type) V4L2_CAP_VIDEO_CAPTURE;
  157. fmt.fmt.pix.width = *width;
  158. fmt.fmt.pix.height = *height;
  159. fmt.fmt.pix.pixelformat = *format;
  160. fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  161. ret = ioctl(_fd, VIDIOC_S_FMT, &fmt);
  162. if (ret < 0) {
  163. hal.console->printf("VideoIn: unable to set format: %s (%d).\n",
  164. strerror(errno), errno);
  165. return false;
  166. }
  167. /* warn if format different from the one that was supposed
  168. * to be set
  169. */
  170. if ((fmt.fmt.pix.pixelformat != *format) ||
  171. (fmt.fmt.pix.width != *width) ||
  172. (fmt.fmt.pix.height != *height)) {
  173. hal.console->printf("format set to (%08x)"
  174. "%ux%u buffer size %u field : %d\n",
  175. fmt.fmt.pix.pixelformat, fmt.fmt.pix.width,
  176. fmt.fmt.pix.height, fmt.fmt.pix.sizeimage,
  177. fmt.fmt.pix.field);
  178. }
  179. *width = fmt.fmt.pix.width;
  180. *height = fmt.fmt.pix.height;
  181. *format = fmt.fmt.pix.pixelformat;
  182. *bytesperline = fmt.fmt.pix.bytesperline;
  183. *sizeimage = fmt.fmt.pix.sizeimage;
  184. return true;
  185. }
  186. bool VideoIn::set_crop(uint32_t left, uint32_t top,
  187. uint32_t width, uint32_t height)
  188. {
  189. struct v4l2_crop crop;
  190. int ret;
  191. memset(&crop, 0, sizeof crop);
  192. crop.c.top = top;
  193. crop.c.left = left;
  194. crop.c.width = width;
  195. crop.c.height = height;
  196. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  197. ret = ioctl(_fd, VIDIOC_S_CROP, &crop);
  198. if (ret < 0) {
  199. hal.console->printf("VideoIn: unable to set crop: %s (%d).\n",
  200. strerror(errno), errno);
  201. return false;
  202. }
  203. return true;
  204. }
  205. void VideoIn::prepare_capture()
  206. {
  207. unsigned int i;
  208. /* Queue the buffers. */
  209. for (i = 0; i < _nbufs; ++i) {
  210. _queue_buffer(i);
  211. }
  212. }
  213. void VideoIn::shrink_8bpp(uint8_t *buffer, uint8_t *new_buffer,
  214. uint32_t width, uint32_t height, uint32_t left,
  215. uint32_t selection_width, uint32_t top,
  216. uint32_t selection_height, uint32_t fx, uint32_t fy)
  217. {
  218. uint32_t i, j, k, kk, px, block_x, block_y, block_position;
  219. uint32_t out_width = selection_width / fx;
  220. uint32_t out_height = selection_height / fy;
  221. uint32_t width_per_fy = width * fy;
  222. uint32_t fx_fy = fx * fy;
  223. uint32_t width_sum, out_width_sum = 0;
  224. /* selection offset */
  225. block_y = top * width;
  226. block_position = left + block_y;
  227. for (i = 0; i < out_height; i++) {
  228. block_x = left;
  229. for (j = 0; j < out_width; j++) {
  230. px = 0;
  231. width_sum = 0;
  232. for(k = 0; k < fy; k++) {
  233. for(kk = 0; kk < fx; kk++) {
  234. px += buffer[block_position + kk + width_sum];
  235. }
  236. width_sum += width;
  237. }
  238. new_buffer[j + out_width_sum] = px / (fx_fy);
  239. block_x += fx;
  240. block_position = block_x + block_y;
  241. }
  242. block_y += width_per_fy;
  243. out_width_sum += out_width;
  244. }
  245. }
  246. void VideoIn::crop_8bpp(uint8_t *buffer, uint8_t *new_buffer,
  247. uint32_t width, uint32_t left, uint32_t crop_width,
  248. uint32_t top, uint32_t crop_height)
  249. {
  250. uint32_t crop_x = left + crop_width;
  251. uint32_t crop_y = top + crop_height;
  252. uint32_t buffer_index = top * width;
  253. uint32_t new_buffer_index = 0;
  254. for (uint32_t j = top; j < crop_y; j++) {
  255. for (uint32_t i = left; i < crop_x; i++) {
  256. new_buffer[i - left + new_buffer_index] = buffer[i + buffer_index];
  257. }
  258. buffer_index += width;
  259. new_buffer_index += crop_width;
  260. }
  261. }
  262. void VideoIn::yuyv_to_grey(uint8_t *buffer, uint32_t buffer_size,
  263. uint8_t *new_buffer)
  264. {
  265. uint32_t new_buffer_position = 0;
  266. for (uint32_t i = 0; i < buffer_size; i += 2) {
  267. new_buffer[new_buffer_position] = buffer[i];
  268. new_buffer_position++;
  269. }
  270. }
  271. uint32_t VideoIn::_timeval_to_us(struct timeval& tv)
  272. {
  273. return (1.0e6 * tv.tv_sec + tv.tv_usec);
  274. }
  275. void VideoIn::_queue_buffer(int index)
  276. {
  277. int ret;
  278. struct v4l2_buffer buf;
  279. memset(&buf, 0, sizeof buf);
  280. buf.index = index;
  281. buf.type = (v4l2_buf_type) V4L2_CAP_VIDEO_CAPTURE;
  282. buf.memory = (v4l2_memory) _memtype;
  283. buf.length = _buffers[index].size;
  284. if (_memtype == V4L2_MEMORY_USERPTR) {
  285. buf.m.userptr = (unsigned long) _buffers[index].mem;
  286. }
  287. ret = ioctl(_fd, VIDIOC_QBUF, &buf);
  288. if (ret < 0) {
  289. hal.console->printf("Unable to queue buffer : %s (%d).\n",
  290. strerror(errno), errno);
  291. }
  292. }
  293. bool VideoIn::_set_streaming(bool enable)
  294. {
  295. int type = V4L2_CAP_VIDEO_CAPTURE;
  296. int ret;
  297. ret = ioctl(_fd, enable ? VIDIOC_STREAMON : VIDIOC_STREAMOFF, &type);
  298. if (ret < 0) {
  299. printf("Unable to %s streaming: %s (%d).\n",
  300. enable ? "start" : "stop", strerror(errno), errno);
  301. return false;
  302. }
  303. return true;
  304. }
  305. bool VideoIn::_dequeue_frame(Frame &frame)
  306. {
  307. struct v4l2_buffer buf;
  308. int ret;
  309. /* Dequeue a buffer. */
  310. memset(&buf, 0, sizeof buf);
  311. buf.type = (v4l2_buf_type) V4L2_CAP_VIDEO_CAPTURE;
  312. buf.memory = (v4l2_memory) _memtype;
  313. ret = ioctl(_fd, VIDIOC_DQBUF, &buf);
  314. if (ret < 0) {
  315. if (errno != EIO) {
  316. hal.console->printf("Unable to dequeue buffer: %s (%d).\n",
  317. strerror(errno), errno);
  318. return false;
  319. }
  320. buf.type = (v4l2_buf_type) V4L2_CAP_VIDEO_CAPTURE;
  321. buf.memory = (v4l2_memory) _memtype;
  322. if (_memtype == V4L2_MEMORY_USERPTR) {
  323. buf.m.userptr = (unsigned long)_buffers[buf.index].mem;
  324. }
  325. }
  326. frame.data = _buffers[buf.index].mem;
  327. frame.buf_index = buf.index;
  328. frame.timestamp = _timeval_to_us(buf.timestamp);
  329. frame.sequence = buf.sequence;
  330. return true;
  331. }
  332. #endif