diff mbox

[FFmpeg-devel,1/2] lavc/qsvenc: enable vp9 encoder

Message ID 20191026141832.6465-1-zhongli_dev@126.com
State New
Headers show

Commit Message

Zhong Li Oct. 26, 2019, 2:18 p.m. UTC
From: Zhong Li <zhongli_dev@126.com>

1. must enable low_power mode since just VDENC can be supported by iHD
driver right now
2. Coding option1 and extra_data are not supported by MSDK
3. IVF header will be inserted in MSDK by default, but it is not needed
for FFmpeg, so disable it.

Signed-off-by: Zhong Li <zhongli_dev@126.com>
---
 Changelog              |  1 +
 configure              |  6 ++++
 libavcodec/Makefile    |  1 +
 libavcodec/allcodecs.c |  1 +
 libavcodec/qsvenc.c    | 62 +++++++++++++++++++++++++++++++++++++++++-
 libavcodec/qsvenc.h    |  6 ++++
 6 files changed, 76 insertions(+), 1 deletion(-)
diff mbox

Patch

diff --git a/Changelog b/Changelog
index f49c4d351d..4ccd6373a4 100644
--- a/Changelog
+++ b/Changelog
@@ -18,6 +18,7 @@  version <next>:
 - arnndn filter
 - bilateral filter
 - maskedmin and maskedmax filters
+- Intel QSV-accelerated VP9 encoding
 
 
 version 4.2:
diff --git a/configure b/configure
index 8413826f9e..7af7569092 100755
--- a/configure
+++ b/configure
@@ -3088,6 +3088,8 @@  vp9_qsv_decoder_select="qsvdec"
 vp9_rkmpp_decoder_deps="rkmpp"
 vp9_vaapi_encoder_deps="VAEncPictureParameterBufferVP9"
 vp9_vaapi_encoder_select="vaapi_encode"
+vp9_qsv_encoder_deps="libmfx MFX_CODEC_VP9"
+vp9_qsv_encoder_select="qsvenc"
 vp9_v4l2m2m_decoder_deps="v4l2_m2m vp9_v4l2_m2m"
 wmv3_crystalhd_decoder_select="crystalhd"
 
@@ -6239,6 +6241,10 @@  enabled liblensfun        && require_pkg_config liblensfun lensfun lensfun.h lf_
 # can find the libraries and headers through other means.
 enabled libmfx            && { check_pkg_config libmfx libmfx "mfx/mfxvideo.h" MFXInit ||
                                { require libmfx "mfx/mfxvideo.h" MFXInit "-llibmfx $advapi32_extralibs" && warn "using libmfx without pkg-config"; } }
+if enabled libmfx; then
+   check_cc MFX_CODEC_VP9 "mfx/mfxvp9.h mfx/mfxstructures.h" "MFX_CODEC_VP9"
+fi
+
 enabled libmodplug        && require_pkg_config libmodplug libmodplug libmodplug/modplug.h ModPlug_Load
 enabled libmp3lame        && require "libmp3lame >= 3.98.3" lame/lame.h lame_set_VBR_quality -lmp3lame $libm_extralibs
 enabled libmysofa         && { check_pkg_config libmysofa libmysofa mysofa.h mysofa_neighborhood_init_withstepdefine ||
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 37a84a6bb4..25e16f9de4 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -685,6 +685,7 @@  OBJS-$(CONFIG_VP9_CUVID_DECODER)       += cuviddec.o
 OBJS-$(CONFIG_VP9_MEDIACODEC_DECODER)  += mediacodecdec.o
 OBJS-$(CONFIG_VP9_RKMPP_DECODER)       += rkmppdec.o
 OBJS-$(CONFIG_VP9_VAAPI_ENCODER)       += vaapi_encode_vp9.o
+OBJS-$(CONFIG_VP9_QSV_ENCODER)         += qsvenc_vp9.o
 OBJS-$(CONFIG_VPLAYER_DECODER)         += textdec.o ass.o
 OBJS-$(CONFIG_VP9_V4L2M2M_DECODER)     += v4l2_m2m_dec.o
 OBJS-$(CONFIG_VQA_DECODER)             += vqavideo.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 41f680101f..23a778e041 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -784,6 +784,7 @@  extern AVCodec ff_vp9_cuvid_decoder;
 extern AVCodec ff_vp9_mediacodec_decoder;
 extern AVCodec ff_vp9_qsv_decoder;
 extern AVCodec ff_vp9_vaapi_encoder;
+extern AVCodec ff_vp9_qsv_encoder;
 
 // The iterate API is not usable with ossfuzz due to the excessive size of binaries created
 #if CONFIG_OSSFUZZ
diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index ba85d645ca..b8eabd78da 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -637,7 +637,8 @@  static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
     // The HEVC encoder plugin currently fails with some old libmfx version if coding options
     // are provided. Can't find the extract libmfx version which fixed it, just enable it from
     // V1.28 in order to keep compatibility security.
-    if ((avctx->codec_id != AV_CODEC_ID_HEVC) || QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 28)) {
+    if (((avctx->codec_id != AV_CODEC_ID_HEVC) || QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 28))
+        && (avctx->codec_id != AV_CODEC_ID_VP9)) {
         q->extco.Header.BufferId      = MFX_EXTBUFF_CODING_OPTION;
         q->extco.Header.BufferSz      = sizeof(q->extco);
 
@@ -761,6 +762,15 @@  FF_ENABLE_DEPRECATION_WARNINGS
 #endif
     }
 
+#if QSV_HAVE_EXT_VP9_PARAM
+    if (avctx->codec_id == AV_CODEC_ID_VP9) {
+       q->extvp9param.Header.BufferId = MFX_EXTBUFF_VP9_PARAM;
+       q->extvp9param.Header.BufferSz = sizeof(q->extvp9param);
+       q->extvp9param.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
+       q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extvp9param;
+    }
+#endif
+
     if (!check_enc_param(avctx,q)) {
         av_log(avctx, AV_LOG_ERROR,
                "some encoding parameters are not supported by the QSV "
@@ -789,6 +799,53 @@  static int qsv_retrieve_enc_jpeg_params(AVCodecContext *avctx, QSVEncContext *q)
     return 0;
 }
 
+static int qsv_retrieve_enc_vp9_params(AVCodecContext *avctx, QSVEncContext *q)
+{
+    int ret = 0;
+#if QSV_HAVE_EXT_VP9_PARAM
+    mfxExtVP9Param vp9_extend_buf = {
+         .Header.BufferId = MFX_EXTBUFF_VP9_PARAM,
+         .Header.BufferSz = sizeof(vp9_extend_buf),
+    };
+#endif
+
+#if QSV_HAVE_CO2
+    mfxExtCodingOption2 co2 = {
+        .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
+        .Header.BufferSz = sizeof(co2),
+    };
+#endif
+
+#if QSV_HAVE_CO3
+    mfxExtCodingOption3 co3 = {
+        .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
+        .Header.BufferSz = sizeof(co3),
+    };
+#endif
+
+    mfxExtBuffer *ext_buffers[] = {
+        (mfxExtBuffer*)&vp9_extend_buf,
+#if QSV_HAVE_CO2
+        (mfxExtBuffer*)&co2,
+#endif
+#if QSV_HAVE_CO3
+        (mfxExtBuffer*)&co3,
+#endif
+    };
+
+    q->param.ExtParam    = ext_buffers;
+    q->param.NumExtParam = FF_ARRAY_ELEMS(ext_buffers);
+
+    ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
+    if (ret < 0)
+        return ff_qsv_print_error(avctx, ret,
+                                  "Error calling GetVideoParam");
+
+    q->packet_size = q->param.mfx.BufferSizeInKB * q->param.mfx.BRCParamMultiplier * 1000;
+
+    return 0;
+}
+
 static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q)
 {
     AVCPBProperties *cpb_props;
@@ -1112,6 +1169,9 @@  int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
     case AV_CODEC_ID_MJPEG:
         ret = qsv_retrieve_enc_jpeg_params(avctx, q);
         break;
+    case AV_CODEC_ID_VP9:
+	ret = qsv_retrieve_enc_vp9_params(avctx, q);
+	break;
     default:
         ret = qsv_retrieve_enc_params(avctx, q);
         break;
diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h
index ec8b5419cb..a3e9357865 100644
--- a/libavcodec/qsvenc.h
+++ b/libavcodec/qsvenc.h
@@ -38,6 +38,8 @@ 
 #define QSV_HAVE_CO3 QSV_VERSION_ATLEAST(1, 11)
 #define QSV_HAVE_CO_VPS  QSV_VERSION_ATLEAST(1, 17)
 
+#define QSV_HAVE_EXT_VP9_PARAM QSV_VERSION_ATLEAST(1, 26)
+
 #define QSV_HAVE_TRELLIS QSV_VERSION_ATLEAST(1, 8)
 #define QSV_HAVE_MAX_SLICE_SIZE QSV_VERSION_ATLEAST(1, 9)
 #define QSV_HAVE_BREF_TYPE      QSV_VERSION_ATLEAST(1, 8)
@@ -122,6 +124,10 @@  typedef struct QSVEncContext {
     mfxExtMultiFrameParam   extmfp;
     mfxExtMultiFrameControl extmfc;
 #endif
+#if QSV_HAVE_EXT_VP9_PARAM
+   mfxExtVP9Param  extvp9param;
+#endif
+
     mfxExtOpaqueSurfaceAlloc opaque_alloc;
     mfxFrameSurface1       **opaque_surfaces;
     AVBufferRef             *opaque_alloc_buf;