1 /*
2  * Copyright (c) 2022-2023 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 #ifndef OHOS_SCALE_CONVERT_PROCESS_H
17 #define OHOS_SCALE_CONVERT_PROCESS_H
18 
19 #include "abstract_data_process.h"
20 
21 #ifdef DCAMERA_SUPPORT_FFMPEG
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 #include <libavutil/imgutils.h>
26 #include <libavutil/opt.h>
27 #include <libavutil/parseutils.h>
28 #include <libswscale/swscale.h>
29 #ifdef __cplusplus
30 };
31 #endif
32 #endif
33 
34 #include <mutex>
35 #include <securec.h>
36 
37 #include "dcamera_pipeline_source.h"
38 #include "image_common_type.h"
39 #include "dcamera_utils_tools.h"
40 
41 namespace OHOS {
42 namespace DistributedHardware {
43 class ScaleConvertProcess : public AbstractDataProcess {
44 public:
ScaleConvertProcess(const std::weak_ptr<DCameraPipelineSource> & callbackPipeSource)45     explicit ScaleConvertProcess(const std::weak_ptr<DCameraPipelineSource>& callbackPipeSource)
46         : callbackPipelineSource_(callbackPipeSource) {}
47     ~ScaleConvertProcess() override;
48 
49     int32_t InitNode(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig,
50         VideoConfigParams& processedConfig) override;
51     int32_t ProcessData(std::vector<std::shared_ptr<DataBuffer>>& inputBuffers) override;
52     void ReleaseProcessNode() override;
53 
54     int32_t GetProperty(const std::string& propertyName, PropertyCarrier& propertyCarrier) override;
55 
56 private:
57     bool IsConvertible(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig);
58     bool IsCorrectImageUnitInfo(const ImageUnitInfo& imgInfo);
59     bool CheckScaleProcessInputInfo(const ImageUnitInfo& srcImgInfo);
60     bool CheckScaleConvertInfo(const ImageUnitInfo& srcImgInfo, const ImageUnitInfo& dstImgInfo);
61     int32_t GetImageUnitInfo(ImageUnitInfo& imgInfo, const std::shared_ptr<DataBuffer>& imgBuf);
62     int32_t ScaleConvert(ImageUnitInfo& srcImgInfo, ImageUnitInfo& dstImgInfo);
63 #ifdef DCAMERA_SUPPORT_FFMPEG
64     int32_t CopyYUV420SrcData(const ImageUnitInfo& srcImgInfo);
65     int32_t CopyNV12SrcData(const ImageUnitInfo& srcImgInfo);
66     int32_t CopyNV21SrcData(const ImageUnitInfo& srcImgInfo);
67     AVPixelFormat GetAVPixelFormat(Videoformat colorFormat);
68 #else
69     int32_t ConvertResolution(ImageUnitInfo& srcImgInfo, ImageUnitInfo& dstImgInfo,
70         std::shared_ptr<DataBuffer>& dstBuf);
71     int32_t ConvertFormatToNV21(ImageUnitInfo& srcImgInfo, ImageUnitInfo& dstImgInfo,
72         std::shared_ptr<DataBuffer>& dstBuf);
73     int32_t ConvertFormatToRGBA(ImageUnitInfo& srcImgInfo, ImageUnitInfo& dstImgInfo,
74         std::shared_ptr<DataBuffer>& dstBuf);
75     void CalculateBuffSize(size_t& dstBuffSize);
76 #endif
77     int32_t ConvertDone(std::vector<std::shared_ptr<DataBuffer>>& outputBuffers);
78 
79 private:
80     constexpr static int32_t DATA_LEN = 4;
81     constexpr static int32_t MEMORY_RATIO_NV = 2;
82     constexpr static int32_t MEMORY_RATIO_YUV = 4;
83     constexpr static int32_t SOURCE_ALIGN = 16;
84     constexpr static int32_t TARGET_ALIGN = 1;
85     constexpr static int32_t YUV_BYTES_PER_PIXEL = 3;
86     constexpr static int32_t Y2UV_RATIO = 2;
87     constexpr static int32_t RGB32_MEMORY_COEFFICIENT = 4;
88     constexpr static uint32_t MEMORY_RATIO_UV = 1;
89 
90 #ifdef DCAMERA_SUPPORT_FFMPEG
91     uint8_t *srcData_[DATA_LEN] = { nullptr };
92     uint8_t *dstData_[DATA_LEN] = { nullptr };
93     int32_t srcLineSize_[DATA_LEN] = { 0 };
94     int32_t dstLineSize_[DATA_LEN] = { 0 };
95     int32_t dstBuffSize_ = 0;
96     SwsContext *swsContext_ = nullptr;
97     std::mutex scaleMutex_;
98 #endif
99     VideoConfigParams sourceConfig_;
100     VideoConfigParams targetConfig_;
101     VideoConfigParams processedConfig_;
102     std::weak_ptr<DCameraPipelineSource> callbackPipelineSource_;
103     std::atomic<bool> isScaleConvert_ = false;
104     FILE *dumpFile_ = nullptr;
105 };
106 } // namespace DistributedHardware
107 } // namespace OHOS
108 #endif // OHOS_SCALE_CONVERT_PROCESS_H
109