UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
hwaccel_vaapi.c File Reference

This file contains functions related to hw acceleration. More...

#include <libavcodec/version.h>
#include <libavutil/pixdesc.h>
#include <libavutil/hwcontext_vaapi.h>
#include "debug.h"
#include "hwaccel_libav_common.h"
#include "hwaccel_vaapi.h"
#include "libavcodec/lavc_common.h"
Include dependency graph for hwaccel_vaapi.c:

Go to the source code of this file.

Classes

struct  vaapi_ctx
 

Macros

#define DEFAULT_SURFACES   20
 
#define MOD_NAME   "[vaapi] "
 

Functions

static enum AVPixelFormat get_sw_format (VADisplay display, AVBufferRef *device_ref, enum AVPixelFormat fallback_fmt)
 
int vaapi_init (struct AVCodecContext *s, struct hw_accel_state *state, codec_t out_codec)
 
void vaapi_uninit (struct hw_accel_state *s)
 

Detailed Description

This file contains functions related to hw acceleration.

Author
Martin Piatka piatk.nosp@m.a@ce.nosp@m.snet..nosp@m.cz

Definition in file hwaccel_vaapi.c.

Macro Definition Documentation

◆ DEFAULT_SURFACES

#define DEFAULT_SURFACES   20

Definition at line 63 of file hwaccel_vaapi.c.

Referenced by vaapi_init().

◆ MOD_NAME

#define MOD_NAME   "[vaapi] "

Definition at line 64 of file hwaccel_vaapi.c.

Function Documentation

◆ get_sw_format()

static enum AVPixelFormat get_sw_format ( VADisplay display,
AVBufferRef * device_ref,
enum AVPixelFormat fallback_fmt )
static

Returns first SW format from valid_sw_formats. This is usually AV_PIX_FMT_YUV420P or AV_PIX_FMT_NV12.

The code borrows heavily from mpv https://github.com/mpv-player/mpv/blob/master/video/out/hwdec/hwdec_vaapi.c namely from function try_format_config().

< just some valid size

Definition at line 210 of file hwaccel_vaapi.c.

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}
#define MSG(l, fmt,...)
Definition debug.h:96
const char * get_avpixfmts_names(const enum AVPixelFormat *pixfmts)
#define DEBUG
Definition main.c:27
This struct represents initialized video display state.

References DEBUG, get_avpixfmts_names(), and MSG.

Referenced by vaapi_init().

Here is the call graph for this function:

◆ vaapi_init()

int vaapi_init ( struct AVCodecContext * s,
struct hw_accel_state * state,
codec_t out_codec )

Definition at line 279 of file hwaccel_vaapi.c.

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
static enum AVPixelFormat get_sw_format(VADisplay display, AVBufferRef *device_ref, enum AVPixelFormat fallback_fmt)
#define DEFAULT_SURFACES
void vaapi_uninit(struct hw_accel_state *s)
AVHWFramesContext * frame_ctx
AVBufferRef * device_ref
AVVAAPIDeviceContext * device_vaapi_ctx
AVBufferRef * hw_frames_ctx
AVHWDeviceContext * device_ctx
@ HWACCEL_VAAPI
Definition types.h:117

References DEFAULT_SURFACES, vaapi_ctx::device_ctx, vaapi_ctx::device_ref, vaapi_ctx::device_vaapi_ctx, vaapi_ctx::frame_ctx, get_sw_format(), vaapi_ctx::hw_frames_ctx, HWACCEL_VAAPI, LOG_LEVEL_ERROR, log_msg(), state, and vaapi_uninit().

Referenced by get_format_callback().

Here is the call graph for this function:

◆ vaapi_uninit()

void vaapi_uninit ( struct hw_accel_state * s)

Definition at line 84 of file hwaccel_vaapi.c.

84 {
85
86 free(s->ctx);
87}
void * ctx
Type depends on hwaccel type.

References hw_accel_state::ctx.

Referenced by vaapi_init().