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.

888 lines
28KB

  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 FFmpeg.
  8. *
  9. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "libavutil/pixfmt.h"
  34. #include "libavutil/time.h"
  35. #include "libavutil/imgutils.h"
  36. #include "avcodec.h"
  37. #include "internal.h"
  38. #include "decode.h"
  39. #include "qsv.h"
  40. #include "qsv_internal.h"
  41. #include "qsvdec.h"
  42. const AVCodecHWConfigInternal *const ff_qsv_hw_configs[] = {
  43. &(const AVCodecHWConfigInternal) {
  44. .public = {
  45. .pix_fmt = AV_PIX_FMT_QSV,
  46. .methods = AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX |
  47. AV_CODEC_HW_CONFIG_METHOD_AD_HOC,
  48. .device_type = AV_HWDEVICE_TYPE_QSV,
  49. },
  50. .hwaccel = NULL,
  51. },
  52. NULL
  53. };
  54. static int ff_qsv_get_continuous_buffer(AVCodecContext *avctx, AVFrame *frame, AVBufferPool *pool)
  55. {
  56. int ret = 0;
  57. ff_decode_frame_props(avctx, frame);
  58. frame->width = avctx->width;
  59. frame->height = avctx->height;
  60. switch (avctx->pix_fmt) {
  61. case AV_PIX_FMT_NV12:
  62. frame->linesize[0] = FFALIGN(avctx->width, 128);
  63. break;
  64. case AV_PIX_FMT_P010:
  65. frame->linesize[0] = 2 * FFALIGN(avctx->width, 128);
  66. break;
  67. default:
  68. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
  69. return AVERROR(EINVAL);
  70. }
  71. frame->linesize[1] = frame->linesize[0];
  72. frame->buf[0] = av_buffer_pool_get(pool);
  73. if (!frame->buf[0])
  74. return AVERROR(ENOMEM);
  75. frame->data[0] = frame->buf[0]->data;
  76. frame->data[1] = frame->data[0] +
  77. frame->linesize[0] * FFALIGN(avctx->height, 64);
  78. ret = ff_attach_decode_data(frame);
  79. if (ret < 0)
  80. return ret;
  81. return 0;
  82. }
  83. static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
  84. AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref)
  85. {
  86. int ret;
  87. if (q->gpu_copy == MFX_GPUCOPY_ON &&
  88. !(q->iopattern & MFX_IOPATTERN_OUT_SYSTEM_MEMORY)) {
  89. av_log(avctx, AV_LOG_WARNING, "GPU-accelerated memory copy "
  90. "only works in system memory mode.\n");
  91. q->gpu_copy = MFX_GPUCOPY_OFF;
  92. }
  93. if (session) {
  94. q->session = session;
  95. } else if (hw_frames_ref) {
  96. if (q->internal_qs.session) {
  97. MFXClose(q->internal_qs.session);
  98. q->internal_qs.session = NULL;
  99. }
  100. av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
  101. q->frames_ctx.hw_frames_ctx = av_buffer_ref(hw_frames_ref);
  102. if (!q->frames_ctx.hw_frames_ctx)
  103. return AVERROR(ENOMEM);
  104. ret = ff_qsv_init_session_frames(avctx, &q->internal_qs.session,
  105. &q->frames_ctx, q->load_plugins,
  106. q->iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY,
  107. q->gpu_copy);
  108. if (ret < 0) {
  109. av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
  110. return ret;
  111. }
  112. q->session = q->internal_qs.session;
  113. } else if (hw_device_ref) {
  114. if (q->internal_qs.session) {
  115. MFXClose(q->internal_qs.session);
  116. q->internal_qs.session = NULL;
  117. }
  118. ret = ff_qsv_init_session_device(avctx, &q->internal_qs.session,
  119. hw_device_ref, q->load_plugins, q->gpu_copy);
  120. if (ret < 0)
  121. return ret;
  122. q->session = q->internal_qs.session;
  123. } else {
  124. if (!q->internal_qs.session) {
  125. ret = ff_qsv_init_internal_session(avctx, &q->internal_qs,
  126. q->load_plugins, q->gpu_copy);
  127. if (ret < 0)
  128. return ret;
  129. }
  130. q->session = q->internal_qs.session;
  131. }
  132. /* make sure the decoder is uninitialized */
  133. MFXVideoDECODE_Close(q->session);
  134. return 0;
  135. }
  136. static inline unsigned int qsv_fifo_item_size(void)
  137. {
  138. return sizeof(mfxSyncPoint*) + sizeof(QSVFrame*);
  139. }
  140. static inline unsigned int qsv_fifo_size(const AVFifoBuffer* fifo)
  141. {
  142. return av_fifo_size(fifo) / qsv_fifo_item_size();
  143. }
  144. static int qsv_decode_preinit(AVCodecContext *avctx, QSVContext *q, enum AVPixelFormat pix_fmt, mfxVideoParam *param)
  145. {
  146. mfxSession session = NULL;
  147. int iopattern = 0;
  148. int ret;
  149. enum AVPixelFormat pix_fmts[3] = {
  150. AV_PIX_FMT_QSV, /* opaque format in case of video memory output */
  151. pix_fmt, /* system memory format obtained from bitstream parser */
  152. AV_PIX_FMT_NONE };
  153. ret = ff_get_format(avctx, pix_fmts);
  154. if (ret < 0) {
  155. q->orig_pix_fmt = avctx->pix_fmt = AV_PIX_FMT_NONE;
  156. return ret;
  157. }
  158. if (!q->async_fifo) {
  159. q->async_fifo = av_fifo_alloc(q->async_depth * qsv_fifo_item_size());
  160. if (!q->async_fifo)
  161. return AVERROR(ENOMEM);
  162. }
  163. if (avctx->pix_fmt == AV_PIX_FMT_QSV && avctx->hwaccel_context) {
  164. AVQSVContext *user_ctx = avctx->hwaccel_context;
  165. session = user_ctx->session;
  166. iopattern = user_ctx->iopattern;
  167. q->ext_buffers = user_ctx->ext_buffers;
  168. q->nb_ext_buffers = user_ctx->nb_ext_buffers;
  169. }
  170. if (avctx->hw_frames_ctx) {
  171. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  172. AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
  173. if (!iopattern) {
  174. if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
  175. iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
  176. else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
  177. iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
  178. }
  179. }
  180. if (!iopattern)
  181. iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
  182. q->iopattern = iopattern;
  183. ff_qsv_print_iopattern(avctx, q->iopattern, "Decoder");
  184. ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx, avctx->hw_device_ctx);
  185. if (ret < 0) {
  186. av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
  187. return ret;
  188. }
  189. param->IOPattern = q->iopattern;
  190. param->AsyncDepth = q->async_depth;
  191. param->ExtParam = q->ext_buffers;
  192. param->NumExtParam = q->nb_ext_buffers;
  193. return 0;
  194. }
  195. static int qsv_decode_init_context(AVCodecContext *avctx, QSVContext *q, mfxVideoParam *param)
  196. {
  197. int ret;
  198. avctx->width = param->mfx.FrameInfo.CropW;
  199. avctx->height = param->mfx.FrameInfo.CropH;
  200. avctx->coded_width = param->mfx.FrameInfo.Width;
  201. avctx->coded_height = param->mfx.FrameInfo.Height;
  202. avctx->level = param->mfx.CodecLevel;
  203. avctx->profile = param->mfx.CodecProfile;
  204. avctx->field_order = ff_qsv_map_picstruct(param->mfx.FrameInfo.PicStruct);
  205. avctx->pix_fmt = ff_qsv_map_fourcc(param->mfx.FrameInfo.FourCC);
  206. ret = MFXVideoDECODE_Init(q->session, param);
  207. if (ret < 0)
  208. return ff_qsv_print_error(avctx, ret,
  209. "Error initializing the MFX video decoder");
  210. q->frame_info = param->mfx.FrameInfo;
  211. if (!avctx->hw_frames_ctx)
  212. q->pool = av_buffer_pool_init(av_image_get_buffer_size(avctx->pix_fmt,
  213. FFALIGN(avctx->width, 128), FFALIGN(avctx->height, 64), 1), av_buffer_allocz);
  214. return 0;
  215. }
  216. static int qsv_decode_header(AVCodecContext *avctx, QSVContext *q, AVPacket *avpkt, enum AVPixelFormat pix_fmt, mfxVideoParam *param)
  217. {
  218. int ret;
  219. mfxBitstream bs = { 0 };
  220. if (avpkt->size) {
  221. bs.Data = avpkt->data;
  222. bs.DataLength = avpkt->size;
  223. bs.MaxLength = bs.DataLength;
  224. bs.TimeStamp = avpkt->pts;
  225. if (avctx->field_order == AV_FIELD_PROGRESSIVE)
  226. bs.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME;
  227. } else
  228. return AVERROR_INVALIDDATA;
  229. if(!q->session) {
  230. ret = qsv_decode_preinit(avctx, q, pix_fmt, param);
  231. if (ret < 0)
  232. return ret;
  233. }
  234. ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
  235. if (ret < 0)
  236. return ret;
  237. param->mfx.CodecId = ret;
  238. ret = MFXVideoDECODE_DecodeHeader(q->session, &bs, param);
  239. if (MFX_ERR_MORE_DATA == ret) {
  240. return AVERROR(EAGAIN);
  241. }
  242. if (ret < 0)
  243. return ff_qsv_print_error(avctx, ret,
  244. "Error decoding stream header");
  245. return 0;
  246. }
  247. static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
  248. {
  249. int ret;
  250. if (q->pool)
  251. ret = ff_qsv_get_continuous_buffer(avctx, frame->frame, q->pool);
  252. else
  253. ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
  254. if (ret < 0)
  255. return ret;
  256. if (frame->frame->format == AV_PIX_FMT_QSV) {
  257. frame->surface = *(mfxFrameSurface1*)frame->frame->data[3];
  258. } else {
  259. frame->surface.Info = q->frame_info;
  260. frame->surface.Data.PitchLow = frame->frame->linesize[0];
  261. frame->surface.Data.Y = frame->frame->data[0];
  262. frame->surface.Data.UV = frame->frame->data[1];
  263. }
  264. if (q->frames_ctx.mids) {
  265. ret = ff_qsv_find_surface_idx(&q->frames_ctx, frame);
  266. if (ret < 0)
  267. return ret;
  268. frame->surface.Data.MemId = &q->frames_ctx.mids[ret];
  269. }
  270. frame->surface.Data.ExtParam = &frame->ext_param;
  271. frame->surface.Data.NumExtParam = 1;
  272. frame->ext_param = (mfxExtBuffer*)&frame->dec_info;
  273. frame->dec_info.Header.BufferId = MFX_EXTBUFF_DECODED_FRAME_INFO;
  274. frame->dec_info.Header.BufferSz = sizeof(frame->dec_info);
  275. frame->used = 1;
  276. return 0;
  277. }
  278. static void qsv_clear_unused_frames(QSVContext *q)
  279. {
  280. QSVFrame *cur = q->work_frames;
  281. while (cur) {
  282. if (cur->used && !cur->surface.Data.Locked && !cur->queued) {
  283. cur->used = 0;
  284. av_frame_unref(cur->frame);
  285. }
  286. cur = cur->next;
  287. }
  288. }
  289. static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
  290. {
  291. QSVFrame *frame, **last;
  292. int ret;
  293. qsv_clear_unused_frames(q);
  294. frame = q->work_frames;
  295. last = &q->work_frames;
  296. while (frame) {
  297. if (!frame->used) {
  298. ret = alloc_frame(avctx, q, frame);
  299. if (ret < 0)
  300. return ret;
  301. *surf = &frame->surface;
  302. return 0;
  303. }
  304. last = &frame->next;
  305. frame = frame->next;
  306. }
  307. frame = av_mallocz(sizeof(*frame));
  308. if (!frame)
  309. return AVERROR(ENOMEM);
  310. frame->frame = av_frame_alloc();
  311. if (!frame->frame) {
  312. av_freep(&frame);
  313. return AVERROR(ENOMEM);
  314. }
  315. *last = frame;
  316. ret = alloc_frame(avctx, q, frame);
  317. if (ret < 0)
  318. return ret;
  319. *surf = &frame->surface;
  320. return 0;
  321. }
  322. static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
  323. {
  324. QSVFrame *cur = q->work_frames;
  325. while (cur) {
  326. if (surf == &cur->surface)
  327. return cur;
  328. cur = cur->next;
  329. }
  330. return NULL;
  331. }
  332. static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
  333. AVFrame *frame, int *got_frame,
  334. AVPacket *avpkt)
  335. {
  336. QSVFrame *out_frame;
  337. mfxFrameSurface1 *insurf;
  338. mfxFrameSurface1 *outsurf;
  339. mfxSyncPoint *sync;
  340. mfxBitstream bs = { { { 0 } } };
  341. int ret;
  342. if (avpkt->size) {
  343. bs.Data = avpkt->data;
  344. bs.DataLength = avpkt->size;
  345. bs.MaxLength = bs.DataLength;
  346. bs.TimeStamp = avpkt->pts;
  347. if (avctx->field_order == AV_FIELD_PROGRESSIVE)
  348. bs.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME;
  349. }
  350. sync = av_mallocz(sizeof(*sync));
  351. if (!sync) {
  352. av_freep(&sync);
  353. return AVERROR(ENOMEM);
  354. }
  355. do {
  356. ret = get_surface(avctx, q, &insurf);
  357. if (ret < 0) {
  358. av_freep(&sync);
  359. return ret;
  360. }
  361. ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
  362. insurf, &outsurf, sync);
  363. if (ret == MFX_WRN_DEVICE_BUSY)
  364. av_usleep(500);
  365. } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
  366. if (ret != MFX_ERR_NONE &&
  367. ret != MFX_ERR_MORE_DATA &&
  368. ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
  369. ret != MFX_ERR_MORE_SURFACE) {
  370. av_freep(&sync);
  371. return ff_qsv_print_error(avctx, ret,
  372. "Error during QSV decoding.");
  373. }
  374. /* make sure we do not enter an infinite loop if the SDK
  375. * did not consume any data and did not return anything */
  376. if (!*sync && !bs.DataOffset) {
  377. bs.DataOffset = avpkt->size;
  378. ++q->zero_consume_run;
  379. if (q->zero_consume_run > 1)
  380. ff_qsv_print_warning(avctx, ret, "A decode call did not consume any data");
  381. } else if (!*sync && bs.DataOffset) {
  382. ++q->buffered_count;
  383. } else {
  384. q->zero_consume_run = 0;
  385. }
  386. if (*sync) {
  387. QSVFrame *out_frame = find_frame(q, outsurf);
  388. if (!out_frame) {
  389. av_log(avctx, AV_LOG_ERROR,
  390. "The returned surface does not correspond to any frame\n");
  391. av_freep(&sync);
  392. return AVERROR_BUG;
  393. }
  394. out_frame->queued = 1;
  395. av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  396. av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
  397. } else {
  398. av_freep(&sync);
  399. }
  400. if ((qsv_fifo_size(q->async_fifo) >= q->async_depth) ||
  401. (!avpkt->size && av_fifo_size(q->async_fifo))) {
  402. AVFrame *src_frame;
  403. av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  404. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  405. out_frame->queued = 0;
  406. if (avctx->pix_fmt != AV_PIX_FMT_QSV) {
  407. do {
  408. ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
  409. } while (ret == MFX_WRN_IN_EXECUTION);
  410. }
  411. av_freep(&sync);
  412. src_frame = out_frame->frame;
  413. ret = av_frame_ref(frame, src_frame);
  414. if (ret < 0)
  415. return ret;
  416. outsurf = &out_frame->surface;
  417. #if FF_API_PKT_PTS
  418. FF_DISABLE_DEPRECATION_WARNINGS
  419. frame->pkt_pts = outsurf->Data.TimeStamp;
  420. FF_ENABLE_DEPRECATION_WARNINGS
  421. #endif
  422. frame->pts = outsurf->Data.TimeStamp;
  423. frame->repeat_pict =
  424. outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
  425. outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
  426. outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
  427. frame->top_field_first =
  428. outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
  429. frame->interlaced_frame =
  430. !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
  431. frame->pict_type = ff_qsv_map_pictype(out_frame->dec_info.FrameType);
  432. //Key frame is IDR frame is only suitable for H264. For HEVC, IRAPs are key frames.
  433. if (avctx->codec_id == AV_CODEC_ID_H264)
  434. frame->key_frame = !!(out_frame->dec_info.FrameType & MFX_FRAMETYPE_IDR);
  435. /* update the surface properties */
  436. if (avctx->pix_fmt == AV_PIX_FMT_QSV)
  437. ((mfxFrameSurface1*)frame->data[3])->Info = outsurf->Info;
  438. *got_frame = 1;
  439. }
  440. return bs.DataOffset;
  441. }
  442. int ff_qsv_decode_close(QSVContext *q)
  443. {
  444. QSVFrame *cur = q->work_frames;
  445. if (q->session)
  446. MFXVideoDECODE_Close(q->session);
  447. while (q->async_fifo && av_fifo_size(q->async_fifo)) {
  448. QSVFrame *out_frame;
  449. mfxSyncPoint *sync;
  450. av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  451. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  452. av_freep(&sync);
  453. }
  454. while (cur) {
  455. q->work_frames = cur->next;
  456. av_frame_free(&cur->frame);
  457. av_freep(&cur);
  458. cur = q->work_frames;
  459. }
  460. av_fifo_free(q->async_fifo);
  461. q->async_fifo = NULL;
  462. ff_qsv_close_internal_session(&q->internal_qs);
  463. av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
  464. av_buffer_unref(&q->frames_ctx.mids_buf);
  465. av_buffer_pool_uninit(&q->pool);
  466. return 0;
  467. }
  468. int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
  469. AVFrame *frame, int *got_frame, AVPacket *pkt)
  470. {
  471. int ret;
  472. mfxVideoParam param = { 0 };
  473. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NV12;
  474. if (!pkt->size)
  475. return qsv_decode(avctx, q, frame, got_frame, pkt);
  476. /* TODO: flush delayed frames on reinit */
  477. // sw_pix_fmt, coded_width/height should be set for ff_get_format(),
  478. // assume sw_pix_fmt is NV12 and coded_width/height to be 1280x720,
  479. // the assumption may be not corret but will be updated after header decoded if not true.
  480. if (q->orig_pix_fmt != AV_PIX_FMT_NONE)
  481. pix_fmt = q->orig_pix_fmt;
  482. if (!avctx->coded_width)
  483. avctx->coded_width = 1280;
  484. if (!avctx->coded_height)
  485. avctx->coded_height = 720;
  486. ret = qsv_decode_header(avctx, q, pkt, pix_fmt, &param);
  487. if (ret >= 0 && (q->orig_pix_fmt != ff_qsv_map_fourcc(param.mfx.FrameInfo.FourCC) ||
  488. avctx->coded_width != param.mfx.FrameInfo.Width ||
  489. avctx->coded_height != param.mfx.FrameInfo.Height)) {
  490. AVPacket zero_pkt = {0};
  491. if (q->buffered_count) {
  492. q->reinit_flag = 1;
  493. /* decode zero-size pkt to flush the buffered pkt before reinit */
  494. q->buffered_count--;
  495. return qsv_decode(avctx, q, frame, got_frame, &zero_pkt);
  496. }
  497. q->reinit_flag = 0;
  498. q->orig_pix_fmt = avctx->pix_fmt = pix_fmt = ff_qsv_map_fourcc(param.mfx.FrameInfo.FourCC);
  499. avctx->coded_width = param.mfx.FrameInfo.Width;
  500. avctx->coded_height = param.mfx.FrameInfo.Height;
  501. ret = qsv_decode_preinit(avctx, q, pix_fmt, &param);
  502. if (ret < 0)
  503. goto reinit_fail;
  504. q->initialized = 0;
  505. }
  506. if (!q->initialized) {
  507. ret = qsv_decode_init_context(avctx, q, &param);
  508. if (ret < 0)
  509. goto reinit_fail;
  510. q->initialized = 1;
  511. }
  512. return qsv_decode(avctx, q, frame, got_frame, pkt);
  513. reinit_fail:
  514. q->orig_pix_fmt = avctx->pix_fmt = AV_PIX_FMT_NONE;
  515. return ret;
  516. }
  517. void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
  518. {
  519. q->orig_pix_fmt = AV_PIX_FMT_NONE;
  520. q->initialized = 0;
  521. }
  522. enum LoadPlugin {
  523. LOAD_PLUGIN_NONE,
  524. LOAD_PLUGIN_HEVC_SW,
  525. LOAD_PLUGIN_HEVC_HW,
  526. };
  527. typedef struct QSVDecContext {
  528. AVClass *class;
  529. QSVContext qsv;
  530. int load_plugin;
  531. AVFifoBuffer *packet_fifo;
  532. AVPacket buffer_pkt;
  533. } QSVDecContext;
  534. static void qsv_clear_buffers(QSVDecContext *s)
  535. {
  536. AVPacket pkt;
  537. while (av_fifo_size(s->packet_fifo) >= sizeof(pkt)) {
  538. av_fifo_generic_read(s->packet_fifo, &pkt, sizeof(pkt), NULL);
  539. av_packet_unref(&pkt);
  540. }
  541. av_packet_unref(&s->buffer_pkt);
  542. }
  543. static av_cold int qsv_decode_close(AVCodecContext *avctx)
  544. {
  545. QSVDecContext *s = avctx->priv_data;
  546. av_freep(&s->qsv.load_plugins);
  547. ff_qsv_decode_close(&s->qsv);
  548. qsv_clear_buffers(s);
  549. av_fifo_free(s->packet_fifo);
  550. return 0;
  551. }
  552. static av_cold int qsv_decode_init(AVCodecContext *avctx)
  553. {
  554. QSVDecContext *s = avctx->priv_data;
  555. int ret;
  556. if (avctx->codec_id == AV_CODEC_ID_VP8) {
  557. static const char *uid_vp8dec_hw = "f622394d8d87452f878c51f2fc9b4131";
  558. av_freep(&s->qsv.load_plugins);
  559. s->qsv.load_plugins = av_strdup(uid_vp8dec_hw);
  560. if (!s->qsv.load_plugins)
  561. return AVERROR(ENOMEM);
  562. } else if (avctx->codec_id == AV_CODEC_ID_VP9) {
  563. static const char *uid_vp9dec_hw = "a922394d8d87452f878c51f2fc9b4131";
  564. av_freep(&s->qsv.load_plugins);
  565. s->qsv.load_plugins = av_strdup(uid_vp9dec_hw);
  566. if (!s->qsv.load_plugins)
  567. return AVERROR(ENOMEM);
  568. }
  569. else if (avctx->codec_id == AV_CODEC_ID_HEVC && s->load_plugin != LOAD_PLUGIN_NONE) {
  570. static const char * const uid_hevcdec_sw = "15dd936825ad475ea34e35f3f54217a6";
  571. static const char * const uid_hevcdec_hw = "33a61c0b4c27454ca8d85dde757c6f8e";
  572. if (s->qsv.load_plugins[0]) {
  573. av_log(avctx, AV_LOG_WARNING,
  574. "load_plugins is not empty, but load_plugin is not set to 'none'."
  575. "The load_plugin value will be ignored.\n");
  576. } else {
  577. av_freep(&s->qsv.load_plugins);
  578. if (s->load_plugin == LOAD_PLUGIN_HEVC_SW)
  579. s->qsv.load_plugins = av_strdup(uid_hevcdec_sw);
  580. else
  581. s->qsv.load_plugins = av_strdup(uid_hevcdec_hw);
  582. if (!s->qsv.load_plugins)
  583. return AVERROR(ENOMEM);
  584. }
  585. }
  586. s->qsv.orig_pix_fmt = AV_PIX_FMT_NV12;
  587. s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
  588. if (!s->packet_fifo) {
  589. ret = AVERROR(ENOMEM);
  590. goto fail;
  591. }
  592. return 0;
  593. fail:
  594. qsv_decode_close(avctx);
  595. return ret;
  596. }
  597. static int qsv_decode_frame(AVCodecContext *avctx, void *data,
  598. int *got_frame, AVPacket *avpkt)
  599. {
  600. QSVDecContext *s = avctx->priv_data;
  601. AVFrame *frame = data;
  602. int ret;
  603. /* buffer the input packet */
  604. if (avpkt->size) {
  605. AVPacket input_ref;
  606. if (av_fifo_space(s->packet_fifo) < sizeof(input_ref)) {
  607. ret = av_fifo_realloc2(s->packet_fifo,
  608. av_fifo_size(s->packet_fifo) + sizeof(input_ref));
  609. if (ret < 0)
  610. return ret;
  611. }
  612. ret = av_packet_ref(&input_ref, avpkt);
  613. if (ret < 0)
  614. return ret;
  615. av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
  616. }
  617. /* process buffered data */
  618. while (!*got_frame) {
  619. /* prepare the input data */
  620. if (s->buffer_pkt.size <= 0) {
  621. /* no more data */
  622. if (av_fifo_size(s->packet_fifo) < sizeof(AVPacket))
  623. return avpkt->size ? avpkt->size : ff_qsv_process_data(avctx, &s->qsv, frame, got_frame, avpkt);
  624. /* in progress of reinit, no read from fifo and keep the buffer_pkt */
  625. if (!s->qsv.reinit_flag) {
  626. av_packet_unref(&s->buffer_pkt);
  627. av_fifo_generic_read(s->packet_fifo, &s->buffer_pkt, sizeof(s->buffer_pkt), NULL);
  628. }
  629. }
  630. ret = ff_qsv_process_data(avctx, &s->qsv, frame, got_frame, &s->buffer_pkt);
  631. if (ret < 0){
  632. /* Drop buffer_pkt when failed to decode the packet. Otherwise,
  633. the decoder will keep decoding the failure packet. */
  634. av_packet_unref(&s->buffer_pkt);
  635. return ret;
  636. }
  637. if (s->qsv.reinit_flag)
  638. continue;
  639. s->buffer_pkt.size -= ret;
  640. s->buffer_pkt.data += ret;
  641. }
  642. return avpkt->size;
  643. }
  644. static void qsv_decode_flush(AVCodecContext *avctx)
  645. {
  646. QSVDecContext *s = avctx->priv_data;
  647. qsv_clear_buffers(s);
  648. ff_qsv_decode_flush(avctx, &s->qsv);
  649. }
  650. #define OFFSET(x) offsetof(QSVDecContext, x)
  651. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  652. #define DEFINE_QSV_DECODER_WITH_OPTION(x, X, bsf_name, opt) \
  653. static const AVClass x##_qsv_class = { \
  654. .class_name = #x "_qsv", \
  655. .item_name = av_default_item_name, \
  656. .option = opt, \
  657. .version = LIBAVUTIL_VERSION_INT, \
  658. }; \
  659. AVCodec ff_##x##_qsv_decoder = { \
  660. .name = #x "_qsv", \
  661. .long_name = NULL_IF_CONFIG_SMALL(#X " video (Intel Quick Sync Video acceleration)"), \
  662. .priv_data_size = sizeof(QSVDecContext), \
  663. .type = AVMEDIA_TYPE_VIDEO, \
  664. .id = AV_CODEC_ID_##X, \
  665. .init = qsv_decode_init, \
  666. .decode = qsv_decode_frame, \
  667. .flush = qsv_decode_flush, \
  668. .close = qsv_decode_close, \
  669. .bsfs = bsf_name, \
  670. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HYBRID, \
  671. .priv_class = &x##_qsv_class, \
  672. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12, \
  673. AV_PIX_FMT_P010, \
  674. AV_PIX_FMT_QSV, \
  675. AV_PIX_FMT_NONE }, \
  676. .hw_configs = ff_qsv_hw_configs, \
  677. .wrapper_name = "qsv", \
  678. }; \
  679. #define DEFINE_QSV_DECODER(x, X, bsf_name) DEFINE_QSV_DECODER_WITH_OPTION(x, X, bsf_name, options)
  680. #if CONFIG_HEVC_QSV_DECODER
  681. static const AVOption hevc_options[] = {
  682. { "async_depth", "Internal parallelization depth, the higher the value the higher the latency.", OFFSET(qsv.async_depth), AV_OPT_TYPE_INT, { .i64 = ASYNC_DEPTH_DEFAULT }, 1, INT_MAX, VD },
  683. { "load_plugin", "A user plugin to load in an internal session", OFFSET(load_plugin), AV_OPT_TYPE_INT, { .i64 = LOAD_PLUGIN_HEVC_HW }, LOAD_PLUGIN_NONE, LOAD_PLUGIN_HEVC_HW, VD, "load_plugin" },
  684. { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_NONE }, 0, 0, VD, "load_plugin" },
  685. { "hevc_sw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_SW }, 0, 0, VD, "load_plugin" },
  686. { "hevc_hw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_HW }, 0, 0, VD, "load_plugin" },
  687. { "load_plugins", "A :-separate list of hexadecimal plugin UIDs to load in an internal session",
  688. OFFSET(qsv.load_plugins), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
  689. { "gpu_copy", "A GPU-accelerated copy between video and system memory", OFFSET(qsv.gpu_copy), AV_OPT_TYPE_INT, { .i64 = MFX_GPUCOPY_DEFAULT }, MFX_GPUCOPY_DEFAULT, MFX_GPUCOPY_OFF, VD, "gpu_copy"},
  690. { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_DEFAULT }, 0, 0, VD, "gpu_copy"},
  691. { "on", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_ON }, 0, 0, VD, "gpu_copy"},
  692. { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_OFF }, 0, 0, VD, "gpu_copy"},
  693. { NULL },
  694. };
  695. DEFINE_QSV_DECODER_WITH_OPTION(hevc, HEVC, "hevc_mp4toannexb", hevc_options)
  696. #endif
  697. static const AVOption options[] = {
  698. { "async_depth", "Internal parallelization depth, the higher the value the higher the latency.", OFFSET(qsv.async_depth), AV_OPT_TYPE_INT, { .i64 = ASYNC_DEPTH_DEFAULT }, 1, INT_MAX, VD },
  699. { "gpu_copy", "A GPU-accelerated copy between video and system memory", OFFSET(qsv.gpu_copy), AV_OPT_TYPE_INT, { .i64 = MFX_GPUCOPY_DEFAULT }, MFX_GPUCOPY_DEFAULT, MFX_GPUCOPY_OFF, VD, "gpu_copy"},
  700. { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_DEFAULT }, 0, 0, VD, "gpu_copy"},
  701. { "on", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_ON }, 0, 0, VD, "gpu_copy"},
  702. { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_OFF }, 0, 0, VD, "gpu_copy"},
  703. { NULL },
  704. };
  705. #if CONFIG_H264_QSV_DECODER
  706. DEFINE_QSV_DECODER(h264, H264, "h264_mp4toannexb")
  707. #endif
  708. #if CONFIG_MPEG2_QSV_DECODER
  709. DEFINE_QSV_DECODER(mpeg2, MPEG2VIDEO, NULL)
  710. #endif
  711. #if CONFIG_VC1_QSV_DECODER
  712. DEFINE_QSV_DECODER(vc1, VC1, NULL)
  713. #endif
  714. #if CONFIG_MJPEG_QSV_DECODER
  715. DEFINE_QSV_DECODER(mjpeg, MJPEG, NULL)
  716. #endif
  717. #if CONFIG_VP8_QSV_DECODER
  718. DEFINE_QSV_DECODER(vp8, VP8, NULL)
  719. #endif
  720. #if CONFIG_VP9_QSV_DECODER
  721. DEFINE_QSV_DECODER(vp9, VP9, NULL)
  722. #endif
  723. #if CONFIG_AV1_QSV_DECODER
  724. DEFINE_QSV_DECODER(av1, AV1, NULL)
  725. #endif