• 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_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     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     DecodeOptions decodeOpts;
246     decodeOpts.desiredDynamicRange = encodeToSdr_ ? DecodeDynamicRange::SDR : DecodeDynamicRange::AUTO;
247     uint32_t ret = SUCCESS;
248     if (pixelMap_ != nullptr) {
249         pixelMap_.reset();  // release old inner pixelmap
250     }
251     pixelMap_ = source.CreatePixelMap(decodeOpts, ret);
252     if (ret != SUCCESS) {
253         IMAGE_LOGE("image source create pixel map failed.");
254         return ret;
255     }
256 
257     if (pixelMap_ == nullptr || pixelMap_.get() == nullptr) {
258         IMAGE_LOGE("create the pixel map unique_ptr fail.");
259         return ERR_IMAGE_MALLOC_ABNORMAL;
260     }
261 
262     return AddImage(*pixelMap_.get());
263 }
264 
AddImage(ImageSource & source,uint32_t index)265 uint32_t ImagePacker::AddImage(ImageSource &source, uint32_t index)
266 {
267     ImageTrace imageTrace("ImagePacker::AddImage by imageSource and index %{public}u", index);
268     DecodeOptions decodeOpts;
269     decodeOpts.desiredDynamicRange = encodeToSdr_ ? DecodeDynamicRange::SDR : DecodeDynamicRange::AUTO;
270     uint32_t ret = SUCCESS;
271     if (pixelMap_ != nullptr) {
272         pixelMap_.reset();  // release old inner pixelmap
273     }
274     pixelMap_ = source.CreatePixelMap(index, decodeOpts, ret);
275     if (ret != SUCCESS) {
276         IMAGE_LOGE("image source create pixel map failed.");
277         return ret;
278     }
279     bool cond = pixelMap_ == nullptr || pixelMap_.get() == nullptr;
280     CHECK_ERROR_RETURN_RET_LOG(cond, ERR_IMAGE_MALLOC_ABNORMAL, "create the pixel map unique_ptr fail.");
281 
282     return AddImage(*pixelMap_.get());
283 }
284 
285 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
AddPicture(Picture & picture)286 uint32_t ImagePacker::AddPicture(Picture &picture)
287 {
288     Picture::DumpPictureIfDumpEnabled(picture, "picture_encode_before");
289     return DoEncodingFunc([this, &picture](ImagePlugin::AbsImageEncoder* encoder) {
290         return encoder->AddPicture(picture);
291     });
292 }
293 #endif
294 
FinalizePacking()295 uint32_t ImagePacker::FinalizePacking()
296 {
297     return DoEncodingFunc([](ImagePlugin::AbsImageEncoder* encoder) {
298         auto res = encoder->FinalizeEncode();
299         if (res != SUCCESS) {
300             IMAGE_LOGE("FinalizePacking failed %{public}d.", res);
301         }
302         return res;
303         }, false);
304 }
305 
FinalizePacking(int64_t & packedSize)306 uint32_t ImagePacker::FinalizePacking(int64_t &packedSize)
307 {
308     uint32_t ret = FinalizePacking();
309     if (packerStream_ != nullptr) {
310         packerStream_->Flush();
311     }
312     packedSize = (packerStream_ != nullptr) ? packerStream_->BytesWritten() : 0;
313     return ret;
314 }
315 
GetEncoder(PluginServer & pluginServer,std::string format)316 static ImagePlugin::AbsImageEncoder* GetEncoder(PluginServer &pluginServer, std::string format)
317 {
318     std::map<std::string, AttrData> capabilities;
319     capabilities.insert(std::map<std::string, AttrData>::value_type(IMAGE_ENCODE_FORMAT, AttrData(format)));
320     return pluginServer.CreateObject<AbsImageEncoder>(AbsImageEncoder::SERVICE_DEFAULT, capabilities);
321 }
322 
GetEncoderPlugin(const PackOption & option)323 bool ImagePacker::GetEncoderPlugin(const PackOption &option)
324 {
325     encoders_.clear();
326     IMAGE_LOGD("GetEncoderPlugin current encoder plugin size %{public}zu.", encoders_.size());
327     auto encoder = GetEncoder(pluginServer_, EXTENDED_ENCODER);
328     if (encoder != nullptr) {
329         encoders_.emplace_back(std::unique_ptr<ImagePlugin::AbsImageEncoder>(encoder));
330     } else {
331         IMAGE_LOGE("GetEncoderPlugin get ext_encoder plugin failed.");
332     }
333     encoder = GetEncoder(pluginServer_, option.format);
334     if (encoder != nullptr) {
335         encoders_.emplace_back(std::unique_ptr<ImagePlugin::AbsImageEncoder>(encoder));
336     } else {
337         IMAGE_LOGD("GetEncoderPlugin get %{public}s plugin failed, use ext_encoder plugin",
338             option.format.c_str());
339     }
340     return encoders_.size() != SIZE_ZERO;
341 }
342 
CopyOptionsToPlugin(const PackOption & opts,PlEncodeOptions & plOpts)343 void ImagePacker::CopyOptionsToPlugin(const PackOption &opts, PlEncodeOptions &plOpts)
344 {
345     plOpts.delayTimes = opts.delayTimes;
346     plOpts.loop = opts.loop;
347     plOpts.numberHint = opts.numberHint;
348     plOpts.quality = opts.quality;
349     plOpts.format = opts.format;
350     plOpts.disposalTypes = opts.disposalTypes;
351     plOpts.needsPackProperties = opts.needsPackProperties;
352     plOpts.desiredDynamicRange = opts.desiredDynamicRange;
353     plOpts.isEditScene = opts.isEditScene;
354 }
355 
FreeOldPackerStream()356 void ImagePacker::FreeOldPackerStream()
357 {
358     if (packerStream_ != nullptr) {
359         packerStream_.reset();
360     }
361 }
362 
IsPackOptionValid(const PackOption & option)363 bool ImagePacker::IsPackOptionValid(const PackOption &option)
364 {
365     return !(option.quality > QUALITY_MAX || option.format.empty());
366 }
367 
DoEncodingFunc(std::function<uint32_t (ImagePlugin::AbsImageEncoder *)> func,bool forAll)368 uint32_t ImagePacker::DoEncodingFunc(std::function<uint32_t(ImagePlugin::AbsImageEncoder*)> func, bool forAll)
369 {
370     if (encoders_.size() == SIZE_ZERO) {
371         IMAGE_LOGE("DoEncodingFunc encoders is empty.");
372         return ERR_IMAGE_DECODE_ABNORMAL;
373     }
374     std::vector<uint32_t> rets;
375     rets.resize(SIZE_ZERO);
376     bool isSuccessOnce = false;
377     for (size_t i = SIZE_ZERO; i < encoders_.size(); i++) {
378         if (!forAll && isSuccessOnce) {
379             IMAGE_LOGD("DoEncodingFunc encoding successed, reset other encoder.");
380             encoders_.at(i).reset();
381             continue;
382         }
383         auto iterRes = func(encoders_.at(i).get());
384         rets.emplace_back(iterRes);
385         if (iterRes == SUCCESS) {
386             isSuccessOnce = true;
387         }
388         if (!forAll && !isSuccessOnce) {
389             IMAGE_LOGD("DoEncodingFunc failed.");
390         }
391     }
392     if (isSuccessOnce) {
393         return SUCCESS;
394     }
395     return (rets.size() == SIZE_ZERO)?ERR_IMAGE_DECODE_ABNORMAL:rets.front();
396 }
397 
398 // class reference need explicit constructor and destructor, otherwise unique_ptr<T> use unnormal
ImagePacker()399 ImagePacker::ImagePacker()
400 {}
401 
~ImagePacker()402 ImagePacker::~ImagePacker()
403 {}
404 } // namespace Media
405 } // namespace OHOS
406