20 #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0602
22 #define _WIN32_WINNT 0x0602
73 #define MF_TIMEBASE (AVRational){1, 10000000}
75 #define MF_INVALID_TIME AV_NOPTS_VALUE
84 while (!(
c->async_need_input ||
c->async_have_output ||
c->draining_done ||
c->async_marker)) {
85 IMFMediaEvent *ev =
NULL;
86 MediaEventType ev_id = 0;
87 HRESULT hr = IMFMediaEventGenerator_GetEvent(
c->async_events, 0, &ev);
93 IMFMediaEvent_GetType(ev, &ev_id);
97 c->async_need_input = 1;
100 c->async_have_output = 1;
103 c->draining_done = 1;
110 IMFMediaEvent_Release(ev);
134 IMFSample_SetSampleTime(
sample, stime);
145 HRESULT hr = IMFSample_GetSampleTime(
sample, &
pts);
158 hr = IMFAttributes_GetBlobSize(
type, &MF_MT_USER_DATA, &sz);
159 if (!FAILED(hr) && sz > 0) {
179 if (!
c->out_info.cbSize && !
c->out_stream_provides_samples) {
180 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &sz);
183 "assuming %d bytes instead.\n", (
int)sz);
184 c->out_info.cbSize = sz;
196 hr = IMFAttributes_GetBlobSize(
type, &MF_MT_MPEG_SEQUENCE_HEADER, &sz);
197 if (!FAILED(hr) && sz > 0) {
201 hr = IMFAttributes_GetBlob(
type, &MF_MT_MPEG_SEQUENCE_HEADER, extradata, sz,
NULL);
221 hr = IMFTransform_GetOutputCurrentType(
c->mft,
c->out_stream_id, &
type);
233 }
else if (
c->is_audio) {
240 IMFMediaType_Release(
type);
255 hr = IMFSample_GetTotalLength(
sample, &
len);
262 hr = IMFSample_ConvertToContiguousBuffer(
sample, &
buffer);
268 IMFMediaBuffer_Release(
buffer);
274 IMFMediaBuffer_Unlock(
buffer);
275 IMFMediaBuffer_Release(
buffer);
279 hr = IMFAttributes_GetUINT32(
sample, &MFSampleExtension_CleanPoint, &t32);
280 if (
c->is_audio || (!FAILED(hr) && t32 != 0))
283 hr = IMFAttributes_GetUINT64(
sample, &MFSampleExtension_DecodeTimestamp, &t);
291 c->reorder_delay = avpkt->
pts - avpkt->
dts;
292 avpkt->
dts -=
c->reorder_delay;
293 avpkt->
pts -=
c->reorder_delay;
310 c->in_info.cbAlignment);
326 hr =
func->MFCreateDXGIDeviceManager(&
c->resetToken, &
c->dxgiManager);
332 hr = IMFDXGIDeviceManager_ResetDevice(
c->dxgiManager, (IUnknown*)
c->device_hwctx->device,
c->resetToken);
338 hr = IMFTransform_ProcessMessage(
c->mft, MFT_MESSAGE_SET_D3D_MANAGER, (ULONG_PTR)
c->dxgiManager);
352 ID3D11Texture2D *d3d11_texture =
NULL;
361 if (!
c->dxgiManager) {
368 d3d11_texture = (ID3D11Texture2D*)
frame->data[0];
369 subIdx = (
int)(intptr_t)
frame->data[1];
371 if (!d3d11_texture) {
382 hr =
func->MFCreateDXGISurfaceBuffer(&IID_ID3D11Texture2D, (IUnknown*)d3d11_texture, subIdx, 0, &
buffer);
385 IMFSample_Release(
sample);
392 IMFMediaBuffer_Release(
buffer);
393 IMFSample_Release(
sample);
397 IMFMediaBuffer_Release(
buffer);
417 c->in_info.cbAlignment);
423 IMFSample_Release(
sample);
429 IMFMediaBuffer_Release(
buffer);
430 IMFSample_Release(
sample);
436 IMFMediaBuffer_SetCurrentLength(
buffer,
size);
437 IMFMediaBuffer_Unlock(
buffer);
438 IMFMediaBuffer_Release(
buffer);
440 IMFSample_Release(
sample);
507 if (
c->async_events) {
510 if (!
c->async_need_input)
514 IMFSample_SetUINT32(
sample, &MFSampleExtension_Discontinuity, TRUE);
516 hr = IMFTransform_ProcessInput(
c->mft,
c->in_stream_id,
sample, 0);
517 if (hr == MF_E_NOTACCEPTING) {
519 }
else if (FAILED(hr)) {
523 c->async_need_input = 0;
524 }
else if (!
c->draining) {
525 hr = IMFTransform_ProcessMessage(
c->mft, MFT_MESSAGE_COMMAND_DRAIN, 0);
531 c->async_need_input = 0;
543 MFT_OUTPUT_DATA_BUFFER out_buffers;
551 if (
c->async_events) {
554 if (!
c->async_have_output ||
c->draining_done) {
560 if (!
c->out_stream_provides_samples) {
563 c->out_info.cbAlignment);
568 out_buffers = (MFT_OUTPUT_DATA_BUFFER) {
569 .dwStreamID =
c->out_stream_id,
574 hr = IMFTransform_ProcessOutput(
c->mft, 0, 1, &out_buffers, &st);
576 if (out_buffers.pEvents)
577 IMFCollection_Release(out_buffers.pEvents);
580 *out_sample = out_buffers.pSample;
585 if (out_buffers.pSample)
586 IMFSample_Release(out_buffers.pSample);
588 if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
590 c->draining_done = 1;
592 }
else if (hr == MF_E_TRANSFORM_STREAM_CHANGE) {
600 c->async_have_output = 0;
612 c->async_have_output = 0;
614 if (
ret >= 0 && !*out_sample)
626 if (!
c->frame->buf[0]) {
632 if (
c->frame->buf[0]) {
638 if (
c->is_video &&
c->codec_api) {
640 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncVideoForceKeyFrame,
FF_VAL_VT_UI4(1));
644 if(!
c->stream_started)
646 HRESULT hr = IMFTransform_ProcessMessage(
c->mft, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0);
652 hr = IMFTransform_ProcessMessage(
c->mft, MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0);
658 c->stream_started = 1;
663 IMFSample_Release(
sample);
674 IMFSample_Release(
sample);
690 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t);
694 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_NUM_CHANNELS, &t);
698 hr = IMFAttributes_GetGUID(
type, &MF_MT_SUBTYPE, &tg);
700 if (IsEqualGUID(&
c->main_subtype, &tg))
705 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &t);
709 score |= (1LL << 31) -
diff;
711 score |= (1LL << 30) +
diff;
715 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AAC_PAYLOAD_TYPE, &t);
716 if (!FAILED(hr) && t != 0)
745 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t);
749 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_NUM_CHANNELS, &t);
767 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t);
773 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_NUM_CHANNELS, &t);
789 hr = IMFAttributes_GetGUID(
type, &MF_MT_SUBTYPE, &tg);
791 if (IsEqualGUID(&
c->main_subtype, &tg))
804 IMFAttributes_SetUINT32(
type, &MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive);
825 IMFAttributes_SetUINT32(
type, &MF_MT_MPEG2_PROFILE,
profile);
828 IMFAttributes_SetUINT32(
type, &MF_MT_AVG_BITRATE, avctx->
bit_rate);
835 if (
c->opt_enc_rc >= 0)
836 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncCommonRateControlMode,
FF_VAL_VT_UI4(
c->opt_enc_rc));
838 if (
c->opt_enc_quality >= 0)
839 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncCommonQuality,
FF_VAL_VT_UI4(
c->opt_enc_quality));
865 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncH264CABACEnable,
FF_VAL_VT_BOOL(1));
867 if (
c->opt_enc_scenario >= 0)
868 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVScenarioInfo,
FF_VAL_VT_UI4(
c->opt_enc_scenario));
916 IMFMediaType *out_type =
NULL;
918 int out_type_index = -1;
926 hr = IMFTransform_GetOutputAvailableType(
c->mft,
c->out_stream_id, n, &
type);
927 if (hr == MF_E_NO_MORE_TYPES || hr == E_NOTIMPL)
929 if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
945 }
else if (
c->is_audio) {
949 if (score > out_type_score) {
951 IMFMediaType_Release(out_type);
953 out_type_score = score;
955 IMFMediaType_AddRef(out_type);
958 IMFMediaType_Release(
type);
964 hr =
c->functions.MFCreateMediaType(&out_type);
974 }
else if (
c->is_audio) {
982 hr = IMFTransform_SetOutputType(
c->mft,
c->out_stream_id, out_type, 0);
985 }
else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
996 IMFMediaType_Release(out_type);
1005 IMFMediaType *in_type =
NULL;
1007 int in_type_index = -1;
1011 for (n = 0; ; n++) {
1015 hr = IMFTransform_GetInputAvailableType(
c->mft,
c->in_stream_id, n, &
type);
1016 if (hr == MF_E_NO_MORE_TYPES || hr == E_NOTIMPL)
1018 if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
1034 }
else if (
c->is_audio) {
1038 if (score > in_type_score) {
1040 IMFMediaType_Release(in_type);
1042 in_type_score = score;
1044 IMFMediaType_AddRef(in_type);
1047 IMFMediaType_Release(
type);
1062 }
else if (
c->is_audio) {
1070 hr = IMFTransform_SetInputType(
c->mft,
c->in_stream_id, in_type, 0);
1073 }
else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
1084 IMFMediaType_Release(in_type);
1094 int need_input = 1, need_output = 1;
1096 for (n = 0; n < 2 && (need_input || need_output); n++) {
1101 need_input =
ret < 1;
1105 need_output =
ret < 1;
1107 if (need_input || need_output) {
1109 need_input, need_output);
1121 hr = IMFTransform_GetInputStreamInfo(
c->mft,
c->in_stream_id, &
c->in_info);
1125 (
int)
c->in_info.cbSize, (
int)
c->in_info.cbAlignment);
1127 hr = IMFTransform_GetOutputStreamInfo(
c->mft,
c->out_stream_id, &
c->out_info);
1130 c->out_stream_provides_samples =
1131 (
c->out_info.dwFlags & MFT_OUTPUT_STREAM_PROVIDES_SAMPLES) ||
1132 (
c->out_info.dwFlags & MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES);
1134 (
int)
c->out_info.cbSize, (
int)
c->out_info.cbAlignment,
1135 c->out_stream_provides_samples ?
" (provides samples)" :
"");
1147 IMFAttributes *attrs;
1153 if (!(
c->is_video &&
c->opt_enc_hw))
1156 hr = IMFTransform_GetAttributes(
c->mft, &attrs);
1162 hr = IMFAttributes_GetUINT32(attrs, &MF_TRANSFORM_ASYNC, &v);
1173 hr = IMFAttributes_SetUINT32(attrs, &MF_TRANSFORM_ASYNC_UNLOCK, TRUE);
1179 hr = IMFTransform_QueryInterface(
c->mft, &IID_IMFMediaEventGenerator, (
void **)&
c->async_events);
1188 IMFAttributes_Release(attrs);
1193 const AVCodec *codec,
int use_hw)
1197 MFT_REGISTER_TYPE_INFO reg = {0};
1206 reg.guidSubtype = *subtype;
1209 reg.guidMajorType = MFMediaType_Audio;
1210 category = MFT_CATEGORY_AUDIO_ENCODER;
1212 reg.guidMajorType = MFMediaType_Video;
1213 category = MFT_CATEGORY_VIDEO_ENCODER;
1235 c->is_video = !
c->is_audio;
1238 if (
c->is_video &&
c->opt_enc_hw)
1244 c->main_subtype = *subtype;
1252 hr = IMFTransform_QueryInterface(
c->mft, &IID_ICodecAPI, (
void **)&
c->codec_api);
1257 hr = IMFTransform_GetStreamIDs(
c->mft, 1, &
c->in_stream_id, 1, &
c->out_stream_id);
1258 if (hr == E_NOTIMPL) {
1259 c->in_stream_id =
c->out_stream_id = 0;
1260 }
else if (FAILED(hr)) {
1273 int sleep = 10000, total = 0;
1275 while (total < 70*1000) {
1292 avctx->
extradata ?
"Got" :
"Didn't get", total / 1000);
1299 #define LOAD_MF_FUNCTION(context, func_name) \
1300 context->functions.func_name = (void *)dlsym(context->library, #func_name); \
1301 if (!context->functions.func_name) { \
1302 av_log(context, AV_LOG_ERROR, "DLL mfplat.dll failed to find function "\
1304 return AVERROR_UNKNOWN; \
1310 #define LOAD_MF_FUNCTION(context, func_name) \
1311 context->functions.func_name = func_name; \
1312 if (!context->functions.func_name) { \
1313 av_log(context, AV_LOG_ERROR, "Failed to find function " #func_name \
1315 return AVERROR_UNKNOWN; \
1327 c->library = dlopen(
"mfplat.dll", 0);
1328 c->d3d_dll = dlopen(
"D3D11.dll", 0);
1354 ICodecAPI_Release(
c->codec_api);
1356 if (
c->async_events)
1357 IMFMediaEventGenerator_Release(
c->async_events);
1363 dlclose(
c->library);
1364 dlclose(
c->d3d_dll);
1389 #define OFFSET(x) offsetof(MFContext, x)
1391 #define MF_ENCODER(MEDIATYPE, NAME, ID, OPTS, FMTS, CAPS, DEFAULTS) \
1392 static const AVClass ff_ ## NAME ## _mf_encoder_class = { \
1393 .class_name = #NAME "_mf", \
1394 .item_name = av_default_item_name, \
1396 .version = LIBAVUTIL_VERSION_INT, \
1398 const FFCodec ff_ ## NAME ## _mf_encoder = { \
1399 .p.priv_class = &ff_ ## NAME ## _mf_encoder_class, \
1400 .p.name = #NAME "_mf", \
1401 CODEC_LONG_NAME(#ID " via MediaFoundation"), \
1402 .p.type = AVMEDIA_TYPE_ ## MEDIATYPE, \
1403 .p.id = AV_CODEC_ID_ ## ID, \
1404 .priv_data_size = sizeof(MFContext), \
1406 .close = mf_close, \
1407 FF_CODEC_RECEIVE_PACKET_CB(mf_receive_packet), \
1410 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
1411 .defaults = DEFAULTS, \
1415 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16),
1417 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID | \
1418 AV_CODEC_CAP_DR1 | AV_CODEC_CAP_VARIABLE_FRAME_SIZE,
1424 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1426 {
"rate_control",
"Select rate control mode",
OFFSET(opt_enc_rc),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
VE, .unit =
"rate_control"},
1427 {
"default",
"Default mode", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0,
VE, .unit =
"rate_control"},
1437 {
"scenario",
"Select usage scenario",
OFFSET(opt_enc_scenario),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
VE, .unit =
"scenario"},
1438 {
"default",
"Default scenario", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0,
VE, .unit =
"scenario"},
1457 CODEC_PIXFMTS(AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P, AV_PIX_FMT_D3D11),
1459 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID | \