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_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 "avsharedmemory.h" 26 #include "format.h" 27 #include "media_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 37 namespace OHOS { 38 namespace DistributedHardware { 39 class DCameraPipelineSink; 40 class EncodeVideoCallback; 41 42 class EncodeDataProcess : public AbstractDataProcess, public std::enable_shared_from_this<EncodeDataProcess> { 43 public: EncodeDataProcess(const std::weak_ptr<DCameraPipelineSink> & callbackPipSink)44 explicit EncodeDataProcess(const std::weak_ptr<DCameraPipelineSink>& callbackPipSink) 45 : callbackPipelineSink_(callbackPipSink) {} 46 ~EncodeDataProcess(); 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 void OnError(); 54 void OnInputBufferAvailable(uint32_t index); 55 void OnOutputFormatChanged(const Media::Format &format); 56 void OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, Media::AVCodecBufferFlag flag); 57 VideoConfigParams GetSourceConfig() const; 58 VideoConfigParams GetTargetConfig() const; 59 60 private: 61 bool IsInEncoderRange(const VideoConfigParams& curConfig); 62 bool IsConvertible(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig); 63 int32_t InitEncoder(); 64 int32_t ConfigureVideoEncoder(); 65 int32_t InitEncoderMetadataFormat(); 66 int32_t InitEncoderBitrateFormat(); 67 int32_t StartVideoEncoder(); 68 int32_t StopVideoEncoder(); 69 void ReleaseVideoEncoder(); 70 int32_t FeedEncoderInputBuffer(std::shared_ptr<DataBuffer>& inputBuffer); 71 sptr<SurfaceBuffer> GetEncoderInputSurfaceBuffer(); 72 int64_t GetEncoderTimeStamp(); 73 void IncreaseWaitEncodeCnt(); 74 void ReduceWaitEncodeCnt(); 75 int32_t GetEncoderOutputBuffer(uint32_t index, Media::AVCodecBufferInfo info); 76 int32_t EncodeDone(std::vector<std::shared_ptr<DataBuffer>>& outputBuffers); 77 78 private: 79 constexpr static int32_t ENCODER_STRIDE_ALIGNMENT = 8; 80 constexpr static int64_t NORM_YUV420_BUFFER_SIZE = 1920 * 1080 * 3 / 2; 81 constexpr static int32_t NORM_RGB32_BUFFER_SIZE = 1920 * 1080 * 4; 82 constexpr static int32_t MIN_FRAME_RATE = 0; 83 constexpr static int32_t MAX_FRAME_RATE = 30; 84 constexpr static int32_t MIN_VIDEO_WIDTH = 320; 85 constexpr static int32_t MIN_VIDEO_HEIGHT = 240; 86 constexpr static int32_t MAX_VIDEO_WIDTH = 1920; 87 constexpr static int32_t MAX_VIDEO_HEIGHT = 1080; 88 constexpr static int32_t IDR_FRAME_INTERVAL_MS = 8000; 89 constexpr static int32_t FIRST_FRAME_OUTPUT_NUM = 2; 90 const int32_t DATABUFF_MAX_SIZE = 100 * 1024 * 1024; 91 92 constexpr static int64_t WIDTH_320_HEIGHT_240 = 320 * 240; 93 constexpr static int64_t WIDTH_480_HEIGHT_360 = 480 * 360; 94 constexpr static int64_t WIDTH_640_HEIGHT_360 = 640 * 360; 95 constexpr static int64_t WIDTH_640_HEIGHT_480 = 640 * 480; 96 constexpr static int64_t WIDTH_720_HEIGHT_540 = 720 * 540; 97 constexpr static int64_t WIDTH_960_HEIGHT_540 = 960 * 540; 98 constexpr static int64_t WIDTH_960_HEIGHT_720 = 960 * 720; 99 constexpr static int64_t WIDTH_1280_HEIGHT_720 = 1280 * 720; 100 constexpr static int64_t WIDTH_1440_HEIGHT_1080 = 1440 * 1080; 101 constexpr static int64_t WIDTH_1920_HEIGHT_1080 = 1920 * 1080; 102 constexpr static int32_t BITRATE_500000 = 500000; 103 constexpr static int32_t BITRATE_1110000 = 1110000; 104 constexpr static int32_t BITRATE_1500000 = 1500000; 105 constexpr static int32_t BITRATE_1800000 = 1800000; 106 constexpr static int32_t BITRATE_2100000 = 2100000; 107 constexpr static int32_t BITRATE_2300000 = 2300000; 108 constexpr static int32_t BITRATE_2800000 = 2800000; 109 constexpr static int32_t BITRATE_3400000 = 3400000; 110 constexpr static int32_t BITRATE_5000000 = 5000000; 111 constexpr static int32_t BITRATE_6000000 = 6000000; 112 const static std::map<std::int64_t, int32_t> ENCODER_BITRATE_TABLE; 113 114 std::weak_ptr<DCameraPipelineSink> callbackPipelineSink_; 115 std::mutex mtxEncoderState_; 116 std::mutex mtxHoldCount_; 117 VideoConfigParams sourceConfig_; 118 VideoConfigParams targetConfig_; 119 VideoConfigParams processedConfig_; 120 std::shared_ptr<Media::AVCodecVideoEncoder> videoEncoder_ = nullptr; 121 std::shared_ptr<Media::AVCodecCallback> encodeVideoCallback_ = nullptr; 122 sptr<Surface> encodeProducerSurface_ = nullptr; 123 124 std::atomic<bool> isEncoderProcess_ = false; 125 int32_t waitEncoderOutputCount_ = 0; 126 int64_t lastFeedEncoderInputBufferTimeUs_ = 0; 127 int64_t inputTimeStampUs_ = 0; 128 std::string processType_; 129 Media::Format metadataFormat_; 130 Media::Format encodeOutputFormat_; 131 }; 132 } // namespace DistributedHardware 133 } // namespace OHOS 134 #endif // OHOS_ENCODE_DATA_PROCESS_H 135