• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 CODEC_HDI_ADAPTER_DECODE_H
17 #define CODEC_HDI_ADAPTER_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 <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 
39 enum class PortIndex { PORT_INDEX_INPUT = 0, PORT_INDEX_OUTPUT = 1 };
40 
41 class CodecHdiAdapterDecode {
42     struct BufferInfo {
43         std::shared_ptr<OmxCodecBuffer> omxBuffer;
44         std::shared_ptr<OHOS::Ashmem> avSharedPtr;
45         PortIndex portIndex;
46         BufferHandle *bufferHandle;
BufferInfoBufferInfo47         BufferInfo()
48         {
49             omxBuffer = nullptr;
50             avSharedPtr = nullptr;
51             portIndex = PortIndex::PORT_INDEX_INPUT;
52             bufferHandle = nullptr;
53         }
~BufferInfoBufferInfo54         ~BufferInfo()
55         {
56             omxBuffer = nullptr;
57             if (avSharedPtr != nullptr) {
58                 avSharedPtr->UnmapAshmem();
59                 avSharedPtr->CloseAshmem();
60                 avSharedPtr = nullptr;
61             }
62             portIndex = PortIndex::PORT_INDEX_INPUT;
63         }
64     };
65 
66 public:
67     CodecHdiAdapterDecode();
68     ~CodecHdiAdapterDecode();
69     bool Init(CommandOpt &opt);
70     bool Configure();
71     bool UseBuffers();
72     void FreeBuffers();
73     void start();
74     void Run();
75     void Release();
76     static int32_t OnEvent(struct CodecCallbackType *self, OMX_EVENTTYPE event, struct EventInfo *info);
77     static int32_t OnEmptyBufferDone(
78         struct CodecCallbackType *self, int64_t appData, const struct OmxCodecBuffer *buffer);
79     static int32_t OnFillBufferDone(
80         struct CodecCallbackType *self, int64_t appData, const struct OmxCodecBuffer *buffer);
81     template <typename T>
InitParam(T & param)82     inline void InitParam(T &param)
83     {
84         int32_t ret = memset_s(&param, sizeof(param), 0x0, sizeof(param));
85         if (ret != EOK) {
86             HDF_LOGE("%{public}s: memset_s param err [%{public}d].", __func__, ret);
87             return;
88         }
89         param.nSize = sizeof(param);
90         param.nVersion.s.nVersionMajor = 1;  // mVersion.s.nVersionMajor;
91     }
92     template <typename T>
InitParamInOhos(T & param)93     inline void InitParamInOhos(T &param)
94     {
95         int32_t ret = memset_s(&param, sizeof(param), 0x0, sizeof(param));
96         if (ret != EOK) {
97             HDF_LOGE("%{public}s: memset_s param err [%{public}d].", __func__, ret);
98             return;
99         }
100         param.size = sizeof(param);
101         param.version.s.nVersionMajor = 1;  // mVersion.s.nVersionMajor;
102     }
103     void WaitForStatusChanged();
104     void OnStatusChanged();
105     bool ReadOnePacket(FILE *fp, uint8_t *buf, uint32_t &filledCount);
106     bool ReadOneFrameFromFile(FILE *fp, uint8_t *buf, uint32_t &filledCount);
107     void DumpOutputToFile(FILE *fp, uint8_t *addr);
108 
109 private:
110     int32_t UseBufferOnPort(PortIndex portIndex);
111     int32_t UseBufferOnPort(PortIndex portIndex, 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     int32_t ConfigMppPassthrough();
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     uint32_t inputBufferSize_;
131     uint32_t needSplit_;
132     uint32_t srcFileSize_;
133     uint32_t totalSrcSize_;
134     struct CodecComponentType *client_;
135     struct CodecCallbackType *callback_;
136     struct CodecComponentManager *omxMgr_;
137     uint32_t componentId_;
138     std::map<int, std::shared_ptr<BufferInfo>> omxBuffers_;  // key is buferid
139     std::list<int> unUsedInBuffers_;
140     std::list<int> unUsedOutBuffers_;
141     std::mutex lockInputBuffers_;
142     std::condition_variable statusCondition_;
143     std::mutex statusLock_;
144     bool exit_;
145     codecMime codecMime_;
146     bool useBufferHandle_;
147     int count_;
148     static constexpr uint32_t alignment_ = 16;
149 };
150 #endif /* CODEC_HDI_ADAPTER_DECODE_H */
151