-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheffect_player.cpp
296 lines (264 loc) · 10.5 KB
/
effect_player.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "effect_player.hpp"
#include <iostream>
#include <optional>
#include <iostream>
namespace bnb::oep
{
class js_callback : public bnb::interfaces::js_callback
{
public:
js_callback(oep_eval_js_result_cb callback)
: m_callback(std::move(callback)){};
void on_result(const std::string& result) override
{
m_callback(result);
}
private:
oep_eval_js_result_cb m_callback;
}; /* class js_callback */
} /* namespace bnb::oep */
namespace bnb::oep
{
/* effect_player::create */
effect_player_sptr interfaces::effect_player::create(int32_t width, int32_t height)
{
// This particular example relies on OpenGL, so it should be explicitly requested
bnb::interfaces::effect_player::set_render_backend(::bnb::interfaces::render_backend_type::opengl);
return std::make_shared<bnb::oep::effect_player>(width, height);
}
// the description of the passed parameters to the Banuba SDK effext_player can be found at the link:
// https://docs.banuba.com/face-ar-sdk/generated/doxygen/html/structbnb_1_1interfaces_1_1effect__player__configuration.html#a810709129e2bc13eae190305861345ce
// NOTE: The parameters fx_width and fx height explicitly influence performance,
// for instance, if you have a small screen, e.g. 6 inches, and your rendering surface
// is large, e.g. 4K, then it is not necessary to render effect in 4K resolution
// since such precision will not be seen on the screen, so the performance can be improved
// via rendering the effect on a smaller surface.
// In the sample effect frame buffer and the surface are synced in surface_created and surface_changed methods.
/* effect_player::effect_player CONSTRUCTOR */
effect_player::effect_player(int32_t width, int32_t height)
: m_ep(bnb::interfaces::effect_player::create(bnb::interfaces::effect_player_configuration::create(
width, // fx_width - the effect's framebuffer width
height // fx_height - the effect's framebuffer height
)))
{
// Disable future filter. See method description for details.
m_ep->set_recognizer_use_future_filter(false);
// Remove freeze during effect activation
m_ep->set_render_consistency_mode(bnb::interfaces::consistency_mode::asynchronous_consistent_when_effect_loaded);
}
/* effect_player::~effect_player */
effect_player::~effect_player()
{
}
/* effect_player::surface_created */
void effect_player::surface_created(int32_t width, int32_t height)
{
m_ep->surface_created(width, height);
surface_changed(width, height);
}
/* effect_player::surface_changed */
void effect_player::surface_changed(int32_t width, int32_t height)
{
m_ep->surface_changed(width, height);
// Set explicitly the framebuffer of Effect Player to sync with surface size
if (auto em = m_ep->effect_manager()) {
em->set_effect_size(width, height);
}
}
/* effect_player::surface_destroyed */
void effect_player::surface_destroyed()
{
m_ep->surface_destroyed();
}
/* effect_player::load_effect */
bool effect_player::load_effect(const std::string& effect)
{
if (auto effect_manager = m_ep->effect_manager()) {
effect_manager->load(effect);
return true;
}
return false;
}
/* effect_player::call_js_method */
bool effect_player::call_js_method(const std::string& method, const std::string& param)
{
if (auto e_manager = m_ep->effect_manager()) {
if (auto effect = e_manager->current()) {
effect->call_js_method(method, param);
} else {
std::cout << "[Error] effect not loaded" << std::endl;
return false;
}
} else {
std::cout << "[Error] effect manager not initialized" << std::endl;
return false;
}
return true;
}
/* effect_player::eval_js */
void effect_player::eval_js(const std::string& script, oep_eval_js_result_cb result_callback)
{
if (auto e_manager = m_ep->effect_manager()) {
if (auto effect = e_manager->current()) {
std::shared_ptr<bnb::oep::js_callback> callback
= result_callback ? std::make_shared<bnb::oep::js_callback>(std::move(result_callback)) : nullptr;
effect->eval_js(script, callback);
} else {
std::cout << "[Error] effect not loaded" << std::endl;
}
} else {
std::cout << "[Error] effect manager not initialized" << std::endl;
}
}
/* effect_player::pause */
void effect_player::pause()
{
m_ep->playback_pause();
}
/* effect_player::resume */
void effect_player::resume()
{
m_ep->playback_play();
}
/* effect_player::stop */
void effect_player::stop()
{
m_ep->playback_stop();
}
/* effect_player::push_frame */
void effect_player::push_frame(pixel_buffer_sptr image, bnb::oep::interfaces::rotation image_orientation, bool require_mirroring)
{
using ns = bnb::oep::interfaces::image_format;
auto bnb_image_format = make_bnb_image_format(image, image_orientation, require_mirroring);
switch (image->get_image_format()) {
case ns::bpc8_rgb:
case ns::bpc8_bgr:
case ns::bpc8_rgba:
case ns::bpc8_bgra:
case ns::bpc8_argb:
m_ep->push_frame(
full_image_t(bpc8_image_t(
color_plane(image->get_base_sptr()),
make_bnb_pixel_format(image),
bnb_image_format)));
break;
case ns::nv12_bt601_full:
case ns::nv12_bt601_video:
case ns::nv12_bt709_full:
case ns::nv12_bt709_video:
m_ep->push_frame(
full_image_t(yuv_image_t(
color_plane(image->get_base_sptr_of_plane(0)),
color_plane(image->get_base_sptr_of_plane(1)),
bnb_image_format,
make_bnb_yuv_format(image))));
break;
case ns::i420_bt601_full:
case ns::i420_bt601_video:
case ns::i420_bt709_full:
case ns::i420_bt709_video:
m_ep->push_frame(
full_image_t(yuv_image_t(
color_plane(image->get_base_sptr_of_plane(0)),
color_plane(image->get_base_sptr_of_plane(1)),
color_plane(image->get_base_sptr_of_plane(2)),
bnb_image_format,
make_bnb_yuv_format(image))));
break;
default:
break;
}
}
/* effect_player::draw */
int64_t effect_player::draw()
{
return m_ep->draw();
}
/* effect_player::make_bnb_image_format */
bnb::image_format effect_player::make_bnb_image_format(pixel_buffer_sptr image, interfaces::rotation orientation, bool require_mirroring)
{
bnb::camera_orientation camera_orient {bnb::camera_orientation::deg_0};
using ns = bnb::oep::interfaces::rotation;
switch (orientation) {
case ns::deg0:
break;
case ns::deg90:
camera_orient = bnb::camera_orientation::deg_90;
break;
case ns::deg180:
camera_orient = bnb::camera_orientation::deg_180;
break;
break;
case ns::deg270:
camera_orient = bnb::camera_orientation::deg_270;
break;
}
return {static_cast<uint32_t>(image->get_width()), static_cast<uint32_t>(image->get_height()), camera_orient, require_mirroring, 0, std::nullopt};
}
/* effect_player::make_bnb_yuv_format */
bnb::yuv_format_t effect_player::make_bnb_yuv_format(pixel_buffer_sptr image)
{
bnb::yuv_format format {bnb::yuv_format::yuv_nv12}; /* i.e. NV12 or I420 */
bnb::color_std standard {bnb::color_std::bt601}; /* i.e. BT.601 or BT.709 */
bnb::color_range range {bnb::color_range::full}; /* i.e. "full" or "video" */
using ns = bnb::oep::interfaces::image_format;
switch (image->get_image_format()) {
case ns::nv12_bt601_full:
break;
case ns::nv12_bt601_video:
range = bnb::color_range::video;
break;
case ns::nv12_bt709_full:
standard = bnb::color_std::bt709;
break;
case ns::nv12_bt709_video:
range = bnb::color_range::video;
standard = bnb::color_std::bt709;
break;
case ns::i420_bt601_full:
format = bnb::yuv_format::yuv_i420;
break;
case ns::i420_bt601_video:
range = bnb::color_range::video;
format = bnb::yuv_format::yuv_i420;
break;
case ns::i420_bt709_full:
format = bnb::yuv_format::yuv_i420;
standard = bnb::color_std::bt709;
break;
case ns::i420_bt709_video:
range = bnb::color_range::video;
format = bnb::yuv_format::yuv_i420;
standard = bnb::color_std::bt709;
break;
default:
break;
}
return {range, standard, format};
}
/* effect_player::make_bnb_pixel_format */
bnb::interfaces::pixel_format effect_player::make_bnb_pixel_format(pixel_buffer_sptr image)
{
bnb::interfaces::pixel_format fmt {bnb::interfaces::pixel_format::rgb};
using ns = bnb::oep::interfaces::image_format;
switch (image->get_image_format()) {
case ns::bpc8_rgb:
break;
case ns::bpc8_bgr:
fmt = bnb::interfaces::pixel_format::bgr;
break;
case ns::bpc8_rgba:
fmt = bnb::interfaces::pixel_format::rgba;
break;
case ns::bpc8_bgra:
fmt = bnb::interfaces::pixel_format::bgra;
break;
case ns::bpc8_argb:
fmt = bnb::interfaces::pixel_format::argb;
break;
default:
break;
}
return fmt;
}
} /* namespace bnb::oep */