]> git.sesse.net Git - ffmpeg/blob - libavcodec/qsv.c
qsvenc: support getting the session from an AVHWFramesContext
[ffmpeg] / libavcodec / qsv.c
1 /*
2  * Intel MediaSDK QSV encoder/decoder shared code
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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_error(int mfx_err)
58 {
59     switch (mfx_err) {
60     case MFX_ERR_NONE:
61         return 0;
62     case MFX_ERR_MEMORY_ALLOC:
63     case MFX_ERR_NOT_ENOUGH_BUFFER:
64         return AVERROR(ENOMEM);
65     case MFX_ERR_INVALID_HANDLE:
66         return AVERROR(EINVAL);
67     case MFX_ERR_DEVICE_FAILED:
68     case MFX_ERR_DEVICE_LOST:
69     case MFX_ERR_LOCK_MEMORY:
70         return AVERROR(EIO);
71     case MFX_ERR_NULL_PTR:
72     case MFX_ERR_UNDEFINED_BEHAVIOR:
73     case MFX_ERR_NOT_INITIALIZED:
74         return AVERROR_BUG;
75     case MFX_ERR_UNSUPPORTED:
76     case MFX_ERR_NOT_FOUND:
77         return AVERROR(ENOSYS);
78     case MFX_ERR_MORE_DATA:
79     case MFX_ERR_MORE_SURFACE:
80     case MFX_ERR_MORE_BITSTREAM:
81         return AVERROR(EAGAIN);
82     case MFX_ERR_INCOMPATIBLE_VIDEO_PARAM:
83     case MFX_ERR_INVALID_VIDEO_PARAM:
84         return AVERROR(EINVAL);
85     case MFX_ERR_ABORTED:
86     case MFX_ERR_UNKNOWN:
87     default:
88         return AVERROR_UNKNOWN;
89     }
90 }
91
92 static int qsv_load_plugins(mfxSession session, const char *load_plugins,
93                             void *logctx)
94 {
95     if (!load_plugins || !*load_plugins)
96         return 0;
97
98     while (*load_plugins) {
99         mfxPluginUID uid;
100         mfxStatus ret;
101         int i, err = 0;
102
103         char *plugin = av_get_token(&load_plugins, ":");
104         if (!plugin)
105             return AVERROR(ENOMEM);
106         if (strlen(plugin) != 2 * sizeof(uid.Data)) {
107             av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
108             err = AVERROR(EINVAL);
109             goto load_plugin_fail;
110         }
111
112         for (i = 0; i < sizeof(uid.Data); i++) {
113             err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
114             if (err != 1) {
115                 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
116                 err = AVERROR(EINVAL);
117                 goto load_plugin_fail;
118             }
119
120         }
121
122         ret = MFXVideoUSER_Load(session, &uid, 1);
123         if (ret < 0) {
124             av_log(logctx, AV_LOG_ERROR, "Could not load the requested plugin: %s\n",
125                    plugin);
126             err = ff_qsv_error(ret);
127             goto load_plugin_fail;
128         }
129
130         if (*load_plugins)
131             load_plugins++;
132 load_plugin_fail:
133         av_freep(&plugin);
134         if (err < 0)
135             return err;
136     }
137
138     return 0;
139
140 }
141
142 int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
143                                  const char *load_plugins)
144 {
145     mfxIMPL impl   = MFX_IMPL_AUTO_ANY;
146     mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
147
148     const char *desc;
149     int ret;
150
151     ret = MFXInit(impl, &ver, session);
152     if (ret < 0) {
153         av_log(avctx, AV_LOG_ERROR, "Error initializing an internal MFX session\n");
154         return ff_qsv_error(ret);
155     }
156
157     ret = qsv_load_plugins(*session, load_plugins, avctx);
158     if (ret < 0) {
159         av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
160         return ret;
161     }
162
163     MFXQueryIMPL(*session, &impl);
164
165     switch (MFX_IMPL_BASETYPE(impl)) {
166     case MFX_IMPL_SOFTWARE:
167         desc = "software";
168         break;
169     case MFX_IMPL_HARDWARE:
170     case MFX_IMPL_HARDWARE2:
171     case MFX_IMPL_HARDWARE3:
172     case MFX_IMPL_HARDWARE4:
173         desc = "hardware accelerated";
174         break;
175     default:
176         desc = "unknown";
177     }
178
179     av_log(avctx, AV_LOG_VERBOSE,
180            "Initialized an internal MFX session using %s implementation\n",
181            desc);
182
183     return 0;
184 }
185
186 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
187                                  mfxFrameAllocResponse *resp)
188 {
189     QSVFramesContext *ctx = pthis;
190     mfxFrameInfo      *i  = &req->Info;
191     mfxFrameInfo      *i1 = &ctx->info;
192
193     if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
194         !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
195         !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
196         return MFX_ERR_UNSUPPORTED;
197     if (i->Width  != i1->Width || i->Height != i1->Height ||
198         i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
199         av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
200                "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
201                i->Width,  i->Height,  i->FourCC,  i->ChromaFormat,
202                i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
203         return MFX_ERR_UNSUPPORTED;
204     }
205
206     resp->mids           = ctx->mids;
207     resp->NumFrameActual = ctx->nb_mids;
208
209     return MFX_ERR_NONE;
210 }
211
212 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
213 {
214     return MFX_ERR_NONE;
215 }
216
217 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
218 {
219     return MFX_ERR_UNSUPPORTED;
220 }
221
222 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
223 {
224     return MFX_ERR_UNSUPPORTED;
225 }
226
227 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
228 {
229     *hdl = mid;
230     return MFX_ERR_NONE;
231 }
232
233 int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
234                                   QSVFramesContext *qsv_frames_ctx,
235                                   const char *load_plugins, int opaque)
236 {
237     static const mfxHandleType handle_types[] = {
238         MFX_HANDLE_VA_DISPLAY,
239         MFX_HANDLE_D3D9_DEVICE_MANAGER,
240         MFX_HANDLE_D3D11_DEVICE,
241     };
242     mfxFrameAllocator frame_allocator = {
243         .pthis  = qsv_frames_ctx,
244         .Alloc  = qsv_frame_alloc,
245         .Lock   = qsv_frame_lock,
246         .Unlock = qsv_frame_unlock,
247         .GetHDL = qsv_frame_get_hdl,
248         .Free   = qsv_frame_free,
249     };
250
251     AVHWFramesContext    *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
252     AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
253     AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
254     mfxSession        parent_session = device_hwctx->session;
255
256     mfxSession    session;
257     mfxVersion    ver;
258     mfxIMPL       impl;
259     mfxHDL        handle = NULL;
260     mfxHandleType handle_type;
261     mfxStatus err;
262
263     int i, ret;
264
265     err = MFXQueryIMPL(parent_session, &impl);
266     if (err == MFX_ERR_NONE)
267         err = MFXQueryVersion(parent_session, &ver);
268     if (err != MFX_ERR_NONE) {
269         av_log(avctx, AV_LOG_ERROR, "Error querying the session attributes\n");
270         return ff_qsv_error(err);
271     }
272
273     for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
274         err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
275         if (err == MFX_ERR_NONE) {
276             handle_type = handle_types[i];
277             break;
278         }
279         handle = NULL;
280     }
281     if (!handle) {
282         av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
283                "from the session\n");
284     }
285
286     err = MFXInit(impl, &ver, &session);
287     if (err != MFX_ERR_NONE) {
288         av_log(avctx, AV_LOG_ERROR,
289                "Error initializing a child MFX session: %d\n", err);
290         return ff_qsv_error(err);
291     }
292
293     if (handle) {
294         err = MFXVideoCORE_SetHandle(session, handle_type, handle);
295         if (err != MFX_ERR_NONE) {
296             av_log(avctx, AV_LOG_ERROR, "Error setting a HW handle: %d\n", err);
297             return ff_qsv_error(err);
298         }
299     }
300
301     ret = qsv_load_plugins(session, load_plugins, avctx);
302     if (ret < 0) {
303         av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
304         return ret;
305     }
306
307     if (!opaque) {
308         av_freep(&qsv_frames_ctx->mids);
309         qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
310                                                 sizeof(*qsv_frames_ctx->mids));
311         if (!qsv_frames_ctx->mids)
312             return AVERROR(ENOMEM);
313
314         qsv_frames_ctx->info    = frames_hwctx->surfaces[0].Info;
315         qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
316         for (i = 0; i < frames_hwctx->nb_surfaces; i++)
317             qsv_frames_ctx->mids[i] = frames_hwctx->surfaces[i].Data.MemId;
318
319         err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
320         if (err != MFX_ERR_NONE) {
321             av_log(avctx, AV_LOG_ERROR, "Error setting a frame allocator: %d\n", err);
322             return ff_qsv_error(err);
323         }
324     }
325
326     *psession = session;
327     return 0;
328 }