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.

657 lines
22KB

  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 <mfx/mfxjpeg.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/error.h"
  28. #include "libavutil/hwcontext.h"
  29. #include "libavutil/hwcontext_qsv.h"
  30. #include "libavutil/imgutils.h"
  31. #include "avcodec.h"
  32. #include "qsv_internal.h"
  33. #if QSV_VERSION_ATLEAST(1, 12)
  34. #include "mfx/mfxvp8.h"
  35. #endif
  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. #if QSV_VERSION_ATLEAST(1, 12)
  51. case AV_CODEC_ID_VP8:
  52. return MFX_CODEC_VP8;
  53. #endif
  54. case AV_CODEC_ID_MJPEG:
  55. return MFX_CODEC_JPEG;
  56. default:
  57. break;
  58. }
  59. return AVERROR(ENOSYS);
  60. }
  61. int ff_qsv_profile_to_mfx(enum AVCodecID codec_id, int profile)
  62. {
  63. if (profile == FF_PROFILE_UNKNOWN)
  64. return MFX_PROFILE_UNKNOWN;
  65. switch (codec_id) {
  66. case AV_CODEC_ID_H264:
  67. case AV_CODEC_ID_HEVC:
  68. return profile;
  69. case AV_CODEC_ID_VC1:
  70. return 4 * profile + 1;
  71. case AV_CODEC_ID_MPEG2VIDEO:
  72. return 0x10 * profile;
  73. }
  74. return MFX_PROFILE_UNKNOWN;
  75. }
  76. static const struct {
  77. mfxStatus mfxerr;
  78. int averr;
  79. const char *desc;
  80. } qsv_errors[] = {
  81. { MFX_ERR_NONE, 0, "success" },
  82. { MFX_ERR_UNKNOWN, AVERROR_UNKNOWN, "unknown error" },
  83. { MFX_ERR_NULL_PTR, AVERROR(EINVAL), "NULL pointer" },
  84. { MFX_ERR_UNSUPPORTED, AVERROR(ENOSYS), "unsupported" },
  85. { MFX_ERR_MEMORY_ALLOC, AVERROR(ENOMEM), "failed to allocate memory" },
  86. { MFX_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOMEM), "insufficient input/output buffer" },
  87. { MFX_ERR_INVALID_HANDLE, AVERROR(EINVAL), "invalid handle" },
  88. { MFX_ERR_LOCK_MEMORY, AVERROR(EIO), "failed to lock the memory block" },
  89. { MFX_ERR_NOT_INITIALIZED, AVERROR_BUG, "not initialized" },
  90. { MFX_ERR_NOT_FOUND, AVERROR(ENOSYS), "specified object was not found" },
  91. /* the following 3 errors should always be handled explicitly, so those "mappings"
  92. * are for completeness only */
  93. { MFX_ERR_MORE_DATA, AVERROR_UNKNOWN, "expect more data at input" },
  94. { MFX_ERR_MORE_SURFACE, AVERROR_UNKNOWN, "expect more surface at output" },
  95. { MFX_ERR_MORE_BITSTREAM, AVERROR_UNKNOWN, "expect more bitstream at output" },
  96. { MFX_ERR_ABORTED, AVERROR_UNKNOWN, "operation aborted" },
  97. { MFX_ERR_DEVICE_LOST, AVERROR(EIO), "device lost" },
  98. { MFX_ERR_INCOMPATIBLE_VIDEO_PARAM, AVERROR(EINVAL), "incompatible video parameters" },
  99. { MFX_ERR_INVALID_VIDEO_PARAM, AVERROR(EINVAL), "invalid video parameters" },
  100. { MFX_ERR_UNDEFINED_BEHAVIOR, AVERROR_BUG, "undefined behavior" },
  101. { MFX_ERR_DEVICE_FAILED, AVERROR(EIO), "device failed" },
  102. { MFX_ERR_INCOMPATIBLE_AUDIO_PARAM, AVERROR(EINVAL), "incompatible audio parameters" },
  103. { MFX_ERR_INVALID_AUDIO_PARAM, AVERROR(EINVAL), "invalid audio parameters" },
  104. { MFX_WRN_IN_EXECUTION, 0, "operation in execution" },
  105. { MFX_WRN_DEVICE_BUSY, 0, "device busy" },
  106. { MFX_WRN_VIDEO_PARAM_CHANGED, 0, "video parameters changed" },
  107. { MFX_WRN_PARTIAL_ACCELERATION, 0, "partial acceleration" },
  108. { MFX_WRN_INCOMPATIBLE_VIDEO_PARAM, 0, "incompatible video parameters" },
  109. { MFX_WRN_VALUE_NOT_CHANGED, 0, "value is saturated" },
  110. { MFX_WRN_OUT_OF_RANGE, 0, "value out of range" },
  111. { MFX_WRN_FILTER_SKIPPED, 0, "filter skipped" },
  112. { MFX_WRN_INCOMPATIBLE_AUDIO_PARAM, 0, "incompatible audio parameters" },
  113. };
  114. int ff_qsv_map_error(mfxStatus mfx_err, const char **desc)
  115. {
  116. int i;
  117. for (i = 0; i < FF_ARRAY_ELEMS(qsv_errors); i++) {
  118. if (qsv_errors[i].mfxerr == mfx_err) {
  119. if (desc)
  120. *desc = qsv_errors[i].desc;
  121. return qsv_errors[i].averr;
  122. }
  123. }
  124. if (desc)
  125. *desc = "unknown error";
  126. return AVERROR_UNKNOWN;
  127. }
  128. int ff_qsv_print_error(void *log_ctx, mfxStatus err,
  129. const char *error_string)
  130. {
  131. const char *desc;
  132. int ret;
  133. ret = ff_qsv_map_error(err, &desc);
  134. av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
  135. return ret;
  136. }
  137. int ff_qsv_print_warning(void *log_ctx, mfxStatus err,
  138. const char *warning_string)
  139. {
  140. const char *desc;
  141. int ret;
  142. ret = ff_qsv_map_error(err, &desc);
  143. av_log(log_ctx, AV_LOG_WARNING, "%s: %s (%d)\n", warning_string, desc, err);
  144. return ret;
  145. }
  146. static enum AVPixelFormat qsv_map_fourcc(uint32_t fourcc)
  147. {
  148. switch (fourcc) {
  149. case MFX_FOURCC_NV12: return AV_PIX_FMT_NV12;
  150. case MFX_FOURCC_P010: return AV_PIX_FMT_P010;
  151. case MFX_FOURCC_P8: return AV_PIX_FMT_PAL8;
  152. }
  153. return AV_PIX_FMT_NONE;
  154. }
  155. int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
  156. {
  157. switch (format) {
  158. case AV_PIX_FMT_YUV420P:
  159. case AV_PIX_FMT_YUVJ420P:
  160. case AV_PIX_FMT_NV12:
  161. *fourcc = MFX_FOURCC_NV12;
  162. return AV_PIX_FMT_NV12;
  163. case AV_PIX_FMT_YUV420P10:
  164. case AV_PIX_FMT_P010:
  165. *fourcc = MFX_FOURCC_P010;
  166. return AV_PIX_FMT_P010;
  167. default:
  168. return AVERROR(ENOSYS);
  169. }
  170. }
  171. int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame)
  172. {
  173. int i;
  174. for (i = 0; i < ctx->nb_mids; i++) {
  175. QSVMid *mid = &ctx->mids[i];
  176. if (mid->handle == frame->surface.Data.MemId)
  177. return i;
  178. }
  179. return AVERROR_BUG;
  180. }
  181. static int qsv_load_plugins(mfxSession session, const char *load_plugins,
  182. void *logctx)
  183. {
  184. if (!load_plugins || !*load_plugins)
  185. return 0;
  186. while (*load_plugins) {
  187. mfxPluginUID uid;
  188. mfxStatus ret;
  189. int i, err = 0;
  190. char *plugin = av_get_token(&load_plugins, ":");
  191. if (!plugin)
  192. return AVERROR(ENOMEM);
  193. if (strlen(plugin) != 2 * sizeof(uid.Data)) {
  194. av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
  195. err = AVERROR(EINVAL);
  196. goto load_plugin_fail;
  197. }
  198. for (i = 0; i < sizeof(uid.Data); i++) {
  199. err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
  200. if (err != 1) {
  201. av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
  202. err = AVERROR(EINVAL);
  203. goto load_plugin_fail;
  204. }
  205. }
  206. ret = MFXVideoUSER_Load(session, &uid, 1);
  207. if (ret < 0) {
  208. char errorbuf[128];
  209. snprintf(errorbuf, sizeof(errorbuf),
  210. "Could not load the requested plugin '%s'", plugin);
  211. err = ff_qsv_print_error(logctx, ret, errorbuf);
  212. goto load_plugin_fail;
  213. }
  214. if (*load_plugins)
  215. load_plugins++;
  216. load_plugin_fail:
  217. av_freep(&plugin);
  218. if (err < 0)
  219. return err;
  220. }
  221. return 0;
  222. }
  223. int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
  224. const char *load_plugins)
  225. {
  226. mfxIMPL impl = MFX_IMPL_AUTO_ANY;
  227. mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
  228. const char *desc;
  229. int ret;
  230. ret = MFXInit(impl, &ver, session);
  231. if (ret < 0)
  232. return ff_qsv_print_error(avctx, ret,
  233. "Error initializing an internal MFX session");
  234. ret = qsv_load_plugins(*session, load_plugins, avctx);
  235. if (ret < 0) {
  236. av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
  237. return ret;
  238. }
  239. MFXQueryIMPL(*session, &impl);
  240. switch (MFX_IMPL_BASETYPE(impl)) {
  241. case MFX_IMPL_SOFTWARE:
  242. desc = "software";
  243. break;
  244. case MFX_IMPL_HARDWARE:
  245. case MFX_IMPL_HARDWARE2:
  246. case MFX_IMPL_HARDWARE3:
  247. case MFX_IMPL_HARDWARE4:
  248. desc = "hardware accelerated";
  249. break;
  250. default:
  251. desc = "unknown";
  252. }
  253. av_log(avctx, AV_LOG_VERBOSE,
  254. "Initialized an internal MFX session using %s implementation\n",
  255. desc);
  256. return 0;
  257. }
  258. static void mids_buf_free(void *opaque, uint8_t *data)
  259. {
  260. AVBufferRef *hw_frames_ref = opaque;
  261. av_buffer_unref(&hw_frames_ref);
  262. av_freep(&data);
  263. }
  264. static AVBufferRef *qsv_create_mids(AVBufferRef *hw_frames_ref)
  265. {
  266. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
  267. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  268. int nb_surfaces = frames_hwctx->nb_surfaces;
  269. AVBufferRef *mids_buf, *hw_frames_ref1;
  270. QSVMid *mids;
  271. int i;
  272. hw_frames_ref1 = av_buffer_ref(hw_frames_ref);
  273. if (!hw_frames_ref1)
  274. return NULL;
  275. mids = av_mallocz_array(nb_surfaces, sizeof(*mids));
  276. if (!mids) {
  277. av_buffer_unref(&hw_frames_ref1);
  278. return NULL;
  279. }
  280. mids_buf = av_buffer_create((uint8_t*)mids, nb_surfaces * sizeof(*mids),
  281. mids_buf_free, hw_frames_ref1, 0);
  282. if (!mids_buf) {
  283. av_buffer_unref(&hw_frames_ref1);
  284. av_freep(&mids);
  285. return NULL;
  286. }
  287. for (i = 0; i < nb_surfaces; i++) {
  288. QSVMid *mid = &mids[i];
  289. mid->handle = frames_hwctx->surfaces[i].Data.MemId;
  290. mid->hw_frames_ref = hw_frames_ref1;
  291. }
  292. return mids_buf;
  293. }
  294. static int qsv_setup_mids(mfxFrameAllocResponse *resp, AVBufferRef *hw_frames_ref,
  295. AVBufferRef *mids_buf)
  296. {
  297. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
  298. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  299. QSVMid *mids = (QSVMid*)mids_buf->data;
  300. int nb_surfaces = frames_hwctx->nb_surfaces;
  301. int i;
  302. // the allocated size of the array is two larger than the number of
  303. // surfaces, we store the references to the frames context and the
  304. // QSVMid array there
  305. resp->mids = av_mallocz_array(nb_surfaces + 2, sizeof(*resp->mids));
  306. if (!resp->mids)
  307. return AVERROR(ENOMEM);
  308. for (i = 0; i < nb_surfaces; i++)
  309. resp->mids[i] = &mids[i];
  310. resp->NumFrameActual = nb_surfaces;
  311. resp->mids[resp->NumFrameActual] = (mfxMemId)av_buffer_ref(hw_frames_ref);
  312. if (!resp->mids[resp->NumFrameActual]) {
  313. av_freep(&resp->mids);
  314. return AVERROR(ENOMEM);
  315. }
  316. resp->mids[resp->NumFrameActual + 1] = av_buffer_ref(mids_buf);
  317. if (!resp->mids[resp->NumFrameActual + 1]) {
  318. av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
  319. av_freep(&resp->mids);
  320. return AVERROR(ENOMEM);
  321. }
  322. return 0;
  323. }
  324. static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
  325. mfxFrameAllocResponse *resp)
  326. {
  327. QSVFramesContext *ctx = pthis;
  328. int ret;
  329. /* this should only be called from an encoder or decoder and
  330. * only allocates video memory frames */
  331. if (!(req->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET |
  332. MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)) ||
  333. !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)))
  334. return MFX_ERR_UNSUPPORTED;
  335. if (req->Type & MFX_MEMTYPE_EXTERNAL_FRAME) {
  336. /* external frames -- fill from the caller-supplied frames context */
  337. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
  338. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  339. mfxFrameInfo *i = &req->Info;
  340. mfxFrameInfo *i1 = &frames_hwctx->surfaces[0].Info;
  341. if (i->Width > i1->Width || i->Height > i1->Height ||
  342. i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
  343. av_log(ctx->logctx, AV_LOG_ERROR, "Mismatching surface properties in an "
  344. "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
  345. i->Width, i->Height, i->FourCC, i->ChromaFormat,
  346. i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
  347. return MFX_ERR_UNSUPPORTED;
  348. }
  349. ret = qsv_setup_mids(resp, ctx->hw_frames_ctx, ctx->mids_buf);
  350. if (ret < 0) {
  351. av_log(ctx->logctx, AV_LOG_ERROR,
  352. "Error filling an external frame allocation request\n");
  353. return MFX_ERR_MEMORY_ALLOC;
  354. }
  355. } else if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME) {
  356. /* internal frames -- allocate a new hw frames context */
  357. AVHWFramesContext *ext_frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
  358. mfxFrameInfo *i = &req->Info;
  359. AVBufferRef *frames_ref, *mids_buf;
  360. AVHWFramesContext *frames_ctx;
  361. AVQSVFramesContext *frames_hwctx;
  362. frames_ref = av_hwframe_ctx_alloc(ext_frames_ctx->device_ref);
  363. if (!frames_ref)
  364. return MFX_ERR_MEMORY_ALLOC;
  365. frames_ctx = (AVHWFramesContext*)frames_ref->data;
  366. frames_hwctx = frames_ctx->hwctx;
  367. frames_ctx->format = AV_PIX_FMT_QSV;
  368. frames_ctx->sw_format = qsv_map_fourcc(i->FourCC);
  369. frames_ctx->width = i->Width;
  370. frames_ctx->height = i->Height;
  371. frames_ctx->initial_pool_size = req->NumFrameSuggested;
  372. frames_hwctx->frame_type = req->Type;
  373. ret = av_hwframe_ctx_init(frames_ref);
  374. if (ret < 0) {
  375. av_log(ctx->logctx, AV_LOG_ERROR,
  376. "Error initializing a frames context for an internal frame "
  377. "allocation request\n");
  378. av_buffer_unref(&frames_ref);
  379. return MFX_ERR_MEMORY_ALLOC;
  380. }
  381. mids_buf = qsv_create_mids(frames_ref);
  382. if (!mids_buf) {
  383. av_buffer_unref(&frames_ref);
  384. return MFX_ERR_MEMORY_ALLOC;
  385. }
  386. ret = qsv_setup_mids(resp, frames_ref, mids_buf);
  387. av_buffer_unref(&mids_buf);
  388. av_buffer_unref(&frames_ref);
  389. if (ret < 0) {
  390. av_log(ctx->logctx, AV_LOG_ERROR,
  391. "Error filling an internal frame allocation request\n");
  392. return MFX_ERR_MEMORY_ALLOC;
  393. }
  394. } else {
  395. return MFX_ERR_UNSUPPORTED;
  396. }
  397. return MFX_ERR_NONE;
  398. }
  399. static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
  400. {
  401. av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
  402. av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual + 1]);
  403. av_freep(&resp->mids);
  404. return MFX_ERR_NONE;
  405. }
  406. static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  407. {
  408. QSVMid *qsv_mid = mid;
  409. AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)qsv_mid->hw_frames_ref->data;
  410. AVQSVFramesContext *hw_frames_hwctx = hw_frames_ctx->hwctx;
  411. int ret;
  412. if (qsv_mid->locked_frame)
  413. return MFX_ERR_UNDEFINED_BEHAVIOR;
  414. /* Allocate a system memory frame that will hold the mapped data. */
  415. qsv_mid->locked_frame = av_frame_alloc();
  416. if (!qsv_mid->locked_frame)
  417. return MFX_ERR_MEMORY_ALLOC;
  418. qsv_mid->locked_frame->format = hw_frames_ctx->sw_format;
  419. /* wrap the provided handle in a hwaccel AVFrame */
  420. qsv_mid->hw_frame = av_frame_alloc();
  421. if (!qsv_mid->hw_frame)
  422. goto fail;
  423. qsv_mid->hw_frame->data[3] = (uint8_t*)&qsv_mid->surf;
  424. qsv_mid->hw_frame->format = AV_PIX_FMT_QSV;
  425. // doesn't really matter what buffer is used here
  426. qsv_mid->hw_frame->buf[0] = av_buffer_alloc(1);
  427. if (!qsv_mid->hw_frame->buf[0])
  428. goto fail;
  429. qsv_mid->hw_frame->width = hw_frames_ctx->width;
  430. qsv_mid->hw_frame->height = hw_frames_ctx->height;
  431. qsv_mid->hw_frame->hw_frames_ctx = av_buffer_ref(qsv_mid->hw_frames_ref);
  432. if (!qsv_mid->hw_frame->hw_frames_ctx)
  433. goto fail;
  434. qsv_mid->surf.Info = hw_frames_hwctx->surfaces[0].Info;
  435. qsv_mid->surf.Data.MemId = qsv_mid->handle;
  436. /* map the data to the system memory */
  437. ret = av_hwframe_map(qsv_mid->locked_frame, qsv_mid->hw_frame,
  438. AV_HWFRAME_MAP_DIRECT);
  439. if (ret < 0)
  440. goto fail;
  441. ptr->Pitch = qsv_mid->locked_frame->linesize[0];
  442. ptr->Y = qsv_mid->locked_frame->data[0];
  443. ptr->U = qsv_mid->locked_frame->data[1];
  444. ptr->V = qsv_mid->locked_frame->data[1] + 1;
  445. return MFX_ERR_NONE;
  446. fail:
  447. av_frame_free(&qsv_mid->hw_frame);
  448. av_frame_free(&qsv_mid->locked_frame);
  449. return MFX_ERR_MEMORY_ALLOC;
  450. }
  451. static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  452. {
  453. QSVMid *qsv_mid = mid;
  454. av_frame_free(&qsv_mid->locked_frame);
  455. av_frame_free(&qsv_mid->hw_frame);
  456. return MFX_ERR_NONE;
  457. }
  458. static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
  459. {
  460. QSVMid *qsv_mid = (QSVMid*)mid;
  461. *hdl = qsv_mid->handle;
  462. return MFX_ERR_NONE;
  463. }
  464. int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession,
  465. AVBufferRef *device_ref, const char *load_plugins)
  466. {
  467. static const mfxHandleType handle_types[] = {
  468. MFX_HANDLE_VA_DISPLAY,
  469. MFX_HANDLE_D3D9_DEVICE_MANAGER,
  470. MFX_HANDLE_D3D11_DEVICE,
  471. };
  472. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref->data;
  473. AVQSVDeviceContext *device_hwctx = device_ctx->hwctx;
  474. mfxSession parent_session = device_hwctx->session;
  475. mfxSession session;
  476. mfxVersion ver;
  477. mfxIMPL impl;
  478. mfxHDL handle = NULL;
  479. mfxHandleType handle_type;
  480. mfxStatus err;
  481. int i, ret;
  482. err = MFXQueryIMPL(parent_session, &impl);
  483. if (err == MFX_ERR_NONE)
  484. err = MFXQueryVersion(parent_session, &ver);
  485. if (err != MFX_ERR_NONE)
  486. return ff_qsv_print_error(avctx, err,
  487. "Error querying the session attributes");
  488. for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
  489. err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
  490. if (err == MFX_ERR_NONE) {
  491. handle_type = handle_types[i];
  492. break;
  493. }
  494. handle = NULL;
  495. }
  496. if (!handle) {
  497. av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
  498. "from the session\n");
  499. }
  500. err = MFXInit(impl, &ver, &session);
  501. if (err != MFX_ERR_NONE)
  502. return ff_qsv_print_error(avctx, err,
  503. "Error initializing a child MFX session");
  504. if (handle) {
  505. err = MFXVideoCORE_SetHandle(session, handle_type, handle);
  506. if (err != MFX_ERR_NONE)
  507. return ff_qsv_print_error(avctx, err,
  508. "Error setting a HW handle");
  509. }
  510. err = MFXJoinSession(parent_session, session);
  511. if (err != MFX_ERR_NONE)
  512. return ff_qsv_print_error(avctx, err,
  513. "Error joining session");
  514. ret = qsv_load_plugins(session, load_plugins, avctx);
  515. if (ret < 0) {
  516. av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
  517. return ret;
  518. }
  519. *psession = session;
  520. return 0;
  521. }
  522. int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession,
  523. QSVFramesContext *qsv_frames_ctx,
  524. const char *load_plugins, int opaque)
  525. {
  526. mfxFrameAllocator frame_allocator = {
  527. .pthis = qsv_frames_ctx,
  528. .Alloc = qsv_frame_alloc,
  529. .Lock = qsv_frame_lock,
  530. .Unlock = qsv_frame_unlock,
  531. .GetHDL = qsv_frame_get_hdl,
  532. .Free = qsv_frame_free,
  533. };
  534. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
  535. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  536. mfxSession session;
  537. mfxStatus err;
  538. int ret;
  539. ret = ff_qsv_init_session_device(avctx, &session,
  540. frames_ctx->device_ref, load_plugins);
  541. if (ret < 0)
  542. return ret;
  543. if (!opaque) {
  544. qsv_frames_ctx->logctx = avctx;
  545. /* allocate the memory ids for the external frames */
  546. av_buffer_unref(&qsv_frames_ctx->mids_buf);
  547. qsv_frames_ctx->mids_buf = qsv_create_mids(qsv_frames_ctx->hw_frames_ctx);
  548. if (!qsv_frames_ctx->mids_buf)
  549. return AVERROR(ENOMEM);
  550. qsv_frames_ctx->mids = (QSVMid*)qsv_frames_ctx->mids_buf->data;
  551. qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
  552. err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
  553. if (err != MFX_ERR_NONE)
  554. return ff_qsv_print_error(avctx, err,
  555. "Error setting a frame allocator");
  556. }
  557. *psession = session;
  558. return 0;
  559. }