Path: blob/master/servers/movie_writer/movie_writer_pngwav.cpp
10277 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"3334uint32_t MovieWriterPNGWAV::get_audio_mix_rate() const {35return mix_rate;36}37AudioServer::SpeakerMode MovieWriterPNGWAV::get_audio_speaker_mode() const {38return speaker_mode;39}4041void MovieWriterPNGWAV::get_supported_extensions(List<String> *r_extensions) const {42r_extensions->push_back("png");43}4445bool MovieWriterPNGWAV::handles_file(const String &p_path) const {46return p_path.get_extension().to_lower() == "png";47}4849String MovieWriterPNGWAV::zeros_str(uint32_t p_index) {50char zeros[MAX_TRAILING_ZEROS + 1];51for (uint32_t i = 0; i < MAX_TRAILING_ZEROS; i++) {52uint32_t idx = MAX_TRAILING_ZEROS - i - 1;53uint32_t digit = (p_index / uint32_t(Math::pow(double(10), double(idx)))) % 10;54zeros[i] = '0' + digit;55}56zeros[MAX_TRAILING_ZEROS] = 0;57return zeros;58}5960Error MovieWriterPNGWAV::write_begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) {61// Quick & Dirty PNGWAV Code based on - https://docs.microsoft.com/en-us/windows/win32/directshow/avi-riff-file-reference6263base_path = p_base_path.get_basename();64if (base_path.is_relative_path()) {65base_path = "res://" + base_path;66}6768{69//Remove existing files before writing anew70uint32_t idx = 0;71Ref<DirAccess> d = DirAccess::open(base_path.get_base_dir());72ERR_FAIL_COND_V(d.is_null(), FAILED);7374String file = base_path.get_file();75while (true) {76String path = file + zeros_str(idx) + ".png";77if (d->remove(path) != OK) {78break;79}80}81}8283f_wav = FileAccess::open(base_path + ".wav", FileAccess::WRITE_READ);84ERR_FAIL_COND_V(f_wav.is_null(), ERR_CANT_OPEN);8586fps = p_fps;8788f_wav->store_buffer((const uint8_t *)"RIFF", 4);89int total_size = 4 /* WAVE */ + 8 /* fmt+size */ + 16 /* format */ + 8 /* data+size */;90f_wav->store_32(total_size); //will store final later91f_wav->store_buffer((const uint8_t *)"WAVE", 4);9293/* FORMAT CHUNK */9495f_wav->store_buffer((const uint8_t *)"fmt ", 4);9697uint32_t channels = 2;98switch (speaker_mode) {99case AudioServer::SPEAKER_MODE_STEREO:100channels = 2;101break;102case AudioServer::SPEAKER_SURROUND_31:103channels = 4;104break;105case AudioServer::SPEAKER_SURROUND_51:106channels = 6;107break;108case AudioServer::SPEAKER_SURROUND_71:109channels = 8;110break;111}112113f_wav->store_32(16); //standard format, no extra fields114f_wav->store_16(1); // compression code, standard PCM115f_wav->store_16(channels); //CHANNELS: 2116117f_wav->store_32(mix_rate);118119/* useless stuff the format asks for */120121int bits_per_sample = 32;122int blockalign = bits_per_sample / 8 * channels;123int bytes_per_sec = mix_rate * blockalign;124125audio_block_size = (mix_rate / fps) * blockalign;126127f_wav->store_32(bytes_per_sec);128f_wav->store_16(blockalign); // block align (unused)129f_wav->store_16(bits_per_sample);130131/* DATA CHUNK */132133f_wav->store_buffer((const uint8_t *)"data", 4);134135f_wav->store_32(0); //data size... wooh136wav_data_size_pos = f_wav->get_position();137138return OK;139}140141Error MovieWriterPNGWAV::write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data) {142ERR_FAIL_COND_V(f_wav.is_null(), ERR_UNCONFIGURED);143144Vector<uint8_t> png_buffer = p_image->save_png_to_buffer();145146Ref<FileAccess> fi = FileAccess::open(base_path + zeros_str(frame_count) + ".png", FileAccess::WRITE);147fi->store_buffer(png_buffer.ptr(), png_buffer.size());148f_wav->store_buffer((const uint8_t *)p_audio_data, audio_block_size);149150frame_count++;151152return OK;153}154155void MovieWriterPNGWAV::write_end() {156if (f_wav.is_valid()) {157uint32_t total_size = 4 /* WAVE */ + 8 /* fmt+size */ + 16 /* format */ + 8 /* data+size */;158uint32_t datasize = f_wav->get_position() - wav_data_size_pos;159f_wav->seek(4);160f_wav->store_32(total_size + datasize);161f_wav->seek(0x28);162f_wav->store_32(datasize);163}164}165166MovieWriterPNGWAV::MovieWriterPNGWAV() {167mix_rate = GLOBAL_GET("editor/movie_writer/mix_rate");168speaker_mode = AudioServer::SpeakerMode(int(GLOBAL_GET("editor/movie_writer/speaker_mode")));169}170171172