25 #include <sys/types.h>
51 { MFX_PROFILE_AVC_BASELINE,
"avc baseline" },
52 { MFX_PROFILE_AVC_MAIN,
"avc main" },
53 { MFX_PROFILE_AVC_EXTENDED,
"avc extended" },
54 { MFX_PROFILE_AVC_HIGH,
"avc high" },
55 { MFX_PROFILE_AVC_HIGH_422,
"avc high 422" },
56 { MFX_PROFILE_AVC_CONSTRAINED_BASELINE,
"avc constrained baseline" },
57 { MFX_PROFILE_AVC_CONSTRAINED_HIGH,
"avc constrained high" },
58 { MFX_PROFILE_AVC_PROGRESSIVE_HIGH,
"avc progressive high" },
62 { MFX_PROFILE_MPEG2_SIMPLE,
"mpeg2 simple" },
63 { MFX_PROFILE_MPEG2_MAIN,
"mpeg2 main" },
64 { MFX_PROFILE_MPEG2_HIGH,
"mpeg2 high" },
68 { MFX_PROFILE_HEVC_MAIN,
"hevc main" },
69 { MFX_PROFILE_HEVC_MAIN10,
"hevc main10" },
70 { MFX_PROFILE_HEVC_MAINSP,
"hevc mainsp" },
71 { MFX_PROFILE_HEVC_REXT,
"hevc rext" },
72 #if QSV_VERSION_ATLEAST(1, 32)
73 { MFX_PROFILE_HEVC_SCC,
"hevc scc" },
78 { MFX_PROFILE_VP9_0,
"vp9 0" },
79 { MFX_PROFILE_VP9_1,
"vp9 1" },
80 { MFX_PROFILE_VP9_2,
"vp9 2" },
81 { MFX_PROFILE_VP9_3,
"vp9 3" },
85 #if QSV_VERSION_ATLEAST(1, 34)
86 { MFX_PROFILE_AV1_MAIN,
"av1 main" },
87 { MFX_PROFILE_AV1_HIGH,
"av1 high" },
88 { MFX_PROFILE_AV1_PRO,
"av1 professional" },
133 for (
i = 0;
i < num_profiles;
i++)
140 static const struct {
144 { MFX_RATECONTROL_CBR,
"CBR" },
145 { MFX_RATECONTROL_VBR,
"VBR" },
146 { MFX_RATECONTROL_CQP,
"CQP" },
148 { MFX_RATECONTROL_AVBR,
"AVBR" },
150 { MFX_RATECONTROL_LA,
"LA" },
151 { MFX_RATECONTROL_ICQ,
"ICQ" },
152 { MFX_RATECONTROL_LA_ICQ,
"LA_ICQ" },
154 { MFX_RATECONTROL_VCM,
"VCM" },
157 { MFX_RATECONTROL_LA_EXT,
"LA_EXT" },
159 { MFX_RATECONTROL_LA_HRD,
"LA_HRD" },
160 { MFX_RATECONTROL_QVBR,
"QVBR" },
163 #define UPDATE_PARAM(a, b) \
171 #define MFX_IMPL_VIA_MASK(impl) (0x0f00 & (impl))
184 if (
val == MFX_CODINGOPTION_ON)
186 else if (
val == MFX_CODINGOPTION_OFF)
192 mfxExtBuffer **coding_opts)
197 mfxExtCodingOption *co = (mfxExtCodingOption*)coding_opts[1];
198 mfxExtCodingOption2 *co2 =
NULL;
199 mfxExtCodingOption3 *co3 =
NULL;
200 mfxExtHEVCTiles *exthevctiles =
NULL;
202 mfxExtHyperModeParam *exthypermodeparam =
NULL;
205 const char *tmp_str =
NULL;
208 co2 = (mfxExtCodingOption2*)coding_opts[q->
co2_idx];
211 co3 = (mfxExtCodingOption3*)coding_opts[q->
co3_idx];
225 "GopPicSize: %"PRIu16
"; GopRefDist: %"PRIu16
"; GopOptFlag:%s%s; IdrInterval: %"PRIu16
"\n",
227 info->GopOptFlag & MFX_GOP_CLOSED ?
" closed" :
"",
228 info->GopOptFlag & MFX_GOP_STRICT ?
" strict" :
"",
234 if (
info->RateControlMethod == MFX_RATECONTROL_CBR ||
235 info->RateControlMethod == MFX_RATECONTROL_VBR
237 ||
info->RateControlMethod == MFX_RATECONTROL_VCM
241 "BufferSizeInKB: %"PRIu16
"; InitialDelayInKB: %"PRIu16
"; TargetKbps: %"PRIu16
"; MaxKbps: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
242 info->BufferSizeInKB,
info->InitialDelayInKB,
info->TargetKbps,
info->MaxKbps,
info->BRCParamMultiplier);
243 }
else if (
info->RateControlMethod == MFX_RATECONTROL_CQP) {
248 else if (
info->RateControlMethod == MFX_RATECONTROL_AVBR) {
250 "TargetKbps: %"PRIu16
"; Accuracy: %"PRIu16
"; Convergence: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
251 info->TargetKbps,
info->Accuracy,
info->Convergence,
info->BRCParamMultiplier);
254 else if (
info->RateControlMethod == MFX_RATECONTROL_LA
255 ||
info->RateControlMethod == MFX_RATECONTROL_LA_HRD
258 "TargetKbps: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
259 info->TargetKbps,
info->BRCParamMultiplier);
260 }
else if (
info->RateControlMethod == MFX_RATECONTROL_ICQ ||
261 info->RateControlMethod == MFX_RATECONTROL_LA_ICQ)
273 co->CAVLC == MFX_CODINGOPTION_ON ?
"CAVLC" :
"CABAC", co->MaxDecFrameBuffering);
275 "NalHrdConformance: %s; SingleSeiNalUnit: %s; VuiVclHrdParameters: %s VuiNalHrdParameters: %s\n",
280 "NalHrdConformance: %s; VuiNalHrdParameters: %s\n",
285 info->FrameInfo.FrameRateExtD,
info->FrameInfo.FrameRateExtN);
289 (
info->RateControlMethod == MFX_RATECONTROL_LA) ||
290 (
info->RateControlMethod == MFX_RATECONTROL_LA_HRD) ||
291 (
info->RateControlMethod == MFX_RATECONTROL_LA_ICQ))
294 av_log(avctx,
AV_LOG_VERBOSE,
"IntRefType: %"PRIu16
"; IntRefCycleSize: %"PRIu16
"; IntRefQPDelta: %"PRId16
"\n",
295 co2->IntRefType, co2->IntRefCycleSize, co2->IntRefQPDelta);
298 co2->MaxFrameSize, co2->MaxSliceSize);
301 "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
305 if (co2->Trellis & MFX_TRELLIS_OFF) {
307 }
else if (!co2->Trellis) {
310 char trellis_type[4];
312 if (co2->Trellis & MFX_TRELLIS_I) trellis_type[
i++] =
'I';
313 if (co2->Trellis & MFX_TRELLIS_P) trellis_type[
i++] =
'P';
314 if (co2->Trellis & MFX_TRELLIS_B) trellis_type[
i++] =
'B';
319 switch (co2->LookAheadDS) {
320 case MFX_LOOKAHEAD_DS_OFF: tmp_str =
"off";
break;
321 case MFX_LOOKAHEAD_DS_2x: tmp_str =
"2x";
break;
322 case MFX_LOOKAHEAD_DS_4x: tmp_str =
"4x";
break;
323 default: tmp_str =
"unknown";
break;
326 "RepeatPPS: %s; NumMbPerSlice: %"PRIu16
"; LookAheadDS: %s\n",
329 switch (co2->BRefType) {
330 case MFX_B_REF_OFF: tmp_str =
"off";
break;
331 case MFX_B_REF_PYRAMID: tmp_str =
"pyramid";
break;
332 default: tmp_str =
"auto";
break;
335 "AdaptiveI: %s; AdaptiveB: %s; BRefType:%s\n",
339 "MinQPI: %"PRIu8
"; MaxQPI: %"PRIu8
"; MinQPP: %"PRIu8
"; MaxQPP: %"PRIu8
"; MinQPB: %"PRIu8
"; MaxQPB: %"PRIu8
"\n",
340 co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
343 switch (co2->SkipFrame) {
344 case MFX_SKIPFRAME_NO_SKIP:
347 case MFX_SKIPFRAME_INSERT_DUMMY:
350 case MFX_SKIPFRAME_INSERT_NOTHING:
353 case MFX_SKIPFRAME_BRC_ONLY:
361 if (
info->RateControlMethod == MFX_RATECONTROL_QVBR)
364 switch (co3->PRefType) {
384 exthevctiles->NumTileColumns, exthevctiles->NumTileRows);
388 if (exthypermodeparam) {
391 if (exthypermodeparam->Mode == MFX_HYPERMODE_OFF)
393 if (exthypermodeparam->Mode == MFX_HYPERMODE_ON)
395 if (exthypermodeparam->Mode == MFX_HYPERMODE_ADAPTIVE)
404 mfxExtBuffer **coding_opts)
407 mfxExtVP9Param *vp9_param =
NULL;
408 mfxExtCodingOption2 *co2 =
NULL;
411 vp9_param = (mfxExtVP9Param *)coding_opts[q->
vp9_idx];
414 co2 = (mfxExtCodingOption2*)coding_opts[q->
co2_idx];
420 "GopPicSize: %"PRIu16
"; GopRefDist: %"PRIu16
"; GopOptFlag:%s%s; IdrInterval: %"PRIu16
"\n",
422 info->GopOptFlag & MFX_GOP_CLOSED ?
" closed" :
"",
423 info->GopOptFlag & MFX_GOP_STRICT ?
" strict" :
"",
429 if (
info->RateControlMethod == MFX_RATECONTROL_CBR ||
430 info->RateControlMethod == MFX_RATECONTROL_VBR) {
432 "BufferSizeInKB: %"PRIu16
"; InitialDelayInKB: %"PRIu16
"; TargetKbps: %"PRIu16
"; MaxKbps: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
433 info->BufferSizeInKB,
info->InitialDelayInKB,
info->TargetKbps,
info->MaxKbps,
info->BRCParamMultiplier);
434 }
else if (
info->RateControlMethod == MFX_RATECONTROL_CQP) {
438 else if (
info->RateControlMethod == MFX_RATECONTROL_ICQ) {
447 info->FrameInfo.FrameRateExtD,
info->FrameInfo.FrameRateExtN);
451 "IntRefType: %"PRIu16
"; IntRefCycleSize: %"PRIu16
"; IntRefQPDelta: %"PRId16
"\n",
452 co2->IntRefType, co2->IntRefCycleSize, co2->IntRefQPDelta);
457 "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
464 "MinQPI: %"PRIu8
"; MaxQPI: %"PRIu8
"; MinQPP: %"PRIu8
"; MaxQPP: %"PRIu8
"; MinQPB: %"PRIu8
"; MaxQPB: %"PRIu8
"\n",
465 co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
483 info->FrameInfo.FrameRateExtD,
info->FrameInfo.FrameRateExtN);
486 #if QSV_HAVE_EXT_AV1_PARAM
488 mfxExtBuffer **coding_opts)
491 mfxExtAV1TileParam *av1_tile_param = (mfxExtAV1TileParam *)coding_opts[0];
492 mfxExtAV1BitstreamParam *av1_bs_param = (mfxExtAV1BitstreamParam *)coding_opts[1];
493 mfxExtCodingOption2 *co2 = (mfxExtCodingOption2*)coding_opts[2];
494 mfxExtCodingOption3 *co3 = (mfxExtCodingOption3*)coding_opts[3];
500 "GopPicSize: %"PRIu16
"; GopRefDist: %"PRIu16
"; GopOptFlag:%s%s; IdrInterval: %"PRIu16
"\n",
502 info->GopOptFlag & MFX_GOP_CLOSED ?
" closed" :
"",
503 info->GopOptFlag & MFX_GOP_STRICT ?
" strict" :
"",
509 if (
info->RateControlMethod == MFX_RATECONTROL_CBR ||
510 info->RateControlMethod == MFX_RATECONTROL_VBR)
512 "BufferSizeInKB: %"PRIu16
"; InitialDelayInKB: %"PRIu16
"; TargetKbps: %"PRIu16
"; MaxKbps: %"PRIu16
"; BRCParamMultiplier: %"PRIu16
"\n",
513 info->BufferSizeInKB,
info->InitialDelayInKB,
info->TargetKbps,
info->MaxKbps,
info->BRCParamMultiplier);
514 else if (
info->RateControlMethod == MFX_RATECONTROL_CQP)
517 else if (
info->RateControlMethod == MFX_RATECONTROL_ICQ)
525 "IntRefType: %"PRIu16
"; IntRefCycleSize: %"PRIu16
526 "; IntRefQPDelta: %"PRId16
"; IntRefCycleDist: %"PRId16
"\n",
527 co2->IntRefType, co2->IntRefCycleSize,
528 co2->IntRefQPDelta, co3->IntRefCycleDist);
533 "BitrateLimit: %s; MBBRC: %s; ExtBRC: %s\n",
539 switch (co2->BRefType) {
545 switch (co3->PRefType) {
553 "MinQPI: %"PRIu8
"; MaxQPI: %"PRIu8
"; MinQPP: %"PRIu8
"; MaxQPP: %"PRIu8
"; MinQPB: %"PRIu8
"; MaxQPB: %"PRIu8
"\n",
554 co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2->MinQPB, co2->MaxQPB);
557 info->FrameInfo.FrameRateExtD,
info->FrameInfo.FrameRateExtN);
560 "NumTileRows: %"PRIu16
"; NumTileColumns: %"PRIu16
"; NumTileGroups: %"PRIu16
"\n",
561 av1_tile_param->NumTileRows, av1_tile_param->NumTileColumns, av1_tile_param->NumTileGroups);
577 int want_vcm = q->
vcm;
581 "VCM ratecontrol mode requested, but is not supported by this SDK version\n");
585 if (want_la + want_qscale + want_vcm > 1) {
587 "More than one of: { constant qscale, lookahead, VCM } requested, "
588 "only one of them can be used at a time.\n");
594 rc_desc =
"constant quantization parameter (CQP)";
599 rc_desc =
"video conferencing mode (VCM)";
603 "setting bitrate. Please use the b option to set the desired "
604 "bitrate.\n", rc_desc);
611 rc_desc =
"VBR with lookahead (LA)";
614 rc_mode = MFX_RATECONTROL_LA_ICQ;
615 rc_desc =
"intelligent constant quality with lookahead (LA_ICQ)";
618 "setting bitrate. Please use the b option to set the desired "
619 "bitrate.\n", rc_desc);
625 rc_desc =
"intelligent constant quality (ICQ)";
630 rc_desc =
"constant bitrate (CBR)";
637 rc_mode = MFX_RATECONTROL_AVBR;
638 rc_desc =
"average variable bitrate (AVBR)";
642 rc_mode = MFX_RATECONTROL_QVBR;
643 rc_desc =
"constant quality with VBR algorithm (QVBR)";
646 rc_desc =
"variable bitrate (VBR)";
650 rc_desc =
"constant quantization parameter (CQP)";
656 "parameter (CQP) by default. Please use the global_quality "
657 "option and other options for a quality-based mode or the b "
658 "option and other options for a bitrate-based mode if the "
659 "default is not the desired choice.\n");
670 mfxVideoParam param_out = { .mfx.CodecId = q->
param.mfx.CodecId };
673 #define UNMATCH(x) (param_out.mfx.x != q->param.mfx.x)
682 if (
UNMATCH(RateControlMethod))
717 if (avctx->
level > 0)
729 q->
param.mfx.FrameInfo.CropX = 0;
730 q->
param.mfx.FrameInfo.CropY = 0;
735 q->
param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420 +
736 !
desc->log2_chroma_w + !
desc->log2_chroma_h;
737 q->
param.mfx.FrameInfo.BitDepthLuma =
desc->comp[0].depth;
738 q->
param.mfx.FrameInfo.BitDepthChroma =
desc->comp[0].depth;
746 q->
param.mfx.FrameInfo.Width = frames_hwctx->surfaces[0].Info.Width;
747 q->
param.mfx.FrameInfo.Height = frames_hwctx->surfaces[0].Info.Height;
758 q->
param.mfx.Interleaved = 1;
760 q->
param.mfx.RestartInterval = 0;
777 int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier;
778 int buffer_size_in_kilobytes, initial_delay_in_kilobytes;
786 if (avctx->
level > 0) {
797 "valid range is 0-%d, using %d instead\n",
798 MFX_TARGETUSAGE_BEST_SPEED, MFX_TARGETUSAGE_BEST_SPEED);
804 q->
param.mfx.LowPower = MFX_CODINGOPTION_ON;
806 q->
param.mfx.LowPower = MFX_CODINGOPTION_UNKNOWN;
808 q->
param.mfx.LowPower = MFX_CODINGOPTION_OFF;
821 q->
param.mfx.EncodedOrder = 0;
822 q->
param.mfx.BufferSizeInKB = 0;
832 q->
param.mfx.FrameInfo.CropX = 0;
833 q->
param.mfx.FrameInfo.CropY = 0;
838 q->
param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420 +
839 !
desc->log2_chroma_w + !
desc->log2_chroma_h;
840 q->
param.mfx.FrameInfo.BitDepthLuma =
desc->comp[0].depth;
841 q->
param.mfx.FrameInfo.BitDepthChroma =
desc->comp[0].depth;
853 q->
param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
857 q->
param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
870 q->
param.mfx.FrameInfo.Width = frames_hwctx->surfaces[0].Info.Width;
871 q->
param.mfx.FrameInfo.Height = frames_hwctx->surfaces[0].Info.Height;
890 target_bitrate_kbps = avctx->
bit_rate / 1000;
892 brc_param_multiplier = (
FFMAX(
FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
893 initial_delay_in_kilobytes) + 0x10000) / 0x10000;
899 switch (q->
param.mfx.RateControlMethod) {
900 case MFX_RATECONTROL_CBR:
901 case MFX_RATECONTROL_VBR:
906 case MFX_RATECONTROL_VCM:
908 case MFX_RATECONTROL_QVBR:
909 q->
param.mfx.BufferSizeInKB = buffer_size_in_kilobytes / brc_param_multiplier;
910 q->
param.mfx.InitialDelayInKB = initial_delay_in_kilobytes / brc_param_multiplier;
911 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
912 q->
param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier;
913 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
914 if (q->
param.mfx.RateControlMethod == MFX_RATECONTROL_QVBR)
917 case MFX_RATECONTROL_CQP:
936 case MFX_RATECONTROL_AVBR:
937 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
940 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
943 case MFX_RATECONTROL_LA:
944 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
946 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
948 case MFX_RATECONTROL_LA_ICQ:
950 case MFX_RATECONTROL_ICQ:
960 q->
extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
964 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
968 q->
extco.RateDistortionOpt = q->
rdo > 0 ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
971 q->
extco.CAVLC = q->
cavlc ? MFX_CODINGOPTION_ON
972 : MFX_CODINGOPTION_UNKNOWN;
976 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
983 q->
extco.AUDelimiter = q->
aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
987 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
992 q->
extco.AUDelimiter = q->
aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1002 q->
extco2.Trellis = (avctx->
trellis == 0) ? MFX_TRELLIS_OFF : (MFX_TRELLIS_I | MFX_TRELLIS_P | MFX_TRELLIS_B);
1004 q->
extco2.Trellis = MFX_TRELLIS_UNKNOWN;
1007 q->
extco2.RepeatPPS = q->
repeat_pps ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1012 q->
extco2.ExtBRC = q->
extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1032 q->
extco2.AdaptiveI = q->
adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1034 q->
extco2.AdaptiveB = q->
adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1040 "qmin and or qmax are set but invalid,"
1041 " please make sure min <= max\n");
1044 if (avctx->
qmin >= 0) {
1049 if (avctx->
qmax >= 0) {
1073 q->
extco2.MBBRC = q->
mbbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1077 q->
extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
1083 q->
extco2.ExtBRC = q->
extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1087 q->
extco2.AdaptiveI = q->
adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1089 q->
extco2.AdaptiveB = q->
adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1093 q->
extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
1102 q->
extmfp.Header.BufferId = MFX_EXTBUFF_MULTI_FRAME_PARAM;
1111 q->
extco3.Header.BufferId = MFX_EXTBUFF_CODING_OPTION3;
1118 q->
extco3.PRefType = MFX_P_REF_DEFAULT;
1121 q->
extco3.PRefType = MFX_P_REF_SIMPLE;
1124 q->
extco3.PRefType = MFX_P_REF_PYRAMID;
1127 q->
extco3.PRefType = MFX_P_REF_DEFAULT;
1129 "invalid p_strategy, set to default\n");
1132 if (q->
extco3.PRefType == MFX_P_REF_PYRAMID &&
1135 "Please set max_b_frames(-bf) to 0 to enable P-pyramid\n");
1148 (q->
profile == MFX_PROFILE_HEVC_REXT ||
1149 q->
profile == MFX_PROFILE_UNKNOWN))
1150 q->
extco3.TargetChromaFormatPlus1 = MFX_CHROMAFORMAT_YUV444 + 1;
1162 MFX_CODINGOPTION_OFF;
1164 q->
extco3.TransformSkip = MFX_CODINGOPTION_UNKNOWN;
1165 q->
extco3.GPB = q->
gpb ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1171 q->
extvp9param.Header.BufferId = MFX_EXTBUFF_VP9_PARAM;
1173 q->
extvp9param.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
1174 #if QSV_HAVE_EXT_VP9_TILES
1181 #if QSV_HAVE_EXT_AV1_PARAM
1184 q->extav1tileparam.Header.BufferId = MFX_EXTBUFF_AV1_TILE_PARAM;
1185 q->extav1tileparam.Header.BufferSz =
sizeof(q->extav1tileparam);
1186 q->extav1tileparam.NumTileColumns = q->
tile_cols;
1187 q->extav1tileparam.NumTileRows = q->
tile_rows;
1190 q->extav1bsparam.Header.BufferId = MFX_EXTBUFF_AV1_BITSTREAM_PARAM;
1191 q->extav1bsparam.Header.BufferSz =
sizeof(q->extav1bsparam);
1192 q->extav1bsparam.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
1196 "This version of runtime doesn't support AV1 encoding\n");
1203 q->
exthevctiles.Header.BufferId = MFX_EXTBUFF_HEVC_TILES;
1211 q->
extvsi.ColourDescriptionPresent = 0;
1216 q->
extvsi.ColourDescriptionPresent = 1;
1228 q->
extvsi.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO;
1237 MFXQueryIMPL(q->
session, &impl);
1243 if (q->
param.mfx.LowPower != MFX_CODINGOPTION_ON) {
1244 av_log(avctx,
AV_LOG_ERROR,
"Dual GFX mode supports only low-power encoding mode \n");
1247 if (q->
param.mfx.CodecId != MFX_CODEC_AVC && q->
param.mfx.CodecId != MFX_CODEC_HEVC) {
1249 "Supported: h264_qsv and hevc_qsv \n");
1252 if (q->
param.mfx.RateControlMethod != MFX_RATECONTROL_VBR &&
1253 q->
param.mfx.RateControlMethod != MFX_RATECONTROL_CQP &&
1254 q->
param.mfx.RateControlMethod != MFX_RATECONTROL_ICQ) {
1256 "Supported: VBR, CQP and ICQ \n");
1258 if ((q->
param.mfx.CodecId == MFX_CODEC_AVC && q->
param.mfx.IdrInterval != 0) ||
1259 (q->
param.mfx.CodecId == MFX_CODEC_HEVC && q->
param.mfx.IdrInterval != 1)) {
1260 av_log(avctx,
AV_LOG_WARNING,
"Dual GFX mode requires closed GOP for AVC and strict GOP for HEVC, -idr_interval 0 \n");
1262 if (q->
param.mfx.GopPicSize < 30) {
1263 av_log(avctx,
AV_LOG_WARNING,
"For better performance in dual GFX mode GopPicSize must be >= 30 \n");
1265 if (q->
param.AsyncDepth < 30) {
1266 av_log(avctx,
AV_LOG_WARNING,
"For better performance in dual GFX mode AsyncDepth must be >= 30 \n");
1269 q->exthypermodeparam.Header.BufferId = MFX_EXTBUFF_HYPER_MODE_PARAM;
1270 q->exthypermodeparam.Header.BufferSz =
sizeof(q->exthypermodeparam);
1271 q->exthypermodeparam.Mode = q->
dual_gfx;
1275 "This version of runtime doesn't support Hyper Encode\n");
1283 "some encoding parameters are not supported by the QSV "
1284 "runtime. Please double check the input parameters.\n");
1298 "Error calling GetVideoParam");
1314 mfxExtVP9Param vp9_extend_buf = {
1315 .Header.BufferId = MFX_EXTBUFF_VP9_PARAM,
1316 .Header.BufferSz =
sizeof(vp9_extend_buf),
1319 mfxExtCodingOption2 co2 = {
1320 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1321 .Header.BufferSz =
sizeof(co2),
1324 mfxExtCodingOption3 co3 = {
1325 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1326 .Header.BufferSz =
sizeof(co3),
1329 mfxExtBuffer *ext_buffers[3];
1330 int ext_buf_num = 0;
1337 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co2;
1342 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co3;
1347 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&vp9_extend_buf;
1350 q->
param.ExtParam = ext_buffers;
1351 q->
param.NumExtParam = ext_buf_num;
1356 "Error calling GetVideoParam");
1367 #if QSV_HAVE_EXT_AV1_PARAM
1369 mfxExtAV1TileParam av1_extend_tile_buf = {
1370 .Header.BufferId = MFX_EXTBUFF_AV1_TILE_PARAM,
1371 .Header.BufferSz =
sizeof(av1_extend_tile_buf),
1373 mfxExtAV1BitstreamParam av1_bs_param = {
1374 .Header.BufferId = MFX_EXTBUFF_AV1_BITSTREAM_PARAM,
1375 .Header.BufferSz =
sizeof(av1_bs_param),
1378 mfxExtCodingOption2 co2 = {
1379 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1380 .Header.BufferSz =
sizeof(co2),
1383 mfxExtCodingOption3 co3 = {
1384 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1385 .Header.BufferSz =
sizeof(co3),
1388 mfxExtBuffer *ext_buffers[] = {
1389 (mfxExtBuffer*)&av1_extend_tile_buf,
1390 (mfxExtBuffer*)&av1_bs_param,
1391 (mfxExtBuffer*)&co2,
1392 (mfxExtBuffer*)&co3,
1397 "This version of runtime doesn't support AV1 encoding\n");
1401 q->
param.ExtParam = ext_buffers;
1407 "Error calling GetVideoParam");
1410 dump_video_av1_param(avctx, q, ext_buffers);
1419 uint8_t sps_buf[512];
1420 uint8_t pps_buf[128];
1422 mfxExtCodingOptionSPSPPS extradata = {
1423 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS,
1424 .Header.BufferSz =
sizeof(extradata),
1425 .SPSBuffer = sps_buf, .SPSBufSize =
sizeof(sps_buf),
1426 .PPSBuffer = pps_buf, .PPSBufSize =
sizeof(pps_buf)
1429 mfxExtCodingOption co = {
1430 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION,
1431 .Header.BufferSz =
sizeof(co),
1433 mfxExtCodingOption2 co2 = {
1434 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1435 .Header.BufferSz =
sizeof(co2),
1437 mfxExtCodingOption3 co3 = {
1438 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1439 .Header.BufferSz =
sizeof(co3),
1442 uint8_t vps_buf[128];
1443 mfxExtCodingOptionVPS extradata_vps = {
1444 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_VPS,
1445 .Header.BufferSz =
sizeof(extradata_vps),
1446 .VPSBuffer = vps_buf,
1447 .VPSBufSize =
sizeof(vps_buf),
1450 mfxExtHEVCTiles hevc_tile_buf = {
1451 .Header.BufferId = MFX_EXTBUFF_HEVC_TILES,
1452 .Header.BufferSz =
sizeof(hevc_tile_buf),
1456 mfxExtHyperModeParam hyper_mode_param_buf = {
1457 .Header.BufferId = MFX_EXTBUFF_HYPER_MODE_PARAM,
1458 .Header.BufferSz =
sizeof(hyper_mode_param_buf),
1465 int ret, ext_buf_num = 0, extradata_offset = 0;
1468 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata;
1469 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co;
1474 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co2;
1479 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co3;
1484 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata_vps;
1487 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&hevc_tile_buf;
1492 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&hyper_mode_param_buf;
1496 q->
param.ExtParam = ext_buffers;
1497 q->
param.NumExtParam = ext_buf_num;
1502 "Error calling GetVideoParam");
1506 if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)
1507 || (q->
hevc_vps && !extradata_vps.VPSBufSize)
1513 avctx->
extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize;
1521 memcpy(avctx->
extradata, vps_buf, extradata_vps.VPSBufSize);
1522 extradata_offset += extradata_vps.VPSBufSize;
1525 memcpy(avctx->
extradata + extradata_offset, sps_buf, extradata.SPSBufSize);
1526 extradata_offset += extradata.SPSBufSize;
1528 memcpy(avctx->
extradata + extradata_offset, pps_buf, extradata.PPSBufSize);
1529 extradata_offset += extradata.PPSBufSize;
1550 mfxFrameSurface1 *surfaces;
1564 for (
i = 0;
i < nb_surfaces;
i++) {
1565 surfaces[
i].Info = q->
req.Info;
1569 q->
opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
1600 q->
param.IOPattern == MFX_IOPATTERN_IN_OPAQUE_MEMORY,
1634 int opaque_alloc = 0;
1656 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
1657 iopattern = MFX_IOPATTERN_IN_OPAQUE_MEMORY;
1658 else if (frames_hwctx->frame_type &
1659 (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
1660 iopattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1662 if (frames_hwctx->frame_type &
1663 (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
1664 iopattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1670 iopattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
1671 q->
param.IOPattern = iopattern;
1681 "Error querying mfx version");
1715 if (j < qsv->nb_ext_buffers)
1726 if (
ret == MFX_WRN_PARTIAL_ACCELERATION) {
1728 }
else if (
ret < 0) {
1730 "Error querying encoder params");
1736 "Error querying (IOSurf) the encoding parameters");
1745 "however libmfx %d.%d doesn't support OPAQUE memory.\n",
1746 q->
ver.Major, q->
ver.Minor);
1754 "Error initializing the encoder");
1757 "Warning in encoder initialization");
1792 enc_ctrl->NumPayload = 0;
1793 enc_ctrl->NumExtParam = 0;
1831 last = &
frame->next;
1839 if (!
frame->frame) {
1856 int max_step[4], filled[4] = { 0 };
1862 for (
int i = 0;
i <
desc->nb_components;
i++) {
1864 int sheight, dheight, plane =
comp->plane;
1872 if (swidth < 0 || dwidth < 0) {
1890 for (
int j = 0; j < sheight; j++) {
1900 for (
int j = sheight; j < dheight; j++)
2034 MFX_PICSTRUCT_FIELD_BFF;
2036 qf->
surface.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
2038 qf->
surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
2040 qf->
surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
2057 if (q->
param.mfx.CodecId == MFX_CODEC_AVC) {
2058 if (q->
param.mfx.CodecProfile == MFX_PROFILE_AVC_BASELINE ||
2059 q->
param.mfx.CodecLevel < MFX_LEVEL_AVC_21 ||
2060 q->
param.mfx.CodecLevel > MFX_LEVEL_AVC_41)
2062 "Interlaced coding is supported"
2063 " at Main/High Profile Level 2.2-4.0\n");
2068 mfxEncodeCtrl *enc_ctrl)
2084 mfxExtEncoderROI *enc_roi =
NULL;
2091 if (!roi_size || sd->
size % roi_size) {
2095 nb_roi = sd->
size / roi_size;
2098 "supported by driver (%d > %d).\n",
2106 enc_roi->Header.BufferId = MFX_EXTBUFF_ENCODER_ROI;
2107 enc_roi->Header.BufferSz =
sizeof(*enc_roi);
2108 enc_roi->NumROI = nb_roi;
2109 enc_roi->ROIMode = MFX_ROI_MODE_QP_DELTA;
2110 for (
i = 0;
i < nb_roi;
i++) {
2116 enc_roi->ROI[
i].DeltaQP =
2120 enc_roi->ROI[
i].DeltaQP);
2122 enc_ctrl->ExtParam[enc_ctrl->NumExtParam] = (mfxExtBuffer *)enc_roi;
2123 enc_ctrl->NumExtParam++;
2129 mfxEncodeCtrl *enc_ctrl)
2135 if (!skip_frame_dict)
2137 enc_ctrl->SkipFrame = strtol(skip_frame_dict->
value,
NULL, 10);
2143 int updated = 0, new_qp = 0;
2148 if (q->
param.mfx.RateControlMethod == MFX_RATECONTROL_CQP) {
2164 "Reset qp = %d/%d/%d for idr/p/b frames\n",
2183 "Reset MaxFrameSize: %d;\n", q->
extco2.MaxFrameSize);
2197 q->
param.mfx.GopPicSize);
2218 q->
extco2.IntRefQPDelta =
2222 "Reset IntRefType: %d; IntRefCycleSize: %d; "
2223 "IntRefQPDelta: %d; IntRefCycleDist: %d\n",
2253 "qmin and or qmax are set but invalid,"
2254 " please make sure min <= max\n");
2264 if (avctx->
qmin >= 0) {
2268 if (avctx->
qmax >= 0) {
2286 "MinQPP: %d; MaxQPP: %d; "
2287 "MinQPB: %d; MaxQPB: %d\n",
2308 q->
extco3.LowDelayBRC = MFX_CODINGOPTION_UNKNOWN;
2334 q->
param.mfx.FrameInfo.FrameRateExtN,
2335 q->
param.mfx.FrameInfo.FrameRateExtD,
2336 (
double)q->
param.mfx.FrameInfo.FrameRateExtN / q->
param.mfx.FrameInfo.FrameRateExtD);
2344 int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier;
2345 int buffer_size_in_kilobytes, initial_delay_in_kilobytes;
2356 target_bitrate_kbps = avctx->
bit_rate / 1000;
2358 brc_param_multiplier = (
FFMAX(
FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
2359 initial_delay_in_kilobytes) + 0x10000) / 0x10000;
2361 q->
param.mfx.BufferSizeInKB = buffer_size_in_kilobytes / brc_param_multiplier;
2362 q->
param.mfx.InitialDelayInKB = initial_delay_in_kilobytes / brc_param_multiplier;
2363 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
2364 q->
param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier;
2365 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
2367 "Reset BufferSizeInKB: %d; InitialDelayInKB: %d; "
2368 "TargetKbps: %d; MaxKbps: %d; BRCParamMultiplier: %d\n",
2369 q->
param.mfx.BufferSizeInKB, q->
param.mfx.InitialDelayInKB,
2370 q->
param.mfx.TargetKbps, q->
param.mfx.MaxKbps, q->
param.mfx.BRCParamMultiplier);
2386 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
2397 mfxExtAVCEncodedFrameInfo *enc_info =
NULL;
2398 mfxExtBuffer **enc_buf =
NULL;
2400 mfxFrameSurface1 *surf =
NULL;
2402 mfxEncodeCtrl* enc_ctrl =
NULL;
2417 enc_ctrl->FrameType = MFX_FRAMETYPE_I | MFX_FRAMETYPE_REF;
2419 enc_ctrl->FrameType |= MFX_FRAMETYPE_IDR;
2440 enc_info->Header.BufferId = MFX_EXTBUFF_ENCODED_FRAME_INFO;
2441 enc_info->Header.BufferSz =
sizeof (*enc_info);
2442 pkt.bs->NumExtParam = 1;
2443 enc_buf =
av_mallocz(
sizeof(mfxExtBuffer *));
2446 enc_buf[0] = (mfxExtBuffer *)enc_info;
2448 pkt.bs->ExtParam = enc_buf;
2473 ret = MFXVideoENCODE_EncodeFrameAsync(q->
session, enc_ctrl, surf,
pkt.bs,
pkt.sync);
2474 if (
ret == MFX_WRN_DEVICE_BUSY)
2476 }
while (
ret == MFX_WRN_DEVICE_BUSY ||
ret == MFX_WRN_IN_EXECUTION);
2482 ret = (
ret == MFX_ERR_MORE_DATA) ?
2516 int needReset = 0,
ret = 0;
2549 if (j < qsv->nb_ext_buffers)
2589 mfxExtAVCEncodedFrameInfo *enc_info;
2590 mfxExtBuffer **enc_buf;
2597 }
while (
ret == MFX_WRN_IN_EXECUTION);
2603 if (qpkt.
bs->FrameType & MFX_FRAMETYPE_IDR || qpkt.
bs->FrameType & MFX_FRAMETYPE_xIDR) {
2606 }
else if (qpkt.
bs->FrameType & MFX_FRAMETYPE_I || qpkt.
bs->FrameType & MFX_FRAMETYPE_xI) {
2610 }
else if (qpkt.
bs->FrameType & MFX_FRAMETYPE_P || qpkt.
bs->FrameType & MFX_FRAMETYPE_xP)
2612 else if (qpkt.
bs->FrameType & MFX_FRAMETYPE_B || qpkt.
bs->FrameType & MFX_FRAMETYPE_xB)
2614 else if (qpkt.
bs->FrameType == MFX_FRAMETYPE_UNKNOWN && qpkt.
bs->DataLength) {
2623 enc_buf = qpkt.
bs->ExtParam;
2624 enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);
2646 MFXVideoENCODE_Close(q->
session);
2667 mfxExtBuffer **enc_buf =
pkt.bs->ExtParam;
2668 mfxExtAVCEncodedFrameInfo *enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);