DivideTimeScaleTrack.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.Track;
20import com.googlecode.mp4parser.authoring.TrackMetaData;
21
22import java.nio.ByteBuffer;
23import java.util.ArrayList;
24import java.util.LinkedList;
25import java.util.List;
26
27/**
28 * Changes the timescale of a track by wrapping the track.
29 */
30public class DivideTimeScaleTrack implements Track {
31    Track source;
32    private int timeScaleDivisor;
33
34    public DivideTimeScaleTrack(Track source, int timeScaleDivisor) {
35        this.source = source;
36        this.timeScaleDivisor = timeScaleDivisor;
37    }
38
39    public SampleDescriptionBox getSampleDescriptionBox() {
40        return source.getSampleDescriptionBox();
41    }
42
43    public List<TimeToSampleBox.Entry> getDecodingTimeEntries() {
44        return adjustTts();
45    }
46
47    public List<CompositionTimeToSample.Entry> getCompositionTimeEntries() {
48        return adjustCtts();
49    }
50
51    public long[] getSyncSamples() {
52        return source.getSyncSamples();
53    }
54
55    public List<SampleDependencyTypeBox.Entry> getSampleDependencies() {
56        return source.getSampleDependencies();
57    }
58
59    public TrackMetaData getTrackMetaData() {
60        TrackMetaData trackMetaData = (TrackMetaData) source.getTrackMetaData().clone();
61        trackMetaData.setTimescale(source.getTrackMetaData().getTimescale() / this.timeScaleDivisor);
62        return trackMetaData;
63    }
64
65    public String getHandler() {
66        return source.getHandler();
67    }
68
69    public boolean isEnabled() {
70        return source.isEnabled();
71    }
72
73    public boolean isInMovie() {
74        return source.isInMovie();
75    }
76
77    public boolean isInPreview() {
78        return source.isInPreview();
79    }
80
81    public boolean isInPoster() {
82        return source.isInPoster();
83    }
84
85    public List<ByteBuffer> getSamples() {
86        return source.getSamples();
87    }
88
89
90    List<CompositionTimeToSample.Entry> adjustCtts() {
91        List<CompositionTimeToSample.Entry> origCtts = this.source.getCompositionTimeEntries();
92        if (origCtts != null) {
93            List<CompositionTimeToSample.Entry> entries2 = new ArrayList<CompositionTimeToSample.Entry>(origCtts.size());
94            for (CompositionTimeToSample.Entry entry : origCtts) {
95                entries2.add(new CompositionTimeToSample.Entry(entry.getCount(), entry.getOffset() / timeScaleDivisor));
96            }
97            return entries2;
98        } else {
99            return null;
100        }
101    }
102
103    List<TimeToSampleBox.Entry> adjustTts() {
104        List<TimeToSampleBox.Entry> origTts = source.getDecodingTimeEntries();
105        LinkedList<TimeToSampleBox.Entry> entries2 = new LinkedList<TimeToSampleBox.Entry>();
106        for (TimeToSampleBox.Entry e : origTts) {
107            entries2.add(new TimeToSampleBox.Entry(e.getCount(), e.getDelta() / timeScaleDivisor));
108        }
109        return entries2;
110    }
111
112    public Box getMediaHeaderBox() {
113        return source.getMediaHeaderBox();
114    }
115
116    public SubSampleInformationBox getSubsampleInformationBox() {
117        return source.getSubsampleInformationBox();
118    }
119
120    @Override
121    public String toString() {
122        return "MultiplyTimeScaleTrack{" +
123                "source=" + source +
124                '}';
125    }
126}
127