Added TagLib (with AUTORS and COPYING files)
[someplayer] / src / taglib / mpeg / xingheader.cpp
1 /***************************************************************************
2     copyright            : (C) 2003 by Ismael Orenstein
3     email                : orenstein@kde.org
4  ***************************************************************************/
5
6 /***************************************************************************
7  *   This library is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU Lesser General Public License version   *
9  *   2.1 as published by the Free Software Foundation.                     *
10  *                                                                         *
11  *   This library is distributed in the hope that it will be useful, but   *
12  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
14  *   Lesser General Public License for more details.                       *
15  *                                                                         *
16  *   You should have received a copy of the GNU Lesser General Public      *
17  *   License along with this library; if not, write to the Free Software   *
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
19  *   USA                                                                   *
20  *                                                                         *
21  *   Alternatively, this file is available under the Mozilla Public        *
22  *   License Version 1.1.  You may obtain a copy of the License at         *
23  *   http://www.mozilla.org/MPL/                                           *
24  ***************************************************************************/
25
26 #include <tbytevector.h>
27 #include <tstring.h>
28 #include <tdebug.h>
29
30 #include "xingheader.h"
31
32 using namespace TagLib;
33
34 class MPEG::XingHeader::XingHeaderPrivate
35 {
36 public:
37   XingHeaderPrivate() :
38     frames(0),
39     size(0),
40     valid(false)
41     {}
42
43   uint frames;
44   uint size;
45   bool valid;
46 };
47
48 MPEG::XingHeader::XingHeader(const ByteVector &data)
49 {
50   d = new XingHeaderPrivate;
51   parse(data);
52 }
53
54 MPEG::XingHeader::~XingHeader()
55 {
56   delete d;
57 }
58
59 bool MPEG::XingHeader::isValid() const
60 {
61   return d->valid;
62 }
63
64 TagLib::uint MPEG::XingHeader::totalFrames() const
65 {
66   return d->frames;
67 }
68
69 TagLib::uint MPEG::XingHeader::totalSize() const
70 {
71   return d->size;
72 }
73
74 int MPEG::XingHeader::xingHeaderOffset(TagLib::MPEG::Header::Version v,
75                                        TagLib::MPEG::Header::ChannelMode c)
76 {
77   if(v == MPEG::Header::Version1) {
78     if(c == MPEG::Header::SingleChannel)
79       return 0x15;
80     else
81       return 0x24;
82   }
83   else {
84     if(c == MPEG::Header::SingleChannel)
85       return 0x0D;
86     else
87       return 0x15;
88   }
89 }
90
91 void MPEG::XingHeader::parse(const ByteVector &data)
92 {
93   // Check to see if a valid Xing header is available.
94
95   if(!data.startsWith("Xing") && !data.startsWith("Info"))
96     return;
97
98   // If the XingHeader doesn't contain the number of frames and the total stream
99   // info it's invalid.
100
101   if(!(data[7] & 0x01)) {
102     debug("MPEG::XingHeader::parse() -- Xing header doesn't contain the total number of frames.");
103     return;
104   }
105
106   if(!(data[7] & 0x02)) {
107     debug("MPEG::XingHeader::parse() -- Xing header doesn't contain the total stream size.");
108     return;
109   }
110
111   d->frames = data.mid(8, 4).toUInt();
112   d->size = data.mid(12, 4).toUInt();
113
114   d->valid = true;
115 }