Debian Codegraph @

modules/jpg/movie_writer_mjpeg.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* movie_writer_mjpeg.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_mjpeg.h"
32 #include "core/config/project_settings.h"
33 #include "core/io/file_access.h"
34
35 uint32_t MovieWriterMJPEG::get_audio_mix_rate() const {
36 return mix_rate;
37 }
38 AudioServer::SpeakerMode MovieWriterMJPEG::get_audio_speaker_mode() const {
39 return speaker_mode;
40 }
41
42 bool MovieWriterMJPEG::handles_file(const String &p_path) const {
43 return p_path.has_extension("avi");
44 }
45
46 void MovieWriterMJPEG::get_supported_extensions(List<String> *r_extensions) const {
47 r_extensions->push_back("avi");
48 }
49
50 Error MovieWriterMJPEG::write_begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) {
51 // Quick & Dirty MJPEG Code based on - https://docs.microsoft.com/en-us/windows/win32/directshow/avi-riff-file-reference
52
53 base_path = p_base_path.get_basename();
54 if (base_path.is_relative_path()) {
55 base_path = "res://" + base_path;
56 }
57
58 base_path += ".avi";
59
60 f = FileAccess::open(base_path, FileAccess::WRITE_READ);
61
62 fps = p_fps;
63
64 ERR_FAIL_COND_V(f.is_null(), ERR_CANT_OPEN);
65
66 f->store_buffer((const uint8_t *)"RIFF", 4);
67 f->store_32(0); // Total length (update later)
68 f->store_buffer((const uint8_t *)"AVI ", 4);
69 f->store_buffer((const uint8_t *)"LIST", 4);
70 f->store_32(300); // 4 + 4 + 4 + 56 + 4 + 4 + 132 + 4 + 4 + 84
71 f->store_buffer((const uint8_t *)"hdrl", 4);
72 f->store_buffer((const uint8_t *)"avih", 4);
73 f->store_32(56);
74
75 f->store_32(1000000 / p_fps); // Microsecs per frame.
76 f->store_32(7000); // Max bytes per second
77 f->store_32(0); // Padding Granularity
78 f->store_32(16);
79 total_frames_ofs = f->get_position();
80 f->store_32(0); // Total frames (update later)
81 f->store_32(0); // Initial frames
82 f->store_32(1); // Streams
83 f->store_32(0); // Suggested buffer size
84 f->store_32(p_movie_size.width); // Movie Width
85 f->store_32(p_movie_size.height); // Movie Height
86 for (uint32_t i = 0; i < 4; i++) {
87 f->store_32(0); // Reserved.
88 }
89 f->store_buffer((const uint8_t *)"LIST", 4);
90 f->store_32(132); // 4 + 4 + 4 + 48 + 4 + 4 + 40 + 4 + 4 + 16
91 f->store_buffer((const uint8_t *)"strl", 4);
92 f->store_buffer((const uint8_t *)"strh", 4);
93 f->store_32(48);
94 f->store_buffer((const uint8_t *)"vids", 4);
95 f->store_buffer((const uint8_t *)"MJPG", 4);
96 f->store_32(0); // Flags
97 f->store_16(0); // Priority
98 f->store_16(0); // Language
99 f->store_32(0); // Initial Frames
100 f->store_32(1); // Scale
101 f->store_32(p_fps); // FPS
102 f->store_32(0); // Start
103 total_frames_ofs2 = f->get_position();
104 f->store_32(0); // Number of frames (to be updated later)
105 f->store_32(0); // Suggested Buffer Size
106 f->store_32(0); // Quality
107 f->store_32(0); // Sample Size
108
109 f->store_buffer((const uint8_t *)"strf", 4);
110 f->store_32(40); // Size.
111 f->store_32(40); // Size.
112
113 f->store_32(p_movie_size.width); // Width
114 f->store_32(p_movie_size.height); // Width
115 f->store_16(1); // Planes
116 f->store_16(24); // Bitcount
117 f->store_buffer((const uint8_t *)"MJPG", 4); // Compression
118
119 f->store_32(((p_movie_size.width * 24 / 8 + 3) & 0xFFFFFFFC) * p_movie_size.height); // SizeImage
120 f->store_32(0); // XPelsXMeter
121 f->store_32(0); // YPelsXMeter
122 f->store_32(0); // ClrUsed
123 f->store_32(0); // ClrImportant
124
125 f->store_buffer((const uint8_t *)"LIST", 4);
126 f->store_32(16);
127
128 f->store_buffer((const uint8_t *)"odml", 4);
129 f->store_buffer((const uint8_t *)"dmlh", 4);
130 f->store_32(4); // sizes
131
132 total_frames_ofs3 = f->get_position();
133 f->store_32(0); // Number of frames (to be updated later)
134
135 // Audio //
136
137 const uint32_t bit_depth = audio_bit_depth;
138 uint32_t channels = 2;
139 switch (speaker_mode) {
140 case AudioServer::SPEAKER_MODE_STEREO:
141 channels = 2;
142 break;
143 case AudioServer::SPEAKER_SURROUND_31:
144 channels = 4;
145 break;
146 case AudioServer::SPEAKER_SURROUND_51:
147 channels = 6;
148 break;
149 case AudioServer::SPEAKER_SURROUND_71:
150 channels = 8;
151 break;
152 }
153 uint32_t blockalign = bit_depth / 8 * channels;
154
155 f->store_buffer((const uint8_t *)"LIST", 4);
156 f->store_32(84); // 4 + 4 + 4 + 48 + 4 + 4 + 16
157 f->store_buffer((const uint8_t *)"strl", 4);
158 f->store_buffer((const uint8_t *)"strh", 4);
159 f->store_32(48);
160 f->store_buffer((const uint8_t *)"auds", 4);
161 f->store_32(0); // Handler
162 f->store_32(0); // Flags
163 f->store_16(0); // Priority
164 f->store_16(0); // Language
165 f->store_32(0); // Initial Frames
166 f->store_32(blockalign); // Scale
167 f->store_32(mix_rate * blockalign); // mix rate
168 f->store_32(0); // Start
169 total_audio_frames_ofs4 = f->get_position();
170 f->store_32(0); // Number of frames (to be updated later)
171 f->store_32(12288); // Suggested Buffer Size
172 f->store_32(0xFFFFFFFF); // Quality
173 f->store_32(blockalign); // Block Align to 32 bits
174
175 audio_block_size = (mix_rate / fps) * blockalign;
176
177 f->store_buffer((const uint8_t *)"strf", 4);
178 f->store_32(16); // Standard format, no extra fields
179 f->store_16(1); // Compression code, standard PCM
180 f->store_16(channels);
181 f->store_32(mix_rate); // Samples (frames) / Sec
182 f->store_32(mix_rate * blockalign); // Bytes / sec
183 f->store_16(blockalign); // Bytes / sec
184 f->store_16(bit_depth); // Bytes / sec
185
186 f->store_buffer((const uint8_t *)"LIST", 4);
187 movi_data_ofs = f->get_position();
188 f->store_32(0); // Number of frames (to be updated later)
189 f->store_buffer((const uint8_t *)"movi", 4);
190
191 return OK;
192 }
193
194 Error MovieWriterMJPEG::write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data) {
195 ERR_FAIL_COND_V(f.is_null(), ERR_UNCONFIGURED);
196
197 Vector<uint8_t> jpg_buffer = p_image->save_jpg_to_buffer(quality);
198 uint32_t s = jpg_buffer.size();
199
200 f->store_buffer((const uint8_t *)"00db", 4); // Stream 0, Video
201 f->store_32(jpg_buffer.size()); // sizes
202 f->store_buffer(jpg_buffer.ptr(), jpg_buffer.size());
203 if (jpg_buffer.size() & 1) {
204 f->store_8(0);
205 s++;
206 }
207 jpg_frame_sizes.push_back(s);
208
209 f->store_buffer((const uint8_t *)"01wb", 4); // Stream 1, Audio.
210 f->store_32(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 }
223
224 frame_count++;
225
226 return OK;
227 }
228
229 void MovieWriterMJPEG::write_end() {
230 if (f.is_valid()) {
231 // Finalize the file (frame indices)
232 f->store_buffer((const uint8_t *)"idx1", 4);
233 f->store_32(8 * 4 * frame_count);
234 uint32_t ofs = 4;
235 uint32_t all_data_size = 0;
236 for (uint32_t i = 0; i < frame_count; i++) {
237 f->store_buffer((const uint8_t *)"00db", 4);
238 f->store_32(16); // AVI_KEYFRAME
239 f->store_32(ofs);
240 f->store_32(jpg_frame_sizes[i]);
241
242 ofs += jpg_frame_sizes[i] + 8;
243
244 f->store_buffer((const uint8_t *)"01wb", 4);
245 f->store_32(16); // AVI_KEYFRAME
246 f->store_32(ofs);
247 f->store_32(audio_block_size);
248
249 ofs += audio_block_size + 8;
250 all_data_size += jpg_frame_sizes[i] + audio_block_size;
251 }
252
253 uint32_t file_size = f->get_position();
254 f->seek(4);
255 f->store_32(file_size - 78);
256 f->seek(total_frames_ofs);
257 f->store_32(frame_count);
258 f->seek(total_frames_ofs2);
259 f->store_32(frame_count);
260 f->seek(total_frames_ofs3);
261 f->store_32(frame_count);
262 f->seek(total_audio_frames_ofs4);
263 f->store_32(frame_count * mix_rate / fps);
264 f->seek(movi_data_ofs);
265 f->store_32(all_data_size + 4 + 16 * frame_count);
266
267 f.unref();
268 }
269 }
270
271 MovieWriterMJPEG::MovieWriterMJPEG() {
272 mix_rate = GLOBAL_GET("editor/movie_writer/mix_rate");
273 speaker_mode = AudioServer::SpeakerMode(int(GLOBAL_GET("editor/movie_writer/speaker_mode")));
274 quality = GLOBAL_GET("editor/movie_writer/video_quality");
275 audio_bit_depth = GLOBAL_GET("editor/movie_writer/audio_bit_depth");
276 }