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.

513 lines
15KB

  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 = avctx->profile;
  125. param.mfx.CodecLevel = 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. param.IOPattern = q->iopattern;
  134. param.AsyncDepth = q->async_depth;
  135. param.ExtParam = q->ext_buffers;
  136. param.NumExtParam = q->nb_ext_buffers;
  137. ret = MFXVideoDECODE_Init(q->session, &param);
  138. if (ret < 0)
  139. return ff_qsv_print_error(avctx, ret,
  140. "Error initializing the MFX video decoder");
  141. q->frame_info = param.mfx.FrameInfo;
  142. return 0;
  143. }
  144. static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
  145. {
  146. int ret;
  147. ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
  148. if (ret < 0)
  149. return ret;
  150. if (frame->frame->format == AV_PIX_FMT_QSV) {
  151. frame->surface = *(mfxFrameSurface1*)frame->frame->data[3];
  152. } else {
  153. frame->surface.Info = q->frame_info;
  154. frame->surface.Data.PitchLow = frame->frame->linesize[0];
  155. frame->surface.Data.Y = frame->frame->data[0];
  156. frame->surface.Data.UV = frame->frame->data[1];
  157. }
  158. if (q->frames_ctx.mids) {
  159. ret = ff_qsv_find_surface_idx(&q->frames_ctx, frame);
  160. if (ret < 0)
  161. return ret;
  162. frame->surface.Data.MemId = &q->frames_ctx.mids[ret];
  163. }
  164. frame->used = 1;
  165. return 0;
  166. }
  167. static void qsv_clear_unused_frames(QSVContext *q)
  168. {
  169. QSVFrame *cur = q->work_frames;
  170. while (cur) {
  171. if (cur->used && !cur->surface.Data.Locked && !cur->queued) {
  172. cur->used = 0;
  173. av_frame_unref(cur->frame);
  174. }
  175. cur = cur->next;
  176. }
  177. }
  178. static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
  179. {
  180. QSVFrame *frame, **last;
  181. int ret;
  182. qsv_clear_unused_frames(q);
  183. frame = q->work_frames;
  184. last = &q->work_frames;
  185. while (frame) {
  186. if (!frame->used) {
  187. ret = alloc_frame(avctx, q, frame);
  188. if (ret < 0)
  189. return ret;
  190. *surf = &frame->surface;
  191. return 0;
  192. }
  193. last = &frame->next;
  194. frame = frame->next;
  195. }
  196. frame = av_mallocz(sizeof(*frame));
  197. if (!frame)
  198. return AVERROR(ENOMEM);
  199. frame->frame = av_frame_alloc();
  200. if (!frame->frame) {
  201. av_freep(&frame);
  202. return AVERROR(ENOMEM);
  203. }
  204. *last = frame;
  205. ret = alloc_frame(avctx, q, frame);
  206. if (ret < 0)
  207. return ret;
  208. *surf = &frame->surface;
  209. return 0;
  210. }
  211. static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
  212. {
  213. QSVFrame *cur = q->work_frames;
  214. while (cur) {
  215. if (surf == &cur->surface)
  216. return cur;
  217. cur = cur->next;
  218. }
  219. return NULL;
  220. }
  221. static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
  222. AVFrame *frame, int *got_frame,
  223. AVPacket *avpkt)
  224. {
  225. QSVFrame *out_frame;
  226. mfxFrameSurface1 *insurf;
  227. mfxFrameSurface1 *outsurf;
  228. mfxSyncPoint *sync;
  229. mfxBitstream bs = { { { 0 } } };
  230. int ret;
  231. if (avpkt->size) {
  232. bs.Data = avpkt->data;
  233. bs.DataLength = avpkt->size;
  234. bs.MaxLength = bs.DataLength;
  235. bs.TimeStamp = avpkt->pts;
  236. }
  237. sync = av_mallocz(sizeof(*sync));
  238. if (!sync) {
  239. av_freep(&sync);
  240. return AVERROR(ENOMEM);
  241. }
  242. do {
  243. ret = get_surface(avctx, q, &insurf);
  244. if (ret < 0)
  245. return ret;
  246. ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
  247. insurf, &outsurf, sync);
  248. if (ret == MFX_WRN_DEVICE_BUSY)
  249. av_usleep(1);
  250. } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
  251. if (ret != MFX_ERR_NONE &&
  252. ret != MFX_ERR_MORE_DATA &&
  253. ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
  254. ret != MFX_ERR_MORE_SURFACE) {
  255. av_freep(&sync);
  256. return ff_qsv_print_error(avctx, ret,
  257. "Error during QSV decoding.");
  258. }
  259. /* make sure we do not enter an infinite loop if the SDK
  260. * did not consume any data and did not return anything */
  261. if (!*sync && !bs.DataOffset) {
  262. ff_qsv_print_warning(avctx, ret, "A decode call did not consume any data");
  263. bs.DataOffset = avpkt->size;
  264. }
  265. if (*sync) {
  266. QSVFrame *out_frame = find_frame(q, outsurf);
  267. if (!out_frame) {
  268. av_log(avctx, AV_LOG_ERROR,
  269. "The returned surface does not correspond to any frame\n");
  270. av_freep(&sync);
  271. return AVERROR_BUG;
  272. }
  273. out_frame->queued = 1;
  274. av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  275. av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
  276. } else {
  277. av_freep(&sync);
  278. }
  279. if (!av_fifo_space(q->async_fifo) ||
  280. (!avpkt->size && av_fifo_size(q->async_fifo))) {
  281. AVFrame *src_frame;
  282. av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  283. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  284. out_frame->queued = 0;
  285. do {
  286. ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
  287. } while (ret == MFX_WRN_IN_EXECUTION);
  288. av_freep(&sync);
  289. src_frame = out_frame->frame;
  290. ret = av_frame_ref(frame, src_frame);
  291. if (ret < 0)
  292. return ret;
  293. outsurf = &out_frame->surface;
  294. #if FF_API_PKT_PTS
  295. FF_DISABLE_DEPRECATION_WARNINGS
  296. frame->pkt_pts = outsurf->Data.TimeStamp;
  297. FF_ENABLE_DEPRECATION_WARNINGS
  298. #endif
  299. frame->pts = outsurf->Data.TimeStamp;
  300. frame->repeat_pict =
  301. outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
  302. outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
  303. outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
  304. frame->top_field_first =
  305. outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
  306. frame->interlaced_frame =
  307. !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
  308. /* update the surface properties */
  309. if (avctx->pix_fmt == AV_PIX_FMT_QSV)
  310. ((mfxFrameSurface1*)frame->data[3])->Info = outsurf->Info;
  311. *got_frame = 1;
  312. }
  313. return bs.DataOffset;
  314. }
  315. int ff_qsv_decode_close(QSVContext *q)
  316. {
  317. QSVFrame *cur = q->work_frames;
  318. if (q->session)
  319. MFXVideoDECODE_Close(q->session);
  320. while (q->async_fifo && av_fifo_size(q->async_fifo)) {
  321. QSVFrame *out_frame;
  322. mfxSyncPoint *sync;
  323. av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  324. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  325. av_freep(&sync);
  326. }
  327. while (cur) {
  328. q->work_frames = cur->next;
  329. av_frame_free(&cur->frame);
  330. av_freep(&cur);
  331. cur = q->work_frames;
  332. }
  333. av_fifo_free(q->async_fifo);
  334. q->async_fifo = NULL;
  335. av_parser_close(q->parser);
  336. avcodec_free_context(&q->avctx_internal);
  337. if (q->internal_session)
  338. MFXClose(q->internal_session);
  339. av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
  340. av_buffer_unref(&q->frames_ctx.mids_buf);
  341. return 0;
  342. }
  343. int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
  344. AVFrame *frame, int *got_frame, AVPacket *pkt)
  345. {
  346. uint8_t *dummy_data;
  347. int dummy_size;
  348. int ret;
  349. if (!q->avctx_internal) {
  350. q->avctx_internal = avcodec_alloc_context3(NULL);
  351. if (!q->avctx_internal)
  352. return AVERROR(ENOMEM);
  353. if (avctx->extradata) {
  354. q->avctx_internal->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  355. if (!q->avctx_internal->extradata)
  356. return AVERROR(ENOMEM);
  357. memcpy(q->avctx_internal->extradata, avctx->extradata,
  358. avctx->extradata_size);
  359. q->avctx_internal->extradata_size = avctx->extradata_size;
  360. }
  361. q->parser = av_parser_init(avctx->codec_id);
  362. if (!q->parser)
  363. return AVERROR(ENOMEM);
  364. q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
  365. q->orig_pix_fmt = AV_PIX_FMT_NONE;
  366. }
  367. if (!pkt->size)
  368. return qsv_decode(avctx, q, frame, got_frame, pkt);
  369. /* we assume the packets are already split properly and want
  370. * just the codec parameters here */
  371. av_parser_parse2(q->parser, q->avctx_internal,
  372. &dummy_data, &dummy_size,
  373. pkt->data, pkt->size, pkt->pts, pkt->dts,
  374. pkt->pos);
  375. /* TODO: flush delayed frames on reinit */
  376. if (q->parser->format != q->orig_pix_fmt ||
  377. q->parser->coded_width != avctx->coded_width ||
  378. q->parser->coded_height != avctx->coded_height) {
  379. enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
  380. AV_PIX_FMT_NONE,
  381. AV_PIX_FMT_NONE };
  382. enum AVPixelFormat qsv_format;
  383. qsv_format = ff_qsv_map_pixfmt(q->parser->format, &q->fourcc);
  384. if (qsv_format < 0) {
  385. av_log(avctx, AV_LOG_ERROR,
  386. "Decoding pixel format '%s' is not supported\n",
  387. av_get_pix_fmt_name(q->parser->format));
  388. ret = AVERROR(ENOSYS);
  389. goto reinit_fail;
  390. }
  391. q->orig_pix_fmt = q->parser->format;
  392. avctx->pix_fmt = pix_fmts[1] = qsv_format;
  393. avctx->width = q->parser->width;
  394. avctx->height = q->parser->height;
  395. avctx->coded_width = q->parser->coded_width;
  396. avctx->coded_height = q->parser->coded_height;
  397. avctx->level = q->avctx_internal->level;
  398. avctx->profile = q->avctx_internal->profile;
  399. ret = ff_get_format(avctx, pix_fmts);
  400. if (ret < 0)
  401. goto reinit_fail;
  402. avctx->pix_fmt = ret;
  403. ret = qsv_decode_init(avctx, q);
  404. if (ret < 0)
  405. goto reinit_fail;
  406. }
  407. return qsv_decode(avctx, q, frame, got_frame, pkt);
  408. reinit_fail:
  409. q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
  410. return ret;
  411. }
  412. void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
  413. {
  414. q->orig_pix_fmt = AV_PIX_FMT_NONE;
  415. }