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.

686 lines
23KB

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