Boxes.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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(BoxStream& stream);
  23. struct Box {
  24. Box() = default;
  25. virtual ~Box() = default;
  26. virtual BoxType box_type() const { return BoxType::None; }
  27. virtual void dump(String const& prepend = {}) const;
  28. };
  29. using BoxList = Vector<NonnullOwnPtr<Box>>;
  30. struct FullBox : public Box {
  31. ErrorOr<void> read_from_stream(BoxStream& stream);
  32. virtual void dump(String const& prepend = {}) const override;
  33. u8 version { 0 };
  34. u32 flags { 0 };
  35. };
  36. struct UnknownBox final : public Box {
  37. static ErrorOr<NonnullOwnPtr<UnknownBox>> create_from_stream(BoxType type, BoxStream& stream)
  38. {
  39. auto box = TRY(try_make<UnknownBox>(type, stream.remaining()));
  40. TRY(box->read_from_stream(stream));
  41. return box;
  42. }
  43. UnknownBox(BoxType type, size_t contents_size)
  44. : m_box_type(type)
  45. , m_contents_size(contents_size)
  46. {
  47. }
  48. virtual ~UnknownBox() override = default;
  49. ErrorOr<void> read_from_stream(BoxStream&);
  50. virtual BoxType box_type() const override { return m_box_type; }
  51. virtual void dump(String const& prepend = {}) const override;
  52. private:
  53. BoxType m_box_type { BoxType::None };
  54. size_t m_contents_size { 0 };
  55. };
  56. #define BOX_SUBTYPE(BoxName) \
  57. static ErrorOr<NonnullOwnPtr<BoxName>> create_from_stream(BoxStream& stream) \
  58. { \
  59. auto box = TRY(try_make<BoxName>()); \
  60. TRY(box->read_from_stream(stream)); \
  61. return box; \
  62. } \
  63. BoxName() = default; \
  64. virtual ~BoxName() override = default; \
  65. ErrorOr<void> read_from_stream(BoxStream& stream); \
  66. virtual BoxType box_type() const override \
  67. { \
  68. return BoxType::BoxName; \
  69. } \
  70. virtual void dump(String const& prepend = {}) const override;
  71. // 4.3 File Type Box
  72. struct FileTypeBox final : public Box {
  73. BOX_SUBTYPE(FileTypeBox);
  74. BrandIdentifier major_brand { BrandIdentifier::None };
  75. u32 minor_version;
  76. Vector<BrandIdentifier> compatible_brands;
  77. };
  78. // A box that contains other boxes.
  79. struct SuperBox : public Box {
  80. SuperBox() = default;
  81. using BoxCallback = Function<ErrorOr<Optional<NonnullOwnPtr<Box>>>(BoxType, BoxStream&)>;
  82. ErrorOr<void> read_from_stream(BoxStream&, BoxCallback);
  83. virtual void dump(String const& prepend = {}) const override;
  84. private:
  85. BoxList m_child_boxes;
  86. };
  87. }