You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
13KB

  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. #include <mfx/mfxvideo.h>
  21. #include <mfx/mfxplugin.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/error.h"
  27. #include "libavutil/hwcontext.h"
  28. #include "libavutil/hwcontext_qsv.h"
  29. #include "avcodec.h"
  30. #include "qsv_internal.h"
  31. int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
  32. {
  33. switch (codec_id) {
  34. case AV_CODEC_ID_H264:
  35. return MFX_CODEC_AVC;
  36. #if QSV_VERSION_ATLEAST(1, 8)
  37. case AV_CODEC_ID_HEVC:
  38. return MFX_CODEC_HEVC;
  39. #endif
  40. case AV_CODEC_ID_MPEG1VIDEO:
  41. case AV_CODEC_ID_MPEG2VIDEO:
  42. return MFX_CODEC_MPEG2;
  43. case AV_CODEC_ID_VC1:
  44. return MFX_CODEC_VC1;
  45. default:
  46. break;
  47. }
  48. return AVERROR(ENOSYS);
  49. }
  50. static const struct {
  51. mfxStatus mfxerr;
  52. int averr;
  53. const char *desc;
  54. } qsv_errors[] = {
  55. { MFX_ERR_NONE, 0, "success" },
  56. { MFX_ERR_UNKNOWN, AVERROR_UNKNOWN, "unknown error" },
  57. { MFX_ERR_NULL_PTR, AVERROR(EINVAL), "NULL pointer" },
  58. { MFX_ERR_UNSUPPORTED, AVERROR(ENOSYS), "unsupported" },
  59. { MFX_ERR_MEMORY_ALLOC, AVERROR(ENOMEM), "failed to allocate memory" },
  60. { MFX_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOMEM), "insufficient input/output buffer" },
  61. { MFX_ERR_INVALID_HANDLE, AVERROR(EINVAL), "invalid handle" },
  62. { MFX_ERR_LOCK_MEMORY, AVERROR(EIO), "failed to lock the memory block" },
  63. { MFX_ERR_NOT_INITIALIZED, AVERROR_BUG, "not initialized" },
  64. { MFX_ERR_NOT_FOUND, AVERROR(ENOSYS), "specified object was not found" },
  65. { MFX_ERR_MORE_DATA, AVERROR(EAGAIN), "expect more data at input" },
  66. { MFX_ERR_MORE_SURFACE, AVERROR(EAGAIN), "expect more surface at output" },
  67. { MFX_ERR_ABORTED, AVERROR_UNKNOWN, "operation aborted" },
  68. { MFX_ERR_DEVICE_LOST, AVERROR(EIO), "device lost" },
  69. { MFX_ERR_INCOMPATIBLE_VIDEO_PARAM, AVERROR(EINVAL), "incompatible video parameters" },
  70. { MFX_ERR_INVALID_VIDEO_PARAM, AVERROR(EINVAL), "invalid video parameters" },
  71. { MFX_ERR_UNDEFINED_BEHAVIOR, AVERROR_BUG, "undefined behavior" },
  72. { MFX_ERR_DEVICE_FAILED, AVERROR(EIO), "device failed" },
  73. { MFX_ERR_MORE_BITSTREAM, AVERROR(EAGAIN), "expect more bitstream at output" },
  74. { MFX_ERR_INCOMPATIBLE_AUDIO_PARAM, AVERROR(EINVAL), "incompatible audio parameters" },
  75. { MFX_ERR_INVALID_AUDIO_PARAM, AVERROR(EINVAL), "invalid audio parameters" },
  76. { MFX_WRN_IN_EXECUTION, 0, "operation in execution" },
  77. { MFX_WRN_DEVICE_BUSY, 0, "device busy" },
  78. { MFX_WRN_VIDEO_PARAM_CHANGED, 0, "video parameters changed" },
  79. { MFX_WRN_PARTIAL_ACCELERATION, 0, "partial acceleration" },
  80. { MFX_WRN_INCOMPATIBLE_VIDEO_PARAM, 0, "incompatible video parameters" },
  81. { MFX_WRN_VALUE_NOT_CHANGED, 0, "value is saturated" },
  82. { MFX_WRN_OUT_OF_RANGE, 0, "value out of range" },
  83. { MFX_WRN_FILTER_SKIPPED, 0, "filter skipped" },
  84. { MFX_WRN_INCOMPATIBLE_AUDIO_PARAM, 0, "incompatible audio parameters" },
  85. };
  86. int ff_qsv_map_error(mfxStatus mfx_err, const char **desc)
  87. {
  88. int i;
  89. for (i = 0; i < FF_ARRAY_ELEMS(qsv_errors); i++) {
  90. if (qsv_errors[i].mfxerr == mfx_err) {
  91. if (desc)
  92. *desc = qsv_errors[i].desc;
  93. return qsv_errors[i].averr;
  94. }
  95. }
  96. if (desc)
  97. *desc = "unknown error";
  98. return AVERROR_UNKNOWN;
  99. }
  100. int ff_qsv_print_error(void *log_ctx, mfxStatus err,
  101. const char *error_string)
  102. {
  103. const char *desc;
  104. int ret;
  105. ret = ff_qsv_map_error(err, &desc);
  106. av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
  107. return ret;
  108. }
  109. int ff_qsv_print_warning(void *log_ctx, mfxStatus err,
  110. const char *warning_string)
  111. {
  112. const char *desc;
  113. int ret;
  114. ret = ff_qsv_map_error(err, &desc);
  115. av_log(log_ctx, AV_LOG_WARNING, "%s: %s (%d)\n", warning_string, desc, err);
  116. return ret;
  117. }
  118. int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
  119. {
  120. switch (format) {
  121. case AV_PIX_FMT_YUV420P:
  122. case AV_PIX_FMT_YUVJ420P:
  123. case AV_PIX_FMT_NV12:
  124. *fourcc = MFX_FOURCC_NV12;
  125. return AV_PIX_FMT_NV12;
  126. case AV_PIX_FMT_YUV420P10:
  127. case AV_PIX_FMT_P010:
  128. *fourcc = MFX_FOURCC_P010;
  129. return AV_PIX_FMT_P010;
  130. default:
  131. return AVERROR(ENOSYS);
  132. }
  133. }
  134. static int qsv_load_plugins(mfxSession session, const char *load_plugins,
  135. void *logctx)
  136. {
  137. if (!load_plugins || !*load_plugins)
  138. return 0;
  139. while (*load_plugins) {
  140. mfxPluginUID uid;
  141. mfxStatus ret;
  142. int i, err = 0;
  143. char *plugin = av_get_token(&load_plugins, ":");
  144. if (!plugin)
  145. return AVERROR(ENOMEM);
  146. if (strlen(plugin) != 2 * sizeof(uid.Data)) {
  147. av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
  148. err = AVERROR(EINVAL);
  149. goto load_plugin_fail;
  150. }
  151. for (i = 0; i < sizeof(uid.Data); i++) {
  152. err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
  153. if (err != 1) {
  154. av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
  155. err = AVERROR(EINVAL);
  156. goto load_plugin_fail;
  157. }
  158. }
  159. ret = MFXVideoUSER_Load(session, &uid, 1);
  160. if (ret < 0) {
  161. char errorbuf[128];
  162. snprintf(errorbuf, sizeof(errorbuf),
  163. "Could not load the requested plugin '%s'", plugin);
  164. err = ff_qsv_print_error(logctx, ret, errorbuf);
  165. goto load_plugin_fail;
  166. }
  167. if (*load_plugins)
  168. load_plugins++;
  169. load_plugin_fail:
  170. av_freep(&plugin);
  171. if (err < 0)
  172. return err;
  173. }
  174. return 0;
  175. }
  176. int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
  177. const char *load_plugins)
  178. {
  179. mfxIMPL impl = MFX_IMPL_AUTO_ANY;
  180. mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
  181. const char *desc;
  182. int ret;
  183. ret = MFXInit(impl, &ver, session);
  184. if (ret < 0)
  185. return ff_qsv_print_error(avctx, ret,
  186. "Error initializing an internal MFX session");
  187. ret = qsv_load_plugins(*session, load_plugins, avctx);
  188. if (ret < 0) {
  189. av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
  190. return ret;
  191. }
  192. MFXQueryIMPL(*session, &impl);
  193. switch (MFX_IMPL_BASETYPE(impl)) {
  194. case MFX_IMPL_SOFTWARE:
  195. desc = "software";
  196. break;
  197. case MFX_IMPL_HARDWARE:
  198. case MFX_IMPL_HARDWARE2:
  199. case MFX_IMPL_HARDWARE3:
  200. case MFX_IMPL_HARDWARE4:
  201. desc = "hardware accelerated";
  202. break;
  203. default:
  204. desc = "unknown";
  205. }
  206. av_log(avctx, AV_LOG_VERBOSE,
  207. "Initialized an internal MFX session using %s implementation\n",
  208. desc);
  209. return 0;
  210. }
  211. static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
  212. mfxFrameAllocResponse *resp)
  213. {
  214. QSVFramesContext *ctx = pthis;
  215. mfxFrameInfo *i = &req->Info;
  216. mfxFrameInfo *i1 = &ctx->info;
  217. if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
  218. !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
  219. !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
  220. return MFX_ERR_UNSUPPORTED;
  221. if (i->Width != i1->Width || i->Height != i1->Height ||
  222. i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
  223. av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
  224. "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
  225. i->Width, i->Height, i->FourCC, i->ChromaFormat,
  226. i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
  227. return MFX_ERR_UNSUPPORTED;
  228. }
  229. resp->mids = ctx->mids;
  230. resp->NumFrameActual = ctx->nb_mids;
  231. return MFX_ERR_NONE;
  232. }
  233. static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
  234. {
  235. return MFX_ERR_NONE;
  236. }
  237. static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  238. {
  239. return MFX_ERR_UNSUPPORTED;
  240. }
  241. static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  242. {
  243. return MFX_ERR_UNSUPPORTED;
  244. }
  245. static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
  246. {
  247. *hdl = mid;
  248. return MFX_ERR_NONE;
  249. }
  250. int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
  251. QSVFramesContext *qsv_frames_ctx,
  252. const char *load_plugins, int opaque)
  253. {
  254. static const mfxHandleType handle_types[] = {
  255. MFX_HANDLE_VA_DISPLAY,
  256. MFX_HANDLE_D3D9_DEVICE_MANAGER,
  257. MFX_HANDLE_D3D11_DEVICE,
  258. };
  259. mfxFrameAllocator frame_allocator = {
  260. .pthis = qsv_frames_ctx,
  261. .Alloc = qsv_frame_alloc,
  262. .Lock = qsv_frame_lock,
  263. .Unlock = qsv_frame_unlock,
  264. .GetHDL = qsv_frame_get_hdl,
  265. .Free = qsv_frame_free,
  266. };
  267. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
  268. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  269. AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
  270. mfxSession parent_session = device_hwctx->session;
  271. mfxSession session;
  272. mfxVersion ver;
  273. mfxIMPL impl;
  274. mfxHDL handle = NULL;
  275. mfxHandleType handle_type;
  276. mfxStatus err;
  277. int i, ret;
  278. err = MFXQueryIMPL(parent_session, &impl);
  279. if (err == MFX_ERR_NONE)
  280. err = MFXQueryVersion(parent_session, &ver);
  281. if (err != MFX_ERR_NONE)
  282. return ff_qsv_print_error(avctx, err,
  283. "Error querying the session attributes");
  284. for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
  285. err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
  286. if (err == MFX_ERR_NONE) {
  287. handle_type = handle_types[i];
  288. break;
  289. }
  290. handle = NULL;
  291. }
  292. if (!handle) {
  293. av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
  294. "from the session\n");
  295. }
  296. err = MFXInit(impl, &ver, &session);
  297. if (err != MFX_ERR_NONE)
  298. return ff_qsv_print_error(avctx, err,
  299. "Error initializing a child MFX session");
  300. if (handle) {
  301. err = MFXVideoCORE_SetHandle(session, handle_type, handle);
  302. if (err != MFX_ERR_NONE)
  303. return ff_qsv_print_error(avctx, err,
  304. "Error setting a HW handle");
  305. }
  306. ret = qsv_load_plugins(session, load_plugins, avctx);
  307. if (ret < 0) {
  308. av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
  309. return ret;
  310. }
  311. if (!opaque) {
  312. av_freep(&qsv_frames_ctx->mids);
  313. qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
  314. sizeof(*qsv_frames_ctx->mids));
  315. if (!qsv_frames_ctx->mids)
  316. return AVERROR(ENOMEM);
  317. qsv_frames_ctx->info = frames_hwctx->surfaces[0].Info;
  318. qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
  319. for (i = 0; i < frames_hwctx->nb_surfaces; i++)
  320. qsv_frames_ctx->mids[i] = frames_hwctx->surfaces[i].Data.MemId;
  321. err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
  322. if (err != MFX_ERR_NONE)
  323. return ff_qsv_print_error(avctx, err,
  324. "Error setting a frame allocator");
  325. }
  326. *psession = session;
  327. return 0;
  328. }