1/*
2 * Copyright 2009 castLabs GmbH, Berlin
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 */
16
17package com.coremedia.iso.boxes;
18
19import com.coremedia.iso.IsoTypeReader;
20import com.coremedia.iso.IsoTypeWriter;
21import com.googlecode.mp4parser.AbstractFullBox;
22
23import java.nio.ByteBuffer;
24import java.util.ArrayList;
25import java.util.List;
26
27/**
28 * aligned(8) class SampleDependencyTypeBox
29 * extends FullBox('sdtp', version = 0, 0) {
30 * for (i=0; i < sample_count; i++){
31 * unsigned int(2) reserved = 0;
32 * unsigned int(2) sample_depends_on;
33 * unsigned int(2) sample_is_depended_on;
34 * unsigned int(2) sample_has_redundancy;
35 * }
36 * }
37 */
38public class SampleDependencyTypeBox extends AbstractFullBox {
39    public static final String TYPE = "sdtp";
40
41    private List<Entry> entries = new ArrayList<Entry>();
42
43    public static class Entry {
44
45        public Entry(int value) {
46            this.value = value;
47        }
48
49        private int value;
50
51
52        public int getReserved() {
53            return (value >> 6) & 0x03;
54        }
55
56        public void setReserved(int res) {
57            value = (res & 0x03) << 6 | value & 0x3f;
58        }
59
60        public int getSampleDependsOn() {
61            return (value >> 4) & 0x03;
62        }
63
64        public void setSampleDependsOn(int sdo) {
65            value = (sdo & 0x03) << 4 | value & 0xcf;
66        }
67
68        public int getSampleIsDependentOn() {
69            return (value >> 2) & 0x03;
70        }
71
72        public void setSampleIsDependentOn(int sido) {
73            value = (sido & 0x03) << 2 | value & 0xf3;
74        }
75
76        public int getSampleHasRedundancy() {
77            return value & 0x03;
78        }
79
80        public void setSampleHasRedundancy(int shr) {
81            value = shr & 0x03 | value & 0xfc;
82        }
83
84        @Override
85        public String toString() {
86            return "Entry{" +
87                    "reserved=" + getReserved() +
88                    ", sampleDependsOn=" + getSampleDependsOn() +
89                    ", sampleIsDependentOn=" + getSampleIsDependentOn() +
90                    ", sampleHasRedundancy=" + getSampleHasRedundancy() +
91                    '}';
92        }
93    }
94
95    public SampleDependencyTypeBox() {
96        super(TYPE);
97    }
98
99    @Override
100    protected long getContentSize() {
101        return 4 + entries.size();
102    }
103
104    @Override
105    protected void getContent(ByteBuffer byteBuffer) {
106        writeVersionAndFlags(byteBuffer);
107        for (Entry entry : entries) {
108            IsoTypeWriter.writeUInt8(byteBuffer, entry.value);
109        }
110    }
111
112    @Override
113    public void _parseDetails(ByteBuffer content) {
114        parseVersionAndFlags(content);
115        while (content.remaining() > 0) {
116            entries.add(new Entry(IsoTypeReader.readUInt8(content)));
117        }
118    }
119
120    public List<Entry> getEntries() {
121        return entries;
122    }
123
124    public void setEntries(List<Entry> entries) {
125        this.entries = entries;
126    }
127
128    @Override
129    public String toString() {
130        final StringBuilder sb = new StringBuilder();
131        sb.append("SampleDependencyTypeBox");
132        sb.append("{entries=").append(entries);
133        sb.append('}');
134        return sb.toString();
135    }
136}
137