• 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 #ifndef CODEC_HDI_ADAPTER_ENCODE_H
16 #define CODEC_HDI_ADAPTER_ENCODE_H
17 
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 <buffer_handle_utils.h>
24 #include <condition_variable>
25 #include <hdf_log.h>
26 #include <securec.h>
27 #include <deque>
28 #include <list>
29 #include <map>
30 #include <memory>
31 #include <mutex>
32 #include <queue>
33 #include <thread>
34 #include <vector>
35 #include "codec_component_manager.h"
36 #include "codec_component_type.h"
37 #include "command_adapter_parse.h"
38 #include "v1_0/display_composer_type.h"
39 #include "v1_0/display_buffer_type.h"
40 #include "v1_0/include/idisplay_buffer.h"
41 
42 enum class PortIndex { PORT_INDEX_INPUT = 0, PORT_INDEX_OUTPUT = 1 };
43 
44 class CodecHdiAdapterEncode {
45     struct BufferInfo {
46         std::shared_ptr<struct OmxCodecBuffer> omxBuffer;
47         std::shared_ptr<OHOS::Ashmem> avSharedPtr;
48         int bufferHandleId;
49         PortIndex portIndex;
BufferInfoBufferInfo50         BufferInfo()
51         {
52             omxBuffer = nullptr;
53             avSharedPtr = nullptr;
54             portIndex = PortIndex::PORT_INDEX_INPUT;
55             bufferHandleId = -1;
56         }
~BufferInfoBufferInfo57         ~BufferInfo()
58         {
59             omxBuffer = nullptr;
60             if (avSharedPtr) {
61                 avSharedPtr->UnmapAshmem();
62                 avSharedPtr->CloseAshmem();
63                 avSharedPtr = nullptr;
64             }
65             portIndex = PortIndex::PORT_INDEX_INPUT;
66             bufferHandleId = -1;
67         }
68     };
69 
70 public:
71     CodecHdiAdapterEncode();
72     ~CodecHdiAdapterEncode();
73 
74     bool Init(CommandOpt &opt);
75     bool Configure();
76     bool UseBuffers();
77     int32_t UseBufferOnPort(PortIndex portIndex);
78     void FreeBuffers();
79     void Run();
80     void Release();
81     static int32_t OnEvent(struct CodecCallbackType *self, OMX_EVENTTYPE event, struct EventInfo *info);
82 
83     static int32_t OnEmptyBufferDone(
84         struct CodecCallbackType *self, int64_t appData, const struct OmxCodecBuffer *buffer);
85     static int32_t OnFillBufferDone(
86         struct CodecCallbackType *self, int64_t appData, const struct OmxCodecBuffer *buffer);
87     template <typename T>
InitParam(T & param)88     inline void InitParam(T &param)
89     {
90         int32_t ret = memset_s(&param, sizeof(param), 0x0, sizeof(param));
91         if (ret != EOK) {
92             HDF_LOGE("%{public}s: memset_s param err [%{public}d].", __func__, ret);
93             return;
94         }
95         param.nSize = sizeof(param);
96         param.nVersion.s.nVersionMajor = 1;
97     }
98     template <typename T>
InitParamInOhos(T & param)99     inline void InitParamInOhos(T &param)
100     {
101         int32_t ret = memset_s(&param, sizeof(param), 0x0, sizeof(param));
102         if (ret != EOK) {
103             HDF_LOGE("%{public}s: memset_s param err [%{public}d].", __func__, ret);
104             return;
105         }
106         param.size = sizeof(param);
107         param.version.s.nVersionMajor = 1;  // mVersion.s.nVersionMajor;
108     }
109     void WaitForStatusChanged();
110     void OnStatusChanged();
111     bool ReadOneFrame(FILE *fp, char *buf, uint32_t &filledCount);
112 
113 private:
114     int32_t OnEmptyBufferDone(const struct OmxCodecBuffer &buffer);
115     int32_t OnFillBufferDone(const struct OmxCodecBuffer &buffer);
116     int32_t ConfigBitMode();
117     int32_t UseBufferOnPort(PortIndex portIndex, int bufferCount, int bufferSize);
118     bool FillAllTheBuffer();
119     int GetFreeBufferId();
120     int32_t ConfigPortDefine();
121     int32_t ConfigMppPassthrough();
122     int32_t ConfigMppExtPassthrough(int32_t codecType);
123     int32_t CheckAndUseBufferHandle();
124     int32_t UseDynaBuffer(int bufferCount, int bufferSize);
125     bool FillCodecBuffer(std::shared_ptr<BufferInfo> bufferInfo, bool &endFlag);
126     int32_t CreateBufferHandle();
127     void FreeBufferHandle();
AlignUp(uint32_t width)128     uint32_t inline AlignUp(uint32_t width)
129     {
130         return (((width) + alignment_ - 1) & (~(alignment_ - 1)));
131     }
132 
133 private:
134     FILE *fpIn_;  // input file
135     FILE *fpOut_;
136     uint32_t width_;
137     uint32_t height_;
138     uint32_t stride_;
139     uint32_t srcFileSize_;
140     uint32_t totalSrcSize_;
141     struct CodecComponentType *client_;
142     struct CodecCallbackType *callback_;
143     struct CodecComponentManager *omxMgr_;
144     uint32_t componentId_;
145     std::map<int, std::shared_ptr<BufferInfo>> omxBuffers_;  // key is bufferID
146     std::list<int> unUsedInBuffers_;
147     std::list<int> unUsedOutBuffers_;
148     std::mutex lockInputBuffers_;
149     std::condition_variable statusCondition_;
150     std::mutex statusLock_;
151     bool exit_;
152     std::map<int, BufferHandle *> bufferHandles_;
153     std::list<int> freeBufferHandles_;
154     bool useBufferHandle_;
155     static constexpr uint32_t alignment_ = 16;
156     static OHOS::HDI::Display::Buffer::V1_0::IDisplayBuffer *gralloc_;
157 };
158 
159 #endif  // CODEC_HDI_ADAPTER_ENCODE_H
160