Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/movie_writer/movie_writer_pngwav.cpp
11352 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
#include "core/io/file_access.h"
35
36
uint32_t MovieWriterPNGWAV::get_audio_mix_rate() const {
37
return mix_rate;
38
}
39
AudioServer::SpeakerMode MovieWriterPNGWAV::get_audio_speaker_mode() const {
40
return speaker_mode;
41
}
42
43
void MovieWriterPNGWAV::get_supported_extensions(List<String> *r_extensions) const {
44
r_extensions->push_back("png");
45
}
46
47
bool MovieWriterPNGWAV::handles_file(const String &p_path) const {
48
return p_path.get_extension().to_lower() == "png";
49
}
50
51
String MovieWriterPNGWAV::zeros_str(uint32_t p_index) {
52
char zeros[MAX_TRAILING_ZEROS + 1];
53
for (uint32_t i = 0; i < MAX_TRAILING_ZEROS; i++) {
54
uint32_t idx = MAX_TRAILING_ZEROS - i - 1;
55
uint32_t digit = (p_index / uint32_t(Math::pow(double(10), double(idx)))) % 10;
56
zeros[i] = '0' + digit;
57
}
58
zeros[MAX_TRAILING_ZEROS] = 0;
59
return zeros;
60
}
61
62
Error MovieWriterPNGWAV::write_begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) {
63
// Quick & Dirty PNGWAV Code based on - https://docs.microsoft.com/en-us/windows/win32/directshow/avi-riff-file-reference
64
65
base_path = p_base_path.get_basename();
66
if (base_path.is_relative_path()) {
67
base_path = "res://" + base_path;
68
}
69
70
{
71
//Remove existing files before writing anew
72
uint32_t idx = 0;
73
Ref<DirAccess> d = DirAccess::open(base_path.get_base_dir());
74
ERR_FAIL_COND_V(d.is_null(), FAILED);
75
76
String file = base_path.get_file();
77
while (true) {
78
String path = file + zeros_str(idx) + ".png";
79
if (d->remove(path) != OK) {
80
break;
81
}
82
}
83
}
84
85
f_wav = FileAccess::open(base_path + ".wav", FileAccess::WRITE_READ);
86
ERR_FAIL_COND_V(f_wav.is_null(), ERR_CANT_OPEN);
87
88
fps = p_fps;
89
90
f_wav->store_buffer((const uint8_t *)"RIFF", 4);
91
int total_size = 4 /* WAVE */ + 8 /* fmt+size */ + 16 /* format */ + 8 /* data+size */;
92
f_wav->store_32(total_size); //will store final later
93
f_wav->store_buffer((const uint8_t *)"WAVE", 4);
94
95
/* FORMAT CHUNK */
96
97
f_wav->store_buffer((const uint8_t *)"fmt ", 4);
98
99
uint32_t channels = 2;
100
switch (speaker_mode) {
101
case AudioServer::SPEAKER_MODE_STEREO:
102
channels = 2;
103
break;
104
case AudioServer::SPEAKER_SURROUND_31:
105
channels = 4;
106
break;
107
case AudioServer::SPEAKER_SURROUND_51:
108
channels = 6;
109
break;
110
case AudioServer::SPEAKER_SURROUND_71:
111
channels = 8;
112
break;
113
}
114
115
f_wav->store_32(16); //standard format, no extra fields
116
f_wav->store_16(1); // compression code, standard PCM
117
f_wav->store_16(channels); //CHANNELS: 2
118
119
f_wav->store_32(mix_rate);
120
121
/* useless stuff the format asks for */
122
123
int bits_per_sample = 32;
124
int blockalign = bits_per_sample / 8 * channels;
125
int bytes_per_sec = mix_rate * blockalign;
126
127
audio_block_size = (mix_rate / fps) * blockalign;
128
129
f_wav->store_32(bytes_per_sec);
130
f_wav->store_16(blockalign); // block align (unused)
131
f_wav->store_16(bits_per_sample);
132
133
/* DATA CHUNK */
134
135
f_wav->store_buffer((const uint8_t *)"data", 4);
136
137
f_wav->store_32(0); //data size... wooh
138
wav_data_size_pos = f_wav->get_position();
139
140
return OK;
141
}
142
143
Error MovieWriterPNGWAV::write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data) {
144
ERR_FAIL_COND_V(f_wav.is_null(), ERR_UNCONFIGURED);
145
146
Vector<uint8_t> png_buffer = p_image->save_png_to_buffer();
147
148
Ref<FileAccess> fi = FileAccess::open(base_path + zeros_str(frame_count) + ".png", FileAccess::WRITE);
149
fi->store_buffer(png_buffer.ptr(), png_buffer.size());
150
f_wav->store_buffer((const uint8_t *)p_audio_data, audio_block_size);
151
152
frame_count++;
153
154
return OK;
155
}
156
157
void MovieWriterPNGWAV::write_end() {
158
if (f_wav.is_valid()) {
159
uint32_t total_size = 4 /* WAVE */ + 8 /* fmt+size */ + 16 /* format */ + 8 /* data+size */;
160
uint32_t datasize = f_wav->get_position() - wav_data_size_pos;
161
f_wav->seek(4);
162
f_wav->store_32(total_size + datasize);
163
f_wav->seek(0x28);
164
f_wav->store_32(datasize);
165
}
166
}
167
168
MovieWriterPNGWAV::MovieWriterPNGWAV() {
169
mix_rate = GLOBAL_GET("editor/movie_writer/mix_rate");
170
speaker_mode = AudioServer::SpeakerMode(int(GLOBAL_GET("editor/movie_writer/speaker_mode")));
171
}
172
173