-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmanual_decode_overlay_encode.c
432 lines (328 loc) · 15.4 KB
/
manual_decode_overlay_encode.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
#include "bcm_host.h"
#include "mmal.h"
#include "mmal_port.h"
#include "mmal_pool.h"
#include "util/mmal_connection.h"
#include "util/mmal_default_components.h"
#include "util/mmal_util.h"
#include "util/mmal_util_params.h"
#include <stdio.h>
#include "interface/vcos/vcos.h"
static const int MAX_BITRATE_LEVEL4 = 25000000; // 25Mbits/s
#define CHECK_STATUS(status, msg) if (status != MMAL_SUCCESS) { fprintf(stderr, msg"\n"); goto error; }
static FILE *source_file;
static FILE *dest_file;
/* Macros abstracting the I/O, just to make the example code clearer */
#define SOURCE_OPEN(uri) \
source_file = fopen(uri, "rb"); if (!source_file) goto error;
#define SOURCE_READ_DATA_INTO_BUFFER(a) \
a->length = fread(a->data, 1, a->alloc_size - 128, source_file); \
a->offset = 0; a->pts = a->dts = MMAL_TIME_UNKNOWN
#define SOURCE_CLOSE() \
if (source_file) fclose(source_file)
#define DEST_OPEN(uri) \
dest_file = fopen(uri, "wb"); if (!dest_file) goto error;
#define DEST_WRITE_DATA_INTO_FILE(buf,len) \
fwrite(buf,1,len,dest_file)
#define DEST_CLOSE() \
if (dest_file) fclose(dest_file)
/** Context for our application */
static struct CONTEXT_T {
VCOS_SEMAPHORE_T semaphore;
MMAL_QUEUE_T *queue_encoded;
MMAL_PORT_T* encoder_input_port;
MMAL_PORT_T* encoder_output_port;
MMAL_POOL_T * encoder_pool_in;
MMAL_STATUS_T status;
} context;
static int framenr=0;
static void log_video_format(MMAL_ES_FORMAT_T *format)
{
if (format->type != MMAL_ES_TYPE_VIDEO)
return;
fprintf(stderr, "fourcc: %s, variant; %s width: %i, height: %i, (%i,%i,%i,%i)\n",
(char *)&format->encoding,
(char *)&format->encoding_variant,
format->es->video.width, format->es->video.height,
format->es->video.crop.x, format->es->video.crop.y,
format->es->video.crop.width, format->es->video.crop.height);
}
/** Callback from the encoder input port.
* Buffer has been consumed and is available to be used again. */
static void encoder_input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
struct CONTEXT_T *ctx = (struct CONTEXT_T *)port->userdata;
/* The encoder is done with the data, just recycle the buffer header into its pool */
mmal_buffer_header_release(buffer);
//fprintf(stderr,"encoder input callback\n");
}
/** Callback from the encoder output port.
* Buffer has been produced by the port and is available for processing. */
static void encoder_output_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
struct CONTEXT_T *ctx = (struct CONTEXT_T *)port->userdata;
/* Queue the encoded video frame so that we can grab it in the main loop*/
mmal_queue_put(ctx->queue_encoded, buffer);
/* Kick the processing thread */
vcos_semaphore_post(&ctx->semaphore);
//fprintf(stderr,"encoder output callback\n");
}
/** Callback from the decoder input port.
* Buffer has been consumed and is available to be used again. */
static void decoder_input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
struct CONTEXT_T *ctx = (struct CONTEXT_T *)port->userdata;
/* The decoder is done with the data, just recycle the buffer header into its pool */
mmal_buffer_header_release(buffer);
/* Kick the processing thread */
vcos_semaphore_post(&ctx->semaphore);
//fprintf(stderr,"decoder input callback\n");
}
/** Callback from the pool. Buffer is available. */
static MMAL_BOOL_T pool_buffer_available_callback(MMAL_POOL_T *pool, MMAL_BUFFER_HEADER_T *buffer,
void *userdata)
{
MMAL_PARAM_UNUSED(userdata);
mmal_queue_put(pool->queue, buffer);
return 0;
}
static void draw_overlay(MMAL_BUFFER_HEADER_T *frame) {
static const int width =1280;
for (int y = framenr+200; y < framenr+300; y++) {
for (int x= framenr+100; x< framenr+200; x++) {
if ((x & 2) || (y & 2)) { //generate a chess-board like pattern
frame->data[y*width+x] = 0;
} else {
frame->data[y*width+x] = 255;
}
}
}
}
/** Callback from the decoder output port.
* Buffer has been produced by the port and is available for processing. */
static void decoder_output_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
struct CONTEXT_T *ctx = (struct CONTEXT_T *)port->userdata;
if (buffer->cmd) {
if (buffer->cmd != MMAL_EVENT_FORMAT_CHANGED) {
fprintf(stderr,"unknown cmd: %u\n",buffer->cmd);
return;
}
MMAL_EVENT_FORMAT_CHANGED_T *event = mmal_event_format_changed_get(buffer);
if (!event){
fprintf(stderr,"could not get format change event");
return;
}
MMAL_STATUS_T status = mmal_format_full_copy(ctx->encoder_input_port->format, event->format);
if (status != MMAL_SUCCESS) {
fprintf(stderr,"could not copy format from event to input format: %s",mmal_status_to_string(status));
return;
}
status = mmal_port_format_commit(ctx->encoder_input_port);
if (status != MMAL_SUCCESS) {
fprintf(stderr,"could not commit input format: %s",mmal_status_to_string(status));
return;
}
MMAL_ES_FORMAT_T* format_out = ctx->encoder_output_port->format;
format_out->bitrate=MAX_BITRATE_LEVEL4;
status = mmal_port_format_commit(ctx->encoder_output_port);
if (status != MMAL_SUCCESS) {
fprintf(stderr,"could not set encoder output format: %s",mmal_status_to_string(status));
return;
}
status = mmal_port_enable(ctx->encoder_input_port, encoder_input_callback);
if (status != MMAL_SUCCESS) {
fprintf(stderr,"could not enable encoder input port: %s",mmal_status_to_string(status));
return;
}
status = mmal_port_enable(ctx->encoder_output_port, encoder_output_callback);
if (status != MMAL_SUCCESS) {
fprintf(stderr,"could not enable encoder output port: %s",mmal_status_to_string(status));
return;
}
//create pool with corret buffer requirements
ctx->encoder_pool_in =mmal_port_pool_create(ctx->encoder_input_port,ctx->encoder_input_port->buffer_num_recommended,ctx->encoder_input_port->buffer_size_recommended);
mmal_pool_callback_set(ctx->encoder_pool_in, pool_buffer_available_callback, NULL);
mmal_buffer_header_release(buffer);
fprintf(stderr,"Encoder enabled\n");
} else {
draw_overlay(buffer);
ctx->status = mmal_port_send_buffer(ctx->encoder_input_port, buffer);
if (ctx->status != MMAL_SUCCESS)
{
fprintf(stderr,"could not send buffer from decoder output to encoder input: %s\n",
mmal_status_to_string(ctx->status));
mmal_buffer_header_release(buffer);
//mmal_event_error_send(connection->out->component, status);
}
}
/* Kick the processing thread */
//vcos_semaphore_post(&ctx->semaphore);
//fprintf(stderr,"decoder output callback\n");
}
/** Callback from the control port.
* Component is sending us an event. */
static void decoder_control_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
struct CONTEXT_T *ctx = (struct CONTEXT_T *)port->userdata;
switch (buffer->cmd)
{
case MMAL_EVENT_EOS:
/* Only sink component generate EOS events */
break;
case MMAL_EVENT_ERROR:
/* Something went wrong. Signal this to the application */
ctx->status = *(MMAL_STATUS_T *)buffer->data;
break;
default:
break;
}
/* Done with the event, recycle it */
mmal_buffer_header_release(buffer);
fprintf(stderr,"control cb. status %s\n",mmal_status_to_string(ctx->status));
}
int main(int argc, char* argv[]) {
MMAL_STATUS_T status;
//MMAL_CONNECTION_T *conn = NULL;
MMAL_COMPONENT_T *decoder = NULL, *encoder=NULL;
MMAL_POOL_T *decoder_pool_in = NULL, *encoder_pool_out = NULL;
MMAL_ES_FORMAT_T * format_in=NULL;
MMAL_BOOL_T eos_sent = MMAL_FALSE, eos_received= MMAL_FALSE;
MMAL_BUFFER_HEADER_T *buffer;
bcm_host_init();
vcos_semaphore_create(&context.semaphore, "example", 1);
SOURCE_OPEN("test.h264_2")
DEST_OPEN("out.h264")
/* Create the components */
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_DECODER, &decoder);
CHECK_STATUS(status, "failed to create decoder");
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_ENCODER, &encoder);
CHECK_STATUS(status, "failed to create encoder");
/* Enable control port so we can receive events from the component */
decoder->control->userdata = (struct MMAL_PORT_USERDATA_T*)(void *)&context;
status = mmal_port_enable(decoder->control, decoder_control_callback);
CHECK_STATUS(status, "failed to enable control port");
/* Enable zero-copy parameters on all ports */
status = mmal_port_parameter_set_boolean(decoder->input[0], MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE);
CHECK_STATUS(status, "failed to set zero copy on decoder input");
status = mmal_port_parameter_set_boolean(encoder->output[0], MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE);
CHECK_STATUS(status, "failed to set zero copy on encoder output");
status = mmal_port_parameter_set_boolean(decoder->output[0], MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE);
CHECK_STATUS(status, "failed to set zero copy on decoder output");
status = mmal_port_parameter_set_boolean(encoder->input[0], MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE);
CHECK_STATUS(status, "failed to set zero copy on encoder input");
/* Set format of video decoder input port */
format_in = decoder->input[0]->format;
format_in->type = MMAL_ES_TYPE_VIDEO;
format_in->encoding = MMAL_ENCODING_H264;
format_in->es->video.width = 1280;
format_in->es->video.height = 720;
format_in->es->video.frame_rate.num = 25;
format_in->es->video.frame_rate.den = 1;
format_in->es->video.par.num = 1;
format_in->es->video.par.den = 1;
/* If the data is known to be framed then the following flag should be set:*/
//format_in->flags |= MMAL_ES_FORMAT_FLAG_FRAMED;
status = mmal_port_format_commit(decoder->input[0]);
CHECK_STATUS(status, "failed to commit format");
//format_out = decoder->output[0]->format;
//status = mmal_port_format_commit(decoder->output[0]);
//CHECK_STATUS(status, "failed to commit format");
MMAL_PARAMETER_VIDEO_PROFILE_T param;
param.hdr.id = MMAL_PARAMETER_PROFILE;
param.hdr.size = sizeof(param);
param.profile[0].level = MMAL_VIDEO_LEVEL_H264_4;
param.profile[0].profile = MMAL_VIDEO_PROFILE_H264_HIGH;
status = mmal_port_parameter_set(encoder->output[0], ¶m.hdr);
CHECK_STATUS(status, "unable to set encoder output profile");
decoder->input[0]->buffer_num = decoder->input[0]->buffer_num_min;
decoder->input[0]->buffer_size = decoder->input[0]->buffer_size_min;
decoder->output[0]->buffer_num = decoder->output[0]->buffer_num_min;
decoder->output[0]->buffer_size = decoder->output[0]->buffer_size_min;
decoder_pool_in = mmal_port_pool_create(decoder->input[0],decoder->input[0]->buffer_num,decoder->input[0]->buffer_size);
encoder_pool_out = mmal_port_pool_create(encoder->output[0], encoder->output[0]->buffer_num, encoder->output[0]->buffer_size);
context.queue_encoded = mmal_queue_create();
context.encoder_pool_in = NULL; //pool cannot be allocated yet, because buffer requirements are not known yet. See decoder output callback
context.encoder_input_port = encoder->input[0];
context.encoder_output_port = encoder->output[0];
/* Store a reference to our context in each port (will be used during callbacks) */
decoder->input[0]->userdata = (void *)&context;
decoder->output[0]->userdata = (void*) &context;
encoder->input[0]->userdata = (void*) &context;
encoder->output[0]->userdata = (void *)&context;
status = mmal_port_enable(decoder->input[0], decoder_input_callback);
CHECK_STATUS(status, "failed to enable decoder input port")
status = mmal_port_enable(decoder->output[0], decoder_output_callback);
CHECK_STATUS(status, "failed to enable decoder output port")
//we do not enable the encoder ports yet. Instead we wait on the format change event on the decoder output. Then we finish configuring the encoder.
/* Start transcoding */
fprintf(stderr, "start transcoding\n");
while(eos_received == MMAL_FALSE)
{
/* Wait for buffer headers to be available on either the decoder input or the encoder output port */
vcos_semaphore_wait(&context.semaphore);
/* Send data to decode to the input port of the video decoder */
if ((buffer = mmal_queue_get(decoder_pool_in->queue)) != NULL) //Get empty buffers from queue
{
SOURCE_READ_DATA_INTO_BUFFER(buffer);
if(!buffer->length) eos_sent = MMAL_TRUE;
buffer->flags = buffer->length ? 0 : MMAL_BUFFER_HEADER_FLAG_EOS;
buffer->pts = buffer->dts = MMAL_TIME_UNKNOWN;
//fprintf(stderr, "sending %i bytes\n", (int)buffer->length);
status = mmal_port_send_buffer(decoder->input[0], buffer);
CHECK_STATUS(status, "failed to send buffer");
}
/* receive encoded frames and store them */
while ((buffer = mmal_queue_get(context.queue_encoded)) != NULL)
{
/* We have a frame, do something with it
* Once we're done with it, we release it. It will automatically go back
* to its original pool so it can be reused for a new video frame.
*/
eos_received = buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS;
if (buffer->cmd)
{
fprintf(stderr, "received event %4.4s\n", (char *)&buffer->cmd);
}
else
{
DEST_WRITE_DATA_INTO_FILE(buffer->data, buffer->length);
fprintf(stderr, "encoded frame %u (flags %x, length %u)\n",framenr++, buffer->flags, buffer->length);
}
mmal_buffer_header_release(buffer);
}
if(encoder->output[0]->is_enabled) { //do not send buffers until all ports and pools are created
/* Send empty buffers to the output port of the decoder */
while((buffer = mmal_queue_get(context.encoder_pool_in->queue)) != NULL)
{
status = mmal_port_send_buffer(decoder->output[0], buffer);
CHECK_STATUS(status, "failed to send empty buffer to decoder output\n");
}
/* Send empty buffers to the output port of the encoder */
while ((buffer = mmal_queue_get(encoder_pool_out->queue)) != NULL)
{
status = mmal_port_send_buffer(encoder->output[0], buffer);
CHECK_STATUS(status, "failed to send empty buffer to encoder output\n");
}
}
}
/* Stop decoding */
fprintf(stderr, "stop transcoding\n");
/* Stop everything. Not strictly necessary since mmal_component_destroy()
* will do that anyway */
mmal_port_disable(decoder->input[0]);
mmal_port_disable(decoder->control);
mmal_port_disable(encoder->input[0]);
mmal_port_disable(decoder->output[0]);
mmal_port_disable(encoder->output[0]);
fprintf(stderr, "done\n");
SOURCE_CLOSE();
DEST_CLOSE();
error:
/* Cleanup everything */
if (decoder)
mmal_component_release(decoder);
if (encoder)
mmal_component_release(encoder);
return status == MMAL_SUCCESS ? 0 : -1;
}