AV1 support

This commit is contained in:
Patrick Nicolas
2024-08-31 22:07:56 +02:00
parent 8b611ee609
commit 7a42691269
11 changed files with 24 additions and 125 deletions
+2 -115
View File
@@ -21,7 +21,6 @@
#include "application.h"
#include "scenes/stream.h"
#include "utils/named_thread.h"
#include <algorithm>
#include <android/hardware_buffer.h>
#include <cassert>
#include <magic_enum.hpp>
@@ -57,6 +56,8 @@ const char * mime(xrt::drivers::wivrn::video_codec codec)
return "video/avc";
case c::h265:
return "video/hevc";
case c::av1:
return "video/AV1";
}
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 wivrn::android
@@ -292,16 +190,6 @@ void decoder::push_data(std::span<std::span<const uint8_t>> data, uint64_t frame
{
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_setString(format.get(), AMEDIAFORMAT_KEY_MIME, mime(description.codec));
// 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_OPERATING_RATE, std::ceil(fps));
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)));
+2
View File
@@ -54,6 +54,8 @@ static AVCodecID codec_id(xrt::drivers::wivrn::video_codec codec)
return AV_CODEC_ID_H264;
case c::h265:
return AV_CODEC_ID_HEVC;
case c::av1:
return AV_CODEC_ID_AV1;
}
assert(false);
__builtin_unreachable();
+1
View File
@@ -81,6 +81,7 @@ enum video_codec
h264,
h265,
hevc = h265,
av1,
};
struct audio_data
+1 -1
View File
@@ -41,7 +41,7 @@ Identifier of the encoder, one of `x264` (software encoding), `nvenc` (Nvidia ha
### `codec`
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)
Default values: full image (`width` = 1, `height` = 1, `offset_x` = 0, `offset_y` = 0)
+2
View File
@@ -46,6 +46,8 @@ NLOHMANN_JSON_SERIALIZE_ENUM(
{h264, "avc"},
{h265, "h265"},
{h265, "hevc"},
{av1, "av1"},
{av1, "AV1"},
})
}
+1
View File
@@ -61,6 +61,7 @@ static void split_bitrate(std::vector<xrt::drivers::wivrn::encoder_settings> & e
w *= 2;
break;
case xrt::drivers::wivrn::h265:
case xrt::drivers::wivrn::av1:
break;
}
encoder.bitrate = w;
-5
View File
@@ -99,11 +99,6 @@ void AvDeleter::operator()(AVCodecContext * x)
avcodec_free_context(&x);
}
void AvDeleter::operator()(AVFilterGraph * x)
{
avfilter_graph_free(&x);
}
void AvDeleter::operator()(AVPacket * x)
{
av_packet_free(&x);
-4
View File
@@ -24,7 +24,6 @@
#include <vulkan/vulkan_core.h>
struct AVBufferRef;
struct AVFilterGraph;
struct AVFrame;
struct AVCodecContext;
struct AVPacket;
@@ -52,15 +51,12 @@ struct AvDeleter
void
operator()(AVCodecContext *);
void
operator()(AVFilterGraph *);
void
operator()(AVPacket *);
};
using av_buffer_ptr = std::unique_ptr<AVBufferRef, AvDeleter>;
using av_frame_ptr = std::unique_ptr<AVFrame, 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>;
av_frame_ptr
@@ -52,6 +52,8 @@ encoder(VideoEncoderFFMPEG::Codec codec)
return "h264_vaapi";
case VideoEncoderFFMPEG::Codec::h265:
return "hevc_vaapi";
case VideoEncoderFFMPEG::Codec::av1:
return "av1_vaapi";
}
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:
encoder_ctx->profile = FF_PROFILE_HEVC_MAIN;
break;
case Codec::av1:
encoder_ctx->profile = FF_PROFILE_AV1_MAIN;
break;
}
for (auto option: settings.options)
{
+3
View File
@@ -142,6 +142,9 @@ std::unique_ptr<VideoEncoder> VideoEncoder::Create(
case h265:
file += ".h265";
break;
case av1:
file += ".av1";
break;
}
res->video_dump.open(file);
}
+7
View File
@@ -128,6 +128,8 @@ static auto encode_guid(video_codec codec)
return NV_ENC_CODEC_H264_GUID;
case h265:
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));
}
@@ -170,6 +172,9 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, encoder_settings & se
case video_codec::h265:
printf("%d HEVC presets\n", count);
break;
case video_codec::av1:
break;
}
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.hevcVUIParameters.videoFullRangeFlag = 1;
break;
case video_codec::av1:
break;
}
settings.range = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
settings.color_model = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709;