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.

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