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