20 #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0602
22 #define _WIN32_WINNT 0x0602
63 #define MF_TIMEBASE (AVRational){1, 10000000}
65 #define MF_INVALID_TIME AV_NOPTS_VALUE
74 while (!(
c->async_need_input ||
c->async_have_output ||
c->draining_done ||
c->async_marker)) {
75 IMFMediaEvent *ev =
NULL;
76 MediaEventType ev_id = 0;
77 HRESULT hr = IMFMediaEventGenerator_GetEvent(
c->async_events, 0, &ev);
83 IMFMediaEvent_GetType(ev, &ev_id);
87 c->async_need_input = 1;
90 c->async_have_output = 1;
100 IMFMediaEvent_Release(ev);
124 IMFSample_SetSampleTime(
sample, stime);
135 HRESULT hr = IMFSample_GetSampleTime(
sample, &
pts);
148 hr = IMFAttributes_GetBlobSize(
type, &MF_MT_USER_DATA, &sz);
149 if (!FAILED(hr) && sz > 0) {
169 if (!
c->out_info.cbSize && !
c->out_stream_provides_samples) {
170 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &sz);
173 "assuming %d bytes instead.\n", (
int)sz);
174 c->out_info.cbSize = sz;
186 hr = IMFAttributes_GetBlobSize(
type, &MF_MT_MPEG_SEQUENCE_HEADER, &sz);
187 if (!FAILED(hr) && sz > 0) {
191 hr = IMFAttributes_GetBlob(
type, &MF_MT_MPEG_SEQUENCE_HEADER, extradata, sz,
NULL);
211 hr = IMFTransform_GetOutputCurrentType(
c->mft,
c->out_stream_id, &
type);
223 }
else if (
c->is_audio) {
230 IMFMediaType_Release(
type);
245 hr = IMFSample_GetTotalLength(
sample, &
len);
252 hr = IMFSample_ConvertToContiguousBuffer(
sample, &
buffer);
258 IMFMediaBuffer_Release(
buffer);
264 IMFMediaBuffer_Unlock(
buffer);
265 IMFMediaBuffer_Release(
buffer);
269 hr = IMFAttributes_GetUINT32(
sample, &MFSampleExtension_CleanPoint, &t32);
270 if (
c->is_audio || (!FAILED(hr) && t32 != 0))
273 hr = IMFAttributes_GetUINT64(
sample, &MFSampleExtension_DecodeTimestamp, &t);
281 c->reorder_delay = avpkt->
pts - avpkt->
dts;
282 avpkt->
dts -=
c->reorder_delay;
283 avpkt->
pts -=
c->reorder_delay;
300 c->in_info.cbAlignment);
321 c->in_info.cbAlignment);
327 IMFSample_Release(
sample);
333 IMFMediaBuffer_Release(
buffer);
334 IMFSample_Release(
sample);
340 IMFMediaBuffer_SetCurrentLength(
buffer,
size);
341 IMFMediaBuffer_Unlock(
buffer);
342 IMFMediaBuffer_Release(
buffer);
344 IMFSample_Release(
sample);
377 if (
c->async_events) {
380 if (!
c->async_need_input)
384 IMFSample_SetUINT32(
sample, &MFSampleExtension_Discontinuity, TRUE);
386 hr = IMFTransform_ProcessInput(
c->mft,
c->in_stream_id,
sample, 0);
387 if (hr == MF_E_NOTACCEPTING) {
389 }
else if (FAILED(hr)) {
393 c->async_need_input = 0;
394 }
else if (!
c->draining) {
395 hr = IMFTransform_ProcessMessage(
c->mft, MFT_MESSAGE_COMMAND_DRAIN, 0);
401 c->async_need_input = 0;
413 MFT_OUTPUT_DATA_BUFFER out_buffers;
421 if (
c->async_events) {
424 if (!
c->async_have_output ||
c->draining_done) {
430 if (!
c->out_stream_provides_samples) {
433 c->out_info.cbAlignment);
438 out_buffers = (MFT_OUTPUT_DATA_BUFFER) {
439 .dwStreamID =
c->out_stream_id,
444 hr = IMFTransform_ProcessOutput(
c->mft, 0, 1, &out_buffers, &st);
446 if (out_buffers.pEvents)
447 IMFCollection_Release(out_buffers.pEvents);
450 *out_sample = out_buffers.pSample;
455 if (out_buffers.pSample)
456 IMFSample_Release(out_buffers.pSample);
458 if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
460 c->draining_done = 1;
462 }
else if (hr == MF_E_TRANSFORM_STREAM_CHANGE) {
470 c->async_have_output = 0;
482 c->async_have_output = 0;
484 if (
ret >= 0 && !*out_sample)
496 if (!
c->frame->buf[0]) {
502 if (
c->frame->buf[0]) {
508 if (
c->is_video &&
c->codec_api) {
510 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncVideoForceKeyFrame,
FF_VAL_VT_UI4(1));
516 IMFSample_Release(
sample);
527 IMFSample_Release(
sample);
543 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t);
547 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_NUM_CHANNELS, &t);
551 hr = IMFAttributes_GetGUID(
type, &MF_MT_SUBTYPE, &tg);
553 if (IsEqualGUID(&
c->main_subtype, &tg))
558 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &t);
562 score |= (1LL << 31) -
diff;
564 score |= (1LL << 30) +
diff;
568 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AAC_PAYLOAD_TYPE, &t);
569 if (!FAILED(hr) && t != 0)
598 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t);
602 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_NUM_CHANNELS, &t);
620 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t);
626 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_NUM_CHANNELS, &t);
642 hr = IMFAttributes_GetGUID(
type, &MF_MT_SUBTYPE, &tg);
644 if (IsEqualGUID(&
c->main_subtype, &tg))
657 IMFAttributes_SetUINT32(
type, &MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive);
663 #if FF_API_TICKS_PER_FRAME
683 IMFAttributes_SetUINT32(
type, &MF_MT_MPEG2_PROFILE,
profile);
686 IMFAttributes_SetUINT32(
type, &MF_MT_AVG_BITRATE, avctx->
bit_rate);
693 if (
c->opt_enc_rc >= 0)
694 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncCommonRateControlMode,
FF_VAL_VT_UI4(
c->opt_enc_rc));
696 if (
c->opt_enc_quality >= 0)
697 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncCommonQuality,
FF_VAL_VT_UI4(
c->opt_enc_quality));
723 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncH264CABACEnable,
FF_VAL_VT_BOOL(1));
725 if (
c->opt_enc_scenario >= 0)
726 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVScenarioInfo,
FF_VAL_VT_UI4(
c->opt_enc_scenario));
759 IMFMediaType *out_type =
NULL;
761 int out_type_index = -1;
769 hr = IMFTransform_GetOutputAvailableType(
c->mft,
c->out_stream_id, n, &
type);
770 if (hr == MF_E_NO_MORE_TYPES || hr == E_NOTIMPL)
772 if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
788 }
else if (
c->is_audio) {
792 if (score > out_type_score) {
794 IMFMediaType_Release(out_type);
796 out_type_score = score;
798 IMFMediaType_AddRef(out_type);
801 IMFMediaType_Release(
type);
807 hr =
c->functions.MFCreateMediaType(&out_type);
817 }
else if (
c->is_audio) {
825 hr = IMFTransform_SetOutputType(
c->mft,
c->out_stream_id, out_type, 0);
828 }
else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
839 IMFMediaType_Release(out_type);
848 IMFMediaType *in_type =
NULL;
850 int in_type_index = -1;
858 hr = IMFTransform_GetInputAvailableType(
c->mft,
c->in_stream_id, n, &
type);
859 if (hr == MF_E_NO_MORE_TYPES || hr == E_NOTIMPL)
861 if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
877 }
else if (
c->is_audio) {
881 if (score > in_type_score) {
883 IMFMediaType_Release(in_type);
885 in_type_score = score;
887 IMFMediaType_AddRef(in_type);
890 IMFMediaType_Release(
type);
905 }
else if (
c->is_audio) {
913 hr = IMFTransform_SetInputType(
c->mft,
c->in_stream_id, in_type, 0);
916 }
else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
927 IMFMediaType_Release(in_type);
937 int need_input = 1, need_output = 1;
939 for (n = 0; n < 2 && (need_input || need_output); n++) {
944 need_input =
ret < 1;
948 need_output =
ret < 1;
950 if (need_input || need_output) {
952 need_input, need_output);
964 hr = IMFTransform_GetInputStreamInfo(
c->mft,
c->in_stream_id, &
c->in_info);
968 (
int)
c->in_info.cbSize, (
int)
c->in_info.cbAlignment);
970 hr = IMFTransform_GetOutputStreamInfo(
c->mft,
c->out_stream_id, &
c->out_info);
973 c->out_stream_provides_samples =
974 (
c->out_info.dwFlags & MFT_OUTPUT_STREAM_PROVIDES_SAMPLES) ||
975 (
c->out_info.dwFlags & MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES);
977 (
int)
c->out_info.cbSize, (
int)
c->out_info.cbAlignment,
978 c->out_stream_provides_samples ?
" (provides samples)" :
"");
990 IMFAttributes *attrs;
996 if (!(
c->is_video &&
c->opt_enc_hw))
999 hr = IMFTransform_GetAttributes(
c->mft, &attrs);
1005 hr = IMFAttributes_GetUINT32(attrs, &MF_TRANSFORM_ASYNC, &v);
1016 hr = IMFAttributes_SetUINT32(attrs, &MF_TRANSFORM_ASYNC_UNLOCK, TRUE);
1022 hr = IMFTransform_QueryInterface(
c->mft, &IID_IMFMediaEventGenerator, (
void **)&
c->async_events);
1031 IMFAttributes_Release(attrs);
1036 const AVCodec *codec,
int use_hw)
1040 MFT_REGISTER_TYPE_INFO reg = {0};
1049 reg.guidSubtype = *subtype;
1052 reg.guidMajorType = MFMediaType_Audio;
1053 category = MFT_CATEGORY_AUDIO_ENCODER;
1055 reg.guidMajorType = MFMediaType_Video;
1056 category = MFT_CATEGORY_VIDEO_ENCODER;
1078 c->is_video = !
c->is_audio;
1081 if (
c->is_video &&
c->opt_enc_hw)
1087 c->main_subtype = *subtype;
1095 hr = IMFTransform_QueryInterface(
c->mft, &IID_ICodecAPI, (
void **)&
c->codec_api);
1100 hr = IMFTransform_GetStreamIDs(
c->mft, 1, &
c->in_stream_id, 1, &
c->out_stream_id);
1101 if (hr == E_NOTIMPL) {
1102 c->in_stream_id =
c->out_stream_id = 0;
1103 }
else if (FAILED(hr)) {
1114 hr = IMFTransform_ProcessMessage(
c->mft, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0);
1120 hr = IMFTransform_ProcessMessage(
c->mft, MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0);
1128 int sleep = 10000, total = 0;
1130 while (total < 70*1000) {
1147 avctx->
extradata ?
"Got" :
"Didn't get", total / 1000);
1154 #define LOAD_MF_FUNCTION(context, func_name) \
1155 context->functions.func_name = (void *)dlsym(context->library, #func_name); \
1156 if (!context->functions.func_name) { \
1157 av_log(context, AV_LOG_ERROR, "DLL mfplat.dll failed to find function "\
1159 return AVERROR_UNKNOWN; \
1165 #define LOAD_MF_FUNCTION(context, func_name) \
1166 context->functions.func_name = func_name; \
1167 if (!context->functions.func_name) { \
1168 av_log(context, AV_LOG_ERROR, "Failed to find function " #func_name \
1170 return AVERROR_UNKNOWN; \
1182 c->library = dlopen(
"mfplat.dll", 0);
1206 ICodecAPI_Release(
c->codec_api);
1208 if (
c->async_events)
1209 IMFMediaEventGenerator_Release(
c->async_events);
1215 dlclose(
c->library);
1240 #define OFFSET(x) offsetof(MFContext, x)
1242 #define MF_ENCODER(MEDIATYPE, NAME, ID, OPTS, FMTS, CAPS, DEFAULTS) \
1243 static const AVClass ff_ ## NAME ## _mf_encoder_class = { \
1244 .class_name = #NAME "_mf", \
1245 .item_name = av_default_item_name, \
1247 .version = LIBAVUTIL_VERSION_INT, \
1249 const FFCodec ff_ ## NAME ## _mf_encoder = { \
1250 .p.priv_class = &ff_ ## NAME ## _mf_encoder_class, \
1251 .p.name = #NAME "_mf", \
1252 CODEC_LONG_NAME(#ID " via MediaFoundation"), \
1253 .p.type = AVMEDIA_TYPE_ ## MEDIATYPE, \
1254 .p.id = AV_CODEC_ID_ ## ID, \
1255 .priv_data_size = sizeof(MFContext), \
1257 .close = mf_close, \
1258 FF_CODEC_RECEIVE_PACKET_CB(mf_receive_packet), \
1261 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
1262 .defaults = DEFAULTS, \
1266 .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, \
1267 AV_SAMPLE_FMT_NONE },
1269 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID | \
1270 AV_CODEC_CAP_DR1 | AV_CODEC_CAP_VARIABLE_FRAME_SIZE,
1276 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1278 {
"rate_control",
"Select rate control mode",
OFFSET(opt_enc_rc),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
VE, .unit =
"rate_control"},
1279 {
"default",
"Default mode", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0,
VE, .unit =
"rate_control"},
1289 {
"scenario",
"Select usage scenario",
OFFSET(opt_enc_scenario),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
VE, .unit =
"scenario"},
1290 {
"default",
"Default scenario", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0,
VE, .unit =
"scenario"},
1309 .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12, \
1310 AV_PIX_FMT_YUV420P, \
1313 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID | \