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.

628 lines
21KB

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