-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcamera_v4l2_cuda.cpp
executable file
·846 lines (699 loc) · 26.5 KB
/
camera_v4l2_cuda.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/*
* Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>
#include <poll.h>
#include <dlfcn.h>
#include "NvVideoConverter.h"
#include "NvEglRenderer.h"
#include "NvUtils.h"
#include "NvCudaProc.h"
#include "nvbuf_utils.h"
#include "Error.h"
#include "camera_v4l2_cuda.h"
#include "opencv_consumer_interface.h"
static bool quit = false;
using namespace std;
static void
print_usage(void)
{
printf("\n\tUsage: camera_v4l2_cuda [OPTIONS]\n\n"
"\tExample: \n"
"\t./camera_v4l2_cuda -d /dev/video0 -s 640x480 -f YUYV -n 30 -c\n\n"
"\tSupported options:\n"
"\t-d\t\tSet V4l2 video device node\n"
"\t-s\t\tSet output resolution of video device\n"
"\t-f\t\tSet output pixel format of video device (supports only YUYV/YVYU/UYVY/VYUY)\n"
"\t-r\t\tSet renderer frame rate (30 fps by default)\n"
"\t-n\t\tSave the n-th frame before VIC processing\n"
"\t-c\t\tEnable CUDA aglorithm (draw a black box in the upper left corner)\n"
"\t-v\t\tEnable verbose message\n"
"\t-h\t\tPrint this usage\n\n"
"\tNOTE: It runs infinitely until you terminate it with <ctrl+c>\n");
}
static bool
parse_cmdline(context_t * ctx, int argc, char **argv)
{
int c;
//if (argc < 2)
//{
// print_usage();
// exit(EXIT_SUCCESS);
//}
while ((c = getopt(argc, argv, "d:s:f:r:n:cvh")) != -1)
{
switch (c)
{
case 'd':
ctx->cam_devname = optarg;
break;
case 's':
if (sscanf(optarg, "%dx%d",
&ctx->cam_w, &ctx->cam_h) != 2)
{
print_usage();
return false;
}
break;
case 'f':
if (strcmp(optarg, "YUYV") == 0)
ctx->cam_pixfmt = V4L2_PIX_FMT_YUYV;
else if (strcmp(optarg, "YVYU") == 0)
ctx->cam_pixfmt = V4L2_PIX_FMT_YVYU;
else if (strcmp(optarg, "VYUY") == 0)
ctx->cam_pixfmt = V4L2_PIX_FMT_VYUY;
else if (strcmp(optarg, "UYVY") == 0)
ctx->cam_pixfmt = V4L2_PIX_FMT_UYVY;
else
{
print_usage();
return false;
}
sprintf(ctx->cam_file, "camera.%s", optarg);
break;
case 'r':
ctx->fps = strtol(optarg, NULL, 10);
break;
case 'n':
ctx->save_n_frame = strtol(optarg, NULL, 10);
break;
case 'c':
ctx->enable_cuda = true;
break;
case 'v':
ctx->enable_verbose = true;
break;
case 'h':
print_usage();
exit(EXIT_SUCCESS);
break;
default:
print_usage();
return false;
}
}
return true;
}
static int
set_defaults(context_t * ctx)
{
memset(ctx, 0, sizeof(context_t));
ctx->cam_devname = "/dev/video0";
ctx->cam_fd = -1;
ctx->cam_pixfmt = V4L2_PIX_FMT_YUYV;
ctx->cam_w = 640;
ctx->cam_h = 480;
ctx->frame = 0;
ctx->save_n_frame = 0;
ctx->conv = NULL;
ctx->vic_pixfmt = V4L2_PIX_FMT_ABGR32;//V4L2_PIX_FMT_YUV420M;
ctx->vic_flip = (enum v4l2_flip_method) -1;
ctx->vic_interpolation = (enum v4l2_interpolation_method) -1;
ctx->vic_tnr = (enum v4l2_tnr_algorithm) -1;
ctx->g_buff = NULL;
ctx->renderer = NULL;
ctx->got_error = false;
ctx->fps = 30;
ctx->conv_output_plane_buf_queue = new queue < nv_buffer * >;
pthread_mutex_init(&ctx->queue_lock, NULL);
pthread_cond_init(&ctx->queue_cond, NULL);
ctx->enable_cuda = false;
ctx->egl_image = NULL;
ctx->egl_display = EGL_NO_DISPLAY;
ctx->lib_file = "/home/nvidia/tegra_multimedia_api/samples/12_camera_v4l2_cuda/opencv_consumer_lib/libopencv_consumer.so";
ctx->mean_file = "/home/ubuntu/Work/caffe/caffe-master/data/ilsvrc12/imagenet_mean.binaryproto";
ctx->model_file = "/home/ubuntu/Work/caffe/caffe-master/models/bvlc_reference_caffenet/deploy.prototxt";
ctx->trained_file = "/home/ubuntu/Work/caffe/caffe-master/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel";
ctx->label_file = "/home/ubuntu/Work/caffe/caffe-master/data/ilsvrc12/synset_words.txt";
// opencv processor initialize
//ctx->lib_handler = dlopen("/home/nvidia/tegra_multimedia_api/samples/12_camera_v4l2_cuda/opencv_consumer_lib/libopencv_consumer.so", RTLD_LAZY);
//if (!ctx->lib_handler)
//{
// printf("Fail to open LIB, continue to run without processing\n");
//}
//else
//{
// ctx->opencv_handler_open = (opencv_handler_open_t)opencv_handler_open;
// if (!ctx->opencv_handler_open)
// ORIGINATE_ERROR("Fail to get sym opencv_handler_open");
// ctx->opencv_handler_close = (opencv_handler_close_t)opencv_handler_close;
// if (!ctx->opencv_handler_close)
// ORIGINATE_ERROR("Fail to get sym opencv_handler_close");
// ctx->opencv_img_processing = (opencv_img_processing_t)opencv_img_processing;
// if (!ctx->opencv_img_processing)
// ORIGINATE_ERROR("Fail to get sym opencv_img_processing");
// ctx->opencv_set_config = (opencv_set_config_t)opencv_set_config;
// if (!ctx->opencv_set_config)
// ORIGINATE_ERROR("Fail to get sym opencv_set_config");
ctx->opencv_handler = opencv_handler_open();
if (!ctx->opencv_handler)
ORIGINATE_ERROR("Fail to opencv_handler_open");
// configure opencv consumer
opencv_set_config(ctx->opencv_handler
, OPENCV_CONSUMER_CONFIG_IMGWIDTH, &ctx->cam_w);
opencv_set_config(ctx->opencv_handler
, OPENCV_CONSUMER_CONFIG_IMGHEIGHT, &ctx->cam_h);
opencv_set_config(ctx->opencv_handler
, OPENCV_CONSUMER_CONFIG_CAFFE_MODELFILE, ctx->model_file);
opencv_set_config(ctx->opencv_handler
, OPENCV_CONSUMER_CONFIG_CAFFE_TRAINEDFILE, ctx->trained_file);
opencv_set_config(ctx->opencv_handler
, OPENCV_CONSUMER_CONFIG_CAFFE_MEANFILE, ctx->mean_file);
opencv_set_config(ctx->opencv_handler
, OPENCV_CONSUMER_CONFIG_CAFFE_LABELFILE, ctx->label_file);
opencv_set_config(ctx->opencv_handler
, OPENCV_CONSUMER_CONFIG_START, NULL);
//}
ctx->enable_verbose = false;
return true;
}
static nv_color_fmt nvcolor_fmt[] =
{
// TODO add more pixel format mapping
{V4L2_PIX_FMT_UYVY, NvBufferColorFormat_UYVY},
{V4L2_PIX_FMT_VYUY, NvBufferColorFormat_VYUY},
{V4L2_PIX_FMT_YUYV, NvBufferColorFormat_YUYV},
{V4L2_PIX_FMT_YVYU, NvBufferColorFormat_YVYU},
};
static NvBufferColorFormat
get_nvbuff_color_fmt(unsigned int v4l2_pixfmt)
{
unsigned i;
for (i = 0; i < sizeof(nvcolor_fmt) / sizeof(nvcolor_fmt[0]); i++)
{
if (v4l2_pixfmt == nvcolor_fmt[i].v4l2_pixfmt)
return nvcolor_fmt[i].nvbuff_color;
}
return NvBufferColorFormat_Invalid;
}
static bool
save_frame_to_file(context_t * ctx, struct v4l2_buffer * buf)
{
int file;
file = open(ctx->cam_file, O_CREAT | O_WRONLY | O_APPEND | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (-1 == file)
ERROR_RETURN("Failed to open file for frame saving");
if (-1 == write(file, ctx->g_buff[buf->index].start,
ctx->g_buff[buf->index].size))
{
close(file);
ERROR_RETURN("Failed to write frame into file");
}
close(file);
return true;
}
static bool
camera_initialize(context_t * ctx)
{
struct v4l2_format fmt;
// Open camera device
ctx->cam_fd = open(ctx->cam_devname, O_RDWR);
if (ctx->cam_fd == -1)
ERROR_RETURN("Failed to open camera device %s: %s (%d)",
ctx->cam_devname, strerror(errno), errno);
// Set camera output format
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = ctx->cam_w;
fmt.fmt.pix.height = ctx->cam_h;
fmt.fmt.pix.pixelformat = ctx->cam_pixfmt;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
if (ioctl(ctx->cam_fd, VIDIOC_S_FMT, &fmt) < 0)
ERROR_RETURN("Failed to set camera output format: %s (%d)",
strerror(errno), errno);
// Get the real format in case the desired is not supported
memset(&fmt, 0, sizeof fmt);
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl(ctx->cam_fd, VIDIOC_G_FMT, &fmt) < 0)
ERROR_RETURN("Failed to get camera output format: %s (%d)",
strerror(errno), errno);
if (fmt.fmt.pix.width != ctx->cam_w ||
fmt.fmt.pix.height != ctx->cam_h ||
fmt.fmt.pix.pixelformat != ctx->cam_pixfmt)
{
WARN("The desired format is not supported");
ctx->cam_w = fmt.fmt.pix.width;
ctx->cam_h = fmt.fmt.pix.height;
ctx->cam_pixfmt =fmt.fmt.pix.pixelformat;
}
INFO("Camera ouput format: (%d x %d) stride: %d, imagesize: %d",
fmt.fmt.pix.width,
fmt.fmt.pix.height,
fmt.fmt.pix.bytesperline,
fmt.fmt.pix.sizeimage);
return true;
}
static bool
vic_initialize(context_t * ctx)
{
// Create VIC (VIdeo Converter) instance
ctx->conv = NvVideoConverter::createVideoConverter("conv");
if (ctx->conv == NULL)
ERROR_RETURN("Failed to create video converter");
if (ctx->vic_flip != -1 && ctx->conv->setFlipMethod(ctx->vic_flip) < 0)
ERROR_RETURN("Failed to set flip method");
if (ctx->vic_interpolation != -1 &&
ctx->conv->setInterpolationMethod(ctx->vic_interpolation) < 0)
ERROR_RETURN("Failed to set interpolation method");
if (ctx->vic_tnr != -1 && ctx->conv->setTnrAlgorithm(ctx->vic_tnr) < 0)
ERROR_RETURN("Failed to set tnr algorithm");
// Set up VIC output plane format
if (ctx->conv->setOutputPlaneFormat(ctx->cam_pixfmt, ctx->cam_w,
ctx->cam_h, V4L2_NV_BUFFER_LAYOUT_PITCH) < 0)
ERROR_RETURN("Failed to set up VIC output plane format");
// Set up VIC capture plane format
// The target format can be reconfigured from set_defaults()
if (ctx->conv->setCapturePlaneFormat(ctx->vic_pixfmt, ctx->cam_w,
ctx->cam_h, V4L2_NV_BUFFER_LAYOUT_PITCH) < 0)
ERROR_RETURN("Failed to set up VIC capture plane format");
// Allocate VIC output plane
if (ctx->conv->output_plane.setupPlane(V4L2_MEMORY_DMABUF,
V4L2_BUFFERS_NUM, false, false) < 0)
ERROR_RETURN("Failed to allocate VIC output plane");
// Allocate VIC capture plane
if (ctx->conv->capture_plane.setupPlane(V4L2_MEMORY_MMAP,
V4L2_BUFFERS_NUM, true, false) < 0)
ERROR_RETURN("Failed to allocate VIC capture plane");
return true;
}
static bool
display_initialize(context_t * ctx)
{
// Create EGL renderer
ctx->renderer = NvEglRenderer::createEglRenderer("renderer0",
ctx->cam_w, ctx->cam_h, 0, 0);
if (!ctx->renderer)
ERROR_RETURN("Failed to create EGL renderer");
ctx->renderer->setFPS(ctx->fps);
if (ctx->enable_cuda)
{
// Get defalut EGL display
ctx->egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (ctx->egl_display == EGL_NO_DISPLAY)
ERROR_RETURN("Failed to get EGL display connection");
// Init EGL display connection
if (!eglInitialize(ctx->egl_display, NULL, NULL))
ERROR_RETURN("Failed to initialize EGL display connection");
}
return true;
}
static bool
init_components(context_t * ctx)
{
if (!camera_initialize(ctx))
ERROR_RETURN("Failed to initialize camera device");
if (!vic_initialize(ctx))
ERROR_RETURN("Failed to initialize video converter");
if (!display_initialize(ctx))
ERROR_RETURN("Failed to initialize display");
INFO("Initialize v4l2 components successfully");
return true;
}
static bool
request_camera_buff(context_t *ctx)
{
// Request camera v4l2 buffer
struct v4l2_requestbuffers rb;
memset(&rb, 0, sizeof(rb));
rb.count = V4L2_BUFFERS_NUM;
rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
rb.memory = V4L2_MEMORY_DMABUF;
if (ioctl(ctx->cam_fd, VIDIOC_REQBUFS, &rb) < 0)
ERROR_RETURN("Failed to request v4l2 buffers: %s (%d)",
strerror(errno), errno);
if (rb.count != V4L2_BUFFERS_NUM)
ERROR_RETURN("V4l2 buffer number is not as desired");
for (unsigned int index = 0; index < V4L2_BUFFERS_NUM; index++)
{
struct v4l2_buffer buf;
// Query camera v4l2 buf length
memset(&buf, 0, sizeof buf);
buf.index = index;
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_DMABUF;
if (ioctl(ctx->cam_fd, VIDIOC_QUERYBUF, &buf) < 0)
ERROR_RETURN("Failed to query buff: %s (%d)",
strerror(errno), errno);
// TODO add support for multi-planer
// Enqueue empty v4l2 buff into camera capture plane
buf.m.fd = (unsigned long)ctx->g_buff[index].dmabuff_fd;
if (buf.length != ctx->g_buff[index].size)
{
WARN("Camera v4l2 buf length is not expected");
ctx->g_buff[index].size = buf.length;
}
if (ioctl(ctx->cam_fd, VIDIOC_QBUF, &buf) < 0)
ERROR_RETURN("Failed to enqueue buffers: %s (%d)",
strerror(errno), errno);
}
return true;
}
static bool
enqueue_vic_buff(context_t *ctx)
{
// Enqueue empty buffer into VIC output plane
for (unsigned int index = 0;
index < ctx->conv->output_plane.getNumBuffers(); index++)
{
struct v4l2_buffer v4l2_buf;
struct v4l2_plane planes[MAX_PLANES];
memset(&v4l2_buf, 0, sizeof(v4l2_buf));
memset(planes, 0, MAX_PLANES * sizeof(struct v4l2_plane));
v4l2_buf.index = index;
v4l2_buf.m.planes = planes;
if (ctx->conv->output_plane.qBuffer(v4l2_buf, NULL) < 0)
ERROR_RETURN("Failed to enqueue empty buffer into VIC output plane");
}
// Enqueue empty buffer into VIC capture plane
for (unsigned int index = 0;
index < ctx->conv->capture_plane.getNumBuffers(); index++)
{
struct v4l2_buffer v4l2_buf;
struct v4l2_plane planes[MAX_PLANES];
memset(&v4l2_buf, 0, sizeof(v4l2_buf));
memset(planes, 0, MAX_PLANES * sizeof(struct v4l2_plane));
v4l2_buf.index = index;
v4l2_buf.m.planes = planes;
if (ctx->conv->capture_plane.qBuffer(v4l2_buf, NULL) < 0)
ERROR_RETURN("Failed to enqueue empty buffer into VIC capture plane");
}
return true;
}
static bool
prepare_buffers(context_t * ctx)
{
// Allocate global buffer context
ctx->g_buff = (nv_buffer *)malloc(V4L2_BUFFERS_NUM * sizeof(nv_buffer));
if (ctx->g_buff == NULL)
ERROR_RETURN("Failed to allocate global buffer context");
// Create buffer and share it with camera and VIC output plane
for (unsigned int index = 0; index < V4L2_BUFFERS_NUM; index++)
{
int fd;
NvBufferParams params = {0};
if (-1 == NvBufferCreate(&fd, ctx->cam_w, ctx->cam_h,
NvBufferLayout_Pitch,
get_nvbuff_color_fmt(ctx->cam_pixfmt)))
ERROR_RETURN("Failed to create NvBuffer");
ctx->g_buff[index].dmabuff_fd = fd;
if (-1 == NvBufferGetParams(fd, ¶ms))
ERROR_RETURN("Failed to get NvBuffer parameters");
// TODO add multi-planar support
// Currently it supports only YUV422 interlaced single-planar
ctx->g_buff[index].size = params.height[0] * params.pitch[0];
ctx->g_buff[index].start = (unsigned char *)mmap(
NULL,
ctx->g_buff[index].size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
ctx->g_buff[index].dmabuff_fd, 0);
}
if (!request_camera_buff(ctx))
ERROR_RETURN("Failed to set up camera buff");
if (!enqueue_vic_buff(ctx))
ERROR_RETURN("Failed to enqueue empty buff into VIC");
INFO("Succeed in preparing stream buffers");
return true;
}
static bool
start_stream(context_t * ctx)
{
enum v4l2_buf_type type;
// Start v4l2 streaming
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl(ctx->cam_fd, VIDIOC_STREAMON, &type) < 0)
ERROR_RETURN("Failed to start streaming: %s (%d)",
strerror(errno), errno);
// Start VIC output plane
if (ctx->conv->output_plane.setStreamStatus(true) < 0)
ERROR_RETURN("Failed to start VIC output plane streaming");
// Start VIC capture plane
if (ctx->conv->capture_plane.setStreamStatus(true) < 0)
ERROR_RETURN("Failed to start VIC capture plane streaming");
usleep(200);
INFO("Camera video streaming on ...");
return true;
}
static void
abort(context_t *ctx)
{
ctx->got_error = true;
if (ctx->conv)
ctx->conv->abort();
}
static bool
conv_output_dqbuf_thread_callback(struct v4l2_buffer *v4l2_buf,
NvBuffer * buffer, NvBuffer * shared_buffer,
void *arg)
{
context_t *ctx = (context_t *) arg;
nv_buffer * cam_g_buff;
if (!v4l2_buf)
{
abort(ctx);
ERROR_RETURN("Failed to dequeue conv output plane buffer");
}
// Fetch nv_buffer to do format conversion
pthread_mutex_lock(&ctx->queue_lock);
while (ctx->conv_output_plane_buf_queue->empty())
{
pthread_cond_wait(&ctx->queue_cond, &ctx->queue_lock);
}
cam_g_buff = ctx->conv_output_plane_buf_queue->front();
ctx->conv_output_plane_buf_queue->pop();
pthread_mutex_unlock(&ctx->queue_lock);
// Got EOS signal and return
if (cam_g_buff->dmabuff_fd == 0)
return false;
else
{
// Enqueue vic output plane
v4l2_buf->m.planes[0].m.fd =
(unsigned long)cam_g_buff->dmabuff_fd;
v4l2_buf->m.planes[0].bytesused = cam_g_buff->size;
}
if (ctx->conv->output_plane.qBuffer(*v4l2_buf, NULL) < 0)
{
abort(ctx);
ERROR_RETURN("Failed to enqueue VIC output plane");
}
return true;
}
static void writeFrameToOpencvConsumer(context_t *ctx, NvBuffer *buffer)
{
NvBuffer::NvBufferPlane *plane = &buffer->planes[0];
uint8_t *pdata = (uint8_t *) plane->data;
// output RGB frame to opencv, only 1 plane
if (ctx->opencv_handler)
{
opencv_img_processing(ctx->opencv_handler
, pdata, plane->fmt.width, plane->fmt.height);
}
}
static bool
conv_capture_dqbuf_thread_callback(struct v4l2_buffer *v4l2_buf,
NvBuffer * buffer, NvBuffer * shared_buffer,
void *arg)
{
context_t *ctx = (context_t *) arg;
if (ctx->enable_cuda)
{
// Create EGLImage from dmabuf fd
ctx->egl_image = NvEGLImageFromFd(ctx->egl_display, buffer->planes[0].fd);
if (ctx->egl_image == NULL)
ERROR_RETURN("Failed to map dmabuf fd (0x%X) to EGLImage",
buffer->planes[0].fd);
// Running algo process with EGLImage via GPU multi cores
HandleEGLImage(&ctx->egl_image);
// Destroy EGLImage
NvDestroyEGLImage(ctx->egl_display, ctx->egl_image);
ctx->egl_image = NULL;
}
writeFrameToOpencvConsumer(ctx, buffer);
// Render the frame into display
//if (v4l2_buf->m.planes[0].bytesused)
// ctx->renderer->render(buffer->planes[0].fd);
if (ctx->conv->capture_plane.qBuffer(*v4l2_buf, buffer) < 0)
{
abort(ctx);
ERROR_RETURN("Failed to queue buffer on VIC capture plane");
}
return true;
}
static void
signal_handle(int signum)
{
printf("Quit due to exit command from user!\n");
quit = true;
}
static bool
start_capture(context_t * ctx)
{
struct sigaction sig_action;
struct pollfd fds[1];
// Ensure a clean shutdown if user types <ctrl+c>
sig_action.sa_handler = signal_handle;
sigemptyset(&sig_action.sa_mask);
sig_action.sa_flags = 0;
sigaction(SIGINT, &sig_action, NULL);
ctx->conv->capture_plane.setDQThreadCallback(conv_capture_dqbuf_thread_callback);
ctx->conv->output_plane.setDQThreadCallback(conv_output_dqbuf_thread_callback);
// Start VIC processing thread
ctx->conv->capture_plane.startDQThread(ctx);
ctx->conv->output_plane.startDQThread(ctx);
// Enable render profiling information
ctx->renderer->enableProfiling();
fds[0].fd = ctx->cam_fd;
fds[0].events = POLLIN;
while (poll(fds, 1, 5000) > 0 && !ctx->got_error &&
!ctx->conv->isInError() && !quit)
{
if (fds[0].revents & POLLIN) {
struct v4l2_buffer v4l2_buf;
// Dequeue camera buff
memset(&v4l2_buf, 0, sizeof(v4l2_buf));
v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
v4l2_buf.memory = V4L2_MEMORY_DMABUF;
if (ioctl(ctx->cam_fd, VIDIOC_DQBUF, &v4l2_buf) < 0)
ERROR_RETURN("Failed to dequeue camera buff: %s (%d)",
strerror(errno), errno);
ctx->frame++;
if (ctx->frame == ctx->save_n_frame)
save_frame_to_file(ctx, &v4l2_buf);
// Push nv_buffer into conv output queue for conversion
pthread_mutex_lock(&ctx->queue_lock);
ctx->conv_output_plane_buf_queue->push(&ctx->g_buff[v4l2_buf.index]);
pthread_cond_broadcast(&ctx->queue_cond);
pthread_mutex_unlock(&ctx->queue_lock);
// Enqueue camera buff
// It might be more reasonable to wait for the completion of
// VIC processing before enqueue current buff. But VIC processing
// time is far less than camera frame interval, so we probably
// don't need such synchonization.
if (ioctl(ctx->cam_fd, VIDIOC_QBUF, &v4l2_buf))
ERROR_RETURN("Failed to queue camera buffers: %s (%d)",
strerror(errno), errno);
}
}
if (quit && !ctx->conv->isInError())
{
// Signal EOS to the dq thread of VIC output plane
ctx->g_buff[0].dmabuff_fd = 0;
pthread_mutex_lock(&ctx->queue_lock);
ctx->conv_output_plane_buf_queue->push(&ctx->g_buff[0]);
pthread_cond_broadcast(&ctx->queue_cond);
pthread_mutex_unlock(&ctx->queue_lock);
}
// Stop VIC dq thread
if (!ctx->got_error)
{
ctx->conv->output_plane.deinitPlane();
ctx->conv->capture_plane.deinitPlane();
}
// Print profiling information when streaming stops.
ctx->renderer->printProfilingStats();
return true;
}
static bool
stop_stream(context_t * ctx)
{
enum v4l2_buf_type type;
// Stop v4l2 streaming
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl(ctx->cam_fd, VIDIOC_STREAMOFF, &type))
ERROR_RETURN("Failed to stop streaming: %s (%d)",
strerror(errno), errno);
// Stop VIC output plane
if (ctx->conv->output_plane.setStreamStatus(false) < 0)
ERROR_RETURN("Failed to stop output plane streaming");
// Stop VIC capture plane
if (ctx->conv->capture_plane.setStreamStatus(false) < 0)
ERROR_RETURN("Failed to stop output plane streaming");
INFO("Camera video streaming off ...");
return true;
}
int
main(int argc, char *argv[])
{
context_t ctx;
int error = 0;
set_defaults(&ctx);
CHECK_ERROR(parse_cmdline(&ctx, argc, argv), cleanup,
"Invalid options specified");
CHECK_ERROR(init_components(&ctx), cleanup,
"Failed to initialize v4l2 components");
CHECK_ERROR(prepare_buffers(&ctx), cleanup,
"Failed to prepare v4l2 buffs");
CHECK_ERROR(start_stream(&ctx), cleanup,
"Failed to start streaming");
CHECK_ERROR(start_capture(&ctx), cleanup,
"Failed to start capturing")
CHECK_ERROR(stop_stream(&ctx), cleanup,
"Failed to stop streaming");
cleanup:
if (ctx.cam_fd > 0)
close(ctx.cam_fd);
if (ctx.renderer != NULL)
delete ctx.renderer;
if (ctx.egl_display && !eglTerminate(ctx.egl_display))
printf("Failed to terminate EGL display connection\n");
if (ctx.conv != NULL)
{
if (ctx.conv->isInError())
{
printf("Video converter is in error\n");
error = 1;
}
delete ctx.conv;
}
if (ctx.g_buff != NULL)
{
for (unsigned i = 0; i < V4L2_BUFFERS_NUM; i++)
if (ctx.g_buff[i].dmabuff_fd)
NvBufferDestroy(ctx.g_buff[i].dmabuff_fd);
free(ctx.g_buff);
}
while(!ctx.conv_output_plane_buf_queue->empty())
{
ctx.conv_output_plane_buf_queue->pop();
}
delete ctx.conv_output_plane_buf_queue;
if (error)
printf("App run failed\n");
else
printf("App run was successful\n");
return -error;
}