• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 #include <OMX_Component.h>
19 #include <OMX_Core.h>
20 #include <OMX_VideoExt.h>
21 #include <ashmem.h>
22 #include <buffer_handle.h>
23 #include <condition_variable>
24 #include <idisplay_gralloc.h>
25 #include <list>
26 #include <map>
27 #include <memory>
28 #include <mutex>
29 #include "codec_callback_type_service.h"
30 #include "codec_callback_type_stub.h"
31 #include "codec_component_manager.h"
32 #include "codec_component_type.h"
33 #include "codec_types.h"
34 #include "command_parse.h"
35 
36 enum class PortIndex { PORT_INDEX_INPUT = 0, PORT_INDEX_OUTPUT = 1 };
37 
38 class CodecHdiDecode {
39     struct BufferInfo {
40         std::shared_ptr<OmxCodecBuffer> omxBuffer;
41         std::shared_ptr<OHOS::Ashmem> avSharedPtr;
42         PortIndex portIndex;
43         BufferHandle *bufferHandle;
BufferInfoBufferInfo44         BufferInfo()
45         {
46             omxBuffer = nullptr;
47             avSharedPtr = nullptr;
48             portIndex = PortIndex::PORT_INDEX_INPUT;
49             bufferHandle = nullptr;
50         }
~BufferInfoBufferInfo51         ~BufferInfo()
52         {
53             omxBuffer = nullptr;
54             if (avSharedPtr != nullptr) {
55                 avSharedPtr->UnmapAshmem();
56                 avSharedPtr->CloseAshmem();
57                 avSharedPtr = nullptr;
58             }
59             if (bufferHandle != nullptr && gralloc_ != nullptr) {
60                 gralloc_->FreeMem(*bufferHandle);
61                 bufferHandle = nullptr;
62             }
63             portIndex = PortIndex::PORT_INDEX_INPUT;
64         }
setBufferHandleBufferInfo65         void setBufferHandle(BufferHandle *bufferHandle)
66         {
67             if (this->bufferHandle != nullptr) {
68                 if (gralloc_ != nullptr) {
69                     gralloc_->FreeMem(*this->bufferHandle);
70                 }
71             }
72             this->bufferHandle = bufferHandle;
73         }
74     };
75 
76 public:
77     explicit CodecHdiDecode();
78     ~CodecHdiDecode();
79     bool Init(CommandOpt &opt);
80     bool Configure();
81     bool UseBuffers();
82     void FreeBuffers();
83     void Run();
84     void Release();
85     static int32_t OnEvent(struct CodecCallbackType *self, OMX_EVENTTYPE event, struct EventInfo *info);
86     static int32_t OnEmptyBufferDone(struct CodecCallbackType *self, int64_t appData,
87                                      const struct OmxCodecBuffer *buffer);
88     static int32_t OnFillBufferDone(struct CodecCallbackType *self, int64_t appData,
89                                     const struct OmxCodecBuffer *buffer);
90     template <typename T>
InitParam(T & param)91     inline void InitParam(T &param)
92     {
93         memset_s(&param, sizeof(param), 0x0, sizeof(param));
94         param.nSize = sizeof(param);
95         param.nVersion.s.nVersionMajor = 1;  // mVersion.s.nVersionMajor;
96     }
97     template <typename T>
InitParamInOhos(T & param)98     inline void InitParamInOhos(T &param)
99     {
100         memset_s(&param, sizeof(param), 0x0, sizeof(param));
101         param.size = sizeof(param);
102         param.version.s.nVersionMajor = 1;  // mVersion.s.nVersionMajor;
103     }
104     void WaitForStatusChanged();
105     void OnStatusChanged();
106     bool ReadOnePacket(FILE *fp, char *buf, uint32_t &filledCount);
107 
108 private:
109     int32_t UseBufferOnPort(PortIndex portIndex);
110     int32_t UseBufferOnPort(PortIndex portIndex, int bufferCount, int bufferSize);
111     int32_t UseBufferHandle(int bufferCount, int bufferSize);
112     int32_t OnEmptyBufferDone(const struct OmxCodecBuffer &buffer);
113     int32_t OnFillBufferDone(const struct OmxCodecBuffer &buffer);
114     int32_t CheckAndUseBufferHandle();
115     int GetYuvSize();
116     int32_t ConfigPortDefine();
117     bool FillAllTheBuffer();
118     int GetFreeBufferId();
AlignUp(uint32_t width)119     uint32_t inline AlignUp(uint32_t width)
120     {
121         return (((width) + alignment_ - 1) & (~(alignment_ - 1)));
122     }
123 
124 private:
125     FILE *fpIn_;  // input file
126     FILE *fpOut_;
127     uint32_t width_;
128     uint32_t height_;
129     uint32_t stride_;
130     struct CodecComponentType *client_;
131     struct CodecCallbackType *callback_;
132     struct CodecComponentManager *omxMgr_;
133     uint32_t componentId_;
134     std::map<int, std::shared_ptr<BufferInfo>> omxBuffers_;  // key is buferid
135     std::list<int> unUsedInBuffers_;
136     std::list<int> unUsedOutBuffers_;
137     std::mutex lockInputBuffers_;
138     std::condition_variable statusCondition_;
139     std::mutex statusLock_;
140     bool exit_;
141     codecMime codecMime_;
142     bool useBufferHandle_;
143     int count_;
144     static constexpr uint32_t alignment_ = 16;
145     static OHOS::HDI::Display::V1_0::IDisplayGralloc *gralloc_;
146 };
147 #endif /* CODEC_HDI_DECODE_H */