1/*
2 * Copyright 2011 castLabs, 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.googlecode.mp4parser.boxes.mp4.objectdescriptors;
18
19import com.coremedia.iso.IsoTypeReader;
20
21import java.io.IOException;
22import java.nio.ByteBuffer;
23
24/*
25abstract aligned(8) expandable(228-1) class BaseDescriptor : bit(8) tag=0 {
26// empty. To be filled by classes extending this class.
27}
28
29int sizeOfInstance = 0;
30bit(1) nextByte;
31bit(7) sizeOfInstance;
32while(nextByte) {
33bit(1) nextByte;
34bit(7) sizeByte;
35sizeOfInstance = sizeOfInstance<<7 | sizeByte;
36}
37 */
38@Descriptor(tags = 0x00)
39public abstract class BaseDescriptor {
40    int tag;
41    int sizeOfInstance;
42    int sizeBytes;
43
44    public BaseDescriptor() {
45    }
46
47    public int getTag() {
48        return tag;
49    }
50
51    public int getSize() {
52        return sizeOfInstance
53                + 1//1 for the tag
54                + sizeBytes;
55    }
56
57    public int getSizeOfInstance() {
58        return sizeOfInstance;
59    }
60
61    public int getSizeBytes() {
62        return sizeBytes;
63    }
64
65    public final void parse(int tag, ByteBuffer bb) throws IOException {
66        this.tag = tag;
67
68        int i = 0;
69        int tmp = IsoTypeReader.readUInt8(bb);
70        i++;
71        sizeOfInstance = tmp & 0x7f;
72        while (tmp >>> 7 == 1) { //nextbyte indicator bit
73            tmp = IsoTypeReader.readUInt8(bb);
74            i++;
75            //sizeOfInstance = sizeOfInstance<<7 | sizeByte;
76            sizeOfInstance = sizeOfInstance << 7 | tmp & 0x7f;
77        }
78        sizeBytes = i;
79        ByteBuffer detailSource = bb.slice();
80        detailSource.limit(sizeOfInstance);
81        parseDetail(detailSource);
82        assert detailSource.remaining() == 0: this.getClass().getSimpleName() + " has not been fully parsed";
83        bb.position(bb.position() + sizeOfInstance);
84    }
85
86    public abstract void parseDetail(ByteBuffer bb) throws IOException;
87
88
89
90    @Override
91    public String toString() {
92        final StringBuilder sb = new StringBuilder();
93        sb.append("BaseDescriptor");
94        sb.append("{tag=").append(tag);
95        sb.append(", sizeOfInstance=").append(sizeOfInstance);
96        sb.append('}');
97        return sb.toString();
98    }
99}
100