From 66acb76bb0492b263215ca9b4d927a7be39ace02 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 16 Jun 2015 18:22:11 +0200 Subject: [PATCH] lavc: add Intel libmfx-based HEVC encoder --- Changelog | 2 +- configure | 1 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/hevc.h | 3 + libavcodec/hevc_ps_enc.c | 116 ++++++++++++++++ libavcodec/qsv.c | 51 ++++++- libavcodec/qsv_internal.h | 7 +- libavcodec/qsvdec.c | 2 +- libavcodec/qsvenc.c | 29 ++-- libavcodec/qsvenc.h | 3 + libavcodec/qsvenc_hevc.c | 272 ++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 4 +- 13 files changed, 475 insertions(+), 17 deletions(-) create mode 100644 libavcodec/hevc_ps_enc.c create mode 100644 libavcodec/qsvenc_hevc.c diff --git a/Changelog b/Changelog index f161cd423c..99b3d1d2a7 100644 --- a/Changelog +++ b/Changelog @@ -38,7 +38,7 @@ version : - DirectDraw Surface image/texture decoder - rewritten ASF demuxer - Go2Meeting decoding support -- Intel QSV-accelerated MPEG-2 video encoding +- Intel QSV-accelerated MPEG-2 video and HEVC encoding version 11: diff --git a/configure b/configure index 7bb3caf714..9d66469067 100755 --- a/configure +++ b/configure @@ -1869,6 +1869,7 @@ hap_encoder_deps="libsnappy" hap_encoder_select="texturedspenc" hevc_decoder_select="bswapdsp cabac golomb videodsp" hevc_nvenc_encoder_deps="nvenc" +hevc_qsv_encoder_select="qsvenc" huffyuv_decoder_select="bswapdsp huffyuvdsp" huffyuv_encoder_select="bswapdsp huffman huffyuvencdsp" iac_decoder_select="imc_decoder" diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 97c44acc13..1529b4b965 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -242,6 +242,7 @@ 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_NVENC_ENCODER) += nvenc_hevc.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 diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index a719e0a6ad..a3f1550f2f 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -484,6 +484,7 @@ void avcodec_register_all(void) REGISTER_ENCODER(H264_NVENC, h264_nvenc); REGISTER_ENCODER(H264_QSV, h264_qsv); REGISTER_ENCODER(HEVC_NVENC, hevc_nvenc); + REGISTER_ENCODER(HEVC_QSV, hevc_qsv); REGISTER_ENCODER(MPEG2_QSV, mpeg2_qsv); /* parsers */ diff --git a/libavcodec/hevc.h b/libavcodec/hevc.h index 8b06e1f4cd..60377c21de 100644 --- a/libavcodec/hevc.h +++ b/libavcodec/hevc.h @@ -1008,6 +1008,9 @@ void ff_hevc_pred_init(HEVCPredContext *hpc, int bit_depth); int ff_hevc_extract_rbsp(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]; diff --git a/libavcodec/hevc_ps_enc.c b/libavcodec/hevc_ps_enc.c new file mode 100644 index 0000000000..007a1321ed --- /dev/null +++ b/libavcodec/hevc_ps_enc.c @@ -0,0 +1,116 @@ +/* + * HEVC Parameter Set encoding + * + * This file is part of Libav. + * + * Libav 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. + * + * Libav 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 Libav; 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; +} diff --git a/libavcodec/qsv.c b/libavcodec/qsv.c index bd9e18d43e..ee6b262f31 100644 --- a/libavcodec/qsv.c +++ b/libavcodec/qsv.c @@ -19,7 +19,12 @@ */ #include +#include +#include +#include + +#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; @@ -77,7 +86,8 @@ int ff_qsv_error(int mfx_err) } } -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 } }; @@ -107,6 +117,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); diff --git a/libavcodec/qsv_internal.h b/libavcodec/qsv_internal.h index d13848a850..a0f4c7cef3 100644 --- a/libavcodec/qsv_internal.h +++ b/libavcodec/qsv_internal.h @@ -30,6 +30,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; @@ -46,6 +50,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 */ diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c index c077b110db..6fd9442b7f 100644 --- a/libavcodec/qsvdec.c +++ b/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; } diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index 690d5aa36d..a56acff2c0 100644 --- a/libavcodec/qsvenc.c +++ b/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; @@ -65,7 +67,7 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) q->param.mfx.BufferSizeInKB = 0; q->param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12; - q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, 16); + q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, q->width_align); q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, 32); q->param.mfx.FrameInfo.CropX = 0; q->param.mfx.FrameInfo.CropY = 0; @@ -124,15 +126,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; } @@ -199,7 +205,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; @@ -303,9 +310,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) diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h index 05d268a948..f3a06eb784 100644 --- a/libavcodec/qsvenc.h +++ b/libavcodec/qsvenc.h @@ -42,6 +42,7 @@ typedef struct QSVEncContext { mfxSession internal_session; int packet_size; + int width_align; mfxVideoParam param; mfxFrameAllocRequest req; @@ -56,6 +57,8 @@ typedef struct QSVEncContext { int preset; int avbr_accuracy; int avbr_convergence; + + char *load_plugins; } QSVEncContext; int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q); diff --git a/libavcodec/qsvenc_hevc.c b/libavcodec/qsvenc_hevc.c new file mode 100644 index 0000000000..03007f1814 --- /dev/null +++ b/libavcodec/qsvenc_hevc.c @@ -0,0 +1,272 @@ +/* + * Intel MediaSDK QSV based HEVC encoder + * + * This file is part of Libav. + * + * Libav 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. + * + * Libav 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 Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +#include +#include + +#include + +#include "libavutil/common.h" +#include "libavutil/opt.h" + +#include "avcodec.h" +#include "bytestream.h" +#include "get_bits.h" +#include "hevc.h" +#include "internal.h" +#include "qsv.h" +#include "qsv_internal.h" +#include "qsvenc.h" + +enum { + LOAD_PLUGIN_NONE, + LOAD_PLUGIN_HEVC_SW, + LOAD_PLUGIN_HEVC_HW, +} LoadPlugin; + +typedef struct QSVHEVCEncContext { + AVClass *class; + QSVEncContext qsv; + int load_plugin; +} QSVHEVCEncContext; + +static int generate_fake_vps(QSVEncContext *q, AVCodecContext *avctx) +{ + GetByteContext gbc; + PutByteContext pbc; + + GetBitContext gb; + HEVCNAL sps_nal = { NULL }; + HEVCSPS sps = { 0 }; + HEVCVPS vps = { 0 }; + uint8_t vps_buf[128], vps_rbsp_buf[128]; + uint8_t *new_extradata; + unsigned int sps_id; + int ret, i, type, vps_size; + + if (!avctx->extradata_size) { + av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx\n"); + return AVERROR_UNKNOWN; + } + + /* parse the SPS */ + ret = ff_hevc_extract_rbsp(avctx->extradata + 4, avctx->extradata_size - 4, &sps_nal); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Error unescaping the SPS buffer\n"); + return ret; + } + + ret = init_get_bits8(&gb, sps_nal.data, sps_nal.size); + if (ret < 0) { + av_freep(&sps_nal.rbsp_buffer); + return ret; + } + + get_bits(&gb, 1); + type = get_bits(&gb, 6); + if (type != NAL_SPS) { + av_log(avctx, AV_LOG_ERROR, "Unexpected NAL type in the extradata: %d\n", + type); + av_freep(&sps_nal.rbsp_buffer); + return AVERROR_INVALIDDATA; + } + get_bits(&gb, 9); + + ret = ff_hevc_parse_sps(&sps, &gb, &sps_id, 0, NULL, avctx); + av_freep(&sps_nal.rbsp_buffer); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Error parsing the SPS\n"); + return ret; + } + + /* generate the VPS */ + vps.vps_max_layers = 1; + vps.vps_max_sub_layers = sps.max_sub_layers; + memcpy(&vps.ptl, &sps.ptl, sizeof(vps.ptl)); + vps.vps_sub_layer_ordering_info_present_flag = 1; + for (i = 0; i < MAX_SUB_LAYERS; i++) { + vps.vps_max_dec_pic_buffering[i] = sps.temporal_layer[i].max_dec_pic_buffering; + vps.vps_num_reorder_pics[i] = sps.temporal_layer[i].num_reorder_pics; + vps.vps_max_latency_increase[i] = sps.temporal_layer[i].max_latency_increase; + } + + vps.vps_num_layer_sets = 1; + vps.vps_timing_info_present_flag = sps.vui.vui_timing_info_present_flag; + vps.vps_num_units_in_tick = sps.vui.vui_num_units_in_tick; + vps.vps_time_scale = sps.vui.vui_time_scale; + vps.vps_poc_proportional_to_timing_flag = sps.vui.vui_poc_proportional_to_timing_flag; + vps.vps_num_ticks_poc_diff_one = sps.vui.vui_num_ticks_poc_diff_one_minus1 + 1; + + /* generate the encoded RBSP form of the VPS */ + ret = ff_hevc_encode_nal_vps(&vps, sps.vps_id, vps_rbsp_buf, sizeof(vps_rbsp_buf)); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Error writing the VPS\n"); + return ret; + } + + /* escape and add the startcode */ + bytestream2_init(&gbc, vps_rbsp_buf, ret); + bytestream2_init_writer(&pbc, vps_buf, sizeof(vps_buf)); + + bytestream2_put_be32(&pbc, 1); // startcode + bytestream2_put_byte(&pbc, NAL_VPS << 1); // NAL + bytestream2_put_byte(&pbc, 1); // header + + while (bytestream2_get_bytes_left(&gbc)) { + uint32_t b = bytestream2_peek_be24(&gbc); + if (b <= 3) { + bytestream2_put_be24(&pbc, 3); + bytestream2_skip(&gbc, 2); + } else + bytestream2_put_byte(&pbc, bytestream2_get_byte(&gbc)); + } + + vps_size = bytestream2_tell_p(&pbc); + new_extradata = av_mallocz(vps_size + avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); + if (!new_extradata) + return AVERROR(ENOMEM); + memcpy(new_extradata, vps_buf, vps_size); + memcpy(new_extradata + vps_size, avctx->extradata, avctx->extradata_size); + + av_freep(&avctx->extradata); + avctx->extradata = new_extradata; + avctx->extradata_size += vps_size; + + return 0; +} + +static av_cold int qsv_enc_init(AVCodecContext *avctx) +{ + QSVHEVCEncContext *q = avctx->priv_data; + int ret; + + if (q->load_plugin != LOAD_PLUGIN_NONE) { + static const char *uid_hevcenc_sw = "2fca99749fdb49aeb121a5b63ef568f7"; + static const char *uid_hevcenc_hw = "e5400a06c74d41f5b12d430bbaa23d0b"; + + if (q->qsv.load_plugins[0]) { + av_log(avctx, AV_LOG_WARNING, + "load_plugins is not empty, but load_plugin is not set to 'none'." + "The load_plugin value will be ignored.\n"); + } else { + av_freep(&q->qsv.load_plugins); + + if (q->load_plugin == LOAD_PLUGIN_HEVC_SW) + q->qsv.load_plugins = av_strdup(uid_hevcenc_sw); + else + q->qsv.load_plugins = av_strdup(uid_hevcenc_hw); + + if (!q->qsv.load_plugins) + return AVERROR(ENOMEM); + } + } + + ret = ff_qsv_enc_init(avctx, &q->qsv); + if (ret < 0) + return ret; + + ret = generate_fake_vps(&q->qsv, avctx); + if (ret < 0) { + ff_qsv_enc_close(avctx, &q->qsv); + return ret; + } + + return 0; +} + +static int qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt, + const AVFrame *frame, int *got_packet) +{ + QSVHEVCEncContext *q = avctx->priv_data; + + return ff_qsv_encode(avctx, &q->qsv, pkt, frame, got_packet); +} + +static av_cold int qsv_enc_close(AVCodecContext *avctx) +{ + QSVHEVCEncContext *q = avctx->priv_data; + + return ff_qsv_enc_close(avctx, &q->qsv); +} + +#define OFFSET(x) offsetof(QSVHEVCEncContext, x) +#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM +static const AVOption options[] = { + { "async_depth", "Maximum processing parallelism", OFFSET(qsv.async_depth), AV_OPT_TYPE_INT, { .i64 = ASYNC_DEPTH_DEFAULT }, 0, INT_MAX, VE }, + { "avbr_accuracy", "Accuracy of the AVBR ratecontrol", OFFSET(qsv.avbr_accuracy), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, + { "avbr_convergence", "Convergence of the AVBR ratecontrol", OFFSET(qsv.avbr_convergence), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, + + { "load_plugin", "A user plugin to load in an internal session", OFFSET(load_plugin), AV_OPT_TYPE_INT, { .i64 = LOAD_PLUGIN_HEVC_SW }, LOAD_PLUGIN_NONE, LOAD_PLUGIN_HEVC_HW, VE, "load_plugin" }, + { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_NONE }, 0, 0, VE, "load_plugin" }, + { "hevc_sw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_SW }, 0, 0, VE, "load_plugin" }, + { "hevc_hw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_HW }, 0, 0, VE, "load_plugin" }, + + { "load_plugins", "A :-separate list of hexadecimal plugin UIDs to load in an internal session", + OFFSET(qsv.load_plugins), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VE }, + + { "profile", NULL, OFFSET(qsv.profile), AV_OPT_TYPE_INT, { .i64 = MFX_PROFILE_UNKNOWN }, 0, INT_MAX, VE, "profile" }, + { "unknown", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_UNKNOWN }, INT_MIN, INT_MAX, VE, "profile" }, + { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAIN }, INT_MIN, INT_MAX, VE, "profile" }, + { "main10", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAIN10 }, INT_MIN, INT_MAX, VE, "profile" }, + { "mainsp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAINSP }, INT_MIN, INT_MAX, VE, "profile" }, + + { "preset", NULL, OFFSET(qsv.preset), AV_OPT_TYPE_INT, { .i64 = MFX_TARGETUSAGE_BALANCED }, 0, 7, VE, "preset" }, + { "fast", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BEST_SPEED }, INT_MIN, INT_MAX, VE, "preset" }, + { "medium", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BALANCED }, INT_MIN, INT_MAX, VE, "preset" }, + { "slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BEST_QUALITY }, INT_MIN, INT_MAX, VE, "preset" }, + + { NULL }, +}; + +static const AVClass class = { + .class_name = "hevc_qsv encoder", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +static const AVCodecDefault qsv_enc_defaults[] = { + { "b", "1M" }, + { "refs", "0" }, + // same as the x264 default + { "g", "250" }, + { "bf", "3" }, + + { "flags", "+cgop" }, + { NULL }, +}; + +AVCodec ff_hevc_qsv_encoder = { + .name = "hevc_qsv", + .long_name = NULL_IF_CONFIG_SMALL("HEVC (Intel Quick Sync Video acceleration)"), + .priv_data_size = sizeof(QSVHEVCEncContext), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_HEVC, + .init = qsv_enc_init, + .encode2 = qsv_enc_frame, + .close = qsv_enc_close, + .capabilities = CODEC_CAP_DELAY, + .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12, + AV_PIX_FMT_QSV, + AV_PIX_FMT_NONE }, + .priv_class = &class, + .defaults = qsv_enc_defaults, +}; diff --git a/libavcodec/version.h b/libavcodec/version.h index 0ea5adaf71..ca928e8385 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -29,8 +29,8 @@ #include "libavutil/version.h" #define LIBAVCODEC_VERSION_MAJOR 56 -#define LIBAVCODEC_VERSION_MINOR 31 -#define LIBAVCODEC_VERSION_MICRO 2 +#define LIBAVCODEC_VERSION_MINOR 32 +#define LIBAVCODEC_VERSION_MICRO 0 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \