-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathm2m-test.c
766 lines (621 loc) · 22.1 KB
/
m2m-test.c
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
/*
* Copyright (C) 2009 by Pawel Osciak, p.osciak <at> samsung.com
* Copyright (C) 2009 by Samsung Electronics Co., Ltd.
* Copyright (C) 2012 by Tomasz Moń <[email protected]>
* Copyright (C) 2015-2016 by Anton Leontiev <[email protected]>
*
* Based on V4L2 video capture example and process-vmalloc.c
* Capture + output (process) V4L2 device tester.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <error.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <linux/videodev2.h>
#include <libavdevice/avdevice.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/dict.h>
#include <libavutil/imgutils.h>
#include <libavutil/opt.h>
#include <libavutil/pixdesc.h>
#include <libavutil/time.h>
#include "m420.h"
#include "log.h"
#include "v4l2-utils.h"
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#define ROUND_UP(x, a) (((x)+(a)-1)&~((a)-1))
#define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_PRIVATE_BASE)
#define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_PRIVATE_BASE + 1)
#define NUM_BUFS 4
/* Some evident defines */
#define MSEC_IN_SEC 1000
#define USEC_IN_SEC 1000000
#define NSEC_IN_SEC 1000000000
static struct ctrl avico_mpeg_ctrls[] = {
{
.id = V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
},
{
.id = V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
},
{
.id = V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET
},
{
.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE
},
{
.id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE
},
{
.id = V4L2_CID_MPEG_VIDEO_BITRATE
}
};
static struct class_ctrls avico_ctrls[] = {
{
.which = V4L2_CTRL_CLASS_MPEG,
.ctrls = avico_mpeg_ctrls,
.cnt = ARRAY_SIZE(avico_mpeg_ctrls)
}
};
static struct m2m_buffer {
struct v4l2_buffer v4l2;
void *buf;
AVFrame *frame;
} out_bufs[NUM_BUFS], cap_bufs[NUM_BUFS];
bool drop = false;
static inline bool is_valid_out_buf(unsigned const outn)
{
return outn < NUM_BUFS && out_bufs[outn].buf;
}
static void m2m_vim2m_controls(int const fd) {
bool hflip = false, vflip = false;
int rc;
struct v4l2_control ctrl;
pr_verb("M2M: Setup vim2m controls...");
if (hflip) {
ctrl.id = V4L2_CID_HFLIP;
ctrl.value = 1;
rc = ioctl(fd, VIDIOC_S_CTRL, &ctrl);
if (rc != 0) error(EXIT_SUCCESS, errno, "Can not set HFLIP");
}
if (vflip) {
ctrl.id = V4L2_CID_VFLIP;
ctrl.value = 1;
rc = ioctl(fd, VIDIOC_S_CTRL, &ctrl);
if (rc != 0) error(EXIT_SUCCESS, errno, "Can not set VFLIP");
}
ctrl.id = V4L2_CID_TRANS_TIME_MSEC;
ctrl.value = 100;
rc = ioctl(fd, VIDIOC_S_CTRL, &ctrl);
if (rc != 0) error(EXIT_SUCCESS, errno, "Can not set transaction time");
ctrl.id = V4L2_CID_TRANS_NUM_BUFS;
ctrl.value = 1;
rc = ioctl(fd, VIDIOC_S_CTRL, &ctrl);
if (rc != 0) error(EXIT_SUCCESS, errno, "Can not set transaction length");
}
static void m2m_buffers_get(int const fd, bool userptr) {
int rc;
pr_verb("M2M: Obtaining buffers...");
struct v4l2_requestbuffers outreqbuf = {
.count = NUM_BUFS,
.type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
.memory = userptr ? V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP
};
struct v4l2_requestbuffers capreqbuf = {
.count = NUM_BUFS,
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
.memory = V4L2_MEMORY_MMAP
};
rc = ioctl(fd, VIDIOC_REQBUFS, &outreqbuf);
if (rc != 0) error(EXIT_FAILURE, errno, "Can not request output buffers");
if (outreqbuf.count == 0) error(EXIT_FAILURE, 0, "Device gives zero output buffers");
pr_debug("M2M: Got %d output buffers", outreqbuf.count);
rc = ioctl(fd, VIDIOC_REQBUFS, &capreqbuf);
if (rc != 0) error(EXIT_FAILURE, errno, "Can not request capture buffers");
if (capreqbuf.count == 0) error(EXIT_FAILURE, 0, "Device gives zero capture buffers");
pr_debug("M2M: Got %d capture buffers", capreqbuf.count);
for (int i = 0; i < outreqbuf.count; ++i) {
struct v4l2_buffer *vbuf = &out_bufs[i].v4l2;
vbuf->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
vbuf->memory = userptr ? V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
vbuf->index = i;
vbuf->flags = 0;
rc = ioctl(fd, VIDIOC_QUERYBUF, vbuf);
if (rc != 0) error(EXIT_FAILURE, errno, "Can not query output buffer");
pr_debug("M2M: Got output buffer #%u: length = %u", i, vbuf->length);
if (userptr) {
out_bufs[i].buf = malloc(vbuf->length);
if (!out_bufs[i].buf) error(EXIT_FAILURE, errno, "Out of memory");
out_bufs[i].v4l2.m.userptr = (unsigned long)out_bufs[i].buf;
} else {
out_bufs[i].buf = mmap(NULL, vbuf->length, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, vbuf->m.offset);
if (out_bufs[i].buf == MAP_FAILED)
error(EXIT_FAILURE, errno, "Can not mmap output buffer");
}
}
for (int i = 0; i < capreqbuf.count; ++i) {
struct v4l2_buffer *vbuf = &cap_bufs[i].v4l2;
vbuf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
vbuf->memory = V4L2_MEMORY_MMAP;
vbuf->index = i;
vbuf->flags = 0;
rc = ioctl(fd, VIDIOC_QUERYBUF, vbuf);
if (rc != 0) error(EXIT_FAILURE, errno, "Can not query capture buffer");
pr_debug("M2M: Got capture buffer #%u: length = %u", i, vbuf->length);
cap_bufs[i].buf = mmap(NULL, vbuf->length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, vbuf->m.offset);
if (cap_bufs[i].buf == MAP_FAILED) error(EXIT_FAILURE, errno, "Can not mmap capture buffer");
}
for (int i = 0; i < capreqbuf.count; ++i) {
struct v4l2_buffer buf = {
.index = i,
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
.memory = V4L2_MEMORY_MMAP
};
v4l2_qbuf(fd, &buf);
}
}
static void queue_outbuf(int const fd, struct SwsContext *dsc, AVFrame * const iframe,
bool const transform, unsigned const index)
{
sws_scale(dsc, (uint8_t const * const*)iframe->data,
iframe->linesize, 0, iframe->height,
out_bufs[index].frame->data,
out_bufs[index].frame->linesize);
/* Process frame */
if (transform)
yuv420_to_m420(out_bufs[index].frame);
out_bufs[index].v4l2.bytesused = out_bufs[index].frame->linesize[0] *
out_bufs[index].frame->height * 3 / 2;
out_bufs[index].v4l2.flags = 0;
v4l2_qbuf(fd, &out_bufs[index].v4l2);
}
static void dequeue_outbuf(int const fd, unsigned const index)
{
v4l2_dqbuf(fd, &out_bufs[index].v4l2);
if (index != out_bufs[index].v4l2.index)
error(EXIT_FAILURE, 0, "Error index of buffer.");
}
static unsigned process_capbuf(int const fd, int const outfd)
{
int rc = 0;
struct v4l2_buffer buf = {
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
.memory = V4L2_MEMORY_MMAP
};
unsigned bytesused = 0;
v4l2_dqbuf(fd, &buf);
if (drop && (buf.flags & V4L2_BUF_FLAG_ERROR)) {
pr_info("Buffer #%d is dropped", buf.sequence);
} else {
bytesused = buf.bytesused;
if (outfd >= 0) {
rc = write(outfd, cap_bufs[buf.index].buf, buf.bytesused);
if (rc < 0)
error(EXIT_FAILURE, errno, "Can not write to output");
}
}
buf.flags = 0;
buf.bytesused = 0;
v4l2_qbuf(fd, &buf);
return bytesused;
}
static void m2m_process(int const fd, int const outfd, struct SwsContext *dsc,
AVFrame * const iframe, bool const transform, unsigned *const encframe,
uint64_t *const outsize, unsigned const outn)
{
int rc = 0;
unsigned bytesused = 0;
if (!(out_bufs[outn].v4l2.flags & V4L2_BUF_FLAG_QUEUED)) {
queue_outbuf(fd, dsc, iframe, transform, outn);
} else {
struct pollfd fds[1] = {
{ fd, POLLOUT | POLLIN }
};
while (1) {
rc = poll(fds, 1, 1000);
if (rc < 0)
error(EXIT_FAILURE, errno, "Poll error");
if (rc == 0)
error(EXIT_FAILURE, 0, "Timeout waiting for data...");
if (fds[0].revents & POLLIN) {
bytesused = process_capbuf(fd, outfd);
*outsize += bytesused;
pr_verb("Compressed frame %u (%u bytes)", *encframe, bytesused);
*encframe += 1;
}
if (fds[0].revents & POLLOUT) {
dequeue_outbuf(fd, outn);
queue_outbuf(fd, dsc, iframe, transform, outn);
break;
}
}
}
}
static inline struct timespec timespec_subtract(struct timespec const start,
struct timespec const stop)
{
struct timespec res = {
.tv_sec = stop.tv_sec - start.tv_sec,
.tv_nsec = stop.tv_nsec - start.tv_nsec
};
if (res.tv_nsec < 0) {
res.tv_sec -= 1;
res.tv_nsec += NSEC_IN_SEC;
}
return res;
}
static inline struct timespec timespec_add(struct timespec const x,
struct timespec const y)
{
struct timespec res = {
.tv_sec = y.tv_sec + x.tv_sec,
.tv_nsec = y.tv_nsec + x.tv_nsec
};
if (res.tv_nsec > NSEC_IN_SEC) {
res.tv_sec += 1;
res.tv_nsec -= NSEC_IN_SEC;
}
return res;
}
static inline unsigned timespec2msec(struct timespec const t)
{
return t.tv_sec * MSEC_IN_SEC + t.tv_nsec / (NSEC_IN_SEC / MSEC_IN_SEC);
}
static inline float timespec2float(struct timespec const t)
{
return t.tv_sec + (float)t.tv_nsec / 1e9;
}
static inline bool checklimit(unsigned const value, unsigned const limit)
{
return limit == 0 || value < limit;
}
/*
* Limitations: The next parts work synchronously and can influence
* each other and overall test performance:
* - functions of FFmpeg
* - yuv420_to_m420()
* - writing of processed (V4L2_BUF_TYPE_VIDEO_CAPTURE) frame
*/
static unsigned process_stream(AVFormatContext *const ifc,
AVCodecContext *const icc, int const stream, struct SwsContext *dsc,
unsigned const offset, unsigned const frames, bool const transform,
int const m2mfd, int const outfd, unsigned *const encframe,
uint64_t *const outsize)
{
static int64_t start_pts = 0;
static unsigned frame = 0, skipped = 0, outn = 0;
AVPacket packet;
int rc = 0;
AVFrame *iframe = av_frame_alloc();
if (iframe == NULL)
error(EXIT_FAILURE, 0, "Can not allocate memory for input frame");
while (checklimit(frame, frames)) {
rc = av_read_frame(ifc, &packet);
if (rc == AVERROR_EOF)
break; /// \todo Draining
else if (rc != 0)
error(EXIT_FAILURE, 0, "Failed to read next packet: %d", rc);
if (!start_pts) start_pts = packet.pts;
if (packet.stream_index != stream)
goto forth;
rc = avcodec_send_packet(icc, &packet);
if (rc)
error(EXIT_FAILURE, 0, "Failed to send packet to decoder");
while((rc = avcodec_receive_frame(icc, iframe)) == 0) {
pr_verb("Frame is read...");
if (skipped < offset) {
skipped++;
pr_verb("Frame skipped!");
continue;
}
m2m_process(m2mfd, outfd, dsc, iframe, transform, encframe, outsize, outn);
if (!is_valid_out_buf(++outn))
outn = 0;
/*if (ofc) {
AVPacket packet = { };
int finished;
if (osc) sws_scale(osc, (uint8_t const* const*)cap_bufs[0].frame->data, cap_bufs[0].frame->linesize, 0, cap_bufs[0].frame->height,
oframe->data, oframe->linesize);
av_init_packet(&packet);
packet.stream_index = 0;
// \todo Use processed video
rc = avcodec_encode_video2(occ, &packet, oframe ?: cap_bufs[0].frame, &finished);
if (rc < 0) error(EXIT_FAILURE, 0, "Can not encode frame");
if (finished) {
rc = av_interleaved_write_frame(ofc, &packet);
if (rc < 0) error(EXIT_FAILURE, 0, "Can not write output packet");
}
}*/
frame += 1;
}
if (rc != 0 && rc != AVERROR(EAGAIN) && rc != AVERROR_EOF)
error(EXIT_FAILURE, 0, "Failed to read decoded frame");
forth:
// Unref data that was allocated by av_read_frame()
av_packet_unref(&packet);
/* if (ofc) av_write_trailer(ofc); */
}
av_frame_free(&iframe);
return frame;
}
static void m2m_drain(int const fd, int const outfd, unsigned encframe, unsigned const frames,
uint64_t *const outsize)
{
unsigned bytesused = 0;
while (checklimit(encframe, frames)) {
bytesused = process_capbuf(fd, outfd);
*outsize += bytesused;
pr_verb("Compressed frame %u (%u bytes)", encframe, bytesused);
encframe += 1;
}
}
#ifndef VERSION
#define VERSION "unversioned"
#endif
static void help(const char *program_name) {
puts("m2m-test " VERSION " \n");
printf("Synopsys: %s -d device [options] file | /dev/videoX\n\n", program_name);
puts("Options:");
puts(" -d arg Specify M2M device to use [mandatory]");
puts(" -e Drop erroneous buffers");
puts(" -f arg Output file descriptor number");
puts(" -l arg Loop over input file (-1 means infinitely)");
puts(" -n arg Specify how many frames should be processed");
puts(" -o arg Output file name (takes precedence over -f)");
puts(" -p arg Specify output pixel format for M2M device");
puts(" -r arg When grabbing from camera specify desired framerate");
puts(" -s arg From which frame processing should be started");
puts(" -t Transform video to M420 [Avico-specific]");
puts(" -u Enable USERPTR memory type on output interface (default: MMAP memory type)");
puts(" -c <ctrl>=<val> Set the value of the controls [VIDIOC_S_EXT_CTRLS]");
puts(" -v Be more verbose. Can be specified multiple times");
}
int main(int argc, char *argv[]) {
AVFormatContext *ifc = NULL; //!< Input format context
/* AVFormatContext *ofc = NULL; //!< Output format context */
AVInputFormat *ifmt = NULL; //!< Input format
AVCodecContext *icc; //!< Input codec context
//AVCodecContext *occ; //!< Output codec context
AVCodec *ic; //!< Input codec
// AVCodec *oc; //!< Output codec
AVDictionary *options = NULL;
enum AVPixelFormat opf = AV_PIX_FMT_NONE; //!< Output pixel format
struct SwsContext *dsc = NULL; //!< Device swscale context
struct SwsContext *osc = NULL; //!< Output swscale context
AVFrame *oframe = NULL; //!< Output frame
uint64_t outsize = 0;
struct timespec loopstart, loopstop, looptime = { 0 };
int rc, opt;
int m2mfd, outfd = -1;
unsigned offset = 0, frames = 0, loops = 1;
char *framerate = NULL;
bool transform = false;
char const *output = NULL, *device = NULL;
char const *opfn = NULL; //!< Output pixel format name
bool userptr = false;
av_register_all();
const char *optstring = "d:ef:hl:n:o:p:r:s:tuc:v";
while ((opt = getopt(argc, argv, optstring)) != -1) {
switch (opt) {
case 'd': device = optarg; break;
case 'e': drop = true; break;
case 'f': outfd = atoi(optarg); break;
case 'h': help(argv[0]); return EXIT_SUCCESS;
case 'l': loops = atoi(optarg); break;
case 'n': frames = atoi(optarg); break;
case 'o': output = optarg; break;
case 'p': opfn = optarg; break;
case 'r': framerate = optarg; break;
case 's': offset = atoi(optarg); break;
case 't': transform = true; break;
case 'u': userptr = true; break;
case 'c': /* skip now, parse later */; break;
case 'v': vlevel++; break;
default: error(EXIT_FAILURE, 0, "Try %s -h for help.", argv[0]);
}
}
if (argc < optind + 1) error(EXIT_FAILURE, 0, "Not enough arguments");
if (device == NULL) error(EXIT_FAILURE, 0, "You must specify device");
char card[32];
m2mfd = v4l2_open(device, V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING, 0, card);
pr_info("Card: %.32s", card);
if (strncmp(card, "vim2m", 32) == 0) {
m2m_vim2m_controls(m2mfd);
}
find_controls(m2mfd, avico_ctrls, ARRAY_SIZE(avico_ctrls));
optind = 0;
while ((opt = getopt(argc, argv, optstring)) != -1) {
switch (opt) {
case 'c':
parse_ctrl_opts(optarg, avico_ctrls, ARRAY_SIZE(avico_ctrls));
break;
}
}
char const *input = argv[optind];
if (framerate && ifmt && ifmt->priv_class &&
av_opt_find(&ifmt->priv_class, "framerate", NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)) {
av_dict_set(&options, "framerate", framerate, 0);
}
// Open video file
if (avformat_open_input(&ifc, input, ifmt, &options) < 0)
error(EXIT_FAILURE, 0, "Can't open file: %s!", argv[optind]);
// Retrieve stream information
if(avformat_find_stream_info(ifc, NULL) < 0)
error(EXIT_FAILURE, 0, "Could not find stream information");
// Dump information about file onto standard error
if (vlevel >= LOG_INFO) av_dump_format(ifc, 0, input, 0);
// Find the first video stream
int video_stream_number = -1;
for (int i = 0; i < ifc->nb_streams; i++)
if (ifc->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_number = i;
break;
}
if (video_stream_number == -1)
error(EXIT_FAILURE, 0, "Didn't find a video stream");
// Find the decoder for the video stream
ic = avcodec_find_decoder(ifc->streams[video_stream_number]->codecpar->codec_id);
if (!ic)
error(EXIT_FAILURE, 0, "Unsupported codec");
// Allocate the codec context for the video stream
icc = avcodec_alloc_context3(ic);
if (!icc)
error(EXIT_FAILURE, 0, "Failed to allocate codec context");
rc = avcodec_parameters_to_context(icc, ifc->streams[video_stream_number]->codecpar);
if (rc)
error(EXIT_FAILURE, 0, "Failed to copy codec parameters to decoder context");
// Open codec
if (avcodec_open2(icc, ic, NULL) < 0)
error(EXIT_FAILURE, 0, "Could not open codec");
if (strncmp(card, "avico", 32) == 0 && !transform && icc->width % 16 > 0)
error(EXIT_FAILURE, 0, "Width must be multiple of 16 when pixel format is M420");
enum AVPixelFormat format = AV_PIX_FMT_YUV420P;
//! \brief Device swscale context
//! \detail Is used to convert read frame to M2M device output pixel format.
dsc = sws_getContext(icc->width, icc->height, icc->pix_fmt,
icc->width, icc->height, format, SWS_BILINEAR, NULL, NULL, NULL);
if (dsc == NULL) error(EXIT_FAILURE, 0, "Can't allocate output swscale context");
if (opfn) opf = av_get_pix_fmt(opfn);
if (opf == AV_PIX_FMT_NONE) opf = format;
if (opf != format) osc = sws_getContext(icc->width, icc->height, format,
icc->width, icc->height, opf, SWS_BILINEAR, NULL, NULL, NULL);
if (osc) {
oframe = av_frame_alloc();
if (oframe == NULL) error(EXIT_FAILURE, 0, "Can not allocate output frame structure");
oframe->width = icc->width;
oframe->height = icc->height;
oframe->format = opf;
rc = av_frame_get_buffer(oframe, 0);
if (rc < 0) error(EXIT_FAILURE, 0, "Can not allocate output frame buffers");
}
struct v4l2_format f_src = {
.fmt = {
.pix = {
.width = icc->width,
.height = icc->height,
.pixelformat = V4L2_PIX_FMT_M420,
.field = V4L2_FIELD_ANY,
.bytesperline = ROUND_UP(icc->width, 16)
/* Default colorspace parameters */
}
}
};
struct v4l2_format f_dst = {
.fmt = {
.pix = {
.width = icc->width,
.height = icc->height,
.pixelformat = V4L2_PIX_FMT_H264,
.field = V4L2_FIELD_ANY
}
}
};
v4l2_setformat(m2mfd, V4L2_BUF_TYPE_VIDEO_OUTPUT, &f_src);
v4l2_pix_fmt_validate(&f_src.fmt.pix, V4L2_PIX_FMT_M420, icc->width, icc->height, ROUND_UP(icc->width, 16));
v4l2_setformat(m2mfd, V4L2_BUF_TYPE_VIDEO_CAPTURE, &f_dst);
v4l2_pix_fmt_validate(&f_dst.fmt.pix, V4L2_PIX_FMT_H264, icc->width, icc->height, 0);
g_s_ctrls(m2mfd, avico_ctrls, ARRAY_SIZE(avico_ctrls), true);
struct v4l2_fract timeperframe;
if (framerate) {
timeperframe.numerator = 1;
timeperframe.denominator = atoi(framerate);
} else {
timeperframe.numerator = ifc->streams[video_stream_number]->r_frame_rate.den;
timeperframe.denominator = ifc->streams[video_stream_number]->r_frame_rate.num;
}
v4l2_framerate_configure(m2mfd, V4L2_BUF_TYPE_VIDEO_OUTPUT, &timeperframe);
v4l2_framerate_configure(m2mfd, V4L2_BUF_TYPE_VIDEO_CAPTURE, &timeperframe);
pr_info("Encoding framerate: %.2f -> %.2f FPS",
v4l2_framerate_get(m2mfd, V4L2_BUF_TYPE_VIDEO_OUTPUT),
v4l2_framerate_get(m2mfd, V4L2_BUF_TYPE_VIDEO_CAPTURE));
m2m_buffers_get(m2mfd, userptr);
v4l2_streamon(m2mfd, V4L2_BUF_TYPE_VIDEO_OUTPUT);
v4l2_streamon(m2mfd, V4L2_BUF_TYPE_VIDEO_CAPTURE);
pr_verb("Allocating AVFrames for obtained buffers...");
int av_frame_size = av_image_get_buffer_size(format, icc->width, icc->height, 16);
for (int i = 0; is_valid_out_buf(i); i++)
if (av_frame_size != out_bufs[i].v4l2.length)
error(EXIT_FAILURE, 0, "FFmpeg and V4L2 buffer sizes are not equal");
for (int i = 0; is_valid_out_buf(i); i++) {
AVFrame *frame = out_bufs[i].frame = av_frame_alloc();
if (!frame) error(EXIT_FAILURE, 0, "Not enough memory");
frame->format = format;
frame->width = icc->width;
frame->height = icc->height;
av_image_fill_arrays(frame->data, frame->linesize, out_bufs[i].buf,
frame->format, frame->width, frame->height, 16);
}
if (output) {
outfd = creat(output, S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
if (outfd < 0)
error(EXIT_FAILURE, errno, "Can not open output file");
}
/* if (output) {
avformat_alloc_output_context2(&ofc, NULL, NULL, output);
if (!ofc) error(EXIT_FAILURE, 0, "Can not allocate output context for %s", output);
oc = avcodec_find_encoder(AV_CODEC_ID_RAWVIDEO);
if (!oc) error(EXIT_FAILURE, 0, "Can not find rawvideo codec");
AVStream *os = avformat_new_stream(ofc, oc);
if (!os) error(EXIT_FAILURE, 0, "Can not allocate output stream");
occ = os->codec;
occ->width = icc->width;
occ->height = icc->height;
occ->pix_fmt = opf;
occ->sample_aspect_ratio = icc->sample_aspect_ratio;
rc = avcodec_open2(occ, oc, NULL);
if (rc < 0) error(EXIT_FAILURE, 0, "Can not initialize output codec context");
if (vlevel >= LOG_INFO) av_dump_format(ofc, 0, output, 1);
if (!(ofc->oformat->flags & AVFMT_NOFILE)) {
rc = avio_open(&ofc->pb, output, AVIO_FLAG_WRITE);
if (rc < 0) error(EXIT_FAILURE, 0, "Could not open output file '%s'", output);
}
rc = avformat_write_header(ofc, NULL);
if (rc < 0) error(EXIT_FAILURE, 0, "Can not write header for output file");
} */
unsigned int frame = 0, encframe = 0;
rc = clock_gettime(CLOCK_MONOTONIC, &loopstart);
pr_verb("Begin processing...");
for (unsigned loop = 0; checklimit(loop, loops) && checklimit(frame, frames); loop++) {
pr_verb("Loop #%u", loop);
if (loop != 0) {
rc = avformat_seek_file(ifc, video_stream_number, 0, 0, 0,
AVSEEK_FLAG_FRAME);
if (rc < 0)
error(EXIT_FAILURE, 0, "Can not rewind input file: %d", rc);
}
frame = process_stream(ifc, icc, video_stream_number, dsc, offset,
frames, transform, m2mfd, outfd, &encframe, &outsize);
}
m2m_drain(m2mfd, outfd, encframe, frame, &outsize);
outsize /= 1024;
pr_info("Output size: %" PRIu64 " KiB", outsize);
rc = clock_gettime(CLOCK_MONOTONIC, &loopstop);
looptime = timespec_subtract(loopstart, loopstop);
pr_info("Total time in main loop: %.1f s (%.1f FPS)",
timespec2float(looptime), frame / timespec2float(looptime));
if (outfd >= 0)
close(outfd);
return EXIT_SUCCESS;
}