2013-09-20 13:55:13 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "vaapi.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/msg.h"
|
2014-08-21 20:04:25 +00:00
|
|
|
#include "osdep/threads.h"
|
2013-09-20 13:55:13 +00:00
|
|
|
#include "mp_image.h"
|
|
|
|
#include "img_format.h"
|
2013-09-27 14:48:45 +00:00
|
|
|
#include "mp_image_pool.h"
|
2013-09-20 13:55:13 +00:00
|
|
|
|
2013-12-21 17:11:01 +00:00
|
|
|
bool check_va_status(struct mp_log *log, VAStatus status, const char *msg)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
|
|
|
if (status != VA_STATUS_SUCCESS) {
|
2013-12-21 17:11:01 +00:00
|
|
|
mp_err(log, "%s: %s\n", msg, vaErrorStr(status));
|
2013-09-20 13:55:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int va_get_colorspace_flag(enum mp_csp csp)
|
|
|
|
{
|
|
|
|
switch (csp) {
|
|
|
|
case MP_CSP_BT_601: return VA_SRC_BT601;
|
|
|
|
case MP_CSP_BT_709: return VA_SRC_BT709;
|
|
|
|
case MP_CSP_SMPTE_240M: return VA_SRC_SMPTE_240;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fmtentry {
|
|
|
|
uint32_t va;
|
|
|
|
enum mp_imgfmt mp;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct fmtentry va_to_imgfmt[] = {
|
|
|
|
{VA_FOURCC_YV12, IMGFMT_420P},
|
|
|
|
{VA_FOURCC_I420, IMGFMT_420P},
|
|
|
|
{VA_FOURCC_IYUV, IMGFMT_420P},
|
|
|
|
{VA_FOURCC_NV12, IMGFMT_NV12},
|
|
|
|
{VA_FOURCC_UYVY, IMGFMT_UYVY},
|
|
|
|
{VA_FOURCC_YUY2, IMGFMT_YUYV},
|
|
|
|
// Note: not sure about endian issues (the mp formats are byte-addressed)
|
|
|
|
{VA_FOURCC_RGBA, IMGFMT_RGBA},
|
|
|
|
{VA_FOURCC_RGBX, IMGFMT_RGBA},
|
|
|
|
{VA_FOURCC_BGRA, IMGFMT_BGRA},
|
|
|
|
{VA_FOURCC_BGRX, IMGFMT_BGRA},
|
|
|
|
{0 , IMGFMT_NONE}
|
|
|
|
};
|
|
|
|
|
|
|
|
enum mp_imgfmt va_fourcc_to_imgfmt(uint32_t fourcc)
|
|
|
|
{
|
|
|
|
for (const struct fmtentry *entry = va_to_imgfmt; entry->va; ++entry) {
|
|
|
|
if (entry->va == fourcc)
|
|
|
|
return entry->mp;
|
|
|
|
}
|
|
|
|
return IMGFMT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t va_fourcc_from_imgfmt(int imgfmt)
|
|
|
|
{
|
|
|
|
for (const struct fmtentry *entry = va_to_imgfmt; entry->va; ++entry) {
|
|
|
|
if (entry->mp == imgfmt)
|
|
|
|
return entry->va;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-22 16:47:14 +00:00
|
|
|
static struct mp_image *ctx_download_image(struct mp_hwdec_ctx *ctx,
|
|
|
|
struct mp_image *mpi,
|
|
|
|
struct mp_image_pool *swpool)
|
|
|
|
{
|
|
|
|
return va_surface_download(mpi, swpool);
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:55:13 +00:00
|
|
|
struct va_image_formats {
|
|
|
|
VAImageFormat *entries;
|
|
|
|
int num;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void va_get_formats(struct mp_vaapi_ctx *ctx)
|
|
|
|
{
|
|
|
|
int num = vaMaxNumImageFormats(ctx->display);
|
|
|
|
VAImageFormat entries[num];
|
|
|
|
VAStatus status = vaQueryImageFormats(ctx->display, entries, &num);
|
2013-12-21 17:11:01 +00:00
|
|
|
if (!CHECK_VA_STATUS(ctx, "vaQueryImageFormats()"))
|
2013-09-20 13:55:13 +00:00
|
|
|
return;
|
|
|
|
struct va_image_formats *formats = talloc_ptrtype(ctx, formats);
|
|
|
|
formats->entries = talloc_array(formats, VAImageFormat, num);
|
|
|
|
formats->num = num;
|
2013-12-21 17:11:01 +00:00
|
|
|
MP_VERBOSE(ctx, "%d image formats available:\n", num);
|
2013-09-20 13:55:13 +00:00
|
|
|
for (int i = 0; i < num; i++) {
|
|
|
|
formats->entries[i] = entries[i];
|
2013-12-21 17:11:01 +00:00
|
|
|
MP_VERBOSE(ctx, " %s\n", VA_STR_FOURCC(entries[i].fourcc));
|
2013-09-20 13:55:13 +00:00
|
|
|
}
|
|
|
|
ctx->image_formats = formats;
|
|
|
|
}
|
|
|
|
|
2015-06-20 20:26:57 +00:00
|
|
|
struct mp_vaapi_ctx *va_initialize(VADisplay *display, struct mp_log *plog,
|
|
|
|
bool probing)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
2013-12-23 10:19:09 +00:00
|
|
|
struct mp_vaapi_ctx *res = NULL;
|
2013-12-21 17:11:01 +00:00
|
|
|
struct mp_log *log = mp_log_new(NULL, plog, "/vaapi");
|
2013-09-20 13:55:13 +00:00
|
|
|
int major_version, minor_version;
|
|
|
|
int status = vaInitialize(display, &major_version, &minor_version);
|
2015-06-20 20:26:57 +00:00
|
|
|
if (status != VA_STATUS_SUCCESS && probing)
|
|
|
|
goto error;
|
2013-12-21 17:11:01 +00:00
|
|
|
if (!check_va_status(log, status, "vaInitialize()"))
|
|
|
|
goto error;
|
2013-09-20 13:55:13 +00:00
|
|
|
|
2013-12-21 17:11:01 +00:00
|
|
|
mp_verbose(log, "VA API version %d.%d\n", major_version, minor_version);
|
2013-09-20 13:55:13 +00:00
|
|
|
|
2013-12-23 10:19:09 +00:00
|
|
|
res = talloc_ptrtype(NULL, res);
|
2013-09-20 13:55:13 +00:00
|
|
|
*res = (struct mp_vaapi_ctx) {
|
2013-12-21 17:11:01 +00:00
|
|
|
.log = talloc_steal(res, log),
|
2013-09-20 13:55:13 +00:00
|
|
|
.display = display,
|
2015-01-22 14:32:23 +00:00
|
|
|
.hwctx = {
|
2015-02-02 21:43:05 +00:00
|
|
|
.type = HWDEC_VAAPI,
|
2015-01-22 14:32:23 +00:00
|
|
|
.priv = res,
|
|
|
|
.vaapi_ctx = res,
|
2015-01-22 16:47:14 +00:00
|
|
|
.download_image = ctx_download_image,
|
2015-01-22 14:32:23 +00:00
|
|
|
},
|
2013-09-20 13:55:13 +00:00
|
|
|
};
|
2014-08-21 20:04:25 +00:00
|
|
|
mpthread_mutex_init_recursive(&res->lock);
|
2013-09-20 13:55:13 +00:00
|
|
|
|
|
|
|
va_get_formats(res);
|
|
|
|
if (!res->image_formats)
|
|
|
|
goto error;
|
|
|
|
return res;
|
|
|
|
|
|
|
|
error:
|
2013-12-21 17:11:01 +00:00
|
|
|
if (res && res->display)
|
2013-09-20 13:55:13 +00:00
|
|
|
vaTerminate(res->display);
|
2013-12-21 17:11:01 +00:00
|
|
|
talloc_free(log);
|
2013-09-20 13:55:13 +00:00
|
|
|
talloc_free(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Undo va_initialize, and close the VADisplay.
|
|
|
|
void va_destroy(struct mp_vaapi_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx) {
|
|
|
|
if (ctx->display)
|
|
|
|
vaTerminate(ctx->display);
|
2014-08-21 20:04:25 +00:00
|
|
|
pthread_mutex_destroy(&ctx->lock);
|
2013-09-20 13:55:13 +00:00
|
|
|
talloc_free(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 21:12:30 +00:00
|
|
|
VAImageFormat *va_image_format_from_imgfmt(struct mp_vaapi_ctx *ctx, int imgfmt)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
2015-01-21 21:12:30 +00:00
|
|
|
struct va_image_formats *formats = ctx->image_formats;
|
2013-09-20 13:55:13 +00:00
|
|
|
const int fourcc = va_fourcc_from_imgfmt(imgfmt);
|
|
|
|
if (!formats || !formats->num || !fourcc)
|
|
|
|
return NULL;
|
|
|
|
for (int i = 0; i < formats->num; i++) {
|
|
|
|
if (formats->entries[i].fourcc == fourcc)
|
|
|
|
return &formats->entries[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-17 17:22:35 +00:00
|
|
|
struct va_surface {
|
2013-12-21 17:11:01 +00:00
|
|
|
struct mp_vaapi_ctx *ctx;
|
2013-09-20 13:55:13 +00:00
|
|
|
VADisplay display;
|
2014-03-17 17:22:48 +00:00
|
|
|
|
|
|
|
VASurfaceID id;
|
|
|
|
int rt_format;
|
|
|
|
|
2015-06-29 13:13:45 +00:00
|
|
|
// The actually allocated surface size (needed for cropping).
|
|
|
|
// mp_images can have a smaller size than this, which means they are
|
|
|
|
// cropped down to a smaller size by removing right/bottom pixels.
|
|
|
|
int w, h;
|
|
|
|
|
2013-09-27 14:48:45 +00:00
|
|
|
VAImage image; // used for software decoding case
|
2014-03-17 17:22:48 +00:00
|
|
|
};
|
2013-09-20 13:55:13 +00:00
|
|
|
|
2014-03-17 17:22:35 +00:00
|
|
|
VASurfaceID va_surface_id(struct mp_image *mpi)
|
|
|
|
{
|
|
|
|
return mpi && mpi->imgfmt == IMGFMT_VAAPI ?
|
|
|
|
(VASurfaceID)(uintptr_t)mpi->planes[3] : VA_INVALID_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct va_surface *va_surface_in_mp_image(struct mp_image *mpi)
|
|
|
|
{
|
|
|
|
return mpi && mpi->imgfmt == IMGFMT_VAAPI ?
|
|
|
|
(struct va_surface*)mpi->planes[0] : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int va_surface_rt_format(struct mp_image *mpi)
|
|
|
|
{
|
|
|
|
struct va_surface *surface = va_surface_in_mp_image(mpi);
|
|
|
|
return surface ? surface->rt_format : 0;
|
|
|
|
}
|
|
|
|
|
2014-03-17 17:22:48 +00:00
|
|
|
static void release_va_surface(void *arg)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
2014-03-17 17:22:48 +00:00
|
|
|
struct va_surface *surface = arg;
|
|
|
|
|
2014-08-21 20:04:25 +00:00
|
|
|
va_lock(surface->ctx);
|
2013-09-20 13:55:13 +00:00
|
|
|
if (surface->id != VA_INVALID_ID) {
|
2014-03-17 17:22:48 +00:00
|
|
|
if (surface->image.image_id != VA_INVALID_ID)
|
|
|
|
vaDestroyImage(surface->display, surface->image.image_id);
|
|
|
|
vaDestroySurfaces(surface->display, &surface->id, 1);
|
2013-09-20 13:55:13 +00:00
|
|
|
}
|
2014-08-21 20:04:25 +00:00
|
|
|
va_unlock(surface->ctx);
|
2013-09-20 13:55:13 +00:00
|
|
|
talloc_free(surface);
|
|
|
|
}
|
|
|
|
|
2014-03-17 17:22:25 +00:00
|
|
|
static struct mp_image *alloc_surface(struct mp_vaapi_ctx *ctx, int rt_format,
|
|
|
|
int w, int h)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
|
|
|
VASurfaceID id = VA_INVALID_ID;
|
|
|
|
VAStatus status;
|
2014-08-21 20:04:25 +00:00
|
|
|
va_lock(ctx);
|
2015-07-08 12:13:27 +00:00
|
|
|
status = vaCreateSurfaces(ctx->display, rt_format, w, h, &id, 1, NULL, 0);
|
2014-08-21 20:04:25 +00:00
|
|
|
va_unlock(ctx);
|
2014-03-17 17:22:25 +00:00
|
|
|
if (!CHECK_VA_STATUS(ctx, "vaCreateSurfaces()"))
|
2013-09-20 13:55:13 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
struct va_surface *surface = talloc_ptrtype(NULL, surface);
|
|
|
|
if (!surface)
|
|
|
|
return NULL;
|
|
|
|
|
2014-03-17 17:22:48 +00:00
|
|
|
*surface = (struct va_surface){
|
|
|
|
.ctx = ctx,
|
|
|
|
.id = id,
|
|
|
|
.rt_format = rt_format,
|
2015-06-29 13:13:45 +00:00
|
|
|
.w = w,
|
|
|
|
.h = h,
|
2014-03-17 17:22:48 +00:00
|
|
|
.display = ctx->display,
|
|
|
|
.image = { .image_id = VA_INVALID_ID, .buf = VA_INVALID_ID },
|
|
|
|
};
|
2013-09-20 13:55:13 +00:00
|
|
|
|
2014-03-17 17:22:25 +00:00
|
|
|
struct mp_image img = {0};
|
|
|
|
mp_image_setfmt(&img, IMGFMT_VAAPI);
|
2014-03-17 17:22:48 +00:00
|
|
|
mp_image_set_size(&img, w, h);
|
2014-03-17 17:22:25 +00:00
|
|
|
img.planes[0] = (uint8_t*)surface;
|
|
|
|
img.planes[3] = (uint8_t*)(uintptr_t)surface->id;
|
|
|
|
return mp_image_new_custom_ref(&img, surface, release_va_surface);
|
2013-09-20 13:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void va_surface_image_destroy(struct va_surface *surface)
|
|
|
|
{
|
2014-03-17 17:22:48 +00:00
|
|
|
if (!surface || surface->image.image_id == VA_INVALID_ID)
|
2013-09-20 13:55:13 +00:00
|
|
|
return;
|
2014-03-17 17:22:48 +00:00
|
|
|
vaDestroyImage(surface->display, surface->image.image_id);
|
|
|
|
surface->image.image_id = VA_INVALID_ID;
|
2013-09-20 13:55:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 17:23:24 +00:00
|
|
|
static int va_surface_image_alloc(struct mp_image *img, VAImageFormat *format)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
2014-03-17 17:22:48 +00:00
|
|
|
struct va_surface *p = va_surface_in_mp_image(img);
|
|
|
|
if (!format || !p)
|
2014-03-17 17:23:24 +00:00
|
|
|
return -1;
|
2014-03-17 17:22:48 +00:00
|
|
|
|
2013-09-20 13:55:13 +00:00
|
|
|
if (p->image.image_id != VA_INVALID_ID &&
|
|
|
|
p->image.format.fourcc == format->fourcc)
|
2014-03-17 17:23:24 +00:00
|
|
|
return 0;
|
2013-09-20 13:55:13 +00:00
|
|
|
|
2014-08-21 20:04:25 +00:00
|
|
|
int r = 0;
|
|
|
|
va_lock(p->ctx);
|
|
|
|
|
2014-03-17 17:22:48 +00:00
|
|
|
va_surface_image_destroy(p);
|
|
|
|
|
2015-06-29 13:13:45 +00:00
|
|
|
VAStatus status = vaCreateImage(p->display, format, p->w, p->h, &p->image);
|
2015-06-29 13:10:07 +00:00
|
|
|
if (!CHECK_VA_STATUS(p->ctx, "vaCreateImage()")) {
|
2013-09-29 11:42:22 +00:00
|
|
|
p->image.image_id = VA_INVALID_ID;
|
2015-06-29 13:10:07 +00:00
|
|
|
r = -1;
|
2013-09-20 13:55:13 +00:00
|
|
|
}
|
2014-08-21 20:04:25 +00:00
|
|
|
|
|
|
|
va_unlock(p->ctx);
|
|
|
|
return r;
|
2013-09-20 13:55:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 17:22:25 +00:00
|
|
|
// img must be a VAAPI surface; make sure its internal VAImage is allocated
|
|
|
|
// to a format corresponding to imgfmt (or return an error).
|
2014-03-17 17:22:35 +00:00
|
|
|
int va_surface_alloc_imgfmt(struct mp_image *img, int imgfmt)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
2014-03-17 17:23:24 +00:00
|
|
|
struct va_surface *p = va_surface_in_mp_image(img);
|
|
|
|
if (!p)
|
2014-03-17 17:22:25 +00:00
|
|
|
return -1;
|
2014-03-17 17:23:24 +00:00
|
|
|
// Multiple FourCCs can refer to the same imgfmt, so check by doing the
|
|
|
|
// surjective conversion first.
|
|
|
|
if (p->image.image_id != VA_INVALID_ID &&
|
|
|
|
va_fourcc_to_imgfmt(p->image.format.fourcc) == imgfmt)
|
|
|
|
return 0;
|
2015-01-21 21:12:30 +00:00
|
|
|
VAImageFormat *format = va_image_format_from_imgfmt(p->ctx, imgfmt);
|
2013-09-20 13:55:13 +00:00
|
|
|
if (!format)
|
2014-03-17 17:22:25 +00:00
|
|
|
return -1;
|
2014-03-17 17:23:24 +00:00
|
|
|
if (va_surface_image_alloc(img, format) < 0)
|
2014-03-17 17:22:25 +00:00
|
|
|
return -1;
|
|
|
|
return 0;
|
2013-09-20 13:55:13 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 17:11:01 +00:00
|
|
|
bool va_image_map(struct mp_vaapi_ctx *ctx, VAImage *image, struct mp_image *mpi)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
|
|
|
int imgfmt = va_fourcc_to_imgfmt(image->format.fourcc);
|
|
|
|
if (imgfmt == IMGFMT_NONE)
|
|
|
|
return false;
|
|
|
|
void *data = NULL;
|
2014-08-21 20:04:25 +00:00
|
|
|
va_lock(ctx);
|
2013-12-21 17:11:01 +00:00
|
|
|
const VAStatus status = vaMapBuffer(ctx->display, image->buf, &data);
|
2014-08-21 20:04:25 +00:00
|
|
|
va_unlock(ctx);
|
2013-12-21 17:11:01 +00:00
|
|
|
if (!CHECK_VA_STATUS(ctx, "vaMapBuffer()"))
|
2013-09-20 13:55:13 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*mpi = (struct mp_image) {0};
|
|
|
|
mp_image_setfmt(mpi, imgfmt);
|
|
|
|
mp_image_set_size(mpi, image->width, image->height);
|
|
|
|
|
|
|
|
for (int p = 0; p < image->num_planes; p++) {
|
|
|
|
mpi->stride[p] = image->pitches[p];
|
|
|
|
mpi->planes[p] = (uint8_t *)data + image->offsets[p];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (image->format.fourcc == VA_FOURCC_YV12) {
|
2013-09-25 20:22:02 +00:00
|
|
|
MPSWAP(unsigned int, mpi->stride[1], mpi->stride[2]);
|
|
|
|
MPSWAP(uint8_t *, mpi->planes[1], mpi->planes[2]);
|
2013-09-20 13:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-21 17:11:01 +00:00
|
|
|
bool va_image_unmap(struct mp_vaapi_ctx *ctx, VAImage *image)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
2014-08-21 20:04:25 +00:00
|
|
|
va_lock(ctx);
|
2013-12-21 17:11:01 +00:00
|
|
|
const VAStatus status = vaUnmapBuffer(ctx->display, image->buf);
|
2014-08-21 20:04:25 +00:00
|
|
|
va_unlock(ctx);
|
2013-12-21 17:11:01 +00:00
|
|
|
return CHECK_VA_STATUS(ctx, "vaUnmapBuffer()");
|
2013-09-20 13:55:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 17:22:35 +00:00
|
|
|
// va_dst: copy destination, must be IMGFMT_VAAPI
|
2014-03-17 17:22:48 +00:00
|
|
|
// sw_src: copy source, must be a software p
|
2014-03-17 17:22:35 +00:00
|
|
|
int va_surface_upload(struct mp_image *va_dst, struct mp_image *sw_src)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
2014-03-17 17:22:48 +00:00
|
|
|
struct va_surface *p = va_surface_in_mp_image(va_dst);
|
|
|
|
if (!p)
|
2014-03-17 17:22:35 +00:00
|
|
|
return -1;
|
2013-09-20 13:55:13 +00:00
|
|
|
|
2014-03-17 17:23:24 +00:00
|
|
|
if (va_surface_alloc_imgfmt(va_dst, sw_src->imgfmt) < 0)
|
2014-03-17 17:22:35 +00:00
|
|
|
return -1;
|
2013-09-27 15:44:21 +00:00
|
|
|
|
2013-09-20 13:55:13 +00:00
|
|
|
struct mp_image img;
|
2013-12-21 17:11:01 +00:00
|
|
|
if (!va_image_map(p->ctx, &p->image, &img))
|
2014-03-17 17:22:35 +00:00
|
|
|
return -1;
|
2015-06-29 13:13:45 +00:00
|
|
|
assert(sw_src->w <= img.w && sw_src->h <= img.h);
|
|
|
|
mp_image_set_size(&img, sw_src->w, sw_src->h); // copy only visible part
|
2014-03-17 17:22:35 +00:00
|
|
|
mp_image_copy(&img, sw_src);
|
2013-12-21 17:11:01 +00:00
|
|
|
va_image_unmap(p->ctx, &p->image);
|
2013-09-20 13:55:13 +00:00
|
|
|
|
2015-06-29 13:10:07 +00:00
|
|
|
va_lock(p->ctx);
|
2015-07-08 12:13:27 +00:00
|
|
|
VAStatus status = vaPutImage(p->display, p->id,
|
|
|
|
p->image.image_id,
|
|
|
|
0, 0, sw_src->w, sw_src->h,
|
|
|
|
0, 0, sw_src->w, sw_src->h);
|
2015-06-29 13:10:07 +00:00
|
|
|
va_unlock(p->ctx);
|
|
|
|
if (!CHECK_VA_STATUS(p->ctx, "vaPutImage()"))
|
|
|
|
return -1;
|
2013-09-20 13:55:13 +00:00
|
|
|
|
2014-03-17 17:22:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-17 17:22:48 +00:00
|
|
|
static struct mp_image *try_download(struct mp_image *src,
|
2013-09-27 14:48:45 +00:00
|
|
|
struct mp_image_pool *pool)
|
|
|
|
{
|
|
|
|
VAStatus status;
|
2014-03-17 17:22:48 +00:00
|
|
|
struct va_surface *p = va_surface_in_mp_image(src);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
2013-09-27 14:48:45 +00:00
|
|
|
|
2014-03-17 17:23:24 +00:00
|
|
|
VAImage *image = &p->image;
|
2013-09-27 14:48:45 +00:00
|
|
|
|
2014-03-17 17:23:24 +00:00
|
|
|
if (image->image_id == VA_INVALID_ID ||
|
|
|
|
!va_fourcc_to_imgfmt(image->format.fourcc))
|
2013-09-27 14:48:45 +00:00
|
|
|
return NULL;
|
|
|
|
|
2015-06-29 13:10:07 +00:00
|
|
|
va_lock(p->ctx);
|
|
|
|
status = vaGetImage(p->display, p->id, 0, 0,
|
2015-06-29 13:13:45 +00:00
|
|
|
p->w, p->h, image->image_id);
|
2015-06-29 13:10:07 +00:00
|
|
|
va_unlock(p->ctx);
|
|
|
|
if (status != VA_STATUS_SUCCESS)
|
|
|
|
return NULL;
|
2013-09-27 14:48:45 +00:00
|
|
|
|
|
|
|
struct mp_image *dst = NULL;
|
|
|
|
struct mp_image tmp;
|
2014-03-17 17:22:48 +00:00
|
|
|
if (va_image_map(p->ctx, image, &tmp)) {
|
2015-06-29 13:13:45 +00:00
|
|
|
assert(src->w <= tmp.w && src->h <= tmp.h);
|
|
|
|
mp_image_set_size(&tmp, src->w, src->h); // copy only visible part
|
2015-01-22 16:47:14 +00:00
|
|
|
dst = mp_image_pool_get(pool, tmp.imgfmt, tmp.w, tmp.h);
|
2015-06-29 13:10:17 +00:00
|
|
|
if (dst) {
|
video: introduce failure path for image allocations
Until now, failure to allocate image data resulted in a crash (i.e.
abort() was called). This was intentional, because it's pretty silly to
degrade playback, and in almost all situations, the OOM will probably
kill you anyway. (And then there's the standard Linux overcommit
behavior, which also will kill you at some point.)
But I changed my opinion, so here we go. This change does not affect
_all_ memory allocations, just image data. Now in most failure cases,
the output will just be skipped. For video filters, this coincidentally
means that failure is treated as EOF (because the playback core assumes
EOF if nothing comes out of the video filter chain). In other
situations, output might be in some way degraded, like skipping frames,
not scaling OSD, and such.
Functions whose return values changed semantics:
mp_image_alloc
mp_image_new_copy
mp_image_new_ref
mp_image_make_writeable
mp_image_setrefp
mp_image_to_av_frame_and_unref
mp_image_from_av_frame
mp_image_new_external_ref
mp_image_new_custom_ref
mp_image_pool_make_writeable
mp_image_pool_get
mp_image_pool_new_copy
mp_vdpau_mixed_frame_create
vf_alloc_out_image
vf_make_out_image_writeable
glGetWindowScreenshot
2014-06-17 20:43:43 +00:00
|
|
|
mp_image_copy(dst, &tmp);
|
2015-06-29 13:10:17 +00:00
|
|
|
mp_image_copy_attributes(dst, src);
|
|
|
|
}
|
2014-03-17 17:22:48 +00:00
|
|
|
va_image_unmap(p->ctx, image);
|
2013-09-27 14:48:45 +00:00
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2014-03-17 17:23:24 +00:00
|
|
|
// Return a software copy of the IMGFMT_VAAPI src image.
|
2013-09-27 14:48:45 +00:00
|
|
|
// pool is optional (used for allocating returned images).
|
2014-03-17 17:22:35 +00:00
|
|
|
struct mp_image *va_surface_download(struct mp_image *src,
|
2013-09-27 14:48:45 +00:00
|
|
|
struct mp_image_pool *pool)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
2014-03-17 17:22:48 +00:00
|
|
|
struct va_surface *p = va_surface_in_mp_image(src);
|
|
|
|
if (!p)
|
2014-03-17 17:22:35 +00:00
|
|
|
return NULL;
|
2014-03-17 17:22:48 +00:00
|
|
|
struct mp_vaapi_ctx *ctx = p->ctx;
|
2014-08-21 20:04:25 +00:00
|
|
|
va_lock(ctx);
|
2014-03-17 17:22:48 +00:00
|
|
|
VAStatus status = vaSyncSurface(p->display, p->id);
|
2014-08-21 20:04:25 +00:00
|
|
|
va_unlock(ctx);
|
2013-12-21 17:11:01 +00:00
|
|
|
if (!CHECK_VA_STATUS(ctx, "vaSyncSurface()"))
|
2013-09-20 13:55:13 +00:00
|
|
|
return NULL;
|
|
|
|
|
2014-03-17 17:23:24 +00:00
|
|
|
struct mp_image *mpi = try_download(src, pool);
|
|
|
|
if (mpi)
|
|
|
|
return mpi;
|
2013-09-27 14:48:45 +00:00
|
|
|
|
2013-09-20 13:55:13 +00:00
|
|
|
// We have no clue which format will work, so try them all.
|
2013-12-21 17:11:01 +00:00
|
|
|
for (int i = 0; i < ctx->image_formats->num; i++) {
|
|
|
|
VAImageFormat *format = &ctx->image_formats->entries[i];
|
2014-03-17 17:23:24 +00:00
|
|
|
if (va_surface_image_alloc(src, format) < 0)
|
|
|
|
continue;
|
|
|
|
mpi = try_download(src, pool);
|
2013-09-27 14:48:45 +00:00
|
|
|
if (mpi)
|
|
|
|
return mpi;
|
2013-09-20 13:55:13 +00:00
|
|
|
}
|
2013-09-27 14:48:45 +00:00
|
|
|
|
2013-12-21 17:11:01 +00:00
|
|
|
MP_ERR(ctx, "failed to get surface data.\n");
|
2013-09-20 13:55:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-17 17:22:25 +00:00
|
|
|
struct pool_alloc_ctx {
|
|
|
|
struct mp_vaapi_ctx *vaapi;
|
|
|
|
int rt_format;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct mp_image *alloc_pool(void *pctx, int fmt, int w, int h)
|
|
|
|
{
|
|
|
|
struct pool_alloc_ctx *alloc_ctx = pctx;
|
|
|
|
if (fmt != IMGFMT_VAAPI)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return alloc_surface(alloc_ctx->vaapi, alloc_ctx->rt_format, w, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The allocator of the given image pool to allocate VAAPI surfaces, using
|
|
|
|
// the given rt_format.
|
|
|
|
void va_pool_set_allocator(struct mp_image_pool *pool, struct mp_vaapi_ctx *ctx,
|
|
|
|
int rt_format)
|
|
|
|
{
|
|
|
|
struct pool_alloc_ctx *alloc_ctx = talloc_ptrtype(pool, alloc_ctx);
|
|
|
|
*alloc_ctx = (struct pool_alloc_ctx){
|
|
|
|
.vaapi = ctx,
|
|
|
|
.rt_format = rt_format,
|
|
|
|
};
|
|
|
|
mp_image_pool_set_allocator(pool, alloc_pool, alloc_ctx);
|
|
|
|
mp_image_pool_set_lru(pool);
|
|
|
|
}
|
2014-05-27 23:37:53 +00:00
|
|
|
|
|
|
|
bool va_guess_if_emulated(struct mp_vaapi_ctx *ctx)
|
|
|
|
{
|
2014-08-21 20:04:25 +00:00
|
|
|
va_lock(ctx);
|
2014-05-27 23:37:53 +00:00
|
|
|
const char *s = vaQueryVendorString(ctx->display);
|
2014-08-21 20:04:25 +00:00
|
|
|
va_unlock(ctx);
|
2014-05-27 23:37:53 +00:00
|
|
|
return s && strstr(s, "VDPAU backend");
|
|
|
|
}
|