movie_writer_pngwav.cpp 6.1 KB

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