MultiplyTimeScaleTrack.java revision dd9eb897ee7c7b507cbdcf80263bb4b5de6966bf
1/* 2 * Copyright 2012 Sebastian Annies, Hamburg 3 * 4 * Licensed under the Apache License, Version 2.0 (the License); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an AS IS BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package com.googlecode.mp4parser.authoring.tracks; 17 18import com.coremedia.iso.boxes.*; 19import com.googlecode.mp4parser.authoring.Movie; 20import com.googlecode.mp4parser.authoring.Track; 21import com.googlecode.mp4parser.authoring.TrackMetaData; 22 23import java.nio.ByteBuffer; 24import java.util.ArrayList; 25import java.util.LinkedList; 26import java.util.List; 27 28import static com.googlecode.mp4parser.util.CastUtils.l2i; 29import static com.googlecode.mp4parser.util.Math.gcd; 30import static com.googlecode.mp4parser.util.Math.lcm; 31import static java.lang.Math.round; 32 33/** 34 * Changes the timescale of a track by wrapping the track. 35 */ 36public class MultiplyTimeScaleTrack implements Track { 37 Track source; 38 private int timeScaleFactor; 39 40 public MultiplyTimeScaleTrack(Track source, int timeScaleFactor) { 41 this.source = source; 42 this.timeScaleFactor = timeScaleFactor; 43 } 44 45 public SampleDescriptionBox getSampleDescriptionBox() { 46 return source.getSampleDescriptionBox(); 47 } 48 49 public List<TimeToSampleBox.Entry> getDecodingTimeEntries() { 50 return adjustTts(source.getDecodingTimeEntries(), timeScaleFactor); 51 } 52 53 public List<CompositionTimeToSample.Entry> getCompositionTimeEntries() { 54 return adjustCtts(source.getCompositionTimeEntries(), timeScaleFactor); 55 } 56 57 public long[] getSyncSamples() { 58 return source.getSyncSamples(); 59 } 60 61 public List<SampleDependencyTypeBox.Entry> getSampleDependencies() { 62 return source.getSampleDependencies(); 63 } 64 65 public TrackMetaData getTrackMetaData() { 66 TrackMetaData trackMetaData = (TrackMetaData) source.getTrackMetaData().clone(); 67 trackMetaData.setTimescale(source.getTrackMetaData().getTimescale() * this.timeScaleFactor); 68 return trackMetaData; 69 } 70 71 public String getHandler() { 72 return source.getHandler(); 73 } 74 75 public boolean isEnabled() { 76 return source.isEnabled(); 77 } 78 79 public boolean isInMovie() { 80 return source.isInMovie(); 81 } 82 83 public boolean isInPreview() { 84 return source.isInPreview(); 85 } 86 87 public boolean isInPoster() { 88 return source.isInPoster(); 89 } 90 91 public List<ByteBuffer> getSamples() { 92 return source.getSamples(); 93 } 94 95 96 static List<CompositionTimeToSample.Entry> adjustCtts(List<CompositionTimeToSample.Entry> source, int timeScaleFactor) { 97 if (source != null) { 98 List<CompositionTimeToSample.Entry> entries2 = new ArrayList<CompositionTimeToSample.Entry>(source.size()); 99 for (CompositionTimeToSample.Entry entry : source) { 100 entries2.add(new CompositionTimeToSample.Entry(entry.getCount(), timeScaleFactor * entry.getOffset())); 101 } 102 return entries2; 103 } else { 104 return null; 105 } 106 } 107 108 static List<TimeToSampleBox.Entry> adjustTts(List<TimeToSampleBox.Entry> source, int timeScaleFactor) { 109 LinkedList<TimeToSampleBox.Entry> entries2 = new LinkedList<TimeToSampleBox.Entry>(); 110 for (TimeToSampleBox.Entry e : source) { 111 entries2.add(new TimeToSampleBox.Entry(e.getCount(), timeScaleFactor * e.getDelta())); 112 } 113 return entries2; 114 } 115 116 public Box getMediaHeaderBox() { 117 return source.getMediaHeaderBox(); 118 } 119 120 public SubSampleInformationBox getSubsampleInformationBox() { 121 return source.getSubsampleInformationBox(); 122 } 123 124 @Override 125 public String toString() { 126 return "MultiplyTimeScaleTrack{" + 127 "source=" + source + 128 '}'; 129 } 130} 131