Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/movie_writer/movie_writer_pngwav.cpp
10277 views
1
/**************************************************************************/
2
/* movie_writer_pngwav.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "movie_writer_pngwav.h"
32
#include "core/config/project_settings.h"
33
#include "core/io/dir_access.h"
34
35
uint32_t MovieWriterPNGWAV::get_audio_mix_rate() const {
36
return mix_rate;
37
}
38
AudioServer::SpeakerMode MovieWriterPNGWAV::get_audio_speaker_mode() const {
39
return speaker_mode;
40
}
41
42
void MovieWriterPNGWAV::get_supported_extensions(List<String> *r_extensions) const {
43
r_extensions->push_back("png");
44
}
45
46
bool MovieWriterPNGWAV::handles_file(const String &p_path) const {
47
return p_path.get_extension().to_lower() == "png";
48
}
49
50
String MovieWriterPNGWAV::zeros_str(uint32_t p_index) {
51
char zeros[MAX_TRAILING_ZEROS + 1];
52
for (uint32_t i = 0; i < MAX_TRAILING_ZEROS; i++) {
53
uint32_t idx = MAX_TRAILING_ZEROS - i - 1;
54
uint32_t digit = (p_index / uint32_t(Math::pow(double(10), double(idx)))) % 10;
55
zeros[i] = '0' + digit;
56
}
57
zeros[MAX_TRAILING_ZEROS] = 0;
58
return zeros;
59
}
60
61
Error MovieWriterPNGWAV::write_begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) {
62
// Quick & Dirty PNGWAV Code based on - https://docs.microsoft.com/en-us/windows/win32/directshow/avi-riff-file-reference
63
64
base_path = p_base_path.get_basename();
65
if (base_path.is_relative_path()) {
66
base_path = "res://" + base_path;
67
}
68
69
{
70
//Remove existing files before writing anew
71
uint32_t idx = 0;
72
Ref<DirAccess> d = DirAccess::open(base_path.get_base_dir());
73
ERR_FAIL_COND_V(d.is_null(), FAILED);
74
75
String file = base_path.get_file();
76
while (true) {
77
String path = file + zeros_str(idx) + ".png";
78
if (d->remove(path) != OK) {
79
break;
80
}
81
}
82
}
83
84
f_wav = FileAccess::open(base_path + ".wav", FileAccess::WRITE_READ);
85
ERR_FAIL_COND_V(f_wav.is_null(), ERR_CANT_OPEN);
86
87
fps = p_fps;
88
89
f_wav->store_buffer((const uint8_t *)"RIFF", 4);
90
int total_size = 4 /* WAVE */ + 8 /* fmt+size */ + 16 /* format */ + 8 /* data+size */;
91
f_wav->store_32(total_size); //will store final later
92
f_wav->store_buffer((const uint8_t *)"WAVE", 4);
93
94
/* FORMAT CHUNK */
95
96
f_wav->store_buffer((const uint8_t *)"fmt ", 4);
97
98
uint32_t channels = 2;
99
switch (speaker_mode) {
100
case AudioServer::SPEAKER_MODE_STEREO:
101
channels = 2;
102
break;
103
case AudioServer::SPEAKER_SURROUND_31:
104
channels = 4;
105
break;
106
case AudioServer::SPEAKER_SURROUND_51:
107
channels = 6;
108
break;
109
case AudioServer::SPEAKER_SURROUND_71:
110
channels = 8;
111
break;
112
}
113
114
f_wav->store_32(16); //standard format, no extra fields
115
f_wav->store_16(1); // compression code, standard PCM
116
f_wav->store_16(channels); //CHANNELS: 2
117
118
f_wav->store_32(mix_rate);
119
120
/* useless stuff the format asks for */
121
122
int bits_per_sample = 32;
123
int blockalign = bits_per_sample / 8 * channels;
124
int bytes_per_sec = mix_rate * blockalign;
125
126
audio_block_size = (mix_rate / fps) * blockalign;
127
128
f_wav->store_32(bytes_per_sec);
129
f_wav->store_16(blockalign); // block align (unused)
130
f_wav->store_16(bits_per_sample);
131
132
/* DATA CHUNK */
133
134
f_wav->store_buffer((const uint8_t *)"data", 4);
135
136
f_wav->store_32(0); //data size... wooh
137
wav_data_size_pos = f_wav->get_position();
138
139
return OK;
140
}
141
142
Error MovieWriterPNGWAV::write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data) {
143
ERR_FAIL_COND_V(f_wav.is_null(), ERR_UNCONFIGURED);
144
145
Vector<uint8_t> png_buffer = p_image->save_png_to_buffer();
146
147
Ref<FileAccess> fi = FileAccess::open(base_path + zeros_str(frame_count) + ".png", FileAccess::WRITE);
148
fi->store_buffer(png_buffer.ptr(), png_buffer.size());
149
f_wav->store_buffer((const uint8_t *)p_audio_data, audio_block_size);
150
151
frame_count++;
152
153
return OK;
154
}
155
156
void MovieWriterPNGWAV::write_end() {
157
if (f_wav.is_valid()) {
158
uint32_t total_size = 4 /* WAVE */ + 8 /* fmt+size */ + 16 /* format */ + 8 /* data+size */;
159
uint32_t datasize = f_wav->get_position() - wav_data_size_pos;
160
f_wav->seek(4);
161
f_wav->store_32(total_size + datasize);
162
f_wav->seek(0x28);
163
f_wav->store_32(datasize);
164
}
165
}
166
167
MovieWriterPNGWAV::MovieWriterPNGWAV() {
168
mix_rate = GLOBAL_GET("editor/movie_writer/mix_rate");
169
speaker_mode = AudioServer::SpeakerMode(int(GLOBAL_GET("editor/movie_writer/speaker_mode")));
170
}
171
172