int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) { int opaque_alloc = 0; int ret; q->param.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY; q->param.AsyncDepth = q->async_depth; q->async_fifo = av_fifo_alloc((1 + q->async_depth) * (sizeof(AVPacket) + sizeof(mfxSyncPoint) + sizeof(mfxBitstream*))); if (!q->async_fifo) return AVERROR(ENOMEM); if (avctx->hwaccel_context) { AVQSVContext *qsv = avctx->hwaccel_context; q->session = qsv->session; q->param.IOPattern = qsv->iopattern; opaque_alloc = qsv->opaque_alloc; } if (!q->session) { ret = ff_qsv_init_internal_session(avctx, &q->internal_session, q->load_plugins); if (ret < 0) return ret; q->session = q->internal_session; } ret = init_video_param(avctx, q); if (ret < 0) return ret; ret = MFXVideoENCODE_QueryIOSurf(q->session, &q->param, &q->req); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error querying the encoding parameters\n"); return ff_qsv_error(ret); } if (opaque_alloc) { ret = qsv_init_opaque_alloc(avctx, q); if (ret < 0) return ret; } if (avctx->hwaccel_context) { AVQSVContext *qsv = avctx->hwaccel_context; int i, j; q->extparam = av_mallocz_array(qsv->nb_ext_buffers + q->nb_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 < q->nb_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 = q->nb_extparam_internal; } ret = MFXVideoENCODE_Init(q->session, &q->param); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error initializing the encoder\n"); return ff_qsv_error(ret); } ret = qsv_retrieve_enc_params(avctx, q); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error retrieving encoding parameters.\n"); return ret; } q->avctx = avctx; return 0; }
static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q) { uint8_t sps_buf[128]; uint8_t pps_buf[128]; mfxExtCodingOptionSPSPPS extradata = { .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS, .Header.BufferSz = sizeof(extradata), .SPSBuffer = sps_buf, .SPSBufSize = sizeof(sps_buf), .PPSBuffer = pps_buf, .PPSBufSize = sizeof(pps_buf) }; mfxExtBuffer *ext_buffers[] = { (mfxExtBuffer*)&extradata, }; 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); ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param); if (ret < 0) return ff_qsv_error(ret); q->packet_size = q->param.mfx.BufferSizeInKB * 1000; if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)) { 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); 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; memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); return 0; } int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) { int ret; q->param.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY; q->param.AsyncDepth = q->async_depth; q->async_fifo = av_fifo_alloc((1 + q->async_depth) * (sizeof(AVPacket) + sizeof(mfxSyncPoint) + sizeof(mfxBitstream*))); if (!q->async_fifo) return AVERROR(ENOMEM); if (avctx->hwaccel_context) { AVQSVContext *qsv = avctx->hwaccel_context; q->session = qsv->session; q->param.IOPattern = qsv->iopattern; } if (!q->session) { ret = ff_qsv_init_internal_session(avctx, &q->internal_qs, q->load_plugins); if (ret < 0) return ret; q->session = q->internal_qs.session; } ret = init_video_param(avctx, q); if (ret < 0) return ret; ret = MFXVideoENCODE_QueryIOSurf(q->session, &q->param, &q->req); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error querying the encoding parameters\n"); return ff_qsv_error(ret); } 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"); } else if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error initializing the encoder\n"); return ff_qsv_error(ret); } ret = qsv_retrieve_enc_params(avctx, q); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error retrieving encoding parameters.\n"); return ret; } q->avctx = avctx; return 0; }
static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q) { uint8_t sps_buf[128]; uint8_t pps_buf[128]; mfxExtCodingOptionSPSPPS extradata = { .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS, .Header.BufferSz = sizeof(extradata), .SPSBuffer = sps_buf, .SPSBufSize = sizeof(sps_buf), .PPSBuffer = pps_buf, .PPSBufSize = sizeof(pps_buf) }; mfxExtBuffer *ext_buffers[] = { (mfxExtBuffer*)&extradata, }; int ret; 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_error(ret); q->packet_size = q->param.mfx.BufferSizeInKB * 1000; if (!extradata.SPSBufSize || !extradata.PPSBufSize) { av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx.\n"); return AVERROR_UNKNOWN; } avctx->extradata = av_malloc(extradata.SPSBufSize + extradata.PPSBufSize + FF_INPUT_BUFFER_PADDING_SIZE); if (!avctx->extradata) return AVERROR(ENOMEM); memcpy(avctx->extradata, sps_buf, extradata.SPSBufSize); memcpy(avctx->extradata + extradata.SPSBufSize, pps_buf, extradata.PPSBufSize); avctx->extradata_size = extradata.SPSBufSize + extradata.PPSBufSize; memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE); return 0; } int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) { int ret; q->param.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY; q->param.AsyncDepth = q->async_depth; if (avctx->hwaccel_context) { AVQSVContext *qsv = avctx->hwaccel_context; q->session = qsv->session; q->param.IOPattern = qsv->iopattern; } if (!q->session) { ret = ff_qsv_init_internal_session(avctx, &q->internal_session); if (ret < 0) return ret; q->session = q->internal_session; } ret = init_video_param(avctx, q); if (ret < 0) return ret; ret = MFXVideoENCODE_QueryIOSurf(q->session, &q->param, &q->req); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error querying the encoding parameters\n"); return ff_qsv_error(ret); } ret = MFXVideoENCODE_Init(q->session, &q->param); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error initializing the encoder\n"); return ff_qsv_error(ret); } ret = qsv_retrieve_enc_params(avctx, q); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error retrieving encoding parameters.\n"); return ret; } avctx->coded_frame = av_frame_alloc(); if (!avctx->coded_frame) return AVERROR(ENOMEM); q->avctx = avctx; return 0; }