summaryrefslogtreecommitdiffstats
path: root/xorg/server/module/rdpXv.c
blob: dc6d5418d4161f4535a6453be998144ab45488b3 (plain)
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
/*
Copyright 2014 Jay Sorg

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

XVideo

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* this should be before all X11 .h files */
#include <xorg-server.h>
#include <xorgVersion.h>

/* all driver need this */
#include <xf86.h>
#include <xf86_OSproc.h>

#include <xf86xv.h>
#include <X11/extensions/Xv.h>
#include <fourcc.h>

#include "rdp.h"
#include "rdpMisc.h"
#include "rdpReg.h"
#include "rdpClientCon.h"

#define LOG_LEVEL 1
#define LLOGLN(_level, _args) \
    do { if (_level < LOG_LEVEL) { ErrorF _args ; ErrorF("\n"); } } while (0)

/* use simd, run time */
int g_xv_use_accel = 1;

/* use simd, compile time, if zero, g_xv_use_accel does not matter */
#define XV_USE_ACCEL 0

#define T_NUM_ENCODINGS 1
static XF86VideoEncodingRec g_xrdpVidEncodings[T_NUM_ENCODINGS] =
{ { 0, "XV_IMAGE", 2046, 2046, { 1, 1 } } };

#define T_NUM_FORMATS 1
static XF86VideoFormatRec g_xrdpVidFormats[T_NUM_FORMATS] =
{ { 0, TrueColor } };

/* YV12
   I420
   12 bpp planar
   YUV 4:2:0 8 bit Y plane followed by 8 bit 2x2 subsampled
   U and V planes. */

/* YUY2
   UYVY
   16 bpp packed
   YUV 4:2:2 Y sample at every pixel, U and V sampled at
   every second pixel */

/* XVIMAGE_YV12 FOURCC_YV12 0x32315659 */
/* XVIMAGE_I420 FOURCC_I420 0x30323449 */
/* XVIMAGE_YUY2 FOURCC_YUY2 0x32595559 */
/* XVIMAGE_UYVY FOURCC_UYVY 0x59565955 */

static XF86ImageRec g_xrdpVidImages[] =
{ XVIMAGE_YV12, XVIMAGE_I420, XVIMAGE_YUY2, XVIMAGE_UYVY };

#define T_MAX_PORTS 1

/*****************************************************************************/
static int
xrdpVidPutVideo(ScrnInfoPtr pScrn, short vid_x, short vid_y,
                short drw_x, short drw_y, short vid_w, short vid_h,
                short drw_w, short drw_h, RegionPtr clipBoxes,
                pointer data, DrawablePtr pDraw)
{
    LLOGLN(0, ("xrdpVidPutVideo:"));
    return Success;
}

/*****************************************************************************/
static int
xrdpVidPutStill(ScrnInfoPtr pScrn, short vid_x, short vid_y,
                short drw_x, short drw_y, short vid_w, short vid_h,
                short drw_w, short drw_h, RegionPtr clipBoxes,
                pointer data, DrawablePtr pDraw)
{
    LLOGLN(0, ("xrdpVidPutStill:"));
    return Success;
}

/*****************************************************************************/
static int
xrdpVidGetVideo(ScrnInfoPtr pScrn, short vid_x, short vid_y,
                short drw_x, short drw_y, short vid_w, short vid_h,
                short drw_w, short drw_h, RegionPtr clipBoxes,
                pointer data, DrawablePtr pDraw)
{
    LLOGLN(0, ("xrdpVidGetVideo:"));
    return Success;
}

/*****************************************************************************/
static int
xrdpVidGetStill(ScrnInfoPtr pScrn, short vid_x, short vid_y,
                short drw_x, short drw_y, short vid_w, short vid_h,
                short drw_w, short drw_h, RegionPtr clipBoxes,
                pointer data, DrawablePtr pDraw)
{
    LLOGLN(0, ("FBDevTIVidGetStill:"));
    return Success;
}

/*****************************************************************************/
static void
xrdpVidStopVideo(ScrnInfoPtr pScrn, pointer data, Bool Cleanup)
{
    LLOGLN(0, ("xrdpVidStopVideo:"));
}

/*****************************************************************************/
static int
xrdpVidSetPortAttribute(ScrnInfoPtr pScrn, Atom attribute,
                        INT32 value, pointer data)
{
    LLOGLN(0, ("xrdpVidSetPortAttribute:"));
    return Success;
}

/*****************************************************************************/
static int
xrdpVidGetPortAttribute(ScrnInfoPtr pScrn, Atom attribute,
                        INT32 *value, pointer data)
{
    LLOGLN(0, ("xrdpVidGetPortAttribute:"));
    return Success;
}

/*****************************************************************************/
static void
xrdpVidQueryBestSize(ScrnInfoPtr pScrn, Bool motion,
                     short vid_w, short vid_h, short drw_w, short drw_h,
                     unsigned int *p_w, unsigned int *p_h, pointer data)
{
    LLOGLN(0, ("xrdpVidQueryBestSize:"));
}

/*****************************************************************************/
static int
YV12_to_RGB32(unsigned char *yuvs, int width, int height, int *rgbs)
{
    int size_total;
    int y;
    int u;
    int v;
    int c;
    int d;
    int e;
    int r;
    int g;
    int b;
    int t;
    int i;
    int j;

    size_total = width * height;
    for (j = 0; j < height; j++)
    {
        for (i = 0; i < width; i++)
        {
            y = yuvs[j * width + i];
            u = yuvs[(j / 2) * (width / 2) + (i / 2) + size_total];
            v = yuvs[(j / 2) * (width / 2) + (i / 2) + size_total + (size_total / 4)];
            c = y - 16;
            d = u - 128;
            e = v - 128;
            t = (298 * c + 409 * e + 128) >> 8;
            b = RDPCLAMP(t, 0, 255);
            t = (298 * c - 100 * d - 208 * e + 128) >> 8;
            g = RDPCLAMP(t, 0, 255);
            t = (298 * c + 516 * d + 128) >> 8;
            r = RDPCLAMP(t, 0, 255);
            rgbs[j * width + i] = (r << 16) | (g << 8) | b;
        }
    }
    return 0;
}

/*****************************************************************************/
static int
I420_to_RGB32(unsigned char *yuvs, int width, int height, int *rgbs)
{
    int size_total;
    int y;
    int u;
    int v;
    int c;
    int d;
    int e;
    int r;
    int g;
    int b;
    int t;
    int i;
    int j;

    size_total = width * height;
    for (j = 0; j < height; j++)
    {
        for (i = 0; i < width; i++)
        {
            y = yuvs[j * width + i];
            v = yuvs[(j / 2) * (width / 2) + (i / 2) + size_total];
            u = yuvs[(j / 2) * (width / 2) + (i / 2) + size_total + (size_total / 4)];
            c = y - 16;
            d = u - 128;
            e = v - 128;
            t = (298 * c + 409 * e + 128) >> 8;
            b = RDPCLAMP(t, 0, 255);
            t = (298 * c - 100 * d - 208 * e + 128) >> 8;
            g = RDPCLAMP(t, 0, 255);
            t = (298 * c + 516 * d + 128) >> 8;
            r = RDPCLAMP(t, 0, 255);
            rgbs[j * width + i] = (r << 16) | (g << 8) | b;
        }
    }
    return 0;
}

/*****************************************************************************/
static int
YUY2_to_RGB32(unsigned char *yuvs, int width, int height, int *rgbs)
{
    int y1;
    int y2;
    int u;
    int v;
    int c;
    int d;
    int e;
    int r;
    int g;
    int b;
    int t;
    int i;
    int j;

    for (j = 0; j < height; j++)
    {
        for (i = 0; i < width; i++)
        {
            y1 = *(yuvs++);
            v = *(yuvs++);
            y2 = *(yuvs++);
            u = *(yuvs++);

            c = y1 - 16;
            d = u - 128;
            e = v - 128;
            t = (298 * c + 409 * e + 128) >> 8;
            b = RDPCLAMP(t, 0, 255);
            t = (298 * c - 100 * d - 208 * e + 128) >> 8;
            g = RDPCLAMP(t, 0, 255);
            t = (298 * c + 516 * d + 128) >> 8;
            r = RDPCLAMP(t, 0, 255);
            rgbs[j * width + i] = (r << 16) | (g << 8) | b;

            i++;
            c = y2 - 16;
            d = u - 128;
            e = v - 128;
            t = (298 * c + 409 * e + 128) >> 8;
            b = RDPCLAMP(t, 0, 255);
            t = (298 * c - 100 * d - 208 * e + 128) >> 8;
            g = RDPCLAMP(t, 0, 255);
            t = (298 * c + 516 * d + 128) >> 8;
            r = RDPCLAMP(t, 0, 255);
            rgbs[j * width + i] = (r << 16) | (g << 8) | b;
        }
    }
    return 0;
}

/*****************************************************************************/
static int
UYVY_to_RGB32(unsigned char *yuvs, int width, int height, int *rgbs)
{
    int y1;
    int y2;
    int u;
    int v;
    int c;
    int d;
    int e;
    int r;
    int g;
    int b;
    int t;
    int i;
    int j;

    for (j = 0; j < height; j++)
    {
        for (i = 0; i < width; i++)
        {
            v = *(yuvs++);
            y1 = *(yuvs++);
            u = *(yuvs++);
            y2 = *(yuvs++);

            c = y1 - 16;
            d = u - 128;
            e = v - 128;
            t = (298 * c + 409 * e + 128) >> 8;
            b = RDPCLAMP(t, 0, 255);
            t = (298 * c - 100 * d - 208 * e + 128) >> 8;
            g = RDPCLAMP(t, 0, 255);
            t = (298 * c + 516 * d + 128) >> 8;
            r = RDPCLAMP(t, 0, 255);
            rgbs[j * width + i] = (r << 16) | (g << 8) | b;

            i++;
            c = y2 - 16;
            d = u - 128;
            e = v - 128;
            t = (298 * c + 409 * e + 128) >> 8;
            b = RDPCLAMP(t, 0, 255);
            t = (298 * c - 100 * d - 208 * e + 128) >> 8;
            g = RDPCLAMP(t, 0, 255);
            t = (298 * c + 516 * d + 128) >> 8;
            r = RDPCLAMP(t, 0, 255);
            rgbs[j * width + i] = (r << 16) | (g << 8) | b;
        }
    }
    return 0;
}

#if 0
/*****************************************************************************/
static int
stretch_RGB32_RGB32(int *src, int src_width, int src_height,
                    int src_x, int src_y, int src_w, int src_h,
                    int *dst, int dst_w, int dst_h)
{
    int mwidth;
    int mheight;
    int index;

    mwidth = RDPMIN(src_width, dst_w);
    mheight = RDPMIN(src_height, dst_h);
    for (index = 0; index < mheight; index++)
    {
        g_memcpy(dst, src, mwidth * 4);
        src += src_width;
        dst += dst_w;
    }
    return 0;
}
#endif

/*****************************************************************************/
static int
stretch_RGB32_RGB32(int *src, int src_width, int src_height,
                    int src_x, int src_y, int src_w, int src_h,
                    int *dst, int dst_w, int dst_h)
{
    int index;
    int jndex;
    int kndex;
    int lndex;
    int oh = (src_w << 16) / dst_w;
    int ih;
    int ov = (src_h << 16) / dst_h;
    int iv;
    int pix;

    LLOGLN(10, ("stretch_RGB32_RGB32: oh 0x%8.8x ov 0x%8.8x", oh, ov));
    iv = ov;
    lndex = src_y;
    for (index = 0; index < dst_h; index++)
    {
        ih = oh;
        kndex = src_x;
        for (jndex = 0; jndex < dst_w; jndex++)
        {
            pix = src[lndex * src_width + kndex];
            dst[index * dst_w + jndex] = pix;
            while (ih > (1 << 16) - 1)
            {
                ih -= 1 << 16;
                kndex++;
            }
            ih += oh;
        }
        while (iv > (1 << 16) - 1)
        {
            iv -= 1 << 16;
            lndex++;
        }
        iv += ov;

    }
    return 0;
}

/*****************************************************************************/
/* see hw/xfree86/common/xf86xv.c for info */
static int
xrdpVidPutImage(ScrnInfoPtr pScrn,
                short src_x, short src_y, short drw_x, short drw_y,
                short src_w, short src_h, short drw_w, short drw_h,
                int format, unsigned char* buf,
                short width, short height,
                Bool sync, RegionPtr clipBoxes,
                pointer data, DrawablePtr dst)
{
    rdpPtr dev;
    char *dst8;
    int *dst32;
    int *src32;
    int *src32a;
    int *rgborg32;
    int *rgbend32;
    int index;
    int jndex;
    int num_clips;
    int error;
    void *mem;
    RegionRec dreg;
    BoxRec box;

    LLOGLN(10, ("xrdpVidPutImage:"));
    LLOGLN(10, ("xrdpVidPutImage: src_x %d srcy_y %d", src_x, src_y));
    dev = XRDPPTR(pScrn);

    index = width * height * 4 + drw_w * drw_h * 4 + 64;
    mem = g_malloc(index, 0);
    if (mem == NULL)
    {
        LLOGLN(0, ("xrdpVidPutImage: memory alloc error"));
        return Success;
    }
    rgborg32 = (int *) RDPALIGN(mem, 16);
    rgbend32 = rgborg32 + width * height;
    rgbend32 = (int *) RDPALIGN(rgbend32, 16);
    error = 0;
    switch (format)
    {
        case FOURCC_YV12:
            LLOGLN(10, ("xrdpVidPutImage: FOURCC_YV12"));
            error = dev->yv12_to_rgb32(buf, width, height, rgborg32);
            break;
        case FOURCC_I420:
            LLOGLN(10, ("xrdpVidPutImage: FOURCC_I420"));
            error = dev->i420_to_rgb32(buf, width, height, rgborg32);
            break;
        case FOURCC_YUY2:
            LLOGLN(10, ("xrdpVidPutImage: FOURCC_YUY2"));
            error = dev->yuy2_to_rgb32(buf, width, height, rgborg32);
            break;
        case FOURCC_UYVY:
            LLOGLN(10, ("xrdpVidPutImage: FOURCC_UYVY"));
            error = dev->uyvy_to_rgb32(buf, width, height, rgborg32);
            break;
        default:
            LLOGLN(0, ("xrdpVidPutImage: unknown format 0x%8.8x", format));
            g_free(mem);
            return Success;
    }
    if (error != 0)
    {
        g_free(mem);
        return Success;
    }
    error = stretch_RGB32_RGB32(rgborg32, width, height,
                                src_x, src_y, src_w, src_h,
                                rgbend32, drw_w, drw_h);
    if (error != 0)
    {
        g_free(mem);
        return Success;
    }

    box.x1 = drw_x;
    box.y1 = drw_y;
    box.x2 = box.x1 + drw_w;
    box.y2 = box.y1 + drw_h;
    LLOGLN(10, ("box 1 %d %d %d %d", box.x1, box.y1, box.x2, box.y2));
    rdpRegionInit(&dreg, &box, 0);

    num_clips = REGION_NUM_RECTS(clipBoxes);
    LLOGLN(10, ("xrdpVidPutImage: num_clips %d", num_clips));
    if (num_clips > 0)
    {
        rdpRegionIntersect(&dreg, &dreg, clipBoxes);
    }

    num_clips = REGION_NUM_RECTS(&dreg);
    for (jndex = 0; jndex < num_clips; jndex++)
    {
        box = REGION_RECTS(&dreg)[jndex];
        LLOGLN(10, ("box 2 %d %d %d %d", box.x1, box.y1, box.x2, box.y2));
        dst8 = dev->pfbMemory + box.y1 * dev->paddedWidthInBytes;
        src32a = rgbend32 + (box.y1 - drw_y) * drw_w;
        for (index = 0; index < box.y2 - box.y1; index++)
        {
            dst32 = (int *) dst8;
            dst32 += box.x1;
            src32 = src32a + (box.x1 - drw_x);
            g_memcpy(dst32, src32, (box.x2 - box.x1) * 4);
            dst8 += dev->paddedWidthInBytes;
            src32a += drw_w;
        }
    }

    rdpClientConAddAllReg(dev, &dreg, dst);
    rdpRegionUninit(&dreg);

    g_free(mem);

    return Success;
}

/*****************************************************************************/
static int
xrdpVidQueryImageAttributes(ScrnInfoPtr pScrn, int id,
                            unsigned short *w, unsigned short *h,
                            int *pitches, int *offsets)
{
    int size, tmp;

    LLOGLN(10, ("xrdpVidQueryImageAttributes:"));
    /* this is same code as all drivers currently have */
    if (*w > 2046)
    {
        *w = 2046;
    }
    if (*h > 2046)
    {
        *h = 2046;
    }
    /* make w multiple of 4 so that resizing works properly */
    *w = (*w + 3) & ~3;
    if (offsets != NULL)
    {
        offsets[0] = 0;
    }
    switch (id)
    {
        case FOURCC_YV12:
        case FOURCC_I420:
            /* make h be even */
            *h = (*h + 1) & ~1;
            /* make w be multiple of 4 (ie. pad it) */
            size = (*w + 3) & ~3;
            /* width of a Y row => width of image */
            if (pitches != NULL)
            {
                pitches[0] = size;
            }
            /* offset of U plane => w * h */
            size *= *h;
            if (offsets != NULL)
            {
                offsets[1] = size;
            }
            /* width of U, V row => width / 2 */
            tmp = ((*w >> 1) + 3) & ~3;
            if (pitches != NULL)
            {
                pitches[1] = pitches[2] = tmp;
            }
            /* offset of V => Y plane + U plane (w * h + w / 2 * h / 2) */
            tmp *= (*h >> 1);
            size += tmp;
            if (offsets != NULL)
            {
                offsets[2] = size;
            }
            size += tmp;
            break;
        case FOURCC_YUY2:
        case FOURCC_UYVY:
            size = (*w) * 2;
            if (pitches != NULL)
            {
                pitches[0] = size;
            }
            size *= *h;
            break;
        default:
            LLOGLN(0, ("xrdpVidQueryImageAttributes: Unsupported image"));
            return 0;
    }
    LLOGLN(10, ("xrdpVidQueryImageAttributes: finished size %d id 0x%x", size, id));
    return size;
}

#if defined(__x86_64__) || defined(__AMD64__) || defined (_M_AMD64)
int
yv12_to_rgb32_amd64_sse2(unsigned char *yuvs, int width, int height, int *rgbs);
int
i420_to_rgb32_amd64_sse2(unsigned char *yuvs, int width, int height, int *rgbs);
int
yuy2_to_rgb32_amd64_sse2(unsigned char *yuvs, int width, int height, int *rgbs);
int
uyvy_to_rgb32_amd64_sse2(unsigned char *yuvs, int width, int height, int *rgbs);
#elif defined(__x86__) || defined(_M_IX86) || defined(__i386__)
int
yv12_to_rgb32_x86_sse2(unsigned char *yuvs, int width, int height, int *rgbs);
int
i420_to_rgb32_x86_sse2(unsigned char *yuvs, int width, int height, int *rgbs);
int
yuy2_to_rgb32_x86_sse2(unsigned char *yuvs, int width, int height, int *rgbs);
int
uyvy_to_rgb32_x86_sse2(unsigned char *yuvs, int width, int height, int *rgbs);
#endif

/*****************************************************************************/
Bool
rdpXvInit(ScreenPtr pScreen, ScrnInfoPtr pScrn)
{
    rdpPtr dev;
    XF86VideoAdaptorPtr adaptor;
    DevUnion* pDevUnion;
    int bytes;

    adaptor = xf86XVAllocateVideoAdaptorRec(pScrn);
    if (adaptor == 0)
    {
        LLOGLN(0, ("rdpXvInit: xf86XVAllocateVideoAdaptorRec failed"));
        return 0;
    }
    adaptor->type = XvInputMask | XvImageMask | XvVideoMask | XvStillMask | XvWindowMask | XvPixmapMask;
    //adaptor->flags = VIDEO_NO_CLIPPING;
    //adaptor->flags = VIDEO_CLIP_TO_VIEWPORT;
    adaptor->flags = 0;
    adaptor->name = XRDP_MODULE_NAME " XVideo Adaptor";
    adaptor->nEncodings = T_NUM_ENCODINGS;
    adaptor->pEncodings = &(g_xrdpVidEncodings[0]);
    adaptor->nFormats = T_NUM_FORMATS;
    adaptor->pFormats = &(g_xrdpVidFormats[0]);
    adaptor->pFormats[0].depth = pScrn->depth;
    LLOGLN(0, ("rdpXvInit: depth %d", pScrn->depth));
    adaptor->nImages = sizeof(g_xrdpVidImages) / sizeof(XF86ImageRec);
    adaptor->pImages = g_xrdpVidImages;
    adaptor->nAttributes = 0;
    adaptor->pAttributes = 0;
    adaptor->nPorts = T_MAX_PORTS;
    bytes = sizeof(DevUnion) * T_MAX_PORTS;
    pDevUnion = (DevUnion*) g_malloc(bytes, 1);
    adaptor->pPortPrivates = pDevUnion;
    adaptor->PutVideo = xrdpVidPutVideo;
    adaptor->PutStill = xrdpVidPutStill;
    adaptor->GetVideo = xrdpVidGetVideo;
    adaptor->GetStill = xrdpVidGetStill;
    adaptor->StopVideo = xrdpVidStopVideo;
    adaptor->SetPortAttribute = xrdpVidSetPortAttribute;
    adaptor->GetPortAttribute = xrdpVidGetPortAttribute;
    adaptor->QueryBestSize = xrdpVidQueryBestSize;
    adaptor->PutImage = xrdpVidPutImage;
    adaptor->QueryImageAttributes = xrdpVidQueryImageAttributes;
    if (!xf86XVScreenInit(pScreen, &adaptor, 1))
    {
        LLOGLN(0, ("rdpXvInit: xf86XVScreenInit failed"));
        return 0;
    }
    xf86XVFreeVideoAdaptorRec(adaptor);

    dev = XRDPPTR(pScrn);
    /* assign functions */
    LLOGLN(0, ("rdpXvInit: assigning yuv functions"));
#if XV_USE_ACCEL
    if (g_xv_use_accel)
    {
#if defined(__x86_64__) || defined(__AMD64__) || defined (_M_AMD64)
        dev->yv12_to_rgb32 = yv12_to_rgb32_amd64_sse2;
        dev->i420_to_rgb32 = i420_to_rgb32_amd64_sse2;
        dev->yuy2_to_rgb32 = yuy2_to_rgb32_amd64_sse2;
        dev->uyvy_to_rgb32 = uyvy_to_rgb32_amd64_sse2;
        LLOGLN(0, ("rdpXvInit: sse amd64 yuv functions assigned"));
#elif defined(__x86__) || defined(_M_IX86) || defined(__i386__)
        dev->yv12_to_rgb32 = yv12_to_rgb32_x86_sse2;
        dev->i420_to_rgb32 = i420_to_rgb32_x86_sse2;
        dev->yuy2_to_rgb32 = yuy2_to_rgb32_x86_sse2;
        dev->uyvy_to_rgb32 = uyvy_to_rgb32_x86_sse2;
        LLOGLN(0, ("rdpXvInit: sse x86 yuv functions assigned"));
#else
        dev->yv12_to_rgb32 = YV12_to_RGB32;
        dev->i420_to_rgb32 = I420_to_RGB32;
        dev->yuy2_to_rgb32 = YUY2_to_RGB32;
        dev->uyvy_to_rgb32 = UYVY_to_RGB32;
        LLOGLN(0, ("rdpXvInit: warning, c yuv functions assigned"));
#endif
    }
    else
    {
        dev->yv12_to_rgb32 = YV12_to_RGB32;
        dev->i420_to_rgb32 = I420_to_RGB32;
        dev->yuy2_to_rgb32 = YUY2_to_RGB32;
        dev->uyvy_to_rgb32 = UYVY_to_RGB32;
        LLOGLN(0, ("rdpXvInit: warning, c yuv functions assigned"));
    }
#else
        dev->yv12_to_rgb32 = YV12_to_RGB32;
        dev->i420_to_rgb32 = I420_to_RGB32;
        dev->yuy2_to_rgb32 = YUY2_to_RGB32;
        dev->uyvy_to_rgb32 = UYVY_to_RGB32;
        LLOGLN(0, ("rdpXvInit: warning, c yuv functions assigned"));
#endif

    return 1;
}