|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.parser.mp4; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | + |
| 22 | +import com.drew.imaging.mp4.Mp4Handler; |
| 23 | +import com.drew.metadata.Metadata; |
| 24 | +import com.drew.metadata.mp4.Mp4Context; |
| 25 | +import com.drew.metadata.mp4.media.Mp4SoundHandler; |
| 26 | + |
| 27 | +import org.apache.tika.io.EndianUtils; |
| 28 | +import org.apache.tika.metadata.Audio; |
| 29 | + |
| 30 | +/** |
| 31 | + * Extends the sound track handling with what the base handler does not read |
| 32 | + * from the sample description: DRM protection markers (protected sample entry |
| 33 | + * formats such as 'drms' or 'enca') and the average bitrate from the 'esds' |
| 34 | + * elementary stream descriptor. See TIKA-4779. |
| 35 | + */ |
| 36 | +class TikaMp4SoundHandler extends Mp4SoundHandler { |
| 37 | + |
| 38 | + private final org.apache.tika.metadata.Metadata tikaMetadata; |
| 39 | + |
| 40 | + TikaMp4SoundHandler(Metadata metadata, Mp4Context context, |
| 41 | + org.apache.tika.metadata.Metadata tikaMetadata) { |
| 42 | + super(metadata, context); |
| 43 | + this.tikaMetadata = tikaMetadata; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Mp4Handler<?> processBox(String type, byte[] payload, long boxSize, |
| 48 | + Mp4Context context) throws IOException { |
| 49 | + if ("stsd".equals(type) && payload != null) { |
| 50 | + extractFromSampleDescriptions(payload); |
| 51 | + } |
| 52 | + return super.processBox(type, payload, boxSize, context); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Walks the sample description entries: 4 bytes version and flags, a |
| 57 | + * 4 byte entry count, then one sample entry per count, each starting with |
| 58 | + * its own size and format fourcc. |
| 59 | + */ |
| 60 | + private void extractFromSampleDescriptions(byte[] b) { |
| 61 | + if (b.length < 8) { |
| 62 | + return; |
| 63 | + } |
| 64 | + long entryCount = EndianUtils.getUIntBE(b, 4); |
| 65 | + int pos = 8; |
| 66 | + for (long i = 0; i < entryCount && pos + 8 <= b.length; i++) { |
| 67 | + long size = EndianUtils.getUIntBE(b, pos); |
| 68 | + if (size < 16 || size > b.length - pos) { |
| 69 | + break; |
| 70 | + } |
| 71 | + int end = pos + (int) size; |
| 72 | + String format = fourCc(b, pos + 4); |
| 73 | + //protected streams replace the codec fourcc with a protected |
| 74 | + //entry format: 'drms' (FairPlay) or 'enca' (ISO common encryption) |
| 75 | + if ("drms".equals(format) || "enca".equals(format)) { |
| 76 | + tikaMetadata.set(Audio.HAS_DRM, true); |
| 77 | + } |
| 78 | + if (pos + 18 <= end) { |
| 79 | + //sample entry: 8 byte header, 6 reserved, 2 data ref index, |
| 80 | + //then version-dependent fixed sound fields before child boxes |
| 81 | + int version = EndianUtils.getUShortBE(b, pos + 16); |
| 82 | + int bitRate = findEsdsAverageBitRate(b, pos + soundEntrySize(version), end); |
| 83 | + if (bitRate > 0) { |
| 84 | + tikaMetadata.set(Audio.BITRATE, bitRate); |
| 85 | + } |
| 86 | + } |
| 87 | + pos = end; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Size of the fixed part of a sound sample entry, after which the child |
| 93 | + * boxes start: 36 bytes for version 0, 52 for version 1 (four extra |
| 94 | + * 32-bit QuickTime fields), 72 for version 2. |
| 95 | + */ |
| 96 | + private static int soundEntrySize(int version) { |
| 97 | + if (version == 1) { |
| 98 | + return 52; |
| 99 | + } |
| 100 | + if (version == 2) { |
| 101 | + return 72; |
| 102 | + } |
| 103 | + return 36; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Scans the child boxes of a sample entry for an 'esds' box and returns |
| 108 | + * its average bitrate, or 0 if there is none. QuickTime version 1/2 |
| 109 | + * entries may nest the 'esds' inside a 'wave' extension box. |
| 110 | + */ |
| 111 | + private static int findEsdsAverageBitRate(byte[] b, int pos, int end) { |
| 112 | + while (pos >= 0 && pos + 8 <= end) { |
| 113 | + long size = EndianUtils.getUIntBE(b, pos); |
| 114 | + if (size < 8 || size > end - pos) { |
| 115 | + return 0; |
| 116 | + } |
| 117 | + String type = fourCc(b, pos + 4); |
| 118 | + if ("esds".equals(type)) { |
| 119 | + return readEsdsAverageBitRate(b, pos + 8, pos + (int) size); |
| 120 | + } |
| 121 | + if ("wave".equals(type)) { |
| 122 | + int nested = findEsdsAverageBitRate(b, pos + 8, pos + (int) size); |
| 123 | + if (nested > 0) { |
| 124 | + return nested; |
| 125 | + } |
| 126 | + } |
| 127 | + pos += (int) size; |
| 128 | + } |
| 129 | + return 0; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Extracts the average bitrate from an 'esds' box body, or returns 0 if |
| 134 | + * the descriptors cannot be walked. The chain is an ES_Descriptor (tag |
| 135 | + * 0x03) with three optional fields signalled by its flags byte, followed |
| 136 | + * by a DecoderConfigDescriptor (tag 0x04) whose fixed fields end with the |
| 137 | + * maximum and average bitrates. |
| 138 | + */ |
| 139 | + private static int readEsdsAverageBitRate(byte[] b, int pos, int end) { |
| 140 | + //4 bytes version and flags, then the ES descriptor |
| 141 | + pos += 4; |
| 142 | + if (pos >= end || b[pos] != 0x03) { |
| 143 | + return 0; |
| 144 | + } |
| 145 | + pos = skipDescriptorLength(b, pos + 1); |
| 146 | + if (pos + 3 > end) { |
| 147 | + return 0; |
| 148 | + } |
| 149 | + //ES_ID (2 bytes), then a flags/priority byte announcing the |
| 150 | + //optional stream dependence, URL and OCR fields |
| 151 | + int flags = b[pos + 2] & 0xFF; |
| 152 | + pos += 3; |
| 153 | + if ((flags & 0x80) != 0) { |
| 154 | + pos += 2; |
| 155 | + } |
| 156 | + if ((flags & 0x40) != 0) { |
| 157 | + if (pos >= end) { |
| 158 | + return 0; |
| 159 | + } |
| 160 | + pos += 1 + (b[pos] & 0xFF); |
| 161 | + } |
| 162 | + if ((flags & 0x20) != 0) { |
| 163 | + pos += 2; |
| 164 | + } |
| 165 | + if (pos >= end || b[pos] != 0x04) { |
| 166 | + return 0; |
| 167 | + } |
| 168 | + pos = skipDescriptorLength(b, pos + 1); |
| 169 | + //object type (1), stream type (1), buffer size (3), max bitrate (4) |
| 170 | + pos += 9; |
| 171 | + if (pos + 4 > end) { |
| 172 | + return 0; |
| 173 | + } |
| 174 | + long averageBitRate = EndianUtils.getUIntBE(b, pos); |
| 175 | + return averageBitRate > 0 && averageBitRate <= Integer.MAX_VALUE |
| 176 | + ? (int) averageBitRate : 0; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Skips a descriptor's variable length encoding (bytes with the high bit |
| 181 | + * set continue the length) and returns the position of the payload. |
| 182 | + */ |
| 183 | + private static int skipDescriptorLength(byte[] b, int pos) { |
| 184 | + while (pos < b.length && (b[pos] & 0x80) != 0) { |
| 185 | + pos++; |
| 186 | + } |
| 187 | + return pos + 1; |
| 188 | + } |
| 189 | + |
| 190 | + private static String fourCc(byte[] b, int pos) { |
| 191 | + return new String(b, pos, 4, StandardCharsets.ISO_8859_1); |
| 192 | + } |
| 193 | +} |
0 commit comments