Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/camera/buffer_decoder.cpp
10277 views
1
/**************************************************************************/
2
/* buffer_decoder.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "buffer_decoder.h"
32
33
#include "servers/camera/camera_feed.h"
34
35
#include <linux/videodev2.h>
36
37
BufferDecoder::BufferDecoder(CameraFeed *p_camera_feed) {
38
camera_feed = p_camera_feed;
39
width = camera_feed->get_format().width;
40
height = camera_feed->get_format().height;
41
image.instantiate();
42
}
43
44
AbstractYuyvBufferDecoder::AbstractYuyvBufferDecoder(CameraFeed *p_camera_feed) :
45
BufferDecoder(p_camera_feed) {
46
switch (camera_feed->get_format().pixel_format) {
47
case V4L2_PIX_FMT_YYUV:
48
component_indexes = new int[4]{ 0, 1, 2, 3 };
49
break;
50
case V4L2_PIX_FMT_YVYU:
51
component_indexes = new int[4]{ 0, 2, 3, 1 };
52
break;
53
case V4L2_PIX_FMT_UYVY:
54
component_indexes = new int[4]{ 1, 3, 0, 2 };
55
break;
56
case V4L2_PIX_FMT_VYUY:
57
component_indexes = new int[4]{ 1, 3, 2, 0 };
58
break;
59
default:
60
component_indexes = new int[4]{ 0, 2, 1, 3 };
61
}
62
}
63
64
AbstractYuyvBufferDecoder::~AbstractYuyvBufferDecoder() {
65
delete[] component_indexes;
66
}
67
68
SeparateYuyvBufferDecoder::SeparateYuyvBufferDecoder(CameraFeed *p_camera_feed) :
69
AbstractYuyvBufferDecoder(p_camera_feed) {
70
y_image_data.resize(width * height);
71
cbcr_image_data.resize(width * height);
72
y_image.instantiate();
73
cbcr_image.instantiate();
74
}
75
76
void SeparateYuyvBufferDecoder::decode(StreamingBuffer p_buffer) {
77
uint8_t *y_dst = (uint8_t *)y_image_data.ptrw();
78
uint8_t *uv_dst = (uint8_t *)cbcr_image_data.ptrw();
79
uint8_t *src = (uint8_t *)p_buffer.start;
80
uint8_t *y0_src = src + component_indexes[0];
81
uint8_t *y1_src = src + component_indexes[1];
82
uint8_t *u_src = src + component_indexes[2];
83
uint8_t *v_src = src + component_indexes[3];
84
85
for (int i = 0; i < width * height; i += 2) {
86
*y_dst++ = *y0_src;
87
*y_dst++ = *y1_src;
88
*uv_dst++ = *u_src;
89
*uv_dst++ = *v_src;
90
91
y0_src += 4;
92
y1_src += 4;
93
u_src += 4;
94
v_src += 4;
95
}
96
97
if (y_image.is_valid()) {
98
y_image->set_data(width, height, false, Image::FORMAT_L8, y_image_data);
99
} else {
100
y_image.instantiate(width, height, false, Image::FORMAT_RGB8, y_image_data);
101
}
102
if (cbcr_image.is_valid()) {
103
cbcr_image->set_data(width, height, false, Image::FORMAT_L8, cbcr_image_data);
104
} else {
105
cbcr_image.instantiate(width, height, false, Image::FORMAT_RGB8, cbcr_image_data);
106
}
107
108
camera_feed->set_ycbcr_images(y_image, cbcr_image);
109
}
110
111
YuyvToGrayscaleBufferDecoder::YuyvToGrayscaleBufferDecoder(CameraFeed *p_camera_feed) :
112
AbstractYuyvBufferDecoder(p_camera_feed) {
113
image_data.resize(width * height);
114
}
115
116
void YuyvToGrayscaleBufferDecoder::decode(StreamingBuffer p_buffer) {
117
uint8_t *dst = (uint8_t *)image_data.ptrw();
118
uint8_t *src = (uint8_t *)p_buffer.start;
119
uint8_t *y0_src = src + component_indexes[0];
120
uint8_t *y1_src = src + component_indexes[1];
121
122
for (int i = 0; i < width * height; i += 2) {
123
*dst++ = *y0_src;
124
*dst++ = *y1_src;
125
126
y0_src += 4;
127
y1_src += 4;
128
}
129
130
if (image.is_valid()) {
131
image->set_data(width, height, false, Image::FORMAT_L8, image_data);
132
} else {
133
image.instantiate(width, height, false, Image::FORMAT_RGB8, image_data);
134
}
135
136
camera_feed->set_rgb_image(image);
137
}
138
139
YuyvToRgbBufferDecoder::YuyvToRgbBufferDecoder(CameraFeed *p_camera_feed) :
140
AbstractYuyvBufferDecoder(p_camera_feed) {
141
image_data.resize(width * height * 3);
142
}
143
144
void YuyvToRgbBufferDecoder::decode(StreamingBuffer p_buffer) {
145
uint8_t *src = (uint8_t *)p_buffer.start;
146
uint8_t *y0_src = src + component_indexes[0];
147
uint8_t *y1_src = src + component_indexes[1];
148
uint8_t *u_src = src + component_indexes[2];
149
uint8_t *v_src = src + component_indexes[3];
150
uint8_t *dst = (uint8_t *)image_data.ptrw();
151
152
for (int i = 0; i < width * height; i += 2) {
153
int u = *u_src;
154
int v = *v_src;
155
int u1 = (((u - 128) << 7) + (u - 128)) >> 6;
156
int rg = (((u - 128) << 1) + (u - 128) + ((v - 128) << 2) + ((v - 128) << 1)) >> 3;
157
int v1 = (((v - 128) << 1) + (v - 128)) >> 1;
158
159
*dst++ = CLAMP(*y0_src + v1, 0, 255);
160
*dst++ = CLAMP(*y0_src - rg, 0, 255);
161
*dst++ = CLAMP(*y0_src + u1, 0, 255);
162
163
*dst++ = CLAMP(*y1_src + v1, 0, 255);
164
*dst++ = CLAMP(*y1_src - rg, 0, 255);
165
*dst++ = CLAMP(*y1_src + u1, 0, 255);
166
167
y0_src += 4;
168
y1_src += 4;
169
u_src += 4;
170
v_src += 4;
171
}
172
173
if (image.is_valid()) {
174
image->set_data(width, height, false, Image::FORMAT_RGB8, image_data);
175
} else {
176
image.instantiate(width, height, false, Image::FORMAT_RGB8, image_data);
177
}
178
179
camera_feed->set_rgb_image(image);
180
}
181
182
CopyBufferDecoder::CopyBufferDecoder(CameraFeed *p_camera_feed, bool p_rgba) :
183
BufferDecoder(p_camera_feed) {
184
rgba = p_rgba;
185
image_data.resize(width * height * (rgba ? 4 : 2));
186
}
187
188
void CopyBufferDecoder::decode(StreamingBuffer p_buffer) {
189
uint8_t *dst = (uint8_t *)image_data.ptrw();
190
memcpy(dst, p_buffer.start, p_buffer.length);
191
192
if (image.is_valid()) {
193
image->set_data(width, height, false, rgba ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8, image_data);
194
} else {
195
image.instantiate(width, height, false, rgba ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8, image_data);
196
}
197
198
camera_feed->set_rgb_image(image);
199
}
200
201
JpegBufferDecoder::JpegBufferDecoder(CameraFeed *p_camera_feed) :
202
BufferDecoder(p_camera_feed) {
203
}
204
205
void JpegBufferDecoder::decode(StreamingBuffer p_buffer) {
206
image_data.resize(p_buffer.length);
207
uint8_t *dst = (uint8_t *)image_data.ptrw();
208
memcpy(dst, p_buffer.start, p_buffer.length);
209
if (image->load_jpg_from_buffer(image_data) == OK) {
210
camera_feed->set_rgb_image(image);
211
}
212
}
213
214