20 #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0602
22 #define _WIN32_WINNT 0x0602
56 #define MF_TIMEBASE (AVRational){1, 10000000}
58 #define MF_INVALID_TIME AV_NOPTS_VALUE
67 while (!(
c->async_need_input ||
c->async_have_output ||
c->draining_done ||
c->async_marker)) {
68 IMFMediaEvent *ev =
NULL;
69 MediaEventType ev_id = 0;
70 HRESULT hr = IMFMediaEventGenerator_GetEvent(
c->async_events, 0, &ev);
76 IMFMediaEvent_GetType(ev, &ev_id);
80 c->async_need_input = 1;
83 c->async_have_output = 1;
93 IMFMediaEvent_Release(ev);
119 IMFSample_SetSampleTime(
sample, stime);
130 HRESULT hr = IMFSample_GetSampleTime(
sample, &
pts);
143 hr = IMFAttributes_GetBlobSize(
type, &MF_MT_USER_DATA, &sz);
144 if (!FAILED(hr) && sz > 0) {
164 if (!
c->out_info.cbSize && !
c->out_stream_provides_samples) {
165 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &sz);
168 "assuming %d bytes instead.\n", (
int)sz);
169 c->out_info.cbSize = sz;
181 hr = IMFAttributes_GetBlobSize(
type, &MF_MT_MPEG_SEQUENCE_HEADER, &sz);
182 if (!FAILED(hr) && sz > 0) {
186 hr = IMFAttributes_GetBlob(
type, &MF_MT_MPEG_SEQUENCE_HEADER, extradata, sz,
NULL);
206 hr = IMFTransform_GetOutputCurrentType(
c->mft,
c->out_stream_id, &
type);
218 }
else if (
c->is_audio) {
225 IMFMediaType_Release(
type);
240 hr = IMFSample_GetTotalLength(
sample, &
len);
253 IMFMediaBuffer_Release(
buffer);
259 IMFMediaBuffer_Unlock(
buffer);
260 IMFMediaBuffer_Release(
buffer);
264 hr = IMFAttributes_GetUINT32(
sample, &MFSampleExtension_CleanPoint, &t32);
265 if (
c->is_audio || (!FAILED(hr) && t32 != 0))
268 hr = IMFAttributes_GetUINT64(
sample, &MFSampleExtension_DecodeTimestamp, &t);
276 c->reorder_delay = avpkt->
pts - avpkt->
dts;
277 avpkt->
dts -=
c->reorder_delay;
278 avpkt->
pts -=
c->reorder_delay;
320 IMFSample_Release(
sample);
326 IMFMediaBuffer_Release(
buffer);
327 IMFSample_Release(
sample);
333 IMFMediaBuffer_SetCurrentLength(
buffer,
size);
334 IMFMediaBuffer_Unlock(
buffer);
335 IMFMediaBuffer_Release(
buffer);
337 IMFSample_Release(
sample);
370 if (
c->async_events) {
373 if (!
c->async_need_input)
377 IMFSample_SetUINT32(
sample, &MFSampleExtension_Discontinuity, TRUE);
379 hr = IMFTransform_ProcessInput(
c->mft,
c->in_stream_id,
sample, 0);
380 if (hr == MF_E_NOTACCEPTING) {
382 }
else if (FAILED(hr)) {
386 c->async_need_input = 0;
387 }
else if (!
c->draining) {
388 hr = IMFTransform_ProcessMessage(
c->mft, MFT_MESSAGE_COMMAND_DRAIN, 0);
394 c->async_need_input = 0;
410 if (
c->is_video &&
c->codec_api) {
412 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncVideoForceKeyFrame,
FF_VAL_VT_UI4(1));
417 IMFSample_Release(
sample);
426 MFT_OUTPUT_DATA_BUFFER out_buffers;
434 if (
c->async_events) {
437 if (!
c->async_have_output ||
c->draining_done) {
443 if (!
c->out_stream_provides_samples) {
449 out_buffers = (MFT_OUTPUT_DATA_BUFFER) {
450 .dwStreamID =
c->out_stream_id,
455 hr = IMFTransform_ProcessOutput(
c->mft, 0, 1, &out_buffers, &st);
457 if (out_buffers.pEvents)
458 IMFCollection_Release(out_buffers.pEvents);
461 *out_sample = out_buffers.pSample;
466 if (out_buffers.pSample)
467 IMFSample_Release(out_buffers.pSample);
469 if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
471 c->draining_done = 1;
473 }
else if (hr == MF_E_TRANSFORM_STREAM_CHANGE) {
481 c->async_have_output = 0;
493 c->async_have_output = 0;
495 if (
ret >= 0 && !*out_sample)
511 IMFSample_Release(
sample);
527 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t);
531 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_NUM_CHANNELS, &t);
532 if (!FAILED(hr) && t == avctx->
channels)
535 hr = IMFAttributes_GetGUID(
type, &MF_MT_SUBTYPE, &
tg);
537 if (IsEqualGUID(&
c->main_subtype, &
tg))
542 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &t);
546 score |= (1LL << 31) -
diff;
548 score |= (1LL << 30) +
diff;
552 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AAC_PAYLOAD_TYPE, &t);
553 if (!FAILED(hr) && t != 0)
582 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t);
586 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_NUM_CHANNELS, &t);
587 if (!FAILED(hr) && t == avctx->
channels)
604 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t);
610 hr = IMFAttributes_GetUINT32(
type, &MF_MT_AUDIO_NUM_CHANNELS, &t);
611 if (FAILED(hr) || t != avctx->
channels) {
626 hr = IMFAttributes_GetGUID(
type, &MF_MT_SUBTYPE, &
tg);
628 if (IsEqualGUID(&
c->main_subtype, &
tg))
641 IMFAttributes_SetUINT32(
type, &MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive);
663 IMFAttributes_SetUINT32(
type, &MF_MT_MPEG2_PROFILE,
profile);
666 IMFAttributes_SetUINT32(
type, &MF_MT_AVG_BITRATE, avctx->
bit_rate);
673 if (
c->opt_enc_rc >= 0)
674 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncCommonRateControlMode,
FF_VAL_VT_UI4(
c->opt_enc_rc));
676 if (
c->opt_enc_quality >= 0)
677 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncCommonQuality,
FF_VAL_VT_UI4(
c->opt_enc_quality));
688 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVEncH264CABACEnable,
FF_VAL_VT_BOOL(1));
690 if (
c->opt_enc_scenario >= 0)
691 ICodecAPI_SetValue(
c->codec_api, &ff_CODECAPI_AVScenarioInfo,
FF_VAL_VT_UI4(
c->opt_enc_scenario));
724 IMFMediaType *out_type =
NULL;
725 int64_t out_type_score = -1;
726 int out_type_index = -1;
734 hr = IMFTransform_GetOutputAvailableType(
c->mft,
c->out_stream_id, n, &
type);
735 if (hr == MF_E_NO_MORE_TYPES || hr == E_NOTIMPL)
737 if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
753 }
else if (
c->is_audio) {
757 if (score > out_type_score) {
759 IMFMediaType_Release(out_type);
761 out_type_score = score;
763 IMFMediaType_AddRef(out_type);
766 IMFMediaType_Release(
type);
772 hr = MFCreateMediaType(&out_type);
782 }
else if (
c->is_audio) {
790 hr = IMFTransform_SetOutputType(
c->mft,
c->out_stream_id, out_type, 0);
793 }
else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
804 IMFMediaType_Release(out_type);
813 IMFMediaType *in_type =
NULL;
814 int64_t in_type_score = -1;
815 int in_type_index = -1;
823 hr = IMFTransform_GetInputAvailableType(
c->mft,
c->in_stream_id, n, &
type);
824 if (hr == MF_E_NO_MORE_TYPES || hr == E_NOTIMPL)
826 if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
842 }
else if (
c->is_audio) {
846 if (score > in_type_score) {
848 IMFMediaType_Release(in_type);
850 in_type_score = score;
852 IMFMediaType_AddRef(in_type);
855 IMFMediaType_Release(
type);
870 }
else if (
c->is_audio) {
878 hr = IMFTransform_SetInputType(
c->mft,
c->in_stream_id, in_type, 0);
881 }
else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
892 IMFMediaType_Release(in_type);
904 for (n = 0; n < 2 && (need_input ||
need_output); n++) {
909 need_input =
ret < 1;
929 hr = IMFTransform_GetInputStreamInfo(
c->mft,
c->in_stream_id, &
c->in_info);
933 (
int)
c->in_info.cbSize, (
int)
c->in_info.cbAlignment);
935 hr = IMFTransform_GetOutputStreamInfo(
c->mft,
c->out_stream_id, &
c->out_info);
938 c->out_stream_provides_samples =
939 (
c->out_info.dwFlags & MFT_OUTPUT_STREAM_PROVIDES_SAMPLES) ||
940 (
c->out_info.dwFlags & MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES);
942 (
int)
c->out_info.cbSize, (
int)
c->out_info.cbAlignment,
943 c->out_stream_provides_samples ?
" (provides samples)" :
"");
955 IMFAttributes *attrs;
961 if (!(
c->is_video &&
c->opt_enc_hw))
964 hr = IMFTransform_GetAttributes(
c->mft, &attrs);
970 hr = IMFAttributes_GetUINT32(attrs, &MF_TRANSFORM_ASYNC, &v);
981 hr = IMFAttributes_SetUINT32(attrs, &MF_TRANSFORM_ASYNC_UNLOCK, TRUE);
987 hr = IMFTransform_QueryInterface(
c->mft, &IID_IMFMediaEventGenerator, (
void **)&
c->async_events);
996 IMFAttributes_Release(attrs);
1004 MFT_REGISTER_TYPE_INFO reg = {0};
1013 reg.guidSubtype = *subtype;
1016 reg.guidMajorType = MFMediaType_Audio;
1017 category = MFT_CATEGORY_AUDIO_ENCODER;
1019 reg.guidMajorType = MFMediaType_Video;
1020 category = MFT_CATEGORY_VIDEO_ENCODER;
1038 c->is_video = !
c->is_audio;
1041 if (
c->is_video &&
c->opt_enc_hw)
1047 c->main_subtype = *subtype;
1055 hr = IMFTransform_QueryInterface(
c->mft, &IID_ICodecAPI, (
void **)&
c->codec_api);
1060 hr = IMFTransform_GetStreamIDs(
c->mft, 1, &
c->in_stream_id, 1, &
c->out_stream_id);
1061 if (hr == E_NOTIMPL) {
1062 c->in_stream_id =
c->out_stream_id = 0;
1063 }
else if (FAILED(hr)) {
1074 hr = IMFTransform_ProcessMessage(
c->mft, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0);
1080 hr = IMFTransform_ProcessMessage(
c->mft, MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0);
1088 int sleep = 10000, total = 0;
1090 while (total < 70*1000) {
1107 avctx->
extradata ?
"Got" :
"Didn't get", total / 1000);
1118 ICodecAPI_Release(
c->codec_api);
1120 if (
c->async_events)
1121 IMFMediaEventGenerator_Release(
c->async_events);
1131 #define OFFSET(x) offsetof(MFContext, x)
1133 #define MF_ENCODER(MEDIATYPE, NAME, ID, OPTS, EXTRA) \
1134 static const AVClass ff_ ## NAME ## _mf_encoder_class = { \
1135 .class_name = #NAME "_mf", \
1136 .item_name = av_default_item_name, \
1138 .version = LIBAVUTIL_VERSION_INT, \
1140 AVCodec ff_ ## NAME ## _mf_encoder = { \
1141 .priv_class = &ff_ ## NAME ## _mf_encoder_class, \
1142 .name = #NAME "_mf", \
1143 .long_name = NULL_IF_CONFIG_SMALL(#ID " via MediaFoundation"), \
1144 .type = AVMEDIA_TYPE_ ## MEDIATYPE, \
1145 .id = AV_CODEC_ID_ ## ID, \
1146 .priv_data_size = sizeof(MFContext), \
1148 .close = mf_close, \
1149 .send_frame = mf_send_frame, \
1150 .receive_packet = mf_receive_packet, \
1152 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID, \
1153 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | \
1154 FF_CODEC_CAP_INIT_CLEANUP, \
1158 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, \
1159 AV_SAMPLE_FMT_NONE },
1165 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1167 {
"rate_control",
"Select rate control mode",
OFFSET(opt_enc_rc),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
VE,
"rate_control"},
1168 {
"default",
"Default mode", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0,
VE,
"rate_control"},
1178 {
"scenario",
"Select usage scenario",
OFFSET(opt_enc_scenario),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
VE,
"scenario"},
1179 {
"default",
"Default scenario", 0,
AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0,
VE,
"scenario"},
1193 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12, \
1194 AV_PIX_FMT_YUV420P, \