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.

549 lines
16KB

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