-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCameraCapture.cpp
233 lines (174 loc) · 6.04 KB
/
CameraCapture.cpp
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
#include "CameraCapture.h"
#include "ReadParams.h"
extern int THREAD_STATE;
extern CRITICAL_SECTION THREAD_STATE_MUTEX;
CameraCapture::CameraCapture(const CameraParams* params) :
OnFrameFunc(nullptr), Input_fmt_Ctx(nullptr), CodecCtx(nullptr),
CamInDict(nullptr), SwsCtx(nullptr), FrameOut(nullptr), Bitrate(5120000),
FrameCnt(0), State(0), CamParams(params), DelayClock(CLOCKS_PER_SEC / 15)
{
};
int CameraCapture::initialize() {
State = 0;
// record return value for ffmpeg function
int ret = 0;
// temp string for logging, data copy... etc.
char str_tmp[256];
str_tmp[255] = 0;
AVDictionary* opt = nullptr;
AVDictionaryEntry* dict_entry = nullptr;
AVInputFormat* in_fmt = nullptr;
avdevice_register_all();
// av_log_set_level(AV_LOG_ERROR);
if (CodecCtx != nullptr) {
avcodec_free_context(&CodecCtx);
CodecCtx = nullptr;
}
if (Input_fmt_Ctx != nullptr) {
avformat_close_input(&Input_fmt_Ctx);
Input_fmt_Ctx = nullptr;
}
if (FrameOut != nullptr) {
if (FrameOut->data[0] != nullptr) {
av_freep(FrameOut->data[0]);
}
av_frame_free(&FrameOut);
FrameOut = nullptr;
}
Input_fmt_Ctx = avformat_alloc_context();
in_fmt = av_find_input_format("dshow");
ret = snprintf(str_tmp, 255, "video_size=%dx%d;framerate=%g;rtbufsize=%llu;",
CamParams->get_width(), CamParams->get_height(), CamParams->get_framerate(), CamParams->get_buffer());
ret = av_dict_parse_string(&opt, str_tmp, "=", ";", 0);
snprintf(str_tmp, 255, "video=%s", CamParams->get_name().c_str());
if (ret = avformat_open_input(&Input_fmt_Ctx, str_tmp, in_fmt, &opt) != 0) {
//exception
av_dict_free(&opt);
av_strerror(ret, str_tmp, 255);
printf("open video input '%s' failed! err = %s", CamParams->get_name().c_str(), str_tmp);
return -1;
}
av_dump_format(Input_fmt_Ctx, 0, Input_fmt_Ctx->url, 0);
if (opt != nullptr) {
printf("the following options are not used:");
dict_entry = nullptr;
while (dict_entry = av_dict_get(opt, "", dict_entry, AV_DICT_IGNORE_SUFFIX)) {
fprintf(stdout, " '%s' = '%s'\n", dict_entry->key, dict_entry->value);
}
av_dict_free(&opt);
}
if (avformat_find_stream_info(Input_fmt_Ctx, nullptr) < 0) {
//exception
printf("getting audio/video stream information from '%s' failed!", CamParams->get_name().c_str());
return -1;
}
//print out the video stream information
printf("video stream from: '%s'\n", CamParams->get_name().c_str());
OutStreamNum = -1;
AVStream* stream = nullptr;
for (unsigned int i = 0; i < Input_fmt_Ctx->nb_streams; i++) {
if (Input_fmt_Ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
OutStreamNum = i;
stream = Input_fmt_Ctx->streams[i];
}
}
if (OutStreamNum == -1 || stream == nullptr) {
//exception
printf("no video stream found in '%s'!\n", CamParams->get_name().c_str());
return -1;
}
AVCodecParameters* codec_par = stream->codecpar;
Bitrate = codec_par->bit_rate;
DelayClock = (int)((double)(CLOCKS_PER_SEC) * (double)(stream->avg_frame_rate.den) / (double)(stream->avg_frame_rate.num));
AVCodec* decoder = avcodec_find_decoder(codec_par->codec_id);
if (decoder == nullptr) {
printf("cannot find decoder for '%s'!", avcodec_get_name(codec_par->codec_id));
return -1;
}
// cout << "decoder is: " << avcodec_get_name(codec_par->codec_id) << endl;
CodecCtx = avcodec_alloc_context3(decoder);
avcodec_parameters_to_context(CodecCtx, codec_par);
// log level: Something went wrong and recovery is not possible.
av_log_set_level(AV_LOG_FATAL);
if (avcodec_open2(CodecCtx, decoder, nullptr) < 0) {
printf("Cannot open video decoder for camera!");
return -1;
}
// sws_getCachedContext: reinitialize or create a new SwsContext struct
SwsCtx = sws_getCachedContext(nullptr, CodecCtx->width, CodecCtx->height, CodecCtx->pix_fmt,
CodecCtx->width, CodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, nullptr, nullptr, nullptr);
FrameOut = av_frame_alloc();
if (FrameOut == nullptr) {
printf("allocating output frame failed!");
return -1;
}
FrameOut->width = CodecCtx->width;
FrameOut->height = CodecCtx->height;
FrameOut->format = AV_PIX_FMT_YUV420P;
DataOutSz = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, FrameOut->width, FrameOut->height, MEM_ALIGN);
ret = av_frame_get_buffer(FrameOut, MEM_ALIGN);
if (ret < 0) {
printf("allocating frame data buffer failed!");
return -1;
}
// printf("data out sz is: %d", DataOutSz); // 3110400
DataOut.resize(DataOutSz);
State = 1;
return 0;
}
// start capture the frame
void CameraCapture::capture() {
if (!State) return;
// record return value for ffmpeg function
int ret;
int packet_cnt = 0;
AVPacket* packet = av_packet_alloc();
AVFrame* frame = av_frame_alloc();
while (true) {
if (THREAD_STATE > 0) break; // if the thread is using, break
ret = av_read_frame(Input_fmt_Ctx, packet);
if (ret < 0) {
break;
}
// cout << "pkt dts: " << packet->dts << " ----- pkt dts: " << packet->pts << endl;
++packet_cnt;
if (packet->buf == nullptr || packet->stream_index != OutStreamNum)
continue;
ret = avcodec_send_packet(CodecCtx, packet);
if (ret != 0 && ret == AVERROR(EAGAIN)) {
printf("sending a packet for decoding failed!");
return;
}
do {
ret = avcodec_receive_frame(CodecCtx, frame);
if (ret == AVERROR(EAGAIN)) {
break;
}
else if (ret != 0) {
printf("reading a frame from decoder failed!");
return;
}
ret = sws_scale(SwsCtx, frame->data, frame->linesize, 0, frame->height,
FrameOut->data, FrameOut->linesize);
if (ret <= 0) {
printf("scaling picture failed!");
return;
}
DataOut.resize(DataOutSz);
av_image_copy_to_buffer(DataOut.data_ptr(), DataOut.size(), FrameOut->data,
FrameOut->linesize, (enum AVPixelFormat)FrameOut->format,
FrameOut->width, FrameOut->height, MEM_ALIGN);
//DataOut.copy(FrameOut->data[0], DataOutSz);
++FrameCnt;
if (OnFrameFunc != nullptr) {
OnFrameFunc(&DataOut);
}
//iprintf("send a frame to encoder, size = %d.", frame->pkt_size);
} while (ret >= 0);
Sleep(1);
av_packet_unref(packet);
av_frame_unref(frame);
}
av_frame_free(&frame);
av_packet_free(&packet);
}