24 # include <initguid.h>
27 # include <va/va_win32.h>
31 # include <va/va_x11.h>
34 # include <va/va_drm.h>
38 # include <va/va_drmcommon.h>
40 # include <drm_fourcc.h>
41 # ifndef DRM_FORMAT_MOD_INVALID
42 # define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
118 } VAAPIFormatDescriptor;
120 #define MAP(va, rt, av, swap_uv) { \
122 VA_RT_FORMAT_ ## rt, \
129 MAP(NV12, YUV420, NV12, 0),
130 #ifdef VA_FOURCC_I420
131 MAP(I420, YUV420, YUV420P, 0),
133 MAP(YV12, YUV420, YUV420P, 1),
134 MAP(IYUV, YUV420, YUV420P, 0),
135 MAP(422
H, YUV422, YUV422P, 0),
136 #ifdef VA_FOURCC_YV16
137 MAP(YV16, YUV422, YUV422P, 1),
139 MAP(UYVY, YUV422, UYVY422, 0),
140 MAP(YUY2, YUV422, YUYV422, 0),
141 #ifdef VA_FOURCC_Y210
142 MAP(Y210, YUV422_10, Y210, 0),
144 #ifdef VA_FOURCC_Y212
145 MAP(Y212, YUV422_12, Y212, 0),
147 MAP(411
P, YUV411, YUV411P, 0),
148 MAP(422
V, YUV422, YUV440P, 0),
149 MAP(444
P, YUV444, YUV444P, 0),
150 #ifdef VA_FOURCC_XYUV
151 MAP(XYUV, YUV444, VUYX, 0),
153 MAP(Y800, YUV400, GRAY8, 0),
154 #ifdef VA_FOURCC_P010
155 MAP(P010, YUV420_10BPP, P010, 0),
157 #ifdef VA_FOURCC_P012
158 MAP(P012, YUV420_12, P012, 0),
160 MAP(BGRA, RGB32, BGRA, 0),
161 MAP(BGRX, RGB32, BGR0, 0),
163 MAP(RGBX, RGB32, RGB0, 0),
164 #ifdef VA_FOURCC_ABGR
165 MAP(ABGR, RGB32, ABGR, 0),
166 MAP(XBGR, RGB32, 0BGR, 0),
168 MAP(ARGB, RGB32, ARGB, 0),
169 MAP(XRGB, RGB32, 0RGB, 0),
170 #ifdef VA_FOURCC_X2R10G10B10
171 MAP(X2R10G10B10, RGB32_10, X2RGB10, 0),
173 #ifdef VA_FOURCC_Y410
176 MAP(Y410, YUV444_10, XV30, 0),
178 #ifdef VA_FOURCC_Y412
181 MAP(Y412, YUV444_12, XV36, 0),
186 static const VAAPIFormatDescriptor *
196 static const VAAPIFormatDescriptor *
208 const VAAPIFormatDescriptor *
desc;
211 return desc->pix_fmt;
218 VAImageFormat **image_format)
223 for (
i = 0;
i <
ctx->nb_formats;
i++) {
226 *image_format = &
ctx->formats[
i].image_format;
234 const void *hwconfig,
240 VASurfaceAttrib *attr_list =
NULL;
244 int err,
i, j, attr_count, pix_fmt_count;
249 vas = vaQuerySurfaceAttributes(hwctx->
display,
config->config_id,
251 if (vas != VA_STATUS_SUCCESS) {
253 "%d (%s).\n", vas, vaErrorStr(vas));
258 attr_list =
av_malloc(attr_count *
sizeof(*attr_list));
264 vas = vaQuerySurfaceAttributes(hwctx->
display,
config->config_id,
265 attr_list, &attr_count);
266 if (vas != VA_STATUS_SUCCESS) {
268 "%d (%s).\n", vas, vaErrorStr(vas));
274 for (
i = 0;
i < attr_count;
i++) {
275 switch (attr_list[
i].
type) {
276 case VASurfaceAttribPixelFormat:
277 fourcc = attr_list[
i].value.value.i;
285 case VASurfaceAttribMinWidth:
286 constraints->
min_width = attr_list[
i].value.value.i;
288 case VASurfaceAttribMinHeight:
289 constraints->
min_height = attr_list[
i].value.value.i;
291 case VASurfaceAttribMaxWidth:
292 constraints->
max_width = attr_list[
i].value.value.i;
294 case VASurfaceAttribMaxHeight:
295 constraints->
max_height = attr_list[
i].value.value.i;
299 if (pix_fmt_count == 0) {
311 for (
i = j = 0;
i < attr_count;
i++) {
314 if (attr_list[
i].
type != VASurfaceAttribPixelFormat)
316 fourcc = attr_list[
i].value.value.i;
322 for (k = 0; k < j; k++) {
341 for (
i = j = 0;
i <
ctx->nb_formats;
i++) {
344 for (k = 0; k < j; k++) {
370 static const struct {
375 #if !VA_CHECK_VERSION(1, 0, 0)
378 "Intel i965 (Quick Sync)",
390 "Splitted-Desktop Systems VDPAU backend for VA-API",
399 VAImageFormat *image_list =
NULL;
401 const char *vendor_string;
402 int err,
i, image_count;
406 image_count = vaMaxNumImageFormats(hwctx->
display);
407 if (image_count <= 0) {
411 image_list =
av_malloc(image_count *
sizeof(*image_list));
416 vas = vaQueryImageFormats(hwctx->
display, image_list, &image_count);
417 if (vas != VA_STATUS_SUCCESS) {
428 for (
i = 0;
i < image_count;
i++) {
438 ctx->formats[
ctx->nb_formats].image_format = image_list[
i];
443 vendor_string = vaQueryVendorString(hwctx->
display);
455 if (strstr(vendor_string,
458 "as known nonstandard driver \"%s\", setting "
469 "nonstandard list, using standard behaviour.\n");
473 "assuming standard behaviour.\n");
496 VASurfaceID surface_id;
499 surface_id = (VASurfaceID)(uintptr_t)
data;
501 vas = vaDestroySurfaces(hwctx->
display, &surface_id, 1);
502 if (vas != VA_STATUS_SUCCESS) {
504 "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
514 VASurfaceID surface_id;
522 vas = vaCreateSurfaces(hwctx->
display,
ctx->rt_format,
525 ctx->attributes,
ctx->nb_attributes);
526 if (vas != VA_STATUS_SUCCESS) {
528 "%d (%s).\n", vas, vaErrorStr(vas));
537 vaDestroySurfaces(hwctx->
display, &surface_id, 1);
557 const VAAPIFormatDescriptor *
desc;
558 VAImageFormat *expected_format;
560 VASurfaceID test_surface_id;
575 int need_pixel_format = 1;
577 if (avfc->
attributes[
i].type == VASurfaceAttribMemoryType)
578 need_memory_type = 0;
579 if (avfc->
attributes[
i].type == VASurfaceAttribPixelFormat)
580 need_pixel_format = 0;
586 sizeof(*
ctx->attributes));
587 if (!
ctx->attributes) {
594 if (need_memory_type) {
595 ctx->attributes[
i++] = (VASurfaceAttrib) {
596 .type = VASurfaceAttribMemoryType,
597 .
flags = VA_SURFACE_ATTRIB_SETTABLE,
598 .value.type = VAGenericValueTypeInteger,
599 .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA,
602 if (need_pixel_format) {
603 ctx->attributes[
i++] = (VASurfaceAttrib) {
604 .type = VASurfaceAttribPixelFormat,
605 .
flags = VA_SURFACE_ATTRIB_SETTABLE,
606 .value.type = VAGenericValueTypeInteger,
607 .value.value.i =
desc->fourcc,
613 ctx->nb_attributes = 0;
616 ctx->rt_format =
desc->rt_format;
652 "user-configured buffer pool.\n");
660 "internal buffer pool.\n");
665 test_surface_id = (VASurfaceID)(uintptr_t)test_surface->
data;
667 ctx->derive_works = 0;
672 vas = vaDeriveImage(hwctx->
display, test_surface_id, &test_image);
673 if (vas == VA_STATUS_SUCCESS) {
674 if (expected_format->fourcc == test_image.format.fourcc) {
676 ctx->derive_works = 1;
679 "derived image format %08x does not match "
680 "expected format %08x.\n",
681 expected_format->fourcc, test_image.format.fourcc);
683 vaDestroyImage(hwctx->
display, test_image.image_id);
686 "deriving image does not work: "
687 "%d (%s).\n", vas, vaErrorStr(vas));
691 "image format is not supported.\n");
733 int i, k, sw_format_available;
735 sw_format_available = 0;
736 for (
i = 0;
i <
ctx->nb_formats;
i++) {
738 sw_format_available = 1;
745 if (sw_format_available) {
751 for (
i = 0;
i <
ctx->nb_formats;
i++) {
768 VASurfaceID surface_id;
771 surface_id = (VASurfaceID)(uintptr_t)hwmap->
source->
data[3];
774 vas = vaUnmapBuffer(hwctx->
display,
map->image.buf);
775 if (vas != VA_STATUS_SUCCESS) {
777 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
782 vas = vaPutImage(hwctx->
display, surface_id,
map->image.image_id,
785 if (vas != VA_STATUS_SUCCESS) {
787 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
791 vas = vaDestroyImage(hwctx->
display,
map->image.image_id);
792 if (vas != VA_STATUS_SUCCESS) {
794 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
805 VASurfaceID surface_id;
806 const VAAPIFormatDescriptor *
desc;
807 VAImageFormat *image_format;
810 void *address =
NULL;
812 #if VA_CHECK_VERSION(1, 21, 0)
813 uint32_t vaflags = 0;
816 surface_id = (VASurfaceID)(uintptr_t)
src->data[3];
840 map->image.image_id = VA_INVALID_ID;
842 vas = vaSyncSurface(hwctx->
display, surface_id);
843 if (vas != VA_STATUS_SUCCESS) {
845 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
859 vas = vaDeriveImage(hwctx->
display, surface_id, &
map->image);
860 if (vas != VA_STATUS_SUCCESS) {
862 "surface %#x: %d (%s).\n",
863 surface_id, vas, vaErrorStr(vas));
867 if (
map->image.format.fourcc != image_format->fourcc) {
869 "is in wrong format: expected %#08x, got %#08x.\n",
870 surface_id, image_format->fourcc,
map->image.format.fourcc);
876 vas = vaCreateImage(hwctx->
display, image_format,
878 if (vas != VA_STATUS_SUCCESS) {
880 "surface %#x: %d (%s).\n",
881 surface_id, vas, vaErrorStr(vas));
886 vas = vaGetImage(hwctx->
display, surface_id, 0, 0,
888 if (vas != VA_STATUS_SUCCESS) {
890 "surface %#x: %d (%s).\n",
891 surface_id, vas, vaErrorStr(vas));
898 #if VA_CHECK_VERSION(1, 21, 0)
900 vaflags |= VA_MAPBUFFER_FLAG_READ;
902 vaflags |= VA_MAPBUFFER_FLAG_WRITE;
904 vas = vaMapBuffer2(hwctx->
display,
map->image.buf, &address, vaflags);
906 vas = vaMapBuffer(hwctx->
display,
map->image.buf, &address);
908 if (vas != VA_STATUS_SUCCESS) {
910 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
921 dst->height =
src->height;
923 for (
i = 0;
i <
map->image.num_planes;
i++) {
924 dst->data[
i] = (uint8_t*)address +
map->image.offsets[
i];
925 dst->linesize[
i] =
map->image.pitches[
i];
929 if (
desc &&
desc->chroma_planes_swapped) {
939 vaUnmapBuffer(hwctx->
display,
map->image.buf);
940 if (
map->image.image_id != VA_INVALID_ID)
941 vaDestroyImage(hwctx->
display,
map->image.image_id);
959 map->format =
dst->format;
966 map->height =
dst->height;
990 map->format =
src->format;
997 map->height =
src->height;
1033 #define DRM_MAP(va, layers, ...) { \
1038 static const struct {
1040 int nb_layer_formats;
1042 } vaapi_drm_format_map[] = {
1043 #ifdef DRM_FORMAT_R8
1044 DRM_MAP(NV12, 2, DRM_FORMAT_R8, DRM_FORMAT_RG88),
1045 DRM_MAP(NV12, 2, DRM_FORMAT_R8, DRM_FORMAT_GR88),
1047 DRM_MAP(NV12, 1, DRM_FORMAT_NV12),
1048 #if defined(VA_FOURCC_P010) && defined(DRM_FORMAT_R16)
1049 DRM_MAP(P010, 2, DRM_FORMAT_R16, DRM_FORMAT_RG1616),
1051 #if defined(VA_FOURCC_P012) && defined(DRM_FORMAT_R16)
1052 DRM_MAP(P012, 2, DRM_FORMAT_R16, DRM_FORMAT_RG1616),
1054 DRM_MAP(BGRA, 1, DRM_FORMAT_ARGB8888),
1055 DRM_MAP(BGRX, 1, DRM_FORMAT_XRGB8888),
1056 DRM_MAP(
RGBA, 1, DRM_FORMAT_ABGR8888),
1057 DRM_MAP(RGBX, 1, DRM_FORMAT_XBGR8888),
1058 #ifdef VA_FOURCC_ABGR
1059 DRM_MAP(ABGR, 1, DRM_FORMAT_RGBA8888),
1060 DRM_MAP(XBGR, 1, DRM_FORMAT_RGBX8888),
1062 DRM_MAP(ARGB, 1, DRM_FORMAT_BGRA8888),
1063 DRM_MAP(XRGB, 1, DRM_FORMAT_BGRX8888),
1064 #if defined(VA_FOURCC_XYUV) && defined(DRM_FORMAT_XYUV8888)
1065 DRM_MAP(XYUV, 1, DRM_FORMAT_XYUV8888),
1067 #if defined(VA_FOURCC_Y412) && defined(DRM_FORMAT_XVYU2101010)
1068 DRM_MAP(Y410, 1, DRM_FORMAT_XVYU2101010),
1070 #if defined(VA_FOURCC_Y412) && defined(DRM_FORMAT_XVYU12_16161616)
1071 DRM_MAP(Y412, 1, DRM_FORMAT_XVYU12_16161616),
1073 #if defined(VA_FOURCC_X2R10G10B10) && defined(DRM_FORMAT_XRGB2101010)
1074 DRM_MAP(X2R10G10B10, 1, DRM_FORMAT_XRGB2101010),
1084 VASurfaceID surface_id = (VASurfaceID)(uintptr_t)hwmap->
priv;
1088 vaDestroySurfaces(dst_dev->
display, &surface_id, 1);
1094 #if VA_CHECK_VERSION(1, 1, 0)
1104 const VAAPIFormatDescriptor *format_desc;
1105 VASurfaceID surface_id;
1106 VAStatus vas = VA_STATUS_SUCCESS;
1110 #
if !VA_CHECK_VERSION(1, 1, 0)
1111 unsigned long buffer_handle;
1112 VASurfaceAttribExternalBuffers buffer_desc;
1113 VASurfaceAttrib attrs[2] = {
1115 .type = VASurfaceAttribMemoryType,
1116 .flags = VA_SURFACE_ATTRIB_SETTABLE,
1117 .value.type = VAGenericValueTypeInteger,
1118 .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME,
1121 .type = VASurfaceAttribExternalBufferDescriptor,
1122 .flags = VA_SURFACE_ATTRIB_SETTABLE,
1123 .value.type = VAGenericValueTypePointer,
1124 .value.value.p = &buffer_desc,
1131 if (
desc->nb_objects != 1) {
1133 "made from a single DRM object.\n");
1139 if (
desc->nb_layers != vaapi_drm_format_map[
i].nb_layer_formats)
1141 for (j = 0; j <
desc->nb_layers; j++) {
1142 if (
desc->layers[j].format !=
1143 vaapi_drm_format_map[
i].layer_formats[j])
1146 if (j !=
desc->nb_layers)
1148 va_fourcc = vaapi_drm_format_map[
i].va_fourcc;
1158 "%08x.\n",
desc->objects[0].fd, va_fourcc);
1163 #if VA_CHECK_VERSION(1, 1, 0)
1167 VADRMPRIMESurfaceDescriptor prime_desc;
1168 VASurfaceAttrib prime_attrs[2] = {
1170 .type = VASurfaceAttribMemoryType,
1171 .flags = VA_SURFACE_ATTRIB_SETTABLE,
1172 .value.type = VAGenericValueTypeInteger,
1173 .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2,
1176 .type = VASurfaceAttribExternalBufferDescriptor,
1177 .flags = VA_SURFACE_ATTRIB_SETTABLE,
1178 .value.type = VAGenericValueTypePointer,
1179 .value.value.p = &prime_desc,
1182 prime_desc.fourcc = va_fourcc;
1183 prime_desc.width = src_fc->
width;
1184 prime_desc.height = src_fc->
height;
1185 prime_desc.num_objects =
desc->nb_objects;
1186 for (
i = 0;
i <
desc->nb_objects; ++
i) {
1187 prime_desc.objects[
i].fd =
desc->objects[
i].fd;
1188 prime_desc.objects[
i].size =
desc->objects[
i].size;
1189 prime_desc.objects[
i].drm_format_modifier =
1190 desc->objects[
i].format_modifier;
1193 prime_desc.num_layers =
desc->nb_layers;
1194 for (
i = 0;
i <
desc->nb_layers; ++
i) {
1195 prime_desc.layers[
i].drm_format =
desc->layers[
i].format;
1196 prime_desc.layers[
i].num_planes =
desc->layers[
i].nb_planes;
1197 for (j = 0; j <
desc->layers[
i].nb_planes; ++j) {
1198 prime_desc.layers[
i].object_index[j] =
1199 desc->layers[
i].planes[j].object_index;
1200 prime_desc.layers[
i].offset[j] =
desc->layers[
i].planes[j].offset;
1201 prime_desc.layers[
i].pitch[j] =
desc->layers[
i].planes[j].pitch;
1204 if (format_desc->chroma_planes_swapped &&
1205 desc->layers[
i].nb_planes == 3) {
1206 FFSWAP(uint32_t, prime_desc.layers[
i].pitch[1],
1207 prime_desc.layers[
i].pitch[2]);
1208 FFSWAP(uint32_t, prime_desc.layers[
i].offset[1],
1209 prime_desc.layers[
i].offset[2]);
1219 vas = vaCreateSurfaces(dst_dev->
display, format_desc->rt_format,
1220 src->width,
src->height, &surface_id, 1,
1222 if (vas != VA_STATUS_SUCCESS)
1226 if (!use_prime2 || vas != VA_STATUS_SUCCESS) {
1228 uintptr_t buffer_handle;
1229 VASurfaceAttribExternalBuffers buffer_desc;
1230 VASurfaceAttrib buffer_attrs[2] = {
1232 .type = VASurfaceAttribMemoryType,
1233 .flags = VA_SURFACE_ATTRIB_SETTABLE,
1234 .value.type = VAGenericValueTypeInteger,
1235 .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME,
1238 .type = VASurfaceAttribExternalBufferDescriptor,
1239 .flags = VA_SURFACE_ATTRIB_SETTABLE,
1240 .value.type = VAGenericValueTypePointer,
1241 .value.value.p = &buffer_desc,
1245 buffer_handle =
desc->objects[0].fd;
1246 buffer_desc.pixel_format = va_fourcc;
1247 buffer_desc.width = src_fc->
width;
1248 buffer_desc.height = src_fc->
height;
1249 buffer_desc.data_size =
desc->objects[0].size;
1250 buffer_desc.buffers = &buffer_handle;
1251 buffer_desc.num_buffers = 1;
1252 buffer_desc.flags = 0;
1255 for (
i = 0;
i <
desc->nb_layers;
i++) {
1256 for (j = 0; j <
desc->layers[
i].nb_planes; j++) {
1257 buffer_desc.pitches[k] =
desc->layers[
i].planes[j].pitch;
1258 buffer_desc.offsets[k] =
desc->layers[
i].planes[j].offset;
1262 buffer_desc.num_planes = k;
1264 if (format_desc->chroma_planes_swapped &&
1265 buffer_desc.num_planes == 3) {
1266 FFSWAP(uint32_t, buffer_desc.pitches[1], buffer_desc.pitches[2]);
1267 FFSWAP(uint32_t, buffer_desc.offsets[1], buffer_desc.offsets[2]);
1270 vas = vaCreateSurfaces(dst_dev->
display, format_desc->rt_format,
1276 buffer_handle =
desc->objects[0].fd;
1277 buffer_desc.pixel_format = va_fourcc;
1278 buffer_desc.width = src_fc->
width;
1279 buffer_desc.height = src_fc->
height;
1280 buffer_desc.data_size =
desc->objects[0].size;
1281 buffer_desc.buffers = &buffer_handle;
1282 buffer_desc.num_buffers = 1;
1283 buffer_desc.flags = 0;
1286 for (
i = 0;
i <
desc->nb_layers;
i++) {
1287 for (j = 0; j <
desc->layers[
i].nb_planes; j++) {
1288 buffer_desc.pitches[k] =
desc->layers[
i].planes[j].pitch;
1289 buffer_desc.offsets[k] =
desc->layers[
i].planes[j].offset;
1293 buffer_desc.num_planes = k;
1295 if (format_desc->chroma_planes_swapped &&
1296 buffer_desc.num_planes == 3) {
1297 FFSWAP(uint32_t, buffer_desc.pitches[1], buffer_desc.pitches[2]);
1298 FFSWAP(uint32_t, buffer_desc.offsets[1], buffer_desc.offsets[2]);
1301 vas = vaCreateSurfaces(dst_dev->
display, format_desc->rt_format,
1306 if (vas != VA_STATUS_SUCCESS) {
1308 "object: %d (%s).\n", vas, vaErrorStr(vas));
1314 &vaapi_unmap_from_drm,
1315 (
void*)(uintptr_t)surface_id);
1320 dst->height =
src->height;
1321 dst->data[3] = (uint8_t*)(uintptr_t)surface_id;
1324 "surface %#x.\n",
desc->objects[0].fd, surface_id);
1329 #if VA_CHECK_VERSION(1, 1, 0)
1346 VASurfaceID surface_id;
1348 VADRMPRIMESurfaceDescriptor va_desc;
1350 uint32_t export_flags;
1353 surface_id = (VASurfaceID)(uintptr_t)
src->data[3];
1355 export_flags = VA_EXPORT_SURFACE_SEPARATE_LAYERS;
1357 export_flags |= VA_EXPORT_SURFACE_READ_ONLY;
1359 vas = vaSyncSurface(hwctx->
display, surface_id);
1360 if (vas != VA_STATUS_SUCCESS) {
1362 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
1368 export_flags |= VA_EXPORT_SURFACE_WRITE_ONLY;
1370 vas = vaExportSurfaceHandle(hwctx->
display, surface_id,
1371 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2,
1372 export_flags, &va_desc);
1373 if (vas != VA_STATUS_SUCCESS) {
1374 if (vas == VA_STATUS_ERROR_UNIMPLEMENTED)
1377 "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
1389 for (
i = 0;
i < va_desc.num_objects;
i++) {
1393 va_desc.objects[
i].drm_format_modifier;
1395 drm_desc->
nb_layers = va_desc.num_layers;
1396 for (
i = 0;
i < va_desc.num_layers;
i++) {
1399 for (j = 0; j < va_desc.layers[
i].num_planes; j++) {
1401 va_desc.layers[
i].object_index[j];
1403 va_desc.layers[
i].offset[j];
1405 va_desc.layers[
i].pitch[j];
1410 &vaapi_unmap_to_drm_esh, drm_desc);
1415 dst->height =
src->height;
1416 dst->data[0] = (uint8_t*)drm_desc;
1421 for (
i = 0;
i < va_desc.num_objects;
i++)
1422 close(va_desc.objects[
i].fd);
1428 #if VA_CHECK_VERSION(0, 36, 0)
1429 typedef struct VAAPIDRMImageBufferMapping {
1431 VABufferInfo buffer_info;
1434 } VAAPIDRMImageBufferMapping;
1440 VAAPIDRMImageBufferMapping *mapping = hwmap->
priv;
1441 VASurfaceID surface_id;
1444 surface_id = (VASurfaceID)(uintptr_t)hwmap->
source->
data[3];
1451 vas = vaReleaseBufferHandle(hwctx->
display, mapping->image.buf);
1452 if (vas != VA_STATUS_SUCCESS) {
1454 "handle of image %#x (derived from surface %#x): "
1455 "%d (%s).\n", mapping->image.buf, surface_id,
1456 vas, vaErrorStr(vas));
1459 vas = vaDestroyImage(hwctx->
display, mapping->image.image_id);
1460 if (vas != VA_STATUS_SUCCESS) {
1462 "derived from surface %#x: %d (%s).\n",
1463 surface_id, vas, vaErrorStr(vas));
1473 VAAPIDRMImageBufferMapping *mapping =
NULL;
1474 VASurfaceID surface_id;
1478 surface_id = (VASurfaceID)(uintptr_t)
src->data[3];
1486 vas = vaDeriveImage(hwctx->
display, surface_id,
1488 if (vas != VA_STATUS_SUCCESS) {
1490 "surface %#x: %d (%s).\n",
1491 surface_id, vas, vaErrorStr(vas));
1497 if (vaapi_drm_format_map[
i].va_fourcc ==
1498 mapping->image.format.fourcc)
1503 "VAAPI format %#x.\n", mapping->image.format.fourcc);
1508 mapping->buffer_info.mem_type =
1509 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
1511 mapping->drm_desc.nb_layers =
1512 vaapi_drm_format_map[
i].nb_layer_formats;
1513 if (mapping->drm_desc.nb_layers > 1) {
1514 if (mapping->drm_desc.nb_layers != mapping->image.num_planes) {
1516 "expected format: got %d planes, but expected %d.\n",
1517 mapping->image.num_planes, mapping->drm_desc.nb_layers);
1522 for(p = 0; p < mapping->drm_desc.nb_layers; p++) {
1524 .format = vaapi_drm_format_map[
i].layer_formats[p],
1528 .offset = mapping->image.offsets[p],
1529 .pitch = mapping->image.pitches[p],
1534 mapping->drm_desc.layers[0].format =
1535 vaapi_drm_format_map[
i].layer_formats[0];
1536 mapping->drm_desc.layers[0].nb_planes = mapping->image.num_planes;
1537 for (p = 0; p < mapping->image.num_planes; p++) {
1540 .offset = mapping->image.offsets[p],
1541 .pitch = mapping->image.pitches[p],
1546 vas = vaAcquireBufferHandle(hwctx->
display, mapping->image.buf,
1547 &mapping->buffer_info);
1548 if (vas != VA_STATUS_SUCCESS) {
1550 "handle from image %#x (derived from surface %#x): "
1551 "%d (%s).\n", mapping->image.buf, surface_id,
1552 vas, vaErrorStr(vas));
1558 mapping->buffer_info.handle);
1560 mapping->drm_desc.nb_objects = 1;
1562 .fd = mapping->buffer_info.handle,
1563 .size = mapping->image.data_size,
1569 dst,
src, &vaapi_unmap_to_drm_abh,
1574 dst->data[0] = (uint8_t*)&mapping->drm_desc;
1576 dst->height =
src->height;
1581 vaReleaseBufferHandle(hwctx->
display, mapping->image.buf);
1583 vaDestroyImage(hwctx->
display, mapping->image.image_id);
1593 #if VA_CHECK_VERSION(1, 1, 0)
1595 err = vaapi_map_to_drm_esh(hwfc,
dst,
src,
flags);
1599 #if VA_CHECK_VERSION(0, 36, 0)
1600 return vaapi_map_to_drm_abh(hwfc,
dst,
src,
flags);
1610 switch (
src->format) {
1623 switch (
dst->format) {
1642 if (priv->x11_display)
1643 XCloseDisplay(priv->x11_display);
1653 static void vaapi_device_log_error(
void *
context,
const char *
message)
1660 static void vaapi_device_log_info(
void *
context,
const char *
message)
1676 vaSetErrorCallback(display, &vaapi_device_log_error,
ctx);
1677 vaSetInfoCallback (display, &vaapi_device_log_info,
ctx);
1682 vas = vaInitialize(display, &major, &minor);
1683 if (vas != VA_STATUS_SUCCESS) {
1685 "connection: %d (%s).\n", vas, vaErrorStr(vas));
1689 "version %d.%d\n", major, minor);
1698 VADisplay display =
NULL;
1700 int try_drm, try_x11, try_win32, try_all;
1708 ctx->user_opaque = priv;
1713 try_all = try_drm = try_x11 = try_win32 = 0;
1714 if (!strcmp(ent->
value,
"drm")) {
1716 }
else if (!strcmp(ent->
value,
"x11")) {
1718 }
else if (!strcmp(ent->
value,
"win32")) {
1727 try_drm = HAVE_VAAPI_DRM;
1728 try_x11 = HAVE_VAAPI_X11;
1729 try_win32 = HAVE_VAAPI_WIN32;
1733 while (!display && try_drm) {
1739 priv->
drm_fd = open(device, O_RDWR);
1741 av_log(
ctx, loglevel,
"Failed to open %s as "
1742 "DRM device node.\n", device);
1747 int n, max_devices = 8;
1755 for (n = 0; n < max_devices; n++) {
1757 "/dev/dri/renderD%d", 128 + n);
1758 priv->
drm_fd = open(path, O_RDWR);
1760 if (errno == ENOENT) {
1761 if (n != max_devices - 1) {
1763 "No render device %s, try next device for "
1764 "DRM render node.\n", path);
1769 "for DRM render node.\n");
1772 "DRM render node for device %d.\n", n);
1779 "Failed to get DRM version for device %d.\n", n);
1784 if (kernel_driver) {
1785 if (strcmp(kernel_driver->
value,
info->name)) {
1787 "with non-matching kernel driver (%s).\n",
1789 drmFreeVersion(
info);
1795 "DRM render node for device %d, "
1796 "with matching kernel driver (%s).\n",
1798 drmFreeVersion(
info);
1801 }
else if (!strcmp(
info->name,
"vgem")) {
1803 "Skipping vgem node for device %d.\n", n);
1804 drmFreeVersion(
info);
1808 }
else if (vendor_id) {
1809 drmDevicePtr device;
1811 if (drmGetDevice(priv->
drm_fd, &device)) {
1813 "Failed to get DRM device info for device %d.\n", n);
1819 snprintf(drm_vendor,
sizeof(drm_vendor),
"0x%x", device->deviceinfo.pci->vendor_id);
1820 if (strcmp(vendor_id->
value, drm_vendor)) {
1822 "with non-matching vendor id (%s).\n",
1823 n, vendor_id->
value);
1824 drmFreeDevice(&device);
1830 "DRM render node for device %d, "
1831 "with matching vendor id (%s).\n",
1832 n, vendor_id->
value);
1833 drmFreeDevice(&device);
1836 drmFreeVersion(
info);
1839 "DRM render node for device %d.\n", n);
1842 if (n >= max_devices)
1846 display = vaGetDisplayDRM(priv->
drm_fd);
1849 "from DRM device %s.\n", device);
1857 if (!display && try_x11) {
1859 priv->x11_display = XOpenDisplay(device);
1860 if (!priv->x11_display) {
1862 "%s.\n", XDisplayName(device));
1864 display = vaGetDisplay(priv->x11_display);
1867 "from X11 display %s.\n", XDisplayName(device));
1872 "X11 display %s.\n", XDisplayName(device));
1877 #if HAVE_VAAPI_WIN32
1878 if (!display && try_win32) {
1881 display = vaGetDisplayWin32(
NULL);
1883 IDXGIFactory2 *pDXGIFactory =
NULL;
1884 IDXGIAdapter *pAdapter =
NULL;
1886 HANDLE dxgi = dlopen(
"dxgi.dll", 0);
1893 if (!pfnCreateDXGIFactory) {
1904 if (SUCCEEDED(pfnCreateDXGIFactory(&IID_IDXGIFactory2,
1905 (
void **)&pDXGIFactory))) {
1906 int adapter = atoi(device);
1907 if (SUCCEEDED(IDXGIFactory2_EnumAdapters(pDXGIFactory,
1910 DXGI_ADAPTER_DESC
desc;
1911 if (SUCCEEDED(IDXGIAdapter2_GetDesc(pAdapter, &
desc))) {
1913 "Using device %04x:%04x (%ls) - LUID %lu %ld.\n",
1915 desc.AdapterLuid.LowPart,
1916 desc.AdapterLuid.HighPart);
1917 display = vaGetDisplayWin32(&
desc.AdapterLuid);
1919 IDXGIAdapter_Release(pAdapter);
1921 IDXGIFactory2_Release(pDXGIFactory);
1930 "from Win32 display.\n");
1935 "Win32 display.\n");
1942 "device %s.\n", device);
1945 "any default device.\n");
1951 #if VA_CHECK_VERSION(0, 38, 0)
1953 vas = vaSetDriverName(display, ent->
value);
1954 if (vas != VA_STATUS_SUCCESS) {
1956 "%s: %d (%s).\n", ent->
value, vas, vaErrorStr(vas));
1957 vaTerminate(display);
1962 "supported with this VAAPI version.\n");
1980 if (src_hwctx->
fd < 0) {
1982 "device to derive a VA display from.\n");
1988 int node_type = drmGetNodeTypeFromFd(src_hwctx->
fd);
1990 if (node_type < 0) {
1992 "to refer to a DRM device.\n");
1995 if (node_type == DRM_NODE_RENDER) {
1998 render_node = drmGetRenderDeviceNameFromFd(src_hwctx->
fd);
2001 "because the device does not have an "
2002 "associated render node.\n");
2005 fd = open(render_node, O_RDWR);
2008 "because the associated render node "
2009 "could not be opened.\n");
2013 "in place of non-render DRM device.\n",
2026 if (fd != src_hwctx->
fd) {
2033 if (fd == src_hwctx->
fd) {
2041 ctx->user_opaque = priv;
2044 display = vaGetDisplayDRM(fd);