]> git.sesse.net Git - ffmpeg/blob - libavcodec/qsv.c
Merge commit 'fc5cdc0d5372f5103c71d5dede296734fe71ead2'
[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 int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
109 {
110     switch (format) {
111     case AV_PIX_FMT_YUV420P:
112     case AV_PIX_FMT_YUVJ420P:
113         *fourcc = MFX_FOURCC_NV12;
114         return AV_PIX_FMT_NV12;
115     case AV_PIX_FMT_YUV420P10:
116         *fourcc = MFX_FOURCC_P010;
117         return AV_PIX_FMT_P010;
118     default:
119         return AVERROR(ENOSYS);
120     }
121 }
122
123 static int qsv_load_plugins(mfxSession session, const char *load_plugins,
124                             void *logctx)
125 {
126     if (!load_plugins || !*load_plugins)
127         return 0;
128
129     while (*load_plugins) {
130         mfxPluginUID uid;
131         mfxStatus ret;
132         int i, err = 0;
133
134         char *plugin = av_get_token(&load_plugins, ":");
135         if (!plugin)
136             return AVERROR(ENOMEM);
137         if (strlen(plugin) != 2 * sizeof(uid.Data)) {
138             av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
139             err = AVERROR(EINVAL);
140             goto load_plugin_fail;
141         }
142
143         for (i = 0; i < sizeof(uid.Data); i++) {
144             err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
145             if (err != 1) {
146                 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
147                 err = AVERROR(EINVAL);
148                 goto load_plugin_fail;
149             }
150
151         }
152
153         ret = MFXVideoUSER_Load(session, &uid, 1);
154         if (ret < 0) {
155             av_log(logctx, AV_LOG_ERROR, "Could not load the requested plugin: %s\n",
156                    plugin);
157             err = ff_qsv_error(ret);
158             goto load_plugin_fail;
159         }
160
161         if (*load_plugins)
162             load_plugins++;
163 load_plugin_fail:
164         av_freep(&plugin);
165         if (err < 0)
166             return err;
167     }
168
169     return 0;
170
171 }
172
173 int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
174                                  const char *load_plugins)
175 {
176     mfxIMPL impl   = MFX_IMPL_AUTO_ANY;
177     mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
178
179     const char *desc;
180     int ret;
181
182     ret = MFXInit(impl, &ver, session);
183     if (ret < 0) {
184         av_log(avctx, AV_LOG_ERROR, "Error initializing an internal MFX session\n");
185         return ff_qsv_error(ret);
186     }
187
188     ret = qsv_load_plugins(*session, load_plugins, avctx);
189     if (ret < 0) {
190         av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
191         return ret;
192     }
193
194     MFXQueryIMPL(*session, &impl);
195
196     switch (MFX_IMPL_BASETYPE(impl)) {
197     case MFX_IMPL_SOFTWARE:
198         desc = "software";
199         break;
200     case MFX_IMPL_HARDWARE:
201     case MFX_IMPL_HARDWARE2:
202     case MFX_IMPL_HARDWARE3:
203     case MFX_IMPL_HARDWARE4:
204         desc = "hardware accelerated";
205         break;
206     default:
207         desc = "unknown";
208     }
209
210     av_log(avctx, AV_LOG_VERBOSE,
211            "Initialized an internal MFX session using %s implementation\n",
212            desc);
213
214     return 0;
215 }
216
217 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
218                                  mfxFrameAllocResponse *resp)
219 {
220     QSVFramesContext *ctx = pthis;
221     mfxFrameInfo      *i  = &req->Info;
222     mfxFrameInfo      *i1 = &ctx->info;
223
224     if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
225         !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
226         !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
227         return MFX_ERR_UNSUPPORTED;
228     if (i->Width  != i1->Width || i->Height != i1->Height ||
229         i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
230         av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
231                "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
232                i->Width,  i->Height,  i->FourCC,  i->ChromaFormat,
233                i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
234         return MFX_ERR_UNSUPPORTED;
235     }
236
237     resp->mids           = ctx->mids;
238     resp->NumFrameActual = ctx->nb_mids;
239
240     return MFX_ERR_NONE;
241 }
242
243 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
244 {
245     return MFX_ERR_NONE;
246 }
247
248 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
249 {
250     return MFX_ERR_UNSUPPORTED;
251 }
252
253 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
254 {
255     return MFX_ERR_UNSUPPORTED;
256 }
257
258 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
259 {
260     *hdl = mid;
261     return MFX_ERR_NONE;
262 }
263
264 int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
265                                   QSVFramesContext *qsv_frames_ctx,
266                                   const char *load_plugins, int opaque)
267 {
268     static const mfxHandleType handle_types[] = {
269         MFX_HANDLE_VA_DISPLAY,
270         MFX_HANDLE_D3D9_DEVICE_MANAGER,
271         MFX_HANDLE_D3D11_DEVICE,
272     };
273     mfxFrameAllocator frame_allocator = {
274         .pthis  = qsv_frames_ctx,
275         .Alloc  = qsv_frame_alloc,
276         .Lock   = qsv_frame_lock,
277         .Unlock = qsv_frame_unlock,
278         .GetHDL = qsv_frame_get_hdl,
279         .Free   = qsv_frame_free,
280     };
281
282     AVHWFramesContext    *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
283     AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
284     AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
285     mfxSession        parent_session = device_hwctx->session;
286
287     mfxSession    session;
288     mfxVersion    ver;
289     mfxIMPL       impl;
290     mfxHDL        handle = NULL;
291     mfxHandleType handle_type;
292     mfxStatus err;
293
294     int i, ret;
295
296     err = MFXQueryIMPL(parent_session, &impl);
297     if (err == MFX_ERR_NONE)
298         err = MFXQueryVersion(parent_session, &ver);
299     if (err != MFX_ERR_NONE) {
300         av_log(avctx, AV_LOG_ERROR, "Error querying the session attributes\n");
301         return ff_qsv_error(err);
302     }
303
304     for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
305         err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
306         if (err == MFX_ERR_NONE) {
307             handle_type = handle_types[i];
308             break;
309         }
310         handle = NULL;
311     }
312     if (!handle) {
313         av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
314                "from the session\n");
315     }
316
317     err = MFXInit(impl, &ver, &session);
318     if (err != MFX_ERR_NONE) {
319         av_log(avctx, AV_LOG_ERROR,
320                "Error initializing a child MFX session: %d\n", err);
321         return ff_qsv_error(err);
322     }
323
324     if (handle) {
325         err = MFXVideoCORE_SetHandle(session, handle_type, handle);
326         if (err != MFX_ERR_NONE) {
327             av_log(avctx, AV_LOG_ERROR, "Error setting a HW handle: %d\n", err);
328             return ff_qsv_error(err);
329         }
330     }
331
332     ret = qsv_load_plugins(session, load_plugins, avctx);
333     if (ret < 0) {
334         av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
335         return ret;
336     }
337
338     if (!opaque) {
339         av_freep(&qsv_frames_ctx->mids);
340         qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
341                                                 sizeof(*qsv_frames_ctx->mids));
342         if (!qsv_frames_ctx->mids)
343             return AVERROR(ENOMEM);
344
345         qsv_frames_ctx->info    = frames_hwctx->surfaces[0].Info;
346         qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
347         for (i = 0; i < frames_hwctx->nb_surfaces; i++)
348             qsv_frames_ctx->mids[i] = frames_hwctx->surfaces[i].Data.MemId;
349
350         err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
351         if (err != MFX_ERR_NONE) {
352             av_log(avctx, AV_LOG_ERROR, "Error setting a frame allocator: %d\n", err);
353             return ff_qsv_error(err);
354         }
355     }
356
357     *psession = session;
358     return 0;
359 }