1 // Copyright (C) 2020 The Android Open Source Project
2 //
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 #include "host-common/H264PingInfoParser.h"
16 
17 #define H264_DEBUG 0
18 
19 #if H264_DEBUG
20 #define RED "\x1B[31m"
21 #define GRN "\x1B[32m"
22 #define YEL "\x1B[33m"
23 #define BLU "\x1B[34m"
24 #define MAG "\x1B[35m"
25 #define CYN "\x1B[36m"
26 #define WHT "\x1B[37m"
27 #define RESET "\x1B[0m"
28 #define H264_PRINT(color, fmt, ...)                                    \
29     fprintf(stderr, color "H264PingInfoParser: %s:%d " fmt "\n" RESET, \
30             __func__, __LINE__, ##__VA_ARGS__);
31 #else
32 #define H264_PRINT(fmt, ...)
33 #endif
34 
35 #define H264_INFO(fmt, ...) H264_PRINT(RESET, fmt, ##__VA_ARGS__);
36 #define H264_WARN(fmt, ...) H264_PRINT(YEL, fmt, ##__VA_ARGS__);
37 #define H264_ERROR(fmt, ...) H264_PRINT(RED, fmt, ##__VA_ARGS__);
38 
39 namespace android {
40 namespace emulation {
41 
H264PingInfoParser(void * ptr)42 H264PingInfoParser::H264PingInfoParser(void* ptr) {
43     mVersion = parseVersion(ptr);
44 }
45 
H264PingInfoParser(uint32_t version)46 H264PingInfoParser::H264PingInfoParser(uint32_t version) {
47     mVersion = version;
48 }
49 
parseVersion(void * ptr)50 uint32_t H264PingInfoParser::parseVersion(void* ptr) {
51     uint8_t* xptr = (uint8_t*)ptr;
52     uint32_t version = *(uint32_t*)xptr;
53     return version;
54 }
55 
parseInitContextParams(void * ptr,InitContextParam & param)56 void H264PingInfoParser::parseInitContextParams(void* ptr,
57                                                 InitContextParam& param) {
58     uint8_t* xptr = (uint8_t*)ptr;
59     param.version = *(uint32_t*)xptr;
60     param.width = *(unsigned int*)(xptr + 8);
61     param.height = *(unsigned int*)(xptr + 16);
62     param.outputWidth = *(unsigned int*)(xptr + 24);
63     param.outputHeight = *(unsigned int*)(xptr + 32);
64     param.outputPixelFormat = static_cast<PixelFormat>(*(uint8_t*)(xptr + 40));
65     param.pHostDecoderId = static_cast<uint64_t*>(getReturnAddress(ptr));
66 }
67 
parseHostDecoderId(void * ptr)68 uint64_t H264PingInfoParser::parseHostDecoderId(void* ptr) {
69     if (nullptr == ptr)
70         return 0;
71     uint64_t key = (*reinterpret_cast<uint64_t*>(ptr));
72     return key;
73 }
74 
parseDecodeFrameParams(void * ptr,DecodeFrameParam & param)75 void H264PingInfoParser::parseDecodeFrameParams(void* ptr,
76                                                 DecodeFrameParam& param) {
77     param.hostDecoderId = parseHostDecoderId(ptr);
78     uint64_t offset = *(uint64_t*)((uint8_t*)ptr + 8);
79     param.pData = (uint8_t*)ptr + offset;
80     param.size = *(size_t*)((uint8_t*)ptr + 16);
81     param.pts = *(size_t*)((uint8_t*)ptr + 24);
82     uint8_t* retptr = (uint8_t*)getReturnAddress(ptr);
83     param.pConsumedBytes = (size_t*)(retptr);
84     param.pDecoderErrorCode = (int*)(retptr + 8);
85 }
86 
parseResetParams(void * ptr,ResetParam & param)87 void H264PingInfoParser::parseResetParams(void* ptr, ResetParam& param) {
88     uint8_t* xptr = (uint8_t*)ptr;
89     param.hostDecoderId = parseHostDecoderId(ptr);
90     param.width = *(unsigned int*)(xptr + 8);
91     param.height = *(unsigned int*)(xptr + 16);
92     param.outputWidth = *(unsigned int*)(xptr + 24);
93     param.outputHeight = *(unsigned int*)(xptr + 32);
94     param.outputPixelFormat = static_cast<PixelFormat>(*(uint8_t*)(xptr + 40));
95 }
96 
parseHostColorBufferId(void * ptr)97 int32_t H264PingInfoParser::parseHostColorBufferId(void* ptr) {
98     // Guest will pass us the hsot color buffer id to send decoded frame to
99     uint8_t* xptr = (uint8_t*)ptr;
100     int32_t colorBufferId = *(int32_t*)(xptr + 16);
101     return colorBufferId;
102 }
103 
getReturnAddress(void * ptr)104 void* H264PingInfoParser::getReturnAddress(void* ptr) {
105     uint8_t* xptr = (uint8_t*)ptr;
106     void* pint = (void*)(xptr + 256);
107     return pint;
108 }
109 
parseGetImageParams(void * ptr,GetImageParam & param)110 void H264PingInfoParser::parseGetImageParams(void* ptr, GetImageParam& param) {
111     param.hostDecoderId = parseHostDecoderId(ptr);
112     if (mVersion == 100) {
113         param.hostColorBufferId = -1;
114     } else if (mVersion == 200) {
115         param.hostColorBufferId = parseHostColorBufferId(ptr);
116     }
117     uint8_t* retptr = (uint8_t*)getReturnAddress(ptr);
118     param.pDecoderErrorCode = (int*)(retptr);
119     param.pRetWidth = (uint32_t*)(retptr + 8);
120     param.pRetHeight = (uint32_t*)(retptr + 16);
121     param.pRetPts = (uint64_t*)(retptr + 24);
122     param.pRetColorPrimaries = (uint32_t*)(retptr + 32);
123     param.pRetColorRange = (uint32_t*)(retptr + 40);
124     param.pRetColorTransfer = (uint32_t*)(retptr + 48);
125     param.pRetColorSpace = (uint32_t*)(retptr + 56);
126 
127     uint8_t* xptr = (uint8_t*)ptr;
128     uint64_t offset = *(uint64_t*)(xptr + 8);
129     param.pDecodedFrame = (uint8_t*)ptr + offset;
130 }
131 
132 }  // namespace emulation
133 }  // namespace android
134