mirror of
https://github.com/mpv-player/mpv
synced 2025-01-11 09:29:29 +00:00
vaapi: support new libavcodec vaapi API
The old API is deprecated, and libavcodec prints a warning at runtime. The new API is a bit nicer and does many things for you, such as managing the underlying hwaccel decoder. libavutil also provides code for managing surfaces (we use their surface pool). The new code does not contain any code from the original MPlayer VAAPI patch (that was used as base for some of the vaapi code in mpv). Thus the new code is LGPL. The new API actually does not add any visible symbols, so the only way to detect it is a version check. Of course, the versions overlap between FFmpeg and Libav, which requires additional care. The new API did not get merged into FFmpeg yet, so there's no check for FFmpeg.
This commit is contained in:
parent
f4263922c7
commit
2afef344fb
@ -75,6 +75,8 @@ struct vd_lavc_hwdec {
|
||||
// efficiency by not blocking on the hardware pipeline by reading back
|
||||
// immediately after decoding.
|
||||
int delay_queue;
|
||||
// If true, AVCodecContext will destroy the underlying decoder.
|
||||
bool volatile_context;
|
||||
int (*probe)(struct lavc_ctx *ctx, struct vd_lavc_hwdec *hwdec,
|
||||
const char *codec);
|
||||
int (*init)(struct lavc_ctx *ctx);
|
||||
|
239
video/decode/vaapi.c
Normal file
239
video/decode/vaapi.c
Normal file
@ -0,0 +1,239 @@
|
||||
/*
|
||||
* This file is part of mpv.
|
||||
*
|
||||
* mpv is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* mpv is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavutil/common.h>
|
||||
#include <libavutil/hwcontext.h>
|
||||
#include <libavutil/hwcontext_vaapi.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "lavc.h"
|
||||
#include "common/common.h"
|
||||
#include "common/av_common.h"
|
||||
#include "video/fmt-conversion.h"
|
||||
#include "video/vaapi.h"
|
||||
#include "video/mp_image_pool.h"
|
||||
#include "video/hwdec.h"
|
||||
#include "video/filter/vf.h"
|
||||
|
||||
#define ADDITIONAL_SURFACES (HWDEC_EXTRA_SURFACES + HWDEC_DELAY_QUEUE_COUNT)
|
||||
|
||||
struct priv {
|
||||
struct mp_log *log;
|
||||
struct mp_vaapi_ctx *ctx;
|
||||
bool own_ctx;
|
||||
|
||||
AVBufferRef *device_ref;
|
||||
AVBufferRef *frames_ref;
|
||||
|
||||
struct mp_image_pool *sw_pool;
|
||||
};
|
||||
|
||||
|
||||
static int init_decoder(struct lavc_ctx *ctx, int w, int h)
|
||||
{
|
||||
struct priv *p = ctx->hwdec_priv;
|
||||
// From avconv_vaapi.c. Disgusting, but apparently this is the best we get.
|
||||
int required_sw_format = ctx->avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
|
||||
AV_PIX_FMT_P010 : AV_PIX_FMT_NV12;
|
||||
|
||||
assert(!ctx->avctx->hw_frames_ctx);
|
||||
|
||||
if (p->frames_ref) {
|
||||
AVHWFramesContext *fctx = (void *)p->frames_ref->data;
|
||||
if (fctx->width != w || fctx->height != h ||
|
||||
fctx->sw_format != required_sw_format)
|
||||
{
|
||||
av_buffer_unref(&p->frames_ref);
|
||||
}
|
||||
}
|
||||
|
||||
if (!p->frames_ref) {
|
||||
p->frames_ref = av_hwframe_ctx_alloc(p->device_ref);
|
||||
if (!p->frames_ref)
|
||||
return -1;
|
||||
|
||||
AVHWFramesContext *fctx = (void *)p->frames_ref->data;
|
||||
|
||||
fctx->format = AV_PIX_FMT_VAAPI;
|
||||
fctx->sw_format = required_sw_format;
|
||||
fctx->width = w;
|
||||
fctx->height = h;
|
||||
|
||||
fctx->initial_pool_size = hwdec_get_max_refs(ctx) + ADDITIONAL_SURFACES;
|
||||
|
||||
va_lock(p->ctx);
|
||||
int res = av_hwframe_ctx_init(p->frames_ref);
|
||||
va_unlock(p->ctx);
|
||||
|
||||
if (res > 0) {
|
||||
MP_ERR(ctx, "Failed to allocate hw frames.\n");
|
||||
av_buffer_unref(&p->frames_ref);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
ctx->avctx->hw_frames_ctx = av_buffer_ref(p->frames_ref);
|
||||
if (!ctx->avctx->hw_frames_ctx)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct mp_image *update_format(struct lavc_ctx *ctx, struct mp_image *img)
|
||||
{
|
||||
va_surface_init_subformat(img);
|
||||
return img;
|
||||
}
|
||||
|
||||
static void uninit(struct lavc_ctx *ctx)
|
||||
{
|
||||
struct priv *p = ctx->hwdec_priv;
|
||||
|
||||
if (!p)
|
||||
return;
|
||||
|
||||
av_buffer_unref(&p->frames_ref);
|
||||
av_buffer_unref(&p->device_ref);
|
||||
|
||||
if (p->own_ctx)
|
||||
va_destroy(p->ctx);
|
||||
|
||||
talloc_free(p);
|
||||
ctx->hwdec_priv = NULL;
|
||||
}
|
||||
|
||||
static int init(struct lavc_ctx *ctx, bool direct)
|
||||
{
|
||||
struct priv *p = talloc_ptrtype(NULL, p);
|
||||
*p = (struct priv) {
|
||||
.log = mp_log_new(p, ctx->log, "vaapi"),
|
||||
.sw_pool = talloc_steal(p, mp_image_pool_new(17)),
|
||||
};
|
||||
|
||||
if (direct) {
|
||||
p->ctx = hwdec_devices_get(ctx->hwdec_devs, HWDEC_VAAPI)->ctx;
|
||||
} else {
|
||||
p->ctx = va_create_standalone(ctx->log, false);
|
||||
if (!p->ctx) {
|
||||
talloc_free(p);
|
||||
return -1;
|
||||
}
|
||||
p->own_ctx = true;
|
||||
}
|
||||
|
||||
ctx->hwdec_priv = p;
|
||||
|
||||
p->device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
|
||||
if (!p->device_ref)
|
||||
return -1;
|
||||
|
||||
AVHWDeviceContext *hwctx = (void *)p->device_ref->data;
|
||||
AVVAAPIDeviceContext *vactx = hwctx->hwctx;
|
||||
|
||||
vactx->display = p->ctx->display;
|
||||
|
||||
// Some mpv downstream code uses this.
|
||||
hwctx->user_opaque = p->ctx;
|
||||
|
||||
if (av_hwdevice_ctx_init(p->device_ref) < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int init_direct(struct lavc_ctx *ctx)
|
||||
{
|
||||
return init(ctx, true);
|
||||
}
|
||||
|
||||
static int probe(struct lavc_ctx *ctx, struct vd_lavc_hwdec *hwdec,
|
||||
const char *codec)
|
||||
{
|
||||
if (!hwdec_devices_load(ctx->hwdec_devs, HWDEC_VAAPI))
|
||||
return HWDEC_ERR_NO_CTX;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int probe_copy(struct lavc_ctx *ctx, struct vd_lavc_hwdec *hwdec,
|
||||
const char *codec)
|
||||
{
|
||||
struct mp_vaapi_ctx *dummy = va_create_standalone(ctx->log, true);
|
||||
if (!dummy)
|
||||
return HWDEC_ERR_NO_CTX;
|
||||
bool emulated = va_guess_if_emulated(dummy);
|
||||
va_destroy(dummy);
|
||||
if (emulated)
|
||||
return HWDEC_ERR_EMULATED;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int init_copy(struct lavc_ctx *ctx)
|
||||
{
|
||||
return init(ctx, false);
|
||||
}
|
||||
|
||||
static struct mp_image *copy_image(struct lavc_ctx *ctx, struct mp_image *img)
|
||||
{
|
||||
struct priv *p = ctx->hwdec_priv;
|
||||
|
||||
struct mp_image *simg = va_surface_download(img, p->sw_pool);
|
||||
talloc_free(img);
|
||||
return simg;
|
||||
}
|
||||
|
||||
static void intel_shit_lock(struct lavc_ctx *ctx)
|
||||
{
|
||||
struct priv *p = ctx->hwdec_priv;
|
||||
va_lock(p->ctx);
|
||||
}
|
||||
|
||||
static void intel_crap_unlock(struct lavc_ctx *ctx)
|
||||
{
|
||||
struct priv *p = ctx->hwdec_priv;
|
||||
va_unlock(p->ctx);
|
||||
}
|
||||
|
||||
const struct vd_lavc_hwdec mp_vd_lavc_vaapi = {
|
||||
.type = HWDEC_VAAPI,
|
||||
.image_format = IMGFMT_VAAPI,
|
||||
.volatile_context = true,
|
||||
.probe = probe,
|
||||
.init = init_direct,
|
||||
.uninit = uninit,
|
||||
.init_decoder = init_decoder,
|
||||
.lock = intel_shit_lock,
|
||||
.unlock = intel_crap_unlock,
|
||||
.process_image = update_format,
|
||||
};
|
||||
|
||||
const struct vd_lavc_hwdec mp_vd_lavc_vaapi_copy = {
|
||||
.type = HWDEC_VAAPI_COPY,
|
||||
.copying = true,
|
||||
.image_format = IMGFMT_VAAPI,
|
||||
.volatile_context = true,
|
||||
.probe = probe_copy,
|
||||
.init = init_copy,
|
||||
.uninit = uninit,
|
||||
.init_decoder = init_decoder,
|
||||
.process_image = copy_image,
|
||||
.delay_queue = HWDEC_DELAY_QUEUE_COUNT,
|
||||
};
|
@ -690,7 +690,8 @@ static enum AVPixelFormat get_format_hwdec(struct AVCodecContext *avctx,
|
||||
ctx->hwdec_h != avctx->coded_height ||
|
||||
ctx->hwdec_fmt != ctx->hwdec->image_format ||
|
||||
ctx->hwdec_profile != avctx->profile ||
|
||||
ctx->hwdec_request_reinit;
|
||||
ctx->hwdec_request_reinit ||
|
||||
ctx->hwdec->volatile_context;
|
||||
ctx->hwdec_w = avctx->coded_width;
|
||||
ctx->hwdec_h = avctx->coded_height;
|
||||
ctx->hwdec_fmt = ctx->hwdec->image_format;
|
||||
|
21
wscript
21
wscript
@ -419,6 +419,13 @@ libav_dependencies = [
|
||||
'req': True,
|
||||
'fmsg': "Unable to find development files for some of the required \
|
||||
FFmpeg/Libav libraries. You need at least {0}. Aborting.".format(libav_versions_string)
|
||||
}, {
|
||||
'name': 'is_ffmpeg',
|
||||
'desc': 'libav* is FFmpeg',
|
||||
# FFmpeg <=> LIBAVUTIL_VERSION_MICRO>=100
|
||||
'func': check_statement('libavutil/version.h',
|
||||
'int x[LIBAVUTIL_VERSION_MICRO >= 100 ? 1 : -1]',
|
||||
use='libav')
|
||||
}, {
|
||||
'name': '--libswresample',
|
||||
'desc': 'libswresample',
|
||||
@ -833,6 +840,20 @@ hwaccel_features = [
|
||||
'desc': 'libavcodec VAAPI hwaccel',
|
||||
'deps': [ 'vaapi' ],
|
||||
'func': check_headers('libavcodec/vaapi.h', use='libav'),
|
||||
}, {
|
||||
'name': '--vaapi-hwaccel-new',
|
||||
'desc': 'libavcodec VAAPI hwaccel (new)',
|
||||
'deps': [ 'vaapi-hwaccel' ],
|
||||
'deps_neg': [ 'is_ffmpeg' ],
|
||||
'func': check_statement('libavcodec/version.h',
|
||||
'int x[LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 26, 0) ? 1 : -1]',
|
||||
use='libav'),
|
||||
}, {
|
||||
'name': '--vaapi-hwaccel-old',
|
||||
'desc': 'libavcodec VAAPI hwaccel (old)',
|
||||
'deps': [ 'vaapi-hwaccel' ],
|
||||
'deps_neg': [ 'vaapi-hwaccel-new' ],
|
||||
'func': check_true,
|
||||
}, {
|
||||
'name': '--videotoolbox-hwaccel',
|
||||
'desc': 'libavcodec videotoolbox hwaccel',
|
||||
|
@ -307,7 +307,8 @@ def build(ctx):
|
||||
( "video/decode/dxva2.c", "d3d-hwaccel" ),
|
||||
( "video/decode/d3d11va.c", "d3d-hwaccel" ),
|
||||
( "video/decode/d3d.c", "win32" ),
|
||||
( "video/decode/vaapi_old.c", "vaapi-hwaccel" ),
|
||||
( "video/decode/vaapi.c", "vaapi-hwaccel-new" ),
|
||||
( "video/decode/vaapi_old.c", "vaapi-hwaccel-old" ),
|
||||
( "video/decode/vd_lavc.c" ),
|
||||
( "video/decode/videotoolbox.c", "videotoolbox-hwaccel" ),
|
||||
( "video/decode/vdpau.c", "vdpau-hwaccel" ),
|
||||
|
Loading…
Reference in New Issue
Block a user