OggVorbis.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "core/OggVorbis.h"
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <vorbis/codec.h>
  5. namespace lynx::oggvorbis
  6. {
  7. bool isOggWav (const std::uint8_t* d, std::size_t size)
  8. {
  9. if (size < 12 || std::memcmp (d, "RIFF", 4) != 0 || std::memcmp (d + 8, "WAVE", 4) != 0)
  10. return false;
  11. for (std::size_t pos = 12; pos + 8 <= size;)
  12. {
  13. std::uint32_t chunk = 0;
  14. std::memcpy (&chunk, d + pos + 4, 4);
  15. if (std::memcmp (d + pos, "fmt ", 4) == 0)
  16. {
  17. if (pos + 10 > size)
  18. return false;
  19. const auto tag = (std::uint16_t) (d[pos + 8] | (d[pos + 9] << 8));
  20. return tag == 0x674F || tag == 0x6750 || tag == 0x6751;
  21. }
  22. pos += 8 + (std::size_t) chunk + (chunk & 1);
  23. }
  24. return false;
  25. }
  26. bool isOggStream (const std::uint8_t* d, std::size_t size)
  27. {
  28. return size >= 4 && std::memcmp (d, "OggS", 4) == 0;
  29. }
  30. namespace
  31. {
  32. // Hands the input to libogg in 4096-byte reads, as FL's loader reads its file.
  33. struct PageReader
  34. {
  35. const std::uint8_t* bytes;
  36. std::size_t size, pos = 0;
  37. ogg_sync_state sync {};
  38. PageReader (const std::uint8_t* b, std::size_t n) : bytes (b), size (n) { ogg_sync_init (&sync); }
  39. ~PageReader() { ogg_sync_clear (&sync); }
  40. PageReader (const PageReader&) = delete;
  41. PageReader& operator= (const PageReader&) = delete;
  42. bool next (ogg_page& page)
  43. {
  44. for (;;)
  45. {
  46. const auto r = ogg_sync_pageout (&sync, &page);
  47. if (r > 0)
  48. return true;
  49. if (r < 0)
  50. continue; // skipped bytes that are not a page
  51. if (pos >= size)
  52. return false;
  53. const auto n = std::min<std::size_t> (4096, size - pos);
  54. auto* buffer = ogg_sync_buffer (&sync, (long) n);
  55. if (buffer == nullptr)
  56. return false;
  57. std::memcpy (buffer, bytes + pos, n);
  58. pos += n;
  59. ogg_sync_wrote (&sync, (long) n);
  60. }
  61. }
  62. };
  63. struct Link
  64. {
  65. ogg_stream_state stream {};
  66. vorbis_info info {};
  67. vorbis_comment comment {};
  68. vorbis_dsp_state dsp {};
  69. vorbis_block block {};
  70. bool synthesis = false;
  71. explicit Link (int serial)
  72. {
  73. ogg_stream_init (&stream, serial);
  74. vorbis_info_init (&info);
  75. vorbis_comment_init (&comment);
  76. }
  77. ~Link()
  78. {
  79. if (synthesis)
  80. {
  81. vorbis_block_clear (&block);
  82. vorbis_dsp_clear (&dsp);
  83. }
  84. ogg_stream_clear (&stream);
  85. vorbis_comment_clear (&comment);
  86. vorbis_info_clear (&info);
  87. }
  88. Link (const Link&) = delete;
  89. Link& operator= (const Link&) = delete;
  90. };
  91. } // namespace
  92. namespace
  93. {
  94. // Round 190px (perf 7): the last Ogg page's granule position (the stream's length in samples per channel for a
  95. // stream that starts at 0), or 0 when there is none to trust. Only a size hint for the decoded vectors: they used to
  96. // grow by doubling, so a long stem ended with up to twice its samples' memory reserved (Thief's vocal: 77.7 MB of
  97. // audio in 128 MB) and was copied at every doubling.
  98. std::size_t lengthHint (const std::uint8_t* bytes, std::size_t size) noexcept
  99. {
  100. for (std::size_t at = size >= 27 ? size - 27 : 0; at-- > 0;)
  101. if (bytes[at] == 'O' && bytes[at + 1] == 'g' && bytes[at + 2] == 'g' && bytes[at + 3] == 'S' && bytes[at + 4] == 0)
  102. {
  103. std::uint64_t g = 0;
  104. for (int k = 7; k >= 0; --k)
  105. g = (g << 8) | bytes[at + 6 + (std::size_t) k];
  106. // (−1: no packet ends on that page; anything beyond 24 hours at 384 kHz: not a length)
  107. return g == ~std::uint64_t { 0 } || g > 384000ull * 86400ull ? 0 : (std::size_t) g;
  108. }
  109. return 0;
  110. }
  111. } // namespace
  112. bool decode (const std::uint8_t* bytes, std::size_t size, Decoded& out)
  113. {
  114. out = {};
  115. if (bytes == nullptr || size == 0)
  116. return false;
  117. PageReader reader (bytes, size);
  118. ogg_page page {};
  119. ogg_packet packet {};
  120. // one pass per chained stream; the first page found starts each one
  121. while (reader.next (page))
  122. {
  123. Link link (ogg_page_serialno (&page));
  124. ogg_stream_pagein (&link.stream, &page);
  125. // the three Vorbis headers; anything else ends the decode
  126. int headers = 0;
  127. for (bool failed = false; headers < 3 && ! failed;)
  128. {
  129. const auto r = ogg_stream_packetout (&link.stream, &packet);
  130. if (r == 0)
  131. {
  132. if (headers == 0 || ! reader.next (page))
  133. failed = true;
  134. else
  135. ogg_stream_pagein (&link.stream, &page);
  136. continue;
  137. }
  138. if (r < 0 || vorbis_synthesis_headerin (&link.info, &link.comment, &packet) < 0)
  139. failed = true;
  140. else
  141. ++headers;
  142. }
  143. if (headers < 3)
  144. break;
  145. const auto channels = link.info.channels;
  146. if (channels < 1 || channels > 2 || (out.channels != 0 && channels != out.channels))
  147. break;
  148. if (out.channels == 0)
  149. {
  150. out.channels = channels;
  151. out.sampleRate = (int) link.info.rate;
  152. out.data.assign ((std::size_t) channels, {});
  153. if (const auto hint = lengthHint (bytes, size); hint > 0)
  154. for (auto& d : out.data)
  155. d.reserve (hint + 8192); // (the last packets may run a little past the last granule)
  156. }
  157. if (vorbis_synthesis_init (&link.dsp, &link.info) != 0)
  158. break;
  159. vorbis_block_init (&link.dsp, &link.block);
  160. link.synthesis = true;
  161. // audio packets; the stream ends at the first end-of-stream page of any serial
  162. for (bool more = reader.next (page); more; more = reader.next (page))
  163. {
  164. ogg_stream_pagein (&link.stream, &page); // (refused for another serial)
  165. for (;;)
  166. {
  167. const auto r = ogg_stream_packetout (&link.stream, &packet);
  168. if (r == 0)
  169. break;
  170. if (r < 0)
  171. continue; // hole
  172. if (vorbis_synthesis (&link.block, &packet) == 0)
  173. vorbis_synthesis_blockin (&link.dsp, &link.block);
  174. float** pcm = nullptr;
  175. for (int n; (n = vorbis_synthesis_pcmout (&link.dsp, &pcm)) > 0;)
  176. {
  177. for (int c = 0; c < channels; ++c)
  178. out.data[(std::size_t) c].insert (out.data[(std::size_t) c].end(), pcm[c], pcm[c] + n);
  179. vorbis_synthesis_read (&link.dsp, n);
  180. }
  181. }
  182. if (ogg_page_eos (&page))
  183. break;
  184. }
  185. }
  186. // (round 190px: no large slack kept — a hint too small left the doubling's spare room, a chained file's more)
  187. for (auto& d : out.data)
  188. if (d.capacity() - d.size() > 16384)
  189. d.shrink_to_fit();
  190. return out.channels > 0;
  191. }
  192. } // namespace lynx::oggvorbis