Path: blob/master/modules/jpg/movie_writer_mjpeg.cpp
10277 views
/**************************************************************************/1/* movie_writer_mjpeg.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_mjpeg.h"31#include "core/config/project_settings.h"3233uint32_t MovieWriterMJPEG::get_audio_mix_rate() const {34return mix_rate;35}36AudioServer::SpeakerMode MovieWriterMJPEG::get_audio_speaker_mode() const {37return speaker_mode;38}3940bool MovieWriterMJPEG::handles_file(const String &p_path) const {41return p_path.get_extension().to_lower() == "avi";42}4344void MovieWriterMJPEG::get_supported_extensions(List<String> *r_extensions) const {45r_extensions->push_back("avi");46}4748Error MovieWriterMJPEG::write_begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) {49// Quick & Dirty MJPEG Code based on - https://docs.microsoft.com/en-us/windows/win32/directshow/avi-riff-file-reference5051base_path = p_base_path.get_basename();52if (base_path.is_relative_path()) {53base_path = "res://" + base_path;54}5556base_path += ".avi";5758f = FileAccess::open(base_path, FileAccess::WRITE_READ);5960fps = p_fps;6162ERR_FAIL_COND_V(f.is_null(), ERR_CANT_OPEN);6364f->store_buffer((const uint8_t *)"RIFF", 4);65f->store_32(0); // Total length (update later)66f->store_buffer((const uint8_t *)"AVI ", 4);67f->store_buffer((const uint8_t *)"LIST", 4);68f->store_32(300); // 4 + 4 + 4 + 56 + 4 + 4 + 132 + 4 + 4 + 8469f->store_buffer((const uint8_t *)"hdrl", 4);70f->store_buffer((const uint8_t *)"avih", 4);71f->store_32(56);7273f->store_32(1000000 / p_fps); // Microsecs per frame.74f->store_32(7000); // Max bytes per second75f->store_32(0); // Padding Granularity76f->store_32(16);77total_frames_ofs = f->get_position();78f->store_32(0); // Total frames (update later)79f->store_32(0); // Initial frames80f->store_32(1); // Streams81f->store_32(0); // Suggested buffer size82f->store_32(p_movie_size.width); // Movie Width83f->store_32(p_movie_size.height); // Movie Height84for (uint32_t i = 0; i < 4; i++) {85f->store_32(0); // Reserved.86}87f->store_buffer((const uint8_t *)"LIST", 4);88f->store_32(132); // 4 + 4 + 4 + 48 + 4 + 4 + 40 + 4 + 4 + 1689f->store_buffer((const uint8_t *)"strl", 4);90f->store_buffer((const uint8_t *)"strh", 4);91f->store_32(48);92f->store_buffer((const uint8_t *)"vids", 4);93f->store_buffer((const uint8_t *)"MJPG", 4);94f->store_32(0); // Flags95f->store_16(0); // Priority96f->store_16(0); // Language97f->store_32(0); // Initial Frames98f->store_32(1); // Scale99f->store_32(p_fps); // FPS100f->store_32(0); // Start101total_frames_ofs2 = f->get_position();102f->store_32(0); // Number of frames (to be updated later)103f->store_32(0); // Suggested Buffer Size104f->store_32(0); // Quality105f->store_32(0); // Sample Size106107f->store_buffer((const uint8_t *)"strf", 4);108f->store_32(40); // Size.109f->store_32(40); // Size.110111f->store_32(p_movie_size.width); // Width112f->store_32(p_movie_size.height); // Width113f->store_16(1); // Planes114f->store_16(24); // Bitcount115f->store_buffer((const uint8_t *)"MJPG", 4); // Compression116117f->store_32(((p_movie_size.width * 24 / 8 + 3) & 0xFFFFFFFC) * p_movie_size.height); // SizeImage118f->store_32(0); // XPelsXMeter119f->store_32(0); // YPelsXMeter120f->store_32(0); // ClrUsed121f->store_32(0); // ClrImportant122123f->store_buffer((const uint8_t *)"LIST", 4);124f->store_32(16);125126f->store_buffer((const uint8_t *)"odml", 4);127f->store_buffer((const uint8_t *)"dmlh", 4);128f->store_32(4); // sizes129130total_frames_ofs3 = f->get_position();131f->store_32(0); // Number of frames (to be updated later)132133// Audio //134135const uint32_t bit_depth = 32;136uint32_t channels = 2;137switch (speaker_mode) {138case AudioServer::SPEAKER_MODE_STEREO:139channels = 2;140break;141case AudioServer::SPEAKER_SURROUND_31:142channels = 4;143break;144case AudioServer::SPEAKER_SURROUND_51:145channels = 6;146break;147case AudioServer::SPEAKER_SURROUND_71:148channels = 8;149break;150}151uint32_t blockalign = bit_depth / 8 * channels;152153f->store_buffer((const uint8_t *)"LIST", 4);154f->store_32(84); // 4 + 4 + 4 + 48 + 4 + 4 + 16155f->store_buffer((const uint8_t *)"strl", 4);156f->store_buffer((const uint8_t *)"strh", 4);157f->store_32(48);158f->store_buffer((const uint8_t *)"auds", 4);159f->store_32(0); // Handler160f->store_32(0); // Flags161f->store_16(0); // Priority162f->store_16(0); // Language163f->store_32(0); // Initial Frames164f->store_32(blockalign); // Scale165f->store_32(mix_rate * blockalign); // mix rate166f->store_32(0); // Start167total_audio_frames_ofs4 = f->get_position();168f->store_32(0); // Number of frames (to be updated later)169f->store_32(12288); // Suggested Buffer Size170f->store_32(0xFFFFFFFF); // Quality171f->store_32(blockalign); // Block Align to 32 bits172173audio_block_size = (mix_rate / fps) * blockalign;174175f->store_buffer((const uint8_t *)"strf", 4);176f->store_32(16); // Standard format, no extra fields177f->store_16(1); // Compression code, standard PCM178f->store_16(channels);179f->store_32(mix_rate); // Samples (frames) / Sec180f->store_32(mix_rate * blockalign); // Bytes / sec181f->store_16(blockalign); // Bytes / sec182f->store_16(bit_depth); // Bytes / sec183184f->store_buffer((const uint8_t *)"LIST", 4);185movi_data_ofs = f->get_position();186f->store_32(0); // Number of frames (to be updated later)187f->store_buffer((const uint8_t *)"movi", 4);188189return OK;190}191192Error MovieWriterMJPEG::write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data) {193ERR_FAIL_COND_V(f.is_null(), ERR_UNCONFIGURED);194195Vector<uint8_t> jpg_buffer = p_image->save_jpg_to_buffer(quality);196uint32_t s = jpg_buffer.size();197198f->store_buffer((const uint8_t *)"00db", 4); // Stream 0, Video199f->store_32(jpg_buffer.size()); // sizes200f->store_buffer(jpg_buffer.ptr(), jpg_buffer.size());201if (jpg_buffer.size() & 1) {202f->store_8(0);203s++;204}205jpg_frame_sizes.push_back(s);206207f->store_buffer((const uint8_t *)"01wb", 4); // Stream 1, Audio.208f->store_32(audio_block_size);209f->store_buffer((const uint8_t *)p_audio_data, audio_block_size);210211frame_count++;212213return OK;214}215216void MovieWriterMJPEG::write_end() {217if (f.is_valid()) {218// Finalize the file (frame indices)219f->store_buffer((const uint8_t *)"idx1", 4);220f->store_32(8 * 4 * frame_count);221uint32_t ofs = 4;222uint32_t all_data_size = 0;223for (uint32_t i = 0; i < frame_count; i++) {224f->store_buffer((const uint8_t *)"00db", 4);225f->store_32(16); // AVI_KEYFRAME226f->store_32(ofs);227f->store_32(jpg_frame_sizes[i]);228229ofs += jpg_frame_sizes[i] + 8;230231f->store_buffer((const uint8_t *)"01wb", 4);232f->store_32(16); // AVI_KEYFRAME233f->store_32(ofs);234f->store_32(audio_block_size);235236ofs += audio_block_size + 8;237all_data_size += jpg_frame_sizes[i] + audio_block_size;238}239240uint32_t file_size = f->get_position();241f->seek(4);242f->store_32(file_size - 78);243f->seek(total_frames_ofs);244f->store_32(frame_count);245f->seek(total_frames_ofs2);246f->store_32(frame_count);247f->seek(total_frames_ofs3);248f->store_32(frame_count);249f->seek(total_audio_frames_ofs4);250f->store_32(frame_count * mix_rate / fps);251f->seek(movi_data_ofs);252f->store_32(all_data_size + 4 + 16 * frame_count);253254f.unref();255}256}257258MovieWriterMJPEG::MovieWriterMJPEG() {259mix_rate = GLOBAL_GET("editor/movie_writer/mix_rate");260speaker_mode = AudioServer::SpeakerMode(int(GLOBAL_GET("editor/movie_writer/speaker_mode")));261quality = GLOBAL_GET("editor/movie_writer/video_quality");262}263264265