• 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 "avsession_pixel_map_adapter.h"
17 #include "avsession_log.h"
18 
19 namespace OHOS::AVSession {
20 namespace {
21     constexpr int32_t MAX_PIXEL_BUFFER_SIZE = 200 * 1024;
22     constexpr uint8_t IMAGE_BYTE_SIZE = 2;
23     constexpr uint8_t DATA_BYTE_SIZE = 4;
24     constexpr uint8_t OFFSET_BYTE = 8;
25 }
26 
27 int32_t AVSessionPixelMapAdapter::originalPixelMapBytes_ = 0;
28 int32_t AVSessionPixelMapAdapter::originalWidth_ = 0;
29 int32_t AVSessionPixelMapAdapter::originalHeight_ = 0;
30 
ConvertFromInner(const std::shared_ptr<AVSessionPixelMap> & innerPixelMap)31 std::shared_ptr<Media::PixelMap> AVSessionPixelMapAdapter::ConvertFromInner(
32     const std::shared_ptr<AVSessionPixelMap>& innerPixelMap)
33 {
34     CHECK_AND_RETURN_RET_LOG(innerPixelMap != nullptr, nullptr, "invalid parameter");
35     CHECK_AND_RETURN_RET_LOG(innerPixelMap->GetInnerImgBuffer().size() > IMAGE_BYTE_SIZE, nullptr,
36         "innerPixelMap innerImgBuffer size less than 2");
37 
38     std::vector<uint8_t> innerImgBuffer = innerPixelMap->GetInnerImgBuffer();
39     innerPixelMap->Clear();
40     uint16_t imgBufferSize = static_cast<uint16_t>(innerImgBuffer[0]);
41     imgBufferSize = (imgBufferSize << OFFSET_BYTE) + innerImgBuffer[1];
42 
43     if (imgBufferSize > sizeof(Media::ImageInfo)) {
44         SLOGE("imgBufferSize greater than %{public}zu", sizeof(Media::ImageInfo));
45         return nullptr;
46     }
47 
48     Media::ImageInfo imageInfo;
49     std::copy(innerImgBuffer.data() + IMAGE_BYTE_SIZE,
50         innerImgBuffer.data() + IMAGE_BYTE_SIZE + imgBufferSize, reinterpret_cast<uint8_t*>(&imageInfo));
51 
52     const std::shared_ptr<Media::PixelMap>& pixelMap = std::make_shared<Media::PixelMap>();
53     pixelMap->SetImageInfo(imageInfo);
54 
55     uint32_t dataSize = 0;
56     for (uint8_t i = 0; i < DATA_BYTE_SIZE; i++) {
57         uint32_t tmpValue = innerImgBuffer[IMAGE_BYTE_SIZE + imgBufferSize + i];
58         dataSize += (tmpValue << (OFFSET_BYTE * (DATA_BYTE_SIZE - i - 1)));
59     }
60     void* dataAddr = static_cast<void*>(innerImgBuffer.data() + IMAGE_BYTE_SIZE + imgBufferSize + DATA_BYTE_SIZE);
61     pixelMap->SetPixelsAddr(dataAddr, nullptr, dataSize, Media::AllocatorType::CUSTOM_ALLOC, nullptr);
62 
63     Media::InitializationOptions options;
64     options.alphaType = imageInfo.alphaType;
65     options.pixelFormat = imageInfo.pixelFormat;
66     options.scaleMode = OHOS::Media::ScaleMode::CENTER_CROP;
67     options.size.width = originalWidth_;
68     options.size.height = originalHeight_;
69     options.editable = true;
70     auto result = Media::PixelMap::Create(*pixelMap, options);
71     if (originalPixelMapBytes_ > MAX_PIXEL_BUFFER_SIZE) {
72         float scaleRatio = static_cast<float>(originalPixelMapBytes_) / static_cast<float>(MAX_PIXEL_BUFFER_SIZE);
73         pixelMap->scale(scaleRatio, scaleRatio);
74     }
75     return std::move(result);
76 }
77 
ConvertToInner(const std::shared_ptr<Media::PixelMap> & pixelMap)78 std::shared_ptr<AVSessionPixelMap> AVSessionPixelMapAdapter::ConvertToInner(
79     const std::shared_ptr<Media::PixelMap>& pixelMap)
80 {
81     CHECK_AND_RETURN_RET_LOG(pixelMap != nullptr, nullptr, "invalid parameter");
82     originalPixelMapBytes_ = pixelMap->GetByteCount();
83     originalWidth_ = pixelMap->GetWidth();
84     originalHeight_ = pixelMap->GetHeight();
85     if (originalPixelMapBytes_ > MAX_PIXEL_BUFFER_SIZE) {
86         float scaleRatio = static_cast<float>(MAX_PIXEL_BUFFER_SIZE) / static_cast<float>(originalPixelMapBytes_);
87         pixelMap->scale(scaleRatio, scaleRatio);
88     }
89     std::shared_ptr<AVSessionPixelMap> innerPixelMap = std::make_shared<AVSessionPixelMap>();
90     std::vector<uint8_t> imgBuffer;
91     Media::ImageInfo imageInfo;
92     pixelMap->GetImageInfo(imageInfo);
93     const auto* buffer = reinterpret_cast<uint8_t*>(&imageInfo);
94     uint16_t imageInfoSize = static_cast<uint16_t>(sizeof(Media::ImageInfo));
95     int32_t pixelDataSize = pixelMap->GetByteCount();
96     size_t bufferSize = IMAGE_BYTE_SIZE + imageInfoSize + DATA_BYTE_SIZE + pixelDataSize;
97     imgBuffer.reserve(bufferSize);
98     imgBuffer.insert(imgBuffer.begin(), (imageInfoSize & 0xFF00) >> OFFSET_BYTE);
99     imgBuffer.insert(imgBuffer.begin() + imgBuffer.size(), (imageInfoSize & 0x00FF));
100     imgBuffer.insert(imgBuffer.begin() + imgBuffer.size(), buffer, buffer + imageInfoSize);
101 
102     uint32_t computedValue = 0xFF000000;
103     for (uint8_t i = 0; i < DATA_BYTE_SIZE; i++) {
104         uint8_t tmpValue = ((pixelDataSize & computedValue) >> (OFFSET_BYTE * (DATA_BYTE_SIZE - i - 1)));
105         imgBuffer.insert(imgBuffer.begin() + imgBuffer.size(), tmpValue);
106         computedValue = computedValue >> OFFSET_BYTE;
107     }
108     imgBuffer.insert(imgBuffer.begin() + imgBuffer.size(), pixelMap->GetPixels(),
109         pixelMap->GetPixels() + pixelDataSize);
110     innerPixelMap->SetInnerImgBuffer(imgBuffer);
111     return innerPixelMap;
112 }
113 } // OHOS::AVSession