Debian Codegraph @

src/com/drew/metadata/mp3/Mp3Reader.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.mp3;
22
23 import com.drew.imaging.ImageProcessingException;
24 import com.drew.lang.SequentialReader;
25 import com.drew.lang.StreamReader;
26 import com.drew.lang.annotations.NotNull;
27 import com.drew.metadata.Metadata;
28
29 import java.io.IOException;
30 import java.io.InputStream;
31
32 /**
33 * Sources: http://id3.org/mp3Frame
34 * https://www.loc.gov/preservation/digital/formats/fdd/fdd000105.shtml
35 *
36 * @author Payton Garland
37 */
38 public class Mp3Reader
39 {
40 public void extract(@NotNull final InputStream inputStream, @NotNull final Metadata metadata)
41 {
42 Mp3Directory directory = new Mp3Directory();
43 metadata.addDirectory(directory);
44
45 try {
46 SequentialReader reader = new StreamReader(inputStream);
47
48 int header = reader.getInt32();
49
50 // ID: MPEG-2.5, MPEG-2, or MPEG-1
51 int id = 0;
52 switch ((header & 0x000180000) >> 19) {
53 case 0:
54 throw new ImageProcessingException("MPEG-2.5 not supported.");
55 case 2:
56 directory.setString(Mp3Directory.TAG_ID, "MPEG-2");
57 id = 2;
58 break;
59 case 3:
60 directory.setString(Mp3Directory.TAG_ID, "MPEG-1");
61 id = 1;
62 break;
63 }
64
65 // Layer Type: 1, 2, 3, or not defined
66 int layer = ((header & 0x00060000) >> 17);
67 switch (layer) {
68 case 0:
69 directory.setString(Mp3Directory.TAG_LAYER, "Not defined");
70 break;
71 case 1:
72 directory.setString(Mp3Directory.TAG_LAYER, "Layer III");
73 break;
74 case 2:
75 directory.setString(Mp3Directory.TAG_LAYER, "Layer II");
76 break;
77 case 3:
78 directory.setString(Mp3Directory.TAG_LAYER, "Layer I");
79 break;
80 }
81
82
83 int protectionBit = ((header & 0x00010000) >> 16);
84
85 // Bitrate: depends on ID and Layer
86 int bitrate = ((header & 0x0000F000) >> 12);
87 if (bitrate != 0 && bitrate != 15)
88 directory.setInt(Mp3Directory.TAG_BITRATE, setBitrate(bitrate, layer, id));
89
90 // Frequency: depends on ID
91 int frequency = ((header & 0x00000C00) >> 10);
92 int[][] frequencyMapping = new int[2][3];
93 frequencyMapping[0] = new int[]{44100, 48000, 32000};
94 frequencyMapping[1] = new int[]{22050, 24000, 16000};
95 if (frequency <= 2) {
96 if (id == 2) {
97 directory.setInt(Mp3Directory.TAG_FREQUENCY, frequencyMapping[1][frequency]);
98 frequency = frequencyMapping[1][frequency];
99 } else if (id == 1) {
100 directory.setInt(Mp3Directory.TAG_FREQUENCY, frequencyMapping[0][frequency]);
101 frequency = frequencyMapping[0][frequency];
102 }
103 } else {
104 directory.addError("Invalid frequency index.");
105 frequency = -1;
106 }
107
108 int paddingBit = ((header & 0x00000200) >> 9);
109
110 // Encoding type: Stereo, Joint Stereo, Dual Channel, or Mono
111 int mode = ((header & 0x000000C0) >> 6);
112 switch (mode){
113 case 0:
114 directory.setString(Mp3Directory.TAG_MODE, "Stereo");
115 break;
116 case 1:
117 directory.setString(Mp3Directory.TAG_MODE, "Joint stereo");
118 break;
119 case 2:
120 directory.setString(Mp3Directory.TAG_MODE, "Dual channel");
121 break;
122 case 3:
123 directory.setString(Mp3Directory.TAG_MODE, "Mono");
124 break;
125 }
126
127 // Copyright boolean
128 int copyright = ((header & 0x00000008) >> 3);
129 switch (copyright) {
130 case 0:
131 directory.setString(Mp3Directory.TAG_COPYRIGHT, "False");
132 break;
133 case 1:
134 directory.setString(Mp3Directory.TAG_COPYRIGHT, "True");
135 break;
136 }
137
138 int emphasis = (header & 0x00000003);
139 switch (emphasis) {
140 case 0:
141 directory.setString(Mp3Directory.TAG_EMPHASIS, "none");
142 break;
143 case 1:
144 directory.setString(Mp3Directory.TAG_EMPHASIS, "50/15ms");
145 break;
146 case 3:
147 directory.setString(Mp3Directory.TAG_EMPHASIS, "CCITT j.17");
148 break;
149 }
150
151 if (frequency != -1 && bitrate != 0 && bitrate != 15) {
152 int frameSize = ((setBitrate(bitrate, layer, id) * 1000) * 144) / frequency;
153 directory.setString(Mp3Directory.TAG_FRAME_SIZE, frameSize + " bytes");
154 }
155 } catch (IOException e) {
156 directory.addError(e.getMessage());
157 } catch (ImageProcessingException e) {
158 directory.addError(e.getMessage());
159 }
160 }
161
162 private static int setBitrate(int bitrate, int layer, int id)
163 {
164 int[][] bitrateMapping = new int[14][6];
165 bitrateMapping[0] = new int[]{32, 32, 32, 32, 32, 8};
166 bitrateMapping[1] = new int[]{64, 48, 40, 64, 48, 16};
167 bitrateMapping[2] = new int[]{96, 56, 48, 96, 56, 24};
168 bitrateMapping[3] = new int[]{128, 64, 56, 128, 64, 32};
169 bitrateMapping[4] = new int[]{160, 80, 64, 160, 80, 64};
170 bitrateMapping[5] = new int[]{192, 96, 80, 192, 96, 80};
171 bitrateMapping[6] = new int[]{224, 112, 96, 224, 112, 56};
172 bitrateMapping[7] = new int[]{256, 128, 112, 256, 128, 64};
173 bitrateMapping[8] = new int[]{288, 160, 128, 28, 160, 128};
174 bitrateMapping[9] = new int[]{320, 192, 160, 320, 192, 160};
175 bitrateMapping[10] = new int[]{352, 224, 192, 352, 224, 112};
176 bitrateMapping[11] = new int[]{384, 256, 224, 384, 256, 128};
177 bitrateMapping[12] = new int[]{416, 320, 256, 416, 320, 256};
178 bitrateMapping[13] = new int[]{448, 384, 320, 448, 384, 320};
179
180 int xPos = 0;
181 int yPos = bitrate - 1;
182
183 if (id == 2) {
184 // MPEG-2
185 switch (layer) {
186 case 1:
187 xPos = 5;
188 break;
189 case 2:
190 xPos = 4;
191 break;
192 case 3:
193 xPos = 3;
194 break;
195 }
196 } else if (id == 1) {
197 // MPEG-1
198 switch (layer) {
199 case 1:
200 xPos = 2;
201 break;
202 case 2:
203 xPos = 1;
204 break;
205 case 3:
206 xPos = 0;
207 break;
208 }
209 }
210
211 return bitrateMapping[yPos][xPos];
212 }
213
214 /*
215 // https://phoxis.org/2010/05/08/synch-safe/
216 private static int getSyncSafeSize(int decode)
217 {
218 int a = decode & 0xFF;
219 int b = (decode >> 8) & 0xFF;
220 int c = (decode >> 16) & 0xFF;
221 int d = (decode >> 24) & 0xFF;
222
223 int decoded = 0x0;
224 decoded = decoded | a;
225 decoded = decoded | (b << 7);
226 decoded = decoded | (c << 14);
227 decoded = decoded | (d << 21);
228 return decoded;
229 }
230 */
231 }