]> git.vomp.tv Git - vompclient.git/blob - demuxeraudio.h
Media Player From Andreas Vogel
[vompclient.git] / demuxeraudio.h
1 /*
2     Copyright 2006 Mark Calderbank, Andreas Vogel
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #ifndef DEMUXERAUDIO_H
22 #define DEMUXERAUDIO_H
23
24 #include "demuxer.h"
25 #include "defines.h"
26 #include "id3.h"
27
28 class PacketBuffer;
29 class DemuxerAudio : public Demuxer
30 {
31   public:
32                 typedef struct {
33                         char mpegVersion[8]; //MPEG1/2/2,5
34                         int  mpegLayer;      //1,2,3
35                         int  bitRate; 
36                         int  avrBitrate;
37                         int  sampleRate;
38                         char info[20];       //channel mode,...
39                 } mpegInfo;
40
41                 typedef struct {
42                         ULONG numFrames;
43                         ULONG numBytes;
44                         ULONG fileSeconds;
45                         UCHAR table[100];
46                 } vbrInfo;
47     DemuxerAudio(int p_vID = 0, int p_aID = 0);
48                 virtual ~DemuxerAudio();
49     virtual void flush();
50                 virtual void reset();
51     virtual int scan(UCHAR* buf, int len);
52     virtual int findPTS(UCHAR* buf, int len, ULLONG* dest);
53     virtual void setVID(int p_vID);
54     virtual void setAID(int p_aID);
55     virtual int put(UCHAR* buf, int len);
56
57                 //special functions for the audioplayer
58                 bool isSync() ;
59                 UINT getSyncErrors();
60
61                 //set the skip factor
62                 void setSkipFactor(int faktor);
63
64                 //how many bytes do we need at the file start
65                 //ID3V2, VBR + some spare to detect a first header
66                 static UINT headerBytes() {
67                         return 4096;
68                 }
69                 //how many bytes do we need at the end
70                 static UINT footerBytes() {
71                         return 128;
72                 }
73                 //return a reference to an ID3 tag
74                 //if not found (from checkStart or checkID3)
75                 //NULL will be returned
76                 const id3_tag * getId3Tag();
77                 const mpegInfo * getMpegInfo();
78                 const vbrInfo * getVBRINfo();
79
80                 //check for a valid header at the beginning (and parse all we find)
81                 //will return the offset in buffer where the first valid
82                 //header was found (with potentially skipping ID3 and VBR)
83                 //return -1 if nothing found
84                 int checkStart(UCHAR * buffer, int len);
85                 //check for ID3V1 (at the end)
86                 //return 0 if found
87                 int checkID3(UCHAR * buffer, int len);
88
89                 //return length if we can do this
90                 //otherwise return 0
91                 ULONG getSecondsFromLen(ULONG len);
92
93                 //position within a file
94                 //return 0 if we cannot
95                 ULONG positionFromSeconds(ULONG seconds);
96
97                 //try to read the iD3 tag value (V2xx) and
98                 //fill it into the ID3 store
99                 bool fillId3Tag(id3_tag * tag,UCHAR * frameData, int frameLen, int dataOffset, bool v23);
100
101     const static UINT PACKET_SIZE=4096;
102   private:
103                 /*max size of a packet
104                 see http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm
105                 assumptions: max br 448kb/s, min sample 16k
106                 L2/L3 FrameLengthInBytes = 144 * BitRate / SampleRate + Padding
107                 L2/3 28*144+8 -> 4040
108                 L1 FrameLengthInBytes = (12 * BitRate / SampleRate + Padding) * 4
109                 L1 (336+32)*4 = 1472
110                 */
111                 Log * log;
112                 
113                 int readHeader(UCHAR* buf, int len, bool writeInfo=false);
114
115                 //info flags for the player
116                 bool inSync; //set if we read a valid header
117                 UINT outOfSync; //counts each invalid read headers
118                 bool hasHdrInfo; //if we have been able to successfully
119                                  //parse the first header
120                 bool hasVBRInfo; //we have VBR and the necessary info
121                 //infos from header
122                 int hdrFramelen;
123                 int hdrBitrate;
124                 int hdrSamplingRate;
125                 //a little bit guessing for VBR
126                 int avrBitrate;
127
128                 int streamtype;
129                 bool isStarting;
130                 ULONG readHeaders;
131                 ULONG globalBytesWritten;
132
133
134                 //search for the start of a header
135                 //return NULL if none found
136                 UCHAR * findHeader(UCHAR * buf, int len, bool writeInfo=false);
137                 //we need a buffer to be able to read at least a complete header
138                 const static int HDRLEN=4;
139                 UCHAR tmpBuffer[PACKET_SIZE+HDRLEN];
140                 UINT tmpFill;
141
142                 //compute the bytes/second from the BR info
143                 //return 0 if not there;
144                 ULONG getBytesPerSecond();
145
146                 //parse ID3V2 at the beginning
147                 //set info if there
148                 //return length of header, -1 if not found
149                 int parseID3V2(UCHAR *data, int len);
150                 int id3_2_3_FrameParse(unsigned char buf[], id3_frame *frame);
151     int id3_2_2_FrameParse(unsigned char buf[], id3_frame *frame);
152
153                 //parse ID3V1 at the end
154                 //set info if there, return -1 otherwise
155     int parseID3V1(UCHAR *data, int len) ;
156
157                 //parse a VBR header and set VBR info
158                 //input has to point to firts MPEG header in file
159                 //potentially after ID3V2
160                 int parseVBR(UCHAR *data, int len);
161
162
163                 class PacketBuffer* buffer;
164
165                 id3_tag *id3;
166                 mpegInfo *info;
167                 vbrInfo *vbr;
168
169
170 };
171
172 #endif