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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
|
/*
*
* Copyright (C) 2002 George Staikos <staikos@kde.org>
* 2004 Dirk Ziegelmeier <dziegel@gmx.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "qvideostream.h"
#include <tqevent.h>
#include <tqimage.h>
#include <tqtimer.h>
#include <kdebug.h>
#include "kxv.h"
#include <sys/types.h>
#include <X11/Xutil.h>
#ifdef HAVE_XSHM
extern "C" {
#include <sys/shm.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/extensions/XShm.h>
}
#endif
#ifdef HAVE_GL
class QVideoStreamGLWidget : public TQGLWidget
{
public:
QVideoStreamGLWidget(TQWidget* parent = 0, const char* name = 0);
virtual ~QVideoStreamGLWidget();
void setInputSize(const TQSize& sz);
void display(const unsigned char *const img, int x, int y, int sw, int sh);
private:
virtual void resizeGL(int w, int h);
void initializeGL();
virtual bool eventFilter( TQObject *o, TQEvent *e );
void calc(TQPoint& p, TQPoint& v);
TQSize _inputSize;
GLuint _tex;
int _tw, _th;
TQWidget* _w;
int _maxGL;
TQSize _sz;
bool _glfun;
TQPoint _ul, _ur, _ll, _lr;
TQPoint _vul, _vur, _vll, _vlr;
TQTimer* _glfunTimer;
};
#endif
class QVideoStreamPrivate
{
public:
QVideoStreamPrivate();
~QVideoStreamPrivate();
KXv *xvHandle;
KXvDevice *xvdev;
XImage *xim;
GC gc;
#ifdef HAVE_GL
QVideoStreamGLWidget* glwidget;
#endif
#ifdef HAVE_XSHM
XShmSegmentInfo shmh;
#endif
};
QVideoStreamPrivate::QVideoStreamPrivate()
{
xvHandle = 0;
xim = 0;
}
QVideoStreamPrivate::~QVideoStreamPrivate()
{
delete xvHandle;
}
QVideoStream::QVideoStream(TQWidget *widget, const char* name)
: TQObject(widget, name),
d(new QVideoStreamPrivate),
_w(widget),
_methods(METHOD_NONE),
_method(METHOD_NONE),
_format(FORMAT_NONE),
_init(false)
{
int dummy;
unsigned int dummy2;
findDisplayProperties(_xFormat, dummy, dummy2, dummy);
_methods = (VideoMethod)(_methods | METHOD_X11);
#ifdef HAVE_XSHM
if (XShmQueryExtension(_w->x11Display())) {
_methods = (VideoMethod)(_methods | METHOD_XSHM);
}
#endif
if (KXv::haveXv()) {
_methods = (VideoMethod)(_methods | METHOD_XV);
#ifdef HAVE_XSHM
_methods = (VideoMethod)(_methods | METHOD_XVSHM);
#endif
}
#ifdef HAVE_GL
if (TQGLFormat::hasOpenGL()) {
_methods = (VideoMethod)(_methods | METHOD_GL);
}
#endif
d->gc = XCreateGC(_w->x11Display(), _w->winId(), 0, NULL);
}
QVideoStream::~QVideoStream()
{
deInit();
XFreeGC(_w->x11Display(), d->gc);
delete d;
}
void QVideoStream::deInit()
{
if (!_init)
return;
_init = false;
_format = FORMAT_NONE;
Q_ASSERT(_methods & _method);
if (!(_methods & _method))
return;
switch (_method) {
case METHOD_XSHM:
#ifdef HAVE_XSHM
XShmDetach(_w->x11Display(), &(d->shmh));
XDestroyImage(d->xim);
d->xim = 0;
shmdt(d->shmh.shmaddr);
#endif
break;
case METHOD_X11:
delete[] d->xim->data;
d->xim->data = 0;
XDestroyImage(d->xim);
d->xim = 0;
break;
case METHOD_XVSHM:
case METHOD_XV:
delete d->xvHandle;
d->xvHandle = 0;
break;
case METHOD_GL:
#ifdef HAVE_GL
delete d->glwidget;
#endif
break;
default:
Q_ASSERT(0);
return;
}
}
void QVideoStream::init()
{
Q_ASSERT(_methods & _method);
if (!(_methods & _method))
return;
switch (_method) {
case METHOD_XSHM:
{
#ifdef HAVE_XSHM
if ( !_inputSize.isValid() ) {
kdWarning() << "QVideoStream::init() (XSHM): Unable to initialize due to invalid input size." << endl;
return;
}
memset(&(d->shmh), 0, sizeof(XShmSegmentInfo));
d->xim = XShmCreateImage(_w->x11Display(),
(Visual*)_w->x11Visual(),
_w->x11Depth(),
ZPixmap, 0, &(d->shmh),
_inputSize.width(),
_inputSize.height());
d->shmh.shmid = shmget(IPC_PRIVATE,
d->xim->bytes_per_line*d->xim->height,
IPC_CREAT|0600);
d->shmh.shmaddr = (char *)shmat(d->shmh.shmid, 0, 0);
d->xim->data = (char*)d->shmh.shmaddr;
d->shmh.readOnly = False;
Status s = XShmAttach(_w->x11Display(), &(d->shmh));
if (s) {
XSync(_w->x11Display(), False);
shmctl(d->shmh.shmid, IPC_RMID, 0);
_format = _xFormat;
_init = true;
} else {
kdWarning() << "XShmAttach failed!" << endl;
XDestroyImage(d->xim);
d->xim = 0;
shmdt(d->shmh.shmaddr);
}
#endif
}
break;
case METHOD_X11:
if ( !_inputSize.isValid() ) {
kdWarning() << "QVideoStream::init() (X11): Unable to initialize due to invalid input size." << endl;
return;
}
d->xim = XCreateImage(_w->x11Display(),
(Visual*)TQT_TQPAINTDEVICE(_w)->x11Visual(),
_w->x11Depth(),
ZPixmap, 0, 0,
_inputSize.width(),
_inputSize.height(),
32, 0);
d->xim->data = new char[d->xim->bytes_per_line*_inputSize.height()];
_format = _xFormat;
_init = true;
break;
case METHOD_XVSHM:
case METHOD_XV:
{
if (d->xvHandle)
delete d->xvHandle;
d->xvHandle = KXv::connect(_w->winId());
KXvDeviceList& xvdl(d->xvHandle->devices());
KXvDevice *xvdev = NULL;
for (xvdev = xvdl.first(); xvdev; xvdev = xvdl.next()) {
if (xvdev->isImageBackend() &&
xvdev->supportsWidget(_w)) {
d->xvdev = xvdev;
d->xvdev->useShm(_method == METHOD_XVSHM);
_format = FORMAT_YUYV;
_init = true;
break;
}
}
if (!_init) {
delete d->xvHandle;
d->xvHandle = 0;
}
}
break;
case METHOD_GL:
#ifdef HAVE_GL
d->glwidget = new QVideoStreamGLWidget(_w, "QVideoStreamGLWidget");
d->glwidget->resize(_w->width(), _w->height());
d->glwidget->show();
_format = FORMAT_BGR24;
_init = true;
#endif
break;
default:
Q_ASSERT(0);
return;
}
}
bool QVideoStream::haveMethod(VideoMethod method) const
{
return _methods & method;
}
QVideo::VideoMethod QVideoStream::method() const
{
return _method;
}
QVideo::VideoMethod QVideoStream::setMethod(VideoMethod method)
{
if (_methods & method) {
deInit();
_method = method;
init();
}
return _method;
}
TQSize QVideoStream::maxSize() const
{
return _size;
}
int QVideoStream::maxWidth() const
{
return _size.width();
}
int QVideoStream::maxHeight() const
{
return _size.height();
}
TQSize QVideoStream::size() const
{
return _size;
}
int QVideoStream::width() const
{
return _size.width();
}
int QVideoStream::height() const
{
return _size.height();
}
TQSize QVideoStream::setSize(const TQSize& sz)
{
_size = sz;
return _size;
}
int QVideoStream::setWidth(int width)
{
if (width < 0)
width = 0;
if (width > maxWidth())
width = maxWidth();
_size.setWidth(width);
return _size.width();
}
int QVideoStream::setHeight(int height)
{
if (height < 0)
height = 0;
if (height > maxHeight())
height = maxHeight();
_size.setHeight(height);
return _size.height();
}
TQSize QVideoStream::inputSize() const
{
return _inputSize;
}
int QVideoStream::inputWidth() const
{
return _inputSize.width();
}
int QVideoStream::inputHeight() const
{
return _inputSize.height();
}
TQSize QVideoStream::setInputSize(const TQSize& sz)
{
if (sz == _inputSize)
return _inputSize;
_inputSize = sz;
if (_method & (METHOD_XSHM | METHOD_X11)) {
deInit();
init();
}
#ifdef HAVE_GL
if (_method & METHOD_GL) {
d->glwidget->setInputSize(_inputSize);
}
#endif
return _inputSize;
}
int QVideoStream::setInputWidth(int width)
{
if (width == _inputSize.width())
return _inputSize.width();
_inputSize.setWidth(width);
if (_method & (METHOD_XSHM | METHOD_X11)) {
deInit();
init();
}
#ifdef HAVE_GL
if (_method & METHOD_GL) {
d->glwidget->setInputSize(_inputSize);
}
#endif
return _inputSize.width();
}
int QVideoStream::setInputHeight(int height)
{
if (height == _inputSize.height())
return _inputSize.height();
_inputSize.setHeight(height);
if (_method & (METHOD_XSHM | METHOD_X11)) {
deInit();
init();
}
#ifdef HAVE_GL
if (_method & METHOD_GL) {
d->glwidget->setInputSize(_inputSize);
}
#endif
return _inputSize.height();
}
bool QVideoStream::supportsFormat(VideoMethod method, ImageFormat format)
{
return (bool)(formatsForMethod(method) & format);
}
QVideo::ImageFormat QVideoStream::formatsForMethod(VideoMethod method)
{
switch(method) {
case METHOD_XSHM:
case METHOD_X11:
return _xFormat;
case METHOD_XV:
case METHOD_XVSHM:
return FORMAT_YUYV;
case METHOD_GL:
return FORMAT_BGR24;
default:
return FORMAT_NONE;
}
}
QVideo::ImageFormat QVideoStream::format() const
{
return _format;
}
bool QVideoStream::setFormat(ImageFormat format)
{
if(supportsFormat(_method, format)) {
_format = format;
return true;
} else {
return false;
}
}
int QVideoStream::displayFrame(const unsigned char *const img)
{
return displayFrame(img, 0, 0, _inputSize.width(), _inputSize.height());
}
int QVideoStream::displayFrame(const unsigned char *const img, int x, int y, int sw, int sh)
{
Q_ASSERT(_init);
if (!_init)
return -1;
Q_ASSERT(_methods & _method);
if (!(_methods & _method))
return -1;
switch (_method) {
case METHOD_XV:
case METHOD_XVSHM:
return d->xvdev->displayImage(_w, img,
_inputSize.width(), _inputSize.height(), x, y, sw, sh,
_size.width(), _size.height());
break;
case METHOD_XSHM:
#ifdef HAVE_XSHM
memcpy(d->xim->data,img,d->xim->bytes_per_line*d->xim->height);
XShmPutImage(_w->x11Display(), _w->winId(), d->gc, d->xim,
x, y,
0, 0,
sw, sh,
0);
XSync(_w->x11Display(), False);
break;
#else
return -1;
#endif
case METHOD_X11:
memcpy(d->xim->data, img, d->xim->bytes_per_line*d->xim->height);
XPutImage(_w->x11Display(), _w->winId(), d->gc, d->xim,
x, y,
0, 0,
sw, sh);
XSync(_w->x11Display(), False);
break;
case METHOD_GL:
#ifdef HAVE_GL
d->glwidget->display(img, x, y, sw, sh);
#endif
break;
default:
Q_ASSERT(0);
return -1;
}
return 0;
}
QVideoStream& QVideoStream::operator<<(const unsigned char *const img)
{
displayFrame(img);
return *this;
}
// ---------------------------------------------------------------------------------------
#ifdef HAVE_GL
QVideoStreamGLWidget::QVideoStreamGLWidget(TQWidget* parent, const char* name)
: TQGLWidget(TQGLFormat(TQGL::DoubleBuffer | TQGL::Rgba | TQGL::DirectRendering), parent, name),
_tex(0),
_w(parent),
_glfun(false)
{
kdDebug() << "QVideoStreamGLWidget::QVideoStreamGLWidget()" << endl;
connect(_w, TQT_SIGNAL(resized(int, int)),
this, TQT_SLOT(resize(int, int)));
topLevelWidget()->installEventFilter(this);
_glfunTimer = new TQTimer();
}
QVideoStreamGLWidget::~QVideoStreamGLWidget()
{
kdDebug() << "QVideoStreamGLWidget::~QVideoStreamGLWidget()" << endl;
delete _glfunTimer;
makeCurrent();
if(_tex != 0) {
glDeleteTextures(1, &_tex);
}
}
bool QVideoStreamGLWidget::eventFilter(TQObject*, TQEvent* e)
{
// For some reason, KeyPress does not work (yields 2), TQEvent::KeyPress is unknown... What the f...????
// I am too lazy to scan the header files for the reason.
if(e->type() == 6) {
TQKeyEvent* ke = static_cast<TQKeyEvent*>(e);
if(ke->key() == TQt::Key_Pause) {
_glfunTimer->start(500, true);
} else if (_glfunTimer->isActive() && (ke->key() == TQt::Key_Escape)) {
_glfun = !_glfun;
}
}
return false;
}
void QVideoStreamGLWidget::initializeGL()
{
kdDebug() << "QVideoStreamGLWidget::initializeGL()" << endl;
setAutoBufferSwap(false);
TQGLFormat f = format();
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_maxGL);
kdDebug() << "OpenGL capabilities (* = required):" << endl;
kdDebug() << " Valid context*: " << isValid() << endl;
kdDebug() << " DoubleBuffer*: " << f.doubleBuffer() << endl;
kdDebug() << " Depth: " << f.depth() << endl;
kdDebug() << " RGBA*: " << f.rgba() << endl;
kdDebug() << " Alpha: " << f.alpha() << endl;
kdDebug() << " Accum: " << f.accum() << endl;
kdDebug() << " Stencil: " << f.stencil() << endl;
kdDebug() << " Stereo: " << f.stereo() << endl;
kdDebug() << " DirectRendering*: " << f.directRendering() << endl;
kdDebug() << " Overlay: " << f.hasOverlay() << endl;
kdDebug() << " Plane: " << f.plane() << endl;
kdDebug() << " MAX_TEXTURE_SIZE: " << _maxGL << endl;
qglClearColor(TQt::black);
glShadeModel(GL_FLAT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
_vul = TQPoint( 4, 10);
_vur = TQPoint(-8, 4);
_vll = TQPoint(10, -4);
_vlr = TQPoint(-8, -10);
}
void QVideoStreamGLWidget::resizeGL(int w, int h)
{
_sz = TQSize(w, h);
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, w, 0.0, h, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
_ul = TQPoint(0, 0);
_ur = TQPoint(w, 0);
_ll = TQPoint(0, h);
_lr = TQPoint(w, h);
}
void QVideoStreamGLWidget::setInputSize(const TQSize& sz)
{
makeCurrent();
_inputSize = sz;
int iw = _inputSize.width();
int ih = _inputSize.height();
if ( (iw > _maxGL) || (ih > _maxGL) ) {
kdWarning() << "QVideoStreamGLWidget::setInputSize(): Texture too large! maxGL: " << _maxGL << endl;
return;
}
// textures have power-of-two x,y dimensions
int i;
for (i = 0; iw >= (1 << i); i++)
;
_tw = (1 << i);
for (i = 0; ih >= (1 << i); i++)
;
_th = (1 << i);
// Generate texture
if(_tex != 0) {
glDeleteTextures(1, &_tex);
}
glGenTextures(1, &_tex);
glBindTexture(GL_TEXTURE_2D, _tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Blank texture
char* dummy = new char[_tw*_th*4];
memset(dummy, 128, _tw*_th*4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, _tw, _th, 0,
GL_RGB, GL_UNSIGNED_BYTE, dummy);
delete[] dummy;
}
void QVideoStreamGLWidget::display(const unsigned char *const img, int x, int y, int sw, int sh)
{
makeCurrent();
// FIXME: Endianess - also support GL_RGB
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _inputSize.width(), _inputSize.height(),
GL_BGR, GL_UNSIGNED_BYTE, img);
// upper right coords
float ur_x = (float)(x + sw) / _tw;
float ur_y = (float)(y + sh) / _th;
// lower left coords
float ll_x = (float)(x) / _tw;
float ll_y = (float)(y) / _th;
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, _tex);
if (!_glfun) {
glBegin(GL_QUADS);
glTexCoord2f(ll_x, ur_y); glVertex2i(0, 0 );
glTexCoord2f(ll_x, ll_y); glVertex2i(0, _sz.height());
glTexCoord2f(ur_x, ll_y); glVertex2i(_sz.width(), _sz.height());
glTexCoord2f(ur_x, ur_y); glVertex2i(_sz.width(), 0 );
glEnd();
} else {
calc(_ul, _vul);
calc(_ur, _vur);
calc(_ll, _vll);
calc(_lr, _vlr);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glTexCoord2f(0, y); glVertex2i(_ul.x(), _ul.y());
glTexCoord2f(0, 0); glVertex2i(_ll.x(), _ll.y());
glTexCoord2f(x, 0); glVertex2i(_lr.x(), _lr.y());
glTexCoord2f(x, y); glVertex2i(_ur.x(), _ur.y());
glEnd();
}
swapBuffers();
glDisable(GL_TEXTURE_2D);
}
void QVideoStreamGLWidget::calc(TQPoint& p, TQPoint& v)
{
p += v;
if(p.x() < 0) {
p.setX(-p.x());
v.setX(-v.x());
}
if(p.y() < 0) {
p.setY(-p.y());
v.setY(-v.y());
}
if(p.x() > _sz.width()) {
p.setX(_sz.width() - (p.x() - _sz.width()));
v.setX(-v.x());
}
if(p.y() > _sz.height()) {
p.setY(_sz.height() - (p.y() - _sz.height()));
v.setY(-v.y());
}
}
#endif
#include "qvideostream.moc"
|