summaryrefslogtreecommitdiff
path: root/v4l2-mfc-example/main.c
blob: 78a0c62e0858a19cb09ea9d404d2f344d62e4004 (plain)
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
/*
 * V4L2 Codec decoding example application
 * Kamil Debski <k.debski@samsung.com>
 *
 * Main file of the application
 *
 * Copyright 2012 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <string.h>
#include <linux/videodev2.h>
#include <pthread.h>
#include <semaphore.h>

#include "args.h"
#include "common.h"
#include "fb.h"
#include "fimc.h"
#include "fileops.h"
#include "mfc.h"
#include "parser.h"

/* This is the size of the buffer for the compressed stream.
 * It limits the maximum compressed frame size. */
#define STREAM_BUUFER_SIZE	(1024 * 1024)
/* The number of compress4ed stream buffers */
#define STREAM_BUFFER_CNT	2

/* The number of extra buffers for the decoded output.
 * This is the number of buffers that the application can keep
 * used and still enable MFC to decode with the hardware. */
#define RESULT_EXTRA_BUFFER_CNT 2

void cleanup(struct instance *i)
{
	if (i->mfc.fd)
		mfc_close(i);
	if (i->fimc.fd)
		fimc_close(i);
	if (i->fb.fd)
		fb_close(i);
	if (i->in.fd)
		input_close(i);
	queue_free(&i->fimc.queue);
}

int extract_and_process_header(struct instance *i)
{
	int used, fs;
	int ret;

	ret = i->parser.func(&i->parser.ctx, i->in.p + i->in.offs,
		i->in.size - i->in.offs, i->mfc.out_buf_addr[0],
		i->mfc.out_buf_size, &used, &fs, 1);

	if (ret == 0) {
		err("Failed to extract header from stream");
		return -1;
	}

	/* For H263 the header is passed with the first frame, so we should
	 * pass it again */
	if (i->parser.codec != V4L2_PIX_FMT_H263)
		i->in.offs += used;
	else
	/* To do this we shall reset the stream parser to the initial
	 * configuration */
		parse_stream_init(&i->parser.ctx);

	dbg("Extracted header of size %d", fs);

	ret = mfc_dec_queue_buf_out(i, 0, fs);

	if (ret)
		return -1;

	ret = mfc_stream(i, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, VIDIOC_STREAMON);

	if (ret)
		return -1;

	return 0;
}

int dequeue_output(struct instance *i, int *n)
{
	struct v4l2_buffer qbuf;
	struct v4l2_plane planes[MFC_OUT_PLANES];

	memzero(qbuf);
	qbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
	qbuf.memory = V4L2_MEMORY_MMAP;
	qbuf.m.planes = planes;
	qbuf.length = 1;

	if (mfc_dec_dequeue_buf(i, &qbuf))
		return -1;

	*n = qbuf.index;

	return 0;
}

int dequeue_capture(struct instance *i, int *n, int *finished)
{
	struct v4l2_buffer qbuf;
	struct v4l2_plane planes[MFC_CAP_PLANES];

	memzero(qbuf);
	qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
	qbuf.memory = V4L2_MEMORY_MMAP;
	qbuf.m.planes = planes;
	qbuf.length = 2;

	if (mfc_dec_dequeue_buf(i, &qbuf))
		return -1;

	dbg("Dequeued MFC sequence: %d\n", qbuf.sequence);

	*finished = qbuf.m.planes[0].bytesused == 0;

	*n = qbuf.index;

	return 0;
}

/* This threads is responsible for parsing the stream and
 * feeding MFC with consecutive frames to decode */
void *parser_thread_func(void *args)
{
	struct instance *i = (struct instance *)args;
	int ret;
	int used, fs, n;

	while (!i->error && !i->finish && !i->parser.finished) {
		n = 0;
		while (n < i->mfc.out_buf_cnt && i->mfc.out_buf_flag[n])
			n++;

		if (n < i->mfc.out_buf_cnt && !i->parser.finished) {
			dbg("parser.func = %p", i->parser.func);
			ret = i->parser.func(&i->parser.ctx,
				i->in.p + i->in.offs, i->in.size - i->in.offs,
				i->mfc.out_buf_addr[n], i->mfc.out_buf_size,
				&used, &fs, 0);


			if (ret == 0 && i->in.offs == i->in.size) {
				dbg("Parser has extracted all frames");
				i->parser.finished = 1;
				fs = 0;
			}

			dbg("Extracted frame of size %d", fs);

			dbg("Before OUTPUT queue");
			ret = mfc_dec_queue_buf_out(i, n, fs);
			dbg("After OUTPUT queue");

			i->mfc.out_buf_flag[n] = 1;

			i->in.offs += used;


		} else {
			dbg("Before OUTPUT dequeue");
			ret = dequeue_output(i, &n);
			dbg("After OUTPUT dequeue");
			i->mfc.out_buf_flag[n] = 0;
			if (ret && !i->parser.finished) {
				err("Failed to dequeue a buffer in parser_thread");
				i->error = 1;
			}
		}
	}
	dbg("Parser thread finished");
	return 0;
}

/* This thread handles the CAPTURE side of MFC. it receives
 * decoded frames and queues empty buffers back to MFC.
 * Also it passes the decoded frames to FIMC, so they
 * can be processed and displayed. */
void *mfc_thread_func(void *args)
{
	struct instance *i = (struct instance *)args;
	int finished;
	int n;

	while (!i->error && !i->finish) {
		if (i->mfc.cap_buf_queued < i->mfc.cap_buf_cnt_min) {
			/* sem_wait - wait until there is a buffer returned from
			 * fimc */
			dbg("Before fimc.done");
			sem_wait(&i->fimc.done);
			dbg("After fimc.done");

			n = 0;
			while (n < i->mfc.cap_buf_cnt &&
				i->mfc.cap_buf_flag[n] != BUF_FREE)
				n++;

			if (n < i->mfc.cap_buf_cnt) {
				/* Can queue a buffer */
				mfc_dec_queue_buf_cap(i, n);
				i->mfc.cap_buf_flag[n] = 1;
				i->mfc.cap_buf_queued++;
			} else {
				err("Something went seriously wrong. There should be a buffer");
				i->error = 1;
				continue;
			}

			continue;
		}

		if (i->mfc.cap_buf_queued < i->mfc.cap_buf_cnt) {
			n = 0;
			while (n < i->mfc.cap_buf_cnt &&
				i->mfc.cap_buf_flag[n] != BUF_FREE)
				n++;

			if (n < i->mfc.cap_buf_cnt) {
				/* sem_wait - we already found a buffer to queue
				 * so no waiting */
				dbg("Before fimc.done");
				sem_wait(&i->fimc.done);
				dbg("After fimc.done");

				/* Can queue a buffer */
				mfc_dec_queue_buf_cap(i, n);
				i->mfc.cap_buf_flag[n] = BUF_MFC;
				i->mfc.cap_buf_queued++;
				continue;
			}
		}

		if (i->mfc.cap_buf_queued >= i->mfc.cap_buf_cnt_min) {
			/* Can dequeue a processed buffer */
			if (dequeue_capture(i, &n, &finished)) {
				err("Error when dequeueing CAPTURE buffer");
				i->error = 1;
				break;
			}

			if (finished) {
				dbg("Finished extracting last frames");
				i->finish = 1;
				break;
			}

			/* Pass to the FIMC */
			i->mfc.cap_buf_flag[n] = BUF_FIMC;
			i->mfc.cap_buf_queued--;
			queue_add(&i->fimc.queue, n);

			sem_post(&i->fimc.todo);

			continue;
		}
	}

	dbg("MFC thread finished");
	return 0;
}

/* This thread handles FIMC processing and optionally frame buffer
 * switching and synchronisation to the vsync of frame buffer. */
void *fimc_thread_func(void *args)
{
	static int frames = 0;
	static int first_run = 1;
	struct instance *i = (struct instance *)args;
	int n, tmp;

	while (!i->error && (!i->finish ||
		(i->finish && !queue_empty(&i->fimc.queue))
		)) {
		dbg("Before fimc.todo");
		sem_wait(&i->fimc.todo);
		dbg("After fimc.todo");

		dbg("Processing by FIMC");

		n = queue_remove(&i->fimc.queue);

		if (i->mfc.cap_buf_flag[n] != BUF_FIMC) {
			err("Buffer chosen to be processed by FIMC in wrong");
			i->error = 1;
			break;
		}

		if (n >= i->mfc.cap_buf_cnt) {
			err("Strange. Could not find the buffer to process.");
			i->error = 1;
			break;
		}

		if (fimc_dec_queue_buf_out_from_mfc(i, n)) {
			i->error = 1;
			break;
		}

		i->fb.cur_buf = 0;

		if (i->fb.double_buf) {
			i->fb.cur_buf++;
			i->fb.cur_buf %= i->fb.buffers;
		}

		if (fimc_dec_queue_buf_cap_from_fb(i, i->fb.cur_buf)) {
			i->error = 1;
			break;
		}

		if (first_run) {
			/* Since our fabulous V4L2 framework enforces that at
			 * least one buffer is queued before switching streaming
			 * on then we need to add the following code. Otherwise
			 * it could be ommited and it all would be handled by
			 * the setup sequence in main.*/
			first_run = 0;

			if (fimc_stream(i, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
							VIDIOC_STREAMON)) {
				i->error = 1;
				break;
			}
			if (fimc_stream(i, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
							VIDIOC_STREAMON)) {
				i->error = 1;
				break;
			}
		}

		frames++;

		dbg("Processed frame number: %d", frames);

		if (fimc_dec_dequeue_buf_cap(i, &tmp)) {
			i->error = 1;
			break;
		}
		if (fimc_dec_dequeue_buf_out(i, &tmp)) {
			i->error = 1;
			break;
		}

		if (i->fb.double_buf) {
			fb_set_virt_y_offset(i, i->fb.height);
			fb_wait_for_vsync(i);
		}

		i->mfc.cap_buf_flag[n] = BUF_FREE;

		sem_post(&i->fimc.done);
	}

	dbg("FIMC thread finished");
	return 0;
}

int main(int argc, char **argv)
{
	struct instance inst;
	pthread_t fimc_thread;
	pthread_t mfc_thread;
	pthread_t parser_thread;
	int n;

	printf("V4L2 Codec decoding example application\n");
	printf("Kamil Debski <k.debski@samsung.com>\n");
	printf("Copyright 2012 Samsung Electronics Co., Ltd.\n\n");

	if (parse_args(&inst, argc, argv)) {
		print_usage(argv[0]);
		return 1;
	}

	if (queue_init(&inst.fimc.queue, MFC_MAX_CAP_BUF))
		return 1;

	if (input_open(&inst, inst.in.name)) {
		cleanup(&inst);
		return 1;
	}

	if (fb_open(&inst, inst.fb.name)) {
		cleanup(&inst);
		return 1;
	}

	if (fimc_open(&inst, inst.fimc.name)) {
		cleanup(&inst);
		return 1;
	}

	if (mfc_open(&inst, inst.mfc.name)) {
		cleanup(&inst);
		return 1;
	}

	dbg("Successfully opened all necessary files and devices");

	if (mfc_dec_setup_output(&inst, inst.parser.codec,
		STREAM_BUUFER_SIZE, STREAM_BUFFER_CNT)) {
		cleanup(&inst);
		return 1;
	}

	parse_stream_init(&inst.parser.ctx);

	if (extract_and_process_header(&inst)) {
		cleanup(&inst);
		return 1;
	}

	if (mfc_dec_setup_capture(&inst, RESULT_EXTRA_BUFFER_CNT)) {
		cleanup(&inst);
		return 1;
	}

	if (dequeue_output(&inst, &n)) {
		cleanup(&inst);
		return 1;
	}

	if (fimc_setup_output_from_mfc(&inst)) {
		cleanup(&inst);
		return 1;
	}

	if (fimc_setup_capture_from_fb(&inst)) {
		cleanup(&inst);
		return 1;
	}

	if (fimc_set_crop(&inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
		inst.mfc.cap_crop_w, inst.mfc.cap_crop_h,
		inst.mfc.cap_crop_left, inst.mfc.cap_crop_top)) {
		cleanup(&inst);
		return 1;
	}

	dbg("I for one welcome our succesfully setup environment.");

	/* Since our fabulous V4L2 framework enforces that at least one buffer
	 * is queued before switching streaming on then we need to add the
	 * following code. Otherwise it could be ommited and it all would be
	 * handled by the mfc_thread.*/

	for (n = 0 ; n < inst.mfc.cap_buf_cnt; n++) {

		if (mfc_dec_queue_buf_cap(&inst, n)) {
			cleanup(&inst);
			return 1;
		}

		inst.mfc.cap_buf_flag[n] = BUF_MFC;
		inst.mfc.cap_buf_queued++;
	}

	if (mfc_stream(&inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
							VIDIOC_STREAMON)) {
		cleanup(&inst);
		return 1;
	}

	sem_init(&inst.fimc.todo, 0, 0);
	sem_init(&inst.fimc.done, 0, 0);

	/* Now we're safe to run the threads */
	dbg("Launching threads");

	if (pthread_create(&parser_thread, NULL, parser_thread_func, &inst)) {
		cleanup(&inst);
		return 1;
	}

	if (pthread_create(&mfc_thread, NULL, mfc_thread_func, &inst)) {
		cleanup(&inst);
		return 1;
	}

	if (pthread_create(&fimc_thread, NULL, fimc_thread_func, &inst)) {
		cleanup(&inst);
		return 1;
	}


	pthread_join(parser_thread, 0);
	pthread_join(mfc_thread, 0);
	pthread_join(fimc_thread, 0);

	dbg("Threads have finished");

	cleanup(&inst);
	return 0;
}