Repository navigation
Expand file tree
/
Copy pathdecoder.cpp
More file actions
323 lines (277 loc) · 10.7 KB
/
Copy pathdecoder.cpp
File metadata and controls
323 lines (277 loc) · 10.7 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "decoder.h"
#include <cerrno>
#include <cstdlib>
#include <list>
// Socket includes
#include <iomanip>
#include <arpa/inet.h>
#include <fcntl.h> //File control definitions
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <termios.h> //POSIX terminal control definitions
#include <unistd.h>
// #define ARDUINO_MSMT
constexpr int kMsgLength = 30;
void Decoder(const char* videoAddress, uint8_t** argbRaw, bool* /*newImg*/)
{
//---------------------------------------------------------------------------------------------------
//---------------------------- Variable initializations
//--------------------------------------------
//---------------------------------------------------------------------------------------------------
// Timekeeping
std::chrono::time_point<std::chrono::high_resolution_clock> overallStart;
std::chrono::time_point<std::chrono::high_resolution_clock> ccEnd;
std::chrono::time_point<std::chrono::high_resolution_clock> timekeeper;
std::chrono::time_point<std::chrono::high_resolution_clock> decodingStart;
std::chrono::time_point<std::chrono::high_resolution_clock> decodingEnd;
// Color conversion
SwsContext* swsCtxYuv2Bgra = nullptr;
int* argbStride = nullptr;
int framecounter = 0;
#ifdef ARTIFICIAL_DELAY
std::list<AVPacket> pktlist;
#endif
//---------------------------------------------------------------------------------------------------
//---------------------------- Arduino initialization --------------------------------------------
//---------------------------------------------------------------------------------------------------
#ifdef ARDUINO_MSMT
// Arduino setup
const char* ard_port = "/dev/ttyACM0";
char buf[kMsgLength] = {0};
int ard;
ard = open(ard_port, O_RDWR | O_NOCTTY);
if (ard == -1)
{
std::cout << "Failed to open Arduino! "
"Is it connected to "
<< ard_port << "?\n"
<< "If yes, do you have rw "
"rights on "
<< ard_port
<< "? Obtain with '$sudo "
"chmod a+rw "
<< ard_port << "'.\n";
exit(1);
}
else
{
struct termios tty;
memset(&tty, 0, sizeof(tty));
/* Error Handling */
if (tcgetattr(ard, &tty) != 0)
{
std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << "\n";
exit(1);
}
/* Set Baud Rate */
cfsetospeed(&tty, (speed_t)B115200);
cfsetispeed(&tty, (speed_t)B115200);
/* Setting other Port Stuff */
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 0; // 0.5 seconds read timeout
tty.c_cflag |= CREAD | CLOCAL; // turn on READ &
// ignore ctrl
// lines
/* Make raw */
cfmakeraw(&tty);
/* Flush Port, then applies attributes
*/
tcflush(ard, TCIFLUSH);
if (tcsetattr(ard, TCSANOW, &tty) != 0)
{
std::cout << "Error " << errno << " from tcsetattr"
<< "\n";
exit(1);
}
int flags = fcntl(ard, F_GETFL, 0);
fcntl(ard, F_SETFL, flags | O_NONBLOCK);
}
int lastval = 255, offset = 25;
std::cout << "Connected to Arduino \n";
#endif
//---------------------------------------------------------------------------------------------------
//---------------------------- Libav initialization
//----------------------------------------------
//---------------------------------------------------------------------------------------------------
// Simple startup without retrieving any stream info
AVCodecContext* cctx = nullptr;
const AVCodec* codec = nullptr;
AVFrame* frame = nullptr;
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100)
av_register_all();
#endif
codec = avcodec_find_decoder(AV_CODEC_ID_H264);
cctx = avcodec_alloc_context3(codec);
cctx->width = width;
cctx->height = height;
cctx->pix_fmt = AV_PIX_FMT_YUV420P;
if (avcodec_open2(cctx, codec, nullptr) < 0)
{
std::cout << "Could not open decoder!\n";
readyToQuit = true;
}
frame = av_frame_alloc();
// Complex startup with retrieving stream info
int frameFinished = 0;
AVFormatContext* fctx = nullptr;
fctx = avformat_alloc_context();
/* open input file, and allocate format context */
std::cout << "Waiting for input..." << std::endl;
if (avformat_open_input(&fctx, videoAddress, nullptr, nullptr) < 0)
{
fprintf(stderr, "Could not open source file %s\n", videoAddress);
exit(1);
}
//---------------------------------------------------------------------------------------------------
//---------------------------- CSP conversion initialization
//-------------------------------------
//---------------------------------------------------------------------------------------------------
swsCtxYuv2Bgra = sws_getContext(width,
height,
AV_PIX_FMT_YUV420P,
targetWidth,
targetHeight,
AV_PIX_FMT_RGBA,
SWS_BILINEAR,
nullptr,
nullptr,
nullptr);
auto* argbData =
static_cast<uint8_t*>(malloc(static_cast<size_t>(targetWidth) * targetHeight * 4 * sizeof(uint8_t)));
argbRaw[0] = argbData;
argbRaw[1] = argbData + static_cast<ptrdiff_t>(targetWidth) * targetHeight;
argbRaw[2] = argbData + static_cast<ptrdiff_t>(targetWidth) * targetHeight * 2;
argbRaw[3] = argbData + static_cast<ptrdiff_t>(targetWidth) * targetHeight * 3;
argbStride = static_cast<int*>(malloc(sizeof(int) * 1));
argbStride[0] = 4 * targetWidth;
timekeeper = std::chrono::high_resolution_clock::now();
overallStart = std::chrono::high_resolution_clock::now();
//---------------------------------------------------------------------------------------------------
//----------------------------------------- Main Loop
//-------------------------------------------
//---------------------------------------------------------------------------------------------------
while (!readyToQuit)
{
AVPacket* pkt = av_packet_alloc();
const int ret = av_read_frame(fctx, pkt);
// Packets with a size equal to 100
// bytes are dummy packets.
// These packets just push the actual
// packets through av_read_frame. We
// don't want to
// process them.
constexpr int kMinPacketSize = 100;
if ((ret >= 0) && (pkt->size > kMinPacketSize))
{
#ifdef ARTIFICIAL_DELAY
if (listlength > 0)
{
// Add to list, remove if list length sufficient
pktlist.push_back(*pkt);
if (pktlist.size() == listlength)
{
(*pkt) = pktlist.front();
}
else
{
while (pktlist.size() >= listlength)
{
pktlist.pop_front();
}
continue;
}
}
#endif
framecounter++;
decodingStart = std::chrono::high_resolution_clock::now();
decode(cctx, frame, &frameFinished, pkt);
decodingEnd = std::chrono::high_resolution_clock::now();
sws_scale(swsCtxYuv2Bgra, frame->data, frame->linesize, 0, cctx->height, argbRaw, argbStride);
ccEnd = std::chrono::high_resolution_clock::now();
newImage = true;
#ifdef ARTIFICIAL_DELAY
if (!pktlist.empty())
{
pktlist.pop_front();
}
#endif
#ifdef ARDUINO_MSMT
// Tell Arduino
int val = (int)argb_raw[0][0];
if (val > lastval + offset)
{
write(ard, "a\n", sizeof(char));
}
lastval = val;
#endif
#ifdef ARDUINO_MSMT
// Reading from Arduino
int length = read(ard, buf, MSGLENGTH * sizeof(char));
if (length > 0)
{
std::string msg = buf, strbuf;
strbuf = msg.substr(msg.find(":") + 2, msg.length());
std::cout << strbuf;
}
#else
constexpr int kFps = 60;
constexpr int kPrintInterval = 5;
if (framecounter % (kFps / kPrintInterval) == 0)
{
std::cout << "\r"
<< "rb=" << pkt->size << "B, "
<< "t_dec="
<< std::chrono::duration_cast<std::chrono::microseconds>(decodingEnd - decodingStart).count()
<< "us, t_cc="
<< std::chrono::duration_cast<std::chrono::microseconds>(ccEnd - decodingEnd).count()
<< std::flush;
}
#endif
}
av_packet_free(&pkt);
}
//---------------------------------------------------------------------------------------------------
//----------------------------------------- Wrapup ------------------------------------------------
//---------------------------------------------------------------------------------------------------
#ifdef ARDUINO_MSMT
// Close Arduino
close(ard);
#endif
sws_freeContext(swsCtxYuv2Bgra);
free(reinterpret_cast<void*>(argbRaw));
free(argbStride);
std::cout << "Successfully terminated decoder thread" << std::endl;
}
int decode(AVCodecContext* avctx, AVFrame* frame, int* gotFrame, AVPacket* pkt)
{
int ret = 0;
*gotFrame = 0;
if (pkt != nullptr)
{
ret = avcodec_send_packet(avctx, pkt);
// In particular, we don't expect
// AVERROR(EAGAIN), because we read all
// decoded frames with
// avcodec_receive_frame() until done.
if (ret < 0)
{
return ret == AVERROR_EOF ? 0 : ret;
}
}
ret = avcodec_receive_frame(avctx, frame);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
{
return ret;
}
if (ret >= 0)
{
*gotFrame = 1;
}
return 0;
}