X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fqsvenc.c;h=ba85d645ca75b184882b89d8532319669521e26a;hb=5345965b3f088ad5acd5151bec421c97470675a4;hp=5aa020d47b3c16594a4dbddc5642d54c8e6dcd00;hpb=5ca7eb36b7353f9e6af05a5a952eead5f6d326dd;p=ffmpeg diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index 5aa020d47b3..ba85d645ca7 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -267,6 +267,11 @@ static void dump_video_param(AVCodecContext *avctx, QSVEncContext *q, #endif #endif +#if QSV_HAVE_GPB + if (avctx->codec_id == AV_CODEC_ID_HEVC) + av_log(avctx, AV_LOG_VERBOSE,"GPB: %s\n", print_threestate(co3->GPB)); +#endif + if (avctx->codec_id == AV_CODEC_ID_H264) { av_log(avctx, AV_LOG_VERBOSE, "Entropy coding: %s; MaxDecFrameBuffering: %"PRIu16"\n", co->CAVLC == MFX_CODINGOPTION_ON ? "CAVLC" : "CABAC", co->MaxDecFrameBuffering); @@ -468,13 +473,6 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier; int buffer_size_in_kilobytes, initial_delay_in_kilobytes; int ret; - mfxVersion ver; - - ret = MFXQueryVersion(q->session,&ver); - if (ret != MFX_ERR_NONE) { - av_log(avctx, AV_LOG_ERROR, "Error getting the session handle\n"); - return AVERROR_UNKNOWN; - } ret = ff_qsv_codec_id_to_mfx(avctx->codec_id); if (ret < 0) @@ -495,9 +493,18 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) } } + if (q->low_power) { #if QSV_HAVE_VDENC - q->param.mfx.LowPower = q->low_power ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF; + q->param.mfx.LowPower = MFX_CODINGOPTION_ON; +#else + av_log(avctx, AV_LOG_WARNING, "The low_power option is " + "not supported with this MSDK version.\n"); + q->low_power = 0; + q->param.mfx.LowPower = MFX_CODINGOPTION_OFF; #endif + } else + q->param.mfx.LowPower = MFX_CODINGOPTION_OFF; + q->param.mfx.CodecProfile = q->profile; q->param.mfx.TargetUsage = avctx->compression_level; q->param.mfx.GopPicSize = FFMAX(0, avctx->gop_size); @@ -530,7 +537,7 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) // If the minor version is greater than or equal to 19, // then can use the same alignment settings as H.264 for HEVC q->width_align = (avctx->codec_id != AV_CODEC_ID_HEVC || - QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 19)) ? 16 : 32; + QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 19)) ? 16 : 32; q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, q->width_align); if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) { @@ -570,7 +577,7 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) //libmfx BRC parameters are 16 bits thus maybe overflow, then BRCParamMultiplier is needed buffer_size_in_kilobytes = avctx->rc_buffer_size / 8000; - initial_delay_in_kilobytes = avctx->rc_initial_buffer_occupancy / 1000; + initial_delay_in_kilobytes = avctx->rc_initial_buffer_occupancy / 8000; target_bitrate_kbps = avctx->bit_rate / 1000; max_bitrate_kbps = avctx->rc_max_rate / 1000; brc_param_multiplier = (FFMAX(FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes), @@ -627,9 +634,10 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) #endif } - // the HEVC encoder plugin currently fails if coding options - // are provided - if (avctx->codec_id != AV_CODEC_ID_HEVC) { + // 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)) { q->extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION; q->extco.Header.BufferSz = sizeof(q->extco); @@ -690,7 +698,10 @@ FF_ENABLE_DEPRECATION_WARNINGS #endif #if QSV_HAVE_TRELLIS - q->extco2.Trellis = q->trellis; + if (avctx->trellis >= 0) + q->extco2.Trellis = (avctx->trellis == 0) ? MFX_TRELLIS_OFF : (MFX_TRELLIS_I | MFX_TRELLIS_P | MFX_TRELLIS_B); + else + q->extco2.Trellis = MFX_TRELLIS_UNKNOWN; #endif #if QSV_VERSION_ATLEAST(1, 8) @@ -729,7 +740,7 @@ FF_ENABLE_DEPRECATION_WARNINGS #endif #if QSV_HAVE_MF - if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) { + if (QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 25)) { q->extmfp.Header.BufferId = MFX_EXTBUFF_MULTI_FRAME_PARAM; q->extmfp.Header.BufferSz = sizeof(q->extmfp); @@ -742,6 +753,10 @@ FF_ENABLE_DEPRECATION_WARNINGS #if QSV_HAVE_CO3 q->extco3.Header.BufferId = MFX_EXTBUFF_CODING_OPTION3; q->extco3.Header.BufferSz = sizeof(q->extco3); +#if QSV_HAVE_GPB + if (avctx->codec_id == AV_CODEC_ID_HEVC) + q->extco3.GPB = q->gpb ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF; +#endif q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco3; #endif } @@ -805,22 +820,37 @@ static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q) }; #endif - mfxExtBuffer *ext_buffers[] = { - (mfxExtBuffer*)&extradata, - (mfxExtBuffer*)&co, +#if QSV_HAVE_CO_VPS + uint8_t vps_buf[128]; + mfxExtCodingOptionVPS extradata_vps = { + .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_VPS, + .Header.BufferSz = sizeof(extradata_vps), + .VPSBuffer = vps_buf, + .VPSBufSize = sizeof(vps_buf), + }; +#endif + + mfxExtBuffer *ext_buffers[2 + QSV_HAVE_CO2 + QSV_HAVE_CO3 + QSV_HAVE_CO_VPS]; + + int need_pps = avctx->codec_id != AV_CODEC_ID_MPEG2VIDEO; + int ret, ext_buf_num = 0, extradata_offset = 0; + + ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata; + ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co; #if QSV_HAVE_CO2 - (mfxExtBuffer*)&co2, + ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co2; #endif #if QSV_HAVE_CO3 - (mfxExtBuffer*)&co3, + ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co3; +#endif +#if QSV_HAVE_CO_VPS + q->hevc_vps = ((avctx->codec_id == AV_CODEC_ID_HEVC) && QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 17)); + if (q->hevc_vps) + ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata_vps; #endif - }; - - int need_pps = avctx->codec_id != AV_CODEC_ID_MPEG2VIDEO; - int ret; q->param.ExtParam = ext_buffers; - q->param.NumExtParam = FF_ARRAY_ELEMS(ext_buffers); + q->param.NumExtParam = ext_buf_num; ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param); if (ret < 0) @@ -829,20 +859,37 @@ static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q) q->packet_size = q->param.mfx.BufferSizeInKB * q->param.mfx.BRCParamMultiplier * 1000; - if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)) { + if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize) +#if QSV_HAVE_CO_VPS + || (q->hevc_vps && !extradata_vps.VPSBufSize) +#endif + ) { av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx.\n"); return AVERROR_UNKNOWN; } - avctx->extradata = av_malloc(extradata.SPSBufSize + need_pps * extradata.PPSBufSize + - AV_INPUT_BUFFER_PADDING_SIZE); + avctx->extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize; +#if QSV_HAVE_CO_VPS + avctx->extradata_size += q->hevc_vps * extradata_vps.VPSBufSize; +#endif + + avctx->extradata = av_malloc(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); if (!avctx->extradata) return AVERROR(ENOMEM); - memcpy(avctx->extradata, sps_buf, extradata.SPSBufSize); - if (need_pps) - memcpy(avctx->extradata + extradata.SPSBufSize, pps_buf, extradata.PPSBufSize); - avctx->extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize; +#if QSV_HAVE_CO_VPS + if (q->hevc_vps) { + memcpy(avctx->extradata, vps_buf, extradata_vps.VPSBufSize); + extradata_offset += extradata_vps.VPSBufSize; + } +#endif + + memcpy(avctx->extradata + extradata_offset, sps_buf, extradata.SPSBufSize); + extradata_offset += extradata.SPSBufSize; + if (need_pps) { + memcpy(avctx->extradata + extradata_offset, pps_buf, extradata.PPSBufSize); + extradata_offset += extradata.PPSBufSize; + } memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); cpb_props = ff_add_cpb_side_data(avctx); @@ -907,29 +954,31 @@ static int qsvenc_init_session(AVCodecContext *avctx, QSVEncContext *q) if (!q->frames_ctx.hw_frames_ctx) return AVERROR(ENOMEM); - ret = ff_qsv_init_session_frames(avctx, &q->internal_session, + ret = ff_qsv_init_session_frames(avctx, &q->internal_qs.session, &q->frames_ctx, q->load_plugins, - q->param.IOPattern == MFX_IOPATTERN_IN_OPAQUE_MEMORY); + q->param.IOPattern == MFX_IOPATTERN_IN_OPAQUE_MEMORY, + MFX_GPUCOPY_OFF); if (ret < 0) { av_buffer_unref(&q->frames_ctx.hw_frames_ctx); return ret; } - q->session = q->internal_session; + q->session = q->internal_qs.session; } else if (avctx->hw_device_ctx) { - ret = ff_qsv_init_session_device(avctx, &q->internal_session, - avctx->hw_device_ctx, q->load_plugins); + ret = ff_qsv_init_session_device(avctx, &q->internal_qs.session, + avctx->hw_device_ctx, q->load_plugins, + MFX_GPUCOPY_OFF); if (ret < 0) return ret; - q->session = q->internal_session; + q->session = q->internal_qs.session; } else { - ret = ff_qsv_init_internal_session(avctx, &q->internal_session, - q->load_plugins); + ret = ff_qsv_init_internal_session(avctx, &q->internal_qs, + q->load_plugins, MFX_GPUCOPY_OFF); if (ret < 0) return ret; - q->session = q->internal_session; + q->session = q->internal_qs.session; } return 0; @@ -985,6 +1034,12 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) if (ret < 0) return ret; + ret = MFXQueryVersion(q->session,&q->ver); + if (ret < 0) { + return ff_qsv_print_error(avctx, ret, + "Error querying mfx version"); + } + // in the mfxInfoMFX struct, JPEG is different from other codecs switch (avctx->codec_id) { case AV_CODEC_ID_MJPEG: @@ -1254,7 +1309,6 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, if (qsv_frame) { surf = &qsv_frame->surface; enc_ctrl = &qsv_frame->enc_ctrl; - memset(enc_ctrl, 0, sizeof(mfxEncodeCtrl)); if (frame->pict_type == AV_PICTURE_TYPE_I) { enc_ctrl->FrameType = MFX_FRAMETYPE_I | MFX_FRAMETYPE_REF; @@ -1455,10 +1509,9 @@ int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q) if (q->session) MFXVideoENCODE_Close(q->session); - if (q->internal_session) - MFXClose(q->internal_session); + q->session = NULL; - q->internal_session = NULL; + ff_qsv_close_internal_session(&q->internal_qs); av_buffer_unref(&q->frames_ctx.hw_frames_ctx); av_buffer_unref(&q->frames_ctx.mids_buf);