forked from cherrythecool/gdwebm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgodot_webm_callback.cpp
More file actions
143 lines (116 loc) · 4.87 KB
/
godot_webm_callback.cpp
File metadata and controls
143 lines (116 loc) · 4.87 KB
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
#include "godot_webm_callback.h"
#include "godot_cpp/core/math.hpp"
#include "godot_cpp/core/print_string.hpp"
#include "godot_cpp/variant/packed_float32_array.hpp"
#include "webm/status.h"
#include "opus.h"
#include <cstdlib>
GodotWebMCallback::GodotWebMCallback(GodotWebMFileInfo* p_file_info) {
file_info = p_file_info;
}
Status GodotWebMCallback::OnTrackEntry(const ElementMetadata& metadata,
const TrackEntry& track_entry) {
if (track_entry.codec_id.is_present()) {
const std::string& codec_id = track_entry.codec_id.value();
GodotWebMTrack track;
GodotWebMSupportedCodec codec = GDWEBM_UNSUPPORTED_CODEC;
track.entry = track_entry;
track.data = nullptr;
if (track_entry.video.is_present()) {
if (codec_id == "V_AV1") {
codec = GDWEBM_SUPPORTED_CODEC_V_AV1;
} else if (codec_id == "V_VP9") {
codec = GDWEBM_SUPPORTED_CODEC_V_VP9;
} else if (codec_id == "V_VP8") {
codec = GDWEBM_SUPPORTED_CODEC_V_VP8;
}
} else if (track_entry.audio.is_present()) {
if (codec_id == "A_OPUS") {
codec = GDWEBM_SUPPORTED_CODEC_A_OPUS;
GodotWebMOpusData* data = new GodotWebMOpusData;
if (data == nullptr) {
godot::print_error("Failed to allocate GodotWebMOpusData when loading Opus track");
return webm::Status(webm::Status::kNotEnoughMemory);
}
data->sample_rate = track_entry.audio.value().sampling_frequency.value();
data->channels = track_entry.audio.value().channels.value();
data->pcm.resize(data->channels * godot::Math::ceil(data->sample_rate * (120.0 / 1000.0)));
int error;
data->decoder = opus_decoder_create(
data->sample_rate,
data->channels,
&error
);
track.data = (void*)data;
if (error != OPUS_OK) {
godot::Array values;
values.push_back(error);
godot::print_error("Error creating Opus Decoder: %d", values);
}
} else if (codec_id == "A_VORBIS") {
codec = GDWEBM_SUPPORTED_CODEC_A_VORBIS;
}
}
track.codec = codec;
file_info->tracks[track_entry.track_number.value()] = track;
}
return webm::Status(webm::Status::kOkCompleted);
}
Status GodotWebMCallback::OnClusterBegin(const ElementMetadata& metadata,
const Cluster& cluster, Action* action) {
*action = Action::kRead;
if (cluster.timecode.is_present()) {
current_timecode_base = cluster.timecode.value();
}
return Status(Status::kOkCompleted);
}
webm::Status GodotWebMCallback::OnInfo(const webm::ElementMetadata &metadata, const webm::Info &info) {
if (info.timecode_scale.is_present()) {
file_info->timecodeScale = info.timecode_scale.value();
}
if (info.duration.is_present()) {
file_info->durationTimecode = info.duration.value();
}
return webm::Status(webm::Status::kOkCompleted);
}
webm::Status GodotWebMCallback::OnFrame(const FrameMetadata& metadata, Reader* reader, std::uint64_t* bytes_remaining) {
if (metadata.parent_element.id != webm::Id::kBlock && metadata.parent_element.id != webm::Id::kSimpleBlock) {
std::uint64_t skipped;
webm::Status skip_status = reader->Skip(*bytes_remaining, &skipped);
*bytes_remaining -= skipped;
while (!skip_status.completed_ok() && *bytes_remaining > 0) {
skip_status = reader->Skip(*bytes_remaining, &skipped);
*bytes_remaining -= skipped;
}
if (!skip_status.completed_ok()) {
return skip_status;
}
return webm::Status(webm::Status::kOkCompleted);
}
GodotWebMFrame frame;
frame.timecode = current_timecode_base + current_timecode;
frame.data.resize(metadata.size);
std::uint64_t read;
webm::Status read_status = reader->Read(*bytes_remaining, frame.data.ptrw(), &read);
*bytes_remaining -= read;
while (!read_status.completed_ok() && *bytes_remaining > 0) {
read_status = reader->Read(*bytes_remaining, frame.data.ptrw(), &read);
*bytes_remaining -= read;
}
godot::Vector<GodotWebMFrame>* frames = &file_info->tracks[current_track].frames;
frames->resize(frames->size() + 1);
frames->ptrw()[frames->size() - 1] = frame;
return webm::Status(webm::Status::kOkCompleted);
}
webm::Status GodotWebMCallback::OnBlockBegin(const ElementMetadata& metadata, const Block& block, Action* action) {
*action = Action::kRead;
current_track = block.track_number;
current_timecode = block.timecode;
return webm::Status(webm::Status::kOkCompleted);
}
webm::Status GodotWebMCallback::OnSimpleBlockBegin(const ElementMetadata& metadata, const SimpleBlock& simple_block, Action* action) {
*action = Action::kRead;
current_track = simple_block.track_number;
current_timecode = simple_block.timecode;
return webm::Status(webm::Status::kOkCompleted);
}