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.

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