2 #include <X11/Xlibint.h>
3 #include <X11/extensions/XvMClib.h>
4 #include <pipe/p_screen.h>
5 #include <pipe/p_video_context.h>
6 #include <pipe/p_video_state.h>
7 #include <pipe/p_state.h>
9 #include <util/u_memory.h>
10 #include "xvmc_private.h"
12 static Status
Validate(Display
*dpy
, XvPortID port
, int surface_type_id
,
13 unsigned int width
, unsigned int height
, int flags
,
14 bool *found_port
, int *screen
, int *chroma_format
, int *mc_type
)
16 bool found_surface
= false;
17 XvAdaptorInfo
*adaptor_info
;
18 unsigned int num_adaptors
;
20 unsigned int max_width
, max_height
;
26 assert(chroma_format
);
31 for (unsigned int i
= 0; i
< XScreenCount(dpy
); ++i
)
33 ret
= XvQueryAdaptors(dpy
, XRootWindow(dpy
, i
), &num_adaptors
, &adaptor_info
);
37 for (unsigned int j
= 0; j
< num_adaptors
&& !*found_port
; ++j
)
39 for (unsigned int k
= 0; k
< adaptor_info
[j
].num_ports
&& !*found_port
; ++k
)
41 XvMCSurfaceInfo
*surface_info
;
43 if (adaptor_info
[j
].base_id
+ k
!= port
)
48 surface_info
= XvMCListSurfaceTypes(dpy
, adaptor_info
[j
].base_id
, &num_types
);
51 XvFreeAdaptorInfo(adaptor_info
);
55 for (unsigned int l
= 0; l
< num_types
&& !found_surface
; ++l
)
57 if (surface_info
[l
].surface_type_id
!= surface_type_id
)
61 max_width
= surface_info
[l
].max_width
;
62 max_height
= surface_info
[l
].max_height
;
63 *chroma_format
= surface_info
[l
].chroma_format
;
64 *mc_type
= surface_info
[l
].mc_type
;
72 XvFreeAdaptorInfo(adaptor_info
);
79 if (width
> max_width
|| height
> max_height
)
81 if (flags
!= XVMC_DIRECT
&& flags
!= 0)
87 static enum pipe_video_profile
ProfileToPipe(int xvmc_profile
)
89 if (xvmc_profile
& XVMC_MPEG_1
)
91 if (xvmc_profile
& XVMC_MPEG_2
)
92 return PIPE_VIDEO_PROFILE_MPEG2_MAIN
;
93 if (xvmc_profile
& XVMC_H263
)
95 if (xvmc_profile
& XVMC_MPEG_4
)
103 static enum pipe_video_chroma_format
FormatToPipe(int xvmc_format
)
107 case XVMC_CHROMA_FORMAT_420
:
108 return PIPE_VIDEO_CHROMA_FORMAT_420
;
109 case XVMC_CHROMA_FORMAT_422
:
110 return PIPE_VIDEO_CHROMA_FORMAT_422
;
111 case XVMC_CHROMA_FORMAT_444
:
112 return PIPE_VIDEO_CHROMA_FORMAT_444
;
120 Status
XvMCCreateContext(Display
*dpy
, XvPortID port
, int surface_type_id
,
121 int width
, int height
, int flags
, XvMCContext
*context
)
128 struct pipe_screen
*screen
;
129 struct pipe_video_context
*vpipe
;
130 XvMCContextPrivate
*context_priv
;
135 return XvMCBadContext
;
137 ret
= Validate(dpy
, port
, surface_type_id
, width
, height
, flags
,
138 &found_port
, &scrn
, &chroma_format
, &mc_type
);
140 /* Success and XvBadPort have the same value */
141 if (ret
!= Success
|| !found_port
)
144 context_priv
= CALLOC(1, sizeof(XvMCContextPrivate
));
148 /* TODO: Reuse screen if process creates another context */
149 screen
= vl_screen_create(dpy
, scrn
);
157 vpipe
= vl_video_create(screen
, ProfileToPipe(mc_type
),
158 FormatToPipe(chroma_format
), width
, height
);
162 screen
->destroy(screen
);
167 context_priv
->vpipe
= vpipe
;
169 context
->context_id
= XAllocID(dpy
);
170 context
->surface_type_id
= surface_type_id
;
171 context
->width
= width
;
172 context
->height
= height
;
173 context
->flags
= flags
;
174 context
->port
= port
;
175 context
->privData
= context_priv
;
182 Status
XvMCDestroyContext(Display
*dpy
, XvMCContext
*context
)
184 struct pipe_screen
*screen
;
185 struct pipe_video_context
*vpipe
;
186 XvMCContextPrivate
*context_priv
;
190 if (!context
|| !context
->privData
)
191 return XvMCBadContext
;
193 context_priv
= context
->privData
;
194 vpipe
= context_priv
->vpipe
;
195 pipe_surface_reference(&context_priv
->backbuffer
, NULL
);
196 screen
= vpipe
->screen
;
197 vpipe
->destroy(vpipe
);
198 screen
->destroy(screen
);
200 context
->privData
= NULL
;