MetaBox.java revision dd9eb897ee7c7b507cbdcf80263bb4b5de6966bf
1/*
2 * Copyright 2008 CoreMedia AG, 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 */
16
17package com.coremedia.iso.boxes;
18
19import com.coremedia.iso.IsoTypeReader;
20import com.coremedia.iso.IsoTypeWriter;
21import com.googlecode.mp4parser.AbstractContainerBox;
22import com.googlecode.mp4parser.util.ByteBufferByteChannel;
23
24import java.io.IOException;
25import java.nio.ByteBuffer;
26
27
28/**
29 * A common base structure to contain general metadata. See ISO/IEC 14496-12 Ch. 8.44.1.
30 */
31public class MetaBox extends AbstractContainerBox {
32    private int version = 0;
33    private int flags = 0;
34
35    public static final String TYPE = "meta";
36
37    public MetaBox() {
38        super(TYPE);
39    }
40
41    @Override
42    public long getContentSize() {
43        if (isMp4Box()) {
44            // it's a fullbox
45            return 4 + super.getContentSize();
46        } else {
47            // it's an apple metabox
48            return super.getContentSize();
49        }
50    }
51
52    @Override
53    public long getNumOfBytesToFirstChild() {
54        if (isMp4Box()) {
55            // it's a fullbox
56            return 12;
57        } else {
58            // it's an apple metabox
59            return 8;
60        }
61    }
62
63    @Override
64    public void _parseDetails(ByteBuffer content) {
65        int pos = content.position();
66        content.get(new byte[4]);
67        String isHdlr = IsoTypeReader.read4cc(content);
68        if ("hdlr".equals(isHdlr)) {
69            //  this is apple bullshit - it's NO FULLBOX
70            content.position(pos);
71            version = -1;
72            flags = -1;
73        } else {
74            content.position(pos);
75            version = IsoTypeReader.readUInt8(content);
76            flags = IsoTypeReader.readUInt24(content);
77        }
78        while (content.remaining() >= 8) {
79            try {
80                boxes.add(boxParser.parseBox(new ByteBufferByteChannel(content), this));
81            } catch (IOException e) {
82                throw new RuntimeException("Sebastian needs to fix 7518765283");
83            }
84        }
85        if (content.remaining() > 0) {
86            throw new RuntimeException("Sebastian needs to fix it 90732r26537");
87        }
88    }
89
90    @Override
91    protected void getContent(ByteBuffer byteBuffer) {
92        if (isMp4Box()) {
93            IsoTypeWriter.writeUInt8(byteBuffer, version);
94            IsoTypeWriter.writeUInt24(byteBuffer, flags);
95        }
96        writeChildBoxes(byteBuffer);
97    }
98
99
100    public boolean isMp4Box() {
101        return version != -1 && flags != -1;
102    }
103
104    public void setMp4Box(boolean mp4) {
105        if (mp4) {
106            version = 0;
107            flags = 0;
108        } else {
109            version = -1;
110            flags = -1;
111        }
112    }
113}
114