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 <cinttypes>
17 #include "image_log.h"
18 #include "image_utils.h"
19 #include "media_errors.h"
20 #include "native_image.h"
21
22 #undef LOG_DOMAIN
23 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
24
25 #undef LOG_TAG
26 #define LOG_TAG "NativeImage"
27
28 namespace {
29 constexpr int32_t NUMI_0 = 0;
30 constexpr uint32_t NUM_0 = 0;
31 constexpr uint32_t NUM_1 = 1;
32 constexpr uint32_t NUM_2 = 2;
33 const std::string DATA_SIZE_TAG = "dataSize";
34 }
35
36 namespace OHOS {
37 namespace Media {
NativeImage(sptr<SurfaceBuffer> buffer,std::shared_ptr<IBufferProcessor> releaser)38 NativeImage::NativeImage(sptr<SurfaceBuffer> buffer,
39 std::shared_ptr<IBufferProcessor> releaser) : buffer_(buffer), releaser_(releaser), timestamp_(0)
40 {}
41
NativeImage(sptr<SurfaceBuffer> buffer,std::shared_ptr<IBufferProcessor> releaser,int64_t timestamp)42 NativeImage::NativeImage(sptr<SurfaceBuffer> buffer, std::shared_ptr<IBufferProcessor> releaser,
43 int64_t timestamp) : buffer_(buffer), releaser_(releaser), timestamp_(timestamp)
44 {}
45
46 struct YUVData {
47 std::vector<uint8_t> y;
48 std::vector<uint8_t> u;
49 std::vector<uint8_t> v;
50 uint64_t ySize;
51 uint64_t uvSize;
52 };
53
DataSwap(uint8_t * a,uint8_t * b,bool flip)54 static inline void DataSwap(uint8_t* a, uint8_t* b, bool flip)
55 {
56 if (flip) {
57 *a = *b;
58 } else {
59 *b = *a;
60 }
61 }
62
IsYUV422SPFormat(int32_t format)63 static inline bool IsYUV422SPFormat(int32_t format)
64 {
65 if (format == int32_t(ImageFormat::YCBCR_422_SP) ||
66 format == int32_t(GRAPHIC_PIXEL_FMT_YCBCR_422_SP)) {
67 return true;
68 }
69 return false;
70 }
71
YUV422SPDataCopy(uint8_t * buffer,uint64_t size,YUVData & data,bool flip)72 static void YUV422SPDataCopy(uint8_t* buffer, uint64_t size, YUVData &data, bool flip)
73 {
74 uint64_t ui = NUM_0;
75 uint64_t vi = NUM_0;
76 for (uint64_t i = NUM_0; i < size; i++) {
77 if (i < data.ySize) {
78 DataSwap(&(buffer[i]), &(data.y[i]), flip);
79 continue;
80 }
81 if (vi >= data.uvSize || ui >= data.uvSize) {
82 // Over write buffer size.
83 continue;
84 }
85 if (i % NUM_2 == NUM_1) {
86 DataSwap(&(buffer[i]), &(data.v[vi]), flip);
87 vi++;
88 } else {
89 DataSwap(&(buffer[i]), &(data.u[ui]), flip);
90 ui++;
91 }
92 }
93 }
GetSurfaceBufferAddr()94 uint8_t* NativeImage::GetSurfaceBufferAddr()
95 {
96 if (buffer_ != nullptr) {
97 return static_cast<uint8_t*>(buffer_->GetVirAddr());
98 }
99 return nullptr;
100 }
SplitYUV422SPComponent()101 int32_t NativeImage::SplitYUV422SPComponent()
102 {
103 auto rawBuffer = GetSurfaceBufferAddr();
104 if (rawBuffer == nullptr) {
105 IMAGE_LOGE("SurfaceBuffer viraddr is nullptr");
106 return ERR_MEDIA_NULL_POINTER;
107 }
108
109 uint64_t surfaceSize = NUM_0;
110 auto res = GetDataSize(surfaceSize);
111 if (res != SUCCESS || surfaceSize == NUM_0) {
112 IMAGE_LOGE("S size is 0");
113 return ERR_MEDIA_DATA_UNSUPPORT;
114 }
115
116 int32_t width = NUM_0;
117 int32_t height = NUM_0;
118 res = GetSize(width, height);
119 if (res != SUCCESS || width <= NUMI_0 || height <= NUMI_0) {
120 IMAGE_LOGE("Invaild width %{public}" PRId32 " height %{public}" PRId32, width, height);
121 return ERR_MEDIA_DATA_UNSUPPORT;
122 }
123
124 struct YUVData yuv;
125 uint64_t uvStride = static_cast<uint64_t>((width + NUM_1) / NUM_2);
126 if (ImageUtils::CheckMulOverflow(width, height)) {
127 IMAGE_LOGE("Invalid width %{public}" PRId32 " height %{public}" PRId32, width, height);
128 return ERR_MEDIA_DATA_UNSUPPORT;
129 }
130 yuv.ySize = static_cast<uint64_t>(width) * static_cast<uint64_t>(height);
131 yuv.uvSize = static_cast<uint64_t>(height) * uvStride;
132 if (surfaceSize < (yuv.ySize + yuv.uvSize * NUM_2)) {
133 IMAGE_LOGE("S size %{public}" PRIu64 " < y plane %{public}" PRIu64
134 " + uv plane %{public}" PRIu64, surfaceSize, yuv.ySize, yuv.uvSize * NUM_2);
135 return ERR_MEDIA_DATA_UNSUPPORT;
136 }
137
138 NativeComponent* y = CreateComponent(int32_t(ComponentType::YUV_Y), yuv.ySize, width, NUM_1, nullptr);
139 NativeComponent* u = CreateComponent(int32_t(ComponentType::YUV_U), yuv.uvSize, uvStride, NUM_2, nullptr);
140 NativeComponent* v = CreateComponent(int32_t(ComponentType::YUV_V), yuv.uvSize, uvStride, NUM_2, nullptr);
141 if ((y == nullptr) || (u == nullptr) || (v == nullptr)) {
142 IMAGE_LOGE("Create Component failed");
143 return ERR_MEDIA_DATA_UNSUPPORT;
144 }
145 yuv.y = y->raw;
146 yuv.u = u->raw;
147 yuv.v = v->raw;
148 YUV422SPDataCopy(rawBuffer, surfaceSize, yuv, false);
149 return SUCCESS;
150 }
151
SplitSurfaceToComponent()152 int32_t NativeImage::SplitSurfaceToComponent()
153 {
154 int32_t format = NUM_0;
155 auto res = GetFormat(format);
156 if (res != SUCCESS) {
157 return res;
158 }
159 switch (format) {
160 case int32_t(ImageFormat::YCBCR_422_SP):
161 case int32_t(GRAPHIC_PIXEL_FMT_YCBCR_422_SP):
162 return SplitYUV422SPComponent();
163 case int32_t(ImageFormat::JPEG):
164 if (CreateCombineComponent(int32_t(ComponentType::JPEG)) != nullptr) {
165 return SUCCESS;
166 }
167 }
168 // Unsupport split component
169 return ERR_MEDIA_DATA_UNSUPPORT;
170 }
171
CombineYUVComponents()172 int32_t NativeImage::CombineYUVComponents()
173 {
174 int32_t format = NUM_0;
175 auto res = GetFormat(format);
176 if (res != SUCCESS) {
177 return res;
178 }
179 if (!IsYUV422SPFormat(format)) {
180 IMAGE_LOGI("No need to combine components for NO YUV format now");
181 return SUCCESS;
182 }
183
184 auto y = GetComponent(int32_t(ComponentType::YUV_Y));
185 auto u = GetComponent(int32_t(ComponentType::YUV_U));
186 auto v = GetComponent(int32_t(ComponentType::YUV_V));
187 if ((y == nullptr) || (u == nullptr) || (v == nullptr)) {
188 IMAGE_LOGE("No component need to combine");
189 return ERR_MEDIA_DATA_UNSUPPORT;
190 }
191 YUVData data;
192 data.ySize = y->raw.size();
193 data.uvSize = u->raw.size();
194 data.y = y->raw;
195 data.u = u->raw;
196 data.v = v->raw;
197
198 uint64_t bufferSize = NUM_0;
199 GetDataSize(bufferSize);
200
201 YUV422SPDataCopy(GetSurfaceBufferAddr(), bufferSize, data, true);
202 return SUCCESS;
203 }
204
BuildComponent(size_t size,int32_t row,int32_t pixel,uint8_t * vir)205 static std::unique_ptr<NativeComponent> BuildComponent(size_t size, int32_t row, int32_t pixel, uint8_t* vir)
206 {
207 if (size == NUM_0 && vir == nullptr) {
208 IMAGE_LOGE("Could't create 0 size component data");
209 return nullptr;
210 }
211 std::unique_ptr<NativeComponent> component = std::make_unique<NativeComponent>();
212 component->pixelStride = pixel;
213 component->rowStride = row;
214 component->size = size;
215 if (vir != nullptr) {
216 component->virAddr = vir;
217 } else {
218 component->raw.resize(size);
219 }
220 return component;
221 }
222
GetCachedComponent(int32_t type)223 NativeComponent* NativeImage::GetCachedComponent(int32_t type)
224 {
225 auto iter = components_.find(type);
226 if (iter != components_.end()) {
227 return iter->second.get();
228 }
229 return nullptr;
230 }
231
CreateComponent(int32_t type,size_t size,int32_t row,int32_t pixel,uint8_t * vir)232 NativeComponent* NativeImage::CreateComponent(int32_t type, size_t size, int32_t row,
233 int32_t pixel, uint8_t* vir)
234 {
235 NativeComponent* res = GetCachedComponent(type);
236 if (res != nullptr) {
237 IMAGE_LOGI("Component %{public}d already exist. No need create", type);
238 return res;
239 }
240
241 std::unique_ptr<NativeComponent> component = BuildComponent(size, row, pixel, vir);
242 if (component == nullptr) {
243 return nullptr;
244 }
245 components_.insert(std::map<int32_t, std::unique_ptr<NativeComponent>>::value_type(type,
246 std::move(component)));
247
248 return GetCachedComponent(type);
249 }
250
CreateCombineComponent(int32_t type)251 NativeComponent* NativeImage::CreateCombineComponent(int32_t type)
252 {
253 uint64_t size = NUM_0;
254 GetDataSize(size);
255 return CreateComponent(type, static_cast<size_t>(size), buffer_->GetStride(), NUM_1, GetSurfaceBufferAddr());
256 }
GetSize(int32_t & width,int32_t & height)257 int32_t NativeImage::GetSize(int32_t &width, int32_t &height)
258 {
259 if (buffer_ == nullptr) {
260 return ERR_MEDIA_DEAD_OBJECT;
261 }
262 width = buffer_->GetWidth();
263 height = buffer_->GetHeight();
264 return SUCCESS;
265 }
266
GetDataSize(uint64_t & size)267 int32_t NativeImage::GetDataSize(uint64_t &size)
268 {
269 if (buffer_ == nullptr) {
270 return ERR_MEDIA_DEAD_OBJECT;
271 }
272
273 size = static_cast<uint64_t>(buffer_->GetSize());
274 auto extraData = buffer_->GetExtraData();
275 if (extraData == nullptr) {
276 IMAGE_LOGI("Nullptr s extra data. return buffer size %{public}" PRIu64, size);
277 return SUCCESS;
278 }
279
280 int32_t extraDataSize = NUMI_0;
281 auto res = extraData->ExtraGet(DATA_SIZE_TAG, extraDataSize);
282 if (res != NUM_0) {
283 IMAGE_LOGI("S ExtraGet dataSize error %{public}d", res);
284 } else if (extraDataSize <= NUMI_0) {
285 IMAGE_LOGI("S ExtraGet dataSize Ok, but size <= 0");
286 } else if (static_cast<uint64_t>(extraDataSize) > size) {
287 IMAGE_LOGI("S ExtraGet dataSize Ok,but dataSize %{public}d is bigger than bufferSize %{public}" PRIu64,
288 extraDataSize, size);
289 } else {
290 IMAGE_LOGD("S ExtraGet dataSize %{public}d", extraDataSize);
291 size = extraDataSize;
292 }
293 return SUCCESS;
294 }
295
GetFormat(int32_t & format)296 int32_t NativeImage::GetFormat(int32_t &format)
297 {
298 if (buffer_ == nullptr) {
299 return ERR_MEDIA_DEAD_OBJECT;
300 }
301 format = buffer_->GetFormat();
302 return SUCCESS;
303 }
304
GetTimestamp(int64_t & timestamp)305 int32_t NativeImage::GetTimestamp(int64_t ×tamp)
306 {
307 if (buffer_ == nullptr) {
308 return ERR_MEDIA_DEAD_OBJECT;
309 }
310 timestamp = timestamp_;
311 return SUCCESS;
312 }
313
GetComponent(int32_t type)314 NativeComponent* NativeImage::GetComponent(int32_t type)
315 {
316 if (buffer_ == nullptr) {
317 return nullptr;
318 }
319
320 // Find type if it has exist.
321 auto component = GetCachedComponent(type);
322 if (component != nullptr) {
323 return component;
324 }
325
326 int32_t format = NUM_0;
327 if (GetFormat(format) == SUCCESS && type == format) {
328 return CreateCombineComponent(type);
329 }
330 SplitSurfaceToComponent();
331 // Try again
332 component = GetCachedComponent(type);
333
334 #ifdef COMPONENT_STRICT_CHECK
335 return component;
336 #else // We don't check the input type anymore, return raw format component!!
337 if (component == nullptr && GetFormat(format) == SUCCESS) {
338 return CreateCombineComponent(format);
339 }
340 return nullptr;
341 #endif
342 }
343
release()344 void NativeImage::release()
345 {
346 if (buffer_ == nullptr) {
347 return;
348 }
349 IMAGE_LOGD("NativeImage release");
350 if (components_.size() > 0) {
351 components_.clear();
352 }
353 if (releaser_ != nullptr && buffer_ != nullptr) {
354 releaser_->BufferRelease(buffer_);
355 }
356 releaser_ = nullptr;
357 buffer_ = nullptr;
358 }
359 } // namespace Media
360 } // namespace OHOS
361