-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebcamCapture.cpp
More file actions
305 lines (264 loc) · 8.35 KB
/
Copy pathWebcamCapture.cpp
File metadata and controls
305 lines (264 loc) · 8.35 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "WebcamCapture.h"
extern "C"
{
#include <libavdevice/avdevice.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
}
#include <iostream>
namespace
{
void logAvError(const char* what, int err)
{
char buf[AV_ERROR_MAX_STRING_SIZE] = {0};
av_strerror(err, buf, sizeof(buf));
std::cerr << "[WebcamCapture] " << what << ": " << buf << "\n";
}
}
WebcamCapture::WebcamCapture() = default;
WebcamCapture::~WebcamCapture()
{
stop();
}
bool WebcamCapture::init(const std::string& device, int width, int height, int fps,
int quality, bool reencode) {
width_ = width;
height_ = height;
reencode_ = reencode;
avdevice_register_all();
const AVInputFormat* inputFmt = av_find_input_format("v4l2");
if (!inputFmt)
{
std::cerr << "[WebcamCapture] v4l2 input format not available in this ffmpeg build\n";
return false;
}
AVDictionary* options = nullptr;
av_dict_set(&options, "video_size", (std::to_string(width) + "x" + std::to_string(height)).c_str(), 0);
av_dict_set(&options, "framerate", std::to_string(fps).c_str(), 0);
av_dict_set(&options, "input_format", reencode ? "yuyv422" : "mjpeg", 0);
int ret = avformat_open_input(&fmtCtx_, device.c_str(), inputFmt, &options);
av_dict_free(&options);
if (ret < 0)
{
logAvError(("avformat_open_input(" + device + ") failed").c_str(), ret);
return false;
}
ret = avformat_find_stream_info(fmtCtx_, nullptr);
if (ret < 0)
{
logAvError("avformat_find_stream_info failed", ret);
cleanup();
return false;
}
videoStreamIndex_ = av_find_best_stream(fmtCtx_, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
if (videoStreamIndex_ < 0)
{
std::cerr << "[WebcamCapture] no video stream found on " << device << "\n";
cleanup();
return false;
}
AVCodecParameters* params = fmtCtx_->streams[videoStreamIndex_]->codecpar;
if (!reencode)
{
if (params->codec_id != AV_CODEC_ID_MJPEG)
{
std::cerr << "[WebcamCapture] expected MJPEG stream in passthrough mode, got codec id "
<< params->codec_id << " (camera may not support MJPEG at this res/fps - "
<< "try --reencode)\n";
cleanup();
return false;
}
std::cerr << "[WebcamCapture] " << device << " opened, " << width_ << "x" << height_
<< ", MJPEG passthrough (demux only, no decode/encode)\n";
return true;
}
// --reencode
const AVCodec* decoder = avcodec_find_decoder(params->codec_id);
if (!decoder)
{
std::cerr << "[WebcamCapture] no decoder found for input codec id " << params->codec_id << "\n";
cleanup();
return false;
}
decCtx_ = avcodec_alloc_context3(decoder);
if (!decCtx_ || avcodec_parameters_to_context(decCtx_, params) < 0)
{
std::cerr << "[WebcamCapture] failed to set up decoder context\n";
cleanup();
return false;
}
ret = avcodec_open2(decCtx_, decoder, nullptr);
if (ret < 0)
{
logAvError("avcodec_open2 (decoder) failed", ret);
cleanup();
return false;
}
const AVCodec* encoder = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
if (!encoder)
{
std::cerr << "[WebcamCapture] no MJPEG encoder available in this ffmpeg build\n";
cleanup();
return false;
}
encCtx_ = avcodec_alloc_context3(encoder);
if (!encCtx_)
{
std::cerr << "[WebcamCapture] avcodec_alloc_context3 (encoder) failed\n";
cleanup();
return false;
}
encCtx_->width = width_;
encCtx_->height = height_;
encCtx_->pix_fmt = AV_PIX_FMT_YUVJ422P; // matches YUYV422's chroma subsampling
encCtx_->time_base = AVRational{1, fps > 0 ? fps : 15};
encCtx_->framerate = AVRational{fps > 0 ? fps : 15, 1};
encCtx_->flags |= AV_CODEC_FLAG_QSCALE;
encCtx_->global_quality = FF_QP2LAMBDA * quality;
ret = avcodec_open2(encCtx_, encoder, nullptr);
if (ret < 0)
{
logAvError("avcodec_open2 (encoder) failed", ret);
cleanup();
return false;
}
swsCtx_ = sws_getContext(decCtx_->width, decCtx_->height, decCtx_->pix_fmt,
encCtx_->width, encCtx_->height, encCtx_->pix_fmt,
SWS_BILINEAR, nullptr, nullptr, nullptr);
if (!swsCtx_)
{
std::cerr << "[WebcamCapture] sws_getContext failed\n";
cleanup();
return false;
}
std::cerr << "[WebcamCapture] " << device << " opened, " << width_ << "x" << height_
<< ", --reencode (decode YUYV422 -> scale -> encode MJPEG, quality " << quality << ")\n";
return true;
}
void WebcamCapture::start()
{
if (running_ || !fmtCtx_)
return;
running_ = true;
readThread_ = std::thread(&WebcamCapture::readLoop, this);
}
void WebcamCapture::stop()
{
running_ = false;
if (readThread_.joinable())
readThread_.join();
cleanup();
}
void WebcamCapture::cleanup()
{
if (swsCtx_) { sws_freeContext(swsCtx_); swsCtx_ = nullptr; }
if (encCtx_) avcodec_free_context(&encCtx_);
if (decCtx_) avcodec_free_context(&decCtx_);
if (fmtCtx_) avformat_close_input(&fmtCtx_);
videoStreamIndex_ = -1;
}
bool WebcamCapture::encodeFrame(AVFrame* decoded)
{
AVFrame* scaled = av_frame_alloc();
if (!scaled)
return false;
scaled->format = encCtx_->pix_fmt;
scaled->width = encCtx_->width;
scaled->height = encCtx_->height;
if (av_frame_get_buffer(scaled, 32) < 0)
{
av_frame_free(&scaled);
return false;
}
sws_scale(swsCtx_, decoded->data, decoded->linesize, 0, decCtx_->height, scaled->data, scaled->linesize);
int ret = avcodec_send_frame(encCtx_, scaled);
av_frame_free(&scaled);
if (ret < 0)
{
logAvError("avcodec_send_frame (encoder) failed", ret);
return false;
}
bool ok = true;
AVPacket* outPkt = av_packet_alloc();
while (ret >= 0)
{
ret = avcodec_receive_packet(encCtx_, outPkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
if (ret < 0)
{
logAvError("avcodec_receive_packet (encoder) failed", ret);
ok = false;
break;
}
if (frameCallback_)
{
std::vector<uint8_t> jpeg(outPkt->data, outPkt->data + outPkt->size);
frameCallback_(std::move(jpeg));
}
av_packet_unref(outPkt);
}
av_packet_free(&outPkt);
return ok;
}
void WebcamCapture::readLoop()
{
AVPacket* pkt = av_packet_alloc();
AVFrame* frame = reencode_ ? av_frame_alloc() : nullptr;
while (running_)
{
int ret = av_read_frame(fmtCtx_, pkt);
if (ret < 0)
{
if (ret == AVERROR(EAGAIN))
{
av_packet_unref(pkt);
continue;
}
logAvError("av_read_frame failed (camera disconnected?)", ret);
break;
}
if (pkt->stream_index != videoStreamIndex_)
{
av_packet_unref(pkt);
continue;
}
if (!reencode_)
{
if (frameCallback_)
{
std::vector<uint8_t> jpeg(pkt->data, pkt->data + pkt->size);
frameCallback_(std::move(jpeg));
}
}
else
{
int sendRet = avcodec_send_packet(decCtx_, pkt);
if (sendRet < 0)
{
logAvError("avcodec_send_packet (decoder) failed", sendRet);
}
else
{
int recvRet = 0;
while (recvRet >= 0)
{
recvRet = avcodec_receive_frame(decCtx_, frame);
if (recvRet == AVERROR(EAGAIN) || recvRet == AVERROR_EOF)
break;
if (recvRet < 0)
{
logAvError("avcodec_receive_frame (decoder) failed", recvRet);
break;
}
encodeFrame(frame);
av_frame_unref(frame);
}
}
}
av_packet_unref(pkt);
}
if (frame) av_frame_free(&frame);
av_packet_free(&pkt);
running_ = false;
}