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.

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