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