-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.cpp
194 lines (158 loc) · 5.73 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
186
187
188
189
190
191
192
193
194
#include "main.h"
#include "zbmp.h"
#include "zppm.h"
#include "zfile.h"
#include "zlist.h"
#include "zoptions.h"
#include <string>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
struct Region {
zu32 x1;
zu32 y1;
zu32 x2;
zu32 y2;
float offset;
};
struct UserData {
ZH264Encoder *encoder;
ZPath output;
float baseqp;
ZList<Region> regions;
zu64 framecount;
};
bool brear = false;
bool setup = false;
bool cont = true;
void freeQuantOffsets(void *ptr){
LOG("Free quantizer offsets");
delete[] (float*)ptr;
}
//void decoderCallback(AVFrame *frame, AVPacket *pkt, const H264_Decoder *decode, void *user){
void decoderCallback(zu32 num, AVFrame *frame, AVPacket *pkt, const ZH264Decoder *decode, void *user){
if(!setup && (decode->context->pix_fmt != AV_PIX_FMT_YUV420P || frame->format != AV_PIX_FMT_YUV420P)){
LOG("Incorrect decode pixel format " << decode->context->pix_fmt << " " << frame->format);
}
if(user != nullptr){
UserData *userdata = (UserData*)user;
ZH264Encoder *encoder = userdata->encoder;
if(!setup){
// Setup encoder
LOG("FPS: " << decode->getFPS());
LOG("Input Setup: " << encoder->inputSetup(frame->width, frame->height, decode->getFPS()));
LOG("Output Setup: " << encoder->outputSetup(frame->width, frame->height, decode->getFPS()));
encoder->infmt = decode->context->pix_fmt;
userdata->framecount = 0;
// Set up regions in macroblocks
zu32 xblocks = frame->width / 16 + (frame->width % 16 ? 1 : 0);
zu32 yblocks = frame->height / 16 + (frame->height % 16 ? 1 : 0);
encoder->quantOffsets().resize(xblocks * yblocks);
for(zu64 i = 0; i < xblocks * yblocks; ++i){
encoder->quantOffsets()[i] = userdata->baseqp;
}
LOG("Setting " << userdata->regions.size() << " regions:");
for(zu64 i = 0; i < userdata->regions.size(); ++i){
LOG(i << ": " << userdata->regions[i].x1 << "," << userdata->regions[i].y1 << "," << userdata->regions[i].x2 << "," << userdata->regions[i].y2 << " qp: " << userdata->regions[i].offset);
for(zu32 y = userdata->regions[i].y1; y <= userdata->regions[i].y2 && y < yblocks; ++y){
for(zu32 x = userdata->regions[i].x1; x <= userdata->regions[i].x2 && x < xblocks; ++x){
encoder->quantOffsets()[x + y * xblocks] = userdata->regions[i].offset;
}
}
}
if(!encoder->open(userdata->output)){
throw ZException("Failed to open encoder");
}
LOG("Encoder Opened: " << userdata->output);
setup = true;
}
encoder->encode(frame->data, frame->linesize);
//userdata->framecount = decode->getFrameCount();
if(num % 10 == 0){
RLOG("\r" << "Update: " << num << "/" << userdata->framecount);
}
if(num == userdata->framecount){
cont = false;
}
} else {
ELOG("Couldn't encode (user == nullptr)");
}
}
#define OPT_QP "quanta"
#define OPT_FPS "fps"
const ZArray<ZOptions::OptDef> optdef = {
{ OPT_QP, 'q', ZOptions::STRING },
{ OPT_FPS, 'F', ZOptions::STRING },
};
int main(int argc, char **argv){
ZLog::logLevelStdOut(ZLog::INFO, "[%time%] %thread% N %log%");
ZLog::logLevelStdOut(ZLog::DEBUG, "[%time%] %thread% D [%function%|%file%:%line%] %log%");
ZLog::logLevelStdErr(ZLog::ERRORS, "\x1b[31m[%time%] %thread% E [%function%|%file%:%line%] %log%\x1b[m");
//ZLog::init();
LOG("Starting H264-ROI");
try {
ZOptions options(optdef);
if(!options.parse(argc, argv)){
return 1;
}
auto args = options.getArgs();
for(zu64 i = 0; i < args.size(); ++i)
LOG(args[i]);
if(args.size() < 2){
LOG("Usage: h264_roi [-q qp_factor] input.h264 output.h264 [regions]...");
LOG("Regions: <first_corner_x>,<first_corner_y>,<second_corner_x>,<second_corner_y>:<quantizer_value>");
return 1;
}
ZPath input = args[0];
ZPath output = args[1];
float baseqp = 0;
if(options.getOpts().contains(OPT_QP))
baseqp = stof(options.getOpts()[OPT_QP].str());
float ffps = 0;
if(options.getOpts().contains(OPT_FPS))
ffps = stof(options.getOpts()[OPT_FPS].str());
ZList<Region> regions;
for(zu64 i = 2; i < args.size(); ++i){
ArZ tokens = args[i].split(':');
if(tokens.size() != 2)
return 2;
ArZ coords = tokens[0].explode(',');
if(coords.size() != 4)
return 3;
regions.push({ (zu32)coords[0].toUint() / 16,
(zu32)coords[1].toUint() / 16,
(zu32)coords[2].toUint() / 16,
(zu32)coords[3].toUint() / 16,
stof(tokens[1].str()) });
}
UserData userdata;
ZH264Decoder decoder;
decoder.forceFPS(ffps);
if(!decoder.open(input, decoderCallback, &userdata)){
ELOG("Failed to open decoder");
return 4;
}
LOG("Decoder Opened: " << input);
ZH264Encoder encoder;
userdata.output = output;
userdata.encoder = &encoder;
userdata.baseqp = baseqp;
userdata.regions = regions;
LOG("Reading frames...");
brear = true;
while(cont){
try {
decoder.readFrame();
} catch(ZException e){
if(e.code() != 5)
ELOG("Exception caught: " << e.what());
break;
}
}
RLOG(ZLog::NEWLN);
encoder.close();
LOG("Finished H264-ROI");
} catch(zexception e){
LOG("Fatal exception: " << e.what);
}
return 0;
}