• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 Shenzhen Kaihong DID 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 CODEC_HDI_DECODE_H
17 #define CODEC_HDI_DECODE_H
18 
19 #include <condition_variable>
20 #include <list>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 #include <ashmem.h>
25 #include <buffer_handle.h>
26 #include <OMX_Component.h>
27 #include <OMX_Core.h>
28 #include <OMX_VideoExt.h>
29 #include <securec.h>
30 #include "codec_hdi_callback.h"
31 #include "codec_utils.h"
32 #include "command_parse.h"
33 #include "hdf_log.h"
34 #include "v1_0/codec_types.h"
35 #include "v1_0/icodec_callback.h"
36 #include "v1_0/icodec_component.h"
37 #include "v1_0/icodec_component_manager.h"
38 #include "v1_0/include/idisplay_buffer.h"
39 
40 using OHOS::HDI::Codec::V1_0::OmxCodecBuffer;
41 class CodecHdiDecode : public ICodecHdiCallBackBase,
42                        public std::enable_shared_from_this<CodecHdiDecode> {
43     enum class PortIndex { PORT_INDEX_INPUT = 0, PORT_INDEX_OUTPUT = 1 };
44     struct BufferInfo {
45         std::shared_ptr<OHOS::HDI::Codec::V1_0::OmxCodecBuffer> omxBuffer;
46         std::shared_ptr<OHOS::Ashmem> avSharedPtr;
47         PortIndex portIndex;
48         BufferHandle *bufferHandle;
BufferInfoBufferInfo49         BufferInfo()
50         {
51             omxBuffer = nullptr;
52             avSharedPtr = nullptr;
53             portIndex = PortIndex::PORT_INDEX_INPUT;
54             bufferHandle = nullptr;
55         }
~BufferInfoBufferInfo56         ~BufferInfo()
57         {
58             omxBuffer = nullptr;
59             if (avSharedPtr != nullptr) {
60                 avSharedPtr->UnmapAshmem();
61                 avSharedPtr->CloseAshmem();
62                 avSharedPtr = nullptr;
63             }
64             if (bufferHandle != nullptr && gralloc_ != nullptr) {
65                 gralloc_->FreeMem(*bufferHandle);
66                 bufferHandle = nullptr;
67             }
68             portIndex = PortIndex::PORT_INDEX_INPUT;
69         }
setBufferHandleBufferInfo70         void setBufferHandle(BufferHandle *bufferHandle)
71         {
72             if (this->bufferHandle != nullptr && gralloc_ != nullptr) {
73                 gralloc_->FreeMem(*this->bufferHandle);
74             }
75             this->bufferHandle = bufferHandle;
76         }
77     };
78     using BufferInfo = struct BufferInfo;
79 
80 public:
81     explicit CodecHdiDecode();
82     virtual ~CodecHdiDecode();
83     bool Init(const CommandOpt &opt);
84     bool Configure();
85     bool UseBuffers();
86     void FreeBuffers();
87     void Run();
88     void Release();
89     int32_t OnEmptyBufferDone(const struct OHOS::HDI::Codec::V1_0::OmxCodecBuffer &buffer) override;
90     int32_t OnFillBufferDone(const struct OHOS::HDI::Codec::V1_0::OmxCodecBuffer &buffer) override;
91     int32_t EventHandler(OHOS::HDI::Codec::V1_0::CodecEventType event,
92         const OHOS::HDI::Codec::V1_0::EventInfo &info) override;
93     void WaitForStatusChanged();
94     void OnStatusChanged();
95     bool ReadOnePacket(FILE *fp, char *buf, uint32_t &filledCount);
96 
97 private:
98     int32_t UseBufferOnPort(PortIndex portIndex);
99     int32_t UseBufferOnPort(PortIndex portIndex, int bufferCount, int bufferSize);
100     int32_t UseBufferHandle(int bufferCount, int bufferSize);
101     int32_t CheckAndUseBufferHandle();
102     int GetYuvSize();
103     int32_t ConfigPortDefine();
104     bool FillAllTheBuffer();
105     int GetFreeBufferId();
106     void FreeOutBuffer();
107     void HandleEventPortSettingsChanged(uint32_t data1, uint32_t data2);
108     int32_t GetComponentName(std::string &compName);
AlignUp(uint32_t width)109     uint32_t inline AlignUp(uint32_t width)
110     {
111         return (((width) + alignment_ - 1) & (~(alignment_ - 1)));
112     }
113 
114 private:
115     FILE *fpIn_;  // input file
116     FILE *fpOut_;
117     uint32_t width_;
118     uint32_t height_;
119     uint32_t stride_;
120     OHOS::sptr<OHOS::HDI::Codec::V1_0::ICodecComponent> client_;
121     OHOS::sptr<OHOS::HDI::Codec::V1_0::ICodecCallback> callback_;
122     OHOS::sptr<OHOS::HDI::Codec::V1_0::ICodecComponentManager> omxMgr_;
123     uint32_t componentId_;
124     std::map<int, std::shared_ptr<BufferInfo>> omxBuffers_;  // key is buferid
125     std::list<int> unUsedInBuffers_;
126     std::list<int> unUsedOutBuffers_;
127     std::mutex lockInputBuffers_;
128     std::condition_variable statusCondition_;
129     std::mutex statusLock_;
130     bool exit_;
131     codecMime codecMime_;
132     bool useBufferHandle_;
133     int count_;
134     static CodecUtil *util_;
135     static constexpr uint32_t alignment_ = 16;
136     static OHOS::HDI::Display::Buffer::V1_0::IDisplayBuffer *gralloc_;
137 };
138 
139 #endif /* CODEC_HDI_DECODE_H */
140