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.

761 lines
25KB

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