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.

569 lines
17KB

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