/* * Copyright 2012 Sebastian Annies, Hamburg * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.mp4parser.authoring.builder; import com.coremedia.iso.BoxParser; import com.coremedia.iso.IsoFile; import com.coremedia.iso.IsoTypeWriter; import com.coremedia.iso.boxes.*; import com.googlecode.mp4parser.BasicContainer; import com.googlecode.mp4parser.DataSource; import com.googlecode.mp4parser.authoring.Movie; import com.googlecode.mp4parser.authoring.Sample; import com.googlecode.mp4parser.authoring.Track; import com.googlecode.mp4parser.authoring.tracks.CencEncyprtedTrack; import com.googlecode.mp4parser.boxes.cenc.CencSampleAuxiliaryDataFormat; import com.googlecode.mp4parser.boxes.ultraviolet.SampleEncryptionBox; import com.googlecode.mp4parser.util.Path; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.WritableByteChannel; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; import static com.googlecode.mp4parser.util.CastUtils.l2i; /** * Creates a plain MP4 file from a video. Plain as plain can be. */ public class DefaultMp4Builder implements Mp4Builder { Set<StaticChunkOffsetBox> chunkOffsetBoxes = new HashSet<StaticChunkOffsetBox>(); Set<SampleAuxiliaryInformationOffsetsBox> sampleAuxiliaryInformationOffsetsBoxes = new HashSet<SampleAuxiliaryInformationOffsetsBox>(); private static Logger LOG = Logger.getLogger(DefaultMp4Builder.class.getName()); HashMap<Track, List<Sample>> track2Sample = new HashMap<Track, List<Sample>>(); HashMap<Track, long[]> track2SampleSizes = new HashMap<Track, long[]>(); private FragmentIntersectionFinder intersectionFinder; public void setIntersectionFinder(FragmentIntersectionFinder intersectionFinder) { this.intersectionFinder = intersectionFinder; } /** * {@inheritDoc} */ public Container build(Movie movie) { if (intersectionFinder == null) { intersectionFinder = new TwoSecondIntersectionFinder(movie, 2); } LOG.fine("Creating movie " + movie); for (Track track : movie.getTracks()) { // getting the samples may be a time consuming activity List<Sample> samples = track.getSamples(); putSamples(track, samples); long[] sizes = new long[samples.size()]; for (int i = 0; i < sizes.length; i++) { Sample b = samples.get(i); sizes[i] = b.getSize(); } track2SampleSizes.put(track, sizes); } BasicContainer isoFile = new BasicContainer(); isoFile.addBox(createFileTypeBox(movie)); Map<Track, int[]> chunks = new HashMap<Track, int[]>(); for (Track track : movie.getTracks()) { chunks.put(track, getChunkSizes(track, movie)); } Box moov = createMovieBox(movie, chunks); isoFile.addBox(moov); List<Box> stszs = Path.getPaths(moov, "trak/mdia/minf/stbl/stsz"); long contentSize = 0; for (Box stsz : stszs) { contentSize += sum(((SampleSizeBox) stsz).getSampleSizes()); } InterleaveChunkMdat mdat = new InterleaveChunkMdat(movie, chunks, contentSize); isoFile.addBox(mdat); /* dataOffset is where the first sample starts. In this special mdat the samples always start at offset 16 so that we can use the same offset for large boxes and small boxes */ long dataOffset = mdat.getDataOffset(); for (StaticChunkOffsetBox chunkOffsetBox : chunkOffsetBoxes) { long[] offsets = chunkOffsetBox.getChunkOffsets(); for (int i = 0; i < offsets.length; i++) { offsets[i] += dataOffset; } } 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 ; // size of all header we were missing otherwise (moov, trak, mdia, minf, stbl) Object b = saio; do { Object current = b; b = ((Box)b).getParent(); for (Box box : ((Container)b).getBoxes()) { if (box == current) { break; } offset += box.getSize(); } } while (b instanceof Box); long[] saioOffsets = saio.getOffsets(); for (int i = 0; i < saioOffsets.length; i++) { saioOffsets[i] = saioOffsets[i] + offset; } saio.setOffsets(saioOffsets); } return isoFile; } protected List<Sample> putSamples(Track track, List<Sample> samples) { return track2Sample.put(track, samples); } protected FileTypeBox createFileTypeBox(Movie movie) { List<String> minorBrands = new LinkedList<String>(); minorBrands.add("isom"); minorBrands.add("iso2"); minorBrands.add("avc1"); return new FileTypeBox("isom", 0, minorBrands); } protected MovieBox createMovieBox(Movie movie, Map<Track, int[]> chunks) { MovieBox movieBox = new MovieBox(); MovieHeaderBox mvhd = new MovieHeaderBox(); mvhd.setCreationTime(new Date()); mvhd.setModificationTime(new Date()); mvhd.setMatrix(movie.getMatrix()); long movieTimeScale = getTimescale(movie); long duration = 0; for (Track track : movie.getTracks()) { long tracksDuration = track.getDuration() * movieTimeScale / track.getTrackMetaData().getTimescale(); if (tracksDuration > duration) { duration = tracksDuration; } } mvhd.setDuration(duration); mvhd.setTimescale(movieTimeScale); // find the next available trackId long nextTrackId = 0; for (Track track : movie.getTracks()) { nextTrackId = nextTrackId < track.getTrackMetaData().getTrackId() ? track.getTrackMetaData().getTrackId() : nextTrackId; } mvhd.setNextTrackId(++nextTrackId); movieBox.addBox(mvhd); for (Track track : movie.getTracks()) { movieBox.addBox(createTrackBox(track, movie, chunks)); } // metadata here Box udta = createUdta(movie); if (udta != null) { movieBox.addBox(udta); } return movieBox; } /** * Override to create a user data box that may contain metadata. * * @param movie source movie * @return a 'udta' box or <code>null</code> if none provided */ protected Box createUdta(Movie movie) { return null; } protected TrackBox createTrackBox(Track track, Movie movie, Map<Track, int[]> chunks) { TrackBox trackBox = new TrackBox(); TrackHeaderBox tkhd = new TrackHeaderBox(); tkhd.setEnabled(true); tkhd.setInMovie(true); tkhd.setInPreview(true); tkhd.setInPoster(true); tkhd.setMatrix(track.getTrackMetaData().getMatrix()); tkhd.setAlternateGroup(track.getTrackMetaData().getGroup()); tkhd.setCreationTime(track.getTrackMetaData().getCreationTime()); // We need to take edit list box into account in trackheader duration // but as long as I don't support edit list boxes it is sufficient to // just translate media duration to movie timescale tkhd.setDuration(track.getDuration() * getTimescale(movie) / track.getTrackMetaData().getTimescale()); tkhd.setHeight(track.getTrackMetaData().getHeight()); tkhd.setWidth(track.getTrackMetaData().getWidth()); tkhd.setLayer(track.getTrackMetaData().getLayer()); tkhd.setModificationTime(new Date()); tkhd.setTrackId(track.getTrackMetaData().getTrackId()); tkhd.setVolume(track.getTrackMetaData().getVolume()); 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); */ MediaBox mdia = new MediaBox(); trackBox.addBox(mdia); MediaHeaderBox mdhd = new MediaHeaderBox(); mdhd.setCreationTime(track.getTrackMetaData().getCreationTime()); mdhd.setDuration(track.getDuration()); mdhd.setTimescale(track.getTrackMetaData().getTimescale()); mdhd.setLanguage(track.getTrackMetaData().getLanguage()); mdia.addBox(mdhd); HandlerBox hdlr = new HandlerBox(); mdia.addBox(hdlr); hdlr.setHandlerType(track.getHandler()); MediaInformationBox minf = new MediaInformationBox(); minf.addBox(track.getMediaHeaderBox()); // dinf: all these three boxes tell us is that the actual // data is in the current file and not somewhere external DataInformationBox dinf = new DataInformationBox(); DataReferenceBox dref = new DataReferenceBox(); dinf.addBox(dref); DataEntryUrlBox url = new DataEntryUrlBox(); url.setFlags(1); dref.addBox(url); minf.addBox(dinf); // Box stbl = createStbl(track, movie, chunks); minf.addBox(stbl); mdia.addBox(minf); return trackBox; } protected Box createStbl(Track track, Movie movie, Map<Track, int[]> chunks) { SampleTableBox stbl = new SampleTableBox(); createStsd(track, stbl); createStts(track, stbl); createCtts(track, stbl); createStss(track, stbl); createSdtp(track, stbl); createStsc(track, chunks, stbl); createStsz(track, stbl); createStco(track, movie, chunks, stbl); if (track instanceof CencEncyprtedTrack) { createCencBoxes((CencEncyprtedTrack) track, stbl, chunks.get(track)); } return stbl; } protected void createCencBoxes(CencEncyprtedTrack track, SampleTableBox stbl, int[] chunkSizes) { SampleAuxiliaryInformationSizesBox saiz = new SampleAuxiliaryInformationSizesBox(); saiz.setAuxInfoType("cenc"); saiz.setFlags(1); List<CencSampleAuxiliaryDataFormat> sampleEncryptionEntries = track.getSampleEncryptionEntries(); if (track.hasSubSampleEncryption()) { short[] sizes = new short[sampleEncryptionEntries.size()]; for (int i = 0; i < sizes.length; i++) { sizes[i] = (short) sampleEncryptionEntries.get(i).getSize(); } saiz.setSampleInfoSizes(sizes); } else { saiz.setDefaultSampleInfoSize(8); // 8 bytes iv saiz.setSampleCount(track.getSamples().size()); } SampleAuxiliaryInformationOffsetsBox saio = new SampleAuxiliaryInformationOffsetsBox(); SampleEncryptionBox senc = new SampleEncryptionBox(); senc.setSubSampleEncryption(track.hasSubSampleEncryption()); senc.setEntries(sampleEncryptionEntries); long offset = senc.getOffsetToFirstIV(); int index = 0; long[] offsets = new long[track.getSamples().size()]; for (int i = 0; i < chunkSizes.length; i++) { offsets[i] = offset; for (int j = 0; j < chunkSizes[i]; j++){ offset += sampleEncryptionEntries.get(index++).getSize(); } } saio.setOffsets(offsets); stbl.addBox(saiz); stbl.addBox(saio); stbl.addBox(senc); sampleAuxiliaryInformationOffsetsBoxes.add(saio); } protected void createStsd(Track track, SampleTableBox stbl) { stbl.addBox(track.getSampleDescriptionBox()); } protected void createStco(Track track, Movie movie, Map<Track, int[]> chunks, SampleTableBox stbl) { int[] tracksChunkSizes = chunks.get(track); // The ChunkOffsetBox we create here is just a stub // since we haven't created the whole structure we can't tell where the // first chunk starts (mdat box). So I just let the chunk offset // start at zero and I will add the mdat offset later. StaticChunkOffsetBox stco = new StaticChunkOffsetBox(); this.chunkOffsetBoxes.add(stco); long offset = 0; long[] chunkOffset = new long[tracksChunkSizes.length]; // all tracks have the same number of chunks if (LOG.isLoggable(Level.FINE)) { LOG.fine("Calculating chunk offsets for track_" + track.getTrackMetaData().getTrackId()); } for (int i = 0; i < tracksChunkSizes.length; i++) { // The filelayout will be: // chunk_1_track_1,... ,chunk_1_track_n, chunk_2_track_1,... ,chunk_2_track_n, ... , chunk_m_track_1,... ,chunk_m_track_n // calculating the offsets if (LOG.isLoggable(Level.FINER)) { LOG.finer("Calculating chunk offsets for track_" + track.getTrackMetaData().getTrackId() + " chunk " + i); } for (Track current : movie.getTracks()) { if (LOG.isLoggable(Level.FINEST)) { LOG.finest("Adding offsets of track_" + current.getTrackMetaData().getTrackId()); } int[] chunkSizes = chunks.get(current); long firstSampleOfChunk = 0; for (int j = 0; j < i; j++) { firstSampleOfChunk += chunkSizes[j]; } if (current == track) { chunkOffset[i] = offset; } for (int j = l2i(firstSampleOfChunk); j < firstSampleOfChunk + chunkSizes[i]; j++) { offset += track2SampleSizes.get(current)[j]; } } } stco.setChunkOffsets(chunkOffset); stbl.addBox(stco); } protected void createStsz(Track track, SampleTableBox stbl) { SampleSizeBox stsz = new SampleSizeBox(); stsz.setSampleSizes(track2SampleSizes.get(track)); stbl.addBox(stsz); } protected void createStsc(Track track, Map<Track, int[]> chunks, SampleTableBox stbl) { int[] tracksChunkSizes = chunks.get(track); SampleToChunkBox stsc = new SampleToChunkBox(); stsc.setEntries(new LinkedList<SampleToChunkBox.Entry>()); long lastChunkSize = Integer.MIN_VALUE; // to be sure the first chunks hasn't got the same size for (int i = 0; i < tracksChunkSizes.length; i++) { // The sample description index references the sample description box // that describes the samples of this chunk. My Tracks cannot have more // than one sample description box. Therefore 1 is always right // the first chunk has the number '1' if (lastChunkSize != tracksChunkSizes[i]) { stsc.getEntries().add(new SampleToChunkBox.Entry(i + 1, tracksChunkSizes[i], 1)); lastChunkSize = tracksChunkSizes[i]; } } stbl.addBox(stsc); } protected void createSdtp(Track track, SampleTableBox stbl) { if (track.getSampleDependencies() != null && !track.getSampleDependencies().isEmpty()) { SampleDependencyTypeBox sdtp = new SampleDependencyTypeBox(); sdtp.setEntries(track.getSampleDependencies()); stbl.addBox(sdtp); } } protected void createStss(Track track, SampleTableBox stbl) { long[] syncSamples = track.getSyncSamples(); if (syncSamples != null && syncSamples.length > 0) { SyncSampleBox stss = new SyncSampleBox(); stss.setSampleNumber(syncSamples); stbl.addBox(stss); } } protected void createCtts(Track track, SampleTableBox stbl) { List<CompositionTimeToSample.Entry> compositionTimeToSampleEntries = track.getCompositionTimeEntries(); if (compositionTimeToSampleEntries != null && !compositionTimeToSampleEntries.isEmpty()) { CompositionTimeToSample ctts = new CompositionTimeToSample(); ctts.setEntries(compositionTimeToSampleEntries); stbl.addBox(ctts); } } protected void createStts(Track track, SampleTableBox stbl) { TimeToSampleBox.Entry lastEntry = null; List<TimeToSampleBox.Entry> entries = new ArrayList<TimeToSampleBox.Entry>(); for (long delta : track.getSampleDurations()) { if (lastEntry != null && lastEntry.getDelta() == delta) { lastEntry.setCount(lastEntry.getCount() + 1); } else { lastEntry = new TimeToSampleBox.Entry(1, delta); entries.add(lastEntry); } } TimeToSampleBox stts = new TimeToSampleBox(); stts.setEntries(entries); stbl.addBox(stts); } private class InterleaveChunkMdat implements Box { List<Track> tracks; List<List<Sample>> chunkList = new ArrayList<List<Sample>>(); Container parent; long contentSize; public Container getParent() { return parent; } public long getOffset() { throw new RuntimeException("Doesn't have any meaning for programmatically created boxes"); } public void setParent(Container parent) { this.parent = parent; } public void parse(DataSource dataSource, ByteBuffer header, long contentSize, BoxParser boxParser) throws IOException { } private InterleaveChunkMdat(Movie movie, Map<Track, int[]> chunks, long contentSize) { this.contentSize = contentSize; this.tracks = movie.getTracks(); for (int i = 0; i < chunks.values().iterator().next().length; i++) { for (Track track : tracks) { int[] chunkSizes = chunks.get(track); long firstSampleOfChunk = 0; for (int j = 0; j < i; j++) { firstSampleOfChunk += chunkSizes[j]; } List<Sample> chunk = DefaultMp4Builder.this.track2Sample.get(track).subList(l2i(firstSampleOfChunk), l2i(firstSampleOfChunk + chunkSizes[i])); chunkList.add(chunk); } } } public long getDataOffset() { Object b = this; long offset = 16; while (b instanceof Box) { for (Box box : ((Box) b).getParent().getBoxes()) { if (b == box) { break; } offset += box.getSize(); } b = ((Box) b).getParent(); } return offset; } public String getType() { return "mdat"; } public long getSize() { return 16 + contentSize; } private boolean isSmallBox(long contentSize) { return (contentSize + 8) < 4294967296L; } public void getBox(WritableByteChannel writableByteChannel) throws IOException { ByteBuffer bb = ByteBuffer.allocate(16); long size = getSize(); if (isSmallBox(size)) { IsoTypeWriter.writeUInt32(bb, size); } else { IsoTypeWriter.writeUInt32(bb, 1); } bb.put(IsoFile.fourCCtoBytes("mdat")); if (isSmallBox(size)) { bb.put(new byte[8]); } else { IsoTypeWriter.writeUInt64(bb, size); } bb.rewind(); writableByteChannel.write(bb); for (List<Sample> samples : chunkList) { for (Sample sample : samples) { sample.writeTo(writableByteChannel); } } } } /** * Gets the chunk sizes for the given track. * * @param track * @param movie * @return */ int[] getChunkSizes(Track track, Movie movie) { long[] referenceChunkStarts = intersectionFinder.sampleNumbers(track); int[] chunkSizes = new int[referenceChunkStarts.length]; for (int i = 0; i < referenceChunkStarts.length; i++) { long start = referenceChunkStarts[i] - 1; long end; if (referenceChunkStarts.length == i + 1) { end = track.getSamples().size(); } else { end = referenceChunkStarts[i + 1] - 1; } chunkSizes[i] = l2i(end - start); // The Stretch makes sure that there are as much audio and video chunks! } assert DefaultMp4Builder.this.track2Sample.get(track).size() == sum(chunkSizes) : "The number of samples and the sum of all chunk lengths must be equal"; return chunkSizes; } private static long sum(int[] ls) { long rc = 0; for (long l : ls) { rc += l; } return rc; } private static long sum(long[] ls) { long rc = 0; for (long l : ls) { rc += l; } return rc; } public long getTimescale(Movie movie) { long timescale = movie.getTracks().iterator().next().getTrackMetaData().getTimescale(); for (Track track : movie.getTracks()) { timescale = gcd(track.getTrackMetaData().getTimescale(), timescale); } return timescale; } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } }