Path: blob/master/servers/movie_writer/movie_writer_pngwav.cpp
11352 views
/**************************************************************************/1/* movie_writer_pngwav.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "movie_writer_pngwav.h"31#include "core/config/project_settings.h"32#include "core/io/dir_access.h"33#include "core/io/file_access.h"3435uint32_t MovieWriterPNGWAV::get_audio_mix_rate() const {36return mix_rate;37}38AudioServer::SpeakerMode MovieWriterPNGWAV::get_audio_speaker_mode() const {39return speaker_mode;40}4142void MovieWriterPNGWAV::get_supported_extensions(List<String> *r_extensions) const {43r_extensions->push_back("png");44}4546bool MovieWriterPNGWAV::handles_file(const String &p_path) const {47return p_path.get_extension().to_lower() == "png";48}4950String MovieWriterPNGWAV::zeros_str(uint32_t p_index) {51char zeros[MAX_TRAILING_ZEROS + 1];52for (uint32_t i = 0; i < MAX_TRAILING_ZEROS; i++) {53uint32_t idx = MAX_TRAILING_ZEROS - i - 1;54uint32_t digit = (p_index / uint32_t(Math::pow(double(10), double(idx)))) % 10;55zeros[i] = '0' + digit;56}57zeros[MAX_TRAILING_ZEROS] = 0;58return zeros;59}6061Error 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-reference6364base_path = p_base_path.get_basename();65if (base_path.is_relative_path()) {66base_path = "res://" + base_path;67}6869{70//Remove existing files before writing anew71uint32_t idx = 0;72Ref<DirAccess> d = DirAccess::open(base_path.get_base_dir());73ERR_FAIL_COND_V(d.is_null(), FAILED);7475String file = base_path.get_file();76while (true) {77String path = file + zeros_str(idx) + ".png";78if (d->remove(path) != OK) {79break;80}81}82}8384f_wav = FileAccess::open(base_path + ".wav", FileAccess::WRITE_READ);85ERR_FAIL_COND_V(f_wav.is_null(), ERR_CANT_OPEN);8687fps = p_fps;8889f_wav->store_buffer((const uint8_t *)"RIFF", 4);90int total_size = 4 /* WAVE */ + 8 /* fmt+size */ + 16 /* format */ + 8 /* data+size */;91f_wav->store_32(total_size); //will store final later92f_wav->store_buffer((const uint8_t *)"WAVE", 4);9394/* FORMAT CHUNK */9596f_wav->store_buffer((const uint8_t *)"fmt ", 4);9798uint32_t channels = 2;99switch (speaker_mode) {100case AudioServer::SPEAKER_MODE_STEREO:101channels = 2;102break;103case AudioServer::SPEAKER_SURROUND_31:104channels = 4;105break;106case AudioServer::SPEAKER_SURROUND_51:107channels = 6;108break;109case AudioServer::SPEAKER_SURROUND_71:110channels = 8;111break;112}113114f_wav->store_32(16); //standard format, no extra fields115f_wav->store_16(1); // compression code, standard PCM116f_wav->store_16(channels); //CHANNELS: 2117118f_wav->store_32(mix_rate);119120/* useless stuff the format asks for */121122int bits_per_sample = 32;123int blockalign = bits_per_sample / 8 * channels;124int bytes_per_sec = mix_rate * blockalign;125126audio_block_size = (mix_rate / fps) * blockalign;127128f_wav->store_32(bytes_per_sec);129f_wav->store_16(blockalign); // block align (unused)130f_wav->store_16(bits_per_sample);131132/* DATA CHUNK */133134f_wav->store_buffer((const uint8_t *)"data", 4);135136f_wav->store_32(0); //data size... wooh137wav_data_size_pos = f_wav->get_position();138139return OK;140}141142Error MovieWriterPNGWAV::write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data) {143ERR_FAIL_COND_V(f_wav.is_null(), ERR_UNCONFIGURED);144145Vector<uint8_t> png_buffer = p_image->save_png_to_buffer();146147Ref<FileAccess> fi = FileAccess::open(base_path + zeros_str(frame_count) + ".png", FileAccess::WRITE);148fi->store_buffer(png_buffer.ptr(), png_buffer.size());149f_wav->store_buffer((const uint8_t *)p_audio_data, audio_block_size);150151frame_count++;152153return OK;154}155156void MovieWriterPNGWAV::write_end() {157if (f_wav.is_valid()) {158uint32_t total_size = 4 /* WAVE */ + 8 /* fmt+size */ + 16 /* format */ + 8 /* data+size */;159uint32_t datasize = f_wav->get_position() - wav_data_size_pos;160f_wav->seek(4);161f_wav->store_32(total_size + datasize);162f_wav->seek(0x28);163f_wav->store_32(datasize);164}165}166167MovieWriterPNGWAV::MovieWriterPNGWAV() {168mix_rate = GLOBAL_GET("editor/movie_writer/mix_rate");169speaker_mode = AudioServer::SpeakerMode(int(GLOBAL_GET("editor/movie_writer/speaker_mode")));170}171172173