Boxes.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2023, Gregory Bertilson <Zaggy1024@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Endian.h>
  8. #include <AK/NonnullOwnPtr.h>
  9. #include <AK/String.h>
  10. #include <AK/Traits.h>
  11. #include <AK/Vector.h>
  12. #include <LibVideo/DecoderError.h>
  13. #include "BoxStream.h"
  14. #include "Enums.h"
  15. namespace Gfx::ISOBMFF {
  16. // ISO/IEC 14496-12 Fifth Edition
  17. // 4.2 Object Structure
  18. struct BoxHeader {
  19. BoxType type { BoxType::None };
  20. u64 contents_size { 0 };
  21. };
  22. ErrorOr<BoxHeader> read_box_header(Stream& stream);
  23. struct Box {
  24. Box() = default;
  25. virtual ~Box() = default;
  26. virtual ErrorOr<void> read_from_stream(BoxStream&) { return {}; }
  27. virtual BoxType box_type() const { return BoxType::None; }
  28. virtual void dump(String const& prepend = {}) const;
  29. };
  30. using BoxList = Vector<NonnullOwnPtr<Box>>;
  31. struct FullBox : public Box {
  32. virtual ErrorOr<void> read_from_stream(BoxStream& stream) override;
  33. virtual void dump(String const& prepend = {}) const override;
  34. u8 version { 0 };
  35. u32 flags { 0 };
  36. };
  37. struct UnknownBox final : public Box {
  38. static ErrorOr<NonnullOwnPtr<UnknownBox>> create_from_stream(BoxType type, BoxStream& stream)
  39. {
  40. auto box = TRY(try_make<UnknownBox>(type, stream.remaining()));
  41. TRY(box->read_from_stream(stream));
  42. return box;
  43. }
  44. UnknownBox(BoxType type, size_t contents_size)
  45. : m_box_type(type)
  46. , m_contents_size(contents_size)
  47. {
  48. }
  49. virtual ~UnknownBox() override = default;
  50. virtual ErrorOr<void> read_from_stream(BoxStream&) override;
  51. virtual BoxType box_type() const override { return m_box_type; }
  52. virtual void dump(String const& prepend = {}) const override;
  53. private:
  54. BoxType m_box_type { BoxType::None };
  55. size_t m_contents_size { 0 };
  56. };
  57. #define BOX_SUBTYPE(BoxName) \
  58. static ErrorOr<NonnullOwnPtr<BoxName>> create_from_stream(BoxStream& stream) \
  59. { \
  60. auto box = TRY(try_make<BoxName>()); \
  61. TRY(box->read_from_stream(stream)); \
  62. return box; \
  63. } \
  64. BoxName() = default; \
  65. virtual ~BoxName() override = default; \
  66. virtual ErrorOr<void> read_from_stream(BoxStream& stream) override; \
  67. virtual BoxType box_type() const override \
  68. { \
  69. return BoxType::BoxName; \
  70. } \
  71. virtual void dump(String const& prepend = {}) const override;
  72. // 4.3 File Type Box
  73. struct FileTypeBox final : public FullBox {
  74. BOX_SUBTYPE(FileTypeBox);
  75. BrandIdentifier major_brand { BrandIdentifier::None };
  76. u32 minor_version;
  77. Vector<BrandIdentifier> compatible_brands;
  78. };
  79. }