]> git.sesse.net Git - ffmpeg/blob - libavcodec/qsv.c
Merge commit 'b55566db4c51d920a6496455bb30a608e5a50a41'
[ffmpeg] / libavcodec / qsv.c
1 /*
2  * Intel MediaSDK QSV encoder/decoder shared code
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <mfx/mfxvideo.h>
22 #include <mfx/mfxplugin.h>
23
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "libavutil/avstring.h"
28 #include "libavutil/common.h"
29 #include "libavutil/error.h"
30 #include "libavutil/hwcontext.h"
31 #include "libavutil/hwcontext_qsv.h"
32
33 #include "avcodec.h"
34 #include "qsv_internal.h"
35
36 int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
37 {
38     switch (codec_id) {
39     case AV_CODEC_ID_H264:
40         return MFX_CODEC_AVC;
41 #if QSV_VERSION_ATLEAST(1, 8)
42     case AV_CODEC_ID_HEVC:
43         return MFX_CODEC_HEVC;
44 #endif
45     case AV_CODEC_ID_MPEG1VIDEO:
46     case AV_CODEC_ID_MPEG2VIDEO:
47         return MFX_CODEC_MPEG2;
48     case AV_CODEC_ID_VC1:
49         return MFX_CODEC_VC1;
50     default:
51         break;
52     }
53
54     return AVERROR(ENOSYS);
55 }
56
57 int ff_qsv_profile_to_mfx(enum AVCodecID codec_id, int profile)
58 {
59     if (profile == FF_PROFILE_UNKNOWN)
60         return MFX_PROFILE_UNKNOWN;
61     switch (codec_id) {
62     case AV_CODEC_ID_H264:
63     case AV_CODEC_ID_HEVC:
64         return profile;
65     case AV_CODEC_ID_VC1:
66         return 4 * profile + 1;
67     case AV_CODEC_ID_MPEG2VIDEO:
68         return 0x10 * profile;
69     }
70     return MFX_PROFILE_UNKNOWN;
71 }
72
73 int ff_qsv_error(int mfx_err)
74 {
75     switch (mfx_err) {
76     case MFX_ERR_NONE:
77         return 0;
78     case MFX_ERR_MEMORY_ALLOC:
79     case MFX_ERR_NOT_ENOUGH_BUFFER:
80         return AVERROR(ENOMEM);
81     case MFX_ERR_INVALID_HANDLE:
82         return AVERROR(EINVAL);
83     case MFX_ERR_DEVICE_FAILED:
84     case MFX_ERR_DEVICE_LOST:
85     case MFX_ERR_LOCK_MEMORY:
86         return AVERROR(EIO);
87     case MFX_ERR_NULL_PTR:
88     case MFX_ERR_UNDEFINED_BEHAVIOR:
89     case MFX_ERR_NOT_INITIALIZED:
90         return AVERROR_BUG;
91     case MFX_ERR_UNSUPPORTED:
92     case MFX_ERR_NOT_FOUND:
93         return AVERROR(ENOSYS);
94     case MFX_ERR_MORE_DATA:
95     case MFX_ERR_MORE_SURFACE:
96     case MFX_ERR_MORE_BITSTREAM:
97         return AVERROR(EAGAIN);
98     case MFX_ERR_INCOMPATIBLE_VIDEO_PARAM:
99     case MFX_ERR_INVALID_VIDEO_PARAM:
100         return AVERROR(EINVAL);
101     case MFX_ERR_ABORTED:
102     case MFX_ERR_UNKNOWN:
103     default:
104         return AVERROR_UNKNOWN;
105     }
106 }
107
108 static int qsv_load_plugins(mfxSession session, const char *load_plugins,
109                             void *logctx)
110 {
111     if (!load_plugins || !*load_plugins)
112         return 0;
113
114     while (*load_plugins) {
115         mfxPluginUID uid;
116         mfxStatus ret;
117         int i, err = 0;
118
119         char *plugin = av_get_token(&load_plugins, ":");
120         if (!plugin)
121             return AVERROR(ENOMEM);
122         if (strlen(plugin) != 2 * sizeof(uid.Data)) {
123             av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
124             err = AVERROR(EINVAL);
125             goto load_plugin_fail;
126         }
127
128         for (i = 0; i < sizeof(uid.Data); i++) {
129             err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
130             if (err != 1) {
131                 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
132                 err = AVERROR(EINVAL);
133                 goto load_plugin_fail;
134             }
135
136         }
137
138         ret = MFXVideoUSER_Load(session, &uid, 1);
139         if (ret < 0) {
140             av_log(logctx, AV_LOG_ERROR, "Could not load the requested plugin: %s\n",
141                    plugin);
142             err = ff_qsv_error(ret);
143             goto load_plugin_fail;
144         }
145
146         if (*load_plugins)
147             load_plugins++;
148 load_plugin_fail:
149         av_freep(&plugin);
150         if (err < 0)
151             return err;
152     }
153
154     return 0;
155
156 }
157
158 int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
159                                  const char *load_plugins)
160 {
161     mfxIMPL impl   = MFX_IMPL_AUTO_ANY;
162     mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
163
164     const char *desc;
165     int ret;
166
167     ret = MFXInit(impl, &ver, session);
168     if (ret < 0) {
169         av_log(avctx, AV_LOG_ERROR, "Error initializing an internal MFX session\n");
170         return ff_qsv_error(ret);
171     }
172
173     ret = qsv_load_plugins(*session, load_plugins, avctx);
174     if (ret < 0) {
175         av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
176         return ret;
177     }
178
179     MFXQueryIMPL(*session, &impl);
180
181     switch (MFX_IMPL_BASETYPE(impl)) {
182     case MFX_IMPL_SOFTWARE:
183         desc = "software";
184         break;
185     case MFX_IMPL_HARDWARE:
186     case MFX_IMPL_HARDWARE2:
187     case MFX_IMPL_HARDWARE3:
188     case MFX_IMPL_HARDWARE4:
189         desc = "hardware accelerated";
190         break;
191     default:
192         desc = "unknown";
193     }
194
195     av_log(avctx, AV_LOG_VERBOSE,
196            "Initialized an internal MFX session using %s implementation\n",
197            desc);
198
199     return 0;
200 }
201
202 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
203                                  mfxFrameAllocResponse *resp)
204 {
205     QSVFramesContext *ctx = pthis;
206     mfxFrameInfo      *i  = &req->Info;
207     mfxFrameInfo      *i1 = &ctx->info;
208
209     if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
210         !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
211         !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
212         return MFX_ERR_UNSUPPORTED;
213     if (i->Width  != i1->Width || i->Height != i1->Height ||
214         i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
215         av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
216                "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
217                i->Width,  i->Height,  i->FourCC,  i->ChromaFormat,
218                i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
219         return MFX_ERR_UNSUPPORTED;
220     }
221
222     resp->mids           = ctx->mids;
223     resp->NumFrameActual = ctx->nb_mids;
224
225     return MFX_ERR_NONE;
226 }
227
228 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
229 {
230     return MFX_ERR_NONE;
231 }
232
233 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
234 {
235     return MFX_ERR_UNSUPPORTED;
236 }
237
238 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
239 {
240     return MFX_ERR_UNSUPPORTED;
241 }
242
243 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
244 {
245     *hdl = mid;
246     return MFX_ERR_NONE;
247 }
248
249 int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
250                                   QSVFramesContext *qsv_frames_ctx,
251                                   const char *load_plugins, int opaque)
252 {
253     static const mfxHandleType handle_types[] = {
254         MFX_HANDLE_VA_DISPLAY,
255         MFX_HANDLE_D3D9_DEVICE_MANAGER,
256         MFX_HANDLE_D3D11_DEVICE,
257     };
258     mfxFrameAllocator frame_allocator = {
259         .pthis  = qsv_frames_ctx,
260         .Alloc  = qsv_frame_alloc,
261         .Lock   = qsv_frame_lock,
262         .Unlock = qsv_frame_unlock,
263         .GetHDL = qsv_frame_get_hdl,
264         .Free   = qsv_frame_free,
265     };
266
267     AVHWFramesContext    *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
268     AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
269     AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
270     mfxSession        parent_session = device_hwctx->session;
271
272     mfxSession    session;
273     mfxVersion    ver;
274     mfxIMPL       impl;
275     mfxHDL        handle = NULL;
276     mfxHandleType handle_type;
277     mfxStatus err;
278
279     int i, ret;
280
281     err = MFXQueryIMPL(parent_session, &impl);
282     if (err == MFX_ERR_NONE)
283         err = MFXQueryVersion(parent_session, &ver);
284     if (err != MFX_ERR_NONE) {
285         av_log(avctx, AV_LOG_ERROR, "Error querying the session attributes\n");
286         return ff_qsv_error(err);
287     }
288
289     for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
290         err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
291         if (err == MFX_ERR_NONE) {
292             handle_type = handle_types[i];
293             break;
294         }
295         handle = NULL;
296     }
297     if (!handle) {
298         av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
299                "from the session\n");
300     }
301
302     err = MFXInit(impl, &ver, &session);
303     if (err != MFX_ERR_NONE) {
304         av_log(avctx, AV_LOG_ERROR,
305                "Error initializing a child MFX session: %d\n", err);
306         return ff_qsv_error(err);
307     }
308
309     if (handle) {
310         err = MFXVideoCORE_SetHandle(session, handle_type, handle);
311         if (err != MFX_ERR_NONE) {
312             av_log(avctx, AV_LOG_ERROR, "Error setting a HW handle: %d\n", err);
313             return ff_qsv_error(err);
314         }
315     }
316
317     ret = qsv_load_plugins(session, load_plugins, avctx);
318     if (ret < 0) {
319         av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
320         return ret;
321     }
322
323     if (!opaque) {
324         av_freep(&qsv_frames_ctx->mids);
325         qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
326                                                 sizeof(*qsv_frames_ctx->mids));
327         if (!qsv_frames_ctx->mids)
328             return AVERROR(ENOMEM);
329
330         qsv_frames_ctx->info    = frames_hwctx->surfaces[0].Info;
331         qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
332         for (i = 0; i < frames_hwctx->nb_surfaces; i++)
333             qsv_frames_ctx->mids[i] = frames_hwctx->surfaces[i].Data.MemId;
334
335         err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
336         if (err != MFX_ERR_NONE) {
337             av_log(avctx, AV_LOG_ERROR, "Error setting a frame allocator: %d\n", err);
338             return ff_qsv_error(err);
339         }
340     }
341
342     *psession = session;
343     return 0;
344 }