|
| 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.Mp4VideoHandler; |
| 26 | + |
| 27 | +import org.apache.tika.io.EndianUtils; |
| 28 | +import org.apache.tika.metadata.Video; |
| 29 | + |
| 30 | +/** |
| 31 | + * Extends the video track handling with what the base handler does not read |
| 32 | + * from the sample description: the average bitrate from the 'btrt' BitRateBox. |
| 33 | + * See TIKA-4802. |
| 34 | + */ |
| 35 | +class TikaMp4VideoHandler extends Mp4VideoHandler { |
| 36 | + |
| 37 | + /** |
| 38 | + * Fixed size of a VisualSampleEntry (ISO/IEC 14496-12) before its child |
| 39 | + * boxes: the 8 byte box header, 8 bytes of SampleEntry (6 reserved, 2 data |
| 40 | + * reference index) and 70 bytes of visual fields ending with the 32 byte |
| 41 | + * compressor name, the depth and a pre-defined field. |
| 42 | + */ |
| 43 | + private static final int VISUAL_ENTRY_SIZE = 86; |
| 44 | + |
| 45 | + private final org.apache.tika.metadata.Metadata tikaMetadata; |
| 46 | + |
| 47 | + TikaMp4VideoHandler(Metadata metadata, Mp4Context context, |
| 48 | + org.apache.tika.metadata.Metadata tikaMetadata) { |
| 49 | + super(metadata, context); |
| 50 | + this.tikaMetadata = tikaMetadata; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Mp4Handler<?> processBox(String type, byte[] payload, long boxSize, |
| 55 | + Mp4Context context) throws IOException { |
| 56 | + if ("stsd".equals(type) && payload != null) { |
| 57 | + extractFromSampleDescriptions(payload); |
| 58 | + } |
| 59 | + return super.processBox(type, payload, boxSize, context); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Walks the sample description entries: 4 bytes version and flags, a |
| 64 | + * 4 byte entry count, then one sample entry per count, each starting with |
| 65 | + * its own size and format fourcc. |
| 66 | + */ |
| 67 | + private void extractFromSampleDescriptions(byte[] b) { |
| 68 | + if (b.length < 8) { |
| 69 | + return; |
| 70 | + } |
| 71 | + long entryCount = EndianUtils.getUIntBE(b, 4); |
| 72 | + int pos = 8; |
| 73 | + for (long i = 0; i < entryCount && pos + 8 <= b.length; i++) { |
| 74 | + long size = EndianUtils.getUIntBE(b, pos); |
| 75 | + if (size < 16 || size > b.length - pos) { |
| 76 | + break; |
| 77 | + } |
| 78 | + int end = pos + (int) size; |
| 79 | + int bitRate = findBtrtAverageBitRate(b, pos + VISUAL_ENTRY_SIZE, end); |
| 80 | + if (bitRate > 0) { |
| 81 | + tikaMetadata.set(Video.BITRATE, bitRate); |
| 82 | + } |
| 83 | + pos = end; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Scans the child boxes of a sample entry for a 'btrt' BitRateBox and |
| 89 | + * returns its average bitrate, or 0 if there is none. The box body is the |
| 90 | + * decoding buffer size, the maximum bitrate and the average bitrate. |
| 91 | + */ |
| 92 | + private static int findBtrtAverageBitRate(byte[] b, int pos, int end) { |
| 93 | + while (pos >= 0 && pos + 8 <= end) { |
| 94 | + long size = EndianUtils.getUIntBE(b, pos); |
| 95 | + if (size < 8 || size > end - pos) { |
| 96 | + return 0; |
| 97 | + } |
| 98 | + if ("btrt".equals(fourCc(b, pos + 4)) && pos + 20 <= end) { |
| 99 | + long averageBitRate = EndianUtils.getUIntBE(b, pos + 16); |
| 100 | + return averageBitRate > 0 && averageBitRate <= Integer.MAX_VALUE |
| 101 | + ? (int) averageBitRate : 0; |
| 102 | + } |
| 103 | + pos += (int) size; |
| 104 | + } |
| 105 | + return 0; |
| 106 | + } |
| 107 | + |
| 108 | + private static String fourCc(byte[] b, int pos) { |
| 109 | + return new String(b, pos, 4, StandardCharsets.ISO_8859_1); |
| 110 | + } |
| 111 | +} |
0 commit comments