• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 #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);
59     void OnOutputFormatChanged(const Media::Format &format);
60     void OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, Media::AVCodecBufferFlag flag);
61     VideoConfigParams GetSourceConfig() const;
62     VideoConfigParams GetTargetConfig() const;
63 
64     int32_t GetProperty(const std::string& propertyName, PropertyCarrier& propertyCarrier) override;
65 
66 private:
67     bool IsInEncoderRange(const VideoConfigParams& curConfig);
68     bool IsConvertible(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig);
69     int32_t InitEncoder();
70     int32_t ConfigureVideoEncoder();
71     int32_t InitEncoderMetadataFormat();
72     int32_t InitEncoderBitrateFormat();
73     int32_t StartVideoEncoder();
74     int32_t StopVideoEncoder();
75     void ReleaseVideoEncoder();
76     int32_t FeedEncoderInputBuffer(std::shared_ptr<DataBuffer>& inputBuffer);
77     sptr<SurfaceBuffer> GetEncoderInputSurfaceBuffer();
78     int64_t GetEncoderTimeStamp();
79     void IncreaseWaitEncodeCnt();
80     void ReduceWaitEncodeCnt();
81     int32_t GetEncoderOutputBuffer(uint32_t index, Media::AVCodecBufferInfo info, Media::AVCodecBufferFlag flag);
82     int32_t EncodeDone(std::vector<std::shared_ptr<DataBuffer>>& outputBuffers);
83 
84 private:
85     constexpr static int32_t ENCODER_STRIDE_ALIGNMENT = 8;
86     constexpr static int64_t NORM_YUV420_BUFFER_SIZE = 1920 * 1080 * 3 / 2;
87     constexpr static int32_t NORM_RGB32_BUFFER_SIZE = 1920 * 1080 * 4;
88     constexpr static int32_t MIN_FRAME_RATE = 0;
89     constexpr static int32_t MAX_FRAME_RATE = 30;
90     constexpr static int32_t MIN_VIDEO_WIDTH = 320;
91     constexpr static int32_t MIN_VIDEO_HEIGHT = 240;
92     constexpr static int32_t MAX_VIDEO_WIDTH = 1920;
93     constexpr static int32_t MAX_VIDEO_HEIGHT = 1080;
94     constexpr static int32_t IDR_FRAME_INTERVAL_MS = 8000;
95     constexpr static int32_t FIRST_FRAME_OUTPUT_NUM = 2;
96     const int32_t DATABUFF_MAX_SIZE = 100 * 1024 * 1024;
97 
98     constexpr static int64_t WIDTH_320_HEIGHT_240 = 320 * 240;
99     constexpr static int64_t WIDTH_480_HEIGHT_360 = 480 * 360;
100     constexpr static int64_t WIDTH_640_HEIGHT_360 = 640 * 360;
101     constexpr static int64_t WIDTH_640_HEIGHT_480 = 640 * 480;
102     constexpr static int64_t WIDTH_720_HEIGHT_540 = 720 * 540;
103     constexpr static int64_t WIDTH_960_HEIGHT_540 = 960 * 540;
104     constexpr static int64_t WIDTH_960_HEIGHT_720 = 960 * 720;
105     constexpr static int64_t WIDTH_1280_HEIGHT_720 = 1280 * 720;
106     constexpr static int64_t WIDTH_1440_HEIGHT_1080 = 1440 * 1080;
107     constexpr static int64_t WIDTH_1920_HEIGHT_1080 = 1920 * 1080;
108     constexpr static int32_t BITRATE_500000 = 500000;
109     constexpr static int32_t BITRATE_1110000 = 1110000;
110     constexpr static int32_t BITRATE_1500000 = 1500000;
111     constexpr static int32_t BITRATE_1800000 = 1800000;
112     constexpr static int32_t BITRATE_2100000 = 2100000;
113     constexpr static int32_t BITRATE_2300000 = 2300000;
114     constexpr static int32_t BITRATE_2800000 = 2800000;
115     constexpr static int32_t BITRATE_3400000 = 3400000;
116     constexpr static int32_t BITRATE_5000000 = 5000000;
117     constexpr static int32_t BITRATE_6000000 = 6000000;
118     const static std::map<std::int64_t, int32_t> ENCODER_BITRATE_TABLE;
119     constexpr static uint64_t S2NS = 1000000000;
120     constexpr static uint32_t US2NS = 1000;
121 
122     std::weak_ptr<DCameraPipelineSink> callbackPipelineSink_;
123     std::mutex mtxEncoderState_;
124     std::mutex mtxHoldCount_;
125     VideoConfigParams sourceConfig_;
126     VideoConfigParams targetConfig_;
127     VideoConfigParams processedConfig_;
128     std::shared_ptr<Media::AVCodecVideoEncoder> videoEncoder_ = nullptr;
129     std::shared_ptr<Media::AVCodecCallback> encodeVideoCallback_ = nullptr;
130     sptr<Surface> encodeProducerSurface_ = nullptr;
131 
132     std::atomic<bool> isEncoderProcess_ = false;
133     int32_t waitEncoderOutputCount_ = 0;
134     int64_t lastFeedEncoderInputBufferTimeUs_ = 0;
135     int64_t inputTimeStampUs_ = 0;
136     std::string processType_;
137     Media::Format metadataFormat_;
138     Media::Format encodeOutputFormat_;
139     std::string surfaceStr_ = "surface";
140     int32_t index_ = FRAME_HEAD;
141 };
142 } // namespace DistributedHardware
143 } // namespace OHOS
144 #endif // OHOS_ENCODE_DATA_PROCESS_H
145