Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mp4 stts box bug. #4190

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 22 additions & 67 deletions trunk/src/kernel/srs_kernel_mp4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3962,52 +3962,12 @@
SrsMp4DecodingTime2SampleBox::SrsMp4DecodingTime2SampleBox()
{
type = SrsMp4BoxTypeSTTS;

index = count = 0;
}

SrsMp4DecodingTime2SampleBox::~SrsMp4DecodingTime2SampleBox()
{
}

srs_error_t SrsMp4DecodingTime2SampleBox::initialize_counter()
{
srs_error_t err = srs_success;

// If only sps/pps and no frames, there is no stts entries.
if (entries.empty()) {
return err;
}

index = 0;
if (index >= entries.size()) {
return srs_error_new(ERROR_MP4_ILLEGAL_TIMESTAMP, "illegal ts, empty stts");
}

count = entries[0].sample_count;

return err;
}

srs_error_t SrsMp4DecodingTime2SampleBox::on_sample(uint32_t sample_index, SrsMp4SttsEntry** ppentry)
{
srs_error_t err = srs_success;

if (sample_index + 1 > count) {
index++;

if (index >= entries.size()) {
return srs_error_new(ERROR_MP4_ILLEGAL_TIMESTAMP, "illegal ts, stts overflow, count=%zd", entries.size());
}

count += entries[index].sample_count;
}

*ppentry = &entries[index];

return err;
}

int SrsMp4DecodingTime2SampleBox::nb_header()
{
return SrsMp4FullBox::nb_header() + 4 + 8*(int)entries.size();
Expand Down Expand Up @@ -5062,21 +5022,15 @@
if (sample->frame_type == SrsVideoAvcFrameTypeKeyFrame) {
stss_entries.push_back(sample->index + 1);
}

if (stts) {
if (previous) {
uint32_t delta = (uint32_t)(sample->dts - previous->dts);
if (stts_entry.sample_delta == 0 || stts_entry.sample_delta == delta) {
stts_entry.sample_delta = delta;
stts_entry.sample_count++;
} else {
stts_entries.push_back(stts_entry);
stts_entry.sample_count = 1;
stts_entry.sample_delta = delta;
}
} else {
// The first sample always in the STTS table.
stts_entry.sample_count++;

if (stts && previous) {
if (sample->dts >= previous->dts && previous->nb_samples > 0) {
uint32_t delta = (uint32_t)(sample->dts - previous->dts) / previous->nb_samples;
stts_entry.sample_count = previous->nb_samples;
// calcaulate delta in the time-scale of the media.
// moov->mvhd->timescale which is hardcoded to 1000, sample->tbn also being hardcoded to 1000.
stts_entry.sample_delta = delta * previous->tbn / 1000;
stts_entries.push_back(stts_entry);
}
}

Expand All @@ -5097,7 +5051,10 @@
previous = sample;
}

if (stts && stts_entry.sample_count) {
if (stts && previous && previous->nb_samples > 0) {
stts_entry.sample_count = previous->nb_samples;
// Can't calculate last sample duration, so set sample_delta to 1.
stts_entry.sample_delta = 1;
stts_entries.push_back(stts_entry);
}

Expand Down Expand Up @@ -5216,11 +5173,6 @@
// Samples per chunk.
stsc->initialize_counter();

// DTS box.
if ((err = stts->initialize_counter()) != srs_success) {
return srs_error_wrap(err, "stts init counter");
}

// CTS/PTS box.
if (ctts && (err = ctts->initialize_counter()) != srs_success) {
return srs_error_wrap(err, "ctts init counter");
Expand Down Expand Up @@ -5249,13 +5201,14 @@
}
sample_relative_offset += sample_size;

SrsMp4SttsEntry* stts_entry = NULL;
if ((err = stts->on_sample(sample->index, &stts_entry)) != srs_success) {
srs_freep(sample);
return srs_error_wrap(err, "stts on sample");
}
if (previous) {
sample->pts = sample->dts = previous->dts + stts_entry->sample_delta;
uint32_t stts_index = sample->index - 1;
if (stts_index >= 0 && stts_index < stts->entries.size()) {
SrsMp4SttsEntry* stts_entry = &stts->entries[stts_index];
sample->pts = sample->dts = previous->dts + stts_entry->sample_count * stts_entry->sample_delta;
Fixed Show fixed Hide fixed
} else {
sample->pts = sample->dts = previous->dts;
}
}

SrsMp4CttsEntry* ctts_entry = NULL;
Expand Down Expand Up @@ -5828,10 +5781,12 @@
ps->type = SrsFrameTypeVideo;
ps->frame_type = (SrsVideoAvcFrameType)ft;
ps->index = nb_videos++;
ps->nb_samples = format->video->nb_samples;
vduration = dts;
} else if (ht == SrsMp4HandlerTypeSOUN) {
ps->type = SrsFrameTypeAudio;
ps->index = nb_audios++;
ps->nb_samples = format->audio->nb_samples;
aduration = dts;
} else {
srs_freep(ps);
Expand Down
15 changes: 5 additions & 10 deletions trunk/src/kernel/srs_kernel_mp4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ class SrsMp4SampleDescriptionBox : public SrsMp4FullBox
};

// 8.6.1.2 Decoding Time to Sample Box (stts), for Audio/Video.
// ISO_IEC_14496-12-base-format-2012.pdf, page 48
// ISO_IEC_14496-12-base-format-2012.pdf, page 36
class SrsMp4SttsEntry
{
public:
Expand All @@ -1581,18 +1581,10 @@ class SrsMp4DecodingTime2SampleBox : public SrsMp4FullBox
public:
// An integer that gives the number of entries in the following table.
std::vector<SrsMp4SttsEntry> entries;
private:
// The index for counter to calc the dts for samples.
uint32_t index;
uint32_t count;

public:
SrsMp4DecodingTime2SampleBox();
virtual ~SrsMp4DecodingTime2SampleBox();
public:
// Initialize the counter.
virtual srs_error_t initialize_counter();
// When got an sample, index starts from 0.
virtual srs_error_t on_sample(uint32_t sample_index, SrsMp4SttsEntry** ppentry);
protected:
virtual int nb_header();
virtual srs_error_t encode_header(SrsBuffer* buf);
Expand Down Expand Up @@ -1893,6 +1885,9 @@ class SrsMp4Sample
// The sample data.
uint32_t nb_data;
uint8_t* data;
// number of nalu|audio-frames in this sample.
uint32_t nb_samples;

public:
SrsMp4Sample();
virtual ~SrsMp4Sample();
Expand Down
Loading