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.

589 lines
18KB

  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 inline unsigned int qsv_fifo_item_size(void)
  98. {
  99. return sizeof(mfxSyncPoint*) + sizeof(QSVFrame*);
  100. }
  101. static inline unsigned int qsv_fifo_size(const AVFifoBuffer* fifo)
  102. {
  103. return av_fifo_size(fifo) / qsv_fifo_item_size();
  104. }
  105. static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q)
  106. {
  107. const AVPixFmtDescriptor *desc;
  108. mfxSession session = NULL;
  109. int iopattern = 0;
  110. mfxVideoParam param = { 0 };
  111. int frame_width = avctx->coded_width;
  112. int frame_height = avctx->coded_height;
  113. int ret;
  114. desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  115. if (!desc)
  116. return AVERROR_BUG;
  117. if (!q->async_fifo) {
  118. q->async_fifo = av_fifo_alloc(q->async_depth * qsv_fifo_item_size());
  119. if (!q->async_fifo)
  120. return AVERROR(ENOMEM);
  121. }
  122. if (avctx->pix_fmt == AV_PIX_FMT_QSV && avctx->hwaccel_context) {
  123. AVQSVContext *user_ctx = avctx->hwaccel_context;
  124. session = user_ctx->session;
  125. iopattern = user_ctx->iopattern;
  126. q->ext_buffers = user_ctx->ext_buffers;
  127. q->nb_ext_buffers = user_ctx->nb_ext_buffers;
  128. }
  129. if (avctx->hw_frames_ctx) {
  130. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  131. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  132. if (!iopattern) {
  133. if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
  134. iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
  135. else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
  136. iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
  137. }
  138. }
  139. if (!iopattern)
  140. iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
  141. q->iopattern = iopattern;
  142. ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx, avctx->hw_device_ctx);
  143. if (ret < 0) {
  144. av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
  145. return ret;
  146. }
  147. ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
  148. if (ret < 0)
  149. return ret;
  150. param.mfx.CodecId = ret;
  151. param.mfx.CodecProfile = ff_qsv_profile_to_mfx(avctx->codec_id, avctx->profile);
  152. param.mfx.CodecLevel = avctx->level == FF_LEVEL_UNKNOWN ? MFX_LEVEL_UNKNOWN : avctx->level;
  153. param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
  154. param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
  155. param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8;
  156. param.mfx.FrameInfo.FourCC = q->fourcc;
  157. param.mfx.FrameInfo.Width = frame_width;
  158. param.mfx.FrameInfo.Height = frame_height;
  159. param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  160. switch (avctx->field_order) {
  161. case AV_FIELD_PROGRESSIVE:
  162. param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
  163. break;
  164. case AV_FIELD_TT:
  165. param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
  166. break;
  167. case AV_FIELD_BB:
  168. param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_BFF;
  169. break;
  170. default:
  171. param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_UNKNOWN;
  172. break;
  173. }
  174. param.IOPattern = q->iopattern;
  175. param.AsyncDepth = q->async_depth;
  176. param.ExtParam = q->ext_buffers;
  177. param.NumExtParam = q->nb_ext_buffers;
  178. ret = MFXVideoDECODE_Init(q->session, &param);
  179. if (ret < 0)
  180. return ff_qsv_print_error(avctx, ret,
  181. "Error initializing the MFX video decoder");
  182. q->frame_info = param.mfx.FrameInfo;
  183. return 0;
  184. }
  185. static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
  186. {
  187. int ret;
  188. ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
  189. if (ret < 0)
  190. return ret;
  191. if (frame->frame->format == AV_PIX_FMT_QSV) {
  192. frame->surface = *(mfxFrameSurface1*)frame->frame->data[3];
  193. } else {
  194. frame->surface.Info = q->frame_info;
  195. frame->surface.Data.PitchLow = frame->frame->linesize[0];
  196. frame->surface.Data.Y = frame->frame->data[0];
  197. frame->surface.Data.UV = frame->frame->data[1];
  198. }
  199. if (q->frames_ctx.mids) {
  200. ret = ff_qsv_find_surface_idx(&q->frames_ctx, frame);
  201. if (ret < 0)
  202. return ret;
  203. frame->surface.Data.MemId = &q->frames_ctx.mids[ret];
  204. }
  205. frame->surface.Data.ExtParam = &frame->ext_param;
  206. frame->surface.Data.NumExtParam = 1;
  207. frame->ext_param = (mfxExtBuffer*)&frame->dec_info;
  208. frame->dec_info.Header.BufferId = MFX_EXTBUFF_DECODED_FRAME_INFO;
  209. frame->dec_info.Header.BufferSz = sizeof(frame->dec_info);
  210. frame->used = 1;
  211. return 0;
  212. }
  213. static void qsv_clear_unused_frames(QSVContext *q)
  214. {
  215. QSVFrame *cur = q->work_frames;
  216. while (cur) {
  217. if (cur->used && !cur->surface.Data.Locked && !cur->queued) {
  218. cur->used = 0;
  219. av_frame_unref(cur->frame);
  220. }
  221. cur = cur->next;
  222. }
  223. }
  224. static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
  225. {
  226. QSVFrame *frame, **last;
  227. int ret;
  228. qsv_clear_unused_frames(q);
  229. frame = q->work_frames;
  230. last = &q->work_frames;
  231. while (frame) {
  232. if (!frame->used) {
  233. ret = alloc_frame(avctx, q, frame);
  234. if (ret < 0)
  235. return ret;
  236. *surf = &frame->surface;
  237. return 0;
  238. }
  239. last = &frame->next;
  240. frame = frame->next;
  241. }
  242. frame = av_mallocz(sizeof(*frame));
  243. if (!frame)
  244. return AVERROR(ENOMEM);
  245. frame->frame = av_frame_alloc();
  246. if (!frame->frame) {
  247. av_freep(&frame);
  248. return AVERROR(ENOMEM);
  249. }
  250. *last = frame;
  251. ret = alloc_frame(avctx, q, frame);
  252. if (ret < 0)
  253. return ret;
  254. *surf = &frame->surface;
  255. return 0;
  256. }
  257. static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
  258. {
  259. QSVFrame *cur = q->work_frames;
  260. while (cur) {
  261. if (surf == &cur->surface)
  262. return cur;
  263. cur = cur->next;
  264. }
  265. return NULL;
  266. }
  267. static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
  268. AVFrame *frame, int *got_frame,
  269. AVPacket *avpkt)
  270. {
  271. QSVFrame *out_frame;
  272. mfxFrameSurface1 *insurf;
  273. mfxFrameSurface1 *outsurf;
  274. mfxSyncPoint *sync;
  275. mfxBitstream bs = { { { 0 } } };
  276. int ret;
  277. if (avpkt->size) {
  278. bs.Data = avpkt->data;
  279. bs.DataLength = avpkt->size;
  280. bs.MaxLength = bs.DataLength;
  281. bs.TimeStamp = avpkt->pts;
  282. if (avctx->field_order == AV_FIELD_PROGRESSIVE)
  283. bs.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME;
  284. }
  285. sync = av_mallocz(sizeof(*sync));
  286. if (!sync) {
  287. av_freep(&sync);
  288. return AVERROR(ENOMEM);
  289. }
  290. do {
  291. ret = get_surface(avctx, q, &insurf);
  292. if (ret < 0) {
  293. av_freep(&sync);
  294. return ret;
  295. }
  296. ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
  297. insurf, &outsurf, sync);
  298. if (ret == MFX_WRN_DEVICE_BUSY)
  299. av_usleep(1);
  300. } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
  301. if (ret != MFX_ERR_NONE &&
  302. ret != MFX_ERR_MORE_DATA &&
  303. ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
  304. ret != MFX_ERR_MORE_SURFACE) {
  305. av_freep(&sync);
  306. return ff_qsv_print_error(avctx, ret,
  307. "Error during QSV decoding.");
  308. }
  309. /* make sure we do not enter an infinite loop if the SDK
  310. * did not consume any data and did not return anything */
  311. if (!*sync && !bs.DataOffset) {
  312. bs.DataOffset = avpkt->size;
  313. ++q->zero_consume_run;
  314. if (q->zero_consume_run > 1)
  315. ff_qsv_print_warning(avctx, ret, "A decode call did not consume any data");
  316. } else {
  317. q->zero_consume_run = 0;
  318. }
  319. if (*sync) {
  320. QSVFrame *out_frame = find_frame(q, outsurf);
  321. if (!out_frame) {
  322. av_log(avctx, AV_LOG_ERROR,
  323. "The returned surface does not correspond to any frame\n");
  324. av_freep(&sync);
  325. return AVERROR_BUG;
  326. }
  327. out_frame->queued = 1;
  328. av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  329. av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
  330. } else {
  331. av_freep(&sync);
  332. }
  333. if ((qsv_fifo_size(q->async_fifo) >= q->async_depth) ||
  334. (!avpkt->size && av_fifo_size(q->async_fifo))) {
  335. AVFrame *src_frame;
  336. av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  337. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  338. out_frame->queued = 0;
  339. if (avctx->pix_fmt != AV_PIX_FMT_QSV) {
  340. do {
  341. ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
  342. } while (ret == MFX_WRN_IN_EXECUTION);
  343. }
  344. av_freep(&sync);
  345. src_frame = out_frame->frame;
  346. ret = av_frame_ref(frame, src_frame);
  347. if (ret < 0)
  348. return ret;
  349. outsurf = &out_frame->surface;
  350. #if FF_API_PKT_PTS
  351. FF_DISABLE_DEPRECATION_WARNINGS
  352. frame->pkt_pts = outsurf->Data.TimeStamp;
  353. FF_ENABLE_DEPRECATION_WARNINGS
  354. #endif
  355. frame->pts = outsurf->Data.TimeStamp;
  356. frame->repeat_pict =
  357. outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
  358. outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
  359. outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
  360. frame->top_field_first =
  361. outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
  362. frame->interlaced_frame =
  363. !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
  364. frame->pict_type = ff_qsv_map_pictype(out_frame->dec_info.FrameType);
  365. //Key frame is IDR frame is only suitable for H264. For HEVC, IRAPs are key frames.
  366. if (avctx->codec_id == AV_CODEC_ID_H264)
  367. frame->key_frame = !!(out_frame->dec_info.FrameType & MFX_FRAMETYPE_IDR);
  368. /* update the surface properties */
  369. if (avctx->pix_fmt == AV_PIX_FMT_QSV)
  370. ((mfxFrameSurface1*)frame->data[3])->Info = outsurf->Info;
  371. *got_frame = 1;
  372. }
  373. return bs.DataOffset;
  374. }
  375. int ff_qsv_decode_close(QSVContext *q)
  376. {
  377. QSVFrame *cur = q->work_frames;
  378. if (q->session)
  379. MFXVideoDECODE_Close(q->session);
  380. while (q->async_fifo && av_fifo_size(q->async_fifo)) {
  381. QSVFrame *out_frame;
  382. mfxSyncPoint *sync;
  383. av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  384. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  385. av_freep(&sync);
  386. }
  387. while (cur) {
  388. q->work_frames = cur->next;
  389. av_frame_free(&cur->frame);
  390. av_freep(&cur);
  391. cur = q->work_frames;
  392. }
  393. av_fifo_free(q->async_fifo);
  394. q->async_fifo = NULL;
  395. av_parser_close(q->parser);
  396. avcodec_free_context(&q->avctx_internal);
  397. if (q->internal_session)
  398. MFXClose(q->internal_session);
  399. av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
  400. av_buffer_unref(&q->frames_ctx.mids_buf);
  401. return 0;
  402. }
  403. int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
  404. AVFrame *frame, int *got_frame, AVPacket *pkt)
  405. {
  406. uint8_t *dummy_data;
  407. int dummy_size;
  408. int ret;
  409. const AVPixFmtDescriptor *desc;
  410. if (!q->avctx_internal) {
  411. q->avctx_internal = avcodec_alloc_context3(NULL);
  412. if (!q->avctx_internal)
  413. return AVERROR(ENOMEM);
  414. if (avctx->extradata) {
  415. q->avctx_internal->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  416. if (!q->avctx_internal->extradata)
  417. return AVERROR(ENOMEM);
  418. memcpy(q->avctx_internal->extradata, avctx->extradata,
  419. avctx->extradata_size);
  420. q->avctx_internal->extradata_size = avctx->extradata_size;
  421. }
  422. q->parser = av_parser_init(avctx->codec_id);
  423. if (!q->parser)
  424. return AVERROR(ENOMEM);
  425. q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
  426. q->orig_pix_fmt = AV_PIX_FMT_NONE;
  427. }
  428. if (!pkt->size)
  429. return qsv_decode(avctx, q, frame, got_frame, pkt);
  430. /* we assume the packets are already split properly and want
  431. * just the codec parameters here */
  432. av_parser_parse2(q->parser, q->avctx_internal,
  433. &dummy_data, &dummy_size,
  434. pkt->data, pkt->size, pkt->pts, pkt->dts,
  435. pkt->pos);
  436. avctx->field_order = q->parser->field_order;
  437. /* TODO: flush delayed frames on reinit */
  438. if (q->parser->format != q->orig_pix_fmt ||
  439. FFALIGN(q->parser->coded_width, 16) != FFALIGN(avctx->coded_width, 16) ||
  440. FFALIGN(q->parser->coded_height, 16) != FFALIGN(avctx->coded_height, 16)) {
  441. enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
  442. AV_PIX_FMT_NONE,
  443. AV_PIX_FMT_NONE };
  444. enum AVPixelFormat qsv_format;
  445. qsv_format = ff_qsv_map_pixfmt(q->parser->format, &q->fourcc);
  446. if (qsv_format < 0) {
  447. av_log(avctx, AV_LOG_ERROR,
  448. "Decoding pixel format '%s' is not supported\n",
  449. av_get_pix_fmt_name(q->parser->format));
  450. ret = AVERROR(ENOSYS);
  451. goto reinit_fail;
  452. }
  453. q->orig_pix_fmt = q->parser->format;
  454. avctx->pix_fmt = pix_fmts[1] = qsv_format;
  455. avctx->width = q->parser->width;
  456. avctx->height = q->parser->height;
  457. avctx->coded_width = FFALIGN(q->parser->coded_width, 16);
  458. avctx->coded_height = FFALIGN(q->parser->coded_height, 16);
  459. avctx->level = q->avctx_internal->level;
  460. avctx->profile = q->avctx_internal->profile;
  461. ret = ff_get_format(avctx, pix_fmts);
  462. if (ret < 0)
  463. goto reinit_fail;
  464. avctx->pix_fmt = ret;
  465. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  466. if (!desc)
  467. goto reinit_fail;
  468. if (desc->comp[0].depth > 8) {
  469. avctx->coded_width = FFALIGN(q->parser->coded_width, 32);
  470. avctx->coded_height = FFALIGN(q->parser->coded_height, 32);
  471. }
  472. ret = qsv_decode_init(avctx, q);
  473. if (ret < 0)
  474. goto reinit_fail;
  475. }
  476. return qsv_decode(avctx, q, frame, got_frame, pkt);
  477. reinit_fail:
  478. q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
  479. return ret;
  480. }
  481. void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
  482. {
  483. q->orig_pix_fmt = AV_PIX_FMT_NONE;
  484. }