-
Notifications
You must be signed in to change notification settings - Fork 42
/
buffer_demo.c
297 lines (225 loc) · 9.57 KB
/
buffer_demo.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
/*
* 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"
#define MMAL_CAMERA_PREVIEW_PORT 0
#define MMAL_CAMERA_VIDEO_PORT 1
#define MMAL_CAMERA_CAPTURE_PORT 2
MMAL_POOL_T *camera_video_port_pool;
MMAL_POOL_T *preview_input_port_pool;
MMAL_PORT_T *preview_input_port = NULL;
static void preview_buffer_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer) {
//printf("INFO:preview_buffer_callback buffer->length = %d\n", buffer->length);
mmal_buffer_header_release(buffer);
}
static void video_buffer_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer) {
static int loop = 0;
static struct timespec t1;
struct timespec t2;
//printf("INFO:video_buffer_callback\n");
if (loop == 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 *preview_new_buffer;
MMAL_POOL_T *pool = (MMAL_POOL_T *) port->userdata;
loop++;
preview_new_buffer = mmal_queue_get(preview_input_port_pool->queue);
if (preview_new_buffer) {
//printf("preview_new_buffer->length = %d \n", preview_new_buffer->length);
//memcpy(preview_new_buffer->data, buffer->data, buffer->length);
memcpy(preview_new_buffer->data, buffer->data, 1280*720); // copy only Y
int i;
for (i=921600; i < 921600+230400; i++) {
preview_new_buffer->data[i] = 0x00;
}
for (i=921600+230400; i < 1382400; i++) {
preview_new_buffer->data[i] = 0b10101010;
}
preview_new_buffer->length = buffer->length;
if (mmal_port_send_buffer(preview_input_port, preview_new_buffer) != MMAL_SUCCESS) {
printf("ERROR: Unable to send buffer \n");
}
} else {
printf("ERROR: mmal_queue_get (%d)\n", preview_new_buffer);
}
if (loop % 10 == 0) {
//fprintf(stderr, "loop = %d \n", loop);
printf("loop = %d, Framerate = %d fps, buffer->length = %d \n", loop, loop / (d + 1), buffer->length);
}
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)
printf("Unable to return a buffer to the video port\n");
}
}
int main(int argc, char** argv) {
MMAL_COMPONENT_T *camera = 0;
MMAL_COMPONENT_T *preview = 0;
MMAL_ES_FORMAT_T *format;
MMAL_STATUS_T status;
MMAL_PORT_T *camera_preview_port = NULL, *camera_video_port = NULL, *camera_still_port = NULL;
MMAL_CONNECTION_T *camera_preview_connection = 0;
printf("Running...\n");
bcm_host_init();
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_CAMERA, &camera);
if (status != MMAL_SUCCESS) {
printf("Error: create camera %x\n", status);
return -1;
}
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 = 1280,
.max_preview_video_h = 720,
.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 = 1280;
format->es->video.height = 720;
format->es->video.crop.x = 0;
format->es->video.crop.y = 0;
format->es->video.crop.width = 1280;
format->es->video.crop.height = 720;
status = mmal_port_format_commit(camera_preview_port);
if (status != MMAL_SUCCESS) {
printf("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 = 1280;
format->es->video.height = 720;
format->es->video.crop.x = 0;
format->es->video.crop.y = 0;
format->es->video.crop.width = 1280;
format->es->video.crop.height = 720;
format->es->video.frame_rate.num = 30;
format->es->video.frame_rate.den = 1;
camera_video_port->buffer_size = camera_video_port->buffer_size_recommended;
camera_video_port->buffer_num = 2;
status = mmal_port_format_commit(camera_video_port);
printf(" camera video buffer_size = %d\n", camera_video_port->buffer_size);
printf(" camera video buffer_num = %d\n", camera_video_port->buffer_num);
if (status != MMAL_SUCCESS) {
printf("Error: unable to commit camera video port format (%u)\n", status);
return -1;
}
// crate pool form camera video port
camera_video_port_pool = (MMAL_POOL_T *)mmal_port_pool_create(camera_video_port, camera_video_port->buffer_num, camera_video_port->buffer_size);
camera_video_port->userdata = (struct MMAL_PORT_USERDATA_T *) camera_video_port_pool;
status = mmal_port_enable(camera_video_port, video_buffer_callback);
if (status != MMAL_SUCCESS) {
printf("Error: unable to enable camera video port (%u)\n", status);
return -1;
}
status = mmal_component_enable(camera);
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_RENDERER, &preview);
if (status != MMAL_SUCCESS) {
printf("Error: unable to create preview (%u)\n", status);
return -1;
}
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) {
printf("Error: unable to set preview port parameters (%u)\n", status);
return -1;
}
}
mmal_format_copy(preview_input_port->format, camera_video_port->format);
format = preview_input_port->format;
format->encoding = MMAL_ENCODING_I420;
format->encoding_variant = MMAL_ENCODING_I420;
format->es->video.width = 1280;
format->es->video.height = 720;
format->es->video.crop.x = 0;
format->es->video.crop.y = 0;
format->es->video.crop.width = 1280;
format->es->video.crop.height = 720;
format->es->video.frame_rate.num = 30;
format->es->video.frame_rate.den = 1;
preview_input_port->buffer_size = camera_video_port->buffer_size_recommended;
preview_input_port->buffer_num = 4;
printf(" preview buffer_size = %d\n", preview_input_port->buffer_size);
printf(" preview buffer_num = %d\n", preview_input_port->buffer_num);
status = mmal_port_format_commit(preview_input_port);
preview_input_port_pool = (MMAL_POOL_T *)mmal_port_pool_create(preview_input_port, preview_input_port->buffer_num, preview_input_port->buffer_size);
preview_input_port->userdata = (struct MMAL_PORT_USERDATA_T *) preview_input_port_pool;
status = mmal_port_enable(preview_input_port, preview_buffer_callback);
if (status != MMAL_SUCCESS) {
printf("Error: unable to enable preview input port (%u)\n", status);
return -1;
}
/*
status = mmal_connection_create(&camera_preview_connection, camera_preview_port, preview_input_port, MMAL_CONNECTION_FLAG_TUNNELLING | MMAL_CONNECTION_FLAG_ALLOCATION_ON_INPUT);
if (status != MMAL_SUCCESS) {
printf("Error: unable to create connection (%u)\n", status);
return -1;
}
status = mmal_connection_enable(camera_preview_connection);
if (status != MMAL_SUCCESS) {
printf("Error: unable to enable connection (%u)\n", status);
return -1;
}
*/
if (1) {
// Send all the buffers to the encoder output port
int num = mmal_queue_length(camera_video_port_pool->queue);
int q;
for (q = 0; q < num; q++) {
MMAL_BUFFER_HEADER_T *buffer = mmal_queue_get(camera_video_port_pool->queue);
if (!buffer)
printf("Unable to get a required buffer %d from pool queue\n", q);
if (mmal_port_send_buffer(camera_video_port, buffer) != MMAL_SUCCESS)
printf("Unable to send a buffer to encoder output port (%d)\n", q);
}
}
if (mmal_port_parameter_set_boolean(camera_video_port, MMAL_PARAMETER_CAPTURE, 1) != MMAL_SUCCESS) {
printf("%s: Failed to start capture\n", __func__);
}
while (1);
return 0;
}