Skip to content

Commit

Permalink
H265: Demux sps for log print and statistic streams.(#3271) (#3286)
Browse files Browse the repository at this point in the history
* BitBuffer: add method to implement bit read operation.

* Codec: demux hevc sps for profile level resolution.

* Statistic: refine hevc profile level resolution.

* Kernel: return error code for demux hevc.

* Kernel: check bitstream length for hevc sps.

* UTest: add BitBuffer read bits utest.

* Kernel: refine print log and utest.

* Kernel: add comment for hevc sps.

Co-authored-by: winlin <winlin@vip.126.com>
  • Loading branch information
chundonglinlin and winlinvip authored Dec 4, 2022
1 parent e6c395e commit a0803b5
Show file tree
Hide file tree
Showing 9 changed files with 729 additions and 19 deletions.
7 changes: 4 additions & 3 deletions trunk/src/app/srs_app_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1074,9 +1074,10 @@ srs_error_t SrsOriginHub::on_video(SrsSharedPtrMessage* shared_video, bool is_se
c->width, c->height, c->video_data_rate / 1000, c->frame_rate, c->duration);
#ifdef SRS_H265
} else if (c->id == SrsVideoCodecIdHEVC) {
// TODO: FIXME: Use the correct information for HEVC.
err = stat->on_video_info(req_, c->id, c->avc_profile, c->avc_level, c->width, c->height);
srs_trace("%dB video sh, codec(%d)", msg->size, c->id);
err = stat->on_video_info(req_, c->id, c->hevc_profile, c->hevc_level, c->width, c->height);
srs_trace("%dB video sh, codec(%d, profile=%s, level=%s, %dx%d, %dkbps, %.1ffps, %.1fs)",
msg->size, c->id, srs_hevc_profile2str(c->hevc_profile).c_str(), srs_hevc_level2str(c->hevc_level).c_str(),
c->width, c->height, c->video_data_rate / 1000, c->frame_rate, c->duration);
#endif
}
if (err != srs_success) {
Expand Down
35 changes: 29 additions & 6 deletions trunk/src/app/srs_app_statistic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,20 @@ srs_error_t SrsStatisticStream::dumps(SrsJsonObject* obj)
obj->set("video", video);

video->set("codec", SrsJsonAny::str(srs_video_codec_id2str(vcodec).c_str()));
video->set("profile", SrsJsonAny::str(srs_avc_profile2str(avc_profile).c_str()));
video->set("level", SrsJsonAny::str(srs_avc_level2str(avc_level).c_str()));

if (vcodec == SrsVideoCodecIdAVC) {
video->set("profile", SrsJsonAny::str(srs_avc_profile2str(avc_profile).c_str()));
video->set("level", SrsJsonAny::str(srs_avc_level2str(avc_level).c_str()));
#ifdef SRS_H265
} else if (vcodec == SrsVideoCodecIdHEVC) {
video->set("profile", SrsJsonAny::str(srs_hevc_profile2str(hevc_profile).c_str()));
video->set("level", SrsJsonAny::str(srs_hevc_level2str(hevc_level).c_str()));
#endif
} else {
video->set("profile", SrsJsonAny::str("Other"));
video->set("level", SrsJsonAny::str("Other"));
}

video->set("width", SrsJsonAny::integer(width));
video->set("height", SrsJsonAny::integer(height));
}
Expand Down Expand Up @@ -335,7 +347,7 @@ SrsStatisticClient* SrsStatistic::find_client(string client_id)
return NULL;
}

srs_error_t SrsStatistic::on_video_info(SrsRequest* req, SrsVideoCodecId vcodec, SrsAvcProfile avc_profile, SrsAvcLevel avc_level, int width, int height)
srs_error_t SrsStatistic::on_video_info(SrsRequest* req, SrsVideoCodecId vcodec, int profile, int level, int width, int height)
{
srs_error_t err = srs_success;

Expand All @@ -344,9 +356,20 @@ srs_error_t SrsStatistic::on_video_info(SrsRequest* req, SrsVideoCodecId vcodec,

stream->has_video = true;
stream->vcodec = vcodec;
stream->avc_profile = avc_profile;
stream->avc_level = avc_level;


if (vcodec == SrsVideoCodecIdAVC) {
stream->avc_profile = (SrsAvcProfile)profile;
stream->avc_level = (SrsAvcLevel)level;
#ifdef SRS_H265
} else if (vcodec == SrsVideoCodecIdHEVC) {
stream->hevc_profile = (SrsHevcProfile)profile;
stream->hevc_level = (SrsHevcLevel)level;
#endif
} else {
stream->avc_profile = (SrsAvcProfile)profile;
stream->avc_level = (SrsAvcLevel)level;
}

stream->width = width;
stream->height = height;

Expand Down
9 changes: 7 additions & 2 deletions trunk/src/app/srs_app_statistic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ struct SrsStatisticStream
SrsAvcProfile avc_profile;
// The level_idc, ISO_IEC_14496-10-AVC-2003.pdf, page 45.
SrsAvcLevel avc_level;
#ifdef SRS_H265
// The profile_idc, T-REC-H.265-202108-I!!PDF-E.pdf, page 559.
SrsHevcProfile hevc_profile;
// The level_idc, T-REC-H.265-202108-I!!PDF-E.pdf, page 684.
SrsHevcLevel hevc_level;
#endif
// The width and height in codec info.
int width;
int height;
Expand Down Expand Up @@ -157,8 +163,7 @@ class SrsStatistic
virtual SrsStatisticClient* find_client(std::string client_id);
public:
// When got video info for stream.
virtual srs_error_t on_video_info(SrsRequest* req, SrsVideoCodecId vcodec, SrsAvcProfile avc_profile,
SrsAvcLevel avc_level, int width, int height);
virtual srs_error_t on_video_info(SrsRequest* req, SrsVideoCodecId vcodec, int avc_profile, int avc_level, int width, int height);
// When got audio info for stream.
virtual srs_error_t on_audio_info(SrsRequest* req, SrsAudioCodecId acodec, SrsAudioSampleRate asample_rate,
SrsAudioChannels asound_type, SrsAacObjectType aac_object);
Expand Down
81 changes: 81 additions & 0 deletions trunk/src/kernel/srs_kernel_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ bool SrsBitBuffer::empty() {
return stream->empty();
}

bool SrsBitBuffer::require_bits(int n)
{
if (n < 0) {
return false;
}

return n <= left_bits();
}

int8_t SrsBitBuffer::read_bit() {
if (!cb_left) {
srs_assert(!stream->empty());
Expand All @@ -391,3 +400,75 @@ int8_t SrsBitBuffer::read_bit() {
return v;
}

int SrsBitBuffer::left_bits()
{
return cb_left + stream->left() * 8;
}

void SrsBitBuffer::skip_bits(int n)
{
srs_assert(n <= left_bits());

for (int i = 0; i < n; i++) {
read_bit();
}
}

int32_t SrsBitBuffer::read_bits(int n)
{
srs_assert(n <= left_bits());

int32_t v = 0;
for (int i = 0; i < n; i++) {
v |= (read_bit() << (n - i - 1));
}
return v;
}

int8_t SrsBitBuffer::read_8bits()
{
// FAST_8
if (!cb_left) {
srs_assert(!stream->empty());
return stream->read_1bytes();
}

return read_bits(8);
}

int16_t SrsBitBuffer::read_16bits()
{
// FAST_16
if (!cb_left) {
srs_assert(!stream->empty());
return stream->read_2bytes();
}

return read_bits(16);
}

int32_t SrsBitBuffer::read_32bits()
{
// FAST_32
if (!cb_left) {
srs_assert(!stream->empty());
return stream->read_4bytes();
}

return read_bits(32);
}

int32_t SrsBitBuffer::read_bits_ue()
{
int32_t r = 0;
int i = 0;

while((read_bit() == 0) && (i < 32) && (left_bits() > 0) ) {
i++;
}

r = read_bits(i);
r += (1 << i) - 1;

return r;
}
8 changes: 8 additions & 0 deletions trunk/src/kernel/srs_kernel_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ class SrsBitBuffer
public:
bool empty();
int8_t read_bit();
bool require_bits(int n);
int left_bits();
void skip_bits(int n);
int32_t read_bits(int n);
int8_t read_8bits();
int16_t read_16bits();
int32_t read_32bits();
int32_t read_bits_ue();
};

#endif
Loading

0 comments on commit a0803b5

Please sign in to comment.