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.

491 lines
14KB

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