Skip to content

Commit

Permalink
adopt elst entries to new movie's timescale
Browse files Browse the repository at this point in the history
  • Loading branch information
sannies committed Sep 3, 2014
1 parent ae39dc5 commit 5f3882c
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.coremedia.iso.BoxParser;
import com.coremedia.iso.boxes.Box;
import com.coremedia.iso.boxes.Container;
import com.googlecode.mp4parser.util.ByteBufferByteChannel;
import com.googlecode.mp4parser.util.LazyList;
import com.googlecode.mp4parser.util.Logger;

Expand Down Expand Up @@ -128,14 +127,17 @@ public <T extends Box> List<T> getBoxes(Class<T> clazz, boolean recursive) {
}

/**
* Add <code>b</code> to the container and sets the parent correctly.
* Add <code>box</code> to the container and sets the parent correctly. If <code>box</code> is <code>null</code>
* nochange will be performed and no error thrown.
*
* @param b will be added to the container
* @param box will be added to the container
*/
public void addBox(Box b) {
boxes = new ArrayList<Box>(getBoxes());
b.setParent(this);
boxes.add(b);
public void addBox(Box box) {
if (box != null) {
boxes = new ArrayList<Box>(getBoxes());
box.setParent(this);
boxes.add(box);
}
}

public void initContainer(DataSource dataSource, long containerSize, BoxParser boxParser) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

import com.coremedia.iso.boxes.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
*
*/
public abstract class AbstractTrack implements Track {
String name;
List<Edit> edits = new ArrayList<Edit>();

public AbstractTrack(String name) {
this.name = name;
Expand Down Expand Up @@ -57,4 +60,11 @@ public long getDuration() {
public String getName() {
return this.name;
}

public List<Edit> getEdits() {
return Collections.unmodifiableList(edits);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.googlecode.mp4parser.authoring;

/**
* Format agnostic EditListBox.Entry.
*/
public class Edit {
public Edit(long timeScale, long segmentDuration, long mediaTime, double mediaRate) {
this.timeScale = timeScale;
this.segmentDuration = segmentDuration;
this.mediaTime = mediaTime;
this.mediaRate = mediaRate;
}

private long timeScale;
private long segmentDuration;
private long mediaTime;
private double mediaRate;

public long getTimeScale() {
return timeScale;
}

public long getSegmentDuration() {
return segmentDuration;
}

public long getMediaTime() {
return mediaTime;
}

public double getMediaRate() {
return mediaRate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class Mp4TrackImpl extends AbstractTrack {
* Creates a track from a TrackBox and potentially fragments. Use <b>fragements parameter
* only</b> to supply additional fragments that are not located in the main file.
*
* @param name a name for the track for better identification
* @param name a name for the track for better identification
* @param trackBox the <code>TrackBox</code> describing the track.
* @param fragments additional fragments if located in more than a single file
*/
Expand Down Expand Up @@ -83,8 +83,8 @@ public Mp4TrackImpl(String name, TrackBox trackBox, IsoFile... fragments) {
final List<TrackExtendsBox> trackExtendsBoxes = mvex.getBoxes(TrackExtendsBox.class);
for (TrackExtendsBox trex : trackExtendsBoxes) {
if (trex.getTrackId() == trackId) {
List<SubSampleInformationBox> subss = Path.getPaths(((Box)trackBox.getParent()).getParent(), "/moof/traf/subs");
if (subss.size()>0) {
List<SubSampleInformationBox> subss = Path.getPaths(((Box) trackBox.getParent()).getParent(), "/moof/traf/subs");
if (subss.size() > 0) {
subSampleInformationBox = new SubSampleInformationBox();
}
List<Long> syncSampleList = new LinkedList<Long>();
Expand All @@ -97,7 +97,7 @@ public Mp4TrackImpl(String name, TrackBox trackBox, IsoFile... fragments) {


SubSampleInformationBox subs = Path.getPath(traf, "subs");
if (subs!=null) {
if (subs != null) {
long difFromLastFragment = sampleNumber - lastSubsSample - 1;
for (SubSampleInformationBox.SubSampleEntry subSampleEntry : subs.getEntries()) {
SubSampleInformationBox.SubSampleEntry se = new SubSampleInformationBox.SubSampleEntry();
Expand Down Expand Up @@ -196,13 +196,20 @@ public Mp4TrackImpl(String name, TrackBox trackBox, IsoFile... fragments) {
trackMetaData.setWidth(tkhd.getWidth());
trackMetaData.setLayer(tkhd.getLayer());
trackMetaData.setMatrix(tkhd.getMatrix());
trackMetaData.setEditList((EditListBox) Path.getPath(trackBox, "edts/elst"));
EditListBox elst = Path.getPath(trackBox, "edts/elst");
MovieHeaderBox mvhd = Path.getPath(trackBox, "../mvhd");
if (elst != null) {
for (EditListBox.Entry e : elst.getEntries()) {
edits.add(new Edit(mvhd.getTimescale(), e.getSegmentDuration(), e.getMediaTime(), e.getMediaRate()));
}
}

}

public void close() throws IOException {
Container c = trackBox.getParent();
if (c instanceof BasicContainer) {
((BasicContainer)c).close();
((BasicContainer) c).close();
}
for (IsoFile fragment : fragments) {
fragment.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ public interface Track extends Closeable {
* @return the track's name
*/
public String getName();

public List<Edit> getEdits();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.googlecode.mp4parser.authoring;

import com.coremedia.iso.boxes.EditListBox;
import com.googlecode.mp4parser.util.Matrix;

import java.util.Date;
Expand All @@ -42,7 +41,6 @@ public class TrackMetaData implements Cloneable {
* in front of track 0, and so on.
*/
int layer;
private EditListBox editList;

public String getLanguage() {
return language;
Expand Down Expand Up @@ -132,14 +130,6 @@ public void setMatrix(Matrix matrix) {
this.matrix = matrix;
}

public void setEditList(EditListBox editList) {
this.editList = editList;
}

public EditListBox getEditList() {
return editList;
}

public Object clone() {
try {
return super.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.coremedia.iso.boxes.*;
import com.googlecode.mp4parser.BasicContainer;
import com.googlecode.mp4parser.DataSource;
import com.googlecode.mp4parser.authoring.Edit;
import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.Sample;
import com.googlecode.mp4parser.authoring.Track;
Expand Down Expand Up @@ -110,14 +111,14 @@ public Container build(Movie movie) {
}
for (SampleAuxiliaryInformationOffsetsBox saio : sampleAuxiliaryInformationOffsetsBoxes) {
long offset = saio.getSize(); // the calculation is systematically wrong by 4, I don't want to debug why. Just a quick correction --san 14.May.13
offset += 4 + 4 + 4 + 4 + 4 + 24 ;
offset += 4 + 4 + 4 + 4 + 4 + 24;
// size of all header we were missing otherwise (moov, trak, mdia, minf, stbl)
Object b = saio;
do {
Object current = b;
b = ((Box)b).getParent();
b = ((Box) b).getParent();

for (Box box : ((Container)b).getBoxes()) {
for (Box box : ((Container) b).getBoxes()) {
if (box == current) {
break;
}
Expand Down Expand Up @@ -229,14 +230,7 @@ protected TrackBox createTrackBox(Track track, Movie movie, Map<Track, int[]> ch

trackBox.addBox(tkhd);

/*
EditBox edit = new EditBox();
EditListBox editListBox = new EditListBox();
editListBox.setEntries(Collections.singletonList(
new EditListBox.Entry(editListBox, (long) (track.getTrackMetaData().getStartTime() * getTimescale(movie)), -1, 1)));
edit.addBox(editListBox);
trackBox.addBox(edit);
*/
trackBox.addBox(createEdts(track, movie));

MediaBox mdia = new MediaBox();
trackBox.addBox(mdia);
Expand Down Expand Up @@ -284,6 +278,28 @@ protected TrackBox createTrackBox(Track track, Movie movie, Map<Track, int[]> ch
return trackBox;
}

protected Box createEdts(Track track, Movie movie) {
if (track.getEdits() != null && track.getEdits().size() > 0) {
EditListBox elst = new EditListBox();
elst.setVersion(1);
List<EditListBox.Entry> entries = new ArrayList<EditListBox.Entry>();

for (Edit edit : track.getEdits()) {
entries.add(new EditListBox.Entry(elst,
edit.getSegmentDuration() * movie.getTimescale() / edit.getTimeScale(),
edit.getMediaTime() * movie.getTimescale() / edit.getTimeScale(),
edit.getMediaRate()));
}

elst.setEntries(entries);
EditBox edts = new EditBox();
edts.addBox(elst);
return edts;
} else {
return null;
}
}

protected Box createStbl(Track track, Movie movie, Map<Track, int[]> chunks) {
SampleTableBox stbl = new SampleTableBox();

Expand All @@ -305,7 +321,7 @@ protected Box createStbl(Track track, Movie movie, Map<Track, int[]> chunks) {
}

protected void createSubs(Track track, SampleTableBox stbl) {
if (track.getSubsampleInformationBox()!=null) {
if (track.getSubsampleInformationBox() != null) {
stbl.addBox(track.getSubsampleInformationBox());
}
}
Expand Down Expand Up @@ -340,7 +356,7 @@ protected void createCencBoxes(CencEncyprtedTrack track, SampleTableBox stbl, in

for (int i = 0; i < chunkSizes.length; i++) {
offsets[i] = offset;
for (int j = 0; j < chunkSizes[i]; j++){
for (int j = 0; j < chunkSizes[i]; j++) {
offset += sampleEncryptionEntries.get(index++).getSize();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.coremedia.iso.boxes.fragment.*;
import com.googlecode.mp4parser.BasicContainer;
import com.googlecode.mp4parser.DataSource;
import com.googlecode.mp4parser.authoring.Edit;
import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.Sample;
import com.googlecode.mp4parser.authoring.Track;
Expand Down Expand Up @@ -773,7 +774,7 @@ protected Box createTrak(Track track, Movie movie) {
LOG.fine("Creating Track " + track);
TrackBox trackBox = new TrackBox();
trackBox.addBox(createTkhd(movie, track));
final Box edts = createEdts(track, movie);
Box edts = createEdts(track, movie);
if (edts != null) {
trackBox.addBox(edts);
}
Expand All @@ -782,8 +783,19 @@ protected Box createTrak(Track track, Movie movie) {
}

protected Box createEdts(Track track, Movie movie) {
final EditListBox elst = track.getTrackMetaData().getEditList();
if (elst != null) {
if (track.getEdits() != null && track.getEdits().size() > 0) {
EditListBox elst = new EditListBox();
elst.setVersion(1);
List<EditListBox.Entry> entries = new ArrayList<EditListBox.Entry>();

for (Edit edit : track.getEdits()) {
entries.add(new EditListBox.Entry(elst,
edit.getSegmentDuration() * movie.getTimescale() / edit.getTimeScale(),
edit.getMediaTime() * movie.getTimescale() / edit.getTimeScale(),
edit.getMediaRate()));
}

elst.setEntries(entries);
EditBox edts = new EditBox();
edts.addBox(elst);
return edts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.coremedia.iso.boxes.sampleentry.AudioSampleEntry;
import com.coremedia.iso.boxes.sampleentry.VisualSampleEntry;
import com.googlecode.mp4parser.MemoryDataSourceImpl;
import com.googlecode.mp4parser.authoring.Edit;
import com.googlecode.mp4parser.authoring.Sample;
import com.googlecode.mp4parser.authoring.Track;
import com.googlecode.mp4parser.authoring.TrackMetaData;
Expand Down Expand Up @@ -208,4 +209,7 @@ public String getName() {
return "enc(" + source.getName() + ")";
}

public List<Edit> getEdits() {
return source.getEdits();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
*/
package com.googlecode.mp4parser.authoring.tracks;

import com.coremedia.iso.boxes.*;
import com.coremedia.iso.boxes.CompositionTimeToSample;
import com.coremedia.iso.boxes.SampleDependencyTypeBox;
import com.coremedia.iso.boxes.SampleDescriptionBox;
import com.coremedia.iso.boxes.SubSampleInformationBox;
import com.googlecode.mp4parser.authoring.Edit;
import com.googlecode.mp4parser.authoring.Sample;
import com.googlecode.mp4parser.authoring.Track;
import com.googlecode.mp4parser.authoring.TrackMetaData;
Expand Down Expand Up @@ -181,4 +185,8 @@ public String toString() {
public String getName() {
return "timeScale(" + source.getName() + ")";
}

public List<Edit> getEdits() {
return source.getEdits();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.googlecode.mp4parser.authoring.tracks;

import com.coremedia.iso.boxes.*;
import com.googlecode.mp4parser.authoring.Edit;
import com.googlecode.mp4parser.authoring.Sample;
import com.googlecode.mp4parser.authoring.Track;
import com.googlecode.mp4parser.authoring.TrackMetaData;
Expand Down Expand Up @@ -48,7 +49,7 @@ public SampleDescriptionBox getSampleDescriptionBox() {
public long[] getSampleDurations() {
long[] scaled = new long[source.getSampleDurations().length];

LinkedList<TimeToSampleBox.Entry> entries2 = new LinkedList<TimeToSampleBox.Entry>();

for (int i = 0; i < source.getSampleDurations().length; i++) {
scaled[i] = source.getSampleDurations()[i] / timeScaleDivisor;
}
Expand Down Expand Up @@ -118,4 +119,8 @@ public String toString() {
public String getName() {
return "timscale(" + source.getName() + ")";
}

public List<Edit> getEdits() {
return source.getEdits();
}
}
Loading

0 comments on commit 5f3882c

Please sign in to comment.