-
Notifications
You must be signed in to change notification settings - Fork 42
/
video_record.c
562 lines (442 loc) · 18.8 KB
/
video_record.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
/*
* File: buffer_demo.c
* Author: Tasanakorn
*
* Created on May 22, 2013, 1:52 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include "bcm_host.h"
#include "interface/vcos/vcos.h"
#include "interface/mmal/mmal.h"
#include "interface/mmal/util/mmal_default_components.h"
#include "interface/mmal/util/mmal_connection.h"
#include <cairo/cairo.h>
#define MMAL_CAMERA_PREVIEW_PORT 0
#define MMAL_CAMERA_VIDEO_PORT 1
#define MMAL_CAMERA_CAPTURE_PORT 2
#define VIDEO_FPS 30
#define VIDEO_WIDTH 1280
#define VIDEO_HEIGHT 720
typedef struct {
int width;
int height;
MMAL_COMPONENT_T *camera;
MMAL_COMPONENT_T *encoder;
MMAL_COMPONENT_T *preview;
MMAL_PORT_T *camera_preview_port;
MMAL_PORT_T *camera_video_port;
MMAL_PORT_T *camera_still_port;
MMAL_POOL_T *camera_video_port_pool;
MMAL_PORT_T *encoder_input_port;
MMAL_POOL_T *encoder_input_pool;
MMAL_PORT_T *encoder_output_port;
MMAL_POOL_T *encoder_output_pool;
uint8_t *overlay_buffer;
uint8_t *overlay_buffer2;
int overlay;
float fps;
} PORT_USERDATA;
static void camera_video_buffer_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer) {
static int frame_count = 0;
static struct timespec t1;
struct timespec t2;
uint8_t *local_overlay_buffer;
//fprintf(stderr, "INFO:%s\n", __func__);
if (frame_count == 0) {
clock_gettime(CLOCK_MONOTONIC, &t1);
}
clock_gettime(CLOCK_MONOTONIC, &t2);
int d = t2.tv_sec - t1.tv_sec;
MMAL_BUFFER_HEADER_T *new_buffer;
MMAL_BUFFER_HEADER_T *output_buffer = 0;
PORT_USERDATA *userdata = (PORT_USERDATA *) port->userdata;
MMAL_POOL_T *pool = userdata->camera_video_port_pool;
frame_count++;
output_buffer = mmal_queue_get(userdata->encoder_input_pool->queue);
//Set pointer to latest updated/drawn double buffer to local pointer
if (userdata->overlay == 0) {
local_overlay_buffer = userdata->overlay_buffer;
}
else {
local_overlay_buffer = userdata->overlay_buffer2;
}
//Try to some colors http://en.wikipedia.org/wiki/YUV
int chrominance_offset = userdata->width * userdata->height;
int v_offset = chrominance_offset / 4;
int chroma = 0;
if (output_buffer) {
mmal_buffer_header_mem_lock(buffer);
memcpy(output_buffer->data, buffer->data, buffer->length);
// dim
int x, y;
for (x = 0; x < 600; x++) {
for (y = 0; y < 100; y++) {
if (local_overlay_buffer[(y * 600 + x) * 4] > 0) {
//copy luma Y
output_buffer->data[y * userdata->width + x ] = 0xdf;
//pointer to chrominance U/V
chroma= y / 2 * userdata->width / 2 + x / 2 + chrominance_offset;
//just guessing colors
output_buffer->data[chroma] = 0x38 ;
output_buffer->data[chroma+v_offset] = 0xb8 ;
}
}
}
output_buffer->length = buffer->length;
mmal_buffer_header_mem_unlock(buffer);
if (mmal_port_send_buffer(userdata->encoder_input_port, output_buffer) != MMAL_SUCCESS) {
fprintf(stderr, "ERROR: Unable to send buffer \n");
}
} else {
fprintf(stderr, "ERROR: mmal_queue_get (%d)\n", output_buffer);
}
if (frame_count % 10 == 0) {
// print framerate every n frame
clock_gettime(CLOCK_MONOTONIC, &t2);
float d = (t2.tv_sec + t2.tv_nsec / 1000000000.0) - (t1.tv_sec + t1.tv_nsec / 1000000000.0);
float fps = 0.0;
if (d > 0) {
fps = frame_count / d;
} else {
fps = frame_count;
}
userdata->fps = fps;
fprintf(stderr, " Frame = %d, Framerate = %.1f fps \n", frame_count, fps);
}
mmal_buffer_header_release(buffer);
// and send one back to the port (if still open)
if (port->is_enabled) {
MMAL_STATUS_T status;
new_buffer = mmal_queue_get(pool->queue);
if (new_buffer) {
status = mmal_port_send_buffer(port, new_buffer);
}
if (!new_buffer || status != MMAL_SUCCESS) {
fprintf(stderr, "Error: Unable to return a buffer to the video port\n");
}
}
}
static void encoder_input_buffer_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer) {
//fprintf(stderr, "INFO:%s\n", __func__);
mmal_buffer_header_release(buffer);
}
static void encoder_output_buffer_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer) {
MMAL_BUFFER_HEADER_T *new_buffer;
PORT_USERDATA *userdata = (PORT_USERDATA *) port->userdata;
MMAL_POOL_T *pool = userdata->encoder_output_pool;
//fprintf(stderr, "INFO:%s\n", __func__);
mmal_buffer_header_mem_lock(buffer);
fwrite(buffer->data, 1, buffer->length, stdout);
mmal_buffer_header_mem_unlock(buffer);
mmal_buffer_header_release(buffer);
if (port->is_enabled) {
MMAL_STATUS_T status;
new_buffer = mmal_queue_get(pool->queue);
if (new_buffer) {
status = mmal_port_send_buffer(port, new_buffer);
}
if (!new_buffer || status != MMAL_SUCCESS) {
fprintf(stderr, "Unable to return a buffer to the video port\n");
}
}
}
int fill_port_buffer(MMAL_PORT_T *port, MMAL_POOL_T *pool) {
int q;
int num = mmal_queue_length(pool->queue);
for (q = 0; q < num; q++) {
MMAL_BUFFER_HEADER_T *buffer = mmal_queue_get(pool->queue);
if (!buffer) {
fprintf(stderr, "Unable to get a required buffer %d from pool queue\n", q);
}
if (mmal_port_send_buffer(port, buffer) != MMAL_SUCCESS) {
fprintf(stderr, "Unable to send a buffer to port (%d)\n", q);
}
}
}
int setup_camera(PORT_USERDATA *userdata) {
MMAL_STATUS_T status;
MMAL_COMPONENT_T *camera = 0;
MMAL_ES_FORMAT_T *format;
MMAL_PORT_T * camera_preview_port;
MMAL_PORT_T * camera_video_port;
MMAL_PORT_T * camera_still_port;
MMAL_POOL_T * camera_video_port_pool;
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_CAMERA, &camera);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: create camera %x\n", status);
return -1;
}
userdata->camera = camera;
userdata->camera_preview_port = camera->output[MMAL_CAMERA_PREVIEW_PORT];
userdata->camera_video_port = camera->output[MMAL_CAMERA_VIDEO_PORT];
userdata->camera_still_port = camera->output[MMAL_CAMERA_CAPTURE_PORT];
camera_preview_port = camera->output[MMAL_CAMERA_PREVIEW_PORT];
camera_video_port = camera->output[MMAL_CAMERA_VIDEO_PORT];
camera_still_port = camera->output[MMAL_CAMERA_CAPTURE_PORT];
{
MMAL_PARAMETER_CAMERA_CONFIG_T cam_config = {
{ MMAL_PARAMETER_CAMERA_CONFIG, sizeof (cam_config)},
.max_stills_w = 1280,
.max_stills_h = 720,
.stills_yuv422 = 0,
.one_shot_stills = 1,
.max_preview_video_w = VIDEO_WIDTH,
.max_preview_video_h = VIDEO_HEIGHT,
.num_preview_video_frames = 3,
.stills_capture_circular_buffer_height = 0,
.fast_preview_resume = 0,
.use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RESET_STC
};
mmal_port_parameter_set(camera->control, &cam_config.hdr);
}
// Setup camera preview port format
format = camera_preview_port->format;
format->encoding = MMAL_ENCODING_OPAQUE;
format->encoding_variant = MMAL_ENCODING_I420;
format->es->video.width = VIDEO_WIDTH;
format->es->video.height = VIDEO_HEIGHT;
format->es->video.crop.x = 0;
format->es->video.crop.y = 0;
format->es->video.crop.width = VIDEO_WIDTH;
format->es->video.crop.height = VIDEO_HEIGHT;
status = mmal_port_format_commit(camera_preview_port);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: camera viewfinder format couldn't be set\n");
return -1;
}
// Setup camera video port format
mmal_format_copy(camera_video_port->format, camera_preview_port->format);
format = camera_video_port->format;
format->encoding = MMAL_ENCODING_I420;
format->encoding_variant = MMAL_ENCODING_I420;
format->es->video.width = VIDEO_WIDTH;
format->es->video.height = VIDEO_HEIGHT;
format->es->video.crop.x = 0;
format->es->video.crop.y = 0;
format->es->video.crop.width = VIDEO_WIDTH;
format->es->video.crop.height = VIDEO_HEIGHT;
format->es->video.frame_rate.num = VIDEO_FPS;
format->es->video.frame_rate.den = 1;
camera_video_port->buffer_size = format->es->video.width * format->es->video.height * 12 / 8;
camera_video_port->buffer_num = 2;
fprintf(stderr, "INFO:camera video buffer_size = %d\n", camera_video_port->buffer_size);
fprintf(stderr, "INFO:camera video buffer_num = %d\n", camera_video_port->buffer_num);
status = mmal_port_format_commit(camera_video_port);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: unable to commit camera video port format (%u)\n", status);
return -1;
}
camera_video_port_pool = (MMAL_POOL_T *) mmal_port_pool_create(camera_video_port, camera_video_port->buffer_num, camera_video_port->buffer_size);
userdata->camera_video_port_pool = camera_video_port_pool;
camera_video_port->userdata = (struct MMAL_PORT_USERDATA_T *) userdata;
status = mmal_port_enable(camera_video_port, camera_video_buffer_callback);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: unable to enable camera video port (%u)\n", status);
return -1;
}
status = mmal_component_enable(camera);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: unable to enable camera (%u)\n", status);
return -1;
}
fill_port_buffer(userdata->camera_video_port, userdata->camera_video_port_pool);
if (mmal_port_parameter_set_boolean(camera_video_port, MMAL_PARAMETER_CAPTURE, 1) != MMAL_SUCCESS) {
printf("%s: Failed to start capture\n", __func__);
}
fprintf(stderr, "INFO: camera created\n");
return 0;
}
int setup_encoder(PORT_USERDATA *userdata) {
MMAL_STATUS_T status;
MMAL_COMPONENT_T *encoder = 0;
MMAL_PORT_T *preview_input_port = NULL;
MMAL_PORT_T *encoder_input_port = NULL, *encoder_output_port = NULL;
MMAL_POOL_T *encoder_input_port_pool;
MMAL_POOL_T *encoder_output_port_pool;
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_ENCODER, &encoder);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: unable to create preview (%u)\n", status);
return -1;
}
encoder_input_port = encoder->input[0];
encoder_output_port = encoder->output[0];
userdata->encoder_input_port = encoder_input_port;
userdata->encoder_output_port = encoder_input_port;
mmal_format_copy(encoder_input_port->format, userdata->camera_video_port->format);
encoder_input_port->buffer_size = encoder_input_port->buffer_size_recommended;
encoder_input_port->buffer_num = 2;
mmal_format_copy(encoder_output_port->format, encoder_input_port->format);
encoder_output_port->buffer_size = encoder_output_port->buffer_size_recommended;
encoder_output_port->buffer_num = 2;
// Commit the port changes to the input port
status = mmal_port_format_commit(encoder_input_port);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: unable to commit encoder input port format (%u)\n", status);
return -1;
}
// Only supporting H264 at the moment
encoder_output_port->format->encoding = MMAL_ENCODING_H264;
encoder_output_port->format->bitrate = 2000000;
encoder_output_port->buffer_size = encoder_output_port->buffer_size_recommended;
if (encoder_output_port->buffer_size < encoder_output_port->buffer_size_min) {
encoder_output_port->buffer_size = encoder_output_port->buffer_size_min;
}
encoder_output_port->buffer_num = encoder_output_port->buffer_num_recommended;
if (encoder_output_port->buffer_num < encoder_output_port->buffer_num_min) {
encoder_output_port->buffer_num = encoder_output_port->buffer_num_min;
}
// Commit the port changes to the output port
status = mmal_port_format_commit(encoder_output_port);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: unable to commit encoder output port format (%u)\n", status);
return -1;
}
fprintf(stderr, " encoder input buffer_size = %d\n", encoder_input_port->buffer_size);
fprintf(stderr, " encoder input buffer_num = %d\n", encoder_input_port->buffer_num);
fprintf(stderr, " encoder output buffer_size = %d\n", encoder_output_port->buffer_size);
fprintf(stderr, " encoder output buffer_num = %d\n", encoder_output_port->buffer_num);
encoder_input_port_pool = (MMAL_POOL_T *) mmal_port_pool_create(encoder_input_port, encoder_input_port->buffer_num, encoder_input_port->buffer_size);
userdata->encoder_input_pool = encoder_input_port_pool;
encoder_input_port->userdata = (struct MMAL_PORT_USERDATA_T *) userdata;
status = mmal_port_enable(encoder_input_port, encoder_input_buffer_callback);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: unable to enable encoder input port (%u)\n", status);
return -1;
}
fprintf(stderr, "INFO:Encoder input pool has been created\n");
encoder_output_port_pool = (MMAL_POOL_T *) mmal_port_pool_create(encoder_output_port, encoder_output_port->buffer_num, encoder_output_port->buffer_size);
userdata->encoder_output_pool = encoder_output_port_pool;
encoder_output_port->userdata = (struct MMAL_PORT_USERDATA_T *) userdata;
status = mmal_port_enable(encoder_output_port, encoder_output_buffer_callback);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: unable to enable encoder output port (%u)\n", status);
return -1;
}
fprintf(stderr, "INFO:Encoder output pool has been created\n");
fill_port_buffer(encoder_output_port, encoder_output_port_pool);
fprintf(stderr, "INFO:Encoder has been created\n");
return 0;
}
int setup_preview(PORT_USERDATA *userdata) {
MMAL_STATUS_T status;
MMAL_COMPONENT_T *preview = 0;
MMAL_CONNECTION_T *camera_preview_connection = 0;
MMAL_PORT_T *preview_input_port;
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_RENDERER, &preview);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: unable to create preview (%u)\n", status);
return -1;
}
userdata->preview = preview;
preview_input_port = preview->input[0];
{
MMAL_DISPLAYREGION_T param;
param.hdr.id = MMAL_PARAMETER_DISPLAYREGION;
param.hdr.size = sizeof (MMAL_DISPLAYREGION_T);
param.set = MMAL_DISPLAY_SET_LAYER;
param.layer = 0;
param.set |= MMAL_DISPLAY_SET_FULLSCREEN;
param.fullscreen = 1;
status = mmal_port_parameter_set(preview_input_port, ¶m.hdr);
if (status != MMAL_SUCCESS && status != MMAL_ENOSYS) {
fprintf(stderr, "Error: unable to set preview port parameters (%u)\n", status);
return -1;
}
}
status = mmal_connection_create(&camera_preview_connection, userdata->camera_preview_port, preview_input_port, MMAL_CONNECTION_FLAG_TUNNELLING | MMAL_CONNECTION_FLAG_ALLOCATION_ON_INPUT);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: unable to create connection (%u)\n", status);
return -1;
}
status = mmal_connection_enable(camera_preview_connection);
if (status != MMAL_SUCCESS) {
fprintf(stderr, "Error: unable to enable connection (%u)\n", status);
return -1;
}
fprintf(stderr, "INFO: preview created\n");
return 0;
}
int setup_video_overlay(PORT_USERDATA *userdata) {
return 0;
}
int main(int argc, char** argv) {
PORT_USERDATA userdata;
MMAL_STATUS_T status;
cairo_surface_t *surface,*surface2;
cairo_t *context,*context2;
memset(&userdata, 0, sizeof (PORT_USERDATA));
userdata.width = VIDEO_WIDTH;
userdata.height = VIDEO_HEIGHT;
userdata.fps = 0.0;
fprintf(stderr, "VIDEO_WIDTH : %i\n", userdata.width );
fprintf(stderr, "VIDEO_HEIGHT: %i\n", userdata.height );
fprintf(stderr, "VIDEO_FPS : %i\n", VIDEO_FPS);
fprintf(stderr, "Running...\n");
bcm_host_init();
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 600, 100);
context = cairo_create(surface);
cairo_rectangle(context, 0.0, 0.0, 600, 100);
cairo_set_source_rgba(context, 0.0, 0.0, 0.0, 1.0);
cairo_fill(context);
userdata.overlay_buffer = cairo_image_surface_get_data(surface);
userdata.overlay = 1;
surface2 = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 600, 100);
context2 = cairo_create(surface2);
cairo_rectangle(context2, 0.0, 0.0, 600, 100);
cairo_set_source_rgba(context2, 0.0, 0.0, 0.0, 1.0);
cairo_fill(context2);
userdata.overlay_buffer2 = cairo_image_surface_get_data(surface2);
if (1 && setup_camera(&userdata) != 0) {
fprintf(stderr, "Error: setup camera %x\n", status);
return -1;
}
if (1 && setup_encoder(&userdata) != 0) {
fprintf(stderr, "Error: setup encoder %x\n", status);
return -1;
}
if (1 && setup_preview(&userdata) != 0) {
fprintf(stderr, "Error: setup preview %x\n", status);
return -1;
}
char text[256];
//fake Speed and GPS data
float lat = 47.4912;
float lon = 8.906;
float speed = 20.0;
while (1) {
//Update Draw to unused buffer that way there is no flickering of the overlay text if the overlay update rate
//and video FPS are not the same
if (userdata.overlay == 1) {
cairo_rectangle(context, 0.0, 0.0, 600, 100);
cairo_set_source_rgba(context, 0.0, 0.0, 0.0, 1.0);
cairo_fill(context);
cairo_move_to(context, 0.0, 0.0);
cairo_set_source_rgba(context, 1.0, 1.0, 1.0, 1.0);
cairo_move_to(context, 0.0, 30.0);
cairo_set_font_size(context, 20.0);
sprintf(text, "%.2fFPS GPS: %.3f, %.3f Speed %.1fkm/h b0", userdata.fps,lat,lon,speed);
cairo_show_text(context, text);
userdata.overlay = 0;
}
else {
cairo_rectangle(context2, 0.0, 0.0, 600, 100);
cairo_set_source_rgba(context2, 0.0, 0.0, 0.0, 1.0);
cairo_fill(context2);
cairo_move_to(context2, 0.0, 0.0);
cairo_set_source_rgba(context2, 1.0, 1.0, 1.0, 1.0);
cairo_move_to(context2, 0.0, 30.0);
cairo_set_font_size(context2, 20.0);
sprintf(text, "%.2fFPS GPS: %.3f, %.3f Speed %.1fkm/h b1", userdata.fps,lat,lon,speed);
//sprintf(text, "%.2fFPS GPS: 0.00000, 0.00000 Speed 0km/h b1", userdata.fps);
cairo_show_text(context2, text);
userdata.overlay = 1;
}
lat += 0.01;
lon += 0.01;
speed += 0.1;
usleep(30000);
}
return 0;
}