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.

683 lines
23KB

  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. enum AVPictureType ff_qsv_map_pictype(int mfx_pic_type)
  182. {
  183. enum AVPictureType type;
  184. switch (mfx_pic_type & 0x7) {
  185. case MFX_FRAMETYPE_I:
  186. if (mfx_pic_type & MFX_FRAMETYPE_S)
  187. type = AV_PICTURE_TYPE_SI;
  188. else
  189. type = AV_PICTURE_TYPE_I;
  190. break;
  191. case MFX_FRAMETYPE_B:
  192. type = AV_PICTURE_TYPE_B;
  193. break;
  194. case MFX_FRAMETYPE_P:
  195. if (mfx_pic_type & MFX_FRAMETYPE_S)
  196. type = AV_PICTURE_TYPE_SP;
  197. else
  198. type = AV_PICTURE_TYPE_P;
  199. break;
  200. }
  201. return type;
  202. }
  203. static int qsv_load_plugins(mfxSession session, const char *load_plugins,
  204. void *logctx)
  205. {
  206. if (!load_plugins || !*load_plugins)
  207. return 0;
  208. while (*load_plugins) {
  209. mfxPluginUID uid;
  210. mfxStatus ret;
  211. int i, err = 0;
  212. char *plugin = av_get_token(&load_plugins, ":");
  213. if (!plugin)
  214. return AVERROR(ENOMEM);
  215. if (strlen(plugin) != 2 * sizeof(uid.Data)) {
  216. av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
  217. err = AVERROR(EINVAL);
  218. goto load_plugin_fail;
  219. }
  220. for (i = 0; i < sizeof(uid.Data); i++) {
  221. err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
  222. if (err != 1) {
  223. av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
  224. err = AVERROR(EINVAL);
  225. goto load_plugin_fail;
  226. }
  227. }
  228. ret = MFXVideoUSER_Load(session, &uid, 1);
  229. if (ret < 0) {
  230. char errorbuf[128];
  231. snprintf(errorbuf, sizeof(errorbuf),
  232. "Could not load the requested plugin '%s'", plugin);
  233. err = ff_qsv_print_error(logctx, ret, errorbuf);
  234. goto load_plugin_fail;
  235. }
  236. if (*load_plugins)
  237. load_plugins++;
  238. load_plugin_fail:
  239. av_freep(&plugin);
  240. if (err < 0)
  241. return err;
  242. }
  243. return 0;
  244. }
  245. int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
  246. const char *load_plugins)
  247. {
  248. mfxIMPL impl = MFX_IMPL_AUTO_ANY;
  249. mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
  250. const char *desc;
  251. int ret;
  252. ret = MFXInit(impl, &ver, session);
  253. if (ret < 0)
  254. return ff_qsv_print_error(avctx, ret,
  255. "Error initializing an internal MFX session");
  256. ret = qsv_load_plugins(*session, load_plugins, avctx);
  257. if (ret < 0) {
  258. av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
  259. return ret;
  260. }
  261. MFXQueryIMPL(*session, &impl);
  262. switch (MFX_IMPL_BASETYPE(impl)) {
  263. case MFX_IMPL_SOFTWARE:
  264. desc = "software";
  265. break;
  266. case MFX_IMPL_HARDWARE:
  267. case MFX_IMPL_HARDWARE2:
  268. case MFX_IMPL_HARDWARE3:
  269. case MFX_IMPL_HARDWARE4:
  270. desc = "hardware accelerated";
  271. break;
  272. default:
  273. desc = "unknown";
  274. }
  275. av_log(avctx, AV_LOG_VERBOSE,
  276. "Initialized an internal MFX session using %s implementation\n",
  277. desc);
  278. return 0;
  279. }
  280. static void mids_buf_free(void *opaque, uint8_t *data)
  281. {
  282. AVBufferRef *hw_frames_ref = opaque;
  283. av_buffer_unref(&hw_frames_ref);
  284. av_freep(&data);
  285. }
  286. static AVBufferRef *qsv_create_mids(AVBufferRef *hw_frames_ref)
  287. {
  288. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
  289. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  290. int nb_surfaces = frames_hwctx->nb_surfaces;
  291. AVBufferRef *mids_buf, *hw_frames_ref1;
  292. QSVMid *mids;
  293. int i;
  294. hw_frames_ref1 = av_buffer_ref(hw_frames_ref);
  295. if (!hw_frames_ref1)
  296. return NULL;
  297. mids = av_mallocz_array(nb_surfaces, sizeof(*mids));
  298. if (!mids) {
  299. av_buffer_unref(&hw_frames_ref1);
  300. return NULL;
  301. }
  302. mids_buf = av_buffer_create((uint8_t*)mids, nb_surfaces * sizeof(*mids),
  303. mids_buf_free, hw_frames_ref1, 0);
  304. if (!mids_buf) {
  305. av_buffer_unref(&hw_frames_ref1);
  306. av_freep(&mids);
  307. return NULL;
  308. }
  309. for (i = 0; i < nb_surfaces; i++) {
  310. QSVMid *mid = &mids[i];
  311. mid->handle = frames_hwctx->surfaces[i].Data.MemId;
  312. mid->hw_frames_ref = hw_frames_ref1;
  313. }
  314. return mids_buf;
  315. }
  316. static int qsv_setup_mids(mfxFrameAllocResponse *resp, AVBufferRef *hw_frames_ref,
  317. AVBufferRef *mids_buf)
  318. {
  319. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
  320. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  321. QSVMid *mids = (QSVMid*)mids_buf->data;
  322. int nb_surfaces = frames_hwctx->nb_surfaces;
  323. int i;
  324. // the allocated size of the array is two larger than the number of
  325. // surfaces, we store the references to the frames context and the
  326. // QSVMid array there
  327. resp->mids = av_mallocz_array(nb_surfaces + 2, sizeof(*resp->mids));
  328. if (!resp->mids)
  329. return AVERROR(ENOMEM);
  330. for (i = 0; i < nb_surfaces; i++)
  331. resp->mids[i] = &mids[i];
  332. resp->NumFrameActual = nb_surfaces;
  333. resp->mids[resp->NumFrameActual] = (mfxMemId)av_buffer_ref(hw_frames_ref);
  334. if (!resp->mids[resp->NumFrameActual]) {
  335. av_freep(&resp->mids);
  336. return AVERROR(ENOMEM);
  337. }
  338. resp->mids[resp->NumFrameActual + 1] = av_buffer_ref(mids_buf);
  339. if (!resp->mids[resp->NumFrameActual + 1]) {
  340. av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
  341. av_freep(&resp->mids);
  342. return AVERROR(ENOMEM);
  343. }
  344. return 0;
  345. }
  346. static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
  347. mfxFrameAllocResponse *resp)
  348. {
  349. QSVFramesContext *ctx = pthis;
  350. int ret;
  351. /* this should only be called from an encoder or decoder and
  352. * only allocates video memory frames */
  353. if (!(req->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET |
  354. MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)) ||
  355. !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)))
  356. return MFX_ERR_UNSUPPORTED;
  357. if (req->Type & MFX_MEMTYPE_EXTERNAL_FRAME) {
  358. /* external frames -- fill from the caller-supplied frames context */
  359. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
  360. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  361. mfxFrameInfo *i = &req->Info;
  362. mfxFrameInfo *i1 = &frames_hwctx->surfaces[0].Info;
  363. if (i->Width > i1->Width || i->Height > i1->Height ||
  364. i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
  365. av_log(ctx->logctx, AV_LOG_ERROR, "Mismatching surface properties in an "
  366. "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
  367. i->Width, i->Height, i->FourCC, i->ChromaFormat,
  368. i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
  369. return MFX_ERR_UNSUPPORTED;
  370. }
  371. ret = qsv_setup_mids(resp, ctx->hw_frames_ctx, ctx->mids_buf);
  372. if (ret < 0) {
  373. av_log(ctx->logctx, AV_LOG_ERROR,
  374. "Error filling an external frame allocation request\n");
  375. return MFX_ERR_MEMORY_ALLOC;
  376. }
  377. } else if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME) {
  378. /* internal frames -- allocate a new hw frames context */
  379. AVHWFramesContext *ext_frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
  380. mfxFrameInfo *i = &req->Info;
  381. AVBufferRef *frames_ref, *mids_buf;
  382. AVHWFramesContext *frames_ctx;
  383. AVQSVFramesContext *frames_hwctx;
  384. frames_ref = av_hwframe_ctx_alloc(ext_frames_ctx->device_ref);
  385. if (!frames_ref)
  386. return MFX_ERR_MEMORY_ALLOC;
  387. frames_ctx = (AVHWFramesContext*)frames_ref->data;
  388. frames_hwctx = frames_ctx->hwctx;
  389. frames_ctx->format = AV_PIX_FMT_QSV;
  390. frames_ctx->sw_format = qsv_map_fourcc(i->FourCC);
  391. frames_ctx->width = i->Width;
  392. frames_ctx->height = i->Height;
  393. frames_ctx->initial_pool_size = req->NumFrameSuggested;
  394. frames_hwctx->frame_type = req->Type;
  395. ret = av_hwframe_ctx_init(frames_ref);
  396. if (ret < 0) {
  397. av_log(ctx->logctx, AV_LOG_ERROR,
  398. "Error initializing a frames context for an internal frame "
  399. "allocation request\n");
  400. av_buffer_unref(&frames_ref);
  401. return MFX_ERR_MEMORY_ALLOC;
  402. }
  403. mids_buf = qsv_create_mids(frames_ref);
  404. if (!mids_buf) {
  405. av_buffer_unref(&frames_ref);
  406. return MFX_ERR_MEMORY_ALLOC;
  407. }
  408. ret = qsv_setup_mids(resp, frames_ref, mids_buf);
  409. av_buffer_unref(&mids_buf);
  410. av_buffer_unref(&frames_ref);
  411. if (ret < 0) {
  412. av_log(ctx->logctx, AV_LOG_ERROR,
  413. "Error filling an internal frame allocation request\n");
  414. return MFX_ERR_MEMORY_ALLOC;
  415. }
  416. } else {
  417. return MFX_ERR_UNSUPPORTED;
  418. }
  419. return MFX_ERR_NONE;
  420. }
  421. static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
  422. {
  423. av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
  424. av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual + 1]);
  425. av_freep(&resp->mids);
  426. return MFX_ERR_NONE;
  427. }
  428. static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  429. {
  430. QSVMid *qsv_mid = mid;
  431. AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)qsv_mid->hw_frames_ref->data;
  432. AVQSVFramesContext *hw_frames_hwctx = hw_frames_ctx->hwctx;
  433. int ret;
  434. if (qsv_mid->locked_frame)
  435. return MFX_ERR_UNDEFINED_BEHAVIOR;
  436. /* Allocate a system memory frame that will hold the mapped data. */
  437. qsv_mid->locked_frame = av_frame_alloc();
  438. if (!qsv_mid->locked_frame)
  439. return MFX_ERR_MEMORY_ALLOC;
  440. qsv_mid->locked_frame->format = hw_frames_ctx->sw_format;
  441. /* wrap the provided handle in a hwaccel AVFrame */
  442. qsv_mid->hw_frame = av_frame_alloc();
  443. if (!qsv_mid->hw_frame)
  444. goto fail;
  445. qsv_mid->hw_frame->data[3] = (uint8_t*)&qsv_mid->surf;
  446. qsv_mid->hw_frame->format = AV_PIX_FMT_QSV;
  447. // doesn't really matter what buffer is used here
  448. qsv_mid->hw_frame->buf[0] = av_buffer_alloc(1);
  449. if (!qsv_mid->hw_frame->buf[0])
  450. goto fail;
  451. qsv_mid->hw_frame->width = hw_frames_ctx->width;
  452. qsv_mid->hw_frame->height = hw_frames_ctx->height;
  453. qsv_mid->hw_frame->hw_frames_ctx = av_buffer_ref(qsv_mid->hw_frames_ref);
  454. if (!qsv_mid->hw_frame->hw_frames_ctx)
  455. goto fail;
  456. qsv_mid->surf.Info = hw_frames_hwctx->surfaces[0].Info;
  457. qsv_mid->surf.Data.MemId = qsv_mid->handle;
  458. /* map the data to the system memory */
  459. ret = av_hwframe_map(qsv_mid->locked_frame, qsv_mid->hw_frame,
  460. AV_HWFRAME_MAP_DIRECT);
  461. if (ret < 0)
  462. goto fail;
  463. ptr->Pitch = qsv_mid->locked_frame->linesize[0];
  464. ptr->Y = qsv_mid->locked_frame->data[0];
  465. ptr->U = qsv_mid->locked_frame->data[1];
  466. ptr->V = qsv_mid->locked_frame->data[1] + 1;
  467. return MFX_ERR_NONE;
  468. fail:
  469. av_frame_free(&qsv_mid->hw_frame);
  470. av_frame_free(&qsv_mid->locked_frame);
  471. return MFX_ERR_MEMORY_ALLOC;
  472. }
  473. static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  474. {
  475. QSVMid *qsv_mid = mid;
  476. av_frame_free(&qsv_mid->locked_frame);
  477. av_frame_free(&qsv_mid->hw_frame);
  478. return MFX_ERR_NONE;
  479. }
  480. static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
  481. {
  482. QSVMid *qsv_mid = (QSVMid*)mid;
  483. *hdl = qsv_mid->handle;
  484. return MFX_ERR_NONE;
  485. }
  486. int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession,
  487. AVBufferRef *device_ref, const char *load_plugins)
  488. {
  489. static const mfxHandleType handle_types[] = {
  490. MFX_HANDLE_VA_DISPLAY,
  491. MFX_HANDLE_D3D9_DEVICE_MANAGER,
  492. MFX_HANDLE_D3D11_DEVICE,
  493. };
  494. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref->data;
  495. AVQSVDeviceContext *device_hwctx = device_ctx->hwctx;
  496. mfxSession parent_session = device_hwctx->session;
  497. mfxSession session;
  498. mfxVersion ver;
  499. mfxIMPL impl;
  500. mfxHDL handle = NULL;
  501. mfxHandleType handle_type;
  502. mfxStatus err;
  503. int i, ret;
  504. err = MFXQueryIMPL(parent_session, &impl);
  505. if (err == MFX_ERR_NONE)
  506. err = MFXQueryVersion(parent_session, &ver);
  507. if (err != MFX_ERR_NONE)
  508. return ff_qsv_print_error(avctx, err,
  509. "Error querying the session attributes");
  510. for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
  511. err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
  512. if (err == MFX_ERR_NONE) {
  513. handle_type = handle_types[i];
  514. break;
  515. }
  516. handle = NULL;
  517. }
  518. if (!handle) {
  519. av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
  520. "from the session\n");
  521. }
  522. err = MFXInit(impl, &ver, &session);
  523. if (err != MFX_ERR_NONE)
  524. return ff_qsv_print_error(avctx, err,
  525. "Error initializing a child MFX session");
  526. if (handle) {
  527. err = MFXVideoCORE_SetHandle(session, handle_type, handle);
  528. if (err != MFX_ERR_NONE)
  529. return ff_qsv_print_error(avctx, err,
  530. "Error setting a HW handle");
  531. }
  532. if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
  533. err = MFXJoinSession(parent_session, session);
  534. if (err != MFX_ERR_NONE)
  535. return ff_qsv_print_error(avctx, err,
  536. "Error joining session");
  537. }
  538. ret = qsv_load_plugins(session, load_plugins, avctx);
  539. if (ret < 0) {
  540. av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
  541. return ret;
  542. }
  543. *psession = session;
  544. return 0;
  545. }
  546. int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession,
  547. QSVFramesContext *qsv_frames_ctx,
  548. const char *load_plugins, int opaque)
  549. {
  550. mfxFrameAllocator frame_allocator = {
  551. .pthis = qsv_frames_ctx,
  552. .Alloc = qsv_frame_alloc,
  553. .Lock = qsv_frame_lock,
  554. .Unlock = qsv_frame_unlock,
  555. .GetHDL = qsv_frame_get_hdl,
  556. .Free = qsv_frame_free,
  557. };
  558. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
  559. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  560. mfxSession session;
  561. mfxStatus err;
  562. int ret;
  563. ret = ff_qsv_init_session_device(avctx, &session,
  564. frames_ctx->device_ref, load_plugins);
  565. if (ret < 0)
  566. return ret;
  567. if (!opaque) {
  568. qsv_frames_ctx->logctx = avctx;
  569. /* allocate the memory ids for the external frames */
  570. av_buffer_unref(&qsv_frames_ctx->mids_buf);
  571. qsv_frames_ctx->mids_buf = qsv_create_mids(qsv_frames_ctx->hw_frames_ctx);
  572. if (!qsv_frames_ctx->mids_buf)
  573. return AVERROR(ENOMEM);
  574. qsv_frames_ctx->mids = (QSVMid*)qsv_frames_ctx->mids_buf->data;
  575. qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
  576. err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
  577. if (err != MFX_ERR_NONE)
  578. return ff_qsv_print_error(avctx, err,
  579. "Error setting a frame allocator");
  580. }
  581. *psession = session;
  582. return 0;
  583. }