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_1/icodec_image.h"
34 #include "image/v2_1/codec_image_type.h"
35 #include "v4_0/codec_types.h"
36 #include "v4_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_1::ICodecImage> image =
70 HDI::Codec::Image::V2_1::ICodecImage::Get(false);
71 if (image == nullptr) {
72 return false;
73 }
74 std::vector<HDI::Codec::Image::V2_1::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_1::CODEC_IMAGE_HEIF &&
81 cap.type == HDI::Codec::Image::V2_1::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 bool cond = (stream == nullptr);
194 CHECK_ERROR_RETURN_RET_LOG(cond, ERR_IMAGE_DATA_ABNORMAL, "make file packer stream failed");
195 FreeOldPackerStream();
196 packerStream_ = std::unique_ptr<FilePackerStream>(stream);
197 return StartPackingImpl(option);
198 }
199
StartPacking(std::ostream & outputStream,const PackOption & option)200 uint32_t ImagePacker::StartPacking(std::ostream &outputStream, const PackOption &option)
201 {
202 ImageTrace imageTrace("ImagePacker::StartPacking by outputStream");
203 if (!IsPackOptionValid(option)) {
204 IMAGE_LOGE("outputStream startPacking option invalid %{public}s, %{public}u.", option.format.c_str(),
205 option.quality);
206 return ERR_IMAGE_INVALID_PARAMETER;
207 }
208 OstreamPackerStream *stream = new (std::nothrow) OstreamPackerStream(outputStream);
209 if (stream == nullptr) {
210 IMAGE_LOGE("make ostream packer stream failed.");
211 return ERR_IMAGE_DATA_ABNORMAL;
212 }
213 FreeOldPackerStream();
214 packerStream_ = std::unique_ptr<OstreamPackerStream>(stream);
215 return StartPackingImpl(option);
216 }
217
218 // JNI adapter method, this method be called by jni and the outputStream be created by jni, here we manage the lifecycle
219 // of the outputStream
StartPackingAdapter(PackerStream & outputStream,const PackOption & option)220 uint32_t ImagePacker::StartPackingAdapter(PackerStream &outputStream, const PackOption &option)
221 {
222 FreeOldPackerStream();
223 packerStream_ = std::unique_ptr<PackerStream>(&outputStream);
224
225 bool cond = !IsPackOptionValid(option);
226 CHECK_ERROR_RETURN_RET_LOG(cond, ERR_IMAGE_INVALID_PARAMETER,
227 "packer stream option invalid %{public}s, %{public}u.",
228 option.format.c_str(), option.quality);
229 return StartPackingImpl(option);
230 }
231
AddImage(PixelMap & pixelMap)232 uint32_t ImagePacker::AddImage(PixelMap &pixelMap)
233 {
234 ImageUtils::DumpPixelMapBeforeEncode(pixelMap);
235 ImageTrace imageTrace("ImagePacker::AddImage by pixelMap");
236
237 return DoEncodingFunc([this, &pixelMap](ImagePlugin::AbsImageEncoder* encoder) {
238 return encoder->AddImage(pixelMap);
239 });
240 }
241
AddImage(ImageSource & source)242 uint32_t ImagePacker::AddImage(ImageSource &source)
243 {
244 ImageTrace imageTrace("ImagePacker::AddImage by imageSource");
245 return AddImage(source, 0);
246 }
247
AddImage(ImageSource & source,uint32_t index)248 uint32_t ImagePacker::AddImage(ImageSource &source, uint32_t index)
249 {
250 ImageTrace imageTrace("ImagePacker::AddImage by imageSource and index %u", index);
251 uint32_t ret = SUCCESS;
252 DecodeOptions decodeOpts;
253 decodeOpts.desiredDynamicRange = encodeToSdr_ ? DecodeDynamicRange::SDR : DecodeDynamicRange::AUTO;
254 bool isHDR = source.IsDecodeHdrImage(decodeOpts);
255 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
256 if (isHDR) {
257 if (picture_ != nullptr) {
258 picture_.reset(); // release old inner picture
259 }
260 DecodingOptionsForPicture decodeOptsForPicture;
261 decodeOptsForPicture.desiredPixelFormat = PixelFormat::NV12;
262 decodeOptsForPicture.desireAuxiliaryPictures = { AuxiliaryPictureType::GAINMAP };
263 picture_ = source.CreatePicture(decodeOptsForPicture, ret);
264 if (ret == SUCCESS && picture_ != nullptr && picture_.get() != nullptr) {
265 IMAGE_LOGD("image source create picture success.");
266 return AddPicture(*picture_.get());
267 }
268 }
269 #endif
270 if (pixelMap_ != nullptr) {
271 pixelMap_.reset(); // release old inner pixelmap
272 }
273 pixelMap_ = source.CreatePixelMapEx(index, decodeOpts, ret);
274 if (ret != SUCCESS) {
275 IMAGE_LOGE("image source create pixel map failed.");
276 return ret;
277 }
278 bool cond = pixelMap_ == nullptr || pixelMap_.get() == nullptr;
279 CHECK_ERROR_RETURN_RET_LOG(cond, ERR_IMAGE_MALLOC_ABNORMAL, "create the pixel map unique_ptr fail.");
280
281 return AddImage(*pixelMap_.get());
282 }
283
284 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
AddPicture(Picture & picture)285 uint32_t ImagePacker::AddPicture(Picture &picture)
286 {
287 Picture::DumpPictureIfDumpEnabled(picture, "picture_encode_before");
288 return DoEncodingFunc([this, &picture](ImagePlugin::AbsImageEncoder* encoder) {
289 return encoder->AddPicture(picture);
290 });
291 }
292 #endif
293
FinalizePacking()294 uint32_t ImagePacker::FinalizePacking()
295 {
296 return DoEncodingFunc([](ImagePlugin::AbsImageEncoder* encoder) {
297 auto res = encoder->FinalizeEncode();
298 if (res != SUCCESS) {
299 IMAGE_LOGE("FinalizePacking failed %{public}d.", res);
300 }
301 return res;
302 }, false);
303 }
304
FinalizePacking(int64_t & packedSize)305 uint32_t ImagePacker::FinalizePacking(int64_t &packedSize)
306 {
307 uint32_t ret = FinalizePacking();
308 if (packerStream_ != nullptr) {
309 packerStream_->Flush();
310 }
311 packedSize = (packerStream_ != nullptr) ? packerStream_->BytesWritten() : 0;
312 return ret;
313 }
314
GetEncoder(PluginServer & pluginServer,std::string format)315 static ImagePlugin::AbsImageEncoder* GetEncoder(PluginServer &pluginServer, std::string format)
316 {
317 std::map<std::string, AttrData> capabilities;
318 capabilities.insert(std::map<std::string, AttrData>::value_type(IMAGE_ENCODE_FORMAT, AttrData(format)));
319 return pluginServer.CreateObject<AbsImageEncoder>(AbsImageEncoder::SERVICE_DEFAULT, capabilities);
320 }
321
GetEncoderPlugin(const PackOption & option)322 bool ImagePacker::GetEncoderPlugin(const PackOption &option)
323 {
324 encoders_.clear();
325 IMAGE_LOGD("GetEncoderPlugin current encoder plugin size %{public}zu.", encoders_.size());
326 auto encoder = GetEncoder(pluginServer_, EXTENDED_ENCODER);
327 if (encoder != nullptr) {
328 encoders_.emplace_back(std::unique_ptr<ImagePlugin::AbsImageEncoder>(encoder));
329 } else {
330 IMAGE_LOGE("GetEncoderPlugin get ext_encoder plugin failed.");
331 }
332 encoder = GetEncoder(pluginServer_, option.format);
333 if (encoder != nullptr) {
334 encoders_.emplace_back(std::unique_ptr<ImagePlugin::AbsImageEncoder>(encoder));
335 } else {
336 IMAGE_LOGD("GetEncoderPlugin get %{public}s plugin failed, use ext_encoder plugin",
337 option.format.c_str());
338 }
339 return encoders_.size() != SIZE_ZERO;
340 }
341
CopyOptionsToPlugin(const PackOption & opts,PlEncodeOptions & plOpts)342 void ImagePacker::CopyOptionsToPlugin(const PackOption &opts, PlEncodeOptions &plOpts)
343 {
344 plOpts.delayTimes = opts.delayTimes;
345 plOpts.loop = opts.loop;
346 plOpts.numberHint = opts.numberHint;
347 plOpts.quality = opts.quality;
348 plOpts.format = opts.format;
349 plOpts.disposalTypes = opts.disposalTypes;
350 plOpts.needsPackProperties = opts.needsPackProperties;
351 plOpts.desiredDynamicRange = opts.desiredDynamicRange;
352 plOpts.isEditScene = opts.isEditScene;
353 }
354
FreeOldPackerStream()355 void ImagePacker::FreeOldPackerStream()
356 {
357 if (packerStream_ != nullptr) {
358 packerStream_.reset();
359 }
360 }
361
IsPackOptionValid(const PackOption & option)362 bool ImagePacker::IsPackOptionValid(const PackOption &option)
363 {
364 return !(option.quality > QUALITY_MAX || option.format.empty());
365 }
366
DoEncodingFunc(std::function<uint32_t (ImagePlugin::AbsImageEncoder *)> func,bool forAll)367 uint32_t ImagePacker::DoEncodingFunc(std::function<uint32_t(ImagePlugin::AbsImageEncoder*)> func, bool forAll)
368 {
369 if (encoders_.size() == SIZE_ZERO) {
370 IMAGE_LOGE("DoEncodingFunc encoders is empty.");
371 return ERR_IMAGE_DECODE_ABNORMAL;
372 }
373 std::vector<uint32_t> rets;
374 rets.resize(SIZE_ZERO);
375 bool isSuccessOnce = false;
376 for (size_t i = SIZE_ZERO; i < encoders_.size(); i++) {
377 if (!forAll && isSuccessOnce) {
378 IMAGE_LOGD("DoEncodingFunc encoding successed, reset other encoder.");
379 encoders_.at(i).reset();
380 continue;
381 }
382 auto iterRes = func(encoders_.at(i).get());
383 rets.emplace_back(iterRes);
384 if (iterRes == SUCCESS) {
385 isSuccessOnce = true;
386 }
387 if (!forAll && !isSuccessOnce) {
388 IMAGE_LOGD("DoEncodingFunc failed.");
389 }
390 }
391 if (isSuccessOnce) {
392 return SUCCESS;
393 }
394 return (rets.size() == SIZE_ZERO)?ERR_IMAGE_DECODE_ABNORMAL:rets.front();
395 }
396
397 // class reference need explicit constructor and destructor, otherwise unique_ptr<T> use unnormal
ImagePacker()398 ImagePacker::ImagePacker()
399 {}
400
~ImagePacker()401 ImagePacker::~ImagePacker()
402 {}
403 } // namespace Media
404 } // namespace OHOS
405