resource_importer_ogg_vorbis.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**************************************************************************/
  2. /* resource_importer_ogg_vorbis.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 "resource_importer_ogg_vorbis.h"
  31. #include "core/io/file_access.h"
  32. #include "core/io/resource_saver.h"
  33. #include "scene/resources/texture.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "editor/import/audio_stream_import_settings.h"
  36. #endif
  37. #include <ogg/ogg.h>
  38. #include <vorbis/codec.h>
  39. String ResourceImporterOggVorbis::get_importer_name() const {
  40. return "oggvorbisstr";
  41. }
  42. String ResourceImporterOggVorbis::get_visible_name() const {
  43. return "oggvorbisstr";
  44. }
  45. void ResourceImporterOggVorbis::get_recognized_extensions(List<String> *p_extensions) const {
  46. p_extensions->push_back("ogg");
  47. }
  48. String ResourceImporterOggVorbis::get_save_extension() const {
  49. return "oggvorbisstr";
  50. }
  51. String ResourceImporterOggVorbis::get_resource_type() const {
  52. return "AudioStreamOggVorbis";
  53. }
  54. bool ResourceImporterOggVorbis::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  55. return true;
  56. }
  57. int ResourceImporterOggVorbis::get_preset_count() const {
  58. return 0;
  59. }
  60. String ResourceImporterOggVorbis::get_preset_name(int p_idx) const {
  61. return String();
  62. }
  63. void ResourceImporterOggVorbis::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
  64. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "loop"), false));
  65. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "loop_offset"), 0));
  66. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), 0));
  67. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,or_greater"), 0));
  68. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,or_greater"), 4));
  69. }
  70. #ifdef TOOLS_ENABLED
  71. bool ResourceImporterOggVorbis::has_advanced_options() const {
  72. return true;
  73. }
  74. void ResourceImporterOggVorbis::show_advanced_options(const String &p_path) {
  75. Ref<AudioStreamOggVorbis> ogg_stream = load_from_file(p_path);
  76. if (ogg_stream.is_valid()) {
  77. AudioStreamImportSettingsDialog::get_singleton()->edit(p_path, "oggvorbisstr", ogg_stream);
  78. }
  79. }
  80. #endif
  81. Error ResourceImporterOggVorbis::import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
  82. bool loop = p_options["loop"];
  83. double loop_offset = p_options["loop_offset"];
  84. double bpm = p_options["bpm"];
  85. int beat_count = p_options["beat_count"];
  86. int bar_beats = p_options["bar_beats"];
  87. Ref<AudioStreamOggVorbis> ogg_vorbis_stream = load_from_file(p_source_file);
  88. if (ogg_vorbis_stream.is_null()) {
  89. return ERR_CANT_OPEN;
  90. }
  91. ogg_vorbis_stream->set_loop(loop);
  92. ogg_vorbis_stream->set_loop_offset(loop_offset);
  93. ogg_vorbis_stream->set_bpm(bpm);
  94. ogg_vorbis_stream->set_beat_count(beat_count);
  95. ogg_vorbis_stream->set_bar_beats(bar_beats);
  96. return ResourceSaver::save(ogg_vorbis_stream, p_save_path + ".oggvorbisstr");
  97. }
  98. ResourceImporterOggVorbis::ResourceImporterOggVorbis() {
  99. }
  100. void ResourceImporterOggVorbis::_bind_methods() {
  101. ClassDB::bind_static_method("ResourceImporterOggVorbis", D_METHOD("load_from_buffer", "buffer"), &ResourceImporterOggVorbis::load_from_buffer);
  102. ClassDB::bind_static_method("ResourceImporterOggVorbis", D_METHOD("load_from_file", "path"), &ResourceImporterOggVorbis::load_from_file);
  103. }
  104. Ref<AudioStreamOggVorbis> ResourceImporterOggVorbis::load_from_buffer(const Vector<uint8_t> &file_data) {
  105. Ref<AudioStreamOggVorbis> ogg_vorbis_stream;
  106. ogg_vorbis_stream.instantiate();
  107. Ref<OggPacketSequence> ogg_packet_sequence;
  108. ogg_packet_sequence.instantiate();
  109. ogg_stream_state stream_state;
  110. ogg_sync_state sync_state;
  111. ogg_page page;
  112. ogg_packet packet;
  113. bool initialized_stream = false;
  114. ogg_sync_init(&sync_state);
  115. int err;
  116. size_t cursor = 0;
  117. size_t packet_count = 0;
  118. bool done = false;
  119. while (!done) {
  120. err = ogg_sync_check(&sync_state);
  121. ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err));
  122. while (ogg_sync_pageout(&sync_state, &page) != 1) {
  123. if (cursor >= size_t(file_data.size())) {
  124. done = true;
  125. break;
  126. }
  127. err = ogg_sync_check(&sync_state);
  128. ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err));
  129. char *sync_buf = ogg_sync_buffer(&sync_state, OGG_SYNC_BUFFER_SIZE);
  130. err = ogg_sync_check(&sync_state);
  131. ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err));
  132. ERR_FAIL_COND_V(cursor > size_t(file_data.size()), Ref<AudioStreamOggVorbis>());
  133. size_t copy_size = file_data.size() - cursor;
  134. if (copy_size > OGG_SYNC_BUFFER_SIZE) {
  135. copy_size = OGG_SYNC_BUFFER_SIZE;
  136. }
  137. memcpy(sync_buf, &file_data[cursor], copy_size);
  138. ogg_sync_wrote(&sync_state, copy_size);
  139. cursor += copy_size;
  140. err = ogg_sync_check(&sync_state);
  141. ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err));
  142. }
  143. if (done) {
  144. break;
  145. }
  146. err = ogg_sync_check(&sync_state);
  147. ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err));
  148. // Have a page now.
  149. if (!initialized_stream) {
  150. if (ogg_stream_init(&stream_state, ogg_page_serialno(&page))) {
  151. ERR_FAIL_V_MSG(Ref<AudioStreamOggVorbis>(), "Failed allocating memory for Ogg Vorbis stream.");
  152. }
  153. initialized_stream = true;
  154. }
  155. ogg_stream_pagein(&stream_state, &page);
  156. err = ogg_stream_check(&stream_state);
  157. ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg stream error " + itos(err));
  158. int desync_iters = 0;
  159. RBMap<uint64_t, Vector<Vector<uint8_t>>> sorted_packets;
  160. int64_t granule_pos = 0;
  161. while (true) {
  162. err = ogg_stream_packetout(&stream_state, &packet);
  163. if (err == -1) {
  164. // According to the docs this is usually recoverable, but don't sit here spinning forever.
  165. desync_iters++;
  166. WARN_PRINT_ONCE("Desync during ogg import.");
  167. ERR_FAIL_COND_V_MSG(desync_iters > 100, Ref<AudioStreamOggVorbis>(), "Packet sync issue during Ogg import");
  168. continue;
  169. } else if (err == 0) {
  170. // Not enough data to fully reconstruct a packet. Go on to the next page.
  171. break;
  172. }
  173. if (packet_count == 0 && vorbis_synthesis_idheader(&packet) == 0) {
  174. print_verbose("Found a non-vorbis-header packet in a header position");
  175. // Clearly this logical stream is not a vorbis stream, so destroy it and try again with the next page.
  176. if (initialized_stream) {
  177. ogg_stream_clear(&stream_state);
  178. initialized_stream = false;
  179. }
  180. break;
  181. }
  182. if (packet.granulepos > granule_pos) {
  183. granule_pos = packet.granulepos;
  184. }
  185. if (packet.bytes > 0) {
  186. PackedByteArray data;
  187. data.resize(packet.bytes);
  188. memcpy(data.ptrw(), packet.packet, packet.bytes);
  189. sorted_packets[granule_pos].push_back(data);
  190. packet_count++;
  191. }
  192. }
  193. Vector<Vector<uint8_t>> packet_data;
  194. for (const KeyValue<uint64_t, Vector<Vector<uint8_t>>> &pair : sorted_packets) {
  195. for (const Vector<uint8_t> &packets : pair.value) {
  196. packet_data.push_back(packets);
  197. }
  198. }
  199. if (initialized_stream && packet_data.size() > 0) {
  200. ogg_packet_sequence->push_page(ogg_page_granulepos(&page), packet_data);
  201. }
  202. }
  203. if (initialized_stream) {
  204. ogg_stream_clear(&stream_state);
  205. }
  206. ogg_sync_clear(&sync_state);
  207. if (ogg_packet_sequence->get_packet_granule_positions().is_empty()) {
  208. ERR_FAIL_V_MSG(Ref<AudioStreamOggVorbis>(), "Ogg Vorbis decoding failed. Check that your data is a valid Ogg Vorbis audio stream.");
  209. }
  210. ogg_vorbis_stream->set_packet_sequence(ogg_packet_sequence);
  211. return ogg_vorbis_stream;
  212. }
  213. Ref<AudioStreamOggVorbis> ResourceImporterOggVorbis::load_from_file(const String &p_path) {
  214. Vector<uint8_t> file_data = FileAccess::get_file_as_bytes(p_path);
  215. ERR_FAIL_COND_V_MSG(file_data.is_empty(), Ref<AudioStreamOggVorbis>(), "Cannot open file '" + p_path + "'.");
  216. return load_from_buffer(file_data);
  217. }