UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
hwaccel_vaapi.c
Go to the documentation of this file.
1
7/*
8 * Copyright (c) 2018-2023 CESNET z.s.p.o.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, is permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 *
24 * This product includes software developed by CESNET z.s.p.o.
25 *
26 * 4. Neither the name of the CESNET nor the names of its contributors may be
27 * used to endorse or promote products derived from this software without
28 * specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
32 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
33 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
34 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
41 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 */
44
45#ifdef HAVE_CONFIG_H
46#include "config.h"
47#include "config_unix.h"
48#include "config_win32.h"
49#endif // defined HAVE_CONFIG_H
50
51#include <libavcodec/version.h>
52#include <libavutil/pixdesc.h>
53#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
54#include <libavcodec/vaapi.h>
55#endif
56#include <libavutil/hwcontext_vaapi.h>
57
58#include "debug.h"
60#include "hwaccel_vaapi.h"
62
63#define DEFAULT_SURFACES 20
64#define MOD_NAME "[vaapi] "
65
66struct vaapi_ctx {
67 AVBufferRef *device_ref;
68 AVHWDeviceContext *device_ctx;
69 AVVAAPIDeviceContext *device_vaapi_ctx;
70
71 AVBufferRef *hw_frames_ctx;
72 AVHWFramesContext *frame_ctx;
73
74#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
75 VAProfile va_profile;
76 VAEntrypoint va_entrypoint;
77 VAConfigID va_config;
78 VAContextID va_context;
79
80 struct vaapi_context decoder_context;
81#endif
82};
83
85
86 free(s->ctx);
87}
88
89#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
90static const struct {
91 enum AVCodecID av_codec_id;
92 int codec_profile;
93 VAProfile va_profile;
94} vaapi_profiles[] = {
95 {AV_CODEC_ID_MPEG2VIDEO, FF_PROFILE_MPEG2_SIMPLE, VAProfileMPEG2Simple},
96 {AV_CODEC_ID_MPEG2VIDEO, FF_PROFILE_MPEG2_MAIN, VAProfileMPEG2Main},
97 {AV_CODEC_ID_H264, FF_PROFILE_H264_CONSTRAINED_BASELINE, VAProfileH264ConstrainedBaseline},
98 {AV_CODEC_ID_H264, FF_PROFILE_H264_BASELINE, VAProfileH264Baseline},
99 {AV_CODEC_ID_H264, FF_PROFILE_H264_MAIN, VAProfileH264Main},
100 {AV_CODEC_ID_H264, FF_PROFILE_H264_HIGH, VAProfileH264High},
101#if VA_CHECK_VERSION(0, 37, 0)
102 {AV_CODEC_ID_HEVC, FF_PROFILE_HEVC_MAIN, VAProfileHEVCMain},
103#endif
104};
105
106static int vaapi_create_context(struct vaapi_ctx *ctx,
107 AVCodecContext *codec_ctx)
108{
109 const AVCodecDescriptor *codec_desc;
110
111 codec_desc = avcodec_descriptor_get(codec_ctx->codec_id);
112 if(!codec_desc){
113 return -1;
114 }
115
116 int profile_count = vaMaxNumProfiles(ctx->device_vaapi_ctx->display);
117 log_msg(LOG_LEVEL_VERBOSE, "VAAPI Profile count: %d\n", profile_count);
118
119 VAProfile *list = av_malloc(profile_count * sizeof(VAProfile));
120 if(!list){
121 return -1;
122 }
123
124 VAStatus status = vaQueryConfigProfiles(ctx->device_vaapi_ctx->display,
125 list, &profile_count);
126 if(status != VA_STATUS_SUCCESS){
127 log_msg(LOG_LEVEL_ERROR, "[lavd] Profile query failed: %d (%s)\n", status, vaErrorStr(status));
128 av_free(list);
129 return -1;
130 }
131
132 VAProfile profile = VAProfileNone;
133 int match = 0;
134
135 for(unsigned i = 0; i < FF_ARRAY_ELEMS(vaapi_profiles); i++){
136 if(vaapi_profiles[i].av_codec_id != codec_ctx->codec_id)
137 continue;
138
139 if(vaapi_profiles[i].codec_profile == codec_ctx->profile){
140 profile = vaapi_profiles[i].va_profile;
141 break;
142 }
143 }
144
145 for(int i = 0; i < profile_count; i++){
146 if(profile == list[i])
147 match = 1;
148 }
149
150 av_freep(&list);
151
152 if(!match){
153 log_msg(LOG_LEVEL_ERROR, "[lavd] Profile not supported \n");
154 return -1;
155 }
156
157 ctx->va_profile = profile;
158 ctx->va_entrypoint = VAEntrypointVLD;
159
160 status = vaCreateConfig(ctx->device_vaapi_ctx->display, ctx->va_profile,
161 ctx->va_entrypoint, 0, 0, &ctx->va_config);
162 if(status != VA_STATUS_SUCCESS){
163 log_msg(LOG_LEVEL_ERROR, "[lavd] Create config failed: %d (%s)\n", status, vaErrorStr(status));
164 return -1;
165 }
166
167 AVVAAPIHWConfig *hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
168 if(!hwconfig){
169 log_msg(LOG_LEVEL_WARNING, "[lavd] Failed to get constraints. Will try to continue anyways...\n");
170 return 0;
171 }
172
173 hwconfig->config_id = ctx->va_config;
174 AVHWFramesConstraints *constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref, hwconfig);
175 if (!constraints){
176 log_msg(LOG_LEVEL_WARNING, "[lavd] Failed to get constraints. Will try to continue anyways...\n");
177 av_freep(&hwconfig);
178 return 0;
179 }
180
181 if (codec_ctx->coded_width < constraints->min_width ||
182 codec_ctx->coded_width > constraints->max_width ||
183 codec_ctx->coded_height < constraints->min_height ||
184 codec_ctx->coded_height > constraints->max_height)
185 {
186 log_msg(LOG_LEVEL_WARNING, "[lavd] VAAPI hw does not support the resolution %dx%d\n",
187 codec_ctx->coded_width,
188 codec_ctx->coded_height);
189 av_hwframe_constraints_free(&constraints);
190 av_freep(&hwconfig);
191 return -1;
192 }
193
194 av_hwframe_constraints_free(&constraints);
195 av_freep(&hwconfig);
196
197 return 0;
198}
199#endif //LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
200
209static enum AVPixelFormat
210get_sw_format(VADisplay display, AVBufferRef *device_ref,
211 enum AVPixelFormat fallback_fmt)
212{
213 enum AVPixelFormat ret = AV_PIX_FMT_NONE;
214 AVVAAPIHWConfig *hwconfig = NULL;
215 VAConfigID config_id = 0;
216 AVHWFramesConstraints *fc = NULL;
217
218 VAStatus status = vaCreateConfig(
219 display, VAProfileNone, VAEntrypointVideoProc, NULL, 0, &config_id);
220 if (status != VA_STATUS_SUCCESS) {
221 MSG(ERROR, "cannot create config\n");
222 goto fail;
223 }
224 fc = av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
225 if (!fc) {
226 MSG(ERROR, "failed to retrieve libavutil frame constraints\n");
227 goto fail;
228 }
229
230 /*
231 * We need a hwframe_ctx to be able to get the valid formats, but to
232 * initialise it, we need a format, so we get the first format from the
233 * hwconfig. We don't care about the other formats in the config because
234 * the transfer formats list will already include them.
235 */
236 AVBufferRef *fref = NULL;
237 fref = av_hwframe_ctx_alloc(device_ref);
238 if (!fref) {
239 MSG(ERROR, "failed to alloc libavutil frame context\n");
240 goto fail;
241 }
242 AVHWFramesContext *fctx = (void *) fref->data;
243 enum {
244 INIT_SIZE = 128,
245 };
246 fctx->format = AV_PIX_FMT_VAAPI;
247 fctx->sw_format = fc->valid_sw_formats[0];
248 fctx->width = INIT_SIZE;
249 fctx->height = INIT_SIZE;
250 if (av_hwframe_ctx_init(fref) < 0) {
251 MSG(ERROR, "failed to init libavutil frame context\n");
252 goto fail;
253 }
254
255 enum AVPixelFormat *fmts = NULL;
256 int rc = av_hwframe_transfer_get_formats(
257 fref, AV_HWFRAME_TRANSFER_DIRECTION_FROM, &fmts, 0);
258 if (rc) {
259 MSG(ERROR, "failed to get libavutil frame context supported "
260 "formats\n");
261 goto fail;
262 }
263 MSG(DEBUG, "Available HW layouts: %s\n", get_avpixfmts_names(fmts));
264 ret = fmts[0];
265
266fail:
267 av_hwframe_constraints_free(&fc);
268 av_buffer_unref(&fref);
269 if (ret == AV_PIX_FMT_NONE) {
270 MSG(WARNING, "Using fallback HW frames layout: %s\n",
271 av_get_pix_fmt_name(ret));
272 ret = fallback_fmt;
273 }
274 MSG(VERBOSE, "Selected HW frames layout: %s\n",
275 av_get_pix_fmt_name(ret));
276 return ret;
277}
278
279int vaapi_init(struct AVCodecContext *s,
280 struct hw_accel_state *state,
281 codec_t out_codec)
282{
283 (void)(out_codec);
284 struct vaapi_ctx *ctx = calloc(1, sizeof(struct vaapi_ctx));
285 if(!ctx){
286 return -1;
287 }
288
289 int ret = create_hw_device_ctx(AV_HWDEVICE_TYPE_VAAPI, &ctx->device_ref);
290 if(ret < 0)
291 goto fail;
292
293 ctx->device_ctx = (AVHWDeviceContext*)(void *)ctx->device_ref->data;
294 ctx->device_vaapi_ctx = ctx->device_ctx->hwctx;
295
296#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
297 ret = vaapi_create_context(ctx, s);
298 if(ret < 0)
299 goto fail;
300#endif
301
302 int decode_surfaces = DEFAULT_SURFACES;
303
304 if (s->active_thread_type & FF_THREAD_FRAME)
305 decode_surfaces += s->thread_count;
306
307 const enum AVPixelFormat sw_format = get_sw_format(
308 ctx->device_vaapi_ctx->display, ctx->device_ref, s->sw_pix_fmt);
309 ret = create_hw_frame_ctx(ctx->device_ref,
310 s->coded_width,
311 s->coded_height,
312 AV_PIX_FMT_VAAPI,
313 sw_format,
314 decode_surfaces,
315 &ctx->hw_frames_ctx);
316 if(ret < 0)
317 goto fail;
318
319 ctx->frame_ctx = (AVHWFramesContext *)(void *) (ctx->hw_frames_ctx->data);
320
321 s->hw_frames_ctx = ctx->hw_frames_ctx;
322
323 state->tmp_frame = av_frame_alloc();
324 if(!state->tmp_frame){
325 ret = -1;
326 goto fail;
327 }
328 state->type = HWACCEL_VAAPI;
329 state->copy = true;
330 state->ctx = ctx;
331 state->uninit = vaapi_uninit;
332
333#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
334 AVVAAPIFramesContext *avfc = ctx->frame_ctx->hwctx;
335 VAStatus status = vaCreateContext(ctx->device_vaapi_ctx->display,
336 ctx->va_config, s->coded_width, s->coded_height,
337 VA_PROGRESSIVE,
338 avfc->surface_ids,
339 avfc->nb_surfaces,
340 &ctx->va_context);
341
342 if(status != VA_STATUS_SUCCESS){
343 log_msg(LOG_LEVEL_ERROR, "[lavd] Create config failed: %d (%s)\n", status, vaErrorStr(status));
344 ret = -1;
345 goto fail;
346 }
347
348 ctx->decoder_context.display = ctx->device_vaapi_ctx->display;
349 ctx->decoder_context.config_id = ctx->va_config;
350 ctx->decoder_context.context_id = ctx->va_context;
351
352 s->hwaccel_context = &ctx->decoder_context;
353#endif //LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
354
355 av_buffer_unref(&ctx->device_ref);
356 return 0;
357
358
359fail:
360 av_frame_free(&state->tmp_frame);
361 av_buffer_unref(&ctx->hw_frames_ctx);
362#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
363 if(ctx->device_vaapi_ctx)
364 vaDestroyConfig(ctx->device_vaapi_ctx->display, ctx->va_config);
365#endif
366 av_buffer_unref(&ctx->device_ref);
367 free(ctx);
368 return ret;
369}
void ** state
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define LOG_LEVEL_VERBOSE
Definition debug.h:63
#define MSG(l, fmt,...)
Definition debug.h:96
#define LOG_LEVEL_WARNING
less severe errors
Definition debug.h:60
This file contains functions related to hw acceleration.
static enum AVPixelFormat get_sw_format(VADisplay display, AVBufferRef *device_ref, enum AVPixelFormat fallback_fmt)
#define DEFAULT_SURFACES
int vaapi_init(struct AVCodecContext *s, struct hw_accel_state *state, codec_t out_codec)
void vaapi_uninit(struct hw_accel_state *s)
This file contains functions related to hw acceleration.
const char * get_avpixfmts_names(const enum AVPixelFormat *pixfmts)
#define DEBUG
Definition main.c:27
This struct represents initialized video display state.
void * ctx
Type depends on hwaccel type.
AVHWFramesContext * frame_ctx
AVBufferRef * device_ref
AVVAAPIDeviceContext * device_vaapi_ctx
AVBufferRef * hw_frames_ctx
AVHWDeviceContext * device_ctx
@ HWACCEL_VAAPI
Definition types.h:117
codec_t
Definition types.h:65