[FFmpeg-devel] [PATCH 1/2] libavcodec: Add support for QSV screen capture plugin

Alexander Bilyak bilyak.alexander at gmail.com
Fri Aug 11 12:10:45 EEST 2017


Intel QSV SDK provide screen capture plugin starting from API ver 1.17
as runtime loadable plugin for QSV decoder.

* add API version selection while initialization of QSV context
(default is still 1.1 for usual encoding/decoding)
---
 configure                  |   2 +
 libavcodec/Makefile        |   1 +
 libavcodec/allcodecs.c     |   1 +
 libavcodec/qsv.c           |   6 +-
 libavcodec/qsv_internal.h  |   2 +-
 libavcodec/qsvdec.c        |  12 ++-
 libavcodec/qsvdec.h        |   5 +
 libavcodec/qsvdec_screen.c | 250 +++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/qsvenc.c        |   3 +-
 9 files changed, 272 insertions(+), 10 deletions(-)
 create mode 100644 libavcodec/qsvdec_screen.c

diff --git a/configure b/configure
index 3a146ed857..942765e8a3 100755
--- a/configure
+++ b/configure
@@ -2520,6 +2520,8 @@ rv20_decoder_select="h263_decoder"
 rv20_encoder_select="h263_encoder"
 rv30_decoder_select="golomb h264pred h264qpel mpegvideo rv34dsp"
 rv40_decoder_select="golomb h264pred h264qpel mpegvideo rv34dsp"
+screen_qsv_decoder_deps="libmfx"
+screen_qsv_decoder_select="qsvdec"
 screenpresso_decoder_select="zlib"
 shorten_decoder_select="bswapdsp"
 sipr_decoder_select="lsp"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 59029a853c..7a2052d4c7 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -519,6 +519,7 @@ OBJS-$(CONFIG_S302M_DECODER)           += s302m.o
 OBJS-$(CONFIG_S302M_ENCODER)           += s302menc.o
 OBJS-$(CONFIG_SANM_DECODER)            += sanm.o
 OBJS-$(CONFIG_SCPR_DECODER)            += scpr.o
+OBJS-$(CONFIG_SCREEN_QSV_DECODER)      += qsvdec_screen.o
 OBJS-$(CONFIG_SCREENPRESSO_DECODER)    += screenpresso.o
 OBJS-$(CONFIG_SDX2_DPCM_DECODER)       += dpcm.o
 OBJS-$(CONFIG_SGI_DECODER)             += sgidec.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 0243f47358..d572c3f27c 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -315,6 +315,7 @@ static void register_all(void)
     REGISTER_ENCDEC (S302M,             s302m);
     REGISTER_DECODER(SANM,              sanm);
     REGISTER_DECODER(SCPR,              scpr);
+	REGISTER_DECODER(SCREEN_QSV,        screen_qsv);
     REGISTER_DECODER(SCREENPRESSO,      screenpresso);
     REGISTER_DECODER(SDX2_DPCM,         sdx2_dpcm);
     REGISTER_ENCDEC (SGI,               sgi);
diff --git a/libavcodec/qsv.c b/libavcodec/qsv.c
index b9e2cd990d..80d0d4af37 100644
--- a/libavcodec/qsv.c
+++ b/libavcodec/qsv.c
@@ -242,15 +242,13 @@ load_plugin_fail:
 }
 
 int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
-                                 const char *load_plugins)
+                                 const char *load_plugins, mfxVersion api_ver)
 {
     mfxIMPL impl   = MFX_IMPL_AUTO_ANY;
-    mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
-
     const char *desc;
     int ret;
 
-    ret = MFXInit(impl, &ver, session);
+    ret = MFXInit(impl, &api_ver, session);
     if (ret < 0)
         return ff_qsv_print_error(avctx, ret,
                                   "Error initializing an internal MFX session");
diff --git a/libavcodec/qsv_internal.h b/libavcodec/qsv_internal.h
index c0305508dd..ebc3082e22 100644
--- a/libavcodec/qsv_internal.h
+++ b/libavcodec/qsv_internal.h
@@ -88,7 +88,7 @@ int ff_qsv_profile_to_mfx(enum AVCodecID codec_id, int profile);
 int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc);
 
 int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
-                                 const char *load_plugins);
+                                 const char *load_plugins, mfxVersion api_ver);
 
 int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession,
                                AVBufferRef *device_ref, const char *load_plugins);
diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c
index c00817f1d9..9aa139b1e8 100644
--- a/libavcodec/qsvdec.c
+++ b/libavcodec/qsvdec.c
@@ -41,8 +41,9 @@
 #include "qsv_internal.h"
 #include "qsvdec.h"
 
-static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
-                            AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref)
+int ff_qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
+                        AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref,
+                        mfxVersion api_ver)
 {
     int ret;
 
@@ -83,7 +84,7 @@ static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession ses
     } else {
         if (!q->internal_session) {
             ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
-                                               q->load_plugins);
+                                               q->load_plugins, api_ver);
             if (ret < 0)
                 return ret;
         }
@@ -145,7 +146,10 @@ static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q)
         iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
     q->iopattern = iopattern;
 
-    ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx, avctx->hw_device_ctx);
+    mfxVersion api_ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
+
+    ret = ff_qsv_init_session(avctx, q, session, avctx->hw_frames_ctx,
+                              avctx->hw_device_ctx, api_ver);
     if (ret < 0) {
         av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
         return ret;
diff --git a/libavcodec/qsvdec.h b/libavcodec/qsvdec.h
index 4e86e4b7f8..704d308fb3 100644
--- a/libavcodec/qsvdec.h
+++ b/libavcodec/qsvdec.h
@@ -70,6 +70,11 @@ typedef struct QSVContext {
     int         nb_ext_buffers;
 } QSVContext;
 
+int ff_qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
+                        AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref,
+                        mfxVersion api_ver);
+
+
 int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
                         AVFrame *frame, int *got_frame, AVPacket *pkt);
 
diff --git a/libavcodec/qsvdec_screen.c b/libavcodec/qsvdec_screen.c
new file mode 100644
index 0000000000..aeea851333
--- /dev/null
+++ b/libavcodec/qsvdec_screen.c
@@ -0,0 +1,250 @@
+/*
+ * Intel QSV screen capture decoder
+ *
+ * This file is part of FFmpeg.
+ *
+ * Copyright (C) 2017 Alexander Bilyak <bilyak.alexander at gmail.com>
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Intel QSV Screen capture decoder interface
+ * @author Alexander Bilyak <bilyak.alexander at gmail.com>
+ * @note This "decoder" uses intel QSV decoder plugin for screen capturing.
+ * It does not decode incoming packets; moreover: input packets should be empty.
+ * Make sure executable could find appropriate plugin during runtime.
+ */
+
+#include <stdint.h>
+#include <string.h>
+
+#include <mfx/mfxvideo.h>
+#include <mfx/mfxsc.h>
+
+#include "libavutil/common.h"
+#include "libavutil/fifo.h"
+#include "libavutil/opt.h"
+
+#include "avcodec.h"
+#include "internal.h"
+#include "qsv_internal.h"
+#include "qsvdec.h"
+#include "qsv.h"
+
+/**
+ * QSV screen capture decoder context
+ */
+typedef struct QSVScreenContext {
+    AVClass *class;                 /**< Class for private options */
+    QSVContext qsv;                 /**< QSV context used for initialization */
+
+    mfxExtScreenCaptureParam param; /**< Screen capture parameters */
+
+    int draw_mouse;                 /**< Draw mouse cursor (private option) */
+} QSVScreenContext;
+
+/**
+ * Closes QSV screen decoder (public decoder API).
+ *
+ * @param avctx Context from avcodec core
+ *
+ * @return 0 on success, a negative AVERROR on error
+ */
+static av_cold int qsv_screen_decode_close(AVCodecContext *avctx)
+{
+    QSVScreenContext *s = avctx->priv_data;
+
+    ff_qsv_decode_close(&s->qsv);
+
+    return 0;
+}
+
+/**
+ * Flushes QSV screen decoder (public decoder API).
+ *
+ * @param avctx Context from avcodec core
+ */
+static void qsv_screen_decode_flush(AVCodecContext *avctx)
+{
+    QSVScreenContext *s = avctx->priv_data;
+
+    ff_qsv_decode_flush(avctx, &s->qsv);
+}
+
+/**
+ * Init internal QSV context for screen capturing
+ *
+ * @param avctx Context from avcodec core
+ *
+ * @param q QSVContext to be initialized
+ *
+ * @return 0 on success, a negative AVERROR on error
+ */
+static int qsv_screen_context_init(AVCodecContext *avctx, QSVContext *q)
+{
+    int ret;
+    mfxSession session = NULL;
+    mfxVideoParam param = { 0 };
+    mfxVersion api_ver = { { 17, 1 } };
+
+    QSVScreenContext *ctx = avctx->priv_data;
+    mfxExtBuffer* buffers = (mfxExtBuffer*)&ctx->param;
+
+    ff_qsv_map_pixfmt(AV_PIX_FMT_NV12, &q->fourcc);
+
+    avctx->pix_fmt      = AV_PIX_FMT_NV12;
+    avctx->field_order  = AV_FIELD_TT;
+    avctx->level        = FF_LEVEL_UNKNOWN;
+    avctx->profile      = FF_PROFILE_UNKNOWN;
+
+    q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
+                                  (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));
+    if (!q->async_fifo)
+        return AVERROR(ENOMEM);
+
+    q->iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
+
+    q->avctx_internal = avcodec_alloc_context3(NULL);
+    if (!q->avctx_internal)
+        return AVERROR(ENOMEM);
+
+    q->orig_pix_fmt   = AV_PIX_FMT_NONE;
+
+    ret = ff_qsv_init_session(avctx, q, session, avctx->hw_frames_ctx, avctx->hw_device_ctx, api_ver);
+    if (ret < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
+        return ret;
+    }
+
+    param.mfx.CodecId      = MFX_CODEC_CAPTURE;
+
+    param.mfx.FrameInfo.FourCC          = q->fourcc;
+    param.mfx.FrameInfo.Width           = FFALIGN(avctx->width, 16);
+    param.mfx.FrameInfo.CropW           = avctx->width;
+    param.mfx.FrameInfo.Height          = FFALIGN(avctx->height, 16);
+    param.mfx.FrameInfo.CropH           = avctx->height;
+    param.mfx.FrameInfo.ChromaFormat    = MFX_CHROMAFORMAT_YUV420;
+
+    ctx->param.Header.BufferId = MFX_EXTBUFF_SCREEN_CAPTURE_PARAM;
+    ctx->param.Header.BufferSz = sizeof(mfxExtScreenCaptureParam);
+    ctx->param.EnableCursorCapture = ctx->draw_mouse;
+
+    param.ExtParam = &buffers;
+    param.NumExtParam = 1;
+
+    param.IOPattern   = q->iopattern;
+
+    ret = MFXVideoDECODE_Init(q->session, &param);
+    if (ret < 0)
+        return ff_qsv_print_error(avctx, ret,
+                                  "Error initializing the MFX video decoder");
+
+    q->frame_info = param.mfx.FrameInfo;
+
+    return 0;
+}
+
+/**
+ * Init QSV screen decoder and loads screen capture plugin (public decoder API).
+ *
+ * @param avctx Context from avcodec core
+ *
+ * @return 0 on success, a negative AVERROR on error
+ */
+static av_cold int qsv_screen_decode_init(AVCodecContext *avctx)
+{
+    QSVScreenContext *s = avctx->priv_data;
+    int ret;
+
+    static const char *uid_screendec_hw = "22d62c07e672408fbb4cc20ed7a053e4";
+
+    av_freep(&s->qsv.load_plugins);
+    s->qsv.load_plugins = av_strdup(uid_screendec_hw);
+    if (!s->qsv.load_plugins)
+        return AVERROR(ENOMEM);
+
+    ret = qsv_screen_context_init(avctx, &s->qsv);
+    if (ret < 0)
+        return ret;
+
+    return 0;
+}
+
+/**
+ * Produce frame from screen capturing plugin (public decoder API).
+ *
+ * @param avctx Context from avcodec core
+ *
+ * @param[out] data Pointer to AVFrame where data will be stored
+ *
+ * @param[out] got_frame zero, if no frame produced, otherwise - nonzero
+ *
+ * @param[in] avpkt Input AVPacket. Should be non-NULL and have zero sized buf
+ *
+ * @return produced packet size on success, a negative AVERROR on error
+ */
+static int qsv_screen_decode_frame(AVCodecContext *avctx, void *data,
+                            int *got_frame, AVPacket *avpkt)
+{
+    QSVScreenContext *ctx = avctx->priv_data;
+    AVFrame *frame    = data;
+    int ret;
+
+    if (avpkt->size) {
+        av_log(avctx, AV_LOG_ERROR, "pkt should be empty with 0-size buf\n");
+    }
+
+    while (!*got_frame) {
+        ret = ff_qsv_process_data(avctx, &ctx->qsv, frame, got_frame, avpkt);
+        if (ret < 0)
+            return ret;
+    }
+
+    return avpkt->size;
+}
+
+#define OFFSET(x) offsetof(QSVScreenContext, x)
+#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
+static const AVOption screen_options[] = {
+    { "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 }, 0, INT_MAX, VD },
+    { "draw_mouse", "Capture the mouse pointer", OFFSET(draw_mouse), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, VD },
+    { NULL },
+};
+
+
+static const AVClass screen_qsv_class = {
+    .class_name = "screen_qsv",
+    .item_name  = av_default_item_name,
+    .option     = screen_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVCodec ff_screen_qsv_decoder = {
+    .name           = "screen_qsv",
+    .long_name      = NULL_IF_CONFIG_SMALL("Fake decoder for screen capturing with intel QSV plugin"),
+    .priv_data_size = sizeof(QSVScreenContext),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_RAWVIDEO,
+    .init           = qsv_screen_decode_init,
+    .decode         = qsv_screen_decode_frame,
+    .flush          = qsv_screen_decode_flush,
+    .close          = qsv_screen_decode_close,
+    .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
+    .priv_class     = &screen_qsv_class,
+    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
+                                                    AV_PIX_FMT_NONE },
+};
diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index 5eb506fb76..04399819c4 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -708,8 +708,9 @@ static int qsvenc_init_session(AVCodecContext *avctx, QSVEncContext *q)
 
         q->session = q->internal_session;
     } else {
+        mfxVersion api_ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
         ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
-                                           q->load_plugins);
+                                           q->load_plugins,  api_ver);
         if (ret < 0)
             return ret;
 
-- 
2.13.0



More information about the ffmpeg-devel mailing list