OggStream.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * OggStream.h
  3. * -----------
  4. * Purpose: Basic Ogg stream parsing functionality
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include "openmpt/base/Endian.hpp"
  12. #include "mpt/io/io.hpp"
  13. #include "../common/FileReaderFwd.h"
  14. OPENMPT_NAMESPACE_BEGIN
  15. namespace Ogg
  16. {
  17. struct PageHeader
  18. {
  19. char capture_pattern[4]; // "OggS"
  20. uint8le version;
  21. uint8le header_type;
  22. uint64le granule_position;
  23. uint32le bitstream_serial_number;
  24. uint32le page_sequence_number;
  25. uint32le CRC_checksum;
  26. uint8le page_segments;
  27. };
  28. MPT_BINARY_STRUCT(PageHeader, 27)
  29. struct PageInfo
  30. {
  31. PageHeader header;
  32. uint8 segment_table[255];
  33. PageInfo()
  34. {
  35. MemsetZero(header);
  36. MemsetZero(segment_table);
  37. }
  38. uint16 GetPagePhysicalSize() const;
  39. uint16 GetPageHeaderSize() const;
  40. uint16 GetPageDataSize() const;
  41. };
  42. // returns false on EOF
  43. bool AdvanceToPageMagic(FileReader &file);
  44. bool ReadPage(FileReader &file, PageInfo &pageInfo, std::vector<uint8> *pageData = nullptr);
  45. bool ReadPage(FileReader &file, PageInfo &pageInfo, std::vector<uint8> &pageData);
  46. bool ReadPage(FileReader &file);
  47. bool ReadPageAndSkipJunk(FileReader &file, PageInfo &pageInfo, std::vector<uint8> &pageData);
  48. bool UpdatePageCRC(PageInfo &pageInfo, const std::vector<uint8> &pageData);
  49. template <typename Tfile>
  50. bool WritePage(Tfile & f, const PageInfo &pageInfo, const std::vector<uint8> &pageData)
  51. {
  52. if(!mpt::IO::Write(f, pageInfo.header))
  53. {
  54. return false;
  55. }
  56. if(!mpt::IO::WriteRaw(f, pageInfo.segment_table, pageInfo.header.page_segments))
  57. {
  58. return false;
  59. }
  60. if(!mpt::IO::WriteRaw(f, pageData.data(), pageData.size()))
  61. {
  62. return false;
  63. }
  64. return true;
  65. }
  66. } // namespace Ogg
  67. OPENMPT_NAMESPACE_END