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,
35         std::shared_ptr<HeifBox> *result, uint32_t &recursionCount);
36 
37     virtual heif_error Write(HeifStreamWriter &writer) const;
38 
GetBoxSize()39     uint64_t GetBoxSize() const { return boxSize_; }
40 
GetHeaderSize()41     uint32_t GetHeaderSize() const { return headerSize_; }
42 
GetBoxType()43     uint32_t GetBoxType() const { return boxType_; }
44 
SetBoxType(uint32_t type)45     void SetBoxType(uint32_t type) { boxType_ = type; }
46 
GetBoxUuidType()47     const std::vector<uint8_t>& GetBoxUuidType() const { return boxUuidType_; }
48 
49     heif_error ParseHeader(HeifStreamReader &reader);
50 
51     void SetHeaderInfo(const HeifBox &box);
52 
53     int InferHeaderSize() const;
54 
InferFullBoxVersion()55     virtual void InferFullBoxVersion() {}
56 
57     void InferAllFullBoxVersion();
58 
59     static std::shared_ptr<HeifBox> MakeBox(uint32_t boxType);
60 
61     template<typename T>
GetChild(uint32_t boxType)62     std::shared_ptr<T> GetChild(uint32_t boxType) const
63     {
64         for (auto &box: children_) {
65             if (box->GetBoxType() == boxType) {
66                 return std::dynamic_pointer_cast<T>(box);
67             }
68         }
69         return nullptr;
70     }
71 
GetChildren()72     const std::vector<std::shared_ptr<HeifBox>> &GetChildren() const { return children_; }
73 
74     template<typename T>
GetChildren(uint32_t boxType)75     std::vector<std::shared_ptr<T>> GetChildren(uint32_t boxType) const
76     {
77         std::vector<std::shared_ptr<T>> res;
78         for (auto &box: children_) {
79             if (box->GetBoxType() == boxType) {
80                 res.push_back(std::dynamic_pointer_cast<T>(box));
81             }
82         }
83         return res;
84     }
85 
AddChild(const std::shared_ptr<HeifBox> & box)86     int AddChild(const std::shared_ptr<HeifBox> &box)
87     {
88         children_.push_back(box);
89         return (int) children_.size() - 1;
90     }
91 
92     const uint32_t MAX_RECURSION_COUNT = 300;
93 
94 private:
95     uint64_t boxSize_ = 0;
96     uint32_t boxType_ = 0;
97     std::vector<uint8_t> boxUuidType_;
98 
99 protected:
100     uint32_t headerSize_ = 0;
101 
102     std::vector<std::shared_ptr<HeifBox>> children_;
103 
104     virtual heif_error ParseContent(HeifStreamReader &reader);
105 
106     virtual heif_error ParseContentChildren(HeifStreamReader &reader, uint32_t &recursionCount);
107 
108     heif_error ReadChildren(HeifStreamReader &reader, uint32_t &recursionCount);
109 
110     heif_error WriteChildren(HeifStreamWriter &writer) const;
111 
112     virtual size_t ReserveHeader(HeifStreamWriter &writer) const;
113 
114     heif_error WriteCalculatedHeader(HeifStreamWriter &, size_t startPos) const;
115 
116     virtual heif_error WriteHeader(HeifStreamWriter &, size_t boxSize) const;
117 };
118 
119 class HeifFullBox : public HeifBox {
120 public:
121     HeifFullBox() = default;
HeifFullBox(uint32_t boxType)122     explicit HeifFullBox(uint32_t boxType): HeifBox(boxType) {}
123 
InferFullBoxVersion()124     void InferFullBoxVersion() override { SetVersion(0); }
125 
126     heif_error ParseFullHeader(HeifStreamReader &reader);
127 
GetVersion()128     uint8_t GetVersion() const { return version_; }
129 
SetVersion(uint8_t version)130     void SetVersion(uint8_t version) { version_ = version; }
131 
GetFlags()132     uint32_t GetFlags() const { return flags_; }
133 
SetFlags(uint32_t flags)134     void SetFlags(uint32_t flags) { flags_ = flags; }
135 
136 protected:
137     size_t ReserveHeader(HeifStreamWriter &writer) const override;
138 
139     heif_error WriteHeader(HeifStreamWriter &, size_t boxSize) const override;
140 
141 private:
142     uint8_t version_ = 0;
143     uint32_t flags_ = 0;
144 };
145 } // namespace ImagePlugin
146 } // namespace OHOS
147 
148 #endif // PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_PARSER_HEIF_BOX_H
149