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_VP9_PROFILE2; ++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
vlVaQueryConfigEntrypoints(VADriverContextP ctx,VAProfile profile,VAEntrypoint * entrypoint_list,int * num_entrypoints)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    assert(*num_entrypoints <= ctx->max_entrypoints);
106 
107    return VA_STATUS_SUCCESS;
108 }
109 
110 VAStatus
vlVaGetConfigAttributes(VADriverContextP ctx,VAProfile profile,VAEntrypoint entrypoint,VAConfigAttrib * attrib_list,int num_attribs)111 vlVaGetConfigAttributes(VADriverContextP ctx, VAProfile profile, VAEntrypoint entrypoint,
112                         VAConfigAttrib *attrib_list, int num_attribs)
113 {
114    struct pipe_screen *pscreen;
115    int i;
116 
117    if (!ctx)
118       return VA_STATUS_ERROR_INVALID_CONTEXT;
119 
120    pscreen = VL_VA_PSCREEN(ctx);
121 
122    for (i = 0; i < num_attribs; ++i) {
123       unsigned int value;
124       if ((entrypoint == VAEntrypointVLD) &&
125           (pscreen->get_video_param(pscreen, ProfileToPipe(profile),
126            PIPE_VIDEO_ENTRYPOINT_BITSTREAM, PIPE_VIDEO_CAP_SUPPORTED))) {
127          switch (attrib_list[i].type) {
128          case VAConfigAttribRTFormat:
129             value = VA_RT_FORMAT_YUV420 | VA_RT_FORMAT_YUV422;
130             if (pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P010,
131                                                    ProfileToPipe(profile),
132                                                    PIPE_VIDEO_ENTRYPOINT_BITSTREAM) ||
133                 pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P016,
134                                                    ProfileToPipe(profile),
135                                                    PIPE_VIDEO_ENTRYPOINT_BITSTREAM))
136                value |= VA_RT_FORMAT_YUV420_10BPP;
137             break;
138          default:
139             value = VA_ATTRIB_NOT_SUPPORTED;
140             break;
141          }
142       } else if ((entrypoint == VAEntrypointEncSlice) &&
143                  (pscreen->get_video_param(pscreen, ProfileToPipe(profile),
144                   PIPE_VIDEO_ENTRYPOINT_ENCODE, PIPE_VIDEO_CAP_SUPPORTED))) {
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_BITSTREAM) ||
151                 pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P016,
152                                                    ProfileToPipe(profile),
153                                                    PIPE_VIDEO_ENTRYPOINT_BITSTREAM))
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 VAConfigAttribEncPackedHeaders:
160             value = 0;
161             break;
162          case VAConfigAttribEncMaxRefFrames:
163             value = 1;
164             break;
165          default:
166             value = VA_ATTRIB_NOT_SUPPORTED;
167             break;
168          }
169       } else if (entrypoint == VAEntrypointVideoProc) {
170          switch (attrib_list[i].type) {
171          case VAConfigAttribRTFormat:
172             value = (VA_RT_FORMAT_YUV420 |
173                      VA_RT_FORMAT_YUV420_10BPP |
174                      VA_RT_FORMAT_RGB32);
175             break;
176          default:
177             value = VA_ATTRIB_NOT_SUPPORTED;
178             break;
179          }
180       } else {
181          value = VA_ATTRIB_NOT_SUPPORTED;
182       }
183       attrib_list[i].value = value;
184    }
185 
186    return VA_STATUS_SUCCESS;
187 }
188 
189 VAStatus
vlVaCreateConfig(VADriverContextP ctx,VAProfile profile,VAEntrypoint entrypoint,VAConfigAttrib * attrib_list,int num_attribs,VAConfigID * config_id)190 vlVaCreateConfig(VADriverContextP ctx, VAProfile profile, VAEntrypoint entrypoint,
191                  VAConfigAttrib *attrib_list, int num_attribs, VAConfigID *config_id)
192 {
193    vlVaDriver *drv;
194    vlVaConfig *config;
195    struct pipe_screen *pscreen;
196    enum pipe_video_profile p;
197    unsigned int supported_rt_formats;
198 
199    if (!ctx)
200       return VA_STATUS_ERROR_INVALID_CONTEXT;
201 
202    drv = VL_VA_DRIVER(ctx);
203 
204    if (!drv)
205       return VA_STATUS_ERROR_INVALID_CONTEXT;
206 
207    config = CALLOC(1, sizeof(vlVaConfig));
208    if (!config)
209       return VA_STATUS_ERROR_ALLOCATION_FAILED;
210 
211    if (profile == VAProfileNone && entrypoint == VAEntrypointVideoProc) {
212       config->entrypoint = PIPE_VIDEO_ENTRYPOINT_UNKNOWN;
213       config->profile = PIPE_VIDEO_PROFILE_UNKNOWN;
214       supported_rt_formats = VA_RT_FORMAT_YUV420 |
215                              VA_RT_FORMAT_YUV420_10BPP |
216                              VA_RT_FORMAT_RGB32;
217       for (int i = 0; i < num_attribs; i++) {
218          if (attrib_list[i].type == VAConfigAttribRTFormat) {
219             if (attrib_list[i].value & supported_rt_formats) {
220                config->rt_format = attrib_list[i].value;
221             } else {
222                FREE(config);
223                return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
224             }
225          }
226       }
227 
228       /* Default value if not specified in the input attributes. */
229       if (!config->rt_format)
230          config->rt_format = supported_rt_formats;
231 
232       mtx_lock(&drv->mutex);
233       *config_id = handle_table_add(drv->htab, config);
234       mtx_unlock(&drv->mutex);
235       return VA_STATUS_SUCCESS;
236    }
237 
238    p = ProfileToPipe(profile);
239    if (p == PIPE_VIDEO_PROFILE_UNKNOWN) {
240       FREE(config);
241       return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
242    }
243 
244    pscreen = VL_VA_PSCREEN(ctx);
245 
246    switch (entrypoint) {
247    case VAEntrypointVLD:
248       if (!pscreen->get_video_param(pscreen, p, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
249 				    PIPE_VIDEO_CAP_SUPPORTED)) {
250          FREE(config);
251          return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
252       }
253 
254       config->entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
255       break;
256 
257    case VAEntrypointEncSlice:
258       if (!pscreen->get_video_param(pscreen, p, PIPE_VIDEO_ENTRYPOINT_ENCODE,
259 				    PIPE_VIDEO_CAP_SUPPORTED)) {
260          FREE(config);
261          return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
262       }
263 
264       config->entrypoint = PIPE_VIDEO_ENTRYPOINT_ENCODE;
265       break;
266 
267    default:
268       FREE(config);
269       return VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
270    }
271 
272    config->profile = p;
273    supported_rt_formats = VA_RT_FORMAT_YUV420 | VA_RT_FORMAT_YUV422;
274    if (pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P010, p,
275          config->entrypoint) ||
276        pscreen->is_video_format_supported(pscreen, PIPE_FORMAT_P016, p,
277          config->entrypoint))
278       supported_rt_formats |= VA_RT_FORMAT_YUV420_10BPP;
279 
280    for (int i = 0; i <num_attribs ; i++) {
281       if (attrib_list[i].type == VAConfigAttribRateControl) {
282          if (attrib_list[i].value == VA_RC_CBR)
283             config->rc = PIPE_H264_ENC_RATE_CONTROL_METHOD_CONSTANT;
284          else if (attrib_list[i].value == VA_RC_VBR)
285             config->rc = PIPE_H264_ENC_RATE_CONTROL_METHOD_VARIABLE;
286          else
287             config->rc = PIPE_H264_ENC_RATE_CONTROL_METHOD_DISABLE;
288       }
289       if (attrib_list[i].type == VAConfigAttribRTFormat) {
290          if (attrib_list[i].value & supported_rt_formats) {
291             config->rt_format = attrib_list[i].value;
292          } else {
293             FREE(config);
294             return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
295          }
296       }
297    }
298 
299    /* Default value if not specified in the input attributes. */
300    if (!config->rt_format)
301       config->rt_format = supported_rt_formats;
302 
303    mtx_lock(&drv->mutex);
304    *config_id = handle_table_add(drv->htab, config);
305    mtx_unlock(&drv->mutex);
306 
307    return VA_STATUS_SUCCESS;
308 }
309 
310 VAStatus
vlVaDestroyConfig(VADriverContextP ctx,VAConfigID config_id)311 vlVaDestroyConfig(VADriverContextP ctx, VAConfigID config_id)
312 {
313    vlVaDriver *drv;
314    vlVaConfig *config;
315 
316    if (!ctx)
317       return VA_STATUS_ERROR_INVALID_CONTEXT;
318 
319    drv = VL_VA_DRIVER(ctx);
320 
321    if (!drv)
322       return VA_STATUS_ERROR_INVALID_CONTEXT;
323 
324    mtx_lock(&drv->mutex);
325    config = handle_table_get(drv->htab, config_id);
326 
327    if (!config) {
328       mtx_unlock(&drv->mutex);
329       return VA_STATUS_ERROR_INVALID_CONFIG;
330    }
331 
332    FREE(config);
333    handle_table_remove(drv->htab, config_id);
334    mtx_unlock(&drv->mutex);
335 
336    return VA_STATUS_SUCCESS;
337 }
338 
339 VAStatus
vlVaQueryConfigAttributes(VADriverContextP ctx,VAConfigID config_id,VAProfile * profile,VAEntrypoint * entrypoint,VAConfigAttrib * attrib_list,int * num_attribs)340 vlVaQueryConfigAttributes(VADriverContextP ctx, VAConfigID config_id, VAProfile *profile,
341                           VAEntrypoint *entrypoint, VAConfigAttrib *attrib_list, int *num_attribs)
342 {
343    vlVaDriver *drv;
344    vlVaConfig *config;
345 
346    if (!ctx)
347       return VA_STATUS_ERROR_INVALID_CONTEXT;
348 
349    drv = VL_VA_DRIVER(ctx);
350 
351    if (!drv)
352       return VA_STATUS_ERROR_INVALID_CONTEXT;
353 
354    mtx_lock(&drv->mutex);
355    config = handle_table_get(drv->htab, config_id);
356    mtx_unlock(&drv->mutex);
357 
358    if (!config)
359       return VA_STATUS_ERROR_INVALID_CONFIG;
360 
361    *profile = PipeToProfile(config->profile);
362 
363    switch (config->entrypoint) {
364    case PIPE_VIDEO_ENTRYPOINT_BITSTREAM:
365       *entrypoint = VAEntrypointVLD;
366       break;
367    case PIPE_VIDEO_ENTRYPOINT_ENCODE:
368       *entrypoint = VAEntrypointEncSlice;
369       break;
370    case PIPE_VIDEO_ENTRYPOINT_UNKNOWN:
371       *entrypoint = VAEntrypointVideoProc;
372       break;
373    default:
374       return VA_STATUS_ERROR_INVALID_CONFIG;
375    }
376 
377    *num_attribs = 1;
378    attrib_list[0].type = VAConfigAttribRTFormat;
379    attrib_list[0].value = config->rt_format;
380 
381    return VA_STATUS_SUCCESS;
382 }
383