25 #include <sys/types.h>
53 { MFX_PROFILE_AVC_BASELINE,
"avc baseline" },
54 { MFX_PROFILE_AVC_MAIN,
"avc main" },
55 { MFX_PROFILE_AVC_EXTENDED,
"avc extended" },
56 { MFX_PROFILE_AVC_HIGH,
"avc high" },
57 { MFX_PROFILE_AVC_HIGH_422,
"avc high 422" },
58 { MFX_PROFILE_AVC_CONSTRAINED_BASELINE,
"avc constrained baseline" },
59 { MFX_PROFILE_AVC_CONSTRAINED_HIGH,
"avc constrained high" },
60 { MFX_PROFILE_AVC_PROGRESSIVE_HIGH,
"avc progressive high" },
64 { MFX_PROFILE_MPEG2_SIMPLE,
"mpeg2 simple" },
65 { MFX_PROFILE_MPEG2_MAIN,
"mpeg2 main" },
66 { MFX_PROFILE_MPEG2_HIGH,
"mpeg2 high" },
70 { MFX_PROFILE_HEVC_MAIN,
"hevc main" },
71 { MFX_PROFILE_HEVC_MAIN10,
"hevc main10" },
72 { MFX_PROFILE_HEVC_MAINSP,
"hevc mainsp" },
73 { MFX_PROFILE_HEVC_REXT,
"hevc rext" },
74 #if QSV_VERSION_ATLEAST(1, 32)
75 { MFX_PROFILE_HEVC_SCC,
"hevc scc" },
80 { MFX_PROFILE_VP9_0,
"vp9 0" },
81 { MFX_PROFILE_VP9_1,
"vp9 1" },
82 { MFX_PROFILE_VP9_2,
"vp9 2" },
83 { MFX_PROFILE_VP9_3,
"vp9 3" },
87 #if QSV_VERSION_ATLEAST(1, 34)
88 { MFX_PROFILE_AV1_MAIN,
"av1 main" },
89 { MFX_PROFILE_AV1_HIGH,
"av1 high" },
90 { MFX_PROFILE_AV1_PRO,
"av1 professional" },
135 for (
i = 0;
i < num_profiles;
i++)
142 static const struct {
146 { MFX_RATECONTROL_CBR,
"CBR" },
147 { MFX_RATECONTROL_VBR,
"VBR" },
148 { MFX_RATECONTROL_CQP,
"CQP" },
150 { MFX_RATECONTROL_AVBR,
"AVBR" },
152 { MFX_RATECONTROL_LA,
"LA" },
153 { MFX_RATECONTROL_ICQ,
"ICQ" },
154 { MFX_RATECONTROL_LA_ICQ,
"LA_ICQ" },
156 { MFX_RATECONTROL_VCM,
"VCM" },
159 { MFX_RATECONTROL_LA_EXT,
"LA_EXT" },
161 { MFX_RATECONTROL_LA_HRD,
"LA_HRD" },
162 { MFX_RATECONTROL_QVBR,
"QVBR" },
165 #define UPDATE_PARAM(a, b) \
173 #define MFX_IMPL_VIA_MASK(impl) (0x0f00 & (impl))
186 if (
val == MFX_CODINGOPTION_ON)
188 else if (
val == MFX_CODINGOPTION_OFF)
194 mfxExtBuffer **coding_opts)
199 mfxExtCodingOption *co = (mfxExtCodingOption*)coding_opts[1];
200 mfxExtCodingOption2 *co2 =
NULL;
201 mfxExtCodingOption3 *co3 =
NULL;
202 mfxExtHEVCTiles *exthevctiles =
NULL;
204 mfxExtHyperModeParam *exthypermodeparam =
NULL;
207 const char *tmp_str =
NULL;
210 co2 = (mfxExtCodingOption2*)coding_opts[q->
co2_idx];
213 co3 = (mfxExtCodingOption3*)coding_opts[q->
co3_idx];
227 "GopPicSize: %"PRIu16
"; GopRefDist: %"PRIu16
"; GopOptFlag:%s%s; IdrInterval: %"PRIu16
"\n",
229 info->GopOptFlag & MFX_GOP_CLOSED ?
" closed" :
"",
230 info->GopOptFlag & MFX_GOP_STRICT ?
" strict" :
"",
236 if (
info->RateControlMethod == MFX_RATECONTROL_CBR ||
237 info->RateControlMethod == MFX_RATECONTROL_VBR
239 ||
info->RateControlMethod == MFX_RATECONTROL_VCM
243 "BufferSizeInKB: %"PRIu16
"; InitialDelayInKB: %"PRIu16
"; TargetKbps: %"PRIu16
"; MaxKbps: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
244 info->BufferSizeInKB,
info->InitialDelayInKB,
info->TargetKbps,
info->MaxKbps,
info->BRCParamMultiplier);
245 }
else if (
info->RateControlMethod == MFX_RATECONTROL_CQP) {
250 else if (
info->RateControlMethod == MFX_RATECONTROL_AVBR) {
252 "TargetKbps: %"PRIu16
"; Accuracy: %"PRIu16
"; Convergence: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
253 info->TargetKbps,
info->Accuracy,
info->Convergence,
info->BRCParamMultiplier);
256 else if (
info->RateControlMethod == MFX_RATECONTROL_LA
257 ||
info->RateControlMethod == MFX_RATECONTROL_LA_HRD
260 "TargetKbps: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
261 info->TargetKbps,
info->BRCParamMultiplier);
262 }
else if (
info->RateControlMethod == MFX_RATECONTROL_ICQ ||
263 info->RateControlMethod == MFX_RATECONTROL_LA_ICQ)
275 co->CAVLC == MFX_CODINGOPTION_ON ?
"CAVLC" :
"CABAC", co->MaxDecFrameBuffering);
277 "NalHrdConformance: %s; SingleSeiNalUnit: %s; VuiVclHrdParameters: %s VuiNalHrdParameters: %s\n",
282 "NalHrdConformance: %s; VuiNalHrdParameters: %s\n",
287 info->FrameInfo.FrameRateExtD,
info->FrameInfo.FrameRateExtN);
291 (
info->RateControlMethod == MFX_RATECONTROL_LA) ||
292 (
info->RateControlMethod == MFX_RATECONTROL_LA_HRD) ||
293 (
info->RateControlMethod == MFX_RATECONTROL_LA_ICQ))
296 av_log(avctx,
AV_LOG_VERBOSE,
"IntRefType: %"PRIu16
"; IntRefCycleSize: %"PRIu16
"; IntRefQPDelta: %"PRId16
"\n",
297 co2->IntRefType, co2->IntRefCycleSize, co2->IntRefQPDelta);
300 co2->MaxFrameSize, co2->MaxSliceSize);
303 "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
307 if (co2->Trellis & MFX_TRELLIS_OFF) {
309 }
else if (!co2->Trellis) {
312 char trellis_type[4];
314 if (co2->Trellis & MFX_TRELLIS_I) trellis_type[
i++] =
'I';
315 if (co2->Trellis & MFX_TRELLIS_P) trellis_type[
i++] =
'P';
316 if (co2->Trellis & MFX_TRELLIS_B) trellis_type[
i++] =
'B';
321 switch (co2->LookAheadDS) {
322 case MFX_LOOKAHEAD_DS_OFF: tmp_str =
"off";
break;
323 case MFX_LOOKAHEAD_DS_2x: tmp_str =
"2x";
break;
324 case MFX_LOOKAHEAD_DS_4x: tmp_str =
"4x";
break;
325 default: tmp_str =
"unknown";
break;
328 "RepeatPPS: %s; NumMbPerSlice: %"PRIu16
"; LookAheadDS: %s\n",
331 switch (co2->BRefType) {
332 case MFX_B_REF_OFF: tmp_str =
"off";
break;
333 case MFX_B_REF_PYRAMID: tmp_str =
"pyramid";
break;
334 default: tmp_str =
"auto";
break;
337 "AdaptiveI: %s; AdaptiveB: %s; BRefType:%s\n",
341 "MinQPI: %"PRIu8
"; MaxQPI: %"PRIu8
"; MinQPP: %"PRIu8
"; MaxQPP: %"PRIu8
"; MinQPB: %"PRIu8
"; MaxQPB: %"PRIu8
"\n",
342 co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
345 switch (co2->SkipFrame) {
346 case MFX_SKIPFRAME_NO_SKIP:
349 case MFX_SKIPFRAME_INSERT_DUMMY:
352 case MFX_SKIPFRAME_INSERT_NOTHING:
355 case MFX_SKIPFRAME_BRC_ONLY:
363 if (
info->RateControlMethod == MFX_RATECONTROL_QVBR)
366 switch (co3->PRefType) {
386 exthevctiles->NumTileColumns, exthevctiles->NumTileRows);
390 if (exthypermodeparam) {
393 if (exthypermodeparam->Mode == MFX_HYPERMODE_OFF)
395 if (exthypermodeparam->Mode == MFX_HYPERMODE_ON)
397 if (exthypermodeparam->Mode == MFX_HYPERMODE_ADAPTIVE)
406 mfxExtBuffer **coding_opts)
409 mfxExtVP9Param *vp9_param =
NULL;
410 mfxExtCodingOption2 *co2 =
NULL;
413 vp9_param = (mfxExtVP9Param *)coding_opts[q->
vp9_idx];
416 co2 = (mfxExtCodingOption2*)coding_opts[q->
co2_idx];
422 "GopPicSize: %"PRIu16
"; GopRefDist: %"PRIu16
"; GopOptFlag:%s%s; IdrInterval: %"PRIu16
"\n",
424 info->GopOptFlag & MFX_GOP_CLOSED ?
" closed" :
"",
425 info->GopOptFlag & MFX_GOP_STRICT ?
" strict" :
"",
431 if (
info->RateControlMethod == MFX_RATECONTROL_CBR ||
432 info->RateControlMethod == MFX_RATECONTROL_VBR) {
434 "BufferSizeInKB: %"PRIu16
"; InitialDelayInKB: %"PRIu16
"; TargetKbps: %"PRIu16
"; MaxKbps: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
435 info->BufferSizeInKB,
info->InitialDelayInKB,
info->TargetKbps,
info->MaxKbps,
info->BRCParamMultiplier);
436 }
else if (
info->RateControlMethod == MFX_RATECONTROL_CQP) {
440 else if (
info->RateControlMethod == MFX_RATECONTROL_ICQ) {
449 info->FrameInfo.FrameRateExtD,
info->FrameInfo.FrameRateExtN);
453 "IntRefType: %"PRIu16
"; IntRefCycleSize: %"PRIu16
"; IntRefQPDelta: %"PRId16
"\n",
454 co2->IntRefType, co2->IntRefCycleSize, co2->IntRefQPDelta);
459 "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
466 "MinQPI: %"PRIu8
"; MaxQPI: %"PRIu8
"; MinQPP: %"PRIu8
"; MaxQPP: %"PRIu8
"; MinQPB: %"PRIu8
"; MaxQPB: %"PRIu8
"\n",
467 co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
485 info->FrameInfo.FrameRateExtD,
info->FrameInfo.FrameRateExtN);
488 #if QSV_HAVE_EXT_AV1_PARAM
490 mfxExtBuffer **coding_opts)
493 mfxExtAV1TileParam *av1_tile_param = (mfxExtAV1TileParam *)coding_opts[0];
494 mfxExtAV1BitstreamParam *av1_bs_param = (mfxExtAV1BitstreamParam *)coding_opts[1];
495 mfxExtCodingOption2 *co2 = (mfxExtCodingOption2*)coding_opts[2];
496 mfxExtCodingOption3 *co3 = (mfxExtCodingOption3*)coding_opts[3];
502 "GopPicSize: %"PRIu16
"; GopRefDist: %"PRIu16
"; GopOptFlag:%s%s; IdrInterval: %"PRIu16
"\n",
504 info->GopOptFlag & MFX_GOP_CLOSED ?
" closed" :
"",
505 info->GopOptFlag & MFX_GOP_STRICT ?
" strict" :
"",
511 if (
info->RateControlMethod == MFX_RATECONTROL_CBR ||
512 info->RateControlMethod == MFX_RATECONTROL_VBR)
514 "BufferSizeInKB: %"PRIu16
"; InitialDelayInKB: %"PRIu16
"; TargetKbps: %"PRIu16
"; MaxKbps: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
515 info->BufferSizeInKB,
info->InitialDelayInKB,
info->TargetKbps,
info->MaxKbps,
info->BRCParamMultiplier);
516 else if (
info->RateControlMethod == MFX_RATECONTROL_CQP)
519 else if (
info->RateControlMethod == MFX_RATECONTROL_ICQ)
527 "IntRefType: %"PRIu16
"; IntRefCycleSize: %"PRIu16
528 "; IntRefQPDelta: %"PRId16
"; IntRefCycleDist: %"PRId16
"\n",
529 co2->IntRefType, co2->IntRefCycleSize,
530 co2->IntRefQPDelta, co3->IntRefCycleDist);
535 "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
541 switch (co2->BRefType) {
547 switch (co3->PRefType) {
555 "MinQPI: %"PRIu8
"; MaxQPI: %"PRIu8
"; MinQPP: %"PRIu8
"; MaxQPP: %"PRIu8
"; MinQPB: %"PRIu8
"; MaxQPB: %"PRIu8
"\n",
556 co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
559 info->FrameInfo.FrameRateExtD,
info->FrameInfo.FrameRateExtN);
562 "NumTileRows: %"PRIu16
"; NumTileColumns: %"PRIu16
"; NumTileGroups: %"PRIu16
"\n",
563 av1_tile_param->NumTileRows, av1_tile_param->NumTileColumns, av1_tile_param->NumTileGroups);
579 int want_vcm = q->
vcm;
583 "VCM ratecontrol mode requested, but is not supported by this SDK version\n");
587 if (want_la + want_qscale + want_vcm > 1) {
589 "More than one of: { constant qscale, lookahead, VCM } requested, "
590 "only one of them can be used at a time.\n");
596 rc_desc =
"constant quantization parameter (CQP)";
601 rc_desc =
"video conferencing mode (VCM)";
605 "setting bitrate. Please use the b option to set the desired "
606 "bitrate.\n", rc_desc);
613 rc_desc =
"VBR with lookahead (LA)";
616 rc_mode = MFX_RATECONTROL_LA_ICQ;
617 rc_desc =
"intelligent constant quality with lookahead (LA_ICQ)";
620 "setting bitrate. Please use the b option to set the desired "
621 "bitrate.\n", rc_desc);
627 rc_desc =
"intelligent constant quality (ICQ)";
632 rc_desc =
"constant bitrate (CBR)";
639 rc_mode = MFX_RATECONTROL_AVBR;
640 rc_desc =
"average variable bitrate (AVBR)";
644 rc_mode = MFX_RATECONTROL_QVBR;
645 rc_desc =
"constant quality with VBR algorithm (QVBR)";
648 rc_desc =
"variable bitrate (VBR)";
652 rc_desc =
"constant quantization parameter (CQP)";
658 "parameter (CQP) by default. Please use the global_quality "
659 "option and other options for a quality-based mode or the b "
660 "option and other options for a bitrate-based mode if the "
661 "default is not the desired choice.\n");
672 mfxVideoParam param_out = { .mfx.CodecId = q->
param.mfx.CodecId };
675 #define UNMATCH(x) (param_out.mfx.x != q->param.mfx.x)
684 if (
UNMATCH(RateControlMethod))
719 if (avctx->
level > 0)
731 q->
param.mfx.FrameInfo.CropX = 0;
732 q->
param.mfx.FrameInfo.CropY = 0;
737 q->
param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420 +
738 !
desc->log2_chroma_w + !
desc->log2_chroma_h;
739 q->
param.mfx.FrameInfo.BitDepthLuma =
desc->comp[0].depth;
740 q->
param.mfx.FrameInfo.BitDepthChroma =
desc->comp[0].depth;
748 mfxFrameInfo *
info = frames_hwctx->nb_surfaces ? &frames_hwctx->surfaces[0].Info : frames_hwctx->info;
749 q->
param.mfx.FrameInfo.Width =
info->Width;
750 q->
param.mfx.FrameInfo.Height =
info->Height;
761 q->
param.mfx.Interleaved = 1;
763 q->
param.mfx.RestartInterval = 0;
780 int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier;
781 int buffer_size_in_kilobytes, initial_delay_in_kilobytes;
789 if (avctx->
level > 0) {
800 "valid range is 0-%d, using %d instead\n",
801 MFX_TARGETUSAGE_BEST_SPEED, MFX_TARGETUSAGE_BEST_SPEED);
807 q->
param.mfx.LowPower = MFX_CODINGOPTION_ON;
809 q->
param.mfx.LowPower = MFX_CODINGOPTION_UNKNOWN;
811 q->
param.mfx.LowPower = MFX_CODINGOPTION_OFF;
824 q->
param.mfx.EncodedOrder = 0;
825 q->
param.mfx.BufferSizeInKB = 0;
835 q->
param.mfx.FrameInfo.CropX = 0;
836 q->
param.mfx.FrameInfo.CropY = 0;
841 q->
param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420 +
842 !
desc->log2_chroma_w + !
desc->log2_chroma_h;
843 q->
param.mfx.FrameInfo.BitDepthLuma =
desc->comp[0].depth;
844 q->
param.mfx.FrameInfo.BitDepthChroma =
desc->comp[0].depth;
856 q->
param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
860 q->
param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
873 mfxFrameInfo *
info = frames_hwctx->nb_surfaces ? &frames_hwctx->surfaces[0].Info : frames_hwctx->info;
874 q->
param.mfx.FrameInfo.Width =
info->Width;
875 q->
param.mfx.FrameInfo.Height =
info->Height;
894 target_bitrate_kbps = avctx->
bit_rate / 1000;
896 brc_param_multiplier = (
FFMAX(
FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
897 initial_delay_in_kilobytes) + 0x10000) / 0x10000;
903 switch (q->
param.mfx.RateControlMethod) {
904 case MFX_RATECONTROL_CBR:
905 case MFX_RATECONTROL_VBR:
910 case MFX_RATECONTROL_VCM:
912 case MFX_RATECONTROL_QVBR:
913 q->
param.mfx.BufferSizeInKB = buffer_size_in_kilobytes / brc_param_multiplier;
914 q->
param.mfx.InitialDelayInKB = initial_delay_in_kilobytes / brc_param_multiplier;
915 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
916 q->
param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier;
917 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
918 if (q->
param.mfx.RateControlMethod == MFX_RATECONTROL_QVBR)
921 case MFX_RATECONTROL_CQP:
940 case MFX_RATECONTROL_AVBR:
941 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
944 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
947 case MFX_RATECONTROL_LA:
948 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
950 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
952 case MFX_RATECONTROL_LA_ICQ:
954 case MFX_RATECONTROL_ICQ:
964 q->
extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
968 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
972 q->
extco.RateDistortionOpt = q->
rdo > 0 ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
975 q->
extco.CAVLC = q->
cavlc ? MFX_CODINGOPTION_ON
976 : MFX_CODINGOPTION_UNKNOWN;
980 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
987 q->
extco.AUDelimiter = q->
aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
991 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
996 q->
extco.AUDelimiter = q->
aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1006 q->
extco2.Trellis = (avctx->
trellis == 0) ? MFX_TRELLIS_OFF : (MFX_TRELLIS_I | MFX_TRELLIS_P | MFX_TRELLIS_B);
1008 q->
extco2.Trellis = MFX_TRELLIS_UNKNOWN;
1011 q->
extco2.RepeatPPS = q->
repeat_pps ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1016 q->
extco2.ExtBRC = q->
extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1036 q->
extco2.AdaptiveI = q->
adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1038 q->
extco2.AdaptiveB = q->
adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1044 "qmin and or qmax are set but invalid,"
1045 " please make sure min <= max\n");
1048 if (avctx->
qmin >= 0) {
1053 if (avctx->
qmax >= 0) {
1077 q->
extco2.MBBRC = q->
mbbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1081 q->
extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
1087 q->
extco2.ExtBRC = q->
extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1091 q->
extco2.AdaptiveI = q->
adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1093 q->
extco2.AdaptiveB = q->
adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1097 q->
extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
1106 q->
extmfp.Header.BufferId = MFX_EXTBUFF_MULTI_FRAME_PARAM;
1115 q->
extco3.Header.BufferId = MFX_EXTBUFF_CODING_OPTION3;
1122 q->
extco3.PRefType = MFX_P_REF_DEFAULT;
1125 q->
extco3.PRefType = MFX_P_REF_SIMPLE;
1128 q->
extco3.PRefType = MFX_P_REF_PYRAMID;
1131 q->
extco3.PRefType = MFX_P_REF_DEFAULT;
1133 "invalid p_strategy, set to default\n");
1136 if (q->
extco3.PRefType == MFX_P_REF_PYRAMID &&
1139 "Please set max_b_frames(-bf) to 0 to enable P-pyramid\n");
1152 (q->
profile == MFX_PROFILE_HEVC_REXT ||
1153 q->
profile == MFX_PROFILE_UNKNOWN))
1154 q->
extco3.TargetChromaFormatPlus1 = MFX_CHROMAFORMAT_YUV444 + 1;
1166 MFX_CODINGOPTION_OFF;
1168 q->
extco3.TransformSkip = MFX_CODINGOPTION_UNKNOWN;
1169 q->
extco3.GPB = q->
gpb ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1175 q->
extvp9param.Header.BufferId = MFX_EXTBUFF_VP9_PARAM;
1177 q->
extvp9param.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
1178 #if QSV_HAVE_EXT_VP9_TILES
1185 #if QSV_HAVE_EXT_AV1_PARAM
1188 q->extav1tileparam.Header.BufferId = MFX_EXTBUFF_AV1_TILE_PARAM;
1189 q->extav1tileparam.Header.BufferSz =
sizeof(q->extav1tileparam);
1190 q->extav1tileparam.NumTileColumns = q->
tile_cols;
1191 q->extav1tileparam.NumTileRows = q->
tile_rows;
1194 q->extav1bsparam.Header.BufferId = MFX_EXTBUFF_AV1_BITSTREAM_PARAM;
1195 q->extav1bsparam.Header.BufferSz =
sizeof(q->extav1bsparam);
1196 q->extav1bsparam.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
1200 "This version of runtime doesn't support AV1 encoding\n");
1207 q->
exthevctiles.Header.BufferId = MFX_EXTBUFF_HEVC_TILES;
1215 q->
extvsi.ColourDescriptionPresent = 0;
1220 q->
extvsi.ColourDescriptionPresent = 1;
1232 q->
extvsi.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO;
1241 MFXQueryIMPL(q->
session, &impl);
1247 if (q->
param.mfx.LowPower != MFX_CODINGOPTION_ON) {
1248 av_log(avctx,
AV_LOG_ERROR,
"Dual GFX mode supports only low-power encoding mode \n");
1251 if (q->
param.mfx.CodecId != MFX_CODEC_AVC && q->
param.mfx.CodecId != MFX_CODEC_HEVC) {
1253 "Supported: h264_qsv and hevc_qsv \n");
1256 if (q->
param.mfx.RateControlMethod != MFX_RATECONTROL_VBR &&
1257 q->
param.mfx.RateControlMethod != MFX_RATECONTROL_CQP &&
1258 q->
param.mfx.RateControlMethod != MFX_RATECONTROL_ICQ) {
1260 "Supported: VBR, CQP and ICQ \n");
1262 if ((q->
param.mfx.CodecId == MFX_CODEC_AVC && q->
param.mfx.IdrInterval != 0) ||
1263 (q->
param.mfx.CodecId == MFX_CODEC_HEVC && q->
param.mfx.IdrInterval != 1)) {
1264 av_log(avctx,
AV_LOG_WARNING,
"Dual GFX mode requires closed GOP for AVC and strict GOP for HEVC, -idr_interval 0 \n");
1266 if (q->
param.mfx.GopPicSize < 30) {
1267 av_log(avctx,
AV_LOG_WARNING,
"For better performance in dual GFX mode GopPicSize must be >= 30 \n");
1269 if (q->
param.AsyncDepth < 30) {
1270 av_log(avctx,
AV_LOG_WARNING,
"For better performance in dual GFX mode AsyncDepth must be >= 30 \n");
1273 q->exthypermodeparam.Header.BufferId = MFX_EXTBUFF_HYPER_MODE_PARAM;
1274 q->exthypermodeparam.Header.BufferSz =
sizeof(q->exthypermodeparam);
1275 q->exthypermodeparam.Mode = q->
dual_gfx;
1279 "This version of runtime doesn't support Hyper Encode\n");
1287 "some encoding parameters are not supported by the QSV "
1288 "runtime. Please double check the input parameters.\n");
1302 "Error calling GetVideoParam");
1318 mfxExtVP9Param vp9_extend_buf = {
1319 .Header.BufferId = MFX_EXTBUFF_VP9_PARAM,
1320 .Header.BufferSz =
sizeof(vp9_extend_buf),
1323 mfxExtCodingOption2 co2 = {
1324 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1325 .Header.BufferSz =
sizeof(co2),
1328 mfxExtCodingOption3 co3 = {
1329 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1330 .Header.BufferSz =
sizeof(co3),
1333 mfxExtBuffer *ext_buffers[3];
1334 int ext_buf_num = 0;
1341 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co2;
1346 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co3;
1351 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&vp9_extend_buf;
1354 q->
param.ExtParam = ext_buffers;
1355 q->
param.NumExtParam = ext_buf_num;
1360 "Error calling GetVideoParam");
1371 #if QSV_HAVE_EXT_AV1_PARAM
1373 mfxExtAV1TileParam av1_extend_tile_buf = {
1374 .Header.BufferId = MFX_EXTBUFF_AV1_TILE_PARAM,
1375 .Header.BufferSz =
sizeof(av1_extend_tile_buf),
1377 mfxExtAV1BitstreamParam av1_bs_param = {
1378 .Header.BufferId = MFX_EXTBUFF_AV1_BITSTREAM_PARAM,
1379 .Header.BufferSz =
sizeof(av1_bs_param),
1382 mfxExtCodingOption2 co2 = {
1383 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1384 .Header.BufferSz =
sizeof(co2),
1387 mfxExtCodingOption3 co3 = {
1388 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1389 .Header.BufferSz =
sizeof(co3),
1392 mfxExtBuffer *ext_buffers[] = {
1393 (mfxExtBuffer*)&av1_extend_tile_buf,
1394 (mfxExtBuffer*)&av1_bs_param,
1395 (mfxExtBuffer*)&co2,
1396 (mfxExtBuffer*)&co3,
1401 "This version of runtime doesn't support AV1 encoding\n");
1405 q->
param.ExtParam = ext_buffers;
1411 "Error calling GetVideoParam");
1414 dump_video_av1_param(avctx, q, ext_buffers);
1423 uint8_t sps_buf[512];
1424 uint8_t pps_buf[128];
1426 mfxExtCodingOptionSPSPPS extradata = {
1427 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS,
1428 .Header.BufferSz =
sizeof(extradata),
1429 .SPSBuffer = sps_buf, .SPSBufSize =
sizeof(sps_buf),
1430 .PPSBuffer = pps_buf, .PPSBufSize =
sizeof(pps_buf)
1433 mfxExtCodingOption co = {
1434 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION,
1435 .Header.BufferSz =
sizeof(co),
1437 mfxExtCodingOption2 co2 = {
1438 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1439 .Header.BufferSz =
sizeof(co2),
1441 mfxExtCodingOption3 co3 = {
1442 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1443 .Header.BufferSz =
sizeof(co3),
1446 uint8_t vps_buf[128];
1447 mfxExtCodingOptionVPS extradata_vps = {
1448 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_VPS,
1449 .Header.BufferSz =
sizeof(extradata_vps),
1450 .VPSBuffer = vps_buf,
1451 .VPSBufSize =
sizeof(vps_buf),
1454 mfxExtHEVCTiles hevc_tile_buf = {
1455 .Header.BufferId = MFX_EXTBUFF_HEVC_TILES,
1456 .Header.BufferSz =
sizeof(hevc_tile_buf),
1460 mfxExtHyperModeParam hyper_mode_param_buf = {
1461 .Header.BufferId = MFX_EXTBUFF_HYPER_MODE_PARAM,
1462 .Header.BufferSz =
sizeof(hyper_mode_param_buf),
1469 int ret, ext_buf_num = 0, extradata_offset = 0;
1472 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata;
1473 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co;
1478 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co2;
1483 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co3;
1488 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata_vps;
1491 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&hevc_tile_buf;
1496 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&hyper_mode_param_buf;
1500 q->
param.ExtParam = ext_buffers;
1501 q->
param.NumExtParam = ext_buf_num;
1506 "Error calling GetVideoParam");
1510 if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)
1511 || (q->
hevc_vps && !extradata_vps.VPSBufSize)
1517 avctx->
extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize;
1525 memcpy(avctx->
extradata, vps_buf, extradata_vps.VPSBufSize);
1526 extradata_offset += extradata_vps.VPSBufSize;
1529 memcpy(avctx->
extradata + extradata_offset, sps_buf, extradata.SPSBufSize);
1530 extradata_offset += extradata.SPSBufSize;
1532 memcpy(avctx->
extradata + extradata_offset, pps_buf, extradata.PPSBufSize);
1533 extradata_offset += extradata.PPSBufSize;
1554 mfxFrameSurface1 *surfaces;
1568 for (
i = 0;
i < nb_surfaces;
i++) {
1569 surfaces[
i].Info = q->
req.Info;
1573 q->
opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
1604 q->
param.IOPattern == MFX_IOPATTERN_IN_OPAQUE_MEMORY,
1638 int opaque_alloc = 0;
1641 #if HAVE_STRUCT_MFXCONFIGINTERFACE
1642 mfxExtBuffer ext_buf;
1643 mfxConfigInterface *iface =
NULL;
1666 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
1667 iopattern = MFX_IOPATTERN_IN_OPAQUE_MEMORY;
1668 else if (frames_hwctx->frame_type &
1669 (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
1670 iopattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1672 if (frames_hwctx->frame_type &
1673 (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
1674 iopattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1680 iopattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
1681 q->
param.IOPattern = iopattern;
1691 "Error querying mfx version");
1740 #if HAVE_STRUCT_MFXCONFIGINTERFACE
1741 ret = MFXVideoCORE_GetHandle(q->
session, MFX_HANDLE_CONFIG_INTERFACE, (mfxHDL *)(&iface));
1744 "Error getting mfx config interface handle");
1747 const char *param_key = param->
key;
1748 const char *param_value = param->
value;
1749 mfxExtBuffer *new_ext_buf;
1755 for (
int i = 0;
i < 2;
i++) {
1756 ret = iface->SetParameter(iface, (mfxU8*)param_key, (mfxU8*)param_value, MFX_STRUCTURE_TYPE_VIDEO_PARAM, &q->
param, &ext_buf);
1757 if (
ret == MFX_ERR_NONE) {
1759 }
else if (
i == 0 &&
ret == MFX_ERR_MORE_EXTBUFFER) {
1770 new_ext_buf = (mfxExtBuffer*)
av_mallocz(ext_buf.BufferSz);
1774 new_ext_buf->BufferId = ext_buf.BufferId;
1775 new_ext_buf->BufferSz = ext_buf.BufferSz;
1793 if (
ret == MFX_WRN_PARTIAL_ACCELERATION) {
1795 }
else if (
ret < 0) {
1797 "Error querying encoder params");
1803 "Error querying (IOSurf) the encoding parameters");
1812 "however libmfx %d.%d doesn't support OPAQUE memory.\n",
1813 q->
ver.Major, q->
ver.Minor);
1821 "Error initializing the encoder");
1824 "Warning in encoder initialization");
1859 enc_ctrl->NumPayload = 0;
1860 enc_ctrl->NumExtParam = 0;
1898 last = &
frame->next;
1906 if (!
frame->frame) {
1923 int max_step[4], filled[4] = { 0 };
1929 for (
int i = 0;
i <
desc->nb_components;
i++) {
1931 int sheight, dheight, plane =
comp->plane;
1939 if (swidth < 0 || dwidth < 0) {
1947 sheight =
frame->height;
1956 if (new_w >
frame->width) {
1957 for (
int j = 0; j < sheight; j++) {
1958 void *line_ptr =
frame->data[plane] + j *
frame->linesize[plane] + swidth;
1962 new_w -
frame->width);
1967 for (
int j = sheight; j < dheight; j++)
1968 memcpy(
frame->data[plane] + j *
frame->linesize[plane],
1969 frame->data[plane] + (sheight - 1) *
frame->linesize[plane],
1983 switch (
frame->format) {
1999 frame->linesize[1] = 0;
2000 total_size =
frame->linesize[0] *
frame->height;
2009 frame->linesize[1] = 0;
2010 total_size =
frame->linesize[0] *
frame->height;
2101 MFX_PICSTRUCT_FIELD_BFF;
2102 if (
frame->repeat_pict == 1)
2103 qf->
surface.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
2104 else if (
frame->repeat_pict == 2)
2105 qf->
surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
2106 else if (
frame->repeat_pict == 4)
2107 qf->
surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
2124 if (q->
param.mfx.CodecId == MFX_CODEC_AVC) {
2125 if (q->
param.mfx.CodecProfile == MFX_PROFILE_AVC_BASELINE ||
2126 q->
param.mfx.CodecLevel < MFX_LEVEL_AVC_21 ||
2127 q->
param.mfx.CodecLevel > MFX_LEVEL_AVC_41)
2129 "Interlaced coding is supported"
2130 " at Main/High Profile Level 2.2-4.0\n");
2135 mfxEncodeCtrl *enc_ctrl)
2151 mfxExtEncoderROI *enc_roi =
NULL;
2158 if (!roi_size || sd->
size % roi_size) {
2162 nb_roi = sd->
size / roi_size;
2165 "supported by driver (%d > %d).\n",
2173 enc_roi->Header.BufferId = MFX_EXTBUFF_ENCODER_ROI;
2174 enc_roi->Header.BufferSz =
sizeof(*enc_roi);
2175 enc_roi->NumROI = nb_roi;
2176 enc_roi->ROIMode = MFX_ROI_MODE_QP_DELTA;
2177 for (
i = 0;
i < nb_roi;
i++) {
2183 enc_roi->ROI[
i].DeltaQP =
2187 enc_roi->ROI[
i].DeltaQP);
2189 enc_ctrl->ExtParam[enc_ctrl->NumExtParam] = (mfxExtBuffer *)enc_roi;
2190 enc_ctrl->NumExtParam++;
2196 mfxEncodeCtrl *enc_ctrl)
2199 if (!
frame->metadata)
2202 if (!skip_frame_dict)
2204 enc_ctrl->SkipFrame = strtol(skip_frame_dict->
value,
NULL, 10);
2210 int updated = 0, new_qp = 0;
2215 if (q->
param.mfx.RateControlMethod == MFX_RATECONTROL_CQP) {
2231 "Reset qp = %d/%d/%d for idr/p/b frames\n",
2250 "Reset MaxFrameSize: %d;\n", q->
extco2.MaxFrameSize);
2264 q->
param.mfx.GopPicSize);
2285 q->
extco2.IntRefQPDelta =
2289 "Reset IntRefType: %d; IntRefCycleSize: %d; "
2290 "IntRefQPDelta: %d; IntRefCycleDist: %d\n",
2320 "qmin and or qmax are set but invalid,"
2321 " please make sure min <= max\n");
2331 if (avctx->
qmin >= 0) {
2335 if (avctx->
qmax >= 0) {
2353 "MinQPP: %d; MaxQPP: %d; "
2354 "MinQPB: %d; MaxQPB: %d\n",
2375 q->
extco3.LowDelayBRC = MFX_CODINGOPTION_UNKNOWN;
2401 q->
param.mfx.FrameInfo.FrameRateExtN,
2402 q->
param.mfx.FrameInfo.FrameRateExtD,
2403 (
double)q->
param.mfx.FrameInfo.FrameRateExtN / q->
param.mfx.FrameInfo.FrameRateExtD);
2411 int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier;
2412 int buffer_size_in_kilobytes, initial_delay_in_kilobytes;
2423 target_bitrate_kbps = avctx->
bit_rate / 1000;
2425 brc_param_multiplier = (
FFMAX(
FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
2426 initial_delay_in_kilobytes) + 0x10000) / 0x10000;
2428 q->
param.mfx.BufferSizeInKB = buffer_size_in_kilobytes / brc_param_multiplier;
2429 q->
param.mfx.InitialDelayInKB = initial_delay_in_kilobytes / brc_param_multiplier;
2430 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
2431 q->
param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier;
2432 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
2434 "Reset BufferSizeInKB: %d; InitialDelayInKB: %d; "
2435 "TargetKbps: %d; MaxKbps: %d; BRCParamMultiplier: %d\n",
2436 q->
param.mfx.BufferSizeInKB, q->
param.mfx.InitialDelayInKB,
2437 q->
param.mfx.TargetKbps, q->
param.mfx.MaxKbps, q->
param.mfx.BRCParamMultiplier);
2453 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
2464 mfxExtAVCEncodedFrameInfo *enc_info =
NULL;
2465 mfxExtBuffer **enc_buf =
NULL;
2467 mfxFrameSurface1 *surf =
NULL;
2469 mfxEncodeCtrl* enc_ctrl =
NULL;
2484 enc_ctrl->FrameType = MFX_FRAMETYPE_I | MFX_FRAMETYPE_REF;
2486 enc_ctrl->FrameType |= MFX_FRAMETYPE_IDR;
2507 enc_info->Header.BufferId = MFX_EXTBUFF_ENCODED_FRAME_INFO;
2508 enc_info->Header.BufferSz =
sizeof (*enc_info);
2509 pkt.bs->NumExtParam = 1;
2510 enc_buf =
av_mallocz(
sizeof(mfxExtBuffer *));
2513 enc_buf[0] = (mfxExtBuffer *)enc_info;
2515 pkt.bs->ExtParam = enc_buf;
2540 ret = MFXVideoENCODE_EncodeFrameAsync(q->
session, enc_ctrl, surf,
pkt.bs,
pkt.sync);
2541 if (
ret == MFX_WRN_DEVICE_BUSY)
2543 }
while (
ret == MFX_WRN_DEVICE_BUSY ||
ret == MFX_WRN_IN_EXECUTION);
2549 ret = (
ret == MFX_ERR_MORE_DATA) ?
2583 int needReset = 0,
ret = 0;
2616 if (j < qsv->nb_ext_buffers)
2656 mfxExtAVCEncodedFrameInfo *enc_info;
2657 mfxExtBuffer **enc_buf;
2664 }
while (
ret == MFX_WRN_IN_EXECUTION);
2670 if (qpkt.
bs->FrameType & MFX_FRAMETYPE_IDR || qpkt.
bs->FrameType & MFX_FRAMETYPE_xIDR) {
2673 }
else if (qpkt.
bs->FrameType & MFX_FRAMETYPE_I || qpkt.
bs->FrameType & MFX_FRAMETYPE_xI) {
2677 }
else if (qpkt.
bs->FrameType & MFX_FRAMETYPE_P || qpkt.
bs->FrameType & MFX_FRAMETYPE_xP)
2679 else if (qpkt.
bs->FrameType & MFX_FRAMETYPE_B || qpkt.
bs->FrameType & MFX_FRAMETYPE_xB)
2681 else if (qpkt.
bs->FrameType == MFX_FRAMETYPE_UNKNOWN && qpkt.
bs->DataLength) {
2690 enc_buf = qpkt.
bs->ExtParam;
2691 enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);
2713 MFXVideoENCODE_Close(q->
session);
2734 mfxExtBuffer **enc_buf =
pkt.bs->ExtParam;
2735 mfxExtAVCEncodedFrameInfo *enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);