1 /* 2 * Copyright (c) 2021 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 "securec.h" 20 #include <cstdint> 21 #include <vector> 22 #include <queue> 23 24 #include "surface.h" 25 #include "media_errors.h" 26 #include "avcodec_common.h" 27 #include "format.h" 28 #include "avsharedmemory.h" 29 #include "avcodec_video_encoder.h" 30 31 #include "data_buffer.h" 32 #include "distributed_camera_errno.h" 33 #include "image_common_type.h" 34 #include "abstract_data_process.h" 35 #include "dcamera_pipeline_sink.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 VideoConfigParams & sourceConfig,const VideoConfigParams & targetConfig,const std::weak_ptr<DCameraPipelineSink> & callbackPipSink)44 EncodeDataProcess(const VideoConfigParams &sourceConfig, const VideoConfigParams &targetConfig, 45 const std::weak_ptr<DCameraPipelineSink>& callbackPipSink) 46 : sourceConfig_(sourceConfig), targetConfig_(targetConfig), callbackPipelineSink_(callbackPipSink) {} 47 ~EncodeDataProcess(); 48 49 int32_t InitNode() 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 InitEncoderMetadataFormat(); 65 int32_t InitEncoderBitrateFormat(); 66 int32_t FeedEncoderInputBuffer(std::shared_ptr<DataBuffer>& inputBuffer); 67 sptr<SurfaceBuffer> GetEncoderInputSurfaceBuffer(); 68 int64_t GetEncoderTimeStamp(); 69 int32_t GetEncoderOutputBuffer(uint32_t index, Media::AVCodecBufferInfo info); 70 int32_t EncodeDone(std::vector<std::shared_ptr<DataBuffer>> outputBuffers); 71 72 private: 73 const static int32_t ENCODER_STRIDE_ALIGNMENT = 8; 74 const static int64_t NORM_YUV420_BUFFER_SIZE = 1920 * 1080 * 3 / 2; 75 const static uint32_t MAX_FRAME_RATE = 30; 76 const static uint32_t MIN_VIDEO_WIDTH = 320; 77 const static uint32_t MIN_VIDEO_HEIGHT = 240; 78 const static uint32_t MAX_VIDEO_WIDTH = 1920; 79 const static uint32_t MAX_VIDEO_HEIGHT = 1080; 80 const static int32_t IDR_FRAME_INTERVAL_MS = 300; 81 const static int32_t FIRST_FRAME_OUTPUT_NUM = 2; 82 83 const static int64_t WIDTH_320_HEIGHT_240 = 320 * 240; 84 const static int64_t WIDTH_480_HEIGHT_360 = 480 * 360; 85 const static int64_t WIDTH_640_HEIGHT_360 = 640 * 360; 86 const static int64_t WIDTH_640_HEIGHT_480 = 640 * 480; 87 const static int64_t WIDTH_720_HEIGHT_540 = 720 * 540; 88 const static int64_t WIDTH_960_HEIGHT_540 = 960 * 540; 89 const static int64_t WIDTH_960_HEIGHT_720 = 960 * 720; 90 const static int64_t WIDTH_1280_HEIGHT_720 = 1280 * 720; 91 const static int64_t WIDTH_1440_HEIGHT_1080 = 1440 * 1080; 92 const static int64_t WIDTH_1920_HEIGHT_1080 = 1920 * 1080; 93 const static int32_t BITRATE_500000 = 500000; 94 const static int32_t BITRATE_1110000 = 1110000; 95 const static int32_t BITRATE_1500000 = 1500000; 96 const static int32_t BITRATE_1800000 = 1800000; 97 const static int32_t BITRATE_2100000 = 2100000; 98 const static int32_t BITRATE_2300000 = 2300000; 99 const static int32_t BITRATE_2800000 = 2800000; 100 const static int32_t BITRATE_3400000 = 3400000; 101 const static int32_t BITRATE_5000000 = 5000000; 102 const static int32_t BITRATE_6000000 = 6000000; 103 const static std::map<std::int64_t, int32_t> ENCODER_BITRATE_TABLE; 104 105 std::mutex mtxEncoderState_; 106 std::mutex mtxHoldCount_; 107 VideoConfigParams sourceConfig_; 108 VideoConfigParams targetConfig_; 109 std::weak_ptr<DCameraPipelineSink> callbackPipelineSink_; 110 std::shared_ptr<Media::VideoEncoder> videoEncoder_ = nullptr; 111 std::shared_ptr<Media::AVCodecCallback> encodeVideoCallback_ = nullptr; 112 sptr<Surface> encodeProducerSurface_ = nullptr; 113 114 bool isEncoderProcess_ = false; 115 int32_t waitEncoderOutputCount_ = 0; 116 int64_t lastFeedEncoderInputBufferTimeUs_ = 0; 117 int64_t inputTimeStampUs_ = 0; 118 std::string processType_; 119 Media::Format metadataFormat_; 120 Media::Format encodeOutputFormat_; 121 }; 122 } 123 } 124 #endif