001 package helliker.id3;
002
003 import java.io.IOException;
004 import java.nio.ByteBuffer;
005 import java.util.logging.Logger;
006
007 /*
008 Copyright (C) 2007 Moritz Ringler
009 This library is free software; you can redistribute it and/or
010 modify it under the terms of the GNU Lesser General Public
011 License as published by the Free Software Foundation; either
012 version 2.1 of the License, or (at your option) any later version.
013 This library is distributed in the hope that it will be useful,
014 but WITHOUT ANY WARRANTY; without even the implied warranty of
015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016 Lesser General Public License for more details.
017 You should have received a copy of the GNU Lesser General Public
018 License along with this library; if not, write to the Free Software
019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020 */
021 public class VBRIHeader extends AbstractVBRHeader{
022 /**
023 * The VBRI header identifier;
024 */
025 private static final String IDENTIFIER = "VBRI";
026
027 /**
028 * number of Frames
029 */
030 private int numFrames;
031
032 /**
033 * number of Bytes
034 */
035 private int numBytes;
036
037 /**
038 * VBR quality setting
039 */
040 private int quality;
041
042 /** The TOC scale **/
043 private int tocScale;
044
045 /** bytes per toc entry **/
046 private int tocBytesPerEntry;
047
048 /** frames per toc entry **/
049 private int tocFramesPerEntry;
050
051 /** length of toc in bytes **/
052 private int tocLength;
053
054 /** number of toc entries **/
055 private int tocNumEntries;
056
057 /**
058 * the length
059 */
060 private int length;
061
062
063 /**
064 * Exsistence Mag
065 */
066 private boolean exists = false;
067
068 private int version = -1;
069
070 public VBRIHeader(ByteBuffer header, int layer,
071 int mpegVersion, int sampleRate)// throws IOException
072 {
073 header.mark();
074 exists = checkHeader(header);
075
076 if (exists) {
077 readHeader(header);
078 super.calc(layer, mpegVersion, sampleRate);
079 }
080 header.reset();
081 }
082
083 /**
084 * Returns true if a VBRI header was found in the file passed to the
085 * constructor.
086 *
087 *@return whether a VBRI header was found
088 */
089 public boolean exists() {
090 return exists;
091 }
092
093 /**
094 * Checks to see if a VBRI header is in this file. If so the buffer will
095 * be positioned after the HEAD_START bytes
096 *
097 * @param channelMode the channel mode read by the MPEGAudioFrameHeader
098 * @param mpegVersion the version value read by the MPEGAudioFrameHeader
099 * @return whether the header was found
100 * @exception IOException if an error occurs
101 */
102 private boolean checkHeader(ByteBuffer header)
103 {
104 int hs = 36;
105 byte[] id = null;
106 try{
107 id = IDENTIFIER.getBytes("US-ASCII");
108 } catch (Exception ex){
109 throw new Error(ex);
110 }
111 int i = 0;
112 int j = 0;
113 for(;i<40 && j < id.length; i++){
114 byte c = header.get();
115 if(c == id[j]){
116 j++;
117 } else {
118 j = 0;
119 }
120 }
121 boolean result = (j == id.length);
122 if(result){
123 if((i -j) != hs){
124 logger.warning("VBRI header found at position " + (i - j) + " after frame start; expected at " + hs);
125 }
126 }
127 return result;
128 }
129
130 /**
131 * Parses the header data.
132 *
133 *@param header the data to read from (pointing after the IDENTIFIER)
134 *@exception IOException if an error occurs
135 *@exception CorruptHeaderException if an error occurs
136 */
137 private void readHeader(ByteBuffer header)
138 //throws IOException
139 {
140
141 version = header.getShort() & 0xFFFF; //unsigned short, 2 bytes
142
143 //skip delay, 2 byte float;
144 header.getShort();
145
146 quality = header.getShort() & 0xFFFF; //unsigned short, 2 bytes
147
148 numBytes = header.getInt(); //unsigned int, 4 bytes
149 if(numBytes < 0){
150 logger.warning("Cannot process VBRI headers with more than " + Integer.MAX_VALUE + " bytes ");
151 exists = false;
152 return;
153 }
154
155 numFrames = header.getInt(); //unsigned int, 4 bytes
156 if(numFrames < 0){
157 logger.warning("Cannot process VBRI headers with more than " + Integer.MAX_VALUE + " bytes ");
158 exists = false;
159 return;
160 }
161
162 tocNumEntries = header.getShort() & 0xFFFF; //unsigned short
163
164 tocScale = header.getShort() & 0xFFFF; //unsigned short
165
166 tocBytesPerEntry = header.getShort() & 0xFFFF; //unsigned short
167 if(tocBytesPerEntry > 4){
168 logger.warning("Corrupt VBRI header: toc entry size is " +
169 tocBytesPerEntry + "; max allowed value is 4.");
170 exists = false;
171 return;
172 }
173
174 tocFramesPerEntry = header.getShort() & 0xFFFF; //unsigned short
175
176 tocLength = tocNumEntries * tocBytesPerEntry;
177 //toc[] = new byte[tocNumEntries * tocEntrySize];
178 length = 26 + tocLength;
179 }
180
181 /**
182 * Returns the number of MPEG frames in the file passed to the constructor. If
183 * a header is not found, -1 is returned.
184 *
185 *@return the number of MPEG frames
186 */
187 public int getNumFrames() {
188 return numFrames;
189 }
190
191
192 /**
193 * Returns the number of data bytes in the mpeg frames. If a header is not
194 * found, -1 is returned.
195 *
196 *@return the number of data bytes in the mpeg frames
197 */
198 public int getNumBytes() {
199 return numBytes;
200 }
201
202 /**
203 * Returns the value of quality.
204 */
205 public int getQuality()
206 {
207 return quality;
208 }
209
210 /**
211 * Returns the value of tocScale.
212 */
213 public int getTOCScale()
214 {
215 return tocScale;
216 }
217
218 /**
219 * Returns the number of bytes per TOC entry.
220 */
221 public int getTOCBytesPerEntry()
222 {
223 return tocBytesPerEntry;
224 }
225
226 /**
227 * Returns the number of frames per TOC entry.
228 */
229 public int getTOCFramesPerEntry()
230 {
231 return tocFramesPerEntry;
232 }
233
234 /**
235 * Returns the length of the TOC in bytes.
236 */
237 public int getTOCLength()
238 {
239 return tocLength;
240 }
241
242 /**
243 * Returns the value of tocNumEntries.
244 */
245 public int getTOCNumEntries()
246 {
247 return tocNumEntries;
248 }
249
250 /**
251 * Returns the VBRI version.
252 */
253 public int getVersion()
254 {
255 return version;
256 }
257
258 /** Returns the length of this header in bytes */
259 public int getLength(){
260 return length;
261 }
262 }