summaryrefslogtreecommitdiff
path: root/plugins/v4l2/xlnx-v4l2.c
blob: c3070417f60da5118bea770c30a9293f4d30e387 (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
/*
 * Xilinx Video Library - V4L2 plugin
 *
 * Copyright (C) 2014-2016 Ideas on board Oy
 *
 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 */

#if 0
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <v4l2.h>
#include <v4l2subdev.h>

#include <xlnx-list.h>
#include <xlnx-tools.h>
#include <xlnx-video.h>

/*static inline struct xlnx_video_node *
to_xlnx_video_node(struct xlnx_video_entity *e) {
	return container_of(e, struct xlnx_video_node, entity);
}*/

/* -----------------------------------------------------------------------------
 * Helper functions
 */

/*
 * struct xlnx_video_format_info - Media bus format information
 * @code: V4L2 media bus format code
 * @pixelformat: V4L2 pixel format FCC identifier
 */
struct xlnx_video_format_info {
	enum v4l2_mbus_pixelcode code;
	__u32 pixelformat;
};

static const struct xlnx_video_format_info formats[] = {
	{ MEDIA_BUS_FMT_RGB888_1X32_PADHI, V4L2_PIX_FMT_BGR32, },
	{ MEDIA_BUS_FMT_UYVY8_1X16, V4L2_PIX_FMT_YUYV, },
	{ MEDIA_BUS_FMT_VUY8_1X24, V4L2_PIX_FMT_YUV444, },
	{ MEDIA_BUS_FMT_SRGGB8_1X8, V4L2_PIX_FMT_SGRBG8, },
	{ MEDIA_BUS_FMT_SGRBG8_1X8, V4L2_PIX_FMT_SGRBG8, },
	{ MEDIA_BUS_FMT_SGBRG8_1X8, V4L2_PIX_FMT_SGBRG8, },
	{ MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_PIX_FMT_SBGGR8, },
};

static __u32 mbus_to_pix(enum v4l2_mbus_pixelcode code)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
		if (formats[i].code == code)
			return formats[i].pixelformat;
	}

	return 0;
}

static enum v4l2_mbus_pixelcode pix_to_mbus(__u32 pixelformat)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
		if (formats[i].pixelformat == pixelformat)
			return formats[i].code;
	}

	return 0;
}

/* -----------------------------------------------------------------------------
 * Video streaming
 */

int xlnx_video_component_set_format(struct xlnx_video_component *xvcomp,
				   struct v4l2_mbus_framefmt *fmt)
{
	struct v4l2_mbus_framefmt format;
	struct v4l2_pix_format v4l2_fmt;
	struct xlnx_video_entity *source = NULL;
	struct xlnx_video_entity *sink;
	struct xlnx_video_node *video;
	struct media_entity_pad *pad;
	int ret;

	/* Configure formats. Start from the input and propagate the format
	 * through the component.
	 */
	format = *initial_fmt;

	list_for_each_entry(sink, &xvcomp->entities, list) {
		if (source == NULL) {
			/* Configure the first entity in the component with the
			 * initial format.
			 */
			pad = sink->source.link->source;

			if ((sink->info->type & MEDIA_ENT_TYPE_MASK) == MEDIA_ENT_T_V4L2_SUBDEV) {
				ret = v4l2_subdev_set_format(sink->entity, &format, 0,
							     V4L2_SUBDEV_FORMAT_ACTIVE);
				if (ret < 0) {
					printf("error: set format on component input failed.\n");
					return ret;
				}
			} else if (sink->info->type == MEDIA_ENT_T_DEVNODE_V4L) {
				video = to_xlnx_video_node(sink);

				memset(&v4l2_fmt, 0, sizeof v4l2_fmt);
				v4l2_fmt.pixelformat = mbus_to_pix(format.code);
				v4l2_fmt.width = format.width;
				v4l2_fmt.height = format.height;

				ret = v4l2_set_format(video->video, &v4l2_fmt);
				if (ret < 0) {
					printf("error: set format failed on %s.\n",
						sink->info->name);
					return ret;
				}

				format.code = pix_to_mbus(v4l2_fmt.pixelformat);
				format.width = v4l2_fmt.width;
				format.height = v4l2_fmt.height;

				video->format = format;
			}

			source = sink;
			continue;
		}

		if ((source->info->type & MEDIA_ENT_TYPE_MASK) == MEDIA_ENT_T_V4L2_SUBDEV) {
			pad = source->source.link->source;

			/* Try to force the output format code onto the source pad. */
			ret = v4l2_subdev_get_format(pad->entity, &format, pad->index,
						     V4L2_SUBDEV_FORMAT_ACTIVE);
			if (ret < 0) {
				printf("error: set format failed on %s:%u.\n",
					pad->entity->info->name, pad->index);
				return ret;
			}

			if (sink->last)
				format = *fmt;
			else
				format.code = fmt->code;

			ret = v4l2_subdev_set_format(pad->entity, &format, pad->index,
						     V4L2_SUBDEV_FORMAT_ACTIVE);
			if (ret < 0) {
				printf("error: set format failed on %s:%u.\n",
					pad->entity->info.name, pad->index);
				return ret;
			}

			if (sink->last)
				*fmt = format;
		}

		source->source.format = format;

		if ((sink->info->type & MEDIA_ENT_TYPE_MASK) == MEDIA_ENT_T_V4L2_SUBDEV) {
			/* Propagate the format to the link target. */
			pad = sink->sink.link->sink;

			ret = v4l2_subdev_set_format(pad->entity, &format, pad->index,
						     V4L2_SUBDEV_FORMAT_ACTIVE);
			if (ret < 0) {
				printf("error: set format failed on %s:%u.\n",
					pad->entity->info.name, pad->index);
				return ret;
			}
		} else if (sink->info->type == MEDIA_ENT_T_DEVNODE_V4L) {
			video = to_xlnx_video_node(sink);

			/* Set the capture format on the capture or output video
			 * node. If the connected source is a pool, v4l2_fmt
			 * already contains the format that has been setup on
			 * the associated capture video node.
			 */
			memset(&v4l2_fmt, 0, sizeof v4l2_fmt);
			v4l2_fmt.pixelformat = mbus_to_pix(format.code);
			v4l2_fmt.width = format.width;
			v4l2_fmt.height = format.height;

			ret = v4l2_set_format(video->video, &v4l2_fmt);
			if (ret < 0) {
				printf("error: set format failed on %s.\n",
					sink->info->name);
				return ret;
			}

			format.code = pix_to_mbus(v4l2_fmt.pixelformat);
			format.width = v4l2_fmt.width;
			format.height = v4l2_fmt.height;

			video->format = format;
		}

		sink->sink.format = format;

		source = sink;
	}

	return 0;
}

static int xlnx_video_validate_entity(struct media_entity *entity,
				      const struct media_link **linkp)
{
	const struct media_link *link_source = NULL;
	const struct media_link *link_sink = NULL;
	const struct media_entity_desc *info;
	unsigned int num_links;
	unsigned int i;

	info = media_entity_get_info(entity);

	if (info->type == MEDIA_ENT_T_DEVNODE_V4L) {
		const struct media_link *link;

		/* Video nodes must have a single pad and a single link. */
		if (info->pads != 1)
			return -EINVAL;

		if (media_entity_get_links_count(entity) != 1)
			return -EINVAL;

		link = media_entity_get_link(entity, 0);
		*linkp = link->sink->entity == entity ? link : NULL;
		return 0;
	}

	if ((info->type & MEDIA_ENT_TYPE_MASK) != MEDIA_ENT_T_V4L2_SUBDEV)
		return -EINVAL;

	/*
	 * Only components with linear internal pipelines are supported, the
	 * entity must have a single connected source and a single connected
	 * sink.
	 */
	num_links = media_entity_get_links_count(entity);

	for (i = 0; i < num_links; ++i) {
		const struct media_link *link = media_entity_get_link(entity, i);

		/*
		 * Components with configurable internal pipelines are not
		 * supported.
		 */
		if (!(link->flags & MEDIA_LNK_FL_IMMUTABLE))
			return -EINVAL;

		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
			continue;

		if (link->source->entity == entity) {
			if (link_source && link_source != link)
				return -EINVAL;
			link_source = link;
		} else {
			if (link_sink && link_sink != link)
				return -EINVAL;
			link_sink = link;
		}
	}

	*linkp = link_sink;
	return 0;
}

static int xlnx_video_scan_component(struct xlnx_video_component *xvcomp)
{
	struct media_entity *entity;

	entity = xvcomp->output->entity;

	while (1) {
		const struct media_link *link;
		int ret;

		ret = xlnx_video_validate_entity(entity, &link);
		if (ret < 0)
			return ret;

		if (link == NULL) {
			const struct media_entity_desc *info;

			info = media_entity_get_info(entity);
			if (info->type == MEDIA_ENT_T_DEVNODE_V4L)
				xvcomp->input.entity = entity;
			break;
		}

		entity = link->source->entity;
	}

	return 0;
}

static int xlnx_video_scan_media(struct xlnx_video *xv, struct media_device *media)
{
}
#endif

#include <errno.h>
#include <stdlib.h>

#include <linux/media-bus-format.h>
#include <linux/videodev2.h>
#include <linux/v4l2-subdev.h>

#include <mediactl.h>

#include <xlnx-plugin.h>

static const struct xlnx_video_plugin_info v4l2_plugin_info = {
	.name = "V4L2 plugin",
	.type = XLNX_VIDEO_PLUGIN_TYPE_HARDWARE,
};

static const struct xlnx_video_plugin_info *v4l2_info(void)
{
	return &v4l2_plugin_info;
}

/*
 * \brief Xilinx V4L2 video node
 */
struct v4l2_video_node {
	/* V4L2 device */
	struct v4l2_device *video;
#if 0
	/* Current video format */
	struct v4l2_mbus_framefmt format;
	struct xlnx_video_buffers_pool *pool;
	/* Number of buffers queued to the driver */
	unsigned int queued;
	/* Whether video capture is running on the device */
	bool running;
#endif
};

struct v4l2_component {
	struct xlnx_video_component *xvcomp;

	struct v4l2_video_node *input;
	struct v4l2_video_node *output;
};

static struct xlnx_video_component *v4l2_scan(struct xlnx_video *xv,
					      struct media_device *mdev)
{
	struct media_entity *entity;
	unsigned int num_entities;
	unsigned int i;

	num_entities = media_get_entities_count(mdev);
	if (num_entities == 0)
		return 0;

	for (i = 0; i < num_entities; ++i) {
		const struct media_entity_desc *info;
		const struct media_pad *pad;
		struct v4l2_component *xvcomp;
		int ret;

		entity = media_get_entity(mdev, i);
		info = media_entity_get_info(entity);

		if (info->type != MEDIA_ENT_T_DEVNODE_V4L ||
		    info->pads != 1)
			continue;

		pad = media_entity_get_pad(entity, 0);
		if (!(pad->flags & MEDIA_PAD_FL_SINK))
			continue;

		xvcomp = calloc(1, sizeof *xvcomp);
		if (!xvcomp)
			return -ENOMEM;

		list_init(&xvcomp->entities);
		xvcomp->xv = xv;
		xvcomp->output.entity = entity;

		ret = xlnx_video_scan_component(xvcomp);
		if (ret < 0) {
			free(xvcomp);
			return ret;
		}

		list_append(&xv->components, &xvcomp->list);
		xv->num_components++;
	}

	return 0;
}

EXPORT_XLNX_VIDEO_PLUGIN(v4l2) = {
	.info = v4l2_info,
	.scan = v4l2_scan,
};