st/xorg: initialize indexes before reswizzling for yuv
[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 #include "xorg_exa_tgsi.h"
10
11 #include "cso_cache/cso_context.h"
12
13 #include "pipe/p_screen.h"
14 #include "pipe/p_inlines.h"
15
16 /*XXX get these from pipe's texture limits */
17 #define IMAGE_MAX_WIDTH 2048
18 #define IMAGE_MAX_HEIGHT 2048
19
20 #define RES_720P_X 1280
21 #define RES_720P_Y 720
22
23
24 /* The ITU-R BT.601 conversion matrix for SDTV. */
25 static const float bt_601[] = {
26 1.0, 0.0, 1.4075, 0,
27 1.0, -0.3455, -0.7169, 0,
28 1.0, 1.7790, 0., 0,
29 };
30
31 /* The ITU-R BT.709 conversion matrix for HDTV. */
32 static const float bt_709[] = {
33 1.0, 0.0, 1.581, 0,
34 1.0, -0.1881, -0.47, 0,
35 1.0, 1.8629, 0., 0,
36 };
37
38 #define MAKE_ATOM(a) MakeAtom(a, sizeof(a) - 1, TRUE)
39
40 static Atom xvBrightness, xvContrast;
41
42 #define NUM_TEXTURED_ATTRIBUTES 2
43 static XF86AttributeRec TexturedAttributes[NUM_TEXTURED_ATTRIBUTES] = {
44 {XvSettable | XvGettable, -128, 127, "XV_BRIGHTNESS"},
45 {XvSettable | XvGettable, 0, 255, "XV_CONTRAST"}
46 };
47
48 #define NUM_FORMATS 3
49 static XF86VideoFormatRec Formats[NUM_FORMATS] = {
50 {15, TrueColor}, {16, TrueColor}, {24, TrueColor}
51 };
52
53 static XF86VideoEncodingRec DummyEncoding[1] = {
54 {
55 0,
56 "XV_IMAGE",
57 IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT,
58 {1, 1}
59 }
60 };
61
62 #define NUM_IMAGES 2
63 static XF86ImageRec Images[NUM_IMAGES] = {
64 XVIMAGE_UYVY,
65 XVIMAGE_YUY2,
66 };
67
68 struct xorg_xv_port_priv {
69 struct xorg_renderer *r;
70
71 RegionRec clip;
72
73 int brightness;
74 int contrast;
75
76 int current_set;
77 /* juggle two sets of seperate Y, U and V
78 * textures */
79 struct pipe_texture *yuv[2][3];
80 };
81
82
83 static void
84 stop_video(ScrnInfoPtr pScrn, pointer data, Bool shutdown)
85 {
86 struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
87
88 REGION_EMPTY(pScrn->pScreen, &priv->clip);
89 }
90
91 static int
92 set_port_attribute(ScrnInfoPtr pScrn,
93 Atom attribute, INT32 value, pointer data)
94 {
95 struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
96
97 if (attribute == xvBrightness) {
98 if ((value < -128) || (value > 127))
99 return BadValue;
100 priv->brightness = value;
101 } else if (attribute == xvContrast) {
102 if ((value < 0) || (value > 255))
103 return BadValue;
104 priv->contrast = value;
105 } else
106 return BadMatch;
107
108 return Success;
109 }
110
111 static int
112 get_port_attribute(ScrnInfoPtr pScrn,
113 Atom attribute, INT32 * value, pointer data)
114 {
115 struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
116
117 if (attribute == xvBrightness)
118 *value = priv->brightness;
119 else if (attribute == xvContrast)
120 *value = priv->contrast;
121 else
122 return BadMatch;
123
124 return Success;
125 }
126
127 static void
128 query_best_size(ScrnInfoPtr pScrn,
129 Bool motion,
130 short vid_w, short vid_h,
131 short drw_w, short drw_h,
132 unsigned int *p_w, unsigned int *p_h, pointer data)
133 {
134 if (vid_w > (drw_w << 1))
135 drw_w = vid_w >> 1;
136 if (vid_h > (drw_h << 1))
137 drw_h = vid_h >> 1;
138
139 *p_w = drw_w;
140 *p_h = drw_h;
141 }
142
143 static INLINE struct pipe_texture *
144 create_component_texture(struct pipe_context *pipe,
145 int width, int height)
146 {
147 struct pipe_screen *screen = pipe->screen;
148 struct pipe_texture *tex = 0;
149 struct pipe_texture templ;
150
151 memset(&templ, 0, sizeof(templ));
152 templ.target = PIPE_TEXTURE_2D;
153 templ.format = PIPE_FORMAT_L8_UNORM;
154 templ.last_level = 0;
155 templ.width[0] = width;
156 templ.height[0] = height;
157 templ.depth[0] = 1;
158 pf_get_block(PIPE_FORMAT_L8_UNORM, &templ.block);
159 templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
160
161 tex = screen->texture_create(screen, &templ);
162
163 return tex;
164 }
165
166 static int
167 check_yuv_textures(struct xorg_xv_port_priv *priv, int width, int height)
168 {
169 struct pipe_texture **dst = priv->yuv[priv->current_set];
170 if (!dst[0] ||
171 dst[0]->width[0] != width ||
172 dst[0]->height[0] != height) {
173 pipe_texture_reference(&dst[0], NULL);
174 }
175 if (!dst[1] ||
176 dst[1]->width[0] != width ||
177 dst[1]->height[0] != height) {
178 pipe_texture_reference(&dst[1], NULL);
179 }
180 if (!dst[2] ||
181 dst[2]->width[0] != width ||
182 dst[2]->height[0] != height) {
183 pipe_texture_reference(&dst[2], NULL);
184 }
185
186 if (!dst[0])
187 dst[0] = create_component_texture(priv->r->pipe, width, height);
188
189 if (!dst[1])
190 dst[1] = create_component_texture(priv->r->pipe, width, height);
191
192 if (!dst[2])
193 dst[2] = create_component_texture(priv->r->pipe, width, height);
194
195 if (!dst[0] || !dst[1] || !dst[2])
196 return BadAlloc;
197
198 return Success;
199 }
200
201 static void
202 copy_packed_data(ScrnInfoPtr pScrn,
203 struct xorg_xv_port_priv *port,
204 int id,
205 unsigned char *buf,
206 int srcPitch,
207 int left,
208 int top,
209 int w, int h)
210 {
211 unsigned char *src;
212 int i, j;
213 struct pipe_texture **dst = port->yuv[port->current_set];
214 struct pipe_transfer *ytrans, *utrans, *vtrans;
215 struct pipe_screen *screen = port->r->pipe->screen;
216 char *ymap, *vmap, *umap;
217 unsigned char y1, y2, u, v;
218 int yidx, uidx, vidx;
219 int y_array_size = w * h;
220
221 src = buf + (top * srcPitch) + (left << 1);
222
223 ytrans = screen->get_tex_transfer(screen, dst[0],
224 0, 0, 0,
225 PIPE_TRANSFER_WRITE,
226 left, top, w, h);
227 utrans = screen->get_tex_transfer(screen, dst[1],
228 0, 0, 0,
229 PIPE_TRANSFER_WRITE,
230 left, top, w, h);
231 vtrans = screen->get_tex_transfer(screen, dst[2],
232 0, 0, 0,
233 PIPE_TRANSFER_WRITE,
234 left, top, w, h);
235
236 ymap = (char*)screen->transfer_map(screen, ytrans);
237 umap = (char*)screen->transfer_map(screen, utrans);
238 vmap = (char*)screen->transfer_map(screen, vtrans);
239
240 yidx = uidx = vidx = 0;
241
242 switch (id) {
243 case FOURCC_YV12: {
244 for (i = 0; i < w; ++i) {
245 for (j = 0; i < h; ++j) {
246 /*XXX use src? */
247 y1 = buf[j*w + i];
248 u = buf[(j/2) * (w/2) + i/2 + y_array_size];
249 v = buf[(j/2) * (w/2) + i/2 + y_array_size + y_array_size/4];
250 ymap[yidx++] = y1;
251 umap[uidx++] = u;
252 vmap[vidx++] = v;
253 }
254 }
255 }
256 break;
257 case FOURCC_UYVY:
258 for (i = 0; i < y_array_size; i +=2 ) {
259 /* extracting two pixels */
260 u = buf[0];
261 y1 = buf[1];
262 v = buf[2];
263 y2 = buf[3];
264 buf += 4;
265
266 ymap[yidx++] = y1;
267 ymap[yidx++] = y2;
268 umap[uidx++] = u;
269 umap[uidx++] = u;
270 vmap[vidx++] = v;
271 vmap[vidx++] = v;
272 }
273 break;
274 case FOURCC_YUY2:
275 for (i = 0; i < y_array_size; i +=2 ) {
276 /* extracting two pixels */
277 y1 = buf[0];
278 u = buf[1];
279 y2 = buf[2];
280 v = buf[3];
281
282 buf += 4;
283
284 ymap[yidx++] = y1;
285 ymap[yidx++] = y2;
286 umap[uidx++] = u;
287 umap[uidx++] = u;
288 vmap[vidx++] = v;
289 vmap[vidx++] = v;
290 }
291 break;
292 default:
293 debug_assert(!"Unsupported yuv format!");
294 break;
295 }
296
297 screen->transfer_unmap(screen, ytrans);
298 screen->transfer_unmap(screen, utrans);
299 screen->transfer_unmap(screen, vtrans);
300 screen->tex_transfer_destroy(ytrans);
301 screen->tex_transfer_destroy(utrans);
302 screen->tex_transfer_destroy(vtrans);
303 }
304
305
306 static void
307 setup_vs_video_constants(struct xorg_renderer *r, struct exa_pixmap_priv *dst)
308 {
309 int width = dst->tex->width[0];
310 int height = dst->tex->height[0];
311 const int param_bytes = 8 * sizeof(float);
312 float vs_consts[8] = {
313 2.f/width, 2.f/height, 1, 1,
314 -1, -1, 0, 0
315 };
316
317 renderer_set_constants(r, PIPE_SHADER_VERTEX,
318 vs_consts, param_bytes);
319 }
320
321 static void
322 setup_fs_video_constants(struct xorg_renderer *r, boolean hdtv)
323 {
324 const int param_bytes = 12 * sizeof(float);
325 const float *video_constants = (hdtv) ? bt_709 : bt_601;
326
327 renderer_set_constants(r, PIPE_SHADER_FRAGMENT,
328 video_constants, param_bytes);
329 }
330
331 static void
332 draw_yuv(struct xorg_xv_port_priv *port, int src_x, int src_y,
333 int dst_x, int dst_y,
334 int w, int h)
335 {
336 int pos[4] = {src_x, src_y,
337 dst_x, dst_y};
338 struct pipe_texture **textures = port->yuv[port->current_set];
339
340 renderer_draw_textures(port->r,
341 pos, w, h,
342 textures,
343 3, /*bound samplers/textures */
344 NULL, NULL /* no transformations */);
345 }
346
347 static void
348 bind_blend_state(struct xorg_xv_port_priv *port)
349 {
350 struct pipe_blend_state blend;
351
352 memset(&blend, 0, sizeof(struct pipe_blend_state));
353 blend.blend_enable = 1;
354 blend.colormask |= PIPE_MASK_RGBA;
355
356 /* porter&duff src */
357 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
358 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
359 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
360 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
361
362 cso_set_blend(port->r->cso, &blend);
363 }
364
365
366 static void
367 bind_shaders(struct xorg_xv_port_priv *port)
368 {
369 unsigned vs_traits = 0, fs_traits = 0;
370 struct xorg_shader shader;
371
372 vs_traits |= VS_YUV;
373 fs_traits |= FS_YUV;
374
375 shader = xorg_shaders_get(port->r->shaders, vs_traits, fs_traits);
376 cso_set_vertex_shader_handle(port->r->cso, shader.vs);
377 cso_set_fragment_shader_handle(port->r->cso, shader.fs);
378 }
379
380 static INLINE void
381 conditional_flush(struct pipe_context *pipe, struct pipe_texture **tex,
382 int num)
383 {
384 int i;
385 for (i = 0; i < num; ++i) {
386 if (tex[i] && pipe->is_texture_referenced(pipe, tex[i], 0, 0) &
387 PIPE_REFERENCED_FOR_WRITE) {
388 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
389 return;
390 }
391 }
392 }
393
394 static void
395 bind_samplers(struct xorg_xv_port_priv *port)
396 {
397 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
398 struct pipe_sampler_state sampler;
399 struct pipe_texture **dst = port->yuv[port->current_set];
400
401 memset(&sampler, 0, sizeof(struct pipe_sampler_state));
402
403 conditional_flush(port->r->pipe, dst, 3);
404
405 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
406 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
407 sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
408 sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
409 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
410 sampler.normalized_coords = 1;
411
412 samplers[0] = &sampler;
413 samplers[1] = &sampler;
414 samplers[2] = &sampler;
415
416
417 cso_set_samplers(port->r->cso, 3,
418 (const struct pipe_sampler_state **)samplers);
419 cso_set_sampler_textures(port->r->cso, 3,
420 dst);
421 }
422
423 static int
424 display_video(ScrnInfoPtr pScrn, struct xorg_xv_port_priv *pPriv, int id,
425 RegionPtr dstRegion,
426 int src_x, int src_y, int src_w, int src_h,
427 int dstX, int dstY,
428 short width, short height,
429 PixmapPtr pPixmap)
430 {
431 modesettingPtr ms = modesettingPTR(pScrn);
432 BoxPtr pbox;
433 int nbox;
434 int dxo, dyo;
435 Bool hdtv;
436 int x, y, w, h;
437 struct exa_pixmap_priv *dst = exaGetPixmapDriverPrivate(pPixmap);
438
439 if (!dst || !dst->tex)
440 XORG_FALLBACK("Xv destination %s", !dst ? "!dst" : "!dst->tex");
441
442 hdtv = ((src_w >= RES_720P_X) && (src_h >= RES_720P_Y));
443
444 REGION_TRANSLATE(pScrn->pScreen, dstRegion, -pPixmap->screen_x,
445 -pPixmap->screen_y);
446
447 dxo = dstRegion->extents.x1;
448 dyo = dstRegion->extents.y1;
449
450 pbox = REGION_RECTS(dstRegion);
451 nbox = REGION_NUM_RECTS(dstRegion);
452
453 renderer_bind_framebuffer(pPriv->r, dst);
454 renderer_bind_viewport(pPriv->r, dst);
455 bind_blend_state(pPriv);
456 renderer_bind_rasterizer(pPriv->r);
457 bind_shaders(pPriv);
458 bind_samplers(pPriv);
459 setup_vs_video_constants(pPriv->r, dst);
460 setup_fs_video_constants(pPriv->r, hdtv);
461
462 while (nbox--) {
463 int box_x1 = pbox->x1;
464 int box_y1 = pbox->y1;
465 int box_x2 = pbox->x2;
466 int box_y2 = pbox->y2;
467
468 x = box_x1;
469 y = box_y1;
470 w = box_x2 - box_x1;
471 h = box_y2 - box_y1;
472
473 draw_yuv(pPriv, x, y, x, y, w, h);
474
475 pbox++;
476 }
477 DamageDamageRegion(&pPixmap->drawable, dstRegion);
478
479 return TRUE;
480 }
481
482 static int
483 put_image(ScrnInfoPtr pScrn,
484 short src_x, short src_y,
485 short drw_x, short drw_y,
486 short src_w, short src_h,
487 short drw_w, short drw_h,
488 int id, unsigned char *buf,
489 short width, short height,
490 Bool sync, RegionPtr clipBoxes, pointer data,
491 DrawablePtr pDraw)
492 {
493 struct xorg_xv_port_priv *pPriv = (struct xorg_xv_port_priv *) data;
494 ScreenPtr pScreen = screenInfo.screens[pScrn->scrnIndex];
495 PixmapPtr pPixmap;
496 INT32 x1, x2, y1, y2;
497 int srcPitch;
498 BoxRec dstBox;
499 int ret;
500
501 /* Clip */
502 x1 = src_x;
503 x2 = src_x + src_w;
504 y1 = src_y;
505 y2 = src_y + src_h;
506
507 dstBox.x1 = drw_x;
508 dstBox.x2 = drw_x + drw_w;
509 dstBox.y1 = drw_y;
510 dstBox.y2 = drw_y + drw_h;
511
512 if (!xf86XVClipVideoHelper(&dstBox, &x1, &x2, &y1, &y2, clipBoxes,
513 width, height))
514 return Success;
515
516 switch (id) {
517 case FOURCC_UYVY:
518 case FOURCC_YUY2:
519 default:
520 srcPitch = width << 1;
521 break;
522 }
523
524 ret = check_yuv_textures(pPriv, width, height);
525
526 if (ret)
527 return ret;
528
529 copy_packed_data(pScrn, pPriv, id, buf, srcPitch,
530 src_x, src_y, width, height);
531
532 if (pDraw->type == DRAWABLE_WINDOW) {
533 pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr)pDraw);
534 } else {
535 pPixmap = (PixmapPtr)pDraw;
536 }
537
538 display_video(pScrn, pPriv, id, clipBoxes,
539 src_x, src_y, src_w, src_h,
540 drw_x, drw_y,
541 drw_w, drw_h, pPixmap);
542
543 pPriv->current_set = (pPriv->current_set + 1) & 1;
544 return Success;
545 }
546
547 static int
548 query_image_attributes(ScrnInfoPtr pScrn,
549 int id,
550 unsigned short *w, unsigned short *h,
551 int *pitches, int *offsets)
552 {
553 int size;
554
555 if (*w > IMAGE_MAX_WIDTH)
556 *w = IMAGE_MAX_WIDTH;
557 if (*h > IMAGE_MAX_HEIGHT)
558 *h = IMAGE_MAX_HEIGHT;
559
560 *w = (*w + 1) & ~1;
561 if (offsets)
562 offsets[0] = 0;
563
564 switch (id) {
565 case FOURCC_UYVY:
566 case FOURCC_YUY2:
567 default:
568 size = *w << 1;
569 if (pitches)
570 pitches[0] = size;
571 size *= *h;
572 break;
573 }
574
575 return size;
576 }
577
578 static struct xorg_xv_port_priv *
579 port_priv_create(struct xorg_renderer *r)
580 {
581 struct xorg_xv_port_priv *priv = NULL;
582
583 priv = calloc(1, sizeof(struct xorg_xv_port_priv));
584
585 if (!priv)
586 return NULL;
587
588 priv->r = r;
589
590 REGION_NULL(pScreen, &priv->clip);
591
592 debug_assert(priv && priv->r);
593
594 return priv;
595 }
596
597 static XF86VideoAdaptorPtr
598 xorg_setup_textured_adapter(ScreenPtr pScreen)
599 {
600 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
601 modesettingPtr ms = modesettingPTR(pScrn);
602 XF86VideoAdaptorPtr adapt;
603 XF86AttributePtr attrs;
604 DevUnion *dev_unions;
605 int nports = 16, i;
606 int nattributes;
607
608 nattributes = NUM_TEXTURED_ATTRIBUTES;
609
610 debug_assert(ms->exa);
611 debug_assert(ms->exa->renderer);
612
613 adapt = calloc(1, sizeof(XF86VideoAdaptorRec));
614 dev_unions = calloc(nports, sizeof(DevUnion));
615 attrs = calloc(nattributes, sizeof(XF86AttributeRec));
616 if (adapt == NULL || dev_unions == NULL || attrs == NULL) {
617 free(adapt);
618 free(dev_unions);
619 free(attrs);
620 return NULL;
621 }
622
623 adapt->type = XvWindowMask | XvInputMask | XvImageMask;
624 adapt->flags = 0;
625 adapt->name = "Gallium3D Textured Video";
626 adapt->nEncodings = 1;
627 adapt->pEncodings = DummyEncoding;
628 adapt->nFormats = NUM_FORMATS;
629 adapt->pFormats = Formats;
630 adapt->nPorts = 0;
631 adapt->pPortPrivates = dev_unions;
632 adapt->nAttributes = nattributes;
633 adapt->pAttributes = attrs;
634 memcpy(attrs, TexturedAttributes, nattributes * sizeof(XF86AttributeRec));
635 adapt->nImages = NUM_IMAGES;
636 adapt->pImages = Images;
637 adapt->PutVideo = NULL;
638 adapt->PutStill = NULL;
639 adapt->GetVideo = NULL;
640 adapt->GetStill = NULL;
641 adapt->StopVideo = stop_video;
642 adapt->SetPortAttribute = set_port_attribute;
643 adapt->GetPortAttribute = get_port_attribute;
644 adapt->QueryBestSize = query_best_size;
645 adapt->PutImage = put_image;
646 adapt->QueryImageAttributes = query_image_attributes;
647
648 for (i = 0; i < nports; i++) {
649 struct xorg_xv_port_priv *priv =
650 port_priv_create(ms->exa->renderer);
651
652 adapt->pPortPrivates[i].ptr = (pointer) (priv);
653 adapt->nPorts++;
654 }
655
656 return adapt;
657 }
658
659 void
660 xorg_init_video(ScreenPtr pScreen)
661 {
662 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
663 /*modesettingPtr ms = modesettingPTR(pScrn);*/
664 XF86VideoAdaptorPtr *adaptors, *new_adaptors = NULL;
665 XF86VideoAdaptorPtr textured_adapter;
666 int num_adaptors;
667
668 num_adaptors = xf86XVListGenericAdaptors(pScrn, &adaptors);
669 new_adaptors = malloc((num_adaptors + 1) * sizeof(XF86VideoAdaptorPtr *));
670 if (new_adaptors == NULL)
671 return;
672
673 memcpy(new_adaptors, adaptors, num_adaptors * sizeof(XF86VideoAdaptorPtr));
674 adaptors = new_adaptors;
675
676 /* Add the adaptors supported by our hardware. First, set up the atoms
677 * that will be used by both output adaptors.
678 */
679 xvBrightness = MAKE_ATOM("XV_BRIGHTNESS");
680 xvContrast = MAKE_ATOM("XV_CONTRAST");
681
682 textured_adapter = xorg_setup_textured_adapter(pScreen);
683
684 debug_assert(textured_adapter);
685
686 if (textured_adapter) {
687 adaptors[num_adaptors++] = textured_adapter;
688 }
689
690 if (num_adaptors) {
691 xf86XVScreenInit(pScreen, adaptors, num_adaptors);
692 } else {
693 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
694 "Disabling Xv because no adaptors could be initialized.\n");
695 }
696
697 free(adaptors);
698 }