forked from alvr-org/ALVR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg_helper.cpp
370 lines (330 loc) · 12.7 KB
/
ffmpeg_helper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "ffmpeg_helper.h"
#include <chrono>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include "alvr_server/bindings.h"
#include "alvr_server/Logger.h"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
#include <libavutil/avutil.h>
}
namespace {
// it seems that ffmpeg does not provide this mapping
AVPixelFormat vk_format_to_av_format(vk::Format vk_fmt)
{
for (int f = AV_PIX_FMT_NONE; f < AV_PIX_FMT_NB; ++f)
{
auto current_fmt = av_vkfmt_from_pixfmt(AVPixelFormat(f));
if (current_fmt and *current_fmt == (VkFormat)vk_fmt)
return AVPixelFormat(f);
}
throw std::runtime_error("unsupported vulkan pixel format " + std::to_string((VkFormat)vk_fmt));
}
}
std::string alvr::AvException::makemsg(const std::string & msg, int averror)
{
char av_msg[AV_ERROR_MAX_STRING_SIZE];
av_strerror(averror, av_msg, sizeof(av_msg));
return msg + " " + av_msg;
}
alvr::VkContext::VkContext(const char *deviceName, const std::vector<const char*> &requiredDeviceExtensions)
{
std::vector<const char*> instance_extensions = {
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
VK_KHR_SURFACE_EXTENSION_NAME,
};
std::vector<const char*> device_extensions = {
VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME,
VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME,
VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
VK_EXT_PHYSICAL_DEVICE_DRM_EXTENSION_NAME,
VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME,
};
device_extensions.insert(device_extensions.end(), requiredDeviceExtensions.begin(), requiredDeviceExtensions.end());
uint32_t instanceExtensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr);
std::vector<VkExtensionProperties> instanceExts(instanceExtensionCount);
vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, instanceExts.data());
for (const char *name : instance_extensions) {
auto it = std::find_if(instanceExts.begin(), instanceExts.end(), [name](VkExtensionProperties e) {
return strcmp(e.extensionName, name) == 0;
});
if (it != instanceExts.end()) {
instanceExtensions.push_back(name);
}
}
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "ALVR";
appInfo.apiVersion = VK_API_VERSION_1_2;
VkInstanceCreateInfo instanceInfo = {};
instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceInfo.pApplicationInfo = &appInfo;
#ifdef DEBUG
const char *validationLayers[] = { "VK_LAYER_KHRONOS_validation" };
instanceInfo.ppEnabledLayerNames = validationLayers;
instanceInfo.enabledLayerCount = 1;
#endif
instanceInfo.enabledExtensionCount = instanceExtensions.size();
instanceInfo.ppEnabledExtensionNames = instanceExtensions.data();
VK_CHECK(vkCreateInstance(&instanceInfo, nullptr, &instance));
uint32_t deviceCount = 0;
VK_CHECK(vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr));
std::vector<VkPhysicalDevice> physicalDevices(deviceCount);
VK_CHECK(vkEnumeratePhysicalDevices(instance, &deviceCount, physicalDevices.data()));
for (VkPhysicalDevice dev : physicalDevices) {
VkPhysicalDeviceProperties props;
vkGetPhysicalDeviceProperties(dev, &props);
if (strcmp(props.deviceName, deviceName) == 0) {
physicalDevice = dev;
break;
}
}
if (!physicalDevice && !physicalDevices.empty()) {
physicalDevice = physicalDevices[0];
}
if (!physicalDevice) {
throw std::runtime_error("Failed to find vulkan device.");
}
VkPhysicalDeviceDrmPropertiesEXT drmProps = {};
drmProps.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRM_PROPERTIES_EXT;
VkPhysicalDeviceProperties2 deviceProps = {};
deviceProps.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
deviceProps.pNext = &drmProps;
vkGetPhysicalDeviceProperties2(physicalDevice, &deviceProps);
nvidia = deviceProps.properties.vendorID == 0x10de;
drmContext = !nvidia;
Info("Using Vulkan device %s", deviceProps.properties.deviceName);
uint32_t deviceExtensionCount = 0;
VK_CHECK(vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &deviceExtensionCount, nullptr));
std::vector<VkExtensionProperties> deviceExts(deviceExtensionCount);
VK_CHECK(vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &deviceExtensionCount, deviceExts.data()));
for (const char *name : device_extensions) {
auto it = std::find_if(deviceExts.begin(), deviceExts.end(), [name](VkExtensionProperties e) {
return strcmp(e.extensionName, name) == 0;
});
if (it != deviceExts.end()) {
deviceExtensions.push_back(name);
}
}
float queuePriority = 1.0;
std::vector<VkDeviceQueueCreateInfo> queueInfos;
uint32_t queueFamilyCount;
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, nullptr);
std::vector<VkQueueFamilyProperties> queueFamilyProperties(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, queueFamilyProperties.data());
bool foundCompute = false;
for (uint32_t i = 0; i < queueFamilyProperties.size(); ++i) {
const bool graphics = queueFamilyProperties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT;
const bool compute = queueFamilyProperties[i].queueFlags & VK_QUEUE_COMPUTE_BIT;
if (graphics) {
queueFamilyIndex = i;
} else if (compute) {
foundCompute = true;
queueFamilyIndexCompute = i;
}
VkDeviceQueueCreateInfo queueInfo = {};
queueInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueInfo.queueFamilyIndex = i;
queueInfo.queueCount = 1;
queueInfo.pQueuePriorities = &queuePriority;
queueInfos.push_back(queueInfo);
}
if (!foundCompute) {
queueFamilyIndexCompute = queueFamilyIndex;
}
VkPhysicalDeviceVulkan12Features features12 = {};
features12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
features12.timelineSemaphore = true;
VkPhysicalDeviceFeatures2 features = {};
features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
features.pNext = &features12;
features.features.samplerAnisotropy = VK_TRUE;
VkDeviceCreateInfo deviceInfo = {};
deviceInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
deviceInfo.pNext = &features;
deviceInfo.queueCreateInfoCount = queueInfos.size();
deviceInfo.pQueueCreateInfos = queueInfos.data();
deviceInfo.enabledExtensionCount = deviceExtensions.size();
deviceInfo.ppEnabledExtensionNames = deviceExtensions.data();
VK_CHECK(vkCreateDevice(physicalDevice, &deviceInfo, nullptr, &device));
for (int i = 128; i < 136; ++i) {
auto path = "/dev/dri/renderD" + std::to_string(i);
int fd = open(path.c_str(), O_RDONLY);
if (fd == -1) {
continue;
}
struct stat s = {};
int ret = fstat(fd, &s);
close(fd);
if (ret != 0) {
continue;
}
dev_t primaryDev = makedev(drmProps.primaryMajor, drmProps.primaryMinor);
dev_t renderDev = makedev(drmProps.renderMajor, drmProps.renderMinor);
if (primaryDev == s.st_rdev || renderDev == s.st_rdev) {
devicePath = path;
break;
}
}
if (devicePath.empty()) {
devicePath = "/dev/dri/renderD128";
}
Info("Using device path %s", devicePath.c_str());
if (drmContext) {
ctx = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_DRM);
AVHWDeviceContext *hwctx = (AVHWDeviceContext *)ctx->data;
AVDRMDeviceContext *drmctx = (AVDRMDeviceContext*)hwctx->hwctx;
drmctx->fd = -1;
} else {
ctx = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VULKAN);
AVHWDeviceContext *hwctx = (AVHWDeviceContext *)ctx->data;
AVVulkanDeviceContext *vkctx = (AVVulkanDeviceContext *)hwctx->hwctx;
vkctx->alloc = nullptr;
vkctx->inst = instance;
vkctx->phys_dev = physicalDevice;
vkctx->act_dev = device;
vkctx->device_features = features;
vkctx->queue_family_index = queueFamilyIndex;
vkctx->nb_graphics_queues = 1;
vkctx->queue_family_tx_index = queueFamilyIndex;
vkctx->nb_tx_queues = 1;
vkctx->queue_family_comp_index = queueFamilyIndex;
vkctx->nb_comp_queues = 1;
#if LIBAVUTIL_VERSION_MAJOR >= 57
vkctx->get_proc_addr = vkGetInstanceProcAddr;
vkctx->queue_family_encode_index = -1;
vkctx->nb_encode_queues = 0;
vkctx->queue_family_decode_index = -1;
vkctx->nb_decode_queues = 0;
#endif
char **inst_extensions = (char**)malloc(sizeof(char*) * instanceExtensions.size());
for (uint32_t i = 0; i < instanceExtensions.size(); ++i) {
inst_extensions[i] = strdup(instanceExtensions[i]);
}
vkctx->enabled_inst_extensions = inst_extensions;
vkctx->nb_enabled_inst_extensions = instanceExtensions.size();
char **dev_extensions = (char**)malloc(sizeof(char*) * deviceExtensions.size());
for (uint32_t i = 0; i < deviceExtensions.size(); ++i) {
dev_extensions[i] = strdup(deviceExtensions[i]);
}
vkctx->enabled_dev_extensions = dev_extensions;
vkctx->nb_enabled_dev_extensions = deviceExtensions.size();
}
int ret = av_hwdevice_ctx_init(ctx);
if (ret)
throw AvException("failed to initialize ffmpeg", ret);
}
alvr::VkContext::~VkContext()
{
av_buffer_unref(&ctx);
vkDestroyDevice(device, nullptr);
vkDestroyInstance(instance, nullptr);
}
alvr::VkFrameCtx::VkFrameCtx(VkContext & vkContext, vk::ImageCreateInfo image_create_info)
{
AVHWFramesContext *frames_ctx = NULL;
int err = 0;
if (!(ctx = av_hwframe_ctx_alloc(vkContext.ctx))) {
throw std::runtime_error("Failed to create vulkan frame context.");
}
frames_ctx = (AVHWFramesContext *)(ctx->data);
frames_ctx->format = vkContext.drmContext ? AV_PIX_FMT_DRM_PRIME : AV_PIX_FMT_VULKAN;
frames_ctx->sw_format = vk_format_to_av_format(image_create_info.format);
frames_ctx->width = image_create_info.extent.width;
frames_ctx->height = image_create_info.extent.height;
frames_ctx->initial_pool_size = 0;
if ((err = av_hwframe_ctx_init(ctx)) < 0) {
av_buffer_unref(&ctx);
throw alvr::AvException("Failed to initialize vulkan frame context:", err);
}
}
alvr::VkFrameCtx::~VkFrameCtx()
{
av_buffer_unref(&ctx);
}
alvr::VkFrame::VkFrame(
const VkContext& vk_ctx,
VkImage image,
VkImageCreateInfo image_info,
VkDeviceSize size,
VkDeviceMemory memory,
DrmImage drm
):
width(image_info.extent.width),
height(image_info.extent.height),
vkimage(image),
vkimageinfo(image_info)
{
device = vk_ctx.get_vk_device();
if (vk_ctx.drmContext) {
av_drmframe = (AVDRMFrameDescriptor*)malloc(sizeof(AVDRMFrameDescriptor));
av_drmframe->nb_objects = 1;
av_drmframe->objects[0].fd = drm.fd;
av_drmframe->objects[0].size = size;
av_drmframe->objects[0].format_modifier = drm.modifier;
av_drmframe->nb_layers = 1;
av_drmframe->layers[0].format = drm.format;
av_drmframe->layers[0].nb_planes = drm.planes;
for (uint32_t i = 0; i < drm.planes; ++i) {
av_drmframe->layers[0].planes[i].object_index = 0;
av_drmframe->layers[0].planes[i].pitch = drm.strides[i];
av_drmframe->layers[0].planes[i].offset = drm.offsets[i];
}
} else {
av_vkframe = av_vk_frame_alloc();
av_vkframe->img[0] = image;
av_vkframe->tiling = image_info.tiling;
av_vkframe->mem[0] = memory;
av_vkframe->size[0] = size;
av_vkframe->layout[0] = VK_IMAGE_LAYOUT_UNDEFINED;
VkSemaphoreTypeCreateInfo timelineInfo = {};
timelineInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO;
timelineInfo.semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE;
VkSemaphoreCreateInfo semInfo = {};
semInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
#if LIBAVUTIL_VERSION_MAJOR >= 57
semInfo.pNext = &timelineInfo;
#endif
vkCreateSemaphore(device, &semInfo, nullptr, &av_vkframe->sem[0]);
}
}
alvr::VkFrame::~VkFrame()
{
if (av_drmframe) {
av_free(av_drmframe);
}
if (av_vkframe) {
vkDestroySemaphore(device, av_vkframe->sem[0], nullptr);
av_free(av_vkframe);
}
}
std::unique_ptr<AVFrame, std::function<void(AVFrame*)>> alvr::VkFrame::make_av_frame(VkFrameCtx &frame_ctx)
{
std::unique_ptr<AVFrame, std::function<void(AVFrame*)>> frame{
av_frame_alloc(),
[](AVFrame *p) {av_frame_free(&p);}
};
frame->width = width;
frame->height = height;
frame->hw_frames_ctx = av_buffer_ref(frame_ctx.ctx);
if (av_drmframe) {
frame->data[0] = (uint8_t*)av_drmframe;
frame->format = AV_PIX_FMT_DRM_PRIME;
}
if (av_vkframe) {
frame->data[0] = (uint8_t*)av_vkframe;
frame->format = AV_PIX_FMT_VULKAN;
}
frame->buf[0] = av_buffer_alloc(1);
frame->pts = std::chrono::steady_clock::now().time_since_epoch().count();
return frame;
}