Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / state_trackers / xorg / xorg_xv.c
1 #include "xorg_tracker.h"
2
3 #include <xf86xv.h>
4 #include <X11/extensions/Xv.h>
5 #include <fourcc.h>
6
7 #include "xorg_exa.h"
8 #include "xorg_renderer.h"
9
10 #include "pipe/p_screen.h"
11 #include "pipe/p_inlines.h"
12
13 /*XXX get these from pipe's texture limits */
14 #define IMAGE_MAX_WIDTH 2048
15 #define IMAGE_MAX_HEIGHT 2048
16
17 #define RES_720P_X 1280
18 #define RES_720P_Y 720
19
20
21 /* The ITU-R BT.601 conversion matrix for SDTV. */
22 static const float bt_601[] = {
23 1.0, 0.0, 1.4075,
24 1.0, -0.3455, -0.7169,
25 1.0, 1.7790, 0.
26 };
27
28 /* The ITU-R BT.709 conversion matrix for HDTV. */
29 static const float bt_709[] = {
30 1.0, 0.0, 1.581,
31 1.0, -0.1881, -0.47,
32 1.0, 1.8629, 0.
33 };
34
35 #define MAKE_ATOM(a) MakeAtom(a, sizeof(a) - 1, TRUE)
36
37 static Atom xvBrightness, xvContrast;
38
39 #define NUM_TEXTURED_ATTRIBUTES 2
40 static XF86AttributeRec TexturedAttributes[NUM_TEXTURED_ATTRIBUTES] = {
41 {XvSettable | XvGettable, -128, 127, "XV_BRIGHTNESS"},
42 {XvSettable | XvGettable, 0, 255, "XV_CONTRAST"}
43 };
44
45 #define NUM_FORMATS 3
46 static XF86VideoFormatRec Formats[NUM_FORMATS] = {
47 {15, TrueColor}, {16, TrueColor}, {24, TrueColor}
48 };
49
50 static XF86VideoEncodingRec DummyEncoding[1] = {
51 {
52 0,
53 "XV_IMAGE",
54 IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT,
55 {1, 1}
56 }
57 };
58
59 #define NUM_IMAGES 2
60 static XF86ImageRec Images[NUM_IMAGES] = {
61 XVIMAGE_UYVY,
62 XVIMAGE_YUY2,
63 };
64
65 struct xorg_xv_port_priv {
66 struct xorg_renderer *r;
67
68 RegionRec clip;
69
70 int brightness;
71 int contrast;
72
73 int current_set;
74 /* juggle two sets of seperate Y, U and V
75 * textures */
76 struct pipe_texture *yuv[2][3];
77 };
78
79
80 static void
81 stop_video(ScrnInfoPtr pScrn, pointer data, Bool shutdown)
82 {
83 struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
84
85 REGION_EMPTY(pScrn->pScreen, &priv->clip);
86 }
87
88 static int
89 set_port_attribute(ScrnInfoPtr pScrn,
90 Atom attribute, INT32 value, pointer data)
91 {
92 struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
93
94 if (attribute == xvBrightness) {
95 if ((value < -128) || (value > 127))
96 return BadValue;
97 priv->brightness = value;
98 } else if (attribute == xvContrast) {
99 if ((value < 0) || (value > 255))
100 return BadValue;
101 priv->contrast = value;
102 } else
103 return BadMatch;
104
105 return Success;
106 }
107
108 static int
109 get_port_attribute(ScrnInfoPtr pScrn,
110 Atom attribute, INT32 * value, pointer data)
111 {
112 struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
113
114 if (attribute == xvBrightness)
115 *value = priv->brightness;
116 else if (attribute == xvContrast)
117 *value = priv->contrast;
118 else
119 return BadMatch;
120
121 return Success;
122 }
123
124 static void
125 query_best_size(ScrnInfoPtr pScrn,
126 Bool motion,
127 short vid_w, short vid_h,
128 short drw_w, short drw_h,
129 unsigned int *p_w, unsigned int *p_h, pointer data)
130 {
131 if (vid_w > (drw_w << 1))
132 drw_w = vid_w >> 1;
133 if (vid_h > (drw_h << 1))
134 drw_h = vid_h >> 1;
135
136 *p_w = drw_w;
137 *p_h = drw_h;
138 }
139
140 static INLINE struct pipe_texture *
141 create_component_texture(struct pipe_context *pipe,
142 int width, int height)
143 {
144 struct pipe_screen *screen = pipe->screen;
145 struct pipe_texture *tex = 0;
146 struct pipe_texture templ;
147
148 memset(&templ, 0, sizeof(templ));
149 templ.target = PIPE_TEXTURE_2D;
150 templ.format = PIPE_FORMAT_L8_UNORM;
151 templ.last_level = 0;
152 templ.width[0] = width;
153 templ.height[0] = height;
154 templ.depth[0] = 1;
155 pf_get_block(PIPE_FORMAT_L8_UNORM, &templ.block);
156 templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
157
158 tex = screen->texture_create(screen, &templ);
159
160 return tex;
161 }
162
163 static int
164 check_yuv_textures(struct xorg_xv_port_priv *priv, int width, int height)
165 {
166 struct pipe_texture **dst = priv->yuv[priv->current_set];
167 if (!dst[0] ||
168 dst[0]->width[0] != width ||
169 dst[0]->height[0] != height) {
170 pipe_texture_reference(&dst[0], NULL);
171 }
172 if (!dst[1] ||
173 dst[1]->width[0] != width ||
174 dst[1]->height[0] != height) {
175 pipe_texture_reference(&dst[1], NULL);
176 }
177 if (!dst[2] ||
178 dst[2]->width[0] != width ||
179 dst[2]->height[0] != height) {
180 pipe_texture_reference(&dst[2], NULL);
181 }
182
183 if (!dst[0])
184 dst[0] = create_component_texture(priv->r->pipe, width, height);
185
186 if (!dst[1])
187 dst[1] = create_component_texture(priv->r->pipe, width, height);
188
189 if (!dst[2])
190 dst[2] = create_component_texture(priv->r->pipe, width, height);
191
192 if (!dst[0] || !dst[1] || !dst[2])
193 return BadAlloc;
194
195 return Success;
196 }
197
198 static void
199 copy_packed_data(ScrnInfoPtr pScrn,
200 struct xorg_xv_port_priv *port,
201 int id,
202 unsigned char *buf,
203 int srcPitch,
204 int left,
205 int top,
206 int w, int h)
207 {
208 unsigned char *src;
209 int i, j;
210 struct pipe_texture **dst = port->yuv[port->current_set];
211 struct pipe_transfer *ytrans, *utrans, *vtrans;
212 struct pipe_screen *screen = port->r->pipe->screen;
213 char *ymap, *vmap, *umap;
214 unsigned char y1, y2, u, v;
215 int yidx, uidx, vidx;
216
217 src = buf + (top * srcPitch) + (left << 1);
218
219 ytrans = screen->get_tex_transfer(screen, dst[0],
220 0, 0, 0,
221 PIPE_TRANSFER_WRITE,
222 left, top, w, h);
223 utrans = screen->get_tex_transfer(screen, dst[1],
224 0, 0, 0,
225 PIPE_TRANSFER_WRITE,
226 left, top, w, h);
227 vtrans = screen->get_tex_transfer(screen, dst[2],
228 0, 0, 0,
229 PIPE_TRANSFER_WRITE,
230 left, top, w, h);
231
232 ymap = screen->transfer_map(screen, ytrans);
233 umap = screen->transfer_map(screen, utrans);
234 vmap = screen->transfer_map(screen, vtrans);
235
236 switch (id) {
237 case FOURCC_YV12: {
238 int y_array_size = w * h;
239 for (i = 0; i < w; ++i) {
240 for (j = 0; i < h; ++j) {
241 /*XXX use src? */
242 y1 = buf[j*w + i];
243 u = buf[(j/2) * (w/2) + i/2 + y_array_size];
244 v = buf[(j/2) * (w/2) + i/2 + y_array_size + y_array_size/4];
245 ymap[yidx++] = y1;
246 umap[uidx++] = u;
247 vmap[vidx++] = v;
248 }
249 }
250 }
251 break;
252 case FOURCC_YUY2:
253 for (j = 0; j < h; ++j) {
254 for (i = 0; i < w; ++i) {
255 /* extracting two pixels */
256 y1 = buf[0];
257 u = buf[1];
258 y2 = buf[2];
259 v = buf[3];
260
261 ymap[yidx++] = y1;
262 ymap[yidx++] = y2;
263 umap[uidx++] = u;
264 umap[uidx++] = u;
265 vmap[vidx++] = v;
266 vmap[vidx++] = v;
267 }
268 }
269 break;
270 default:
271 debug_assert(!"Unsupported yuv format!");
272 break;
273 }
274
275 screen->transfer_unmap(screen, ytrans);
276 screen->transfer_unmap(screen, utrans);
277 screen->transfer_unmap(screen, vtrans);
278 screen->tex_transfer_destroy(ytrans);
279 screen->tex_transfer_destroy(utrans);
280 screen->tex_transfer_destroy(vtrans);
281 }
282
283 static void
284 setup_video_constants(struct xorg_renderer *r, boolean hdtv)
285 {
286 struct pipe_context *pipe = r->pipe;
287 const int param_bytes = 9 * sizeof(float);
288 struct pipe_constant_buffer *cbuf = &r->vs_const_buffer;
289
290 pipe_buffer_reference(&cbuf->buffer, NULL);
291 cbuf->buffer = pipe_buffer_create(pipe->screen, 16,
292 PIPE_BUFFER_USAGE_CONSTANT,
293 param_bytes);
294
295 if (cbuf->buffer) {
296 const float *video_constants = (hdtv) ? bt_709 : bt_601;
297
298 pipe_buffer_write(pipe->screen, cbuf->buffer,
299 0, param_bytes, video_constants);
300 }
301 pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, 0, cbuf);
302 }
303
304 static int
305 display_video(ScrnInfoPtr pScrn, struct xorg_xv_port_priv *pPriv, int id,
306 RegionPtr dstRegion,
307 short width, short height,
308 int x1, int y1, int x2, int y2,
309 short src_w, short src_h, short drw_w, short drw_h,
310 PixmapPtr pPixmap)
311 {
312 BoxPtr pbox;
313 int nbox;
314 int dxo, dyo;
315 Bool hdtv;
316 float tc0[2], tc1[2], tc2[2];
317
318 hdtv = ((src_w >= RES_720P_X) && (src_h >= RES_720P_Y));
319 setup_video_constants(pPriv->r, hdtv);
320
321 REGION_TRANSLATE(pScrn->pScreen, dstRegion, -pPixmap->screen_x,
322 -pPixmap->screen_y);
323
324 dxo = dstRegion->extents.x1;
325 dyo = dstRegion->extents.y1;
326
327 pbox = REGION_RECTS(dstRegion);
328 nbox = REGION_NUM_RECTS(dstRegion);
329
330 while (nbox--) {
331 int box_x1 = pbox->x1;
332 int box_y1 = pbox->y1;
333 int box_x2 = pbox->x2;
334 int box_y2 = pbox->y2;
335
336 tc0[0] = (double) (box_x1 - dxo) / (double) drw_w; /* u0 */
337 tc0[1] = (double) (box_y1 - dyo) / (double) drw_h; /* v0 */
338 tc1[0] = (double) (box_x2 - dxo) / (double) drw_w; /* u1 */
339 tc1[1] = tc0[1];
340 tc2[0] = tc0[0];
341 tc2[1] = (double) (box_y2 - dyo) / (double) drw_h; /* v1 */
342
343 #if 0
344 x = box_x1;
345 y = box_y1;
346 w = box_x2 - box_x1;
347 h = box_y2 - box_y1;
348
349 pbox++;
350 draw_yuv(pScrn, x, y, w, h, &src, 1, FALSE,
351 0, tc0, tc1, tc2, 1,
352 pPriv->conversionData);
353 #endif
354 }
355 DamageDamageRegion(&pPixmap->drawable, dstRegion);
356 return TRUE;
357 }
358
359 static int
360 put_image(ScrnInfoPtr pScrn,
361 short src_x, short src_y,
362 short drw_x, short drw_y,
363 short src_w, short src_h,
364 short drw_w, short drw_h,
365 int id, unsigned char *buf,
366 short width, short height,
367 Bool sync, RegionPtr clipBoxes, pointer data,
368 DrawablePtr pDraw)
369 {
370 struct xorg_xv_port_priv *pPriv = (struct xorg_xv_port_priv *) data;
371 ScreenPtr pScreen = screenInfo.screens[pScrn->scrnIndex];
372 PixmapPtr pPixmap;
373 INT32 x1, x2, y1, y2;
374 int srcPitch;
375 BoxRec dstBox;
376 int ret;
377
378 /* Clip */
379 x1 = src_x;
380 x2 = src_x + src_w;
381 y1 = src_y;
382 y2 = src_y + src_h;
383
384 dstBox.x1 = drw_x;
385 dstBox.x2 = drw_x + drw_w;
386 dstBox.y1 = drw_y;
387 dstBox.y2 = drw_y + drw_h;
388
389 if (!xf86XVClipVideoHelper(&dstBox, &x1, &x2, &y1, &y2, clipBoxes,
390 width, height))
391 return Success;
392
393 switch (id) {
394 case FOURCC_UYVY:
395 case FOURCC_YUY2:
396 default:
397 srcPitch = width << 1;
398 break;
399 }
400
401 ret = check_yuv_textures(pPriv, width, height);
402
403 if (ret)
404 return ret;
405
406 copy_packed_data(pScrn, pPriv, id, buf, srcPitch,
407 src_x, src_y, width, height);
408
409 if (pDraw->type == DRAWABLE_WINDOW) {
410 pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr)pDraw);
411 } else {
412 pPixmap = (PixmapPtr)pDraw;
413 }
414
415 display_video(pScrn, pPriv, id, clipBoxes, width, height,
416 x1, y1, x2, y2,
417 src_w, src_h, drw_w, drw_h, pPixmap);
418
419 pPriv->current_set = (pPriv->current_set + 1) & 1;
420 return Success;
421 }
422
423 static int
424 query_image_attributes(ScrnInfoPtr pScrn,
425 int id,
426 unsigned short *w, unsigned short *h,
427 int *pitches, int *offsets)
428 {
429 int size;
430
431 if (*w > IMAGE_MAX_WIDTH)
432 *w = IMAGE_MAX_WIDTH;
433 if (*h > IMAGE_MAX_HEIGHT)
434 *h = IMAGE_MAX_HEIGHT;
435
436 *w = (*w + 1) & ~1;
437 if (offsets)
438 offsets[0] = 0;
439
440 switch (id) {
441 case FOURCC_UYVY:
442 case FOURCC_YUY2:
443 default:
444 size = *w << 1;
445 if (pitches)
446 pitches[0] = size;
447 size *= *h;
448 break;
449 }
450
451 return size;
452 }
453
454 static struct xorg_xv_port_priv *
455 port_priv_create(struct xorg_renderer *r)
456 {
457 struct xorg_xv_port_priv *priv = NULL;
458
459 priv = calloc(1, sizeof(struct xorg_xv_port_priv));
460
461 if (!priv)
462 return NULL;
463
464 priv->r = r;
465
466 REGION_NULL(pScreen, &priv->clip);
467
468 debug_assert(priv && priv->r);
469
470 return priv;
471 }
472
473 static XF86VideoAdaptorPtr
474 xorg_setup_textured_adapter(ScreenPtr pScreen)
475 {
476 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
477 modesettingPtr ms = modesettingPTR(pScrn);
478 XF86VideoAdaptorPtr adapt;
479 XF86AttributePtr attrs;
480 DevUnion *dev_unions;
481 int nports = 16, i;
482 int nattributes;
483
484 nattributes = NUM_TEXTURED_ATTRIBUTES;
485
486 debug_assert(ms->exa);
487 debug_assert(ms->exa->renderer);
488
489 adapt = calloc(1, sizeof(XF86VideoAdaptorRec));
490 dev_unions = calloc(nports, sizeof(DevUnion));
491 attrs = calloc(nattributes, sizeof(XF86AttributeRec));
492 if (adapt == NULL || dev_unions == NULL || attrs == NULL) {
493 free(adapt);
494 free(dev_unions);
495 free(attrs);
496 return NULL;
497 }
498
499 adapt->type = XvWindowMask | XvInputMask | XvImageMask;
500 adapt->flags = 0;
501 adapt->name = "Gallium3D Textured Video";
502 adapt->nEncodings = 1;
503 adapt->pEncodings = DummyEncoding;
504 adapt->nFormats = NUM_FORMATS;
505 adapt->pFormats = Formats;
506 adapt->nPorts = 0;
507 adapt->pPortPrivates = dev_unions;
508 adapt->nAttributes = nattributes;
509 adapt->pAttributes = attrs;
510 memcpy(attrs, TexturedAttributes, nattributes * sizeof(XF86AttributeRec));
511 adapt->nImages = NUM_IMAGES;
512 adapt->pImages = Images;
513 adapt->PutVideo = NULL;
514 adapt->PutStill = NULL;
515 adapt->GetVideo = NULL;
516 adapt->GetStill = NULL;
517 adapt->StopVideo = stop_video;
518 adapt->SetPortAttribute = set_port_attribute;
519 adapt->GetPortAttribute = get_port_attribute;
520 adapt->QueryBestSize = query_best_size;
521 adapt->PutImage = put_image;
522 adapt->QueryImageAttributes = query_image_attributes;
523
524 for (i = 0; i < nports; i++) {
525 struct xorg_xv_port_priv *priv =
526 port_priv_create(ms->exa->renderer);
527
528 adapt->pPortPrivates[i].ptr = (pointer) (priv);
529 adapt->nPorts++;
530 }
531
532 return adapt;
533 }
534
535 void
536 xorg_init_video(ScreenPtr pScreen)
537 {
538 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
539 /*modesettingPtr ms = modesettingPTR(pScrn);*/
540 XF86VideoAdaptorPtr *adaptors, *new_adaptors = NULL;
541 XF86VideoAdaptorPtr textured_adapter;
542 int num_adaptors;
543
544 num_adaptors = xf86XVListGenericAdaptors(pScrn, &adaptors);
545 new_adaptors = malloc((num_adaptors + 1) * sizeof(XF86VideoAdaptorPtr *));
546 if (new_adaptors == NULL)
547 return;
548
549 memcpy(new_adaptors, adaptors, num_adaptors * sizeof(XF86VideoAdaptorPtr));
550 adaptors = new_adaptors;
551
552 /* Add the adaptors supported by our hardware. First, set up the atoms
553 * that will be used by both output adaptors.
554 */
555 xvBrightness = MAKE_ATOM("XV_BRIGHTNESS");
556 xvContrast = MAKE_ATOM("XV_CONTRAST");
557
558 textured_adapter = xorg_setup_textured_adapter(pScreen);
559
560 debug_assert(textured_adapter);
561
562 if (textured_adapter) {
563 adaptors[num_adaptors++] = textured_adapter;
564 }
565
566 if (num_adaptors) {
567 xf86XVScreenInit(pScreen, adaptors, num_adaptors);
568 } else {
569 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
570 "Disabling Xv because no adaptors could be initialized.\n");
571 }
572
573 free(adaptors);
574 }