initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
6
servers/movie_writer/SCsub
Normal file
6
servers/movie_writer/SCsub
Normal file
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
from misc.utility.scons_hints import *
|
||||
|
||||
Import("env")
|
||||
|
||||
env.add_source_files(env.servers_sources, "*.cpp")
|
261
servers/movie_writer/movie_writer.cpp
Normal file
261
servers/movie_writer/movie_writer.cpp
Normal file
@@ -0,0 +1,261 @@
|
||||
/**************************************************************************/
|
||||
/* movie_writer.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "movie_writer.h"
|
||||
#include "core/config/project_settings.h"
|
||||
#include "core/io/dir_access.h"
|
||||
#include "core/os/time.h"
|
||||
#include "scene/main/window.h"
|
||||
#include "servers/audio/audio_driver_dummy.h"
|
||||
#include "servers/display_server.h"
|
||||
#include "servers/rendering_server.h"
|
||||
|
||||
MovieWriter *MovieWriter::writers[MovieWriter::MAX_WRITERS];
|
||||
uint32_t MovieWriter::writer_count = 0;
|
||||
|
||||
void MovieWriter::add_writer(MovieWriter *p_writer) {
|
||||
ERR_FAIL_COND(writer_count == MAX_WRITERS);
|
||||
writers[writer_count++] = p_writer;
|
||||
}
|
||||
|
||||
MovieWriter *MovieWriter::find_writer_for_file(const String &p_file) {
|
||||
for (int32_t i = writer_count - 1; i >= 0; i--) { // More recent last, to have override ability.
|
||||
if (writers[i]->handles_file(p_file)) {
|
||||
return writers[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
uint32_t MovieWriter::get_audio_mix_rate() const {
|
||||
uint32_t ret = 48000;
|
||||
GDVIRTUAL_CALL(_get_audio_mix_rate, ret);
|
||||
return ret;
|
||||
}
|
||||
AudioServer::SpeakerMode MovieWriter::get_audio_speaker_mode() const {
|
||||
AudioServer::SpeakerMode ret = AudioServer::SPEAKER_MODE_STEREO;
|
||||
GDVIRTUAL_CALL(_get_audio_speaker_mode, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error MovieWriter::write_begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) {
|
||||
Error ret = ERR_UNCONFIGURED;
|
||||
GDVIRTUAL_CALL(_write_begin, p_movie_size, p_fps, p_base_path, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error MovieWriter::write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data) {
|
||||
Error ret = ERR_UNCONFIGURED;
|
||||
GDVIRTUAL_CALL(_write_frame, p_image, p_audio_data, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void MovieWriter::write_end() {
|
||||
GDVIRTUAL_CALL(_write_end);
|
||||
}
|
||||
|
||||
bool MovieWriter::handles_file(const String &p_path) const {
|
||||
bool ret = false;
|
||||
GDVIRTUAL_CALL(_handles_file, p_path, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void MovieWriter::get_supported_extensions(List<String> *r_extensions) const {
|
||||
Vector<String> exts;
|
||||
GDVIRTUAL_CALL(_get_supported_extensions, exts);
|
||||
for (int i = 0; i < exts.size(); i++) {
|
||||
r_extensions->push_back(exts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void MovieWriter::begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) {
|
||||
project_name = GLOBAL_GET("application/config/name");
|
||||
|
||||
print_line(vformat("Movie Maker mode enabled, recording movie at %d FPS...", p_fps));
|
||||
|
||||
// When using Display/Window/Stretch/Mode = Viewport, use the project's
|
||||
// configured viewport size instead of the size of the window in the OS
|
||||
Size2i actual_movie_size = p_movie_size;
|
||||
String stretch_mode = GLOBAL_GET("display/window/stretch/mode");
|
||||
if (stretch_mode == "viewport") {
|
||||
actual_movie_size.width = GLOBAL_GET("display/window/size/viewport_width");
|
||||
actual_movie_size.height = GLOBAL_GET("display/window/size/viewport_height");
|
||||
|
||||
print_line(vformat("Movie Maker mode using project viewport size: %dx%d",
|
||||
actual_movie_size.width, actual_movie_size.height));
|
||||
}
|
||||
|
||||
// Check for available disk space and warn the user if needed.
|
||||
Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
|
||||
String path = p_base_path.get_basename();
|
||||
if (path.is_relative_path()) {
|
||||
path = "res://" + path;
|
||||
}
|
||||
dir->open(path);
|
||||
if (dir->get_space_left() < 10 * Math::pow(1024.0, 3.0)) {
|
||||
// Less than 10 GiB available.
|
||||
WARN_PRINT(vformat("Current available space on disk is low (%s). MovieWriter will fail during movie recording if the disk runs out of available space.", String::humanize_size(dir->get_space_left())));
|
||||
}
|
||||
|
||||
cpu_time = 0.0f;
|
||||
gpu_time = 0.0f;
|
||||
encoding_time_usec = 0;
|
||||
|
||||
mix_rate = get_audio_mix_rate();
|
||||
AudioDriverDummy::get_dummy_singleton()->set_mix_rate(mix_rate);
|
||||
AudioDriverDummy::get_dummy_singleton()->set_speaker_mode(AudioDriver::SpeakerMode(get_audio_speaker_mode()));
|
||||
fps = p_fps;
|
||||
if ((mix_rate % fps) != 0) {
|
||||
WARN_PRINT("MovieWriter's audio mix rate (" + itos(mix_rate) + ") can not be divided by the recording FPS (" + itos(fps) + "). Audio may go out of sync over time.");
|
||||
}
|
||||
|
||||
audio_channels = AudioDriverDummy::get_dummy_singleton()->get_channels();
|
||||
audio_mix_buffer.resize(mix_rate * audio_channels / fps);
|
||||
|
||||
write_begin(actual_movie_size, p_fps, p_base_path);
|
||||
}
|
||||
|
||||
void MovieWriter::_bind_methods() {
|
||||
ClassDB::bind_static_method("MovieWriter", D_METHOD("add_writer", "writer"), &MovieWriter::add_writer);
|
||||
|
||||
GDVIRTUAL_BIND(_get_audio_mix_rate)
|
||||
GDVIRTUAL_BIND(_get_audio_speaker_mode)
|
||||
|
||||
GDVIRTUAL_BIND(_handles_file, "path")
|
||||
|
||||
GDVIRTUAL_BIND(_write_begin, "movie_size", "fps", "base_path")
|
||||
GDVIRTUAL_BIND(_write_frame, "frame_image", "audio_frame_block")
|
||||
GDVIRTUAL_BIND(_write_end)
|
||||
|
||||
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/mix_rate", PROPERTY_HINT_RANGE, "8000,192000,1,suffix:Hz"), 48000);
|
||||
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/speaker_mode", PROPERTY_HINT_ENUM, "Stereo,3.1,5.1,7.1"), 0);
|
||||
GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "editor/movie_writer/video_quality", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), 0.75);
|
||||
GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "editor/movie_writer/ogv/audio_quality", PROPERTY_HINT_RANGE, "-0.1,1.0,0.01"), 0.5);
|
||||
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);
|
||||
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/movie_writer/ogv/keyframe_interval", PROPERTY_HINT_RANGE, "1,1024,1"), 64);
|
||||
|
||||
// Used by the editor.
|
||||
GLOBAL_DEF_BASIC("editor/movie_writer/movie_file", "");
|
||||
GLOBAL_DEF_BASIC("editor/movie_writer/disable_vsync", false);
|
||||
GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "editor/movie_writer/fps", PROPERTY_HINT_RANGE, "1,300,1,suffix:FPS"), 60);
|
||||
}
|
||||
|
||||
void MovieWriter::set_extensions_hint() {
|
||||
RBSet<String> found;
|
||||
for (uint32_t i = 0; i < writer_count; i++) {
|
||||
List<String> extensions;
|
||||
writers[i]->get_supported_extensions(&extensions);
|
||||
for (const String &ext : extensions) {
|
||||
found.insert(ext);
|
||||
}
|
||||
}
|
||||
|
||||
String ext_hint;
|
||||
|
||||
for (const String &S : found) {
|
||||
if (ext_hint != "") {
|
||||
ext_hint += ",";
|
||||
}
|
||||
ext_hint += "*." + S;
|
||||
}
|
||||
ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "editor/movie_writer/movie_file", PROPERTY_HINT_GLOBAL_SAVE_FILE, ext_hint));
|
||||
}
|
||||
|
||||
void MovieWriter::add_frame() {
|
||||
const int movie_time_seconds = Engine::get_singleton()->get_frames_drawn() / fps;
|
||||
const int frame_remainder = Engine::get_singleton()->get_frames_drawn() % fps;
|
||||
const String movie_time = vformat("%s:%s:%s:%s",
|
||||
String::num(movie_time_seconds / 3600, 0).pad_zeros(2),
|
||||
String::num((movie_time_seconds % 3600) / 60, 0).pad_zeros(2),
|
||||
String::num(movie_time_seconds % 60, 0).pad_zeros(2),
|
||||
String::num(frame_remainder, 0).pad_zeros(2));
|
||||
|
||||
Window *main_window = Window::get_from_id(DisplayServer::MAIN_WINDOW_ID);
|
||||
if (main_window) {
|
||||
main_window->set_title(vformat("MovieWriter: Frame %d (time: %s) - %s", Engine::get_singleton()->get_frames_drawn(), movie_time, project_name));
|
||||
}
|
||||
|
||||
RID main_vp_rid = RenderingServer::get_singleton()->viewport_find_from_screen_attachment(DisplayServer::MAIN_WINDOW_ID);
|
||||
RID main_vp_texture = RenderingServer::get_singleton()->viewport_get_texture(main_vp_rid);
|
||||
Ref<Image> vp_tex = RenderingServer::get_singleton()->texture_2d_get(main_vp_texture);
|
||||
if (RenderingServer::get_singleton()->viewport_is_using_hdr_2d(main_vp_rid)) {
|
||||
vp_tex->convert(Image::FORMAT_RGBA8);
|
||||
vp_tex->linear_to_srgb();
|
||||
}
|
||||
|
||||
RenderingServer::get_singleton()->viewport_set_measure_render_time(main_vp_rid, true);
|
||||
cpu_time += RenderingServer::get_singleton()->viewport_get_measured_render_time_cpu(main_vp_rid);
|
||||
cpu_time += RenderingServer::get_singleton()->get_frame_setup_time_cpu();
|
||||
gpu_time += RenderingServer::get_singleton()->viewport_get_measured_render_time_gpu(main_vp_rid);
|
||||
|
||||
AudioDriverDummy::get_dummy_singleton()->mix_audio(mix_rate / fps, audio_mix_buffer.ptr());
|
||||
|
||||
uint64_t encoding_start_usec = Time::get_singleton()->get_ticks_usec();
|
||||
write_frame(vp_tex, audio_mix_buffer.ptr());
|
||||
uint64_t encoding_end_usec = Time::get_singleton()->get_ticks_usec();
|
||||
encoding_time_usec += encoding_end_usec - encoding_start_usec;
|
||||
}
|
||||
|
||||
void MovieWriter::end() {
|
||||
uint64_t encoding_start_usec = Time::get_singleton()->get_ticks_usec();
|
||||
write_end();
|
||||
uint64_t encoding_end_usec = Time::get_singleton()->get_ticks_usec();
|
||||
encoding_time_usec += encoding_end_usec - encoding_start_usec;
|
||||
|
||||
// Print a report with various statistics.
|
||||
print_line("--------------------------------------------------------------------------------");
|
||||
String movie_path = Engine::get_singleton()->get_write_movie_path();
|
||||
if (movie_path.is_relative_path()) {
|
||||
// Print absolute path to make finding the file easier,
|
||||
// and to make it clickable in terminal emulators that support this.
|
||||
movie_path = ProjectSettings::get_singleton()->globalize_path("res://").path_join(movie_path);
|
||||
}
|
||||
print_line(vformat("Done recording movie at path: %s", movie_path));
|
||||
|
||||
const int movie_time_seconds = Engine::get_singleton()->get_frames_drawn() / fps;
|
||||
const int frame_remainder = Engine::get_singleton()->get_frames_drawn() % fps;
|
||||
const String movie_time = vformat("%s:%s:%s:%s",
|
||||
String::num(movie_time_seconds / 3600, 0).pad_zeros(2),
|
||||
String::num((movie_time_seconds % 3600) / 60, 0).pad_zeros(2),
|
||||
String::num(movie_time_seconds % 60, 0).pad_zeros(2),
|
||||
String::num(frame_remainder, 0).pad_zeros(2));
|
||||
|
||||
const int real_time_seconds = Time::get_singleton()->get_ticks_msec() / 1000;
|
||||
const String real_time = vformat("%s:%s:%s",
|
||||
String::num(real_time_seconds / 3600, 0).pad_zeros(2),
|
||||
String::num((real_time_seconds % 3600) / 60, 0).pad_zeros(2),
|
||||
String::num(real_time_seconds % 60, 0).pad_zeros(2));
|
||||
|
||||
print_line(vformat("%d frames at %d FPS (movie length: %s), recorded in %s (%d%% of real-time speed).", Engine::get_singleton()->get_frames_drawn(), fps, movie_time, real_time, (float(MAX(1, movie_time_seconds)) / MAX(1, real_time_seconds)) * 100));
|
||||
print_line(vformat("CPU render time: %.2f seconds (average: %.2f ms/frame)", cpu_time / 1000, cpu_time / Engine::get_singleton()->get_frames_drawn()));
|
||||
print_line(vformat("GPU render time: %.2f seconds (average: %.2f ms/frame)", gpu_time / 1000, gpu_time / Engine::get_singleton()->get_frames_drawn()));
|
||||
print_line(vformat("Encoding time: %.2f seconds (average: %.2f ms/frame)", encoding_time_usec / 1000000.f, encoding_time_usec / 1000.f / Engine::get_singleton()->get_frames_drawn()));
|
||||
print_line("--------------------------------------------------------------------------------");
|
||||
}
|
91
servers/movie_writer/movie_writer.h
Normal file
91
servers/movie_writer/movie_writer.h
Normal file
@@ -0,0 +1,91 @@
|
||||
/**************************************************************************/
|
||||
/* movie_writer.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/io/image.h"
|
||||
#include "core/templates/local_vector.h"
|
||||
#include "servers/audio_server.h"
|
||||
|
||||
class MovieWriter : public Object {
|
||||
GDCLASS(MovieWriter, Object);
|
||||
|
||||
uint64_t fps = 0;
|
||||
uint64_t mix_rate = 0;
|
||||
uint32_t audio_channels = 0;
|
||||
|
||||
float cpu_time = 0.0f;
|
||||
float gpu_time = 0.0f;
|
||||
uint64_t encoding_time_usec = 0;
|
||||
|
||||
String project_name;
|
||||
|
||||
LocalVector<int32_t> audio_mix_buffer;
|
||||
|
||||
enum {
|
||||
MAX_WRITERS = 8
|
||||
};
|
||||
static MovieWriter *writers[];
|
||||
static uint32_t writer_count;
|
||||
|
||||
protected:
|
||||
virtual uint32_t get_audio_mix_rate() const;
|
||||
virtual AudioServer::SpeakerMode get_audio_speaker_mode() const;
|
||||
|
||||
virtual Error write_begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path);
|
||||
virtual Error write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data);
|
||||
virtual void write_end();
|
||||
|
||||
GDVIRTUAL0RC_REQUIRED(uint32_t, _get_audio_mix_rate)
|
||||
GDVIRTUAL0RC_REQUIRED(AudioServer::SpeakerMode, _get_audio_speaker_mode)
|
||||
|
||||
GDVIRTUAL1RC_REQUIRED(bool, _handles_file, const String &)
|
||||
GDVIRTUAL0RC_REQUIRED(Vector<String>, _get_supported_extensions)
|
||||
|
||||
GDVIRTUAL3R_REQUIRED(Error, _write_begin, const Size2i &, uint32_t, const String &)
|
||||
GDVIRTUAL2R_REQUIRED(Error, _write_frame, const Ref<Image> &, GDExtensionConstPtr<int32_t>)
|
||||
GDVIRTUAL0_REQUIRED(_write_end)
|
||||
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
virtual bool handles_file(const String &p_path) const;
|
||||
virtual void get_supported_extensions(List<String> *r_extensions) const;
|
||||
|
||||
static void add_writer(MovieWriter *p_writer);
|
||||
static MovieWriter *find_writer_for_file(const String &p_file);
|
||||
|
||||
void begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path);
|
||||
void add_frame();
|
||||
|
||||
static void set_extensions_hint();
|
||||
|
||||
void end();
|
||||
};
|
170
servers/movie_writer/movie_writer_pngwav.cpp
Normal file
170
servers/movie_writer/movie_writer_pngwav.cpp
Normal file
@@ -0,0 +1,170 @@
|
||||
/**************************************************************************/
|
||||
/* movie_writer_pngwav.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "movie_writer_pngwav.h"
|
||||
#include "core/config/project_settings.h"
|
||||
#include "core/io/dir_access.h"
|
||||
|
||||
uint32_t MovieWriterPNGWAV::get_audio_mix_rate() const {
|
||||
return mix_rate;
|
||||
}
|
||||
AudioServer::SpeakerMode MovieWriterPNGWAV::get_audio_speaker_mode() const {
|
||||
return speaker_mode;
|
||||
}
|
||||
|
||||
void MovieWriterPNGWAV::get_supported_extensions(List<String> *r_extensions) const {
|
||||
r_extensions->push_back("png");
|
||||
}
|
||||
|
||||
bool MovieWriterPNGWAV::handles_file(const String &p_path) const {
|
||||
return p_path.get_extension().to_lower() == "png";
|
||||
}
|
||||
|
||||
String MovieWriterPNGWAV::zeros_str(uint32_t p_index) {
|
||||
char zeros[MAX_TRAILING_ZEROS + 1];
|
||||
for (uint32_t i = 0; i < MAX_TRAILING_ZEROS; i++) {
|
||||
uint32_t idx = MAX_TRAILING_ZEROS - i - 1;
|
||||
uint32_t digit = (p_index / uint32_t(Math::pow(double(10), double(idx)))) % 10;
|
||||
zeros[i] = '0' + digit;
|
||||
}
|
||||
zeros[MAX_TRAILING_ZEROS] = 0;
|
||||
return zeros;
|
||||
}
|
||||
|
||||
Error MovieWriterPNGWAV::write_begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) {
|
||||
// Quick & Dirty PNGWAV Code based on - https://docs.microsoft.com/en-us/windows/win32/directshow/avi-riff-file-reference
|
||||
|
||||
base_path = p_base_path.get_basename();
|
||||
if (base_path.is_relative_path()) {
|
||||
base_path = "res://" + base_path;
|
||||
}
|
||||
|
||||
{
|
||||
//Remove existing files before writing anew
|
||||
uint32_t idx = 0;
|
||||
Ref<DirAccess> d = DirAccess::open(base_path.get_base_dir());
|
||||
ERR_FAIL_COND_V(d.is_null(), FAILED);
|
||||
|
||||
String file = base_path.get_file();
|
||||
while (true) {
|
||||
String path = file + zeros_str(idx) + ".png";
|
||||
if (d->remove(path) != OK) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
f_wav = FileAccess::open(base_path + ".wav", FileAccess::WRITE_READ);
|
||||
ERR_FAIL_COND_V(f_wav.is_null(), ERR_CANT_OPEN);
|
||||
|
||||
fps = p_fps;
|
||||
|
||||
f_wav->store_buffer((const uint8_t *)"RIFF", 4);
|
||||
int total_size = 4 /* WAVE */ + 8 /* fmt+size */ + 16 /* format */ + 8 /* data+size */;
|
||||
f_wav->store_32(total_size); //will store final later
|
||||
f_wav->store_buffer((const uint8_t *)"WAVE", 4);
|
||||
|
||||
/* FORMAT CHUNK */
|
||||
|
||||
f_wav->store_buffer((const uint8_t *)"fmt ", 4);
|
||||
|
||||
uint32_t channels = 2;
|
||||
switch (speaker_mode) {
|
||||
case AudioServer::SPEAKER_MODE_STEREO:
|
||||
channels = 2;
|
||||
break;
|
||||
case AudioServer::SPEAKER_SURROUND_31:
|
||||
channels = 4;
|
||||
break;
|
||||
case AudioServer::SPEAKER_SURROUND_51:
|
||||
channels = 6;
|
||||
break;
|
||||
case AudioServer::SPEAKER_SURROUND_71:
|
||||
channels = 8;
|
||||
break;
|
||||
}
|
||||
|
||||
f_wav->store_32(16); //standard format, no extra fields
|
||||
f_wav->store_16(1); // compression code, standard PCM
|
||||
f_wav->store_16(channels); //CHANNELS: 2
|
||||
|
||||
f_wav->store_32(mix_rate);
|
||||
|
||||
/* useless stuff the format asks for */
|
||||
|
||||
int bits_per_sample = 32;
|
||||
int blockalign = bits_per_sample / 8 * channels;
|
||||
int bytes_per_sec = mix_rate * blockalign;
|
||||
|
||||
audio_block_size = (mix_rate / fps) * blockalign;
|
||||
|
||||
f_wav->store_32(bytes_per_sec);
|
||||
f_wav->store_16(blockalign); // block align (unused)
|
||||
f_wav->store_16(bits_per_sample);
|
||||
|
||||
/* DATA CHUNK */
|
||||
|
||||
f_wav->store_buffer((const uint8_t *)"data", 4);
|
||||
|
||||
f_wav->store_32(0); //data size... wooh
|
||||
wav_data_size_pos = f_wav->get_position();
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error MovieWriterPNGWAV::write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data) {
|
||||
ERR_FAIL_COND_V(f_wav.is_null(), ERR_UNCONFIGURED);
|
||||
|
||||
Vector<uint8_t> png_buffer = p_image->save_png_to_buffer();
|
||||
|
||||
Ref<FileAccess> fi = FileAccess::open(base_path + zeros_str(frame_count) + ".png", FileAccess::WRITE);
|
||||
fi->store_buffer(png_buffer.ptr(), png_buffer.size());
|
||||
f_wav->store_buffer((const uint8_t *)p_audio_data, audio_block_size);
|
||||
|
||||
frame_count++;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
void MovieWriterPNGWAV::write_end() {
|
||||
if (f_wav.is_valid()) {
|
||||
uint32_t total_size = 4 /* WAVE */ + 8 /* fmt+size */ + 16 /* format */ + 8 /* data+size */;
|
||||
uint32_t datasize = f_wav->get_position() - wav_data_size_pos;
|
||||
f_wav->seek(4);
|
||||
f_wav->store_32(total_size + datasize);
|
||||
f_wav->seek(0x28);
|
||||
f_wav->store_32(datasize);
|
||||
}
|
||||
}
|
||||
|
||||
MovieWriterPNGWAV::MovieWriterPNGWAV() {
|
||||
mix_rate = GLOBAL_GET("editor/movie_writer/mix_rate");
|
||||
speaker_mode = AudioServer::SpeakerMode(int(GLOBAL_GET("editor/movie_writer/speaker_mode")));
|
||||
}
|
68
servers/movie_writer/movie_writer_pngwav.h
Normal file
68
servers/movie_writer/movie_writer_pngwav.h
Normal file
@@ -0,0 +1,68 @@
|
||||
/**************************************************************************/
|
||||
/* movie_writer_pngwav.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "servers/movie_writer/movie_writer.h"
|
||||
|
||||
class MovieWriterPNGWAV : public MovieWriter {
|
||||
GDCLASS(MovieWriterPNGWAV, MovieWriter)
|
||||
|
||||
enum {
|
||||
MAX_TRAILING_ZEROS = 8 // more than 10 days at 60fps, no hard drive can put up with this anyway :)
|
||||
};
|
||||
|
||||
uint32_t mix_rate = 48000;
|
||||
AudioServer::SpeakerMode speaker_mode = AudioServer::SPEAKER_MODE_STEREO;
|
||||
String base_path;
|
||||
uint32_t frame_count = 0;
|
||||
uint32_t fps = 0;
|
||||
|
||||
uint32_t audio_block_size = 0;
|
||||
|
||||
Ref<FileAccess> f_wav;
|
||||
uint32_t wav_data_size_pos = 0;
|
||||
|
||||
String zeros_str(uint32_t p_index);
|
||||
|
||||
protected:
|
||||
virtual uint32_t get_audio_mix_rate() const override;
|
||||
virtual AudioServer::SpeakerMode get_audio_speaker_mode() const override;
|
||||
virtual void get_supported_extensions(List<String> *r_extensions) const override;
|
||||
|
||||
virtual Error write_begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) override;
|
||||
virtual Error write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data) override;
|
||||
virtual void write_end() override;
|
||||
|
||||
virtual bool handles_file(const String &p_path) const override;
|
||||
|
||||
public:
|
||||
MovieWriterPNGWAV();
|
||||
};
|
Reference in New Issue
Block a user