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.

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