1 /* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_PARSER_HEIF_BOX_H 17 #define PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_PARSER_HEIF_BOX_H 18 19 #include "heif_utils.h" 20 #include "heif_error.h" 21 #include "heif_stream.h" 22 #include "heif_type.h" 23 24 namespace OHOS { 25 namespace ImagePlugin { 26 class HeifBox { 27 public: 28 HeifBox() = default; 29 HeifBox(uint32_t boxType)30 explicit HeifBox(uint32_t boxType) { boxType_ = boxType; } 31 32 virtual ~HeifBox() = default; 33 34 static heif_error MakeFromReader(HeifStreamReader &reader, std::shared_ptr<HeifBox> *result); 35 36 virtual heif_error Write(HeifStreamWriter &writer) const; 37 GetBoxSize()38 uint64_t GetBoxSize() const { return boxSize_; } 39 GetHeaderSize()40 uint32_t GetHeaderSize() const { return headerSize_; } 41 GetBoxType()42 uint32_t GetBoxType() const { return boxType_; } 43 SetBoxType(uint32_t type)44 void SetBoxType(uint32_t type) { boxType_ = type; } 45 GetBoxUuidType()46 const std::vector<uint8_t>& GetBoxUuidType() const { return boxUuidType_; } 47 48 heif_error ParseHeader(HeifStreamReader &reader); 49 50 void SetHeaderInfo(const HeifBox &box); 51 52 int InferHeaderSize() const; 53 InferFullBoxVersion()54 virtual void InferFullBoxVersion() {} 55 56 void InferAllFullBoxVersion(); 57 58 static std::shared_ptr<HeifBox> MakeBox(uint32_t boxType); 59 60 template<typename T> GetChild(uint32_t boxType)61 std::shared_ptr<T> GetChild(uint32_t boxType) const 62 { 63 for (auto &box: children_) { 64 if (box->GetBoxType() == boxType) { 65 return std::dynamic_pointer_cast<T>(box); 66 } 67 } 68 return nullptr; 69 } 70 GetChildren()71 const std::vector<std::shared_ptr<HeifBox>> &GetChildren() const { return children_; } 72 73 template<typename T> GetChildren(uint32_t boxType)74 std::vector<std::shared_ptr<T>> GetChildren(uint32_t boxType) const 75 { 76 std::vector<std::shared_ptr<T>> res; 77 for (auto &box: children_) { 78 if (box->GetBoxType() == boxType) { 79 res.push_back(std::dynamic_pointer_cast<T>(box)); 80 } 81 } 82 return res; 83 } 84 AddChild(const std::shared_ptr<HeifBox> & box)85 int AddChild(const std::shared_ptr<HeifBox> &box) 86 { 87 children_.push_back(box); 88 return (int) children_.size() - 1; 89 } 90 91 private: 92 uint64_t boxSize_ = 0; 93 uint32_t boxType_ = 0; 94 std::vector<uint8_t> boxUuidType_; 95 96 protected: 97 uint32_t headerSize_ = 0; 98 99 std::vector<std::shared_ptr<HeifBox>> children_; 100 101 virtual heif_error ParseContent(HeifStreamReader &reader); 102 103 heif_error ReadChildren(HeifStreamReader &reader); 104 105 heif_error WriteChildren(HeifStreamWriter &writer) const; 106 107 virtual size_t ReserveHeader(HeifStreamWriter &writer) const; 108 109 heif_error WriteCalculatedHeader(HeifStreamWriter &, size_t startPos) const; 110 111 virtual heif_error WriteHeader(HeifStreamWriter &, size_t boxSize) const; 112 }; 113 114 class HeifFullBox : public HeifBox { 115 public: 116 HeifFullBox() = default; HeifFullBox(uint32_t boxType)117 explicit HeifFullBox(uint32_t boxType): HeifBox(boxType) {} 118 InferFullBoxVersion()119 void InferFullBoxVersion() override { SetVersion(0); } 120 121 heif_error ParseFullHeader(HeifStreamReader &reader); 122 GetVersion()123 uint8_t GetVersion() const { return version_; } 124 SetVersion(uint8_t version)125 void SetVersion(uint8_t version) { version_ = version; } 126 GetFlags()127 uint32_t GetFlags() const { return flags_; } 128 SetFlags(uint32_t flags)129 void SetFlags(uint32_t flags) { flags_ = flags; } 130 131 protected: 132 size_t ReserveHeader(HeifStreamWriter &writer) const override; 133 134 heif_error WriteHeader(HeifStreamWriter &, size_t boxSize) const override; 135 136 private: 137 uint8_t version_ = 0; 138 uint32_t flags_ = 0; 139 }; 140 } // namespace ImagePlugin 141 } // namespace OHOS 142 143 #endif // PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_PARSER_HEIF_BOX_H 144