| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 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 |
|
| 73 |
|
| 74 |
|
| 75 |
if (handler.shouldAcceptContainer(boxType)) { |
| 76 |
|
| 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 |
|
| 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 |
} |