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