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.

824 lines
27KB

  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. #if QSV_VERSION_ATLEAST(1, 19)
  58. case AV_CODEC_ID_VP9:
  59. return MFX_CODEC_VP9;
  60. #endif
  61. #if QSV_VERSION_ATLEAST(1, 34)
  62. case AV_CODEC_ID_AV1:
  63. return MFX_CODEC_AV1;
  64. #endif
  65. default:
  66. break;
  67. }
  68. return AVERROR(ENOSYS);
  69. }
  70. int ff_qsv_level_to_mfx(enum AVCodecID codec_id, int level)
  71. {
  72. if (level == FF_LEVEL_UNKNOWN)
  73. return MFX_LEVEL_UNKNOWN;
  74. switch (codec_id) {
  75. case AV_CODEC_ID_HEVC:
  76. return level / 3;
  77. default:
  78. return level;
  79. }
  80. }
  81. static const struct {
  82. int mfx_iopattern;
  83. const char *desc;
  84. } qsv_iopatterns[] = {
  85. {MFX_IOPATTERN_IN_VIDEO_MEMORY, "input is video memory surface" },
  86. {MFX_IOPATTERN_IN_SYSTEM_MEMORY, "input is system memory surface" },
  87. {MFX_IOPATTERN_IN_OPAQUE_MEMORY, "input is opaque memory surface" },
  88. {MFX_IOPATTERN_OUT_VIDEO_MEMORY, "output is video memory surface" },
  89. {MFX_IOPATTERN_OUT_SYSTEM_MEMORY, "output is system memory surface" },
  90. {MFX_IOPATTERN_OUT_OPAQUE_MEMORY, "output is opaque memory surface" },
  91. };
  92. int ff_qsv_print_iopattern(void *log_ctx, int mfx_iopattern,
  93. const char *extra_string)
  94. {
  95. const char *desc = NULL;
  96. for (int i = 0; i < FF_ARRAY_ELEMS(qsv_iopatterns); i++) {
  97. if (qsv_iopatterns[i].mfx_iopattern == mfx_iopattern) {
  98. desc = qsv_iopatterns[i].desc;
  99. }
  100. }
  101. if (!desc)
  102. desc = "unknown iopattern";
  103. av_log(log_ctx, AV_LOG_VERBOSE, "%s: %s\n", extra_string, desc);
  104. return 0;
  105. }
  106. static const struct {
  107. mfxStatus mfxerr;
  108. int averr;
  109. const char *desc;
  110. } qsv_errors[] = {
  111. { MFX_ERR_NONE, 0, "success" },
  112. { MFX_ERR_UNKNOWN, AVERROR_UNKNOWN, "unknown error" },
  113. { MFX_ERR_NULL_PTR, AVERROR(EINVAL), "NULL pointer" },
  114. { MFX_ERR_UNSUPPORTED, AVERROR(ENOSYS), "unsupported" },
  115. { MFX_ERR_MEMORY_ALLOC, AVERROR(ENOMEM), "failed to allocate memory" },
  116. { MFX_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOMEM), "insufficient input/output buffer" },
  117. { MFX_ERR_INVALID_HANDLE, AVERROR(EINVAL), "invalid handle" },
  118. { MFX_ERR_LOCK_MEMORY, AVERROR(EIO), "failed to lock the memory block" },
  119. { MFX_ERR_NOT_INITIALIZED, AVERROR_BUG, "not initialized" },
  120. { MFX_ERR_NOT_FOUND, AVERROR(ENOSYS), "specified object was not found" },
  121. /* the following 3 errors should always be handled explicitly, so those "mappings"
  122. * are for completeness only */
  123. { MFX_ERR_MORE_DATA, AVERROR_UNKNOWN, "expect more data at input" },
  124. { MFX_ERR_MORE_SURFACE, AVERROR_UNKNOWN, "expect more surface at output" },
  125. { MFX_ERR_MORE_BITSTREAM, AVERROR_UNKNOWN, "expect more bitstream at output" },
  126. { MFX_ERR_ABORTED, AVERROR_UNKNOWN, "operation aborted" },
  127. { MFX_ERR_DEVICE_LOST, AVERROR(EIO), "device lost" },
  128. { MFX_ERR_INCOMPATIBLE_VIDEO_PARAM, AVERROR(EINVAL), "incompatible video parameters" },
  129. { MFX_ERR_INVALID_VIDEO_PARAM, AVERROR(EINVAL), "invalid video parameters" },
  130. { MFX_ERR_UNDEFINED_BEHAVIOR, AVERROR_BUG, "undefined behavior" },
  131. { MFX_ERR_DEVICE_FAILED, AVERROR(EIO), "device failed" },
  132. { MFX_ERR_INCOMPATIBLE_AUDIO_PARAM, AVERROR(EINVAL), "incompatible audio parameters" },
  133. { MFX_ERR_INVALID_AUDIO_PARAM, AVERROR(EINVAL), "invalid audio parameters" },
  134. { MFX_WRN_IN_EXECUTION, 0, "operation in execution" },
  135. { MFX_WRN_DEVICE_BUSY, 0, "device busy" },
  136. { MFX_WRN_VIDEO_PARAM_CHANGED, 0, "video parameters changed" },
  137. { MFX_WRN_PARTIAL_ACCELERATION, 0, "partial acceleration" },
  138. { MFX_WRN_INCOMPATIBLE_VIDEO_PARAM, 0, "incompatible video parameters" },
  139. { MFX_WRN_VALUE_NOT_CHANGED, 0, "value is saturated" },
  140. { MFX_WRN_OUT_OF_RANGE, 0, "value out of range" },
  141. { MFX_WRN_FILTER_SKIPPED, 0, "filter skipped" },
  142. { MFX_WRN_INCOMPATIBLE_AUDIO_PARAM, 0, "incompatible audio parameters" },
  143. };
  144. int ff_qsv_map_error(mfxStatus mfx_err, const char **desc)
  145. {
  146. int i;
  147. for (i = 0; i < FF_ARRAY_ELEMS(qsv_errors); i++) {
  148. if (qsv_errors[i].mfxerr == mfx_err) {
  149. if (desc)
  150. *desc = qsv_errors[i].desc;
  151. return qsv_errors[i].averr;
  152. }
  153. }
  154. if (desc)
  155. *desc = "unknown error";
  156. return AVERROR_UNKNOWN;
  157. }
  158. int ff_qsv_print_error(void *log_ctx, mfxStatus err,
  159. const char *error_string)
  160. {
  161. const char *desc;
  162. int ret;
  163. ret = ff_qsv_map_error(err, &desc);
  164. av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
  165. return ret;
  166. }
  167. int ff_qsv_print_warning(void *log_ctx, mfxStatus err,
  168. const char *warning_string)
  169. {
  170. const char *desc;
  171. int ret;
  172. ret = ff_qsv_map_error(err, &desc);
  173. av_log(log_ctx, AV_LOG_WARNING, "%s: %s (%d)\n", warning_string, desc, err);
  174. return ret;
  175. }
  176. enum AVPixelFormat ff_qsv_map_fourcc(uint32_t fourcc)
  177. {
  178. switch (fourcc) {
  179. case MFX_FOURCC_NV12: return AV_PIX_FMT_NV12;
  180. case MFX_FOURCC_P010: return AV_PIX_FMT_P010;
  181. case MFX_FOURCC_P8: return AV_PIX_FMT_PAL8;
  182. #if CONFIG_VAAPI
  183. case MFX_FOURCC_YUY2: return AV_PIX_FMT_YUYV422;
  184. #if QSV_VERSION_ATLEAST(1, 27)
  185. case MFX_FOURCC_Y210: return AV_PIX_FMT_Y210;
  186. #endif
  187. #endif
  188. }
  189. return AV_PIX_FMT_NONE;
  190. }
  191. int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
  192. {
  193. switch (format) {
  194. case AV_PIX_FMT_YUV420P:
  195. case AV_PIX_FMT_YUVJ420P:
  196. case AV_PIX_FMT_NV12:
  197. *fourcc = MFX_FOURCC_NV12;
  198. return AV_PIX_FMT_NV12;
  199. case AV_PIX_FMT_YUV420P10:
  200. case AV_PIX_FMT_P010:
  201. *fourcc = MFX_FOURCC_P010;
  202. return AV_PIX_FMT_P010;
  203. #if CONFIG_VAAPI
  204. case AV_PIX_FMT_YUV422P:
  205. case AV_PIX_FMT_YUYV422:
  206. *fourcc = MFX_FOURCC_YUY2;
  207. return AV_PIX_FMT_YUYV422;
  208. #if QSV_VERSION_ATLEAST(1, 27)
  209. case AV_PIX_FMT_YUV422P10:
  210. case AV_PIX_FMT_Y210:
  211. *fourcc = MFX_FOURCC_Y210;
  212. return AV_PIX_FMT_Y210;
  213. #endif
  214. #endif
  215. default:
  216. return AVERROR(ENOSYS);
  217. }
  218. }
  219. int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame)
  220. {
  221. int i;
  222. for (i = 0; i < ctx->nb_mids; i++) {
  223. QSVMid *mid = &ctx->mids[i];
  224. if (mid->handle == frame->surface.Data.MemId)
  225. return i;
  226. }
  227. return AVERROR_BUG;
  228. }
  229. enum AVFieldOrder ff_qsv_map_picstruct(int mfx_pic_struct)
  230. {
  231. enum AVFieldOrder field = AV_FIELD_UNKNOWN;
  232. switch (mfx_pic_struct & 0xF) {
  233. case MFX_PICSTRUCT_PROGRESSIVE:
  234. field = AV_FIELD_PROGRESSIVE;
  235. break;
  236. case MFX_PICSTRUCT_FIELD_TFF:
  237. field = AV_FIELD_TT;
  238. break;
  239. case MFX_PICSTRUCT_FIELD_BFF:
  240. field = AV_FIELD_BB;
  241. break;
  242. }
  243. return field;
  244. }
  245. enum AVPictureType ff_qsv_map_pictype(int mfx_pic_type)
  246. {
  247. enum AVPictureType type;
  248. switch (mfx_pic_type & 0x7) {
  249. case MFX_FRAMETYPE_I:
  250. if (mfx_pic_type & MFX_FRAMETYPE_S)
  251. type = AV_PICTURE_TYPE_SI;
  252. else
  253. type = AV_PICTURE_TYPE_I;
  254. break;
  255. case MFX_FRAMETYPE_B:
  256. type = AV_PICTURE_TYPE_B;
  257. break;
  258. case MFX_FRAMETYPE_P:
  259. if (mfx_pic_type & MFX_FRAMETYPE_S)
  260. type = AV_PICTURE_TYPE_SP;
  261. else
  262. type = AV_PICTURE_TYPE_P;
  263. break;
  264. case MFX_FRAMETYPE_UNKNOWN:
  265. type = AV_PICTURE_TYPE_NONE;
  266. break;
  267. default:
  268. av_assert0(0);
  269. }
  270. return type;
  271. }
  272. static int qsv_load_plugins(mfxSession session, const char *load_plugins,
  273. void *logctx)
  274. {
  275. if (!load_plugins || !*load_plugins)
  276. return 0;
  277. while (*load_plugins) {
  278. mfxPluginUID uid;
  279. mfxStatus ret;
  280. int i, err = 0;
  281. char *plugin = av_get_token(&load_plugins, ":");
  282. if (!plugin)
  283. return AVERROR(ENOMEM);
  284. if (strlen(plugin) != 2 * sizeof(uid.Data)) {
  285. av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
  286. err = AVERROR(EINVAL);
  287. goto load_plugin_fail;
  288. }
  289. for (i = 0; i < sizeof(uid.Data); i++) {
  290. err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
  291. if (err != 1) {
  292. av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
  293. err = AVERROR(EINVAL);
  294. goto load_plugin_fail;
  295. }
  296. }
  297. ret = MFXVideoUSER_Load(session, &uid, 1);
  298. if (ret < 0) {
  299. char errorbuf[128];
  300. snprintf(errorbuf, sizeof(errorbuf),
  301. "Could not load the requested plugin '%s'", plugin);
  302. err = ff_qsv_print_error(logctx, ret, errorbuf);
  303. goto load_plugin_fail;
  304. }
  305. if (*load_plugins)
  306. load_plugins++;
  307. load_plugin_fail:
  308. av_freep(&plugin);
  309. if (err < 0)
  310. return err;
  311. }
  312. return 0;
  313. }
  314. //This code is only required for Linux since a display handle is required.
  315. //For Windows the session is complete and ready to use.
  316. #ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
  317. static int ff_qsv_set_display_handle(AVCodecContext *avctx, QSVSession *qs)
  318. {
  319. AVDictionary *child_device_opts = NULL;
  320. AVVAAPIDeviceContext *hwctx;
  321. int ret;
  322. av_dict_set(&child_device_opts, "kernel_driver", "i915", 0);
  323. av_dict_set(&child_device_opts, "driver", "iHD", 0);
  324. ret = av_hwdevice_ctx_create(&qs->va_device_ref, AV_HWDEVICE_TYPE_VAAPI, NULL, child_device_opts, 0);
  325. av_dict_free(&child_device_opts);
  326. if (ret < 0) {
  327. av_log(avctx, AV_LOG_ERROR, "Failed to create a VAAPI device.\n");
  328. return ret;
  329. } else {
  330. qs->va_device_ctx = (AVHWDeviceContext*)qs->va_device_ref->data;
  331. hwctx = qs->va_device_ctx->hwctx;
  332. ret = MFXVideoCORE_SetHandle(qs->session,
  333. (mfxHandleType)MFX_HANDLE_VA_DISPLAY, (mfxHDL)hwctx->display);
  334. if (ret < 0) {
  335. return ff_qsv_print_error(avctx, ret, "Error during set display handle\n");
  336. }
  337. }
  338. return 0;
  339. }
  340. #endif //AVCODEC_QSV_LINUX_SESSION_HANDLE
  341. int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs,
  342. const char *load_plugins, int gpu_copy)
  343. {
  344. mfxIMPL impl = MFX_IMPL_AUTO_ANY;
  345. mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
  346. mfxInitParam init_par = { MFX_IMPL_AUTO_ANY };
  347. const char *desc;
  348. int ret;
  349. #if QSV_VERSION_ATLEAST(1, 16)
  350. init_par.GPUCopy = gpu_copy;
  351. #endif
  352. init_par.Implementation = impl;
  353. init_par.Version = ver;
  354. ret = MFXInitEx(init_par, &qs->session);
  355. if (ret < 0)
  356. return ff_qsv_print_error(avctx, ret,
  357. "Error initializing an internal MFX session");
  358. #ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
  359. ret = ff_qsv_set_display_handle(avctx, qs);
  360. if (ret < 0)
  361. return ret;
  362. #endif
  363. ret = qsv_load_plugins(qs->session, load_plugins, avctx);
  364. if (ret < 0) {
  365. av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
  366. return ret;
  367. }
  368. MFXQueryIMPL(qs->session, &impl);
  369. switch (MFX_IMPL_BASETYPE(impl)) {
  370. case MFX_IMPL_SOFTWARE:
  371. desc = "software";
  372. break;
  373. case MFX_IMPL_HARDWARE:
  374. case MFX_IMPL_HARDWARE2:
  375. case MFX_IMPL_HARDWARE3:
  376. case MFX_IMPL_HARDWARE4:
  377. desc = "hardware accelerated";
  378. break;
  379. default:
  380. desc = "unknown";
  381. }
  382. av_log(avctx, AV_LOG_VERBOSE,
  383. "Initialized an internal MFX session using %s implementation\n",
  384. desc);
  385. return 0;
  386. }
  387. static void mids_buf_free(void *opaque, uint8_t *data)
  388. {
  389. AVBufferRef *hw_frames_ref = opaque;
  390. av_buffer_unref(&hw_frames_ref);
  391. av_freep(&data);
  392. }
  393. static AVBufferRef *qsv_create_mids(AVBufferRef *hw_frames_ref)
  394. {
  395. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
  396. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  397. int nb_surfaces = frames_hwctx->nb_surfaces;
  398. AVBufferRef *mids_buf, *hw_frames_ref1;
  399. QSVMid *mids;
  400. int i;
  401. hw_frames_ref1 = av_buffer_ref(hw_frames_ref);
  402. if (!hw_frames_ref1)
  403. return NULL;
  404. mids = av_mallocz_array(nb_surfaces, sizeof(*mids));
  405. if (!mids) {
  406. av_buffer_unref(&hw_frames_ref1);
  407. return NULL;
  408. }
  409. mids_buf = av_buffer_create((uint8_t*)mids, nb_surfaces * sizeof(*mids),
  410. mids_buf_free, hw_frames_ref1, 0);
  411. if (!mids_buf) {
  412. av_buffer_unref(&hw_frames_ref1);
  413. av_freep(&mids);
  414. return NULL;
  415. }
  416. for (i = 0; i < nb_surfaces; i++) {
  417. QSVMid *mid = &mids[i];
  418. mid->handle = frames_hwctx->surfaces[i].Data.MemId;
  419. mid->hw_frames_ref = hw_frames_ref1;
  420. }
  421. return mids_buf;
  422. }
  423. static int qsv_setup_mids(mfxFrameAllocResponse *resp, AVBufferRef *hw_frames_ref,
  424. AVBufferRef *mids_buf)
  425. {
  426. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
  427. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  428. QSVMid *mids = (QSVMid*)mids_buf->data;
  429. int nb_surfaces = frames_hwctx->nb_surfaces;
  430. int i;
  431. // the allocated size of the array is two larger than the number of
  432. // surfaces, we store the references to the frames context and the
  433. // QSVMid array there
  434. resp->mids = av_mallocz_array(nb_surfaces + 2, sizeof(*resp->mids));
  435. if (!resp->mids)
  436. return AVERROR(ENOMEM);
  437. for (i = 0; i < nb_surfaces; i++)
  438. resp->mids[i] = &mids[i];
  439. resp->NumFrameActual = nb_surfaces;
  440. resp->mids[resp->NumFrameActual] = (mfxMemId)av_buffer_ref(hw_frames_ref);
  441. if (!resp->mids[resp->NumFrameActual]) {
  442. av_freep(&resp->mids);
  443. return AVERROR(ENOMEM);
  444. }
  445. resp->mids[resp->NumFrameActual + 1] = av_buffer_ref(mids_buf);
  446. if (!resp->mids[resp->NumFrameActual + 1]) {
  447. av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
  448. av_freep(&resp->mids);
  449. return AVERROR(ENOMEM);
  450. }
  451. return 0;
  452. }
  453. static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
  454. mfxFrameAllocResponse *resp)
  455. {
  456. QSVFramesContext *ctx = pthis;
  457. int ret;
  458. /* this should only be called from an encoder or decoder and
  459. * only allocates video memory frames */
  460. if (!(req->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET |
  461. MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)) ||
  462. !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)))
  463. return MFX_ERR_UNSUPPORTED;
  464. if (req->Type & MFX_MEMTYPE_EXTERNAL_FRAME) {
  465. /* external frames -- fill from the caller-supplied frames context */
  466. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
  467. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  468. mfxFrameInfo *i = &req->Info;
  469. mfxFrameInfo *i1 = &frames_hwctx->surfaces[0].Info;
  470. if (i->Width > i1->Width || i->Height > i1->Height ||
  471. i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
  472. av_log(ctx->logctx, AV_LOG_ERROR, "Mismatching surface properties in an "
  473. "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
  474. i->Width, i->Height, i->FourCC, i->ChromaFormat,
  475. i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
  476. return MFX_ERR_UNSUPPORTED;
  477. }
  478. ret = qsv_setup_mids(resp, ctx->hw_frames_ctx, ctx->mids_buf);
  479. if (ret < 0) {
  480. av_log(ctx->logctx, AV_LOG_ERROR,
  481. "Error filling an external frame allocation request\n");
  482. return MFX_ERR_MEMORY_ALLOC;
  483. }
  484. } else if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME) {
  485. /* internal frames -- allocate a new hw frames context */
  486. AVHWFramesContext *ext_frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
  487. mfxFrameInfo *i = &req->Info;
  488. AVBufferRef *frames_ref, *mids_buf;
  489. AVHWFramesContext *frames_ctx;
  490. AVQSVFramesContext *frames_hwctx;
  491. frames_ref = av_hwframe_ctx_alloc(ext_frames_ctx->device_ref);
  492. if (!frames_ref)
  493. return MFX_ERR_MEMORY_ALLOC;
  494. frames_ctx = (AVHWFramesContext*)frames_ref->data;
  495. frames_hwctx = frames_ctx->hwctx;
  496. frames_ctx->format = AV_PIX_FMT_QSV;
  497. frames_ctx->sw_format = ff_qsv_map_fourcc(i->FourCC);
  498. frames_ctx->width = i->Width;
  499. frames_ctx->height = i->Height;
  500. frames_ctx->initial_pool_size = req->NumFrameSuggested;
  501. frames_hwctx->frame_type = req->Type;
  502. ret = av_hwframe_ctx_init(frames_ref);
  503. if (ret < 0) {
  504. av_log(ctx->logctx, AV_LOG_ERROR,
  505. "Error initializing a frames context for an internal frame "
  506. "allocation request\n");
  507. av_buffer_unref(&frames_ref);
  508. return MFX_ERR_MEMORY_ALLOC;
  509. }
  510. mids_buf = qsv_create_mids(frames_ref);
  511. if (!mids_buf) {
  512. av_buffer_unref(&frames_ref);
  513. return MFX_ERR_MEMORY_ALLOC;
  514. }
  515. ret = qsv_setup_mids(resp, frames_ref, mids_buf);
  516. av_buffer_unref(&mids_buf);
  517. av_buffer_unref(&frames_ref);
  518. if (ret < 0) {
  519. av_log(ctx->logctx, AV_LOG_ERROR,
  520. "Error filling an internal frame allocation request\n");
  521. return MFX_ERR_MEMORY_ALLOC;
  522. }
  523. } else {
  524. return MFX_ERR_UNSUPPORTED;
  525. }
  526. return MFX_ERR_NONE;
  527. }
  528. static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
  529. {
  530. av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
  531. av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual + 1]);
  532. av_freep(&resp->mids);
  533. return MFX_ERR_NONE;
  534. }
  535. static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  536. {
  537. QSVMid *qsv_mid = mid;
  538. AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)qsv_mid->hw_frames_ref->data;
  539. AVQSVFramesContext *hw_frames_hwctx = hw_frames_ctx->hwctx;
  540. int ret;
  541. if (qsv_mid->locked_frame)
  542. return MFX_ERR_UNDEFINED_BEHAVIOR;
  543. /* Allocate a system memory frame that will hold the mapped data. */
  544. qsv_mid->locked_frame = av_frame_alloc();
  545. if (!qsv_mid->locked_frame)
  546. return MFX_ERR_MEMORY_ALLOC;
  547. qsv_mid->locked_frame->format = hw_frames_ctx->sw_format;
  548. /* wrap the provided handle in a hwaccel AVFrame */
  549. qsv_mid->hw_frame = av_frame_alloc();
  550. if (!qsv_mid->hw_frame)
  551. goto fail;
  552. qsv_mid->hw_frame->data[3] = (uint8_t*)&qsv_mid->surf;
  553. qsv_mid->hw_frame->format = AV_PIX_FMT_QSV;
  554. // doesn't really matter what buffer is used here
  555. qsv_mid->hw_frame->buf[0] = av_buffer_alloc(1);
  556. if (!qsv_mid->hw_frame->buf[0])
  557. goto fail;
  558. qsv_mid->hw_frame->width = hw_frames_ctx->width;
  559. qsv_mid->hw_frame->height = hw_frames_ctx->height;
  560. qsv_mid->hw_frame->hw_frames_ctx = av_buffer_ref(qsv_mid->hw_frames_ref);
  561. if (!qsv_mid->hw_frame->hw_frames_ctx)
  562. goto fail;
  563. qsv_mid->surf.Info = hw_frames_hwctx->surfaces[0].Info;
  564. qsv_mid->surf.Data.MemId = qsv_mid->handle;
  565. /* map the data to the system memory */
  566. ret = av_hwframe_map(qsv_mid->locked_frame, qsv_mid->hw_frame,
  567. AV_HWFRAME_MAP_DIRECT);
  568. if (ret < 0)
  569. goto fail;
  570. ptr->Pitch = qsv_mid->locked_frame->linesize[0];
  571. ptr->Y = qsv_mid->locked_frame->data[0];
  572. ptr->U = qsv_mid->locked_frame->data[1];
  573. ptr->V = qsv_mid->locked_frame->data[1] + 1;
  574. return MFX_ERR_NONE;
  575. fail:
  576. av_frame_free(&qsv_mid->hw_frame);
  577. av_frame_free(&qsv_mid->locked_frame);
  578. return MFX_ERR_MEMORY_ALLOC;
  579. }
  580. static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  581. {
  582. QSVMid *qsv_mid = mid;
  583. av_frame_free(&qsv_mid->locked_frame);
  584. av_frame_free(&qsv_mid->hw_frame);
  585. return MFX_ERR_NONE;
  586. }
  587. static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
  588. {
  589. QSVMid *qsv_mid = (QSVMid*)mid;
  590. *hdl = qsv_mid->handle;
  591. return MFX_ERR_NONE;
  592. }
  593. int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession,
  594. AVBufferRef *device_ref, const char *load_plugins,
  595. int gpu_copy)
  596. {
  597. static const mfxHandleType handle_types[] = {
  598. MFX_HANDLE_VA_DISPLAY,
  599. MFX_HANDLE_D3D9_DEVICE_MANAGER,
  600. MFX_HANDLE_D3D11_DEVICE,
  601. };
  602. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref->data;
  603. AVQSVDeviceContext *device_hwctx = device_ctx->hwctx;
  604. mfxSession parent_session = device_hwctx->session;
  605. mfxInitParam init_par = { MFX_IMPL_AUTO_ANY };
  606. mfxHDL handle = NULL;
  607. mfxSession session;
  608. mfxVersion ver;
  609. mfxIMPL impl;
  610. mfxHandleType handle_type;
  611. mfxStatus err;
  612. int i, ret;
  613. err = MFXQueryIMPL(parent_session, &impl);
  614. if (err == MFX_ERR_NONE)
  615. err = MFXQueryVersion(parent_session, &ver);
  616. if (err != MFX_ERR_NONE)
  617. return ff_qsv_print_error(avctx, err,
  618. "Error querying the session attributes");
  619. for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
  620. err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
  621. if (err == MFX_ERR_NONE) {
  622. handle_type = handle_types[i];
  623. break;
  624. }
  625. handle = NULL;
  626. }
  627. if (!handle) {
  628. av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
  629. "from the session\n");
  630. }
  631. #if QSV_VERSION_ATLEAST(1, 16)
  632. init_par.GPUCopy = gpu_copy;
  633. #endif
  634. init_par.Implementation = impl;
  635. init_par.Version = ver;
  636. err = MFXInitEx(init_par, &session);
  637. if (err != MFX_ERR_NONE)
  638. return ff_qsv_print_error(avctx, err,
  639. "Error initializing a child MFX session");
  640. if (handle) {
  641. err = MFXVideoCORE_SetHandle(session, handle_type, handle);
  642. if (err != MFX_ERR_NONE)
  643. return ff_qsv_print_error(avctx, err,
  644. "Error setting a HW handle");
  645. }
  646. if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
  647. err = MFXJoinSession(parent_session, session);
  648. if (err != MFX_ERR_NONE)
  649. return ff_qsv_print_error(avctx, err,
  650. "Error joining session");
  651. }
  652. ret = qsv_load_plugins(session, load_plugins, avctx);
  653. if (ret < 0) {
  654. av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
  655. return ret;
  656. }
  657. *psession = session;
  658. return 0;
  659. }
  660. int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession,
  661. QSVFramesContext *qsv_frames_ctx,
  662. const char *load_plugins, int opaque, int gpu_copy)
  663. {
  664. mfxFrameAllocator frame_allocator = {
  665. .pthis = qsv_frames_ctx,
  666. .Alloc = qsv_frame_alloc,
  667. .Lock = qsv_frame_lock,
  668. .Unlock = qsv_frame_unlock,
  669. .GetHDL = qsv_frame_get_hdl,
  670. .Free = qsv_frame_free,
  671. };
  672. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
  673. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  674. mfxSession session;
  675. mfxStatus err;
  676. int ret;
  677. ret = ff_qsv_init_session_device(avctx, &session,
  678. frames_ctx->device_ref, load_plugins, gpu_copy);
  679. if (ret < 0)
  680. return ret;
  681. if (!opaque) {
  682. qsv_frames_ctx->logctx = avctx;
  683. /* allocate the memory ids for the external frames */
  684. av_buffer_unref(&qsv_frames_ctx->mids_buf);
  685. qsv_frames_ctx->mids_buf = qsv_create_mids(qsv_frames_ctx->hw_frames_ctx);
  686. if (!qsv_frames_ctx->mids_buf)
  687. return AVERROR(ENOMEM);
  688. qsv_frames_ctx->mids = (QSVMid*)qsv_frames_ctx->mids_buf->data;
  689. qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
  690. err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
  691. if (err != MFX_ERR_NONE)
  692. return ff_qsv_print_error(avctx, err,
  693. "Error setting a frame allocator");
  694. }
  695. *psession = session;
  696. return 0;
  697. }
  698. int ff_qsv_close_internal_session(QSVSession *qs)
  699. {
  700. if (qs->session) {
  701. MFXClose(qs->session);
  702. qs->session = NULL;
  703. }
  704. #ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
  705. av_buffer_unref(&qs->va_device_ref);
  706. #endif
  707. return 0;
  708. }