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_log.h"
22 #include "image_mime_type.h"
23 #include "image_trace.h"
24 #include "image_utils.h"
25 #include "media_errors.h"
26 #include "ostream_packer_stream.h"
27 #include "plugin_server.h"
28 #include "string_ex.h"
29 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
30 #include "include/jpeg_encoder.h"
31 #endif
32 #ifdef HEIF_HW_ENCODE_ENABLE
33 #include "image/v2_0/icodec_image.h"
34 #include "image/v2_0/codec_image_type.h"
35 #include "v3_0/codec_types.h"
36 #include "v3_0/icodec_component_manager.h"
37 #endif
38
39 #undef LOG_DOMAIN
40 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
41
42 #undef LOG_TAG
43 #define LOG_TAG "ImagePacker"
44
45 namespace OHOS {
46 namespace Media {
47 using namespace ImagePlugin;
48 using namespace MultimediaPlugin;
49 static constexpr uint8_t QUALITY_MAX = 100;
50 const static std::string EXTENDED_ENCODER = "image/jpeg,image/png,image/webp";
51 static constexpr size_t SIZE_ZERO = 0;
52
53 PluginServer &ImagePacker::pluginServer_ = ImageUtils::GetPluginServer();
54
55 #ifdef HEIF_HW_ENCODE_ENABLE
IsEncodeSecureMode(const std::string & name)56 static bool IsEncodeSecureMode(const std::string &name)
57 {
58 std::string prefix = ".secure";
59 if (name.length() <= prefix.length()) {
60 return false;
61 }
62 return name.rfind(prefix) == (name.length() - prefix.length());
63 }
64 #endif
65
IsSupportHeifEncode()66 static bool IsSupportHeifEncode()
67 {
68 #ifdef HEIF_HW_ENCODE_ENABLE
69 sptr<HDI::Codec::Image::V2_0::ICodecImage> image =
70 HDI::Codec::Image::V2_0::ICodecImage::Get(false);
71 if (image == nullptr) {
72 return false;
73 }
74 std::vector<HDI::Codec::Image::V2_0::CodecImageCapability> capList;
75 int32_t ret = image->GetImageCapability(capList);
76 if (ret != HDF_SUCCESS || capList.empty()) {
77 return false;
78 }
79 for (const auto& cap : capList) {
80 if (cap.role == HDI::Codec::Image::V2_0::CODEC_IMAGE_HEIF &&
81 cap.type == HDI::Codec::Image::V2_0::CODEC_IMAGE_TYPE_ENCODER && !IsEncodeSecureMode(cap.name)) {
82 return true;
83 }
84 }
85 #endif
86 return false;
87 }
88
GetSupportedFormats(std::set<std::string> & formats)89 uint32_t ImagePacker::GetSupportedFormats(std::set<std::string> &formats)
90 {
91 formats.clear();
92 std::vector<ClassInfo> classInfos;
93 uint32_t ret =
94 pluginServer_.PluginServerGetClassInfo<AbsImageEncoder>(AbsImageEncoder::SERVICE_DEFAULT, classInfos);
95 if (ret != SUCCESS) {
96 IMAGE_LOGE("get class info from plugin server failed, ret:%{public}u.", ret);
97 return ret;
98 }
99 for (auto &info : classInfos) {
100 std::map<std::string, AttrData> &capbility = info.capabilities;
101 auto iter = capbility.find(IMAGE_ENCODE_FORMAT);
102 if (iter == capbility.end()) {
103 continue;
104 }
105 AttrData &attr = iter->second;
106 std::string format;
107 if (attr.GetValue(format) != SUCCESS) {
108 IMAGE_LOGE("attr data get format failed.");
109 continue;
110 }
111 std::vector<std::string> splitedVector;
112 SplitStr(format, ",", splitedVector);
113 for (std::string item : splitedVector) {
114 formats.insert(item);
115 }
116 }
117 static bool isSupportHeif = IsSupportHeifEncode();
118 if (isSupportHeif) {
119 formats.insert(ImageUtils::GetEncodedHeifFormat());
120 }
121 return SUCCESS;
122 }
123
StartPackingImpl(const PackOption & option)124 uint32_t ImagePacker::StartPackingImpl(const PackOption &option)
125 {
126 if (packerStream_ == nullptr || packerStream_.get() == nullptr) {
127 IMAGE_LOGE("make buffer packer stream failed.");
128 return ERR_IMAGE_DATA_ABNORMAL;
129 }
130 if (!GetEncoderPlugin(option)) {
131 IMAGE_LOGE("StartPackingImpl get encoder plugin failed.");
132 return ERR_IMAGE_MISMATCHED_FORMAT;
133 }
134 encodeToSdr_ = ((option.desiredDynamicRange == EncodeDynamicRange::SDR) ||
135 (option.format != IMAGE_JPEG_FORMAT && option.format != IMAGE_HEIF_FORMAT &&
136 option.format != IMAGE_HEIC_FORMAT));
137 PlEncodeOptions plOpts;
138 CopyOptionsToPlugin(option, plOpts);
139 return DoEncodingFunc([this, &plOpts](ImagePlugin::AbsImageEncoder* encoder) {
140 return encoder->StartEncode(*packerStream_.get(), plOpts);
141 });
142 }
143
StartPacking(uint8_t * outputData,uint32_t maxSize,const PackOption & option)144 uint32_t ImagePacker::StartPacking(uint8_t *outputData, uint32_t maxSize, const PackOption &option)
145 {
146 ImageTrace imageTrace("ImagePacker::StartPacking by outputData");
147 if (!IsPackOptionValid(option)) {
148 IMAGE_LOGE("array startPacking option invalid %{public}s, %{public}u.", option.format.c_str(),
149 option.quality);
150 return ERR_IMAGE_INVALID_PARAMETER;
151 }
152
153 if (outputData == nullptr) {
154 IMAGE_LOGE("output buffer is null.");
155 return ERR_IMAGE_INVALID_PARAMETER;
156 }
157 BufferPackerStream *stream = new (std::nothrow) BufferPackerStream(outputData, maxSize);
158 if (stream == nullptr) {
159 IMAGE_LOGE("make buffer packer stream failed.");
160 return ERR_IMAGE_DATA_ABNORMAL;
161 }
162 FreeOldPackerStream();
163 packerStream_ = std::unique_ptr<BufferPackerStream>(stream);
164 return StartPackingImpl(option);
165 }
166
StartPacking(const std::string & filePath,const PackOption & option)167 uint32_t ImagePacker::StartPacking(const std::string &filePath, const PackOption &option)
168 {
169 ImageTrace imageTrace("ImagePacker::StartPacking by filePath");
170 if (!IsPackOptionValid(option)) {
171 IMAGE_LOGE("filepath startPacking option invalid %{public}s, %{public}u.", option.format.c_str(),
172 option.quality);
173 return ERR_IMAGE_INVALID_PARAMETER;
174 }
175 FilePackerStream *stream = new (std::nothrow) FilePackerStream(filePath);
176 if (stream == nullptr) {
177 IMAGE_LOGE("make file packer stream failed.");
178 return ERR_IMAGE_DATA_ABNORMAL;
179 }
180 FreeOldPackerStream();
181 packerStream_ = std::unique_ptr<FilePackerStream>(stream);
182 return StartPackingImpl(option);
183 }
184
StartPacking(const int & fd,const PackOption & option)185 uint32_t ImagePacker::StartPacking(const int &fd, const PackOption &option)
186 {
187 ImageTrace imageTrace("ImagePacker::StartPacking by fd");
188 if (!IsPackOptionValid(option)) {
189 IMAGE_LOGE("fd startPacking option invalid %{public}s, %{public}u.", option.format.c_str(), option.quality);
190 return ERR_IMAGE_INVALID_PARAMETER;
191 }
192 FilePackerStream *stream = new (std::nothrow) FilePackerStream(fd);
193 if (stream == nullptr) {
194 IMAGE_LOGE("make file packer stream failed.");
195 return ERR_IMAGE_DATA_ABNORMAL;
196 }
197 FreeOldPackerStream();
198 packerStream_ = std::unique_ptr<FilePackerStream>(stream);
199 return StartPackingImpl(option);
200 }
201
StartPacking(std::ostream & outputStream,const PackOption & option)202 uint32_t ImagePacker::StartPacking(std::ostream &outputStream, const PackOption &option)
203 {
204 ImageTrace imageTrace("ImagePacker::StartPacking by outputStream");
205 if (!IsPackOptionValid(option)) {
206 IMAGE_LOGE("outputStream startPacking option invalid %{public}s, %{public}u.", option.format.c_str(),
207 option.quality);
208 return ERR_IMAGE_INVALID_PARAMETER;
209 }
210 OstreamPackerStream *stream = new (std::nothrow) OstreamPackerStream(outputStream);
211 if (stream == nullptr) {
212 IMAGE_LOGE("make ostream packer stream failed.");
213 return ERR_IMAGE_DATA_ABNORMAL;
214 }
215 FreeOldPackerStream();
216 packerStream_ = std::unique_ptr<OstreamPackerStream>(stream);
217 return StartPackingImpl(option);
218 }
219
220 // JNI adapter method, this method be called by jni and the outputStream be created by jni, here we manage the lifecycle
221 // of the outputStream
StartPackingAdapter(PackerStream & outputStream,const PackOption & option)222 uint32_t ImagePacker::StartPackingAdapter(PackerStream &outputStream, const PackOption &option)
223 {
224 FreeOldPackerStream();
225 packerStream_ = std::unique_ptr<PackerStream>(&outputStream);
226
227 if (!IsPackOptionValid(option)) {
228 IMAGE_LOGE("packer stream option invalid %{public}s, %{public}u.", option.format.c_str(), option.quality);
229 return ERR_IMAGE_INVALID_PARAMETER;
230 }
231 return StartPackingImpl(option);
232 }
233
AddImage(PixelMap & pixelMap)234 uint32_t ImagePacker::AddImage(PixelMap &pixelMap)
235 {
236 ImageUtils::DumpPixelMapBeforeEncode(pixelMap);
237 ImageTrace imageTrace("ImagePacker::AddImage by pixelMap");
238
239 return DoEncodingFunc([this, &pixelMap](ImagePlugin::AbsImageEncoder* encoder) {
240 return encoder->AddImage(pixelMap);
241 });
242 }
243
AddImage(ImageSource & source)244 uint32_t ImagePacker::AddImage(ImageSource &source)
245 {
246 ImageTrace imageTrace("ImagePacker::AddImage by imageSource");
247 DecodeOptions decodeOpts;
248 decodeOpts.desiredDynamicRange = encodeToSdr_ ? DecodeDynamicRange::SDR : DecodeDynamicRange::AUTO;
249 uint32_t ret = SUCCESS;
250 if (pixelMap_ != nullptr) {
251 pixelMap_.reset(); // release old inner pixelmap
252 }
253 pixelMap_ = source.CreatePixelMap(decodeOpts, ret);
254 if (ret != SUCCESS) {
255 IMAGE_LOGE("image source create pixel map failed.");
256 return ret;
257 }
258
259 if (pixelMap_ == nullptr || pixelMap_.get() == nullptr) {
260 IMAGE_LOGE("create the pixel map unique_ptr fail.");
261 return ERR_IMAGE_MALLOC_ABNORMAL;
262 }
263
264 return AddImage(*pixelMap_.get());
265 }
266
AddImage(ImageSource & source,uint32_t index)267 uint32_t ImagePacker::AddImage(ImageSource &source, uint32_t index)
268 {
269 ImageTrace imageTrace("ImagePacker::AddImage by imageSource and index %{public}u", index);
270 DecodeOptions decodeOpts;
271 decodeOpts.desiredDynamicRange = encodeToSdr_ ? DecodeDynamicRange::SDR : DecodeDynamicRange::AUTO;
272 uint32_t ret = SUCCESS;
273 if (pixelMap_ != nullptr) {
274 pixelMap_.reset(); // release old inner pixelmap
275 }
276 pixelMap_ = source.CreatePixelMap(index, decodeOpts, ret);
277 if (ret != SUCCESS) {
278 IMAGE_LOGE("image source create pixel map failed.");
279 return ret;
280 }
281 if (pixelMap_ == nullptr || pixelMap_.get() == nullptr) {
282 IMAGE_LOGE("create the pixel map unique_ptr fail.");
283 return ERR_IMAGE_MALLOC_ABNORMAL;
284 }
285
286 return AddImage(*pixelMap_.get());
287 }
288
289 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
AddPicture(Picture & picture)290 uint32_t ImagePacker::AddPicture(Picture &picture)
291 {
292 return DoEncodingFunc([this, &picture](ImagePlugin::AbsImageEncoder* encoder) {
293 return encoder->AddPicture(picture);
294 });
295 }
296 #endif
297
FinalizePacking()298 uint32_t ImagePacker::FinalizePacking()
299 {
300 return DoEncodingFunc([](ImagePlugin::AbsImageEncoder* encoder) {
301 auto res = encoder->FinalizeEncode();
302 if (res != SUCCESS) {
303 IMAGE_LOGE("FinalizePacking failed %{public}d.", res);
304 }
305 return res;
306 }, false);
307 }
308
FinalizePacking(int64_t & packedSize)309 uint32_t ImagePacker::FinalizePacking(int64_t &packedSize)
310 {
311 uint32_t ret = FinalizePacking();
312 if (packerStream_ != nullptr) {
313 packerStream_->Flush();
314 }
315 packedSize = (packerStream_ != nullptr) ? packerStream_->BytesWritten() : 0;
316 return ret;
317 }
318
GetEncoder(PluginServer & pluginServer,std::string format)319 static ImagePlugin::AbsImageEncoder* GetEncoder(PluginServer &pluginServer, std::string format)
320 {
321 std::map<std::string, AttrData> capabilities;
322 capabilities.insert(std::map<std::string, AttrData>::value_type(IMAGE_ENCODE_FORMAT, AttrData(format)));
323 return pluginServer.CreateObject<AbsImageEncoder>(AbsImageEncoder::SERVICE_DEFAULT, capabilities);
324 }
325
GetEncoderPlugin(const PackOption & option)326 bool ImagePacker::GetEncoderPlugin(const PackOption &option)
327 {
328 encoders_.clear();
329 IMAGE_LOGD("GetEncoderPlugin current encoder plugin size %{public}zu.", encoders_.size());
330 auto encoder = GetEncoder(pluginServer_, EXTENDED_ENCODER);
331 if (encoder != nullptr) {
332 encoders_.emplace_back(std::unique_ptr<ImagePlugin::AbsImageEncoder>(encoder));
333 } else {
334 IMAGE_LOGE("GetEncoderPlugin get ext_encoder plugin failed.");
335 }
336 encoder = GetEncoder(pluginServer_, option.format);
337 if (encoder != nullptr) {
338 encoders_.emplace_back(std::unique_ptr<ImagePlugin::AbsImageEncoder>(encoder));
339 } else {
340 IMAGE_LOGD("GetEncoderPlugin get %{public}s plugin failed, use ext_encoder plugin",
341 option.format.c_str());
342 }
343 return encoders_.size() != SIZE_ZERO;
344 }
345
CopyOptionsToPlugin(const PackOption & opts,PlEncodeOptions & plOpts)346 void ImagePacker::CopyOptionsToPlugin(const PackOption &opts, PlEncodeOptions &plOpts)
347 {
348 plOpts.delayTimes = opts.delayTimes;
349 plOpts.loop = opts.loop;
350 plOpts.numberHint = opts.numberHint;
351 plOpts.quality = opts.quality;
352 plOpts.format = opts.format;
353 plOpts.disposalTypes = opts.disposalTypes;
354 plOpts.needsPackProperties = opts.needsPackProperties;
355 plOpts.desiredDynamicRange = opts.desiredDynamicRange;
356 plOpts.isEditScene = opts.isEditScene;
357 }
358
FreeOldPackerStream()359 void ImagePacker::FreeOldPackerStream()
360 {
361 if (packerStream_ != nullptr) {
362 packerStream_.reset();
363 }
364 }
365
IsPackOptionValid(const PackOption & option)366 bool ImagePacker::IsPackOptionValid(const PackOption &option)
367 {
368 return !(option.quality > QUALITY_MAX || option.format.empty());
369 }
370
DoEncodingFunc(std::function<uint32_t (ImagePlugin::AbsImageEncoder *)> func,bool forAll)371 uint32_t ImagePacker::DoEncodingFunc(std::function<uint32_t(ImagePlugin::AbsImageEncoder*)> func, bool forAll)
372 {
373 if (encoders_.size() == SIZE_ZERO) {
374 IMAGE_LOGE("DoEncodingFunc encoders is empty.");
375 return ERR_IMAGE_DECODE_ABNORMAL;
376 }
377 std::vector<uint32_t> rets;
378 rets.resize(SIZE_ZERO);
379 bool isSuccessOnce = false;
380 for (size_t i = SIZE_ZERO; i < encoders_.size(); i++) {
381 if (!forAll && isSuccessOnce) {
382 IMAGE_LOGD("DoEncodingFunc encoding successed, reset other encoder.");
383 encoders_.at(i).reset();
384 continue;
385 }
386 auto iterRes = func(encoders_.at(i).get());
387 rets.emplace_back(iterRes);
388 if (iterRes == SUCCESS) {
389 isSuccessOnce = true;
390 }
391 if (!forAll && !isSuccessOnce) {
392 IMAGE_LOGD("DoEncodingFunc failed.");
393 }
394 }
395 if (isSuccessOnce) {
396 return SUCCESS;
397 }
398 return (rets.size() == SIZE_ZERO)?ERR_IMAGE_DECODE_ABNORMAL:rets.front();
399 }
400
401 // class reference need explicit constructor and destructor, otherwise unique_ptr<T> use unnormal
ImagePacker()402 ImagePacker::ImagePacker()
403 {}
404
~ImagePacker()405 ImagePacker::~ImagePacker()
406 {}
407 } // namespace Media
408 } // namespace OHOS
409