• 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_trace.h"
23 #include "image_utils.h"
24 #include "media_errors.h"
25 #include "ostream_packer_stream.h"
26 #include "plugin_server.h"
27 #if defined(A_PLATFORM) || defined(IOS_PLATFORM)
28 #include "include/jpeg_encoder.h"
29 #endif
30 
31 #undef LOG_DOMAIN
32 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
33 
34 #undef LOG_TAG
35 #define LOG_TAG "ImagePacker"
36 
37 namespace OHOS {
38 namespace Media {
39 using namespace ImagePlugin;
40 using namespace MultimediaPlugin;
41 static constexpr uint8_t QUALITY_MAX = 100;
42 const static std::string EXTENDED_ENCODER = "image/extended";
43 static constexpr size_t SIZE_ZERO = 0;
44 
45 PluginServer &ImagePacker::pluginServer_ = ImageUtils::GetPluginServer();
46 
GetSupportedFormats(std::set<std::string> & formats)47 uint32_t ImagePacker::GetSupportedFormats(std::set<std::string> &formats)
48 {
49     formats.clear();
50     std::vector<ClassInfo> classInfos;
51     uint32_t ret =
52         pluginServer_.PluginServerGetClassInfo<AbsImageEncoder>(AbsImageEncoder::SERVICE_DEFAULT, classInfos);
53     if (ret != SUCCESS) {
54         IMAGE_LOGE("get class info from plugin server failed, ret:%{public}u.", ret);
55         return ret;
56     }
57     for (auto &info : classInfos) {
58         std::map<std::string, AttrData> &capbility = info.capabilities;
59         auto iter = capbility.find(IMAGE_ENCODE_FORMAT);
60         if (iter == capbility.end()) {
61             continue;
62         }
63         AttrData &attr = iter->second;
64         std::string format;
65         if (attr.GetValue(format) != SUCCESS) {
66             IMAGE_LOGE("attr data get format failed.");
67             continue;
68         }
69         formats.insert(format);
70     }
71     return SUCCESS;
72 }
73 
StartPackingImpl(const PackOption & option)74 uint32_t ImagePacker::StartPackingImpl(const PackOption &option)
75 {
76     if (packerStream_ == nullptr || packerStream_.get() == nullptr) {
77         IMAGE_LOGE("make buffer packer stream failed.");
78         return ERR_IMAGE_DATA_ABNORMAL;
79     }
80     if (!GetEncoderPlugin(option)) {
81         IMAGE_LOGE("StartPackingImpl get encoder plugin failed.");
82         return ERR_IMAGE_MISMATCHED_FORMAT;
83     }
84     PlEncodeOptions plOpts;
85     CopyOptionsToPlugin(option, plOpts);
86     return DoEncodingFunc([this, &plOpts](ImagePlugin::AbsImageEncoder* encoder) {
87         return encoder->StartEncode(*packerStream_.get(), plOpts);
88     });
89 }
90 
StartPacking(uint8_t * outputData,uint32_t maxSize,const PackOption & option)91 uint32_t ImagePacker::StartPacking(uint8_t *outputData, uint32_t maxSize, const PackOption &option)
92 {
93     ImageTrace imageTrace("ImagePacker::StartPacking by outputData");
94     if (!IsPackOptionValid(option)) {
95         IMAGE_LOGE("array startPacking option invalid %{public}s, %{public}u.", option.format.c_str(),
96             option.quality);
97         return ERR_IMAGE_INVALID_PARAMETER;
98     }
99 
100     if (outputData == nullptr) {
101         IMAGE_LOGE("output buffer is null.");
102         return ERR_IMAGE_INVALID_PARAMETER;
103     }
104     BufferPackerStream *stream = new (std::nothrow) BufferPackerStream(outputData, maxSize);
105     if (stream == nullptr) {
106         IMAGE_LOGE("make buffer packer stream failed.");
107         return ERR_IMAGE_DATA_ABNORMAL;
108     }
109     FreeOldPackerStream();
110     packerStream_ = std::unique_ptr<BufferPackerStream>(stream);
111     return StartPackingImpl(option);
112 }
113 
StartPacking(const std::string & filePath,const PackOption & option)114 uint32_t ImagePacker::StartPacking(const std::string &filePath, const PackOption &option)
115 {
116     ImageTrace imageTrace("ImagePacker::StartPacking by filePath");
117     if (!IsPackOptionValid(option)) {
118         IMAGE_LOGE("filepath startPacking option invalid %{public}s, %{public}u.", option.format.c_str(),
119             option.quality);
120         return ERR_IMAGE_INVALID_PARAMETER;
121     }
122     FilePackerStream *stream = new (std::nothrow) FilePackerStream(filePath);
123     if (stream == nullptr) {
124         IMAGE_LOGE("make file packer stream failed.");
125         return ERR_IMAGE_DATA_ABNORMAL;
126     }
127     FreeOldPackerStream();
128     packerStream_ = std::unique_ptr<FilePackerStream>(stream);
129     return StartPackingImpl(option);
130 }
131 
StartPacking(const int & fd,const PackOption & option)132 uint32_t ImagePacker::StartPacking(const int &fd, const PackOption &option)
133 {
134     ImageTrace imageTrace("ImagePacker::StartPacking by fd");
135     if (!IsPackOptionValid(option)) {
136         IMAGE_LOGE("fd startPacking option invalid %{public}s, %{public}u.", option.format.c_str(), option.quality);
137         return ERR_IMAGE_INVALID_PARAMETER;
138     }
139     FilePackerStream *stream = new (std::nothrow) FilePackerStream(fd);
140     if (stream == nullptr) {
141         IMAGE_LOGE("make file packer stream failed.");
142         return ERR_IMAGE_DATA_ABNORMAL;
143     }
144     FreeOldPackerStream();
145     packerStream_ = std::unique_ptr<FilePackerStream>(stream);
146     return StartPackingImpl(option);
147 }
148 
StartPacking(std::ostream & outputStream,const PackOption & option)149 uint32_t ImagePacker::StartPacking(std::ostream &outputStream, const PackOption &option)
150 {
151     ImageTrace imageTrace("ImagePacker::StartPacking by outputStream");
152     if (!IsPackOptionValid(option)) {
153         IMAGE_LOGE("outputStream startPacking option invalid %{public}s, %{public}u.", option.format.c_str(),
154             option.quality);
155         return ERR_IMAGE_INVALID_PARAMETER;
156     }
157     OstreamPackerStream *stream = new (std::nothrow) OstreamPackerStream(outputStream);
158     if (stream == nullptr) {
159         IMAGE_LOGE("make ostream packer stream failed.");
160         return ERR_IMAGE_DATA_ABNORMAL;
161     }
162     FreeOldPackerStream();
163     packerStream_ = std::unique_ptr<OstreamPackerStream>(stream);
164     return StartPackingImpl(option);
165 }
166 
167 // JNI adapter method, this method be called by jni and the outputStream be created by jni, here we manage the lifecycle
168 // of the outputStream
StartPackingAdapter(PackerStream & outputStream,const PackOption & option)169 uint32_t ImagePacker::StartPackingAdapter(PackerStream &outputStream, const PackOption &option)
170 {
171     FreeOldPackerStream();
172     packerStream_ = std::unique_ptr<PackerStream>(&outputStream);
173 
174     if (!IsPackOptionValid(option)) {
175         IMAGE_LOGE("packer stream option invalid %{public}s, %{public}u.", option.format.c_str(), option.quality);
176         return ERR_IMAGE_INVALID_PARAMETER;
177     }
178     return StartPackingImpl(option);
179 }
180 
AddImage(PixelMap & pixelMap)181 uint32_t ImagePacker::AddImage(PixelMap &pixelMap)
182 {
183     ImageUtils::DumpPixelMapBeforeEncode(pixelMap);
184     ImageTrace imageTrace("ImagePacker::AddImage by pixelMap");
185     return DoEncodingFunc([this, &pixelMap](ImagePlugin::AbsImageEncoder* encoder) {
186         return encoder->AddImage(pixelMap);
187     });
188 }
189 
AddImage(ImageSource & source)190 uint32_t ImagePacker::AddImage(ImageSource &source)
191 {
192     ImageTrace imageTrace("ImagePacker::AddImage by imageSource");
193     DecodeOptions opts;
194     uint32_t ret = SUCCESS;
195     if (pixelMap_ != nullptr) {
196         pixelMap_.reset();  // release old inner pixelmap
197     }
198     pixelMap_ = source.CreatePixelMap(opts, ret);
199     if (ret != SUCCESS) {
200         IMAGE_LOGE("image source create pixel map failed.");
201         return ret;
202     }
203     if (pixelMap_ == nullptr || pixelMap_.get() == nullptr) {
204         IMAGE_LOGE("create the pixel map unique_ptr fail.");
205         return ERR_IMAGE_MALLOC_ABNORMAL;
206     }
207     return AddImage(*pixelMap_.get());
208 }
209 
AddImage(ImageSource & source,uint32_t index)210 uint32_t ImagePacker::AddImage(ImageSource &source, uint32_t index)
211 {
212     ImageTrace imageTrace("ImagePacker::AddImage by imageSource and index:%{public}u", index);
213     DecodeOptions opts;
214     uint32_t ret = SUCCESS;
215     if (pixelMap_ != nullptr) {
216         pixelMap_.reset();  // release old inner pixelmap
217     }
218     pixelMap_ = source.CreatePixelMap(index, opts, ret);
219     if (ret != SUCCESS) {
220         IMAGE_LOGE("image source create pixel map failed.");
221         return ret;
222     }
223     if (pixelMap_ == nullptr || pixelMap_.get() == nullptr) {
224         IMAGE_LOGE("create the pixel map unique_ptr fail.");
225         return ERR_IMAGE_MALLOC_ABNORMAL;
226     }
227     return AddImage(*pixelMap_.get());
228 }
229 
FinalizePacking()230 uint32_t ImagePacker::FinalizePacking()
231 {
232     ImageTrace imageTrace("ImagePacker::FinalizePacking");
233     return DoEncodingFunc([](ImagePlugin::AbsImageEncoder* encoder) {
234         auto res = encoder->FinalizeEncode();
235         if (res != SUCCESS) {
236             IMAGE_LOGE("FinalizePacking failed %{public}d.", res);
237         }
238         return res;
239         }, false);
240 }
241 
FinalizePacking(int64_t & packedSize)242 uint32_t ImagePacker::FinalizePacking(int64_t &packedSize)
243 {
244     uint32_t ret = FinalizePacking();
245     if (packerStream_ != nullptr) {
246         packerStream_->Flush();
247     }
248     packedSize = (packerStream_ != nullptr) ? packerStream_->BytesWritten() : 0;
249     return ret;
250 }
251 
GetEncoder(PluginServer & pluginServer,std::string format)252 static ImagePlugin::AbsImageEncoder* GetEncoder(PluginServer &pluginServer, std::string format)
253 {
254     std::map<std::string, AttrData> capabilities;
255     capabilities.insert(std::map<std::string, AttrData>::value_type(IMAGE_ENCODE_FORMAT, AttrData(format)));
256     return pluginServer.CreateObject<AbsImageEncoder>(AbsImageEncoder::SERVICE_DEFAULT, capabilities);
257 }
258 
GetEncoderPlugin(const PackOption & option)259 bool ImagePacker::GetEncoderPlugin(const PackOption &option)
260 {
261     encoders_.clear();
262     IMAGE_LOGD("GetEncoderPlugin current encoder plugin size %{public}zu.", encoders_.size());
263     auto encoder = GetEncoder(pluginServer_, EXTENDED_ENCODER);
264     if (encoder != nullptr) {
265         encoders_.emplace_back(std::unique_ptr<ImagePlugin::AbsImageEncoder>(encoder));
266     } else {
267         IMAGE_LOGE("GetEncoderPlugin get ext_encoder plugin failed.");
268     }
269     encoder = GetEncoder(pluginServer_, option.format);
270     if (encoder != nullptr) {
271         encoders_.emplace_back(std::unique_ptr<ImagePlugin::AbsImageEncoder>(encoder));
272     } else {
273         IMAGE_LOGD("GetEncoderPlugin get %{public}s plugin failed, use ext_encoder plugin",
274             option.format.c_str());
275     }
276     return encoders_.size() != SIZE_ZERO;
277 }
278 
CopyOptionsToPlugin(const PackOption & opts,PlEncodeOptions & plOpts)279 void ImagePacker::CopyOptionsToPlugin(const PackOption &opts, PlEncodeOptions &plOpts)
280 {
281     plOpts.numberHint = opts.numberHint;
282     plOpts.quality = opts.quality;
283     plOpts.format = opts.format;
284 }
285 
FreeOldPackerStream()286 void ImagePacker::FreeOldPackerStream()
287 {
288     if (packerStream_ != nullptr) {
289         packerStream_.reset();
290     }
291 }
292 
IsPackOptionValid(const PackOption & option)293 bool ImagePacker::IsPackOptionValid(const PackOption &option)
294 {
295     return !(option.quality > QUALITY_MAX || option.format.empty());
296 }
297 
DoEncodingFunc(std::function<uint32_t (ImagePlugin::AbsImageEncoder *)> func,bool forAll)298 uint32_t ImagePacker::DoEncodingFunc(std::function<uint32_t(ImagePlugin::AbsImageEncoder*)> func, bool forAll)
299 {
300     if (encoders_.size() == SIZE_ZERO) {
301         IMAGE_LOGE("DoEncodingFunc encoders is empty.");
302         return ERR_IMAGE_DECODE_ABNORMAL;
303     }
304     std::vector<uint32_t> rets;
305     rets.resize(SIZE_ZERO);
306     bool isSuccessOnce = false;
307     for (size_t i = SIZE_ZERO; i < encoders_.size(); i++) {
308         if (!forAll && isSuccessOnce) {
309             IMAGE_LOGD("DoEncodingFunc encoding successed, reset other encoder.");
310             encoders_.at(i).reset();
311             continue;
312         }
313         auto iterRes = func(encoders_.at(i).get());
314         rets.emplace_back(iterRes);
315         if (iterRes == SUCCESS) {
316             isSuccessOnce = true;
317         }
318         if (!forAll && !isSuccessOnce) {
319             IMAGE_LOGD("DoEncodingFunc failed.");
320         }
321     }
322     if (isSuccessOnce) {
323         return SUCCESS;
324     }
325     return (rets.size() == SIZE_ZERO)?ERR_IMAGE_DECODE_ABNORMAL:rets.front();
326 }
327 
328 // class reference need explicit constructor and destructor, otherwise unique_ptr<T> use unnormal
ImagePacker()329 ImagePacker::ImagePacker()
330 {}
331 
~ImagePacker()332 ImagePacker::~ImagePacker()
333 {}
334 } // namespace Media
335 } // namespace OHOS
336