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.

344 lines
9.9KB

  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. int ff_qsv_error(int mfx_err)
  51. {
  52. switch (mfx_err) {
  53. case MFX_ERR_NONE:
  54. return 0;
  55. case MFX_ERR_MEMORY_ALLOC:
  56. case MFX_ERR_NOT_ENOUGH_BUFFER:
  57. return AVERROR(ENOMEM);
  58. case MFX_ERR_INVALID_HANDLE:
  59. return AVERROR(EINVAL);
  60. case MFX_ERR_DEVICE_FAILED:
  61. case MFX_ERR_DEVICE_LOST:
  62. case MFX_ERR_LOCK_MEMORY:
  63. return AVERROR(EIO);
  64. case MFX_ERR_NULL_PTR:
  65. case MFX_ERR_UNDEFINED_BEHAVIOR:
  66. case MFX_ERR_NOT_INITIALIZED:
  67. return AVERROR_BUG;
  68. case MFX_ERR_UNSUPPORTED:
  69. case MFX_ERR_NOT_FOUND:
  70. return AVERROR(ENOSYS);
  71. case MFX_ERR_MORE_DATA:
  72. case MFX_ERR_MORE_SURFACE:
  73. case MFX_ERR_MORE_BITSTREAM:
  74. return AVERROR(EAGAIN);
  75. case MFX_ERR_INCOMPATIBLE_VIDEO_PARAM:
  76. case MFX_ERR_INVALID_VIDEO_PARAM:
  77. return AVERROR(EINVAL);
  78. case MFX_ERR_ABORTED:
  79. case MFX_ERR_UNKNOWN:
  80. default:
  81. return AVERROR_UNKNOWN;
  82. }
  83. }
  84. int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
  85. {
  86. switch (format) {
  87. case AV_PIX_FMT_YUV420P:
  88. case AV_PIX_FMT_YUVJ420P:
  89. *fourcc = MFX_FOURCC_NV12;
  90. return AV_PIX_FMT_NV12;
  91. case AV_PIX_FMT_YUV420P10:
  92. *fourcc = MFX_FOURCC_P010;
  93. return AV_PIX_FMT_P010;
  94. default:
  95. return AVERROR(ENOSYS);
  96. }
  97. }
  98. static int qsv_load_plugins(mfxSession session, const char *load_plugins,
  99. void *logctx)
  100. {
  101. if (!load_plugins || !*load_plugins)
  102. return 0;
  103. while (*load_plugins) {
  104. mfxPluginUID uid;
  105. mfxStatus ret;
  106. int i, err = 0;
  107. char *plugin = av_get_token(&load_plugins, ":");
  108. if (!plugin)
  109. return AVERROR(ENOMEM);
  110. if (strlen(plugin) != 2 * sizeof(uid.Data)) {
  111. av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
  112. err = AVERROR(EINVAL);
  113. goto load_plugin_fail;
  114. }
  115. for (i = 0; i < sizeof(uid.Data); i++) {
  116. err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
  117. if (err != 1) {
  118. av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
  119. err = AVERROR(EINVAL);
  120. goto load_plugin_fail;
  121. }
  122. }
  123. ret = MFXVideoUSER_Load(session, &uid, 1);
  124. if (ret < 0) {
  125. av_log(logctx, AV_LOG_ERROR, "Could not load the requested plugin: %s\n",
  126. plugin);
  127. err = ff_qsv_error(ret);
  128. goto load_plugin_fail;
  129. }
  130. if (*load_plugins)
  131. load_plugins++;
  132. load_plugin_fail:
  133. av_freep(&plugin);
  134. if (err < 0)
  135. return err;
  136. }
  137. return 0;
  138. }
  139. int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
  140. const char *load_plugins)
  141. {
  142. mfxIMPL impl = MFX_IMPL_AUTO_ANY;
  143. mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
  144. const char *desc;
  145. int ret;
  146. ret = MFXInit(impl, &ver, session);
  147. if (ret < 0) {
  148. av_log(avctx, AV_LOG_ERROR, "Error initializing an internal MFX session\n");
  149. return ff_qsv_error(ret);
  150. }
  151. ret = qsv_load_plugins(*session, load_plugins, avctx);
  152. if (ret < 0) {
  153. av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
  154. return ret;
  155. }
  156. MFXQueryIMPL(*session, &impl);
  157. switch (MFX_IMPL_BASETYPE(impl)) {
  158. case MFX_IMPL_SOFTWARE:
  159. desc = "software";
  160. break;
  161. case MFX_IMPL_HARDWARE:
  162. case MFX_IMPL_HARDWARE2:
  163. case MFX_IMPL_HARDWARE3:
  164. case MFX_IMPL_HARDWARE4:
  165. desc = "hardware accelerated";
  166. break;
  167. default:
  168. desc = "unknown";
  169. }
  170. av_log(avctx, AV_LOG_VERBOSE,
  171. "Initialized an internal MFX session using %s implementation\n",
  172. desc);
  173. return 0;
  174. }
  175. static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
  176. mfxFrameAllocResponse *resp)
  177. {
  178. QSVFramesContext *ctx = pthis;
  179. mfxFrameInfo *i = &req->Info;
  180. mfxFrameInfo *i1 = &ctx->info;
  181. if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
  182. !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
  183. !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
  184. return MFX_ERR_UNSUPPORTED;
  185. if (i->Width != i1->Width || i->Height != i1->Height ||
  186. i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
  187. av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
  188. "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
  189. i->Width, i->Height, i->FourCC, i->ChromaFormat,
  190. i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
  191. return MFX_ERR_UNSUPPORTED;
  192. }
  193. resp->mids = ctx->mids;
  194. resp->NumFrameActual = ctx->nb_mids;
  195. return MFX_ERR_NONE;
  196. }
  197. static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
  198. {
  199. return MFX_ERR_NONE;
  200. }
  201. static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  202. {
  203. return MFX_ERR_UNSUPPORTED;
  204. }
  205. static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  206. {
  207. return MFX_ERR_UNSUPPORTED;
  208. }
  209. static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
  210. {
  211. *hdl = mid;
  212. return MFX_ERR_NONE;
  213. }
  214. int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
  215. QSVFramesContext *qsv_frames_ctx,
  216. const char *load_plugins, int opaque)
  217. {
  218. static const mfxHandleType handle_types[] = {
  219. MFX_HANDLE_VA_DISPLAY,
  220. MFX_HANDLE_D3D9_DEVICE_MANAGER,
  221. MFX_HANDLE_D3D11_DEVICE,
  222. };
  223. mfxFrameAllocator frame_allocator = {
  224. .pthis = qsv_frames_ctx,
  225. .Alloc = qsv_frame_alloc,
  226. .Lock = qsv_frame_lock,
  227. .Unlock = qsv_frame_unlock,
  228. .GetHDL = qsv_frame_get_hdl,
  229. .Free = qsv_frame_free,
  230. };
  231. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
  232. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  233. AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
  234. mfxSession parent_session = device_hwctx->session;
  235. mfxSession session;
  236. mfxVersion ver;
  237. mfxIMPL impl;
  238. mfxHDL handle = NULL;
  239. mfxHandleType handle_type;
  240. mfxStatus err;
  241. int i, ret;
  242. err = MFXQueryIMPL(parent_session, &impl);
  243. if (err == MFX_ERR_NONE)
  244. err = MFXQueryVersion(parent_session, &ver);
  245. if (err != MFX_ERR_NONE) {
  246. av_log(avctx, AV_LOG_ERROR, "Error querying the session attributes\n");
  247. return ff_qsv_error(err);
  248. }
  249. for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
  250. err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
  251. if (err == MFX_ERR_NONE) {
  252. handle_type = handle_types[i];
  253. break;
  254. }
  255. handle = NULL;
  256. }
  257. if (!handle) {
  258. av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
  259. "from the session\n");
  260. }
  261. err = MFXInit(impl, &ver, &session);
  262. if (err != MFX_ERR_NONE) {
  263. av_log(avctx, AV_LOG_ERROR,
  264. "Error initializing a child MFX session: %d\n", err);
  265. return ff_qsv_error(err);
  266. }
  267. if (handle) {
  268. err = MFXVideoCORE_SetHandle(session, handle_type, handle);
  269. if (err != MFX_ERR_NONE) {
  270. av_log(avctx, AV_LOG_ERROR, "Error setting a HW handle: %d\n", err);
  271. return ff_qsv_error(err);
  272. }
  273. }
  274. ret = qsv_load_plugins(session, load_plugins, avctx);
  275. if (ret < 0) {
  276. av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
  277. return ret;
  278. }
  279. if (!opaque) {
  280. av_freep(&qsv_frames_ctx->mids);
  281. qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
  282. sizeof(*qsv_frames_ctx->mids));
  283. if (!qsv_frames_ctx->mids)
  284. return AVERROR(ENOMEM);
  285. qsv_frames_ctx->info = frames_hwctx->surfaces[0].Info;
  286. qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
  287. for (i = 0; i < frames_hwctx->nb_surfaces; i++)
  288. qsv_frames_ctx->mids[i] = frames_hwctx->surfaces[i].Data.MemId;
  289. err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
  290. if (err != MFX_ERR_NONE) {
  291. av_log(avctx, AV_LOG_ERROR, "Error setting a frame allocator: %d\n", err);
  292. return ff_qsv_error(err);
  293. }
  294. }
  295. *psession = session;
  296. return 0;
  297. }