25 #include <sys/types.h>
52 { MFX_PROFILE_AVC_BASELINE,
"avc baseline" },
53 { MFX_PROFILE_AVC_MAIN,
"avc main" },
54 { MFX_PROFILE_AVC_EXTENDED,
"avc extended" },
55 { MFX_PROFILE_AVC_HIGH,
"avc high" },
56 { MFX_PROFILE_AVC_HIGH_422,
"avc high 422" },
57 { MFX_PROFILE_AVC_CONSTRAINED_BASELINE,
"avc constrained baseline" },
58 { MFX_PROFILE_AVC_CONSTRAINED_HIGH,
"avc constrained high" },
59 { MFX_PROFILE_AVC_PROGRESSIVE_HIGH,
"avc progressive high" },
63 { MFX_PROFILE_MPEG2_SIMPLE,
"mpeg2 simple" },
64 { MFX_PROFILE_MPEG2_MAIN,
"mpeg2 main" },
65 { MFX_PROFILE_MPEG2_HIGH,
"mpeg2 high" },
69 { MFX_PROFILE_HEVC_MAIN,
"hevc main" },
70 { MFX_PROFILE_HEVC_MAIN10,
"hevc main10" },
71 { MFX_PROFILE_HEVC_MAINSP,
"hevc mainsp" },
72 { MFX_PROFILE_HEVC_REXT,
"hevc rext" },
73 #if QSV_VERSION_ATLEAST(1, 32)
74 { MFX_PROFILE_HEVC_SCC,
"hevc scc" },
79 { MFX_PROFILE_VP9_0,
"vp9 0" },
80 { MFX_PROFILE_VP9_1,
"vp9 1" },
81 { MFX_PROFILE_VP9_2,
"vp9 2" },
82 { MFX_PROFILE_VP9_3,
"vp9 3" },
86 #if QSV_VERSION_ATLEAST(1, 34)
87 { MFX_PROFILE_AV1_MAIN,
"av1 main" },
88 { MFX_PROFILE_AV1_HIGH,
"av1 high" },
89 { MFX_PROFILE_AV1_PRO,
"av1 professional" },
134 for (
i = 0;
i < num_profiles;
i++)
141 static const struct {
145 { MFX_RATECONTROL_CBR,
"CBR" },
146 { MFX_RATECONTROL_VBR,
"VBR" },
147 { MFX_RATECONTROL_CQP,
"CQP" },
149 { MFX_RATECONTROL_AVBR,
"AVBR" },
151 { MFX_RATECONTROL_LA,
"LA" },
152 { MFX_RATECONTROL_ICQ,
"ICQ" },
153 { MFX_RATECONTROL_LA_ICQ,
"LA_ICQ" },
155 { MFX_RATECONTROL_VCM,
"VCM" },
158 { MFX_RATECONTROL_LA_EXT,
"LA_EXT" },
160 { MFX_RATECONTROL_LA_HRD,
"LA_HRD" },
161 { MFX_RATECONTROL_QVBR,
"QVBR" },
164 #define UPDATE_PARAM(a, b) \
172 #define MFX_IMPL_VIA_MASK(impl) (0x0f00 & (impl))
185 if (
val == MFX_CODINGOPTION_ON)
187 else if (
val == MFX_CODINGOPTION_OFF)
193 mfxExtBuffer **coding_opts)
198 mfxExtCodingOption *co = (mfxExtCodingOption*)coding_opts[1];
199 mfxExtCodingOption2 *co2 =
NULL;
200 mfxExtCodingOption3 *co3 =
NULL;
201 mfxExtHEVCTiles *exthevctiles =
NULL;
203 mfxExtHyperModeParam *exthypermodeparam =
NULL;
206 const char *tmp_str =
NULL;
209 co2 = (mfxExtCodingOption2*)coding_opts[q->
co2_idx];
212 co3 = (mfxExtCodingOption3*)coding_opts[q->
co3_idx];
226 "GopPicSize: %"PRIu16
"; GopRefDist: %"PRIu16
"; GopOptFlag:%s%s; IdrInterval: %"PRIu16
"\n",
228 info->GopOptFlag & MFX_GOP_CLOSED ?
" closed" :
"",
229 info->GopOptFlag & MFX_GOP_STRICT ?
" strict" :
"",
235 if (
info->RateControlMethod == MFX_RATECONTROL_CBR ||
236 info->RateControlMethod == MFX_RATECONTROL_VBR
238 ||
info->RateControlMethod == MFX_RATECONTROL_VCM
242 "BufferSizeInKB: %"PRIu16
"; InitialDelayInKB: %"PRIu16
"; TargetKbps: %"PRIu16
"; MaxKbps: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
243 info->BufferSizeInKB,
info->InitialDelayInKB,
info->TargetKbps,
info->MaxKbps,
info->BRCParamMultiplier);
244 }
else if (
info->RateControlMethod == MFX_RATECONTROL_CQP) {
249 else if (
info->RateControlMethod == MFX_RATECONTROL_AVBR) {
251 "TargetKbps: %"PRIu16
"; Accuracy: %"PRIu16
"; Convergence: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
252 info->TargetKbps,
info->Accuracy,
info->Convergence,
info->BRCParamMultiplier);
255 else if (
info->RateControlMethod == MFX_RATECONTROL_LA
256 ||
info->RateControlMethod == MFX_RATECONTROL_LA_HRD
259 "TargetKbps: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
260 info->TargetKbps,
info->BRCParamMultiplier);
261 }
else if (
info->RateControlMethod == MFX_RATECONTROL_ICQ ||
262 info->RateControlMethod == MFX_RATECONTROL_LA_ICQ)
274 co->CAVLC == MFX_CODINGOPTION_ON ?
"CAVLC" :
"CABAC", co->MaxDecFrameBuffering);
276 "NalHrdConformance: %s; SingleSeiNalUnit: %s; VuiVclHrdParameters: %s VuiNalHrdParameters: %s\n",
281 "NalHrdConformance: %s; VuiNalHrdParameters: %s\n",
286 info->FrameInfo.FrameRateExtD,
info->FrameInfo.FrameRateExtN);
290 (
info->RateControlMethod == MFX_RATECONTROL_LA) ||
291 (
info->RateControlMethod == MFX_RATECONTROL_LA_HRD) ||
292 (
info->RateControlMethod == MFX_RATECONTROL_LA_ICQ))
295 av_log(avctx,
AV_LOG_VERBOSE,
"IntRefType: %"PRIu16
"; IntRefCycleSize: %"PRIu16
"; IntRefQPDelta: %"PRId16
"\n",
296 co2->IntRefType, co2->IntRefCycleSize, co2->IntRefQPDelta);
299 co2->MaxFrameSize, co2->MaxSliceSize);
302 "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
306 if (co2->Trellis & MFX_TRELLIS_OFF) {
308 }
else if (!co2->Trellis) {
311 char trellis_type[4];
313 if (co2->Trellis & MFX_TRELLIS_I) trellis_type[
i++] =
'I';
314 if (co2->Trellis & MFX_TRELLIS_P) trellis_type[
i++] =
'P';
315 if (co2->Trellis & MFX_TRELLIS_B) trellis_type[
i++] =
'B';
320 switch (co2->LookAheadDS) {
321 case MFX_LOOKAHEAD_DS_OFF: tmp_str =
"off";
break;
322 case MFX_LOOKAHEAD_DS_2x: tmp_str =
"2x";
break;
323 case MFX_LOOKAHEAD_DS_4x: tmp_str =
"4x";
break;
324 default: tmp_str =
"unknown";
break;
327 "RepeatPPS: %s; NumMbPerSlice: %"PRIu16
"; LookAheadDS: %s\n",
330 switch (co2->BRefType) {
331 case MFX_B_REF_OFF: tmp_str =
"off";
break;
332 case MFX_B_REF_PYRAMID: tmp_str =
"pyramid";
break;
333 default: tmp_str =
"auto";
break;
336 "AdaptiveI: %s; AdaptiveB: %s; BRefType:%s\n",
340 "MinQPI: %"PRIu8
"; MaxQPI: %"PRIu8
"; MinQPP: %"PRIu8
"; MaxQPP: %"PRIu8
"; MinQPB: %"PRIu8
"; MaxQPB: %"PRIu8
"\n",
341 co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
344 switch (co2->SkipFrame) {
345 case MFX_SKIPFRAME_NO_SKIP:
348 case MFX_SKIPFRAME_INSERT_DUMMY:
351 case MFX_SKIPFRAME_INSERT_NOTHING:
354 case MFX_SKIPFRAME_BRC_ONLY:
362 if (
info->RateControlMethod == MFX_RATECONTROL_QVBR)
365 switch (co3->PRefType) {
385 exthevctiles->NumTileColumns, exthevctiles->NumTileRows);
389 if (exthypermodeparam) {
392 if (exthypermodeparam->Mode == MFX_HYPERMODE_OFF)
394 if (exthypermodeparam->Mode == MFX_HYPERMODE_ON)
396 if (exthypermodeparam->Mode == MFX_HYPERMODE_ADAPTIVE)
405 mfxExtBuffer **coding_opts)
408 mfxExtVP9Param *vp9_param =
NULL;
409 mfxExtCodingOption2 *co2 =
NULL;
412 vp9_param = (mfxExtVP9Param *)coding_opts[q->
vp9_idx];
415 co2 = (mfxExtCodingOption2*)coding_opts[q->
co2_idx];
421 "GopPicSize: %"PRIu16
"; GopRefDist: %"PRIu16
"; GopOptFlag:%s%s; IdrInterval: %"PRIu16
"\n",
423 info->GopOptFlag & MFX_GOP_CLOSED ?
" closed" :
"",
424 info->GopOptFlag & MFX_GOP_STRICT ?
" strict" :
"",
430 if (
info->RateControlMethod == MFX_RATECONTROL_CBR ||
431 info->RateControlMethod == MFX_RATECONTROL_VBR) {
433 "BufferSizeInKB: %"PRIu16
"; InitialDelayInKB: %"PRIu16
"; TargetKbps: %"PRIu16
"; MaxKbps: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
434 info->BufferSizeInKB,
info->InitialDelayInKB,
info->TargetKbps,
info->MaxKbps,
info->BRCParamMultiplier);
435 }
else if (
info->RateControlMethod == MFX_RATECONTROL_CQP) {
439 else if (
info->RateControlMethod == MFX_RATECONTROL_ICQ) {
448 info->FrameInfo.FrameRateExtD,
info->FrameInfo.FrameRateExtN);
452 "IntRefType: %"PRIu16
"; IntRefCycleSize: %"PRIu16
"; IntRefQPDelta: %"PRId16
"\n",
453 co2->IntRefType, co2->IntRefCycleSize, co2->IntRefQPDelta);
458 "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
465 "MinQPI: %"PRIu8
"; MaxQPI: %"PRIu8
"; MinQPP: %"PRIu8
"; MaxQPP: %"PRIu8
"; MinQPB: %"PRIu8
"; MaxQPB: %"PRIu8
"\n",
466 co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
484 info->FrameInfo.FrameRateExtD,
info->FrameInfo.FrameRateExtN);
487 #if QSV_HAVE_EXT_AV1_PARAM
489 mfxExtBuffer **coding_opts)
492 mfxExtAV1TileParam *av1_tile_param = (mfxExtAV1TileParam *)coding_opts[0];
493 mfxExtAV1BitstreamParam *av1_bs_param = (mfxExtAV1BitstreamParam *)coding_opts[1];
494 mfxExtCodingOption2 *co2 = (mfxExtCodingOption2*)coding_opts[2];
495 mfxExtCodingOption3 *co3 = (mfxExtCodingOption3*)coding_opts[3];
501 "GopPicSize: %"PRIu16
"; GopRefDist: %"PRIu16
"; GopOptFlag:%s%s; IdrInterval: %"PRIu16
"\n",
503 info->GopOptFlag & MFX_GOP_CLOSED ?
" closed" :
"",
504 info->GopOptFlag & MFX_GOP_STRICT ?
" strict" :
"",
510 if (
info->RateControlMethod == MFX_RATECONTROL_CBR ||
511 info->RateControlMethod == MFX_RATECONTROL_VBR)
513 "BufferSizeInKB: %"PRIu16
"; InitialDelayInKB: %"PRIu16
"; TargetKbps: %"PRIu16
"; MaxKbps: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
514 info->BufferSizeInKB,
info->InitialDelayInKB,
info->TargetKbps,
info->MaxKbps,
info->BRCParamMultiplier);
515 else if (
info->RateControlMethod == MFX_RATECONTROL_CQP)
518 else if (
info->RateControlMethod == MFX_RATECONTROL_ICQ)
526 "IntRefType: %"PRIu16
"; IntRefCycleSize: %"PRIu16
527 "; IntRefQPDelta: %"PRId16
"; IntRefCycleDist: %"PRId16
"\n",
528 co2->IntRefType, co2->IntRefCycleSize,
529 co2->IntRefQPDelta, co3->IntRefCycleDist);
534 "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
540 switch (co2->BRefType) {
546 switch (co3->PRefType) {
554 "MinQPI: %"PRIu8
"; MaxQPI: %"PRIu8
"; MinQPP: %"PRIu8
"; MaxQPP: %"PRIu8
"; MinQPB: %"PRIu8
"; MaxQPB: %"PRIu8
"\n",
555 co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
558 info->FrameInfo.FrameRateExtD,
info->FrameInfo.FrameRateExtN);
561 "NumTileRows: %"PRIu16
"; NumTileColumns: %"PRIu16
"; NumTileGroups: %"PRIu16
"\n",
562 av1_tile_param->NumTileRows, av1_tile_param->NumTileColumns, av1_tile_param->NumTileGroups);
578 int want_vcm = q->
vcm;
582 "VCM ratecontrol mode requested, but is not supported by this SDK version\n");
586 if (want_la + want_qscale + want_vcm > 1) {
588 "More than one of: { constant qscale, lookahead, VCM } requested, "
589 "only one of them can be used at a time.\n");
595 rc_desc =
"constant quantization parameter (CQP)";
600 rc_desc =
"video conferencing mode (VCM)";
604 "setting bitrate. Please use the b option to set the desired "
605 "bitrate.\n", rc_desc);
612 rc_desc =
"VBR with lookahead (LA)";
615 rc_mode = MFX_RATECONTROL_LA_ICQ;
616 rc_desc =
"intelligent constant quality with lookahead (LA_ICQ)";
619 "setting bitrate. Please use the b option to set the desired "
620 "bitrate.\n", rc_desc);
626 rc_desc =
"intelligent constant quality (ICQ)";
631 rc_desc =
"constant bitrate (CBR)";
638 rc_mode = MFX_RATECONTROL_AVBR;
639 rc_desc =
"average variable bitrate (AVBR)";
643 rc_mode = MFX_RATECONTROL_QVBR;
644 rc_desc =
"constant quality with VBR algorithm (QVBR)";
647 rc_desc =
"variable bitrate (VBR)";
651 rc_desc =
"constant quantization parameter (CQP)";
657 "parameter (CQP) by default. Please use the global_quality "
658 "option and other options for a quality-based mode or the b "
659 "option and other options for a bitrate-based mode if the "
660 "default is not the desired choice.\n");
671 mfxVideoParam param_out = { .mfx.CodecId = q->
param.mfx.CodecId };
674 #define UNMATCH(x) (param_out.mfx.x != q->param.mfx.x)
683 if (
UNMATCH(RateControlMethod))
718 if (avctx->
level > 0)
730 q->
param.mfx.FrameInfo.CropX = 0;
731 q->
param.mfx.FrameInfo.CropY = 0;
736 q->
param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420 +
737 !
desc->log2_chroma_w + !
desc->log2_chroma_h;
738 q->
param.mfx.FrameInfo.BitDepthLuma =
desc->comp[0].depth;
739 q->
param.mfx.FrameInfo.BitDepthChroma =
desc->comp[0].depth;
747 q->
param.mfx.FrameInfo.Width = frames_hwctx->surfaces[0].Info.Width;
748 q->
param.mfx.FrameInfo.Height = frames_hwctx->surfaces[0].Info.Height;
759 q->
param.mfx.Interleaved = 1;
761 q->
param.mfx.RestartInterval = 0;
778 int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier;
779 int buffer_size_in_kilobytes, initial_delay_in_kilobytes;
787 if (avctx->
level > 0) {
798 "valid range is 0-%d, using %d instead\n",
799 MFX_TARGETUSAGE_BEST_SPEED, MFX_TARGETUSAGE_BEST_SPEED);
805 q->
param.mfx.LowPower = MFX_CODINGOPTION_ON;
807 q->
param.mfx.LowPower = MFX_CODINGOPTION_UNKNOWN;
809 q->
param.mfx.LowPower = MFX_CODINGOPTION_OFF;
822 q->
param.mfx.EncodedOrder = 0;
823 q->
param.mfx.BufferSizeInKB = 0;
833 q->
param.mfx.FrameInfo.CropX = 0;
834 q->
param.mfx.FrameInfo.CropY = 0;
839 q->
param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420 +
840 !
desc->log2_chroma_w + !
desc->log2_chroma_h;
841 q->
param.mfx.FrameInfo.BitDepthLuma =
desc->comp[0].depth;
842 q->
param.mfx.FrameInfo.BitDepthChroma =
desc->comp[0].depth;
854 q->
param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
858 q->
param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
871 q->
param.mfx.FrameInfo.Width = frames_hwctx->surfaces[0].Info.Width;
872 q->
param.mfx.FrameInfo.Height = frames_hwctx->surfaces[0].Info.Height;
891 target_bitrate_kbps = avctx->
bit_rate / 1000;
893 brc_param_multiplier = (
FFMAX(
FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
894 initial_delay_in_kilobytes) + 0x10000) / 0x10000;
900 switch (q->
param.mfx.RateControlMethod) {
901 case MFX_RATECONTROL_CBR:
902 case MFX_RATECONTROL_VBR:
907 case MFX_RATECONTROL_VCM:
909 case MFX_RATECONTROL_QVBR:
910 q->
param.mfx.BufferSizeInKB = buffer_size_in_kilobytes / brc_param_multiplier;
911 q->
param.mfx.InitialDelayInKB = initial_delay_in_kilobytes / brc_param_multiplier;
912 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
913 q->
param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier;
914 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
915 if (q->
param.mfx.RateControlMethod == MFX_RATECONTROL_QVBR)
918 case MFX_RATECONTROL_CQP:
937 case MFX_RATECONTROL_AVBR:
938 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
941 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
944 case MFX_RATECONTROL_LA:
945 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
947 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
949 case MFX_RATECONTROL_LA_ICQ:
951 case MFX_RATECONTROL_ICQ:
961 q->
extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
965 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
969 q->
extco.RateDistortionOpt = q->
rdo > 0 ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
972 q->
extco.CAVLC = q->
cavlc ? MFX_CODINGOPTION_ON
973 : MFX_CODINGOPTION_UNKNOWN;
977 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
984 q->
extco.AUDelimiter = q->
aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
988 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
993 q->
extco.AUDelimiter = q->
aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1003 q->
extco2.Trellis = (avctx->
trellis == 0) ? MFX_TRELLIS_OFF : (MFX_TRELLIS_I | MFX_TRELLIS_P | MFX_TRELLIS_B);
1005 q->
extco2.Trellis = MFX_TRELLIS_UNKNOWN;
1008 q->
extco2.RepeatPPS = q->
repeat_pps ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1013 q->
extco2.ExtBRC = q->
extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1033 q->
extco2.AdaptiveI = q->
adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1035 q->
extco2.AdaptiveB = q->
adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1041 "qmin and or qmax are set but invalid,"
1042 " please make sure min <= max\n");
1045 if (avctx->
qmin >= 0) {
1050 if (avctx->
qmax >= 0) {
1074 q->
extco2.MBBRC = q->
mbbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1078 q->
extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
1084 q->
extco2.ExtBRC = q->
extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1088 q->
extco2.AdaptiveI = q->
adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1090 q->
extco2.AdaptiveB = q->
adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1094 q->
extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
1103 q->
extmfp.Header.BufferId = MFX_EXTBUFF_MULTI_FRAME_PARAM;
1112 q->
extco3.Header.BufferId = MFX_EXTBUFF_CODING_OPTION3;
1119 q->
extco3.PRefType = MFX_P_REF_DEFAULT;
1122 q->
extco3.PRefType = MFX_P_REF_SIMPLE;
1125 q->
extco3.PRefType = MFX_P_REF_PYRAMID;
1128 q->
extco3.PRefType = MFX_P_REF_DEFAULT;
1130 "invalid p_strategy, set to default\n");
1133 if (q->
extco3.PRefType == MFX_P_REF_PYRAMID &&
1136 "Please set max_b_frames(-bf) to 0 to enable P-pyramid\n");
1149 (q->
profile == MFX_PROFILE_HEVC_REXT ||
1150 q->
profile == MFX_PROFILE_UNKNOWN))
1151 q->
extco3.TargetChromaFormatPlus1 = MFX_CHROMAFORMAT_YUV444 + 1;
1163 MFX_CODINGOPTION_OFF;
1165 q->
extco3.TransformSkip = MFX_CODINGOPTION_UNKNOWN;
1166 q->
extco3.GPB = q->
gpb ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1172 q->
extvp9param.Header.BufferId = MFX_EXTBUFF_VP9_PARAM;
1174 q->
extvp9param.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
1175 #if QSV_HAVE_EXT_VP9_TILES
1182 #if QSV_HAVE_EXT_AV1_PARAM
1185 q->extav1tileparam.Header.BufferId = MFX_EXTBUFF_AV1_TILE_PARAM;
1186 q->extav1tileparam.Header.BufferSz =
sizeof(q->extav1tileparam);
1187 q->extav1tileparam.NumTileColumns = q->
tile_cols;
1188 q->extav1tileparam.NumTileRows = q->
tile_rows;
1191 q->extav1bsparam.Header.BufferId = MFX_EXTBUFF_AV1_BITSTREAM_PARAM;
1192 q->extav1bsparam.Header.BufferSz =
sizeof(q->extav1bsparam);
1193 q->extav1bsparam.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
1197 "This version of runtime doesn't support AV1 encoding\n");
1204 q->
exthevctiles.Header.BufferId = MFX_EXTBUFF_HEVC_TILES;
1212 q->
extvsi.ColourDescriptionPresent = 0;
1217 q->
extvsi.ColourDescriptionPresent = 1;
1229 q->
extvsi.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO;
1238 MFXQueryIMPL(q->
session, &impl);
1244 if (q->
param.mfx.LowPower != MFX_CODINGOPTION_ON) {
1245 av_log(avctx,
AV_LOG_ERROR,
"Dual GFX mode supports only low-power encoding mode \n");
1248 if (q->
param.mfx.CodecId != MFX_CODEC_AVC && q->
param.mfx.CodecId != MFX_CODEC_HEVC) {
1250 "Supported: h264_qsv and hevc_qsv \n");
1253 if (q->
param.mfx.RateControlMethod != MFX_RATECONTROL_VBR &&
1254 q->
param.mfx.RateControlMethod != MFX_RATECONTROL_CQP &&
1255 q->
param.mfx.RateControlMethod != MFX_RATECONTROL_ICQ) {
1257 "Supported: VBR, CQP and ICQ \n");
1259 if ((q->
param.mfx.CodecId == MFX_CODEC_AVC && q->
param.mfx.IdrInterval != 0) ||
1260 (q->
param.mfx.CodecId == MFX_CODEC_HEVC && q->
param.mfx.IdrInterval != 1)) {
1261 av_log(avctx,
AV_LOG_WARNING,
"Dual GFX mode requires closed GOP for AVC and strict GOP for HEVC, -idr_interval 0 \n");
1263 if (q->
param.mfx.GopPicSize < 30) {
1264 av_log(avctx,
AV_LOG_WARNING,
"For better performance in dual GFX mode GopPicSize must be >= 30 \n");
1266 if (q->
param.AsyncDepth < 30) {
1267 av_log(avctx,
AV_LOG_WARNING,
"For better performance in dual GFX mode AsyncDepth must be >= 30 \n");
1270 q->exthypermodeparam.Header.BufferId = MFX_EXTBUFF_HYPER_MODE_PARAM;
1271 q->exthypermodeparam.Header.BufferSz =
sizeof(q->exthypermodeparam);
1272 q->exthypermodeparam.Mode = q->
dual_gfx;
1276 "This version of runtime doesn't support Hyper Encode\n");
1284 "some encoding parameters are not supported by the QSV "
1285 "runtime. Please double check the input parameters.\n");
1299 "Error calling GetVideoParam");
1315 mfxExtVP9Param vp9_extend_buf = {
1316 .Header.BufferId = MFX_EXTBUFF_VP9_PARAM,
1317 .Header.BufferSz =
sizeof(vp9_extend_buf),
1320 mfxExtCodingOption2 co2 = {
1321 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1322 .Header.BufferSz =
sizeof(co2),
1325 mfxExtCodingOption3 co3 = {
1326 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1327 .Header.BufferSz =
sizeof(co3),
1330 mfxExtBuffer *ext_buffers[3];
1331 int ext_buf_num = 0;
1338 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co2;
1343 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co3;
1348 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&vp9_extend_buf;
1351 q->
param.ExtParam = ext_buffers;
1352 q->
param.NumExtParam = ext_buf_num;
1357 "Error calling GetVideoParam");
1368 #if QSV_HAVE_EXT_AV1_PARAM
1370 mfxExtAV1TileParam av1_extend_tile_buf = {
1371 .Header.BufferId = MFX_EXTBUFF_AV1_TILE_PARAM,
1372 .Header.BufferSz =
sizeof(av1_extend_tile_buf),
1374 mfxExtAV1BitstreamParam av1_bs_param = {
1375 .Header.BufferId = MFX_EXTBUFF_AV1_BITSTREAM_PARAM,
1376 .Header.BufferSz =
sizeof(av1_bs_param),
1379 mfxExtCodingOption2 co2 = {
1380 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1381 .Header.BufferSz =
sizeof(co2),
1384 mfxExtCodingOption3 co3 = {
1385 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1386 .Header.BufferSz =
sizeof(co3),
1389 mfxExtBuffer *ext_buffers[] = {
1390 (mfxExtBuffer*)&av1_extend_tile_buf,
1391 (mfxExtBuffer*)&av1_bs_param,
1392 (mfxExtBuffer*)&co2,
1393 (mfxExtBuffer*)&co3,
1398 "This version of runtime doesn't support AV1 encoding\n");
1402 q->
param.ExtParam = ext_buffers;
1408 "Error calling GetVideoParam");
1411 dump_video_av1_param(avctx, q, ext_buffers);
1420 uint8_t sps_buf[512];
1421 uint8_t pps_buf[128];
1423 mfxExtCodingOptionSPSPPS extradata = {
1424 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS,
1425 .Header.BufferSz =
sizeof(extradata),
1426 .SPSBuffer = sps_buf, .SPSBufSize =
sizeof(sps_buf),
1427 .PPSBuffer = pps_buf, .PPSBufSize =
sizeof(pps_buf)
1430 mfxExtCodingOption co = {
1431 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION,
1432 .Header.BufferSz =
sizeof(co),
1434 mfxExtCodingOption2 co2 = {
1435 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1436 .Header.BufferSz =
sizeof(co2),
1438 mfxExtCodingOption3 co3 = {
1439 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1440 .Header.BufferSz =
sizeof(co3),
1443 uint8_t vps_buf[128];
1444 mfxExtCodingOptionVPS extradata_vps = {
1445 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_VPS,
1446 .Header.BufferSz =
sizeof(extradata_vps),
1447 .VPSBuffer = vps_buf,
1448 .VPSBufSize =
sizeof(vps_buf),
1451 mfxExtHEVCTiles hevc_tile_buf = {
1452 .Header.BufferId = MFX_EXTBUFF_HEVC_TILES,
1453 .Header.BufferSz =
sizeof(hevc_tile_buf),
1457 mfxExtHyperModeParam hyper_mode_param_buf = {
1458 .Header.BufferId = MFX_EXTBUFF_HYPER_MODE_PARAM,
1459 .Header.BufferSz =
sizeof(hyper_mode_param_buf),
1466 int ret, ext_buf_num = 0, extradata_offset = 0;
1469 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata;
1470 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co;
1475 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co2;
1480 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co3;
1485 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata_vps;
1488 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&hevc_tile_buf;
1493 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&hyper_mode_param_buf;
1497 q->
param.ExtParam = ext_buffers;
1498 q->
param.NumExtParam = ext_buf_num;
1503 "Error calling GetVideoParam");
1507 if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)
1508 || (q->
hevc_vps && !extradata_vps.VPSBufSize)
1514 avctx->
extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize;
1522 memcpy(avctx->
extradata, vps_buf, extradata_vps.VPSBufSize);
1523 extradata_offset += extradata_vps.VPSBufSize;
1526 memcpy(avctx->
extradata + extradata_offset, sps_buf, extradata.SPSBufSize);
1527 extradata_offset += extradata.SPSBufSize;
1529 memcpy(avctx->
extradata + extradata_offset, pps_buf, extradata.PPSBufSize);
1530 extradata_offset += extradata.PPSBufSize;
1551 mfxFrameSurface1 *surfaces;
1565 for (
i = 0;
i < nb_surfaces;
i++) {
1566 surfaces[
i].Info = q->
req.Info;
1570 q->
opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
1601 q->
param.IOPattern == MFX_IOPATTERN_IN_OPAQUE_MEMORY,
1635 int opaque_alloc = 0;
1657 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
1658 iopattern = MFX_IOPATTERN_IN_OPAQUE_MEMORY;
1659 else if (frames_hwctx->frame_type &
1660 (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
1661 iopattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1663 if (frames_hwctx->frame_type &
1664 (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
1665 iopattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1671 iopattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
1672 q->
param.IOPattern = iopattern;
1682 "Error querying mfx version");
1716 if (j < qsv->nb_ext_buffers)
1727 if (
ret == MFX_WRN_PARTIAL_ACCELERATION) {
1729 }
else if (
ret < 0) {
1731 "Error querying encoder params");
1737 "Error querying (IOSurf) the encoding parameters");
1746 "however libmfx %d.%d doesn't support OPAQUE memory.\n",
1747 q->
ver.Major, q->
ver.Minor);
1755 "Error initializing the encoder");
1758 "Warning in encoder initialization");
1793 enc_ctrl->NumPayload = 0;
1794 enc_ctrl->NumExtParam = 0;
1832 last = &
frame->next;
1840 if (!
frame->frame) {
1857 int max_step[4], filled[4] = { 0 };
1863 for (
int i = 0;
i <
desc->nb_components;
i++) {
1865 int sheight, dheight, plane =
comp->plane;
1873 if (swidth < 0 || dwidth < 0) {
1881 sheight =
frame->height;
1890 if (new_w >
frame->width) {
1891 for (
int j = 0; j < sheight; j++) {
1892 void *line_ptr =
frame->data[plane] + j *
frame->linesize[plane] + swidth;
1896 new_w -
frame->width);
1901 for (
int j = sheight; j < dheight; j++)
1902 memcpy(
frame->data[plane] + j *
frame->linesize[plane],
1903 frame->data[plane] + (sheight - 1) *
frame->linesize[plane],
1917 switch (
frame->format) {
1933 frame->linesize[1] = 0;
1934 total_size =
frame->linesize[0] *
frame->height;
1943 frame->linesize[1] = 0;
1944 total_size =
frame->linesize[0] *
frame->height;
2035 MFX_PICSTRUCT_FIELD_BFF;
2036 if (
frame->repeat_pict == 1)
2037 qf->
surface.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
2038 else if (
frame->repeat_pict == 2)
2039 qf->
surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
2040 else if (
frame->repeat_pict == 4)
2041 qf->
surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
2058 if (q->
param.mfx.CodecId == MFX_CODEC_AVC) {
2059 if (q->
param.mfx.CodecProfile == MFX_PROFILE_AVC_BASELINE ||
2060 q->
param.mfx.CodecLevel < MFX_LEVEL_AVC_21 ||
2061 q->
param.mfx.CodecLevel > MFX_LEVEL_AVC_41)
2063 "Interlaced coding is supported"
2064 " at Main/High Profile Level 2.2-4.0\n");
2069 mfxEncodeCtrl *enc_ctrl)
2085 mfxExtEncoderROI *enc_roi =
NULL;
2092 if (!roi_size || sd->
size % roi_size) {
2096 nb_roi = sd->
size / roi_size;
2099 "supported by driver (%d > %d).\n",
2107 enc_roi->Header.BufferId = MFX_EXTBUFF_ENCODER_ROI;
2108 enc_roi->Header.BufferSz =
sizeof(*enc_roi);
2109 enc_roi->NumROI = nb_roi;
2110 enc_roi->ROIMode = MFX_ROI_MODE_QP_DELTA;
2111 for (
i = 0;
i < nb_roi;
i++) {
2117 enc_roi->ROI[
i].DeltaQP =
2121 enc_roi->ROI[
i].DeltaQP);
2123 enc_ctrl->ExtParam[enc_ctrl->NumExtParam] = (mfxExtBuffer *)enc_roi;
2124 enc_ctrl->NumExtParam++;
2130 mfxEncodeCtrl *enc_ctrl)
2133 if (!
frame->metadata)
2136 if (!skip_frame_dict)
2138 enc_ctrl->SkipFrame = strtol(skip_frame_dict->
value,
NULL, 10);
2144 int updated = 0, new_qp = 0;
2149 if (q->
param.mfx.RateControlMethod == MFX_RATECONTROL_CQP) {
2165 "Reset qp = %d/%d/%d for idr/p/b frames\n",
2184 "Reset MaxFrameSize: %d;\n", q->
extco2.MaxFrameSize);
2198 q->
param.mfx.GopPicSize);
2219 q->
extco2.IntRefQPDelta =
2223 "Reset IntRefType: %d; IntRefCycleSize: %d; "
2224 "IntRefQPDelta: %d; IntRefCycleDist: %d\n",
2254 "qmin and or qmax are set but invalid,"
2255 " please make sure min <= max\n");
2265 if (avctx->
qmin >= 0) {
2269 if (avctx->
qmax >= 0) {
2287 "MinQPP: %d; MaxQPP: %d; "
2288 "MinQPB: %d; MaxQPB: %d\n",
2309 q->
extco3.LowDelayBRC = MFX_CODINGOPTION_UNKNOWN;
2335 q->
param.mfx.FrameInfo.FrameRateExtN,
2336 q->
param.mfx.FrameInfo.FrameRateExtD,
2337 (
double)q->
param.mfx.FrameInfo.FrameRateExtN / q->
param.mfx.FrameInfo.FrameRateExtD);
2345 int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier;
2346 int buffer_size_in_kilobytes, initial_delay_in_kilobytes;
2357 target_bitrate_kbps = avctx->
bit_rate / 1000;
2359 brc_param_multiplier = (
FFMAX(
FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
2360 initial_delay_in_kilobytes) + 0x10000) / 0x10000;
2362 q->
param.mfx.BufferSizeInKB = buffer_size_in_kilobytes / brc_param_multiplier;
2363 q->
param.mfx.InitialDelayInKB = initial_delay_in_kilobytes / brc_param_multiplier;
2364 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
2365 q->
param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier;
2366 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
2368 "Reset BufferSizeInKB: %d; InitialDelayInKB: %d; "
2369 "TargetKbps: %d; MaxKbps: %d; BRCParamMultiplier: %d\n",
2370 q->
param.mfx.BufferSizeInKB, q->
param.mfx.InitialDelayInKB,
2371 q->
param.mfx.TargetKbps, q->
param.mfx.MaxKbps, q->
param.mfx.BRCParamMultiplier);
2387 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
2398 mfxExtAVCEncodedFrameInfo *enc_info =
NULL;
2399 mfxExtBuffer **enc_buf =
NULL;
2401 mfxFrameSurface1 *surf =
NULL;
2403 mfxEncodeCtrl* enc_ctrl =
NULL;
2418 enc_ctrl->FrameType = MFX_FRAMETYPE_I | MFX_FRAMETYPE_REF;
2420 enc_ctrl->FrameType |= MFX_FRAMETYPE_IDR;
2441 enc_info->Header.BufferId = MFX_EXTBUFF_ENCODED_FRAME_INFO;
2442 enc_info->Header.BufferSz =
sizeof (*enc_info);
2443 pkt.bs->NumExtParam = 1;
2444 enc_buf =
av_mallocz(
sizeof(mfxExtBuffer *));
2447 enc_buf[0] = (mfxExtBuffer *)enc_info;
2449 pkt.bs->ExtParam = enc_buf;
2474 ret = MFXVideoENCODE_EncodeFrameAsync(q->
session, enc_ctrl, surf,
pkt.bs,
pkt.sync);
2475 if (
ret == MFX_WRN_DEVICE_BUSY)
2477 }
while (
ret == MFX_WRN_DEVICE_BUSY ||
ret == MFX_WRN_IN_EXECUTION);
2483 ret = (
ret == MFX_ERR_MORE_DATA) ?
2517 int needReset = 0,
ret = 0;
2550 if (j < qsv->nb_ext_buffers)
2590 mfxExtAVCEncodedFrameInfo *enc_info;
2591 mfxExtBuffer **enc_buf;
2598 }
while (
ret == MFX_WRN_IN_EXECUTION);
2604 if (qpkt.
bs->FrameType & MFX_FRAMETYPE_IDR || qpkt.
bs->FrameType & MFX_FRAMETYPE_xIDR) {
2607 }
else if (qpkt.
bs->FrameType & MFX_FRAMETYPE_I || qpkt.
bs->FrameType & MFX_FRAMETYPE_xI) {
2611 }
else if (qpkt.
bs->FrameType & MFX_FRAMETYPE_P || qpkt.
bs->FrameType & MFX_FRAMETYPE_xP)
2613 else if (qpkt.
bs->FrameType & MFX_FRAMETYPE_B || qpkt.
bs->FrameType & MFX_FRAMETYPE_xB)
2615 else if (qpkt.
bs->FrameType == MFX_FRAMETYPE_UNKNOWN && qpkt.
bs->DataLength) {
2624 enc_buf = qpkt.
bs->ExtParam;
2625 enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);
2647 MFXVideoENCODE_Close(q->
session);
2668 mfxExtBuffer **enc_buf =
pkt.bs->ExtParam;
2669 mfxExtAVCEncodedFrameInfo *enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);