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