Skip to content

Commit 1c9eec6

Browse files
committed
Merge pull request #104951 from jitspoe/master.avi_16bit_audio
Default mjpeg avi movie writer to 16 bit audio and add an editor option so it can still write 32 bit.
2 parents 822fd08 + 9a4ec17 commit 1c9eec6

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

doc/classes/ProjectSettings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,9 @@
10911091
<member name="editor/import/use_multiple_threads" type="bool" setter="" getter="" default="true">
10921092
If [code]true[/code] importing of resources is run on multiple threads.
10931093
</member>
1094+
<member name="editor/movie_writer/audio_bit_depth" type="int" setter="" getter="" default="16">
1095+
Number of bits per audio sample written to the [code].avi[/code] file. Only 16 and 32-bit are supported.
1096+
</member>
10941097
<member name="editor/movie_writer/disable_vsync" type="bool" setter="" getter="" default="false">
10951098
If [code]true[/code], requests V-Sync to be disabled when writing a movie (similar to setting [member display/window/vsync/vsync_mode] to [b]Disabled[/b]). This can speed up video writing if the hardware is fast enough to render, encode and save the video at a framerate higher than the monitor's refresh rate.
10961099
[b]Note:[/b] [member editor/movie_writer/disable_vsync] has no effect if the operating system or graphics driver forces V-Sync with no way for applications to disable it.

modules/jpg/movie_writer_mjpeg.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Error MovieWriterMJPEG::write_begin(const Size2i &p_movie_size, uint32_t p_fps,
134134

135135
// Audio //
136136

137-
const uint32_t bit_depth = 32;
137+
const uint32_t bit_depth = audio_bit_depth;
138138
uint32_t channels = 2;
139139
switch (speaker_mode) {
140140
case AudioServer::SPEAKER_MODE_STEREO:
@@ -208,7 +208,18 @@ Error MovieWriterMJPEG::write_frame(const Ref<Image> &p_image, const int32_t *p_
208208

209209
f->store_buffer((const uint8_t *)"01wb", 4); // Stream 1, Audio.
210210
f->store_32(audio_block_size);
211-
f->store_buffer((const uint8_t *)p_audio_data, audio_block_size);
211+
if (audio_bit_depth == 16) {
212+
// Convert from 32bit to 16bit.
213+
Vector<int16_t> audio_buffer_16;
214+
int num_samples = audio_block_size / 2;
215+
audio_buffer_16.resize(num_samples);
216+
for (int i = 0; i < num_samples; ++i) {
217+
audio_buffer_16.write[i] = (int16_t)(p_audio_data[i] >> 16);
218+
}
219+
f->store_buffer((const uint8_t *)audio_buffer_16.ptr(), audio_block_size);
220+
} else {
221+
f->store_buffer((const uint8_t *)p_audio_data, audio_block_size);
222+
}
212223

213224
frame_count++;
214225

@@ -261,4 +272,5 @@ MovieWriterMJPEG::MovieWriterMJPEG() {
261272
mix_rate = GLOBAL_GET("editor/movie_writer/mix_rate");
262273
speaker_mode = AudioServer::SpeakerMode(int(GLOBAL_GET("editor/movie_writer/speaker_mode")));
263274
quality = GLOBAL_GET("editor/movie_writer/video_quality");
275+
audio_bit_depth = GLOBAL_GET("editor/movie_writer/audio_bit_depth");
264276
}

modules/jpg/movie_writer_mjpeg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class MovieWriterMJPEG : public MovieWriter {
3737

3838
uint32_t mix_rate = 48000;
3939
AudioServer::SpeakerMode speaker_mode = AudioServer::SPEAKER_MODE_STEREO;
40+
uint32_t audio_bit_depth = 16;
4041
String base_path;
4142
uint32_t frame_count = 0;
4243
uint32_t fps = 0;

servers/movie_writer/movie_writer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ void MovieWriter::_bind_methods() {
157157
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/mix_rate", PROPERTY_HINT_RANGE, "8000,192000,1,suffix:Hz"), 48000);
158158
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/speaker_mode", PROPERTY_HINT_ENUM, "Stereo,3.1,5.1,7.1"), 0);
159159
GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "editor/movie_writer/video_quality", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), 0.75);
160+
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/audio_bit_depth", PROPERTY_HINT_ENUM, "16:16,32:32"), 16);
160161
GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "editor/movie_writer/ogv/audio_quality", PROPERTY_HINT_RANGE, "-0.1,1.0,0.01"), 0.5);
161162
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/ogv/encoding_speed", PROPERTY_HINT_ENUM, "Fastest (Lowest Efficiency):4,Fast (Low Efficiency):3,Slow (High Efficiency):2,Slowest (Highest Efficiency):1"), 4);
162163
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/ogv/keyframe_interval", PROPERTY_HINT_RANGE, "1,1024,1"), 64);

0 commit comments

Comments
 (0)