c5d69bdc974dc30c854bdbf1884c36970579f30d
[mesa.git] / src / gallium / state_trackers / va / config.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include "pipe/p_screen.h"
30
31 #include "util/u_video.h"
32 #include "util/u_memory.h"
33
34 #include "vl/vl_winsys.h"
35
36 #include "va_private.h"
37
38 #include "util/u_handle_table.h"
39
40 DEBUG_GET_ONCE_BOOL_OPTION(mpeg4, "VAAPI_MPEG4_ENABLED", false)
41
42 VAStatus
43 vlVaQueryConfigProfiles(VADriverContextP ctx, VAProfile *profile_list, int *num_profiles)
44 {
45 struct pipe_screen *pscreen;
46 enum pipe_video_profile p;
47 VAProfile vap;
48
49 if (!ctx)
50 return VA_STATUS_ERROR_INVALID_CONTEXT;
51
52 *num_profiles = 0;
53
54 pscreen = VL_VA_PSCREEN(ctx);
55 for (p = PIPE_VIDEO_PROFILE_MPEG2_SIMPLE; p <= PIPE_VIDEO_PROFILE_HEVC_MAIN_444; ++p) {
56 if (u_reduce_video_profile(p) == PIPE_VIDEO_FORMAT_MPEG4 && !debug_get_option_mpeg4())
57 continue;
58
59 if (pscreen->get_video_param(pscreen, p, PIPE_VIDEO_ENTRYPOINT_BITSTREAM, PIPE_VIDEO_CAP_SUPPORTED)) {
60 vap = PipeToProfile(p);
61 if (vap != VAProfileNone)
62 profile_list[(*num_profiles)++] = vap;
63 }
64 }
65
66 /* Support postprocessing through vl_compositor */
67 profile_list[(*num_profiles)++] = VAProfileNone;
68
69 return VA_STATUS_SUCCESS;
70 }
71
72 VAStatus
73 vlVaQueryConfigEntrypoints(VADriverContextP ctx, VAProfile profile,
74 VAEntrypoint *entrypoint_list, int *num_entrypoints)
75 {
76 struct pipe_screen *pscreen;
77 enum pipe_video_profile p;
78
79 if (!ctx)
80 return VA_STATUS_ERROR_INVALID_CONTEXT;
81
82 *num_entrypoints = 0;
83
84 if (profile == VAProfileNone) {
85 entrypoint_list[(*num_entrypoints)++] = VAEntrypointVideoProc;
86 return VA_STATUS_SUCCESS;
87 }
88
89 p = ProfileToPipe(profile);
90 if (p == PIPE_VIDEO_PROFILE_UNKNOWN)
91 return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
92
93 pscreen = VL_VA_PSCREEN(ctx);
94 if (pscreen->get_video_param(pscreen, p, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
95 PIPE_VIDEO_CAP_SUPPORTED))
96 entrypoint_list[(*num_entrypoints)++] = VAEntrypointVLD;
97
98 if (pscreen->get_video_param(pscreen, p, PIPE_VIDEO_ENTRYPOINT_ENCODE,
99 PIPE_VIDEO_CAP_SUPPORTED))
100 entrypoint_list[(*num_entrypoints)++] = VAEntrypointEncSlice;
101
102 if (num_entrypoints == 0)
103 return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
104
105 return VA_STATUS_SUCCESS;
106 }
107
108 VAStatus
109 vlVaGetConfigAttributes(VADriverContextP ctx, VAProfile profile, VAEntrypoint entrypoint,
110 VAConfigAttrib *attrib_list, int num_attribs)
111 {
112 struct pipe_screen *pscreen;
113 int i;
114
115 if (!ctx)
116 return VA_STATUS_ERROR_INVALID_CONTEXT;
117
118 pscreen = VL_VA_PSCREEN(ctx);
119
120 for (i = 0; i < num_attribs; ++i) {
121 unsigned int value;
122 if (entrypoint == VAEntrypointVLD) {
123 switch (attrib_list[i].type) {
124 case VAConfigAttribRTFormat:
125 value = VA_RT_FORMAT_YUV420;
126 if (pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P016,
127 ProfileToPipe(profile),
128 PIPE_VIDEO_ENTRYPOINT_BITSTREAM))
129 value |= VA_RT_FORMAT_YUV420_10BPP;
130 break;
131 default:
132 value = VA_ATTRIB_NOT_SUPPORTED;
133 break;
134 }
135 } else if (entrypoint == VAEntrypointEncSlice) {
136 switch (attrib_list[i].type) {
137 case VAConfigAttribRTFormat:
138 value = VA_RT_FORMAT_YUV420;
139 break;
140 case VAConfigAttribRateControl:
141 value = VA_RC_CQP | VA_RC_CBR | VA_RC_VBR;
142 break;
143 case VAConfigAttribEncPackedHeaders:
144 value = 0;
145 break;
146 case VAConfigAttribEncMaxRefFrames:
147 value = 1;
148 break;
149 default:
150 value = VA_ATTRIB_NOT_SUPPORTED;
151 break;
152 }
153 } else if (entrypoint == VAEntrypointVideoProc) {
154 switch (attrib_list[i].type) {
155 case VAConfigAttribRTFormat:
156 value = (VA_RT_FORMAT_YUV420 |
157 VA_RT_FORMAT_YUV420_10BPP |
158 VA_RT_FORMAT_RGB32);
159 break;
160 default:
161 value = VA_ATTRIB_NOT_SUPPORTED;
162 break;
163 }
164 } else {
165 value = VA_ATTRIB_NOT_SUPPORTED;
166 }
167 attrib_list[i].value = value;
168 }
169
170 return VA_STATUS_SUCCESS;
171 }
172
173 VAStatus
174 vlVaCreateConfig(VADriverContextP ctx, VAProfile profile, VAEntrypoint entrypoint,
175 VAConfigAttrib *attrib_list, int num_attribs, VAConfigID *config_id)
176 {
177 vlVaDriver *drv;
178 vlVaConfig *config;
179 struct pipe_screen *pscreen;
180 enum pipe_video_profile p;
181 unsigned int supported_rt_formats;
182
183 if (!ctx)
184 return VA_STATUS_ERROR_INVALID_CONTEXT;
185
186 drv = VL_VA_DRIVER(ctx);
187
188 if (!drv)
189 return VA_STATUS_ERROR_INVALID_CONTEXT;
190
191 config = CALLOC(1, sizeof(vlVaConfig));
192 if (!config)
193 return VA_STATUS_ERROR_ALLOCATION_FAILED;
194
195 if (profile == VAProfileNone && entrypoint == VAEntrypointVideoProc) {
196 config->entrypoint = VAEntrypointVideoProc;
197 config->profile = PIPE_VIDEO_PROFILE_UNKNOWN;
198 supported_rt_formats = VA_RT_FORMAT_YUV420 |
199 VA_RT_FORMAT_YUV420_10BPP |
200 VA_RT_FORMAT_RGB32;
201 for (int i = 0; i < num_attribs; i++) {
202 if (attrib_list[i].type == VAConfigAttribRTFormat) {
203 if (attrib_list[i].value & supported_rt_formats) {
204 config->rt_format = attrib_list[i].value;
205 } else {
206 FREE(config);
207 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
208 }
209 }
210 }
211
212 /* Default value if not specified in the input attributes. */
213 if (!config->rt_format)
214 config->rt_format = supported_rt_formats;
215
216 mtx_lock(&drv->mutex);
217 *config_id = handle_table_add(drv->htab, config);
218 mtx_unlock(&drv->mutex);
219 return VA_STATUS_SUCCESS;
220 }
221
222 p = ProfileToPipe(profile);
223 if (p == PIPE_VIDEO_PROFILE_UNKNOWN) {
224 FREE(config);
225 return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
226 }
227
228 pscreen = VL_VA_PSCREEN(ctx);
229
230 switch (entrypoint) {
231 case VAEntrypointVLD:
232 if (!pscreen->get_video_param(pscreen, p, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
233 PIPE_VIDEO_CAP_SUPPORTED)) {
234 FREE(config);
235 return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
236 }
237
238 config->entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
239 break;
240
241 case VAEntrypointEncSlice:
242 if (!pscreen->get_video_param(pscreen, p, PIPE_VIDEO_ENTRYPOINT_ENCODE,
243 PIPE_VIDEO_CAP_SUPPORTED)) {
244 FREE(config);
245 return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
246 }
247
248 config->entrypoint = PIPE_VIDEO_ENTRYPOINT_ENCODE;
249 break;
250
251 default:
252 FREE(config);
253 return VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
254 }
255
256 config->profile = p;
257 supported_rt_formats = VA_RT_FORMAT_YUV420;
258 if (pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P016, p,
259 config->entrypoint))
260 supported_rt_formats |= VA_RT_FORMAT_YUV420_10BPP;
261
262 for (int i = 0; i <num_attribs ; i++) {
263 if (attrib_list[i].type == VAConfigAttribRateControl) {
264 if (attrib_list[i].value == VA_RC_CBR)
265 config->rc = PIPE_H264_ENC_RATE_CONTROL_METHOD_CONSTANT;
266 else if (attrib_list[i].value == VA_RC_VBR)
267 config->rc = PIPE_H264_ENC_RATE_CONTROL_METHOD_VARIABLE;
268 else
269 config->rc = PIPE_H264_ENC_RATE_CONTROL_METHOD_DISABLE;
270 }
271 if (attrib_list[i].type == VAConfigAttribRTFormat) {
272 if (attrib_list[i].value & supported_rt_formats) {
273 config->rt_format = attrib_list[i].value;
274 } else {
275 FREE(config);
276 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
277 }
278 }
279 }
280
281 /* Default value if not specified in the input attributes. */
282 if (!config->rt_format)
283 config->rt_format = supported_rt_formats;
284
285 mtx_lock(&drv->mutex);
286 *config_id = handle_table_add(drv->htab, config);
287 mtx_unlock(&drv->mutex);
288
289 return VA_STATUS_SUCCESS;
290 }
291
292 VAStatus
293 vlVaDestroyConfig(VADriverContextP ctx, VAConfigID config_id)
294 {
295 vlVaDriver *drv;
296 vlVaConfig *config;
297
298 if (!ctx)
299 return VA_STATUS_ERROR_INVALID_CONTEXT;
300
301 drv = VL_VA_DRIVER(ctx);
302
303 if (!drv)
304 return VA_STATUS_ERROR_INVALID_CONTEXT;
305
306 mtx_lock(&drv->mutex);
307 config = handle_table_get(drv->htab, config_id);
308
309 if (!config)
310 return VA_STATUS_ERROR_INVALID_CONFIG;
311
312 FREE(config);
313 handle_table_remove(drv->htab, config_id);
314 mtx_unlock(&drv->mutex);
315
316 return VA_STATUS_SUCCESS;
317 }
318
319 VAStatus
320 vlVaQueryConfigAttributes(VADriverContextP ctx, VAConfigID config_id, VAProfile *profile,
321 VAEntrypoint *entrypoint, VAConfigAttrib *attrib_list, int *num_attribs)
322 {
323 vlVaDriver *drv;
324 vlVaConfig *config;
325
326 if (!ctx)
327 return VA_STATUS_ERROR_INVALID_CONTEXT;
328
329 drv = VL_VA_DRIVER(ctx);
330
331 if (!drv)
332 return VA_STATUS_ERROR_INVALID_CONTEXT;
333
334 mtx_lock(&drv->mutex);
335 config = handle_table_get(drv->htab, config_id);
336 mtx_unlock(&drv->mutex);
337
338 if (!config)
339 return VA_STATUS_ERROR_INVALID_CONFIG;
340
341 *profile = PipeToProfile(config->profile);
342
343 if (config->profile == PIPE_VIDEO_PROFILE_UNKNOWN) {
344 *entrypoint = VAEntrypointVideoProc;
345 *num_attribs = 0;
346 return VA_STATUS_SUCCESS;
347 }
348
349 *entrypoint = config->entrypoint;
350
351 *num_attribs = 1;
352 attrib_list[0].type = VAConfigAttribRTFormat;
353 attrib_list[0].value = config->rt_format;
354
355 return VA_STATUS_SUCCESS;
356 }