Debian Codegraph @

src/com/drew/metadata/mp4/media/Mp4UuidBoxHandler.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.metadata.mp4.media;
22
23 import com.drew.imaging.mp4.Mp4Handler;
24 import com.drew.lang.ByteArrayReader;
25 import com.drew.lang.ByteTrie;
26 import com.drew.lang.SequentialByteArrayReader;
27 import com.drew.lang.SequentialReader;
28 import com.drew.lang.annotations.NotNull;
29 import com.drew.metadata.Metadata;
30 import com.drew.metadata.exif.ExifReader;
31 import com.drew.metadata.iptc.IptcReader;
32 import com.drew.metadata.mp4.Mp4BoxTypes;
33 import com.drew.metadata.mp4.Mp4Context;
34 import com.drew.metadata.photoshop.PhotoshopReader;
35 import com.drew.metadata.xmp.XmpReader;
36
37 import java.io.IOException;
38 import java.nio.ByteBuffer;
39 import java.util.UUID;
40
41 import static com.drew.metadata.mp4.media.Mp4UuidBoxDirectory.TAG_USER_DATA;
42 import static com.drew.metadata.mp4.media.Mp4UuidBoxDirectory.TAG_UUID;
43
44 public class Mp4UuidBoxHandler extends Mp4Handler<Mp4UuidBoxDirectory>
45 {
46 // http://xhelmboyx.tripod.com/formats/mp4-layout.txt
47 // http://fileformats.archiveteam.org/wiki/Boxes/atoms_format#UUID_boxes
48
49 private enum UuidType
50 {
51 Unknown,
52 Exif, // 0537cdab-9d0c-4431-a72a-fa561f2a113e
53 PhotoshopImageResources, // 2c4c0100-8504-40b9-a03e-562148d6dfeb
54 IptcIim, // 33c7a4d2-b81d-4723-a0ba-f1a3e097ad38
55 PiffTrackEncryptionBox, // 8974dbce-7be7-4c51-84f9-7148f9882554
56 GeoJp2WorldFileBox, // 96a9f1f1-dc98-402d-a7ae-d68e34451809
57 PiffSampleEncryptionBox, // a2394f52-5a9b-4f14-a244-6c427c648df4
58 GeoJp2GeoTiffBox, // b14bf8bd-083d-4b43-a5ae-8cd7d5a6ce03
59 Xmp, // be7acfcb-97a9-42e8-9c71-999491e3afac
60 PiffProtectionSystemSpecificHeaderBox, // d08a4f18-10f3-4a82-b6c8-32d8aba183d3
61 }
62
63 private final static ByteTrie<UuidType> _uuidLookup;
64
65 static
66 {
67 _uuidLookup = new ByteTrie<UuidType>();
68 _uuidLookup.setDefaultValue(UuidType.Unknown);
69
70 _uuidLookup.addPath(UuidType.Exif, new byte[] { 0x05, 0x37, (byte)0xcd, (byte)0xab, (byte)0x9d, 0x0c, 0x44, 0x31, (byte)0xa7, 0x2a, (byte)0xfa, 0x56, 0x1f, 0x2a, 0x11, 0x3e });
71 _uuidLookup.addPath(UuidType.PhotoshopImageResources, new byte[] { 0x2c, 0x4c, 0x01, 0x00, (byte)0x85, 0x04, 0x40, (byte)0xb9, (byte)0xa0, 0x3e, 0x56, 0x21, 0x48, (byte)0xd6, (byte)0xdf, (byte)0xeb });
72 _uuidLookup.addPath(UuidType.IptcIim, new byte[] { 0x33, (byte)0xc7, (byte)0xa4, (byte)0xd2, (byte)0xb8, 0x1d, 0x47, 0x23, (byte)0xa0, (byte)0xba, (byte)0xf1, (byte)0xa3, (byte)0xe0, (byte)0x97, (byte)0xad, 0x38 });
73 _uuidLookup.addPath(UuidType.PiffTrackEncryptionBox, new byte[] { (byte)0x89, 0x74, (byte)0xdb, (byte)0xce, 0x7b, (byte)0xe7, 0x4c, 0x51, (byte)0x84, (byte)0xf9, 0x71, 0x48, (byte)0xf9, (byte)0x88, 0x25, 0x54 });
74 _uuidLookup.addPath(UuidType.GeoJp2WorldFileBox, new byte[] { (byte)0x96, (byte)0xa9, (byte)0xf1, (byte)0xf1, (byte)0xdc, (byte)0x98, 0x40, 0x2d, (byte)0xa7, (byte)0xae, (byte)0xd6, (byte)0x8e, 0x34, 0x45, 0x18, 0x09 });
75 _uuidLookup.addPath(UuidType.PiffSampleEncryptionBox, new byte[] { (byte)0xa2, 0x39, 0x4f, 0x52, 0x5a, (byte)0x9b, 0x4f, 0x14, (byte)0xa2, 0x44, 0x6c, 0x42, 0x7c, 0x64, (byte)0x8d, (byte)0xf4 });
76 _uuidLookup.addPath(UuidType.GeoJp2GeoTiffBox, new byte[] { (byte)0xb1, 0x4b, (byte)0xf8, (byte)0xbd, 0x08, 0x3d, 0x4b, 0x43, (byte)0xa5, (byte)0xae, (byte)0x8c, (byte)0xd7, (byte)0xd5, (byte)0xa6, (byte)0xce, 0x03 });
77 _uuidLookup.addPath(UuidType.Xmp, new byte[] { (byte)0xbe, 0x7a, (byte)0xcf, (byte)0xcb, (byte)0x97, (byte)0xa9, 0x42, (byte)0xe8, (byte)0x9c, 0x71, (byte)0x99, (byte)0x94, (byte)0x91, (byte)0xe3, (byte)0xaf, (byte)0xac });
78 _uuidLookup.addPath(UuidType.PiffProtectionSystemSpecificHeaderBox, new byte[] { (byte)0xd0, (byte)0x8a, 0x4f, 0x18, 0x10, (byte)0xf3, 0x4a, (byte)0x82, (byte)0xb6, (byte)0xc8, 0x32, (byte)0xd8, (byte)0xab, (byte)0xa1, (byte)0x83, (byte)0xd3 });
79 }
80
81 public Mp4UuidBoxHandler(Metadata metadata)
82 {
83 super(metadata);
84 }
85
86 @NotNull
87 @Override
88 protected Mp4UuidBoxDirectory getDirectory()
89 {
90 return new Mp4UuidBoxDirectory();
91 }
92
93 @Override
94 protected boolean shouldAcceptBox(@NotNull String type)
95 {
96 return type.equals(Mp4BoxTypes.BOX_USER_DEFINED);
97 }
98
99 @Override
100 protected boolean shouldAcceptContainer(@NotNull String type)
101 {
102 return false;
103 }
104
105 @Override
106 public Mp4Handler<?> processBox(@NotNull String type, byte[] payload, long boxSize, Mp4Context context) throws IOException
107 {
108 if (payload != null && payload.length >= 16) {
109 UuidType uuidType = _uuidLookup.find(payload);
110
111 if (uuidType == null) {
112 return this;
113 }
114
115 switch (uuidType) {
116 case Exif:
117 new ExifReader().extract(new ByteArrayReader(payload, 16), metadata, 0, directory);
118 break;
119 case IptcIim:
120 new IptcReader().extract(new SequentialByteArrayReader(payload, 16), metadata, payload.length - 16, directory);
121 break;
122 case PhotoshopImageResources:
123 new PhotoshopReader().extract(new SequentialByteArrayReader(payload, 16), payload.length - 16, metadata, directory);
124 break;
125 case Xmp:
126 new XmpReader().extract(payload, 16, payload.length - 16, metadata, directory);
127 break;
128 default:
129 SequentialReader reader = new SequentialByteArrayReader(payload);
130 String usertype = getUuid(reader.getBytes(16));
131 byte[] userData = reader.getBytes(reader.available());
132 directory.setString(TAG_UUID, usertype);
133 directory.setByteArray(TAG_USER_DATA, userData);
134 break;
135 }
136 }
137
138 return this;
139 }
140
141 private static String getUuid(byte[] bytes) {
142 ByteBuffer bb = ByteBuffer.wrap(bytes);
143 UUID uuid = new UUID(bb.getLong(), bb.getLong());
144
145 return uuid.toString();
146 }
147 }