1/************************************************************************** 2 * 3 * Copyright 2009 Younes Manton. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include <assert.h> 29 30#include <X11/Xlibint.h> 31#include <X11/extensions/XvMClib.h> 32 33#include "pipe/p_screen.h" 34#include "pipe/p_video_codec.h" 35#include "pipe/p_state.h" 36 37#include "util/u_memory.h" 38#include "util/u_math.h" 39#include "util/format/u_format.h" 40#include "util/u_sampler.h" 41#include "util/u_surface.h" 42#include "util/u_rect.h" 43#include "vl/vl_winsys.h" 44 45#include "xvmc_private.h" 46 47#define FOURCC_RGB 0x0000003 48#define FOURCC_AI44 0x34344941 49#define FOURCC_IA44 0x34344149 50 51static enum pipe_format XvIDToPipe(struct pipe_screen *screen, 52 int xvimage_id) 53{ 54 enum pipe_format ret; 55 assert(screen); 56 57 switch (xvimage_id) { 58 case FOURCC_RGB: 59 ret = PIPE_FORMAT_B8G8R8X8_UNORM; 60 break; 61 62 case FOURCC_AI44: 63 ret = PIPE_FORMAT_R4A4_UNORM; 64 if (!screen->is_format_supported( 65 screen, ret, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW)) 66 ret = PIPE_FORMAT_B4G4R4A4_UNORM; 67 break; 68 69 case FOURCC_IA44: 70 ret = PIPE_FORMAT_A4R4_UNORM; 71 if (!screen->is_format_supported( 72 screen, ret, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW)) 73 ret = PIPE_FORMAT_B4G4R4A4_UNORM; 74 break; 75 76 default: 77 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id); 78 return PIPE_FORMAT_NONE; 79 } 80 81 if (!screen->is_format_supported( 82 screen, ret, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW)) { 83 XVMC_MSG(XVMC_ERR, "[XvMC] Unsupported 2D format %s for Xv image ID 0x%08X.\n", util_format_name(ret), xvimage_id); 84 ret = PIPE_FORMAT_NONE; 85 } 86 return ret; 87 88} 89 90static unsigned NumPaletteEntries4XvID(int xvimage_id) 91{ 92 switch (xvimage_id) { 93 case FOURCC_RGB: 94 return 0; 95 96 case FOURCC_AI44: 97 case FOURCC_IA44: 98 return 16; 99 100 default: 101 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id); 102 return 0; 103 } 104} 105 106static int PipeToComponentOrder(struct pipe_screen *screen, 107 enum pipe_format format, 108 enum pipe_format *palette_format, 109 char *component_order) 110{ 111 assert(screen); 112 assert(component_order); 113 assert(palette_format); 114 115 switch (format) { 116 case PIPE_FORMAT_B8G8R8X8_UNORM: 117 return 0; 118 119 case PIPE_FORMAT_A4R4_UNORM: 120 case PIPE_FORMAT_R4A4_UNORM: 121 case PIPE_FORMAT_B4G4R4A4_UNORM: 122 *palette_format = PIPE_FORMAT_R8G8B8X8_UNORM; 123 component_order[0] = 'Y'; 124 component_order[1] = 'U'; 125 component_order[2] = 'V'; 126 component_order[3] = 'A'; 127 if (!screen->is_format_supported( 128 screen, *palette_format, PIPE_TEXTURE_1D, 0, 0, 129 PIPE_BIND_SAMPLER_VIEW)) { 130 /* One of these formats better be supported... */ 131 *palette_format = PIPE_FORMAT_B8G8R8X8_UNORM; 132 component_order[0] = 'V'; 133 component_order[2] = 'Y'; 134 } 135 return 4; 136 137 default: 138 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized PIPE_FORMAT 0x%08X.\n", format); 139 component_order[0] = 0; 140 component_order[1] = 0; 141 component_order[2] = 0; 142 component_order[3] = 0; 143 return 0; 144 } 145} 146 147static Status Validate(Display *dpy, XvPortID port, int surface_type_id, int xvimage_id) 148{ 149 XvImageFormatValues *subpictures; 150 int num_subpics; 151 int i; 152 153 subpictures = XvMCListSubpictureTypes(dpy, port, surface_type_id, &num_subpics); 154 if (num_subpics < 1) { 155 free(subpictures); 156 return BadMatch; 157 } 158 if (!subpictures) 159 return BadAlloc; 160 161 for (i = 0; i < num_subpics; ++i) { 162 if (subpictures[i].id == xvimage_id) { 163 XVMC_MSG(XVMC_TRACE, "[XvMC] Found requested subpicture format.\n" \ 164 "[XvMC] port=%u\n" \ 165 "[XvMC] surface id=0x%08X\n" \ 166 "[XvMC] image id=0x%08X\n" \ 167 "[XvMC] type=%08X\n" \ 168 "[XvMC] byte order=%08X\n" \ 169 "[XvMC] bits per pixel=%u\n" \ 170 "[XvMC] format=%08X\n" \ 171 "[XvMC] num planes=%d\n", 172 port, surface_type_id, xvimage_id, subpictures[i].type, subpictures[i].byte_order, 173 subpictures[i].bits_per_pixel, subpictures[i].format, subpictures[i].num_planes); 174 if (subpictures[i].type == XvRGB) { 175 XVMC_MSG(XVMC_TRACE, "[XvMC] depth=%d\n" \ 176 "[XvMC] red mask=0x%08X\n" \ 177 "[XvMC] green mask=0x%08X\n" \ 178 "[XvMC] blue mask=0x%08X\n", 179 subpictures[i].depth, subpictures[i].red_mask, 180 subpictures[i].green_mask, subpictures[i].blue_mask); 181 } 182 else if (subpictures[i].type == XvYUV) { 183 XVMC_MSG(XVMC_TRACE, "[XvMC] y sample bits=0x%08X\n" \ 184 "[XvMC] u sample bits=0x%08X\n" \ 185 "[XvMC] v sample bits=0x%08X\n" \ 186 "[XvMC] horz y period=%u\n" \ 187 "[XvMC] horz u period=%u\n" \ 188 "[XvMC] horz v period=%u\n" \ 189 "[XvMC] vert y period=%u\n" \ 190 "[XvMC] vert u period=%u\n" \ 191 "[XvMC] vert v period=%u\n", 192 subpictures[i].y_sample_bits, subpictures[i].u_sample_bits, subpictures[i].v_sample_bits, 193 subpictures[i].horz_y_period, subpictures[i].horz_u_period, subpictures[i].horz_v_period, 194 subpictures[i].vert_y_period, subpictures[i].vert_u_period, subpictures[i].vert_v_period); 195 } 196 break; 197 } 198 } 199 200 free(subpictures); 201 202 return i < num_subpics ? Success : BadMatch; 203} 204 205static void 206upload_sampler(struct pipe_context *pipe, struct pipe_sampler_view *dst, 207 const struct pipe_box *dst_box, const void *src, unsigned src_stride, 208 unsigned src_x, unsigned src_y) 209{ 210 struct pipe_transfer *transfer; 211 void *map; 212 213 map = pipe->texture_map(pipe, dst->texture, 0, PIPE_MAP_WRITE, 214 dst_box, &transfer); 215 if (!map) 216 return; 217 218 util_copy_rect(map, dst->texture->format, transfer->stride, 0, 0, 219 dst_box->width, dst_box->height, 220 src, src_stride, src_x, src_y); 221 222 pipe->texture_unmap(pipe, transfer); 223} 224 225static void 226upload_sampler_convert(struct pipe_context *pipe, struct pipe_sampler_view *dst, 227 const struct pipe_box *dst_box, const XvImage *image, 228 unsigned src_x, unsigned src_y) 229{ 230 struct pipe_transfer *transfer; 231 int i, j; 232 char *map, *src; 233 234 map = pipe->texture_map(pipe, dst->texture, 0, PIPE_MAP_WRITE, 235 dst_box, &transfer); 236 if (!map) 237 return; 238 239 src = image->data; 240 src += src_y * image->width + src_x; 241 if (image->id == FOURCC_AI44) { 242 /* The format matches what we want, we just have to insert dummy 243 * bytes. So just copy the same value in twice. 244 */ 245 for (i = 0; i < dst_box->height; i++, map += transfer->stride, src += image->width) 246 for (j = 0; j < dst_box->width; j++) 247 map[j * 2 + 0] = map[j * 2 + 1] = src[j]; 248 } else { 249 assert(image->id == FOURCC_IA44); 250 /* Same idea as above, but we have to swap the low and high nibbles. 251 */ 252 for (i = 0; i < dst_box->height; i++, map += transfer->stride, src += image->width) 253 for (j = 0; j < dst_box->width; j++) 254 map[j * 2 + 0] = map[j * 2 + 1] = (src[j] >> 4) | (src[j] << 4); 255 } 256 257 pipe->texture_unmap(pipe, transfer); 258} 259 260PUBLIC 261Status XvMCCreateSubpicture(Display *dpy, XvMCContext *context, XvMCSubpicture *subpicture, 262 unsigned short width, unsigned short height, int xvimage_id) 263{ 264 XvMCContextPrivate *context_priv; 265 XvMCSubpicturePrivate *subpicture_priv; 266 struct pipe_context *pipe; 267 struct pipe_resource tex_templ, *tex; 268 struct pipe_sampler_view sampler_templ; 269 enum pipe_format palette_format; 270 Status ret; 271 272 XVMC_MSG(XVMC_TRACE, "[XvMC] Creating subpicture %p.\n", subpicture); 273 274 assert(dpy); 275 276 if (!context) 277 return XvMCBadContext; 278 279 context_priv = context->privData; 280 pipe = context_priv->pipe; 281 282 if (!subpicture) 283 return XvMCBadSubpicture; 284 285 if (width > context_priv->subpicture_max_width || 286 height > context_priv->subpicture_max_height) 287 return BadValue; 288 289 ret = Validate(dpy, context->port, context->surface_type_id, xvimage_id); 290 if (ret != Success) 291 return ret; 292 293 subpicture_priv = CALLOC(1, sizeof(XvMCSubpicturePrivate)); 294 if (!subpicture_priv) 295 return BadAlloc; 296 297 memset(&tex_templ, 0, sizeof(tex_templ)); 298 tex_templ.target = PIPE_TEXTURE_2D; 299 tex_templ.format = XvIDToPipe(pipe->screen, xvimage_id); 300 tex_templ.last_level = 0; 301 if (pipe->screen->get_video_param(pipe->screen, 302 PIPE_VIDEO_PROFILE_UNKNOWN, 303 PIPE_VIDEO_ENTRYPOINT_UNKNOWN, 304 PIPE_VIDEO_CAP_NPOT_TEXTURES)) { 305 tex_templ.width0 = width; 306 tex_templ.height0 = height; 307 } 308 else { 309 tex_templ.width0 = util_next_power_of_two(width); 310 tex_templ.height0 = util_next_power_of_two(height); 311 } 312 tex_templ.depth0 = 1; 313 tex_templ.array_size = 1; 314 tex_templ.usage = PIPE_USAGE_DYNAMIC; 315 tex_templ.bind = PIPE_BIND_SAMPLER_VIEW; 316 tex_templ.flags = 0; 317 318 tex = pipe->screen->resource_create(pipe->screen, &tex_templ); 319 320 memset(&sampler_templ, 0, sizeof(sampler_templ)); 321 u_sampler_view_default_template(&sampler_templ, tex, tex->format); 322 323 subpicture_priv->sampler = pipe->create_sampler_view(pipe, tex, &sampler_templ); 324 pipe_resource_reference(&tex, NULL); 325 if (!subpicture_priv->sampler) { 326 FREE(subpicture_priv); 327 return BadAlloc; 328 } 329 330 subpicture_priv->context = context; 331 subpicture->subpicture_id = XAllocID(dpy); 332 subpicture->context_id = context->context_id; 333 subpicture->xvimage_id = xvimage_id; 334 subpicture->width = width; 335 subpicture->height = height; 336 subpicture->num_palette_entries = NumPaletteEntries4XvID(xvimage_id); 337 subpicture->entry_bytes = PipeToComponentOrder( 338 pipe->screen, tex_templ.format, &palette_format, 339 subpicture->component_order); 340 subpicture->privData = subpicture_priv; 341 342 if (subpicture->num_palette_entries > 0) { 343 tex_templ.target = PIPE_TEXTURE_1D; 344 tex_templ.format = palette_format; 345 tex_templ.width0 = subpicture->num_palette_entries; 346 tex_templ.height0 = 1; 347 tex_templ.usage = PIPE_USAGE_DEFAULT; 348 349 tex = pipe->screen->resource_create(pipe->screen, &tex_templ); 350 351 memset(&sampler_templ, 0, sizeof(sampler_templ)); 352 u_sampler_view_default_template(&sampler_templ, tex, tex->format); 353 sampler_templ.swizzle_a = PIPE_SWIZZLE_1; 354 subpicture_priv->palette = pipe->create_sampler_view(pipe, tex, &sampler_templ); 355 pipe_resource_reference(&tex, NULL); 356 if (!subpicture_priv->sampler) { 357 FREE(subpicture_priv); 358 return BadAlloc; 359 } 360 } 361 362 SyncHandle(); 363 364 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p created.\n", subpicture); 365 366 return Success; 367} 368 369PUBLIC 370Status XvMCClearSubpicture(Display *dpy, XvMCSubpicture *subpicture, short x, short y, 371 unsigned short width, unsigned short height, unsigned int color) 372{ 373 XvMCSubpicturePrivate *subpicture_priv; 374 XvMCContextPrivate *context_priv; 375 struct pipe_context *pipe; 376 struct pipe_sampler_view *dst; 377 struct pipe_box dst_box = {x, y, 0, width, height, 1}; 378 struct pipe_transfer *transfer; 379 union util_color uc; 380 void *map; 381 382 assert(dpy); 383 384 if (!subpicture) 385 return XvMCBadSubpicture; 386 387 /* Convert color to float */ 388 util_format_unpack_rgba(PIPE_FORMAT_B8G8R8A8_UNORM, uc.f, &color, 1); 389 390 subpicture_priv = subpicture->privData; 391 context_priv = subpicture_priv->context->privData; 392 pipe = context_priv->pipe; 393 dst = subpicture_priv->sampler; 394 395 /* TODO: Assert clear rect is within bounds? Or clip? */ 396 map = pipe->texture_map(pipe, dst->texture, 0, PIPE_MAP_WRITE, 397 &dst_box, &transfer); 398 if (!map) 399 return XvMCBadSubpicture; 400 401 util_fill_rect(map, dst->texture->format, transfer->stride, 0, 0, 402 dst_box.width, dst_box.height, &uc); 403 404 pipe->texture_unmap(pipe, transfer); 405 return Success; 406} 407 408PUBLIC 409Status XvMCCompositeSubpicture(Display *dpy, XvMCSubpicture *subpicture, XvImage *image, 410 short srcx, short srcy, unsigned short width, unsigned short height, 411 short dstx, short dsty) 412{ 413 XvMCSubpicturePrivate *subpicture_priv; 414 XvMCContextPrivate *context_priv; 415 struct pipe_context *pipe; 416 struct pipe_box dst_box = {dstx, dsty, 0, width, height, 1}; 417 unsigned src_stride; 418 419 XVMC_MSG(XVMC_TRACE, "[XvMC] Compositing subpicture %p.\n", subpicture); 420 421 assert(dpy); 422 423 if (!subpicture) 424 return XvMCBadSubpicture; 425 426 assert(image); 427 428 if (subpicture->xvimage_id != image->id) 429 return BadMatch; 430 431 /* No planar support for now */ 432 if (image->num_planes != 1) 433 return BadMatch; 434 435 subpicture_priv = subpicture->privData; 436 context_priv = subpicture_priv->context->privData; 437 pipe = context_priv->pipe; 438 439 /* clipping should be done by upload_sampler and regardles what the documentation 440 says image->pitches[0] doesn't seems to be in bytes, so don't use it */ 441 if ((image->id == FOURCC_IA44 || image->id == FOURCC_AI44) && 442 subpicture_priv->sampler->texture->format == PIPE_FORMAT_B4G4R4A4_UNORM) { 443 upload_sampler_convert(pipe, subpicture_priv->sampler, &dst_box, image, srcx, srcy); 444 } else { 445 src_stride = image->width * util_format_get_blocksize(subpicture_priv->sampler->texture->format); 446 upload_sampler(pipe, subpicture_priv->sampler, &dst_box, image->data, src_stride, srcx, srcy); 447 } 448 449 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p composited.\n", subpicture); 450 451 return Success; 452} 453 454PUBLIC 455Status XvMCDestroySubpicture(Display *dpy, XvMCSubpicture *subpicture) 456{ 457 XvMCSubpicturePrivate *subpicture_priv; 458 459 XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying subpicture %p.\n", subpicture); 460 461 assert(dpy); 462 463 if (!subpicture) 464 return XvMCBadSubpicture; 465 466 subpicture_priv = subpicture->privData; 467 pipe_sampler_view_reference(&subpicture_priv->sampler, NULL); 468 pipe_sampler_view_reference(&subpicture_priv->palette, NULL); 469 FREE(subpicture_priv); 470 471 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p destroyed.\n", subpicture); 472 473 return Success; 474} 475 476PUBLIC 477Status XvMCSetSubpicturePalette(Display *dpy, XvMCSubpicture *subpicture, unsigned char *palette) 478{ 479 XvMCSubpicturePrivate *subpicture_priv; 480 XvMCContextPrivate *context_priv; 481 struct pipe_context *pipe; 482 struct pipe_box dst_box = {0, 0, 0, 0, 1, 1}; 483 484 assert(dpy); 485 assert(palette); 486 487 if (!subpicture) 488 return XvMCBadSubpicture; 489 490 subpicture_priv = subpicture->privData; 491 context_priv = subpicture_priv->context->privData; 492 pipe = context_priv->pipe; 493 494 dst_box.width = subpicture->num_palette_entries; 495 496 upload_sampler(pipe, subpicture_priv->palette, &dst_box, palette, 0, 0, 0); 497 498 XVMC_MSG(XVMC_TRACE, "[XvMC] Palette of Subpicture %p set.\n", subpicture); 499 500 return Success; 501} 502 503PUBLIC 504Status XvMCBlendSubpicture(Display *dpy, XvMCSurface *target_surface, XvMCSubpicture *subpicture, 505 short subx, short suby, unsigned short subw, unsigned short subh, 506 short surfx, short surfy, unsigned short surfw, unsigned short surfh) 507{ 508 struct u_rect src_rect = {subx, subx + subw, suby, suby + subh}; 509 struct u_rect dst_rect = {surfx, surfx + surfw, surfy, surfy + surfh}; 510 511 XvMCSurfacePrivate *surface_priv; 512 XvMCSubpicturePrivate *subpicture_priv; 513 514 XVMC_MSG(XVMC_TRACE, "[XvMC] Associating subpicture %p with surface %p.\n", subpicture, target_surface); 515 516 assert(dpy); 517 518 if (!target_surface) 519 return XvMCBadSurface; 520 521 if (!subpicture) 522 return XvMCBadSubpicture; 523 524 if (target_surface->context_id != subpicture->context_id) 525 return BadMatch; 526 527 /* TODO: Verify against subpicture independent scaling */ 528 529 surface_priv = target_surface->privData; 530 subpicture_priv = subpicture->privData; 531 532 /* TODO: Assert rects are within bounds? Or clip? */ 533 subpicture_priv->src_rect = src_rect; 534 subpicture_priv->dst_rect = dst_rect; 535 536 surface_priv->subpicture = subpicture; 537 subpicture_priv->surface = target_surface; 538 539 return Success; 540} 541 542PUBLIC 543Status XvMCBlendSubpicture2(Display *dpy, XvMCSurface *source_surface, XvMCSurface *target_surface, 544 XvMCSubpicture *subpicture, short subx, short suby, unsigned short subw, unsigned short subh, 545 short surfx, short surfy, unsigned short surfw, unsigned short surfh) 546{ 547 assert(dpy); 548 549 if (!source_surface || !target_surface) 550 return XvMCBadSurface; 551 552 if (!subpicture) 553 return XvMCBadSubpicture; 554 555 if (source_surface->context_id != subpicture->context_id) 556 return BadMatch; 557 558 if (source_surface->context_id != subpicture->context_id) 559 return BadMatch; 560 561 /* TODO: Assert rects are within bounds? Or clip? */ 562 563 return Success; 564} 565 566PUBLIC 567Status XvMCSyncSubpicture(Display *dpy, XvMCSubpicture *subpicture) 568{ 569 assert(dpy); 570 571 if (!subpicture) 572 return XvMCBadSubpicture; 573 574 return Success; 575} 576 577PUBLIC 578Status XvMCFlushSubpicture(Display *dpy, XvMCSubpicture *subpicture) 579{ 580 assert(dpy); 581 582 if (!subpicture) 583 return XvMCBadSubpicture; 584 585 return Success; 586} 587 588PUBLIC 589Status XvMCGetSubpictureStatus(Display *dpy, XvMCSubpicture *subpicture, int *status) 590{ 591 assert(dpy); 592 593 if (!subpicture) 594 return XvMCBadSubpicture; 595 596 assert(status); 597 598 /* TODO */ 599 *status = 0; 600 601 return Success; 602} 603