• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #include "image_packer.h"
17 
18 #include "buffer_packer_stream.h"
19 #include "file_packer_stream.h"
20 #include "image/abs_image_encoder.h"
21 #include "image_utils.h"
22 #include "log_tags.h"
23 #include "media_errors.h"
24 #include "ostream_packer_stream.h"
25 #include "plugin_server.h"
26 #if defined(_ANDROID) || defined(_IOS)
27 #include "include/jpeg_encoder.h"
28 #endif
29 
30 namespace OHOS {
31 namespace Media {
32 using namespace OHOS::HiviewDFX;
33 using namespace ImagePlugin;
34 using namespace MultimediaPlugin;
35 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_TAG_DOMAIN_ID_IMAGE, "ImagePacker" };
36 static constexpr uint8_t QUALITY_MAX = 100;
37 
38 PluginServer &ImagePacker::pluginServer_ = ImageUtils::GetPluginServer();
39 
GetSupportedFormats(std::set<std::string> & formats)40 uint32_t ImagePacker::GetSupportedFormats(std::set<std::string> &formats)
41 {
42     formats.clear();
43     std::vector<ClassInfo> classInfos;
44     uint32_t ret =
45         pluginServer_.PluginServerGetClassInfo<AbsImageEncoder>(AbsImageEncoder::SERVICE_DEFAULT, classInfos);
46     if (ret != SUCCESS) {
47         HiLog::Error(LABEL, "get class info from plugin server,ret:%{public}u.", ret);
48         return ret;
49     }
50     for (auto &info : classInfos) {
51         std::map<std::string, AttrData> &capbility = info.capabilities;
52         auto iter = capbility.find(IMAGE_ENCODE_FORMAT);
53         if (iter == capbility.end()) {
54             continue;
55         }
56         AttrData &attr = iter->second;
57         std::string format;
58         if (attr.GetValue(format) != SUCCESS) {
59             HiLog::Error(LABEL, "attr data get format failed.");
60             continue;
61         }
62         formats.insert(format);
63     }
64     return SUCCESS;
65 }
66 
StartPackingImpl(const PackOption & option)67 uint32_t ImagePacker::StartPackingImpl(const PackOption &option)
68 {
69     if (packerStream_ == nullptr || packerStream_.get() == nullptr) {
70         HiLog::Error(LABEL, "make buffer packer stream failed.");
71         return ERR_IMAGE_DATA_ABNORMAL;
72     }
73     if (!GetEncoderPlugin(option)) {
74         HiLog::Error(LABEL, "StartPackingImpl get encoder plugin failed.");
75         return ERR_IMAGE_MISMATCHED_FORMAT;
76     }
77     PlEncodeOptions plOpts;
78     CopyOptionsToPlugin(option, plOpts);
79     return encoder_->StartEncode(*packerStream_.get(), plOpts);
80 }
81 
StartPacking(uint8_t * outputData,uint32_t maxSize,const PackOption & option)82 uint32_t ImagePacker::StartPacking(uint8_t *outputData, uint32_t maxSize, const PackOption &option)
83 {
84     if (!IsPackOptionValid(option)) {
85         HiLog::Error(LABEL, "array startPacking option invalid %{public}s, %{public}u.", option.format.c_str(),
86                      option.quality);
87         return ERR_IMAGE_INVALID_PARAMETER;
88     }
89 
90     if (outputData == nullptr) {
91         HiLog::Error(LABEL, "output buffer is null.");
92         return ERR_IMAGE_INVALID_PARAMETER;
93     }
94     BufferPackerStream *stream = new (std::nothrow) BufferPackerStream(outputData, maxSize);
95     if (stream == nullptr) {
96         HiLog::Error(LABEL, "make buffer packer stream failed.");
97         return ERR_IMAGE_DATA_ABNORMAL;
98     }
99     FreeOldPackerStream();
100     packerStream_ = std::unique_ptr<BufferPackerStream>(stream);
101     return StartPackingImpl(option);
102 }
103 
StartPacking(const std::string & filePath,const PackOption & option)104 uint32_t ImagePacker::StartPacking(const std::string &filePath, const PackOption &option)
105 {
106     if (!IsPackOptionValid(option)) {
107         HiLog::Error(LABEL, "filepath startPacking option invalid %{public}s, %{public}u.", option.format.c_str(),
108                      option.quality);
109         return ERR_IMAGE_INVALID_PARAMETER;
110     }
111     FilePackerStream *stream = new (std::nothrow) FilePackerStream(filePath);
112     if (stream == nullptr) {
113         HiLog::Error(LABEL, "make file packer stream failed.");
114         return ERR_IMAGE_DATA_ABNORMAL;
115     }
116     FreeOldPackerStream();
117     packerStream_ = std::unique_ptr<FilePackerStream>(stream);
118     return StartPackingImpl(option);
119 }
120 
StartPacking(const int & fd,const PackOption & option)121 uint32_t ImagePacker::StartPacking(const int &fd, const PackOption &option)
122 {
123     if (!IsPackOptionValid(option)) {
124         HiLog::Error(LABEL, "fd startPacking option invalid %{public}s, %{public}u.", option.format.c_str(),
125                      option.quality);
126         return ERR_IMAGE_INVALID_PARAMETER;
127     }
128     FilePackerStream *stream = new (std::nothrow) FilePackerStream(fd);
129     if (stream == nullptr) {
130         HiLog::Error(LABEL, "make file packer stream failed.");
131         return ERR_IMAGE_DATA_ABNORMAL;
132     }
133     FreeOldPackerStream();
134     packerStream_ = std::unique_ptr<FilePackerStream>(stream);
135     return StartPackingImpl(option);
136 }
137 
StartPacking(std::ostream & outputStream,const PackOption & option)138 uint32_t ImagePacker::StartPacking(std::ostream &outputStream, const PackOption &option)
139 {
140     if (!IsPackOptionValid(option)) {
141         HiLog::Error(LABEL, "outputStream startPacking option invalid %{public}s, %{public}u.", option.format.c_str(),
142                      option.quality);
143         return ERR_IMAGE_INVALID_PARAMETER;
144     }
145     OstreamPackerStream *stream = new (std::nothrow) OstreamPackerStream(outputStream);
146     if (stream == nullptr) {
147         HiLog::Error(LABEL, "make ostream packer stream failed.");
148         return ERR_IMAGE_DATA_ABNORMAL;
149     }
150     FreeOldPackerStream();
151     packerStream_ = std::unique_ptr<OstreamPackerStream>(stream);
152     return StartPackingImpl(option);
153 }
154 
155 // JNI adapter method, this method be called by jni and the outputStream be created by jni, here we manage the lifecycle
156 // of the outputStream
StartPackingAdapter(PackerStream & outputStream,const PackOption & option)157 uint32_t ImagePacker::StartPackingAdapter(PackerStream &outputStream, const PackOption &option)
158 {
159     FreeOldPackerStream();
160     packerStream_ = std::unique_ptr<PackerStream>(&outputStream);
161 
162     if (!IsPackOptionValid(option)) {
163         HiLog::Error(LABEL, "packer stream option invalid %{public}s, %{public}u.", option.format.c_str(),
164                      option.quality);
165         return ERR_IMAGE_INVALID_PARAMETER;
166     }
167     return StartPackingImpl(option);
168 }
169 
AddImage(PixelMap & pixelMap)170 uint32_t ImagePacker::AddImage(PixelMap &pixelMap)
171 {
172     if (encoder_ == nullptr) {
173         HiLog::Error(LABEL, "AddImage get encoder plugin failed.");
174         return ERR_IMAGE_MISMATCHED_FORMAT;
175     }
176     return encoder_->AddImage(pixelMap);
177 }
178 
AddImage(ImageSource & source)179 uint32_t ImagePacker::AddImage(ImageSource &source)
180 {
181     DecodeOptions opts;
182     uint32_t ret = SUCCESS;
183     if (pixelMap_ != nullptr) {
184         pixelMap_.reset();  // release old inner pixelmap
185     }
186     pixelMap_ = source.CreatePixelMap(opts, ret);
187     if (ret != SUCCESS) {
188         HiLog::Error(LABEL, "image source create pixel map failed.");
189         return ret;
190     }
191     if (pixelMap_ == nullptr || pixelMap_.get() == nullptr) {
192         HiLog::Error(LABEL, "create the pixel map unique_ptr fail.");
193         return ERR_IMAGE_MALLOC_ABNORMAL;
194     }
195     return AddImage(*pixelMap_.get());
196 }
197 
AddImage(ImageSource & source,uint32_t index)198 uint32_t ImagePacker::AddImage(ImageSource &source, uint32_t index)
199 {
200     DecodeOptions opts;
201     uint32_t ret = SUCCESS;
202     if (pixelMap_ != nullptr) {
203         pixelMap_.reset();  // release old inner pixelmap
204     }
205     pixelMap_ = source.CreatePixelMap(index, opts, ret);
206     if (ret != SUCCESS) {
207         HiLog::Error(LABEL, "image source create pixel map failed.");
208         return ret;
209     }
210     if (pixelMap_ == nullptr || pixelMap_.get() == nullptr) {
211         HiLog::Error(LABEL, "create the pixel map unique_ptr fail.");
212         return ERR_IMAGE_MALLOC_ABNORMAL;
213     }
214     return AddImage(*pixelMap_.get());
215 }
216 
FinalizePacking()217 uint32_t ImagePacker::FinalizePacking()
218 {
219     if (encoder_ == nullptr) {
220         HiLog::Error(LABEL, "FinalizePacking get encoder plugin failed.");
221         return ERR_IMAGE_MISMATCHED_FORMAT;
222     }
223     return encoder_->FinalizeEncode();
224 }
225 
FinalizePacking(int64_t & packedSize)226 uint32_t ImagePacker::FinalizePacking(int64_t &packedSize)
227 {
228     uint32_t ret = FinalizePacking();
229     packedSize = (packerStream_ != nullptr) ? packerStream_->BytesWritten() : 0;
230     return ret;
231 }
232 
GetEncoderPlugin(const PackOption & option)233 bool ImagePacker::GetEncoderPlugin(const PackOption &option)
234 {
235     std::map<std::string, AttrData> capabilities;
236     capabilities.insert(std::map<std::string, AttrData>::value_type(IMAGE_ENCODE_FORMAT, AttrData(option.format)));
237     if (encoder_ != nullptr) {
238         encoder_.reset();
239     }
240 #if defined(_ANDROID) || defined(_IOS)
241     encoder_ = std::make_unique<ImagePlugin::JpegEncoder>();
242 #else
243     encoder_ = std::unique_ptr<ImagePlugin::AbsImageEncoder>(
244         pluginServer_.CreateObject<AbsImageEncoder>(AbsImageEncoder::SERVICE_DEFAULT, capabilities));
245 #endif
246     return (encoder_ != nullptr);
247 }
248 
CopyOptionsToPlugin(const PackOption & opts,PlEncodeOptions & plOpts)249 void ImagePacker::CopyOptionsToPlugin(const PackOption &opts, PlEncodeOptions &plOpts)
250 {
251     plOpts.numberHint = opts.numberHint;
252     plOpts.quality = opts.quality;
253 }
254 
FreeOldPackerStream()255 void ImagePacker::FreeOldPackerStream()
256 {
257     if (packerStream_ != nullptr) {
258         packerStream_.reset();
259     }
260 }
261 
IsPackOptionValid(const PackOption & option)262 bool ImagePacker::IsPackOptionValid(const PackOption &option)
263 {
264     return !(option.quality > QUALITY_MAX || option.format.empty());
265 }
266 
267 // class reference need explicit constructor and destructor, otherwise unique_ptr<T> use unnormal
ImagePacker()268 ImagePacker::ImagePacker()
269 {}
270 
~ImagePacker()271 ImagePacker::~ImagePacker()
272 {}
273 } // namespace Media
274 } // namespace OHOS
275