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);
678 IMFAttributes_SetUINT32(
type, &MF_MT_MPEG2_PROFILE,
profile);
681 IMFAttributes_SetUINT32(
type, &MF_MT_AVG_BITRATE, avctx->
bit_rate);
688 if (
c->opt_enc_rc >= 0)
689 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncCommonRateControlMode,
FF_VAL_VT_UI4(
c->opt_enc_rc));
691 if (
c->opt_enc_quality >= 0)
692 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncCommonQuality,
FF_VAL_VT_UI4(
c->opt_enc_quality));
718 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncH264CABACEnable,
FF_VAL_VT_BOOL(1));
720 if (
c->opt_enc_scenario >= 0)
721 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVScenarioInfo,
FF_VAL_VT_UI4(
c->opt_enc_scenario));
754 IMFMediaType *out_type =
NULL;
756 int out_type_index = -1;
764 hr = IMFTransform_GetOutputAvailableType(
c->mft,
c->out_stream_id, n, &
type);
765 if (hr == MF_E_NO_MORE_TYPES || hr == E_NOTIMPL)
767 if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
783 }
else if (
c->is_audio) {
787 if (score > out_type_score) {
789 IMFMediaType_Release(out_type);
791 out_type_score = score;
793 IMFMediaType_AddRef(out_type);
796 IMFMediaType_Release(
type);
802 hr =
c->functions.MFCreateMediaType(&out_type);
812 }
else if (
c->is_audio) {
820 hr = IMFTransform_SetOutputType(
c->mft,
c->out_stream_id, out_type, 0);
823 }
else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
834 IMFMediaType_Release(out_type);
843 IMFMediaType *in_type =
NULL;
845 int in_type_index = -1;
853 hr = IMFTransform_GetInputAvailableType(
c->mft,
c->in_stream_id, n, &
type);
854 if (hr == MF_E_NO_MORE_TYPES || hr == E_NOTIMPL)
856 if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
872 }
else if (
c->is_audio) {
876 if (score > in_type_score) {
878 IMFMediaType_Release(in_type);
880 in_type_score = score;
882 IMFMediaType_AddRef(in_type);
885 IMFMediaType_Release(
type);
900 }
else if (
c->is_audio) {
908 hr = IMFTransform_SetInputType(
c->mft,
c->in_stream_id, in_type, 0);
911 }
else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
922 IMFMediaType_Release(in_type);
932 int need_input = 1, need_output = 1;
934 for (n = 0; n < 2 && (need_input || need_output); n++) {
939 need_input =
ret < 1;
943 need_output =
ret < 1;
945 if (need_input || need_output) {
947 need_input, need_output);
959 hr = IMFTransform_GetInputStreamInfo(
c->mft,
c->in_stream_id, &
c->in_info);
963 (
int)
c->in_info.cbSize, (
int)
c->in_info.cbAlignment);
965 hr = IMFTransform_GetOutputStreamInfo(
c->mft,
c->out_stream_id, &
c->out_info);
968 c->out_stream_provides_samples =
969 (
c->out_info.dwFlags & MFT_OUTPUT_STREAM_PROVIDES_SAMPLES) ||
970 (
c->out_info.dwFlags & MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES);
972 (
int)
c->out_info.cbSize, (
int)
c->out_info.cbAlignment,
973 c->out_stream_provides_samples ?
" (provides samples)" :
"");
985 IMFAttributes *attrs;
991 if (!(
c->is_video &&
c->opt_enc_hw))
994 hr = IMFTransform_GetAttributes(
c->mft, &attrs);
1000 hr = IMFAttributes_GetUINT32(attrs, &MF_TRANSFORM_ASYNC, &v);
1011 hr = IMFAttributes_SetUINT32(attrs, &MF_TRANSFORM_ASYNC_UNLOCK, TRUE);
1017 hr = IMFTransform_QueryInterface(
c->mft, &IID_IMFMediaEventGenerator, (
void **)&
c->async_events);
1026 IMFAttributes_Release(attrs);
1031 const AVCodec *codec,
int use_hw)
1035 MFT_REGISTER_TYPE_INFO reg = {0};
1044 reg.guidSubtype = *subtype;
1047 reg.guidMajorType = MFMediaType_Audio;
1048 category = MFT_CATEGORY_AUDIO_ENCODER;
1050 reg.guidMajorType = MFMediaType_Video;
1051 category = MFT_CATEGORY_VIDEO_ENCODER;
1073 c->is_video = !
c->is_audio;
1076 if (
c->is_video &&
c->opt_enc_hw)
1082 c->main_subtype = *subtype;
1090 hr = IMFTransform_QueryInterface(
c->mft, &IID_ICodecAPI, (
void **)&
c->codec_api);
1095 hr = IMFTransform_GetStreamIDs(
c->mft, 1, &
c->in_stream_id, 1, &
c->out_stream_id);
1096 if (hr == E_NOTIMPL) {
1097 c->in_stream_id =
c->out_stream_id = 0;
1098 }
else if (FAILED(hr)) {
1109 hr = IMFTransform_ProcessMessage(
c->mft, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0);
1115 hr = IMFTransform_ProcessMessage(
c->mft, MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0);
1123 int sleep = 10000, total = 0;
1125 while (total < 70*1000) {
1142 avctx->
extradata ?
"Got" :
"Didn't get", total / 1000);
1149 #define LOAD_MF_FUNCTION(context, func_name) \
1150 context->functions.func_name = (void *)dlsym(context->library, #func_name); \
1151 if (!context->functions.func_name) { \
1152 av_log(context, AV_LOG_ERROR, "DLL mfplat.dll failed to find function "\
1154 return AVERROR_UNKNOWN; \
1160 #define LOAD_MF_FUNCTION(context, func_name) \
1161 context->functions.func_name = func_name; \
1162 if (!context->functions.func_name) { \
1163 av_log(context, AV_LOG_ERROR, "Failed to find function " #func_name \
1165 return AVERROR_UNKNOWN; \
1177 c->library = dlopen(
"mfplat.dll", 0);
1201 ICodecAPI_Release(
c->codec_api);
1203 if (
c->async_events)
1204 IMFMediaEventGenerator_Release(
c->async_events);
1210 dlclose(
c->library);
1235 #define OFFSET(x) offsetof(MFContext, x)
1237 #define MF_ENCODER(MEDIATYPE, NAME, ID, OPTS, FMTS, CAPS, DEFAULTS) \
1238 static const AVClass ff_ ## NAME ## _mf_encoder_class = { \
1239 .class_name = #NAME "_mf", \
1240 .item_name = av_default_item_name, \
1242 .version = LIBAVUTIL_VERSION_INT, \
1244 const FFCodec ff_ ## NAME ## _mf_encoder = { \
1245 .p.priv_class = &ff_ ## NAME ## _mf_encoder_class, \
1246 .p.name = #NAME "_mf", \
1247 CODEC_LONG_NAME(#ID " via MediaFoundation"), \
1248 .p.type = AVMEDIA_TYPE_ ## MEDIATYPE, \
1249 .p.id = AV_CODEC_ID_ ## ID, \
1250 .priv_data_size = sizeof(MFContext), \
1252 .close = mf_close, \
1253 FF_CODEC_RECEIVE_PACKET_CB(mf_receive_packet), \
1256 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
1257 .defaults = DEFAULTS, \
1261 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16),
1263 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID | \
1264 AV_CODEC_CAP_DR1 | AV_CODEC_CAP_VARIABLE_FRAME_SIZE,
1270 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1272 {
"rate_control",
"Select rate control mode",
OFFSET(opt_enc_rc),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
VE, .unit =
"rate_control"},
1273 {
"default",
"Default mode", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0,
VE, .unit =
"rate_control"},
1283 {
"scenario",
"Select usage scenario",
OFFSET(opt_enc_scenario),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
VE, .unit =
"scenario"},
1284 {
"default",
"Default scenario", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0,
VE, .unit =
"scenario"},
1303 CODEC_PIXFMTS(AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P),
1305 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID | \