-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
480 lines (398 loc) · 13.3 KB
/
main.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
// http://git.videolan.org/git/x264.git
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <turbojpeg.h>
#include <x264.h>
#define CLEAR(x) memset(&(x), 0, sizeof(x))
struct buffer {
void *start;
size_t length;
};
static char *dev_name;
static int fd = -1;
struct buffer *buffers;
static unsigned int n_buffers;
static int out_buf;
static int force_format;
static int frame_count = 100;
static int frame_number = 0;
static tjhandle jpegDecompressor = nullptr;
static constexpr auto width = 1280u;
static constexpr auto height = 720u;
x264_t *h;
x264_param_t param;
x264_picture_t pic;
FILE* fo = nullptr;
static void errno_exit(const char *s)
{
fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
exit(1);
}
static int xioctl(int fh, int request, void *arg)
{
int r;
do {
r = ioctl(fh, request, arg);
} while (-1 == r && EINTR == errno);
return r;
}
static void process_image(unsigned char *p, int size)
{
unsigned char buff[width * height * 2];
x264_picture_t pic_out;
memset(&buff, 0, sizeof(buff));
// tjDecompressHeader2(jpegDecompressor, p, size, &w, &h, &subsample);
// printf("%d\n", subsample);
//tjDecompressToYUV(jpegDecompressor, p, size, buff, TJFLAG_FASTDCT);
pic.img.i_csp = X264_CSP_I422;
pic.img.i_stride[0] = width ;
pic.img.i_stride[1] = 640;
pic.img.i_stride[2] = 640;
pic.img.i_stride[3] = 0;
tjDecompressToYUVPlanes(jpegDecompressor, p, size, pic.img.plane, width, pic.img.i_stride, height, TJFLAG_FASTDCT);
// sprintf(filename, "F%05d.y", frame_number);
// FILE *f=fopen(filename,"wb");
// fwrite(buff, 1, sizeof(buff), f);
// fclose(f);
x264_nal_t* nals;
int i_nals;
int frame_size = x264_encoder_encode(h, &nals, &i_nals, &pic, &pic_out);
if (frame_size >= 0)
{
printf("FS: %d %d\n", frame_size, frame_number);
// OK
if(frame_size > 0) {
fwrite( nals->p_payload, frame_size, 1, fo);
}
}
}
static int read_frame(void)
{
struct v4l2_buffer buf;
unsigned int i;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
switch (errno) {
case EAGAIN:
return 1;
case EIO:
/* Could ignore EIO, see spec. */
/* fall through */
default:
return -1;
}
}
assert(buf.index < n_buffers);
process_image(static_cast<unsigned char*>(buffers[buf.index].start), buf.bytesused);
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
return -1;
++frame_number;
if(frame_number > 300) {
return -1;
}
return 0;
}
static void mainloop(void)
{
unsigned int count;
count = frame_count;
for (;;) {
fd_set fds;
struct timeval tv;
int r;
FD_ZERO(&fds);
FD_SET(fd, &fds);
/* Timeout. */
tv.tv_sec = 2;
tv.tv_usec = 0;
r = select(fd + 1, &fds, NULL, NULL, &tv);
if (-1 == r) {
if (EINTR == errno)
continue;
return;
}
if (0 == r) {
fprintf(stderr, "xselect timeout\n");
return;
}
if (read_frame() < 0)
break;
/* EAGAIN - continue select loop. */
}
}
static void stop_capturing(void)
{
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))
errno_exit("VIDIOC_STREAMOFF");
}
static void start_capturing(void)
{
unsigned int i;
enum v4l2_buf_type type;
for (i = 0; i < n_buffers; ++i) {
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF");
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
errno_exit("VIDIOC_STREAMON");
}
static void uninit_device(void)
{
unsigned int i;
for (i = 0; i < n_buffers; ++i)
if (-1 == munmap(buffers[i].start, buffers[i].length))
errno_exit("munmap");
free(buffers);
}
static void init_mmap(void)
{
struct v4l2_requestbuffers req;
CLEAR(req);
req.count = 4;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
if (EINVAL == errno) {
fprintf(stderr, "%s does not support "
"memory mapping\n", dev_name);
exit(EXIT_FAILURE);
} else {
errno_exit("VIDIOC_REQBUFS");
}
}
if (req.count < 2) {
fprintf(stderr, "Insufficient buffer memory on %s\n",
dev_name);
exit(EXIT_FAILURE);
}
buffers = static_cast<struct buffer*>(calloc(req.count, sizeof(*buffers)));
if (!buffers) {
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = n_buffers;
if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
errno_exit("VIDIOC_QUERYBUF");
buffers[n_buffers].length = buf.length;
buffers[n_buffers].start =
mmap(NULL /* start anywhere */,
buf.length,
PROT_READ | PROT_WRITE /* required */,
MAP_SHARED,/* recommended */
fd, buf.m.offset);
if (MAP_FAILED == buffers[n_buffers].start)
errno_exit("mmap");
}
}
static void init_device(void)
{
struct v4l2_capability cap;
struct v4l2_cropcap cropcap;
struct v4l2_crop crop;
struct v4l2_format fmt;
unsigned int min;
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
if (EINVAL == errno) {
fprintf(stderr, "%s is no V4L2 device\n",
dev_name);
exit(EXIT_FAILURE);
} else {
errno_exit("VIDIOC_QUERYCAP");
}
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
fprintf(stderr, "%s is no video capture device\n",
dev_name);
exit(EXIT_FAILURE);
}
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
fprintf(stderr, "%s does not support streaming i/o\n",
dev_name);
exit(EXIT_FAILURE);
}
CLEAR(cropcap);
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) {
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
crop.c = cropcap.defrect; /* reset to default */
if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) {
switch (errno) {
case EINVAL:
/* Cropping not supported. */
break;
default:
/* Errors ignored. */
break;
}
}
} else {
/* Errors ignored. */
}
CLEAR(fmt);
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = width; //replace
fmt.fmt.pix.height = height; //replace
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG ; //replace
fmt.fmt.pix.field = V4L2_FIELD_ANY;
if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
errno_exit("VIDIOC_S_FMT");
/* Note VIDIOC_S_FMT may change width and height. */
/* Buggy driver paranoia. */
init_mmap();
}
static void close_device(void)
{
if (-1 == close(fd))
errno_exit("close");
fd = -1;
}
static void open_device(void)
{
struct stat st;
if (-1 == stat(dev_name, &st)) {
fprintf(stderr, "Cannot identify '%s': %d, %s\n",
dev_name, errno, strerror(errno));
exit(EXIT_FAILURE);
}
if (!S_ISCHR(st.st_mode)) {
fprintf(stderr, "%s is no device\n", dev_name);
exit(EXIT_FAILURE);
}
fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
if (-1 == fd) {
fprintf(stderr, "Cannot open '%s': %d, %s\n",
dev_name, errno, strerror(errno));
exit(EXIT_FAILURE);
}
}
int main(int argc, char **argv)
{
x264_picture_t pic_out;
constexpr auto fps = 30u;
if( x264_param_default_preset( ¶m, "superfast", NULL ) < 0 ) {
fprintf(stderr, "Cannot identify x264_param_default_presets\n");
exit(EXIT_FAILURE);
}
param.i_csp = X264_CSP_I422;
param.i_width = width;
param.i_height = height;
param.i_threads = 1;
param.i_timebase_num = 1;
param.i_timebase_den = fps;
param.b_vfr_input = 0;
param.b_repeat_headers = 1;
param.b_annexb = 1;
param.rc.i_rc_method = X264_RC_ABR;
param.rc.i_bitrate = 1000000u;
/* Apply profile restrictions. */
if( x264_param_apply_profile( ¶m, "high422" ) < 0 ) {
fprintf(stderr, "Cannot identify x264_param_apply_profile\n");
exit(EXIT_FAILURE);
}
if( x264_picture_alloc( &pic, param.i_csp, param.i_width, param.i_height ) < 0 ) {
fprintf(stderr, "Cannot identify x264_picture_alloc\n");
exit(EXIT_FAILURE);
}
h = x264_encoder_open( ¶m );
if( !h ) {
fprintf(stderr, "Cannot identify x264_encoder_open\n");
exit(EXIT_FAILURE);
}
fo = fopen("out.x264", "wb");
jpegDecompressor = tjInitDecompress();
dev_name = "/dev/video0";
open_device();
init_device();
start_capturing();
mainloop();
stop_capturing();
uninit_device();
close_device();
/* Flush delayed frames */
while( x264_encoder_delayed_frames( h ) )
{
x264_nal_t* nals;
int i_nals;
auto frame_size = x264_encoder_encode( h, &nals, &i_nals, NULL, &pic_out );
if( frame_size < 0 )
exit(1);
else if( frame_size )
{
if( !fwrite( nals->p_payload, frame_size, 1, fo ) )
exit(1);
}
}
x264_encoder_close( h );
x264_picture_clean( &pic );
fclose(fo);
fprintf(stderr, "\n");
return 0;
}
#if 0
int i_frame = 0;
int i_frame_size;
x264_nal_t *nal;
int i_nal;
int luma_size = width * height;
int chroma_size = luma_size / 4;
/* Encode frames */
for( ;; i_frame++ )
{
/* Read input frame */
if( fread( pic.img.plane[0], 1, luma_size, stdin ) != luma_size )
break;
if( fread( pic.img.plane[1], 1, chroma_size, stdin ) != chroma_size )
break;
if( fread( pic.img.plane[2], 1, chroma_size, stdin ) != chroma_size )
break;
pic.i_pts = i_frame;
i_frame_size = x264_encoder_encode( h, &nal, &i_nal, &pic, &pic_out );
if( i_frame_size < 0 )
goto fail;
else if( i_frame_size )
{
if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
goto fail;
}
}
/* Flush delayed frames */
while( x264_encoder_delayed_frames( h ) )
{
i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
if( i_frame_size < 0 )
goto fail;
else if( i_frame_size )
{
if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
goto fail;
}
}
}
#endif