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.IsoFile;
20import com.coremedia.iso.IsoTypeReader;
21import com.coremedia.iso.IsoTypeWriter;
22import com.googlecode.mp4parser.AbstractFullBox;
23
24import java.nio.ByteBuffer;
25import java.util.LinkedList;
26import java.util.List;
27
28import static com.googlecode.mp4parser.util.CastUtils.l2i;
29
30public class SampleAuxiliaryInformationSizesBox extends AbstractFullBox {
31    public static final String TYPE = "saiz";
32
33    private int defaultSampleInfoSize;
34    private List<Short> sampleInfoSizes = new LinkedList<Short>();
35    private int sampleCount;
36    private String auxInfoType;
37    private String auxInfoTypeParameter;
38
39    public SampleAuxiliaryInformationSizesBox() {
40        super(TYPE);
41    }
42
43    @Override
44    protected long getContentSize() {
45        int size = 4;
46        if ((getFlags() & 1) == 1) {
47            size += 8;
48        }
49
50        size += 5;
51        size += defaultSampleInfoSize == 0 ? sampleInfoSizes.size() : 0;
52        return size;
53    }
54
55    @Override
56    protected void getContent(ByteBuffer byteBuffer) {
57        writeVersionAndFlags(byteBuffer);
58        if ((getFlags() & 1) == 1) {
59            byteBuffer.put(IsoFile.fourCCtoBytes(auxInfoType));
60            byteBuffer.put(IsoFile.fourCCtoBytes(auxInfoTypeParameter));
61        }
62
63        IsoTypeWriter.writeUInt8(byteBuffer, defaultSampleInfoSize);
64
65        if (defaultSampleInfoSize == 0) {
66            IsoTypeWriter.writeUInt32(byteBuffer, sampleInfoSizes.size());
67            for (short sampleInfoSize : sampleInfoSizes) {
68                IsoTypeWriter.writeUInt8(byteBuffer, sampleInfoSize);
69            }
70        } else {
71            IsoTypeWriter.writeUInt32(byteBuffer, sampleCount);
72        }
73    }
74
75    @Override
76    public void _parseDetails(ByteBuffer content) {
77        parseVersionAndFlags(content);
78        if ((getFlags() & 1) == 1) {
79            auxInfoType = IsoTypeReader.read4cc(content);
80            auxInfoTypeParameter = IsoTypeReader.read4cc(content);
81        }
82
83        defaultSampleInfoSize = (short) IsoTypeReader.readUInt8(content);
84        sampleCount = l2i(IsoTypeReader.readUInt32(content));
85
86        sampleInfoSizes.clear();
87
88        if (defaultSampleInfoSize == 0) {
89            for (int i = 0; i < sampleCount; i++) {
90                sampleInfoSizes.add((short) IsoTypeReader.readUInt8(content));
91            }
92        }
93    }
94
95    public String getAuxInfoType() {
96        return auxInfoType;
97    }
98
99    public void setAuxInfoType(String auxInfoType) {
100        this.auxInfoType = auxInfoType;
101    }
102
103    public String getAuxInfoTypeParameter() {
104        return auxInfoTypeParameter;
105    }
106
107    public void setAuxInfoTypeParameter(String auxInfoTypeParameter) {
108        this.auxInfoTypeParameter = auxInfoTypeParameter;
109    }
110
111    public int getDefaultSampleInfoSize() {
112        return defaultSampleInfoSize;
113    }
114
115    public void setDefaultSampleInfoSize(int defaultSampleInfoSize) {
116        assert defaultSampleInfoSize <= 255;
117        this.defaultSampleInfoSize = defaultSampleInfoSize;
118    }
119
120    public List<Short> getSampleInfoSizes() {
121        return sampleInfoSizes;
122    }
123
124    public void setSampleInfoSizes(List<Short> sampleInfoSizes) {
125        this.sampleInfoSizes = sampleInfoSizes;
126    }
127
128    public int getSampleCount() {
129        return sampleCount;
130    }
131
132    public void setSampleCount(int sampleCount) {
133        this.sampleCount = sampleCount;
134    }
135
136    @Override
137    public String toString() {
138        return "SampleAuxiliaryInformationSizesBox{" +
139                "defaultSampleInfoSize=" + defaultSampleInfoSize +
140                ", sampleCount=" + sampleCount +
141                ", auxInfoType='" + auxInfoType + '\'' +
142                ", auxInfoTypeParameter='" + auxInfoTypeParameter + '\'' +
143                '}';
144    }
145}
146