-
Notifications
You must be signed in to change notification settings - Fork 30
/
H264FramedLiveSource.cpp
executable file
·433 lines (361 loc) · 9.21 KB
/
H264FramedLiveSource.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
/*
* H264FramedLiveSource.cpp
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <linux/types.h>
#include <linux/videodev2.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
#include <assert.h>
#include "encoder_define.hh"
#include "H264FramedLiveSource.hh"
#include <BasicUsageEnvironment.hh>
#define WIDTH 640
#define HEIGHT 480
#define widthStep 960
#define FILE_VIDEO "/dev/video0"
extern class Device Camera;
void Device::init_mmap(void)
{
struct v4l2_requestbuffers reqbufs;
memset(&reqbufs, 0, sizeof(reqbufs));
reqbufs.count = 4;
reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbufs.memory = V4L2_MEMORY_MMAP;
if(-1 == ioctl(fd,VIDIOC_REQBUFS,&reqbufs))
{
perror("Fail to ioctl 'VIDIOC_REQBUFS'");
exit(EXIT_FAILURE);
}
n_buffer = reqbufs.count;
printf("n_buffer = %d\n", n_buffer);
usr_buf = (BUFTYPE*)calloc(reqbufs.count, sizeof(BUFTYPE));
if(usr_buf == NULL)
{
printf("Out of memory\n");
exit(-1);
}
for(n_buffer = 0; n_buffer < reqbufs.count; ++n_buffer)
{
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = n_buffer;
if(-1 == ioctl(fd,VIDIOC_QUERYBUF,&buf))
{
perror("Fail to ioctl : VIDIOC_QUERYBUF");
exit(EXIT_FAILURE);
}
usr_buf[n_buffer].length = buf.length;
usr_buf[n_buffer].start = (char *)mmap(NULL,buf.length,PROT_READ | PROT_WRITE,MAP_SHARED, fd,buf.m.offset);
if(MAP_FAILED == usr_buf[n_buffer].start)
{
perror("Fail to mmap");
exit(EXIT_FAILURE);
}
}
}
void Device::open_camera(void)
{
struct v4l2_input inp;
fd = open(FILE_VIDEO, O_RDWR | O_NONBLOCK,0);
if(fd < 0)
{
fprintf(stderr, "%s open err \n", FILE_VIDEO);
exit(EXIT_FAILURE);
};
inp.index = 0;
if (-1 == ioctl (fd, VIDIOC_S_INPUT, &inp))
{
fprintf(stderr, "VIDIOC_S_INPUT \n");
}
}
void Device::init_camera(void)
{
struct v4l2_capability cap;
struct v4l2_format tv_fmt;
struct v4l2_fmtdesc fmtdesc;
int ret;
memset(&fmtdesc, 0, sizeof(fmtdesc));
fmtdesc.index = 0 ;
fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
ret=ioctl(fd, VIDIOC_QUERYCAP, &cap);
if(ret < 0)
{
fprintf(stderr, "fail to ioctl VIDEO_QUERYCAP \n");
exit(EXIT_FAILURE);
}
if(!(cap.capabilities & V4L2_BUF_TYPE_VIDEO_CAPTURE))
{
fprintf(stderr, "The Current device is not a video capture device \n");
exit(EXIT_FAILURE);
}
if(!(cap.capabilities & V4L2_CAP_STREAMING))
{
printf("The Current device does not support streaming i/o\n");
exit(EXIT_FAILURE);
}
printf("\ncamera driver name is : %s\n",cap.driver);
printf("camera device name is : %s\n",cap.card);
printf("camera bus information: %s\n",cap.bus_info);
tv_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
tv_fmt.fmt.pix.width = WIDTH;
tv_fmt.fmt.pix.height = HEIGHT;
tv_fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
tv_fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
if (ioctl(fd, VIDIOC_S_FMT, &tv_fmt)< 0)
{
fprintf(stderr,"VIDIOC_S_FMT set err\n");
exit(-1);
close(fd);
}
}
void Device::start_capture(void)
{
unsigned int i;
enum v4l2_buf_type type;
for(i = 0; i < n_buffer; i++)
{
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if(-1 == ioctl(fd, VIDIOC_QBUF, &buf))
{
perror("Fail to ioctl 'VIDIOC_QBUF'");
exit(EXIT_FAILURE);
}
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(-1 == ioctl(fd, VIDIOC_STREAMON, &type))
{
printf("i=%d.\n", i);
perror("VIDIOC_STREAMON");
close(fd);
exit(EXIT_FAILURE);
}
}
void Device::read_one_frame(void)
{
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if(-1 == ioctl(fd, VIDIOC_DQBUF,&buf))
{
perror("Fail to ioctl 'VIDIOC_DQBUF'");
exit(EXIT_FAILURE);
}
assert(buf.index < n_buffer);
//encode_frame(usr_buf[buf.index].start, usr_buf[buf.index].length);
frame_len = compress_frame(&en, -1, usr_buf[buf.index].start, usr_buf[buf.index].length, h264_buf);
if(-1 == ioctl(fd, VIDIOC_QBUF,&buf))
{
perror("Fail to ioctl 'VIDIOC_QBUF'");
exit(EXIT_FAILURE);
}
}
void Device::stop_capture(void)
{
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(-1 == ioctl(fd,VIDIOC_STREAMOFF,&type))
{
perror("Fail to ioctl 'VIDIOC_STREAMOFF'");
exit(EXIT_FAILURE);
}
}
void Device::close_camera(void)
{
unsigned int i;
for(i = 0;i < n_buffer; i++)
{
if(-1 == munmap(usr_buf[i].start,usr_buf[i].length))
{
exit(-1);
}
}
free(usr_buf);
if(-1 == close(fd))
{
perror("Fail to close fd");
exit(EXIT_FAILURE);
}
}
int Device::camera_able_read(void)
{
fd_set fds;
struct timeval tv;
int ret;
FD_ZERO(&fds);
FD_SET(fd,&fds); /*Timeout*/
tv.tv_sec = 2;
tv.tv_usec = 0;
ret = select(fd + 1,&fds,NULL,NULL,&tv);
if(-1 == ret)
{
if(EINTR == errno)
return -1;
perror("Fail to select");
exit(EXIT_FAILURE);
}
if(0 == ret)
{
fprintf(stderr,"select Timeout\n");
//exit(-1);
}
return ret;
}
void Device::init_encoder(void)
{
compress_begin(&en, WIDTH, HEIGHT);
h264_buf = (char *) malloc( WIDTH * HEIGHT * 2);
}
void Device::compress_begin(Encoder *en, int width, int height)
{
en->param = (x264_param_t *) malloc(sizeof(x264_param_t));
en->picture = (x264_picture_t *) malloc(sizeof(x264_picture_t));
x264_param_default(en->param); //set default param
en->param->i_frame_reference=3;
en->param->rc.i_rc_method=X264_RC_ABR;
en->param->b_cabac =0;
en->param->b_interlaced=0;
en->param->i_level_idc=30;
en->param->i_keyint_max=en->param->i_fps_num*1.5;
en->param->i_keyint_min=1;
en->param->i_threads = X264_SYNC_LOOKAHEAD_AUTO;
en->param->i_width = WIDTH;
en->param->i_height = HEIGHT;
en->param->i_frame_total = 0;
en->param->i_keyint_max = 10;
en->param->rc.i_lookahead = 0;
en->param->i_bframe = 5;
en->param->b_open_gop = 0;
en->param->i_bframe_pyramid = 0;
en->param->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
en->param->rc.i_bitrate = 1024 * 10;
en->param->i_fps_num = 25;
en->param->i_fps_den = 1;
#if 0
en->param->i_threads = X264_SYNC_LOOKAHEAD_AUTO;
en->param->i_width = WIDTH; //set frame width
en->param->i_height = HEIGHT; //set frame height
en->param->rc.i_lookahead = 0;
en->param->i_fps_num = 30;
en->param->i_fps_den = 1;
#endif
en->param->i_csp = X264_CSP_I422;
x264_param_apply_profile(en->param, x264_profile_names[4]);
if ((en->handle = x264_encoder_open(en->param)) == 0) {
return;
}
x264_picture_alloc(en->picture, X264_CSP_I422, en->param->i_width,
en->param->i_height);
}
int Device::compress_frame(Encoder *en, int type, char *in, int len, char *out)
{
x264_picture_t pic_out;
int index_y, index_u, index_v;
int num;
int nNal = -1;
int result = 0;
int i = 0;
char *p_out = out;
char *y = (char*)en->picture->img.plane[0];
char *u = (char*)en->picture->img.plane[1];
char *v = (char*)en->picture->img.plane[2];
index_y = 0;
index_u = 0;
index_v = 0;
num = WIDTH * HEIGHT * 2 - 4 ;
for(i=0; i<num; i=i+4)
{
*(y + (index_y++)) = *(in + i);
*(u + (index_u++)) = *(in + i + 1);
*(y + (index_y++)) = *(in + i + 2);
*(v + (index_v++)) = *(in + i + 3);
}
switch (type) {
case 0:
en->picture->i_type = X264_TYPE_P;
break;
case 1:
en->picture->i_type = X264_TYPE_IDR;
break;
case 2:
en->picture->i_type = X264_TYPE_I;
break;
default:
en->picture->i_type = X264_TYPE_AUTO;
break;
}
en->picture->i_pts ++;
if (x264_encoder_encode(en->handle, &(en->nal), &nNal, en->picture,
&pic_out) < 0) {
return -1;
}
for (i = 0; i < nNal; i++) {
memcpy(p_out, en->nal[i].p_payload, en->nal[i].i_payload);
p_out += en->nal[i].i_payload;
result += en->nal[i].i_payload;
}
return result;
}
void Device::getnextframe(void)
{
int ret;
ret = camera_able_read();
if(ret > 0)
{
read_one_frame();
fwrite(Camera.h264_buf, Camera.frame_len, 1, Camera.pipe_fd);
}
else
{
}
}
void Device::compress_end(Encoder *en)
{
if (en->handle) {
x264_encoder_close(en->handle);
}
if (en->picture) {
x264_picture_clean(en->picture);
free(en->picture);
en->picture = 0;
}
if (en->param) {
free(en->param);
en->param = 0;
}
}
void Device::close_encoder()
{
compress_end(&en);
free(h264_buf);
}
void Device::Init()
{
open_camera();
init_camera();
init_mmap();
start_capture();
init_encoder();
}
void Device::Destory()
{
stop_capture();
close_encoder();
close_camera();
}