| 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.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 |
|
| 58 |
|
| 59 |
reader.skip(4); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
long numberOfEntries = reader.getUInt32(); |
| 64 |
long sampleDescriptionSize = reader.getUInt32(); |
| 65 |
String format = reader.getString(4); |
| 66 |
reader.skip(6); |
| 67 |
int dataReferenceIndex = reader.getUInt16(); |
| 68 |
|
| 69 |
|
| 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); |
| 81 |
int frameCount = reader.getUInt16(); |
| 82 |
String compressorname = reader.getString(32); |
| 83 |
int depth = reader.getUInt16(); |
| 84 |
reader.skip(2); |
| 85 |
|
| 86 |
|
| 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 |
|
| 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 |
|
| 113 |
|
| 114 |
reader.skip(4); |
| 115 |
|
| 116 |
|
| 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 |
|
| 129 |
|
| 130 |
reader.skip(4); |
| 131 |
|
| 132 |
|
| 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); |
| 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 |
} |