File: common.h

package info (click to toggle)
v4l2loopback 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 664 kB
  • sloc: ansic: 6,550; sh: 301; makefile: 200
file content (154 lines) | stat: -rw-r--r-- 4,001 bytes parent folder | download | duplicates (3)
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
/* -*- c-file-style: "linux" -*- */
/*
 * common.h  --  some commong functions
 *
 * Copyright (C) 2023 IOhannes m zmoelnig (zmoelnig@iem.at)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

#include <stdio.h>
#include <linux/videodev2.h>

static char *fourcc2str(unsigned int fourcc, char buf[4])
{
	buf[0] = (fourcc >> 0) & 0xFF;
	buf[1] = (fourcc >> 8) & 0xFF;
	buf[2] = (fourcc >> 16) & 0xFF;
	buf[3] = (fourcc >> 24) & 0xFF;

	return buf;
}
static const char *field2str(unsigned int field)
{
	switch (field) {
	case V4L2_FIELD_ANY:
		return "any";
	case V4L2_FIELD_NONE:
		return "none";
	case V4L2_FIELD_TOP:
		return "top";
	case V4L2_FIELD_BOTTOM:
		return "bottom";
	case V4L2_FIELD_INTERLACED:
		return "interlaced";
	case V4L2_FIELD_SEQ_TB:
		return "seq/topbottom";
	case V4L2_FIELD_SEQ_BT:
		return "seq/bottomtop";
	case V4L2_FIELD_ALTERNATE:
		return "alternate";
	case V4L2_FIELD_INTERLACED_TB:
		return "interlaced/topbottom";
	case V4L2_FIELD_INTERLACED_BT:
		return "interlaced/bottomtop";

	default:
		break;
	}
	return "unknown";
}

static const char *buftype2str(unsigned int type)
{
	switch (type) {
	default:
		break;
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		return "CAPTURE";
	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
		return "CAPTURE(planar)";
	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
		return "OUTPUT";
	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
		return "OUTPUT(planar)";
	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
		return "OUTPUT(overlay)";
	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
		return "OVERLAY";
	case V4L2_BUF_TYPE_VBI_CAPTURE:
		return "VBI(capture)";
	case V4L2_BUF_TYPE_VBI_OUTPUT:
		return "VBI(output)";
	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
		return "SlicedVBI(capture)";
	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
		return "SlicedVBI(output)";
	case V4L2_BUF_TYPE_SDR_CAPTURE:
		return "SDR(capture)";
	case V4L2_BUF_TYPE_SDR_OUTPUT:
		return "SDR(output)";
	case V4L2_BUF_TYPE_META_CAPTURE:
		return "META(capture)";
	case V4L2_BUF_TYPE_META_OUTPUT:
		return "META(output)";
	case V4L2_BUF_TYPE_PRIVATE:
		return "private";
	}
	return "unknown";
}

static const char *bufmemory2str(unsigned int mem)
{
	switch (mem) {
	case V4L2_MEMORY_MMAP:
		return "MMAP";
	case V4L2_MEMORY_USERPTR:
		return "USERPTR";
	case V4L2_MEMORY_OVERLAY:
		return "OVERLAY";
	case V4L2_MEMORY_DMABUF:
		return "DMABUF";
	default:
		break;
	}
	return "unknown";
}

static const char *snprintf_format(char *buf, size_t size,
				   struct v4l2_format *fmt)
{
	char fourcc[5];
	fourcc[4] = 0;
	switch (fmt->type) {
	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		snprintf(buf, size,
			 "%s:%dx%d:%s bytes/line=%u sizeimage=%u field=%s",
			 buftype2str(fmt->type), fmt->fmt.pix.width,
			 fmt->fmt.pix.height,
			 fourcc2str(fmt->fmt.pix.pixelformat, fourcc),
			 fmt->fmt.pix.bytesperline, fmt->fmt.pix.sizeimage,
			 field2str(fmt->fmt.pix.field));
		break;
	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
		snprintf(buf, size, "%s:%dx%d:%s (%d planes) field=%s",
			 buftype2str(fmt->type), fmt->fmt.pix_mp.width,
			 fmt->fmt.pix_mp.height,
			 fourcc2str(fmt->fmt.pix_mp.pixelformat, fourcc),
			 fmt->fmt.pix_mp.num_planes,
			 field2str(fmt->fmt.pix_mp.field));
	default:
		snprintf(buf, size, "TODO: %s(type=%d)", __FUNCTION__,
			 (fmt->type));
	}
	return buf;
}

static const char *snprintf_buffer(char *strbuf, size_t size,
				   struct v4l2_buffer *buf)
{
	snprintf(
		strbuf, size,
		"buffer#%d @ %p %s bytesused=%d, length=%d flags=0x%08X field=%s timestamp=%ld.%06ld memory=%s (offset=%d)",
		buf->index, buf, buftype2str(buf->type), buf->bytesused,
		buf->length, buf->flags, field2str(buf->field),
		buf->timestamp.tv_sec, buf->timestamp.tv_usec,
		bufmemory2str(buf->memory), buf->m.offset);
	return strbuf;
}