|
| 1 | +/* |
| 2 | + * FLAC library (Java) |
| 3 | + * Copyright (c) Project Nayuki |
| 4 | + * https://www.nayuki.io/page/flac-library-java |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + * You should have received a copy of the GNU Lesser General Public License |
| 14 | + * along with this program (see COPYING.txt and COPYING.LESSER.txt). |
| 15 | + * If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.coloryr.allmusic.server.core.decoder.flac; |
| 19 | + |
| 20 | +import java.io.EOFException; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +/** |
| 26 | + * A basic implementation of most functionality required by FlacLowLevelInpuut. |
| 27 | + */ |
| 28 | +public abstract class AbstractFlacLowLevelInput implements FlacLowLevelInput { |
| 29 | + |
| 30 | + /*---- Fields ----*/ |
| 31 | + |
| 32 | + private static final int RICE_DECODING_TABLE_BITS = 13; // Configurable, must be positive |
| 33 | + private static final int RICE_DECODING_TABLE_MASK = (1 << RICE_DECODING_TABLE_BITS) - 1; |
| 34 | + private static final byte[][] RICE_DECODING_CONSUMED_TABLES = new byte[31][1 << RICE_DECODING_TABLE_BITS]; |
| 35 | + private static final int[][] RICE_DECODING_VALUE_TABLES = new int[31][1 << RICE_DECODING_TABLE_BITS]; |
| 36 | + private static final int RICE_DECODING_CHUNK = 4; // Configurable, must be positive, and RICE_DECODING_CHUNK * |
| 37 | + private static byte[] CRC8_TABLE = new byte[256]; |
| 38 | + private static char[] CRC16_TABLE = new char[256]; |
| 39 | + |
| 40 | + static { |
| 41 | + for (int param = 0; param < RICE_DECODING_CONSUMED_TABLES.length; param++) { |
| 42 | + byte[] consumed = RICE_DECODING_CONSUMED_TABLES[param]; |
| 43 | + int[] values = RICE_DECODING_VALUE_TABLES[param]; |
| 44 | + for (int i = 0; ; i++) { |
| 45 | + int numBits = (i >>> param) + 1 + param; |
| 46 | + if (numBits > RICE_DECODING_TABLE_BITS) break; |
| 47 | + int bits = ((1 << param) | (i & ((1 << param) - 1))); |
| 48 | + int shift = RICE_DECODING_TABLE_BITS - numBits; |
| 49 | + for (int j = 0; j < (1 << shift); j++) { |
| 50 | + consumed[(bits << shift) | j] = (byte) numBits; |
| 51 | + values[(bits << shift) | j] = (i >>> 1) ^ -(i & 1); |
| 52 | + } |
| 53 | + } |
| 54 | + if (consumed[0] != 0) throw new AssertionError(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + static { |
| 59 | + for (int i = 0; i < CRC8_TABLE.length; i++) { |
| 60 | + int temp8 = i; |
| 61 | + int temp16 = i << 8; |
| 62 | + for (int j = 0; j < 8; j++) { |
| 63 | + temp8 = (temp8 << 1) ^ ((temp8 >>> 7) * 0x107); |
| 64 | + temp16 = (temp16 << 1) ^ ((temp16 >>> 15) * 0x18005); |
| 65 | + } |
| 66 | + CRC8_TABLE[i] = (byte) temp8; |
| 67 | + CRC16_TABLE[i] = (char) temp16; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /*---- Constructors ----*/ |
| 72 | + |
| 73 | + // Data from the underlying stream is first stored into this byte buffer before further processing. |
| 74 | + private long byteBufferStartPos; |
| 75 | + |
| 76 | + /*---- Methods ----*/ |
| 77 | + |
| 78 | + /*-- Stream position --*/ |
| 79 | + private byte[] byteBuffer; |
| 80 | + private int byteBufferLen; |
| 81 | + private int byteBufferIndex; |
| 82 | + // The buffer of next bits to return to a reader. Note that byteBufferIndex is incremented when byte |
| 83 | + // values are put into the bit buffer, but they might not have been consumed by the ultimate reader yet. |
| 84 | + private long bitBuffer; // Only the bottom bitBufferLen bits are valid; the top bits are garbage. |
| 85 | + |
| 86 | + /*-- Reading bitwise integers --*/ |
| 87 | + private int bitBufferLen; // Always in the range [0, 64]. |
| 88 | + // Current state of the CRC calculations. |
| 89 | + private int crc8; // Always a uint8 value. |
| 90 | + private int crc16; // Always a uint16 value. |
| 91 | + private int crcStartIndex; // In the range [0, byteBufferLen], unless byteBufferLen = -1. |
| 92 | + |
| 93 | + /*-- Reading bytes --*/ |
| 94 | + |
| 95 | + public AbstractFlacLowLevelInput() { |
| 96 | + byteBuffer = new byte[4096]; |
| 97 | + positionChanged(0); |
| 98 | + } |
| 99 | + |
| 100 | + public long getPosition() { |
| 101 | + return byteBufferStartPos + byteBufferIndex - (bitBufferLen + 7) / 8; |
| 102 | + } |
| 103 | + |
| 104 | + public int getBitPosition() { |
| 105 | + return (-bitBufferLen) & 7; |
| 106 | + } |
| 107 | + |
| 108 | + // When a subclass handles seekTo() and didn't throw UnsupportedOperationException, |
| 109 | + // it must call this method to flush the buffers of upcoming data. |
| 110 | + protected void positionChanged(long pos) { |
| 111 | + byteBufferStartPos = pos; |
| 112 | + Arrays.fill(byteBuffer, (byte) 0); // Defensive clearing, should have no visible effect outside of debugging |
| 113 | + byteBufferLen = 0; |
| 114 | + byteBufferIndex = 0; |
| 115 | + bitBuffer = 0; // Defensive clearing, should have no visible effect outside of debugging |
| 116 | + bitBufferLen = 0; |
| 117 | + resetCrcs(); |
| 118 | + } |
| 119 | + |
| 120 | + /*-- CRC calculations --*/ |
| 121 | + |
| 122 | + // Either returns silently or throws an exception. |
| 123 | + private void checkByteAligned() { |
| 124 | + if (bitBufferLen % 8 != 0) throw new IllegalStateException("Not at a byte boundary"); |
| 125 | + } |
| 126 | + |
| 127 | + public int readUint(int n) throws IOException { |
| 128 | + if (n < 0 || n > 32) throw new IllegalArgumentException(); |
| 129 | + while (bitBufferLen < n) { |
| 130 | + int b = readUnderlying(); |
| 131 | + if (b == -1) throw new EOFException(); |
| 132 | + bitBuffer = (bitBuffer << 8) | b; |
| 133 | + bitBufferLen += 8; |
| 134 | + assert 0 <= bitBufferLen && bitBufferLen <= 64; |
| 135 | + } |
| 136 | + int result = (int) (bitBuffer >>> (bitBufferLen - n)); |
| 137 | + if (n != 32) { |
| 138 | + result &= (1 << n) - 1; |
| 139 | + assert (result >>> n) == 0; |
| 140 | + } |
| 141 | + bitBufferLen -= n; |
| 142 | + assert 0 <= bitBufferLen && bitBufferLen <= 64; |
| 143 | + return result; |
| 144 | + } |
| 145 | + |
| 146 | + public int readSignedInt(int n) throws IOException { |
| 147 | + int shift = 32 - n; |
| 148 | + return (readUint(n) << shift) >> shift; |
| 149 | + } |
| 150 | + |
| 151 | + public void readRiceSignedInts(int param, long[] result, int start, int end) throws IOException { |
| 152 | + if (param < 0 || param > 31) throw new IllegalArgumentException(); |
| 153 | + long unaryLimit = 1L << (53 - param); |
| 154 | + |
| 155 | + byte[] consumeTable = RICE_DECODING_CONSUMED_TABLES[param]; |
| 156 | + int[] valueTable = RICE_DECODING_VALUE_TABLES[param]; |
| 157 | + while (true) { |
| 158 | + middle: |
| 159 | + while (start <= end - RICE_DECODING_CHUNK) { |
| 160 | + if (bitBufferLen < RICE_DECODING_CHUNK * RICE_DECODING_TABLE_BITS) { |
| 161 | + if (byteBufferIndex <= byteBufferLen - 8) { |
| 162 | + fillBitBuffer(); |
| 163 | + } else break; |
| 164 | + } |
| 165 | + for (int i = 0; i < RICE_DECODING_CHUNK; i++, start++) { |
| 166 | + // Fast decoder |
| 167 | + int extractedBits = (int) (bitBuffer >>> (bitBufferLen - RICE_DECODING_TABLE_BITS)) |
| 168 | + & RICE_DECODING_TABLE_MASK; |
| 169 | + int consumed = consumeTable[extractedBits]; |
| 170 | + if (consumed == 0) break middle; |
| 171 | + bitBufferLen -= consumed; |
| 172 | + result[start] = valueTable[extractedBits]; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Slow decoder |
| 177 | + if (start >= end) break; |
| 178 | + long val = 0; |
| 179 | + while (readUint(1) == 0) { |
| 180 | + if (val >= unaryLimit) { |
| 181 | + // At this point, the final decoded value would be so large that the result of the |
| 182 | + // downstream restoreLpc() calculation would not fit in the output sample's bit depth - |
| 183 | + // hence why we stop early and throw an exception. However, this check is conservative |
| 184 | + // and doesn't catch all the cases where the post-LPC result wouldn't fit. |
| 185 | + throw new DataFormatException("Residual value too large"); |
| 186 | + } |
| 187 | + val++; |
| 188 | + } |
| 189 | + val = (val << param) | readUint(param); // Note: Long masking unnecessary because param <= 31 |
| 190 | + assert (val >>> 53) == 0; // Must fit a uint53 by design due to unaryLimit |
| 191 | + val = (val >>> 1) ^ -(val & 1); // Transform uint53 to int53 according to Rice coding of signed numbers |
| 192 | + assert (val >> 52) == 0 || (val >> 52) == -1; // Must fit a signed int53 by design |
| 193 | + result[start] = val; |
| 194 | + start++; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + /*-- Miscellaneous --*/ |
| 199 | + |
| 200 | + // Appends at least 8 bits to the bit buffer, or throws EOFException. |
| 201 | + private void fillBitBuffer() throws IOException { |
| 202 | + int i = byteBufferIndex; |
| 203 | + int n = Math.min((64 - bitBufferLen) >>> 3, byteBufferLen - i); |
| 204 | + byte[] b = byteBuffer; |
| 205 | + if (n > 0) { |
| 206 | + for (int j = 0; j < n; j++, i++) bitBuffer = (bitBuffer << 8) | (b[i] & 0xFF); |
| 207 | + bitBufferLen += n << 3; |
| 208 | + } else if (bitBufferLen <= 56) { |
| 209 | + int temp = readUnderlying(); |
| 210 | + if (temp == -1) throw new EOFException(); |
| 211 | + bitBuffer = (bitBuffer << 8) | temp; |
| 212 | + bitBufferLen += 8; |
| 213 | + } |
| 214 | + assert 8 <= bitBufferLen && bitBufferLen <= 64; |
| 215 | + byteBufferIndex += n; |
| 216 | + } |
| 217 | + |
| 218 | + /*---- Tables of constants ----*/ |
| 219 | + |
| 220 | + // For Rice decoding |
| 221 | + |
| 222 | + public int readByte() throws IOException { |
| 223 | + checkByteAligned(); |
| 224 | + if (bitBufferLen >= 8) return readUint(8); |
| 225 | + else { |
| 226 | + assert bitBufferLen == 0; |
| 227 | + return readUnderlying(); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + public void readFully(byte[] b) throws IOException { |
| 232 | + Objects.requireNonNull(b); |
| 233 | + checkByteAligned(); |
| 234 | + for (int i = 0; i < b.length; i++) b[i] = (byte) readUint(8); |
| 235 | + } |
| 236 | + |
| 237 | + // Reads a byte from the byte buffer (if available) or from the underlying stream, returning either a uint8 or -1. |
| 238 | + private int readUnderlying() throws IOException { |
| 239 | + if (byteBufferIndex >= byteBufferLen) { |
| 240 | + if (byteBufferLen == -1) return -1; |
| 241 | + byteBufferStartPos += byteBufferLen; |
| 242 | + updateCrcs(0); |
| 243 | + byteBufferLen = readUnderlying(byteBuffer, 0, byteBuffer.length); |
| 244 | + crcStartIndex = 0; |
| 245 | + if (byteBufferLen <= 0) return -1; |
| 246 | + byteBufferIndex = 0; |
| 247 | + } |
| 248 | + assert byteBufferIndex < byteBufferLen; |
| 249 | + int temp = byteBuffer[byteBufferIndex] & 0xFF; |
| 250 | + byteBufferIndex++; |
| 251 | + return temp; |
| 252 | + } |
| 253 | + |
| 254 | + // Reads up to 'len' bytes from the underlying byte-based input stream into the given array subrange. |
| 255 | + // Returns a value in the range [0, len] for a successful read, or -1 if the end of stream was reached. |
| 256 | + protected abstract int readUnderlying(byte[] buf, int off, int len) throws IOException; |
| 257 | + |
| 258 | + public void resetCrcs() { |
| 259 | + checkByteAligned(); |
| 260 | + crcStartIndex = byteBufferIndex - bitBufferLen / 8; |
| 261 | + crc8 = 0; |
| 262 | + crc16 = 0; |
| 263 | + } |
| 264 | + // RICE_DECODING_TABLE_BITS <= 64 |
| 265 | + |
| 266 | + public int getCrc8() { |
| 267 | + checkByteAligned(); |
| 268 | + updateCrcs(bitBufferLen / 8); |
| 269 | + if ((crc8 >>> 8) != 0) throw new AssertionError(); |
| 270 | + return crc8; |
| 271 | + } |
| 272 | + |
| 273 | + // For CRC calculations |
| 274 | + |
| 275 | + public int getCrc16() { |
| 276 | + checkByteAligned(); |
| 277 | + updateCrcs(bitBufferLen / 8); |
| 278 | + if ((crc16 >>> 16) != 0) throw new AssertionError(); |
| 279 | + return crc16; |
| 280 | + } |
| 281 | + |
| 282 | + // Updates the two CRC values with data in byteBuffer[crcStartIndex : byteBufferIndex - unusedTrailingBytes]. |
| 283 | + private void updateCrcs(int unusedTrailingBytes) { |
| 284 | + int end = byteBufferIndex - unusedTrailingBytes; |
| 285 | + for (int i = crcStartIndex; i < end; i++) { |
| 286 | + int b = byteBuffer[i] & 0xFF; |
| 287 | + crc8 = CRC8_TABLE[crc8 ^ b] & 0xFF; |
| 288 | + crc16 = CRC16_TABLE[(crc16 >>> 8) ^ b] ^ ((crc16 & 0xFF) << 8); |
| 289 | + assert (crc8 >>> 8) == 0; |
| 290 | + assert (crc16 >>> 16) == 0; |
| 291 | + } |
| 292 | + crcStartIndex = end; |
| 293 | + } |
| 294 | + |
| 295 | + // Note: This class only uses memory and has no native resources. It's not strictly necessary to |
| 296 | + // call the implementation of AbstractFlacLowLevelInput.close() here, but it's a good habit anyway. |
| 297 | + public void close() throws IOException { |
| 298 | + byteBuffer = null; |
| 299 | + byteBufferLen = -1; |
| 300 | + byteBufferIndex = -1; |
| 301 | + bitBuffer = 0; |
| 302 | + bitBufferLen = -1; |
| 303 | + crc8 = -1; |
| 304 | + crc16 = -1; |
| 305 | + crcStartIndex = -1; |
| 306 | + } |
| 307 | + |
| 308 | +} |
0 commit comments