diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index 55140e1135..7189ac4080 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -160,7 +160,7 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) q->extco.PicTimingSEI = q->pic_timing_sei ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN; - q->extparam[0] = (mfxExtBuffer *)&q->extco; + q->extparam_internal[0] = (mfxExtBuffer *)&q->extco; #if QSV_VERSION_ATLEAST(1,6) q->extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2; @@ -175,11 +175,9 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) q->extco2.LookAheadDS = q->look_ahead_downsampling; #endif - q->extparam[1] = (mfxExtBuffer *)&q->extco2; + q->extparam_internal[1] = (mfxExtBuffer *)&q->extco2; #endif - q->param.ExtParam = q->extparam; - q->param.NumExtParam = FF_ARRAY_ELEMS(q->extparam); } return 0; @@ -278,6 +276,35 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) return ff_qsv_error(ret); } + if (avctx->hwaccel_context) { + AVQSVContext *qsv = avctx->hwaccel_context; + int i, j; + + q->extparam = av_mallocz_array(qsv->nb_ext_buffers + FF_ARRAY_ELEMS(q->extparam_internal), + sizeof(*q->extparam)); + if (!q->extparam) + return AVERROR(ENOMEM); + + q->param.ExtParam = q->extparam; + for (i = 0; i < qsv->nb_ext_buffers; i++) + q->param.ExtParam[i] = qsv->ext_buffers[i]; + q->param.NumExtParam = qsv->nb_ext_buffers; + + for (i = 0; i < FF_ARRAY_ELEMS(q->extparam_internal); i++) { + for (j = 0; j < qsv->nb_ext_buffers; j++) { + if (qsv->ext_buffers[j]->BufferId == q->extparam_internal[i]->BufferId) + break; + } + if (j < qsv->nb_ext_buffers) + continue; + + q->param.ExtParam[q->param.NumExtParam++] = q->extparam_internal[i]; + } + } else { + q->param.ExtParam = q->extparam_internal; + q->param.NumExtParam = FF_ARRAY_ELEMS(q->extparam_internal); + } + ret = MFXVideoENCODE_Init(q->session, &q->param); if (MFX_WRN_PARTIAL_ACCELERATION==ret) { av_log(avctx, AV_LOG_WARNING, "Encoder will work with partial HW acceleration\n"); @@ -574,5 +601,7 @@ int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q) av_fifo_free(q->async_fifo); q->async_fifo = NULL; + av_freep(&q->extparam); + return 0; } diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h index 2a21f8217d..13f1419a76 100644 --- a/libavcodec/qsvenc.h +++ b/libavcodec/qsvenc.h @@ -52,10 +52,11 @@ typedef struct QSVEncContext { mfxExtCodingOption extco; #if QSV_VERSION_ATLEAST(1,6) mfxExtCodingOption2 extco2; - mfxExtBuffer *extparam[2]; + mfxExtBuffer *extparam_internal[2]; #else - mfxExtBuffer *extparam[1]; + mfxExtBuffer *extparam_internal[1]; #endif + mfxExtBuffer **extparam; AVFifoBuffer *async_fifo;