History

hwcontext_vaapi: Add DRM to VAAPI mapping

Mark Thompson committed 9 years ago

Commit 170c6531 parent 9cb23cd1 file · +228 −1Browse files

Unified · Split

libavutil/hwcontext_vaapi.c+228−1
@@ -25,6 +25,11 @@ · 24 unmodified lines
25# include <va/va_drm.h>25# include <va/va_drm.h>
26#endif26#endif
27 27
28#if CONFIG_LIBDRM
29# include <va/va_drmcommon.h>
30# include <drm_fourcc.h>
31#endif
32
28#include <fcntl.h>33#include <fcntl.h>
29#if HAVE_UNISTD_H34#if HAVE_UNISTD_H
30# include <unistd.h>35# include <unistd.h>
@@ -41,6 +46,10 @@ · 10 unmodified lines
41#include "pixdesc.h"46#include "pixdesc.h"
42#include "pixfmt.h"47#include "pixfmt.h"
43 48
49#if CONFIG_LIBDRM
50# include "hwcontext_drm.h"
51#endif
52
44typedef struct VAAPIDevicePriv {53typedef struct VAAPIDevicePriv {
45#if HAVE_VAAPI_X1154#if HAVE_VAAPI_X11
46 Display *x11_display;55 Display *x11_display;
@@ -897,6 +906,173 @@ static int vaapi_map_from(AVHWFramesContext *hwfc, AVFrame *dst, · 850 unmodified lines
897 return 0;906 return 0;
898}907}
899 908
909#if CONFIG_LIBDRM
910
911#define DRM_MAP(va, layers, ...) { \
912 VA_FOURCC_ ## va, \
913 layers, \
914 { __VA_ARGS__ } \
915 }
916static const struct {
917 uint32_t va_fourcc;
918 int nb_layer_formats;
919 uint32_t layer_formats[AV_DRM_MAX_PLANES];
920} vaapi_drm_format_map[] = {
921 DRM_MAP(NV12, 2, DRM_FORMAT_R8, DRM_FORMAT_RG88),
922 DRM_MAP(NV12, 1, DRM_FORMAT_NV12),
923#ifdef VA_FOURCC_P010
924 DRM_MAP(P010, 2, DRM_FORMAT_R16, DRM_FORMAT_RG1616),
925#endif
926 DRM_MAP(BGRA, 1, DRM_FORMAT_BGRA8888),
927 DRM_MAP(BGRX, 1, DRM_FORMAT_BGRX8888),
928 DRM_MAP(RGBA, 1, DRM_FORMAT_RGBA8888),
929 DRM_MAP(RGBX, 1, DRM_FORMAT_RGBX8888),
930#ifdef VA_FOURCC_ABGR
931 DRM_MAP(ABGR, 1, DRM_FORMAT_ABGR8888),
932 DRM_MAP(XBGR, 1, DRM_FORMAT_XBGR8888),
933#endif
934 DRM_MAP(ARGB, 1, DRM_FORMAT_ARGB8888),
935 DRM_MAP(XRGB, 1, DRM_FORMAT_XRGB8888),
936};
937#undef DRM_MAP
938
939static void vaapi_unmap_from_drm(AVHWFramesContext *dst_fc,
940 HWMapDescriptor *hwmap)
941{
942 AVVAAPIDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
943
944 VASurfaceID surface_id = (VASurfaceID)(uintptr_t)hwmap->priv;
945
946 av_log(dst_fc, AV_LOG_DEBUG, "Destroy surface %#x.\n", surface_id);
947
948 vaDestroySurfaces(dst_dev->display, &surface_id, 1);
949}
950
951static int vaapi_map_from_drm(AVHWFramesContext *src_fc, AVFrame *dst,
952 const AVFrame *src, int flags)
953{
954 AVHWFramesContext *dst_fc =
955 (AVHWFramesContext*)dst->hw_frames_ctx->data;
956 AVVAAPIDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
957 const AVDRMFrameDescriptor *desc;
958 VASurfaceID surface_id;
959 VAStatus vas;
960 uint32_t va_fourcc, va_rt_format;
961 int err, i, j, k;
962
963 unsigned long buffer_handle;
964 VASurfaceAttribExternalBuffers buffer_desc;
965 VASurfaceAttrib attrs[2] = {
966 {
967 .type = VASurfaceAttribMemoryType,
968 .flags = VA_SURFACE_ATTRIB_SETTABLE,
969 .value.type = VAGenericValueTypeInteger,
970 .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME,
971 },
972 {
973 .type = VASurfaceAttribExternalBufferDescriptor,
974 .flags = VA_SURFACE_ATTRIB_SETTABLE,
975 .value.type = VAGenericValueTypePointer,
976 .value.value.p = &buffer_desc,
977 }
978 };
979
980 desc = (AVDRMFrameDescriptor*)src->data[0];
981
982 if (desc->nb_objects != 1) {
983 av_log(dst_fc, AV_LOG_ERROR, "VAAPI can only map frames "
984 "made from a single DRM object.\n");
985 return AVERROR(EINVAL);
986 }
987
988 va_fourcc = 0;
989 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_drm_format_map); i++) {
990 if (desc->nb_layers != vaapi_drm_format_map[i].nb_layer_formats)
991 continue;
992 for (j = 0; j < desc->nb_layers; j++) {
993 if (desc->layers[j].format !=
994 vaapi_drm_format_map[i].layer_formats[j])
995 break;
996 }
997 if (j != desc->nb_layers)
998 continue;
999 va_fourcc = vaapi_drm_format_map[i].va_fourcc;
1000 break;
1001 }
1002 if (!va_fourcc) {
1003 av_log(dst_fc, AV_LOG_ERROR, "DRM format not supported "
1004 "by VAAPI.\n");
1005 return AVERROR(EINVAL);
1006 }
1007
1008 av_log(dst_fc, AV_LOG_DEBUG, "Map DRM object %d to VAAPI as "
1009 "%08x.\n", desc->objects[0].fd, va_fourcc);
1010
1011 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++) {
1012 if (vaapi_format_map[i].fourcc == va_fourcc)
1013 va_rt_format = vaapi_format_map[i].rt_format;
1014 }
1015
1016 buffer_handle = desc->objects[0].fd;
1017 buffer_desc.pixel_format = va_fourcc;
1018 buffer_desc.width = src_fc->width;
1019 buffer_desc.height = src_fc->height;
1020 buffer_desc.data_size = desc->objects[0].size;
1021 buffer_desc.buffers = &buffer_handle;
1022 buffer_desc.num_buffers = 1;
1023 buffer_desc.flags = 0;
1024
1025 k = 0;
1026 for (i = 0; i < desc->nb_layers; i++) {
1027 for (j = 0; j < desc->layers[i].nb_planes; j++) {
1028 buffer_desc.pitches[k] = desc->layers[i].planes[j].pitch;
1029 buffer_desc.offsets[k] = desc->layers[i].planes[j].offset;
1030 ++k;
1031 }
1032 }
1033 buffer_desc.num_planes = k;
1034
1035 vas = vaCreateSurfaces(dst_dev->display, va_rt_format,
1036 src->width, src->height,
1037 &surface_id, 1,
1038 attrs, FF_ARRAY_ELEMS(attrs));
1039 if (vas != VA_STATUS_SUCCESS) {
1040 av_log(dst_fc, AV_LOG_ERROR, "Failed to create surface from DRM "
1041 "object: %d (%s).\n", vas, vaErrorStr(vas));
1042 return AVERROR(EIO);
1043 }
1044 av_log(dst_fc, AV_LOG_DEBUG, "Create surface %#x.\n", surface_id);
1045
1046 err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
1047 &vaapi_unmap_from_drm,
1048 (void*)(uintptr_t)surface_id);
1049 if (err < 0)
1050 return err;
1051
1052 dst->width = src->width;
1053 dst->height = src->height;
1054 dst->data[3] = (uint8_t*)(uintptr_t)surface_id;
1055
1056 av_log(dst_fc, AV_LOG_DEBUG, "Mapped DRM object %d to "
1057 "surface %#x.\n", desc->objects[0].fd, surface_id);
1058
1059 return 0;
1060}
1061#endif
1062
1063static int vaapi_map_to(AVHWFramesContext *hwfc, AVFrame *dst,
1064 const AVFrame *src, int flags)
1065{
1066 switch (src->format) {
1067#if CONFIG_LIBDRM
1068 case AV_PIX_FMT_DRM_PRIME:
1069 return vaapi_map_from_drm(hwfc, dst, src, flags);
1070#endif
1071 default:
1072 return AVERROR(ENOSYS);
1073 }
1074}
1075
900static void vaapi_device_free(AVHWDeviceContext *ctx)1076static void vaapi_device_free(AVHWDeviceContext *ctx)
901{1077{
902 AVVAAPIDeviceContext *hwctx = ctx->hwctx;1078 AVVAAPIDeviceContext *hwctx = ctx->hwctx;
@@ -999,6 +1175,56 @@ static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device, · 96 unmodified lines
999 return 0;1175 return 0;
1000}1176}
1001 1177
1178static int vaapi_device_derive(AVHWDeviceContext *ctx,
1179 AVHWDeviceContext *src_ctx, int flags)
1180{
1181#if CONFIG_LIBDRM
1182 if (src_ctx->type == AV_HWDEVICE_TYPE_DRM) {
1183 AVDRMDeviceContext *src_hwctx = src_ctx->hwctx;
1184 AVVAAPIDeviceContext *hwctx = ctx->hwctx;
1185 VADisplay *display;
1186 VAStatus vas;
1187 VAAPIDevicePriv *priv;
1188 int major, minor;
1189
1190 if (src_hwctx->fd < 0) {
1191 av_log(ctx, AV_LOG_ERROR, "DRM instance requires an associated "
1192 "device to derive a VA display from.\n");
1193 return AVERROR(EINVAL);
1194 }
1195
1196 priv = av_mallocz(sizeof(*priv));
1197 if (!priv)
1198 return AVERROR(ENOMEM);
1199
1200 // Inherits the fd from the source context, which will close it.
1201 priv->drm_fd = -1;
1202
1203 ctx->user_opaque = priv;
1204 ctx->free = &vaapi_device_free;
1205
1206 display = vaGetDisplayDRM(src_hwctx->fd);
1207 if (!display) {
1208 av_log(ctx, AV_LOG_ERROR, "Failed to open a VA display from "
1209 "DRM device.\n");
1210 return AVERROR(EIO);
1211 }
1212
1213 hwctx->display = display;
1214
1215 vas = vaInitialize(display, &major, &minor);
1216 if (vas != VA_STATUS_SUCCESS) {
1217 av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI "
1218 "connection: %d (%s).\n", vas, vaErrorStr(vas));
1219 return AVERROR(EIO);
1220 }
1221
1222 return 0;
1223 }
1224#endif
1225 return AVERROR(ENOSYS);
1226}
1227
1002const HWContextType ff_hwcontext_type_vaapi = {1228const HWContextType ff_hwcontext_type_vaapi = {
1003 .type = AV_HWDEVICE_TYPE_VAAPI,1229 .type = AV_HWDEVICE_TYPE_VAAPI,
1004 .name = "VAAPI",1230 .name = "VAAPI",
@@ -1010,6 +1236,7 @@ const HWContextType ff_hwcontext_type_vaapi = { · 5 unmodified lines
1010 .frames_priv_size = sizeof(VAAPIFramesContext),1236 .frames_priv_size = sizeof(VAAPIFramesContext),
1011 1237
1012 .device_create = &vaapi_device_create,1238 .device_create = &vaapi_device_create,
1239 .device_derive = &vaapi_device_derive,
1013 .device_init = &vaapi_device_init,1240 .device_init = &vaapi_device_init,
1014 .device_uninit = &vaapi_device_uninit,1241 .device_uninit = &vaapi_device_uninit,
1015 .frames_get_constraints = &vaapi_frames_get_constraints,1242 .frames_get_constraints = &vaapi_frames_get_constraints,
@@ -1019,7 +1246,7 @@ const HWContextType ff_hwcontext_type_vaapi = { · 3 unmodified lines
1019 .transfer_get_formats = &vaapi_transfer_get_formats,1246 .transfer_get_formats = &vaapi_transfer_get_formats,
1020 .transfer_data_to = &vaapi_transfer_data_to,1247 .transfer_data_to = &vaapi_transfer_data_to,
1021 .transfer_data_from = &vaapi_transfer_data_from,1248 .transfer_data_from = &vaapi_transfer_data_from,
1022 .map_to = NULL,1249 .map_to = &vaapi_map_to,
1023 .map_from = &vaapi_map_from,1250 .map_from = &vaapi_map_from,
1024 1251
1025 .pix_fmts = (const enum AVPixelFormat[]) {1252 .pix_fmts = (const enum AVPixelFormat[]) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t