summaryrefslogtreecommitdiff
path: root/mm-video-v4l2/vidc/common/src/vidc_vendor_extensions.cpp
blob: 3b06bc7caeb53cce60537cc0c1d3a348a047f118 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*--------------------------------------------------------------------------
Copyright (c) 2017, The Linux Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.
    * Neither the name of The Linux Foundation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------*/

#define LOG_TAG "OMX-VENDOR-EXT"
#include <utils/Log.h>
#include "vidc_debug.h"

#include "OMX_Core.h"
#include "OMX_QCOMExtns.h"
#include "OMX_VideoExt.h"
#include "OMX_IndexExt.h"
#include "vidc_vendor_extensions.h"

VendorExtension::VendorExtension(OMX_INDEXTYPE id, const char *name, OMX_DIRTYPE dir,
        const ParamListBuilder& p)
    : mId(id),
      mName(name),
      mPortDir(dir),
      mParams(std::move(p.mParams)),
      mIsSet(false) {
}

// copy extension Info to OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE* struct passed
OMX_ERRORTYPE VendorExtension::copyInfoTo(
        OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext) const {

    // Extension info
    strlcpy((char *)ext->cName, mName.c_str(), OMX_MAX_STRINGNAME_SIZE);
    ext->eDir = mPortDir;
    ext->nParamCount = paramCount();

    // Per-parameter info
    // Must be copied only if there are enough params to fill-in
    if (ext->nParamSizeUsed < ext->nParamCount) {
        return OMX_ErrorNone;
    }

    int i = 0;
    for (const Param& p : mParams) {
        strlcpy((char *)ext->nParam[i].cKey, p.name(), OMX_MAX_STRINGNAME_SIZE);
        ext->nParam[i].bSet = mIsSet ? OMX_TRUE : OMX_FALSE;
        ext->nParam[i].eValueType = p.type();
        ++i;
    }
    return OMX_ErrorNone;
}

bool VendorExtension::setParamInt32(
        OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, const char *paramKey,
        OMX_S32 setInt32) const {
    int paramIndex = indexOfParam(paramKey);
    if (!_isParamAccessTypeOK(ext, paramIndex, OMX_AndroidVendorValueInt32)) {
        return false;
    }
    ext->nParam[paramIndex].nInt32 = setInt32;
    return true;
}

bool VendorExtension::setParamInt64(
        OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, const char *paramKey,
        OMX_S64 setInt64) const {
    int paramIndex = indexOfParam(paramKey);
    if (!_isParamAccessTypeOK(ext, paramIndex, OMX_AndroidVendorValueInt64)) {
        return false;
    }
    ext->nParam[paramIndex].nInt64 = setInt64;
    return true;
}

bool VendorExtension::setParamString(
        OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, const char *paramKey,
        const char *setStr) const {
    int paramIndex = indexOfParam(paramKey);
    if (!_isParamAccessTypeOK(ext, paramIndex, OMX_AndroidVendorValueString)) {
        return false;
    }
    strlcpy((char *)ext->nParam[paramIndex].cString, setStr, OMX_MAX_STRINGVALUE_SIZE);
    return true;
}

bool VendorExtension::readParamInt32(
        OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, const char *paramKey,
        OMX_S32 *readInt32) const {
    int paramIndex = indexOfParam(paramKey);
    if (!_isParamAccessTypeOK(ext, paramIndex, OMX_AndroidVendorValueInt32)) {
        return false;
    }
    if (ext->nParam[paramIndex].bSet == OMX_TRUE) {
        *readInt32 = ext->nParam[paramIndex].nInt32;
        return true;
    }
    return false;
}

bool VendorExtension::readParamInt64(
        OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, const char *paramKey,
        OMX_S64 *readInt64) const {
    int paramIndex = indexOfParam(paramKey);
    if (!_isParamAccessTypeOK(ext, paramIndex, OMX_AndroidVendorValueInt64)) {
        return false;
    }
    if (ext->nParam[paramIndex].bSet == OMX_TRUE) {
        *readInt64 = ext->nParam[paramIndex].nInt64;
        return true;
    }
    return false;
}

bool VendorExtension::readParamString(
            OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, const char *paramKey,
            char *readStr) const {
    int paramIndex = indexOfParam(paramKey);
    if (!_isParamAccessTypeOK(ext, paramIndex, OMX_AndroidVendorValueString)) {
        return false;
    }
    if (ext->nParam[paramIndex].bSet == OMX_TRUE) {
        strlcpy(readStr,
                (const char *)ext->nParam[paramIndex].cString, OMX_MAX_STRINGVALUE_SIZE);
        return true;
    }
    return false;
}

// Checkers
OMX_ERRORTYPE VendorExtension::isConfigValid(
    OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext) const {
    ALOGI("isConfigValid");

    if (ext->nParamSizeUsed < ext->nParamCount) {
        DEBUG_PRINT_ERROR("allotted params(%u) < required(%u) for %s",
                ext->nParamSizeUsed, ext->nParamCount, mName.c_str());
        return OMX_ErrorBadParameter;
    }
    if (ext->nParamCount != paramCount()) {
        DEBUG_PRINT_ERROR("incorrect param count(%u) v/s required(%u) for %s",
                ext->nParamCount, paramCount(), mName.c_str());
        return OMX_ErrorBadParameter;
    }
    if (strncmp((char *)ext->cName, mName.c_str(), OMX_MAX_STRINGNAME_SIZE) != 0) {
        DEBUG_PRINT_ERROR("extension name mismatch(%s) v/s expected(%s)",
                (char *)ext->cName, mName.c_str());
        return OMX_ErrorBadParameter;
    }

    for (OMX_U32 i = 0; i < paramCount(); ++i) {
        if (!_isParamAccessOK(ext, i)) {
            ALOGI("_isParamAccessOK failed for %u", i);
            return OMX_ErrorBadParameter;
        }
    }

    return OMX_ErrorNone;
}

bool VendorExtension::isConfigKey(
         OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext,
         const char *paramKey) const {

    bool retStatus = false;
    if (!strncmp((char *)ext->cName, paramKey, OMX_MAX_STRINGNAME_SIZE)) {
        retStatus = true;
    }

    return retStatus;
}

//static
const char* VendorExtension::typeString(OMX_ANDROID_VENDOR_VALUETYPE type) {
    switch (type) {
        case OMX_AndroidVendorValueInt32: return "Int32";
        case OMX_AndroidVendorValueInt64: return "Int64";
        case OMX_AndroidVendorValueString: return "String";
        default: return "InvalidType";
    }
}

std::string VendorExtension::debugString() const {
    std::string str = "vendor." + mName + "{";
    for (const Param& p : mParams) {
        str += "{ ";
        str += p.name();
        str += " : ";
        str += typeString(p.type());
        str += " },  ";
    }
    str += "}";
    return str;
}

bool VendorExtension::_isParamAccessTypeOK(
        OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, int paramIndex,
        OMX_ANDROID_VENDOR_VALUETYPE type) const {
    if (paramIndex < 0
            || paramIndex >= (int)ext->nParamSizeUsed
            || paramIndex >= (int)paramCount()) {
        DEBUG_PRINT_ERROR("Invalid Param index(%d) for %s (max=%u)",
                paramIndex, mName.c_str(), paramCount());
        return false;
    }
    if (type != mParams[paramIndex].type()) {
        DEBUG_PRINT_ERROR("Invalid Type for field(%s) for %s.%s (expected=%s)",
                typeString(type), mName.c_str(), mParams[paramIndex].name(),
                typeString(mParams[paramIndex].type()));
        return false;
    }
    return true;
}

bool VendorExtension::_isParamAccessOK(
        OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, int paramIndex) const {
    if (paramIndex < 0
            || paramIndex >= (int)ext->nParamSizeUsed
            || paramIndex >= (int)paramCount()) {
        DEBUG_PRINT_ERROR("Invalid Param index(%d) for %s (max=%u)",
                paramIndex, mName.c_str(), paramCount());
        return false;
    }
    if (ext->nParam[paramIndex].eValueType != mParams[paramIndex].type()) {
        DEBUG_PRINT_ERROR("Invalid Type for field(%s) for %s.%s (expected=%s)",
                typeString(ext->nParam[paramIndex].eValueType),
                mName.c_str(), mParams[paramIndex].name(),
                typeString(mParams[paramIndex].type()));
        return false;
    }
    if (strncmp((const char *)ext->nParam[paramIndex].cKey,
            mParams[paramIndex].name(), OMX_MAX_STRINGNAME_SIZE) != 0) {
        DEBUG_PRINT_ERROR("Invalid Key for field(%s) for %s.%s (expected=%s)",
                ext->nParam[paramIndex].cKey,
                mName.c_str(), mParams[paramIndex].name(),
                mParams[paramIndex].name());
        return false;
    }
    return true;
}

int VendorExtension::indexOfParam(const char *key) const {
    int i = 0;
    for (const Param& p : mParams) {
        if (!strncmp(key, p.name(), OMX_MAX_STRINGNAME_SIZE)) {
            return i;
        }
        ++i;
    }
    DEBUG_PRINT_ERROR("Failed to lookup param(%s) in extension(%s)",
            key, mName.c_str());
    return -1;
}

void VendorExtensionStore::dumpExtensions(const char *prefix) const {
    DEBUG_PRINT_HIGH("%s : Vendor extensions supported (%u)", prefix, size());
    for (const VendorExtension& v : mExt) {
        DEBUG_PRINT_HIGH("   %s", v.debugString().c_str());
    }
}