OggVorbis.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. bool decode (const std::uint8_t* bytes, std::size_t size, Decoded& out)
  93. {
  94. out = {};
  95. if (bytes == nullptr || size == 0)
  96. return false;
  97. PageReader reader (bytes, size);
  98. ogg_page page {};
  99. ogg_packet packet {};
  100. // one pass per chained stream; the first page found starts each one
  101. while (reader.next (page))
  102. {
  103. Link link (ogg_page_serialno (&page));
  104. ogg_stream_pagein (&link.stream, &page);
  105. // the three Vorbis headers; anything else ends the decode
  106. int headers = 0;
  107. for (bool failed = false; headers < 3 && ! failed;)
  108. {
  109. const auto r = ogg_stream_packetout (&link.stream, &packet);
  110. if (r == 0)
  111. {
  112. if (headers == 0 || ! reader.next (page))
  113. failed = true;
  114. else
  115. ogg_stream_pagein (&link.stream, &page);
  116. continue;
  117. }
  118. if (r < 0 || vorbis_synthesis_headerin (&link.info, &link.comment, &packet) < 0)
  119. failed = true;
  120. else
  121. ++headers;
  122. }
  123. if (headers < 3)
  124. break;
  125. const auto channels = link.info.channels;
  126. if (channels < 1 || channels > 2 || (out.channels != 0 && channels != out.channels))
  127. break;
  128. if (out.channels == 0)
  129. {
  130. out.channels = channels;
  131. out.sampleRate = (int) link.info.rate;
  132. out.data.assign ((std::size_t) channels, {});
  133. }
  134. if (vorbis_synthesis_init (&link.dsp, &link.info) != 0)
  135. break;
  136. vorbis_block_init (&link.dsp, &link.block);
  137. link.synthesis = true;
  138. // audio packets; the stream ends at the first end-of-stream page of any serial
  139. for (bool more = reader.next (page); more; more = reader.next (page))
  140. {
  141. ogg_stream_pagein (&link.stream, &page); // (refused for another serial)
  142. for (;;)
  143. {
  144. const auto r = ogg_stream_packetout (&link.stream, &packet);
  145. if (r == 0)
  146. break;
  147. if (r < 0)
  148. continue; // hole
  149. if (vorbis_synthesis (&link.block, &packet) == 0)
  150. vorbis_synthesis_blockin (&link.dsp, &link.block);
  151. float** pcm = nullptr;
  152. for (int n; (n = vorbis_synthesis_pcmout (&link.dsp, &pcm)) > 0;)
  153. {
  154. for (int c = 0; c < channels; ++c)
  155. out.data[(std::size_t) c].insert (out.data[(std::size_t) c].end(), pcm[c], pcm[c] + n);
  156. vorbis_synthesis_read (&link.dsp, n);
  157. }
  158. }
  159. if (ogg_page_eos (&page))
  160. break;
  161. }
  162. }
  163. return out.channels > 0;
  164. }
  165. } // namespace lynx::oggvorbis