Skip to content

Commit

Permalink
improved avc1 packet grouping.
Browse files Browse the repository at this point in the history
  • Loading branch information
ponchio committed Oct 9, 2017
1 parent 2ea250a commit 8f92eb0
Show file tree
Hide file tree
Showing 5 changed files with 474 additions and 30 deletions.
2 changes: 1 addition & 1 deletion atom.cpp
Expand Up @@ -116,7 +116,7 @@ void Atom::print(int offset) {
//lets just read the first entry
int entries = readInt(4);
cout << indent << " Entries: " << entries << endl;
for(int i = 0; i < entries && i < 10; i++)
for(int i = 0; i < entries && i < 30; i++)
cout << indent << " samples: " << readInt(8 + 8*i) << " for: " << readInt(12 + 8*i) << endl;

} else if(name == string("stss")) { //sync sample: (keyframes)
Expand Down
28 changes: 24 additions & 4 deletions mp4.cpp
Expand Up @@ -175,6 +175,7 @@ void Mp4::saveVideo(string filename) {
Track &track = tracks[i];
track.writeToAtoms();
//convert to movie timescale
cout << "Track duration: " << track.duration << " movie timescale: " << timescale << " track timescale: " << track.timescale << endl;
int track_duration = (int)(double)track.duration * ((double)timescale / (double)track.timescale);
if(track_duration > duration) duration = track_duration;

Expand Down Expand Up @@ -235,7 +236,7 @@ void Mp4::analyze() {
int offset = track.offsets[k] - (mdat->start + 8);
int begin = mdat->readInt(offset);
int next = mdat->readInt(offset + 4);
cout << k << " Size: " << track.sizes[k] << " offset " << track.offsets[k]
cout << "\n" << k << " Size: " << track.sizes[k] << " offset " << track.offsets[k]
<< " begin: " << hex << begin << " " << next << dec << endl;
}

Expand All @@ -251,13 +252,19 @@ void Mp4::analyze() {
<< " begin: " << hex << begin << " " << next << " end: " << end << dec << endl;

bool matches = track.codec.matchSample(start, maxlength);
int length= track.codec.getLength(start, maxlength);
int duration = 0;
int length= track.codec.getLength(start, maxlength, duration);
//TODO check if duration is working with the stts duration.

if(!matches) {
cout << "Match failed! Hit enter for next match." << endl;
getchar();
}
//assert(matches);
cout << "Length: " << length << " true length: " << track.sizes[i] << endl;
if(length != track.sizes[i])
getchar();

//assert(length == track.sizes[i]);

}
Expand Down Expand Up @@ -318,9 +325,14 @@ void Mp4::repair(string filename) {
break;
}


for(unsigned int i = 0; i < tracks.size(); i++)
tracks[i].clear();


//mp4a can be decoded and repors the number of samples (duration in samplerate scale).
//in some videos the duration (stts) can be variable and we can rebuild them using these values.
vector<int> audiotimes;
unsigned long count = 0;
off_t offset = 0;
while(offset < mdat->contentSize()) {
Expand Down Expand Up @@ -354,8 +366,9 @@ void Mp4::repair(string filename) {
for(unsigned int i = 0; i < tracks.size(); i++) {
Track &track = tracks[i];
//sometime audio packets are difficult to match, but if they are the only ones....
int duration =0;
if(tracks.size() > 1 && !track.codec.matchSample(start, maxlength)) continue;
int length = track.codec.getLength(start, maxlength);
int length = track.codec.getLength(start, maxlength, duration);
if(length < -1 || length > 800000) {
cout << endl << "Invalid length. " << length << ". Wrong match in track: " << i << endl;
continue;
Expand All @@ -376,6 +389,9 @@ void Mp4::repair(string filename) {
track.sizes.push_back(length);
offset += length;

if(duration)
audiotimes.push_back(duration);

found = true;
break;
}
Expand All @@ -398,8 +414,12 @@ void Mp4::repair(string filename) {

cout << "Found " << count << " packets\n";

for(unsigned int i = 0; i < tracks.size(); i++)
for(unsigned int i = 0; i < tracks.size(); i++) {
if(audiotimes.size() == tracks[i].offsets.size())
swap(audiotimes, tracks[i].times);

tracks[i].fixTimes();
}

Atom *original_mdat = root->atomByName("mdat");
mdat->start = original_mdat->start;
Expand Down

0 comments on commit 8f92eb0

Please sign in to comment.