-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_util.cc
119 lines (88 loc) · 2.81 KB
/
video_util.cc
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
#include <cassert>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include "Frame.hh"
using namespace cv;
void encode(const std::vector<std::string> &args)
{
VideoCapture cap("/home/klokik/Movies/sintel_trailer-480p.mp4");
assert(cap.isOpened());
std::ofstream ofs;
ofs.open("video.bfps", std::ios_base::out|std::ios_base::binary);
bool first_frame = true;
while (1)
{
Mat cap_mat;
cap >> cap_mat;
if(cap_mat.empty())
break;
cvtColor(cap_mat, cap_mat, CV_BGR2GRAY);
resize(cap_mat, cap_mat, Size(0, 0), 0.1, 0.1);
if (first_frame)
{
std::cout << "Source size: (" << cap_mat.cols << ";" << cap_mat.rows << ")" <<std::endl;
first_frame = false;
}
BIVCodec::ImageMatrix mat_source(cap_mat.cols, cap_mat.rows, BIVCodec::ColorSpace::Grayscale, cap_mat.ptr(0));
// mat_source = std::move(BIVCodec::matrixMap(mat_source, [](auto a) { return a/256; }));
BIVCodec::ImageBSP bsp_source(mat_source, BIVCodec::ColorSpace::Grayscale);
auto frame_chain = std::move(bsp_source.asFrameChain());
for (auto frame : frame_chain)
{
auto data = frame.serialize();
assert(data.size() == 8);
ofs.write(reinterpret_cast<char*>(&data[0]), data.size());
}
std::cout << "|" << std::flush;
}
std::cout << std::endl;
ofs.close();
}
void playback(const std::vector<std::string> &args)
{
std::ifstream ifs;
ifs.open("video.bfps", std::ios_base::in|std::ios_base::binary);
BIVCodec::ImageBSP bsp_image(BIVCodec::ColorSpace::Grayscale);
char data[8];
while (!ifs.eof())
{
BIVCodec::Frame frame;
ifs.read(&data[0], 8);
frame.deserialize(reinterpret_cast<uint8_t*>(&data[0]));
if (frame.header.type == BIVCodec::FrameHeader::HeaderType::Sync)
{
auto sync = std::static_pointer_cast<BIVCodec::FrameSyncData>(frame.data);
bsp_image.applyFrameData(*sync);
BIVCodec::ImageMatrix mat_image = std::move(bsp_image.asImageMatrix(std::min(sync->width*4, 512)));
mat_image = std::move(BIVCodec::matrixMap(mat_image, [](auto a) { return a/256; }));
Mat dec_mat(mat_image.height, mat_image.width, CV_32F, mat_image.data());
imshow("BIVCodec", dec_mat);
std::cout << "|" << std::flush;
if (waitKey(5) == 27)
break;
}
else
bsp_image.applyFrameData(*std::static_pointer_cast<BIVCodec::FrameImageData>(frame.data));
}
std::cout << std::endl;
ifs.close();
}
int main(int argc, const char **argv)
{
if (argc == 1)
{
std::cout << "encode or playback?" << std::endl;
return 0;
}
std::vector<std::string> args;
for(int i = 1; i < argc; ++i)
args.push_back(std::string(argv[i]));
if (args[0] == "encode")
encode(args);
else if(args[0] == "playback")
playback(args);
return 0;
}