File: plugin_loader.h

package info (click to toggle)
libvpl-tools 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,652 kB
  • sloc: cpp: 107,469; python: 4,303; ansic: 3,202; sh: 159; lisp: 52; makefile: 13
file content (212 lines) | stat: -rw-r--r-- 7,437 bytes parent folder | download | duplicates (2)
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
/*############################################################################
  # Copyright (C) 2005 Intel Corporation
  #
  # SPDX-License-Identifier: MIT
  ############################################################################*/

#pragma once

#ifndef __PLUGIN_LOADER_H__
    #define __PLUGIN_LOADER_H__

    #include "plugin_utils.h"
    #include "sample_utils.h"
    #include "vm/so_defs.h"

    #include <iomanip> // for std::setfill, std::setw
    #include <iostream>
    #include <memory> // for std::unique_ptr

class MsdkSoModule {
protected:
    msdk_so_handle m_module;

public:
    MsdkSoModule() : m_module(NULL) {}
    MsdkSoModule(const std::string& pluginName) : m_module(NULL) {
        m_module = msdk_so_load(pluginName.c_str());
        if (NULL == m_module) {
            MSDK_TRACE_ERROR(std::string("Failed to load shared module: ") + pluginName);
        }
    }
    template <class T>
    T GetAddr(const std::string& fncName) {
        T pCreateFunc = reinterpret_cast<T>(msdk_so_get_addr(m_module, fncName.c_str()));
        if (NULL == pCreateFunc) {
            MSDK_TRACE_ERROR(std::string("Failed to get function addres: ") + fncName.c_str());
        }
        return pCreateFunc;
    }

    virtual ~MsdkSoModule() {
        if (m_module) {
            msdk_so_free(m_module);
            m_module = NULL;
        }
    }
};

/*
* Rationale: class to load+register any mediasdk plugin decoder/encoder/generic by given name
*/
class PluginLoader : public MFXPlugin {
protected:
    mfxPluginType ePluginType;

    mfxSession m_session;
    mfxPluginUID m_uid;

private:
    const char* msdkGetPluginName(const mfxPluginUID& guid) {
        if (AreGuidsEqual(guid, MFX_PLUGINID_HEVCD_SW))
            return "Intel (R) Media SDK plugin for HEVC DECODE";
        else if (AreGuidsEqual(guid, MFX_PLUGINID_HEVCD_HW))
            return "Intel (R) Media SDK HW plugin for HEVC DECODE";
        else if (AreGuidsEqual(guid, MFX_PLUGINID_HEVCE_SW))
            return "Intel (R) Media SDK plugin for HEVC ENCODE";
        else if (AreGuidsEqual(guid, MFX_PLUGINID_HEVCE_HW))
            return "Intel (R) Media SDK HW plugin for HEVC ENCODE";
        else if (AreGuidsEqual(guid, MFX_PLUGINID_H264LA_HW))
            return "Intel (R) Media SDK plugin for LA ENC";
        else if (AreGuidsEqual(guid, MFX_PLUGINID_ITELECINE_HW))
            return "Intel (R) Media SDK PTIR plugin (HW)";
        else if (AreGuidsEqual(guid, MFX_PLUGINID_HEVCE_GACC))
            return "Intel (R) Media SDK GPU-Accelerated plugin for HEVC ENCODE";
        else if (AreGuidsEqual(guid, MFX_PLUGINID_VP9D_HW))
            return "Intel (R) Media SDK HW plugin for VP9 DECODE";
        else if (AreGuidsEqual(guid, MFX_PLUGINID_VP9E_HW))
            return "Intel (R) Media SDK HW plugin for VP9 ENCODE";
        else
    #if !defined(_WIN32) && !defined(_WIN64)
            if (AreGuidsEqual(guid, MFX_PLUGINID_HEVC_FEI_ENCODE))
            return "Intel (R) Media SDK HW plugin for HEVC FEI ENCODE";
        else
    #endif
            return "Unknown plugin";
    }

public:
    PluginLoader(mfxPluginType type,
                 mfxSession session,
                 const mfxPluginUID& uid,
                 mfxU32 version,
                 const mfxChar* pluginName,
                 mfxU32 len)
            : ePluginType(type),
              m_session(),
              m_uid() {
        mfxStatus sts = MFX_ERR_NONE;
        std::stringstream strStream;

        MSDK_MEMCPY(&m_uid, sizeof(m_uid), &uid, sizeof(uid));
        for (size_t i = 0; i != sizeof(mfxPluginUID); i++) {
            strStream << "0x" << std::setfill('0') << std::setw(2) << std::hex
                      << (int)m_uid.Data[i];
            if (i != (sizeof(mfxPluginUID) - 1))
                strStream << ", ";
        }

        if ((ePluginType == MFX_PLUGINTYPE_AUDIO_DECODE) ||
            (ePluginType == MFX_PLUGINTYPE_AUDIO_ENCODE)) {
            // Audio plugins are not loaded by path
            sts = MFX_ERR_UNSUPPORTED;
        }
        else {
            sts = MFXVideoUSER_LoadByPath(session, &m_uid, version, pluginName, len);
        }

        if (MFX_ERR_NONE != sts) {
            MSDK_TRACE_ERROR("Failed to load plugin from GUID, sts="
                             << sts << ": { " << strStream.str() << " } ("
                             << msdkGetPluginName(m_uid) << ")");
        }
        else {
            MSDK_TRACE_INFO("Plugin was loaded from GUID");
            m_session = session;
        }
    }

    PluginLoader(mfxPluginType type, mfxSession session, const mfxPluginUID& uid, mfxU32 version)
            : ePluginType(type),
              m_session(),
              m_uid() {
        mfxStatus sts = MFX_ERR_NONE;
        std::stringstream strStream;

        MSDK_MEMCPY(&m_uid, sizeof(m_uid), &uid, sizeof(uid));
        for (size_t i = 0; i != sizeof(mfxPluginUID); i++) {
            strStream << "0x" << std::setfill('0') << std::setw(2) << std::hex
                      << (int)m_uid.Data[i];
            if (i != (sizeof(mfxPluginUID) - 1))
                strStream << ", ";
        }

        if ((ePluginType == MFX_PLUGINTYPE_AUDIO_DECODE) ||
            (ePluginType == MFX_PLUGINTYPE_AUDIO_ENCODE)) {
            sts = MFXAudioUSER_Load(session, &m_uid, version);
        }
        else {
            sts = MFXVideoUSER_Load(session, &m_uid, version);
        }

        if (MFX_ERR_NONE != sts) {
            MSDK_TRACE_ERROR("Failed to load plugin from GUID, sts="
                             << sts << ": { " << strStream.str() << " } ("
                             << msdkGetPluginName(m_uid) << ")");
        }
        else {
            MSDK_TRACE_INFO("Plugin was loaded from GUID"
                            << ": { " << strStream.str() << " } (" << msdkGetPluginName(m_uid)
                            << ")");
            m_session = session;
        }
    }

    virtual ~PluginLoader() {
        mfxStatus sts = MFX_ERR_NONE;
        if (m_session) {
            if ((ePluginType == MFX_PLUGINTYPE_AUDIO_DECODE) ||
                (ePluginType == MFX_PLUGINTYPE_AUDIO_ENCODE)) {
                sts = MFXAudioUSER_UnLoad(m_session, &m_uid);
            }
            else {
                sts = MFXVideoUSER_UnLoad(m_session, &m_uid);
            }

            if (sts != MFX_ERR_NONE) {
                MSDK_TRACE_ERROR("Failed to unload plugin from GUID, sts=" << sts);
            }
            else {
                MSDK_TRACE_INFO("MFXBaseUSER_UnLoad(session=0x" << m_session << "), sts=" << sts);
            }
        }
    }

    bool IsOk() {
        return m_session != 0;
    }
    virtual mfxStatus PluginInit(mfxCoreInterface* /*core*/) {
        return MFX_ERR_NULL_PTR;
    }
    virtual mfxStatus PluginClose() {
        return MFX_ERR_NULL_PTR;
    }
    virtual mfxStatus GetPluginParam(mfxPluginParam* /*par*/) {
        return MFX_ERR_NULL_PTR;
    }
    virtual mfxStatus Execute(mfxThreadTask /*task*/, mfxU32 /*uid_p*/, mfxU32 /*uid_a*/) {
        return MFX_ERR_NULL_PTR;
    }
    virtual mfxStatus FreeResources(mfxThreadTask /*task*/, mfxStatus /*sts*/) {
        return MFX_ERR_NULL_PTR;
    }
    virtual void Release() {}
    virtual mfxStatus Close() {
        return MFX_ERR_NULL_PTR;
    }
    virtual mfxStatus SetAuxParams(void* /*auxParam*/, int /*auxParamSize*/) {
        return MFX_ERR_NULL_PTR;
    }
};

#endif // PLUGIN_LOADER