2
0

v4l2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * Copyright (C) 2018 Paul Kocialkowski <paul.kocialkowski@bootlin.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  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. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/mman.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include <linux/media.h>
  29. #include <linux/videodev2.h>
  30. #include <mpeg2-ctrls.h>
  31. #include <h264-ctrls.h>
  32. #include <hevc-ctrls.h>
  33. #include "v4l2-request-test.h"
  34. #define SOURCE_SIZE_MAX (1024 * 1024)
  35. static bool type_is_output(unsigned int type)
  36. {
  37. switch (type) {
  38. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  39. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  40. return true;
  41. default:
  42. return false;
  43. }
  44. }
  45. static bool type_is_mplane(unsigned int type)
  46. {
  47. switch (type) {
  48. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  49. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  50. return true;
  51. default:
  52. return false;
  53. }
  54. }
  55. static int query_capabilities(int video_fd, unsigned int *capabilities)
  56. {
  57. struct v4l2_capability capability;
  58. int rc;
  59. memset(&capability, 0, sizeof(capability));
  60. rc = ioctl(video_fd, VIDIOC_QUERYCAP, &capability);
  61. if (rc < 0)
  62. return -1;
  63. if (capabilities != NULL) {
  64. if ((capability.capabilities & V4L2_CAP_DEVICE_CAPS) != 0)
  65. *capabilities = capability.device_caps;
  66. else
  67. *capabilities = capability.capabilities;
  68. }
  69. return 0;
  70. }
  71. static bool find_format(int video_fd, unsigned int type,
  72. unsigned int pixelformat)
  73. {
  74. struct v4l2_fmtdesc fmtdesc;
  75. int rc;
  76. memset(&fmtdesc, 0, sizeof(fmtdesc));
  77. fmtdesc.type = type;
  78. fmtdesc.index = 0;
  79. do {
  80. rc = ioctl(video_fd, VIDIOC_ENUM_FMT, &fmtdesc);
  81. if (rc < 0)
  82. break;
  83. if (fmtdesc.pixelformat == pixelformat)
  84. return true;
  85. fmtdesc.index++;
  86. } while (rc >= 0);
  87. return false;
  88. }
  89. static void setup_format(struct v4l2_format *format, unsigned int type,
  90. unsigned int width, unsigned int height,
  91. unsigned int pixelformat)
  92. {
  93. unsigned int sizeimage;
  94. memset(format, 0, sizeof(*format));
  95. format->type = type;
  96. sizeimage = type_is_output(type) ? SOURCE_SIZE_MAX : 0;
  97. if (type_is_mplane(type)) {
  98. format->fmt.pix_mp.width = width;
  99. format->fmt.pix_mp.height = height;
  100. format->fmt.pix_mp.plane_fmt[0].sizeimage = sizeimage;
  101. format->fmt.pix_mp.pixelformat = pixelformat;
  102. } else {
  103. format->fmt.pix.width = width;
  104. format->fmt.pix.height = height;
  105. format->fmt.pix.sizeimage = sizeimage;
  106. format->fmt.pix.pixelformat = pixelformat;
  107. }
  108. }
  109. static int try_format(int video_fd, unsigned int type, unsigned int width,
  110. unsigned int height, unsigned int pixelformat)
  111. {
  112. struct v4l2_format format;
  113. int rc;
  114. setup_format(&format, type, width, height, pixelformat);
  115. rc = ioctl(video_fd, VIDIOC_TRY_FMT, &format);
  116. if (rc < 0) {
  117. fprintf(stderr, "Unable to try format for type %d: %s\n", type,
  118. strerror(errno));
  119. return -1;
  120. }
  121. return 0;
  122. }
  123. static int set_format(int video_fd, unsigned int type, unsigned int width,
  124. unsigned int height, unsigned int pixelformat)
  125. {
  126. struct v4l2_format format;
  127. int rc;
  128. setup_format(&format, type, width, height, pixelformat);
  129. rc = ioctl(video_fd, VIDIOC_S_FMT, &format);
  130. if (rc < 0) {
  131. fprintf(stderr, "Unable to set format for type %d: %s\n", type,
  132. strerror(errno));
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. static int get_format(int video_fd, unsigned int type, unsigned int *width,
  138. unsigned int *height, unsigned int *bytesperline,
  139. unsigned int *sizes, unsigned int *planes_count)
  140. {
  141. struct v4l2_format format;
  142. unsigned int count;
  143. unsigned int i;
  144. int rc;
  145. memset(&format, 0, sizeof(format));
  146. format.type = type;
  147. rc = ioctl(video_fd, VIDIOC_G_FMT, &format);
  148. if (rc < 0) {
  149. fprintf(stderr, "Unable to get format for type %d: %s\n", type,
  150. strerror(errno));
  151. return -1;
  152. }
  153. if (type_is_mplane(type)) {
  154. count = format.fmt.pix_mp.num_planes;
  155. if (width != NULL)
  156. *width = format.fmt.pix_mp.width;
  157. if (height != NULL)
  158. *height = format.fmt.pix_mp.height;
  159. if (planes_count != NULL)
  160. if (*planes_count > 0 && *planes_count < count)
  161. count = *planes_count;
  162. if (bytesperline != NULL)
  163. for (i = 0; i < count; i++)
  164. bytesperline[i] =
  165. format.fmt.pix_mp.plane_fmt[i].bytesperline;
  166. if (sizes != NULL)
  167. for (i = 0; i < count; i++)
  168. sizes[i] = format.fmt.pix_mp.plane_fmt[i].sizeimage;
  169. if (planes_count != NULL)
  170. *planes_count = count;
  171. } else {
  172. if (width != NULL)
  173. *width = format.fmt.pix.width;
  174. if (height != NULL)
  175. *height = format.fmt.pix.height;
  176. if (bytesperline != NULL)
  177. bytesperline[0] = format.fmt.pix.bytesperline;
  178. if (sizes != NULL)
  179. sizes[0] = format.fmt.pix.sizeimage;
  180. if (planes_count != NULL)
  181. *planes_count = 1;
  182. }
  183. return 0;
  184. }
  185. static int create_buffers(int video_fd, unsigned int type,
  186. unsigned int buffers_count, unsigned int *index_base)
  187. {
  188. struct v4l2_create_buffers buffers;
  189. int rc;
  190. memset(&buffers, 0, sizeof(buffers));
  191. buffers.format.type = type;
  192. buffers.memory = V4L2_MEMORY_MMAP;
  193. buffers.count = buffers_count;
  194. rc = ioctl(video_fd, VIDIOC_G_FMT, &buffers.format);
  195. if (rc < 0) {
  196. fprintf(stderr, "Unable to get format for type %d: %s\n", type,
  197. strerror(errno));
  198. return -1;
  199. }
  200. rc = ioctl(video_fd, VIDIOC_CREATE_BUFS, &buffers);
  201. if (rc < 0) {
  202. fprintf(stderr, "Unable to create buffer for type %d: %s\n",
  203. type, strerror(errno));
  204. return -1;
  205. }
  206. if (index_base != NULL)
  207. *index_base = buffers.index;
  208. return 0;
  209. }
  210. static int query_buffer(int video_fd, unsigned int type, unsigned int index,
  211. unsigned int *lengths, unsigned int *offsets,
  212. unsigned int buffers_count)
  213. {
  214. struct v4l2_plane planes[buffers_count];
  215. struct v4l2_buffer buffer;
  216. unsigned int i;
  217. int rc;
  218. memset(planes, 0, sizeof(planes));
  219. memset(&buffer, 0, sizeof(buffer));
  220. buffer.type = type;
  221. buffer.memory = V4L2_MEMORY_MMAP;
  222. buffer.index = index;
  223. buffer.length = buffers_count;
  224. buffer.m.planes = planes;
  225. rc = ioctl(video_fd, VIDIOC_QUERYBUF, &buffer);
  226. if (rc < 0) {
  227. fprintf(stderr, "Unable to query buffer: %s\n",
  228. strerror(errno));
  229. return -1;
  230. }
  231. if (type_is_mplane(type)) {
  232. if (lengths != NULL)
  233. for (i = 0; i < buffer.length; i++)
  234. lengths[i] = buffer.m.planes[i].length;
  235. if (offsets != NULL)
  236. for (i = 0; i < buffer.length; i++)
  237. offsets[i] = buffer.m.planes[i].m.mem_offset;
  238. } else {
  239. if (lengths != NULL)
  240. lengths[0] = buffer.length;
  241. if (offsets != NULL)
  242. offsets[0] = buffer.m.offset;
  243. }
  244. return 0;
  245. }
  246. static int request_buffers(int video_fd, unsigned int type,
  247. unsigned int buffers_count)
  248. {
  249. struct v4l2_requestbuffers buffers;
  250. int rc;
  251. memset(&buffers, 0, sizeof(buffers));
  252. buffers.type = type;
  253. buffers.memory = V4L2_MEMORY_MMAP;
  254. buffers.count = buffers_count;
  255. rc = ioctl(video_fd, VIDIOC_REQBUFS, &buffers);
  256. if (rc < 0) {
  257. fprintf(stderr, "Unable to request buffers: %s\n",
  258. strerror(errno));
  259. return -1;
  260. }
  261. return 0;
  262. }
  263. static int queue_buffer(int video_fd, int request_fd, unsigned int type,
  264. uint64_t ts, unsigned int index, unsigned int size,
  265. unsigned int buffers_count)
  266. {
  267. struct v4l2_plane planes[buffers_count];
  268. struct v4l2_buffer buffer;
  269. unsigned int i;
  270. int rc;
  271. memset(planes, 0, sizeof(planes));
  272. memset(&buffer, 0, sizeof(buffer));
  273. buffer.type = type;
  274. buffer.memory = V4L2_MEMORY_MMAP;
  275. buffer.index = index;
  276. buffer.length = buffers_count;
  277. buffer.m.planes = planes;
  278. for (i = 0; i < buffers_count; i++)
  279. if (type_is_mplane(type))
  280. buffer.m.planes[i].bytesused = size;
  281. else
  282. buffer.bytesused = size;
  283. if (request_fd >= 0) {
  284. buffer.flags = V4L2_BUF_FLAG_REQUEST_FD;
  285. buffer.request_fd = request_fd;
  286. }
  287. buffer.timestamp.tv_usec = ts / 1000;
  288. buffer.timestamp.tv_sec = ts / 1000000000ULL;
  289. rc = ioctl(video_fd, VIDIOC_QBUF, &buffer);
  290. if (rc < 0) {
  291. fprintf(stderr, "Unable to queue buffer: %s\n",
  292. strerror(errno));
  293. return -1;
  294. }
  295. return 0;
  296. }
  297. static int dequeue_buffer(int video_fd, int request_fd, unsigned int type,
  298. unsigned int index, unsigned int buffers_count,
  299. bool *error)
  300. {
  301. struct v4l2_plane planes[buffers_count];
  302. struct v4l2_buffer buffer;
  303. int rc;
  304. memset(planes, 0, sizeof(planes));
  305. memset(&buffer, 0, sizeof(buffer));
  306. buffer.type = type;
  307. buffer.memory = V4L2_MEMORY_MMAP;
  308. buffer.index = index;
  309. buffer.length = buffers_count;
  310. buffer.m.planes = planes;
  311. if (request_fd >= 0) {
  312. buffer.flags = V4L2_BUF_FLAG_REQUEST_FD;
  313. buffer.request_fd = request_fd;
  314. }
  315. rc = ioctl(video_fd, VIDIOC_DQBUF, &buffer);
  316. if (rc < 0) {
  317. fprintf(stderr, "Unable to dequeue buffer: %s\n",
  318. strerror(errno));
  319. return -1;
  320. }
  321. if (error != NULL)
  322. *error = !!(buffer.flags & V4L2_BUF_FLAG_ERROR);
  323. return 0;
  324. }
  325. static int export_buffer(int video_fd, unsigned int type, unsigned int index,
  326. unsigned int flags, int *export_fds,
  327. unsigned int export_fds_count)
  328. {
  329. struct v4l2_exportbuffer exportbuffer;
  330. unsigned int i;
  331. int rc;
  332. for (i = 0; i < export_fds_count; i++) {
  333. memset(&exportbuffer, 0, sizeof(exportbuffer));
  334. exportbuffer.type = type;
  335. exportbuffer.index = index;
  336. exportbuffer.plane = i;
  337. exportbuffer.flags = flags;
  338. rc = ioctl(video_fd, VIDIOC_EXPBUF, &exportbuffer);
  339. if (rc < 0) {
  340. fprintf(stderr, "Unable to export buffer: %s\n",
  341. strerror(errno));
  342. return -1;
  343. }
  344. export_fds[i] = exportbuffer.fd;
  345. }
  346. return 0;
  347. }
  348. static int set_control(int video_fd, int request_fd, unsigned int id,
  349. void *data, unsigned int size)
  350. {
  351. struct v4l2_ext_control control;
  352. struct v4l2_ext_controls controls;
  353. int rc;
  354. memset(&control, 0, sizeof(control));
  355. memset(&controls, 0, sizeof(controls));
  356. control.id = id;
  357. control.ptr = data;
  358. control.size = size;
  359. controls.controls = &control;
  360. controls.count = 1;
  361. if (request_fd >= 0) {
  362. controls.which = V4L2_CTRL_WHICH_REQUEST_VAL;
  363. controls.request_fd = request_fd;
  364. }
  365. rc = ioctl(video_fd, VIDIOC_S_EXT_CTRLS, &controls);
  366. if (rc < 0) {
  367. fprintf(stderr, "Unable to set control: %s\n", strerror(errno));
  368. return -1;
  369. }
  370. return 0;
  371. }
  372. static int set_stream(int video_fd, unsigned int type, bool enable)
  373. {
  374. enum v4l2_buf_type buf_type = type;
  375. int rc;
  376. rc = ioctl(video_fd, enable ? VIDIOC_STREAMON : VIDIOC_STREAMOFF,
  377. &buf_type);
  378. if (rc < 0) {
  379. fprintf(stderr, "Unable to %sable stream: %s\n",
  380. enable ? "en" : "dis", strerror(errno));
  381. return -1;
  382. }
  383. return 0;
  384. }
  385. static int set_format_controls(int video_fd, int request_fd,
  386. enum codec_type type, union controls *frame)
  387. {
  388. struct {
  389. enum codec_type type;
  390. char *description;
  391. unsigned int id;
  392. void *data;
  393. unsigned int size;
  394. } glue[] = {
  395. { CODEC_TYPE_MPEG2, "slice parameters",
  396. V4L2_CID_MPEG_VIDEO_MPEG2_SLICE_PARAMS,
  397. &frame->mpeg2.slice_params,
  398. sizeof(frame->mpeg2.slice_params) },
  399. { CODEC_TYPE_MPEG2, "quantization matrices",
  400. V4L2_CID_MPEG_VIDEO_MPEG2_QUANTIZATION,
  401. &frame->mpeg2.quantization,
  402. sizeof(frame->mpeg2.quantization) },
  403. #ifdef V4L2_PIX_FMT_H264_SLICE
  404. { CODEC_TYPE_H264, "decode parameters",
  405. V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAMS,
  406. &frame->h264.decode_params, sizeof(frame->h264.decode_params) },
  407. { CODEC_TYPE_H264, "picture parameter set",
  408. V4L2_CID_MPEG_VIDEO_H264_PPS, &frame->h264.pps,
  409. sizeof(frame->h264.pps) },
  410. { CODEC_TYPE_H264, "sequence parameter set",
  411. V4L2_CID_MPEG_VIDEO_H264_SPS, &frame->h264.sps,
  412. sizeof(frame->h264.sps) },
  413. { CODEC_TYPE_H264, "scaling matrix",
  414. V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX,
  415. &frame->h264.scaling_matrix,
  416. sizeof(frame->h264.scaling_matrix) },
  417. { CODEC_TYPE_H264, "scaling matrix",
  418. V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAMS,
  419. &frame->h264.slice_params, sizeof(frame->h264.slice_params) },
  420. #endif
  421. #ifdef V4L2_PIX_FMT_HEVC_SLICE
  422. { CODEC_TYPE_H265, "sequence parameter set",
  423. V4L2_CID_MPEG_VIDEO_HEVC_SPS, &frame->h265.sps,
  424. sizeof(frame->h265.sps) },
  425. { CODEC_TYPE_H265, "picture parameter set",
  426. V4L2_CID_MPEG_VIDEO_HEVC_PPS, &frame->h265.pps,
  427. sizeof(frame->h265.pps) },
  428. { CODEC_TYPE_H265, "slice parameters",
  429. V4L2_CID_MPEG_VIDEO_HEVC_SLICE_PARAMS,
  430. &frame->h265.slice_params, sizeof(frame->h265.slice_params) },
  431. #endif
  432. };
  433. unsigned int i;
  434. int rc;
  435. for (i = 0; i < ARRAY_SIZE(glue); i++) {
  436. if (glue[i].type != type)
  437. continue;
  438. rc = set_control(video_fd, request_fd, glue[i].id, glue[i].data,
  439. glue[i].size);
  440. if (rc < 0) {
  441. fprintf(stderr, "Unable to set %s control\n",
  442. glue[i].description);
  443. return -1;
  444. }
  445. }
  446. return 0;
  447. }
  448. static int codec_source_format(enum codec_type type)
  449. {
  450. switch (type) {
  451. case CODEC_TYPE_MPEG2:
  452. return V4L2_PIX_FMT_MPEG2_SLICE;
  453. #ifdef V4L2_PIX_FMT_H264_SLICE
  454. case CODEC_TYPE_H264:
  455. return V4L2_PIX_FMT_H264_SLICE;
  456. #endif
  457. #ifdef V4L2_PIX_FMT_HEVC_SLICE
  458. case CODEC_TYPE_H265:
  459. return V4L2_PIX_FMT_HEVC_SLICE;
  460. #endif
  461. default:
  462. fprintf(stderr, "Invalid format type\n");
  463. return -1;
  464. }
  465. }
  466. bool video_engine_capabilities_test(int video_fd,
  467. unsigned int capabilities_required)
  468. {
  469. unsigned int capabilities;
  470. int rc;
  471. rc = query_capabilities(video_fd, &capabilities);
  472. if (rc < 0) {
  473. fprintf(stderr, "Unable to query video capabilities: %s\n",
  474. strerror(errno));
  475. return false;
  476. }
  477. if ((capabilities & capabilities_required) != capabilities_required)
  478. return false;
  479. return true;
  480. }
  481. bool video_engine_format_test(int video_fd, bool mplane, unsigned int width,
  482. unsigned int height, unsigned int format)
  483. {
  484. unsigned int type;
  485. int rc;
  486. type = mplane ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
  487. V4L2_BUF_TYPE_VIDEO_CAPTURE;
  488. rc = try_format(video_fd, type, width, height, format);
  489. return rc >= 0;
  490. }
  491. int video_engine_start(int video_fd, int media_fd, unsigned int width,
  492. unsigned int height, struct format_description *format,
  493. enum codec_type type, struct video_buffer **buffers,
  494. unsigned int buffers_count, struct video_setup *setup)
  495. {
  496. struct video_buffer *buffer;
  497. unsigned int source_format;
  498. unsigned int source_length;
  499. unsigned int source_map_offset;
  500. unsigned int destination_format;
  501. void *destination_map[VIDEO_MAX_PLANES];
  502. unsigned int destination_map_lengths[VIDEO_MAX_PLANES];
  503. unsigned int destination_map_offsets[VIDEO_MAX_PLANES];
  504. unsigned int destination_sizes[VIDEO_MAX_PLANES];
  505. unsigned int destination_bytesperlines[VIDEO_MAX_PLANES];
  506. unsigned int destination_planes_count;
  507. unsigned int export_fds_count;
  508. unsigned int output_type, capture_type;
  509. unsigned int format_width, format_height;
  510. unsigned int i, j;
  511. int request_fd;
  512. int rc;
  513. *buffers = malloc(buffers_count * sizeof(**buffers));
  514. memset(*buffers, 0, buffers_count * sizeof(**buffers));
  515. if (format->v4l2_mplane) {
  516. output_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  517. capture_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  518. } else {
  519. output_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  520. capture_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  521. }
  522. setup->output_type = output_type;
  523. setup->capture_type = capture_type;
  524. source_format = codec_source_format(type);
  525. rc = set_format(video_fd, output_type, width, height, source_format);
  526. if (rc < 0) {
  527. fprintf(stderr, "Unable to set source format\n");
  528. goto error;
  529. }
  530. destination_format = format->v4l2_format;
  531. rc = set_format(video_fd, capture_type, width, height,
  532. destination_format);
  533. if (rc < 0) {
  534. fprintf(stderr, "Unable to set destination format\n");
  535. goto error;
  536. }
  537. destination_planes_count = format->planes_count;
  538. rc = get_format(video_fd, capture_type, &format_width, &format_height,
  539. destination_bytesperlines, destination_sizes, NULL);
  540. if (rc < 0) {
  541. fprintf(stderr, "Unable to get destination format\n");
  542. goto error;
  543. }
  544. rc = create_buffers(video_fd, output_type, buffers_count, NULL);
  545. if (rc < 0) {
  546. fprintf(stderr, "Unable to create source buffers\n");
  547. goto error;
  548. }
  549. for (i = 0; i < buffers_count; i++) {
  550. buffer = &((*buffers)[i]);
  551. rc = query_buffer(video_fd, output_type, i, &source_length,
  552. &source_map_offset, 1);
  553. if (rc < 0) {
  554. fprintf(stderr, "Unable to request source buffer\n");
  555. goto error;
  556. }
  557. buffer->source_map = mmap(NULL, source_length,
  558. PROT_READ | PROT_WRITE, MAP_SHARED,
  559. video_fd, source_map_offset);
  560. if (buffer->source_map == MAP_FAILED) {
  561. fprintf(stderr, "Unable to map source buffer\n");
  562. goto error;
  563. }
  564. buffer->source_data = buffer->source_map;
  565. buffer->source_size = source_length;
  566. }
  567. rc = create_buffers(video_fd, capture_type, buffers_count, NULL);
  568. if (rc < 0) {
  569. fprintf(stderr, "Unable to create destination buffers\n");
  570. goto error;
  571. }
  572. for (i = 0; i < buffers_count; i++) {
  573. buffer = &((*buffers)[i]);
  574. rc = query_buffer(video_fd, capture_type, i,
  575. destination_map_lengths,
  576. destination_map_offsets,
  577. format->v4l2_buffers_count);
  578. if (rc < 0) {
  579. fprintf(stderr,
  580. "Unable to request destination buffer\n");
  581. goto error;
  582. }
  583. for (j = 0; j < format->v4l2_buffers_count; j++) {
  584. destination_map[j] = mmap(NULL,
  585. destination_map_lengths[j],
  586. PROT_READ | PROT_WRITE,
  587. MAP_SHARED,
  588. video_fd,
  589. destination_map_offsets[j]);
  590. if (destination_map[j] == MAP_FAILED) {
  591. fprintf(stderr,
  592. "Unable to map destination buffer\n");
  593. goto error;
  594. }
  595. }
  596. /*
  597. * FIXME: Handle this per-pixelformat, trying to generalize it
  598. * is not a reasonable approach. The final description should be
  599. * in terms of (logical) planes.
  600. */
  601. if (format->v4l2_buffers_count == 1) {
  602. destination_sizes[0] = destination_bytesperlines[0] *
  603. format_height;
  604. for (j = 1; j < destination_planes_count; j++)
  605. destination_sizes[j] = destination_sizes[0] / 2;
  606. for (j = 0; j < destination_planes_count; j++) {
  607. buffer->destination_map[j] =
  608. j == 0 ? destination_map[0] : NULL;
  609. buffer->destination_map_lengths[j] =
  610. j == 0 ? destination_map_lengths[0] : 0;
  611. buffer->destination_offsets[j] =
  612. j > 0 ? destination_sizes[j - 1] : 0;
  613. buffer->destination_data[j] =
  614. (void *)((unsigned char *)
  615. destination_map[0] +
  616. buffer->destination_offsets[j]);
  617. buffer->destination_sizes[j] =
  618. destination_sizes[j];
  619. buffer->destination_bytesperlines[j] =
  620. destination_bytesperlines[0];
  621. }
  622. } else if (format->v4l2_buffers_count ==
  623. destination_planes_count) {
  624. for (j = 0; j < destination_planes_count; j++) {
  625. buffer->destination_map[j] = destination_map[j];
  626. buffer->destination_map_lengths[j] =
  627. destination_map_lengths[j];
  628. buffer->destination_offsets[j] = 0;
  629. buffer->destination_data[j] =
  630. destination_map[j];
  631. buffer->destination_sizes[j] =
  632. destination_sizes[j];
  633. buffer->destination_bytesperlines[j] =
  634. destination_bytesperlines[j];
  635. }
  636. } else {
  637. fprintf(stderr,
  638. "Unsupported combination of %d buffers with %d planes\n",
  639. format->v4l2_buffers_count,
  640. destination_planes_count);
  641. goto error;
  642. }
  643. buffer->destination_planes_count = destination_planes_count;
  644. buffer->destination_buffers_count = format->v4l2_buffers_count;
  645. export_fds_count = format->v4l2_buffers_count;
  646. for (j = 0; j < export_fds_count; j++)
  647. buffer->export_fds[j] = -1;
  648. rc = export_buffer(video_fd, capture_type, i, O_RDONLY,
  649. buffer->export_fds, export_fds_count);
  650. if (rc < 0) {
  651. fprintf(stderr,
  652. "Unable to export destination buffer\n");
  653. goto error;
  654. }
  655. rc = ioctl(media_fd, MEDIA_IOC_REQUEST_ALLOC, &request_fd);
  656. if (rc < 0) {
  657. fprintf(stderr,
  658. "Unable to allocate media request: %s\n",
  659. strerror(errno));
  660. goto error;
  661. }
  662. buffer->request_fd = request_fd;
  663. }
  664. rc = set_stream(video_fd, output_type, true);
  665. if (rc < 0) {
  666. fprintf(stderr, "Unable to enable source stream\n");
  667. goto error;
  668. }
  669. rc = set_stream(video_fd, capture_type, true);
  670. if (rc < 0) {
  671. fprintf(stderr, "Unable to enable destination stream\n");
  672. goto error;
  673. }
  674. rc = 0;
  675. goto complete;
  676. error:
  677. free(*buffers);
  678. *buffers = NULL;
  679. complete:
  680. return rc;
  681. }
  682. int video_engine_stop(int video_fd, struct video_buffer *buffers,
  683. unsigned int buffers_count, struct video_setup *setup)
  684. {
  685. unsigned int i, j;
  686. int rc;
  687. rc = set_stream(video_fd, setup->output_type, false);
  688. if (rc < 0) {
  689. fprintf(stderr, "Unable to enable source stream\n");
  690. return -1;
  691. }
  692. rc = set_stream(video_fd, setup->capture_type, false);
  693. if (rc < 0) {
  694. fprintf(stderr, "Unable to enable destination stream\n");
  695. return -1;
  696. }
  697. for (i = 0; i < buffers_count; i++) {
  698. munmap(buffers[i].source_data, buffers[i].source_size);
  699. for (j = 0; j < buffers[i].destination_buffers_count; j++) {
  700. if (buffers[i].destination_map[j] == NULL)
  701. break;
  702. munmap(buffers[i].destination_map[j],
  703. buffers[i].destination_map_lengths[j]);
  704. if (buffers[i].export_fds[j] >= 0)
  705. close(buffers[i].export_fds[j]);
  706. }
  707. for (j = 0; j < buffers[i].destination_buffers_count; j++) {
  708. if (buffers[i].export_fds[j] < 0)
  709. break;
  710. close(buffers[i].export_fds[j]);
  711. }
  712. close(buffers[i].request_fd);
  713. }
  714. free(buffers);
  715. return 0;
  716. }
  717. int video_engine_decode(int video_fd, unsigned int index, union controls *frame,
  718. enum codec_type type, uint64_t ts, void *source_data,
  719. unsigned int source_size, struct video_buffer *buffers,
  720. struct video_setup *setup)
  721. {
  722. struct timeval tv = { 0, 300000 };
  723. int request_fd = -1;
  724. fd_set except_fds;
  725. bool source_error, destination_error;
  726. int rc;
  727. request_fd = buffers[index].request_fd;
  728. memcpy(buffers[index].source_data, source_data, source_size);
  729. rc = set_format_controls(video_fd, request_fd, type, frame);
  730. if (rc < 0) {
  731. fprintf(stderr, "Unable to set format controls\n");
  732. return -1;
  733. }
  734. rc = queue_buffer(video_fd, request_fd, setup->output_type, ts, index,
  735. source_size, 1);
  736. if (rc < 0) {
  737. fprintf(stderr, "Unable to queue source buffer\n");
  738. return -1;
  739. }
  740. rc = queue_buffer(video_fd, -1, setup->capture_type, 0, index, 0,
  741. buffers[index].destination_buffers_count);
  742. if (rc < 0) {
  743. fprintf(stderr, "Unable to queue destination buffer\n");
  744. return -1;
  745. }
  746. rc = ioctl(request_fd, MEDIA_REQUEST_IOC_QUEUE, NULL);
  747. if (rc < 0) {
  748. fprintf(stderr, "Unable to queue media request: %s\n",
  749. strerror(errno));
  750. return -1;
  751. }
  752. FD_ZERO(&except_fds);
  753. FD_SET(request_fd, &except_fds);
  754. rc = select(request_fd + 1, NULL, NULL, &except_fds, &tv);
  755. if (rc == 0) {
  756. fprintf(stderr, "Timeout when waiting for media request\n");
  757. return -1;
  758. } else if (rc < 0) {
  759. fprintf(stderr, "Unable to select media request: %s\n",
  760. strerror(errno));
  761. return -1;
  762. }
  763. rc = dequeue_buffer(video_fd, -1, setup->output_type, index, 1,
  764. &source_error);
  765. if (rc < 0) {
  766. fprintf(stderr, "Unable to dequeue source buffer\n");
  767. return -1;
  768. }
  769. rc = dequeue_buffer(video_fd, -1, setup->capture_type, index,
  770. buffers[index].destination_buffers_count,
  771. &destination_error);
  772. if (rc < 0) {
  773. fprintf(stderr, "Unable to dequeue destination buffer\n");
  774. return -1;
  775. }
  776. if (source_error || destination_error) {
  777. fprintf(stderr, "Error encountered during decoding\n");
  778. return -1;
  779. }
  780. rc = ioctl(request_fd, MEDIA_REQUEST_IOC_REINIT, NULL);
  781. if (rc < 0) {
  782. fprintf(stderr, "Unable to reinit media request: %s\n",
  783. strerror(errno));
  784. return -1;
  785. }
  786. return 0;
  787. }