Libav
qsv.c
Go to the documentation of this file.
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"
32 
33 #include "avcodec.h"
34 #include "qsv_internal.h"
35 
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
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 static const struct {
58  mfxStatus mfxerr;
59  int averr;
60  const char *desc;
61 } qsv_errors[] = {
62  { MFX_ERR_NONE, 0, "success" },
63  { MFX_ERR_UNKNOWN, AVERROR_UNKNOWN, "unknown error" },
64  { MFX_ERR_NULL_PTR, AVERROR(EINVAL), "NULL pointer" },
65  { MFX_ERR_UNSUPPORTED, AVERROR(ENOSYS), "unsupported" },
66  { MFX_ERR_MEMORY_ALLOC, AVERROR(ENOMEM), "failed to allocate memory" },
67  { MFX_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOMEM), "insufficient input/output buffer" },
68  { MFX_ERR_INVALID_HANDLE, AVERROR(EINVAL), "invalid handle" },
69  { MFX_ERR_LOCK_MEMORY, AVERROR(EIO), "failed to lock the memory block" },
70  { MFX_ERR_NOT_INITIALIZED, AVERROR_BUG, "not initialized" },
71  { MFX_ERR_NOT_FOUND, AVERROR(ENOSYS), "specified object was not found" },
72  /* the following 3 errors should always be handled explicitly, so those "mappings"
73  * are for completeness only */
74  { MFX_ERR_MORE_DATA, AVERROR_UNKNOWN, "expect more data at input" },
75  { MFX_ERR_MORE_SURFACE, AVERROR_UNKNOWN, "expect more surface at output" },
76  { MFX_ERR_MORE_BITSTREAM, AVERROR_UNKNOWN, "expect more bitstream at output" },
77  { MFX_ERR_ABORTED, AVERROR_UNKNOWN, "operation aborted" },
78  { MFX_ERR_DEVICE_LOST, AVERROR(EIO), "device lost" },
79  { MFX_ERR_INCOMPATIBLE_VIDEO_PARAM, AVERROR(EINVAL), "incompatible video parameters" },
80  { MFX_ERR_INVALID_VIDEO_PARAM, AVERROR(EINVAL), "invalid video parameters" },
81  { MFX_ERR_UNDEFINED_BEHAVIOR, AVERROR_BUG, "undefined behavior" },
82  { MFX_ERR_DEVICE_FAILED, AVERROR(EIO), "device failed" },
83  { MFX_ERR_INCOMPATIBLE_AUDIO_PARAM, AVERROR(EINVAL), "incompatible audio parameters" },
84  { MFX_ERR_INVALID_AUDIO_PARAM, AVERROR(EINVAL), "invalid audio parameters" },
85 
86  { MFX_WRN_IN_EXECUTION, 0, "operation in execution" },
87  { MFX_WRN_DEVICE_BUSY, 0, "device busy" },
88  { MFX_WRN_VIDEO_PARAM_CHANGED, 0, "video parameters changed" },
89  { MFX_WRN_PARTIAL_ACCELERATION, 0, "partial acceleration" },
90  { MFX_WRN_INCOMPATIBLE_VIDEO_PARAM, 0, "incompatible video parameters" },
91  { MFX_WRN_VALUE_NOT_CHANGED, 0, "value is saturated" },
92  { MFX_WRN_OUT_OF_RANGE, 0, "value out of range" },
93  { MFX_WRN_FILTER_SKIPPED, 0, "filter skipped" },
94  { MFX_WRN_INCOMPATIBLE_AUDIO_PARAM, 0, "incompatible audio parameters" },
95 };
96 
97 int ff_qsv_map_error(mfxStatus mfx_err, const char **desc)
98 {
99  int i;
100  for (i = 0; i < FF_ARRAY_ELEMS(qsv_errors); i++) {
101  if (qsv_errors[i].mfxerr == mfx_err) {
102  if (desc)
103  *desc = qsv_errors[i].desc;
104  return qsv_errors[i].averr;
105  }
106  }
107  if (desc)
108  *desc = "unknown error";
109  return AVERROR_UNKNOWN;
110 }
111 
112 int ff_qsv_print_error(void *log_ctx, mfxStatus err,
113  const char *error_string)
114 {
115  const char *desc;
116  int ret;
117  ret = ff_qsv_map_error(err, &desc);
118  av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
119  return ret;
120 }
121 
122 int ff_qsv_print_warning(void *log_ctx, mfxStatus err,
123  const char *warning_string)
124 {
125  const char *desc;
126  int ret;
127  ret = ff_qsv_map_error(err, &desc);
128  av_log(log_ctx, AV_LOG_WARNING, "%s: %s (%d)\n", warning_string, desc, err);
129  return ret;
130 }
131 
133 {
134  switch (format) {
135  case AV_PIX_FMT_YUV420P:
136  case AV_PIX_FMT_YUVJ420P:
137  case AV_PIX_FMT_NV12:
138  *fourcc = MFX_FOURCC_NV12;
139  return AV_PIX_FMT_NV12;
141  case AV_PIX_FMT_P010:
142  *fourcc = MFX_FOURCC_P010;
143  return AV_PIX_FMT_P010;
144  default:
145  return AVERROR(ENOSYS);
146  }
147 }
148 
149 static int qsv_load_plugins(mfxSession session, const char *load_plugins,
150  void *logctx)
151 {
152  if (!load_plugins || !*load_plugins)
153  return 0;
154 
155  while (*load_plugins) {
156  mfxPluginUID uid;
157  mfxStatus ret;
158  int i, err = 0;
159 
160  char *plugin = av_get_token(&load_plugins, ":");
161  if (!plugin)
162  return AVERROR(ENOMEM);
163  if (strlen(plugin) != 2 * sizeof(uid.Data)) {
164  av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
165  err = AVERROR(EINVAL);
166  goto load_plugin_fail;
167  }
168 
169  for (i = 0; i < sizeof(uid.Data); i++) {
170  err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
171  if (err != 1) {
172  av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
173  err = AVERROR(EINVAL);
174  goto load_plugin_fail;
175  }
176 
177  }
178 
179  ret = MFXVideoUSER_Load(session, &uid, 1);
180  if (ret < 0) {
181  char errorbuf[128];
182  snprintf(errorbuf, sizeof(errorbuf),
183  "Could not load the requested plugin '%s'", plugin);
184  err = ff_qsv_print_error(logctx, ret, errorbuf);
185  goto load_plugin_fail;
186  }
187 
188  if (*load_plugins)
189  load_plugins++;
190 load_plugin_fail:
191  av_freep(&plugin);
192  if (err < 0)
193  return err;
194  }
195 
196  return 0;
197 
198 }
199 
200 int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
201  const char *load_plugins)
202 {
203  mfxIMPL impl = MFX_IMPL_AUTO_ANY;
204  mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
205 
206  const char *desc;
207  int ret;
208 
209  ret = MFXInit(impl, &ver, session);
210  if (ret < 0)
211  return ff_qsv_print_error(avctx, ret,
212  "Error initializing an internal MFX session");
213 
214  ret = qsv_load_plugins(*session, load_plugins, avctx);
215  if (ret < 0) {
216  av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
217  return ret;
218  }
219 
220  MFXQueryIMPL(*session, &impl);
221 
222  switch (MFX_IMPL_BASETYPE(impl)) {
223  case MFX_IMPL_SOFTWARE:
224  desc = "software";
225  break;
226  case MFX_IMPL_HARDWARE:
227  case MFX_IMPL_HARDWARE2:
228  case MFX_IMPL_HARDWARE3:
229  case MFX_IMPL_HARDWARE4:
230  desc = "hardware accelerated";
231  break;
232  default:
233  desc = "unknown";
234  }
235 
236  av_log(avctx, AV_LOG_VERBOSE,
237  "Initialized an internal MFX session using %s implementation\n",
238  desc);
239 
240  return 0;
241 }
242 
243 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
244  mfxFrameAllocResponse *resp)
245 {
246  QSVFramesContext *ctx = pthis;
247  mfxFrameInfo *i = &req->Info;
248  mfxFrameInfo *i1 = &ctx->info;
249 
250  if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
251  !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
252  !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
253  return MFX_ERR_UNSUPPORTED;
254  if (i->Width != i1->Width || i->Height != i1->Height ||
255  i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
256  av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
257  "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
258  i->Width, i->Height, i->FourCC, i->ChromaFormat,
259  i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
260  return MFX_ERR_UNSUPPORTED;
261  }
262 
263  resp->mids = ctx->mids;
264  resp->NumFrameActual = ctx->nb_mids;
265 
266  return MFX_ERR_NONE;
267 }
268 
269 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
270 {
271  return MFX_ERR_NONE;
272 }
273 
274 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
275 {
276  return MFX_ERR_UNSUPPORTED;
277 }
278 
279 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
280 {
281  return MFX_ERR_UNSUPPORTED;
282 }
283 
284 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
285 {
286  *hdl = mid;
287  return MFX_ERR_NONE;
288 }
289 
290 int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
291  QSVFramesContext *qsv_frames_ctx,
292  const char *load_plugins, int opaque)
293 {
294  static const mfxHandleType handle_types[] = {
295  MFX_HANDLE_VA_DISPLAY,
296  MFX_HANDLE_D3D9_DEVICE_MANAGER,
297  MFX_HANDLE_D3D11_DEVICE,
298  };
299  mfxFrameAllocator frame_allocator = {
300  .pthis = qsv_frames_ctx,
301  .Alloc = qsv_frame_alloc,
302  .Lock = qsv_frame_lock,
303  .Unlock = qsv_frame_unlock,
304  .GetHDL = qsv_frame_get_hdl,
305  .Free = qsv_frame_free,
306  };
307 
308  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
309  AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
310  AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
311  mfxSession parent_session = device_hwctx->session;
312 
313  mfxSession session;
314  mfxVersion ver;
315  mfxIMPL impl;
316  mfxHDL handle = NULL;
317  mfxHandleType handle_type;
318  mfxStatus err;
319 
320  int i, ret;
321 
322  err = MFXQueryIMPL(parent_session, &impl);
323  if (err == MFX_ERR_NONE)
324  err = MFXQueryVersion(parent_session, &ver);
325  if (err != MFX_ERR_NONE)
326  return ff_qsv_print_error(avctx, err,
327  "Error querying the session attributes");
328 
329  for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
330  err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
331  if (err == MFX_ERR_NONE) {
332  handle_type = handle_types[i];
333  break;
334  }
335  handle = NULL;
336  }
337  if (!handle) {
338  av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
339  "from the session\n");
340  }
341 
342  err = MFXInit(impl, &ver, &session);
343  if (err != MFX_ERR_NONE)
344  return ff_qsv_print_error(avctx, err,
345  "Error initializing a child MFX session");
346 
347  if (handle) {
348  err = MFXVideoCORE_SetHandle(session, handle_type, handle);
349  if (err != MFX_ERR_NONE)
350  return ff_qsv_print_error(avctx, err,
351  "Error setting a HW handle");
352  }
353 
354  ret = qsv_load_plugins(session, load_plugins, avctx);
355  if (ret < 0) {
356  av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
357  return ret;
358  }
359 
360  if (!opaque) {
361  av_freep(&qsv_frames_ctx->mids);
362  qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
363  sizeof(*qsv_frames_ctx->mids));
364  if (!qsv_frames_ctx->mids)
365  return AVERROR(ENOMEM);
366 
367  qsv_frames_ctx->info = frames_hwctx->surfaces[0].Info;
368  qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
369  for (i = 0; i < frames_hwctx->nb_surfaces; i++)
370  qsv_frames_ctx->mids[i] = frames_hwctx->surfaces[i].Data.MemId;
371 
372  err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
373  if (err != MFX_ERR_NONE)
374  return ff_qsv_print_error(avctx, err,
375  "Error setting a frame allocator");
376  }
377 
378  *psession = session;
379  return 0;
380 }
uint32_t fourcc
Definition: hwcontext_qsv.c:90
static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req, mfxFrameAllocResponse *resp)
Definition: qsv.c:243
mfxFrameInfo info
Definition: qsv_internal.h:52
static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
Definition: qsv.c:279
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
mfxHandleType handle_type
Definition: hwcontext_qsv.c:75
This struct is allocated as AVHWFramesContext.hwctx.
Definition: hwcontext_qsv.h:42
static int qsv_load_plugins(mfxSession session, const char *load_plugins, void *logctx)
Definition: qsv.c:149
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int averr
Definition: qsv.c:59
#define FF_ARRAY_ELEMS(a)
int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession, QSVFramesContext *qsv_frames_ctx, const char *load_plugins, int opaque)
Definition: qsv.c:290
AVBufferRef * hw_frames_ctx
Definition: qsv_internal.h:51
int ff_qsv_print_error(void *log_ctx, mfxStatus err, const char *error_string)
Definition: qsv.c:112
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
#define AV_PIX_FMT_P010
Definition: pixfmt.h:288
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:85
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
int ff_qsv_map_error(mfxStatus mfx_err, const char **desc)
Convert a libmfx error code into a libav error code.
Definition: qsv.c:97
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_qsv.h:35
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:193
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
error code definitions
#define AVERROR(e)
Definition: error.h:43
static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
Definition: qsv.c:269
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:86
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:121
int ff_qsv_print_warning(void *log_ctx, mfxStatus err, const char *warning_string)
Definition: qsv.c:122
int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
Definition: qsv.c:132
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:142
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:71
#define QSV_VERSION_MINOR
Definition: qsv_internal.h:31
enum AVCodecID codec_id
Definition: avconv_vaapi.c:149
const char * desc
Definition: qsv.c:60
int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
Definition: qsv.c:36
static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
Definition: qsv.c:284
AVFormatContext * ctx
Definition: movenc.c:48
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:198
static const mfxHandleType handle_types[]
NULL
Definition: eval.c:55
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1409
uint8_t * data
The data buffer.
Definition: buffer.h:89
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:155
#define AVERROR_BUG
Bug detected, please report the issue.
Definition: error.h:60
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:263
const char * format
Definition: movenc.c:47
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:117
mfxMemId * mids
Definition: qsv_internal.h:53
static const struct @65 qsv_errors[]
mfxStatus mfxerr
Definition: qsv.c:58
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:59
common internal and external API header
static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
Definition: qsv.c:274
int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session, const char *load_plugins)
Definition: qsv.c:200
#define QSV_VERSION_MAJOR
Definition: qsv_internal.h:30
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:61
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:205
An API-specific header for AV_HWDEVICE_TYPE_QSV.
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57