Debian Codegraph @

src/com/drew/metadata/avi/AviRiffHandler.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.avi;
22
23 import com.drew.imaging.riff.RiffHandler;
24 import com.drew.lang.ByteArrayReader;
25 import com.drew.lang.annotations.NotNull;
26 import com.drew.metadata.Metadata;
27
28 import java.io.IOException;
29
30 /**
31 * Implementation of {@link RiffHandler} specialising in AVI support.
32 *
33 * Extracts data from chunk/list types:
34 *
35 * <ul>
36 * <li><code>"avih"</code>: width, height, streams</li>
37 * <li><code>"strh"</code>: frames/second, samples/second, duration, video codec</li>
38 * </ul>
39 *
40 * Sources: http://www.alexander-noe.com/video/documentation/avi.pdf
41 * https://msdn.microsoft.com/en-us/library/ms899422.aspx
42 * https://www.loc.gov/preservation/digital/formats/fdd/fdd000025.shtml
43 *
44 * @author Payton Garland
45 */
46 public class AviRiffHandler implements RiffHandler
47 {
48 @NotNull
49 private final AviDirectory _directory;
50
51 public AviRiffHandler(@NotNull Metadata metadata)
52 {
53 _directory = new AviDirectory();
54 metadata.addDirectory(_directory);
55 }
56
57 public boolean shouldAcceptRiffIdentifier(@NotNull String identifier)
58 {
59 return identifier.equals(AviDirectory.FORMAT);
60 }
61
62 public boolean shouldAcceptChunk(@NotNull String fourCC)
63 {
64 return fourCC.equals(AviDirectory.CHUNK_STREAM_HEADER)
65 || fourCC.equals(AviDirectory.CHUNK_MAIN_HEADER)
66 || fourCC.equals(AviDirectory.CHUNK_DATETIME_ORIGINAL);
67 }
68
69 public boolean shouldAcceptList(@NotNull String fourCC)
70 {
71 return fourCC.equals(AviDirectory.LIST_HEADER)
72 || fourCC.equals(AviDirectory.LIST_STREAM_HEADER)
73 || fourCC.equals(AviDirectory.FORMAT);
74 }
75
76 public void processChunk(@NotNull String fourCC, @NotNull byte[] payload)
77 {
78 try {
79 if (fourCC.equals(AviDirectory.CHUNK_STREAM_HEADER)) {
80 ByteArrayReader reader = new ByteArrayReader(payload);
81 reader.setMotorolaByteOrder(false);
82
83 String fccType = new String(reader.getBytes(0, 4));
84 String fccHandler = new String(reader.getBytes(4, 4));
85 // int dwFlags = reader.getInt32(8);
86 // int wPriority = reader.getInt16(12);
87 // int wLanguage = reader.getInt16(14);
88 // int dwInitialFrames = reader.getInt32(16);
89 float dwScale = reader.getFloat32(20);
90 float dwRate = reader.getFloat32(24);
91 // int dwStart = reader.getInt32(28);
92 int dwLength = reader.getInt32(32);
93 // int dwSuggestedBufferSize = reader.getInt32(36);
94 // int dwQuality = reader.getInt32(40);
95 // int dwSampleSize = reader.getInt32(44);
96 // byte[] rcFrame = reader.getBytes(48, 2);
97
98 if (fccType.equals("vids")) {
99 if (!_directory.containsTag(AviDirectory.TAG_FRAMES_PER_SECOND)) {
100 _directory.setDouble(AviDirectory.TAG_FRAMES_PER_SECOND, (dwRate / dwScale));
101
102 double duration = dwLength / (dwRate / dwScale);
103 int hours = (int) duration / (int) (Math.pow(60, 2));
104 int minutes = ((int) duration / (int) (Math.pow(60, 1))) - (hours * 60);
105 int seconds = (int) Math.round((duration / (Math.pow(60, 0))) - (minutes * 60));
106 String time = String.format("%1$02d:%2$02d:%3$02d", hours, minutes, seconds);
107
108 _directory.setString(AviDirectory.TAG_DURATION, time);
109 _directory.setString(AviDirectory.TAG_VIDEO_CODEC, fccHandler);
110 }
111 } else if (fccType.equals("auds")) {
112 if (!_directory.containsTag(AviDirectory.TAG_SAMPLES_PER_SECOND)) {
113 _directory.setDouble(AviDirectory.TAG_SAMPLES_PER_SECOND, (dwRate / dwScale));
114 }
115 }
116 } else if (fourCC.equals(AviDirectory.CHUNK_MAIN_HEADER)) {
117 ByteArrayReader reader = new ByteArrayReader(payload);
118 reader.setMotorolaByteOrder(false);
119
120 // int dwMicroSecPerFrame = reader.getInt32(0);
121 // int dwMaxBytesPerSec = reader.getInt32(4);
122 // int dwPaddingGranularity = reader.getInt32(8);
123 // int dwFlags = reader.getInt32(12);
124 // int dwTotalFrames = reader.getInt32(16);
125 // int dwInitialFrames = reader.getInt32(20);
126 int dwStreams = reader.getInt32(24);
127 // int dwSuggestedBufferSize = reader.getInt32(28);
128 int dwWidth = reader.getInt32(32);
129 int dwHeight = reader.getInt32(36);
130 // byte[] dwReserved = reader.getBytes(40, 4);
131
132 _directory.setInt(AviDirectory.TAG_WIDTH, dwWidth);
133 _directory.setInt(AviDirectory.TAG_HEIGHT, dwHeight);
134 _directory.setInt(AviDirectory.TAG_STREAMS, dwStreams);
135 } else if (fourCC.equals(AviDirectory.CHUNK_DATETIME_ORIGINAL)) {
136 ByteArrayReader reader = new ByteArrayReader(payload);
137 String str = reader.getString(0, payload.length, "ASCII");
138 if (str.length() == 26 && str.endsWith(String.valueOf(new char[] { 0x0A, 0x00 }))) {
139 // ?0A 00? "New Line" + padded to nearest WORD boundary
140 str = str.substring(0, 24);
141 }
142 _directory.setString(AviDirectory.TAG_DATETIME_ORIGINAL, str);
143 }
144 } catch (IOException ex) {
145 _directory.addError(ex.getMessage());
146 }
147 }
148
149 @Override
150 public void addError(@NotNull String message)
151 {
152 _directory.addError(message);
153 }
154 }