• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DECODE_DATA_PROCESS_H
17 #define OHOS_DECODE_DATA_PROCESS_H
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <queue>
22 #include <thread>
23 #include <vector>
24 
25 #include "avcodec_common.h"
26 #include "avcodec_video_decoder.h"
27 #include "avsharedmemory.h"
28 #include "event.h"
29 #include "event_bus.h"
30 #include "event_sender.h"
31 #include "event_registration.h"
32 #include "eventbus_handler.h"
33 #include "format.h"
34 #include "ibuffer_consumer_listener.h"
35 #include "media_errors.h"
36 #include "securec.h"
37 #include "surface.h"
38 
39 #include "abstract_data_process.h"
40 #include "data_buffer.h"
41 #include "dcamera_codec_event.h"
42 #include "dcamera_pipeline_source.h"
43 #include "distributed_camera_errno.h"
44 #include "image_common_type.h"
45 
46 namespace OHOS {
47 namespace DistributedHardware {
48 class DCameraPipelineSource;
49 class DecodeVideoCallback;
50 
51 class DecodeDataProcess : public EventSender, public EventBusHandler<DCameraCodecEvent>, public AbstractDataProcess,
52     public std::enable_shared_from_this<DecodeDataProcess> {
53 public:
DecodeDataProcess(const std::shared_ptr<EventBus> & eventBusPipeline,const std::weak_ptr<DCameraPipelineSource> & callbackPipSource)54     DecodeDataProcess(const std::shared_ptr<EventBus>& eventBusPipeline,
55         const std::weak_ptr<DCameraPipelineSource>& callbackPipSource)
56         : eventBusPipeline_(eventBusPipeline), callbackPipelineSource_(callbackPipSource) {}
57     ~DecodeDataProcess();
58 
59     int32_t InitNode(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig,
60         VideoConfigParams& processedConfig) override;
61     int32_t ProcessData(std::vector<std::shared_ptr<DataBuffer>>& inputBuffers) override;
62     void ReleaseProcessNode() override;
63     void OnEvent(DCameraCodecEvent& ev) override;
64 
65     void OnError();
66     void OnInputBufferAvailable(uint32_t index);
67     void OnOutputFormatChanged(const Media::Format &format);
68     void OnOutputBufferAvailable(uint32_t index, const Media::AVCodecBufferInfo& info,
69         const Media::AVCodecBufferFlag& flag);
70     void GetDecoderOutputBuffer(const sptr<Surface>& surface);
71     VideoConfigParams GetSourceConfig() const;
72     VideoConfigParams GetTargetConfig() const;
73 
74 private:
75     bool IsInDecoderRange(const VideoConfigParams& curConfig);
76     bool IsConvertible(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig);
77     void InitCodecEvent();
78     int32_t InitDecoder();
79     int32_t ConfigureVideoDecoder();
80     int32_t InitDecoderMetadataFormat();
81     int32_t SetDecoderOutputSurface();
82     int32_t StartVideoDecoder();
83     int32_t StopVideoDecoder();
84     void ReleaseVideoDecoder();
85     void ReleaseDecoderSurface();
86     void ReleaseCodecEvent();
87     int32_t FeedDecoderInputBuffer();
88     int64_t GetDecoderTimeStamp();
89     void IncreaseWaitDecodeCnt();
90     void ReduceWaitDecodeCnt();
91     void CopyDecodedImage(const sptr<SurfaceBuffer>& surBuf, int64_t timeStampUs, int32_t alignedWidth,
92         int32_t alignedHeight);
93     int32_t CopyYUVPlaneByRow(const ImageUnitInfo& srcImgInfo, const ImageUnitInfo& dstImgInfo);
94     int32_t CheckCopyImageInfo(const ImageUnitInfo& srcImgInfo, const ImageUnitInfo& dstImgInfo);
95     bool IsCorrectImageUnitInfo(const ImageUnitInfo& imgInfo);
96     bool IsCorrectSurfaceBuffer(const sptr<SurfaceBuffer>& surBuf, int32_t alignedWidth, int32_t alignedHeight);
97     void PostOutputDataBuffers(std::shared_ptr<DataBuffer>& outputBuffer);
98     int32_t DecodeDone(std::vector<std::shared_ptr<DataBuffer>>& outputBuffers);
99 
100 private:
101     constexpr static int32_t VIDEO_DECODER_QUEUE_MAX = 1000;
102     constexpr static int32_t MAX_YUV420_BUFFER_SIZE = 1920 * 1080 * 3 / 2 * 2;
103     constexpr static int32_t MAX_RGB32_BUFFER_SIZE = 1920 * 1080 * 4 * 2;
104     constexpr static int32_t MIN_FRAME_RATE = 0;
105     constexpr static int32_t MAX_FRAME_RATE = 30;
106     constexpr static int32_t MIN_VIDEO_WIDTH = 320;
107     constexpr static int32_t MIN_VIDEO_HEIGHT = 240;
108     constexpr static int32_t MAX_VIDEO_WIDTH = 1920;
109     constexpr static int32_t MAX_VIDEO_HEIGHT = 1080;
110     constexpr static int32_t FIRST_FRAME_INPUT_NUM = 2;
111     constexpr static int32_t RGB32_MEMORY_COEFFICIENT = 4;
112     constexpr static int32_t YUV_BYTES_PER_PIXEL = 3;
113     constexpr static int32_t Y2UV_RATIO = 2;
114     constexpr static int32_t BUFFER_MAX_SIZE = 50 * 1024 * 1024;
115     constexpr static int32_t ALIGNED_WIDTH_MAX_SIZE = 10000;
116 
117     std::shared_ptr<EventBus> eventBusPipeline_;
118     std::weak_ptr<DCameraPipelineSource> callbackPipelineSource_;
119     std::mutex mtxDecoderState_;
120     std::mutex mtxHoldCount_;
121     VideoConfigParams sourceConfig_;
122     VideoConfigParams targetConfig_;
123     VideoConfigParams processedConfig_;
124     std::shared_ptr<EventBus> eventBusDecode_ = nullptr;
125     std::shared_ptr<EventRegistration> eventBusRegHandleDecode_ = nullptr;
126     std::shared_ptr<EventRegistration> eventBusRegHandlePipeline2Decode_ = nullptr;
127     std::shared_ptr<Media::AVCodecVideoDecoder> videoDecoder_ = nullptr;
128     std::shared_ptr<Media::AVCodecCallback> decodeVideoCallback_ = nullptr;
129     sptr<Surface> decodeConsumerSurface_ = nullptr;
130     sptr<Surface> decodeProducerSurface_ = nullptr;
131     sptr<IBufferConsumerListener> decodeSurfaceListener_ = nullptr;
132 
133     std::atomic<bool> isDecoderProcess_ = false;
134     int32_t waitDecoderOutputCount_ = 0;
135     int32_t alignedHeight_ = 0;
136     int64_t lastFeedDecoderInputBufferTimeUs_ = 0;
137     int64_t outputTimeStampUs_ = 0;
138     std::string processType_;
139     Media::Format metadataFormat_;
140     Media::Format decodeOutputFormat_;
141     Media::AVCodecBufferInfo outputInfo_;
142     std::queue<std::shared_ptr<DataBuffer>> inputBuffersQueue_;
143     std::queue<uint32_t> availableInputIndexsQueue_;
144 };
145 } // namespace DistributedHardware
146 } // namespace OHOS
147 #endif // OHOS_DECODE_DATA_PROCESS_H
148