Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resync fork with master #1

Merged
merged 7 commits into from
Sep 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified jflac-codec/src/main/java/org/kc7bfi/jflac/Constants.java
100755 → 100644
Empty file.
15 changes: 5 additions & 10 deletions jflac-codec/src/main/java/org/kc7bfi/jflac/FLACDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public class FLACDecoder {
private ChannelData[] channelData = new ChannelData[Constants.MAX_CHANNELS];
private int outputCapacity = 0;
private int outputChannels = 0;
private int lastFrameNumber;
private long samplesDecoded = 0;
private StreamInfo streamInfo;
private Frame frame = new Frame();
Expand Down Expand Up @@ -98,8 +97,7 @@ public class FLACDecoder {
public FLACDecoder(InputStream inputStream) {
this.inputStream = inputStream;
this.bitStream = new BitInputStream(inputStream);
//state = DECODER_SEARCH_FOR_METADATA;
lastFrameNumber = 0;
//state = DECODER_SEARCH_FOR_METADATA;
samplesDecoded = 0;
//state = DECODER_SEARCH_FOR_METADATA;
}
Expand Down Expand Up @@ -687,7 +685,7 @@ public void readFrame() throws IOException, FrameDecodeException {
boolean gotAFrame = false;
int channel;
int i;
int mid, side, left, right;
int mid, side;
short frameCRC; /* the one we calculate from the input stream */
//int x;

Expand Down Expand Up @@ -758,12 +756,9 @@ public void readFrame() throws IOException, FrameDecodeException {
mid = channelData[0].getOutput()[i];
side = channelData[1].getOutput()[i];
mid <<= 1;
if ((side & 1) != 0) // i.e. if 'side' is odd...
mid++;
left = mid + side;
right = mid - side;
channelData[0].getOutput()[i] = left >> 1;
channelData[1].getOutput()[i] = right >> 1;
mid |= (side & 1); /* i.e. if 'side' is odd... */
channelData[0].getOutput()[i] = (mid + side) >> 1;
channelData[1].getOutput()[i] = (mid - side) >> 1;
}
//System.exit(1);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public abstract class Channel {

/** Partisioned Rice Encoding Method. */
public static final int ENTROPY_CODING_METHOD_PARTITIONED_RICE = 0;
public static final int ENTROPY_CODING_METHOD_PARTITIONED_RICE2 = 1;

/** The size of the encoding method field (in bits). */
public static final int ENTROPY_CODING_METHOD_TYPE_LEN = 2;
Expand Down
8 changes: 5 additions & 3 deletions jflac-codec/src/main/java/org/kc7bfi/jflac/frame/ChannelFixed.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ChannelFixed extends Channel {
*/
public ChannelFixed(BitInputStream is, Header header, ChannelData channelData, int bps, int wastedBits, int order) throws IOException {
super(header, wastedBits);

this.residual = channelData.getResidual();
this.order = order;

Expand All @@ -63,13 +63,15 @@ public ChannelFixed(BitInputStream is, Header header, ChannelData channelData, i
int type = is.readRawUInt(ENTROPY_CODING_METHOD_TYPE_LEN);
EntropyPartitionedRice pr;
switch (type) {
case ENTROPY_CODING_METHOD_PARTITIONED_RICE :
case ENTROPY_CODING_METHOD_PARTITIONED_RICE2 :
case ENTROPY_CODING_METHOD_PARTITIONED_RICE :
int u32 = is.readRawUInt(ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN);
pr = new EntropyPartitionedRice();
entropyCodingMethod = pr;
pr.order = u32;
pr.contents = channelData.getPartitionedRiceContents();
pr.readResidual(is, order, pr.order, header, channelData.getResidual());
pr.readResidual(is, order, pr.order, header, channelData.getResidual(),
(type == ENTROPY_CODING_METHOD_PARTITIONED_RICE2));
break;
default :
throw new IOException("STREAM_DECODER_UNPARSEABLE_STREAM");
Expand Down
8 changes: 5 additions & 3 deletions jflac-codec/src/main/java/org/kc7bfi/jflac/frame/ChannelLPC.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ public ChannelLPC(BitInputStream is, Header header, ChannelData channelData, int
// read entropy coding method info
int codingType = is.readRawUInt(ENTROPY_CODING_METHOD_TYPE_LEN);
//System.out.println("codingType="+codingType);
switch (codingType) {
case ENTROPY_CODING_METHOD_PARTITIONED_RICE :
switch (codingType) {
case ENTROPY_CODING_METHOD_PARTITIONED_RICE2 :
case ENTROPY_CODING_METHOD_PARTITIONED_RICE :
entropyCodingMethod = new EntropyPartitionedRice();
((EntropyPartitionedRice) entropyCodingMethod).order = is.readRawUInt(ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN);
((EntropyPartitionedRice) entropyCodingMethod).contents = channelData.getPartitionedRiceContents();
Expand All @@ -103,7 +104,8 @@ public ChannelLPC(BitInputStream is, Header header, ChannelData channelData, int
order,
((EntropyPartitionedRice) entropyCodingMethod).order,
header,
channelData.getResidual());
channelData.getResidual(),
(codingType == ENTROPY_CODING_METHOD_PARTITIONED_RICE2));
}

//System.out.println();
Expand Down
24 changes: 19 additions & 5 deletions jflac-codec/src/main/java/org/kc7bfi/jflac/frame/EntropyPartitionedRice.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@
*/
public class EntropyPartitionedRice extends EntropyCodingMethod {
private static final int ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN = 4; /* bits */
private static final int ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN = 5; /* bits */
private static final int ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN = 5; /* bits */

private static final int ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER = 15;

/**< == (1<<FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)-1 */
private static final int ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER = 31;
/**< == (1<<FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN)-1 */

protected int order; // The partition order, i.e. # of contexts = 2 ^ order.
protected EntropyPartitionedRiceContents contents; // The context's Rice parameters and/or raw bits.

Expand All @@ -44,24 +49,33 @@ public class EntropyPartitionedRice extends EntropyCodingMethod {
* @param partitionOrder The partition order
* @param header The FLAC Frame Header
* @param residual The residual signal (output)
* @param isExtended The RICE2 indicator flag
* @throws IOException On error reading from InputBitStream
*/
void readResidual(BitInputStream is, int predictorOrder, int partitionOrder, Header header, int[] residual) throws IOException {
void readResidual(BitInputStream is, int predictorOrder, int partitionOrder, Header header, int[] residual, boolean isExtended) throws IOException {
//System.out.println("readREsidual Pred="+predictorOrder+" part="+partitionOrder);
int sample = 0;

int sample;
int partitions = 1 << partitionOrder;
int partitionSamples = partitionOrder > 0 ? header.blockSize >> partitionOrder : header.blockSize - predictorOrder;

int pLen = isExtended ? ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
int pEsc = isExtended ? ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;

contents.ensureSize(Math.max(6, partitionOrder));
contents.parameters = new int[partitions];

sample = 0;
for (int partition = 0; partition < partitions; partition++) {
int riceParameter = is.readRawUInt(ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN);
int riceParameter = is.readRawUInt(pLen);
contents.parameters[partition] = riceParameter;
if (riceParameter < ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
if (riceParameter < pEsc) {
contents.rawBits[partition] = 0;
int u = (partitionOrder == 0 || partition > 0) ? partitionSamples : partitionSamples - predictorOrder;
is.readRiceSignedBlock(residual, sample, u, riceParameter);
sample += u;
} else {
System.out.println("readResidual elsecase, pEsc = " + pEsc);
riceParameter = is.readRawUInt(ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN);
contents.rawBits[partition] = riceParameter;
for (int u = (partitionOrder == 0 || partition > 0) ? 0 : predictorOrder; u < partitionSamples; u++, sample++) {
Expand Down
38 changes: 23 additions & 15 deletions jflac-codec/src/main/java/org/kc7bfi/jflac/frame/Header.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,13 @@ public Header(BitInputStream is, byte[] headerWarmup, StreamInfo streamInfo) thr
int blocksizeHint = 0;
int sampleRateHint = 0;
ByteData rawHeader = new ByteData(16); // MAGIC NUMBER based on the maximum frame header size, including CRC
boolean isKnownVariableBlockSizeStream = (streamInfo != null && streamInfo.getMinBlockSize() != streamInfo.getMaxBlockSize());
boolean isKnownFixedBlockSizeStream = (streamInfo != null && streamInfo.getMinBlockSize() == streamInfo.getMaxBlockSize());

// init the raw header with the saved bits from synchronization
rawHeader.append(headerWarmup[0]);
rawHeader.append(headerWarmup[1]);

// check to make sure that the reserved bits are 0
if ((rawHeader.getData(1) & 0x03) != 0) { // MAGIC NUMBER
// check to make sure that the reserved bit is 0
if ((rawHeader.getData(1) & 0x02) != 0) { // MAGIC NUMBER
throw new BadHeaderException("Bad Magic Number: " + (rawHeader.getData(1) & 0xff));
}

Expand All @@ -101,11 +99,8 @@ public Header(BitInputStream is, byte[] headerWarmup, StreamInfo streamInfo) thr

int bsType = (rawHeader.getData(2) >> 4) & 0x0f;
switch (bsType) {
case 0 :
if (!isKnownFixedBlockSizeStream)
throw new BadHeaderException("Unknown Block Size (0)");
blockSize = streamInfo.getMinBlockSize();
break;
case 0 :
throw new BadHeaderException("Unknown Block Size (0)");
case 1 :
blockSize = 192;
break;
Expand Down Expand Up @@ -142,9 +137,14 @@ public Header(BitInputStream is, byte[] headerWarmup, StreamInfo streamInfo) thr
sampleRate = streamInfo.getSampleRate();
break;
case 1 :
sampleRate = 88200;
break;
case 2 :
sampleRate = 176400;
break;
case 3 :
throw new BadHeaderException("Bad Sample Rate (" + srType + ")");
sampleRate = 192000;
break;
case 4 :
sampleRate = 8000;
break;
Expand Down Expand Up @@ -231,22 +231,30 @@ public Header(BitInputStream is, byte[] headerWarmup, StreamInfo streamInfo) thr
break;
}

if ((rawHeader.getData(3) & 0x01) != 0) { // this should be a zero padding bit
throw new BadHeaderException("this should be a zero padding bit");
/* check to make sure that reserved bit is 0 */
if ((rawHeader.getData(3) & 0x01) != 0) { /* MAGIC NUMBER */
throw new BadHeaderException("Bad Magic Number: " + (rawHeader.getData(3) & 0x01));
}

if ((blocksizeHint != 0) && isKnownVariableBlockSizeStream) {
/* read the frame's starting sample number (or frame number as the case may be) */
if(
(rawHeader.getData(1) & 0x01) != 0 ||
/*@@@ this clause is a concession to the old way of doing variable blocksize; the only known implementation is flake and can probably be removed without inconveniencing anyone */
(streamInfo != null && streamInfo.getMinBlockSize() != streamInfo.getMaxBlockSize())
) { /* variable blocksize */
sampleNumber = is.readUTF8Long(rawHeader);
if (sampleNumber == 0xffffffffffffffffL) { // i.e. non-UTF8 code...
throw new BadHeaderException("Bad Sample Number");
}
} else {
}
else
{ /* fixed blocksize */
int lastFrameNumber = is.readUTF8Int(rawHeader);
if (lastFrameNumber == 0xffffffff) { // i.e. non-UTF8 code...
throw new BadHeaderException("Bad Last Frame");
}
sampleNumber = (long) streamInfo.getMinBlockSize() * (long) lastFrameNumber;
}
}

if (blocksizeHint != 0) {
int blockSizeCode = is.readRawUInt(8);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
package org.kc7bfi.jflac.sound.spi;

/**
* libFLAC - Free Lossless Audio Codec library
* Copyright (C) 2001,2002,2003 Josh Coalson
/*
* Copyright 2011 The jFLAC Project
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* This library 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
* Library General Public License for more details.
* http://www.apache.org/licenses/LICENSE-2.0
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.kc7bfi.jflac.sound.spi;

import org.kc7bfi.jflac.Constants;
import org.kc7bfi.jflac.FLACDecoder;
import org.kc7bfi.jflac.io.BitInputStream;
Expand All @@ -30,6 +26,7 @@
import javax.sound.sampled.spi.AudioFileReader;
import java.io.*;
import java.net.URL;
import java.util.HashMap;

/**
* Provider for Flac audio file reading services. This implementation can parse
Expand All @@ -44,6 +41,11 @@ public class FlacAudioFileReader extends AudioFileReader {

private static final boolean DEBUG = false;

/**
* Property key for the duration in microseconds.
*/
public static final String KEY_DURATION = "duration";

private FLACDecoder decoder;
private StreamInfo streamInfo;

Expand Down Expand Up @@ -252,7 +254,12 @@ protected AudioFileFormat getAudioFileFormat(InputStream bitStream, int mediaLen
if (DEBUG) {
System.out.println("FLAC file reader: got stream with format "+format);
}
return new AudioFileFormat(FlacFileFormatType.FLAC, format, AudioSystem.NOT_SPECIFIED);
final HashMap<String, Object> props = new HashMap<String, Object>();
if (streamInfo.getSampleRate() > 0) {
final long duration = (streamInfo.getTotalSamples() * 1000L * 1000L) / streamInfo.getSampleRate();
props.put(KEY_DURATION, duration);
}
return new AudioFileFormat(FlacFileFormatType.FLAC, format, (int)streamInfo.getTotalSamples(), props);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2011 The jFLAC Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.kc7bfi.jflac.sound.spi;

import junit.framework.TestCase;
Expand Down Expand Up @@ -162,6 +178,8 @@ public void testGetAudioFileFormatWithFlacFile() throws UnsupportedAudioFileExce
final AudioFileFormat audioFileFormat = flacAudioFileReader.getAudioFileFormat(getFlacTestFile("cymbals.flac"));
assertNotNull(audioFileFormat);
assertEquals("flac", audioFileFormat.getType().getExtension());
assertEquals(new Long(9338775), audioFileFormat.getProperty("duration"));
assertEquals(411840, audioFileFormat.getFrameLength());
final AudioFormat format = audioFileFormat.getFormat();
assertEquals(44100f, format.getSampleRate());
assertEquals(16, format.getSampleSizeInBits());
Expand Down