| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.metadata.mp4; |
| 22 |
|
| 23 |
import com.drew.imaging.mp4.Mp4Handler; |
| 24 |
import com.drew.lang.DateUtil; |
| 25 |
import com.drew.lang.Rational; |
| 26 |
import com.drew.lang.SequentialByteArrayReader; |
| 27 |
import com.drew.lang.SequentialReader; |
| 28 |
import com.drew.lang.annotations.NotNull; |
| 29 |
import com.drew.lang.annotations.Nullable; |
| 30 |
import com.drew.metadata.Metadata; |
| 31 |
import com.drew.metadata.mp4.media.*; |
| 32 |
|
| 33 |
import java.io.IOException; |
| 34 |
import java.nio.charset.Charset; |
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.regex.Matcher; |
| 37 |
import java.util.regex.Pattern; |
| 38 |
|
| 39 |
import static com.drew.metadata.mp4.Mp4Directory.TAG_LATITUDE; |
| 40 |
import static com.drew.metadata.mp4.Mp4Directory.TAG_LONGITUDE; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class Mp4BoxHandler extends Mp4Handler<Mp4Directory> |
| 46 |
{ |
| 47 |
public Mp4BoxHandler(Metadata metadata) |
| 48 |
{ |
| 49 |
super(metadata); |
| 50 |
} |
| 51 |
|
| 52 |
@NotNull |
| 53 |
@Override |
| 54 |
protected Mp4Directory getDirectory() |
| 55 |
{ |
| 56 |
return new Mp4Directory(); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public boolean shouldAcceptBox(@NotNull String type) |
| 61 |
{ |
| 62 |
return type.equals(Mp4BoxTypes.BOX_FILE_TYPE) |
| 63 |
|| type.equals(Mp4BoxTypes.BOX_MOVIE_HEADER) |
| 64 |
|| type.equals(Mp4BoxTypes.BOX_HANDLER) |
| 65 |
|| type.equals(Mp4BoxTypes.BOX_MEDIA_HEADER) |
| 66 |
|| type.equals(Mp4BoxTypes.BOX_TRACK_HEADER) |
| 67 |
|| type.equals(Mp4BoxTypes.BOX_USER_DATA) |
| 68 |
|| type.equals(Mp4BoxTypes.BOX_USER_DEFINED); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public boolean shouldAcceptContainer(@NotNull String type) |
| 73 |
{ |
| 74 |
return type.equals(Mp4ContainerTypes.BOX_TRACK) |
| 75 |
|| type.equals(Mp4ContainerTypes.BOX_METADATA) |
| 76 |
|| type.equals(Mp4ContainerTypes.BOX_MOVIE) |
| 77 |
|| type.equals(Mp4ContainerTypes.BOX_MEDIA); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public Mp4Handler<?> processBox(@NotNull String type, @Nullable byte[] payload, long boxSize, Mp4Context context) throws IOException |
| 82 |
{ |
| 83 |
if (payload != null) { |
| 84 |
SequentialReader reader = new SequentialByteArrayReader(payload); |
| 85 |
if (type.equals(Mp4BoxTypes.BOX_MOVIE_HEADER)) { |
| 86 |
processMovieHeader(reader); |
| 87 |
} else if (type.equals(Mp4BoxTypes.BOX_FILE_TYPE)) { |
| 88 |
processFileType(reader, boxSize); |
| 89 |
} else if (type.equals(Mp4BoxTypes.BOX_HANDLER)) { |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
reader.skip(4); |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
reader.skip(4); |
| 98 |
String handlerType = reader.getString(4); |
| 99 |
reader.skip(12); |
| 100 |
String name = reader.getNullTerminatedString((int)boxSize - 32, Charset.defaultCharset()); |
| 101 |
|
| 102 |
final String HANDLER_SOUND_MEDIA = "soun"; |
| 103 |
final String HANDLER_VIDEO_MEDIA = "vide"; |
| 104 |
final String HANDLER_HINT_MEDIA = "hint"; |
| 105 |
final String HANDLER_TEXT_MEDIA = "text"; |
| 106 |
final String HANDLER_META_MEDIA = "meta"; |
| 107 |
|
| 108 |
if (handlerType.equals(HANDLER_SOUND_MEDIA)) { |
| 109 |
return new Mp4SoundHandler(metadata, context); |
| 110 |
} else if (handlerType.equals(HANDLER_VIDEO_MEDIA)) { |
| 111 |
return new Mp4VideoHandler(metadata, context); |
| 112 |
} else if (handlerType.equals(HANDLER_HINT_MEDIA)) { |
| 113 |
return new Mp4HintHandler(metadata, context); |
| 114 |
} else if (handlerType.equals(HANDLER_TEXT_MEDIA)) { |
| 115 |
return new Mp4TextHandler(metadata, context); |
| 116 |
} else if (handlerType.equals(HANDLER_META_MEDIA)) { |
| 117 |
return new Mp4MetaHandler(metadata, context); |
| 118 |
} |
| 119 |
return this; |
| 120 |
} else if (type.equals(Mp4BoxTypes.BOX_MEDIA_HEADER)) { |
| 121 |
processMediaHeader(reader, context); |
| 122 |
} else if (type.equals(Mp4BoxTypes.BOX_TRACK_HEADER)) { |
| 123 |
processTrackHeader(reader); |
| 124 |
} else if (type.equals(Mp4BoxTypes.BOX_USER_DEFINED)) { |
| 125 |
Mp4UuidBoxHandler userBoxHandler = new Mp4UuidBoxHandler(metadata); |
| 126 |
userBoxHandler.processBox(type, payload, boxSize, context); |
| 127 |
} else if (type.equals(Mp4BoxTypes.BOX_USER_DATA)) { |
| 128 |
processUserData(reader, payload.length); |
| 129 |
} |
| 130 |
} else { |
| 131 |
if (type.equals(Mp4ContainerTypes.BOX_COMPRESSED_MOVIE)) { |
| 132 |
directory.addError("Compressed MP4 movies not supported"); |
| 133 |
} |
| 134 |
} |
| 135 |
return this; |
| 136 |
} |
| 137 |
|
| 138 |
private static final Pattern COORDINATE_PATTERN = Pattern.compile("([+-]\\d+\\.\\d+)([+-]\\d+\\.\\d+)"); |
| 139 |
|
| 140 |
private void processUserData(@NotNull SequentialReader reader, int length) throws IOException |
| 141 |
{ |
| 142 |
final int LOCATION_CODE = 0xA978797A; |
| 143 |
|
| 144 |
String coordinateString = null; |
| 145 |
|
| 146 |
while (reader.getPosition() < length) { |
| 147 |
long size = reader.getUInt32(); |
| 148 |
if (size <= 4) |
| 149 |
break; |
| 150 |
int kind = reader.getInt32(); |
| 151 |
if (kind == LOCATION_CODE) { |
| 152 |
int xyzLength = reader.getUInt16(); |
| 153 |
reader.skip(2); |
| 154 |
coordinateString = reader.getString(xyzLength, "UTF-8"); |
| 155 |
} else if (size >= 8) { |
| 156 |
reader.skip(size - 8); |
| 157 |
} else { |
| 158 |
return; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if (coordinateString != null) { |
| 163 |
final Matcher matcher = COORDINATE_PATTERN.matcher(coordinateString); |
| 164 |
if (matcher.find()) { |
| 165 |
final double latitude = Double.parseDouble(matcher.group(1)); |
| 166 |
final double longitude = Double.parseDouble(matcher.group(2)); |
| 167 |
|
| 168 |
directory.setDouble(TAG_LATITUDE, latitude); |
| 169 |
directory.setDouble(TAG_LONGITUDE, longitude); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
private void processFileType(@NotNull SequentialReader reader, long boxSize) throws IOException |
| 175 |
{ |
| 176 |
|
| 177 |
|
| 178 |
String majorBrand = reader.getString(4); |
| 179 |
long minorVersion = reader.getUInt32(); |
| 180 |
|
| 181 |
|
| 182 |
ArrayList<String> compatibleBrands = new ArrayList<String>(); |
| 183 |
for (int i = 16; i < boxSize; i += 4) { |
| 184 |
compatibleBrands.add(reader.getString(4)); |
| 185 |
} |
| 186 |
|
| 187 |
directory.setString(Mp4Directory.TAG_MAJOR_BRAND, majorBrand); |
| 188 |
directory.setLong(Mp4Directory.TAG_MINOR_VERSION, minorVersion); |
| 189 |
directory.setStringArray(Mp4Directory.TAG_COMPATIBLE_BRANDS, compatibleBrands.toArray(new String[compatibleBrands.size()])); |
| 190 |
} |
| 191 |
|
| 192 |
private void processMovieHeader(@NotNull SequentialReader reader) throws IOException |
| 193 |
{ |
| 194 |
|
| 195 |
|
| 196 |
short version = reader.getUInt8(); |
| 197 |
|
| 198 |
reader.skip(3); |
| 199 |
|
| 200 |
long creationTime; |
| 201 |
long modificationTime; |
| 202 |
long timescale; |
| 203 |
long duration; |
| 204 |
|
| 205 |
if (version == 1) { |
| 206 |
creationTime = reader.getInt64(); |
| 207 |
modificationTime = reader.getInt64(); |
| 208 |
timescale = reader.getUInt32(); |
| 209 |
duration = reader.getInt64(); |
| 210 |
} else { |
| 211 |
creationTime = reader.getUInt32(); |
| 212 |
modificationTime = reader.getUInt32(); |
| 213 |
timescale = reader.getUInt32(); |
| 214 |
duration = reader.getUInt32(); |
| 215 |
} |
| 216 |
|
| 217 |
int rate = reader.getInt32(); |
| 218 |
int volume = reader.getInt16(); |
| 219 |
reader.skip(2); |
| 220 |
reader.skip(8); |
| 221 |
int[] matrix = new int[]{ |
| 222 |
reader.getInt32(), |
| 223 |
reader.getInt32(), |
| 224 |
reader.getInt32(), |
| 225 |
reader.getInt32(), |
| 226 |
reader.getInt32(), |
| 227 |
reader.getInt32(), |
| 228 |
reader.getInt32(), |
| 229 |
reader.getInt32(), |
| 230 |
reader.getInt32() |
| 231 |
}; |
| 232 |
reader.skip(24); |
| 233 |
long nextTrackID = reader.getUInt32(); |
| 234 |
|
| 235 |
|
| 236 |
directory.setDate(Mp4Directory.TAG_CREATION_TIME, DateUtil.get1Jan1904EpochDate(creationTime)); |
| 237 |
directory.setDate(Mp4Directory.TAG_MODIFICATION_TIME, DateUtil.get1Jan1904EpochDate(modificationTime)); |
| 238 |
|
| 239 |
|
| 240 |
directory.setLong(Mp4Directory.TAG_DURATION, duration); |
| 241 |
directory.setLong(Mp4Directory.TAG_TIME_SCALE, timescale); |
| 242 |
directory.setRational(Mp4Directory.TAG_DURATION_SECONDS, new Rational(duration, timescale)); |
| 243 |
|
| 244 |
directory.setIntArray(Mp4Directory.TAG_TRANSFORMATION_MATRIX, matrix); |
| 245 |
|
| 246 |
|
| 247 |
double preferredRateInteger = (rate & 0xFFFF0000) >> 16; |
| 248 |
double preferredRateFraction = (rate & 0x0000FFFF) / Math.pow(2, 4); |
| 249 |
directory.setDouble(Mp4Directory.TAG_PREFERRED_RATE, preferredRateInteger + preferredRateFraction); |
| 250 |
|
| 251 |
|
| 252 |
double preferredVolumeInteger = (volume & 0xFF00) >> 8; |
| 253 |
double preferredVolumeFraction = (volume & 0x00FF) / Math.pow(2, 2); |
| 254 |
directory.setDouble(Mp4Directory.TAG_PREFERRED_VOLUME, preferredVolumeInteger + preferredVolumeFraction); |
| 255 |
|
| 256 |
directory.setLong(Mp4Directory.TAG_NEXT_TRACK_ID, nextTrackID); |
| 257 |
} |
| 258 |
|
| 259 |
private void processMediaHeader(@NotNull SequentialReader reader, Mp4Context context) throws IOException |
| 260 |
{ |
| 261 |
|
| 262 |
|
| 263 |
int version = reader.getUInt8(); |
| 264 |
|
| 265 |
reader.skip(3); |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
if (version == 1) { |
| 270 |
context.creationTime = reader.getInt64(); |
| 271 |
context.modificationTime = reader.getInt64(); |
| 272 |
context.timeScale = (long)reader.getInt32(); |
| 273 |
context.duration = reader.getInt64(); |
| 274 |
} else { |
| 275 |
context.creationTime = reader.getUInt32(); |
| 276 |
context.modificationTime = reader.getUInt32(); |
| 277 |
context.timeScale = reader.getUInt32(); |
| 278 |
context.duration = reader.getUInt32(); |
| 279 |
} |
| 280 |
|
| 281 |
int languageBits = reader.getInt16(); |
| 282 |
|
| 283 |
context.language = new String(new char[] |
| 284 |
{ |
| 285 |
(char)(((languageBits & 0x7C00) >> 10) + 0x60), |
| 286 |
(char)(((languageBits & 0x03E0) >> 5) + 0x60), |
| 287 |
(char)((languageBits & 0x001F) + 0x60) |
| 288 |
}); |
| 289 |
} |
| 290 |
|
| 291 |
private void processTrackHeader(@NotNull SequentialReader reader) throws IOException |
| 292 |
{ |
| 293 |
|
| 294 |
|
| 295 |
int version = reader.getUInt8(); |
| 296 |
|
| 297 |
reader.skip(3); |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
long creationTime; |
| 302 |
long modificationTime; |
| 303 |
long trackID; |
| 304 |
long duration; |
| 305 |
|
| 306 |
if (version == 1) { |
| 307 |
creationTime = reader.getInt64(); |
| 308 |
modificationTime = reader.getInt64(); |
| 309 |
trackID = reader.getInt32(); |
| 310 |
reader.skip(4); |
| 311 |
duration = reader.getInt64(); |
| 312 |
} else { |
| 313 |
creationTime = reader.getUInt32(); |
| 314 |
modificationTime = reader.getUInt32(); |
| 315 |
trackID = reader.getUInt32(); |
| 316 |
reader.skip(4); |
| 317 |
duration = reader.getUInt32(); |
| 318 |
} |
| 319 |
|
| 320 |
reader.skip(8); |
| 321 |
|
| 322 |
int layer = reader.getInt16(); |
| 323 |
int alternateGroup = reader.getInt16(); |
| 324 |
int volume = reader.getInt16(); |
| 325 |
|
| 326 |
reader.skip(2); |
| 327 |
|
| 328 |
int[] matrix = new int[9]; |
| 329 |
for (int i = 0; i < 9; i++) { |
| 330 |
matrix[i] = reader.getInt32(); |
| 331 |
} |
| 332 |
|
| 333 |
long width = reader.getInt32(); |
| 334 |
long height = reader.getInt32(); |
| 335 |
|
| 336 |
|
| 337 |
if (width != 0 && height != 0 && directory.getDoubleObject(Mp4Directory.TAG_ROTATION) == null) { |
| 338 |
int x = matrix[1] + matrix[4]; |
| 339 |
int y = matrix[0] + matrix[3]; |
| 340 |
double theta = Math.atan2(y, x); |
| 341 |
double degree = Math.toDegrees(theta); |
| 342 |
degree -= 45; |
| 343 |
directory.setDouble(Mp4Directory.TAG_ROTATION, degree); |
| 344 |
} |
| 345 |
} |
| 346 |
} |