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
|
/*
* KDE Xv interface
*
* Copyright (C) 2001 George Staikos (staikos@kde.org)
*
* 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 <assert.h>
#include <tqwindowdefs.h>
#include <tqwidget.h>
#include <kdebug.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "kxv.h"
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/StringDefs.h>
#include <X11/Xatom.h>
#ifdef HAVE_XSHM
extern "C" {
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
}
#endif
#ifdef HAVE_LIBXV
#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>
#endif
#ifdef HAVE_LIBXVMC
#include <X11/extensions/XvMC.h>
#include <X11/extensions/XvMClib.h>
#endif
KXv::KXv()
{
xv_adaptors = 0;
_devs.setAutoDelete(true);
}
KXv::~KXv()
{
kdDebug() << "KXv::~KXv: Close Xv connection." << endl;
_devs.clear();
#ifdef HAVE_LIBXV
if (xv_adaptors > 0)
XvFreeAdaptorInfo((XvAdaptorInfo *)xv_adaptor_info);
#endif
}
KXvDeviceList& KXv::devices()
{
return _devs;
}
bool KXv::haveXv()
{
#ifndef HAVE_LIBXV
return false;
#else
unsigned int tmp;
if (Success != XvQueryExtension(qt_xdisplay(),
&tmp,
&tmp,
&tmp,
&tmp,
&tmp))
return false;
return true;
#endif
}
KXv* KXv::connect(Drawable d)
{
KXv *xvptr;
xvptr = new KXv;
if (!xvptr->init(d)) {
kdDebug() << "KXv::connect: Xv init failed." << endl;
delete xvptr;
return NULL;
}
kdDebug() << "KXv::connect: Xv init completed." << endl;
return xvptr;
}
bool KXv::init(Drawable d)
{
#ifndef HAVE_LIBXV
return false;
#else
if (Success != XvQueryExtension(qt_xdisplay(),
&xv_version,
&xv_release,
&xv_request,
&xv_event,
&xv_error)) {
kdWarning() << "KXv::init: Xv extension not available." << endl;
return false;
}
#ifdef HAVE_LIBXVMC
// Causes crashes for some people.
// if (Success == XvMCQueryExtension(qt_xdisplay(),0,0)) {
// kdDebug() << "Found XvMC!" << endl;
// }
#endif
if (Success != XvQueryAdaptors(qt_xdisplay(),
d,
&xv_adaptors,
(XvAdaptorInfo **)&xv_adaptor_info)) {
// Note technically fatal... what to do?
kdWarning() << "KXv::init: XvQueryAdaptors failed." << endl;
}
XvAdaptorInfo *ai = (XvAdaptorInfo *)xv_adaptor_info;
for (unsigned int i = 0; i < xv_adaptors; i++) {
KXvDevice *xvd = new KXvDevice;
xvd->xv_type = ai[i].type;
xvd->xv_port = ai[i].base_id;
xvd->xv_name = ai[i].name;
xvd->xv_adaptor = i;
xvd->xv_nvisualformats = ai[i].num_formats;
xvd->xv_visualformats = ai[i].formats;
if (ai[i].type & XvInputMask &&
ai[i].type & XvVideoMask ) {
kdDebug() << "KXv::init: Xv VideoMask port " << ai[i].base_id << " was found."
<< " Device is: " << ai[i].name << "." << endl;
}
if (ai[i].type & XvInputMask &&
ai[i].type & XvImageMask ) {
kdDebug() << "KXv::init: Xv ImageMask port " << ai[i].base_id << " was found."
<< " Device is: " << ai[i].name << "." << endl;
}
if (xvd->init()) {
_devs.append(xvd);
} else {
delete xvd;
}
}
return true;
#endif
}
bool KXvDevice::grabStill(TQImage* /*pix*/, int /*dw*/, int /*dh*/)
{
#ifndef HAVE_LIBXV
return false;
#else
return false;
#endif
}
int KXvDevice::displayImage(TQWidget *widget, const unsigned char *const data, int w, int h, int dw, int dh)
{
if (!widget)
return -1;
return displayImage(widget->winId(), data, w, h, 0, 0, w, h, dw, dh);
}
int KXvDevice::displayImage(TQWidget *widget, const unsigned char *const data, int w, int h, int x, int y, int sw, int sh, int dw, int dh)
{
if (!widget)
return -1;
return displayImage(widget->winId(), data, w, h, x, y, sw, sh, dw, dh);
}
int KXvDevice::displayImage(Window win, const unsigned char *const data, int w, int h, int dw, int dh)
{
return displayImage(win, data, w, h, 0, 0, w, h, dw, dh);
}
int KXvDevice::displayImage(Window win, const unsigned char *const data, int w, int h, int x, int y, int sw, int sh, int dw, int dh)
{
#ifndef HAVE_LIBXV
return -1;
#else
Q_ASSERT(xv_port != -1);
// Must be a video capable device!
if (!(xv_type & XvImageMask) || !(xv_type & XvInputMask)) {
kdWarning() << "KXvDevice::displayImage: This is not a video capable device." << endl;
return -1;
}
if (xv_image_w != w || xv_image_h != h || !xv_image)
rebuildImage(w, h, _shm);
if (!xv_image)
return -1;
if (win != xv_last_win && xv_gc) {
XFreeGC(qt_xdisplay(), xv_gc);
xv_gc = 0;
}
if (!xv_gc) {
xv_last_win = win;
xv_gc = XCreateGC(qt_xdisplay(), win, 0, NULL);
}
int rc = 0;
Q_ASSERT(xv_image);
if (!_shm) {
static_cast<XvImage*>(xv_image)->data =
(char *)const_cast<unsigned char*>(data);
rc = XvPutImage(qt_xdisplay(), xv_port, win, xv_gc,
static_cast<XvImage*>(xv_image), x, y, sw, sh, 0, 0, dw, dh);
} else {
#ifdef HAVE_XSHM
memcpy(static_cast<XvImage*>(xv_image)->data, data, static_cast<XvImage*>(xv_image)->data_size);
rc = XvShmPutImage(qt_xdisplay(), xv_port, win, xv_gc,
static_cast<XvImage*>(xv_image), x, y, sw, sh, 0, 0, dw, dh, 0);
#endif
}
XSync(qt_xdisplay(), False);
return rc;
#endif
}
bool KXvDevice::startVideo(TQWidget *w, int dw, int dh)
{
if (!w) return false;
return startVideo(w->winId(), dw, dh);
}
bool KXvDevice::startVideo(Window w, int dw, int dh)
{
#ifndef HAVE_LIBXV
return false;
#else
int sx = 0, sy = 0, dx = 0, dy = 0, sw = dw, sh = dh;
// Must be a video capable device!
if (!(xv_type & XvVideoMask) || !(xv_type & XvInputMask)) {
kdWarning() << "KXvDevice::startVideo: This is not a video capable device." << endl;
return false;
}
if (videoStarted) stopVideo();
if (xv_port == -1) {
kdWarning() << "KXvDevice::startVideo: No xv_port." << endl;
return false;
}
if (w != xv_last_win && xv_gc) {
XFreeGC(qt_xdisplay(), xv_gc);
xv_gc = 0;
}
if (!xv_gc) {
xv_last_win = w;
xv_gc = XCreateGC(qt_xdisplay(), w, 0, NULL);
}
if (-1 != xv_encoding) {
sw = ((XvEncodingInfo *)xv_encoding_info)[xv_encoding].width;
sh = ((XvEncodingInfo *)xv_encoding_info)[xv_encoding].height;
}
// xawtv does this here:
// ng_ratio_fixup(&dw, &dh, &dx, &dy);
kdDebug() << "XvPutVideo: " << qt_xdisplay()
<< " " << xv_port << " " << w << " " << xv_gc
<< " " << sx << " " << sy << " " << sw << " " << sh
<< " " << dx << " " << dy << " " << dw << " " << dh << endl;
XvPutVideo(qt_xdisplay(), xv_port, w, xv_gc, sx, sy, sw, sh, dx, dy, dw, dh);
videoStarted = true;
videoWindow = w;
return true;
#endif
}
bool KXvDevice::stopVideo()
{
#ifndef HAVE_LIBXV
return false;
#else
if (!videoStarted)
return true;
if (xv_port == -1) {
kdWarning() << "KXvDevice::stopVideo: No xv_port." << endl;
return false;
}
XvStopVideo(qt_xdisplay(), xv_port, videoWindow);
videoStarted = false;
return true;
#endif
}
KXvDevice::KXvDevice()
{
xv_encoding_info = NULL;
xv_formatvalues = NULL;
xv_attr = NULL;
xv_port = -1;
xv_encoding = -1;
xv_name = TQString();
xv_type = -1;
xv_adaptor = -1;
_shm = false;
#ifdef HAVE_LIBXV
xv_imageformat = 0x32595559; // FIXME (YUY2)
#ifdef HAVE_XSHM
if (!XShmQueryExtension(qt_xdisplay())) {
_haveShm = false;
} else {
_shm = true;
_haveShm = true;
}
xv_shminfo = new XShmSegmentInfo;
#else
xv_shminfo = 0;
#endif
#endif
xv_gc = 0;
xv_last_win = 0;
videoStarted = false;
_attrs.setAutoDelete(true);
xv_image = 0;
xv_image_w = 320;
xv_image_h = 200;
}
KXvDevice::~KXvDevice()
{
#ifdef HAVE_LIBXV
_attrs.clear();
if (videoStarted) stopVideo();
if (xv_encoding_info)
XvFreeEncodingInfo((XvEncodingInfo *)xv_encoding_info);
XFree(xv_formatvalues);
XFree(xv_attr);
#ifdef HAVE_XSHM
delete (XShmSegmentInfo*)xv_shminfo;
#endif
destroyImage();
#endif
if (xv_gc)
XFreeGC(qt_xdisplay(), xv_gc);
#ifdef HAVE_LIBXV
if (xv_port != -1)
XvUngrabPort(qt_xdisplay(), xv_port, CurrentTime);
#endif
}
bool KXvDevice::init()
{
#ifndef HAVE_LIBXV
return false;
#else
assert(xv_port != -1); // make sure we were prepped by KXv already.
if (XvGrabPort(qt_xdisplay(), xv_port, CurrentTime)) {
kdWarning() << "KXvDevice::init(): Unable to grab Xv port." << endl;
return false;
}
if (Success != XvQueryEncodings(qt_xdisplay(),
xv_port,
&xv_encodings,
(XvEncodingInfo **)&xv_encoding_info)) {
kdWarning() << "KXvDevice::init: Xv QueryEncodings failed. Dropping Xv support for this device." << endl;
return false;
}
// Package the encodings up nicely
for (unsigned int i = 0; i < xv_encodings; i++) {
//kdDebug() << "Added encoding: " << ((XvEncodingInfo *)xv_encoding_info)[i].name << endl;
_encodingList << ((XvEncodingInfo *)xv_encoding_info)[i].name;
}
xv_attr = XvQueryPortAttributes(qt_xdisplay(),
xv_port,
&xv_encoding_attributes);
XvAttribute *xvattr = (XvAttribute *)xv_attr;
kdDebug() << "Attributes for port " << xv_port << endl;
for (int i = 0; i < xv_encoding_attributes; i++) {
assert(xvattr);
kdDebug() << " -> " << xvattr[i].name
<< ((xvattr[i].flags & XvGettable) ? " get" : "")
<< ((xvattr[i].flags & XvSettable) ? " set" : "")
<< " Range: " << xvattr[i].min_value
<< " -> " << xvattr[i].max_value << endl;
KXvDeviceAttribute *xvda = new KXvDeviceAttribute;
xvda->name = xvattr[i].name;
xvda->min = xvattr[i].min_value;
xvda->max = xvattr[i].max_value;
xvda->flags = xvattr[i].flags;
_attrs.append(xvda);
}
XvImageFormatValues *fo;
fo = XvListImageFormats(qt_xdisplay(), xv_port, &xv_formats);
xv_formatvalues = (void *)fo;
kdDebug() << "Image formats for port " << xv_port << endl;
for (int i = 0; i < xv_formats; i++) {
assert(fo);
TQString imout;
imout.sprintf(" 0x%x (%c%c%c%c) %s",
fo[i].id,
fo[i].id & 0xff,
(fo[i].id >> 8) & 0xff,
(fo[i].id >> 16) & 0xff,
(fo[i].id >> 24) & 0xff,
((fo[i].format == XvPacked) ?
"Packed" : "Planar"));
kdDebug() << imout << endl;
}
kdDebug() << "Disabling double buffering." << endl;
setAttribute("XV_DOUBLE_BUFFER", 0);
return true;
#endif
}
bool KXvDevice::supportsWidget(TQWidget *w)
{
#ifndef HAVE_LIBXV
return false;
#else
for (int i = 0; i < xv_nvisualformats; i++) {
if (static_cast<XvFormat*>(xv_visualformats)[i].visual_id
== static_cast<Visual*>(w->x11Visual())->visualid) {
return true;
}
}
return false;
#endif
}
bool KXvDevice::isVideoSource()
{
#ifndef HAVE_LIBXV
return false;
#else
if (xv_type & XvVideoMask && xv_type & XvInputMask)
return true;
return false;
#endif
}
bool KXvDevice::isImageBackend()
{
#ifndef HAVE_LIBXV
return false;
#else
if (xv_type & XvImageMask && xv_type & XvInputMask)
return true;
return false;
#endif
}
const KXvDeviceAttributes& KXvDevice::attributes()
{
return _attrs;
}
bool KXvDevice::getAttributeRange(const TQString& attribute, int *min, int *max)
{
for (KXvDeviceAttribute *at = _attrs.first(); at != NULL; at = _attrs.next()) {
if (at->name == attribute) {
if (min) *min = at->min;
if (max) *max = at->max;
return true;
}
}
return false;
}
bool KXvDevice::getAttribute(const TQString& attribute, int *val)
{
#ifndef HAVE_LIBXV
return false;
#else
for (KXvDeviceAttribute *at = _attrs.first(); at != NULL; at = _attrs.next()) {
if (at->name == attribute) {
if (val)
XvGetPortAttribute(qt_xdisplay(), xv_port, at->atom(), val);
return true;
}
}
return false;
#endif
}
bool KXvDevice::setAttribute(const TQString& attribute, int val)
{
#ifndef HAVE_LIBXV
return false;
#else
for (KXvDeviceAttribute *at = _attrs.first(); at != NULL; at = _attrs.next()) {
if (at->name == attribute) {
XvSetPortAttribute(qt_xdisplay(), xv_port, at->atom(), val);
XSync(qt_xdisplay(), False);
return true;
}
}
return false;
#endif
}
const TQString& KXvDevice::name() const
{
return xv_name;
}
int KXvDevice::port() const
{
return xv_port;
}
const TQStringList& KXvDevice::encodings() const
{
return _encodingList;
}
bool KXvDevice::encoding(TQString& encoding)
{
#ifndef HAVE_LIBXV
return false;
#else
XvEncodingID enc;
for (KXvDeviceAttribute *at = _attrs.first(); at != 0L; at = _attrs.next()) {
if (at->name == "XV_ENCODING") {
XvGetPortAttribute(qt_xdisplay(), xv_port, at->atom(), (int*)&enc);
kdDebug() << "KXvDevice: encoding: " << enc << endl;
encoding = enc;
return true;
}
}
return false;
#endif
}
bool KXvDevice::setEncoding(const TQString& e)
{
#ifdef HAVE_LIBXV
for (unsigned int i = 0; i < xv_encodings; i++) {
if (e == ((XvEncodingInfo *)xv_encoding_info)[i].name) {
xv_encoding = i;
return setAttribute("XV_ENCODING",
((XvEncodingInfo *)xv_encoding_info)[i].encoding_id);
}
}
#endif
return false;
}
bool KXvDevice::videoPlaying() const
{
return videoStarted;
}
bool KXvDevice::useShm(bool on)
{
#ifndef HAVE_XSHM
if (on) {
return false;
}
#endif
if (!_haveShm) {
return false;
}
if (_shm != on)
rebuildImage(xv_image_w, xv_image_h, on);
if (_haveShm) // This can change in rebuildImage()
_shm = on;
return _shm;
}
bool KXvDevice::usingShm() const
{
return _shm;
}
#include <unistd.h>
void KXvDevice::rebuildImage(int w, int h, bool shm)
{
if (xv_image) {
destroyImage();
}
#ifdef HAVE_LIBXV
if (!shm) {
xv_image = (void*)XvCreateImage(qt_xdisplay(), xv_port, xv_imageformat,
0, w, h);
if (!xv_image) {
kdWarning() << "KXvDevice::rebuildImage: XvCreateImage failed." << endl;
}
} else {
#ifdef HAVE_XSHM
memset(xv_shminfo, 0, sizeof(XShmSegmentInfo));
xv_image = (void*)XvShmCreateImage(qt_xdisplay(), xv_port, xv_imageformat,
0, w, h, static_cast<XShmSegmentInfo*>(xv_shminfo));
if (!xv_image) {
kdWarning() << "KXvDevice::rebuildImage: Error using SHM with Xv! Disabling SHM..." << endl;
_haveShm = false;
_shm = false;
xv_image = (void*)XvCreateImage(qt_xdisplay(), xv_port, xv_imageformat,
0, w, h);
if (!xv_image) {
kdWarning() << "KXvDevice::rebuildImage: XvCreateImage failed." << endl;
}
} else {
static_cast<XShmSegmentInfo*>(xv_shminfo)->shmid =
shmget(IPC_PRIVATE,
static_cast<XvImage*>(xv_image)->data_size,
IPC_CREAT | 0600);
static_cast<XShmSegmentInfo*>(xv_shminfo)->shmaddr =
(char*)shmat(static_cast<XShmSegmentInfo*>(xv_shminfo)->shmid, 0, 0);
static_cast<XShmSegmentInfo*>(xv_shminfo)->readOnly = True;
static_cast<XvImage*>(xv_image)->data =
static_cast<XShmSegmentInfo*>(xv_shminfo)->shmaddr;
XShmAttach(qt_xdisplay(), static_cast<XShmSegmentInfo*>(xv_shminfo));
XSync(qt_xdisplay(), False);
shmctl(static_cast<XShmSegmentInfo*>(xv_shminfo)->shmid, IPC_RMID, 0);
}
#endif
}
Q_ASSERT(xv_image != 0);
xv_image_w = w;
xv_image_h = h;
#endif
}
void KXvDevice::destroyImage()
{
#ifdef HAVE_LIBXV
if (!_shm) {
if (xv_image) {
static_cast<XvImage*>(xv_image)->data = 0;
}
} else {
if (xv_image) {
#ifdef HAVE_XSHM
shmdt(static_cast<XShmSegmentInfo*>(xv_shminfo)->shmaddr);
#endif
}
}
XFree(xv_image);
xv_image = 0;
#endif
}
Atom KXvDeviceAttribute::atom()
{
return XInternAtom(qt_xdisplay(), name.latin1(), False);
}
|