-
Notifications
You must be signed in to change notification settings - Fork 40
/
FlvParser.h
149 lines (118 loc) · 3.36 KB
/
FlvParser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef FLVPARSER_H
#define FLVPARSER_H
#include <vector>
#include "Videojj.h"
using namespace std;
typedef unsigned long long uint64_t;
class CFlvParser
{
public:
CFlvParser();
virtual ~CFlvParser();
int Parse(unsigned char *pBuf, int nBufSize, int &nUsedLen);
int PrintInfo();
int DumpH264(const std::string &path);
int DumpAAC(const std::string &path);
int DumpFlv(const std::string &path);
private:
typedef struct FlvHeader_s
{
int nVersion;
int bHaveVideo, bHaveAudio;
int nHeadSize;
unsigned char *pFlvHeader;
} FlvHeader;
struct TagHeader
{
int nType;
int nDataSize;
int nTimeStamp;
int nTSEx;
int nStreamID;
unsigned int nTotalTS;
TagHeader() : nType(0), nDataSize(0), nTimeStamp(0), nTSEx(0), nStreamID(0), nTotalTS(0) {}
~TagHeader() {}
};
class Tag
{
public:
Tag() : _pTagHeader(NULL), _pTagData(NULL), _pMedia(NULL), _nMediaLen(0) {}
void Init(TagHeader *pHeader, unsigned char *pBuf, int nLeftLen);
TagHeader _header;
unsigned char *_pTagHeader;
unsigned char *_pTagData;
unsigned char *_pMedia;
int _nMediaLen;
};
class CVideoTag : public Tag
{
public:
CVideoTag(TagHeader *pHeader, unsigned char *pBuf, int nLeftLen, CFlvParser *pParser);
int _nFrameType;
int _nCodecID;
int ParseH264Tag(CFlvParser *pParser);
int ParseH264Configuration(CFlvParser *pParser, unsigned char *pTagData);
int ParseNalu(CFlvParser *pParser, unsigned char *pTagData);
};
class CAudioTag : public Tag
{
public:
CAudioTag(TagHeader *pHeader, unsigned char *pBuf, int nLeftLen, CFlvParser *pParser);
int _nSoundFormat;
int _nSoundRate;
int _nSoundSize;
int _nSoundType;
// aac
static int _aacProfile;
static int _sampleRateIndex;
static int _channelConfig;
int ParseAACTag(CFlvParser *pParser);
int ParseAudioSpecificConfig(CFlvParser *pParser, unsigned char *pTagData);
int ParseRawAAC(CFlvParser *pParser, unsigned char *pTagData);
};
struct FlvStat
{
int nMetaNum, nVideoNum, nAudioNum;
int nMaxTimeStamp;
int nLengthSize;
FlvStat() : nMetaNum(0), nVideoNum(0), nAudioNum(0), nMaxTimeStamp(0), nLengthSize(0){}
~FlvStat() {}
};
static unsigned int ShowU32(unsigned char *pBuf) { return (pBuf[0] << 24) | (pBuf[1] << 16) | (pBuf[2] << 8) | pBuf[3]; }
static unsigned int ShowU24(unsigned char *pBuf) { return (pBuf[0] << 16) | (pBuf[1] << 8) | (pBuf[2]); }
static unsigned int ShowU16(unsigned char *pBuf) { return (pBuf[0] << 8) | (pBuf[1]); }
static unsigned int ShowU8(unsigned char *pBuf) { return (pBuf[0]); }
static void WriteU64(uint64_t & x, int length, int value)
{
uint64_t mask = 0xFFFFFFFFFFFFFFFF >> (64 - length);
x = (x << length) | ((uint64_t)value & mask);
}
static unsigned int WriteU32(unsigned int n)
{
unsigned int nn = 0;
unsigned char *p = (unsigned char *)&n;
unsigned char *pp = (unsigned char *)&nn;
pp[0] = p[3];
pp[1] = p[2];
pp[2] = p[1];
pp[3] = p[0];
return nn;
}
friend class Tag;
private:
FlvHeader *CreateFlvHeader(unsigned char *pBuf);
int DestroyFlvHeader(FlvHeader *pHeader);
Tag *CreateTag(unsigned char *pBuf, int nLeftLen);
int DestroyTag(Tag *pTag);
int Stat();
int StatVideo(Tag *pTag);
int IsUserDataTag(Tag *pTag);
private:
FlvHeader* _pFlvHeader;
vector<Tag *> _vpTag;
FlvStat _sStat;
CVideojj *_vjj;
// H.264
int _nNalUnitLength;
};
#endif // FLVPARSER_H