summaryrefslogtreecommitdiff
path: root/lib/test-source.c
blob: 743d8b38e9f9a62887bb29c34efb9ec1e831a38b (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Test video source
 *
 * Copyright (C) 2018 Paul Elder
 *
 * Contact: Paul Elder <paul.elder@ideasonboard.com>
 */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <linux/videodev2.h>

#include "events.h"
#include "test-source.h"
#include "tools.h"
#include "video-buffers.h"

#define BLACK   0x80108010
#define BLUE    0x7620f020
#define CYAN    0x10bc9abc
#define GREEN   0x2aad1aad
#define GREY    0x80b480b4
#define MAGENTA 0xe64ed64e
#define RED     0xf03f663f
#define WHITE   0x80eb80eb
#define YELLOW  0x8adb10db

struct test_source {
	struct video_source src;

	unsigned int width;
	unsigned int height;
	unsigned int pixelformat;
};

#define to_test_source(s) container_of(s, struct test_source, src)

static void test_source_destroy(struct video_source *s)
{
	struct test_source *src = to_test_source(s);

	free(src);
}

static int test_source_set_format(struct video_source *s,
				  struct v4l2_pix_format *fmt)
{
	struct test_source *src = to_test_source(s);

	src->width = fmt->width;
	src->height = fmt->height;
	src->pixelformat = fmt->pixelformat;

	if (src->pixelformat != v4l2_fourcc('Y', 'U', 'Y', 'V'))
		return -EINVAL;

	return 0;
}

static int test_source_set_frame_rate(struct video_source *s __attribute__((unused)),
				      unsigned int fps __attribute__((unused)))
{
	return 0;
}

static int test_source_free_buffers(struct video_source *s __attribute__((unused)))
{
	return 0;
}

static int test_source_stream_on(struct video_source *s __attribute__((unused)))
{
	return 0;
}

static int test_source_stream_off(struct video_source *s __attribute__((unused)))
{
	return 0;
}

static void test_source_fill_buffer(struct video_source *s,
				    struct video_buffer *buf)
{
	struct test_source *src = to_test_source(s);
	unsigned int bpl;
	unsigned int i, j;
	void *mem = buf->mem;

	bpl = src->width * 2;
	for (i = 0; i < src->height; ++i) {
		for (j = 0; j < bpl; j += 4) {
			if (j < bpl * 1 / 8)
				*(unsigned int *)(mem + i*bpl + j) = WHITE;
			else if (j < bpl * 2 / 8)
				*(unsigned int *)(mem + i*bpl + j) = YELLOW;
			else if (j < bpl * 3 / 8)
				*(unsigned int *)(mem + i*bpl + j) = CYAN;
			else if (j < bpl * 4 / 8)
				*(unsigned int *)(mem + i*bpl + j) = GREEN;
			else if (j < bpl * 5 / 8)
				*(unsigned int *)(mem + i*bpl + j) = MAGENTA;
			else if (j < bpl * 6 / 8)
				*(unsigned int *)(mem + i*bpl + j) = RED;
			else if (j < bpl * 7 / 8)
				*(unsigned int *)(mem + i*bpl + j) = BLUE;
			else
				*(unsigned int *)(mem + i*bpl + j) = BLACK;
		}
	}

	buf->bytesused = bpl * src->height;
}

static const struct video_source_ops test_source_ops = {
	.destroy = test_source_destroy,
	.set_format = test_source_set_format,
	.set_frame_rate = test_source_set_frame_rate,
	.free_buffers = test_source_free_buffers,
	.stream_on = test_source_stream_on,
	.stream_off = test_source_stream_off,
	.queue_buffer = NULL,
	.fill_buffer = test_source_fill_buffer,
};

struct video_source *test_video_source_create()
{
	struct test_source *src;

	src = malloc(sizeof *src);
	if (!src)
		return NULL;

	memset(src, 0, sizeof *src);
	src->src.ops = &test_source_ops;

	return &src->src;
}

void test_video_source_init(struct video_source *s, struct events *events)
{
	struct test_source *src = to_test_source(s);

	src->src.events = events;
}