Debian Codegraph @

src/com/drew/imaging/mp4/Mp4Reader.java

raw symbols SCIP
1 /*
2 * Copyright 2002-2019 Drew Noakes and contributors
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 * More information about this project is available at:
17 *
18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21 package com.drew.imaging.mp4;
22
23 import com.drew.lang.StreamReader;
24 import com.drew.lang.annotations.NotNull;
25 import com.drew.metadata.mp4.Mp4Context;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29
30 /**
31 * @author Payton Garland
32 */
33 public class Mp4Reader
34 {
35 private Mp4Reader() {}
36
37 public static void extract(@NotNull InputStream inputStream, @NotNull Mp4Handler<?> handler)
38 {
39 StreamReader reader = new StreamReader(inputStream);
40 reader.setMotorolaByteOrder(true);
41
42 Mp4Context context = new Mp4Context();
43
44 processBoxes(reader, -1, handler, context);
45 }
46
47 private static void processBoxes(StreamReader reader, long atomEnd, Mp4Handler<?> handler, Mp4Context context)
48 {
49 try {
50 while (atomEnd == -1 || reader.getPosition() < atomEnd) {
51
52 long boxSize = reader.getUInt32();
53
54 String boxType = reader.getString(4);
55
56 boolean isLargeSize = boxSize == 1;
57
58 if (isLargeSize) {
59 boxSize = reader.getInt64();
60 }
61
62 if (boxSize > Integer.MAX_VALUE) {
63 handler.addError("Box size too large.");
64 break;
65 }
66
67 if (boxSize < 8) {
68 handler.addError("Box size too small.");
69 break;
70 }
71
72 // Determine if fourCC is container/atom and process accordingly.
73 // Unknown atoms will be skipped
74
75 if (handler.shouldAcceptContainer(boxType)) {
76 // Recur, to process nested boxes within container box
77 processBoxes(reader, boxSize + reader.getPosition() - 8, handler.processContainer(boxType, boxSize, context), context);
78 } else if (handler.shouldAcceptBox(boxType)) {
79 handler = handler.processBox(boxType, reader.getBytes((int)boxSize - 8), boxSize, context);
80 } else if (isLargeSize) {
81 if (boxSize < 16) {
82 // TODO capture this error in a directory
83 break;
84 }
85 reader.skip(boxSize - 16);
86 } else {
87 reader.skip(boxSize - 8);
88 }
89 }
90 } catch (IOException e) {
91 handler.addError(e.getMessage());
92 }
93 }
94 }