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 40 namespace OHOS { 41 namespace DistributedHardware { 42 class ScaleConvertProcess : public AbstractDataProcess { 43 public: ScaleConvertProcess(const std::weak_ptr<DCameraPipelineSource> & callbackPipeSource)44 explicit ScaleConvertProcess(const std::weak_ptr<DCameraPipelineSource>& callbackPipeSource) 45 : callbackPipelineSource_(callbackPipeSource) {} 46 ~ScaleConvertProcess() override; 47 48 int32_t InitNode(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig, 49 VideoConfigParams& processedConfig) override; 50 int32_t ProcessData(std::vector<std::shared_ptr<DataBuffer>>& inputBuffers) override; 51 void ReleaseProcessNode() override; 52 53 int32_t GetProperty(const std::string& propertyName, PropertyCarrier& propertyCarrier) override; 54 55 private: 56 bool IsConvertible(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig); 57 bool IsCorrectImageUnitInfo(const ImageUnitInfo& imgInfo); 58 bool CheckScaleProcessInputInfo(const ImageUnitInfo& srcImgInfo); 59 bool CheckScaleConvertInfo(const ImageUnitInfo& srcImgInfo, const ImageUnitInfo& dstImgInfo); 60 int32_t GetImageUnitInfo(ImageUnitInfo& imgInfo, const std::shared_ptr<DataBuffer>& imgBuf); 61 int32_t ScaleConvert(ImageUnitInfo& srcImgInfo, ImageUnitInfo& dstImgInfo); 62 #ifdef DCAMERA_SUPPORT_FFMPEG 63 int32_t CopyYUV420SrcData(const ImageUnitInfo& srcImgInfo); 64 int32_t CopyNV12SrcData(const ImageUnitInfo& srcImgInfo); 65 int32_t CopyNV21SrcData(const ImageUnitInfo& srcImgInfo); 66 AVPixelFormat GetAVPixelFormat(Videoformat colorFormat); 67 #else 68 int32_t ConvertResolution(ImageUnitInfo& srcImgInfo, ImageUnitInfo& dstImgInfo, 69 std::shared_ptr<DataBuffer>& dstBuf); 70 int32_t ConvertFormatToNV21(ImageUnitInfo& srcImgInfo, ImageUnitInfo& dstImgInfo, 71 std::shared_ptr<DataBuffer>& dstBuf); 72 int32_t ConvertFormatToRGBA(ImageUnitInfo& srcImgInfo, ImageUnitInfo& dstImgInfo, 73 std::shared_ptr<DataBuffer>& dstBuf); 74 void CalculateBuffSize(size_t& dstBuffSize); 75 #endif 76 int32_t ConvertDone(std::vector<std::shared_ptr<DataBuffer>>& outputBuffers); 77 78 private: 79 constexpr static int32_t DATA_LEN = 4; 80 constexpr static int32_t MEMORY_RATIO_NV = 2; 81 constexpr static int32_t MEMORY_RATIO_YUV = 4; 82 constexpr static int32_t SOURCE_ALIGN = 16; 83 constexpr static int32_t TARGET_ALIGN = 1; 84 constexpr static int32_t YUV_BYTES_PER_PIXEL = 3; 85 constexpr static int32_t Y2UV_RATIO = 2; 86 constexpr static int32_t RGB32_MEMORY_COEFFICIENT = 4; 87 constexpr static uint32_t MEMORY_RATIO_UV = 1; 88 89 #ifdef DCAMERA_SUPPORT_FFMPEG 90 uint8_t *srcData_[DATA_LEN] = { nullptr }; 91 uint8_t *dstData_[DATA_LEN] = { nullptr }; 92 int32_t srcLineSize_[DATA_LEN] = { 0 }; 93 int32_t dstLineSize_[DATA_LEN] = { 0 }; 94 int32_t dstBuffSize_ = 0; 95 SwsContext *swsContext_ = nullptr; 96 std::mutex scaleMutex_; 97 #endif 98 VideoConfigParams sourceConfig_; 99 VideoConfigParams targetConfig_; 100 VideoConfigParams processedConfig_; 101 std::weak_ptr<DCameraPipelineSource> callbackPipelineSource_; 102 std::atomic<bool> isScaleConvert_ = false; 103 }; 104 } // namespace DistributedHardware 105 } // namespace OHOS 106 #endif // OHOS_SCALE_CONVERT_PROCESS_H 107