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_ENCODE_DATA_PROCESS_H 17 #define OHOS_ENCODE_DATA_PROCESS_H 18 19 #include <cstdint> 20 #include <queue> 21 #include <vector> 22 23 #include "avcodec_common.h" 24 #include "avcodec_video_encoder.h" 25 #include "buffer/avsharedmemory.h" 26 #include "meta/format.h" 27 #include "avcodec_errors.h" 28 #include "securec.h" 29 #include "surface.h" 30 31 #include "abstract_data_process.h" 32 #include "data_buffer.h" 33 #include "dcamera_pipeline_sink.h" 34 #include "distributed_camera_errno.h" 35 #include "image_common_type.h" 36 #include "distributed_camera_constants.h" 37 38 #include "v1_0/display_composer_type.h" 39 40 namespace OHOS { 41 namespace DistributedHardware { 42 using namespace OHOS::HDI::Display::Composer::V1_0; 43 class DCameraPipelineSink; 44 class EncodeVideoCallback; 45 46 class EncodeDataProcess : public AbstractDataProcess, public std::enable_shared_from_this<EncodeDataProcess> { 47 public: EncodeDataProcess(const std::weak_ptr<DCameraPipelineSink> & callbackPipSink)48 explicit EncodeDataProcess(const std::weak_ptr<DCameraPipelineSink>& callbackPipSink) 49 : callbackPipelineSink_(callbackPipSink) {} 50 ~EncodeDataProcess() override; 51 52 int32_t InitNode(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig, 53 VideoConfigParams& processedConfig) override; 54 int32_t ProcessData(std::vector<std::shared_ptr<DataBuffer>>& inputBuffers) override; 55 void ReleaseProcessNode() override; 56 57 void OnError(); 58 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<Media::AVSharedMemory> buffer); 59 void OnOutputFormatChanged(const Media::Format &format); 60 void OnOutputBufferAvailable(uint32_t index, MediaAVCodec::AVCodecBufferInfo info, 61 MediaAVCodec::AVCodecBufferFlag flag, std::shared_ptr<Media::AVSharedMemory> buffer); 62 VideoConfigParams GetSourceConfig() const; 63 VideoConfigParams GetTargetConfig() const; 64 65 int32_t GetProperty(const std::string& propertyName, PropertyCarrier& propertyCarrier) override; 66 67 private: 68 bool IsInEncoderRange(const VideoConfigParams& curConfig); 69 bool IsConvertible(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig); 70 int32_t InitEncoder(); 71 int32_t ConfigureVideoEncoder(); 72 int32_t InitEncoderMetadataFormat(); 73 int32_t InitEncoderBitrateFormat(); 74 int32_t StartVideoEncoder(); 75 int32_t StopVideoEncoder(); 76 void ReleaseVideoEncoder(); 77 int32_t FeedEncoderInputBuffer(std::shared_ptr<DataBuffer>& inputBuffer); 78 sptr<SurfaceBuffer> GetEncoderInputSurfaceBuffer(); 79 int64_t GetEncoderTimeStamp(); 80 void IncreaseWaitEncodeCnt(); 81 void ReduceWaitEncodeCnt(); 82 int32_t GetEncoderOutputBuffer(uint32_t index, MediaAVCodec::AVCodecBufferInfo info, 83 MediaAVCodec::AVCodecBufferFlag flag, std::shared_ptr<Media::AVSharedMemory>& buffer); 84 int32_t EncodeDone(std::vector<std::shared_ptr<DataBuffer>>& outputBuffers); 85 86 private: 87 constexpr static int32_t ENCODER_STRIDE_ALIGNMENT = 8; 88 constexpr static int64_t NORM_YUV420_BUFFER_SIZE = 1920 * 1080 * 3 / 2; 89 constexpr static int32_t NORM_RGB32_BUFFER_SIZE = 1920 * 1080 * 4; 90 constexpr static int32_t MIN_FRAME_RATE = 0; 91 constexpr static double MAX_FRAME_RATE = 30; 92 constexpr static int32_t MIN_VIDEO_WIDTH = 320; 93 constexpr static int32_t MIN_VIDEO_HEIGHT = 240; 94 constexpr static int32_t MAX_VIDEO_WIDTH = 1920; 95 constexpr static int32_t MAX_VIDEO_HEIGHT = 1080; 96 constexpr static int32_t IDR_FRAME_INTERVAL_MS = 8000; 97 constexpr static int32_t FIRST_FRAME_OUTPUT_NUM = 2; 98 const int32_t DATABUFF_MAX_SIZE = 100 * 1024 * 1024; 99 100 constexpr static int64_t WIDTH_320_HEIGHT_240 = 320 * 240; 101 constexpr static int64_t WIDTH_480_HEIGHT_360 = 480 * 360; 102 constexpr static int64_t WIDTH_640_HEIGHT_360 = 640 * 360; 103 constexpr static int64_t WIDTH_640_HEIGHT_480 = 640 * 480; 104 constexpr static int64_t WIDTH_720_HEIGHT_540 = 720 * 540; 105 constexpr static int64_t WIDTH_960_HEIGHT_540 = 960 * 540; 106 constexpr static int64_t WIDTH_960_HEIGHT_720 = 960 * 720; 107 constexpr static int64_t WIDTH_1280_HEIGHT_720 = 1280 * 720; 108 constexpr static int64_t WIDTH_1440_HEIGHT_1080 = 1440 * 1080; 109 constexpr static int64_t WIDTH_1920_HEIGHT_1080 = 1920 * 1080; 110 constexpr static int32_t BITRATE_500000 = 500000; 111 constexpr static int32_t BITRATE_1110000 = 1110000; 112 constexpr static int32_t BITRATE_1500000 = 1500000; 113 constexpr static int32_t BITRATE_1800000 = 1800000; 114 constexpr static int32_t BITRATE_2100000 = 2100000; 115 constexpr static int32_t BITRATE_2300000 = 2300000; 116 constexpr static int32_t BITRATE_2800000 = 2800000; 117 constexpr static int32_t BITRATE_3400000 = 3400000; 118 constexpr static int32_t BITRATE_5000000 = 5000000; 119 constexpr static int32_t BITRATE_6000000 = 6000000; 120 const static std::map<std::int64_t, int32_t> ENCODER_BITRATE_TABLE; 121 constexpr static uint64_t S2NS = 1000000000; 122 constexpr static uint32_t US2NS = 1000; 123 124 std::weak_ptr<DCameraPipelineSink> callbackPipelineSink_; 125 std::mutex mtxEncoderState_; 126 std::mutex mtxHoldCount_; 127 VideoConfigParams sourceConfig_; 128 VideoConfigParams targetConfig_; 129 VideoConfigParams processedConfig_; 130 std::shared_ptr<MediaAVCodec::AVCodecVideoEncoder> videoEncoder_ = nullptr; 131 std::shared_ptr<MediaAVCodec::AVCodecCallback> encodeVideoCallback_ = nullptr; 132 sptr<Surface> encodeProducerSurface_ = nullptr; 133 134 std::atomic<bool> isEncoderProcess_ = false; 135 int32_t waitEncoderOutputCount_ = 0; 136 int64_t lastFeedEncoderInputBufferTimeUs_ = 0; 137 int64_t inputTimeStampUs_ = 0; 138 std::string processType_; 139 Media::Format metadataFormat_; 140 Media::Format encodeOutputFormat_; 141 std::string surfaceStr_ = "surface"; 142 int32_t index_ = FRAME_HEAD; 143 }; 144 } // namespace DistributedHardware 145 } // namespace OHOS 146 #endif // OHOS_ENCODE_DATA_PROCESS_H 147