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