-
Notifications
You must be signed in to change notification settings - Fork 123
/
ARVideoView.cpp
315 lines (283 loc) · 9.87 KB
/
ARVideoView.cpp
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
/*
* ARVideoView.cpp
* artoolkitX
*
* This file is part of artoolkitX.
*
* artoolkitX 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 3 of the License, or
* (at your option) any later version.
*
* artoolkitX 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with artoolkitX. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent modules, and to
* copy and distribute the resulting executable under terms of your choice,
* provided that you also meet, for each linked independent module, the terms and
* conditions of the license of that module. An independent module is a module
* which is neither derived from nor based on this library. If you modify this
* library, you may extend this exception to your version of the library, but you
* are not obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* Copyright 2018 Realmax, Inc.
*
* Author(s): Philip Lamb.
*
*/
#include <ARX/ARVideoView.h>
#if USE_GL_STATE_CACHE
# include <ARX/ARG/glStateCache2.h>
#endif
#include <algorithm>
ARVideoView::ARVideoView() :
m_arglContextSettings(NULL),
m_drawTypeSet(DrawType::None),
m_drawTime({0, 0}),
m_contentWidth(0),
m_contentHeight(0),
m_contextWidth(0),
m_contextHeight(0),
m_viewport{0},
m_hAlign(HorizontalAlignment::H_ALIGN_CENTRE),
m_vAlign(VerticalAlignment::V_ALIGN_CENTRE),
m_scalingMode(ScalingMode::SCALE_MODE_FIT),
m_distortionCompensation(true),
m_rotate90(false),
m_flipH(false),
m_flipV(false)
{
}
bool ARVideoView::initWithVideoSource(const ARVideoSource& vs, const int contextWidth, const int contextHeight)
{
ARLOGd("ARVideoView::initWithVideoSource.\n");
// If glStateCache is in use, need to flush it here, since we can't guarantee external users will have been using it.
#if USE_GL_STATE_CACHE
glStateCacheFlush();
#endif
// Allow re-initialization.
if (m_arglContextSettings) {
arglCleanup(m_arglContextSettings);
}
ARParamLT *paramLT = vs.getCameraParameters();
if (paramLT) {
m_arglContextSettings = arglSetupForCurrentContext(&(paramLT->param), vs.getPixelFormat());
} else {
ARParam param;
arParamClear(¶m, vs.getVideoWidth(), vs.getVideoHeight(), AR_DIST_FUNCTION_VERSION_DEFAULT);
m_arglContextSettings = arglSetupForCurrentContext(¶m, vs.getPixelFormat());
m_distortionCompensation = false;
}
if (!m_arglContextSettings) {
ARLOGe("Error initializing display of video frames from source.\n");
return false;
}
m_drawTypeSet = DrawType::Main;
m_contentWidth = vs.getVideoWidth();
m_contentHeight = vs.getVideoHeight();
m_contextWidth = contextWidth;
m_contextHeight = contextHeight;
update();
return true;
}
void ARVideoView::update()
{
if (m_arglContextSettings) {
arglSetRotate90(m_arglContextSettings, m_rotate90 ? 1 : 0);
arglSetFlipH(m_arglContextSettings, m_flipH ? 1 : 0);
arglSetFlipV(m_arglContextSettings, m_flipV ? 1 : 0);
arglDistortionCompensationSet(m_arglContextSettings, m_distortionCompensation ? 1 : 0);
}
// Calculate viewPort.
int left, bottom, w, h;
if (m_scalingMode == ScalingMode::SCALE_MODE_STRETCH) {
w = m_contextWidth;
h = m_contextHeight;
} else {
int contentWidthFinalOrientation = (m_rotate90 ? m_contentHeight : m_contentWidth);
int contentHeightFinalOrientation = (m_rotate90 ? m_contentWidth : m_contentHeight);
if (m_scalingMode == ScalingMode::SCALE_MODE_FIT || m_scalingMode == ScalingMode::SCALE_MODE_FILL) {
float scaleRatioWidth, scaleRatioHeight, scaleRatio;
scaleRatioWidth = (float)m_contextWidth / (float)contentWidthFinalOrientation;
scaleRatioHeight = (float)m_contextHeight / (float)contentHeightFinalOrientation;
if (m_scalingMode == ScalingMode::SCALE_MODE_FILL) scaleRatio = (std::max)(scaleRatioHeight, scaleRatioWidth);
else scaleRatio = (std::min)(scaleRatioHeight, scaleRatioWidth);
w = (int)((float)contentWidthFinalOrientation * scaleRatio);
h = (int)((float)contentHeightFinalOrientation * scaleRatio);
} else {
w = contentWidthFinalOrientation;
h = contentHeightFinalOrientation;
}
}
if (m_hAlign == HorizontalAlignment::H_ALIGN_LEFT) left = 0;
else if (m_hAlign == HorizontalAlignment::H_ALIGN_RIGHT) left = m_contextWidth - w;
else left = (m_contextWidth - w) / 2;
if (m_vAlign == VerticalAlignment::V_ALIGN_BOTTOM) bottom = 0;
else if (m_vAlign == VerticalAlignment::V_ALIGN_TOP) bottom = m_contextHeight - h;
else bottom = (m_contextHeight - h) / 2;
m_viewport[0] = left;
m_viewport[1] = bottom;
m_viewport[2] = w;
m_viewport[3] = h;
ARLOGd("ARVideoView::update() calculated viewport {%d, %d, %d, %d}\n", left, bottom, w, h);
}
bool ARVideoView::distortionCompensation() const
{
return m_distortionCompensation;
}
void ARVideoView::setDistortionCompensation(const bool distortionCompensation)
{
if (m_distortionCompensation != distortionCompensation) {
m_distortionCompensation = distortionCompensation;
update();
}
}
bool ARVideoView::rotate90() const
{
return m_rotate90;
}
void ARVideoView::setRotate90(const bool rotate90)
{
if (m_rotate90 != rotate90) {
m_rotate90 = rotate90;
update();
}
}
bool ARVideoView::flipH() const
{
return m_flipH;
}
void ARVideoView::setFlipH(const bool flipH)
{
if (m_flipH != flipH) {
m_flipH = flipH;
update();
}
}
bool ARVideoView::flipV() const
{
return m_flipV;
}
void ARVideoView::setFlipV(const bool flipV)
{
if (m_flipV != flipV) {
m_flipV = flipV;
update();
}
}
ARVideoView::HorizontalAlignment ARVideoView::horizontalAlignment() const
{
return m_hAlign;
}
void ARVideoView::setHorizontalAlignment(const HorizontalAlignment hAlign)
{
if (m_hAlign != hAlign) {
m_hAlign = hAlign;
update();
}
}
ARVideoView::VerticalAlignment ARVideoView::verticalAlignment() const
{
return m_vAlign;
}
void ARVideoView::setVerticalAlignment(const VerticalAlignment vAlign)
{
if (m_vAlign != vAlign) {
m_vAlign = vAlign;
update();
}
}
ARVideoView::ScalingMode ARVideoView::scalingMode() const
{
return m_scalingMode;
}
void ARVideoView::setScalingMode(const ScalingMode scalingMode)
{
if (m_scalingMode != scalingMode) {
m_scalingMode = scalingMode;
update();
}
}
ARVideoView::Size ARVideoView::contextSize() const
{
return {m_contextWidth, m_contextHeight};
}
void ARVideoView::setContextSize(const Size size)
{
if (m_contextWidth != size.width || m_contextHeight != size.height) {
m_contextWidth = size.width;
m_contextHeight = size.height;
update();
}
}
void ARVideoView::getViewport(int32_t viewport[4]) const
{
if (!viewport) return;
viewport[0] = m_viewport[0];
viewport[1] = m_viewport[1];
viewport[2] = m_viewport[2];
viewport[3] = m_viewport[3];
}
void ARVideoView::draw(ARVideoSource* vs)
{
// If glStateCache is in use, need to flush it here, since we can't guarantee external users will have been using it.
#if USE_GL_STATE_CACHE
glStateCacheFlush();
#endif
AR2VideoBufferT *frame = vs->checkoutFrameIfNewerThan(m_drawTime);
if (frame) {
m_drawTime = frame->time;
if (m_drawTypeSet != DrawType::Main) {
arglPixelFormatSet(m_arglContextSettings, vs->getPixelFormat());
arglPixelBufferSizeSet(m_arglContextSettings, m_contentWidth, m_contentHeight);
arglSetPixelZoom(m_arglContextSettings, 1.0f);
m_drawTypeSet = DrawType::Main;
}
if (frame->bufPlaneCount == 2) {
if (!arglPixelBufferDataUploadBiPlanar(m_arglContextSettings, frame->bufPlanes[0], frame->bufPlanes[1])) {
ARLOGe("arglPixelBufferDataUploadBiPlanar.\n");
}
} else {
if (!arglPixelBufferDataUpload(m_arglContextSettings, frame->buff)) {
ARLOGe("arglPixelBufferDataUpload.\n");
}
}
vs->checkinFrame();
} else {
ARLOGd("ARVideoView::draw frame=NULL.\n");
}
arglDispImage(m_arglContextSettings, m_viewport);
}
void ARVideoView::drawDebugImage(const uint8_t *image, int pyramidLevel)
{
// If glStateCache is in use, need to flush it here, since we can't guarantee external users will have been using it.
#if USE_GL_STATE_CACHE
glStateCacheFlush();
#endif
if (m_drawTypeSet != DrawType::Debug) {
arglPixelFormatSet(m_arglContextSettings, AR_PIXEL_FORMAT_MONO);
arglPixelBufferSizeSet(m_arglContextSettings, m_contentWidth >> pyramidLevel, m_contentHeight >> pyramidLevel);
arglSetPixelZoom(m_arglContextSettings, (float)(1 << pyramidLevel));
m_drawTypeSet = DrawType::Debug;
}
if (image) {
if (!arglPixelBufferDataUploadBiPlanar(m_arglContextSettings, (ARUint8 *)image, NULL)) {
ARLOGe("arglPixelBufferDataUploadBiPlanar.\n");
}
} else {
ARLOGd("ARVideoView::drawDebugImage image=NULL.\n");
}
arglDispImage(m_arglContextSettings, m_viewport);
}
ARVideoView::~ARVideoView()
{
arglCleanup(m_arglContextSettings);
}