-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencv_ffmpeg_encode.cpp
More file actions
152 lines (130 loc) · 2.91 KB
/
opencv_ffmpeg_encode.cpp
File metadata and controls
152 lines (130 loc) · 2.91 KB
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
#include "AvH264.h"
#include <iostream>
AvH264::AvH264() {
cdc_ = NULL;
cdc_ctx_ = NULL;
avf_ = NULL;
avp_ = NULL;
filename = "test.h264";
}
int AvH264::open(AvH264EncConfig h264_config)
{
//open camera
cap.open(0);
//open out file
errno_t err;
err = fopen_s(&fp_file, filename, "wb");
if (err)
{
printf("Could not open output file\n");
}
pts_ = 0;
cdc_ = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!cdc_) {
return -1;
}
cdc_ctx_ = avcodec_alloc_context3(cdc_);
if (!cdc_ctx_) {
return -1;
}
cdc_ctx_->bit_rate = h264_config.bit_rate;
cdc_ctx_->width = h264_config.width;
cdc_ctx_->height = h264_config.height;
cdc_ctx_->time_base = { 1, h264_config.frame_rate };
cdc_ctx_->framerate = { h264_config.frame_rate, 1 };
cdc_ctx_->gop_size = h264_config.gop_size;
cdc_ctx_->max_b_frames = h264_config.max_b_frames;
cdc_ctx_->pix_fmt = AV_PIX_FMT_YUV420P;
cdc_ctx_->codec_id = AV_CODEC_ID_H264;
cdc_ctx_->codec_type = AVMEDIA_TYPE_VIDEO;
//cdc_ctx_->qmin = 10;
//cdc_ctx_->qmax = 51;
//cdc_ctx_->qcompress = 0.6;
AVDictionary *dict = 0;
av_dict_set(&dict, "preset", "slow", 0);
av_dict_set(&dict, "tune", "zerolatency", 0);
av_dict_set(&dict, "profile", "main", 0);
avf_ = av_frame_alloc();
avp_ = av_packet_alloc();
if (!avf_ || !avp_) {
return -1;
}
frame_size_ = cdc_ctx_->width * cdc_ctx_->height;
avf_->format = cdc_ctx_->pix_fmt;
avf_->width = cdc_ctx_->width;
avf_->height = cdc_ctx_->height;
// alloc memory
int r = av_frame_get_buffer(avf_, 0);
if (r < 0) {
return -1;
}
r = av_frame_make_writable(avf_);
if (r < 0) {
return -1;
}
return avcodec_open2(cdc_ctx_, cdc_, &dict);
}
cv::Mat AvH264::GetMat()
{
cap >> frame;
return frame;
}
void AvH264::close() {
if (cdc_ctx_)
avcodec_free_context(&cdc_ctx_);
if (avf_)
av_frame_free(&avf_);
if (avp_)
av_packet_free(&avp_);
}
AVPacket *AvH264::encode(cv::Mat mat) {
if (mat.empty())
return NULL;
cv::resize(mat, mat, cv::Size(cdc_ctx_->width, cdc_ctx_->height));
cv::Mat yuv;
cv::cvtColor(mat, yuv, cv::COLOR_BGR2YUV_I420);
unsigned char *pdata = yuv.data;
// fill yuv420
// yyy yyy yyy yyy
// uuu
// vvv
avf_->data[0] = pdata;
avf_->data[1] = pdata + frame_size_;
avf_->data[2] = pdata + frame_size_ * 5 / 4;
avf_->pts = pts_++;
int r = avcodec_send_frame(cdc_ctx_, avf_);
while (r >= 0) {
r = avcodec_receive_packet(cdc_ctx_, avp_);
if (r == 0) {
//avp_->stream_index = 0;
//return avp_;
fwrite(avp_->data, 1, avp_->size, fp_file);
av_packet_unref(avp_);
}
if (r == AVERROR(EAGAIN) || r == AVERROR_EOF) {
return NULL;
}
}
return NULL;
}
int main()
{
AvH264 h264;
AvH264EncConfig conf;
conf.bit_rate = 320000;
conf.width = 640;
conf.height = 480;
conf.gop_size = 250;
conf.max_b_frames = 0;
conf.frame_rate = 25;
h264.open(conf);
while(true){
// get mat
cv::Mat f = h264.GetMat();
// do encode
AVPacket *pkt = h264.encode(f);
// do pkt
}
h264.close();
}