• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 == nullptr) {
66                 return nullptr;
67             }
68             if (box->GetBoxType() == boxType) {
69                 return std::dynamic_pointer_cast<T>(box);
70             }
71         }
72         return nullptr;
73     }
74 
GetChildren()75     const std::vector<std::shared_ptr<HeifBox>> &GetChildren() const { return children_; }
76 
77     template<typename T>
GetChildren(uint32_t boxType)78     std::vector<std::shared_ptr<T>> GetChildren(uint32_t boxType) const
79     {
80         std::vector<std::shared_ptr<T>> res;
81         for (auto &box: children_) {
82             if (box->GetBoxType() == boxType) {
83                 res.push_back(std::dynamic_pointer_cast<T>(box));
84             }
85         }
86         return res;
87     }
88 
AddChild(const std::shared_ptr<HeifBox> & box)89     int AddChild(const std::shared_ptr<HeifBox> &box)
90     {
91         children_.push_back(box);
92         return (int) children_.size() - 1;
93     }
94 
95     const uint32_t MAX_RECURSION_COUNT = 300;
96 
97 private:
98     uint64_t boxSize_ = 0;
99     uint32_t boxType_ = 0;
100     std::vector<uint8_t> boxUuidType_;
101 
102 protected:
103     uint32_t headerSize_ = 0;
104 
105     std::vector<std::shared_ptr<HeifBox>> children_;
106 
107     virtual heif_error ParseContent(HeifStreamReader &reader);
108 
109     virtual heif_error ParseContentChildren(HeifStreamReader &reader, uint32_t &recursionCount);
110 
111     heif_error ReadChildren(HeifStreamReader &reader, uint32_t &recursionCount);
112 
113     heif_error WriteChildren(HeifStreamWriter &writer) const;
114 
115     virtual size_t ReserveHeader(HeifStreamWriter &writer) const;
116 
117     heif_error WriteCalculatedHeader(HeifStreamWriter &, size_t startPos) const;
118 
119     virtual heif_error WriteHeader(HeifStreamWriter &, size_t boxSize) const;
120 };
121 
122 class HeifFullBox : public HeifBox {
123 public:
124     HeifFullBox() = default;
HeifFullBox(uint32_t boxType)125     explicit HeifFullBox(uint32_t boxType): HeifBox(boxType) {}
126 
InferFullBoxVersion()127     void InferFullBoxVersion() override { SetVersion(0); }
128 
129     heif_error ParseFullHeader(HeifStreamReader &reader);
130 
GetVersion()131     uint8_t GetVersion() const { return version_; }
132 
SetVersion(uint8_t version)133     void SetVersion(uint8_t version) { version_ = version; }
134 
GetFlags()135     uint32_t GetFlags() const { return flags_; }
136 
SetFlags(uint32_t flags)137     void SetFlags(uint32_t flags) { flags_ = flags; }
138 
139 protected:
140     size_t ReserveHeader(HeifStreamWriter &writer) const override;
141 
142     heif_error WriteHeader(HeifStreamWriter &, size_t boxSize) const override;
143 
144 private:
145     uint8_t version_ = 0;
146     uint32_t flags_ = 0;
147 };
148 } // namespace ImagePlugin
149 } // namespace OHOS
150 
151 #endif // PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_PARSER_HEIF_BOX_H
152