-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
185 lines (149 loc) · 6.59 KB
/
main.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
#include <iostream>
#include "main.hpp"
#include "utils.hpp"
#include "Logger.hpp"
#include "ConfigReader.hpp"
#include "MatrixReader.hpp"
#ifdef ENCODER
#include "ImageEncoder.hpp"
#include "VideoEncoder.hpp"
#endif
#ifdef DECODER
#include "ImageDecoder.hpp"
#include "VideoDecoder.hpp"
#endif
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cerr << "One argument, the name of a settings file, expected!" << std::endl;
return 1;
}
dc::ConfigReader c;
if (!c.read(argv[1])) {
std::cerr << "Error reading file '" << argv[1] << "'!" << std::endl;
std::cerr << c.getErrorDescription() << std::endl;
return 2;
}
// Enforce existence of all expected keys.
const bool input_is_image = c.verifyForImage();
const std::string errstri = c.getErrorDescription();
const bool input_is_encvideo = c.verifyForVideo(true);
const std::string errstrev = c.getErrorDescription();
const bool input_is_decvideo = c.verifyForVideo(false);
const std::string errstrdv = c.getErrorDescription();
if (input_is_image && !(input_is_encvideo || input_is_decvideo)) {
// pass
} else if ((input_is_encvideo || input_is_decvideo) && !input_is_image) {
// pass
} else {
std::cerr << "Error in settings!" << std::endl;
if (errstri.size() > 0) std::cerr << errstri << std::endl;
if (errstrev.size() > 0) std::cerr << errstrev << std::endl;
if (errstrdv.size() > 0) std::cerr << errstrdv << std::endl;
return 3;
}
#ifdef LOG_OFF
util::Logger::Create("");
#else
util::Logger::Create(c.getValue(dc::ImageSetting::logfile));
#endif
util::Logger::WriteLn("Input settings:", false);
util::Logger::WriteLn("-------------------------", false);
util::Logger::WriteLn(c.toString(), false);
const std::string encfile = c.getValue(dc::ImageSetting::encfile),
decfile = c.getValue(dc::ImageSetting::decfile);
bool success = true;
util::timepoint_t start = util::TimerStart();
#ifdef ENCODER
const std::string rawfile = c.getValue(dc::ImageSetting::rawfile);
if (rawfile == encfile) {
std::cerr << "Error in settings! Encoded filename must be different from raw filename!" << std::endl;
return 3;
}
dc::MatrixReader<> m;
if (!m.read(c.getValue(dc::ImageSetting::quantfile))) {
return 4;
}
util::Logger::WriteLn("Quantization matrix:", false);
util::Logger::WriteLn("-------------------------", false);
util::Logger::WriteLn(m.toString(), false);
uint16_t width, height, rle, gop, merange;
try {
width = util::lexical_cast<uint16_t>(c.getValue(dc::ImageSetting::width).c_str());
height = util::lexical_cast<uint16_t>(c.getValue(dc::ImageSetting::height).c_str());
rle = util::lexical_cast<uint16_t>(c.getValue(dc::ImageSetting::rle).c_str());
if (input_is_encvideo) {
gop = util::lexical_cast<uint16_t>(c.getValue(dc::VideoSetting::gop).c_str());
merange = util::lexical_cast<uint16_t>(c.getValue(dc::VideoSetting::merange).c_str());
}
} catch (Exceptions::CastingException const& e) {
util::Logger::WriteLn(e.getMessage());
return 5;
}
if (input_is_image) {
dc::ImageEncoder enc(rawfile, encfile, width, height, rle, m);
if ((success = enc.process())) {
enc.saveResult();
util::Logger::WriteLn("", false);
util::Logger::WriteLn(std::string_format("Elapsed time: %f milliseconds",
util::TimerDuration_ms(start)));
util::Logger::WriteLn("", false);
util::Logger::WriteLn("", false);
} else {
util::Logger::WriteLn("Error processing raw image for encoding! See log for details.");
}
} else if (input_is_encvideo) {
dc::VideoEncoder enc(rawfile, encfile, width, height, rle, m, gop, merange);
if ((success = enc.process())) {
enc.saveResult();
util::Logger::WriteLn("", false);
util::Logger::WriteLn(std::string_format("Elapsed time: %f milliseconds",
util::TimerDuration_ms(start)));
util::Logger::WriteLn("", false);
util::Logger::WriteLn("", false);
} else {
util::Logger::WriteLn("Error processing raw video for encoding! See log for details.");
}
}
#endif
#ifdef DECODER
if (encfile == decfile) {
std::cerr << "Error in settings! Decoded filename must be different from encoded!" << std::endl;
return 3;
}
if (success) {
start = util::TimerStart();
if (input_is_image) {
dc::ImageDecoder dec(encfile, decfile);
if (dec.process()) {
dec.saveResult();
util::Logger::WriteLn("", false);
util::Logger::WriteLn(std::string_format("Elapsed time: %f milliseconds",
util::TimerDuration_ms(start)));
util::Logger::WriteLn("", false);
} else {
util::Logger::Write("Error processing raw image for decoding! See log for details.");
}
} else if (input_is_decvideo) {
uint16_t motioncomp;
try {
motioncomp = util::lexical_cast<uint16_t>(c.getValue(dc::VideoSetting::motioncompensation).c_str());
} catch (Exceptions::CastingException const& e) {
util::Logger::WriteLn(e.getMessage());
return 5;
}
dc::VideoDecoder dec(encfile, decfile, motioncomp);
if (dec.process()) {
dec.saveResult();
util::Logger::WriteLn("", false);
util::Logger::WriteLn(std::string_format("Elapsed time: %f milliseconds",
util::TimerDuration_ms(start)));
util::Logger::WriteLn("", false);
} else {
util::Logger::Write("Error processing raw video for decoding! See log for details.");
}
}
}
#endif
util::Logger::Destroy();
return 0;
}