Skip to content

Commit 311bdf3

Browse files
authored
Merge pull request #457 from drewnoakes/mp3
Tweak MP3 code
2 parents 482cb69 + 5fdd5c9 commit 311bdf3

File tree

4 files changed

+37
-47
lines changed

4 files changed

+37
-47
lines changed

Source/com/drew/imaging/FileType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public enum FileType
4848
Mp4("MP4", "MPEG-4 Part 14", "video/mp4", "mp4", "m4a", "m4p", "m4b", "m4r", "m4v"),
4949
Heif("HEIF", "High Efficiency Image File Format", "image/heif", "heif", "heic"),
5050
Eps("EPS", "Encapsulated PostScript", "application/postscript", "eps", "epsf", "epsi"),
51-
Mp3("MP3", "MP3", "audio/mpeg", "mp3"),
51+
Mp3("MP3", "MPEG Audio Layer III", "audio/mpeg", "mp3"),
5252

5353
/** Sony camera raw. */
5454
Arw("ARW", "Sony Camera Raw", null, "arw"),

Source/com/drew/metadata/mp3/Mp3Directory.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
*/
3131
public class Mp3Directory extends Directory
3232
{
33-
3433
public static final int TAG_ID = 1;
3534
public static final int TAG_LAYER = 2;
3635
public static final int TAG_BITRATE = 3;

Source/com/drew/metadata/mp3/Mp3Reader.java

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,52 +37,46 @@
3737
*/
3838
public class Mp3Reader
3939
{
40-
4140
public void extract(@NotNull final InputStream inputStream, @NotNull final Metadata metadata)
4241
{
4342
Mp3Directory directory = new Mp3Directory();
4443
metadata.addDirectory(directory);
4544

4645
try {
47-
inputStream.reset();
4846
SequentialReader reader = new StreamReader(inputStream);
4947

5048
int header = reader.getInt32();
5149

5250
// ID: MPEG-2.5, MPEG-2, or MPEG-1
53-
double id = 0;
51+
int id = 0;
5452
switch ((header & 0x000180000) >> 19) {
55-
case (0):
56-
directory.setString(Mp3Directory.TAG_ID, "MPEG-2.5");
57-
id = 2.5;
53+
case 0:
5854
throw new ImageProcessingException("MPEG-2.5 not supported.");
59-
case (2):
55+
case 2:
6056
directory.setString(Mp3Directory.TAG_ID, "MPEG-2");
6157
id = 2;
6258
break;
63-
case (3):
59+
case 3:
6460
directory.setString(Mp3Directory.TAG_ID, "MPEG-1");
6561
id = 1;
6662
break;
67-
default:
6863
}
6964

7065
// Layer Type: 1, 2, 3, or not defined
7166
int layer = ((header & 0x00060000) >> 17);
7267
switch (layer) {
73-
case(0):
68+
case 0:
7469
directory.setString(Mp3Directory.TAG_LAYER, "Not defined");
7570
break;
76-
case(1):
71+
case 1:
7772
directory.setString(Mp3Directory.TAG_LAYER, "Layer III");
7873
break;
79-
case(2):
74+
case 2:
8075
directory.setString(Mp3Directory.TAG_LAYER, "Layer II");
8176
break;
82-
case(3):
77+
case 3:
8378
directory.setString(Mp3Directory.TAG_LAYER, "Layer I");
8479
break;
85-
default:
8680
}
8781

8882

@@ -99,10 +93,10 @@ public void extract(@NotNull final InputStream inputStream, @NotNull final Metad
9993
frequencyMapping[0] = new int[]{44100, 48000, 32000};
10094
frequencyMapping[1] = new int[]{22050, 24000, 16000};
10195
if (id == 2) {
102-
directory.setInt(directory.TAG_FREQUENCY, frequencyMapping[1][frequency]);
96+
directory.setInt(Mp3Directory.TAG_FREQUENCY, frequencyMapping[1][frequency]);
10397
frequency = frequencyMapping[1][frequency];
10498
} else if (id == 1) {
105-
directory.setInt(directory.TAG_FREQUENCY, frequencyMapping[0][frequency]);
99+
directory.setInt(Mp3Directory.TAG_FREQUENCY, frequencyMapping[0][frequency]);
106100
frequency = frequencyMapping[0][frequency];
107101
}
108102

@@ -112,57 +106,56 @@ public void extract(@NotNull final InputStream inputStream, @NotNull final Metad
112106
// Encoding type: Stereo, Joint Stereo, Dual Channel, or Mono
113107
int mode = ((header & 0x000000C0) >> 6);
114108
switch (mode){
115-
case(0):
109+
case 0:
116110
directory.setString(Mp3Directory.TAG_MODE, "Stereo");
117111
break;
118-
case(1):
112+
case 1:
119113
directory.setString(Mp3Directory.TAG_MODE, "Joint stereo");
120114
break;
121-
case(2):
115+
case 2:
122116
directory.setString(Mp3Directory.TAG_MODE, "Dual channel");
123117
break;
124-
case(3):
118+
case 3:
125119
directory.setString(Mp3Directory.TAG_MODE, "Mono");
126120
break;
127-
default:
128121
}
129122

130123
// Copyright boolean
131124
int copyright = ((header & 0x00000008) >> 3);
132125
switch (copyright) {
133-
case(0):
126+
case 0:
134127
directory.setString(Mp3Directory.TAG_COPYRIGHT, "False");
135128
break;
136-
case(1):
129+
case 1:
137130
directory.setString(Mp3Directory.TAG_COPYRIGHT, "True");
138131
break;
139-
default:
140132
}
141133

142134
int emphasis = (header & 0x00000003);
143135
switch (emphasis) {
144-
case (0):
136+
case 0:
145137
directory.setString(Mp3Directory.TAG_EMPHASIS, "none");
146138
break;
147-
case (1):
139+
case 1:
148140
directory.setString(Mp3Directory.TAG_EMPHASIS, "50/15ms");
149141
break;
150-
case (3):
142+
case 3:
151143
directory.setString(Mp3Directory.TAG_EMPHASIS, "CCITT j.17");
152144
break;
153-
default:
154145
}
155146

156-
int frameSize = ((setBitrate(bitrate, layer, id) * 1000) * 144) / frequency;
157-
directory.setString(Mp3Directory.TAG_FRAME_SIZE, frameSize + " bytes");
147+
if (bitrate != 0 && bitrate != 15) {
148+
int frameSize = ((setBitrate(bitrate, layer, id) * 1000) * 144) / frequency;
149+
directory.setString(Mp3Directory.TAG_FRAME_SIZE, frameSize + " bytes");
150+
}
158151
} catch (IOException e) {
159152
e.printStackTrace();
160153
} catch (ImageProcessingException e) {
161154
e.printStackTrace();
162155
}
163156
}
164157

165-
public int setBitrate(int bitrate, int layer, double id)
158+
private static int setBitrate(int bitrate, int layer, int id)
166159
{
167160
int[][] bitrateMapping = new int[14][6];
168161
bitrateMapping[0] = new int[]{32, 32, 32, 32, 32, 8};
@@ -186,40 +179,37 @@ public int setBitrate(int bitrate, int layer, double id)
186179
if (id == 2) {
187180
// MPEG-2
188181
switch (layer) {
189-
case (1):
182+
case 1:
190183
xPos = 5;
191184
break;
192-
case (2):
185+
case 2:
193186
xPos = 4;
194187
break;
195-
case (3):
188+
case 3:
196189
xPos = 3;
197190
break;
198-
default:
199191
}
200192
} else if (id == 1) {
201193
// MPEG-1
202194
switch (layer) {
203-
case(1):
195+
case 1:
204196
xPos = 2;
205197
break;
206-
case(2):
198+
case 2:
207199
xPos = 1;
208200
break;
209-
case(3):
201+
case 3:
210202
xPos = 0;
211203
break;
212-
default:
213204
}
214205
}
215206

216207
return bitrateMapping[yPos][xPos];
217208
}
218209

219-
/**
220-
* https://phoxis.org/2010/05/08/synch-safe/
221-
*/
222-
public static int getSyncSafeSize(int decode)
210+
/*
211+
// https://phoxis.org/2010/05/08/synch-safe/
212+
private static int getSyncSafeSize(int decode)
223213
{
224214
int a = decode & 0xFF;
225215
int b = (decode >> 8) & 0xFF;
@@ -233,4 +223,5 @@ public static int getSyncSafeSize(int decode)
233223
decoded = decoded | (d << 21);
234224
return decoded;
235225
}
226+
*/
236227
}

Source/com/drew/metadata/mp4/media/Mp4UuidBoxDirectory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public class Mp4UuidBoxDirectory extends Mp4MediaDirectory
3535
static
3636
{
3737
Mp4UuidBoxDirectory.addMp4MediaTags(_tagNameMap);
38-
_tagNameMap.put(TAG_UUID, "uuid");
39-
_tagNameMap.put(TAG_USER_DATA, "data");
38+
_tagNameMap.put(TAG_UUID, "UUID");
39+
_tagNameMap.put(TAG_USER_DATA, "Data");
4040
}
4141

4242
public Mp4UuidBoxDirectory()

0 commit comments

Comments
 (0)