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