1 /*
   2  * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.media.sound;
  27 
  28 import java.io.IOException;
  29 import java.util.Objects;
  30 import java.util.Vector;
  31 
  32 import javax.sound.sampled.AudioFormat;
  33 import javax.sound.sampled.AudioInputStream;
  34 import javax.sound.sampled.AudioSystem;
  35 
  36 
  37 /**
  38  * U-law encodes linear data, and decodes u-law data to linear data.
  39  *
  40  * @author Kara Kytle
  41  */
  42 public final class UlawCodec extends SunCodec {
  43 
  44     /* Tables used for U-law decoding */
  45 
  46     private static final byte[] ULAW_TABH = new byte[256];
  47     private static final byte[] ULAW_TABL = new byte[256];
  48 
  49     private static final AudioFormat.Encoding[] ulawEncodings = {AudioFormat.Encoding.ULAW,
  50                                                                  AudioFormat.Encoding.PCM_SIGNED};
  51 
  52     private static final short seg_end [] = {0xFF, 0x1FF, 0x3FF,
  53                                              0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
  54 
  55     /**
  56      * Initializes the decode tables
  57      */
  58     static {
  59         for (int i=0;i<256;i++) {
  60             int ulaw = ~i;
  61             int t;
  62 
  63             ulaw &= 0xFF;
  64             t = ((ulaw & 0xf)<<3) + 132;
  65             t <<= ((ulaw & 0x70) >> 4);
  66             t = ( (ulaw&0x80) != 0 ) ? (132-t) : (t-132);
  67 
  68             ULAW_TABL[i] = (byte) (t&0xff);
  69             ULAW_TABH[i] = (byte) ((t>>8) & 0xff);
  70         }
  71     }
  72 
  73 
  74     /**
  75      * Constructs a new ULAW codec object.
  76      */
  77     public UlawCodec() {
  78         super(ulawEncodings, ulawEncodings);
  79     }
  80 
  81     /**
  82      */
  83     public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat){
  84         if( AudioFormat.Encoding.PCM_SIGNED.equals(sourceFormat.getEncoding()) ) {
  85             if( sourceFormat.getSampleSizeInBits() == 16 ) {
  86                 AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
  87                 enc[0] = AudioFormat.Encoding.ULAW;
  88                 return enc;
  89             } else {
  90                 return new AudioFormat.Encoding[0];
  91             }
  92         } else if (AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding())) {
  93             if (sourceFormat.getSampleSizeInBits() == 8) {
  94                 AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
  95                 enc[0] = AudioFormat.Encoding.PCM_SIGNED;
  96                 return enc;
  97             } else {
  98                 return new AudioFormat.Encoding[0];
  99             }
 100         } else {
 101             return new AudioFormat.Encoding[0];
 102         }
 103     }
 104 
 105 
 106     /**
 107      */
 108     public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
 109         Objects.requireNonNull(targetEncoding);
 110         Objects.requireNonNull(sourceFormat);
 111         if( (AudioFormat.Encoding.PCM_SIGNED.equals(targetEncoding)
 112              && AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding()))
 113             ||
 114             (AudioFormat.Encoding.ULAW.equals(targetEncoding)
 115              && AudioFormat.Encoding.PCM_SIGNED.equals(sourceFormat.getEncoding()))) {
 116                 return getOutputFormats(sourceFormat);
 117             } else {
 118                 return new AudioFormat[0];
 119             }
 120     }
 121 
 122     /**
 123      */
 124     public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream){
 125         AudioFormat sourceFormat = sourceStream.getFormat();
 126         AudioFormat.Encoding sourceEncoding = sourceFormat.getEncoding();
 127 
 128         if (!isConversionSupported(targetEncoding,sourceStream.getFormat())) {
 129             throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
 130         }
 131         if (sourceEncoding.equals(targetEncoding)) {
 132             return sourceStream;
 133         }
 134         AudioFormat targetFormat = null;
 135         if (AudioFormat.Encoding.ULAW.equals(sourceEncoding) &&
 136             AudioFormat.Encoding.PCM_SIGNED.equals(targetEncoding) ) {
 137             targetFormat = new AudioFormat( targetEncoding,
 138                                             sourceFormat.getSampleRate(),
 139                                             16,
 140                                             sourceFormat.getChannels(),
 141                                             2*sourceFormat.getChannels(),
 142                                             sourceFormat.getSampleRate(),
 143                                             sourceFormat.isBigEndian());
 144         } else if (AudioFormat.Encoding.PCM_SIGNED.equals(sourceEncoding) &&
 145                    AudioFormat.Encoding.ULAW.equals(targetEncoding)) {
 146             targetFormat = new AudioFormat( targetEncoding,
 147                                             sourceFormat.getSampleRate(),
 148                                             8,
 149                                             sourceFormat.getChannels(),
 150                                             sourceFormat.getChannels(),
 151                                             sourceFormat.getSampleRate(),
 152                                             false);
 153         } else {
 154             throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
 155         }
 156 
 157         return getConvertedStream(targetFormat, sourceStream);
 158     }
 159 
 160     /**
 161      * use old code...
 162      */
 163     public AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream){
 164         if (!isConversionSupported(targetFormat, sourceStream.getFormat()))
 165             throw new IllegalArgumentException("Unsupported conversion: "
 166                                                + sourceStream.getFormat().toString() + " to "
 167                                                + targetFormat.toString());
 168         return getConvertedStream(targetFormat, sourceStream);
 169     }
 170 
 171 
 172     // OLD CODE
 173 
 174     /**
 175      * Opens the codec with the specified parameters.
 176      * @param stream stream from which data to be processed should be read
 177      * @param outputFormat desired data format of the stream after processing
 178      * @return stream from which processed data may be read
 179      * @throws IllegalArgumentException if the format combination supplied is
 180      * not supported.
 181      */
 182     /*  public AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) { */
 183     private AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) {
 184         AudioInputStream cs = null;
 185 
 186         AudioFormat inputFormat = stream.getFormat();
 187 
 188         if( inputFormat.matches(outputFormat) ) {
 189             cs = stream;
 190         } else {
 191             cs = (AudioInputStream) (new UlawCodecStream(stream, outputFormat));
 192         }
 193         return cs;
 194     }
 195 
 196     /**
 197      * Obtains the set of output formats supported by the codec
 198      * given a particular input format.
 199      * If no output formats are supported for this input format,
 200      * returns an array of length 0.
 201      * @return array of supported output formats.
 202      */
 203     /*  public AudioFormat[] getOutputFormats(AudioFormat inputFormat) { */
 204     private AudioFormat[] getOutputFormats(AudioFormat inputFormat) {
 205 
 206         Vector<AudioFormat> formats = new Vector<>();
 207         AudioFormat format;
 208 
 209         if ((inputFormat.getSampleSizeInBits() == 16)
 210             && AudioFormat.Encoding.PCM_SIGNED.equals(inputFormat.getEncoding())) {
 211             format = new AudioFormat(AudioFormat.Encoding.ULAW,
 212                                      inputFormat.getSampleRate(),
 213                                      8,
 214                                      inputFormat.getChannels(),
 215                                      inputFormat.getChannels(),
 216                                      inputFormat.getSampleRate(),
 217                                      false );
 218             formats.addElement(format);
 219         }
 220         if (inputFormat.getSampleSizeInBits() == 8
 221                 && AudioFormat.Encoding.ULAW.equals(inputFormat.getEncoding())) {
 222             format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
 223                                      inputFormat.getSampleRate(), 16,
 224                                      inputFormat.getChannels(),
 225                                      inputFormat.getChannels() * 2,
 226                                      inputFormat.getSampleRate(), false);
 227             formats.addElement(format);
 228 
 229             format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
 230                                      inputFormat.getSampleRate(), 16,
 231                                      inputFormat.getChannels(),
 232                                      inputFormat.getChannels() * 2,
 233                                      inputFormat.getSampleRate(), true);
 234             formats.addElement(format);
 235         }
 236 
 237         AudioFormat[] formatArray = new AudioFormat[formats.size()];
 238         for (int i = 0; i < formatArray.length; i++) {
 239             formatArray[i] = formats.elementAt(i);
 240         }
 241         return formatArray;
 242     }
 243 
 244 
 245     private final class UlawCodecStream extends AudioInputStream {
 246 
 247         private static final int tempBufferSize = 64;
 248         private byte tempBuffer [] = null;
 249 
 250         /**
 251          * True to encode to u-law, false to decode to linear
 252          */
 253         boolean encode = false;
 254 
 255         AudioFormat encodeFormat;
 256         AudioFormat decodeFormat;
 257 
 258         byte tabByte1[] = null;
 259         byte tabByte2[] = null;
 260         int highByte = 0;
 261         int lowByte  = 1;
 262 
 263         UlawCodecStream(AudioInputStream stream, AudioFormat outputFormat) {
 264             super(stream, outputFormat, AudioSystem.NOT_SPECIFIED);
 265 
 266             AudioFormat inputFormat = stream.getFormat();
 267 
 268             // throw an IllegalArgumentException if not ok
 269             if (!(isConversionSupported(outputFormat, inputFormat))) {
 270                 throw new IllegalArgumentException("Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
 271             }
 272 
 273             //$$fb 2002-07-18: fix for 4714846: JavaSound ULAW (8-bit) encoder erroneously depends on endian-ness
 274             boolean PCMIsBigEndian;
 275 
 276             // determine whether we are encoding or decoding
 277             if (AudioFormat.Encoding.ULAW.equals(inputFormat.getEncoding())) {
 278                 encode = false;
 279                 encodeFormat = inputFormat;
 280                 decodeFormat = outputFormat;
 281                 PCMIsBigEndian = outputFormat.isBigEndian();
 282             } else {
 283                 encode = true;
 284                 encodeFormat = outputFormat;
 285                 decodeFormat = inputFormat;
 286                 PCMIsBigEndian = inputFormat.isBigEndian();
 287                 tempBuffer = new byte[tempBufferSize];
 288             }
 289 
 290             // setup tables according to byte order
 291             if (PCMIsBigEndian) {
 292                 tabByte1 = ULAW_TABH;
 293                 tabByte2 = ULAW_TABL;
 294                 highByte = 0;
 295                 lowByte  = 1;
 296             } else {
 297                 tabByte1 = ULAW_TABL;
 298                 tabByte2 = ULAW_TABH;
 299                 highByte = 1;
 300                 lowByte  = 0;
 301             }
 302 
 303             // set the AudioInputStream length in frames if we know it
 304             if (stream instanceof AudioInputStream) {
 305                 frameLength = stream.getFrameLength();
 306             }
 307             // set framePos to zero
 308             framePos = 0;
 309             frameSize = inputFormat.getFrameSize();
 310             if (frameSize == AudioSystem.NOT_SPECIFIED) {
 311                 frameSize = 1;
 312             }
 313         }
 314 
 315 
 316         /*
 317          * $$jb 2/23/99
 318          * Used to determine segment number in uLaw encoding
 319          */
 320         private short search(short val, short table[], short size) {
 321             for(short i = 0; i < size; i++) {
 322                 if (val <= table[i]) { return i; }
 323             }
 324             return size;
 325         }
 326 
 327         /**
 328          * Note that this won't actually read anything; must read in
 329          * two-byte units.
 330          */
 331         public int read() throws IOException {
 332             byte[] b = new byte[1];
 333             if (read(b, 0, b.length) == 1) {
 334                 return b[1] & 0xFF;
 335             }
 336             return -1;
 337         }
 338 
 339         public int read(byte[] b) throws IOException {
 340             return read(b, 0, b.length);
 341         }
 342 
 343         public int read(byte[] b, int off, int len) throws IOException {
 344             // don't read fractional frames
 345             if( len%frameSize != 0 ) {
 346                 len -= (len%frameSize);
 347             }
 348             if (encode) {
 349                 short BIAS = 0x84;
 350                 short mask;
 351                 short seg;
 352                 int i;
 353 
 354                 short sample;
 355                 byte enc;
 356 
 357                 int readCount = 0;
 358                 int currentPos = off;
 359                 int readLeft = len*2;
 360                 int readLen = ( (readLeft>tempBufferSize) ? tempBufferSize : readLeft );
 361 
 362                 while ((readCount = super.read(tempBuffer,0,readLen))>0) {
 363                     for(i = 0; i < readCount; i+=2) {
 364                         /* Get the sample from the tempBuffer */
 365                         sample = (short)(( (tempBuffer[i + highByte]) << 8) & 0xFF00);
 366                         sample |= (short)( (short) (tempBuffer[i + lowByte]) & 0xFF);
 367 
 368                         /* Get the sign and the magnitude of the value. */
 369                         if(sample < 0) {
 370                             sample = (short) (BIAS - sample);
 371                             mask = 0x7F;
 372                         } else {
 373                             sample += BIAS;
 374                             mask = 0xFF;
 375                         }
 376                         /* Convert the scaled magnitude to segment number. */
 377                         seg = search(sample, seg_end, (short) 8);
 378                         /*
 379                          * Combine the sign, segment, quantization bits;
 380                          * and complement the code word.
 381                          */
 382                         if (seg >= 8) {  /* out of range, return maximum value. */
 383                             enc = (byte) (0x7F ^ mask);
 384                         } else {
 385                             enc = (byte) ((seg << 4) | ((sample >> (seg+3)) & 0xF));
 386                             enc ^= mask;
 387                         }
 388                         /* Now put the encoded sample where it belongs */
 389                         b[currentPos] = enc;
 390                         currentPos++;
 391                     }
 392                     /* And update pointers and counters for next iteration */
 393                     readLeft -= readCount;
 394                     readLen = ( (readLeft>tempBufferSize) ? tempBufferSize : readLeft );
 395                 }
 396                 if( currentPos==off && readCount<0 ) {  // EOF or error on read
 397                     return readCount;
 398                 }
 399                 return (currentPos - off);  /* Number of bytes written to new buffer */
 400             } else {
 401                 int i;
 402                 int readLen = len/2;
 403                 int readOffset = off + len/2;
 404                 int readCount = super.read(b, readOffset, readLen);
 405 
 406                 if(readCount<0) {               // EOF or error
 407                     return readCount;
 408                 }
 409                 for (i = off; i < (off + (readCount*2)); i+=2) {
 410                     b[i]        = tabByte1[b[readOffset] & 0xFF];
 411                     b[i+1]      = tabByte2[b[readOffset] & 0xFF];
 412                     readOffset++;
 413                 }
 414                 return (i - off);
 415             }
 416         }
 417 
 418         @Override
 419         public long skip(final long n) throws IOException {
 420             // Implementation of this method assumes that we support
 421             // encoding/decoding from/to 8/16 bits only
 422             return encode ? super.skip(n * 2) / 2 : super.skip(n / 2) * 2;
 423         }
 424     } // end class UlawCodecStream
 425 } // end class ULAW