Skip to content

Commit 897eeac

Browse files
committed
✨ Procedural -> OOP
1 parent 084e882 commit 897eeac

File tree

3 files changed

+161
-178
lines changed

3 files changed

+161
-178
lines changed

badapple.hpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ inline int play(
6060
y += y & 1;
6161
const int xy = x * y;
6262

63-
VideoProperties *vp = analysis(video, x, y);
63+
#ifdef DECODE_FFMPEG
64+
Decoder_FFmpeg *decoder = new Decoder_FFmpeg(video.c_str());
65+
#else
66+
#ifdef DECODE_OPENCV
67+
Decoder_OpenCV *decoder = new Decoder_OpenCV(video);
68+
#endif
69+
#endif
70+
VideoProperties *vp = decoder->analysis();
6471
if (!vp) {
6572
throws("Failed to analysis video.");
6673
return 1;
@@ -80,7 +87,7 @@ inline int play(
8087
char *buffer = (char *)malloc(print_size + 2);
8188
Timer *timer = new Timer(clk);
8289

83-
if (ready_to_read()) {
90+
if (decoder->ready_to_read(x, y)) {
8491
throws("Failed to read video.");
8592
return 1;
8693
}
@@ -113,7 +120,7 @@ inline int play(
113120
}
114121

115122
for (auto i = 0;; i++) {
116-
if (read_a_frame(f)) {
123+
if (decoder->read_a_frame(f)) {
117124
if (!i) {
118125
throws("The first frame is empty.");
119126
return 1;
@@ -148,15 +155,13 @@ inline int play(
148155
}
149156
buffer[buffer_tail++] = '\n';
150157

151-
#ifdef DEBUG
152-
for (int _ = 0; _ <= print_size; _++) {
153-
if (buffer[_] == '\n' || (buffer[_] <= 126 && buffer[_] >= 32)) continue;
154-
printf("[%d:%d]", _, buffer[_]);
155-
fflush(stdout);
156-
throws("WTF");
157-
exit(0);
158-
}
159-
#endif
158+
// for (int _ = 0; _ <= print_size; _++) {
159+
// if (buffer[_] == '\n' || (buffer[_] <= 126 && buffer[_] >= 32)) continue;
160+
// printf("[%d:%d]", _, buffer[_]);
161+
// fflush(stdout);
162+
// throws("WTF");
163+
// exit(0);
164+
// }
160165

161166
if (preload) {
162167
fwrite(buffer, 1, print_size + 1, fp);
@@ -168,7 +173,7 @@ inline int play(
168173
}
169174
}
170175

171-
cls();
176+
decoder->cls();
172177
if (preload) {
173178
fclose(fp);
174179
}

decode_ffmpeg.hpp

Lines changed: 108 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -2,135 +2,129 @@
22

33
#include "base.hpp"
44

5-
namespace decode_ffmpeg {
6-
7-
const int BUFFER_SIZE = 1 << 8;
8-
const int STDOUT_SIZE = 1 << 20;
9-
10-
inline int exec_r(const char *cmd, char *result) {
11-
FILE *p = r_popen(cmd);
12-
13-
if (!p) return 1;
14-
int code = 0;
15-
16-
int t = 0;
17-
while (!feof(p)) {
18-
fread(result + t, 1, BUFFER_SIZE, p);
19-
t += BUFFER_SIZE;
20-
if (t >= STDOUT_SIZE) {
21-
code = 2;
22-
break;
5+
class Decoder_FFmpeg : public Decoder {
6+
private:
7+
const int BUFFER_SIZE = 1 << 8;
8+
const int STDOUT_SIZE = 1 << 20;
9+
10+
inline int exec_r(const char *cmd, char *result) {
11+
FILE *p = r_popen(cmd);
12+
13+
if (!p) return 1;
14+
int code = 0;
15+
16+
int t = 0;
17+
while (!feof(p)) {
18+
fread(result + t, 1, BUFFER_SIZE, p);
19+
t += BUFFER_SIZE;
20+
if (t >= STDOUT_SIZE) {
21+
code = 2;
22+
break;
23+
}
2324
}
24-
}
25-
pipe_pclose(p);
26-
27-
return code;
28-
}
25+
pipe_pclose(p);
2926

30-
std::string video;
31-
FILE *fp;
32-
int x, y, xy;
33-
} // namespace decode_ffmpeg
27+
return code;
28+
}
3429

35-
inline VideoProperties *analysis(std::string _video, int _x, int _y) {
36-
using namespace decode_ffmpeg;
37-
video = _video;
38-
x = _x;
39-
y = _y;
40-
xy = x * y;
30+
FILE *fp;
4131

42-
std::string cmd = (std::string) "ffprobe -v quiet -show_streams -select_streams v \"" + video + "\"";
43-
// printf("%s\n", cmd.c_str());
32+
public:
33+
Decoder_FFmpeg(std::string _video) : Decoder(_video){};
4434

45-
VideoProperties *vp = new VideoProperties();
35+
inline VideoProperties *analysis() {
36+
std::string cmd = (std::string) "ffprobe -v quiet -show_streams -select_streams v \"" + video + "\"";
37+
// printf("%s\n", cmd.c_str());
4638

47-
double rate_l, rate_r; // fps = rate_l / rate_r;
48-
char result_c[STDOUT_SIZE];
49-
if (exec_r(cmd.c_str(), result_c)) {
50-
throws("Failed to analysis video.");
51-
return nullptr;
52-
}
53-
std::string result_s(result_c), _k;
54-
std::size_t p, p2;
55-
56-
_k = "\nwidth=";
57-
p = result_s.find(_k);
58-
if (p == std::string::npos) {
59-
throws("This video has no width.");
60-
return nullptr;
61-
}
62-
p += _k.length();
63-
p2 = result_s.find("\n", p);
64-
sscanf(result_s.substr(p, p2 - p).c_str(), "%d", &vp->width);
65-
66-
_k = "\nheight=";
67-
p = result_s.find(_k);
68-
if (p == std::string::npos) {
69-
throws("This video has no height.");
70-
return nullptr;
71-
}
72-
p += _k.length();
73-
p2 = result_s.find("\n", p);
74-
sscanf(result_s.substr(p, p2 - p).c_str(), "%d", &vp->height);
39+
VideoProperties *vp = new VideoProperties();
7540

76-
_k = "\nnb_frames=";
77-
p = result_s.find(_k);
78-
if (p == std::string::npos) {
41+
double rate_l, rate_r; // fps = rate_l / rate_r;
42+
char result_c[STDOUT_SIZE];
43+
if (exec_r(cmd.c_str(), result_c)) {
44+
throws("Failed to analysis video.");
45+
return nullptr;
46+
}
47+
std::string result_s(result_c), _k;
48+
std::size_t p, p2;
49+
50+
_k = "\nwidth=";
51+
p = result_s.find(_k);
52+
if (p == std::string::npos) {
53+
throws("This video has no width.");
54+
return nullptr;
55+
}
7956
p += _k.length();
8057
p2 = result_s.find("\n", p);
81-
sscanf(result_s.substr(p, p2 - p).c_str(), "%d", &vp->nb_frames);
82-
}
58+
sscanf(result_s.substr(p, p2 - p).c_str(), "%d", &vp->width);
8359

84-
double _l, _r;
85-
_k = "\nr_frame_rate=";
86-
p = result_s.find(_k);
87-
if (p == std::string::npos) {
88-
throws("This video has no frame rate.");
89-
return nullptr;
90-
}
91-
p += _k.length();
92-
p2 = result_s.find("/", p);
93-
sscanf(result_s.substr(p, p2 - p).c_str(), "%lf", &_l);
94-
p = p2 + 1;
95-
p2 = result_s.find("\n", p);
96-
sscanf(result_s.substr(p, p2 - p).c_str(), "%lf", &_r);
97-
if (_r < 1) {
98-
throws("This video has no frame rate.");
99-
return nullptr;
100-
}
101-
vp->rate = _l / _r;
60+
_k = "\nheight=";
61+
p = result_s.find(_k);
62+
if (p == std::string::npos) {
63+
throws("This video has no height.");
64+
return nullptr;
65+
}
66+
p += _k.length();
67+
p2 = result_s.find("\n", p);
68+
sscanf(result_s.substr(p, p2 - p).c_str(), "%d", &vp->height);
69+
70+
_k = "\nnb_frames=";
71+
p = result_s.find(_k);
72+
if (p == std::string::npos) {
73+
p += _k.length();
74+
p2 = result_s.find("\n", p);
75+
sscanf(result_s.substr(p, p2 - p).c_str(), "%d", &vp->nb_frames);
76+
}
10277

103-
_k = "\nduration=";
104-
p = result_s.find(_k);
105-
if (p != std::string::npos) {
78+
double _l, _r;
79+
_k = "\nr_frame_rate=";
80+
p = result_s.find(_k);
81+
if (p == std::string::npos) {
82+
throws("This video has no frame rate.");
83+
return nullptr;
84+
}
10685
p += _k.length();
86+
p2 = result_s.find("/", p);
87+
sscanf(result_s.substr(p, p2 - p).c_str(), "%lf", &_l);
88+
p = p2 + 1;
10789
p2 = result_s.find("\n", p);
108-
sscanf(result_s.substr(p, p2 - p).c_str(), "%lf", &vp->duration);
109-
}
90+
sscanf(result_s.substr(p, p2 - p).c_str(), "%lf", &_r);
91+
if (_r < 1) {
92+
throws("This video has no frame rate.");
93+
return nullptr;
94+
}
95+
vp->rate = _l / _r;
96+
97+
_k = "\nduration=";
98+
p = result_s.find(_k);
99+
if (p != std::string::npos) {
100+
p += _k.length();
101+
p2 = result_s.find("\n", p);
102+
sscanf(result_s.substr(p, p2 - p).c_str(), "%lf", &vp->duration);
103+
}
110104

111-
return vp;
112-
}
105+
return vp;
106+
}
113107

114-
inline int ready_to_read() {
115-
using namespace decode_ffmpeg;
108+
inline int ready_to_read(int _x, int _y) {
109+
x = _x;
110+
y = _y;
111+
xy = x * y;
112+
std::string cmd = (std::string) "ffmpeg -v quiet -i \"" + video + "\" -vf scale=" + std::to_string(x) + ":" + std::to_string(y) + " -c:v rawvideo -pix_fmt gray -f rawvideo -";
113+
// printf("%s\n", cmd.c_str());
114+
115+
fp = rb_popen(cmd);
116+
if (!fp) {
117+
throws("Failed to build pipe.");
118+
return 1;
119+
}
120+
return 0;
121+
}
116122

117-
std::string cmd = (std::string) "ffmpeg -v quiet -i \"" + video + "\" -vf scale=" + std::to_string(x) + ":" + std::to_string(y) + " -c:v rawvideo -pix_fmt gray -f rawvideo -";
118-
// printf("%s\n", cmd.c_str());
123+
inline int read_a_frame(B *f) {
124+
return xy ^ fread(f, 1, xy, fp);
125+
}
119126

120-
fp = rb_popen(cmd);
121-
if (!fp) {
122-
throws("Failed to build pipe.");
123-
return 1;
127+
inline void cls() {
128+
pipe_pclose(fp);
124129
}
125-
return 0;
126-
}
127-
128-
inline int read_a_frame(B *f) {
129-
using namespace decode_ffmpeg;
130-
return xy ^ fread(f, 1, xy, fp);
131-
}
132-
133-
inline void cls() {
134-
using namespace decode_ffmpeg;
135-
pipe_pclose(fp);
136-
}
130+
};

0 commit comments

Comments
 (0)