[fix] fix av1 rtp packetizer

This commit is contained in:
dijunkun
2024-10-15 10:29:18 +08:00
parent c13cffb58e
commit 9a6def32fd
7 changed files with 42 additions and 66 deletions

View File

@@ -67,8 +67,6 @@ int AomAv1Decoder::Decode(
fwrite((unsigned char *)data, 1, size, file_av1_); fwrite((unsigned char *)data, 1, size, file_av1_);
} }
LOG_ERROR("Decode size {}", size);
aom_codec_iter_t iter = nullptr; aom_codec_iter_t iter = nullptr;
aom_codec_err_t ret = aom_codec_err_t ret =
aom_codec_decode(&aom_av1_decoder_ctx_, data, size, nullptr); aom_codec_decode(&aom_av1_decoder_ctx_, data, size, nullptr);

View File

@@ -98,7 +98,6 @@ int Dav1dAv1Decoder::Init() {
int Dav1dAv1Decoder::Decode( int Dav1dAv1Decoder::Decode(
const uint8_t *data, int size, const uint8_t *data, int size,
std::function<void(VideoFrame)> on_receive_decoded_frame) { std::function<void(VideoFrame)> on_receive_decoded_frame) {
LOG_ERROR("frame size = {}", size);
if (SAVE_RECEIVED_AV1_STREAM) { if (SAVE_RECEIVED_AV1_STREAM) {
fwrite((unsigned char *)data, 1, size, file_av1_); fwrite((unsigned char *)data, 1, size, file_av1_);
} }

View File

@@ -52,6 +52,32 @@ bool ByteBufferReader::ReadUVarint(uint64_t* val) {
return false; 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) { bool ByteBufferReader::Consume(size_t size) {
if (size > Length()) return false; if (size > Length()) return false;
start_ += size; start_ += size;

View File

@@ -25,7 +25,7 @@ class ByteBufferReader {
bool ReadBytes(char* val, size_t len); bool ReadBytes(char* val, size_t len);
bool ReadUInt8(uint8_t* val); bool ReadUInt8(uint8_t* val);
bool ReadUVarint(uint64_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); bool Consume(size_t size);

View File

@@ -50,13 +50,10 @@ std::vector<Obu> ParseObus(uint8_t* payload, int payload_size) {
std::vector<Obu> result; std::vector<Obu> result;
ByteBufferReader payload_reader(reinterpret_cast<const char*>(payload), ByteBufferReader payload_reader(reinterpret_cast<const char*>(payload),
payload_size); payload_size);
int count = 0;
while (payload_reader.Length() > 0) { while (payload_reader.Length() > 0) {
count++;
Obu obu; Obu obu;
bool has_ext_header = false; bool has_ext_header = false;
payload_reader.ReadUInt8(&obu.header); payload_reader.ReadUInt8(&obu.header);
LOG_ERROR("obu.header = [{:B}]", obu.header);
obu.size = 1; obu.size = 1;
if (ObuHasExtension(obu.header)) { if (ObuHasExtension(obu.header)) {
if (payload_reader.Length() == 0) { 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()); payload_reader.Consume(payload_reader.Length());
} else { } else {
uint64_t size = 0; 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()) { size > payload_reader.Length()) {
LOG_ERROR( LOG_ERROR(
"Malformed AV1 input: declared payload_size {} is larger than " "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>( obu.payload = std::vector<uint8_t>(
reinterpret_cast<const uint8_t*>(payload_reader.Data()), reinterpret_cast<const uint8_t*>(payload_reader.Data()),
reinterpret_cast<const uint8_t*>(payload_reader.Data()) + size); 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); payload_reader.Consume(size);
LOG_ERROR("Has size = {}", size);
} }
obu.size += obu.payload.size(); obu.size += obu.payload.size();
// Skip obus that shouldn't be transfered over rtp. // 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.extension_header);
} }
obu.payload.insert(obu.payload.begin(), obu.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 && // // if (obu_type != kObuTypeTemporalDelimiter && //
// obu_type != kObuTypeTileList && // // obu_type != kObuTypeTileList && //
// obu_type != kObuTypePadding) { // 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,
for (int i = 0; i < result.size(); i++) { // ObuType(result[i].payload[0]),
LOG_ERROR("[{}] Obu size = [{}], Obu type [{}|{}]", i, result[i].size, // ObuTypeToString((OBU_TYPE)ObuType(result[i].header)));
ObuType(result[i].payload[0]), // }
ObuTypeToString((OBU_TYPE)ObuType(result[i].header)));
}
return result; return result;
} }

View File

@@ -519,23 +519,13 @@ void RtpCodec::Encode(VideoFrameType frame_type, uint8_t* buffer, size_t size,
} }
} }
} else if (RtpPacket::PAYLOAD_TYPE::AV1 == payload_type_) { } 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); std::vector<Obu> obus = ParseObus(buffer, size);
// LOG_ERROR("Total size = [{}]", size);
uint32_t timestamp = uint32_t timestamp =
std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch()) std::chrono::system_clock::now().time_since_epoch())
.count(); .count();
for (int i = 0; i < obus.size(); i++) { 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) { if (obus[i].size <= MAX_NALU_LEN) {
RtpPacket rtp_packet; RtpPacket rtp_packet;
rtp_packet.SetVerion(version_); 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); 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); 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); packets.emplace_back(rtp_packet);
} else { } else {
size_t last_packet_size = obus[i].size % MAX_NALU_LEN; 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 ? 1
: 0; : 0;
rtp_packet.SetAv1AggrHeader(z, y, w, n); 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) { if (index == packet_num - 1 && last_packet_size > 0) {
rtp_packet.EncodeAv1(obus[i].payload.data() + index * MAX_NALU_LEN, rtp_packet.EncodeAv1(obus[i].payload.data() + index * MAX_NALU_LEN,
last_packet_size); last_packet_size);

View File

@@ -1,8 +1,6 @@
#include "rtp_video_receiver.h" #include "rtp_video_receiver.h"
#include "byte_buffer.h"
#include "log.h" #include "log.h"
#include "obu_parser.h"
#define NV12_BUFFER_SIZE (1280 * 720 * 3 / 2) #define NV12_BUFFER_SIZE (1280 * 720 * 3 / 2)
#define RTCP_RR_INTERVAL 1000 #define RTCP_RR_INTERVAL 1000
@@ -16,8 +14,6 @@ RtpVideoReceiver::~RtpVideoReceiver() {
} }
void RtpVideoReceiver::InsertRtpPacket(RtpPacket& rtp_packet) { 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_) { if (!rtp_statistics_) {
rtp_statistics_ = std::make_unique<RtpStatistics>(); rtp_statistics_ = std::make_unique<RtpStatistics>();
rtp_statistics_->Start(); rtp_statistics_->Start();
@@ -239,9 +235,6 @@ bool RtpVideoReceiver::CheckIsH264FrameCompleted(RtpPacket& rtp_packet) {
} }
bool RtpVideoReceiver::CheckIsAv1FrameCompleted(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()) { if (rtp_packet.Av1FrameEnd()) {
uint16_t end_seq = rtp_packet.SequenceNumber(); uint16_t end_seq = rtp_packet.SequenceNumber();
size_t start = end_seq; size_t start = end_seq;
@@ -279,14 +272,6 @@ bool RtpVideoReceiver::CheckIsAv1FrameCompleted(RtpPacket& rtp_packet) {
incomplete_frame_list_.erase(start); 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( compelete_video_frame_queue_.push(
VideoFrame(nv12_data_, complete_frame_size)); VideoFrame(nv12_data_, complete_frame_size));