mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 20:25:34 +08:00
[fix] fix av1 rtp packetizer
This commit is contained in:
@@ -67,8 +67,6 @@ int AomAv1Decoder::Decode(
|
||||
fwrite((unsigned char *)data, 1, size, file_av1_);
|
||||
}
|
||||
|
||||
LOG_ERROR("Decode size {}", size);
|
||||
|
||||
aom_codec_iter_t iter = nullptr;
|
||||
aom_codec_err_t ret =
|
||||
aom_codec_decode(&aom_av1_decoder_ctx_, data, size, nullptr);
|
||||
|
||||
@@ -98,7 +98,6 @@ int Dav1dAv1Decoder::Init() {
|
||||
int Dav1dAv1Decoder::Decode(
|
||||
const uint8_t *data, int size,
|
||||
std::function<void(VideoFrame)> on_receive_decoded_frame) {
|
||||
LOG_ERROR("frame size = {}", size);
|
||||
if (SAVE_RECEIVED_AV1_STREAM) {
|
||||
fwrite((unsigned char *)data, 1, size, file_av1_);
|
||||
}
|
||||
|
||||
@@ -52,6 +52,32 @@ bool ByteBufferReader::ReadUVarint(uint64_t* val) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ByteBufferReader::ReadUVarint(uint64_t* val, size_t* len) {
|
||||
if (!val) {
|
||||
return false;
|
||||
}
|
||||
// Integers are deserialized 7 bits at a time, with each byte having a
|
||||
// continuation byte (msb=1) if there are more bytes to be read.
|
||||
uint64_t v = 0;
|
||||
for (int i = 0; i < 64; i += 7) {
|
||||
char byte;
|
||||
if (!ReadBytes(&byte, 1)) {
|
||||
return false;
|
||||
}
|
||||
// Read the first 7 bits of the byte, then offset by bits read so far.
|
||||
v |= (static_cast<uint64_t>(byte) & 0x7F) << i;
|
||||
// True if the msb is not a continuation byte.
|
||||
if (static_cast<uint64_t>(byte) < 0x80) {
|
||||
*val = v;
|
||||
if (len) {
|
||||
*len = i / 8 + (i % 8 ? 1 : 0) + 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ByteBufferReader::Consume(size_t size) {
|
||||
if (size > Length()) return false;
|
||||
start_ += size;
|
||||
|
||||
@@ -25,7 +25,7 @@ class ByteBufferReader {
|
||||
bool ReadBytes(char* val, size_t len);
|
||||
bool ReadUInt8(uint8_t* val);
|
||||
bool ReadUVarint(uint64_t* val);
|
||||
bool ReadUVarint2(uint64_t* val, size_t* len);
|
||||
bool ReadUVarint(uint64_t* val, size_t* len);
|
||||
|
||||
bool Consume(size_t size);
|
||||
|
||||
|
||||
@@ -50,13 +50,10 @@ std::vector<Obu> ParseObus(uint8_t* payload, int payload_size) {
|
||||
std::vector<Obu> result;
|
||||
ByteBufferReader payload_reader(reinterpret_cast<const char*>(payload),
|
||||
payload_size);
|
||||
int count = 0;
|
||||
while (payload_reader.Length() > 0) {
|
||||
count++;
|
||||
Obu obu;
|
||||
bool has_ext_header = false;
|
||||
payload_reader.ReadUInt8(&obu.header);
|
||||
LOG_ERROR("obu.header = [{:B}]", obu.header);
|
||||
obu.size = 1;
|
||||
if (ObuHasExtension(obu.header)) {
|
||||
if (payload_reader.Length() == 0) {
|
||||
@@ -78,7 +75,8 @@ std::vector<Obu> ParseObus(uint8_t* payload, int payload_size) {
|
||||
payload_reader.Consume(payload_reader.Length());
|
||||
} else {
|
||||
uint64_t size = 0;
|
||||
if (!payload_reader.ReadUVarint(&size) ||
|
||||
size_t size_len = 0;
|
||||
if (!payload_reader.ReadUVarint(&size, &size_len) ||
|
||||
size > payload_reader.Length()) {
|
||||
LOG_ERROR(
|
||||
"Malformed AV1 input: declared payload_size {} is larger than "
|
||||
@@ -92,10 +90,15 @@ std::vector<Obu> ParseObus(uint8_t* payload, int payload_size) {
|
||||
obu.payload = std::vector<uint8_t>(
|
||||
reinterpret_cast<const uint8_t*>(payload_reader.Data()),
|
||||
reinterpret_cast<const uint8_t*>(payload_reader.Data()) + size);
|
||||
obu.payload.insert(obu.payload.begin(), size);
|
||||
|
||||
std::vector<uint8_t> size_data = std::vector<uint8_t>(
|
||||
reinterpret_cast<const uint8_t*>(payload_reader.Data() - size_len),
|
||||
reinterpret_cast<const uint8_t*>(payload_reader.Data() - size_len) +
|
||||
size_len);
|
||||
obu.payload.insert(obu.payload.begin(), size_data.begin(),
|
||||
size_data.end());
|
||||
}
|
||||
payload_reader.Consume(size);
|
||||
LOG_ERROR("Has size = {}", size);
|
||||
}
|
||||
obu.size += obu.payload.size();
|
||||
// Skip obus that shouldn't be transfered over rtp.
|
||||
@@ -104,25 +107,18 @@ std::vector<Obu> ParseObus(uint8_t* payload, int payload_size) {
|
||||
obu.payload.insert(obu.payload.begin(), obu.extension_header);
|
||||
}
|
||||
obu.payload.insert(obu.payload.begin(), obu.header);
|
||||
// if (obu_type != kObuTypeTileList && //
|
||||
// obu_type != kObuTypePadding) {
|
||||
result.push_back(obu);
|
||||
LOG_ERROR("obu size = {}", obu.payload.size());
|
||||
// }
|
||||
// if (obu_type != kObuTypeTemporalDelimiter && //
|
||||
// obu_type != kObuTypeTileList && //
|
||||
// obu_type != kObuTypePadding) {
|
||||
// result.push_back(obu);
|
||||
result.push_back(obu);
|
||||
// }
|
||||
}
|
||||
|
||||
LOG_ERROR("count = [{}]", count);
|
||||
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
LOG_ERROR("[{}] Obu size = [{}], Obu type [{}|{}]", i, result[i].size,
|
||||
ObuType(result[i].payload[0]),
|
||||
ObuTypeToString((OBU_TYPE)ObuType(result[i].header)));
|
||||
}
|
||||
// for (int i = 0; i < result.size(); i++) {
|
||||
// LOG_ERROR("[{}] Obu size = [{}], Obu type [{}|{}]", i, result[i].size,
|
||||
// ObuType(result[i].payload[0]),
|
||||
// ObuTypeToString((OBU_TYPE)ObuType(result[i].header)));
|
||||
// }
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -519,23 +519,13 @@ void RtpCodec::Encode(VideoFrameType frame_type, uint8_t* buffer, size_t size,
|
||||
}
|
||||
}
|
||||
} else if (RtpPacket::PAYLOAD_TYPE::AV1 == payload_type_) {
|
||||
LOG_ERROR("source [{:X} {:X} {:X} {:X}], size={}", buffer[0], buffer[1],
|
||||
buffer[2], buffer[3], size);
|
||||
std::vector<Obu> obus = ParseObus(buffer, size);
|
||||
// LOG_ERROR("Total size = [{}]", size);
|
||||
|
||||
uint32_t timestamp =
|
||||
std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count();
|
||||
|
||||
for (int i = 0; i < obus.size(); i++) {
|
||||
// LOG_ERROR("1 [{}] Obu size = [{}], Obu type [{}]", i, obus[i].size,
|
||||
// ObuTypeToString((OBU_TYPE)ObuType(obus[i].header)));
|
||||
|
||||
LOG_ERROR("0 output [{:X} {:X}], size={}", obus[i].payload.data()[0],
|
||||
obus[i].payload.data()[1], obus[i].payload.size());
|
||||
|
||||
if (obus[i].size <= MAX_NALU_LEN) {
|
||||
RtpPacket rtp_packet;
|
||||
rtp_packet.SetVerion(version_);
|
||||
@@ -557,19 +547,7 @@ void RtpCodec::Encode(VideoFrameType frame_type, uint8_t* buffer, size_t size,
|
||||
}
|
||||
|
||||
rtp_packet.SetAv1AggrHeader(0, 0, 1, 0);
|
||||
if (obus[i].payload.data() == nullptr) {
|
||||
LOG_ERROR("obus[i].payload.data() is nullptr");
|
||||
}
|
||||
if (obus[i].size == 0) {
|
||||
LOG_ERROR("obus[i].size == 0");
|
||||
}
|
||||
|
||||
rtp_packet.EncodeAv1(obus[i].payload.data(), obus[i].size);
|
||||
// int type = (obus[i].payload[0] & 0b0'1111'000) >> 3;
|
||||
int type = (rtp_packet.Payload()[0] & 0b0'1111'000) >> 3;
|
||||
LOG_ERROR("1 output [{:X} {:X}], size={}", rtp_packet.Payload()[0],
|
||||
rtp_packet.Payload()[1], rtp_packet.PayloadSize());
|
||||
|
||||
packets.emplace_back(rtp_packet);
|
||||
} else {
|
||||
size_t last_packet_size = obus[i].size % MAX_NALU_LEN;
|
||||
@@ -602,12 +580,6 @@ void RtpCodec::Encode(VideoFrameType frame_type, uint8_t* buffer, size_t size,
|
||||
? 1
|
||||
: 0;
|
||||
rtp_packet.SetAv1AggrHeader(z, y, w, n);
|
||||
if (obus[i].payload.data() == nullptr) {
|
||||
LOG_ERROR("obus[i].payload.data() is nullptr");
|
||||
}
|
||||
if (obus[i].size == 0) {
|
||||
LOG_ERROR("obus[i].size == 0");
|
||||
}
|
||||
if (index == packet_num - 1 && last_packet_size > 0) {
|
||||
rtp_packet.EncodeAv1(obus[i].payload.data() + index * MAX_NALU_LEN,
|
||||
last_packet_size);
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#include "rtp_video_receiver.h"
|
||||
|
||||
#include "byte_buffer.h"
|
||||
#include "log.h"
|
||||
#include "obu_parser.h"
|
||||
|
||||
#define NV12_BUFFER_SIZE (1280 * 720 * 3 / 2)
|
||||
#define RTCP_RR_INTERVAL 1000
|
||||
@@ -16,8 +14,6 @@ RtpVideoReceiver::~RtpVideoReceiver() {
|
||||
}
|
||||
|
||||
void RtpVideoReceiver::InsertRtpPacket(RtpPacket& rtp_packet) {
|
||||
LOG_ERROR("1 input [{:X} {:X}], size={}", rtp_packet.Payload()[0],
|
||||
rtp_packet.Payload()[1], rtp_packet.PayloadSize());
|
||||
if (!rtp_statistics_) {
|
||||
rtp_statistics_ = std::make_unique<RtpStatistics>();
|
||||
rtp_statistics_->Start();
|
||||
@@ -239,9 +235,6 @@ bool RtpVideoReceiver::CheckIsH264FrameCompleted(RtpPacket& rtp_packet) {
|
||||
}
|
||||
|
||||
bool RtpVideoReceiver::CheckIsAv1FrameCompleted(RtpPacket& rtp_packet) {
|
||||
LOG_ERROR("2 input [{:X} {:X}], size={}", rtp_packet.Payload()[0],
|
||||
rtp_packet.Payload()[1], rtp_packet.PayloadSize());
|
||||
|
||||
if (rtp_packet.Av1FrameEnd()) {
|
||||
uint16_t end_seq = rtp_packet.SequenceNumber();
|
||||
size_t start = end_seq;
|
||||
@@ -279,14 +272,6 @@ bool RtpVideoReceiver::CheckIsAv1FrameCompleted(RtpPacket& rtp_packet) {
|
||||
incomplete_frame_list_.erase(start);
|
||||
}
|
||||
|
||||
obu::Obu obu;
|
||||
ByteBufferReader payload_reader(reinterpret_cast<const char*>(nv12_data_),
|
||||
complete_frame_size);
|
||||
payload_reader.ReadUInt8(&obu.header);
|
||||
int type = (nv12_data_[0] & 0b0'1111'000) >> 3;
|
||||
|
||||
LOG_ERROR("complete_frame_size = {}, type = {}", complete_frame_size,
|
||||
type);
|
||||
compelete_video_frame_queue_.push(
|
||||
VideoFrame(nv12_data_, complete_frame_size));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user