Browse Source

Merge commit '66acb76bb0492b263215ca9b4d927a7be39ace02'

* commit '66acb76bb0492b263215ca9b4d927a7be39ace02':
  lavc: add Intel libmfx-based HEVC encoder

Conflicts:
	Changelog
	configure
	libavcodec/Makefile
	libavcodec/allcodecs.c
	libavcodec/qsv.c
	libavcodec/qsvenc.c
	libavcodec/version.h

Merged-by: Michael Niedermayer <michaelni@gmx.at>
Michael Niedermayer 9 years ago
parent
commit
7871eb4361
10 changed files with 199 additions and 16 deletions
  1. 1 1
      Changelog
  2. 1 0
      configure
  3. 1 0
      libavcodec/Makefile
  4. 1 0
      libavcodec/allcodecs.c
  5. 3 0
      libavcodec/hevc.h
  6. 116 0
      libavcodec/hevc_ps_enc.c
  7. 50 1
      libavcodec/qsv.c
  8. 6 1
      libavcodec/qsv_internal.h
  9. 1 1
      libavcodec/qsvdec.c
  10. 19 12
      libavcodec/qsvenc.c

+ 1 - 1
Changelog

@@ -14,7 +14,7 @@ version <next>:
 - Go2Meeting decoding support
 - adrawgraph audio and drawgraph video filter
 - removegrain video filter
-- Intel QSV-accelerated MPEG-2 video encoding
+- Intel QSV-accelerated MPEG-2 video and HEVC encoding
 
 
 version 2.7:

+ 1 - 0
configure

@@ -2214,6 +2214,7 @@ hap_decoder_select="snappy texturedsp"
 hap_encoder_deps="libsnappy"
 hap_encoder_select="texturedspenc"
 hevc_decoder_select="bswapdsp cabac golomb videodsp"
+hevc_qsv_encoder_select="qsvenc"
 huffyuv_decoder_select="bswapdsp huffyuvdsp llviddsp"
 huffyuv_encoder_select="bswapdsp huffman huffyuvencdsp llviddsp"
 iac_decoder_select="imc_decoder"

+ 1 - 0
libavcodec/Makefile

@@ -286,6 +286,7 @@ OBJS-$(CONFIG_HAP_ENCODER)             += hapenc.o
 OBJS-$(CONFIG_HEVC_DECODER)            += hevc.o hevc_mvs.o hevc_ps.o hevc_sei.o \
                                           hevc_cabac.o hevc_refs.o hevcpred.o    \
                                           hevcdsp.o hevc_filter.o hevc_parse.o
+OBJS-$(CONFIG_HEVC_QSV_ENCODER)        += qsvenc_hevc.o hevc_ps_enc.o hevc_parse.o
 OBJS-$(CONFIG_HNM4_VIDEO_DECODER)      += hnm4video.o
 OBJS-$(CONFIG_HQ_HQA_DECODER)          += hq_hqa.o hq_hqadata.o hq_hqadsp.o \
                                           canopus.o

+ 1 - 0
libavcodec/allcodecs.c

@@ -569,6 +569,7 @@ void avcodec_register_all(void)
     REGISTER_ENCODER(NVENC,             nvenc);
     REGISTER_ENCODER(NVENC_H264,        nvenc_h264);
     REGISTER_ENCODER(NVENC_HEVC,        nvenc_hevc);
+    REGISTER_ENCODER(HEVC_QSV,          hevc_qsv);
     REGISTER_ENCODER(MPEG2_QSV,         mpeg2_qsv);
 
     /* parsers */

+ 3 - 0
libavcodec/hevc.h

@@ -1054,6 +1054,9 @@ void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size);
 int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
                          HEVCNAL *nal);
 
+int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id,
+                           uint8_t *buf, int buf_size);
+
 extern const uint8_t ff_hevc_qpel_extra_before[4];
 extern const uint8_t ff_hevc_qpel_extra_after[4];
 extern const uint8_t ff_hevc_qpel_extra[4];

+ 116 - 0
libavcodec/hevc_ps_enc.c

@@ -0,0 +1,116 @@
+/*
+ * HEVC Parameter Set encoding
+ *
+ * This file is part of FFmpeg.
+ *
+ * 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
+ */
+
+#include "golomb.h"
+#include "hevc.h"
+#include "put_bits.h"
+
+static void write_ptl_layer(PutBitContext *pb, PTLCommon *ptl)
+{
+    int i;
+
+    put_bits(pb, 2, ptl->profile_space);
+    put_bits(pb, 1, ptl->tier_flag);
+    put_bits(pb, 5, ptl->profile_idc);
+    for (i = 0; i < 32; i++)
+        put_bits(pb, 1, ptl->profile_compatibility_flag[i]);
+    put_bits(pb, 1, ptl->progressive_source_flag);
+    put_bits(pb, 1, ptl->interlaced_source_flag);
+    put_bits(pb, 1, ptl->non_packed_constraint_flag);
+    put_bits(pb, 1, ptl->frame_only_constraint_flag);
+    put_bits32(pb, 0);   // reserved
+    put_bits(pb, 12, 0); // reserved
+}
+
+static void write_ptl(PutBitContext *pb, PTL *ptl, int max_num_sub_layers)
+{
+    int i;
+
+    write_ptl_layer(pb, &ptl->general_ptl);
+    put_bits(pb, 8, ptl->general_ptl.level_idc);
+
+    for (i = 0; i < max_num_sub_layers - 1; i++) {
+        put_bits(pb, 1, ptl->sub_layer_profile_present_flag[i]);
+        put_bits(pb, 1, ptl->sub_layer_level_present_flag[i]);
+    }
+
+    if (max_num_sub_layers > 1)
+        for (i = max_num_sub_layers - 1; i < 8; i++)
+            put_bits(pb, 2, 0); // reserved
+
+    for (i = 0; i < max_num_sub_layers - 1; i++) {
+        if (ptl->sub_layer_profile_present_flag[i])
+            write_ptl_layer(pb, &ptl->sub_layer_ptl[i]);
+        if (ptl->sub_layer_level_present_flag[i])
+            put_bits(pb, 8, ptl->sub_layer_ptl[i].level_idc);
+    }
+}
+
+int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id,
+                           uint8_t *buf, int buf_size)
+{
+    PutBitContext pb;
+    int i;
+
+    init_put_bits(&pb, buf, buf_size);
+    put_bits(&pb,  4, id);
+    put_bits(&pb,  2, 3);                               // reserved
+    put_bits(&pb,  6, vps->vps_max_layers - 1);
+    put_bits(&pb,  3, vps->vps_max_sub_layers - 1);
+    put_bits(&pb,  1, vps->vps_temporal_id_nesting_flag);
+    put_bits(&pb, 16, 0xffff);                          // reserved
+
+    write_ptl(&pb, &vps->ptl, vps->vps_max_sub_layers);
+
+    put_bits(&pb, 1, vps->vps_sub_layer_ordering_info_present_flag);
+    for (i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_layers - 1;
+         i < vps->vps_max_sub_layers; i++) {
+        set_ue_golomb(&pb, vps->vps_max_dec_pic_buffering[i] - 1);
+        set_ue_golomb(&pb, vps->vps_num_reorder_pics[i]);
+        set_ue_golomb(&pb, vps->vps_max_latency_increase[i] + 1);
+    }
+
+    put_bits(&pb, 6, vps->vps_max_layer_id);
+    set_ue_golomb(&pb, vps->vps_num_layer_sets - 1);
+
+    // writing layer_id_included_flag not supported
+    if (vps->vps_num_layer_sets > 1)
+        return AVERROR_PATCHWELCOME;
+
+    put_bits(&pb, 1, vps->vps_timing_info_present_flag);
+    if (vps->vps_timing_info_present_flag) {
+        put_bits32(&pb, vps->vps_num_units_in_tick);
+        put_bits32(&pb, vps->vps_time_scale);
+        put_bits(&pb, 1, vps->vps_poc_proportional_to_timing_flag);
+        if (vps->vps_poc_proportional_to_timing_flag)
+            set_ue_golomb(&pb, vps->vps_num_ticks_poc_diff_one - 1);
+
+        // writing HRD parameters not supported
+        if (vps->vps_num_hrd_parameters)
+            return AVERROR_PATCHWELCOME;
+    }
+
+    put_bits(&pb, 1, 0);    // extension flag
+
+    put_bits(&pb, 1, 1);    // stop bit
+    avpriv_align_put_bits(&pb);
+
+    return put_bits_count(&pb) / 8;
+}

+ 50 - 1
libavcodec/qsv.c

@@ -19,7 +19,12 @@
  */
 
 #include <mfx/mfxvideo.h>
+#include <mfx/mfxplugin.h>
 
+#include <stdio.h>
+#include <string.h>
+
+#include "libavutil/avstring.h"
 #include "libavutil/error.h"
 
 #include "avcodec.h"
@@ -30,6 +35,10 @@ int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
     switch (codec_id) {
     case AV_CODEC_ID_H264:
         return MFX_CODEC_AVC;
+#if QSV_VERSION_ATLEAST(1, 8)
+    case AV_CODEC_ID_HEVC:
+        return MFX_CODEC_HEVC;
+#endif
     case AV_CODEC_ID_MPEG1VIDEO:
     case AV_CODEC_ID_MPEG2VIDEO:
         return MFX_CODEC_MPEG2;
@@ -154,7 +163,8 @@ static int ff_qsv_set_display_handle(AVCodecContext *avctx, mfxSession session)
  * @param avctx    ffmpeg metadata for this codec context
  * @param session  the MSDK session used
  */
-int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session)
+int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
+                                 const char *load_plugins)
 {
     mfxIMPL impl   = MFX_IMPL_AUTO_ANY;
     mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
@@ -188,6 +198,45 @@ int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session)
         desc = "unknown";
     }
 
+    if (load_plugins && *load_plugins) {
+        while (*load_plugins) {
+            mfxPluginUID uid;
+            int i, err = 0;
+
+            char *plugin = av_get_token(&load_plugins, ":");
+            if (!plugin)
+                return AVERROR(ENOMEM);
+            if (strlen(plugin) != 2 * sizeof(uid.Data)) {
+                av_log(avctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
+                err = AVERROR(EINVAL);
+                goto load_plugin_fail;
+            }
+
+            for (i = 0; i < sizeof(uid.Data); i++) {
+                err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
+                if (err != 1) {
+                    av_log(avctx, AV_LOG_ERROR, "Invalid plugin UID\n");
+                    err = AVERROR(EINVAL);
+                    goto load_plugin_fail;
+                }
+
+            }
+
+            ret = MFXVideoUSER_Load(*session, &uid, 1);
+            if (ret < 0) {
+                av_log(avctx, AV_LOG_ERROR, "Could not load the requested plugin: %s\n",
+                       plugin);
+                err = ff_qsv_error(ret);
+                goto load_plugin_fail;
+            }
+
+load_plugin_fail:
+            av_freep(&plugin);
+            if (err < 0)
+                return err;
+        }
+    }
+
     av_log(avctx, AV_LOG_VERBOSE,
            "Initialized an internal MFX session using %s implementation\n",
            desc);

+ 6 - 1
libavcodec/qsv_internal.h

@@ -45,6 +45,10 @@
 
 #define ASYNC_DEPTH_DEFAULT 4       // internal parallelism
 
+#define QSV_VERSION_ATLEAST(MAJOR, MINOR)   \
+    (MFX_VERSION_MAJOR > (MAJOR) ||         \
+     MFX_VERSION_MAJOR == (MAJOR) && MFX_VERSION_MINOR >= (MINOR))
+
 typedef struct QSVFrame {
     AVFrame *frame;
     mfxFrameSurface1 *surface;
@@ -61,6 +65,7 @@ int ff_qsv_error(int mfx_err);
 
 int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id);
 
-int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session);
+int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
+                                 const char *load_plugins);
 
 #endif /* AVCODEC_QSV_INTERNAL_H */

+ 1 - 1
libavcodec/qsvdec.c

@@ -52,7 +52,7 @@ static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession ses
 {
     if (!session) {
         if (!q->internal_session) {
-            int ret = ff_qsv_init_internal_session(avctx, &q->internal_session);
+            int ret = ff_qsv_init_internal_session(avctx, &q->internal_session, NULL);
             if (ret < 0)
                 return ret;
         }

+ 19 - 12
libavcodec/qsvenc.c

@@ -49,6 +49,8 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
         return AVERROR_BUG;
     q->param.mfx.CodecId = ret;
 
+    q->width_align = avctx->codec_id == AV_CODEC_ID_HEVC ? 32 : 16;
+
     if (avctx->level > 0)
         q->param.mfx.CodecLevel = avctx->level;
 
@@ -74,7 +76,7 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
     q->param.mfx.FrameInfo.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
     q->param.mfx.FrameInfo.BitDepthLuma   = 8;
     q->param.mfx.FrameInfo.BitDepthChroma = 8;
-    q->param.mfx.FrameInfo.Width          = FFALIGN(avctx->width, 16);
+    q->param.mfx.FrameInfo.Width          = FFALIGN(avctx->width, q->width_align);
 
     if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
        /* A true field layout (TFF or BFF) is not important here,
@@ -86,7 +88,7 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
         q->param.mfx.FrameInfo.Height    = FFALIGN(avctx->height, 32);
     } else {
         q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
-        q->param.mfx.FrameInfo.Height    = FFALIGN(avctx->height, 16);
+        q->param.mfx.FrameInfo.Height    = FFALIGN(avctx->height, avctx->codec_id == AV_CODEC_ID_HEVC ? 32 : 16);
     }
 
     if (avctx->framerate.den > 0 && avctx->framerate.num > 0) {
@@ -135,15 +137,19 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
         break;
     }
 
-    q->extco.Header.BufferId      = MFX_EXTBUFF_CODING_OPTION;
-    q->extco.Header.BufferSz      = sizeof(q->extco);
-    q->extco.CAVLC                = avctx->coder_type == FF_CODER_TYPE_VLC ?
-                                    MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
+    // the HEVC encoder plugin currently fails if coding options
+    // are provided
+    if (avctx->codec_id != AV_CODEC_ID_HEVC) {
+        q->extco.Header.BufferId      = MFX_EXTBUFF_CODING_OPTION;
+        q->extco.Header.BufferSz      = sizeof(q->extco);
+        q->extco.CAVLC                = avctx->coder_type == FF_CODER_TYPE_VLC ?
+                                        MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
 
-    q->extparam[0] = (mfxExtBuffer *)&q->extco;
+        q->extparam[0] = (mfxExtBuffer *)&q->extco;
 
-    q->param.ExtParam    = q->extparam;
-    q->param.NumExtParam = FF_ARRAY_ELEMS(q->extparam);
+        q->param.ExtParam    = q->extparam;
+        q->param.NumExtParam = FF_ARRAY_ELEMS(q->extparam);
+    }
 
     return 0;
 }
@@ -210,7 +216,8 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
     }
 
     if (!q->session) {
-        ret = ff_qsv_init_internal_session(avctx, &q->internal_session);
+        ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
+                                           q->load_plugins);
         if (ret < 0)
             return ret;
 
@@ -316,9 +323,9 @@ static int submit_frame(QSVEncContext *q, const AVFrame *frame,
     }
 
     /* make a copy if the input is not padded as libmfx requires */
-    if (frame->height & 31 || frame->linesize[0] & 15) {
+    if (frame->height & 31 || frame->linesize[0] & (q->width_align - 1)) {
         qf->frame->height = FFALIGN(frame->height, 32);
-        qf->frame->width  = FFALIGN(frame->width, 16);
+        qf->frame->width  = FFALIGN(frame->width, q->width_align);
 
         ret = ff_get_buffer(q->avctx, qf->frame, AV_GET_BUFFER_FLAG_REF);
         if (ret < 0)

Some files were not shown because too many files changed in this diff