24 #include "config_components.h"
27 #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)";
605 rc_desc =
"VBR with lookahead (LA)";
608 rc_mode = MFX_RATECONTROL_LA_ICQ;
609 rc_desc =
"intelligent constant quality with lookahead (LA_ICQ)";
614 rc_desc =
"intelligent constant quality (ICQ)";
618 rc_desc =
"constant bitrate (CBR)";
625 rc_mode = MFX_RATECONTROL_AVBR;
626 rc_desc =
"average variable bitrate (AVBR)";
630 rc_mode = MFX_RATECONTROL_QVBR;
631 rc_desc =
"constant quality with VBR algorithm (QVBR)";
635 rc_desc =
"variable bitrate (VBR)";
646 mfxVideoParam param_out = { .mfx.CodecId = q->
param.mfx.CodecId };
649 #define UNMATCH(x) (param_out.mfx.x != q->param.mfx.x)
658 if (
UNMATCH(RateControlMethod))
693 if (avctx->
level > 0)
705 q->
param.mfx.FrameInfo.CropX = 0;
706 q->
param.mfx.FrameInfo.CropY = 0;
711 q->
param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420 +
712 !
desc->log2_chroma_w + !
desc->log2_chroma_h;
713 q->
param.mfx.FrameInfo.BitDepthLuma =
desc->comp[0].depth;
714 q->
param.mfx.FrameInfo.BitDepthChroma =
desc->comp[0].depth;
722 q->
param.mfx.FrameInfo.Width = frames_hwctx->surfaces[0].Info.Width;
723 q->
param.mfx.FrameInfo.Height = frames_hwctx->surfaces[0].Info.Height;
734 q->
param.mfx.Interleaved = 1;
736 q->
param.mfx.RestartInterval = 0;
753 int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier;
754 int buffer_size_in_kilobytes, initial_delay_in_kilobytes;
762 if (avctx->
level > 0) {
773 "valid range is 0-%d, using %d instead\n",
774 MFX_TARGETUSAGE_BEST_SPEED, MFX_TARGETUSAGE_BEST_SPEED);
780 q->
param.mfx.LowPower = MFX_CODINGOPTION_ON;
782 q->
param.mfx.LowPower = MFX_CODINGOPTION_UNKNOWN;
784 q->
param.mfx.LowPower = MFX_CODINGOPTION_OFF;
797 q->
param.mfx.EncodedOrder = 0;
798 q->
param.mfx.BufferSizeInKB = 0;
808 q->
param.mfx.FrameInfo.CropX = 0;
809 q->
param.mfx.FrameInfo.CropY = 0;
814 q->
param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420 +
815 !
desc->log2_chroma_w + !
desc->log2_chroma_h;
816 q->
param.mfx.FrameInfo.BitDepthLuma =
desc->comp[0].depth;
817 q->
param.mfx.FrameInfo.BitDepthChroma =
desc->comp[0].depth;
829 q->
param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
833 q->
param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
844 q->
param.mfx.FrameInfo.Width = frames_hwctx->surfaces[0].Info.Width;
845 q->
param.mfx.FrameInfo.Height = frames_hwctx->surfaces[0].Info.Height;
864 target_bitrate_kbps = avctx->
bit_rate / 1000;
866 brc_param_multiplier = (
FFMAX(
FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
867 initial_delay_in_kilobytes) + 0x10000) / 0x10000;
873 switch (q->
param.mfx.RateControlMethod) {
874 case MFX_RATECONTROL_CBR:
875 case MFX_RATECONTROL_VBR:
880 case MFX_RATECONTROL_VCM:
882 case MFX_RATECONTROL_QVBR:
883 q->
param.mfx.BufferSizeInKB = buffer_size_in_kilobytes / brc_param_multiplier;
884 q->
param.mfx.InitialDelayInKB = initial_delay_in_kilobytes / brc_param_multiplier;
885 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
886 q->
param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier;
887 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
888 if (q->
param.mfx.RateControlMethod == MFX_RATECONTROL_QVBR)
891 case MFX_RATECONTROL_CQP:
910 case MFX_RATECONTROL_AVBR:
911 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
914 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
917 case MFX_RATECONTROL_LA:
918 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
920 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
922 case MFX_RATECONTROL_LA_ICQ:
924 case MFX_RATECONTROL_ICQ:
934 q->
extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
938 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
942 q->
extco.RateDistortionOpt = q->
rdo > 0 ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
945 q->
extco.CAVLC = q->
cavlc ? MFX_CODINGOPTION_ON
946 : MFX_CODINGOPTION_UNKNOWN;
950 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
957 q->
extco.AUDelimiter = q->
aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
961 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
966 q->
extco.AUDelimiter = q->
aud ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
976 q->
extco2.Trellis = (avctx->
trellis == 0) ? MFX_TRELLIS_OFF : (MFX_TRELLIS_I | MFX_TRELLIS_P | MFX_TRELLIS_B);
978 q->
extco2.Trellis = MFX_TRELLIS_UNKNOWN;
981 q->
extco2.RepeatPPS = q->
repeat_pps ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
986 q->
extco2.ExtBRC = q->
extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1006 q->
extco2.AdaptiveI = q->
adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1008 q->
extco2.AdaptiveB = q->
adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1014 "qmin and or qmax are set but invalid,"
1015 " please make sure min <= max\n");
1018 if (avctx->
qmin >= 0) {
1023 if (avctx->
qmax >= 0) {
1047 q->
extco2.MBBRC = q->
mbbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1051 q->
extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
1057 q->
extco2.ExtBRC = q->
extbrc ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1061 q->
extco2.AdaptiveI = q->
adaptive_i ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1063 q->
extco2.AdaptiveB = q->
adaptive_b ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1067 q->
extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
1076 q->
extmfp.Header.BufferId = MFX_EXTBUFF_MULTI_FRAME_PARAM;
1085 q->
extco3.Header.BufferId = MFX_EXTBUFF_CODING_OPTION3;
1092 q->
extco3.PRefType = MFX_P_REF_DEFAULT;
1095 q->
extco3.PRefType = MFX_P_REF_SIMPLE;
1098 q->
extco3.PRefType = MFX_P_REF_PYRAMID;
1101 q->
extco3.PRefType = MFX_P_REF_DEFAULT;
1103 "invalid p_strategy, set to default\n");
1106 if (q->
extco3.PRefType == MFX_P_REF_PYRAMID &&
1109 "Please set max_b_frames(-bf) to 0 to enable P-pyramid\n");
1131 MFX_CODINGOPTION_OFF;
1133 q->
extco3.TransformSkip = MFX_CODINGOPTION_UNKNOWN;
1134 q->
extco3.GPB = q->
gpb ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
1140 q->
extvp9param.Header.BufferId = MFX_EXTBUFF_VP9_PARAM;
1142 q->
extvp9param.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
1143 #if QSV_HAVE_EXT_VP9_TILES
1150 #if QSV_HAVE_EXT_AV1_PARAM
1153 q->extav1tileparam.Header.BufferId = MFX_EXTBUFF_AV1_TILE_PARAM;
1154 q->extav1tileparam.Header.BufferSz =
sizeof(q->extav1tileparam);
1155 q->extav1tileparam.NumTileColumns = q->
tile_cols;
1156 q->extav1tileparam.NumTileRows = q->
tile_rows;
1159 q->extav1bsparam.Header.BufferId = MFX_EXTBUFF_AV1_BITSTREAM_PARAM;
1160 q->extav1bsparam.Header.BufferSz =
sizeof(q->extav1bsparam);
1161 q->extav1bsparam.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
1165 "This version of runtime doesn't support AV1 encoding\n");
1172 q->
exthevctiles.Header.BufferId = MFX_EXTBUFF_HEVC_TILES;
1180 q->
extvsi.ColourDescriptionPresent = 0;
1185 q->
extvsi.ColourDescriptionPresent = 1;
1197 q->
extvsi.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO;
1206 MFXQueryIMPL(q->
session, &impl);
1212 if (q->
param.mfx.LowPower != MFX_CODINGOPTION_ON) {
1213 av_log(avctx,
AV_LOG_ERROR,
"Dual GFX mode supports only low-power encoding mode \n");
1216 if (q->
param.mfx.CodecId != MFX_CODEC_AVC && q->
param.mfx.CodecId != MFX_CODEC_HEVC) {
1218 "Supported: h264_qsv and hevc_qsv \n");
1221 if (q->
param.mfx.RateControlMethod != MFX_RATECONTROL_VBR &&
1222 q->
param.mfx.RateControlMethod != MFX_RATECONTROL_CQP &&
1223 q->
param.mfx.RateControlMethod != MFX_RATECONTROL_ICQ) {
1225 "Supported: VBR, CQP and ICQ \n");
1227 if ((q->
param.mfx.CodecId == MFX_CODEC_AVC && q->
param.mfx.IdrInterval != 0) ||
1228 (q->
param.mfx.CodecId == MFX_CODEC_HEVC && q->
param.mfx.IdrInterval != 1)) {
1229 av_log(avctx,
AV_LOG_WARNING,
"Dual GFX mode requires closed GOP for AVC and strict GOP for HEVC, -idr_interval 0 \n");
1231 if (q->
param.mfx.GopPicSize < 30) {
1232 av_log(avctx,
AV_LOG_WARNING,
"For better performance in dual GFX mode GopPicSize must be >= 30 \n");
1234 if (q->
param.AsyncDepth < 30) {
1235 av_log(avctx,
AV_LOG_WARNING,
"For better performance in dual GFX mode AsyncDepth must be >= 30 \n");
1238 q->exthypermodeparam.Header.BufferId = MFX_EXTBUFF_HYPER_MODE_PARAM;
1239 q->exthypermodeparam.Header.BufferSz =
sizeof(q->exthypermodeparam);
1240 q->exthypermodeparam.Mode = q->
dual_gfx;
1244 "This version of runtime doesn't support Hyper Encode\n");
1252 "some encoding parameters are not supported by the QSV "
1253 "runtime. Please double check the input parameters.\n");
1267 "Error calling GetVideoParam");
1283 mfxExtVP9Param vp9_extend_buf = {
1284 .Header.BufferId = MFX_EXTBUFF_VP9_PARAM,
1285 .Header.BufferSz =
sizeof(vp9_extend_buf),
1288 mfxExtCodingOption2 co2 = {
1289 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1290 .Header.BufferSz =
sizeof(co2),
1293 mfxExtCodingOption3 co3 = {
1294 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1295 .Header.BufferSz =
sizeof(co3),
1298 mfxExtBuffer *ext_buffers[3];
1299 int ext_buf_num = 0;
1306 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co2;
1311 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co3;
1316 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&vp9_extend_buf;
1319 q->
param.ExtParam = ext_buffers;
1320 q->
param.NumExtParam = ext_buf_num;
1325 "Error calling GetVideoParam");
1336 #if QSV_HAVE_EXT_AV1_PARAM
1338 mfxExtAV1TileParam av1_extend_tile_buf = {
1339 .Header.BufferId = MFX_EXTBUFF_AV1_TILE_PARAM,
1340 .Header.BufferSz =
sizeof(av1_extend_tile_buf),
1342 mfxExtAV1BitstreamParam av1_bs_param = {
1343 .Header.BufferId = MFX_EXTBUFF_AV1_BITSTREAM_PARAM,
1344 .Header.BufferSz =
sizeof(av1_bs_param),
1347 mfxExtCodingOption2 co2 = {
1348 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1349 .Header.BufferSz =
sizeof(co2),
1352 mfxExtCodingOption3 co3 = {
1353 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1354 .Header.BufferSz =
sizeof(co3),
1357 mfxExtBuffer *ext_buffers[] = {
1358 (mfxExtBuffer*)&av1_extend_tile_buf,
1359 (mfxExtBuffer*)&av1_bs_param,
1360 (mfxExtBuffer*)&co2,
1361 (mfxExtBuffer*)&co3,
1366 "This version of runtime doesn't support AV1 encoding\n");
1370 q->
param.ExtParam = ext_buffers;
1376 "Error calling GetVideoParam");
1379 dump_video_av1_param(avctx, q, ext_buffers);
1388 uint8_t sps_buf[512];
1389 uint8_t pps_buf[128];
1391 mfxExtCodingOptionSPSPPS extradata = {
1392 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS,
1393 .Header.BufferSz =
sizeof(extradata),
1394 .SPSBuffer = sps_buf, .SPSBufSize =
sizeof(sps_buf),
1395 .PPSBuffer = pps_buf, .PPSBufSize =
sizeof(pps_buf)
1398 mfxExtCodingOption co = {
1399 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION,
1400 .Header.BufferSz =
sizeof(co),
1402 mfxExtCodingOption2 co2 = {
1403 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
1404 .Header.BufferSz =
sizeof(co2),
1406 mfxExtCodingOption3 co3 = {
1407 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION3,
1408 .Header.BufferSz =
sizeof(co3),
1411 uint8_t vps_buf[128];
1412 mfxExtCodingOptionVPS extradata_vps = {
1413 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_VPS,
1414 .Header.BufferSz =
sizeof(extradata_vps),
1415 .VPSBuffer = vps_buf,
1416 .VPSBufSize =
sizeof(vps_buf),
1419 mfxExtHEVCTiles hevc_tile_buf = {
1420 .Header.BufferId = MFX_EXTBUFF_HEVC_TILES,
1421 .Header.BufferSz =
sizeof(hevc_tile_buf),
1425 mfxExtHyperModeParam hyper_mode_param_buf = {
1426 .Header.BufferId = MFX_EXTBUFF_HYPER_MODE_PARAM,
1427 .Header.BufferSz =
sizeof(hyper_mode_param_buf),
1434 int ret, ext_buf_num = 0, extradata_offset = 0;
1437 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata;
1438 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co;
1443 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co2;
1448 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&co3;
1453 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&extradata_vps;
1456 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&hevc_tile_buf;
1461 ext_buffers[ext_buf_num++] = (mfxExtBuffer*)&hyper_mode_param_buf;
1465 q->
param.ExtParam = ext_buffers;
1466 q->
param.NumExtParam = ext_buf_num;
1471 "Error calling GetVideoParam");
1475 if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)
1476 || (q->
hevc_vps && !extradata_vps.VPSBufSize)
1482 avctx->
extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize;
1490 memcpy(avctx->
extradata, vps_buf, extradata_vps.VPSBufSize);
1491 extradata_offset += extradata_vps.VPSBufSize;
1494 memcpy(avctx->
extradata + extradata_offset, sps_buf, extradata.SPSBufSize);
1495 extradata_offset += extradata.SPSBufSize;
1497 memcpy(avctx->
extradata + extradata_offset, pps_buf, extradata.PPSBufSize);
1498 extradata_offset += extradata.PPSBufSize;
1519 mfxFrameSurface1 *surfaces;
1533 for (
i = 0;
i < nb_surfaces;
i++) {
1534 surfaces[
i].Info = q->
req.Info;
1538 q->
opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
1569 q->
param.IOPattern == MFX_IOPATTERN_IN_OPAQUE_MEMORY,
1603 int opaque_alloc = 0;
1625 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
1626 iopattern = MFX_IOPATTERN_IN_OPAQUE_MEMORY;
1627 else if (frames_hwctx->frame_type &
1628 (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
1629 iopattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1631 if (frames_hwctx->frame_type &
1632 (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET))
1633 iopattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1639 iopattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
1640 q->
param.IOPattern = iopattern;
1650 "Error querying mfx version");
1684 if (j < qsv->nb_ext_buffers)
1695 if (
ret == MFX_WRN_PARTIAL_ACCELERATION) {
1697 }
else if (
ret < 0) {
1699 "Error querying encoder params");
1705 "Error querying (IOSurf) the encoding parameters");
1714 "however libmfx %d.%d doesn't support OPAQUE memory.\n",
1715 q->
ver.Major, q->
ver.Minor);
1723 "Error initializing the encoder");
1726 "Warning in encoder initialization");
1761 enc_ctrl->NumPayload = 0;
1762 enc_ctrl->NumExtParam = 0;
1800 last = &
frame->next;
1808 if (!
frame->frame) {
1825 int max_step[4], filled[4] = { 0 };
1831 for (
int i = 0;
i <
desc->nb_components;
i++) {
1833 int sheight, dheight, plane =
comp->plane;
1841 if (swidth < 0 || dwidth < 0) {
1849 sheight =
frame->height;
1858 if (new_w >
frame->width) {
1859 for (
int j = 0; j < sheight; j++) {
1860 void *line_ptr =
frame->data[plane] + j *
frame->linesize[plane] + swidth;
1864 new_w -
frame->width);
1869 for (
int j = sheight; j < dheight; j++)
1870 memcpy(
frame->data[plane] + j *
frame->linesize[plane],
1871 frame->data[plane] + (sheight - 1) *
frame->linesize[plane],
1945 !
frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE :
1946 frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF :
1947 MFX_PICSTRUCT_FIELD_BFF;
1948 if (
frame->repeat_pict == 1)
1949 qf->
surface.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
1950 else if (
frame->repeat_pict == 2)
1951 qf->
surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
1952 else if (
frame->repeat_pict == 4)
1953 qf->
surface.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
1970 if (q->
param.mfx.CodecId == MFX_CODEC_AVC) {
1971 if (q->
param.mfx.CodecProfile == MFX_PROFILE_AVC_BASELINE ||
1972 q->
param.mfx.CodecLevel < MFX_LEVEL_AVC_21 ||
1973 q->
param.mfx.CodecLevel > MFX_LEVEL_AVC_41)
1975 "Interlaced coding is supported"
1976 " at Main/High Profile Level 2.2-4.0\n");
1981 mfxEncodeCtrl *enc_ctrl)
1997 mfxExtEncoderROI *enc_roi =
NULL;
2004 if (!roi_size || sd->
size % roi_size) {
2008 nb_roi = sd->
size / roi_size;
2011 "supported by driver (%d > %d).\n",
2019 enc_roi->Header.BufferId = MFX_EXTBUFF_ENCODER_ROI;
2020 enc_roi->Header.BufferSz =
sizeof(*enc_roi);
2021 enc_roi->NumROI = nb_roi;
2022 enc_roi->ROIMode = MFX_ROI_MODE_QP_DELTA;
2023 for (
i = 0;
i < nb_roi;
i++) {
2029 enc_roi->ROI[
i].DeltaQP =
2033 enc_roi->ROI[
i].DeltaQP);
2035 enc_ctrl->ExtParam[enc_ctrl->NumExtParam] = (mfxExtBuffer *)enc_roi;
2036 enc_ctrl->NumExtParam++;
2042 mfxEncodeCtrl *enc_ctrl)
2045 if (!
frame->metadata)
2048 if (!skip_frame_dict)
2050 enc_ctrl->SkipFrame = strtol(skip_frame_dict->
value,
NULL, 10);
2056 int updated = 0, new_qp = 0;
2061 if (q->
param.mfx.RateControlMethod == MFX_RATECONTROL_CQP) {
2077 "Reset qp = %d/%d/%d for idr/p/b frames\n",
2096 "Reset MaxFrameSize: %d;\n", q->
extco2.MaxFrameSize);
2110 q->
param.mfx.GopPicSize);
2131 q->
extco2.IntRefQPDelta =
2135 "Reset IntRefType: %d; IntRefCycleSize: %d; "
2136 "IntRefQPDelta: %d; IntRefCycleDist: %d\n",
2166 "qmin and or qmax are set but invalid,"
2167 " please make sure min <= max\n");
2177 if (avctx->
qmin >= 0) {
2181 if (avctx->
qmax >= 0) {
2199 "MinQPP: %d; MaxQPP: %d; "
2200 "MinQPB: %d; MaxQPB: %d\n",
2219 q->
extco3.LowDelayBRC = MFX_CODINGOPTION_UNKNOWN;
2245 q->
param.mfx.FrameInfo.FrameRateExtN,
2246 q->
param.mfx.FrameInfo.FrameRateExtD,
2247 (
double)q->
param.mfx.FrameInfo.FrameRateExtN / q->
param.mfx.FrameInfo.FrameRateExtD);
2255 int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier;
2256 int buffer_size_in_kilobytes, initial_delay_in_kilobytes;
2267 target_bitrate_kbps = avctx->
bit_rate / 1000;
2269 brc_param_multiplier = (
FFMAX(
FFMAX3(target_bitrate_kbps, max_bitrate_kbps, buffer_size_in_kilobytes),
2270 initial_delay_in_kilobytes) + 0x10000) / 0x10000;
2272 q->
param.mfx.BufferSizeInKB = buffer_size_in_kilobytes / brc_param_multiplier;
2273 q->
param.mfx.InitialDelayInKB = initial_delay_in_kilobytes / brc_param_multiplier;
2274 q->
param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier;
2275 q->
param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier;
2276 q->
param.mfx.BRCParamMultiplier = brc_param_multiplier;
2278 "Reset BufferSizeInKB: %d; InitialDelayInKB: %d; "
2279 "TargetKbps: %d; MaxKbps: %d; BRCParamMultiplier: %d\n",
2280 q->
param.mfx.BufferSizeInKB, q->
param.mfx.InitialDelayInKB,
2281 q->
param.mfx.TargetKbps, q->
param.mfx.MaxKbps, q->
param.mfx.BRCParamMultiplier);
2297 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
2308 mfxExtAVCEncodedFrameInfo *enc_info =
NULL;
2309 mfxExtBuffer **enc_buf =
NULL;
2311 mfxFrameSurface1 *surf =
NULL;
2313 mfxEncodeCtrl* enc_ctrl =
NULL;
2328 enc_ctrl->FrameType = MFX_FRAMETYPE_I | MFX_FRAMETYPE_REF;
2330 enc_ctrl->FrameType |= MFX_FRAMETYPE_IDR;
2351 enc_info->Header.BufferId = MFX_EXTBUFF_ENCODED_FRAME_INFO;
2352 enc_info->Header.BufferSz =
sizeof (*enc_info);
2353 pkt.bs->NumExtParam = 1;
2354 enc_buf =
av_mallocz(
sizeof(mfxExtBuffer *));
2357 enc_buf[0] = (mfxExtBuffer *)enc_info;
2359 pkt.bs->ExtParam = enc_buf;
2384 ret = MFXVideoENCODE_EncodeFrameAsync(q->
session, enc_ctrl, surf,
pkt.bs,
pkt.sync);
2385 if (
ret == MFX_WRN_DEVICE_BUSY)
2387 }
while (
ret == MFX_WRN_DEVICE_BUSY ||
ret == MFX_WRN_IN_EXECUTION);
2393 ret = (
ret == MFX_ERR_MORE_DATA) ?
2398 if (
ret == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM &&
frame &&
frame->interlaced_frame)
2427 int needReset = 0,
ret = 0;
2460 if (j < qsv->nb_ext_buffers)
2500 mfxExtAVCEncodedFrameInfo *enc_info;
2501 mfxExtBuffer **enc_buf;
2508 }
while (
ret == MFX_WRN_IN_EXECUTION);
2514 if (qpkt.
bs->FrameType & MFX_FRAMETYPE_IDR || qpkt.
bs->FrameType & MFX_FRAMETYPE_xIDR) {
2517 }
else if (qpkt.
bs->FrameType & MFX_FRAMETYPE_I || qpkt.
bs->FrameType & MFX_FRAMETYPE_xI)
2519 else if (qpkt.
bs->FrameType & MFX_FRAMETYPE_P || qpkt.
bs->FrameType & MFX_FRAMETYPE_xP)
2521 else if (qpkt.
bs->FrameType & MFX_FRAMETYPE_B || qpkt.
bs->FrameType & MFX_FRAMETYPE_xB)
2523 else if (qpkt.
bs->FrameType == MFX_FRAMETYPE_UNKNOWN) {
2532 enc_buf = qpkt.
bs->ExtParam;
2533 enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);
2555 MFXVideoENCODE_Close(q->
session);
2576 mfxExtBuffer **enc_buf =
pkt.bs->ExtParam;
2577 mfxExtAVCEncodedFrameInfo *enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);