ogg_packet_sequence.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**************************************************************************/
  2. /* ogg_packet_sequence.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 "ogg_packet_sequence.h"
  31. #include "core/variant/typed_array.h"
  32. void OggPacketSequence::push_page(int64_t p_granule_pos, const Vector<PackedByteArray> &p_data) {
  33. Vector<PackedByteArray> data_stored;
  34. for (int i = 0; i < p_data.size(); i++) {
  35. data_stored.push_back(p_data[i]);
  36. }
  37. page_granule_positions.push_back(p_granule_pos);
  38. page_data.push_back(data_stored);
  39. data_version++;
  40. }
  41. void OggPacketSequence::set_packet_data(const TypedArray<Array> &p_data) {
  42. data_version++; // Update the data version so old playbacks know that they can't rely on us anymore.
  43. page_data.clear();
  44. for (int page_idx = 0; page_idx < p_data.size(); page_idx++) {
  45. // Push a new page. We cleared the vector so this will be at index `page_idx`.
  46. page_data.push_back(Vector<PackedByteArray>());
  47. TypedArray<PackedByteArray> this_page_data = p_data[page_idx];
  48. for (int packet = 0; packet < this_page_data.size(); packet++) {
  49. page_data.write[page_idx].push_back(this_page_data[packet]);
  50. }
  51. }
  52. }
  53. TypedArray<Array> OggPacketSequence::get_packet_data() const {
  54. TypedArray<Array> ret;
  55. for (const Vector<PackedByteArray> &page : page_data) {
  56. Array page_variant;
  57. for (const PackedByteArray &packet : page) {
  58. page_variant.push_back(packet);
  59. }
  60. ret.push_back(page_variant);
  61. }
  62. return ret;
  63. }
  64. void OggPacketSequence::set_packet_granule_positions(const PackedInt64Array &p_granule_positions) {
  65. data_version++; // Update the data version so old playbacks know that they can't rely on us anymore.
  66. page_granule_positions.clear();
  67. for (int page_idx = 0; page_idx < p_granule_positions.size(); page_idx++) {
  68. int64_t granule_pos = p_granule_positions[page_idx];
  69. page_granule_positions.push_back(granule_pos);
  70. }
  71. }
  72. PackedInt64Array OggPacketSequence::get_packet_granule_positions() const {
  73. PackedInt64Array ret;
  74. for (int64_t granule_pos : page_granule_positions) {
  75. ret.push_back(granule_pos);
  76. }
  77. return ret;
  78. }
  79. void OggPacketSequence::set_sampling_rate(float p_sampling_rate) {
  80. sampling_rate = p_sampling_rate;
  81. }
  82. float OggPacketSequence::get_sampling_rate() const {
  83. return sampling_rate;
  84. }
  85. int64_t OggPacketSequence::get_final_granule_pos() const {
  86. if (!page_granule_positions.is_empty()) {
  87. return page_granule_positions[page_granule_positions.size() - 1];
  88. }
  89. return -1;
  90. }
  91. float OggPacketSequence::get_length() const {
  92. int64_t granule_pos = get_final_granule_pos();
  93. if (granule_pos < 0) {
  94. return 0;
  95. }
  96. return granule_pos / sampling_rate;
  97. }
  98. Ref<OggPacketSequencePlayback> OggPacketSequence::instantiate_playback() {
  99. Ref<OggPacketSequencePlayback> playback;
  100. playback.instantiate();
  101. playback->ogg_packet_sequence = Ref<OggPacketSequence>(this);
  102. playback->data_version = data_version;
  103. return playback;
  104. }
  105. void OggPacketSequence::_bind_methods() {
  106. ClassDB::bind_method(D_METHOD("set_packet_data", "packet_data"), &OggPacketSequence::set_packet_data);
  107. ClassDB::bind_method(D_METHOD("get_packet_data"), &OggPacketSequence::get_packet_data);
  108. ClassDB::bind_method(D_METHOD("set_packet_granule_positions", "granule_positions"), &OggPacketSequence::set_packet_granule_positions);
  109. ClassDB::bind_method(D_METHOD("get_packet_granule_positions"), &OggPacketSequence::get_packet_granule_positions);
  110. ClassDB::bind_method(D_METHOD("set_sampling_rate", "sampling_rate"), &OggPacketSequence::set_sampling_rate);
  111. ClassDB::bind_method(D_METHOD("get_sampling_rate"), &OggPacketSequence::get_sampling_rate);
  112. ClassDB::bind_method(D_METHOD("get_length"), &OggPacketSequence::get_length);
  113. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "packet_data", PROPERTY_HINT_ARRAY_TYPE, "PackedByteArray", PROPERTY_USAGE_NO_EDITOR), "set_packet_data", "get_packet_data");
  114. ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT64_ARRAY, "granule_positions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_packet_granule_positions", "get_packet_granule_positions");
  115. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sampling_rate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_sampling_rate", "get_sampling_rate");
  116. }
  117. bool OggPacketSequencePlayback::next_ogg_packet(ogg_packet **p_packet) const {
  118. ERR_FAIL_COND_V(data_version != ogg_packet_sequence->data_version, false);
  119. ERR_FAIL_COND_V(ogg_packet_sequence->page_data.is_empty(), false);
  120. ERR_FAIL_COND_V(ogg_packet_sequence->page_granule_positions.is_empty(), false);
  121. ERR_FAIL_COND_V(page_cursor >= ogg_packet_sequence->page_data.size(), false);
  122. // Move on to the next page if need be. This happens first to help simplify seek logic.
  123. while (packet_cursor >= ogg_packet_sequence->page_data[page_cursor].size()) {
  124. packet_cursor = 0;
  125. page_cursor++;
  126. if (page_cursor >= ogg_packet_sequence->page_data.size()) {
  127. return false;
  128. }
  129. }
  130. ERR_FAIL_COND_V(page_cursor >= ogg_packet_sequence->page_data.size(), false);
  131. packet->b_o_s = page_cursor == 0 && packet_cursor == 0;
  132. packet->e_o_s = page_cursor == ogg_packet_sequence->page_data.size() - 1 && packet_cursor == ogg_packet_sequence->page_data[page_cursor].size() - 1;
  133. packet->granulepos = packet_cursor == ogg_packet_sequence->page_data[page_cursor].size() - 1 ? ogg_packet_sequence->page_granule_positions[page_cursor] : -1;
  134. packet->packetno = packetno++;
  135. packet->bytes = ogg_packet_sequence->page_data[page_cursor][packet_cursor].size();
  136. packet->packet = (unsigned char *)(ogg_packet_sequence->page_data[page_cursor][packet_cursor].ptr());
  137. *p_packet = packet;
  138. packet_cursor++;
  139. return true;
  140. }
  141. uint32_t OggPacketSequencePlayback::seek_page_internal(int64_t granule, uint32_t after_page_inclusive, uint32_t before_page_inclusive) {
  142. // FIXME: This function needs better corner case handling.
  143. if (before_page_inclusive == after_page_inclusive) {
  144. return before_page_inclusive;
  145. }
  146. uint32_t actual_middle_page = after_page_inclusive + (before_page_inclusive - after_page_inclusive) / 2;
  147. // Complicating the bisection search algorithm, the middle page might not have a packet that ends on it,
  148. // which means it might not have a correct granule position. Find a nearby page that does have a packet ending on it.
  149. uint32_t bisection_page = -1;
  150. // Don't include before_page_inclusive because that always succeeds and will cause infinite recursion later.
  151. for (uint32_t test_page = actual_middle_page; test_page < before_page_inclusive; test_page++) {
  152. if (ogg_packet_sequence->page_data[test_page].size() > 0) {
  153. bisection_page = test_page;
  154. break;
  155. }
  156. }
  157. // Check if we have to go backwards.
  158. if (bisection_page == (unsigned int)-1) {
  159. for (uint32_t test_page = actual_middle_page; test_page >= after_page_inclusive; test_page--) {
  160. if (ogg_packet_sequence->page_data[test_page].size() > 0) {
  161. bisection_page = test_page;
  162. break;
  163. }
  164. }
  165. }
  166. if (bisection_page == (unsigned int)-1) {
  167. return -1;
  168. }
  169. int64_t bisection_granule_pos = ogg_packet_sequence->page_granule_positions[bisection_page];
  170. if (granule > bisection_granule_pos) {
  171. return seek_page_internal(granule, bisection_page + 1, before_page_inclusive);
  172. } else {
  173. return seek_page_internal(granule, after_page_inclusive, bisection_page);
  174. }
  175. }
  176. bool OggPacketSequencePlayback::seek_page(int64_t p_granule_pos) {
  177. int correct_page = seek_page_internal(p_granule_pos, 0, ogg_packet_sequence->page_data.size() - 1);
  178. if (correct_page == -1) {
  179. return false;
  180. }
  181. packet_cursor = 0;
  182. page_cursor = correct_page;
  183. // Don't pretend subsequent packets are contiguous with previous ones.
  184. packetno = 0;
  185. return true;
  186. }
  187. int64_t OggPacketSequencePlayback::get_page_number() const {
  188. return page_cursor;
  189. }
  190. bool OggPacketSequencePlayback::set_page_number(int64_t p_page_number) {
  191. if (p_page_number >= 0 && p_page_number < ogg_packet_sequence->page_data.size()) {
  192. page_cursor = p_page_number;
  193. packet_cursor = 0;
  194. packetno = 0;
  195. return true;
  196. }
  197. return false;
  198. }
  199. OggPacketSequencePlayback::OggPacketSequencePlayback() {
  200. packet = new ogg_packet();
  201. }
  202. OggPacketSequencePlayback::~OggPacketSequencePlayback() {
  203. delete packet;
  204. }