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.

572 lines
17KB

  1. /*
  2. * Intel MediaSDK QSV codec-independent code
  3. *
  4. * copyright (c) 2013 Luca Barbato
  5. * copyright (c) 2015 Anton Khirnov <anton@khirnov.net>
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include <mfx/mfxvideo.h>
  26. #include "libavutil/common.h"
  27. #include "libavutil/hwcontext.h"
  28. #include "libavutil/hwcontext_qsv.h"
  29. #include "libavutil/mem.h"
  30. #include "libavutil/log.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/pixfmt.h"
  33. #include "libavutil/time.h"
  34. #include "avcodec.h"
  35. #include "internal.h"
  36. #include "qsv.h"
  37. #include "qsv_internal.h"
  38. #include "qsvdec.h"
  39. const AVCodecHWConfigInternal *ff_qsv_hw_configs[] = {
  40. &(const AVCodecHWConfigInternal) {
  41. .public = {
  42. .pix_fmt = AV_PIX_FMT_QSV,
  43. .methods = AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX |
  44. AV_CODEC_HW_CONFIG_METHOD_AD_HOC,
  45. .device_type = AV_HWDEVICE_TYPE_QSV,
  46. },
  47. .hwaccel = NULL,
  48. },
  49. NULL
  50. };
  51. static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
  52. AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref)
  53. {
  54. int ret;
  55. if (session) {
  56. q->session = session;
  57. } else if (hw_frames_ref) {
  58. if (q->internal_session) {
  59. MFXClose(q->internal_session);
  60. q->internal_session = NULL;
  61. }
  62. av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
  63. q->frames_ctx.hw_frames_ctx = av_buffer_ref(hw_frames_ref);
  64. if (!q->frames_ctx.hw_frames_ctx)
  65. return AVERROR(ENOMEM);
  66. ret = ff_qsv_init_session_frames(avctx, &q->internal_session,
  67. &q->frames_ctx, q->load_plugins,
  68. q->iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY);
  69. if (ret < 0) {
  70. av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
  71. return ret;
  72. }
  73. q->session = q->internal_session;
  74. } else if (hw_device_ref) {
  75. if (q->internal_session) {
  76. MFXClose(q->internal_session);
  77. q->internal_session = NULL;
  78. }
  79. ret = ff_qsv_init_session_device(avctx, &q->internal_session,
  80. hw_device_ref, q->load_plugins);
  81. if (ret < 0)
  82. return ret;
  83. q->session = q->internal_session;
  84. } else {
  85. if (!q->internal_session) {
  86. ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
  87. q->load_plugins);
  88. if (ret < 0)
  89. return ret;
  90. }
  91. q->session = q->internal_session;
  92. }
  93. /* make sure the decoder is uninitialized */
  94. MFXVideoDECODE_Close(q->session);
  95. return 0;
  96. }
  97. static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q)
  98. {
  99. const AVPixFmtDescriptor *desc;
  100. mfxSession session = NULL;
  101. int iopattern = 0;
  102. mfxVideoParam param = { 0 };
  103. int frame_width = avctx->coded_width;
  104. int frame_height = avctx->coded_height;
  105. int ret;
  106. desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  107. if (!desc)
  108. return AVERROR_BUG;
  109. if (!q->async_fifo) {
  110. q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
  111. (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));
  112. if (!q->async_fifo)
  113. return AVERROR(ENOMEM);
  114. }
  115. if (avctx->pix_fmt == AV_PIX_FMT_QSV && avctx->hwaccel_context) {
  116. AVQSVContext *user_ctx = avctx->hwaccel_context;
  117. session = user_ctx->session;
  118. iopattern = user_ctx->iopattern;
  119. q->ext_buffers = user_ctx->ext_buffers;
  120. q->nb_ext_buffers = user_ctx->nb_ext_buffers;
  121. }
  122. if (avctx->hw_frames_ctx) {
  123. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  124. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  125. if (!iopattern) {
  126. if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
  127. iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
  128. else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
  129. iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
  130. }
  131. frame_width = frames_hwctx->surfaces[0].Info.Width;
  132. frame_height = frames_hwctx->surfaces[0].Info.Height;
  133. }
  134. if (!iopattern)
  135. iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
  136. q->iopattern = iopattern;
  137. ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx, avctx->hw_device_ctx);
  138. if (ret < 0) {
  139. av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
  140. return ret;
  141. }
  142. ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
  143. if (ret < 0)
  144. return ret;
  145. param.mfx.CodecId = ret;
  146. param.mfx.CodecProfile = ff_qsv_profile_to_mfx(avctx->codec_id, avctx->profile);
  147. param.mfx.CodecLevel = avctx->level == FF_LEVEL_UNKNOWN ? MFX_LEVEL_UNKNOWN : avctx->level;
  148. param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
  149. param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
  150. param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8;
  151. param.mfx.FrameInfo.FourCC = q->fourcc;
  152. param.mfx.FrameInfo.Width = frame_width;
  153. param.mfx.FrameInfo.Height = frame_height;
  154. param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  155. switch (avctx->field_order) {
  156. case AV_FIELD_PROGRESSIVE:
  157. param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
  158. break;
  159. case AV_FIELD_TT:
  160. param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
  161. break;
  162. case AV_FIELD_BB:
  163. param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_BFF;
  164. break;
  165. default:
  166. param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_UNKNOWN;
  167. break;
  168. }
  169. param.IOPattern = q->iopattern;
  170. param.AsyncDepth = q->async_depth;
  171. param.ExtParam = q->ext_buffers;
  172. param.NumExtParam = q->nb_ext_buffers;
  173. ret = MFXVideoDECODE_Init(q->session, &param);
  174. if (ret < 0)
  175. return ff_qsv_print_error(avctx, ret,
  176. "Error initializing the MFX video decoder");
  177. q->frame_info = param.mfx.FrameInfo;
  178. return 0;
  179. }
  180. static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
  181. {
  182. int ret;
  183. ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
  184. if (ret < 0)
  185. return ret;
  186. if (frame->frame->format == AV_PIX_FMT_QSV) {
  187. frame->surface = *(mfxFrameSurface1*)frame->frame->data[3];
  188. } else {
  189. frame->surface.Info = q->frame_info;
  190. frame->surface.Data.PitchLow = frame->frame->linesize[0];
  191. frame->surface.Data.Y = frame->frame->data[0];
  192. frame->surface.Data.UV = frame->frame->data[1];
  193. }
  194. if (q->frames_ctx.mids) {
  195. ret = ff_qsv_find_surface_idx(&q->frames_ctx, frame);
  196. if (ret < 0)
  197. return ret;
  198. frame->surface.Data.MemId = &q->frames_ctx.mids[ret];
  199. }
  200. frame->used = 1;
  201. return 0;
  202. }
  203. static void qsv_clear_unused_frames(QSVContext *q)
  204. {
  205. QSVFrame *cur = q->work_frames;
  206. while (cur) {
  207. if (cur->used && !cur->surface.Data.Locked && !cur->queued) {
  208. cur->used = 0;
  209. av_frame_unref(cur->frame);
  210. }
  211. cur = cur->next;
  212. }
  213. }
  214. static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
  215. {
  216. QSVFrame *frame, **last;
  217. int ret;
  218. qsv_clear_unused_frames(q);
  219. frame = q->work_frames;
  220. last = &q->work_frames;
  221. while (frame) {
  222. if (!frame->used) {
  223. ret = alloc_frame(avctx, q, frame);
  224. if (ret < 0)
  225. return ret;
  226. *surf = &frame->surface;
  227. return 0;
  228. }
  229. last = &frame->next;
  230. frame = frame->next;
  231. }
  232. frame = av_mallocz(sizeof(*frame));
  233. if (!frame)
  234. return AVERROR(ENOMEM);
  235. frame->frame = av_frame_alloc();
  236. if (!frame->frame) {
  237. av_freep(&frame);
  238. return AVERROR(ENOMEM);
  239. }
  240. *last = frame;
  241. ret = alloc_frame(avctx, q, frame);
  242. if (ret < 0)
  243. return ret;
  244. *surf = &frame->surface;
  245. return 0;
  246. }
  247. static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
  248. {
  249. QSVFrame *cur = q->work_frames;
  250. while (cur) {
  251. if (surf == &cur->surface)
  252. return cur;
  253. cur = cur->next;
  254. }
  255. return NULL;
  256. }
  257. static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
  258. AVFrame *frame, int *got_frame,
  259. AVPacket *avpkt)
  260. {
  261. QSVFrame *out_frame;
  262. mfxFrameSurface1 *insurf;
  263. mfxFrameSurface1 *outsurf;
  264. mfxSyncPoint *sync;
  265. mfxBitstream bs = { { { 0 } } };
  266. int ret;
  267. if (avpkt->size) {
  268. bs.Data = avpkt->data;
  269. bs.DataLength = avpkt->size;
  270. bs.MaxLength = bs.DataLength;
  271. bs.TimeStamp = avpkt->pts;
  272. }
  273. sync = av_mallocz(sizeof(*sync));
  274. if (!sync) {
  275. av_freep(&sync);
  276. return AVERROR(ENOMEM);
  277. }
  278. do {
  279. ret = get_surface(avctx, q, &insurf);
  280. if (ret < 0) {
  281. av_freep(&sync);
  282. return ret;
  283. }
  284. ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
  285. insurf, &outsurf, sync);
  286. if (ret == MFX_WRN_DEVICE_BUSY)
  287. av_usleep(1);
  288. } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
  289. if (ret != MFX_ERR_NONE &&
  290. ret != MFX_ERR_MORE_DATA &&
  291. ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
  292. ret != MFX_ERR_MORE_SURFACE) {
  293. av_freep(&sync);
  294. return ff_qsv_print_error(avctx, ret,
  295. "Error during QSV decoding.");
  296. }
  297. /* make sure we do not enter an infinite loop if the SDK
  298. * did not consume any data and did not return anything */
  299. if (!*sync && !bs.DataOffset) {
  300. bs.DataOffset = avpkt->size;
  301. ++q->zero_consume_run;
  302. if (q->zero_consume_run > 1)
  303. ff_qsv_print_warning(avctx, ret, "A decode call did not consume any data");
  304. } else {
  305. q->zero_consume_run = 0;
  306. }
  307. if (*sync) {
  308. QSVFrame *out_frame = find_frame(q, outsurf);
  309. if (!out_frame) {
  310. av_log(avctx, AV_LOG_ERROR,
  311. "The returned surface does not correspond to any frame\n");
  312. av_freep(&sync);
  313. return AVERROR_BUG;
  314. }
  315. out_frame->queued = 1;
  316. av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  317. av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
  318. } else {
  319. av_freep(&sync);
  320. }
  321. if (!av_fifo_space(q->async_fifo) ||
  322. (!avpkt->size && av_fifo_size(q->async_fifo))) {
  323. AVFrame *src_frame;
  324. av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  325. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  326. out_frame->queued = 0;
  327. if (avctx->pix_fmt != AV_PIX_FMT_QSV) {
  328. do {
  329. ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
  330. } while (ret == MFX_WRN_IN_EXECUTION);
  331. }
  332. av_freep(&sync);
  333. src_frame = out_frame->frame;
  334. ret = av_frame_ref(frame, src_frame);
  335. if (ret < 0)
  336. return ret;
  337. outsurf = &out_frame->surface;
  338. #if FF_API_PKT_PTS
  339. FF_DISABLE_DEPRECATION_WARNINGS
  340. frame->pkt_pts = outsurf->Data.TimeStamp;
  341. FF_ENABLE_DEPRECATION_WARNINGS
  342. #endif
  343. frame->pts = outsurf->Data.TimeStamp;
  344. frame->repeat_pict =
  345. outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
  346. outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
  347. outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
  348. frame->top_field_first =
  349. outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
  350. frame->interlaced_frame =
  351. !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
  352. /* update the surface properties */
  353. if (avctx->pix_fmt == AV_PIX_FMT_QSV)
  354. ((mfxFrameSurface1*)frame->data[3])->Info = outsurf->Info;
  355. *got_frame = 1;
  356. }
  357. return bs.DataOffset;
  358. }
  359. int ff_qsv_decode_close(QSVContext *q)
  360. {
  361. QSVFrame *cur = q->work_frames;
  362. if (q->session)
  363. MFXVideoDECODE_Close(q->session);
  364. while (q->async_fifo && av_fifo_size(q->async_fifo)) {
  365. QSVFrame *out_frame;
  366. mfxSyncPoint *sync;
  367. av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  368. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  369. av_freep(&sync);
  370. }
  371. while (cur) {
  372. q->work_frames = cur->next;
  373. av_frame_free(&cur->frame);
  374. av_freep(&cur);
  375. cur = q->work_frames;
  376. }
  377. av_fifo_free(q->async_fifo);
  378. q->async_fifo = NULL;
  379. av_parser_close(q->parser);
  380. avcodec_free_context(&q->avctx_internal);
  381. if (q->internal_session)
  382. MFXClose(q->internal_session);
  383. av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
  384. av_buffer_unref(&q->frames_ctx.mids_buf);
  385. return 0;
  386. }
  387. int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
  388. AVFrame *frame, int *got_frame, AVPacket *pkt)
  389. {
  390. uint8_t *dummy_data;
  391. int dummy_size;
  392. int ret;
  393. const AVPixFmtDescriptor *desc;
  394. if (!q->avctx_internal) {
  395. q->avctx_internal = avcodec_alloc_context3(NULL);
  396. if (!q->avctx_internal)
  397. return AVERROR(ENOMEM);
  398. if (avctx->extradata) {
  399. q->avctx_internal->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  400. if (!q->avctx_internal->extradata)
  401. return AVERROR(ENOMEM);
  402. memcpy(q->avctx_internal->extradata, avctx->extradata,
  403. avctx->extradata_size);
  404. q->avctx_internal->extradata_size = avctx->extradata_size;
  405. }
  406. q->parser = av_parser_init(avctx->codec_id);
  407. if (!q->parser)
  408. return AVERROR(ENOMEM);
  409. q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
  410. q->orig_pix_fmt = AV_PIX_FMT_NONE;
  411. }
  412. if (!pkt->size)
  413. return qsv_decode(avctx, q, frame, got_frame, pkt);
  414. /* we assume the packets are already split properly and want
  415. * just the codec parameters here */
  416. av_parser_parse2(q->parser, q->avctx_internal,
  417. &dummy_data, &dummy_size,
  418. pkt->data, pkt->size, pkt->pts, pkt->dts,
  419. pkt->pos);
  420. /* TODO: flush delayed frames on reinit */
  421. if (q->parser->format != q->orig_pix_fmt ||
  422. FFALIGN(q->parser->coded_width, 16) != FFALIGN(avctx->coded_width, 16) ||
  423. FFALIGN(q->parser->coded_height, 16) != FFALIGN(avctx->coded_height, 16)) {
  424. enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
  425. AV_PIX_FMT_NONE,
  426. AV_PIX_FMT_NONE };
  427. enum AVPixelFormat qsv_format;
  428. qsv_format = ff_qsv_map_pixfmt(q->parser->format, &q->fourcc);
  429. if (qsv_format < 0) {
  430. av_log(avctx, AV_LOG_ERROR,
  431. "Decoding pixel format '%s' is not supported\n",
  432. av_get_pix_fmt_name(q->parser->format));
  433. ret = AVERROR(ENOSYS);
  434. goto reinit_fail;
  435. }
  436. q->orig_pix_fmt = q->parser->format;
  437. avctx->pix_fmt = pix_fmts[1] = qsv_format;
  438. avctx->width = q->parser->width;
  439. avctx->height = q->parser->height;
  440. avctx->coded_width = FFALIGN(q->parser->coded_width, 16);
  441. avctx->coded_height = FFALIGN(q->parser->coded_height, 16);
  442. avctx->field_order = q->parser->field_order;
  443. avctx->level = q->avctx_internal->level;
  444. avctx->profile = q->avctx_internal->profile;
  445. ret = ff_get_format(avctx, pix_fmts);
  446. if (ret < 0)
  447. goto reinit_fail;
  448. avctx->pix_fmt = ret;
  449. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  450. if (!desc)
  451. goto reinit_fail;
  452. if (desc->comp[0].depth > 8) {
  453. avctx->coded_width = FFALIGN(q->parser->coded_width, 32);
  454. avctx->coded_height = FFALIGN(q->parser->coded_height, 32);
  455. }
  456. ret = qsv_decode_init(avctx, q);
  457. if (ret < 0)
  458. goto reinit_fail;
  459. }
  460. return qsv_decode(avctx, q, frame, got_frame, pkt);
  461. reinit_fail:
  462. q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
  463. return ret;
  464. }
  465. void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
  466. {
  467. q->orig_pix_fmt = AV_PIX_FMT_NONE;
  468. }