hwcontext: add a QSV implementation
[libav.git] / libavcodec / qsv.c
CommitLineData
d0a63d8b
AK
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>
66acb76b 22#include <mfx/mfxplugin.h>
d0a63d8b 23
66acb76b
AK
24#include <stdio.h>
25#include <string.h>
26
27#include "libavutil/avstring.h"
d0a63d8b
AK
28#include "libavutil/error.h"
29
30#include "avcodec.h"
31#include "qsv_internal.h"
32
33int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
34{
35 switch (codec_id) {
36 case AV_CODEC_ID_H264:
37 return MFX_CODEC_AVC;
66acb76b
AK
38#if QSV_VERSION_ATLEAST(1, 8)
39 case AV_CODEC_ID_HEVC:
40 return MFX_CODEC_HEVC;
41#endif
d0a63d8b
AK
42 case AV_CODEC_ID_MPEG1VIDEO:
43 case AV_CODEC_ID_MPEG2VIDEO:
44 return MFX_CODEC_MPEG2;
45 case AV_CODEC_ID_VC1:
46 return MFX_CODEC_VC1;
47 default:
48 break;
49 }
50
51 return AVERROR(ENOSYS);
52}
53
54int ff_qsv_error(int mfx_err)
55{
56 switch (mfx_err) {
57 case MFX_ERR_NONE:
58 return 0;
59 case MFX_ERR_MEMORY_ALLOC:
60 case MFX_ERR_NOT_ENOUGH_BUFFER:
61 return AVERROR(ENOMEM);
62 case MFX_ERR_INVALID_HANDLE:
63 return AVERROR(EINVAL);
64 case MFX_ERR_DEVICE_FAILED:
65 case MFX_ERR_DEVICE_LOST:
66 case MFX_ERR_LOCK_MEMORY:
67 return AVERROR(EIO);
68 case MFX_ERR_NULL_PTR:
69 case MFX_ERR_UNDEFINED_BEHAVIOR:
70 case MFX_ERR_NOT_INITIALIZED:
71 return AVERROR_BUG;
72 case MFX_ERR_UNSUPPORTED:
73 case MFX_ERR_NOT_FOUND:
74 return AVERROR(ENOSYS);
75 case MFX_ERR_MORE_DATA:
76 case MFX_ERR_MORE_SURFACE:
77 case MFX_ERR_MORE_BITSTREAM:
78 return AVERROR(EAGAIN);
79 case MFX_ERR_INCOMPATIBLE_VIDEO_PARAM:
80 case MFX_ERR_INVALID_VIDEO_PARAM:
81 return AVERROR(EINVAL);
82 case MFX_ERR_ABORTED:
83 case MFX_ERR_UNKNOWN:
84 default:
85 return AVERROR_UNKNOWN;
86 }
87}
88
66acb76b
AK
89int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
90 const char *load_plugins)
d0a63d8b
AK
91{
92 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
93 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
94
95 const char *desc;
96 int ret;
97
98 ret = MFXInit(impl, &ver, session);
99 if (ret < 0) {
100 av_log(avctx, AV_LOG_ERROR, "Error initializing an internal MFX session\n");
101 return ff_qsv_error(ret);
102 }
103
66acb76b
AK
104 if (load_plugins && *load_plugins) {
105 while (*load_plugins) {
106 mfxPluginUID uid;
107 int i, err = 0;
108
109 char *plugin = av_get_token(&load_plugins, ":");
110 if (!plugin)
111 return AVERROR(ENOMEM);
112 if (strlen(plugin) != 2 * sizeof(uid.Data)) {
113 av_log(avctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
114 err = AVERROR(EINVAL);
115 goto load_plugin_fail;
116 }
117
118 for (i = 0; i < sizeof(uid.Data); i++) {
119 err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
120 if (err != 1) {
121 av_log(avctx, AV_LOG_ERROR, "Invalid plugin UID\n");
122 err = AVERROR(EINVAL);
123 goto load_plugin_fail;
124 }
125
126 }
127
128 ret = MFXVideoUSER_Load(*session, &uid, 1);
129 if (ret < 0) {
130 av_log(avctx, AV_LOG_ERROR, "Could not load the requested plugin: %s\n",
131 plugin);
132 err = ff_qsv_error(ret);
133 goto load_plugin_fail;
134 }
135
92c1a83e
LB
136 if (*load_plugins)
137 load_plugins++;
66acb76b
AK
138load_plugin_fail:
139 av_freep(&plugin);
140 if (err < 0)
141 return err;
142 }
143 }
144
ce9d7da7
LB
145 MFXQueryIMPL(*session, &impl);
146
147 switch (MFX_IMPL_BASETYPE(impl)) {
148 case MFX_IMPL_SOFTWARE:
149 desc = "software";
150 break;
151 case MFX_IMPL_HARDWARE:
152 case MFX_IMPL_HARDWARE2:
153 case MFX_IMPL_HARDWARE3:
154 case MFX_IMPL_HARDWARE4:
155 desc = "hardware accelerated";
156 break;
157 default:
158 desc = "unknown";
159 }
160
d0a63d8b
AK
161 av_log(avctx, AV_LOG_VERBOSE,
162 "Initialized an internal MFX session using %s implementation\n",
163 desc);
164
165 return 0;
166}