-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
120 lines (100 loc) · 4.68 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
#include <cairo.h>
#include <elements.hpp>
#include <VapourSynth4.h>
#include <VSScript4.h>
#include <exception>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
using namespace cycfi::elements;
int main(int argc, char** argv)
{
try {
int default_frame_number{0};
const char* script_file_path{[&] {
return argc >= 2 ? argv[1] : throw std::invalid_argument{"VSPreview error: Script file not specified"};
}()};
int frame_number{[&] {
if (argc < 3) {
return default_frame_number;
}
int number{std::stoi(argv[2])};
return number >= 0 ? number : throw std::invalid_argument{"VSPreview error: frame number must be a non-negative"};
}()};
// Init VS api and video data
const VSAPI* vs_api{[] {
const VSAPI* ptr{getVapourSynthAPI(VAPOURSYNTH_API_VERSION)};
return ptr ? ptr : throw std::runtime_error{"VSPreview error: VSApi loading error"};
}()};
const VSSCRIPTAPI* vs_script_api{[] {
const VSSCRIPTAPI* ptr{getVSScriptAPI(VSSCRIPT_API_VERSION)};
return ptr ? ptr : throw std::runtime_error{"VSPreview error: VSScriptApi loading error"};
}()};
VSScript* vs_script{[&] {
VSScript* ptr{vs_script_api->createScript(nullptr)};
return ptr ? ptr : throw std::runtime_error{"VSPreview error: Script creating error"};
}()};
vs_script_api->evaluateFile(vs_script, script_file_path);
VSNode* video_node{[&] {
VSNode* ptr{vs_script_api->getOutputNode(vs_script, 0)};
return ptr ? ptr : throw std::runtime_error{vs_script_api->getError(vs_script)};
}()};
// Safe max frame number
int video_frames_quantity{vs_api->getVideoInfo(video_node)->numFrames};
if (video_frames_quantity <= frame_number) {
throw std::invalid_argument{
"VSPreview error: frame number out of range. Selected frame: "
+ std::to_string(frame_number)
+ ". Max frame number: "
+ std::to_string(video_frames_quantity-1)
};
}
// Convert video format to RGB24
VSCore* vs_core{vs_script_api->getCore(vs_script)};
VSPlugin* pResizePlugin{vs_api->getPluginByID("com.vapoursynth.resize", vs_core)};
VSMap* pArgumentMap{vs_api->createMap()};
vs_api->mapSetNode(pArgumentMap, "clip", video_node, 0);
vs_api->mapSetInt(pArgumentMap, "format", pfRGB24, 0);
vs_api->mapSetData(pArgumentMap, "matrix_in_s", "709", 3, dtUtf8, 0);
VSMap* pResultMap{vs_api->invoke(pResizePlugin, "Bicubic", pArgumentMap)};
VSNode* pPreviewNode{vs_api->mapGetNode(pResultMap, "clip", 0, nullptr)};
vs_api->freeMap(pArgumentMap);
vs_api->freeMap(pResultMap);
const VSFrame* video_frame{vs_api->getFrame(frame_number, pPreviewNode, nullptr, 0)};
// Prepare video frame for elements
int frame_width{vs_api->getFrameWidth(video_frame, 0)};
int frame_height{vs_api->getFrameHeight(video_frame, 0)};
const uint8_t* frame_raw_plane_red{vs_api->getReadPtr(video_frame, 0)};
const uint8_t* frame_raw_plane_green{vs_api->getReadPtr(video_frame, 1)};
const uint8_t* frame_raw_plane_blue{vs_api->getReadPtr(video_frame, 2)};
// Init elements pixmap
pixmap_ptr pixmap_obj{std::make_shared<pixmap>(point{static_cast<float>(frame_width), static_cast<float>(frame_height)})};
pixmap_context pixmap_context{*pixmap_obj};
cairo_t* cairo_context{pixmap_context.context()};
cairo_surface_t* cairo_surface{cairo_get_target(cairo_context)};
unsigned char* surface_data{cairo_image_surface_get_data(cairo_surface)};
// copy frame to pixmap context
cairo_surface_flush(cairo_surface);
for (int i{0}; i < frame_width*frame_height*4; i += 4) {
surface_data[i] = frame_raw_plane_blue[i/4];
surface_data[i+1] = frame_raw_plane_green[i/4];
surface_data[i+2] = frame_raw_plane_red[i/4];
surface_data[i+3] = 255; // Alpha
}
cairo_surface_mark_dirty(cairo_surface);
// Init elements and create image from pixmap
app _app(argc, argv, "VapourSynth Preview 2", "com.endill.vapoursynth-preview-2");
window _win(_app.name());
_win.on_close = [&_app]() { _app.stop(); };
view view_(_win);
view_.content(
image{pixmap_obj}
);
_app.run();
vs_script_api->freeScript(vs_script);
} catch (std::exception &exception) {
std::cout << exception.what() << std::endl;
}
return 0;
}