AV1 support
This commit is contained in:
@@ -21,7 +21,6 @@
|
|||||||
#include "application.h"
|
#include "application.h"
|
||||||
#include "scenes/stream.h"
|
#include "scenes/stream.h"
|
||||||
#include "utils/named_thread.h"
|
#include "utils/named_thread.h"
|
||||||
#include <algorithm>
|
|
||||||
#include <android/hardware_buffer.h>
|
#include <android/hardware_buffer.h>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <magic_enum.hpp>
|
#include <magic_enum.hpp>
|
||||||
@@ -57,6 +56,8 @@ const char * mime(xrt::drivers::wivrn::video_codec codec)
|
|||||||
return "video/avc";
|
return "video/avc";
|
||||||
case c::h265:
|
case c::h265:
|
||||||
return "video/hevc";
|
return "video/hevc";
|
||||||
|
case c::av1:
|
||||||
|
return "video/AV1";
|
||||||
}
|
}
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
@@ -70,109 +71,6 @@ void check(media_status_t status, const char * msg)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace nal_h264
|
|
||||||
{
|
|
||||||
static const int sps = 7;
|
|
||||||
static const int pps = 8;
|
|
||||||
} // namespace nal_h264
|
|
||||||
|
|
||||||
namespace nal_h265
|
|
||||||
{
|
|
||||||
static const int vps = 32;
|
|
||||||
static const int sps = 33;
|
|
||||||
static const int pps = 34;
|
|
||||||
static const int aud = 35;
|
|
||||||
static const int filler = 38;
|
|
||||||
}; // namespace nal_h265
|
|
||||||
|
|
||||||
enum class nal_class
|
|
||||||
{
|
|
||||||
csd,
|
|
||||||
data,
|
|
||||||
garbage
|
|
||||||
};
|
|
||||||
|
|
||||||
nal_class get_nal_class_h264(uint8_t * nal)
|
|
||||||
{
|
|
||||||
uint8_t nal_type = (nal[2] == 0 ? nal[4] : nal[3]) & 0x1F;
|
|
||||||
// spdlog::info("H264 NAL type {}", (int)nal_type);
|
|
||||||
switch (nal_type)
|
|
||||||
{
|
|
||||||
case nal_h264::sps:
|
|
||||||
case nal_h264::pps:
|
|
||||||
return nal_class::csd;
|
|
||||||
default:
|
|
||||||
return nal_class::data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
nal_class get_nal_class_h265(uint8_t * nal)
|
|
||||||
{
|
|
||||||
uint8_t nal_type = ((nal[2] == 0 ? nal[4] : nal[3]) >> 1) & 0x3F;
|
|
||||||
// spdlog::info("H265 NAL type {}", (int)nal_type);
|
|
||||||
switch (nal_type)
|
|
||||||
{
|
|
||||||
case nal_h265::vps:
|
|
||||||
case nal_h265::sps:
|
|
||||||
case nal_h265::pps:
|
|
||||||
return nal_class::csd;
|
|
||||||
case nal_h265::aud:
|
|
||||||
case nal_h265::filler:
|
|
||||||
return nal_class::garbage;
|
|
||||||
default:
|
|
||||||
return nal_class::data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
nal_class get_nal_class(uint8_t * nal, xrt::drivers::wivrn::video_codec codec)
|
|
||||||
{
|
|
||||||
switch (codec)
|
|
||||||
{
|
|
||||||
case xrt::drivers::wivrn::video_codec::h264:
|
|
||||||
return get_nal_class_h264(nal);
|
|
||||||
|
|
||||||
case xrt::drivers::wivrn::video_codec::h265:
|
|
||||||
return get_nal_class_h265(nal);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::pair<std::vector<uint8_t>, std::vector<uint8_t>> filter_csd(std::span<uint8_t> packet,
|
|
||||||
xrt::drivers::wivrn::video_codec codec)
|
|
||||||
{
|
|
||||||
if (packet.size() < 4)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
std::array<uint8_t, 3> header = {{0, 0, 1}};
|
|
||||||
auto end = packet.end();
|
|
||||||
auto header_start = packet.begin();
|
|
||||||
|
|
||||||
std::pair<std::vector<uint8_t>, std::vector<uint8_t>> out;
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
next_header--;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (get_nal_class(&*header_start, codec))
|
|
||||||
{
|
|
||||||
case nal_class::csd:
|
|
||||||
out.first.insert(out.first.end(), header_start, next_header);
|
|
||||||
break;
|
|
||||||
case nal_class::data:
|
|
||||||
out.second.insert(out.second.end(), header_start, next_header);
|
|
||||||
break;
|
|
||||||
case nal_class::garbage:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
header_start = next_header;
|
|
||||||
}
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace wivrn::android
|
namespace wivrn::android
|
||||||
@@ -292,16 +190,6 @@ void decoder::push_data(std::span<std::span<const uint8_t>> data, uint64_t frame
|
|||||||
{
|
{
|
||||||
if (!media_codec)
|
if (!media_codec)
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> contiguous_data;
|
|
||||||
for (const auto & d: data)
|
|
||||||
contiguous_data.insert(contiguous_data.end(), d.begin(), d.end());
|
|
||||||
auto [csd, not_csd] = filter_csd(contiguous_data, description.codec);
|
|
||||||
if (csd.empty())
|
|
||||||
{
|
|
||||||
// We can't initizale the encoder, drop the data
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
AMediaFormat_ptr format(AMediaFormat_new());
|
AMediaFormat_ptr format(AMediaFormat_new());
|
||||||
AMediaFormat_setString(format.get(), AMEDIAFORMAT_KEY_MIME, mime(description.codec));
|
AMediaFormat_setString(format.get(), AMEDIAFORMAT_KEY_MIME, mime(description.codec));
|
||||||
// AMediaFormat_setInt32(format.get(), "vendor.qti-ext-dec-low-latency.enable", 1); // Qualcomm low
|
// AMediaFormat_setInt32(format.get(), "vendor.qti-ext-dec-low-latency.enable", 1); // Qualcomm low
|
||||||
@@ -310,7 +198,6 @@ void decoder::push_data(std::span<std::span<const uint8_t>> data, uint64_t frame
|
|||||||
AMediaFormat_setInt32(format.get(), AMEDIAFORMAT_KEY_HEIGHT, description.height);
|
AMediaFormat_setInt32(format.get(), AMEDIAFORMAT_KEY_HEIGHT, description.height);
|
||||||
AMediaFormat_setInt32(format.get(), AMEDIAFORMAT_KEY_OPERATING_RATE, std::ceil(fps));
|
AMediaFormat_setInt32(format.get(), AMEDIAFORMAT_KEY_OPERATING_RATE, std::ceil(fps));
|
||||||
AMediaFormat_setInt32(format.get(), AMEDIAFORMAT_KEY_PRIORITY, 0);
|
AMediaFormat_setInt32(format.get(), AMEDIAFORMAT_KEY_PRIORITY, 0);
|
||||||
// AMediaFormat_setBuffer(format.get(), AMEDIAFORMAT_KEY_CSD_0, csd.data(), csd.size());
|
|
||||||
|
|
||||||
media_codec.reset(AMediaCodec_createDecoderByType(mime(description.codec)));
|
media_codec.reset(AMediaCodec_createDecoderByType(mime(description.codec)));
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ static AVCodecID codec_id(xrt::drivers::wivrn::video_codec codec)
|
|||||||
return AV_CODEC_ID_H264;
|
return AV_CODEC_ID_H264;
|
||||||
case c::h265:
|
case c::h265:
|
||||||
return AV_CODEC_ID_HEVC;
|
return AV_CODEC_ID_HEVC;
|
||||||
|
case c::av1:
|
||||||
|
return AV_CODEC_ID_AV1;
|
||||||
}
|
}
|
||||||
assert(false);
|
assert(false);
|
||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ enum video_codec
|
|||||||
h264,
|
h264,
|
||||||
h265,
|
h265,
|
||||||
hevc = h265,
|
hevc = h265,
|
||||||
|
av1,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct audio_data
|
struct audio_data
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ Identifier of the encoder, one of `x264` (software encoding), `nvenc` (Nvidia ha
|
|||||||
### `codec`
|
### `codec`
|
||||||
Default value: `h265`
|
Default value: `h265`
|
||||||
|
|
||||||
One of `h264` or `h265`. If using `x264` encoder, value is ignored and `h264` is used.
|
One of `h264`, `h265` or `av1`. If using `x264` encoder, value is ignored and `h264` is used. `av1` is only supported on `vaapi` encoder.
|
||||||
|
|
||||||
### `width`, `height`, `offset_x`, `offset_y` (advanced)
|
### `width`, `height`, `offset_x`, `offset_y` (advanced)
|
||||||
Default values: full image (`width` = 1, `height` = 1, `offset_x` = 0, `offset_y` = 0)
|
Default values: full image (`width` = 1, `height` = 1, `offset_x` = 0, `offset_y` = 0)
|
||||||
|
|||||||
@@ -46,6 +46,8 @@ NLOHMANN_JSON_SERIALIZE_ENUM(
|
|||||||
{h264, "avc"},
|
{h264, "avc"},
|
||||||
{h265, "h265"},
|
{h265, "h265"},
|
||||||
{h265, "hevc"},
|
{h265, "hevc"},
|
||||||
|
{av1, "av1"},
|
||||||
|
{av1, "AV1"},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ static void split_bitrate(std::vector<xrt::drivers::wivrn::encoder_settings> & e
|
|||||||
w *= 2;
|
w *= 2;
|
||||||
break;
|
break;
|
||||||
case xrt::drivers::wivrn::h265:
|
case xrt::drivers::wivrn::h265:
|
||||||
|
case xrt::drivers::wivrn::av1:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
encoder.bitrate = w;
|
encoder.bitrate = w;
|
||||||
|
|||||||
@@ -99,11 +99,6 @@ void AvDeleter::operator()(AVCodecContext * x)
|
|||||||
avcodec_free_context(&x);
|
avcodec_free_context(&x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AvDeleter::operator()(AVFilterGraph * x)
|
|
||||||
{
|
|
||||||
avfilter_graph_free(&x);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AvDeleter::operator()(AVPacket * x)
|
void AvDeleter::operator()(AVPacket * x)
|
||||||
{
|
{
|
||||||
av_packet_free(&x);
|
av_packet_free(&x);
|
||||||
|
|||||||
@@ -24,7 +24,6 @@
|
|||||||
#include <vulkan/vulkan_core.h>
|
#include <vulkan/vulkan_core.h>
|
||||||
|
|
||||||
struct AVBufferRef;
|
struct AVBufferRef;
|
||||||
struct AVFilterGraph;
|
|
||||||
struct AVFrame;
|
struct AVFrame;
|
||||||
struct AVCodecContext;
|
struct AVCodecContext;
|
||||||
struct AVPacket;
|
struct AVPacket;
|
||||||
@@ -52,15 +51,12 @@ struct AvDeleter
|
|||||||
void
|
void
|
||||||
operator()(AVCodecContext *);
|
operator()(AVCodecContext *);
|
||||||
void
|
void
|
||||||
operator()(AVFilterGraph *);
|
|
||||||
void
|
|
||||||
operator()(AVPacket *);
|
operator()(AVPacket *);
|
||||||
};
|
};
|
||||||
|
|
||||||
using av_buffer_ptr = std::unique_ptr<AVBufferRef, AvDeleter>;
|
using av_buffer_ptr = std::unique_ptr<AVBufferRef, AvDeleter>;
|
||||||
using av_frame_ptr = std::unique_ptr<AVFrame, AvDeleter>;
|
using av_frame_ptr = std::unique_ptr<AVFrame, AvDeleter>;
|
||||||
using av_codec_context_ptr = std::unique_ptr<AVCodecContext, AvDeleter>;
|
using av_codec_context_ptr = std::unique_ptr<AVCodecContext, AvDeleter>;
|
||||||
using av_filter_graph_ptr = std::unique_ptr<AVFilterGraph, AvDeleter>;
|
|
||||||
using av_packet_ptr = std::unique_ptr<AVPacket, AvDeleter>;
|
using av_packet_ptr = std::unique_ptr<AVPacket, AvDeleter>;
|
||||||
|
|
||||||
av_frame_ptr
|
av_frame_ptr
|
||||||
|
|||||||
@@ -52,6 +52,8 @@ encoder(VideoEncoderFFMPEG::Codec codec)
|
|||||||
return "h264_vaapi";
|
return "h264_vaapi";
|
||||||
case VideoEncoderFFMPEG::Codec::h265:
|
case VideoEncoderFFMPEG::Codec::h265:
|
||||||
return "hevc_vaapi";
|
return "hevc_vaapi";
|
||||||
|
case VideoEncoderFFMPEG::Codec::av1:
|
||||||
|
return "av1_vaapi";
|
||||||
}
|
}
|
||||||
throw std::runtime_error("invalid codec " + std::to_string(int(codec)));
|
throw std::runtime_error("invalid codec " + std::to_string(int(codec)));
|
||||||
}
|
}
|
||||||
@@ -192,6 +194,9 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk, xrt::drivers::wivrn::en
|
|||||||
case Codec::h265:
|
case Codec::h265:
|
||||||
encoder_ctx->profile = FF_PROFILE_HEVC_MAIN;
|
encoder_ctx->profile = FF_PROFILE_HEVC_MAIN;
|
||||||
break;
|
break;
|
||||||
|
case Codec::av1:
|
||||||
|
encoder_ctx->profile = FF_PROFILE_AV1_MAIN;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
for (auto option: settings.options)
|
for (auto option: settings.options)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -142,6 +142,9 @@ std::unique_ptr<VideoEncoder> VideoEncoder::Create(
|
|||||||
case h265:
|
case h265:
|
||||||
file += ".h265";
|
file += ".h265";
|
||||||
break;
|
break;
|
||||||
|
case av1:
|
||||||
|
file += ".av1";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
res->video_dump.open(file);
|
res->video_dump.open(file);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -128,6 +128,8 @@ static auto encode_guid(video_codec codec)
|
|||||||
return NV_ENC_CODEC_H264_GUID;
|
return NV_ENC_CODEC_H264_GUID;
|
||||||
case h265:
|
case h265:
|
||||||
return NV_ENC_CODEC_HEVC_GUID;
|
return NV_ENC_CODEC_HEVC_GUID;
|
||||||
|
case av1:
|
||||||
|
throw std::runtime_error("AV1 not implemented on nvenc");
|
||||||
}
|
}
|
||||||
throw std::out_of_range("Invalid codec " + std::to_string(codec));
|
throw std::out_of_range("Invalid codec " + std::to_string(codec));
|
||||||
}
|
}
|
||||||
@@ -170,6 +172,9 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, encoder_settings & se
|
|||||||
case video_codec::h265:
|
case video_codec::h265:
|
||||||
printf("%d HEVC presets\n", count);
|
printf("%d HEVC presets\n", count);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case video_codec::av1:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (GUID & i: presets)
|
for (GUID & i: presets)
|
||||||
@@ -216,6 +221,8 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, encoder_settings & se
|
|||||||
params.encodeCodecConfig.hevcConfig.idrPeriod = NVENC_INFINITE_GOPLENGTH;
|
params.encodeCodecConfig.hevcConfig.idrPeriod = NVENC_INFINITE_GOPLENGTH;
|
||||||
params.encodeCodecConfig.hevcConfig.hevcVUIParameters.videoFullRangeFlag = 1;
|
params.encodeCodecConfig.hevcConfig.hevcVUIParameters.videoFullRangeFlag = 1;
|
||||||
break;
|
break;
|
||||||
|
case video_codec::av1:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
settings.range = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
|
settings.range = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
|
||||||
settings.color_model = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709;
|
settings.color_model = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709;
|
||||||
|
|||||||
Reference in New Issue
Block a user