• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DECODE_DATA_PROCESS_H
17 #define OHOS_DECODE_DATA_PROCESS_H
18 
19 #include "securec.h"
20 #include <cstdint>
21 #include <vector>
22 #include <queue>
23 #include <thread>
24 #include <chrono>
25 
26 #include "surface.h"
27 #include "media_errors.h"
28 #include "avcodec_common.h"
29 #include "format.h"
30 #include "avsharedmemory.h"
31 #include "avcodec_video_decoder.h"
32 #include "event.h"
33 #include "event_bus.h"
34 #include "event_sender.h"
35 #include "eventbus_handler.h"
36 #include "event_registration.h"
37 
38 #include "data_buffer.h"
39 #include "distributed_camera_errno.h"
40 #include "image_common_type.h"
41 #include "dcamera_codec_event.h"
42 #include "abstract_data_process.h"
43 #include "dcamera_pipeline_source.h"
44 
45 namespace OHOS {
46 namespace DistributedHardware {
47 class DCameraPipelineSource;
48 class DecodeVideoCallback;
49 
50 class DecodeDataProcess : public EventSender, public EventBusHandler<DCameraCodecEvent>, public AbstractDataProcess,
51     public std::enable_shared_from_this<DecodeDataProcess> {
52 public:
DecodeDataProcess(const VideoConfigParams & sourceConfig,const VideoConfigParams & targetConfig,const std::shared_ptr<EventBus> & eventBusPipeline,const std::weak_ptr<DCameraPipelineSource> & callbackPipSource)53     DecodeDataProcess(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig,
54         const std::shared_ptr<EventBus>& eventBusPipeline,
55         const std::weak_ptr<DCameraPipelineSource>& callbackPipSource)
56         : sourceConfig_(sourceConfig), targetConfig_(targetConfig), eventBusPipeline_(eventBusPipeline),
57         callbackPipelineSource_(callbackPipSource) {}
58     ~DecodeDataProcess();
59 
60     int32_t InitNode() 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 InitDecoderMetadataFormat();
80     int32_t SetDecoderOutputSurface();
81     int32_t FeedDecoderInputBuffer();
82     int64_t GetDecoderTimeStamp();
83     int32_t GetAlignedHeight();
84     void CopyDecodedImage(const sptr<SurfaceBuffer>& surBuf, int64_t timeStampUs, int32_t alignedWidth,
85         int32_t alignedHeight);
86     int32_t CopyYUVPlaneByRow(const ImageUnitInfo& srcImgInfo, const ImageUnitInfo& dstImgInfo);
87     int32_t CheckCopyImageInfo(const ImageUnitInfo& srcImgInfo, const ImageUnitInfo& dstImgInfo);
88     bool IsCorrectImageUnitInfo(const ImageUnitInfo& imgInfo);
89     void PostOutputDataBuffers(std::shared_ptr<DataBuffer>& outputBuffer);
90     int32_t DecodeDone(std::vector<std::shared_ptr<DataBuffer>> outputBuffers);
91 
92 private:
93     const static int32_t VIDEO_DECODER_QUEUE_MAX = 1000;
94     const static int32_t MAX_YUV420_BUFFER_SIZE = 1920 * 1080 * 3 / 2 * 2;
95     const static uint32_t MAX_FRAME_RATE = 30;
96     const static uint32_t MIN_VIDEO_WIDTH = 320;
97     const static uint32_t MIN_VIDEO_HEIGHT = 240;
98     const static uint32_t MAX_VIDEO_WIDTH = 1920;
99     const static uint32_t MAX_VIDEO_HEIGHT = 1080;
100     const static int32_t FIRST_FRAME_INPUT_NUM = 2;
101 
102     std::mutex mtxDecoderState_;
103     std::mutex mtxHoldCount_;
104     VideoConfigParams sourceConfig_;
105     VideoConfigParams targetConfig_;
106     std::shared_ptr<EventBus> eventBusPipeline_;
107     std::weak_ptr<DCameraPipelineSource> callbackPipelineSource_;
108     std::shared_ptr<EventBus> eventBusDecode_ = nullptr;
109     std::shared_ptr<EventRegistration> eventBusRegHandleDecode_ = nullptr;
110     std::shared_ptr<EventRegistration> eventBusRegHandlePipeline2Decode_ = nullptr;
111     std::shared_ptr<Media::VideoDecoder> videoDecoder_ = nullptr;
112     std::shared_ptr<Media::AVCodecCallback> decodeVideoCallback_ = nullptr;
113     sptr<Surface> decodeConsumerSurface_ = nullptr;
114     sptr<Surface> decodeProducerSurface_ = nullptr;
115     sptr<IBufferConsumerListener> decodeSurfaceListener_ = nullptr;
116 
117     bool isDecoderProcess_ = false;
118     int32_t waitDecoderOutputCount_ = 0;
119     int32_t alignedHeight_ = 0;
120     int64_t lastFeedDecoderInputBufferTimeUs_ = 0;
121     int64_t outputTimeStampUs_ = 0;
122     std::string processType_;
123     Media::Format metadataFormat_;
124     Media::Format decodeOutputFormat_;
125     Media::AVCodecBufferInfo outputInfo_;
126     std::queue<std::shared_ptr<DataBuffer>> inputBuffersQueue_;
127     std::queue<uint32_t> availableInputIndexsQueue_;
128 };
129 
130 class DecodeSurfaceListener : public IBufferConsumerListener {
131 public:
DecodeSurfaceListener(sptr<Surface> surface,std::weak_ptr<DecodeDataProcess> decodeVideoNode)132     DecodeSurfaceListener(sptr<Surface> surface, std::weak_ptr<DecodeDataProcess> decodeVideoNode)
133         : surface_(surface), decodeVideoNode_(decodeVideoNode) {}
134     ~DecodeSurfaceListener();
135 
136     void OnBufferAvailable() override;
137     void SetSurface(const sptr<Surface>& surface);
138     void SetDecodeVideoNode(const std::weak_ptr<DecodeDataProcess>& decodeVideoNode);
139 
140 private:
141     sptr<Surface> surface_;
142     std::weak_ptr<DecodeDataProcess> decodeVideoNode_;
143 };
144 }
145 }
146 #endif