-
Notifications
You must be signed in to change notification settings - Fork 2
/
h264frame.h
executable file
·211 lines (189 loc) · 7.92 KB
/
h264frame.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
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
/*****************************************************************************/
/* The contents of this file are subject to the Mozilla Public License */
/* Version 1.0 (the "License"); you may not use this file except in */
/* compliance with the License. You may obtain a copy of the License at */
/* http://www.mozilla.org/MPL/ */
/* */
/* Software distributed under the License is distributed on an "AS IS" */
/* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the */
/* License for the specific language governing rights and limitations under */
/* the License. */
/* */
/* The Original Code is the Open H323 Library. */
/* */
/* The Initial Developer of the Original Code is Matthias Schneider */
/* Copyright (C) 2007 Matthias Schneider, All Rights Reserved. */
/* */
/* Contributor(s): Matthias Schneider (ma30002000@yahoo.de) */
/* */
/* Alternatively, the contents of this file may be used under the terms of */
/* the GNU General Public License Version 2 or later (the "GPL"), in which */
/* case the provisions of the GPL are applicable instead of those above. If */
/* you wish to allow use of your version of this file only under the terms */
/* of the GPL and not to allow others to use your version of this file under */
/* the MPL, indicate your decision by deleting the provisions above and */
/* replace them with the notice and other provisions required by the GPL. */
/* If you do not delete the provisions above, a recipient may use your */
/* version of this file under either the MPL or the GPL. */
/* */
/* The Original Code was written by Matthias Schneider <ma30002000@yahoo.de> */
/*****************************************************************************/
#ifndef __H264FRAME_H__
#define __H264FRAME_H__ 1
// GVX 3000 does not like STAP packets... So we waste 40 bytes per connection...
//#define SEND_STAP_PACKETS 1
#ifdef _MSC_VER
#include "vs-stdint.h"
#include "rtpframe.h"
#else
#include <stdint.h>
#include "rtpframe.h"
#endif
#define H264_NAL_TYPE_NON_IDR_SLICE 1
#define H264_NAL_TYPE_DP_A_SLICE 2
#define H264_NAL_TYPE_DP_B_SLICE 3
#define H264_NAL_TYPE_DP_C_SLICE 0x4
#define H264_NAL_TYPE_IDR_SLICE 0x5
#define H264_NAL_TYPE_SEI 0x6
#define H264_NAL_TYPE_SEQ_PARAM 0x7
#define H264_NAL_TYPE_PIC_PARAM 0x8
#define H264_NAL_TYPE_ACCESS_UNIT 0x9
#define H264_NAL_TYPE_END_OF_SEQ 0xa
#define H264_NAL_TYPE_END_OF_STREAM 0xb
#define H264_NAL_TYPE_FILLER_DATA 0xc
#define H264_NAL_TYPE_SEQ_EXTENSION 0xd
const static struct h264_level {
unsigned level_idc;
unsigned mbps; /* max macroblock processing rate (macroblocks/sec) */
unsigned frame_size; /* max frame size (macroblocks) */
unsigned dpb; /* max decoded picture buffer (bytes) */
long unsigned bitrate;/* max bitrate (kbit/sec) */
unsigned cpb; /* max vbv buffer (kbit) */
unsigned mv_range; /* max vertical mv component range (pixels) */
unsigned mvs_per_2mb; /* max mvs per 2 consecutive mbs. */
unsigned slice_rate;
unsigned bipred8x8; /* limit bipred to >=8x8 */
unsigned direct8x8; /* limit b_direct to >=8x8 */
unsigned frame_only; /* forbid interlacing */
} h264_levels[] = {
{ 10, 1485, 99, 152064, 64000, 175, 64, 64, 0, 0, 0, 1 },
{ 9, 1485, 99, 152064, 128000, 350, 64, 64, 0, 0, 0, 1 },
{ 11, 3000, 396, 345600, 192000, 500, 128, 64, 0, 0, 0, 1 },
{ 12, 6000, 396, 912384, 384000, 1000, 128, 64, 0, 0, 0, 1 },
{ 13, 11880, 396, 912384, 768000, 2000, 128, 64, 0, 0, 0, 1 },
{ 20, 11880, 396, 912384, 2000000, 2000, 128, 64, 0, 0, 0, 1 },
{ 21, 19800, 792, 1824768, 4000000, 4000, 256, 64, 0, 0, 0, 0 },
{ 22, 20250, 1620, 3110400, 4000000, 4000, 256, 64, 0, 0, 0, 0 },
{ 30, 40500, 1620, 3110400, 10000000, 10000, 256, 32, 22, 0, 1, 0 },
{ 31, 108000, 3600, 6912000, 14000000, 14000, 512, 16, 60, 1, 1, 0 },
{ 32, 216000, 5120, 7864320, 20000000, 20000, 512, 16, 60, 1, 1, 0 },
{ 40, 245760, 8192, 12582912, 20000000, 25000, 512, 16, 60, 1, 1, 0 },
{ 41, 245760, 8192, 12582912, 50000000, 62500, 512, 16, 24, 1, 1, 0 },
{ 42, 522240, 8704, 13369344, 50000000, 62500, 512, 16, 24, 1, 1, 1 },
{ 50, 589824, 22080, 42393600, 135000000, 135000, 512, 16, 24, 1, 1, 1 },
{ 51, 983040, 36864, 70778880, 240000000, 240000, 512, 16, 24, 1, 1, 1 },
{ 0 }
};
// This table is used in order to select a different resolution if the desired one
// consists of more macroblocks than the level limit
const static struct h264_resolution {
unsigned width;
unsigned height;
unsigned macroblocks;
} h264_resolutions[] = {
{ 1920, 1088, 8160 },
{ 1600, 1200, 7500 },
{ 1408, 1152, 6336 },
{ 1280, 1024, 5120 },
{ 1280, 720, 3600 },
{ 1024, 768, 3072 },
{ 800, 600, 1900 },
{ 704, 576, 1584 },
{ 640, 480, 1200 },
{ 352, 288, 396 },
{ 320, 240, 300 },
{ 176, 144, 99 },
{ 128, 96, 48 },
{ 0 }
};
#ifndef LICENCE_MPL
extern "C"
{
#ifdef _MSC_VER
#include "x264.h"
#else
#include "x264.h"
#endif
}
#endif
enum codecInFlags {
silenceFrame = 1,
forceIFrame = 2
};
enum codecOutFlags {
isLastFrame = 1,
isIFrame = 2,
requestIFrame = 4
};
typedef struct h264_nal_t
{
unsigned int offset;
unsigned int length;
unsigned char type;
} h264_nal_t;
class H264Frame
{
public:
H264Frame();
~H264Frame();
void BeginNewFrame();
#ifndef LICENCE_MPL
void SetFromFrame (x264_nal_t *NALs, int numberOfNALs);
#endif
void SetMaxPayloadSize (unsigned short maxPayloadSize)
{
_maxPayloadSize = maxPayloadSize;
}
void SetTimestamp (unsigned long timestamp)
{
_timestamp = timestamp;
}
bool GetRTPFrame (RTPFrame & frame, unsigned int & flags);
bool HasRTPFrames ()
{
if (_currentNAL < _numberOfNALsInFrame) return true; else return false;
}
bool SetFromRTPFrame (RTPFrame & frame, unsigned int & flags);
unsigned char* GetFramePtr ()
{
return (_encodedFrame);
}
unsigned int GetFrameSize () {
return (_encodedFrameLen);
}
bool IsSync ();
private:
bool EncapsulateSTAP (RTPFrame & frame, unsigned int & flags);
bool EncapsulateFU (RTPFrame & frame, unsigned int & flags);
bool DeencapsulateFU (RTPFrame & frame, unsigned int & flags);
bool DeencapsulateSTAP (RTPFrame & frame, unsigned int & flags);
void AddDataToEncodedFrame (unsigned char *data, unsigned int dataLen, unsigned char header, bool addHeader);
bool IsStartCode (const unsigned char *positionInFrame);
// general stuff
unsigned long _timestamp;
unsigned short _maxPayloadSize;
unsigned char* _encodedFrame;
unsigned int _encodedFrameLen;
h264_nal_t* _NALs;
unsigned int _numberOfNALsInFrame;
unsigned int _currentNAL;
unsigned int _numberOfNALsReserved;
// for encapsulation
unsigned int _currentNALFURemainingLen;
unsigned char* _currentNALFURemainingDataPtr;
unsigned char _currentNALFUHeader0;
unsigned char _currentNALFUHeader1;
// for deencapsulation
unsigned short _currentFU;
};
#endif /* __H264FRAME_H__ */