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) 2001 Jonathan Hilliker
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 /**
022 * Reads information from a Xing info header. Thanks to the Scilla project
023 * (http://www.xs4all.nl/~rwvtveer/scilla/) for code ideas.
024 */
025 public class XingVBRHeader extends AbstractVBRHeader {
026 private static final Logger logger = Logger.getLogger(MPEGAudioFrameHeader.class.getPackage().getName());
027
028 /**
029 * The XING header identifier.
030 */
031 private static final String IDENTIFIER = "Xing";
032
033 /**
034 * the Size of the Flags
035 */
036 private static final int FLAGS_SIZE = 4;
037 /**
038 * the TOC Size
039 */
040 private static final int TOC_SIZE = 100;
041
042 /**
043 * number of Frames
044 */
045 private int numFrames = -1;
046 /**
047 * number of Bytes
048 */
049 private int numBytes = -1;
050 /**
051 * ???
052 */
053 private int vbrScale = -1;
054 /**
055 * ???
056 */
057 private byte[] toc;
058 /**
059 * the length
060 */
061 private int length = -1;
062 /**
063 * Exsistence Mag
064 */
065 private boolean exists = false;
066
067 private int filesize = -1;
068
069
070 /**
071 * Looks for a Xing VBR header in the file. If it is found then that means
072 * this is a variable bit rate file and all the data in the header will be
073 * parsed. The position of the header buffer will be unchanged, and its mark
074 * will be set to its position when this method returns.
075 *
076 *@param xingbytes the bytes to read the xing header from
077 *@param layer the layer value read by the
078 * MPEGAudioFrameHeader
079 *@param mpegVersion the version value read by the
080 * MPEGAudioFrameHeader
081 *@param sampleRate the sample rate read by the
082 * MPEGAudioFrameHeader
083 *@param channelMode the channel mode read by the
084 * MPEGAudioFrameHeader
085 *@exception IOException if an error occurs
086 *@exception CorruptHeaderException if an error occurs
087 */
088 public XingVBRHeader(ByteBuffer header, int layer,
089 int mpegVersion, int sampleRate,
090 int channelMode, long filesize)
091 throws IOException{
092
093 if(filesize <= Integer.MAX_VALUE){
094 this.filesize = (int) filesize;
095 }
096
097 header.mark();
098 exists = checkHeader(header, channelMode, mpegVersion);
099
100 if (exists) {
101 readHeader(header);
102 super.calc(layer, mpegVersion, sampleRate);
103 }
104 header.reset();
105 }
106
107
108 /**
109 * Checks to see if a xing header is in this file. If so the buffer will
110 * be positioned after the HEAD_START bytes
111 *
112 * @param raf the file to read from
113 * @param offset the location of the first mpeg frame
114 * @param channelMode the channel mode read by the MPEGAudioFrameHeader
115 * @param mpegVersion the version value read by the MPEGAudioFrameHeader
116 * @return whether the header was found
117 * @exception IOException if an error occurs
118 */
119 private boolean checkHeader(ByteBuffer header, int channelMode,
120 int mpegVersion)
121 {
122 int hs;
123 byte[] id = null;
124 try{
125 id = IDENTIFIER.getBytes("US-ASCII");
126 } catch (Exception ex){
127 throw new Error(ex);
128 }
129 if (mpegVersion == MPEGAudioFrameHeader.MPEG_V_1) {
130 hs = (channelMode == MPEGAudioFrameHeader.MONO_MODE)? 21 : 36;
131 } else {
132 hs = (channelMode == MPEGAudioFrameHeader.MONO_MODE)? 13 : 21;
133 }
134 int j = 0;
135 int i = 0;
136 for(;i<40 && j < id.length; i++){
137 byte c = header.get();
138 if(c == id[j]){
139 j++;
140 } else {
141 j = 0;
142 }
143 }
144 boolean result = (j == id.length);
145 if(result){
146 if((i -j) != hs){
147 logger.warning("Xing header found at position " + (i - j) + " after frame start; expected at " + hs);
148 }
149 }
150 return result;
151 }
152
153
154 /**
155 * Parses the header data.
156 *
157 *@param header the data to read from (pointing after the
158 * IDENTIFIER)
159 *@exception IOException if an error occurs
160 *@exception CorruptHeaderException if an error occurs
161 */
162 private void readHeader(ByteBuffer header)
163 throws IOException{
164
165 length = IDENTIFIER.length();
166 byte flags[] = new byte[FLAGS_SIZE];
167 header.get(flags);
168 length += flags.length;
169
170 if (BinaryParser.bitSet(flags[3], 0)) {
171 numFrames = header.getInt();
172 length += 4;
173 }
174 if (BinaryParser.bitSet(flags[3], 1)) {
175 numBytes = header.getInt();
176 length += 4;
177 }
178 if (BinaryParser.bitSet(flags[3], 2)) {
179 toc = new byte[TOC_SIZE];
180 header.get(toc);
181 length += TOC_SIZE;
182 }
183 if (BinaryParser.bitSet(flags[3], 3)) {
184 vbrScale = header.getInt();
185 length += 4;
186 }
187
188 // If bytes aren't given then we can get a reasonable guess with the
189 // file length
190 if (numBytes <= 0 && filesize > 0) {
191 numBytes = filesize;
192 }
193 }
194
195 /**
196 * Returns true if a Xing VBR header was found in the file passed to the
197 * constructor.
198 *
199 *@return whether a Xing VBR header was found
200 */
201 public boolean exists() {
202 return exists;
203 }
204
205
206 /**
207 * Returns the number of MPEG frames in the file passed to the constructor. If
208 * a header is not found, -1 is returned.
209 *
210 *@return the number of MPEG frames
211 */
212 public int getNumFrames() {
213 return numFrames;
214 }
215
216
217 /**
218 * Returns the number of data bytes in the mpeg frames. If a header is not
219 * found, -1 is returned.
220 *
221 *@return the number of data bytes in the mpeg frames
222 */
223 public int getNumBytes() {
224 return numBytes;
225 }
226
227
228 /**
229 * Returns the VBR scale used to generate this VBR file. If a header is not
230 * found, -1 is returned.
231 *
232 *@return the VBR scale used to generate this VBR file
233 */
234 public int getVBRScale() {
235 return vbrScale;
236 }
237
238
239 /**
240 * Returns the toc used to seek to an area of this VBR file. If a header is
241 * not found an empty array is returned.
242 *
243 *@return the toc used to seek to an area of this VBR file
244 */
245 public byte[] getTOC() {
246 return toc;
247 }
248
249 /**
250 * Returns the length (in bytes) of this Xing VBR header.
251 *
252 *@return the length of this Xing VBR header
253 */
254 public int getLength() {
255 return length;
256 }
257
258 }// XingVBRHeader
259