| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* The YUY2 shader code is based on face-responder. The code is under public domain: |
| 4 |
* https://bitbucket.org/nateharward/face-responder/src/0c3b4b957039d9f4bf1da09b9471371942de2601/yuv42201_laplace.frag?at=master |
| 5 |
* |
| 6 |
* All other OpenGL code: |
| 7 |
* |
| 8 |
* Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "capture.h" |
| 12 |
|
| 13 |
#include <QtCore/QTextStream> |
| 14 |
#include <QtCore/QCoreApplication> |
| 15 |
#include <QtGui/QOpenGLContext> |
| 16 |
#if QT_VERSION < 0x060000 |
| 17 |
#include <QtGui/QOpenGLPaintDevice> |
| 18 |
#else |
| 19 |
#include <QOpenGLPaintDevice> |
| 20 |
#endif |
| 21 |
#include <QtGui/QContextMenuEvent> |
| 22 |
#include <QtGui/QKeyEvent> |
| 23 |
#include <QtGui/QPainter> |
| 24 |
#include <QtCore/QSocketNotifier> |
| 25 |
#include <QtMath> |
| 26 |
#include <QTimer> |
| 27 |
#include <QApplication> |
| 28 |
|
| 29 |
#include <netinet/in.h> |
| 30 |
#include "v4l2-info.h" |
| 31 |
|
| 32 |
const __u32 formats[] = { |
| 33 |
V4L2_PIX_FMT_YUYV, |
| 34 |
V4L2_PIX_FMT_YVYU, |
| 35 |
V4L2_PIX_FMT_UYVY, |
| 36 |
V4L2_PIX_FMT_VYUY, |
| 37 |
V4L2_PIX_FMT_YUV422P, |
| 38 |
V4L2_PIX_FMT_YVU420, |
| 39 |
V4L2_PIX_FMT_YUV420, |
| 40 |
V4L2_PIX_FMT_NV12, |
| 41 |
V4L2_PIX_FMT_NV21, |
| 42 |
V4L2_PIX_FMT_NV16, |
| 43 |
V4L2_PIX_FMT_NV61, |
| 44 |
V4L2_PIX_FMT_NV24, |
| 45 |
V4L2_PIX_FMT_NV42, |
| 46 |
V4L2_PIX_FMT_NV16M, |
| 47 |
V4L2_PIX_FMT_NV61M, |
| 48 |
V4L2_PIX_FMT_YVU420M, |
| 49 |
V4L2_PIX_FMT_YUV420M, |
| 50 |
V4L2_PIX_FMT_YVU422M, |
| 51 |
V4L2_PIX_FMT_YUV422M, |
| 52 |
V4L2_PIX_FMT_YVU444M, |
| 53 |
V4L2_PIX_FMT_YUV444M, |
| 54 |
V4L2_PIX_FMT_NV12M, |
| 55 |
V4L2_PIX_FMT_NV21M, |
| 56 |
V4L2_PIX_FMT_YUV444, |
| 57 |
V4L2_PIX_FMT_YUV555, |
| 58 |
V4L2_PIX_FMT_YUV565, |
| 59 |
V4L2_PIX_FMT_YUV32, |
| 60 |
V4L2_PIX_FMT_AYUV32, |
| 61 |
V4L2_PIX_FMT_XYUV32, |
| 62 |
V4L2_PIX_FMT_VUYA32, |
| 63 |
V4L2_PIX_FMT_VUYX32, |
| 64 |
V4L2_PIX_FMT_YUVA32, |
| 65 |
V4L2_PIX_FMT_YUVX32, |
| 66 |
V4L2_PIX_FMT_RGB32, |
| 67 |
V4L2_PIX_FMT_XRGB32, |
| 68 |
V4L2_PIX_FMT_ARGB32, |
| 69 |
V4L2_PIX_FMT_RGBX32, |
| 70 |
V4L2_PIX_FMT_RGBA32, |
| 71 |
V4L2_PIX_FMT_BGR32, |
| 72 |
V4L2_PIX_FMT_XBGR32, |
| 73 |
V4L2_PIX_FMT_ABGR32, |
| 74 |
V4L2_PIX_FMT_BGRX32, |
| 75 |
V4L2_PIX_FMT_BGRA32, |
| 76 |
V4L2_PIX_FMT_RGB24, |
| 77 |
V4L2_PIX_FMT_BGR24, |
| 78 |
V4L2_PIX_FMT_RGB565, |
| 79 |
V4L2_PIX_FMT_RGB565X, |
| 80 |
V4L2_PIX_FMT_RGB444, |
| 81 |
V4L2_PIX_FMT_XRGB444, |
| 82 |
V4L2_PIX_FMT_ARGB444, |
| 83 |
V4L2_PIX_FMT_XBGR444, |
| 84 |
V4L2_PIX_FMT_ABGR444, |
| 85 |
V4L2_PIX_FMT_RGBX444, |
| 86 |
V4L2_PIX_FMT_RGBA444, |
| 87 |
V4L2_PIX_FMT_BGRX444, |
| 88 |
V4L2_PIX_FMT_BGRA444, |
| 89 |
V4L2_PIX_FMT_RGB555, |
| 90 |
V4L2_PIX_FMT_XRGB555, |
| 91 |
V4L2_PIX_FMT_ARGB555, |
| 92 |
V4L2_PIX_FMT_RGB555X, |
| 93 |
V4L2_PIX_FMT_XRGB555X, |
| 94 |
V4L2_PIX_FMT_ARGB555X, |
| 95 |
V4L2_PIX_FMT_RGBX555, |
| 96 |
V4L2_PIX_FMT_RGBA555, |
| 97 |
V4L2_PIX_FMT_XBGR555, |
| 98 |
V4L2_PIX_FMT_ABGR555, |
| 99 |
V4L2_PIX_FMT_BGRX555, |
| 100 |
V4L2_PIX_FMT_BGRA555, |
| 101 |
V4L2_PIX_FMT_RGB332, |
| 102 |
V4L2_PIX_FMT_BGR666, |
| 103 |
V4L2_PIX_FMT_SBGGR8, |
| 104 |
V4L2_PIX_FMT_SGBRG8, |
| 105 |
V4L2_PIX_FMT_SGRBG8, |
| 106 |
V4L2_PIX_FMT_SRGGB8, |
| 107 |
V4L2_PIX_FMT_SBGGR10, |
| 108 |
V4L2_PIX_FMT_SGBRG10, |
| 109 |
V4L2_PIX_FMT_SGRBG10, |
| 110 |
V4L2_PIX_FMT_SRGGB10, |
| 111 |
V4L2_PIX_FMT_SBGGR12, |
| 112 |
V4L2_PIX_FMT_SGBRG12, |
| 113 |
V4L2_PIX_FMT_SGRBG12, |
| 114 |
V4L2_PIX_FMT_SRGGB12, |
| 115 |
V4L2_PIX_FMT_SBGGR16, |
| 116 |
V4L2_PIX_FMT_SGBRG16, |
| 117 |
V4L2_PIX_FMT_SGRBG16, |
| 118 |
V4L2_PIX_FMT_SRGGB16, |
| 119 |
V4L2_PIX_FMT_HSV24, |
| 120 |
V4L2_PIX_FMT_HSV32, |
| 121 |
V4L2_PIX_FMT_GREY, |
| 122 |
V4L2_PIX_FMT_Y10, |
| 123 |
V4L2_PIX_FMT_Y12, |
| 124 |
V4L2_PIX_FMT_Y16, |
| 125 |
V4L2_PIX_FMT_Y16_BE, |
| 126 |
V4L2_PIX_FMT_Z16, |
| 127 |
0 |
| 128 |
}; |
| 129 |
|
| 130 |
const __u32 fields[] = { |
| 131 |
V4L2_FIELD_NONE, |
| 132 |
V4L2_FIELD_TOP, |
| 133 |
V4L2_FIELD_BOTTOM, |
| 134 |
V4L2_FIELD_INTERLACED, |
| 135 |
V4L2_FIELD_SEQ_TB, |
| 136 |
V4L2_FIELD_SEQ_BT, |
| 137 |
V4L2_FIELD_ALTERNATE, |
| 138 |
V4L2_FIELD_INTERLACED_TB, |
| 139 |
V4L2_FIELD_INTERLACED_BT, |
| 140 |
0 |
| 141 |
}; |
| 142 |
|
| 143 |
const __u32 colorspaces[] = { |
| 144 |
V4L2_COLORSPACE_SMPTE170M, |
| 145 |
V4L2_COLORSPACE_SMPTE240M, |
| 146 |
V4L2_COLORSPACE_REC709, |
| 147 |
V4L2_COLORSPACE_470_SYSTEM_M, |
| 148 |
V4L2_COLORSPACE_470_SYSTEM_BG, |
| 149 |
V4L2_COLORSPACE_SRGB, |
| 150 |
V4L2_COLORSPACE_OPRGB, |
| 151 |
V4L2_COLORSPACE_DCI_P3, |
| 152 |
V4L2_COLORSPACE_BT2020, |
| 153 |
0 |
| 154 |
}; |
| 155 |
|
| 156 |
const __u32 xfer_funcs[] = { |
| 157 |
V4L2_XFER_FUNC_709, |
| 158 |
V4L2_XFER_FUNC_SRGB, |
| 159 |
V4L2_XFER_FUNC_OPRGB, |
| 160 |
V4L2_XFER_FUNC_DCI_P3, |
| 161 |
V4L2_XFER_FUNC_SMPTE2084, |
| 162 |
V4L2_XFER_FUNC_SMPTE240M, |
| 163 |
V4L2_XFER_FUNC_NONE, |
| 164 |
0 |
| 165 |
}; |
| 166 |
|
| 167 |
const __u32 ycbcr_encs[] = { |
| 168 |
V4L2_YCBCR_ENC_601, |
| 169 |
V4L2_YCBCR_ENC_709, |
| 170 |
V4L2_YCBCR_ENC_XV601, |
| 171 |
V4L2_YCBCR_ENC_XV709, |
| 172 |
V4L2_YCBCR_ENC_BT2020, |
| 173 |
V4L2_YCBCR_ENC_BT2020_CONST_LUM, |
| 174 |
V4L2_YCBCR_ENC_SMPTE240M, |
| 175 |
0 |
| 176 |
}; |
| 177 |
|
| 178 |
const __u32 hsv_encs[] = { |
| 179 |
V4L2_HSV_ENC_180, |
| 180 |
V4L2_HSV_ENC_256, |
| 181 |
0 |
| 182 |
}; |
| 183 |
|
| 184 |
const __u32 quantizations[] = { |
| 185 |
V4L2_QUANTIZATION_FULL_RANGE, |
| 186 |
V4L2_QUANTIZATION_LIM_RANGE, |
| 187 |
0 |
| 188 |
}; |
| 189 |
|
| 190 |
enum { |
| 191 |
CAPTURE_GL_WIN_RESIZE, |
| 192 |
CAPTURE_GL_WIN_SCROLLBAR, |
| 193 |
}; |
| 194 |
|
| 195 |
static void checkSubMenuItem(QMenu *menu, __u32 value) |
| 196 |
{ |
| 197 |
for (auto &action : menu->actions()) { |
| 198 |
if (action->data() == value) { |
| 199 |
action->setChecked(true); |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
static QAction *addSubMenuItem(QActionGroup *grp, QMenu *menu, const QString &text, int val) |
| 206 |
{ |
| 207 |
QAction *a = grp->addAction(menu->addAction(text)); |
| 208 |
|
| 209 |
a->setData(QVariant(val)); |
| 210 |
a->setCheckable(true); |
| 211 |
return a; |
| 212 |
} |
| 213 |
|
| 214 |
CaptureWin::CaptureWin(QScrollArea *sa, QWidget *parent) : |
| 215 |
QOpenGLWidget(parent), |
| 216 |
m_fd(0), |
| 217 |
m_sock(0), |
| 218 |
m_v4l_queue(0), |
| 219 |
m_frame(0), |
| 220 |
m_verbose(false), |
| 221 |
m_no_loop(false), |
| 222 |
m_pause(false), |
| 223 |
m_fromFrame(0), |
| 224 |
m_ctx(0), |
| 225 |
m_origPixelFormat(0), |
| 226 |
m_fps(0), |
| 227 |
m_singleStep(false), |
| 228 |
m_singleStepStart(0), |
| 229 |
m_singleStepNext(false), |
| 230 |
m_screenTextureCount(0), |
| 231 |
m_program(0), |
| 232 |
m_curIndex(-1), |
| 233 |
m_nextIndex(-1), |
| 234 |
m_scrollArea(sa) |
| 235 |
{ |
| 236 |
m_curSize[0] = 0; |
| 237 |
m_curData[0] = 0; |
| 238 |
m_canOverrideResolution = false; |
| 239 |
m_pixelaspect.numerator = 1; |
| 240 |
m_pixelaspect.denominator = 1; |
| 241 |
QMenu *menu = new QMenu("Override Pixel Format (P)"); |
| 242 |
m_fmtMenu = menu; |
| 243 |
QActionGroup *grp = new QActionGroup(menu); |
| 244 |
addSubMenuItem(grp, menu, "No Override", 0)->setChecked(true); |
| 245 |
for (unsigned i = 0; formats[i]; i++) { |
| 246 |
std::string fmt = "'" + fcc2s(formats[i]) + "' " + pixfmt2s(formats[i]); |
| 247 |
|
| 248 |
addSubMenuItem(grp, menu, fmt.c_str(), formats[i]); |
| 249 |
} |
| 250 |
connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(fmtChanged(QAction *))); |
| 251 |
|
| 252 |
menu = new QMenu("Override Field (I)"); |
| 253 |
m_fieldMenu = menu; |
| 254 |
grp = new QActionGroup(menu); |
| 255 |
addSubMenuItem(grp, menu, "No Override", -1)->setChecked(true); |
| 256 |
for (unsigned i = 0; fields[i]; i++) |
| 257 |
addSubMenuItem(grp, menu, |
| 258 |
field2s(fields[i]).c_str(), fields[i]); |
| 259 |
connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(fieldChanged(QAction *))); |
| 260 |
|
| 261 |
menu = new QMenu("Override Colorspace (C)"); |
| 262 |
m_colorspaceMenu = menu; |
| 263 |
grp = new QActionGroup(menu); |
| 264 |
addSubMenuItem(grp, menu, "No Override", -1)->setChecked(true); |
| 265 |
for (unsigned i = 0; colorspaces[i]; i++) |
| 266 |
addSubMenuItem(grp, menu, |
| 267 |
colorspace2s(colorspaces[i]).c_str(), colorspaces[i]); |
| 268 |
connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(colorspaceChanged(QAction *))); |
| 269 |
|
| 270 |
menu = new QMenu("Override Transfer Function (X)"); |
| 271 |
m_xferFuncMenu = menu; |
| 272 |
grp = new QActionGroup(menu); |
| 273 |
addSubMenuItem(grp, menu, "No Override", -1)->setChecked(true); |
| 274 |
for (unsigned i = 0; xfer_funcs[i]; i++) |
| 275 |
addSubMenuItem(grp, menu, |
| 276 |
xfer_func2s(xfer_funcs[i]).c_str(), xfer_funcs[i]); |
| 277 |
connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(xferFuncChanged(QAction *))); |
| 278 |
|
| 279 |
menu = new QMenu("Override Y'CbCr Encoding (Y)"); |
| 280 |
m_ycbcrEncMenu = menu; |
| 281 |
grp = new QActionGroup(menu); |
| 282 |
addSubMenuItem(grp, menu, "No Override", -1)->setChecked(true); |
| 283 |
for (unsigned i = 0; ycbcr_encs[i]; i++) |
| 284 |
addSubMenuItem(grp, menu, |
| 285 |
ycbcr_enc2s(ycbcr_encs[i]).c_str(), ycbcr_encs[i]); |
| 286 |
connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(ycbcrEncChanged(QAction *))); |
| 287 |
|
| 288 |
menu = new QMenu("Override HSV Encoding (H)"); |
| 289 |
m_hsvEncMenu = menu; |
| 290 |
grp = new QActionGroup(menu); |
| 291 |
addSubMenuItem(grp, menu, "No Override", -1)->setChecked(true); |
| 292 |
for (unsigned i = 0; hsv_encs[i]; i++) |
| 293 |
addSubMenuItem(grp, menu, |
| 294 |
ycbcr_enc2s(hsv_encs[i]).c_str(), hsv_encs[i]); |
| 295 |
connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(hsvEncChanged(QAction *))); |
| 296 |
|
| 297 |
menu = new QMenu("Override Quantization (R)"); |
| 298 |
m_quantMenu = menu; |
| 299 |
grp = new QActionGroup(menu); |
| 300 |
addSubMenuItem(grp, menu, "No Override", -1)->setChecked(true); |
| 301 |
for (unsigned i = 0; quantizations[i]; i++) |
| 302 |
addSubMenuItem(grp, menu, |
| 303 |
quantization2s(quantizations[i]).c_str(), quantizations[i]); |
| 304 |
connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(quantChanged(QAction *))); |
| 305 |
|
| 306 |
menu = new QMenu("Display Options"); |
| 307 |
m_displayMenu = menu; |
| 308 |
grp = new QActionGroup(menu); |
| 309 |
addSubMenuItem(grp, menu, "Frame Resize", CAPTURE_GL_WIN_RESIZE)->setChecked(true); |
| 310 |
addSubMenuItem(grp, menu, "Window Scrollbars", CAPTURE_GL_WIN_SCROLLBAR)->setChecked(false); |
| 311 |
connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(windowScalingChanged(QAction *))); |
| 312 |
|
| 313 |
m_resolutionOverride = new QAction("Override resolution", this); |
| 314 |
m_resolutionOverride->setCheckable(true); |
| 315 |
connect(m_resolutionOverride, SIGNAL(triggered(bool)), |
| 316 |
this, SLOT(resolutionOverrideChanged(bool))); |
| 317 |
|
| 318 |
m_enterFullScreen = new QAction("Enter fullscreen (F)", this); |
| 319 |
connect(m_enterFullScreen, SIGNAL(triggered(bool)), |
| 320 |
this, SLOT(toggleFullScreen(bool))); |
| 321 |
|
| 322 |
m_exitFullScreen = new QAction("Exit fullscreen (F or Esc)", this); |
| 323 |
connect(m_exitFullScreen, SIGNAL(triggered(bool)), |
| 324 |
this, SLOT(toggleFullScreen(bool))); |
| 325 |
} |
| 326 |
|
| 327 |
CaptureWin::~CaptureWin() |
| 328 |
{ |
| 329 |
makeCurrent(); |
| 330 |
delete m_program; |
| 331 |
} |
| 332 |
|
| 333 |
void CaptureWin::resizeEvent(QResizeEvent *event) |
| 334 |
{ |
| 335 |
QSize origSize = correctAspect(QSize(m_origWidth, m_origHeight)); |
| 336 |
QSize window = size(); |
| 337 |
qreal scale; |
| 338 |
|
| 339 |
if ((qreal)window.width() / (qreal)origSize.width() > |
| 340 |
(qreal)window.height() / (qreal)origSize.height()) { |
| 341 |
// Horizontal scale factor > vert. scale factor |
| 342 |
scale = (qreal)window.height() / (qreal)origSize.height(); |
| 343 |
} else { |
| 344 |
scale = (qreal)window.width() / (qreal)origSize.width(); |
| 345 |
} |
| 346 |
m_viewSize = QSize((qreal)m_origWidth * scale, (qreal)m_origHeight * scale); |
| 347 |
QOpenGLWidget::resizeEvent(event); |
| 348 |
} |
| 349 |
|
| 350 |
void CaptureWin::updateShader() |
| 351 |
{ |
| 352 |
setV4LFormat(m_v4l_fmt); |
| 353 |
if (m_mode == AppModeTest || m_mode == AppModeTPG || m_mode == AppModeFile) { |
| 354 |
delete m_timer; |
| 355 |
m_timer = NULL; |
| 356 |
startTimer(); |
| 357 |
} |
| 358 |
m_updateShader = true; |
| 359 |
} |
| 360 |
|
| 361 |
void CaptureWin::showCurrentOverrides() |
| 362 |
{ |
| 363 |
static bool firstTime = true; |
| 364 |
const char *prefix = firstTime ? "" : "New "; |
| 365 |
|
| 366 |
if (m_mode == AppModeTest) |
| 367 |
return; |
| 368 |
|
| 369 |
if (m_canOverrideResolution || firstTime) { |
| 370 |
printf("%sPixel Format: '%s' %s\n", prefix, |
| 371 |
fcc2s(m_origPixelFormat).c_str(), |
| 372 |
pixfmt2s(m_origPixelFormat).c_str()); |
| 373 |
printf("%sField: %s\n", prefix, field2s(m_origField).c_str()); |
| 374 |
} |
| 375 |
printf("%sColorspace: %s\n", prefix, colorspace2s(m_origColorspace).c_str()); |
| 376 |
printf("%sTransfer Function: %s\n", prefix, xfer_func2s(m_origXferFunc).c_str()); |
| 377 |
if (m_is_hsv) |
| 378 |
printf("%sHSV Encoding: %s\n", prefix, ycbcr_enc2s(m_origHSVEnc).c_str()); |
| 379 |
else if (!m_is_rgb) |
| 380 |
printf("%sY'CbCr Encoding: %s\n", prefix, ycbcr_enc2s(m_origYCbCrEnc).c_str()); |
| 381 |
printf("%sQuantization: %s\n", prefix, quantization2s(m_origQuantization).c_str()); |
| 382 |
firstTime = false; |
| 383 |
} |
| 384 |
|
| 385 |
void CaptureWin::restoreAll(bool checked) |
| 386 |
{ |
| 387 |
m_overridePixelFormat = m_origPixelFormat; |
| 388 |
m_overrideField = m_origField; |
| 389 |
m_overrideColorspace = m_origColorspace; |
| 390 |
m_overrideXferFunc = m_origXferFunc; |
| 391 |
m_overrideYCbCrEnc = m_origYCbCrEnc; |
| 392 |
m_overrideHSVEnc = m_origHSVEnc; |
| 393 |
m_overrideQuantization = m_origQuantization; |
| 394 |
m_overrideWidth = m_overrideHeight = 0; |
| 395 |
showCurrentOverrides(); |
| 396 |
restoreSize(); |
| 397 |
} |
| 398 |
|
| 399 |
void CaptureWin::fmtChanged(QAction *a) |
| 400 |
{ |
| 401 |
m_overridePixelFormat = a->data().toInt(); |
| 402 |
if (m_overridePixelFormat == 0) |
| 403 |
m_overridePixelFormat = m_origPixelFormat; |
| 404 |
printf("New Pixel Format: '%s' %s\n", |
| 405 |
fcc2s(m_overridePixelFormat).c_str(), |
| 406 |
pixfmt2s(m_overridePixelFormat).c_str()); |
| 407 |
updateShader(); |
| 408 |
} |
| 409 |
|
| 410 |
void CaptureWin::fieldChanged(QAction *a) |
| 411 |
{ |
| 412 |
m_overrideField = a->data().toInt(); |
| 413 |
if (m_overrideField == 0xffffffff) |
| 414 |
m_overrideField = m_origField; |
| 415 |
printf("New Field: %s\n", field2s(m_overrideField).c_str()); |
| 416 |
updateShader(); |
| 417 |
} |
| 418 |
|
| 419 |
void CaptureWin::colorspaceChanged(QAction *a) |
| 420 |
{ |
| 421 |
m_overrideColorspace = a->data().toInt(); |
| 422 |
if (m_overrideColorspace == 0xffffffff) |
| 423 |
m_overrideColorspace = m_origColorspace; |
| 424 |
printf("New Colorspace: %s\n", colorspace2s(m_overrideColorspace).c_str()); |
| 425 |
updateShader(); |
| 426 |
} |
| 427 |
|
| 428 |
void CaptureWin::xferFuncChanged(QAction *a) |
| 429 |
{ |
| 430 |
m_overrideXferFunc = a->data().toInt(); |
| 431 |
if (m_overrideXferFunc == 0xffffffff) |
| 432 |
m_overrideXferFunc = m_origXferFunc; |
| 433 |
printf("New Transfer Function: %s\n", xfer_func2s(m_overrideXferFunc).c_str()); |
| 434 |
updateShader(); |
| 435 |
} |
| 436 |
|
| 437 |
void CaptureWin::ycbcrEncChanged(QAction *a) |
| 438 |
{ |
| 439 |
m_overrideYCbCrEnc = a->data().toInt(); |
| 440 |
if (m_overrideYCbCrEnc == 0xffffffff) |
| 441 |
m_overrideYCbCrEnc = m_origYCbCrEnc; |
| 442 |
printf("New Y'CbCr Encoding: %s\n", ycbcr_enc2s(m_overrideYCbCrEnc).c_str()); |
| 443 |
updateShader(); |
| 444 |
} |
| 445 |
|
| 446 |
void CaptureWin::hsvEncChanged(QAction *a) |
| 447 |
{ |
| 448 |
m_overrideHSVEnc = a->data().toInt(); |
| 449 |
if (m_overrideHSVEnc == 0xffffffff) |
| 450 |
m_overrideHSVEnc = m_origHSVEnc; |
| 451 |
printf("New HSV Encoding: %s\n", ycbcr_enc2s(m_overrideHSVEnc).c_str()); |
| 452 |
updateShader(); |
| 453 |
} |
| 454 |
|
| 455 |
void CaptureWin::quantChanged(QAction *a) |
| 456 |
{ |
| 457 |
m_overrideQuantization = a->data().toInt(); |
| 458 |
if (m_overrideQuantization == 0xffffffff) |
| 459 |
m_overrideQuantization = m_origQuantization; |
| 460 |
printf("New Quantization Range: %s\n", quantization2s(m_overrideQuantization).c_str()); |
| 461 |
updateShader(); |
| 462 |
} |
| 463 |
|
| 464 |
void CaptureWin::restoreSize(bool) |
| 465 |
{ |
| 466 |
QSize s = correctAspect(QSize(m_origWidth, m_origHeight)); |
| 467 |
|
| 468 |
m_scrollArea->resize(s); |
| 469 |
resize(s); |
| 470 |
updateShader(); |
| 471 |
} |
| 472 |
|
| 473 |
QSize CaptureWin::correctAspect(const QSize &s) const |
| 474 |
{ |
| 475 |
qreal aspect |
| 476 |
= (qreal)m_pixelaspect.denominator |
| 477 |
/ (qreal)m_pixelaspect.numerator; |
| 478 |
|
| 479 |
qreal w = s.width(); |
| 480 |
qreal h = s.height(); |
| 481 |
|
| 482 |
if (aspect < 1.0) |
| 483 |
h *= 1.0 / aspect; |
| 484 |
else |
| 485 |
w *= aspect; |
| 486 |
return QSize(qFloor(w), qFloor(h)); |
| 487 |
} |
| 488 |
|
| 489 |
void CaptureWin::windowScalingChanged(QAction *a) |
| 490 |
{ |
| 491 |
m_scrollArea->setWidgetResizable(a->data().toInt() == CAPTURE_GL_WIN_RESIZE); |
| 492 |
resize(correctAspect(QSize(m_origWidth, m_origHeight))); |
| 493 |
updateShader(); |
| 494 |
} |
| 495 |
|
| 496 |
void CaptureWin::resolutionOverrideChanged(bool checked) |
| 497 |
{ |
| 498 |
if (m_overrideWidth) |
| 499 |
m_origWidth = m_overrideWidth; |
| 500 |
if (m_overrideHeight) |
| 501 |
m_origHeight = m_overrideHeight; |
| 502 |
|
| 503 |
restoreSize(); |
| 504 |
} |
| 505 |
|
| 506 |
void CaptureWin::toggleFullScreen(bool) |
| 507 |
{ |
| 508 |
if (m_scrollArea->isFullScreen()) |
| 509 |
m_scrollArea->showNormal(); |
| 510 |
else |
| 511 |
m_scrollArea->showFullScreen(); |
| 512 |
} |
| 513 |
|
| 514 |
void CaptureWin::contextMenuEvent(QContextMenuEvent *event) |
| 515 |
{ |
| 516 |
QMenu menu(this); |
| 517 |
|
| 518 |
QAction *act = menu.addAction("Restore All"); |
| 519 |
connect(act, SIGNAL(triggered(bool)), this, SLOT(restoreAll(bool))); |
| 520 |
|
| 521 |
act = menu.addAction("Reset window"); |
| 522 |
connect(act, SIGNAL(triggered(bool)), this, SLOT(restoreSize(bool))); |
| 523 |
|
| 524 |
if (m_scrollArea->isFullScreen()) |
| 525 |
menu.addAction(m_exitFullScreen); |
| 526 |
else |
| 527 |
menu.addAction(m_enterFullScreen); |
| 528 |
|
| 529 |
if (m_canOverrideResolution) { |
| 530 |
menu.addAction(m_resolutionOverride); |
| 531 |
menu.addMenu(m_fmtMenu); |
| 532 |
} |
| 533 |
menu.addMenu(m_fieldMenu); |
| 534 |
menu.addMenu(m_colorspaceMenu); |
| 535 |
menu.addMenu(m_xferFuncMenu); |
| 536 |
if (m_is_hsv) |
| 537 |
menu.addMenu(m_hsvEncMenu); |
| 538 |
else if (!m_is_rgb) |
| 539 |
menu.addMenu(m_ycbcrEncMenu); |
| 540 |
menu.addMenu(m_quantMenu); |
| 541 |
menu.addMenu(m_displayMenu); |
| 542 |
|
| 543 |
menu.exec(event->globalPos()); |
| 544 |
} |
| 545 |
|
| 546 |
void CaptureWin::mouseDoubleClickEvent(QMouseEvent * e) |
| 547 |
{ |
| 548 |
if (e->button() != Qt::LeftButton) |
| 549 |
return; |
| 550 |
|
| 551 |
toggleFullScreen(); |
| 552 |
} |
| 553 |
|
| 554 |
void CaptureWin::cycleMenu(__u32 &overrideVal, __u32 origVal, |
| 555 |
const __u32 values[], bool hasShift, bool hasCtrl) |
| 556 |
{ |
| 557 |
unsigned i; |
| 558 |
|
| 559 |
if (overrideVal == 0xffffffff || hasCtrl) |
| 560 |
overrideVal = origVal; |
| 561 |
if (hasCtrl) |
| 562 |
return; |
| 563 |
for (i = 0; values[i] && values[i] != overrideVal; i++); |
| 564 |
if (!values[i]) |
| 565 |
overrideVal = values[0]; |
| 566 |
else if (hasShift) { |
| 567 |
if (i) |
| 568 |
overrideVal = values[i - 1]; |
| 569 |
else for (i = 0; values[i]; i++) |
| 570 |
overrideVal = values[i]; |
| 571 |
} else { |
| 572 |
if (!values[i + 1]) |
| 573 |
overrideVal = values[0]; |
| 574 |
else |
| 575 |
overrideVal = values[i + 1]; |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
void CaptureWin::keyPressEvent(QKeyEvent *event) |
| 580 |
{ |
| 581 |
unsigned w = m_v4l_fmt.g_width(); |
| 582 |
unsigned h = m_v4l_fmt.g_frame_height(); |
| 583 |
unsigned p = m_overrideHorPadding; |
| 584 |
bool hasShift = event->modifiers() & Qt::ShiftModifier; |
| 585 |
bool hasCtrl = event->modifiers() & Qt::ControlModifier; |
| 586 |
bool scalingEnabled = m_canOverrideResolution && |
| 587 |
m_resolutionOverride->isChecked(); |
| 588 |
|
| 589 |
switch (event->key()) { |
| 590 |
case Qt::Key_Space: |
| 591 |
if (m_mode == AppModeTest) |
| 592 |
m_cnt = 1; |
| 593 |
else if (m_singleStep && m_frame > m_singleStepStart) |
| 594 |
m_singleStepNext = true; |
| 595 |
else if (!m_singleStep && m_mode == AppModeFile) |
| 596 |
m_pause = !m_pause; |
| 597 |
return; |
| 598 |
case Qt::Key_Backspace: |
| 599 |
if (m_mode == AppModeFile && |
| 600 |
m_singleStep && m_frame > m_singleStepStart) { |
| 601 |
if (m_file.pos() >= m_imageSize * 2) { |
| 602 |
m_file.seek(m_file.pos() - m_imageSize * 2); |
| 603 |
m_frame -= 2; |
| 604 |
m_singleStepNext = true; |
| 605 |
} |
| 606 |
} |
| 607 |
return; |
| 608 |
case Qt::Key_Escape: |
| 609 |
if (!m_scrollArea->isFullScreen()) |
| 610 |
return; |
| 611 |
case Qt::Key_Left: |
| 612 |
if (hasShift) { |
| 613 |
if (scalingEnabled && p >= 2) |
| 614 |
p -= 2; |
| 615 |
} else { |
| 616 |
if (scalingEnabled && w > 16) |
| 617 |
w -= 2; |
| 618 |
} |
| 619 |
break; |
| 620 |
case Qt::Key_Right: |
| 621 |
if (hasShift) { |
| 622 |
if (scalingEnabled && p < 10240) |
| 623 |
p += 2; |
| 624 |
} else { |
| 625 |
if (scalingEnabled && w < 10240) |
| 626 |
w += 2; |
| 627 |
} |
| 628 |
break; |
| 629 |
case Qt::Key_Up: |
| 630 |
if (scalingEnabled && h > 16) |
| 631 |
h -= 2; |
| 632 |
break; |
| 633 |
case Qt::Key_Down: |
| 634 |
if (scalingEnabled && h < 10240) |
| 635 |
h += 2; |
| 636 |
break; |
| 637 |
|
| 638 |
case Qt::Key_C: |
| 639 |
cycleMenu(m_overrideColorspace, m_origColorspace, |
| 640 |
colorspaces, hasShift, hasCtrl); |
| 641 |
printf("New Colorspace: %s\n", colorspace2s(m_overrideColorspace).c_str()); |
| 642 |
checkSubMenuItem(m_colorspaceMenu, m_overrideColorspace); |
| 643 |
updateShader(); |
| 644 |
return; |
| 645 |
case Qt::Key_F: |
| 646 |
toggleFullScreen(); |
| 647 |
return; |
| 648 |
case Qt::Key_H: |
| 649 |
if (!m_is_hsv) |
| 650 |
return; |
| 651 |
cycleMenu(m_overrideHSVEnc, m_origHSVEnc, |
| 652 |
hsv_encs, hasShift, hasCtrl); |
| 653 |
printf("New HSV Encoding: %s\n", ycbcr_enc2s(m_overrideHSVEnc).c_str()); |
| 654 |
checkSubMenuItem(m_hsvEncMenu, m_overrideHSVEnc); |
| 655 |
updateShader(); |
| 656 |
return; |
| 657 |
case Qt::Key_I: |
| 658 |
cycleMenu(m_overrideField, m_origField, |
| 659 |
fields, hasShift, hasCtrl); |
| 660 |
printf("New Field: %s\n", field2s(m_overrideField).c_str()); |
| 661 |
checkSubMenuItem(m_fieldMenu, m_overrideField); |
| 662 |
updateShader(); |
| 663 |
return; |
| 664 |
case Qt::Key_P: |
| 665 |
if (!m_canOverrideResolution) |
| 666 |
return; |
| 667 |
cycleMenu(m_overridePixelFormat, m_origPixelFormat, |
| 668 |
formats, hasShift, hasCtrl); |
| 669 |
printf("New Pixel Format: '%s' %s\n", fcc2s(m_overridePixelFormat).c_str(), |
| 670 |
pixfmt2s(m_overridePixelFormat).c_str()); |
| 671 |
checkSubMenuItem(m_fmtMenu, m_overridePixelFormat); |
| 672 |
updateShader(); |
| 673 |
return; |
| 674 |
case Qt::Key_Q: |
| 675 |
QApplication::quit(); |
| 676 |
return; |
| 677 |
case Qt::Key_R: |
| 678 |
cycleMenu(m_overrideQuantization, m_origQuantization, |
| 679 |
quantizations, hasShift, hasCtrl); |
| 680 |
printf("New Quantization Range: %s\n", quantization2s(m_overrideQuantization).c_str()); |
| 681 |
checkSubMenuItem(m_quantMenu, m_overrideQuantization); |
| 682 |
updateShader(); |
| 683 |
return; |
| 684 |
case Qt::Key_X: |
| 685 |
cycleMenu(m_overrideXferFunc, m_origXferFunc, |
| 686 |
xfer_funcs, hasShift, hasCtrl); |
| 687 |
printf("New Transfer Function: %s\n", xfer_func2s(m_overrideXferFunc).c_str()); |
| 688 |
checkSubMenuItem(m_xferFuncMenu, m_overrideXferFunc); |
| 689 |
updateShader(); |
| 690 |
return; |
| 691 |
case Qt::Key_Y: |
| 692 |
if (m_is_rgb || m_is_hsv) |
| 693 |
return; |
| 694 |
cycleMenu(m_overrideYCbCrEnc, m_origYCbCrEnc, |
| 695 |
ycbcr_encs, hasShift, hasCtrl); |
| 696 |
printf("New Y'CbCr Encoding: %s\n", ycbcr_enc2s(m_overrideYCbCrEnc).c_str()); |
| 697 |
checkSubMenuItem(m_ycbcrEncMenu, m_overrideYCbCrEnc); |
| 698 |
updateShader(); |
| 699 |
return; |
| 700 |
default: |
| 701 |
QOpenGLWidget::keyPressEvent(event); |
| 702 |
return; |
| 703 |
} |
| 704 |
|
| 705 |
if (scalingEnabled) { |
| 706 |
if (m_scrollArea->widgetResizable()) |
| 707 |
m_scrollArea->resize(w, h); |
| 708 |
else |
| 709 |
resize(w, h); |
| 710 |
|
| 711 |
if ((w + p) != m_v4l_fmt.g_bytesperline()) { |
| 712 |
printf("New horizontal resolution: %u + %u (%u)\n", w, p, w + p); |
| 713 |
setOverrideHorPadding(p); |
| 714 |
updateShader(); |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
bool CaptureWin::supportedFmt(__u32 fmt) |
| 720 |
{ |
| 721 |
switch (fmt) { |
| 722 |
case V4L2_PIX_FMT_RGB565X: |
| 723 |
case V4L2_PIX_FMT_Y16_BE: |
| 724 |
return m_haveSwapBytes; |
| 725 |
|
| 726 |
/* |
| 727 |
* Note for RGB555(X) formats: |
| 728 |
* openGL ES doesn't support GL_UNSIGNED_SHORT_1_5_5_5_REV |
| 729 |
*/ |
| 730 |
case V4L2_PIX_FMT_RGB555: |
| 731 |
case V4L2_PIX_FMT_XRGB555: |
| 732 |
case V4L2_PIX_FMT_ARGB555: |
| 733 |
case V4L2_PIX_FMT_RGB555X: |
| 734 |
case V4L2_PIX_FMT_XRGB555X: |
| 735 |
case V4L2_PIX_FMT_ARGB555X: |
| 736 |
case V4L2_PIX_FMT_RGBX555: |
| 737 |
case V4L2_PIX_FMT_RGBA555: |
| 738 |
case V4L2_PIX_FMT_XBGR555: |
| 739 |
case V4L2_PIX_FMT_ABGR555: |
| 740 |
case V4L2_PIX_FMT_BGRX555: |
| 741 |
case V4L2_PIX_FMT_BGRA555: |
| 742 |
case V4L2_PIX_FMT_YUV555: |
| 743 |
case V4L2_PIX_FMT_RGB332: |
| 744 |
case V4L2_PIX_FMT_BGR666: |
| 745 |
return !context()->isOpenGLES(); |
| 746 |
} |
| 747 |
|
| 748 |
return true; |
| 749 |
} |
| 750 |
|
| 751 |
void CaptureWin::checkError(const char *msg) |
| 752 |
{ |
| 753 |
int err; |
| 754 |
unsigned errNo = 0; |
| 755 |
|
| 756 |
while ((err = glGetError()) != GL_NO_ERROR) |
| 757 |
fprintf(stderr, "OpenGL Error (no: %u) code 0x%x: %s.\n", errNo++, err, msg); |
| 758 |
|
| 759 |
if (errNo) |
| 760 |
std::exit(errNo); |
| 761 |
} |
| 762 |
|
| 763 |
void CaptureWin::configureTexture(size_t idx) |
| 764 |
{ |
| 765 |
glBindTexture(GL_TEXTURE_2D, m_screenTexture[idx]); |
| 766 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 767 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 768 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 769 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 770 |
} |
| 771 |
|
| 772 |
void CaptureWin::setOverrideWidth(__u32 w) |
| 773 |
{ |
| 774 |
m_overrideWidth = w; |
| 775 |
|
| 776 |
if (!m_overrideWidth && m_canOverrideResolution) |
| 777 |
m_resolutionOverride->setChecked(true); |
| 778 |
} |
| 779 |
|
| 780 |
void CaptureWin::setOverrideHeight(__u32 h) |
| 781 |
{ |
| 782 |
m_overrideHeight = h; |
| 783 |
|
| 784 |
if (!m_overrideHeight && m_canOverrideResolution) |
| 785 |
m_resolutionOverride->setChecked(true); |
| 786 |
} |
| 787 |
|
| 788 |
void CaptureWin::setOverrideHorPadding(__u32 p) |
| 789 |
{ |
| 790 |
m_overrideHorPadding = p; |
| 791 |
|
| 792 |
if (!m_overrideHorPadding && m_canOverrideResolution) |
| 793 |
m_resolutionOverride->setChecked(true); |
| 794 |
} |
| 795 |
|
| 796 |
void CaptureWin::setModeV4L2(cv4l_fd *fd) |
| 797 |
{ |
| 798 |
m_mode = AppModeV4L2; |
| 799 |
m_fd = fd; |
| 800 |
QSocketNotifier *readSock = new QSocketNotifier(fd->g_fd(), |
| 801 |
QSocketNotifier::Read, this); |
| 802 |
QSocketNotifier *excepSock = new QSocketNotifier(fd->g_fd(), |
| 803 |
QSocketNotifier::Exception, this); |
| 804 |
|
| 805 |
v4l2_event_subscription sub = { }; |
| 806 |
|
| 807 |
sub.type = V4L2_EVENT_SOURCE_CHANGE; |
| 808 |
m_fd->subscribe_event(sub); |
| 809 |
connect(readSock, SIGNAL(activated(int)), this, SLOT(v4l2ReadEvent())); |
| 810 |
connect(excepSock, SIGNAL(activated(int)), this, SLOT(v4l2ExceptionEvent())); |
| 811 |
|
| 812 |
if (m_verbose && m_fd->g_direct()) |
| 813 |
printf("using libv4l2\n"); |
| 814 |
} |
| 815 |
|
| 816 |
void CaptureWin::setModeSocket(int socket, int port) |
| 817 |
{ |
| 818 |
m_mode = AppModeSocket; |
| 819 |
m_sock = socket; |
| 820 |
m_port = port; |
| 821 |
if (m_ctx) |
| 822 |
free(m_ctx); |
| 823 |
m_ctx = fwht_alloc(m_v4l_fmt.g_pixelformat(), m_v4l_fmt.g_width(), m_v4l_fmt.g_height(), |
| 824 |
m_v4l_fmt.g_width(), m_v4l_fmt.g_height(), |
| 825 |
m_v4l_fmt.g_field(), m_v4l_fmt.g_colorspace(), m_v4l_fmt.g_xfer_func(), |
| 826 |
m_v4l_fmt.g_ycbcr_enc(), m_v4l_fmt.g_quantization()); |
| 827 |
|
| 828 |
QSocketNotifier *readSock = new QSocketNotifier(m_sock, |
| 829 |
QSocketNotifier::Read, this); |
| 830 |
|
| 831 |
connect(readSock, SIGNAL(activated(int)), this, SLOT(sockReadEvent())); |
| 832 |
} |
| 833 |
|
| 834 |
void CaptureWin::setModeFile(const QString &filename) |
| 835 |
{ |
| 836 |
m_mode = AppModeFile; |
| 837 |
m_file.setFileName(filename); |
| 838 |
if (!m_file.open(QIODevice::ReadOnly)) { |
| 839 |
fprintf(stderr, "could not open %s\n", filename.toUtf8().data()); |
| 840 |
std::exit(EXIT_FAILURE); |
| 841 |
} |
| 842 |
m_canOverrideResolution = true; |
| 843 |
} |
| 844 |
|
| 845 |
void CaptureWin::setModeTPG() |
| 846 |
{ |
| 847 |
m_mode = AppModeTPG; |
| 848 |
} |
| 849 |
|
| 850 |
void CaptureWin::setModeTest(unsigned cnt) |
| 851 |
{ |
| 852 |
m_mode = AppModeTest; |
| 853 |
m_test = cnt; |
| 854 |
} |
| 855 |
|
| 856 |
void CaptureWin::setQueue(cv4l_queue *q) |
| 857 |
{ |
| 858 |
m_v4l_queue = q; |
| 859 |
if (m_origPixelFormat == 0) |
| 860 |
updateOrigValues(); |
| 861 |
} |
| 862 |
|
| 863 |
bool CaptureWin::updateV4LFormat(const cv4l_fmt &fmt) |
| 864 |
{ |
| 865 |
m_is_rgb = true; |
| 866 |
m_is_hsv = false; |
| 867 |
m_uses_gl_red = false; |
| 868 |
m_accepts_srgb = true; |
| 869 |
m_is_bayer = false; |
| 870 |
|
| 871 |
switch (fmt.g_pixelformat()) { |
| 872 |
case V4L2_PIX_FMT_YUV422P: |
| 873 |
case V4L2_PIX_FMT_YUV420: |
| 874 |
case V4L2_PIX_FMT_YVU420: |
| 875 |
case V4L2_PIX_FMT_YUV420M: |
| 876 |
case V4L2_PIX_FMT_YVU420M: |
| 877 |
case V4L2_PIX_FMT_YUV422M: |
| 878 |
case V4L2_PIX_FMT_YVU422M: |
| 879 |
case V4L2_PIX_FMT_YUV444M: |
| 880 |
case V4L2_PIX_FMT_YVU444M: |
| 881 |
case V4L2_PIX_FMT_NV12: |
| 882 |
case V4L2_PIX_FMT_NV21: |
| 883 |
case V4L2_PIX_FMT_NV16: |
| 884 |
case V4L2_PIX_FMT_NV61: |
| 885 |
case V4L2_PIX_FMT_NV24: |
| 886 |
case V4L2_PIX_FMT_NV42: |
| 887 |
case V4L2_PIX_FMT_NV16M: |
| 888 |
case V4L2_PIX_FMT_NV61M: |
| 889 |
case V4L2_PIX_FMT_NV12M: |
| 890 |
case V4L2_PIX_FMT_NV21M: |
| 891 |
m_uses_gl_red = true; |
| 892 |
/* fall through */ |
| 893 |
case V4L2_PIX_FMT_YUYV: |
| 894 |
case V4L2_PIX_FMT_YVYU: |
| 895 |
case V4L2_PIX_FMT_UYVY: |
| 896 |
case V4L2_PIX_FMT_VYUY: |
| 897 |
case V4L2_PIX_FMT_YUV444: |
| 898 |
case V4L2_PIX_FMT_YUV555: |
| 899 |
case V4L2_PIX_FMT_YUV565: |
| 900 |
case V4L2_PIX_FMT_YUV32: |
| 901 |
case V4L2_PIX_FMT_AYUV32: |
| 902 |
case V4L2_PIX_FMT_XYUV32: |
| 903 |
case V4L2_PIX_FMT_VUYA32: |
| 904 |
case V4L2_PIX_FMT_VUYX32: |
| 905 |
case V4L2_PIX_FMT_YUVA32: |
| 906 |
case V4L2_PIX_FMT_YUVX32: |
| 907 |
m_is_rgb = false; |
| 908 |
m_accepts_srgb = false; |
| 909 |
break; |
| 910 |
case V4L2_PIX_FMT_HSV24: |
| 911 |
case V4L2_PIX_FMT_HSV32: |
| 912 |
m_is_rgb = false; |
| 913 |
m_is_hsv = true; |
| 914 |
m_accepts_srgb = false; |
| 915 |
break; |
| 916 |
case V4L2_PIX_FMT_SBGGR8: |
| 917 |
case V4L2_PIX_FMT_SGBRG8: |
| 918 |
case V4L2_PIX_FMT_SGRBG8: |
| 919 |
case V4L2_PIX_FMT_SRGGB8: |
| 920 |
case V4L2_PIX_FMT_SBGGR10: |
| 921 |
case V4L2_PIX_FMT_SGBRG10: |
| 922 |
case V4L2_PIX_FMT_SGRBG10: |
| 923 |
case V4L2_PIX_FMT_SRGGB10: |
| 924 |
case V4L2_PIX_FMT_SBGGR12: |
| 925 |
case V4L2_PIX_FMT_SGBRG12: |
| 926 |
case V4L2_PIX_FMT_SGRBG12: |
| 927 |
case V4L2_PIX_FMT_SRGGB12: |
| 928 |
case V4L2_PIX_FMT_SBGGR16: |
| 929 |
case V4L2_PIX_FMT_SGBRG16: |
| 930 |
case V4L2_PIX_FMT_SGRBG16: |
| 931 |
case V4L2_PIX_FMT_SRGGB16: |
| 932 |
m_is_bayer = true; |
| 933 |
/* fall through */ |
| 934 |
case V4L2_PIX_FMT_GREY: |
| 935 |
case V4L2_PIX_FMT_Y10: |
| 936 |
case V4L2_PIX_FMT_Y12: |
| 937 |
case V4L2_PIX_FMT_Y16: |
| 938 |
case V4L2_PIX_FMT_Y16_BE: |
| 939 |
case V4L2_PIX_FMT_Z16: |
| 940 |
m_uses_gl_red = true; |
| 941 |
/* fall through */ |
| 942 |
case V4L2_PIX_FMT_BGR666: |
| 943 |
case V4L2_PIX_FMT_RGB565: |
| 944 |
case V4L2_PIX_FMT_RGB565X: |
| 945 |
case V4L2_PIX_FMT_RGB444: |
| 946 |
case V4L2_PIX_FMT_XRGB444: |
| 947 |
case V4L2_PIX_FMT_ARGB444: |
| 948 |
case V4L2_PIX_FMT_XBGR444: |
| 949 |
case V4L2_PIX_FMT_ABGR444: |
| 950 |
case V4L2_PIX_FMT_RGBX444: |
| 951 |
case V4L2_PIX_FMT_RGBA444: |
| 952 |
case V4L2_PIX_FMT_BGRX444: |
| 953 |
case V4L2_PIX_FMT_BGRA444: |
| 954 |
case V4L2_PIX_FMT_RGB32: |
| 955 |
case V4L2_PIX_FMT_XRGB32: |
| 956 |
case V4L2_PIX_FMT_ARGB32: |
| 957 |
case V4L2_PIX_FMT_RGBX32: |
| 958 |
case V4L2_PIX_FMT_RGBA32: |
| 959 |
case V4L2_PIX_FMT_BGR32: |
| 960 |
case V4L2_PIX_FMT_XBGR32: |
| 961 |
case V4L2_PIX_FMT_ABGR32: |
| 962 |
case V4L2_PIX_FMT_BGRX32: |
| 963 |
case V4L2_PIX_FMT_BGRA32: |
| 964 |
m_accepts_srgb = false; |
| 965 |
/* fall through */ |
| 966 |
case V4L2_PIX_FMT_RGB24: |
| 967 |
case V4L2_PIX_FMT_BGR24: |
| 968 |
case V4L2_PIX_FMT_RGB555: |
| 969 |
case V4L2_PIX_FMT_XRGB555: |
| 970 |
case V4L2_PIX_FMT_ARGB555: |
| 971 |
case V4L2_PIX_FMT_RGB555X: |
| 972 |
case V4L2_PIX_FMT_XRGB555X: |
| 973 |
case V4L2_PIX_FMT_ARGB555X: |
| 974 |
case V4L2_PIX_FMT_RGBX555: |
| 975 |
case V4L2_PIX_FMT_RGBA555: |
| 976 |
case V4L2_PIX_FMT_XBGR555: |
| 977 |
case V4L2_PIX_FMT_ABGR555: |
| 978 |
case V4L2_PIX_FMT_BGRX555: |
| 979 |
case V4L2_PIX_FMT_BGRA555: |
| 980 |
case V4L2_PIX_FMT_RGB332: |
| 981 |
break; |
| 982 |
default: |
| 983 |
return false; |
| 984 |
} |
| 985 |
return true; |
| 986 |
} |
| 987 |
|
| 988 |
bool CaptureWin::setV4LFormat(cv4l_fmt &fmt) |
| 989 |
{ |
| 990 |
m_is_sdtv = false; |
| 991 |
|
| 992 |
if (m_mode == AppModeFile && m_overridePixelFormat) |
| 993 |
fmt.s_pixelformat(m_overridePixelFormat); |
| 994 |
|
| 995 |
if (!updateV4LFormat(fmt)) |
| 996 |
return false; |
| 997 |
|
| 998 |
if (m_is_bayer) { |
| 999 |
m_v4l_fmt.s_field(V4L2_FIELD_NONE); |
| 1000 |
m_overrideField = 0; |
| 1001 |
} |
| 1002 |
if (m_mode == AppModeFile && m_overrideWidth) |
| 1003 |
fmt.s_width(m_overrideWidth); |
| 1004 |
if (m_mode == AppModeFile && m_overrideHorPadding) |
| 1005 |
fmt.s_bytesperline(fmt.g_bytesperline() + m_overrideHorPadding); |
| 1006 |
if (m_mode == AppModeFile && m_overrideField != 0xffffffff) |
| 1007 |
fmt.s_field(m_overrideField); |
| 1008 |
if (m_mode == AppModeFile && m_overrideHeight) |
| 1009 |
fmt.s_frame_height(m_overrideHeight); |
| 1010 |
if (m_overrideColorspace != 0xffffffff) |
| 1011 |
fmt.s_colorspace(m_overrideColorspace); |
| 1012 |
if (m_is_hsv && m_overrideHSVEnc != 0xffffffff) |
| 1013 |
fmt.s_ycbcr_enc(m_overrideHSVEnc); |
| 1014 |
else if (!m_is_rgb && m_overrideYCbCrEnc != 0xffffffff) |
| 1015 |
fmt.s_ycbcr_enc(m_overrideYCbCrEnc); |
| 1016 |
if (m_overrideXferFunc != 0xffffffff) |
| 1017 |
fmt.s_xfer_func(m_overrideXferFunc); |
| 1018 |
if (m_overrideQuantization != 0xffffffff) |
| 1019 |
fmt.s_quantization(m_overrideQuantization); |
| 1020 |
|
| 1021 |
m_v4l_fmt = fmt; |
| 1022 |
|
| 1023 |
v4l2_input in; |
| 1024 |
|
| 1025 |
m_std = 0; |
| 1026 |
if (m_fd && !m_fd->g_input(in.index) && !m_fd->enum_input(in, true, in.index)) { |
| 1027 |
if (in.capabilities & V4L2_IN_CAP_STD) { |
| 1028 |
m_is_sdtv = true; |
| 1029 |
if (m_fd->g_std(m_std)) |
| 1030 |
m_std = fmt.g_frame_height() <= 480 ? |
| 1031 |
V4L2_STD_525_60 : V4L2_STD_625_50; |
| 1032 |
} else if (in.capabilities & V4L2_IN_CAP_DV_TIMINGS) { |
| 1033 |
v4l2_dv_timings timings; |
| 1034 |
if (m_fd->g_dv_timings(timings) == 0) { |
| 1035 |
m_is_sdtv = timings.bt.width <= 720 && timings.bt.height <= 576; |
| 1036 |
if (m_is_sdtv) |
| 1037 |
m_std = timings.bt.height <= 480 ? |
| 1038 |
V4L2_STD_525_60 : V4L2_STD_625_50; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
switch (fmt.g_colorspace()) { |
| 1044 |
case V4L2_COLORSPACE_SMPTE170M: |
| 1045 |
case V4L2_COLORSPACE_SMPTE240M: |
| 1046 |
case V4L2_COLORSPACE_REC709: |
| 1047 |
case V4L2_COLORSPACE_470_SYSTEM_M: |
| 1048 |
case V4L2_COLORSPACE_470_SYSTEM_BG: |
| 1049 |
case V4L2_COLORSPACE_SRGB: |
| 1050 |
case V4L2_COLORSPACE_OPRGB: |
| 1051 |
case V4L2_COLORSPACE_BT2020: |
| 1052 |
case V4L2_COLORSPACE_DCI_P3: |
| 1053 |
break; |
| 1054 |
default: |
| 1055 |
// If the colorspace was not specified, then guess |
| 1056 |
// based on the pixel format. |
| 1057 |
if (m_is_rgb) |
| 1058 |
m_v4l_fmt.s_colorspace(V4L2_COLORSPACE_SRGB); |
| 1059 |
else if (m_is_sdtv) |
| 1060 |
m_v4l_fmt.s_colorspace(V4L2_COLORSPACE_SMPTE170M); |
| 1061 |
else |
| 1062 |
m_v4l_fmt.s_colorspace(V4L2_COLORSPACE_REC709); |
| 1063 |
break; |
| 1064 |
} |
| 1065 |
if (fmt.g_xfer_func() == V4L2_XFER_FUNC_DEFAULT) |
| 1066 |
m_v4l_fmt.s_xfer_func(V4L2_MAP_XFER_FUNC_DEFAULT(m_v4l_fmt.g_colorspace())); |
| 1067 |
if (m_is_hsv) |
| 1068 |
m_v4l_fmt.s_ycbcr_enc(fmt.g_hsv_enc()); |
| 1069 |
else if (fmt.g_ycbcr_enc() == V4L2_YCBCR_ENC_DEFAULT) |
| 1070 |
m_v4l_fmt.s_ycbcr_enc(V4L2_MAP_YCBCR_ENC_DEFAULT(m_v4l_fmt.g_colorspace())); |
| 1071 |
if (fmt.g_quantization() == V4L2_QUANTIZATION_DEFAULT) |
| 1072 |
m_v4l_fmt.s_quantization(V4L2_MAP_QUANTIZATION_DEFAULT(m_is_rgb, |
| 1073 |
m_v4l_fmt.g_colorspace(), m_v4l_fmt.g_ycbcr_enc())); |
| 1074 |
|
| 1075 |
if (m_accepts_srgb && |
| 1076 |
(m_v4l_fmt.g_quantization() == V4L2_QUANTIZATION_LIM_RANGE || |
| 1077 |
m_v4l_fmt.g_xfer_func() != V4L2_XFER_FUNC_SRGB)) { |
| 1078 |
/* Can't let openGL convert from non-linear to linear */ |
| 1079 |
m_accepts_srgb = false; |
| 1080 |
} |
| 1081 |
|
| 1082 |
if (m_verbose) { |
| 1083 |
v4l2_fmtdesc fmt; |
| 1084 |
|
| 1085 |
strcpy((char *)fmt.description, fcc2s(m_v4l_fmt.g_pixelformat()).c_str()); |
| 1086 |
if (m_fd) { |
| 1087 |
m_fd->enum_fmt(fmt, true); |
| 1088 |
while (fmt.pixelformat != m_v4l_fmt.g_pixelformat()) { |
| 1089 |
if (m_fd->enum_fmt(fmt)) |
| 1090 |
break; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
printf("\n"); |
| 1095 |
switch (m_mode) { |
| 1096 |
case AppModeSocket: |
| 1097 |
printf("Mode: Capture from socket\n"); |
| 1098 |
break; |
| 1099 |
case AppModeFile: |
| 1100 |
printf("Mode: Read from file\n"); |
| 1101 |
break; |
| 1102 |
case AppModeTest: |
| 1103 |
printf("Mode: Test Formats\n"); |
| 1104 |
break; |
| 1105 |
case AppModeTPG: |
| 1106 |
printf("Mode: Test Pattern Generator\n"); |
| 1107 |
break; |
| 1108 |
case AppModeV4L2: |
| 1109 |
default: |
| 1110 |
printf("Mode: Capture\n"); |
| 1111 |
break; |
| 1112 |
} |
| 1113 |
printf("Width x Height: %ux%u\n", m_v4l_fmt.g_width(), m_v4l_fmt.g_height()); |
| 1114 |
printf("Field: %s\n", field2s(m_v4l_fmt.g_field()).c_str()); |
| 1115 |
printf("Pixel Format: %s ('%s')\n", fmt.description, fcc2s(m_v4l_fmt.g_pixelformat()).c_str()); |
| 1116 |
printf("Colorspace: %s\n", colorspace2s(m_v4l_fmt.g_colorspace()).c_str()); |
| 1117 |
if (m_is_hsv) |
| 1118 |
printf("HSV Encoding: %s\n", ycbcr_enc2s(m_v4l_fmt.g_hsv_enc()).c_str()); |
| 1119 |
else if (!m_is_rgb) |
| 1120 |
printf("Y'CbCr Encoding: %s\n", ycbcr_enc2s(m_v4l_fmt.g_ycbcr_enc()).c_str()); |
| 1121 |
printf("Transfer Function: %s\n", xfer_func2s(m_v4l_fmt.g_xfer_func()).c_str()); |
| 1122 |
printf("Quantization: %s\n", quantization2s(m_v4l_fmt.g_quantization()).c_str()); |
| 1123 |
for (unsigned i = 0; i < m_v4l_fmt.g_num_planes(); i++) { |
| 1124 |
printf("Plane %d Image Size: %u\n", i, m_v4l_fmt.g_sizeimage(i)); |
| 1125 |
printf("Plane %d Bytes per Line: %u\n", i, m_v4l_fmt.g_bytesperline(i)); |
| 1126 |
} |
| 1127 |
} |
| 1128 |
return true; |
| 1129 |
} |
| 1130 |
|
| 1131 |
void CaptureWin::setPixelAspect(const v4l2_fract &pixelaspect) |
| 1132 |
{ |
| 1133 |
m_pixelaspect = pixelaspect; |
| 1134 |
} |
| 1135 |
|
| 1136 |
void CaptureWin::v4l2ReadEvent() |
| 1137 |
{ |
| 1138 |
cv4l_buffer buf(m_fd->g_type()); |
| 1139 |
|
| 1140 |
if (m_pause) |
| 1141 |
return; |
| 1142 |
|
| 1143 |
if (m_singleStep && m_frame > m_singleStepStart && !m_singleStepNext) |
| 1144 |
return; |
| 1145 |
|
| 1146 |
m_singleStepNext = false; |
| 1147 |
if (m_fd->dqbuf(buf)) |
| 1148 |
return; |
| 1149 |
|
| 1150 |
for (unsigned i = 0; i < m_v4l_queue->g_num_planes(); i++) { |
| 1151 |
m_nextData[i] = (__u8 *)m_v4l_queue->g_dataptr(buf.g_index(), i); |
| 1152 |
m_nextSize[i] = buf.g_bytesused(i); |
| 1153 |
} |
| 1154 |
int next = m_nextIndex; |
| 1155 |
m_nextIndex = buf.g_index(); |
| 1156 |
if (next != -1) { |
| 1157 |
buf.s_index(next); |
| 1158 |
m_fd->qbuf(buf); |
| 1159 |
} |
| 1160 |
m_frame++; |
| 1161 |
update(); |
| 1162 |
if (m_cnt == 0) |
| 1163 |
return; |
| 1164 |
if (--m_cnt == 0) |
| 1165 |
std::exit(EXIT_SUCCESS); |
| 1166 |
} |
| 1167 |
|
| 1168 |
void CaptureWin::v4l2ExceptionEvent() |
| 1169 |
{ |
| 1170 |
v4l2_event ev; |
| 1171 |
cv4l_fmt fmt; |
| 1172 |
|
| 1173 |
while (m_fd->dqevent(ev) == 0) { |
| 1174 |
switch (ev.type) { |
| 1175 |
case V4L2_EVENT_SOURCE_CHANGE: |
| 1176 |
m_fd->g_fmt(fmt); |
| 1177 |
if (!setV4LFormat(fmt)) { |
| 1178 |
fprintf(stderr, "Unsupported format: '%s' %s\n", |
| 1179 |
fcc2s(fmt.g_pixelformat()).c_str(), |
| 1180 |
pixfmt2s(fmt.g_pixelformat()).c_str()); |
| 1181 |
std::exit(EXIT_FAILURE); |
| 1182 |
} |
| 1183 |
updateOrigValues(); |
| 1184 |
m_updateShader = true; |
| 1185 |
break; |
| 1186 |
} |
| 1187 |
} |
| 1188 |
} |
| 1189 |
|
| 1190 |
void CaptureWin::listenForNewConnection() |
| 1191 |
{ |
| 1192 |
cv4l_fmt fmt; |
| 1193 |
v4l2_fract pixelaspect = { 1, 1 }; |
| 1194 |
|
| 1195 |
::close(m_sock); |
| 1196 |
|
| 1197 |
for (unsigned p = 0; p < m_v4l_fmt.g_num_planes(); p++) { |
| 1198 |
m_curSize[p] = 0; |
| 1199 |
delete [] m_curData[p]; |
| 1200 |
m_curData[p] = NULL; |
| 1201 |
} |
| 1202 |
|
| 1203 |
int sock_fd; |
| 1204 |
|
| 1205 |
for (;;) { |
| 1206 |
sock_fd = initSocket(m_port, fmt, pixelaspect); |
| 1207 |
if (setV4LFormat(fmt)) |
| 1208 |
break; |
| 1209 |
fprintf(stderr, "Unsupported format: '%s' %s\n", |
| 1210 |
fcc2s(fmt.g_pixelformat()).c_str(), |
| 1211 |
pixfmt2s(fmt.g_pixelformat()).c_str()); |
| 1212 |
::close(sock_fd); |
| 1213 |
} |
| 1214 |
if (m_ctx) |
| 1215 |
free(m_ctx); |
| 1216 |
m_ctx = fwht_alloc(fmt.g_pixelformat(), fmt.g_width(), fmt.g_height(), |
| 1217 |
fmt.g_width(), fmt.g_height(), |
| 1218 |
fmt.g_field(), fmt.g_colorspace(), fmt.g_xfer_func(), |
| 1219 |
fmt.g_ycbcr_enc(), fmt.g_quantization()); |
| 1220 |
setPixelAspect(pixelaspect); |
| 1221 |
updateOrigValues(); |
| 1222 |
setModeSocket(sock_fd, m_port); |
| 1223 |
restoreSize(); |
| 1224 |
} |
| 1225 |
|
| 1226 |
int CaptureWin::read_u32(__u32 &v) |
| 1227 |
{ |
| 1228 |
int n; |
| 1229 |
|
| 1230 |
v = 0; |
| 1231 |
n = read(m_sock, &v, sizeof(v)); |
| 1232 |
if (n != sizeof(v)) { |
| 1233 |
fprintf(stderr, "could not read __u32\n"); |
| 1234 |
return -1; |
| 1235 |
} |
| 1236 |
v = ntohl(v); |
| 1237 |
return 0; |
| 1238 |
} |
| 1239 |
|
| 1240 |
void CaptureWin::sockReadEvent() |
| 1241 |
{ |
| 1242 |
int n; |
| 1243 |
|
| 1244 |
if (m_pause) |
| 1245 |
return; |
| 1246 |
|
| 1247 |
if (m_singleStep && m_frame > m_singleStepStart && !m_singleStepNext) |
| 1248 |
return; |
| 1249 |
m_singleStepNext = false; |
| 1250 |
|
| 1251 |
if (m_origPixelFormat == 0) |
| 1252 |
updateOrigValues(); |
| 1253 |
|
| 1254 |
if (m_curSize[0] == 0) { |
| 1255 |
for (unsigned p = 0; p < m_v4l_fmt.g_num_planes(); p++) { |
| 1256 |
m_curSize[p] = m_v4l_fmt.g_sizeimage(p); |
| 1257 |
m_curData[p] = new __u8[m_curSize[p]]; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
unsigned packet, sz; |
| 1262 |
bool is_fwht; |
| 1263 |
|
| 1264 |
if (read_u32(packet)) |
| 1265 |
goto new_conn; |
| 1266 |
|
| 1267 |
if (packet == V4L_STREAM_PACKET_END) { |
| 1268 |
fprintf(stderr, "END packet read\n"); |
| 1269 |
goto new_conn; |
| 1270 |
} |
| 1271 |
|
| 1272 |
if (read_u32(sz)) |
| 1273 |
goto new_conn; |
| 1274 |
|
| 1275 |
if (packet != V4L_STREAM_PACKET_FRAME_VIDEO_RLE && |
| 1276 |
packet != V4L_STREAM_PACKET_FRAME_VIDEO_FWHT) { |
| 1277 |
char buf[1024]; |
| 1278 |
|
| 1279 |
fprintf(stderr, "expected FRAME_VIDEO, got 0x%08x\n", packet); |
| 1280 |
while (sz) { |
| 1281 |
unsigned rdsize = sz > sizeof(buf) ? sizeof(buf) : sz; |
| 1282 |
|
| 1283 |
n = read(m_sock, buf, rdsize); |
| 1284 |
if (n < 0) { |
| 1285 |
fprintf(stderr, "error reading %d bytes\n", sz); |
| 1286 |
goto new_conn; |
| 1287 |
} |
| 1288 |
sz -= n; |
| 1289 |
} |
| 1290 |
return; |
| 1291 |
} |
| 1292 |
|
| 1293 |
is_fwht = m_ctx && packet == V4L_STREAM_PACKET_FRAME_VIDEO_FWHT; |
| 1294 |
|
| 1295 |
if (read_u32(sz)) |
| 1296 |
goto new_conn; |
| 1297 |
|
| 1298 |
if (sz != V4L_STREAM_PACKET_FRAME_VIDEO_SIZE_HDR) { |
| 1299 |
fprintf(stderr, "unsupported FRAME_VIDEO size\n"); |
| 1300 |
goto new_conn; |
| 1301 |
} |
| 1302 |
if (read_u32(sz) || // ignore field |
| 1303 |
read_u32(sz)) // ignore flags |
| 1304 |
goto new_conn; |
| 1305 |
|
| 1306 |
for (unsigned p = 0; p < m_v4l_fmt.g_num_planes(); p++) { |
| 1307 |
__u32 max_size = is_fwht ? m_ctx->comp_max_size : m_curSize[p]; |
| 1308 |
__u8 *dst = is_fwht ? m_ctx->state.compressed_frame : m_curData[p]; |
| 1309 |
__u32 data_size; |
| 1310 |
__u32 offset; |
| 1311 |
__u32 size; |
| 1312 |
|
| 1313 |
if (read_u32(sz)) |
| 1314 |
goto new_conn; |
| 1315 |
if (sz != V4L_STREAM_PACKET_FRAME_VIDEO_SIZE_PLANE_HDR) { |
| 1316 |
fprintf(stderr, "unsupported FRAME_VIDEO plane size\n"); |
| 1317 |
goto new_conn; |
| 1318 |
} |
| 1319 |
if (read_u32(size) || read_u32(data_size)) |
| 1320 |
goto new_conn; |
| 1321 |
offset = is_fwht ? 0 : size - data_size; |
| 1322 |
sz = data_size; |
| 1323 |
|
| 1324 |
if (data_size > max_size) { |
| 1325 |
fprintf(stderr, "data size is too large (%u > %u)\n", |
| 1326 |
data_size, max_size); |
| 1327 |
goto new_conn; |
| 1328 |
} |
| 1329 |
while (sz) { |
| 1330 |
n = read(m_sock, dst + offset, sz); |
| 1331 |
if (n < 0) { |
| 1332 |
fprintf(stderr, "error reading %d bytes\n", sz); |
| 1333 |
goto new_conn; |
| 1334 |
} |
| 1335 |
if ((__u32)n == sz) |
| 1336 |
break; |
| 1337 |
offset += n; |
| 1338 |
sz -= n; |
| 1339 |
} |
| 1340 |
if (is_fwht) |
| 1341 |
fwht_decompress(m_ctx, dst, data_size, m_curData[p], m_curSize[p]); |
| 1342 |
else |
| 1343 |
rle_decompress(dst, size, data_size, |
| 1344 |
rle_calc_bpl(m_v4l_fmt.g_bytesperline(p), m_v4l_fmt.g_pixelformat())); |
| 1345 |
} |
| 1346 |
m_frame++; |
| 1347 |
update(); |
| 1348 |
if (m_cnt == 0) |
| 1349 |
return; |
| 1350 |
if (--m_cnt == 0) |
| 1351 |
std::exit(EXIT_SUCCESS); |
| 1352 |
return; |
| 1353 |
|
| 1354 |
new_conn: |
| 1355 |
listenForNewConnection(); |
| 1356 |
} |
| 1357 |
|
| 1358 |
void CaptureWin::resizeGL(int w, int h) |
| 1359 |
{ |
| 1360 |
if (!m_canOverrideResolution || !m_resolutionOverride->isChecked()) |
| 1361 |
return; |
| 1362 |
w &= ~1; |
| 1363 |
h &= ~1; |
| 1364 |
m_overrideWidth = w; |
| 1365 |
m_overrideHeight = h; |
| 1366 |
m_viewSize = QSize(m_overrideWidth, m_overrideHeight); |
| 1367 |
updateShader(); |
| 1368 |
printf("New resolution: %ux%u\n", w, h); |
| 1369 |
} |
| 1370 |
|
| 1371 |
void CaptureWin::updateOrigValues() |
| 1372 |
{ |
| 1373 |
m_origWidth = m_v4l_fmt.g_width(); |
| 1374 |
m_origHeight = m_v4l_fmt.g_frame_height(); |
| 1375 |
m_overrideWidth = m_overrideHeight = 0; |
| 1376 |
m_origPixelFormat = m_v4l_fmt.g_pixelformat(); |
| 1377 |
m_origField = m_v4l_fmt.g_field(); |
| 1378 |
m_origColorspace = m_v4l_fmt.g_colorspace(); |
| 1379 |
m_origXferFunc = m_v4l_fmt.g_xfer_func(); |
| 1380 |
if (m_is_rgb) |
| 1381 |
m_origXferFunc = V4L2_YCBCR_ENC_601; |
| 1382 |
else if (m_is_hsv) |
| 1383 |
m_origHSVEnc = m_v4l_fmt.g_hsv_enc(); |
| 1384 |
else |
| 1385 |
m_origYCbCrEnc = m_v4l_fmt.g_ycbcr_enc(); |
| 1386 |
m_origQuantization = m_v4l_fmt.g_quantization(); |
| 1387 |
showCurrentOverrides(); |
| 1388 |
m_viewSize = QSize(m_origWidth, m_origHeight); |
| 1389 |
} |
| 1390 |
|
| 1391 |
void CaptureWin::initImageFormat() |
| 1392 |
{ |
| 1393 |
updateV4LFormat(m_v4l_fmt); |
| 1394 |
tpg_s_fourcc(&m_tpg, m_v4l_fmt.g_pixelformat()); |
| 1395 |
bool is_alt = m_v4l_fmt.g_field() == V4L2_FIELD_ALTERNATE; |
| 1396 |
|
| 1397 |
tpg_reset_source(&m_tpg, m_v4l_fmt.g_width(), |
| 1398 |
m_v4l_fmt.g_frame_height(), m_v4l_fmt.g_field()); |
| 1399 |
tpg_s_field(&m_tpg, m_v4l_fmt.g_first_field(m_std), is_alt); |
| 1400 |
tpg_s_colorspace(&m_tpg, m_v4l_fmt.g_colorspace()); |
| 1401 |
tpg_s_xfer_func(&m_tpg, m_v4l_fmt.g_xfer_func()); |
| 1402 |
if (m_is_hsv) |
| 1403 |
tpg_s_hsv_enc(&m_tpg, m_v4l_fmt.g_hsv_enc()); |
| 1404 |
else if (!m_is_rgb) |
| 1405 |
tpg_s_ycbcr_enc(&m_tpg, m_v4l_fmt.g_ycbcr_enc()); |
| 1406 |
tpg_s_quantization(&m_tpg, m_v4l_fmt.g_quantization()); |
| 1407 |
m_v4l_fmt.s_num_planes(tpg_g_buffers(&m_tpg)); |
| 1408 |
for (unsigned p = 0; p < m_v4l_fmt.g_num_planes(); p++) { |
| 1409 |
if (m_mode == AppModeFile && m_overrideHorPadding) |
| 1410 |
tpg_s_bytesperline(&m_tpg, p, tpg_g_bytesperline(&m_tpg, p) + m_overrideHorPadding); |
| 1411 |
|
| 1412 |
m_v4l_fmt.s_bytesperline(tpg_g_bytesperline(&m_tpg, p), p); |
| 1413 |
m_v4l_fmt.s_sizeimage(tpg_calc_plane_size(&m_tpg, p), p); |
| 1414 |
} |
| 1415 |
if (tpg_g_buffers(&m_tpg) == 1) { |
| 1416 |
unsigned size = 0; |
| 1417 |
|
| 1418 |
for (unsigned p = 0; p < tpg_g_planes(&m_tpg); p++) |
| 1419 |
size += tpg_calc_plane_size(&m_tpg, p); |
| 1420 |
m_v4l_fmt.s_sizeimage(size, 0); |
| 1421 |
} |
| 1422 |
if (m_verbose) |
| 1423 |
tpg_log_status(&m_tpg); |
| 1424 |
} |
| 1425 |
|
| 1426 |
void CaptureWin::startTimer() |
| 1427 |
{ |
| 1428 |
if (m_origPixelFormat == 0) |
| 1429 |
updateOrigValues(); |
| 1430 |
initImageFormat(); |
| 1431 |
|
| 1432 |
m_timer = new QTimer(this); |
| 1433 |
connect(m_timer, SIGNAL(timeout()), this, SLOT(tpgUpdateFrame())); |
| 1434 |
|
| 1435 |
m_imageSize = 0; |
| 1436 |
for (unsigned p = 0; p < m_v4l_fmt.g_num_planes(); p++) |
| 1437 |
m_imageSize += m_v4l_fmt.g_sizeimage(p); |
| 1438 |
|
| 1439 |
if (m_file.isOpen() && (m_fromFrame + 1) * m_imageSize > m_file.size()) { |
| 1440 |
fprintf(stderr, "the file size is too small (expect at least %u, got %llu)\n", |
| 1441 |
(m_fromFrame + 1) * m_imageSize, m_file.size()); |
| 1442 |
} |
| 1443 |
|
| 1444 |
if (m_file.isOpen()) { |
| 1445 |
m_file.seek(m_fromFrame * m_imageSize); |
| 1446 |
m_frame = m_fromFrame; |
| 1447 |
} |
| 1448 |
|
| 1449 |
for (unsigned p = 0; p < m_v4l_fmt.g_num_planes(); p++) { |
| 1450 |
m_curSize[p] = m_v4l_fmt.g_sizeimage(p); |
| 1451 |
delete [] m_curData[p]; |
| 1452 |
if (m_canOverrideResolution) |
| 1453 |
m_curData[p] = new __u8[4096 * 2160 * (p ? 2 : 4)]; |
| 1454 |
else |
| 1455 |
m_curData[p] = new __u8[m_curSize[p]]; |
| 1456 |
if (m_file.isOpen()) |
| 1457 |
m_file.read((char *)m_curData[p], m_curSize[p]); |
| 1458 |
else |
| 1459 |
tpg_fillbuffer(&m_tpg, 0, p, m_curData[p]); |
| 1460 |
} |
| 1461 |
bool is_alt = m_v4l_fmt.g_field() == V4L2_FIELD_ALTERNATE; |
| 1462 |
tpg_update_mv_count(&m_tpg, is_alt); |
| 1463 |
m_timer->setTimerType(Qt::PreciseTimer); |
| 1464 |
m_timer->setSingleShot(false); |
| 1465 |
m_timer->setInterval(1000.0 / (m_fps * (is_alt ? 2 : 1))); |
| 1466 |
m_timer->start(1000.0 / (m_fps * (is_alt ? 2 : 1))); |
| 1467 |
if (is_alt && m_cnt) |
| 1468 |
m_cnt *= 2; |
| 1469 |
if (m_mode == AppModeTest) { |
| 1470 |
fprintf(stderr, "test %s ('%s'), %s, %s, %s, ", |
| 1471 |
pixfmt2s(formats[m_testState.fmt_idx]).c_str(), |
| 1472 |
fcc2s(formats[m_testState.fmt_idx]).c_str(), |
| 1473 |
field2s(fields[m_testState.field_idx]).c_str(), |
| 1474 |
colorspace2s(colorspaces[m_testState.colorspace_idx]).c_str(), |
| 1475 |
xfer_func2s(xfer_funcs[m_testState.xfer_func_idx]).c_str()); |
| 1476 |
if (m_is_rgb) |
| 1477 |
fprintf(stderr, "%s\n", |
| 1478 |
quantization2s(quantizations[m_testState.quant_idx]).c_str()); |
| 1479 |
else if (m_is_hsv) |
| 1480 |
fprintf(stderr, "%s, %s\n", |
| 1481 |
ycbcr_enc2s(ycbcr_encs[m_testState.hsv_enc_idx]).c_str(), |
| 1482 |
quantization2s(quantizations[m_testState.quant_idx]).c_str()); |
| 1483 |
else |
| 1484 |
fprintf(stderr, "%s, %s\n", |
| 1485 |
ycbcr_enc2s(ycbcr_encs[m_testState.ycbcr_enc_idx]).c_str(), |
| 1486 |
quantization2s(quantizations[m_testState.quant_idx]).c_str()); |
| 1487 |
} |
| 1488 |
m_frame++; |
| 1489 |
if (m_verbose) |
| 1490 |
printf("frame %d\n", m_frame - 1); |
| 1491 |
} |
| 1492 |
|
| 1493 |
void CaptureWin::tpgUpdateFrame() |
| 1494 |
{ |
| 1495 |
bool is_alt = m_v4l_fmt.g_field() == V4L2_FIELD_ALTERNATE; |
| 1496 |
|
| 1497 |
if (m_mode != AppModeTest && m_pause) |
| 1498 |
return; |
| 1499 |
|
| 1500 |
if (m_mode != AppModeTest && m_singleStep && m_frame > m_singleStepStart && |
| 1501 |
!m_singleStepNext) |
| 1502 |
return; |
| 1503 |
m_singleStepNext = false; |
| 1504 |
|
| 1505 |
if (m_mode == AppModeFile && m_file.pos() + m_imageSize > m_file.size()) { |
| 1506 |
if (m_no_loop) { |
| 1507 |
static bool done; |
| 1508 |
|
| 1509 |
if (!done) { |
| 1510 |
if (m_verbose) |
| 1511 |
printf("done\n"); |
| 1512 |
done = true; |
| 1513 |
} |
| 1514 |
return; |
| 1515 |
} |
| 1516 |
if (m_verbose) |
| 1517 |
printf("loop\n"); |
| 1518 |
m_file.seek(0); |
| 1519 |
m_frame = 0; |
| 1520 |
} |
| 1521 |
|
| 1522 |
if (m_mode != AppModeFile && is_alt) { |
| 1523 |
if (m_tpg.field == V4L2_FIELD_TOP) |
| 1524 |
tpg_s_field(&m_tpg, V4L2_FIELD_BOTTOM, true); |
| 1525 |
else |
| 1526 |
tpg_s_field(&m_tpg, V4L2_FIELD_TOP, true); |
| 1527 |
} |
| 1528 |
|
| 1529 |
for (unsigned p = 0; p < m_v4l_fmt.g_num_planes(); p++) { |
| 1530 |
if (m_mode == AppModeFile) |
| 1531 |
m_file.read((char *)m_curData[p], m_curSize[p]); |
| 1532 |
else |
| 1533 |
tpg_fillbuffer(&m_tpg, 0, p, m_curData[p]); |
| 1534 |
} |
| 1535 |
m_frame++; |
| 1536 |
if (m_verbose) |
| 1537 |
printf("frame %d\n", m_frame - 1); |
| 1538 |
update(); |
| 1539 |
if (m_cnt != 1) |
| 1540 |
tpg_update_mv_count(&m_tpg, is_alt); |
| 1541 |
|
| 1542 |
if (m_cnt == 0) |
| 1543 |
return; |
| 1544 |
if (--m_cnt) |
| 1545 |
return; |
| 1546 |
|
| 1547 |
delete m_timer; |
| 1548 |
m_timer = NULL; |
| 1549 |
if (!m_test) |
| 1550 |
std::exit(EXIT_SUCCESS); |
| 1551 |
|
| 1552 |
m_cnt = m_test; |
| 1553 |
|
| 1554 |
bool mask_quant = m_testState.mask & QUANT_MASK; |
| 1555 |
bool mask_ycbcr_enc = m_is_rgb || m_is_hsv || (m_testState.mask & YCBCR_HSV_ENC_MASK); |
| 1556 |
bool mask_hsv_enc = !m_is_hsv || (m_testState.mask & YCBCR_HSV_ENC_MASK); |
| 1557 |
bool mask_xfer_func = m_testState.mask & XFER_FUNC_MASK; |
| 1558 |
bool mask_colorspace = m_testState.mask & COLORSPACE_MASK; |
| 1559 |
bool mask_field = m_is_bayer || (m_testState.mask & FIELD_MASK); |
| 1560 |
bool mask_fmt = m_testState.mask & FMT_MASK; |
| 1561 |
|
| 1562 |
if (mask_quant || |
| 1563 |
quantizations[++m_testState.quant_idx] == 0) { |
| 1564 |
if (!mask_quant) |
| 1565 |
m_testState.quant_idx = 0; |
| 1566 |
if (mask_ycbcr_enc || |
| 1567 |
ycbcr_encs[++m_testState.ycbcr_enc_idx] == 0) { |
| 1568 |
if (!mask_ycbcr_enc) |
| 1569 |
m_testState.ycbcr_enc_idx = 0; |
| 1570 |
if (mask_hsv_enc || |
| 1571 |
hsv_encs[++m_testState.hsv_enc_idx] == 0) { |
| 1572 |
if (!mask_hsv_enc) |
| 1573 |
m_testState.hsv_enc_idx = 0; |
| 1574 |
if (mask_xfer_func || |
| 1575 |
xfer_funcs[++m_testState.xfer_func_idx] == 0) { |
| 1576 |
if (!mask_xfer_func) |
| 1577 |
m_testState.xfer_func_idx = 0; |
| 1578 |
if (mask_colorspace || |
| 1579 |
colorspaces[++m_testState.colorspace_idx] == 0) { |
| 1580 |
if (!mask_colorspace) |
| 1581 |
m_testState.colorspace_idx = 0; |
| 1582 |
if (mask_field || |
| 1583 |
fields[++m_testState.field_idx] == 0) { |
| 1584 |
if (!mask_field) |
| 1585 |
m_testState.field_idx = 0; |
| 1586 |
if (mask_fmt || |
| 1587 |
formats[++m_testState.fmt_idx] == 0) |
| 1588 |
std::exit(EXIT_SUCCESS); |
| 1589 |
} |
| 1590 |
} |
| 1591 |
} |
| 1592 |
} |
| 1593 |
} |
| 1594 |
} |
| 1595 |
|
| 1596 |
while (!supportedFmt(formats[m_testState.fmt_idx])) |
| 1597 |
if (formats[++m_testState.fmt_idx] == 0) |
| 1598 |
std::exit(EXIT_SUCCESS); |
| 1599 |
|
| 1600 |
m_v4l_fmt.s_pixelformat(formats[m_testState.fmt_idx]); |
| 1601 |
updateV4LFormat(m_v4l_fmt); |
| 1602 |
m_v4l_fmt.s_field(fields[m_testState.field_idx]); |
| 1603 |
m_v4l_fmt.s_colorspace(colorspaces[m_testState.colorspace_idx]); |
| 1604 |
m_v4l_fmt.s_xfer_func(xfer_funcs[m_testState.xfer_func_idx]); |
| 1605 |
if (m_is_hsv) |
| 1606 |
m_v4l_fmt.s_ycbcr_enc(hsv_encs[m_testState.hsv_enc_idx]); |
| 1607 |
else |
| 1608 |
m_v4l_fmt.s_ycbcr_enc(ycbcr_encs[m_testState.ycbcr_enc_idx]); |
| 1609 |
m_v4l_fmt.s_quantization(quantizations[m_testState.quant_idx]); |
| 1610 |
|
| 1611 |
if (m_accepts_srgb && |
| 1612 |
(m_v4l_fmt.g_quantization() == V4L2_QUANTIZATION_LIM_RANGE || |
| 1613 |
m_v4l_fmt.g_xfer_func() != V4L2_XFER_FUNC_SRGB)) { |
| 1614 |
/* Can't let openGL convert from non-linear to linear */ |
| 1615 |
m_accepts_srgb = false; |
| 1616 |
} |
| 1617 |
startTimer(); |
| 1618 |
m_updateShader = true; |
| 1619 |
} |