Skip to content

Commit

Permalink
Merge pull request #457 from drewnoakes/mp3
Browse files Browse the repository at this point in the history
Tweak MP3 code
  • Loading branch information
drewnoakes authored Jan 15, 2020
2 parents 482cb69 + 5fdd5c9 commit 311bdf3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Source/com/drew/imaging/FileType.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public enum FileType
Mp4("MP4", "MPEG-4 Part 14", "video/mp4", "mp4", "m4a", "m4p", "m4b", "m4r", "m4v"),
Heif("HEIF", "High Efficiency Image File Format", "image/heif", "heif", "heic"),
Eps("EPS", "Encapsulated PostScript", "application/postscript", "eps", "epsf", "epsi"),
Mp3("MP3", "MP3", "audio/mpeg", "mp3"),
Mp3("MP3", "MPEG Audio Layer III", "audio/mpeg", "mp3"),

/** Sony camera raw. */
Arw("ARW", "Sony Camera Raw", null, "arw"),
Expand Down
1 change: 0 additions & 1 deletion Source/com/drew/metadata/mp3/Mp3Directory.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
*/
public class Mp3Directory extends Directory
{

public static final int TAG_ID = 1;
public static final int TAG_LAYER = 2;
public static final int TAG_BITRATE = 3;
Expand Down
77 changes: 34 additions & 43 deletions Source/com/drew/metadata/mp3/Mp3Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,52 +37,46 @@
*/
public class Mp3Reader
{

public void extract(@NotNull final InputStream inputStream, @NotNull final Metadata metadata)
{
Mp3Directory directory = new Mp3Directory();
metadata.addDirectory(directory);

try {
inputStream.reset();
SequentialReader reader = new StreamReader(inputStream);

int header = reader.getInt32();

// ID: MPEG-2.5, MPEG-2, or MPEG-1
double id = 0;
int id = 0;
switch ((header & 0x000180000) >> 19) {
case (0):
directory.setString(Mp3Directory.TAG_ID, "MPEG-2.5");
id = 2.5;
case 0:
throw new ImageProcessingException("MPEG-2.5 not supported.");
case (2):
case 2:
directory.setString(Mp3Directory.TAG_ID, "MPEG-2");
id = 2;
break;
case (3):
case 3:
directory.setString(Mp3Directory.TAG_ID, "MPEG-1");
id = 1;
break;
default:
}

// Layer Type: 1, 2, 3, or not defined
int layer = ((header & 0x00060000) >> 17);
switch (layer) {
case(0):
case 0:
directory.setString(Mp3Directory.TAG_LAYER, "Not defined");
break;
case(1):
case 1:
directory.setString(Mp3Directory.TAG_LAYER, "Layer III");
break;
case(2):
case 2:
directory.setString(Mp3Directory.TAG_LAYER, "Layer II");
break;
case(3):
case 3:
directory.setString(Mp3Directory.TAG_LAYER, "Layer I");
break;
default:
}


Expand All @@ -99,10 +93,10 @@ public void extract(@NotNull final InputStream inputStream, @NotNull final Metad
frequencyMapping[0] = new int[]{44100, 48000, 32000};
frequencyMapping[1] = new int[]{22050, 24000, 16000};
if (id == 2) {
directory.setInt(directory.TAG_FREQUENCY, frequencyMapping[1][frequency]);
directory.setInt(Mp3Directory.TAG_FREQUENCY, frequencyMapping[1][frequency]);
frequency = frequencyMapping[1][frequency];
} else if (id == 1) {
directory.setInt(directory.TAG_FREQUENCY, frequencyMapping[0][frequency]);
directory.setInt(Mp3Directory.TAG_FREQUENCY, frequencyMapping[0][frequency]);
frequency = frequencyMapping[0][frequency];
}

Expand All @@ -112,57 +106,56 @@ public void extract(@NotNull final InputStream inputStream, @NotNull final Metad
// Encoding type: Stereo, Joint Stereo, Dual Channel, or Mono
int mode = ((header & 0x000000C0) >> 6);
switch (mode){
case(0):
case 0:
directory.setString(Mp3Directory.TAG_MODE, "Stereo");
break;
case(1):
case 1:
directory.setString(Mp3Directory.TAG_MODE, "Joint stereo");
break;
case(2):
case 2:
directory.setString(Mp3Directory.TAG_MODE, "Dual channel");
break;
case(3):
case 3:
directory.setString(Mp3Directory.TAG_MODE, "Mono");
break;
default:
}

// Copyright boolean
int copyright = ((header & 0x00000008) >> 3);
switch (copyright) {
case(0):
case 0:
directory.setString(Mp3Directory.TAG_COPYRIGHT, "False");
break;
case(1):
case 1:
directory.setString(Mp3Directory.TAG_COPYRIGHT, "True");
break;
default:
}

int emphasis = (header & 0x00000003);
switch (emphasis) {
case (0):
case 0:
directory.setString(Mp3Directory.TAG_EMPHASIS, "none");
break;
case (1):
case 1:
directory.setString(Mp3Directory.TAG_EMPHASIS, "50/15ms");
break;
case (3):
case 3:
directory.setString(Mp3Directory.TAG_EMPHASIS, "CCITT j.17");
break;
default:
}

int frameSize = ((setBitrate(bitrate, layer, id) * 1000) * 144) / frequency;
directory.setString(Mp3Directory.TAG_FRAME_SIZE, frameSize + " bytes");
if (bitrate != 0 && bitrate != 15) {
int frameSize = ((setBitrate(bitrate, layer, id) * 1000) * 144) / frequency;
directory.setString(Mp3Directory.TAG_FRAME_SIZE, frameSize + " bytes");
}
} catch (IOException e) {
e.printStackTrace();
} catch (ImageProcessingException e) {
e.printStackTrace();
}
}

public int setBitrate(int bitrate, int layer, double id)
private static int setBitrate(int bitrate, int layer, int id)
{
int[][] bitrateMapping = new int[14][6];
bitrateMapping[0] = new int[]{32, 32, 32, 32, 32, 8};
Expand All @@ -186,40 +179,37 @@ public int setBitrate(int bitrate, int layer, double id)
if (id == 2) {
// MPEG-2
switch (layer) {
case (1):
case 1:
xPos = 5;
break;
case (2):
case 2:
xPos = 4;
break;
case (3):
case 3:
xPos = 3;
break;
default:
}
} else if (id == 1) {
// MPEG-1
switch (layer) {
case(1):
case 1:
xPos = 2;
break;
case(2):
case 2:
xPos = 1;
break;
case(3):
case 3:
xPos = 0;
break;
default:
}
}

return bitrateMapping[yPos][xPos];
}

/**
* https://phoxis.org/2010/05/08/synch-safe/
*/
public static int getSyncSafeSize(int decode)
/*
// https://phoxis.org/2010/05/08/synch-safe/
private static int getSyncSafeSize(int decode)
{
int a = decode & 0xFF;
int b = (decode >> 8) & 0xFF;
Expand All @@ -233,4 +223,5 @@ public static int getSyncSafeSize(int decode)
decoded = decoded | (d << 21);
return decoded;
}
*/
}
4 changes: 2 additions & 2 deletions Source/com/drew/metadata/mp4/media/Mp4UuidBoxDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class Mp4UuidBoxDirectory extends Mp4MediaDirectory
static
{
Mp4UuidBoxDirectory.addMp4MediaTags(_tagNameMap);
_tagNameMap.put(TAG_UUID, "uuid");
_tagNameMap.put(TAG_USER_DATA, "data");
_tagNameMap.put(TAG_UUID, "UUID");
_tagNameMap.put(TAG_USER_DATA, "Data");
}

public Mp4UuidBoxDirectory()
Expand Down

0 comments on commit 311bdf3

Please sign in to comment.