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
|
#include "xorg_tracker.h"
#include <xf86xv.h>
#include <X11/extensions/Xv.h>
#include <fourcc.h>
#include "xorg_exa.h"
#include "xorg_renderer.h"
#include "xorg_exa_tgsi.h"
#include "cso_cache/cso_context.h"
#include "pipe/p_screen.h"
#include "pipe/p_inlines.h"
/*XXX get these from pipe's texture limits */
#define IMAGE_MAX_WIDTH 2048
#define IMAGE_MAX_HEIGHT 2048
#define RES_720P_X 1280
#define RES_720P_Y 720
/* The ITU-R BT.601 conversion matrix for SDTV. */
static const float bt_601[] = {
1.0, 0.0, 1.4075, 0,
1.0, -0.3455, -0.7169, 0,
1.0, 1.7790, 0., 0,
};
/* The ITU-R BT.709 conversion matrix for HDTV. */
static const float bt_709[] = {
1.0, 0.0, 1.581, 0,
1.0, -0.1881, -0.47, 0,
1.0, 1.8629, 0., 0,
};
#define MAKE_ATOM(a) MakeAtom(a, sizeof(a) - 1, TRUE)
static Atom xvBrightness, xvContrast;
#define NUM_TEXTURED_ATTRIBUTES 2
static XF86AttributeRec TexturedAttributes[NUM_TEXTURED_ATTRIBUTES] = {
{XvSettable | XvGettable, -128, 127, "XV_BRIGHTNESS"},
{XvSettable | XvGettable, 0, 255, "XV_CONTRAST"}
};
#define NUM_FORMATS 3
static XF86VideoFormatRec Formats[NUM_FORMATS] = {
{15, TrueColor}, {16, TrueColor}, {24, TrueColor}
};
static XF86VideoEncodingRec DummyEncoding[1] = {
{
0,
"XV_IMAGE",
IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT,
{1, 1}
}
};
#define NUM_IMAGES 2
static XF86ImageRec Images[NUM_IMAGES] = {
XVIMAGE_UYVY,
XVIMAGE_YUY2,
};
struct xorg_xv_port_priv {
struct xorg_renderer *r;
RegionRec clip;
int brightness;
int contrast;
int current_set;
/* juggle two sets of seperate Y, U and V
* textures */
struct pipe_texture *yuv[2][3];
};
static void
stop_video(ScrnInfoPtr pScrn, pointer data, Bool shutdown)
{
struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
REGION_EMPTY(pScrn->pScreen, &priv->clip);
}
static int
set_port_attribute(ScrnInfoPtr pScrn,
Atom attribute, INT32 value, pointer data)
{
struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
if (attribute == xvBrightness) {
if ((value < -128) || (value > 127))
return BadValue;
priv->brightness = value;
} else if (attribute == xvContrast) {
if ((value < 0) || (value > 255))
return BadValue;
priv->contrast = value;
} else
return BadMatch;
return Success;
}
static int
get_port_attribute(ScrnInfoPtr pScrn,
Atom attribute, INT32 * value, pointer data)
{
struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
if (attribute == xvBrightness)
*value = priv->brightness;
else if (attribute == xvContrast)
*value = priv->contrast;
else
return BadMatch;
return Success;
}
static void
query_best_size(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)
{
if (vid_w > (drw_w << 1))
drw_w = vid_w >> 1;
if (vid_h > (drw_h << 1))
drw_h = vid_h >> 1;
*p_w = drw_w;
*p_h = drw_h;
}
static INLINE struct pipe_texture *
create_component_texture(struct pipe_context *pipe,
int width, int height)
{
struct pipe_screen *screen = pipe->screen;
struct pipe_texture *tex = 0;
struct pipe_texture templ;
memset(&templ, 0, sizeof(templ));
templ.target = PIPE_TEXTURE_2D;
templ.format = PIPE_FORMAT_L8_UNORM;
templ.last_level = 0;
templ.width[0] = width;
templ.height[0] = height;
templ.depth[0] = 1;
pf_get_block(PIPE_FORMAT_L8_UNORM, &templ.block);
templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
tex = screen->texture_create(screen, &templ);
return tex;
}
static int
check_yuv_textures(struct xorg_xv_port_priv *priv, int width, int height)
{
struct pipe_texture **dst = priv->yuv[priv->current_set];
if (!dst[0] ||
dst[0]->width[0] != width ||
dst[0]->height[0] != height) {
pipe_texture_reference(&dst[0], NULL);
}
if (!dst[1] ||
dst[1]->width[0] != width ||
dst[1]->height[0] != height) {
pipe_texture_reference(&dst[1], NULL);
}
if (!dst[2] ||
dst[2]->width[0] != width ||
dst[2]->height[0] != height) {
pipe_texture_reference(&dst[2], NULL);
}
if (!dst[0])
dst[0] = create_component_texture(priv->r->pipe, width, height);
if (!dst[1])
dst[1] = create_component_texture(priv->r->pipe, width, height);
if (!dst[2])
dst[2] = create_component_texture(priv->r->pipe, width, height);
if (!dst[0] || !dst[1] || !dst[2])
return BadAlloc;
return Success;
}
static void
copy_packed_data(ScrnInfoPtr pScrn,
struct xorg_xv_port_priv *port,
int id,
unsigned char *buf,
int srcPitch,
int left,
int top,
int w, int h)
{
unsigned char *src;
int i, j;
struct pipe_texture **dst = port->yuv[port->current_set];
struct pipe_transfer *ytrans, *utrans, *vtrans;
struct pipe_screen *screen = port->r->pipe->screen;
char *ymap, *vmap, *umap;
unsigned char y1, y2, u, v;
int yidx, uidx, vidx;
src = buf + (top * srcPitch) + (left << 1);
ytrans = screen->get_tex_transfer(screen, dst[0],
0, 0, 0,
PIPE_TRANSFER_WRITE,
left, top, w, h);
utrans = screen->get_tex_transfer(screen, dst[1],
0, 0, 0,
PIPE_TRANSFER_WRITE,
left, top, w, h);
vtrans = screen->get_tex_transfer(screen, dst[2],
0, 0, 0,
PIPE_TRANSFER_WRITE,
left, top, w, h);
ymap = screen->transfer_map(screen, ytrans);
umap = screen->transfer_map(screen, utrans);
vmap = screen->transfer_map(screen, vtrans);
switch (id) {
case FOURCC_YV12: {
int y_array_size = w * h;
for (i = 0; i < w; ++i) {
for (j = 0; i < h; ++j) {
/*XXX use src? */
y1 = buf[j*w + i];
u = buf[(j/2) * (w/2) + i/2 + y_array_size];
v = buf[(j/2) * (w/2) + i/2 + y_array_size + y_array_size/4];
ymap[yidx++] = y1;
umap[uidx++] = u;
vmap[vidx++] = v;
}
}
}
break;
case FOURCC_YUY2:
for (j = 0; j < h; ++j) {
for (i = 0; i < w; ++i) {
/* extracting two pixels */
y1 = buf[0];
u = buf[1];
y2 = buf[2];
v = buf[3];
ymap[yidx++] = y1;
ymap[yidx++] = y2;
umap[uidx++] = u;
umap[uidx++] = u;
vmap[vidx++] = v;
vmap[vidx++] = v;
}
}
break;
default:
debug_assert(!"Unsupported yuv format!");
break;
}
screen->transfer_unmap(screen, ytrans);
screen->transfer_unmap(screen, utrans);
screen->transfer_unmap(screen, vtrans);
screen->tex_transfer_destroy(ytrans);
screen->tex_transfer_destroy(utrans);
screen->tex_transfer_destroy(vtrans);
}
static void
setup_vs_video_constants(struct xorg_renderer *r, struct exa_pixmap_priv *dst)
{
int width = dst->tex->width[0];
int height = dst->tex->height[0];
const int param_bytes = 8 * sizeof(float);
float vs_consts[8] = {
2.f/width, 2.f/height, 1, 1,
-1, -1, 0, 0
};
renderer_set_constants(r, PIPE_SHADER_VERTEX,
vs_consts, param_bytes);
}
static void
setup_fs_video_constants(struct xorg_renderer *r, boolean hdtv)
{
const int param_bytes = 12 * sizeof(float);
const float *video_constants = (hdtv) ? bt_709 : bt_601;
renderer_set_constants(r, PIPE_SHADER_FRAGMENT,
video_constants, param_bytes);
}
static void
draw_yuv(struct xorg_xv_port_priv *port, int src_x, int src_y,
int dst_x, int dst_y,
int w, int h)
{
int pos[4] = {src_x, src_y,
dst_x, dst_y};
struct pipe_texture **textures = port->yuv[port->current_set];
renderer_draw_textures(port->r,
pos, w, h,
textures,
3, /*bound samplers/textures */
NULL, NULL /* no transformations */);
}
static void
bind_blend_state(struct xorg_xv_port_priv *port)
{
struct pipe_blend_state blend;
memset(&blend, 0, sizeof(struct pipe_blend_state));
blend.blend_enable = 1;
blend.colormask |= PIPE_MASK_RGBA;
/* porter&duff src */
blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
cso_set_blend(port->r->cso, &blend);
}
static void
bind_shaders(struct xorg_xv_port_priv *port)
{
unsigned vs_traits = 0, fs_traits = 0;
struct xorg_shader shader;
vs_traits |= VS_YUV;
fs_traits |= FS_YUV;
shader = xorg_shaders_get(port->r->shaders, vs_traits, fs_traits);
cso_set_vertex_shader_handle(port->r->cso, shader.vs);
cso_set_fragment_shader_handle(port->r->cso, shader.fs);
}
static INLINE void
conditional_flush(struct pipe_context *pipe, struct pipe_texture **tex,
int num)
{
int i;
for (i = 0; i < num; ++i) {
if (tex[i] && pipe->is_texture_referenced(pipe, tex[i], 0, 0) &
PIPE_REFERENCED_FOR_WRITE) {
pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
return;
}
}
}
static void
bind_samplers(struct xorg_xv_port_priv *port)
{
struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
struct pipe_sampler_state sampler;
struct pipe_texture **dst = port->yuv[port->current_set];
memset(&sampler, 0, sizeof(struct pipe_sampler_state));
conditional_flush(port->r->pipe, dst, 3);
sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
sampler.normalized_coords = 1;
samplers[0] = &sampler;
samplers[1] = &sampler;
samplers[2] = &sampler;
cso_set_samplers(port->r->cso, 3,
(const struct pipe_sampler_state **)samplers);
cso_set_sampler_textures(port->r->cso, 3,
dst);
}
static int
display_video(ScrnInfoPtr pScrn, struct xorg_xv_port_priv *pPriv, int id,
RegionPtr dstRegion,
int src_x, int src_y, int src_w, int src_h,
int dstX, int dstY,
short width, short height,
PixmapPtr pPixmap)
{
modesettingPtr ms = modesettingPTR(pScrn);
BoxPtr pbox;
int nbox;
int dxo, dyo;
Bool hdtv;
int x, y, w, h;
struct exa_pixmap_priv *dst = exaGetPixmapDriverPrivate(pPixmap);
if (!dst || !dst->tex)
XORG_FALLBACK("Xv destination %s", !dst ? "!dst" : "!dst->tex");
hdtv = ((src_w >= RES_720P_X) && (src_h >= RES_720P_Y));
REGION_TRANSLATE(pScrn->pScreen, dstRegion, -pPixmap->screen_x,
-pPixmap->screen_y);
dxo = dstRegion->extents.x1;
dyo = dstRegion->extents.y1;
pbox = REGION_RECTS(dstRegion);
nbox = REGION_NUM_RECTS(dstRegion);
renderer_bind_framebuffer(pPriv->r, dst);
renderer_bind_viewport(pPriv->r, dst);
bind_blend_state(pPriv);
renderer_bind_rasterizer(pPriv->r);
bind_shaders(pPriv);
bind_samplers(pPriv);
setup_vs_video_constants(pPriv->r, dst);
setup_fs_video_constants(pPriv->r, hdtv);
while (nbox--) {
int box_x1 = pbox->x1;
int box_y1 = pbox->y1;
int box_x2 = pbox->x2;
int box_y2 = pbox->y2;
x = box_x1;
y = box_y1;
w = box_x2 - box_x1;
h = box_y2 - box_y1;
draw_yuv(pPriv, x, y, x, y, w, h);
pbox++;
}
DamageDamageRegion(&pPixmap->drawable, dstRegion);
return TRUE;
}
static int
put_image(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 id, unsigned char *buf,
short width, short height,
Bool sync, RegionPtr clipBoxes, pointer data,
DrawablePtr pDraw)
{
struct xorg_xv_port_priv *pPriv = (struct xorg_xv_port_priv *) data;
ScreenPtr pScreen = screenInfo.screens[pScrn->scrnIndex];
PixmapPtr pPixmap;
INT32 x1, x2, y1, y2;
int srcPitch;
BoxRec dstBox;
int ret;
/* Clip */
x1 = src_x;
x2 = src_x + src_w;
y1 = src_y;
y2 = src_y + src_h;
dstBox.x1 = drw_x;
dstBox.x2 = drw_x + drw_w;
dstBox.y1 = drw_y;
dstBox.y2 = drw_y + drw_h;
if (!xf86XVClipVideoHelper(&dstBox, &x1, &x2, &y1, &y2, clipBoxes,
width, height))
return Success;
switch (id) {
case FOURCC_UYVY:
case FOURCC_YUY2:
default:
srcPitch = width << 1;
break;
}
ret = check_yuv_textures(pPriv, width, height);
if (ret)
return ret;
copy_packed_data(pScrn, pPriv, id, buf, srcPitch,
src_x, src_y, width, height);
if (pDraw->type == DRAWABLE_WINDOW) {
pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr)pDraw);
} else {
pPixmap = (PixmapPtr)pDraw;
}
display_video(pScrn, pPriv, id, clipBoxes,
src_x, src_y, src_w, src_h,
drw_x, drw_y,
drw_w, drw_h, pPixmap);
pPriv->current_set = (pPriv->current_set + 1) & 1;
return Success;
}
static int
query_image_attributes(ScrnInfoPtr pScrn,
int id,
unsigned short *w, unsigned short *h,
int *pitches, int *offsets)
{
int size;
if (*w > IMAGE_MAX_WIDTH)
*w = IMAGE_MAX_WIDTH;
if (*h > IMAGE_MAX_HEIGHT)
*h = IMAGE_MAX_HEIGHT;
*w = (*w + 1) & ~1;
if (offsets)
offsets[0] = 0;
switch (id) {
case FOURCC_UYVY:
case FOURCC_YUY2:
default:
size = *w << 1;
if (pitches)
pitches[0] = size;
size *= *h;
break;
}
return size;
}
static struct xorg_xv_port_priv *
port_priv_create(struct xorg_renderer *r)
{
struct xorg_xv_port_priv *priv = NULL;
priv = calloc(1, sizeof(struct xorg_xv_port_priv));
if (!priv)
return NULL;
priv->r = r;
REGION_NULL(pScreen, &priv->clip);
debug_assert(priv && priv->r);
return priv;
}
static XF86VideoAdaptorPtr
xorg_setup_textured_adapter(ScreenPtr pScreen)
{
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
modesettingPtr ms = modesettingPTR(pScrn);
XF86VideoAdaptorPtr adapt;
XF86AttributePtr attrs;
DevUnion *dev_unions;
int nports = 16, i;
int nattributes;
nattributes = NUM_TEXTURED_ATTRIBUTES;
debug_assert(ms->exa);
debug_assert(ms->exa->renderer);
adapt = calloc(1, sizeof(XF86VideoAdaptorRec));
dev_unions = calloc(nports, sizeof(DevUnion));
attrs = calloc(nattributes, sizeof(XF86AttributeRec));
if (adapt == NULL || dev_unions == NULL || attrs == NULL) {
free(adapt);
free(dev_unions);
free(attrs);
return NULL;
}
adapt->type = XvWindowMask | XvInputMask | XvImageMask;
adapt->flags = 0;
adapt->name = "Gallium3D Textured Video";
adapt->nEncodings = 1;
adapt->pEncodings = DummyEncoding;
adapt->nFormats = NUM_FORMATS;
adapt->pFormats = Formats;
adapt->nPorts = 0;
adapt->pPortPrivates = dev_unions;
adapt->nAttributes = nattributes;
adapt->pAttributes = attrs;
memcpy(attrs, TexturedAttributes, nattributes * sizeof(XF86AttributeRec));
adapt->nImages = NUM_IMAGES;
adapt->pImages = Images;
adapt->PutVideo = NULL;
adapt->PutStill = NULL;
adapt->GetVideo = NULL;
adapt->GetStill = NULL;
adapt->StopVideo = stop_video;
adapt->SetPortAttribute = set_port_attribute;
adapt->GetPortAttribute = get_port_attribute;
adapt->QueryBestSize = query_best_size;
adapt->PutImage = put_image;
adapt->QueryImageAttributes = query_image_attributes;
for (i = 0; i < nports; i++) {
struct xorg_xv_port_priv *priv =
port_priv_create(ms->exa->renderer);
adapt->pPortPrivates[i].ptr = (pointer) (priv);
adapt->nPorts++;
}
return adapt;
}
void
xorg_init_video(ScreenPtr pScreen)
{
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
/*modesettingPtr ms = modesettingPTR(pScrn);*/
XF86VideoAdaptorPtr *adaptors, *new_adaptors = NULL;
XF86VideoAdaptorPtr textured_adapter;
int num_adaptors;
num_adaptors = xf86XVListGenericAdaptors(pScrn, &adaptors);
new_adaptors = malloc((num_adaptors + 1) * sizeof(XF86VideoAdaptorPtr *));
if (new_adaptors == NULL)
return;
memcpy(new_adaptors, adaptors, num_adaptors * sizeof(XF86VideoAdaptorPtr));
adaptors = new_adaptors;
/* Add the adaptors supported by our hardware. First, set up the atoms
* that will be used by both output adaptors.
*/
xvBrightness = MAKE_ATOM("XV_BRIGHTNESS");
xvContrast = MAKE_ATOM("XV_CONTRAST");
textured_adapter = xorg_setup_textured_adapter(pScreen);
debug_assert(textured_adapter);
if (textured_adapter) {
adaptors[num_adaptors++] = textured_adapter;
}
if (num_adaptors) {
xf86XVScreenInit(pScreen, adaptors, num_adaptors);
} else {
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"Disabling Xv because no adaptors could be initialized.\n");
}
free(adaptors);
}
|