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