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 TUNGSTEN GRAPHICS 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_decoder.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_rect.h"
43 #include "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(int xvimage_id
)
55 return PIPE_FORMAT_B8G8R8X8_UNORM
;
58 return PIPE_FORMAT_A4R4_UNORM
;
61 return PIPE_FORMAT_R4A4_UNORM
;
64 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
65 return PIPE_FORMAT_NONE
;
69 static unsigned NumPaletteEntries4XvID(int xvimage_id
)
80 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
85 static int PipeToComponentOrder(enum pipe_format format
, char *component_order
)
87 assert(component_order
);
90 case PIPE_FORMAT_B8G8R8X8_UNORM
:
93 case PIPE_FORMAT_R4A4_UNORM
:
94 case PIPE_FORMAT_A4R4_UNORM
:
95 component_order
[0] = 'Y';
96 component_order
[1] = 'U';
97 component_order
[2] = 'V';
98 component_order
[3] = 'A';
102 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized PIPE_FORMAT 0x%08X.\n", format
);
103 component_order
[0] = 0;
104 component_order
[1] = 0;
105 component_order
[2] = 0;
106 component_order
[3] = 0;
111 static Status
Validate(Display
*dpy
, XvPortID port
, int surface_type_id
, int xvimage_id
)
113 XvImageFormatValues
*subpictures
;
117 subpictures
= XvMCListSubpictureTypes(dpy
, port
, surface_type_id
, &num_subpics
);
118 if (num_subpics
< 1) {
126 for (i
= 0; i
< num_subpics
; ++i
) {
127 if (subpictures
[i
].id
== xvimage_id
) {
128 XVMC_MSG(XVMC_TRACE
, "[XvMC] Found requested subpicture format.\n" \
130 "[XvMC] surface id=0x%08X\n" \
131 "[XvMC] image id=0x%08X\n" \
132 "[XvMC] type=%08X\n" \
133 "[XvMC] byte order=%08X\n" \
134 "[XvMC] bits per pixel=%u\n" \
135 "[XvMC] format=%08X\n" \
136 "[XvMC] num planes=%d\n",
137 port
, surface_type_id
, xvimage_id
, subpictures
[i
].type
, subpictures
[i
].byte_order
,
138 subpictures
[i
].bits_per_pixel
, subpictures
[i
].format
, subpictures
[i
].num_planes
);
139 if (subpictures
[i
].type
== XvRGB
) {
140 XVMC_MSG(XVMC_TRACE
, "[XvMC] depth=%d\n" \
141 "[XvMC] red mask=0x%08X\n" \
142 "[XvMC] green mask=0x%08X\n" \
143 "[XvMC] blue mask=0x%08X\n",
144 subpictures
[i
].depth
, subpictures
[i
].red_mask
,
145 subpictures
[i
].green_mask
, subpictures
[i
].blue_mask
);
147 else if (subpictures
[i
].type
== XvYUV
) {
148 XVMC_MSG(XVMC_TRACE
, "[XvMC] y sample bits=0x%08X\n" \
149 "[XvMC] u sample bits=0x%08X\n" \
150 "[XvMC] v sample bits=0x%08X\n" \
151 "[XvMC] horz y period=%u\n" \
152 "[XvMC] horz u period=%u\n" \
153 "[XvMC] horz v period=%u\n" \
154 "[XvMC] vert y period=%u\n" \
155 "[XvMC] vert u period=%u\n" \
156 "[XvMC] vert v period=%u\n",
157 subpictures
[i
].y_sample_bits
, subpictures
[i
].u_sample_bits
, subpictures
[i
].v_sample_bits
,
158 subpictures
[i
].horz_y_period
, subpictures
[i
].horz_u_period
, subpictures
[i
].horz_v_period
,
159 subpictures
[i
].vert_y_period
, subpictures
[i
].vert_u_period
, subpictures
[i
].vert_v_period
);
167 return i
< num_subpics
? Success
: BadMatch
;
171 upload_sampler(struct pipe_context
*pipe
, struct pipe_sampler_view
*dst
,
172 const struct pipe_box
*dst_box
, const void *src
, unsigned src_stride
,
173 unsigned src_x
, unsigned src_y
)
175 struct pipe_transfer
*transfer
;
178 transfer
= pipe
->get_transfer(pipe
, dst
->texture
, 0, PIPE_TRANSFER_WRITE
, dst_box
);
182 map
= pipe
->transfer_map(pipe
, transfer
);
184 util_copy_rect(map
, dst
->texture
->format
, transfer
->stride
, 0, 0,
185 dst_box
->width
, dst_box
->height
,
186 src
, src_stride
, src_x
, src_y
);
188 pipe
->transfer_unmap(pipe
, transfer
);
191 pipe
->transfer_destroy(pipe
, transfer
);
195 Status
XvMCCreateSubpicture(Display
*dpy
, XvMCContext
*context
, XvMCSubpicture
*subpicture
,
196 unsigned short width
, unsigned short height
, int xvimage_id
)
198 XvMCContextPrivate
*context_priv
;
199 XvMCSubpicturePrivate
*subpicture_priv
;
200 struct pipe_context
*pipe
;
201 struct pipe_resource tex_templ
, *tex
;
202 struct pipe_sampler_view sampler_templ
;
205 XVMC_MSG(XVMC_TRACE
, "[XvMC] Creating subpicture %p.\n", subpicture
);
210 return XvMCBadContext
;
212 context_priv
= context
->privData
;
213 pipe
= context_priv
->pipe
;
216 return XvMCBadSubpicture
;
218 if (width
> context_priv
->subpicture_max_width
||
219 height
> context_priv
->subpicture_max_height
)
222 ret
= Validate(dpy
, context
->port
, context
->surface_type_id
, xvimage_id
);
226 subpicture_priv
= CALLOC(1, sizeof(XvMCSubpicturePrivate
));
227 if (!subpicture_priv
)
230 memset(&tex_templ
, 0, sizeof(tex_templ
));
231 tex_templ
.target
= PIPE_TEXTURE_2D
;
232 tex_templ
.format
= XvIDToPipe(xvimage_id
);
233 tex_templ
.last_level
= 0;
234 if (pipe
->screen
->get_video_param(pipe
->screen
,
235 PIPE_VIDEO_PROFILE_UNKNOWN
,
236 PIPE_VIDEO_CAP_NPOT_TEXTURES
)) {
237 tex_templ
.width0
= width
;
238 tex_templ
.height0
= height
;
241 tex_templ
.width0
= util_next_power_of_two(width
);
242 tex_templ
.height0
= util_next_power_of_two(height
);
244 tex_templ
.depth0
= 1;
245 tex_templ
.array_size
= 1;
246 tex_templ
.usage
= PIPE_USAGE_DYNAMIC
;
247 tex_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
250 tex
= pipe
->screen
->resource_create(pipe
->screen
, &tex_templ
);
252 memset(&sampler_templ
, 0, sizeof(sampler_templ
));
253 u_sampler_view_default_template(&sampler_templ
, tex
, tex
->format
);
255 subpicture_priv
->sampler
= pipe
->create_sampler_view(pipe
, tex
, &sampler_templ
);
256 pipe_resource_reference(&tex
, NULL
);
257 if (!subpicture_priv
->sampler
) {
258 FREE(subpicture_priv
);
262 subpicture_priv
->context
= context
;
263 subpicture
->subpicture_id
= XAllocID(dpy
);
264 subpicture
->context_id
= context
->context_id
;
265 subpicture
->xvimage_id
= xvimage_id
;
266 subpicture
->width
= width
;
267 subpicture
->height
= height
;
268 subpicture
->num_palette_entries
= NumPaletteEntries4XvID(xvimage_id
);
269 subpicture
->entry_bytes
= PipeToComponentOrder(tex_templ
.format
, subpicture
->component_order
);
270 subpicture
->privData
= subpicture_priv
;
272 if (subpicture
->num_palette_entries
> 0) {
273 tex_templ
.target
= PIPE_TEXTURE_1D
;
274 tex_templ
.format
= PIPE_FORMAT_R8G8B8X8_UNORM
;
275 tex_templ
.width0
= subpicture
->num_palette_entries
;
276 tex_templ
.height0
= 1;
277 tex_templ
.usage
= PIPE_USAGE_STATIC
;
279 tex
= pipe
->screen
->resource_create(pipe
->screen
, &tex_templ
);
281 memset(&sampler_templ
, 0, sizeof(sampler_templ
));
282 u_sampler_view_default_template(&sampler_templ
, tex
, tex
->format
);
283 sampler_templ
.swizzle_a
= PIPE_SWIZZLE_ONE
;
284 subpicture_priv
->palette
= pipe
->create_sampler_view(pipe
, tex
, &sampler_templ
);
285 pipe_resource_reference(&tex
, NULL
);
286 if (!subpicture_priv
->sampler
) {
287 FREE(subpicture_priv
);
294 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p created.\n", subpicture
);
300 Status
XvMCClearSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
, short x
, short y
,
301 unsigned short width
, unsigned short height
, unsigned int color
)
303 XvMCSubpicturePrivate
*subpicture_priv
;
304 XvMCContextPrivate
*context_priv
;
305 struct pipe_context
*pipe
;
306 struct pipe_sampler_view
*dst
;
307 struct pipe_box dst_box
= {x
, y
, 0, width
, height
, 1};
308 struct pipe_transfer
*transfer
;
315 return XvMCBadSubpicture
;
317 /* Convert color to float */
318 util_format_read_4f(PIPE_FORMAT_B8G8R8A8_UNORM
,
322 subpicture_priv
= subpicture
->privData
;
323 context_priv
= subpicture_priv
->context
->privData
;
324 pipe
= context_priv
->pipe
;
325 dst
= subpicture_priv
->sampler
;
327 /* TODO: Assert clear rect is within bounds? Or clip? */
328 transfer
= pipe
->get_transfer(pipe
, dst
->texture
, 0, PIPE_TRANSFER_WRITE
, &dst_box
);
330 return XvMCBadSubpicture
;
332 map
= pipe
->transfer_map(pipe
, transfer
);
334 util_fill_rect(map
, dst
->texture
->format
, transfer
->stride
, 0, 0,
335 dst_box
.width
, dst_box
.height
, &uc
);
337 pipe
->transfer_unmap(pipe
, transfer
);
340 pipe
->transfer_destroy(pipe
, transfer
);
346 Status
XvMCCompositeSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
, XvImage
*image
,
347 short srcx
, short srcy
, unsigned short width
, unsigned short height
,
348 short dstx
, short dsty
)
350 XvMCSubpicturePrivate
*subpicture_priv
;
351 XvMCContextPrivate
*context_priv
;
352 struct pipe_context
*pipe
;
353 struct pipe_box dst_box
= {dstx
, dsty
, 0, width
, height
, 1};
356 XVMC_MSG(XVMC_TRACE
, "[XvMC] Compositing subpicture %p.\n", subpicture
);
361 return XvMCBadSubpicture
;
365 if (subpicture
->xvimage_id
!= image
->id
)
368 /* No planar support for now */
369 if (image
->num_planes
!= 1)
372 subpicture_priv
= subpicture
->privData
;
373 context_priv
= subpicture_priv
->context
->privData
;
374 pipe
= context_priv
->pipe
;
376 /* clipping should be done by upload_sampler and regardles what the documentation
377 says image->pitches[0] doesn't seems to be in bytes, so don't use it */
378 src_stride
= image
->width
* util_format_get_blocksize(subpicture_priv
->sampler
->texture
->format
);
379 upload_sampler(pipe
, subpicture_priv
->sampler
, &dst_box
, image
->data
, src_stride
, srcx
, srcy
);
381 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p composited.\n", subpicture
);
387 Status
XvMCDestroySubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
389 XvMCSubpicturePrivate
*subpicture_priv
;
391 XVMC_MSG(XVMC_TRACE
, "[XvMC] Destroying subpicture %p.\n", subpicture
);
396 return XvMCBadSubpicture
;
398 subpicture_priv
= subpicture
->privData
;
399 pipe_sampler_view_reference(&subpicture_priv
->sampler
, NULL
);
400 pipe_sampler_view_reference(&subpicture_priv
->palette
, NULL
);
401 FREE(subpicture_priv
);
403 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p destroyed.\n", subpicture
);
409 Status
XvMCSetSubpicturePalette(Display
*dpy
, XvMCSubpicture
*subpicture
, unsigned char *palette
)
411 XvMCSubpicturePrivate
*subpicture_priv
;
412 XvMCContextPrivate
*context_priv
;
413 struct pipe_context
*pipe
;
414 struct pipe_box dst_box
= {0, 0, 0, 0, 1, 1};
420 return XvMCBadSubpicture
;
422 subpicture_priv
= subpicture
->privData
;
423 context_priv
= subpicture_priv
->context
->privData
;
424 pipe
= context_priv
->pipe
;
426 dst_box
.width
= subpicture
->num_palette_entries
;
428 upload_sampler(pipe
, subpicture_priv
->palette
, &dst_box
, palette
, 0, 0, 0);
430 XVMC_MSG(XVMC_TRACE
, "[XvMC] Palette of Subpicture %p set.\n", subpicture
);
436 Status
XvMCBlendSubpicture(Display
*dpy
, XvMCSurface
*target_surface
, XvMCSubpicture
*subpicture
,
437 short subx
, short suby
, unsigned short subw
, unsigned short subh
,
438 short surfx
, short surfy
, unsigned short surfw
, unsigned short surfh
)
440 struct u_rect src_rect
= {subx
, subx
+ subw
, suby
, suby
+ subh
};
441 struct u_rect dst_rect
= {surfx
, surfx
+ surfw
, surfy
, surfy
+ surfh
};
443 XvMCSurfacePrivate
*surface_priv
;
444 XvMCSubpicturePrivate
*subpicture_priv
;
446 XVMC_MSG(XVMC_TRACE
, "[XvMC] Associating subpicture %p with surface %p.\n", subpicture
, target_surface
);
451 return XvMCBadSurface
;
454 return XvMCBadSubpicture
;
456 if (target_surface
->context_id
!= subpicture
->context_id
)
459 /* TODO: Verify against subpicture independent scaling */
461 surface_priv
= target_surface
->privData
;
462 subpicture_priv
= subpicture
->privData
;
464 /* TODO: Assert rects are within bounds? Or clip? */
465 subpicture_priv
->src_rect
= src_rect
;
466 subpicture_priv
->dst_rect
= dst_rect
;
468 surface_priv
->subpicture
= subpicture
;
469 subpicture_priv
->surface
= target_surface
;
475 Status
XvMCBlendSubpicture2(Display
*dpy
, XvMCSurface
*source_surface
, XvMCSurface
*target_surface
,
476 XvMCSubpicture
*subpicture
, short subx
, short suby
, unsigned short subw
, unsigned short subh
,
477 short surfx
, short surfy
, unsigned short surfw
, unsigned short surfh
)
481 if (!source_surface
|| !target_surface
)
482 return XvMCBadSurface
;
485 return XvMCBadSubpicture
;
487 if (source_surface
->context_id
!= subpicture
->context_id
)
490 if (source_surface
->context_id
!= subpicture
->context_id
)
493 /* TODO: Assert rects are within bounds? Or clip? */
499 Status
XvMCSyncSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
504 return XvMCBadSubpicture
;
510 Status
XvMCFlushSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
515 return XvMCBadSubpicture
;
521 Status
XvMCGetSubpictureStatus(Display
*dpy
, XvMCSubpicture
*subpicture
, int *status
)
526 return XvMCBadSubpicture
;