Coverage Report - org.jaudiotagger.audio.mp3.MPEGFrameHeader
 
Classes in this File Line Coverage Branch Coverage Complexity
MPEGFrameHeader
94%
253/269
74%
43/58
0
 
 1  
 /**
 2  
  * @author : Paul Taylor
 3  
  * <p/>
 4  
  * Version @version:$Id: MPEGFrameHeader.java,v 1.18 2008/07/21 10:48:01 paultaylor Exp $
 5  
  * Date :${DATE}
 6  
  * <p/>
 7  
  * Jaikoz Copyright Copyright (C) 2003 -2005 JThink Ltd
 8  
  */
 9  
 package org.jaudiotagger.audio.mp3;
 10  
 
 11  
 import org.jaudiotagger.FileConstants;
 12  
 import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
 13  
 import org.jaudiotagger.logging.AbstractTagDisplayFormatter;
 14  
 
 15  
 import java.nio.ByteBuffer;
 16  
 import java.util.HashMap;
 17  
 import java.util.Map;
 18  
 
 19  
 
 20  
 /**
 21  
  * Represents a MPEGFrameHeader, an MP3 is made up of a number of frames each frame starts with a four
 22  
  * byte frame header.
 23  
  */
 24  
 public class MPEGFrameHeader
 25  
 {
 26  
     /**
 27  
      * Constants for MP3 Frame header, each frame has a basic header of
 28  
      * 4 bytes
 29  
      */
 30  
     private static final int BYTE_1 = 0;
 31  
     private static final int BYTE_2 = 1;
 32  
     private static final int BYTE_3 = 2;
 33  
     private static final int BYTE_4 = 3;
 34  
     public static final int HEADER_SIZE = 4;
 35  
 
 36  
     /**
 37  
      * Sync Value to identify the start of an MPEGFrame
 38  
      */
 39  
     public static final int SYNC_SIZE = 2;
 40  
 
 41  
     public static final int SYNC_BYTE1 = 0xFF;
 42  
     public static final int SYNC_BYTE2 = 0xE0;
 43  
 
 44  48
     private static int position = 0;
 45  48
     private static final byte[] header = new byte[HEADER_SIZE];
 46  
 
 47  
 
 48  
     /**
 49  
      * Constants for MPEG Version
 50  
      */
 51  48
     public static final Map<Integer, String> mpegVersionMap = new HashMap<Integer, String>();
 52  
     public final static int VERSION_2_5 = 0;
 53  
     public final static int VERSION_2 = 2;
 54  
     public final static int VERSION_1 = 3;
 55  
 
 56  
     static
 57  
     {
 58  48
         mpegVersionMap.put(VERSION_2_5, "MPEG-2.5");
 59  48
         mpegVersionMap.put(VERSION_2, "MPEG-2");
 60  48
         mpegVersionMap.put(VERSION_1, "MPEG-1");
 61  
     }
 62  
 
 63  
     /**
 64  
      * Constants for MPEG Layer
 65  
      */
 66  48
     public static final Map<Integer, String> mpegLayerMap = new HashMap<Integer, String>();
 67  
     public final static int LAYER_I = 3;
 68  
     public final static int LAYER_II = 2;
 69  
     public final static int LAYER_III = 1;
 70  
 
 71  
     static
 72  
     {
 73  48
         mpegLayerMap.put(LAYER_I, "Layer 1");
 74  48
         mpegLayerMap.put(LAYER_II, "Layer 2");
 75  48
         mpegLayerMap.put(LAYER_III, "Layer 3");
 76  
     }
 77  
 
 78  
     /**
 79  
      * Slot Size is dependent on Layer
 80  
      */
 81  
     public final static int LAYER_I_SLOT_SIZE = 4;
 82  
     public final static int LAYER_II_SLOT_SIZE = 1;
 83  
     public final static int LAYER_III_SLOT_SIZE = 1;
 84  
 
 85  
     /**
 86  
      * Bit Rates, the setBitrate varies for different Version and Layer
 87  
      */
 88  48
     private static final Map<Integer, Integer> bitrateMap = new HashMap<Integer, Integer>();
 89  
 
 90  
     static
 91  
     {
 92  
         // MPEG-1, Layer I (E)
 93  48
         bitrateMap.put(0x1E, 32);
 94  48
         bitrateMap.put(0x2E, 64);
 95  48
         bitrateMap.put(0x3E, 96);
 96  48
         bitrateMap.put(0x4E, 128);
 97  48
         bitrateMap.put(0x5E, 160);
 98  48
         bitrateMap.put(0x6E, 192);
 99  48
         bitrateMap.put(0x7E, 224);
 100  48
         bitrateMap.put(0x8E, 256);
 101  48
         bitrateMap.put(0x9E, 288);
 102  48
         bitrateMap.put(0xAE, 320);
 103  48
         bitrateMap.put(0xBE, 352);
 104  48
         bitrateMap.put(0xCE, 384);
 105  48
         bitrateMap.put(0xDE, 416);
 106  48
         bitrateMap.put(0xEE, 448);
 107  
         // MPEG-1, Layer II (C)
 108  48
         bitrateMap.put(0x1C, 32);
 109  48
         bitrateMap.put(0x2C, 48);
 110  48
         bitrateMap.put(0x3C, 56);
 111  48
         bitrateMap.put(0x4C, 64);
 112  48
         bitrateMap.put(0x5C, 80);
 113  48
         bitrateMap.put(0x6C, 96);
 114  48
         bitrateMap.put(0x7C, 112);
 115  48
         bitrateMap.put(0x8C, 128);
 116  48
         bitrateMap.put(0x9C, 160);
 117  48
         bitrateMap.put(0xAC, 192);
 118  48
         bitrateMap.put(0xBC, 224);
 119  48
         bitrateMap.put(0xCC, 256);
 120  48
         bitrateMap.put(0xDC, 320);
 121  48
         bitrateMap.put(0xEC, 384);
 122  
         // MPEG-1, Layer III (A)
 123  48
         bitrateMap.put(0x1A, 32);
 124  48
         bitrateMap.put(0x2A, 40);
 125  48
         bitrateMap.put(0x3A, 48);
 126  48
         bitrateMap.put(0x4A, 56);
 127  48
         bitrateMap.put(0x5A, 64);
 128  48
         bitrateMap.put(0x6A, 80);
 129  48
         bitrateMap.put(0x7A, 96);
 130  48
         bitrateMap.put(0x8A, 112);
 131  48
         bitrateMap.put(0x9A, 128);
 132  48
         bitrateMap.put(0xAA, 160);
 133  48
         bitrateMap.put(0xBA, 192);
 134  48
         bitrateMap.put(0xCA, 224);
 135  48
         bitrateMap.put(0xDA, 256);
 136  48
         bitrateMap.put(0xEA, 320);
 137  
         // MPEG-2, Layer I (6)
 138  48
         bitrateMap.put(0x16, 32);
 139  48
         bitrateMap.put(0x26, 48);
 140  48
         bitrateMap.put(0x36, 56);
 141  48
         bitrateMap.put(0x46, 64);
 142  48
         bitrateMap.put(0x56, 80);
 143  48
         bitrateMap.put(0x66, 96);
 144  48
         bitrateMap.put(0x76, 112);
 145  48
         bitrateMap.put(0x86, 128);
 146  48
         bitrateMap.put(0x96, 144);
 147  48
         bitrateMap.put(0xA6, 160);
 148  48
         bitrateMap.put(0xB6, 176);
 149  48
         bitrateMap.put(0xC6, 192);
 150  48
         bitrateMap.put(0xD6, 224);
 151  48
         bitrateMap.put(0xE6, 256);
 152  
         // MPEG-2, Layer II (4)
 153  48
         bitrateMap.put(0x14, 8);
 154  48
         bitrateMap.put(0x24, 16);
 155  48
         bitrateMap.put(0x34, 24);
 156  48
         bitrateMap.put(0x44, 32);
 157  48
         bitrateMap.put(0x54, 40);
 158  48
         bitrateMap.put(0x64, 48);
 159  48
         bitrateMap.put(0x74, 56);
 160  48
         bitrateMap.put(0x84, 64);
 161  48
         bitrateMap.put(0x94, 80);
 162  48
         bitrateMap.put(0xA4, 96);
 163  48
         bitrateMap.put(0xB4, 112);
 164  48
         bitrateMap.put(0xC4, 128);
 165  48
         bitrateMap.put(0xD4, 144);
 166  48
         bitrateMap.put(0xE4, 160);
 167  
         // MPEG-2, Layer III (2)
 168  48
         bitrateMap.put(0x12, 8);
 169  48
         bitrateMap.put(0x22, 16);
 170  48
         bitrateMap.put(0x32, 24);
 171  48
         bitrateMap.put(0x42, 32);
 172  48
         bitrateMap.put(0x52, 40);
 173  48
         bitrateMap.put(0x62, 48);
 174  48
         bitrateMap.put(0x72, 56);
 175  48
         bitrateMap.put(0x82, 64);
 176  48
         bitrateMap.put(0x92, 80);
 177  48
         bitrateMap.put(0xA2, 96);
 178  48
         bitrateMap.put(0xB2, 112);
 179  48
         bitrateMap.put(0xC2, 128);
 180  48
         bitrateMap.put(0xD2, 144);
 181  48
         bitrateMap.put(0xE2, 160);
 182  
     }
 183  
 
 184  
     /**
 185  
      * Constants for Channel mode
 186  
      */
 187  48
     protected static final Map<Integer, String> modeMap = new HashMap<Integer, String>();
 188  
     public final static int MODE_STEREO = 0;
 189  
     public final static int MODE_JOINT_STEREO = 1;
 190  
     public final static int MODE_DUAL_CHANNEL = 2;
 191  
     public final static int MODE_MONO = 3;
 192  
 
 193  
     static
 194  
     {
 195  48
         modeMap.put(MODE_STEREO, "Stereo");
 196  48
         modeMap.put(MODE_JOINT_STEREO, "Joint Stereo");
 197  48
         modeMap.put(MODE_DUAL_CHANNEL, "Dual");
 198  48
         modeMap.put(MODE_MONO, "Mono");
 199  
     }
 200  
 
 201  
     /**
 202  
      * Constants for Emphasis
 203  
      */
 204  48
     private static final Map<Integer, String> emphasisMap = new HashMap<Integer, String>();
 205  
     public final static int EMPHASIS_NONE = 0;
 206  
     public final static int EMPHASIS_5015MS = 1;
 207  
     public final static int EMPHASIS_RESERVED = 2;
 208  
     public final static int EMPHASIS_CCITT = 3;
 209  
 
 210  
     static
 211  
     {
 212  48
         emphasisMap.put(EMPHASIS_NONE, "None");
 213  48
         emphasisMap.put(EMPHASIS_5015MS, "5015MS");
 214  48
         emphasisMap.put(EMPHASIS_RESERVED, "Reserved");
 215  48
         emphasisMap.put(EMPHASIS_CCITT, "CCITT");
 216  
     }
 217  
 
 218  
 
 219  48
     private static final Map<Integer, String> modeExtensionMap = new HashMap<Integer, String>();
 220  
     private final static int MODE_EXTENSION_NONE = 0;
 221  
     private final static int MODE_EXTENSION_ONE = 1;
 222  
     private final static int MODE_EXTENSION_TWO = 2;
 223  
     private final static int MODE_EXTENSION_THREE = 3;
 224  
 
 225  48
     private static final Map<Integer, String> modeExtensionLayerIIIMap = new HashMap<Integer, String>();
 226  
     private final static int MODE_EXTENSION_OFF_OFF = 0;
 227  
     private final static int MODE_EXTENSION_ON_OFF = 1;
 228  
     private final static int MODE_EXTENSION_OFF_ON = 2;
 229  
     private final static int MODE_EXTENSION_ON_ON = 3;
 230  
 
 231  
     static
 232  
     {
 233  48
         modeExtensionMap.put(MODE_EXTENSION_NONE, "4-31");
 234  48
         modeExtensionMap.put(MODE_EXTENSION_ONE, "8-31");
 235  48
         modeExtensionMap.put(MODE_EXTENSION_TWO, "12-31");
 236  48
         modeExtensionMap.put(MODE_EXTENSION_THREE, "16-31");
 237  
 
 238  48
         modeExtensionLayerIIIMap.put(MODE_EXTENSION_OFF_OFF, "off-off");
 239  48
         modeExtensionLayerIIIMap.put(MODE_EXTENSION_ON_OFF, "on-off");
 240  48
         modeExtensionLayerIIIMap.put(MODE_EXTENSION_OFF_ON, "off-on");
 241  48
         modeExtensionLayerIIIMap.put(MODE_EXTENSION_ON_ON, "on-on");
 242  
     }
 243  
 
 244  
     /**
 245  
      * Sampling Rate in Hz
 246  
      */
 247  48
     private static final Map<Integer, Map<Integer, Integer>> samplingRateMap = new HashMap<Integer, Map<Integer, Integer>>();
 248  48
     private static final Map<Integer, Integer> samplingV1Map = new HashMap<Integer, Integer>();
 249  48
     private static final Map<Integer, Integer> samplingV2Map = new HashMap<Integer, Integer>();
 250  48
     private static final Map<Integer, Integer> samplingV25Map = new HashMap<Integer, Integer>();
 251  
 
 252  
     static
 253  
     {
 254  48
         samplingV1Map.put(0, 44100);
 255  48
         samplingV1Map.put(1, 48000);
 256  48
         samplingV1Map.put(2, 32000);
 257  
 
 258  48
         samplingV2Map.put(0, 22050);
 259  48
         samplingV2Map.put(1, 24000);
 260  48
         samplingV2Map.put(2, 16000);
 261  
 
 262  48
         samplingV25Map.put(0, 11025);
 263  48
         samplingV25Map.put(1, 12000);
 264  48
         samplingV25Map.put(2, 8000);
 265  
 
 266  48
         samplingRateMap.put(VERSION_1, samplingV1Map);
 267  48
         samplingRateMap.put(VERSION_2, samplingV2Map);
 268  48
         samplingRateMap.put(VERSION_2_5, samplingV25Map);
 269  
     }
 270  
 
 271  
     /* Samples Per Frame */
 272  48
     private static final Map<Integer, Map<Integer, Integer>> samplesPerFrameMap = new HashMap<Integer, Map<Integer, Integer>>();
 273  48
     private static final Map<Integer, Integer> samplesPerFrameV1Map = new HashMap<Integer, Integer>();
 274  48
     private static final Map<Integer, Integer> samplesPerFrameV2Map = new HashMap<Integer, Integer>();
 275  48
     private static final Map<Integer, Integer> samplesPerFrameV25Map = new HashMap<Integer, Integer>();
 276  
 
 277  
     static
 278  
     {
 279  48
         samplesPerFrameV1Map.put(LAYER_I, 384);
 280  48
         samplesPerFrameV1Map.put(LAYER_II, 1152);
 281  48
         samplesPerFrameV1Map.put(LAYER_III, 1152);
 282  
 
 283  48
         samplesPerFrameV2Map.put(LAYER_I, 384);
 284  48
         samplesPerFrameV2Map.put(LAYER_II, 1152);
 285  48
         samplesPerFrameV2Map.put(LAYER_III, 1152);
 286  
 
 287  48
         samplesPerFrameV25Map.put(LAYER_I, 384);
 288  48
         samplesPerFrameV25Map.put(LAYER_II, 1152);
 289  48
         samplesPerFrameV25Map.put(LAYER_III, 1152);
 290  
 
 291  48
         samplesPerFrameMap.put(VERSION_1, samplesPerFrameV1Map);
 292  48
         samplesPerFrameMap.put(VERSION_2, samplesPerFrameV2Map);
 293  48
         samplesPerFrameMap.put(VERSION_2_5, samplesPerFrameV25Map);
 294  
 
 295  48
     }
 296  
 
 297  
 
 298  
     private static final int SCALE_BY_THOUSAND = 1000;
 299  
     private static final int LAYER_I_FRAME_SIZE_COEFFICIENT = 12;
 300  
     private static final int LAYER_II_FRAME_SIZE_COEFFICIENT = 144;
 301  
     private static final int LAYER_III_FRAME_SIZE_COEFFICIENT = 144;
 302  
 
 303  
     /**
 304  
      * MP3 Frame Header bit mask
 305  
      */
 306  
     private static final int MASK_MP3_ID = FileConstants.BIT3;
 307  
 
 308  
     /**
 309  
      * MP3 version, confusingly for MP3s the version is 1.
 310  
      */
 311  
     private static final int MASK_MP3_VERSION = FileConstants.BIT4 | FileConstants.BIT3;
 312  
 
 313  
     /**
 314  
      * MP3 Layer, for MP3s the Layer is 3
 315  
      */
 316  
     private static final int MASK_MP3_LAYER = FileConstants.BIT2 | FileConstants.BIT1;
 317  
 
 318  
     /**
 319  
      * Does it include a CRC Checksum at end of header, this can be used to check the header.
 320  
      */
 321  
     private static final int MASK_MP3_PROTECTION = FileConstants.BIT0;
 322  
 
 323  
     /**
 324  
      * The setBitrate of this MP3
 325  
      */
 326  
     private static final int MASK_MP3_BITRATE = FileConstants.BIT7 | FileConstants.BIT6 | FileConstants.BIT5 | FileConstants.BIT4;
 327  
 
 328  
     /**
 329  
      * The sampling/frequency rate
 330  
      */
 331  
     private static final int MASK_MP3_FREQUENCY = FileConstants.BIT3 + FileConstants.BIT2;
 332  
 
 333  
     /**
 334  
      * An extra padding bit is sometimes used to make sure frames are exactly the right length
 335  
      */
 336  
     private static final int MASK_MP3_PADDING = FileConstants.BIT1;
 337  
 
 338  
     /**
 339  
      * Private bit set, for application specific
 340  
      */
 341  
     private static final int MASK_MP3_PRIVACY = FileConstants.BIT0;
 342  
 
 343  
     /**
 344  
      * Channel Mode, Stero/Mono/Dual Channel
 345  
      */
 346  
     private static final int MASK_MP3_MODE = FileConstants.BIT7 | FileConstants.BIT6;
 347  
 
 348  
     /**
 349  
      * MP3 Frame Header bit mask
 350  
      */
 351  
     private static final int MASK_MP3_MODE_EXTENSION = FileConstants.BIT5 | FileConstants.BIT4;
 352  
 
 353  
     /**
 354  
      * MP3 Frame Header bit mask
 355  
      */
 356  
     private static final int MASK_MP3_COPY = FileConstants.BIT3;
 357  
 
 358  
     /**
 359  
      * MP3 Frame Header bit mask
 360  
      */
 361  
     private static final int MASK_MP3_HOME = FileConstants.BIT2;
 362  
 
 363  
     /**
 364  
      * MP3 Frame Header bit mask
 365  
      */
 366  
     private static final int MASK_MP3_EMPHASIS = FileConstants.BIT1 | FileConstants.BIT0;
 367  
 
 368  
 
 369  
     private byte[] mpegBytes;
 370  
 
 371  
     /**
 372  
      * The version of this MPEG frame (see the constants)
 373  
      */
 374  
     private int version;
 375  
 
 376  
     private String versionAsString;
 377  
 
 378  
     /**
 379  
      * Contains the mpeg layer of this frame (see constants)
 380  
      */
 381  
     private int layer;
 382  
 
 383  
     private String layerAsString;
 384  
     /**
 385  
      * Bitrate of this frame
 386  
      */
 387  
     private Integer bitRate;
 388  
 
 389  
     /**
 390  
      * Channel Mode of this Frame (see constants)
 391  
      */
 392  
     private int channelMode;
 393  
 
 394  
     /**
 395  
      * Channel Mode of this Frame As English String
 396  
      */
 397  
     private String channelModeAsString;
 398  
 
 399  
     /**
 400  
      * Emphasis of this frame
 401  
      */
 402  
     private int emphasis;
 403  
 
 404  
     /**
 405  
      * Emphasis mode string
 406  
      */
 407  
     private String emphasisAsString;
 408  
 
 409  
     /**
 410  
      * Mode Extension
 411  
      */
 412  
     private String modeExtension;
 413  
 
 414  
     /**
 415  
      * Flag indicating if this frame has padding byte
 416  
      */
 417  
     private boolean isPadding;
 418  
 
 419  
     /**
 420  
      * Flag indicating if this frame contains copyrighted material
 421  
      */
 422  
     private boolean isCopyrighted;
 423  
 
 424  
     /**
 425  
      * Flag indicating if this frame contains original material
 426  
      */
 427  
     private boolean isOriginal;
 428  
 
 429  
     /**
 430  
      * Flag indicating if this frame is protected
 431  
      */
 432  
     private boolean isProtected;
 433  
 
 434  
 
 435  
     /**
 436  
      * Flag indicating if this frame is private
 437  
      */
 438  
     private boolean isPrivate;
 439  
 
 440  
     private Integer samplingRate;
 441  
 
 442  
 
 443  
     /**
 444  
      * Gets the layerVersion attribute of the MPEGFrame object
 445  
      *
 446  
      * @return The layerVersion value
 447  
      */
 448  
     public int getLayer()
 449  
     {
 450  30
         return layer;
 451  
     }
 452  
 
 453  
     public String getLayerAsString()
 454  
     {
 455  16
         return layerAsString;
 456  
     }
 457  
 
 458  
     /**
 459  
      * Gets the copyrighted attribute of the MPEGFrame object
 460  
      */
 461  
     private void setCopyrighted()
 462  
     {
 463  514
         isCopyrighted = (mpegBytes[BYTE_4] & MASK_MP3_COPY) != 0;
 464  514
     }
 465  
 
 466  
 
 467  
     /**
 468  
      * Set the version of this frame as an int value (see constants)
 469  
      */
 470  
     private void setVersion() throws InvalidAudioFrameException
 471  
     {
 472  
         //MPEG Version
 473  574
         version = (byte) ((mpegBytes[BYTE_2] & MASK_MP3_VERSION) >> 3);
 474  574
         versionAsString = mpegVersionMap.get(version);
 475  574
         if (versionAsString == null)
 476  
         {
 477  0
             throw new InvalidAudioFrameException("Invalid mpeg version");
 478  
         }
 479  574
     }
 480  
 
 481  
     /**
 482  
      * Sets the original attribute of the MPEGFrame object
 483  
      */
 484  
     private void setOriginal()
 485  
     {
 486  514
         isOriginal = (mpegBytes[BYTE_4] & MASK_MP3_HOME) != 0;
 487  514
     }
 488  
 
 489  
     /**
 490  
      * Sets the protected attribute of the MPEGFrame object
 491  
      */
 492  
     private void setProtected()
 493  
     {
 494  572
         isProtected = (mpegBytes[BYTE_2] & MASK_MP3_PROTECTION) == 0x00;
 495  572
     }
 496  
 
 497  
     /**
 498  
      * Sets the private attribute of the MPEGFrame object
 499  
      */
 500  
     private void setPrivate()
 501  
     {
 502  514
         isPrivate = (mpegBytes[BYTE_3] & MASK_MP3_PRIVACY) != 0;
 503  514
     }
 504  
 
 505  
     /**
 506  
      * Get the setBitrate of this frame
 507  
      */
 508  
     private void setBitrate() throws InvalidAudioFrameException
 509  
     {
 510  
         /* BitRate, get by checking header setBitrate bits and MPEG Version and Layer */
 511  572
         int bitRateIndex = mpegBytes[BYTE_3] & MASK_MP3_BITRATE | mpegBytes[BYTE_2] & MASK_MP3_ID | mpegBytes[BYTE_2] & MASK_MP3_LAYER;
 512  
 
 513  572
         bitRate = bitrateMap.get(bitRateIndex);
 514  572
         if (bitRate == null)
 515  
         {
 516  44
             throw new InvalidAudioFrameException("Invalid bitrate");
 517  
         }
 518  528
     }
 519  
 
 520  
 
 521  
     /**
 522  
      * Set the Mpeg channel mode of this frame as a constant (see constants)
 523  
      */
 524  
     private void setChannelMode() throws InvalidAudioFrameException
 525  
     {
 526  514
         channelMode = (mpegBytes[BYTE_4] & MASK_MP3_MODE) >>> 6;
 527  514
         channelModeAsString = modeMap.get(channelMode);
 528  514
         if (channelModeAsString == null)
 529  
         {
 530  0
             throw new InvalidAudioFrameException("Invalid channel mode");
 531  
         }
 532  514
     }
 533  
 
 534  
     /**
 535  
      * Get the setEmphasis mode of this frame in a string representation
 536  
      */
 537  
     private void setEmphasis() throws InvalidAudioFrameException
 538  
     {
 539  514
         emphasis = mpegBytes[BYTE_4] & MASK_MP3_EMPHASIS;
 540  514
         emphasisAsString = emphasisMap.get(emphasis);
 541  514
         if (getEmphasisAsString() == null)
 542  
         {
 543  0
             throw new InvalidAudioFrameException("Invalid emphasis");
 544  
         }
 545  514
     }
 546  
 
 547  
 
 548  
     /**
 549  
      * Set whether this frame uses padding bytes
 550  
      */
 551  
     private void setPadding()
 552  
     {
 553  514
         isPadding = (mpegBytes[BYTE_3] & MASK_MP3_PADDING) != 0;
 554  514
     }
 555  
 
 556  
 
 557  
     /**
 558  
      * Get the layer version of this frame as a constant int value (see constants)
 559  
      */
 560  
     private void setLayer() throws InvalidAudioFrameException
 561  
     {
 562  574
         layer = (mpegBytes[BYTE_2] & MASK_MP3_LAYER) >>> 1;
 563  574
         layerAsString = mpegLayerMap.get(layer);
 564  574
         if (layerAsString == null)
 565  
         {
 566  2
             throw new InvalidAudioFrameException("Invalid Layer");
 567  
         }
 568  572
     }
 569  
 
 570  
 
 571  
     /**
 572  
      * Sets the string representation of the mode extension of this frame
 573  
      */
 574  
     private void setModeExtension() throws InvalidAudioFrameException
 575  
     {
 576  514
         int index = (mpegBytes[BYTE_4] & MASK_MP3_MODE_EXTENSION) >> 4;
 577  514
         if (layer == LAYER_III)
 578  
         {
 579  463
             modeExtension = modeExtensionLayerIIIMap.get(index);
 580  463
             if (getModeExtension() == null)
 581  
             {
 582  0
                 throw new InvalidAudioFrameException("Invalid Mode Extension");
 583  
             }
 584  
         }
 585  
         else
 586  
         {
 587  51
             modeExtension = modeExtensionMap.get(index);
 588  51
             if (getModeExtension() == null)
 589  
             {
 590  0
                 throw new InvalidAudioFrameException("Invalid Mode Extension");
 591  
             }
 592  
         }
 593  514
     }
 594  
 
 595  
     /**
 596  
      * set the sampling rate in Hz of this frame
 597  
      */
 598  
     private void setSamplingRate() throws InvalidAudioFrameException
 599  
     {
 600  
         //Frequency
 601  528
         int index = (mpegBytes[BYTE_3] & MASK_MP3_FREQUENCY) >>> 2;
 602  528
         Map<Integer, Integer> samplingRateMapForVersion = samplingRateMap.get(version);
 603  528
         if (samplingRateMapForVersion == null)
 604  
         {
 605  0
             throw new InvalidAudioFrameException("Invalid version");
 606  
         }
 607  528
         samplingRate = samplingRateMapForVersion.get(index);
 608  528
         if (samplingRate == null)
 609  
         {
 610  14
             throw new InvalidAudioFrameException("Invalid sampling rate");
 611  
         }
 612  514
     }
 613  
 
 614  
     /**
 615  
      * Gets the number of channels
 616  
      *
 617  
      * @return The setChannelMode value
 618  
      */
 619  
     public int getNumberOfChannels()
 620  
     {
 621  16
         switch (channelMode)
 622  
         {
 623  
             case MODE_DUAL_CHANNEL:
 624  0
                 return 2;
 625  
             case MODE_JOINT_STEREO:
 626  3
                 return 2;
 627  
             case MODE_MONO:
 628  13
                 return 1;
 629  
             case MODE_STEREO:
 630  0
                 return 2;
 631  
             default:
 632  0
                 return 0;
 633  
         }
 634  
     }
 635  
 
 636  
     public int getChannelMode()
 637  
     {
 638  555
         return channelMode;
 639  
     }
 640  
 
 641  
     public String getChannelModeAsString()
 642  
     {
 643  16
         return channelModeAsString;
 644  
     }
 645  
 
 646  
     /**
 647  
      * Gets the mPEGVersion attribute of the MPEGFrame object
 648  
      *
 649  
      * @return The mPEGVersion value
 650  
      */
 651  
     public int getVersion()
 652  
     {
 653  1382
         return version;
 654  
     }
 655  
 
 656  
     public String getVersionAsString()
 657  
     {
 658  16
         return versionAsString;
 659  
     }
 660  
 
 661  
     /**
 662  
      * Gets the paddingLength attribute of the MPEGFrame object
 663  
      *
 664  
      * @return The paddingLength value
 665  
      */
 666  
     public int getPaddingLength()
 667  
     {
 668  732
         if (isPadding())
 669  
         {
 670  177
             return 1;
 671  
         }
 672  
         else
 673  
         {
 674  555
             return 0;
 675  
         }
 676  
     }
 677  
 
 678  
     public Integer getBitRate()
 679  
     {
 680  1168
         return bitRate;
 681  
     }
 682  
 
 683  
     public Integer getSamplingRate()
 684  
     {
 685  1195
         return samplingRate;
 686  
     }
 687  
 
 688  
     /*
 689  
      * Gets this frame length in bytes, value should always be rounded down to the nearest byte (not rounded up)
 690  
      *
 691  
      * Calculation is Bitrate (scaled to bps) divided by sampling frequency (in Hz), The larger the bitrate the larger
 692  
      * the frame but the more samples per second the smaller the value, also have to take into account frame padding
 693  
      * Have to multiple by a coefficient constant depending upon the layer it is encoded in,
 694  
 
 695  
      */
 696  
     public int getFrameLength()
 697  
     {
 698  732
         switch (version)
 699  
         {
 700  
             case VERSION_2:
 701  
             case VERSION_2_5:
 702  70
                 switch (layer)
 703  
                 {
 704  
                     case LAYER_I:
 705  8
                         return (LAYER_I_FRAME_SIZE_COEFFICIENT * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength()) * LAYER_I_SLOT_SIZE;
 706  
 
 707  
                     case LAYER_II:
 708  10
                         if (this.getChannelMode() == MODE_MONO)
 709  
                         {
 710  0
                             return (LAYER_II_FRAME_SIZE_COEFFICIENT / 2) * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength() * LAYER_II_SLOT_SIZE;
 711  
                         }
 712  
                         else
 713  
                         {
 714  10
                             return (LAYER_II_FRAME_SIZE_COEFFICIENT) * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength() * LAYER_II_SLOT_SIZE;
 715  
                         }
 716  
 
 717  
                     case LAYER_III:
 718  52
                         if (this.getChannelMode() == MODE_MONO)
 719  
                         {
 720  51
                             return (LAYER_III_FRAME_SIZE_COEFFICIENT / 2) * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength() * LAYER_III_SLOT_SIZE;
 721  
                         }
 722  
                         else
 723  
                         {
 724  1
                             return (LAYER_III_FRAME_SIZE_COEFFICIENT) * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength() * LAYER_III_SLOT_SIZE;
 725  
                         }
 726  
 
 727  
 
 728  
                     default:
 729  0
                         throw new RuntimeException("Mp3 Unknown Layer:" + layer);
 730  
 
 731  
                 }
 732  
 
 733  
 
 734  
             case VERSION_1:
 735  662
                 switch (layer)
 736  
                 {
 737  
                     case LAYER_I:
 738  139
                         return (LAYER_I_FRAME_SIZE_COEFFICIENT * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength()) * LAYER_I_SLOT_SIZE;
 739  
 
 740  
                     case LAYER_II:
 741  34
                         return LAYER_II_FRAME_SIZE_COEFFICIENT * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength() * LAYER_II_SLOT_SIZE;
 742  
 
 743  
                     case LAYER_III:
 744  489
                         return LAYER_III_FRAME_SIZE_COEFFICIENT * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength() * LAYER_III_SLOT_SIZE;
 745  
 
 746  
                     default:
 747  0
                         throw new RuntimeException("Mp3 Unknown Layer:" + layer);
 748  
 
 749  
                 }
 750  
 
 751  
             default:
 752  0
                 throw new RuntimeException("Mp3 Unknown Version:" + version);
 753  
 
 754  
         }
 755  
     }
 756  
 
 757  
     /**
 758  
      * Get the number of samples in a frame, all frames in a file have a set number of samples as defined by their MPEG Versiona
 759  
      * and Layer
 760  
      */
 761  
     public int getNoOfSamples()
 762  
     {
 763  475
         Integer noOfSamples = samplesPerFrameMap.get(version).get(layer);
 764  475
         return noOfSamples;
 765  
     }
 766  
 
 767  
 
 768  
     public boolean isPadding()
 769  
     {
 770  732
         return isPadding;
 771  
     }
 772  
 
 773  
     public boolean isCopyrighted()
 774  
     {
 775  13
         return isCopyrighted;
 776  
     }
 777  
 
 778  
     public boolean isOriginal()
 779  
     {
 780  13
         return isOriginal;
 781  
     }
 782  
 
 783  
     public boolean isProtected()
 784  
     {
 785  13
         return isProtected;
 786  
     }
 787  
 
 788  
     public boolean isPrivate()
 789  
     {
 790  13
         return isPrivate;
 791  
     }
 792  
 
 793  
     public boolean isVariableBitRate()
 794  
     {
 795  29
         return false;
 796  
     }
 797  
 
 798  
     public int getEmphasis()
 799  
     {
 800  0
         return emphasis;
 801  
     }
 802  
 
 803  
     public String getEmphasisAsString()
 804  
     {
 805  514
         return emphasisAsString;
 806  
     }
 807  
 
 808  
     public String getModeExtension()
 809  
     {
 810  514
         return modeExtension;
 811  
     }
 812  
 
 813  
 
 814  
     /**
 815  
      * Hide Constructor
 816  
      */
 817  
     private MPEGFrameHeader() throws InvalidAudioFrameException
 818  0
     {
 819  
 
 820  0
     }
 821  
 
 822  
     /**
 823  
      * Try and create a new MPEG frame with the given byte array and decodes its contents
 824  
      * If decoding header causes a problem it is not a valid header
 825  
      *
 826  
      * @param b the array of bytes representing this mpeg frame
 827  
      * @throws InvalidAudioFrameException if does not match expected format
 828  
      */
 829  
     private MPEGFrameHeader(byte[] b) throws InvalidAudioFrameException
 830  574
     {
 831  574
         mpegBytes = b;
 832  574
         setVersion();
 833  574
         setLayer();
 834  572
         setProtected();
 835  572
         setBitrate();
 836  528
         setSamplingRate();
 837  514
         setPadding();
 838  514
         setPrivate();
 839  514
         setChannelMode();
 840  514
         setModeExtension();
 841  514
         setCopyrighted();
 842  514
         setOriginal();
 843  514
         setEmphasis();
 844  514
     }
 845  
 
 846  
     /**
 847  
      * Parse the MPEGFrameHeader of an MP3File, file pointer returns at end of the frame header
 848  
      *
 849  
      * @param bb the byte buffer containing the header
 850  
      * @return
 851  
      * @throws InvalidAudioFrameException if there is no header at this point
 852  
      */
 853  
     public static MPEGFrameHeader parseMPEGHeader(ByteBuffer bb) throws InvalidAudioFrameException
 854  
     {
 855  574
         int position = bb.position();
 856  574
         bb.get(header, 0, HEADER_SIZE);
 857  574
         bb.position(position);
 858  574
         MPEGFrameHeader frameHeader = new MPEGFrameHeader(header);
 859  
 
 860  514
         return frameHeader;
 861  
     }
 862  
 
 863  
     /**
 864  
      * Gets the MPEGFrame attribute of the MPEGFrame object
 865  
      *
 866  
      * @return The mPEGFrame value
 867  
      */
 868  
     public static boolean isMPEGFrame(ByteBuffer bb)
 869  
     {
 870  99474
         position = bb.position();
 871  99474
         return (((bb.get(position) & SYNC_BYTE1) == SYNC_BYTE1) && ((bb.get(position + 1) & SYNC_BYTE2) == SYNC_BYTE2));
 872  
 
 873  
     }
 874  
 
 875  
     /**
 876  
      * @return a string represntation
 877  
      */
 878  
     public String toString()
 879  
     {
 880  25
         return " mpeg frameheader:" + " frame length:" + getFrameLength() + " version:" + versionAsString + " layer:" + layerAsString + " channelMode:" + channelModeAsString + " noOfSamples:" + getNoOfSamples() + " samplingRate:" + samplingRate + " isPadding:" + isPadding + " isProtected:" + isProtected + " isPrivate:" + isPrivate + " isCopyrighted:" + isCopyrighted + " isOriginal:" + isCopyrighted + " isVariableBitRate" + this.isVariableBitRate() + " header as binary:" + AbstractTagDisplayFormatter.displayAsBinary(mpegBytes[BYTE_1]) + " " + AbstractTagDisplayFormatter.displayAsBinary(mpegBytes[BYTE_2]) + " " + AbstractTagDisplayFormatter.displayAsBinary(mpegBytes[BYTE_3]) + " " + AbstractTagDisplayFormatter.displayAsBinary(mpegBytes[BYTE_4]);
 881  
     }
 882  
 }
 883