#include <mfx/mfxvideo.h>
#include "libavutil/common.h"
+#include "libavutil/hwcontext.h"
+#include "libavutil/hwcontext_qsv.h"
#include "libavutil/mem.h"
#include "libavutil/log.h"
#include "libavutil/pixfmt.h"
}
}
-static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
+static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
+ AVBufferRef *hw_frames_ref)
{
- if (!session) {
+ int ret;
+
+ if (session) {
+ q->session = session;
+ } else if (hw_frames_ref) {
+ if (q->internal_session) {
+ MFXClose(q->internal_session);
+ q->internal_session = NULL;
+ }
+ av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
+
+ q->frames_ctx.hw_frames_ctx = av_buffer_ref(hw_frames_ref);
+ if (!q->frames_ctx.hw_frames_ctx)
+ return AVERROR(ENOMEM);
+
+ ret = ff_qsv_init_session_hwcontext(avctx, &q->internal_session,
+ &q->frames_ctx, q->load_plugins,
+ q->iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY);
+ if (ret < 0) {
+ av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
+ return ret;
+ }
+
+ q->session = q->internal_session;
+ } else {
if (!q->internal_session) {
- int ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
- q->load_plugins);
+ ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
+ q->load_plugins);
if (ret < 0)
return ret;
}
q->session = q->internal_session;
- } else {
- q->session = session;
}
/* make sure the decoder is uninitialized */
return 0;
}
-static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
+static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q)
{
+ mfxSession session = NULL;
+ int iopattern = 0;
mfxVideoParam param = { { 0 } };
int ret;
if (!q->async_fifo) {
q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
- (sizeof(mfxSyncPoint) + sizeof(QSVFrame*)));
+ (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));
if (!q->async_fifo)
return AVERROR(ENOMEM);
}
- ret = qsv_init_session(avctx, q, session);
+ if (avctx->hwaccel_context) {
+ AVQSVContext *user_ctx = avctx->hwaccel_context;
+ session = user_ctx->session;
+ iopattern = user_ctx->iopattern;
+ q->ext_buffers = user_ctx->ext_buffers;
+ q->nb_ext_buffers = user_ctx->nb_ext_buffers;
+ }
+
+ if (avctx->hw_frames_ctx) {
+ AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
+ AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
+
+ if (!iopattern) {
+ if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
+ iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
+ else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
+ iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
+ }
+ }
+
+ if (!iopattern)
+ iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
+ q->iopattern = iopattern;
+
+ ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
return ret;
}
-
ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
if (ret < 0)
return ret;
QSVFrame *out_frame;
mfxFrameSurface1 *insurf;
mfxFrameSurface1 *outsurf;
- mfxSyncPoint sync;
+ mfxSyncPoint *sync;
mfxBitstream bs = { { { 0 } } };
int ret;
bs.TimeStamp = avpkt->pts;
}
+ sync = av_mallocz(sizeof(*sync));
+ if (!sync) {
+ av_freep(&sync);
+ return AVERROR(ENOMEM);
+ }
+
do {
ret = get_surface(avctx, q, &insurf);
if (ret < 0)
return ret;
ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
- insurf, &outsurf, &sync);
+ insurf, &outsurf, sync);
if (ret == MFX_WRN_DEVICE_BUSY)
av_usleep(1);
ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
ret != MFX_ERR_MORE_SURFACE) {
av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
+ av_freep(&sync);
return ff_qsv_error(ret);
}
/* make sure we do not enter an infinite loop if the SDK
* did not consume any data and did not return anything */
- if (!sync && !bs.DataOffset) {
+ if (!*sync && !bs.DataOffset) {
av_log(avctx, AV_LOG_WARNING, "A decode call did not consume any data\n");
bs.DataOffset = avpkt->size;
}
- if (sync) {
+ if (*sync) {
QSVFrame *out_frame = find_frame(q, outsurf);
if (!out_frame) {
av_log(avctx, AV_LOG_ERROR,
"The returned surface does not correspond to any frame\n");
+ av_freep(&sync);
return AVERROR_BUG;
}
out_frame->queued = 1;
av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
+ } else {
+ av_freep(&sync);
}
if (!av_fifo_space(q->async_fifo) ||
av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
out_frame->queued = 0;
- MFXVideoCORE_SyncOperation(q->session, sync, 60000);
+ do {
+ ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
+ } while (ret == MFX_WRN_IN_EXECUTION);
+
+ av_freep(&sync);
src_frame = out_frame->frame;
outsurf = out_frame->surface;
- frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
+#if FF_API_PKT_PTS
+FF_DISABLE_DEPRECATION_WARNINGS
+ frame->pkt_pts = outsurf->Data.TimeStamp;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+ frame->pts = outsurf->Data.TimeStamp;
frame->repeat_pict =
outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
if (q->session)
MFXVideoDECODE_Close(q->session);
+ while (q->async_fifo && av_fifo_size(q->async_fifo)) {
+ QSVFrame *out_frame;
+ mfxSyncPoint *sync;
+
+ av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
+ av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
+
+ av_freep(&sync);
+ }
+
while (cur) {
q->work_frames = cur->next;
av_frame_free(&cur->frame);
if (q->internal_session)
MFXClose(q->internal_session);
+ av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
+ av_freep(&q->frames_ctx.mids);
+ q->frames_ctx.nb_mids = 0;
+
return 0;
}
if (q->parser->format != q->orig_pix_fmt ||
q->parser->coded_width != avctx->coded_width ||
q->parser->coded_height != avctx->coded_height) {
- mfxSession session = NULL;
-
enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
AV_PIX_FMT_NONE,
AV_PIX_FMT_NONE };
avctx->pix_fmt = ret;
- if (avctx->hwaccel_context) {
- AVQSVContext *user_ctx = avctx->hwaccel_context;
- session = user_ctx->session;
- q->iopattern = user_ctx->iopattern;
- q->ext_buffers = user_ctx->ext_buffers;
- q->nb_ext_buffers = user_ctx->nb_ext_buffers;
- }
-
- ret = qsv_decode_init(avctx, q, session);
+ ret = qsv_decode_init(avctx, q);
if (ret < 0)
goto reinit_fail;
}