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>
56#include <libavutil/hwcontext_vaapi.h>
63#define DEFAULT_SURFACES 20
64#define MOD_NAME "[vaapi] "
74#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
76 VAEntrypoint va_entrypoint;
78 VAContextID va_context;
80 struct vaapi_context decoder_context;
89#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
91 enum AVCodecID av_codec_id;
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},
106static int vaapi_create_context(
struct vaapi_ctx *ctx,
107 AVCodecContext *codec_ctx)
109 const AVCodecDescriptor *codec_desc;
111 codec_desc = avcodec_descriptor_get(codec_ctx->codec_id);
119 VAProfile *list = av_malloc(profile_count *
sizeof(VAProfile));
125 list, &profile_count);
126 if(
status != VA_STATUS_SUCCESS){
132 VAProfile profile = VAProfileNone;
135 for(
unsigned i = 0; i < FF_ARRAY_ELEMS(vaapi_profiles); i++){
136 if(vaapi_profiles[i].av_codec_id != codec_ctx->codec_id)
139 if(vaapi_profiles[i].codec_profile == codec_ctx->profile){
140 profile = vaapi_profiles[i].va_profile;
145 for(
int i = 0; i < profile_count; i++){
146 if(profile == list[i])
157 ctx->va_profile = profile;
158 ctx->va_entrypoint = VAEntrypointVLD;
161 ctx->va_entrypoint, 0, 0, &ctx->va_config);
162 if(
status != VA_STATUS_SUCCESS){
167 AVVAAPIHWConfig *hwconfig = av_hwdevice_hwconfig_alloc(ctx->
device_ref);
173 hwconfig->config_id = ctx->va_config;
174 AVHWFramesConstraints *constraints = av_hwdevice_get_hwframe_constraints(ctx->
device_ref, hwconfig);
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)
187 codec_ctx->coded_width,
188 codec_ctx->coded_height);
189 av_hwframe_constraints_free(&constraints);
194 av_hwframe_constraints_free(&constraints);
209static enum AVPixelFormat
211 enum AVPixelFormat fallback_fmt)
213 enum AVPixelFormat ret = AV_PIX_FMT_NONE;
214 AVVAAPIHWConfig *hwconfig = NULL;
215 VAConfigID config_id = 0;
216 AVHWFramesConstraints *fc = NULL;
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");
224 fc = av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
226 MSG(ERROR,
"failed to retrieve libavutil frame constraints\n");
236 AVBufferRef *fref = NULL;
237 fref = av_hwframe_ctx_alloc(device_ref);
239 MSG(ERROR,
"failed to alloc libavutil frame context\n");
242 AVHWFramesContext *fctx = (
void *) fref->data;
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");
255 enum AVPixelFormat *fmts = NULL;
256 int rc = av_hwframe_transfer_get_formats(
257 fref, AV_HWFRAME_TRANSFER_DIRECTION_FROM, &fmts, 0);
259 MSG(ERROR,
"failed to get libavutil frame context supported "
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));
274 MSG(VERBOSE,
"Selected HW frames layout: %s\n",
275 av_get_pix_fmt_name(ret));
289 int ret = create_hw_device_ctx(AV_HWDEVICE_TYPE_VAAPI, &ctx->
device_ref);
296#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
297 ret = vaapi_create_context(ctx, s);
304 if (s->active_thread_type & FF_THREAD_FRAME)
305 decode_surfaces += s->thread_count;
323 state->tmp_frame = av_frame_alloc();
324 if(!
state->tmp_frame){
333#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
334 AVVAAPIFramesContext *avfc = ctx->
frame_ctx->hwctx;
336 ctx->va_config, s->coded_width, s->coded_height,
342 if(
status != VA_STATUS_SUCCESS){
349 ctx->decoder_context.config_id = ctx->va_config;
350 ctx->decoder_context.context_id = ctx->va_context;
352 s->hwaccel_context = &ctx->decoder_context;
360 av_frame_free(&
state->tmp_frame);
362#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 74, 100)
void log_msg(int level, const char *format,...)
#define LOG_LEVEL_ERROR
general errors
#define LOG_LEVEL_VERBOSE
#define LOG_LEVEL_WARNING
less severe errors
This file contains functions related to hw acceleration.
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)
This file contains functions related to hw acceleration.
const char * get_avpixfmts_names(const enum AVPixelFormat *pixfmts)
This struct represents initialized video display state.
void * ctx
Type depends on hwaccel type.
AVHWFramesContext * frame_ctx
AVVAAPIDeviceContext * device_vaapi_ctx
AVBufferRef * hw_frames_ctx
AVHWDeviceContext * device_ctx