8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV

Reviewed-by: prr, amenkov
This commit is contained in:
Sergey Bylokhov 2016-02-18 22:11:29 +03:00
parent 8372c1c09a
commit 717ad7019c
5 changed files with 244 additions and 95 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,11 +26,11 @@
package com.sun.media.sound;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFileFormat.Type;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
@ -49,11 +49,6 @@ public final class AiffFileReader extends SunFileReader {
throws UnsupportedAudioFileException, IOException {
DataInputStream dis = new DataInputStream(stream);
// assumes a stream at the beginning of the file which has already
// passed the magic number test...
// leaves the input stream at the beginning of the audio data
int fileRead = 0;
int dataLength = 0;
AudioFormat format = null;
// Read the magic number
@ -65,9 +60,9 @@ public final class AiffFileReader extends SunFileReader {
throw new UnsupportedAudioFileException("not an AIFF file");
}
int frameLength = 0;
int length = dis.readInt();
int iffType = dis.readInt();
fileRead += 12;
int totallength;
if(length <= 0 ) {
@ -91,7 +86,6 @@ public final class AiffFileReader extends SunFileReader {
// Read the chunk name
int chunkName = dis.readInt();
int chunkLen = dis.readInt();
fileRead += 8;
int chunkRead = 0;
@ -112,7 +106,13 @@ public final class AiffFileReader extends SunFileReader {
if (channels <= 0) {
throw new UnsupportedAudioFileException("Invalid number of channels");
}
dis.readInt(); // numSampleFrames
frameLength = dis.readInt(); // numSampleFrames
if (frameLength < 0) {
// AiffFileFormat uses int, unlike AIS which uses long
//TODO this (negative) value should be passed as long to AIS
frameLength = AudioSystem.NOT_SPECIFIED;
}
int sampleSizeInBits = dis.readUnsignedShort();
if (sampleSizeInBits < 1 || sampleSizeInBits > 32) {
throw new UnsupportedAudioFileException("Invalid AIFF/COMM sampleSize");
@ -149,38 +149,17 @@ public final class AiffFileReader extends SunFileReader {
break;
case AiffFileFormat.SSND_MAGIC:
// Data chunk.
// we are getting *weird* numbers for chunkLen sometimes;
// this really should be the size of the data chunk....
int dataOffset = dis.readInt();
int blocksize = dis.readInt();
int dataOffset = dis.readInt(); // for now unused in javasound
int blocksize = dis.readInt(); // for now unused in javasound
chunkRead += 8;
// okay, now we are done reading the header. we need to set the size
// of the data segment. we know that sometimes the value we get for
// the chunksize is absurd. this is the best i can think of:if the
// value seems okay, use it. otherwise, we get our value of
// length by assuming that everything left is the data segment;
// its length should be our original length (for all AIFF data chunks)
// minus what we've read so far.
// $$kk: we should be able to get length for the data chunk right after
// we find "SSND." however, some aiff files give *weird* numbers. what
// is going on??
if (chunkLen < length) {
dataLength = chunkLen - chunkRead;
} else {
// $$kk: 11.03.98: this seems dangerous!
dataLength = length - (fileRead + chunkRead);
}
ssndFound = true;
break;
} // switch
fileRead += chunkRead;
// skip the remainder of this chunk
if (!ssndFound) {
int toSkip = chunkLen - chunkRead;
if (toSkip > 0) {
fileRead += dis.skipBytes(toSkip);
dis.skipBytes(toSkip);
}
}
} // while
@ -188,36 +167,12 @@ public final class AiffFileReader extends SunFileReader {
if (format == null) {
throw new UnsupportedAudioFileException("missing COMM chunk");
}
AudioFileFormat.Type type = aifc?AudioFileFormat.Type.AIFC:AudioFileFormat.Type.AIFF;
Type type = aifc ? Type.AIFC : Type.AIFF;
return new AiffFileFormat(type, totallength, format, dataLength / format.getFrameSize());
return new AiffFileFormat(type, totallength, format, frameLength);
}
// HELPER METHODS
/** write_ieee_extended(DataOutputStream dos, double f) throws IOException {
* Extended precision IEEE floating-point conversion routine.
* @argument DataOutputStream
* @argument double
* @return void
* @exception IOException
*/
private void write_ieee_extended(DataOutputStream dos, double f) throws IOException {
int exponent = 16398;
double highMantissa = f;
// For now write the integer portion of f
// $$jb: 03.30.99: stay in synch with JMF on this!!!!
while (highMantissa < 44000) {
highMantissa *= 2;
exponent--;
}
dos.writeShort(exponent);
dos.writeInt( ((int) highMantissa) << 16);
dos.writeInt(0); // low Mantissa
}
/**
* read_ieee_extended
* Extended precision IEEE floating-point conversion routine.

View File

@ -59,7 +59,6 @@ public final class AiffFileWriter extends SunFileWriter {
super(new AudioFileFormat.Type[]{AudioFileFormat.Type.AIFF});
}
// METHODS TO IMPLEMENT AudioFileWriter
@Override
@ -83,7 +82,6 @@ public final class AiffFileWriter extends SunFileWriter {
return new AudioFileFormat.Type[0];
}
@Override
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
Objects.requireNonNull(stream);
@ -102,11 +100,9 @@ public final class AiffFileWriter extends SunFileWriter {
throw new IOException("stream length not specified");
}
int bytesWritten = writeAiffFile(stream, aiffFileFormat, out);
return bytesWritten;
return writeAiffFile(stream, aiffFileFormat, out);
}
@Override
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
Objects.requireNonNull(stream);
@ -129,12 +125,15 @@ public final class AiffFileWriter extends SunFileWriter {
// $$kk: 10.22.99: jan: please either implement this or throw an exception!
// $$fb: 2001-07-13: done. Fixes Bug 4479981
int ssndBlockSize = (aiffFileFormat.getFormat().getChannels() * aiffFileFormat.getFormat().getSampleSizeInBits());
int channels = aiffFileFormat.getFormat().getChannels();
int sampleSize = aiffFileFormat.getFormat().getSampleSizeInBits();
int ssndBlockSize = channels * ((sampleSize + 7) / 8);
int aiffLength=bytesWritten;
int ssndChunkSize=aiffLength-aiffFileFormat.getHeaderSize()+16;
long dataSize=ssndChunkSize-16;
int numFrames=(int) (dataSize*8/ssndBlockSize);
//TODO possibly incorrect round
int numFrames = (int) (dataSize / ssndBlockSize);
RandomAccessFile raf=new RandomAccessFile(out, "rw");
// skip FORM magic
@ -173,12 +172,7 @@ public final class AiffFileWriter extends SunFileWriter {
AudioFormat streamFormat = stream.getFormat();
AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
float sampleRate;
int sampleSizeInBits;
int channels;
int frameSize;
float frameRate;
int fileSize;
boolean convert8to16 = false;
@ -235,7 +229,6 @@ public final class AiffFileWriter extends SunFileWriter {
return fileFormat;
}
private int writeAiffFile(InputStream in, AiffFileFormat aiffFileFormat, OutputStream out) throws IOException {
int bytesRead = 0;
@ -275,25 +268,20 @@ public final class AiffFileWriter extends SunFileWriter {
AudioFormat.Encoding encoding = null;
//$$fb a little bit nicer handling of constants
//int headerSize = 54;
int headerSize = aiffFileFormat.getHeaderSize();
//int fverChunkSize = 0;
int fverChunkSize = aiffFileFormat.getFverChunkSize();
//int commChunkSize = 26;
int commChunkSize = aiffFileFormat.getCommChunkSize();
int aiffLength = -1;
int ssndChunkSize = -1;
//int ssndOffset = headerSize - 16;
int ssndOffset = aiffFileFormat.getSsndChunkOffset();
short channels = (short) format.getChannels();
short sampleSize = (short) format.getSampleSizeInBits();
int ssndBlockSize = (channels * sampleSize);
int numFrames = aiffFileFormat.getFrameLength();
long dataSize = -1;
int ssndBlockSize = channels * ((sampleSize + 7) / 8);
int numFrames = aiffFileFormat.getFrameLength();
long dataSize = -1;
if( numFrames != AudioSystem.NOT_SPECIFIED) {
dataSize = (long) numFrames * ssndBlockSize / 8;
dataSize = (long) numFrames * ssndBlockSize;
ssndChunkSize = (int)dataSize + 16;
aiffLength = (int)dataSize+headerSize;
}
@ -403,9 +391,6 @@ public final class AiffFileWriter extends SunFileWriter {
}
// HELPER METHODS
private static final int DOUBLE_MANTISSA_LENGTH = 52;
@ -452,6 +437,4 @@ public final class AiffFileWriter extends SunFileWriter {
dos.writeShort(extendedBits79To64);
dos.writeLong(extendedBits63To0);
}
}

View File

@ -255,16 +255,17 @@ public final class WaveExtensibleFileReader extends SunFileReader {
public AudioInputStream getAudioInputStream(final InputStream stream)
throws UnsupportedAudioFileException, IOException {
AudioFileFormat format = getAudioFileFormat(stream);
final AudioFileFormat format = getAudioFileFormat(stream);
// we've got everything, the stream is supported and it is at the
// beginning of the header, so find the data chunk again and return an
// AudioInputStream
RIFFReader riffiterator = new RIFFReader(stream);
final RIFFReader riffiterator = new RIFFReader(stream);
while (riffiterator.hasNextChunk()) {
RIFFReader chunk = riffiterator.nextChunk();
if (chunk.getFormat().equals("data")) {
return new AudioInputStream(chunk, format.getFormat(), chunk
.getSize());
final AudioFormat af = format.getFormat();
final long length = chunk.getSize() / af.getFrameSize();
return new AudioInputStream(chunk, af, length);
}
}
throw new UnsupportedAudioFileException();

View File

@ -95,16 +95,17 @@ public final class WaveFloatFileReader extends SunFileReader {
public AudioInputStream getAudioInputStream(final InputStream stream)
throws UnsupportedAudioFileException, IOException {
AudioFileFormat format = getAudioFileFormat(stream);
final AudioFileFormat format = getAudioFileFormat(stream);
// we've got everything, the stream is supported and it is at the
// beginning of the header, so find the data chunk again and return an
// AudioInputStream
RIFFReader riffiterator = new RIFFReader(stream);
final RIFFReader riffiterator = new RIFFReader(stream);
while (riffiterator.hasNextChunk()) {
RIFFReader chunk = riffiterator.nextChunk();
if (chunk.getFormat().equals("data")) {
return new AudioInputStream(chunk, format.getFormat(),
chunk.getSize());
final AudioFormat af = format.getFormat();
final long length = chunk.getSize() / af.getFrameSize();
return new AudioInputStream(chunk, af, length);
}
}
throw new UnsupportedAudioFileException();

View File

@ -0,0 +1,209 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.spi.AudioFileWriter;
import javax.sound.sampled.spi.FormatConversionProvider;
import static java.util.ServiceLoader.load;
import static javax.sound.sampled.AudioFileFormat.Type.AIFC;
import static javax.sound.sampled.AudioFileFormat.Type.AIFF;
import static javax.sound.sampled.AudioFileFormat.Type.AU;
import static javax.sound.sampled.AudioFileFormat.Type.SND;
import static javax.sound.sampled.AudioFileFormat.Type.WAVE;
import static javax.sound.sampled.AudioSystem.NOT_SPECIFIED;
/**
* @test
* @bug 8038139
*/
public final class FrameLengthAfterConversion {
/**
* We will try to use all formats, in this case all our providers will be
* covered by supported/unsupported formats.
*/
private static final List<AudioFormat> formats = new ArrayList<>(23000);
private static final AudioFormat.Encoding[] encodings = {
AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,
AudioFormat.Encoding.PCM_SIGNED, AudioFormat.Encoding.PCM_UNSIGNED,
AudioFormat.Encoding.PCM_FLOAT, new AudioFormat.Encoding("Test")
};
private static final int[] sampleBits = {
1, 4, 8, 11, 16, 20, 24, 32
};
private static final int[] channels = {
1, 2, 3, 4, 5
};
private static final AudioFileFormat.Type[] types = {
WAVE, AU, AIFF, AIFC, SND,
new AudioFileFormat.Type("TestName", "TestExt")
};
private static final int FRAME_LENGTH = 10;
static {
for (final int sampleSize : sampleBits) {
for (final int channel : channels) {
for (final AudioFormat.Encoding enc : encodings) {
final int frameSize = ((sampleSize + 7) / 8) * channel;
formats.add(new AudioFormat(enc, 44100, sampleSize, channel,
frameSize, 44100, true));
formats.add(new AudioFormat(enc, 44100, sampleSize, channel,
frameSize, 44100, false));
}
}
}
}
public static void main(final String[] args) {
for (final FormatConversionProvider fcp : load(
FormatConversionProvider.class)) {
System.out.println("fcp = " + fcp);
for (final AudioFormat from : formats) {
for (final AudioFormat to : formats) {
testAfterConversion(fcp, to, getStream(from, true));
}
}
}
for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
System.out.println("afw = " + afw);
for (final AudioFileFormat.Type type : types) {
for (final AudioFormat from : formats) {
testAfterSaveToStream(afw, type, getStream(from, true));
}
}
}
for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
System.out.println("afw = " + afw);
for (final AudioFileFormat.Type type : types) {
for (final AudioFormat from : formats) {
testAfterSaveToFile(afw, type, getStream(from, true));
}
}
}
for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
System.out.println("afw = " + afw);
for (final AudioFileFormat.Type type : types) {
for (final AudioFormat from : formats) {
testAfterSaveToFile(afw, type, getStream(from, false));
}
}
}
}
/**
* Verifies the frame length after the stream was saved/read to/from
* stream.
*/
private static void testAfterSaveToStream(final AudioFileWriter afw,
final AudioFileFormat.Type type,
final AudioInputStream ais) {
try {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
afw.write(ais, type, out);
final InputStream input = new ByteArrayInputStream(
out.toByteArray());
validate(AudioSystem.getAudioInputStream(input).getFrameLength());
} catch (IllegalArgumentException | UnsupportedAudioFileException
| IOException ignored) {
}
}
/**
* Verifies the frame length after the stream was saved/read to/from file.
*/
private static void testAfterSaveToFile(final AudioFileWriter afw,
final AudioFileFormat.Type type,
AudioInputStream ais) {
try {
final File temp = File.createTempFile("sound", ".tmp");
temp.deleteOnExit();
afw.write(ais, type, temp);
ais = AudioSystem.getAudioInputStream(temp);
final long frameLength = ais.getFrameLength();
ais.close();
temp.delete();
validate(frameLength);
} catch (IllegalArgumentException | UnsupportedAudioFileException
| IOException ignored) {
}
}
/**
* Verifies the frame length after the stream was converted to other
* stream.
*
* @see FormatConversionProvider#getAudioInputStream(AudioFormat,
* AudioInputStream)
*/
private static void testAfterConversion(final FormatConversionProvider fcp,
final AudioFormat to,
final AudioInputStream ais) {
if (fcp.isConversionSupported(to, ais.getFormat())) {
validate(fcp.getAudioInputStream(to, ais).getFrameLength());
}
}
/**
* Throws an exception if the frameLength is specified and is not equal to
* the gold value.
*/
private static void validate(final long frameLength) {
if (frameLength != FRAME_LENGTH) {
System.err.println("Expected: " + FRAME_LENGTH);
System.err.println("Actual: " + frameLength);
throw new RuntimeException();
}
}
private static AudioInputStream getStream(final AudioFormat format,
final boolean frameLength) {
final int dataSize = FRAME_LENGTH * format.getFrameSize();
final InputStream in = new ByteArrayInputStream(new byte[dataSize]);
if (frameLength) {
return new AudioInputStream(in, format, FRAME_LENGTH);
} else {
return new AudioInputStream(in, format, NOT_SPECIFIED);
}
}
}