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.

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