blob: 5b4b0da608cdc7b55097beddbfc5970ba85e905d [file] [log] [blame]
Fritz Koenigcdba6532021-01-22 16:11:17 -08001/*
2 * Copyright 2021 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6// https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/dev-encoder.html
Miguel Casas266b2e42021-02-05 22:05:58 -05007#include <assert.h>
Fritz Koenigcdba6532021-01-22 16:11:17 -08008#include <ctype.h>
9#include <errno.h>
10#include <fcntl.h>
11#include <getopt.h>
12#include <linux/videodev2.h>
13#include <stdint.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <sys/ioctl.h>
18#include <sys/mman.h>
19#include <sys/stat.h>
20#include <sys/types.h>
Miguel Casas0a13d7a2021-02-05 22:15:06 -050021#include <time.h>
Fritz Koenigcdba6532021-01-22 16:11:17 -080022#include <unistd.h>
23
24static const char *kEncodeDevice = "/dev/video-enc";
25static const int kInputbufferMaxSize = 4 * 1024 * 1024;
26static const int kRequestBufferCount = 8;
27static const uint32_t kIVFHeaderSignature = v4l2_fourcc('D', 'K', 'I', 'F');
28
29struct mmap_buffers {
30 void *start[VIDEO_MAX_PLANES];
31 size_t length[VIDEO_MAX_PLANES];
32 struct gbm_bo *bo;
33};
34
35struct queue {
36 int v4lfd;
37 enum v4l2_buf_type type;
38 uint32_t fourcc;
39 struct mmap_buffers *buffers;
40 uint32_t raw_width;
41 uint32_t raw_height;
42 uint32_t encoded_width;
43 uint32_t encoded_height;
44 uint32_t cnt;
45 uint32_t frame_cnt;
46 uint32_t num_planes;
Fritz Koenig62733512021-02-17 18:36:21 -080047 uint32_t framerate;
Fritz Koenigcdba6532021-01-22 16:11:17 -080048};
49
50struct encoder_cfg {
51 uint32_t gop_size;
52 uint32_t bitrate;
53 enum v4l2_mpeg_video_h264_entropy_mode h264_entropy_mode;
54 enum v4l2_mpeg_video_h264_level h264_level;
55 enum v4l2_mpeg_video_h264_profile h264_profile;
56 enum v4l2_mpeg_video_header_mode header_mode;
57 enum v4l2_mpeg_video_bitrate_mode bitrate_mode;
58};
59
60struct ivf_file_header {
61 uint32_t signature;
62 uint16_t version;
63 uint16_t header_length;
64 uint32_t fourcc;
65 uint16_t width;
66 uint16_t height;
67 uint32_t denominator;
68 uint32_t numerator;
69 uint32_t frame_cnt;
70 uint32_t unused;
71} __attribute__((packed));
72
73struct ivf_frame_header {
74 uint32_t size;
75 uint64_t timestamp;
76} __attribute__((packed));
77
78void print_fourcc(uint32_t fourcc)
79{
80 printf("%c%c%c%c\n", fourcc & 0xff, fourcc >> 8 & 0xff, fourcc >> 16 & 0xff,
81 fourcc >> 24 & 0xff);
82}
83
84int query_format(int v4lfd, enum v4l2_buf_type type, uint32_t fourcc)
85{
86 struct v4l2_fmtdesc fmtdesc;
87 memset(&fmtdesc, 0, sizeof(fmtdesc));
88
89 fmtdesc.type = type;
90 while (ioctl(v4lfd, VIDIOC_ENUM_FMT, &fmtdesc) == 0) {
91 if (fourcc == 0)
92 print_fourcc(fmtdesc.pixelformat);
93 else if (fourcc == fmtdesc.pixelformat)
94 return 1;
95 fmtdesc.index++;
96 }
97
98 return 0;
99}
100
101void enumerate_menu(int v4lfd, uint32_t id, uint32_t min, uint32_t max)
102{
103 struct v4l2_querymenu querymenu;
104 memset(&querymenu, 0, sizeof(querymenu));
105
106 querymenu.id = id;
107 for (querymenu.index = min; querymenu.index <= max; querymenu.index++) {
108 if (0 == ioctl(v4lfd, VIDIOC_QUERYMENU, &querymenu))
109 fprintf(stderr, " %s\n", querymenu.name);
110 }
111}
112
113int capabilities(int v4lfd, uint32_t OUTPUT_format, uint32_t CAPTURE_format,
114 int verbose_capabilities)
115{
116 struct v4l2_capability cap;
117 memset(&cap, 0, sizeof(cap));
118 int ret = ioctl(v4lfd, VIDIOC_QUERYCAP, &cap);
119 if (ret != 0)
120 perror("VIDIOC_QUERYCAP failed");
121
122 printf("driver=\"%s\" bus_info=\"%s\" card=\"%s\" fd=0x%x\n", cap.driver, cap.bus_info,
123 cap.card, v4lfd);
124
125 if (!query_format(v4lfd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, OUTPUT_format)) {
126 printf("Supported OUTPUT formats:\n");
127 query_format(v4lfd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 0);
128 ret = 1;
129 }
130
131 if (!query_format(v4lfd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, CAPTURE_format)) {
132 printf("Supported CAPTURE formats:\n");
133 query_format(v4lfd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 0);
134 ret = 1;
135 }
136
137 if (verbose_capabilities) {
138 struct v4l2_query_ext_ctrl queryctrl;
139 memset(&queryctrl, 0, sizeof(queryctrl));
140
141 for (queryctrl.id = V4L2_CID_BASE; queryctrl.id < V4L2_CID_LASTP1; queryctrl.id++) {
142 if (0 == ioctl(v4lfd, VIDIOC_QUERY_EXT_CTRL, &queryctrl)) {
143 fprintf(stderr, "control %s : %s\n", queryctrl.name,
144 queryctrl.flags & V4L2_CTRL_FLAG_DISABLED ? "disabled"
145 : "enabled");
146 if (queryctrl.type == V4L2_CTRL_TYPE_MENU)
147 enumerate_menu(v4lfd, queryctrl.id, queryctrl.minimum,
148 queryctrl.maximum);
149 } else if (errno == EINVAL) {
150 continue;
151 }
152 }
153
154 for (queryctrl.id = V4L2_CID_PRIVATE_BASE;; queryctrl.id++) {
155 if (0 == ioctl(v4lfd, VIDIOC_QUERY_EXT_CTRL, &queryctrl)) {
156 if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
157 continue;
158
159 fprintf(stderr, "control %s\n", queryctrl.name);
160
161 if (queryctrl.type == V4L2_CTRL_TYPE_MENU)
162 enumerate_menu(v4lfd, queryctrl.id, queryctrl.minimum,
163 queryctrl.maximum);
164 } else if (errno == EINVAL) {
165 break;
166 }
167 }
168
169 memset(&queryctrl, 0, sizeof(queryctrl));
170 queryctrl.id = V4L2_CTRL_CLASS_MPEG | V4L2_CTRL_FLAG_NEXT_CTRL;
171 while (0 == ioctl(v4lfd, VIDIOC_QUERY_EXT_CTRL, &queryctrl)) {
172 fprintf(stderr, "control %s\n", queryctrl.name);
173
174 if (queryctrl.type == V4L2_CTRL_TYPE_MENU)
175 enumerate_menu(v4lfd, queryctrl.id, queryctrl.minimum,
176 queryctrl.maximum);
177
178 if (V4L2_CTRL_ID2CLASS(queryctrl.id) != V4L2_CTRL_CLASS_MPEG)
179 break;
180 /* ... */
181 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
182 }
183 }
184 return ret;
185}
186
Fritz Koenig62733512021-02-17 18:36:21 -0800187int queue_OUTPUT_buffer(struct queue *queue, struct mmap_buffers *buffers, uint32_t index)
188{
189 // compute frame timestamp
190 const float usec_per_frame = (1.0 / queue->framerate) * 1000000;
191 const uint64_t usec_time_stamp = usec_per_frame * queue->frame_cnt;
192 const uint64_t tv_sec = usec_time_stamp / 1000000;
193
194 struct v4l2_buffer v4l2_buffer;
195 struct v4l2_plane planes[VIDEO_MAX_PLANES];
196 memset(&v4l2_buffer, 0, sizeof(v4l2_buffer));
197
198 v4l2_buffer.index = index;
199 v4l2_buffer.type = queue->type;
200 v4l2_buffer.memory = V4L2_MEMORY_MMAP;
201 v4l2_buffer.length = queue->num_planes;
202 v4l2_buffer.timestamp.tv_sec = tv_sec;
203 v4l2_buffer.timestamp.tv_usec = usec_time_stamp - tv_sec;
204 v4l2_buffer.sequence = queue->frame_cnt;
205 v4l2_buffer.m.planes = planes;
206 for (uint32_t i = 0; i < queue->num_planes; ++i) {
207 v4l2_buffer.m.planes[i].length = buffers[index].length[i];
208 v4l2_buffer.m.planes[i].bytesused = buffers[index].length[i];
209 v4l2_buffer.m.planes[i].data_offset = 0;
210 }
211
212 int ret = ioctl(queue->v4lfd, VIDIOC_QBUF, &v4l2_buffer);
213 if (ret != 0) {
214 perror("VIDIOC_QBUF failed");
215 return -1;
216 }
217
218 queue->frame_cnt++;
219
220 return 0;
221}
222
Miguel Casas266b2e42021-02-05 22:05:58 -0500223// This function copies the contents pointed by |fp| tp |queue|s |index| buffer.
224int submit_raw_frame_in_bulk(FILE *fp, struct queue *queue, uint32_t index)
Fritz Koenigcdba6532021-01-22 16:11:17 -0800225{
Miguel Casas266b2e42021-02-05 22:05:58 -0500226 assert(queue->num_planes == 1 || queue->num_planes == 2);
227 assert(queue->raw_width == queue->encoded_width);
228 // TODO: the code below assumes NV12 because the Chroma planes are copied in
229 // one call. Extend to YV12 if ever the need arises.
230 assert(queue->fourcc == v4l2_fourcc('N', 'V', '1', '2'));
Fritz Koenigcdba6532021-01-22 16:11:17 -0800231
Miguel Casas266b2e42021-02-05 22:05:58 -0500232 struct mmap_buffers *buffers = queue->buffers;
Fritz Koenigcdba6532021-01-22 16:11:17 -0800233
234 // read y plane first
235 size_t frame_size = queue->raw_width * queue->raw_height;
236 uint8_t *buffer = buffers[index].start[0];
237
238 if (fread(buffer, frame_size, 1, fp) != 1) {
239 fprintf(stderr, "unable to read luma frame\n");
240 return -1;
241 }
242
243 // now read uv
244 frame_size >>= 1;
245 if (queue->num_planes == 2)
246 buffer = buffers[index].start[1];
Fritz Koenigcdba6532021-01-22 16:11:17 -0800247 else
Miguel Casas266b2e42021-02-05 22:05:58 -0500248 buffer += queue->encoded_width * queue->encoded_height;
Fritz Koenigcdba6532021-01-22 16:11:17 -0800249
250 if (fread(buffer, frame_size, 1, fp) != 1) {
251 fprintf(stderr, "unable to read chroma frame\n");
252 return -1;
253 }
254
Fritz Koenig62733512021-02-17 18:36:21 -0800255 return queue_OUTPUT_buffer(queue, buffers, index);
Fritz Koenigcdba6532021-01-22 16:11:17 -0800256}
257
Miguel Casas8ed87472021-02-05 22:12:07 -0500258// This function copies the contents pointed by |fp| to |queue|s |index| buffer.
259// It's assumed that
260int submit_raw_frame_row_by_row(FILE *fp, uint32_t file_format, struct queue *queue, uint32_t index)
261{
262 assert(queue->raw_width != queue->encoded_width);
263 assert(queue->num_planes == 1 || queue->num_planes == 2);
264 assert(queue->fourcc == v4l2_fourcc('Y', 'V', '1', '2') ||
265 queue->fourcc == v4l2_fourcc('N', 'V', '1', '2'));
266 assert(file_format == v4l2_fourcc('Y', 'V', '1', '2') ||
267 file_format == v4l2_fourcc('N', 'V', '1', '2'));
268
269 struct mmap_buffers *buffers = queue->buffers;
270
271 // Read Y plane first, row by row.
272 uint8_t *buffer = buffers[index].start[0];
273 for (int row = 0; row < queue->raw_height; ++row) {
274 if (fread(buffer, queue->raw_width, 1, fp) != 1) {
275 fprintf(stderr, "unable to read luma row\n");
276 return -1;
277 }
278 buffer += queue->encoded_width;
279 }
280
281 if (queue->num_planes == 2)
282 buffer = buffers[index].start[1];
283 else
284 buffer = buffers[index].start[0] + queue->encoded_width * queue->encoded_height;
285
286 // Now read the U and V planes.
287 if (queue->fourcc == v4l2_fourcc('Y', 'V', '1', '2') &&
288 file_format == v4l2_fourcc('Y', 'V', '1', '2')) {
289 printf("copying YV12 to YV12\n");
290 for (int row = 0; row < queue->raw_height / 4; ++row) {
291 if (fread(buffer, queue->raw_width, 1, fp) != 1) {
292 fprintf(stderr, "unable to read chroma row\n");
293 return -1;
294 }
295 buffer += queue->encoded_width;
296 }
297
298 if (queue->num_planes == 2) {
299 buffer = buffers[index].start[1] +
300 queue->encoded_width * queue->encoded_height / 4;
301 } else {
302 buffer = buffers[index].start[0] +
303 5 * queue->encoded_width * queue->encoded_height / 4;
304 }
305
306 for (int row = 0; row < queue->raw_height / 4; ++row) {
307 if (fread(buffer, queue->raw_width, 1, fp) != 1) {
308 fprintf(stderr, "unable to read chroma row\n");
309 return -1;
310 }
311 buffer += queue->encoded_width;
312 }
313 } else if (queue->fourcc == v4l2_fourcc('N', 'V', '1', '2') &&
314 file_format == v4l2_fourcc('Y', 'V', '1', '2') && queue->num_planes == 1) {
315 const int kNumPlanes = 2u;
316 for (int plane = 0; plane < kNumPlanes; ++plane) {
317 // Copy all chroma samples from |fp| one by one in even |buffer| positions,
318 // then rewind |buffer|, move it one position right and copy from |fp| into
319 // the odd |buffer| positions.
320 for (int row = 0; row < queue->raw_height / 4; ++row) {
321 for (int col = 0; col < queue->raw_width / 2; ++col) {
322 if (fread(buffer, 1 /*size */, 1 /*nmemb*/, fp) != 1) {
323 fprintf(stderr, "unable to read chroma byte\n");
324 return -1;
325 }
326 buffer += 2;
327 }
328 buffer += queue->encoded_width - queue->raw_width;
329
330 for (int col = 0; col < queue->raw_width / 2; ++col) {
331 if (fread(buffer, 1 /*size */, 1 /*nmemb*/, fp) != 1) {
332 fprintf(stderr, "unable to read chroma byte\n");
333 return -1;
334 }
335 buffer += 2;
336 }
337 buffer += queue->encoded_width - queue->raw_width;
338 }
339 // Rewind |buffer| to start writing the other Chroma samples.
340 buffer -= queue->encoded_width * queue->raw_height / 2;
341 buffer++; }
342
343 } else {
344 fprintf(
345 stderr,
346 "combination of queue format, number of planes, and file format unsupported\n");
347 return -1;
348 }
349
Fritz Koenig62733512021-02-17 18:36:21 -0800350 return queue_OUTPUT_buffer(queue, buffers, index);
Miguel Casas8ed87472021-02-05 22:12:07 -0500351}
352
Miguel Casas266b2e42021-02-05 22:05:58 -0500353// This function copies the content of |fp| into the |index|th buffer of
354// |queue|. Depending on |file_format| and the |queue| format, and the raw and
355// encoded sizes of the latter, we might do a copy in bulk or need conversion.
356int submit_raw_frame(FILE *fp, uint32_t file_format, struct queue *queue, uint32_t index)
357{
358 if (queue->raw_width == queue->encoded_width && queue->fourcc == file_format &&
359 queue->fourcc == v4l2_fourcc('N', 'V', '1', '2')) {
360 return submit_raw_frame_in_bulk(fp, queue, index);
361 }
362
Miguel Casas8ed87472021-02-05 22:12:07 -0500363 return submit_raw_frame_row_by_row(fp, file_format, queue, index);
Miguel Casas266b2e42021-02-05 22:05:58 -0500364}
365
Fritz Koenigcdba6532021-01-22 16:11:17 -0800366void cleanup_queue(struct queue *queue)
367{
368 if (queue->cnt) {
369 struct mmap_buffers *buffers = queue->buffers;
370
371 for (uint32_t i = 0; i < queue->cnt; i++)
372 for (uint32_t j = 0; j < queue->num_planes; j++) {
373 munmap(buffers[i].start[j], buffers[i].length[j]);
374 }
375
376 free(queue->buffers);
377 queue->cnt = 0;
378 }
379}
380
381int request_mmap_buffers(struct queue *queue, struct v4l2_requestbuffers *reqbuf)
382{
383 const int v4lfd = queue->v4lfd;
384 const uint32_t buffer_alloc = reqbuf->count * sizeof(struct mmap_buffers);
385 struct mmap_buffers *buffers = (struct mmap_buffers *)malloc(buffer_alloc);
386 memset(buffers, 0, buffer_alloc);
387 queue->buffers = buffers;
388 queue->cnt = reqbuf->count;
389
390 int ret;
391 for (uint32_t i = 0; i < reqbuf->count; i++) {
392 struct v4l2_buffer buffer;
393 struct v4l2_plane planes[VIDEO_MAX_PLANES];
394 memset(&buffer, 0, sizeof(buffer));
395 buffer.type = reqbuf->type;
396 buffer.memory = V4L2_MEMORY_MMAP;
397 buffer.index = i;
398 buffer.length = queue->num_planes;
399 buffer.m.planes = planes;
400 ret = ioctl(v4lfd, VIDIOC_QUERYBUF, &buffer);
401 if (ret != 0) {
402 printf("VIDIOC_QUERYBUF failed: %d\n", ret);
403 break;
404 }
405
406 for (uint32_t j = 0; j < queue->num_planes; j++) {
407 buffers[i].length[j] = buffer.m.planes[j].length;
408 buffers[i].start[j] =
409 mmap(NULL, buffer.m.planes[j].length, PROT_READ | PROT_WRITE,
410 MAP_SHARED, v4lfd, buffer.m.planes[j].m.mem_offset);
411 if (MAP_FAILED == buffers[i].start[j]) {
412 fprintf(stderr,
413 "failed to mmap buffer of length(%d) and offset(0x%x)\n",
414 buffer.m.planes[j].length, buffer.m.planes[j].m.mem_offset);
415 }
416 }
417 }
418
419 return ret;
420}
421
422int queue_CAPTURE_buffer(struct queue *queue, uint32_t index)
423{
424 struct mmap_buffers *buffers = queue->buffers;
425 struct v4l2_buffer v4l2_buffer;
426 struct v4l2_plane planes[VIDEO_MAX_PLANES];
427 memset(&v4l2_buffer, 0, sizeof v4l2_buffer);
428 memset(&planes, 0, sizeof planes);
429
430 v4l2_buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
431 v4l2_buffer.memory = V4L2_MEMORY_MMAP;
432 v4l2_buffer.index = index;
433 v4l2_buffer.m.planes = planes;
434 v4l2_buffer.length = queue->num_planes;
435
436 v4l2_buffer.m.planes[0].length = buffers[index].length[0];
437 v4l2_buffer.m.planes[0].bytesused = buffers[index].length[0];
438 v4l2_buffer.m.planes[0].data_offset = 0;
439
440 int ret = ioctl(queue->v4lfd, VIDIOC_QBUF, &v4l2_buffer);
441 if (ret != 0) {
442 perror("VIDIOC_QBUF failed");
443 }
444
445 return ret;
446}
447
448// 4.5.2.5. Initialization
Fritz Koenig62733512021-02-17 18:36:21 -0800449int Initialization(struct queue *OUTPUT_queue, struct queue *CAPTURE_queue)
Fritz Koenigcdba6532021-01-22 16:11:17 -0800450{
451 int ret = 0;
452
453 // 1. Set the coded format on the CAPTURE queue via VIDIOC_S_FMT().
454 if (!ret) {
455 struct v4l2_format fmt;
456 memset(&fmt, 0, sizeof(fmt));
457
458 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
459 fmt.fmt.pix_mp.pixelformat = CAPTURE_queue->fourcc;
460 fmt.fmt.pix_mp.width = CAPTURE_queue->raw_width;
461 fmt.fmt.pix_mp.height = CAPTURE_queue->raw_height;
462 fmt.fmt.pix_mp.plane_fmt[0].sizeimage = kInputbufferMaxSize;
463 fmt.fmt.pix_mp.num_planes = 1;
464
465 int ret = ioctl(CAPTURE_queue->v4lfd, VIDIOC_S_FMT, &fmt);
466 if (ret != 0)
467 perror("VIDIOC_S_FMT failed");
468
469 CAPTURE_queue->encoded_width = fmt.fmt.pix_mp.width;
470 CAPTURE_queue->encoded_height = fmt.fmt.pix_mp.height;
471 }
472
473 // 3. Set the raw source format on the OUTPUT queue via VIDIOC_S_FMT().
474 if (!ret) {
475 struct v4l2_format fmt;
476 memset(&fmt, 0, sizeof(fmt));
477
478 fmt.type = OUTPUT_queue->type;
479 fmt.fmt.pix_mp.pixelformat = OUTPUT_queue->fourcc;
480 fmt.fmt.pix_mp.width = OUTPUT_queue->raw_width;
481 fmt.fmt.pix_mp.height = OUTPUT_queue->raw_height;
482 fmt.fmt.pix_mp.num_planes = 1;
483
484 int ret = ioctl(OUTPUT_queue->v4lfd, VIDIOC_S_FMT, &fmt);
485 if (ret != 0)
486 perror("VIDIOC_S_FMT failed");
487
488 OUTPUT_queue->encoded_width = fmt.fmt.pix_mp.width;
489 OUTPUT_queue->encoded_height = fmt.fmt.pix_mp.height;
490
491 OUTPUT_queue->num_planes = fmt.fmt.pix_mp.num_planes;
492 }
493
494 // 4. Set the raw frame interval on the OUTPUT queue via VIDIOC_S_PARM()
495 if (!ret) {
496 struct v4l2_streamparm parms;
497 memset(&parms, 0, sizeof(parms));
498 parms.type = OUTPUT_queue->type;
499 // Note that we are provided "frames per second" but V4L2 expects "time per
500 // frame"; hence we provide the reciprocal of the framerate here.
501 parms.parm.output.timeperframe.numerator = 1;
Fritz Koenig62733512021-02-17 18:36:21 -0800502 parms.parm.output.timeperframe.denominator = OUTPUT_queue->framerate;
Fritz Koenigcdba6532021-01-22 16:11:17 -0800503
504 ret = ioctl(OUTPUT_queue->v4lfd, VIDIOC_S_PARM, &parms);
505 if (ret != 0)
506 perror("VIDIOC_S_PARAM failed");
507 }
508
509 // 6. Optional. Set the visible resolution for the stream metadata via
510 // VIDIOC_S_SELECTION() on the OUTPUT queue if it is desired to be
511 // different than the full OUTPUT resolution.
512 if (!ret) {
513 struct v4l2_selection selection_arg;
514 memset(&selection_arg, 0, sizeof(selection_arg));
515 selection_arg.type = OUTPUT_queue->type;
516 selection_arg.target = V4L2_SEL_TGT_CROP;
517 selection_arg.r.left = 0;
518 selection_arg.r.top = 0;
519 selection_arg.r.width = OUTPUT_queue->raw_width;
520 selection_arg.r.height = OUTPUT_queue->raw_height;
521
522 ret = ioctl(OUTPUT_queue->v4lfd, VIDIOC_S_SELECTION, &selection_arg);
523
524 if (ret != 0)
525 perror("VIDIOC_S_SELECTION failed");
526
527 // TODO(fritz) : check returned values are same as sent values
528 }
529
530 // 7. Allocate buffers for both OUTPUT and CAPTURE via VIDIOC_REQBUFS().
531 // This may be performed in any order.
532 if (!ret) {
533 struct v4l2_requestbuffers reqbuf;
534 memset(&reqbuf, 0, sizeof(reqbuf));
535 reqbuf.count = kRequestBufferCount;
536 reqbuf.type = OUTPUT_queue->type;
537 reqbuf.memory = V4L2_MEMORY_MMAP;
538
539 ret = ioctl(OUTPUT_queue->v4lfd, VIDIOC_REQBUFS, &reqbuf);
540 if (ret != 0)
541 perror("VIDIOC_REQBUFS failed");
542
543 printf("%d buffers requested, %d buffers for uncompressed frames returned\n",
544 kRequestBufferCount, reqbuf.count);
545
546 ret = request_mmap_buffers(OUTPUT_queue, &reqbuf);
547 }
548
549 if (!ret) {
550 struct v4l2_requestbuffers reqbuf;
551 memset(&reqbuf, 0, sizeof(reqbuf));
552 reqbuf.count = kRequestBufferCount;
553 reqbuf.type = CAPTURE_queue->type;
554 reqbuf.memory = V4L2_MEMORY_MMAP;
555
556 ret = ioctl(OUTPUT_queue->v4lfd, VIDIOC_REQBUFS, &reqbuf);
557 if (ret != 0)
558 perror("VIDIOC_REQBUFS failed");
559
560 printf("%d buffers requested, %d buffers for compressed frames returned\n",
561 kRequestBufferCount, reqbuf.count);
562
563 ret = request_mmap_buffers(CAPTURE_queue, &reqbuf);
564 for (uint32_t i = 0; i < reqbuf.count; i++) {
565 queue_CAPTURE_buffer(CAPTURE_queue, i);
566 }
567 }
568
569 return ret;
570}
571
572int configure_h264(int v4lfd, struct encoder_cfg *cfg)
573{
574 int ret = 0;
575 const int kH264CtrlCnt = 4;
576
577 struct v4l2_ext_control ext_ctrl[kH264CtrlCnt];
578 memset(&ext_ctrl, 0, sizeof(ext_ctrl));
579
580 ext_ctrl[0].id = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
581 ext_ctrl[0].value = cfg->h264_profile;
582
583 ext_ctrl[1].id = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
584 ext_ctrl[1].value = cfg->h264_level;
585
586 ext_ctrl[2].id = V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE;
587 ext_ctrl[2].value = cfg->h264_entropy_mode;
588
589 ext_ctrl[3].id = V4L2_CID_MPEG_VIDEO_HEADER_MODE;
590 ext_ctrl[3].value = cfg->header_mode;
591
592 struct v4l2_ext_controls ext_ctrls;
593 memset(&ext_ctrls, 0, sizeof(ext_ctrls));
594
595 ext_ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
596 ext_ctrls.count = kH264CtrlCnt;
597 ext_ctrls.controls = ext_ctrl;
598
599 ret = ioctl(v4lfd, VIDIOC_S_EXT_CTRLS, &ext_ctrls);
600
601 if (ret != 0)
602 perror("VIDIOC_S_EXT_CTRLS failed");
603
604 for (uint32_t i = 0; i < kH264CtrlCnt; ++i)
605 ext_ctrl[i].value = 0;
606
607 ret = ioctl(v4lfd, VIDIOC_G_EXT_CTRLS, &ext_ctrls);
608 if (ret != 0)
609 perror("VIDIOC_G_EXT_CTRLS failed");
610
611 if (ext_ctrl[0].value != cfg->h264_profile)
612 fprintf(stderr, "requested profile(%d) was not used, using (%d) instead.\n",
613 cfg->h264_profile, ext_ctrl[0].value);
614
615 if (ext_ctrl[1].value != cfg->h264_level)
616 fprintf(stderr, "requested level(%d) was not used, using (%d) instead.\n",
617 cfg->h264_level, ext_ctrl[1].value);
618
619 if (ext_ctrl[2].value != cfg->h264_entropy_mode)
620 fprintf(stderr, "requested entropy mode(%d) was not used, using (%d) instead.\n",
621 cfg->h264_entropy_mode, ext_ctrl[2].value);
622
623 if (ext_ctrl[3].value != cfg->header_mode)
624 fprintf(stderr, "requested entropy mode(%d) was not used, using (%d) instead.\n",
625 cfg->header_mode, ext_ctrl[3].value);
626
627 return ret;
628}
629
630int configure_common(int v4lfd, struct encoder_cfg *cfg)
631{
632 int ret = 0;
633 const int kCommonCtrlCnt = 5;
634
635 struct v4l2_ext_control ext_ctrl[kCommonCtrlCnt];
636 memset(&ext_ctrl, 0, sizeof(ext_ctrl));
637
638 ext_ctrl[0].id = V4L2_CID_MPEG_VIDEO_BITRATE;
639 ext_ctrl[0].value = cfg->bitrate;
640
641 ext_ctrl[1].id = V4L2_CID_MPEG_VIDEO_BITRATE_PEAK;
642 ext_ctrl[1].value = cfg->bitrate * 2;
643
644 ext_ctrl[2].id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
645 ext_ctrl[2].value = cfg->gop_size;
646
647 ext_ctrl[3].id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE;
648 ext_ctrl[3].value = 1;
649
650 ext_ctrl[4].id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE;
651 ext_ctrl[4].value = cfg->bitrate_mode;
652
653 struct v4l2_ext_controls ext_ctrls;
654 memset(&ext_ctrls, 0, sizeof(ext_ctrls));
655
656 ext_ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
657 ext_ctrls.count = kCommonCtrlCnt;
658 ext_ctrls.controls = ext_ctrl;
659
660 ret = ioctl(v4lfd, VIDIOC_S_EXT_CTRLS, &ext_ctrls);
661
662 if (ret != 0)
663 perror("VIDIOC_S_EXT_CTRLS failed");
664
665 for (uint32_t i = 0; i < kCommonCtrlCnt; ++i)
666 ext_ctrl[i].value = 0;
667
668 ret = ioctl(v4lfd, VIDIOC_G_EXT_CTRLS, &ext_ctrls);
669 if (ret != 0)
670 perror("VIDIOC_G_EXT_CTRLS failed");
671
672 if (ext_ctrl[0].value != cfg->bitrate)
673 fprintf(stderr,
674 "requested bitrate(%d) was outside of the limit, using (%d) instead.\n",
675 cfg->bitrate, ext_ctrl[0].value);
676
677 if (ext_ctrl[1].value != cfg->bitrate * 2)
678 fprintf(
679 stderr,
680 "requested bitrate peak(%d) was outside of the limit, using (%d) instead.\n",
681 cfg->bitrate * 2, ext_ctrl[1].value);
682
683 if (ext_ctrl[2].value != cfg->gop_size)
684 fprintf(stderr, "requested gop size(%d) was not used, using (%d) instead.\n",
685 cfg->gop_size, ext_ctrl[2].value);
686
687 if (ext_ctrl[3].value != 1)
688 fprintf(stderr,
689 "requested frame rate control (%d) was not used, using (%d) instead.\n", 1,
690 ext_ctrl[3].value);
691
692 if (ext_ctrl[4].value != cfg->bitrate_mode)
693 fprintf(stderr, "requested bitrate mode(%d) was not used, using (%d) instead.\n",
694 cfg->bitrate_mode, ext_ctrl[4].value);
695
696 return ret;
697}
698
699int dequeue_buffer(struct queue *queue, uint32_t *index, uint32_t *bytesused, uint32_t *data_offset,
700 uint64_t *timestamp)
701{
702 struct v4l2_buffer v4l2_buffer;
703 struct v4l2_plane planes[VIDEO_MAX_PLANES] = { 0 };
704 memset(&v4l2_buffer, 0, sizeof(v4l2_buffer));
705 v4l2_buffer.type = queue->type;
706 v4l2_buffer.length = queue->num_planes;
707 v4l2_buffer.m.planes = planes;
708 v4l2_buffer.m.planes[0].bytesused = 0;
709 int ret = ioctl(queue->v4lfd, VIDIOC_DQBUF, &v4l2_buffer);
710
711 if (ret != 0 && errno != EAGAIN)
712 perror("VIDIOC_DQBUF failed");
713
714 *index = v4l2_buffer.index;
715 if (bytesused)
716 *bytesused = v4l2_buffer.m.planes[0].bytesused;
717 if (data_offset)
718 *data_offset = v4l2_buffer.m.planes[0].data_offset;
719 if (timestamp)
720 *timestamp = v4l2_buffer.timestamp.tv_usec;
721 return ret;
722}
723
Miguel Casas266b2e42021-02-05 22:05:58 -0500724int encode(FILE *fp_input, uint32_t file_format, struct queue *OUTPUT_queue, struct queue *CAPTURE_queue,
Fritz Koenigcdba6532021-01-22 16:11:17 -0800725 uint32_t frames_to_decode)
726{
727 int ret = 0;
728 char output_file_name[256];
729 int use_ivf = 0;
730
Miguel Casas266b2e42021-02-05 22:05:58 -0500731 if (OUTPUT_queue->num_planes == 0 || OUTPUT_queue->num_planes > 2) {
732 fprintf(stderr, " unsupported number of planes: %d\n", OUTPUT_queue->num_planes);
733 return -1;
734 }
Fritz Koenigcdba6532021-01-22 16:11:17 -0800735 fprintf(stderr, "encoding\n");
736
737 if (CAPTURE_queue->fourcc == v4l2_fourcc('V', 'P', '8', '0'))
738 use_ivf = 1;
739
740 sprintf(output_file_name, "output%s", use_ivf ? ".ivf" : ".h264");
741 FILE *fp_output = fopen(output_file_name, "wb");
742 if (!fp_output) {
743 fprintf(stderr, "unable to write to file: %s\n", output_file_name);
744 ret = 1;
745 }
746
747 // write header
748 if (use_ivf) {
749 struct ivf_file_header header;
750 header.signature = kIVFHeaderSignature;
751 header.version = 0;
752 header.header_length = sizeof(struct ivf_file_header);
753 header.fourcc = CAPTURE_queue->fourcc;
754 header.width = CAPTURE_queue->raw_width;
755 header.height = CAPTURE_queue->raw_height;
756 // hard coded 30fps
757 header.denominator = 30;
758 header.numerator = 1;
759 header.frame_cnt = frames_to_decode;
760 header.unused = 0;
761
762 if (fwrite(&header, sizeof(struct ivf_file_header), 1, fp_output) != 1) {
763 fprintf(stderr, "unable to write ivf file header\n");
764 }
765 }
766
Miguel Casas0a13d7a2021-02-05 22:15:06 -0500767 struct timespec start, stop;
768 clock_gettime(CLOCK_REALTIME, &start);
Fritz Koenigcdba6532021-01-22 16:11:17 -0800769 if (!ret) {
770 // prime input by filling up the OUTPUT queue with raw frames
771 for (uint32_t i = 0; i < OUTPUT_queue->cnt; ++i) {
Miguel Casas266b2e42021-02-05 22:05:58 -0500772 if (submit_raw_frame(fp_input, file_format, OUTPUT_queue, i)) {
Fritz Koenigcdba6532021-01-22 16:11:17 -0800773 fprintf(stderr, "unable to submit raw frame\n");
774 ret = 1;
775 }
776 }
777 }
778
779 if (!ret) {
780 ret = ioctl(OUTPUT_queue->v4lfd, VIDIOC_STREAMON, &OUTPUT_queue->type);
781 if (ret != 0)
782 perror("VIDIOC_STREAMON failed on OUTPUT");
783 }
784
785 if (!ret) {
786 ret = ioctl(CAPTURE_queue->v4lfd, VIDIOC_STREAMON, &CAPTURE_queue->type);
787 if (ret != 0)
788 perror("VIDIOC_STREAMON failed on CAPTURE");
789 }
790
Miguel Casas0a13d7a2021-02-05 22:15:06 -0500791 uint32_t cnt = OUTPUT_queue->cnt; // We pre-uploaded a few before.
Fritz Koenigcdba6532021-01-22 16:11:17 -0800792 if (!ret) {
Fritz Koenigcdba6532021-01-22 16:11:17 -0800793 while (cnt < frames_to_decode) {
794 // handle CAPTURE queue first
795 {
796 uint32_t index = 0;
797 uint32_t bytesused = 0;
798 uint32_t data_offset = 0;
799 uint64_t timestamp = 0;
800
801 // first get the newly encoded frame
802 ret = dequeue_buffer(CAPTURE_queue, &index, &bytesused,
803 &data_offset, &timestamp);
804 if (ret != 0)
805 continue;
806
807 if (use_ivf) {
808 struct ivf_frame_header header;
809 header.size = bytesused - data_offset;
810 header.timestamp = timestamp;
811
812 if (fwrite(&header, sizeof(struct ivf_frame_header), 1,
813 fp_output) != 1) {
814 fprintf(stderr,
815 "unable to write ivf frame header\n");
816 }
817 }
818 fwrite(CAPTURE_queue->buffers[index].start[0] + data_offset,
819 bytesused - data_offset, 1, fp_output);
820
821 // done with the buffer, queue it back up
822 queue_CAPTURE_buffer(CAPTURE_queue, index);
823 }
824
825 // handle OUTPUT queue second
826 {
827 uint32_t index = 0;
828
829 ret = dequeue_buffer(OUTPUT_queue, &index, 0, 0, 0);
830 if (ret != 0)
831 continue;
832
Miguel Casas266b2e42021-02-05 22:05:58 -0500833 if (submit_raw_frame(fp_input, file_format, OUTPUT_queue, index))
Fritz Koenigcdba6532021-01-22 16:11:17 -0800834 break;
835 }
836 cnt++;
837 }
838 }
Miguel Casas0a13d7a2021-02-05 22:15:06 -0500839 clock_gettime(CLOCK_REALTIME, &stop);
840 const double elapsed_ns =
841 (stop.tv_sec - start.tv_sec) * 1e9 + (stop.tv_nsec - start.tv_nsec);
842 const double fps = cnt * 1e9 / elapsed_ns;
843 printf("%d frames encoded in %fns (%ffps)\n", cnt, elapsed_ns, fps);
Fritz Koenigcdba6532021-01-22 16:11:17 -0800844
845 if (fp_output) {
846 fclose(fp_output);
847 }
848 return ret;
849}
850
851static void print_help(const char *argv0)
852{
853 printf("usage: %s [OPTIONS]\n", argv0);
Miguel Casas266b2e42021-02-05 22:05:58 -0500854 printf(" -f, --file file to encode\n");
855 printf(" -i, --file_format pixel format of the file (yv12, nv12)\n");
Fritz Koenigcdba6532021-01-22 16:11:17 -0800856 printf(" -w, --width width of image\n");
857 printf(" -h, --height height of image\n");
858 printf(" -m, --max max number of frames to decode\n");
859 printf(" -r, --rate frames per second\n");
860 printf(" -b, --bitrate bits per second\n");
861 printf(" -g, --gop gop length\n");
862 printf(" -c, --codec codec\n");
Miguel Casase4c6efd2021-02-16 22:10:05 -0500863 printf(" -e, --end_usage rate control mode: VBR (default), CBR\n");
Fritz Koenigcdba6532021-01-22 16:11:17 -0800864 printf(" -v, --verbose verbose capabilities\n");
Miguel Casas266b2e42021-02-05 22:05:58 -0500865 printf(" -q, --buffer_fmt OUTPUT queue format\n");
Fritz Koenigcdba6532021-01-22 16:11:17 -0800866}
867
868static const struct option longopts[] = {
869 { "file", required_argument, NULL, 'f' },
Miguel Casas266b2e42021-02-05 22:05:58 -0500870 { "file_format", required_argument, NULL, 'i' },
Fritz Koenigcdba6532021-01-22 16:11:17 -0800871 { "width", required_argument, NULL, 'w' },
872 { "height", required_argument, NULL, 'h' },
873 { "max", required_argument, NULL, 'm' },
Fritz Koenig62733512021-02-17 18:36:21 -0800874 { "fps", required_argument, NULL, 'r' },
Fritz Koenigcdba6532021-01-22 16:11:17 -0800875 { "bitrate", required_argument, NULL, 'b' },
876 { "gop", required_argument, NULL, 'g' },
877 { "codec", required_argument, NULL, 'c' },
Miguel Casase4c6efd2021-02-16 22:10:05 -0500878 { "end_usage", required_argument, NULL, 'e' },
Fritz Koenigcdba6532021-01-22 16:11:17 -0800879 { "verbose", no_argument, NULL, 'v' },
Miguel Casas266b2e42021-02-05 22:05:58 -0500880 { "buffer_fmt", required_argument, NULL, 'q' },
Fritz Koenigcdba6532021-01-22 16:11:17 -0800881 { 0, 0, 0, 0 },
882};
883
884int main(int argc, char *argv[])
885{
Miguel Casas266b2e42021-02-05 22:05:58 -0500886 uint32_t file_format = v4l2_fourcc('N', 'V', '1', '2');
Fritz Koenigcdba6532021-01-22 16:11:17 -0800887 uint32_t OUTPUT_format = v4l2_fourcc('N', 'V', '1', '2');
888 uint32_t CAPTURE_format = v4l2_fourcc('H', '2', '6', '4');
889 char *file_name = NULL;
890 uint32_t width = 0;
891 uint32_t height = 0;
892 uint32_t frames_to_decode = 0;
893 uint32_t framerate = 30;
894 int verbose_capabilities = 0;
895 int c;
896
897 struct encoder_cfg cfg = { .gop_size = 20,
898 .bitrate = 1000,
899 .h264_entropy_mode = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
900 .h264_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
901 .h264_profile = V4L2_MPEG_VIDEO_H264_PROFILE_MAIN,
902 .header_mode = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
903 .bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR };
904
Miguel Casase4c6efd2021-02-16 22:10:05 -0500905 while ((c = getopt_long(argc, argv, "f:i:w:h:m:r:b:g:c:e:vq:", longopts, NULL)) != -1) {
Fritz Koenigcdba6532021-01-22 16:11:17 -0800906 switch (c) {
907 case 'f':
908 file_name = strdup(optarg);
909 break;
Miguel Casas266b2e42021-02-05 22:05:58 -0500910 case 'i':
911 if (strlen(optarg) == 4) {
912 file_format =
913 v4l2_fourcc(toupper(optarg[0]), toupper(optarg[1]),
914 toupper(optarg[2]), toupper(optarg[3]));
915 printf("using (%s) as the file format\n", optarg);
916 }
917 break;
Fritz Koenigcdba6532021-01-22 16:11:17 -0800918 case 'w':
919 width = atoi(optarg);
920 break;
921 case 'h':
922 height = atoi(optarg);
923 break;
924 case 'm':
925 frames_to_decode = atoi(optarg);
926 break;
927 case 'r':
928 framerate = atoi(optarg);
929 break;
930 case 'b':
931 cfg.bitrate = atoi(optarg);
932 break;
933 case 'g':
934 cfg.gop_size = atoi(optarg);
935 break;
936 case 'c':
937 if (strlen(optarg) == 4) {
938 CAPTURE_format =
939 v4l2_fourcc(toupper(optarg[0]), toupper(optarg[1]),
940 toupper(optarg[2]), toupper(optarg[3]));
941 printf("using (%s) as the codec\n", optarg);
942 }
943 break;
Miguel Casase4c6efd2021-02-16 22:10:05 -0500944 case 'e':
945 if (strlen(optarg) == 3 && toupper(optarg[0]) == 'C' &&
946 toupper(optarg[1]) == 'B' && toupper(optarg[2]) == 'R') {
947 cfg.bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
948 }
949 break;
Fritz Koenigcdba6532021-01-22 16:11:17 -0800950 case 'v':
951 verbose_capabilities = 1;
952 break;
Miguel Casas266b2e42021-02-05 22:05:58 -0500953 case 'q':
Fritz Koenigcdba6532021-01-22 16:11:17 -0800954 if (strlen(optarg) == 4) {
955 OUTPUT_format =
956 v4l2_fourcc(toupper(optarg[0]), toupper(optarg[1]),
957 toupper(optarg[2]), toupper(optarg[3]));
958 printf("using (%s) as the OUTPUT queue buffer format\n",
959 optarg);
960 }
961 break;
962 default:
963 break;
964 }
965 }
966
967 if (!file_name || width == 0 || height == 0) {
968 fprintf(stderr, "Invalid parameters!\n");
969 print_help(argv[0]);
970 exit(1);
971 }
972
973 FILE *fp = fopen(file_name, "rb");
974 if (!fp) {
975 fprintf(stderr, "%s: unable to open file.\n", file_name);
976 exit(1);
977 }
978
979 if (!frames_to_decode) {
980 fseek(fp, 0, SEEK_END);
981 uint64_t length = ftell(fp);
982 uint32_t frame_size = (3 * width * height) >> 1;
983 frames_to_decode = length / frame_size;
984 fseek(fp, 0, SEEK_SET);
985 }
986
Miguel Casase4c6efd2021-02-16 22:10:05 -0500987 fprintf(stderr, "encoding %d frames using %s bitrate control\n",
988 frames_to_decode,
989 (cfg.bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) ? "CBR" : "VBR");
Fritz Koenigcdba6532021-01-22 16:11:17 -0800990
991 int v4lfd = open(kEncodeDevice, O_RDWR | O_NONBLOCK | O_CLOEXEC);
992 if (v4lfd < 0) {
993 fprintf(stderr, "Unable to open device file: %s\n", kEncodeDevice);
994 exit(EXIT_FAILURE);
995 }
996
997 if (capabilities(v4lfd, OUTPUT_format, CAPTURE_format, verbose_capabilities) != 0) {
998 fprintf(stderr, "Capabilities not present for encode.\n");
999 exit(EXIT_FAILURE);
1000 }
1001
1002 struct queue OUTPUT_queue = { .v4lfd = v4lfd,
1003 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
1004 .fourcc = OUTPUT_format,
1005 .raw_width = width,
1006 .raw_height = height,
1007 .frame_cnt = 0,
Fritz Koenig62733512021-02-17 18:36:21 -08001008 .num_planes = 1,
1009 .framerate = framerate };
Fritz Koenigcdba6532021-01-22 16:11:17 -08001010
1011 struct queue CAPTURE_queue = { .v4lfd = v4lfd,
1012 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1013 .fourcc = CAPTURE_format,
1014 .raw_width = width,
1015 .raw_height = height,
Fritz Koenig62733512021-02-17 18:36:21 -08001016 .num_planes = 1,
1017 .framerate = framerate };
Fritz Koenigcdba6532021-01-22 16:11:17 -08001018
Fritz Koenig62733512021-02-17 18:36:21 -08001019 int ret = Initialization(&OUTPUT_queue, &CAPTURE_queue);
Fritz Koenigcdba6532021-01-22 16:11:17 -08001020
1021 // not all configurations are supported, so we don't need to track
1022 // the return value
1023 if (!ret) {
1024 configure_common(v4lfd, &cfg);
1025
1026 if (v4l2_fourcc('H', '2', '6', '4') == CAPTURE_format)
1027 configure_h264(v4lfd, &cfg);
1028 }
1029
1030 if (!ret)
Miguel Casas266b2e42021-02-05 22:05:58 -05001031 ret = encode(fp, file_format, &OUTPUT_queue, &CAPTURE_queue, frames_to_decode);
Fritz Koenigcdba6532021-01-22 16:11:17 -08001032
1033 cleanup_queue(&OUTPUT_queue);
1034 cleanup_queue(&CAPTURE_queue);
1035 close(v4lfd);
1036
1037 return 0;
1038}