move stuff in common, clang-format, update copyright
This commit is contained in:
@@ -1,53 +1,73 @@
|
||||
// Copyright 2022, Guillaume Meunier
|
||||
// Copyright 2022, Patrick Nicolas
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "VideoEncoderFFMPEG.h"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <stdexcept>
|
||||
|
||||
extern "C" {
|
||||
extern "C"
|
||||
{
|
||||
#include <libavcodec/avcodec.h>
|
||||
}
|
||||
|
||||
namespace {
|
||||
namespace
|
||||
{
|
||||
|
||||
bool
|
||||
should_keep_nal_h264(const uint8_t *header_start)
|
||||
bool should_keep_nal_h264(const uint8_t * header_start)
|
||||
{
|
||||
uint8_t nal_type = (header_start[2] == 0 ? header_start[4] : header_start[3]) & 0x1F;
|
||||
switch (nal_type) {
|
||||
case 6: // supplemental enhancement information
|
||||
case 9: // access unit delimiter
|
||||
return false;
|
||||
default: return true;
|
||||
switch (nal_type)
|
||||
{
|
||||
case 6: // supplemental enhancement information
|
||||
case 9: // access unit delimiter
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
should_keep_nal_h265(const uint8_t *header_start)
|
||||
bool should_keep_nal_h265(const uint8_t * header_start)
|
||||
{
|
||||
uint8_t nal_type = ((header_start[2] == 0 ? header_start[4] : header_start[3]) >> 1) & 0x3F;
|
||||
switch (nal_type) {
|
||||
case 35: // access unit delimiter
|
||||
case 39: // supplemental enhancement information
|
||||
return false;
|
||||
default: return true;
|
||||
switch (nal_type)
|
||||
{
|
||||
case 35: // access unit delimiter
|
||||
case 39: // supplemental enhancement information
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
filter_NAL(const uint8_t *input, size_t input_size, std::vector<uint8_t> &out, VideoEncoderFFMPEG::Codec codec)
|
||||
void filter_NAL(const uint8_t * input, size_t input_size, std::vector<uint8_t> & out, VideoEncoderFFMPEG::Codec codec)
|
||||
{
|
||||
if (input_size < 4)
|
||||
return;
|
||||
std::array<uint8_t, 3> header = {{0, 0, 1}};
|
||||
auto end = input + input_size;
|
||||
auto header_start = input;
|
||||
while (header_start != end) {
|
||||
while (header_start != end)
|
||||
{
|
||||
auto next_header = std::search(header_start + 3, end, header.begin(), header.end());
|
||||
if (next_header != end and next_header[-1] == 0) {
|
||||
if (next_header != end and next_header[-1] == 0)
|
||||
{
|
||||
next_header--;
|
||||
}
|
||||
if (codec == VideoEncoderFFMPEG::Codec::h264 and should_keep_nal_h264(header_start))
|
||||
@@ -60,21 +80,22 @@ filter_NAL(const uint8_t *input, size_t input_size, std::vector<uint8_t> &out, V
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
void
|
||||
VideoEncoderFFMPEG::Encode(int index, bool idr, std::chrono::steady_clock::time_point target_timestamp)
|
||||
void VideoEncoderFFMPEG::Encode(int index, bool idr, std::chrono::steady_clock::time_point target_timestamp)
|
||||
{
|
||||
PushFrame(index, idr, target_timestamp);
|
||||
AVPacket *enc_pkt = av_packet_alloc();
|
||||
AVPacket * enc_pkt = av_packet_alloc();
|
||||
int err = avcodec_receive_packet(encoder_ctx.get(), enc_pkt);
|
||||
if (err == 0) {
|
||||
if (err == 0)
|
||||
{
|
||||
SendData({enc_pkt->data, enc_pkt->data + enc_pkt->size});
|
||||
av_packet_free(&enc_pkt);
|
||||
}
|
||||
if (err == AVERROR(EAGAIN)) {
|
||||
if (err == AVERROR(EAGAIN))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (err) {
|
||||
if (err)
|
||||
{
|
||||
throw std::runtime_error("frame encoding failed, code " + std::to_string(err));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,29 @@
|
||||
// Copyright 2022, Guillaume Meunier
|
||||
// Copyright 2022, Patrick Nicolas
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include "ffmpeg_helper.h"
|
||||
#include "../video_encoder.h"
|
||||
#include "ffmpeg_helper.h"
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
class VideoEncoderFFMPEG : public VideoEncoder
|
||||
{
|
||||
|
||||
@@ -1,15 +1,31 @@
|
||||
// Copyright 2022, Guillaume Meunier
|
||||
// Copyright 2022, Patrick Nicolas
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "VideoEncoderVA.h"
|
||||
#include "ffmpeg_helper.h"
|
||||
#include "vk/vk_helpers.h"
|
||||
#include <cassert>
|
||||
#include <stdexcept>
|
||||
#include <chrono>
|
||||
#include <stdexcept>
|
||||
|
||||
extern "C" {
|
||||
extern "C"
|
||||
{
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavfilter/avfilter.h>
|
||||
#include <libavfilter/buffersink.h>
|
||||
@@ -20,25 +36,29 @@ extern "C" {
|
||||
#include <libavutil/opt.h>
|
||||
}
|
||||
|
||||
namespace {
|
||||
namespace
|
||||
{
|
||||
|
||||
const char *
|
||||
encoder(VideoEncoderFFMPEG::Codec codec)
|
||||
{
|
||||
switch (codec) {
|
||||
case VideoEncoderFFMPEG::Codec::h264: return "h264_vaapi";
|
||||
case VideoEncoderFFMPEG::Codec::h265: return "hevc_vaapi";
|
||||
switch (codec)
|
||||
{
|
||||
case VideoEncoderFFMPEG::Codec::h264:
|
||||
return "h264_vaapi";
|
||||
case VideoEncoderFFMPEG::Codec::h265:
|
||||
return "hevc_vaapi";
|
||||
}
|
||||
throw std::runtime_error("invalid codec " + std::to_string(int(codec)));
|
||||
}
|
||||
|
||||
void
|
||||
set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
|
||||
void set_hwframe_ctx(AVCodecContext * ctx, AVBufferRef * hw_device_ctx)
|
||||
{
|
||||
av_buffer_ptr hw_frames_ref(av_hwframe_ctx_alloc(hw_device_ctx));
|
||||
int err = 0;
|
||||
|
||||
if (not hw_frames_ref) {
|
||||
if (not hw_frames_ref)
|
||||
{
|
||||
throw std::runtime_error("Failed to create VAAPI frame context.");
|
||||
}
|
||||
auto frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data);
|
||||
@@ -47,7 +67,8 @@ set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
|
||||
frames_ctx->width = ctx->width;
|
||||
frames_ctx->height = ctx->height;
|
||||
frames_ctx->initial_pool_size = 3;
|
||||
if ((err = av_hwframe_ctx_init(hw_frames_ref.get())) < 0) {
|
||||
if ((err = av_hwframe_ctx_init(hw_frames_ref.get())) < 0)
|
||||
{
|
||||
throw std::system_error(err, av_error_category(), "Failed to initialize VAAPI frame context");
|
||||
}
|
||||
ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref.get());
|
||||
@@ -56,7 +77,7 @@ set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
|
||||
}
|
||||
|
||||
std::string
|
||||
get_render_device(vk_bundle *vk)
|
||||
get_render_device(vk_bundle * vk)
|
||||
{
|
||||
VkPhysicalDeviceDrmPropertiesEXT drmProps{};
|
||||
drmProps.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRM_PROPERTIES_EXT;
|
||||
@@ -67,7 +88,8 @@ get_render_device(vk_bundle *vk)
|
||||
|
||||
vk->vkGetPhysicalDeviceProperties2(vk->physical_device, &props);
|
||||
|
||||
if (!drmProps.hasRender) {
|
||||
if (!drmProps.hasRender)
|
||||
{
|
||||
U_LOG_E("Failed to find render DRM device");
|
||||
throw std::runtime_error("Failed to find render DRM device");
|
||||
}
|
||||
@@ -76,8 +98,8 @@ get_render_device(vk_bundle *vk)
|
||||
|
||||
} // namespace
|
||||
|
||||
VideoEncoderVA::VideoEncoderVA(vk_bundle *vk, const encoder_settings &settings, float fps)
|
||||
: vk(vk), width(settings.width), height(settings.height), offset_x(settings.offset_x), offset_y(settings.offset_y)
|
||||
VideoEncoderVA::VideoEncoderVA(vk_bundle * vk, const encoder_settings & settings, float fps) :
|
||||
vk(vk), width(settings.width), height(settings.height), offset_x(settings.offset_x), offset_y(settings.offset_y)
|
||||
{
|
||||
codec = settings.codec;
|
||||
/* VAAPI Encoding pipeline
|
||||
@@ -91,32 +113,41 @@ VideoEncoderVA::VideoEncoderVA(vk_bundle *vk, const encoder_settings &settings,
|
||||
* and the encoder that takes the converted frame and produces packets.
|
||||
*/
|
||||
|
||||
AVBufferRef *tmp;
|
||||
AVBufferRef * tmp;
|
||||
const std::string device = get_render_device(vk);
|
||||
int err = av_hwdevice_ctx_create(&tmp, AV_HWDEVICE_TYPE_VAAPI, device.c_str(), NULL, 0);
|
||||
if (err < 0) {
|
||||
if (err < 0)
|
||||
{
|
||||
throw std::system_error(err, av_error_category(), "Failed create a VAAPI device");
|
||||
}
|
||||
hw_ctx_vaapi.reset(tmp);
|
||||
|
||||
const char *encoder_name = encoder(codec);
|
||||
const AVCodec *codec = avcodec_find_encoder_by_name(encoder_name);
|
||||
if (codec == nullptr) {
|
||||
const char * encoder_name = encoder(codec);
|
||||
const AVCodec * codec = avcodec_find_encoder_by_name(encoder_name);
|
||||
if (codec == nullptr)
|
||||
{
|
||||
throw std::runtime_error(std::string("Failed to find encoder ") + encoder_name);
|
||||
}
|
||||
|
||||
encoder_ctx.reset(avcodec_alloc_context3(codec));
|
||||
if (not encoder_ctx) {
|
||||
if (not encoder_ctx)
|
||||
{
|
||||
throw std::runtime_error("failed to allocate VAAPI encoder");
|
||||
}
|
||||
|
||||
AVDictionary *opts = nullptr;
|
||||
for (auto option : settings.options) {
|
||||
AVDictionary * opts = nullptr;
|
||||
for (auto option: settings.options)
|
||||
{
|
||||
av_dict_set(&opts, option.first.c_str(), option.second.c_str(), 0);
|
||||
}
|
||||
switch (this->codec) {
|
||||
case Codec::h264: encoder_ctx->profile = FF_PROFILE_H264_MAIN; break;
|
||||
case Codec::h265: encoder_ctx->profile = FF_PROFILE_HEVC_MAIN; break;
|
||||
switch (this->codec)
|
||||
{
|
||||
case Codec::h264:
|
||||
encoder_ctx->profile = FF_PROFILE_H264_MAIN;
|
||||
break;
|
||||
case Codec::h265:
|
||||
encoder_ctx->profile = FF_PROFILE_HEVC_MAIN;
|
||||
break;
|
||||
}
|
||||
|
||||
encoder_ctx->width = width;
|
||||
@@ -133,56 +164,66 @@ VideoEncoderVA::VideoEncoderVA(vk_bundle *vk, const encoder_settings &settings,
|
||||
|
||||
err = avcodec_open2(encoder_ctx.get(), codec, &opts);
|
||||
av_dict_free(&opts);
|
||||
if (err < 0) {
|
||||
if (err < 0)
|
||||
{
|
||||
throw std::system_error(err, av_error_category(), "Cannot open video encoder codec");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
VideoEncoderVA::PushFrame(uint32_t frame_index, bool idr, std::chrono::steady_clock::time_point pts)
|
||||
void VideoEncoderVA::PushFrame(uint32_t frame_index, bool idr, std::chrono::steady_clock::time_point pts)
|
||||
{
|
||||
assert(frame_index < mapped_frames.size());
|
||||
av_frame_ptr encoder_frame = make_av_frame();
|
||||
int err = av_buffersrc_add_frame_flags(filter_in, mapped_frames[frame_index].get(),
|
||||
AV_BUFFERSRC_FLAG_PUSH | AV_BUFFERSRC_FLAG_KEEP_REF);
|
||||
if (err != 0) {
|
||||
int err = av_buffersrc_add_frame_flags(filter_in, mapped_frames[frame_index].get(), AV_BUFFERSRC_FLAG_PUSH | AV_BUFFERSRC_FLAG_KEEP_REF);
|
||||
if (err != 0)
|
||||
{
|
||||
throw std::system_error(err, av_error_category(), "av_buffersrc_add_frame failed");
|
||||
}
|
||||
err = av_buffersink_get_frame(filter_out, encoder_frame.get());
|
||||
if (err != 0) {
|
||||
if (err != 0)
|
||||
{
|
||||
throw std::system_error(err, av_error_category(), "av_buffersink_get_frame failed");
|
||||
}
|
||||
|
||||
encoder_frame->pict_type = idr ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_NONE;
|
||||
encoder_frame->pts = pts.time_since_epoch().count();
|
||||
err = avcodec_send_frame(encoder_ctx.get(), encoder_frame.get());
|
||||
if (err) {
|
||||
if (err)
|
||||
{
|
||||
throw std::system_error(err, av_error_category(), "avcodec_send_frame failed");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
VideoEncoderVA::SetImages(
|
||||
int width, int height, VkFormat format, int num_images, VkImage *images, VkImageView *views, VkDeviceMemory *memory)
|
||||
void VideoEncoderVA::SetImages(
|
||||
int width,
|
||||
int height,
|
||||
VkFormat format,
|
||||
int num_images,
|
||||
VkImage * images,
|
||||
VkImageView * views,
|
||||
VkDeviceMemory * memory)
|
||||
{
|
||||
mapped_frames.clear();
|
||||
av_buffer_ptr va_ctx(av_hwframe_ctx_alloc(hw_ctx_vaapi.get()));
|
||||
if (!va_ctx) {
|
||||
if (!va_ctx)
|
||||
{
|
||||
throw std::runtime_error("Failed to create VAAPI frame context.");
|
||||
}
|
||||
AVHWFramesContext *va_frames_ctx = (AVHWFramesContext *)(va_ctx->data);
|
||||
AVHWFramesContext * va_frames_ctx = (AVHWFramesContext *)(va_ctx->data);
|
||||
va_frames_ctx->format = AV_PIX_FMT_VAAPI;
|
||||
va_frames_ctx->sw_format = vk_format_to_av_format(format);
|
||||
va_frames_ctx->width = width;
|
||||
va_frames_ctx->height = height;
|
||||
va_frames_ctx->initial_pool_size = num_images;
|
||||
int err = av_hwframe_ctx_init(va_ctx.get());
|
||||
if (err) {
|
||||
if (err)
|
||||
{
|
||||
throw std::system_error(err, av_error_category(), "Failed to create VAAPI frame context");
|
||||
}
|
||||
|
||||
mapped_frames.reserve(num_images);
|
||||
for (int i = 0; i < num_images; ++i) {
|
||||
for (int i = 0; i < num_images; ++i)
|
||||
{
|
||||
VkMemoryRequirements req;
|
||||
vk->vkGetImageMemoryRequirements(vk->device, images[i], &req);
|
||||
|
||||
@@ -199,7 +240,7 @@ VideoEncoderVA::SetImages(
|
||||
drm_frame->height = height;
|
||||
drm_frame->buf[0] = av_buffer_alloc(sizeof(AVDRMFrameDescriptor));
|
||||
drm_frame->data[0] = drm_frame->buf[0]->data;
|
||||
auto &drm_frame_desc = *(AVDRMFrameDescriptor *)drm_frame->data[0];
|
||||
auto & drm_frame_desc = *(AVDRMFrameDescriptor *)drm_frame->data[0];
|
||||
drm_frame_desc.nb_objects = 1;
|
||||
drm_frame_desc.objects[0].fd = fd;
|
||||
drm_frame_desc.objects[0].size = req.size;
|
||||
@@ -210,7 +251,8 @@ VideoEncoderVA::SetImages(
|
||||
|
||||
// DRM format modifiers seem to be broken on ffmpeg or radeon, or both
|
||||
bool drm_tiling = use_drm_format_modifiers and vk->has_EXT_image_drm_format_modifier;
|
||||
if (drm_tiling) {
|
||||
if (drm_tiling)
|
||||
{
|
||||
VkImageDrmFormatModifierPropertiesEXT modifiers{};
|
||||
modifiers.sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT;
|
||||
vk->vkGetImageDrmFormatModifierPropertiesEXT(vk->device, images[i], &modifiers);
|
||||
@@ -218,9 +260,10 @@ VideoEncoderVA::SetImages(
|
||||
}
|
||||
// if we don't have modifiers, we did linear tiling
|
||||
VkImageSubresource subresource{(use_drm_format_modifiers and vk->has_EXT_image_drm_format_modifier)
|
||||
? VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT
|
||||
: VK_IMAGE_ASPECT_COLOR_BIT,
|
||||
0, 0};
|
||||
? VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT
|
||||
: VK_IMAGE_ASPECT_COLOR_BIT,
|
||||
0,
|
||||
0};
|
||||
VkSubresourceLayout layout;
|
||||
vk->vkGetImageSubresourceLayout(vk->device, images[i], &subresource, &layout);
|
||||
drm_frame_desc.layers[0].planes[0].offset = layout.offset;
|
||||
@@ -243,19 +286,18 @@ VideoEncoderVA::SetImages(
|
||||
InitFilterGraph();
|
||||
}
|
||||
|
||||
void
|
||||
VideoEncoderVA::InitFilterGraph()
|
||||
void VideoEncoderVA::InitFilterGraph()
|
||||
{
|
||||
assert(not mapped_frames.empty());
|
||||
|
||||
filter_graph.reset(avfilter_graph_alloc());
|
||||
|
||||
AVFilterInOut *outputs = avfilter_inout_alloc();
|
||||
AVFilterInOut *inputs = avfilter_inout_alloc();
|
||||
AVFilterInOut * outputs = avfilter_inout_alloc();
|
||||
AVFilterInOut * inputs = avfilter_inout_alloc();
|
||||
|
||||
filter_in = avfilter_graph_alloc_filter(filter_graph.get(), avfilter_get_by_name("buffer"), "in");
|
||||
|
||||
AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
|
||||
AVBufferSrcParameters * par = av_buffersrc_parameters_alloc();
|
||||
memset(par, 0, sizeof(*par));
|
||||
par->width = mapped_frames[0]->width;
|
||||
par->height = mapped_frames[0]->height;
|
||||
@@ -266,8 +308,8 @@ VideoEncoderVA::InitFilterGraph()
|
||||
av_free(par);
|
||||
|
||||
int err;
|
||||
if ((err = avfilter_graph_create_filter(&filter_out, avfilter_get_by_name("buffersink"), "out", NULL, NULL,
|
||||
filter_graph.get()))) {
|
||||
if ((err = avfilter_graph_create_filter(&filter_out, avfilter_get_by_name("buffersink"), "out", NULL, NULL, filter_graph.get())))
|
||||
{
|
||||
throw std::system_error(err, av_error_category(), "filter_out creation failed");
|
||||
}
|
||||
|
||||
@@ -286,11 +328,13 @@ VideoEncoderVA::InitFilterGraph()
|
||||
err = avfilter_graph_parse_ptr(filter_graph.get(), scale_param.c_str(), &inputs, &outputs, NULL);
|
||||
avfilter_inout_free(&outputs);
|
||||
avfilter_inout_free(&inputs);
|
||||
if (err < 0) {
|
||||
if (err < 0)
|
||||
{
|
||||
throw std::system_error(err, av_error_category(), "avfilter_graph_parse_ptr failed");
|
||||
}
|
||||
|
||||
if ((err = avfilter_graph_config(filter_graph.get(), NULL))) {
|
||||
if ((err = avfilter_graph_config(filter_graph.get(), NULL)))
|
||||
{
|
||||
throw std::system_error(err, av_error_category(), "avfilter_graph_config failed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,30 @@
|
||||
// Copyright 2022, Guillaume Meunier
|
||||
// Copyright 2022, Patrick Nicolas
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "VideoEncoderFFMPEG.h"
|
||||
#include "vk/vk_helpers.h"
|
||||
#include "ffmpeg_helper.h"
|
||||
#include "vk/vk_helpers.h"
|
||||
|
||||
extern "C" {
|
||||
extern "C"
|
||||
{
|
||||
#include <libavutil/version.h>
|
||||
}
|
||||
|
||||
@@ -20,16 +36,16 @@ struct AVFilterContext;
|
||||
class VideoEncoderVA : public VideoEncoderFFMPEG
|
||||
{
|
||||
public:
|
||||
VideoEncoderVA(vk_bundle *vk, const encoder_settings &settings, float fps);
|
||||
VideoEncoderVA(vk_bundle * vk, const encoder_settings & settings, float fps);
|
||||
|
||||
void
|
||||
SetImages(int width,
|
||||
int height,
|
||||
VkFormat format,
|
||||
int num_images,
|
||||
VkImage *images,
|
||||
VkImageView *views,
|
||||
VkDeviceMemory *memory) override;
|
||||
VkImage * images,
|
||||
VkImageView * views,
|
||||
VkDeviceMemory * memory) override;
|
||||
|
||||
void
|
||||
PushFrame(uint32_t frame_index, bool idr, std::chrono::steady_clock::time_point pts) override;
|
||||
@@ -38,7 +54,7 @@ private:
|
||||
void
|
||||
InitFilterGraph();
|
||||
|
||||
vk_bundle *vk;
|
||||
vk_bundle * vk;
|
||||
int width;
|
||||
int height;
|
||||
// relevant part of the input image to encode
|
||||
@@ -47,6 +63,6 @@ private:
|
||||
av_buffer_ptr hw_ctx_vaapi;
|
||||
std::vector<av_frame_ptr> mapped_frames;
|
||||
av_filter_graph_ptr filter_graph;
|
||||
AVFilterContext *filter_in = nullptr;
|
||||
AVFilterContext *filter_out = nullptr;
|
||||
AVFilterContext * filter_in = nullptr;
|
||||
AVFilterContext * filter_out = nullptr;
|
||||
};
|
||||
|
||||
@@ -1,11 +1,27 @@
|
||||
// Copyright 2022, Guillaume Meunier
|
||||
// Copyright 2022, Patrick Nicolas
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ffmpeg_helper.h"
|
||||
#include <libdrm/drm_fourcc.h>
|
||||
|
||||
extern "C" {
|
||||
extern "C"
|
||||
{
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavfilter/avfilter.h>
|
||||
#include <libavutil/hwcontext.h>
|
||||
@@ -13,7 +29,8 @@ extern "C" {
|
||||
#include <libavutil/opt.h>
|
||||
}
|
||||
|
||||
namespace {
|
||||
namespace
|
||||
{
|
||||
struct : public std::error_category
|
||||
{
|
||||
const char *
|
||||
@@ -41,11 +58,15 @@ av_error_category()
|
||||
AVPixelFormat
|
||||
vk_format_to_av_format(VkFormat vk_fmt)
|
||||
{
|
||||
switch (vk_fmt) {
|
||||
case VK_FORMAT_B8G8R8A8_SRGB: return AV_PIX_FMT_BGRA;
|
||||
default: break;
|
||||
switch (vk_fmt)
|
||||
{
|
||||
case VK_FORMAT_B8G8R8A8_SRGB:
|
||||
return AV_PIX_FMT_BGRA;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
for (int f = AV_PIX_FMT_NONE; f < AV_PIX_FMT_NB; ++f) {
|
||||
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);
|
||||
@@ -56,40 +77,40 @@ vk_format_to_av_format(VkFormat vk_fmt)
|
||||
uint32_t
|
||||
vk_format_to_fourcc(VkFormat vk_fmt)
|
||||
{
|
||||
switch (vk_fmt) {
|
||||
case VK_FORMAT_B8G8R8A8_SRGB: return DRM_FORMAT_ARGB8888;
|
||||
case VK_FORMAT_B8G8R8A8_UNORM: return DRM_FORMAT_ARGB8888;
|
||||
default: break;
|
||||
switch (vk_fmt)
|
||||
{
|
||||
case VK_FORMAT_B8G8R8A8_SRGB:
|
||||
return DRM_FORMAT_ARGB8888;
|
||||
case VK_FORMAT_B8G8R8A8_UNORM:
|
||||
return DRM_FORMAT_ARGB8888;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
throw std::runtime_error("unsupported vulkan pixel format " + std::to_string((VkFormat)vk_fmt));
|
||||
}
|
||||
|
||||
void
|
||||
AvDeleter::operator()(AVBufferRef *x)
|
||||
void AvDeleter::operator()(AVBufferRef * x)
|
||||
{
|
||||
av_buffer_unref(&x);
|
||||
}
|
||||
|
||||
void
|
||||
AvDeleter::operator()(AVFrame *x)
|
||||
void AvDeleter::operator()(AVFrame * x)
|
||||
{
|
||||
av_frame_unref(x);
|
||||
}
|
||||
|
||||
void
|
||||
AvDeleter::operator()(AVCodecContext *x)
|
||||
void AvDeleter::operator()(AVCodecContext * x)
|
||||
{
|
||||
avcodec_free_context(&x);
|
||||
}
|
||||
|
||||
void
|
||||
AvDeleter::operator()(AVFilterGraph *x)
|
||||
void AvDeleter::operator()(AVFilterGraph * x)
|
||||
{
|
||||
avfilter_graph_free(&x);
|
||||
}
|
||||
|
||||
av_frame_ptr
|
||||
make_av_frame(AVFrame *frame)
|
||||
make_av_frame(AVFrame * frame)
|
||||
{
|
||||
return av_frame_ptr(frame);
|
||||
}
|
||||
|
||||
@@ -1,21 +1,37 @@
|
||||
// Copyright 2022, Guillaume Meunier
|
||||
// Copyright 2022, Patrick Nicolas
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "vk/vk_helpers.h"
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <system_error>
|
||||
#include <vector>
|
||||
#include "vk/vk_helpers.h"
|
||||
|
||||
struct AVBufferRef;
|
||||
struct AVFilterGraph;
|
||||
struct AVFrame;
|
||||
struct AVCodecContext;
|
||||
|
||||
extern "C" {
|
||||
extern "C"
|
||||
{
|
||||
#include <libavutil/avutil.h>
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user