summaryrefslogtreecommitdiff
path: root/dxr3pesframe.h
blob: 26e334a2667e246e69da0d6d14a6c568ad785f1c (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * dxr3pesframe.h
 *
 * Copyright (C) 2002-2004 Kai Möller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifndef _DXR3PESFRAME_H_
#define _DXR3PESFRAME_H_

#include <assert.h>
#include <stdint.h>

// ==================================
const int ASPECTRATIO_4_3 = 0;
const int ASPECTRATIO_16_9 = 1;

// ==================================
enum eVideoFrameType
{
    I_FRAME,
    P_FRAME,
    B_FRAME,
    UNKNOWN_FRAME
};

// ==================================
// XXX: Should we use here std:vector?
template <class T>
class cDxr3SafeArray
{
public:
    // ==================================
    enum eSafeArrayException
    {
	SAFE_ARRAY_INDEX_OUT_OF_BOUND
    };

    cDxr3SafeArray(T* pBuf, uint32_t length) :
	m_pBuf(pBuf), m_length(length) {};
    cDxr3SafeArray(const cDxr3SafeArray& from) :
	m_pBuf(from.m_pBuf), m_length(from.m_length) {};
    virtual ~cDxr3SafeArray() {};

    T& operator[](uint32_t index) throw (eSafeArrayException)
    {
	if (index >= m_length)
	    throw(SAFE_ARRAY_INDEX_OUT_OF_BOUND);
	return m_pBuf[index];
    };
    cDxr3SafeArray SubArray(uint32_t offset, uint32_t length)
    {
	if (offset + length > m_length)
	    throw(SAFE_ARRAY_INDEX_OUT_OF_BOUND);
	return cDxr3SafeArray(m_pBuf + offset, length);
    };

    uint32_t GetLength(void)
    {
	return m_length;
    };

protected:
    T* m_pBuf;
    uint32_t m_length;

private:
    cDxr3SafeArray(); // no standard constructor
};


// ==================================
// pes - packetized elementary stream
class cDxr3PesFrame
{
public:

    // ==================================
    enum ePesDataType
    {
	PES_AUDIO_DATA,
	PES_VIDEO_DATA,
	PES_PRIVATE_DATA,
	PES_UNKNOWN_DATA
    };

    // ==================================
    enum ePesFrameError
    {
	PES_GENERAL_ERROR
    };

public:
    cDxr3PesFrame() :
	m_pesDataType(PES_UNKNOWN_DATA),
	m_bValid(false),
	m_pPesStart(0),
	m_pEsStart(0),
	m_esLength(0),
	m_pts(0),
	m_videoFrameType(UNKNOWN_FRAME),
	m_aspectRatio(m_staticAspectRatio),
	m_horizontalSize(m_staticHorizontalSize),
	m_verticalSize(m_staticVerticalSize),
	m_streamId(0),
	m_pNextStart(0),
	m_remainingLength(0),
	m_offset(0) {};

    virtual ~cDxr3PesFrame() {}

    bool ExtractNextFrame(const uint8_t* pBuf, uint32_t length)
	throw (ePesFrameError);

    ePesDataType GetPesDataType(void) const
    {
	assert(m_bValid);
	return m_pesDataType;
    };
    const uint8_t* GetPesStart(void) const
    {
	assert(m_bValid);
	return m_pPesStart;
    };
    const uint8_t* GetEsStart(void) const
    {
	assert(m_bValid);
	return m_pEsStart;
    };
    uint32_t GetEsLength(void) const
    {
	assert(m_bValid);
	return m_esLength;
    };

    const uint8_t* GetNextStart(void) const
    {
	return m_pNextStart;
    };
    uint32_t GetRemainingLength(void) const
    {
	return m_remainingLength;
    };

    uint32_t GetPts(void) const
    {
	assert(m_bValid);
	return m_pts;
    };

    eVideoFrameType GetFrameType(void) const
    {
	assert(m_bValid);
	assert(m_pesDataType == PES_VIDEO_DATA);
	return m_videoFrameType;
    };
    uint32_t GetAspectRatio(void) const
    {
	assert(m_bValid);
	assert(m_pesDataType == PES_VIDEO_DATA);
	return m_aspectRatio;
    };
    uint32_t GetHorizontalSize(void) const
    {
	assert(m_bValid);
	assert(m_pesDataType == PES_VIDEO_DATA);
	return m_horizontalSize;
    };
    uint32_t GetVerticalSize(void) const
    {
	assert(m_bValid);
	assert(m_pesDataType == PES_VIDEO_DATA);
	return m_verticalSize;
    };
    uint8_t GetStreamId(void) const
    {
	assert(m_bValid);
	assert(m_pesDataType == PES_VIDEO_DATA);
	return m_streamId;
    };
    int GetOffset(void) const
    {
	assert(m_bValid);
	return m_offset;
    };

    bool IsValid(void)
    {
	return m_bValid;
    };

protected:
    bool IsPesHeader(cDxr3SafeArray<uint8_t> header)
	throw (cDxr3SafeArray<uint8_t>::eSafeArrayException);
    void ExtractPts(cDxr3SafeArray<uint8_t> ptsData)
	throw (cDxr3SafeArray<uint8_t>::eSafeArrayException);
    int ExtractVideoData(cDxr3SafeArray<uint8_t> esFrame)
	throw (cDxr3SafeArray<uint8_t>::eSafeArrayException);

    void InitData(void)
    {
	m_pesDataType = PES_UNKNOWN_DATA;
	m_bValid = false;
	m_pPesStart = 0;
	m_pEsStart = 0;
	m_esLength = 0;
	m_pts = 0;
	m_videoFrameType = UNKNOWN_FRAME;
	m_aspectRatio = m_staticAspectRatio;
	m_horizontalSize = m_staticHorizontalSize;
	m_verticalSize = m_staticVerticalSize;
	m_streamId = 0;
	m_pNextStart = 0;
	m_remainingLength = 0;
	m_offset = 0;
    }

    ePesDataType m_pesDataType;
    bool m_bValid;
    const uint8_t* m_pPesStart;
    const uint8_t* m_pEsStart;
    uint32_t m_esLength;
    uint32_t m_pts;

    eVideoFrameType m_videoFrameType;
    uint32_t m_aspectRatio;
    uint32_t m_horizontalSize;
    uint32_t m_verticalSize;
    uint8_t m_streamId;

    const uint8_t* m_pNextStart;
    uint32_t m_remainingLength;
    int m_offset;

    static uint32_t m_staticAspectRatio;
    static uint32_t m_staticHorizontalSize;
    static uint32_t m_staticVerticalSize;

protected:
    static const uint32_t MAX_PES_HEADER_SIZE;

private:
    cDxr3PesFrame(cDxr3PesFrame&); // no copy constructor

};

#endif /*_DXR3PESFRAME_H_*/

// Local variables:
// mode: c++
// c-file-style: "stroustrup"
// c-file-offsets: ((inline-open . 0))
// indent-tabs-mode: t
// End: