Debian Codegraph @

src/com/drew/metadata/mp4/media/Mp4VideoHandler.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.lang.SequentialReader;
24 import com.drew.lang.annotations.NotNull;
25 import com.drew.metadata.Metadata;
26 import com.drew.metadata.mp4.Mp4BoxTypes;
27 import com.drew.metadata.mp4.Mp4Context;
28 import com.drew.metadata.mp4.Mp4Dictionary;
29 import com.drew.metadata.mp4.Mp4MediaHandler;
30
31 import java.io.IOException;
32 import java.util.ArrayList;
33
34 public class Mp4VideoHandler extends Mp4MediaHandler<Mp4VideoDirectory>
35 {
36 public Mp4VideoHandler(Metadata metadata, Mp4Context context)
37 {
38 super(metadata, context);
39 }
40
41 @Override
42 protected String getMediaInformation()
43 {
44 return Mp4BoxTypes.BOX_VIDEO_MEDIA_INFO;
45 }
46
47 @NotNull
48 @Override
49 protected Mp4VideoDirectory getDirectory()
50 {
51 return new Mp4VideoDirectory();
52 }
53
54 @Override
55 public void processSampleDescription(@NotNull SequentialReader reader) throws IOException
56 {
57 // ISO/IED 14496-12:2015 pg.7
58
59 reader.skip(4); // 1 byte header, 3 bytes flags
60
61 // ISO/IED 14496-12:2015 pg.33
62
63 long numberOfEntries = reader.getUInt32();
64 long sampleDescriptionSize = reader.getUInt32();
65 String format = reader.getString(4);
66 reader.skip(6); // Reserved
67 int dataReferenceIndex = reader.getUInt16();
68
69 // ISO/IED 14496-12:2015 pg.156
70
71 int version = reader.getInt16();
72 int revisionLevel = reader.getInt16();
73 String vendor = reader.getString(4);
74 int temporalQuality = reader.getInt32();
75 int spatialQuality = reader.getInt32();
76 int width = reader.getUInt16();
77 int height = reader.getUInt16();
78 long horizresolution = reader.getUInt32();
79 long vertresolution = reader.getUInt32();
80 reader.skip(4); // Reserved
81 int frameCount = reader.getUInt16();
82 String compressorname = reader.getString(32);
83 int depth = reader.getUInt16();
84 reader.skip(2); // Pre-defined
85
86 // TODO review this
87 Mp4Dictionary.setLookup(Mp4VideoDirectory.TAG_COMPRESSION_TYPE, format, directory);
88
89 directory.setInt(Mp4VideoDirectory.TAG_WIDTH, width);
90 directory.setInt(Mp4VideoDirectory.TAG_HEIGHT, height);
91
92 String compressorName = compressorname.trim();
93 if (!compressorName.isEmpty()) {
94 directory.setString(Mp4VideoDirectory.TAG_COMPRESSOR_NAME, compressorName);
95 }
96
97 directory.setInt(Mp4VideoDirectory.TAG_DEPTH, depth);
98
99 // Calculate horizontal res
100 double horizontalInteger = (horizresolution & 0xFFFF0000) >> 16;
101 double horizontalFraction = (horizresolution & 0xFFFF) / Math.pow(2, 4);
102 directory.setDouble(Mp4VideoDirectory.TAG_HORIZONTAL_RESOLUTION, horizontalInteger + horizontalFraction);
103
104 double verticalInteger = (vertresolution & 0xFFFF0000) >> 16;
105 double verticalFraction = (vertresolution & 0xFFFF) / Math.pow(2, 4);
106 directory.setDouble(Mp4VideoDirectory.TAG_VERTICAL_RESOLUTION, verticalInteger + verticalFraction);
107 }
108
109 @Override
110 public void processMediaInformation(@NotNull SequentialReader reader) throws IOException
111 {
112 // ISO/IED 14496-12:2015 pg.7
113
114 reader.skip(4); // 1 byte header, 3 bytes flags
115
116 // ISO/IED 14496-12:2015 pg.155
117
118 int graphicsMode = reader.getUInt16();
119 int[] opcolor = new int[]{reader.getUInt16(), reader.getUInt16(), reader.getUInt16()};
120
121 directory.setIntArray(Mp4VideoDirectory.TAG_OPCOLOR, opcolor);
122 directory.setInt(Mp4VideoDirectory.TAG_GRAPHICS_MODE, graphicsMode);
123 }
124
125 @Override
126 public void processTimeToSample(@NotNull SequentialReader reader, Mp4Context context) throws IOException
127 {
128 // ISO/IED 14496-12:2015 pg.7
129
130 reader.skip(4); // 1 byte header, 3 bytes flags
131
132 // ISO/IED 14496-12:2015 pg.37
133
134 float sampleCount = 0;
135
136 long entryCount = reader.getUInt32();
137
138 for (int i = 0; i < entryCount; i++) {
139 sampleCount += reader.getUInt32();
140 reader.skip(4); // sample delta
141 }
142
143 if (context.timeScale != null && context.duration != null) {
144 float frameRate = (float)context.timeScale / ((float)context.duration / sampleCount);
145 directory.setFloat(Mp4VideoDirectory.TAG_FRAME_RATE, frameRate);
146 }
147 }
148 }