aboutsummaryrefslogtreecommitdiff
path: root/system/codecs/c2/decoders/hevcdec/MediaHevcDecoder.cpp
blob: f1bc356f4a333ec71467bd2780508eb871957cb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <utils/Log.h>

#define DEBUG 0
#if DEBUG
#define DDD(...) ALOGD(__VA_ARGS__)
#else
#define DDD(...) ((void)0)
#endif

#include "MediaHevcDecoder.h"
#include "goldfish_media_utils.h"
#include <string.h>

MediaHevcDecoder::MediaHevcDecoder(RenderMode renderMode)
    : mRenderMode(renderMode) {
    if (renderMode == RenderMode::RENDER_BY_HOST_GPU) {
        mVersion = 200;
    } else if (renderMode == RenderMode::RENDER_BY_GUEST_CPU) {
        mVersion = 100;
    }
}

void MediaHevcDecoder::initHevcContext(unsigned int width, unsigned int height,
                                       unsigned int outWidth,
                                       unsigned int outHeight,
                                       PixelFormat pixFmt) {
    auto transport = GoldfishMediaTransport::getInstance();
    if (!mHasAddressSpaceMemory) {
        int slot = transport->getMemorySlot();
        if (slot < 0) {
            ALOGE("ERROR: Failed to initHevcContext: cannot get memory slot");
            return;
        }
        mSlot = slot;
        mAddressOffSet = static_cast<unsigned int>(mSlot) * (1 << 20);
        DDD("got memory lot %d addrr %x", mSlot, mAddressOffSet);
        mHasAddressSpaceMemory = true;
    }
    transport->writeParam(mVersion, 0, mAddressOffSet);
    transport->writeParam(width, 1, mAddressOffSet);
    transport->writeParam(height, 2, mAddressOffSet);
    transport->writeParam(outWidth, 3, mAddressOffSet);
    transport->writeParam(outHeight, 4, mAddressOffSet);
    transport->writeParam(static_cast<uint64_t>(pixFmt), 5, mAddressOffSet);
    transport->sendOperation(MediaCodecType::HevcCodec,
                             MediaOperation::InitContext, mAddressOffSet);
    auto *retptr = transport->getReturnAddr(mAddressOffSet);
    mHostHandle = *(uint64_t *)(retptr);
    DDD("initHevcContext: got handle to host %lld", mHostHandle);
}

void MediaHevcDecoder::resetHevcContext(unsigned int width, unsigned int height,
                                        unsigned int outWidth,
                                        unsigned int outHeight,
                                        PixelFormat pixFmt) {
    auto transport = GoldfishMediaTransport::getInstance();
    if (!mHasAddressSpaceMemory) {
        ALOGE("%s no address space memory", __func__);
        return;
    }
    transport->writeParam((uint64_t)mHostHandle, 0, mAddressOffSet);
    transport->writeParam(width, 1, mAddressOffSet);
    transport->writeParam(height, 2, mAddressOffSet);
    transport->writeParam(outWidth, 3, mAddressOffSet);
    transport->writeParam(outHeight, 4, mAddressOffSet);
    transport->writeParam(static_cast<uint64_t>(pixFmt), 5, mAddressOffSet);
    transport->sendOperation(MediaCodecType::HevcCodec, MediaOperation::Reset,
                             mAddressOffSet);
    DDD("resetHevcContext: done");
}

void MediaHevcDecoder::destroyHevcContext() {

    DDD("return memory lot %d addrr %x", (int)(mAddressOffSet >> 23),
        mAddressOffSet);
    auto transport = GoldfishMediaTransport::getInstance();
    transport->writeParam((uint64_t)mHostHandle, 0, mAddressOffSet);
    transport->sendOperation(MediaCodecType::HevcCodec,
                             MediaOperation::DestroyContext, mAddressOffSet);
    transport->returnMemorySlot(mSlot);
    mHasAddressSpaceMemory = false;
}

hevc_result_t MediaHevcDecoder::decodeFrame(uint8_t *img, size_t szBytes,
                                            uint64_t pts) {
    DDD("decode frame: use handle to host %lld", mHostHandle);
    hevc_result_t res = {0, 0};
    if (!mHasAddressSpaceMemory) {
        ALOGE("%s no address space memory", __func__);
        return res;
    }
    auto transport = GoldfishMediaTransport::getInstance();
    uint8_t *hostSrc = transport->getInputAddr(mAddressOffSet);
    if (img != nullptr && szBytes > 0) {
        memcpy(hostSrc, img, szBytes);
    }
    transport->writeParam((uint64_t)mHostHandle, 0, mAddressOffSet);
    transport->writeParam(transport->offsetOf((uint64_t)(hostSrc)) -
                              mAddressOffSet,
                          1, mAddressOffSet);
    transport->writeParam((uint64_t)szBytes, 2, mAddressOffSet);
    transport->writeParam((uint64_t)pts, 3, mAddressOffSet);
    transport->sendOperation(MediaCodecType::HevcCodec,
                             MediaOperation::DecodeImage, mAddressOffSet);

    auto *retptr = transport->getReturnAddr(mAddressOffSet);
    res.bytesProcessed = *(uint64_t *)(retptr);
    res.ret = *(int *)(retptr + 8);

    return res;
}

void MediaHevcDecoder::sendMetadata(MetaDataColorAspects *ptr) {
    DDD("send metadata to host %p", ptr);
    if (!mHasAddressSpaceMemory) {
        ALOGE("%s no address space memory", __func__);
        return;
    }
    MetaDataColorAspects& meta = *ptr;
    auto transport = GoldfishMediaTransport::getInstance();
    transport->writeParam((uint64_t)mHostHandle, 0, mAddressOffSet);
    transport->writeParam(meta.type, 1, mAddressOffSet);
    transport->writeParam(meta.primaries, 2, mAddressOffSet);
    transport->writeParam(meta.range, 3, mAddressOffSet);
    transport->writeParam(meta.transfer, 4, mAddressOffSet);
    transport->sendOperation(MediaCodecType::HevcCodec, MediaOperation::SendMetadata, mAddressOffSet);
}

void MediaHevcDecoder::flush() {
    if (!mHasAddressSpaceMemory) {
        ALOGE("%s no address space memory", __func__);
        return;
    }
    DDD("flush: use handle to host %lld", mHostHandle);
    auto transport = GoldfishMediaTransport::getInstance();
    transport->writeParam((uint64_t)mHostHandle, 0, mAddressOffSet);
    transport->sendOperation(MediaCodecType::HevcCodec, MediaOperation::Flush,
                             mAddressOffSet);
}

hevc_image_t MediaHevcDecoder::getImage() {
    DDD("getImage: use handle to host %lld", mHostHandle);
    hevc_image_t res{};
    if (!mHasAddressSpaceMemory) {
        ALOGE("%s no address space memory", __func__);
        return res;
    }
    auto transport = GoldfishMediaTransport::getInstance();
    uint8_t *dst = transport->getInputAddr(
        mAddressOffSet); // Note: reuse the same addr for input and output
    transport->writeParam((uint64_t)mHostHandle, 0, mAddressOffSet);
    transport->writeParam(transport->offsetOf((uint64_t)(dst)) - mAddressOffSet,
                          1, mAddressOffSet);
    transport->writeParam(-1, 2, mAddressOffSet);
    transport->sendOperation(MediaCodecType::HevcCodec,
                             MediaOperation::GetImage, mAddressOffSet);
    auto *retptr = transport->getReturnAddr(mAddressOffSet);
    res.ret = *(int *)(retptr);
    if (res.ret >= 0) {
        res.data = dst;
        res.width = *(uint32_t *)(retptr + 8);
        res.height = *(uint32_t *)(retptr + 16);
        res.pts = *(uint64_t *)(retptr + 24);
        res.color_primaries = *(uint32_t *)(retptr + 32);
        res.color_range = *(uint32_t *)(retptr + 40);
        res.color_trc = *(uint32_t *)(retptr + 48);
        res.colorspace = *(uint32_t *)(retptr + 56);
    } else if (res.ret == (int)(Err::DecoderRestarted)) {
        res.width = *(uint32_t *)(retptr + 8);
        res.height = *(uint32_t *)(retptr + 16);
    }
    return res;
}

hevc_image_t
MediaHevcDecoder::renderOnHostAndReturnImageMetadata(int hostColorBufferId) {
    DDD("%s: use handle to host %lld", __func__, mHostHandle);
    hevc_image_t res{};
    if (hostColorBufferId < 0) {
        ALOGE("%s negative color buffer id %d", __func__, hostColorBufferId);
        return res;
    }
    DDD("%s send color buffer id %d", __func__, hostColorBufferId);
    if (!mHasAddressSpaceMemory) {
        ALOGE("%s no address space memory", __func__);
        return res;
    }
    auto transport = GoldfishMediaTransport::getInstance();
    uint8_t *dst = transport->getInputAddr(
        mAddressOffSet); // Note: reuse the same addr for input and output
    transport->writeParam((uint64_t)mHostHandle, 0, mAddressOffSet);
    transport->writeParam(transport->offsetOf((uint64_t)(dst)) - mAddressOffSet,
                          1, mAddressOffSet);
    transport->writeParam((uint64_t)hostColorBufferId, 2, mAddressOffSet);
    transport->sendOperation(MediaCodecType::HevcCodec,
                             MediaOperation::GetImage, mAddressOffSet);
    auto *retptr = transport->getReturnAddr(mAddressOffSet);
    res.ret = *(int *)(retptr);
    if (res.ret >= 0) {
        res.data = dst; // note: the data could be junk
        res.width = *(uint32_t *)(retptr + 8);
        res.height = *(uint32_t *)(retptr + 16);
        res.pts = *(uint64_t *)(retptr + 24);
        res.color_primaries = *(uint32_t *)(retptr + 32);
        res.color_range = *(uint32_t *)(retptr + 40);
        res.color_trc = *(uint32_t *)(retptr + 48);
        res.colorspace = *(uint32_t *)(retptr + 56);
    } else if (res.ret == (int)(Err::DecoderRestarted)) {
        res.width = *(uint32_t *)(retptr + 8);
        res.height = *(uint32_t *)(retptr + 16);
    }
    return res;
}