1 /**************************************************************************
3 * Copyright 2009 Younes Manton.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 #include <X11/Xlibint.h>
31 #include <X11/extensions/XvMClib.h>
33 #include "pipe/p_screen.h"
34 #include "pipe/p_video_codec.h"
35 #include "pipe/p_state.h"
37 #include "util/u_memory.h"
38 #include "util/u_math.h"
39 #include "util/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"
45 #include "xvmc_private.h"
47 #define FOURCC_RGB 0x0000003
48 #define FOURCC_AI44 0x34344941
49 #define FOURCC_IA44 0x34344149
51 static enum pipe_format
XvIDToPipe(struct pipe_screen
*screen
,
59 ret
= PIPE_FORMAT_B8G8R8X8_UNORM
;
63 ret
= PIPE_FORMAT_R4A4_UNORM
;
64 if (!screen
->is_format_supported(
65 screen
, ret
, PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
))
66 ret
= PIPE_FORMAT_B4G4R4A4_UNORM
;
70 ret
= PIPE_FORMAT_A4R4_UNORM
;
71 if (!screen
->is_format_supported(
72 screen
, ret
, PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
))
73 ret
= PIPE_FORMAT_B4G4R4A4_UNORM
;
77 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
78 return PIPE_FORMAT_NONE
;
81 if (!screen
->is_format_supported(
82 screen
, ret
, PIPE_TEXTURE_2D
, 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
;
90 static unsigned NumPaletteEntries4XvID(int xvimage_id
)
101 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
106 static int PipeToComponentOrder(struct pipe_screen
*screen
,
107 enum pipe_format format
,
108 enum pipe_format
*palette_format
,
109 char *component_order
)
112 assert(component_order
);
113 assert(palette_format
);
116 case PIPE_FORMAT_B8G8R8X8_UNORM
:
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,
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';
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;
147 static Status
Validate(Display
*dpy
, XvPortID port
, int surface_type_id
, int xvimage_id
)
149 XvImageFormatValues
*subpictures
;
153 subpictures
= XvMCListSubpictureTypes(dpy
, port
, surface_type_id
, &num_subpics
);
154 if (num_subpics
< 1) {
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" \
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
);
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
);
202 return i
< num_subpics
? Success
: BadMatch
;
206 upload_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
)
210 struct pipe_transfer
*transfer
;
213 map
= pipe
->transfer_map(pipe
, dst
->texture
, 0, PIPE_TRANSFER_WRITE
,
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
);
222 pipe
->transfer_unmap(pipe
, transfer
);
226 upload_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
)
230 struct pipe_transfer
*transfer
;
234 map
= pipe
->transfer_map(pipe
, dst
->texture
, 0, PIPE_TRANSFER_WRITE
,
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.
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
];
249 assert(image
->id
== FOURCC_IA44
);
250 /* Same idea as above, but we have to swap the low and high nibbles.
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);
257 pipe
->transfer_unmap(pipe
, transfer
);
261 Status
XvMCCreateSubpicture(Display
*dpy
, XvMCContext
*context
, XvMCSubpicture
*subpicture
,
262 unsigned short width
, unsigned short height
, int xvimage_id
)
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
;
272 XVMC_MSG(XVMC_TRACE
, "[XvMC] Creating subpicture %p.\n", subpicture
);
277 return XvMCBadContext
;
279 context_priv
= context
->privData
;
280 pipe
= context_priv
->pipe
;
283 return XvMCBadSubpicture
;
285 if (width
> context_priv
->subpicture_max_width
||
286 height
> context_priv
->subpicture_max_height
)
289 ret
= Validate(dpy
, context
->port
, context
->surface_type_id
, xvimage_id
);
293 subpicture_priv
= CALLOC(1, sizeof(XvMCSubpicturePrivate
));
294 if (!subpicture_priv
)
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
;
309 tex_templ
.width0
= util_next_power_of_two(width
);
310 tex_templ
.height0
= util_next_power_of_two(height
);
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
;
318 tex
= pipe
->screen
->resource_create(pipe
->screen
, &tex_templ
);
320 memset(&sampler_templ
, 0, sizeof(sampler_templ
));
321 u_sampler_view_default_template(&sampler_templ
, tex
, tex
->format
);
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
);
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
;
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
;
349 tex
= pipe
->screen
->resource_create(pipe
->screen
, &tex_templ
);
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
);
364 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p created.\n", subpicture
);
370 Status
XvMCClearSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
, short x
, short y
,
371 unsigned short width
, unsigned short height
, unsigned int color
)
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
;
385 return XvMCBadSubpicture
;
387 /* Convert color to float */
388 util_format_read_4f(PIPE_FORMAT_B8G8R8A8_UNORM
,
392 subpicture_priv
= subpicture
->privData
;
393 context_priv
= subpicture_priv
->context
->privData
;
394 pipe
= context_priv
->pipe
;
395 dst
= subpicture_priv
->sampler
;
397 /* TODO: Assert clear rect is within bounds? Or clip? */
398 map
= pipe
->transfer_map(pipe
, dst
->texture
, 0, PIPE_TRANSFER_WRITE
,
399 &dst_box
, &transfer
);
401 return XvMCBadSubpicture
;
403 util_fill_rect(map
, dst
->texture
->format
, transfer
->stride
, 0, 0,
404 dst_box
.width
, dst_box
.height
, &uc
);
406 pipe
->transfer_unmap(pipe
, transfer
);
411 Status
XvMCCompositeSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
, XvImage
*image
,
412 short srcx
, short srcy
, unsigned short width
, unsigned short height
,
413 short dstx
, short dsty
)
415 XvMCSubpicturePrivate
*subpicture_priv
;
416 XvMCContextPrivate
*context_priv
;
417 struct pipe_context
*pipe
;
418 struct pipe_box dst_box
= {dstx
, dsty
, 0, width
, height
, 1};
421 XVMC_MSG(XVMC_TRACE
, "[XvMC] Compositing subpicture %p.\n", subpicture
);
426 return XvMCBadSubpicture
;
430 if (subpicture
->xvimage_id
!= image
->id
)
433 /* No planar support for now */
434 if (image
->num_planes
!= 1)
437 subpicture_priv
= subpicture
->privData
;
438 context_priv
= subpicture_priv
->context
->privData
;
439 pipe
= context_priv
->pipe
;
441 /* clipping should be done by upload_sampler and regardles what the documentation
442 says image->pitches[0] doesn't seems to be in bytes, so don't use it */
443 if ((image
->id
== FOURCC_IA44
|| image
->id
== FOURCC_AI44
) &&
444 subpicture_priv
->sampler
->texture
->format
== PIPE_FORMAT_B4G4R4A4_UNORM
) {
445 upload_sampler_convert(pipe
, subpicture_priv
->sampler
, &dst_box
, image
, srcx
, srcy
);
447 src_stride
= image
->width
* util_format_get_blocksize(subpicture_priv
->sampler
->texture
->format
);
448 upload_sampler(pipe
, subpicture_priv
->sampler
, &dst_box
, image
->data
, src_stride
, srcx
, srcy
);
451 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p composited.\n", subpicture
);
457 Status
XvMCDestroySubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
459 XvMCSubpicturePrivate
*subpicture_priv
;
461 XVMC_MSG(XVMC_TRACE
, "[XvMC] Destroying subpicture %p.\n", subpicture
);
466 return XvMCBadSubpicture
;
468 subpicture_priv
= subpicture
->privData
;
469 pipe_sampler_view_reference(&subpicture_priv
->sampler
, NULL
);
470 pipe_sampler_view_reference(&subpicture_priv
->palette
, NULL
);
471 FREE(subpicture_priv
);
473 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p destroyed.\n", subpicture
);
479 Status
XvMCSetSubpicturePalette(Display
*dpy
, XvMCSubpicture
*subpicture
, unsigned char *palette
)
481 XvMCSubpicturePrivate
*subpicture_priv
;
482 XvMCContextPrivate
*context_priv
;
483 struct pipe_context
*pipe
;
484 struct pipe_box dst_box
= {0, 0, 0, 0, 1, 1};
490 return XvMCBadSubpicture
;
492 subpicture_priv
= subpicture
->privData
;
493 context_priv
= subpicture_priv
->context
->privData
;
494 pipe
= context_priv
->pipe
;
496 dst_box
.width
= subpicture
->num_palette_entries
;
498 upload_sampler(pipe
, subpicture_priv
->palette
, &dst_box
, palette
, 0, 0, 0);
500 XVMC_MSG(XVMC_TRACE
, "[XvMC] Palette of Subpicture %p set.\n", subpicture
);
506 Status
XvMCBlendSubpicture(Display
*dpy
, XvMCSurface
*target_surface
, XvMCSubpicture
*subpicture
,
507 short subx
, short suby
, unsigned short subw
, unsigned short subh
,
508 short surfx
, short surfy
, unsigned short surfw
, unsigned short surfh
)
510 struct u_rect src_rect
= {subx
, subx
+ subw
, suby
, suby
+ subh
};
511 struct u_rect dst_rect
= {surfx
, surfx
+ surfw
, surfy
, surfy
+ surfh
};
513 XvMCSurfacePrivate
*surface_priv
;
514 XvMCSubpicturePrivate
*subpicture_priv
;
516 XVMC_MSG(XVMC_TRACE
, "[XvMC] Associating subpicture %p with surface %p.\n", subpicture
, target_surface
);
521 return XvMCBadSurface
;
524 return XvMCBadSubpicture
;
526 if (target_surface
->context_id
!= subpicture
->context_id
)
529 /* TODO: Verify against subpicture independent scaling */
531 surface_priv
= target_surface
->privData
;
532 subpicture_priv
= subpicture
->privData
;
534 /* TODO: Assert rects are within bounds? Or clip? */
535 subpicture_priv
->src_rect
= src_rect
;
536 subpicture_priv
->dst_rect
= dst_rect
;
538 surface_priv
->subpicture
= subpicture
;
539 subpicture_priv
->surface
= target_surface
;
545 Status
XvMCBlendSubpicture2(Display
*dpy
, XvMCSurface
*source_surface
, XvMCSurface
*target_surface
,
546 XvMCSubpicture
*subpicture
, short subx
, short suby
, unsigned short subw
, unsigned short subh
,
547 short surfx
, short surfy
, unsigned short surfw
, unsigned short surfh
)
551 if (!source_surface
|| !target_surface
)
552 return XvMCBadSurface
;
555 return XvMCBadSubpicture
;
557 if (source_surface
->context_id
!= subpicture
->context_id
)
560 if (source_surface
->context_id
!= subpicture
->context_id
)
563 /* TODO: Assert rects are within bounds? Or clip? */
569 Status
XvMCSyncSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
574 return XvMCBadSubpicture
;
580 Status
XvMCFlushSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
585 return XvMCBadSubpicture
;
591 Status
XvMCGetSubpictureStatus(Display
*dpy
, XvMCSubpicture
*subpicture
, int *status
)
596 return XvMCBadSubpicture
;