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 #include "vl/vl_codec.h"
36
37 #include "va_private.h"
38
39 #include "util/u_handle_table.h"
40
41 DEBUG_GET_ONCE_BOOL_OPTION(mpeg4, "VAAPI_MPEG4_ENABLED", false)
42
43 VAStatus
vlVaQueryConfigProfiles(VADriverContextP ctx,VAProfile * profile_list,int * num_profiles)44 vlVaQueryConfigProfiles(VADriverContextP ctx, VAProfile *profile_list, int *num_profiles)
45 {
46 struct pipe_screen *pscreen;
47 enum pipe_video_profile p;
48 VAProfile vap;
49
50 if (!ctx)
51 return VA_STATUS_ERROR_INVALID_CONTEXT;
52
53 *num_profiles = 0;
54
55 pscreen = VL_VA_PSCREEN(ctx);
56 for (p = PIPE_VIDEO_PROFILE_MPEG2_SIMPLE; p <= PIPE_VIDEO_PROFILE_AV1_MAIN; ++p) {
57 if (u_reduce_video_profile(p) == PIPE_VIDEO_FORMAT_MPEG4 && !debug_get_option_mpeg4())
58 continue;
59
60 if (vl_codec_supported(pscreen, p, false) ||
61 vl_codec_supported(pscreen, p, true)) {
62 vap = PipeToProfile(p);
63 if (vap != VAProfileNone)
64 profile_list[(*num_profiles)++] = vap;
65 }
66 }
67
68 /* Support postprocessing through vl_compositor */
69 profile_list[(*num_profiles)++] = VAProfileNone;
70
71 return VA_STATUS_SUCCESS;
72 }
73
74 VAStatus
vlVaQueryConfigEntrypoints(VADriverContextP ctx,VAProfile profile,VAEntrypoint * entrypoint_list,int * num_entrypoints)75 vlVaQueryConfigEntrypoints(VADriverContextP ctx, VAProfile profile,
76 VAEntrypoint *entrypoint_list, int *num_entrypoints)
77 {
78 struct pipe_screen *pscreen;
79 enum pipe_video_profile p;
80
81 if (!ctx)
82 return VA_STATUS_ERROR_INVALID_CONTEXT;
83
84 *num_entrypoints = 0;
85
86 if (profile == VAProfileNone) {
87 entrypoint_list[(*num_entrypoints)++] = VAEntrypointVideoProc;
88 return VA_STATUS_SUCCESS;
89 }
90
91 p = ProfileToPipe(profile);
92 if (p == PIPE_VIDEO_PROFILE_UNKNOWN ||
93 (u_reduce_video_profile(p) == PIPE_VIDEO_FORMAT_MPEG4 &&
94 !debug_get_option_mpeg4()))
95 return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
96
97 pscreen = VL_VA_PSCREEN(ctx);
98 if (vl_codec_supported(pscreen, p, false))
99 entrypoint_list[(*num_entrypoints)++] = VAEntrypointVLD;
100
101 if (vl_codec_supported(pscreen, p, true))
102 entrypoint_list[(*num_entrypoints)++] = VAEntrypointEncSlice;
103
104 if (*num_entrypoints == 0)
105 return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
106
107 assert(*num_entrypoints <= ctx->max_entrypoints);
108
109 return VA_STATUS_SUCCESS;
110 }
111
112 VAStatus
vlVaGetConfigAttributes(VADriverContextP ctx,VAProfile profile,VAEntrypoint entrypoint,VAConfigAttrib * attrib_list,int num_attribs)113 vlVaGetConfigAttributes(VADriverContextP ctx, VAProfile profile, VAEntrypoint entrypoint,
114 VAConfigAttrib *attrib_list, int num_attribs)
115 {
116 struct pipe_screen *pscreen;
117 int i;
118
119 if (!ctx)
120 return VA_STATUS_ERROR_INVALID_CONTEXT;
121
122 pscreen = VL_VA_PSCREEN(ctx);
123
124 for (i = 0; i < num_attribs; ++i) {
125 unsigned int value;
126 if ((entrypoint == VAEntrypointVLD) &&
127 (vl_codec_supported(pscreen, ProfileToPipe(profile), false))) {
128 switch (attrib_list[i].type) {
129 case VAConfigAttribRTFormat:
130 value = VA_RT_FORMAT_YUV420 | VA_RT_FORMAT_YUV422;
131 if (pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P010,
132 ProfileToPipe(profile),
133 PIPE_VIDEO_ENTRYPOINT_BITSTREAM) ||
134 pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P016,
135 ProfileToPipe(profile),
136 PIPE_VIDEO_ENTRYPOINT_BITSTREAM))
137 value |= VA_RT_FORMAT_YUV420_10BPP;
138 break;
139 default:
140 value = VA_ATTRIB_NOT_SUPPORTED;
141 break;
142 }
143 } else if ((entrypoint == VAEntrypointEncSlice) &&
144 (vl_codec_supported(pscreen, ProfileToPipe(profile), true))) {
145 switch (attrib_list[i].type) {
146 case VAConfigAttribRTFormat:
147 value = VA_RT_FORMAT_YUV420;
148 if (pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P010,
149 ProfileToPipe(profile),
150 PIPE_VIDEO_ENTRYPOINT_ENCODE) ||
151 pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P016,
152 ProfileToPipe(profile),
153 PIPE_VIDEO_ENTRYPOINT_ENCODE))
154 value |= VA_RT_FORMAT_YUV420_10BPP;
155 break;
156 case VAConfigAttribRateControl:
157 value = VA_RC_CQP | VA_RC_CBR | VA_RC_VBR;
158 break;
159 case VAConfigAttribEncRateControlExt:
160 value = pscreen->get_video_param(pscreen, ProfileToPipe(profile),
161 PIPE_VIDEO_ENTRYPOINT_ENCODE,
162 PIPE_VIDEO_CAP_MAX_TEMPORAL_LAYERS);
163 if (value > 0) {
164 value -= 1;
165 value |= (1 << 8); /* temporal_layer_bitrate_control_flag */
166 }
167 break;
168 case VAConfigAttribEncPackedHeaders:
169 value = VA_ENC_PACKED_HEADER_NONE;
170 if (u_reduce_video_profile(ProfileToPipe(profile)) == PIPE_VIDEO_FORMAT_HEVC)
171 value |= VA_ENC_PACKED_HEADER_SEQUENCE;
172 break;
173 case VAConfigAttribEncMaxSlices:
174 {
175 /**
176 * \brief Maximum number of slices per frame. Read-only.
177 *
178 * This attribute determines the maximum number of slices the
179 * driver can support to encode a single frame.
180 */
181 int maxSlicesPerEncodedPic = pscreen->get_video_param(pscreen, ProfileToPipe(profile),
182 PIPE_VIDEO_ENTRYPOINT_ENCODE,
183 PIPE_VIDEO_CAP_ENC_MAX_SLICES_PER_FRAME);
184 if (maxSlicesPerEncodedPic <= 0)
185 value = VA_ATTRIB_NOT_SUPPORTED;
186 else
187 value = maxSlicesPerEncodedPic;
188 } break;
189 case VAConfigAttribEncMaxRefFrames:
190 {
191 int maxL0L1ReferencesPerFrame = pscreen->get_video_param(pscreen, ProfileToPipe(profile),
192 PIPE_VIDEO_ENTRYPOINT_ENCODE,
193 PIPE_VIDEO_CAP_ENC_MAX_REFERENCES_PER_FRAME);
194 if (maxL0L1ReferencesPerFrame <= 0)
195 value = 1;
196 else
197 value = maxL0L1ReferencesPerFrame;
198 } break;
199 case VAConfigAttribEncSliceStructure:
200 {
201 /* The VA enum values match the pipe_video_cap_slice_structure definitions*/
202 int supportedSliceStructuresFlagSet = pscreen->get_video_param(pscreen, ProfileToPipe(profile),
203 PIPE_VIDEO_ENTRYPOINT_ENCODE,
204 PIPE_VIDEO_CAP_ENC_SLICES_STRUCTURE);
205 if (supportedSliceStructuresFlagSet <= 0)
206 value = VA_ATTRIB_NOT_SUPPORTED;
207 else
208 value = supportedSliceStructuresFlagSet;
209 } break;
210 default:
211 value = VA_ATTRIB_NOT_SUPPORTED;
212 break;
213 }
214 } else if (entrypoint == VAEntrypointVideoProc) {
215 switch (attrib_list[i].type) {
216 case VAConfigAttribRTFormat:
217 value = (VA_RT_FORMAT_YUV420 |
218 VA_RT_FORMAT_YUV420_10BPP |
219 VA_RT_FORMAT_RGB32);
220 break;
221 default:
222 value = VA_ATTRIB_NOT_SUPPORTED;
223 break;
224 }
225 } else {
226 value = VA_ATTRIB_NOT_SUPPORTED;
227 }
228 attrib_list[i].value = value;
229 }
230
231 return VA_STATUS_SUCCESS;
232 }
233
234 VAStatus
vlVaCreateConfig(VADriverContextP ctx,VAProfile profile,VAEntrypoint entrypoint,VAConfigAttrib * attrib_list,int num_attribs,VAConfigID * config_id)235 vlVaCreateConfig(VADriverContextP ctx, VAProfile profile, VAEntrypoint entrypoint,
236 VAConfigAttrib *attrib_list, int num_attribs, VAConfigID *config_id)
237 {
238 vlVaDriver *drv;
239 vlVaConfig *config;
240 struct pipe_screen *pscreen;
241 enum pipe_video_profile p;
242 unsigned int supported_rt_formats;
243
244 if (!ctx)
245 return VA_STATUS_ERROR_INVALID_CONTEXT;
246
247 drv = VL_VA_DRIVER(ctx);
248
249 if (!drv)
250 return VA_STATUS_ERROR_INVALID_CONTEXT;
251
252 config = CALLOC(1, sizeof(vlVaConfig));
253 if (!config)
254 return VA_STATUS_ERROR_ALLOCATION_FAILED;
255
256 if (profile == VAProfileNone) {
257 if (entrypoint != VAEntrypointVideoProc) {
258 FREE(config);
259 return VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
260 }
261
262 config->entrypoint = PIPE_VIDEO_ENTRYPOINT_PROCESSING;
263 config->profile = PIPE_VIDEO_PROFILE_UNKNOWN;
264 supported_rt_formats = VA_RT_FORMAT_YUV420 |
265 VA_RT_FORMAT_YUV420_10BPP |
266 VA_RT_FORMAT_RGB32;
267 for (int i = 0; i < num_attribs; i++) {
268 if (attrib_list[i].type == VAConfigAttribRTFormat) {
269 if (attrib_list[i].value & supported_rt_formats) {
270 config->rt_format = attrib_list[i].value;
271 } else {
272 FREE(config);
273 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
274 }
275 } else {
276 /*other attrib_types are not supported.*/
277 FREE(config);
278 return VA_STATUS_ERROR_INVALID_VALUE;
279 }
280 }
281
282 /* Default value if not specified in the input attributes. */
283 if (!config->rt_format)
284 config->rt_format = supported_rt_formats;
285
286 mtx_lock(&drv->mutex);
287 *config_id = handle_table_add(drv->htab, config);
288 mtx_unlock(&drv->mutex);
289 return VA_STATUS_SUCCESS;
290 }
291
292 p = ProfileToPipe(profile);
293 if (p == PIPE_VIDEO_PROFILE_UNKNOWN ||
294 (u_reduce_video_profile(p) == PIPE_VIDEO_FORMAT_MPEG4 &&
295 !debug_get_option_mpeg4())) {
296 FREE(config);
297 return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
298 }
299
300 pscreen = VL_VA_PSCREEN(ctx);
301
302 switch (entrypoint) {
303 case VAEntrypointVLD:
304 supported_rt_formats = VA_RT_FORMAT_YUV420 | VA_RT_FORMAT_YUV422;
305 if (!vl_codec_supported(pscreen, p, false)) {
306 FREE(config);
307 return VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
308 }
309
310 config->entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
311 break;
312
313 case VAEntrypointEncSlice:
314 supported_rt_formats = VA_RT_FORMAT_YUV420;
315 if (!vl_codec_supported(pscreen, p, true)) {
316 FREE(config);
317 return VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
318 }
319
320 config->entrypoint = PIPE_VIDEO_ENTRYPOINT_ENCODE;
321 break;
322
323 default:
324 FREE(config);
325 return VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
326 }
327
328 config->profile = p;
329 if (pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P010, p,
330 config->entrypoint) ||
331 pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P016, p,
332 config->entrypoint))
333 supported_rt_formats |= VA_RT_FORMAT_YUV420_10BPP;
334
335 for (int i = 0; i <num_attribs ; i++) {
336 if (attrib_list[i].type != VAConfigAttribRTFormat &&
337 entrypoint == VAEntrypointVLD ) {
338 FREE(config);
339 return VA_STATUS_ERROR_INVALID_VALUE;
340 }
341 if (attrib_list[i].type == VAConfigAttribRateControl) {
342 if (attrib_list[i].value == VA_RC_CBR)
343 config->rc = PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT;
344 else if (attrib_list[i].value == VA_RC_VBR)
345 config->rc = PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE;
346 else if (attrib_list[i].value == VA_RC_CQP)
347 config->rc = PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE;
348 else {
349 FREE(config);
350 return VA_STATUS_ERROR_INVALID_VALUE;
351 }
352 }
353 if (attrib_list[i].type == VAConfigAttribRTFormat) {
354 if (attrib_list[i].value & supported_rt_formats) {
355 config->rt_format = attrib_list[i].value;
356 } else {
357 FREE(config);
358 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
359 }
360 }
361 if (attrib_list[i].type == VAConfigAttribEncPackedHeaders) {
362 if ((attrib_list[i].value > 1) ||
363 (attrib_list[i].value &&
364 u_reduce_video_profile(ProfileToPipe(profile)) !=
365 PIPE_VIDEO_FORMAT_HEVC) ||
366 (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE)) {
367 FREE(config);
368 return VA_STATUS_ERROR_INVALID_VALUE;
369 }
370 }
371 }
372
373 /* Default value if not specified in the input attributes. */
374 if (!config->rt_format)
375 config->rt_format = supported_rt_formats;
376
377 mtx_lock(&drv->mutex);
378 *config_id = handle_table_add(drv->htab, config);
379 mtx_unlock(&drv->mutex);
380
381 return VA_STATUS_SUCCESS;
382 }
383
384 VAStatus
vlVaDestroyConfig(VADriverContextP ctx,VAConfigID config_id)385 vlVaDestroyConfig(VADriverContextP ctx, VAConfigID config_id)
386 {
387 vlVaDriver *drv;
388 vlVaConfig *config;
389
390 if (!ctx)
391 return VA_STATUS_ERROR_INVALID_CONTEXT;
392
393 drv = VL_VA_DRIVER(ctx);
394
395 if (!drv)
396 return VA_STATUS_ERROR_INVALID_CONTEXT;
397
398 mtx_lock(&drv->mutex);
399 config = handle_table_get(drv->htab, config_id);
400
401 if (!config) {
402 mtx_unlock(&drv->mutex);
403 return VA_STATUS_ERROR_INVALID_CONFIG;
404 }
405
406 FREE(config);
407 handle_table_remove(drv->htab, config_id);
408 mtx_unlock(&drv->mutex);
409
410 return VA_STATUS_SUCCESS;
411 }
412
413 VAStatus
vlVaQueryConfigAttributes(VADriverContextP ctx,VAConfigID config_id,VAProfile * profile,VAEntrypoint * entrypoint,VAConfigAttrib * attrib_list,int * num_attribs)414 vlVaQueryConfigAttributes(VADriverContextP ctx, VAConfigID config_id, VAProfile *profile,
415 VAEntrypoint *entrypoint, VAConfigAttrib *attrib_list, int *num_attribs)
416 {
417 vlVaDriver *drv;
418 vlVaConfig *config;
419
420 if (!ctx)
421 return VA_STATUS_ERROR_INVALID_CONTEXT;
422
423 drv = VL_VA_DRIVER(ctx);
424
425 if (!drv)
426 return VA_STATUS_ERROR_INVALID_CONTEXT;
427
428 mtx_lock(&drv->mutex);
429 config = handle_table_get(drv->htab, config_id);
430 mtx_unlock(&drv->mutex);
431
432 if (!config)
433 return VA_STATUS_ERROR_INVALID_CONFIG;
434
435 *profile = PipeToProfile(config->profile);
436
437 switch (config->entrypoint) {
438 case PIPE_VIDEO_ENTRYPOINT_BITSTREAM:
439 *entrypoint = VAEntrypointVLD;
440 break;
441 case PIPE_VIDEO_ENTRYPOINT_ENCODE:
442 *entrypoint = VAEntrypointEncSlice;
443 break;
444 case PIPE_VIDEO_ENTRYPOINT_PROCESSING:
445 *entrypoint = VAEntrypointVideoProc;
446 break;
447 default:
448 return VA_STATUS_ERROR_INVALID_CONFIG;
449 }
450
451 *num_attribs = 1;
452 attrib_list[0].type = VAConfigAttribRTFormat;
453 attrib_list[0].value = config->rt_format;
454
455 return VA_STATUS_SUCCESS;
456 }
457