-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_encoder.h
220 lines (168 loc) · 4.06 KB
/
video_encoder.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
212
213
214
215
216
217
218
219
220
#ifndef __LU_VIDEO_ENCODER__
#define __LU_VIDEO_ENCODER__
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
// ffmpeg headers
extern "C"{
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <libavcodec/avcodec.h>
}
using namespace std;
using namespace cv;
class VideoEncoder {
private:
int video_codec;
Size resolution;
FILE* output;
AVCodecContext* c;
AVFrame* av_frame;
AVPacket pkt;
int initialization_flag;
static uint8_t ENDCODE[];
// default bitrate in bit (8Mbits/s)
static const int DEFAULT_BITRATE = 8000000;
// bitrate in bit
int bitrate;
// default quantization parameter (30)
static string DEFAULT_H264_QP;
// quantization parameter
string h264_qp;
// default fps
static const int DEFAULT_FPS = 25;
// fps
int fps;
// default size of GOP
static const int DEFAULT_SIZE_GOP = 300;
// size of GOP
int size_gop;
// default max B frames between I or P frames
static const int DEFAULT_B_FRAME = 3;
// max B frames between I or P frames
int b_frame;
// default H.264 preset (medium)
static string DEFAULT_H264_PRESET;
// h264 preset
string h264_preset;
// default H.264 profile
static string DEFAULT_H264_PROFILE;
// H.264 profile
string h264_profile;
int frameN; // presentation timestamp. Default -1. Use it as ++ frameN.
int got_output; // flag of avcodec_encode_video2, no need to initialize.
bool disable_bitrate_control;
/**
* flush the buffered frames to the file and close the codec
*
* @param non
* @return true if flush is successful, false otherwise
*/
bool flush();
/**
* convert BGR Mat to YUV mat
* Note that the imread returns BGR Mat in default in OpenCV
*
* @param input BGR Mat of type CV_8UC3
* @return YUV Mat of type CV_8UC3. It shares different memory from the input Mat.
*/
Mat BGR2YUV(const Mat& input);
/**
* Convert RGB to YUV
* @param rgb A vector of 3 containing R, G and B components. The range is [0,255]
* @return A vector of 3 containing Y, U and V components. The first element is Y. The range is [0,255]
*/
vector<unsigned char> RGB2YUV(vector<unsigned char> rgb);
public:
static const int VIDEO_CODEC_H264 = 1;
VideoEncoder(string filepath, Size resolution, int video_codec);
~VideoEncoder();
/**
* initializing codec. Seperate it from the constructor to enable user-controled parameters
*
* @param none
* @return none
*/
void init();
/**
* encode a frame of BGR Mat
*
* @param input BGR Mat of type CV_8UC3
* @return true if encoding is successful, false otherwise
*/
bool write(const Mat& frame);
// initialization status flag (==0 success, <0 error, >0 warning)
// 0 - successfull
// -1 - could not open file
// -2 - could not
// -3 - could not allocate video codec context
// -4 - could not open codec
// -5 - could not allocate frame
// -6 - could not allocate raw picture buffer
// 1 - Undefined video codec. Use default H.264 codec
int getInitializationFlag();
/**
* flush the buffered frames to the file and close the codec and the output file
*
* @param non
* @return true if flush is successful, false otherwise
*/
bool flush_close();
/**
* set quantization parameter
*
* @param qp 0-69. 0 is lossless.
* @return none
*/
void setQP(int qp);
/**
* set bitrate
*
* @param bitrate in bits/s
* @return none
*/
void setBitrate(int bitrate);
/**
* set fps
*
* @param fps
* @return none
*/
void setFPS(int fps);
/**
* set size of GOP
*
* @param size_gop
* @return none
*/
void setSizeGOP(int size_gop);
/**
* set max B frames between I or P frames
*
* @param b_frame
* @return none
*/
void setBFrame(int b_frame);
/**
* set h.264 preset
*
* @param preset string
* @return none
*/
void setPreset(string preset);
/**
* set h.264 profile
*
* @param profile string
* @return none
*/
void setProfile(string profile);
/**
* bit rate control is used by default. Calling disableBitrateControl switch to fixed quantization parameter
*
* @param none
* @return none
*/
void disableBitrateControl();
};
#endif