1 /*****************************************************************************
2 * mft.c : Media Foundation Transform audio/video decoder
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
6 * Author: Felix Abecassis <felix.abecassis@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
30 /* Needed for many mingw macros. */
33 /* Avoid having GUIDs being defined as "extern". */
37 # define STDCALL __stdcall
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_codec.h>
46 #define _VIDEOINFOHEADER_
47 #include <vlc_codecs.h>
50 #include <mftransform.h>
52 #include <mfobjects.h>
54 static int Open(vlc_object_t *);
55 static void Close(vlc_object_t *);
58 set_description(N_("Media Foundation Transform decoder"))
60 set_capability("decoder", 1)
61 set_callbacks(Open, Close)
62 set_category(CAT_INPUT)
63 set_subcategory(SUBCAT_INPUT_VCODEC)
69 HRESULT (STDCALL *fptr_MFTEnumEx)(GUID guidCategory, UINT32 Flags,
70 const MFT_REGISTER_TYPE_INFO *pInputType,
71 const MFT_REGISTER_TYPE_INFO *pOutputType,
72 IMFActivate ***pppMFTActivate, UINT32 *pcMFTActivate);
73 HRESULT (STDCALL *fptr_MFCreateSample)(IMFSample **ppIMFSample);
74 HRESULT (STDCALL *fptr_MFCreateMemoryBuffer)(DWORD cbMaxLength, IMFMediaBuffer **ppBuffer);
75 HRESULT (STDCALL *fptr_MFCreateAlignedMemoryBuffer)(DWORD cbMaxLength, DWORD fAlignmentFlags, IMFMediaBuffer **ppBuffer);
84 const GUID* major_type;
87 /* For asynchronous MFT */
89 IMFMediaEventGenerator *event_generator;
90 int pending_input_events;
91 int pending_output_events;
94 DWORD input_stream_id;
95 IMFMediaType *input_type;
98 DWORD output_stream_id;
99 IMFSample *output_sample;
100 IMFMediaType *output_type;
106 static const int pi_channels_maps[9] =
110 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
111 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
112 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
113 | AOUT_CHAN_REARRIGHT,
114 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
115 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
116 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
117 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
118 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
119 | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
120 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
121 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
122 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
126 /* Possibly missing from mingw headers */
127 #ifndef MF_E_TRANSFORM_NEED_MORE_INPUT
128 # define MF_E_TRANSFORM_NEED_MORE_INPUT _HRESULT_TYPEDEF_(0xc00d6d72)
131 #ifndef MF_E_TRANSFORM_STREAM_CHANGE
132 # define MF_E_TRANSFORM_STREAM_CHANGE _HRESULT_TYPEDEF_(0xc00d6d61)
135 #ifndef MF_E_NO_EVENTS_AVAILABLE
136 # define MF_E_NO_EVENTS_AVAILABLE _HRESULT_TYPEDEF_(0xC00D3E80L)
139 #ifndef MF_EVENT_FLAG_NO_WAIT
140 # define MF_EVENT_FLAG_NO_WAIT 0x00000001
144 * The MFTransformXXX values might not be defined in mingw headers,
145 * thus we use our own enum with the VLC prefix.
149 VLC_METransformUnknown = 600,
150 VLC_METransformNeedInput,
151 VLC_METransformHaveOutput,
152 VLC_METransformDrainComplete,
153 VLC_METransformMarker,
163 * We need this table since the FOURCC used for GUID is not the same
164 * as the FOURCC used by VLC, for instance h264 vs H264.
166 static const pair_format_guid video_format_table[] =
168 { VLC_CODEC_H264, &MFVideoFormat_H264 },
169 { VLC_CODEC_MJPG, &MFVideoFormat_MJPG },
170 { VLC_CODEC_WMV1, &MFVideoFormat_WMV1 },
171 { VLC_CODEC_WMV2, &MFVideoFormat_WMV2 },
172 { VLC_CODEC_WMV3, &MFVideoFormat_WMV3 },
173 { VLC_CODEC_VC1, &MFVideoFormat_WVC1 },
177 DEFINE_GUID(MFAudioFormat_Dolby_AC3, 0xe06d802c, 0xdb46, 0x11cf, 0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea);
179 * We cannot use the FOURCC code for audio either since the
180 * WAVE_FORMAT value is used to create the GUID.
182 static const pair_format_guid audio_format_table[] =
184 { VLC_CODEC_MPGA, &MFAudioFormat_MPEG },
185 { VLC_CODEC_MP3, &MFAudioFormat_MP3 },
186 { VLC_CODEC_DTS, &MFAudioFormat_DTS },
187 { VLC_CODEC_MP4A, &MFAudioFormat_AAC },
188 { VLC_CODEC_WMA2, &MFAudioFormat_WMAudioV8 },
189 { VLC_CODEC_A52, &MFAudioFormat_Dolby_AC3 },
193 static const GUID *FormatToGUID(const pair_format_guid table[], vlc_fourcc_t fourcc)
195 for (int i = 0; table[i].fourcc; ++i)
196 if (table[i].fourcc == fourcc)
197 return table[i].guid;
203 * Low latency mode for Windows 8. Without this option, the H264
204 * decoder will fill *all* its internal buffers before returning a
205 * frame. Because of this behavior, the decoder might return no frame
206 * for more than 500 ms, making it unusable for playback.
208 DEFINE_GUID(CODECAPI_AVLowLatencyMode, 0x9c27891a, 0xed7a, 0x40e1, 0x88, 0xe8, 0xb2, 0x27, 0x27, 0xa0, 0x24, 0xee);
210 static int SetInputType(decoder_t *p_dec, DWORD stream_id, IMFMediaType **result)
212 decoder_sys_t *p_sys = p_dec->p_sys;
217 IMFMediaType *input_media_type = NULL;
219 /* Search a suitable input type for the MFT. */
220 int input_type_index = 0;
222 for (int i = 0; !found; ++i)
224 hr = IMFTransform_GetInputAvailableType(p_sys->mft, stream_id, i, &input_media_type);
225 if (hr == MF_E_NO_MORE_TYPES)
227 else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET)
229 /* The output type must be set before setting the input type for this MFT. */
236 hr = IMFMediaType_GetGUID(input_media_type, &MF_MT_SUBTYPE, &subtype);
240 if (IsEqualGUID(&subtype, p_sys->subtype))
244 input_type_index = i;
246 IMFMediaType_Release(input_media_type);
247 input_media_type = NULL;
252 hr = IMFTransform_GetInputAvailableType(p_sys->mft, stream_id, input_type_index, &input_media_type);
256 if (p_dec->fmt_in.i_cat == VIDEO_ES)
258 UINT64 width = p_dec->fmt_in.video.i_width;
259 UINT64 height = p_dec->fmt_in.video.i_height;
260 UINT64 frame_size = (width << 32) | height;
261 hr = IMFMediaType_SetUINT64(input_media_type, &MF_MT_FRAME_SIZE, frame_size);
267 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_ORIGINAL_WAVE_FORMAT_TAG, p_sys->subtype->Data1);
270 if (p_dec->fmt_in.audio.i_rate)
272 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, p_dec->fmt_in.audio.i_rate);
276 if (p_dec->fmt_in.audio.i_channels)
278 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_NUM_CHANNELS, p_dec->fmt_in.audio.i_channels);
282 if (p_dec->fmt_in.audio.i_bitspersample)
284 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_BITS_PER_SAMPLE, p_dec->fmt_in.audio.i_bitspersample);
288 if (p_dec->fmt_in.audio.i_blockalign)
290 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_BLOCK_ALIGNMENT, p_dec->fmt_in.audio.i_blockalign);
294 if (p_dec->fmt_in.i_bitrate)
296 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, p_dec->fmt_in.i_bitrate / 8);
302 if (p_dec->fmt_in.i_extra > 0)
304 UINT32 blob_size = 0;
305 hr = IMFMediaType_GetBlobSize(input_media_type, &MF_MT_USER_DATA, &blob_size);
307 * Do not overwrite existing user data in the input type, this
308 * can cause the MFT to reject the type.
310 if (hr == MF_E_ATTRIBUTENOTFOUND)
312 hr = IMFMediaType_SetBlob(input_media_type, &MF_MT_USER_DATA,
313 (const UINT8*)p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra);
319 hr = IMFTransform_SetInputType(p_sys->mft, stream_id, input_media_type, 0);
323 *result = input_media_type;
328 msg_Err(p_dec, "Error in SetInputType()");
329 if (input_media_type)
330 IMFMediaType_Release(input_media_type);
334 static int SetOutputType(decoder_t *p_dec, DWORD stream_id, IMFMediaType **result)
336 decoder_sys_t *p_sys = p_dec->p_sys;
341 IMFMediaType *output_media_type = NULL;
344 * Enumerate available output types. The list is ordered by
345 * preference thus we will use the first one unless YV12/I420 is
346 * available for video or float32 for audio.
348 int output_type_index = 0;
350 for (int i = 0; !found; ++i)
352 hr = IMFTransform_GetOutputAvailableType(p_sys->mft, stream_id, i, &output_media_type);
353 if (hr == MF_E_NO_MORE_TYPES)
355 else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET)
357 /* The input type must be set before setting the output type for this MFT. */
364 hr = IMFMediaType_GetGUID(output_media_type, &MF_MT_SUBTYPE, &subtype);
368 if (p_dec->fmt_in.i_cat == VIDEO_ES)
370 if (IsEqualGUID(&subtype, &MFVideoFormat_YV12) || IsEqualGUID(&subtype, &MFVideoFormat_I420))
375 UINT32 bits_per_sample;
376 hr = IMFMediaType_GetUINT32(output_media_type, &MF_MT_AUDIO_BITS_PER_SAMPLE, &bits_per_sample);
379 if (bits_per_sample == 32 && IsEqualGUID(&subtype, &MFAudioFormat_Float))
384 output_type_index = i;
386 IMFMediaType_Release(output_media_type);
387 output_media_type = NULL;
390 * It's not an error if we don't find the output type we were
391 * looking for, in this case we use the first available type which
392 * is the "preferred" output type for this MFT.
395 hr = IMFTransform_GetOutputAvailableType(p_sys->mft, stream_id, output_type_index, &output_media_type);
399 hr = IMFTransform_SetOutputType(p_sys->mft, stream_id, output_media_type, 0);
404 hr = IMFMediaType_GetGUID(output_media_type, &MF_MT_SUBTYPE, &subtype);
408 if (p_dec->fmt_in.i_cat == VIDEO_ES)
410 p_dec->fmt_out.video = p_dec->fmt_in.video;
411 p_dec->fmt_out.i_codec = vlc_fourcc_GetCodec(p_dec->fmt_in.i_cat, subtype.Data1);
415 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
417 UINT32 bitspersample = 0;
418 hr = IMFMediaType_GetUINT32(output_media_type, &MF_MT_AUDIO_BITS_PER_SAMPLE, &bitspersample);
419 if (SUCCEEDED(hr) && bitspersample)
420 p_dec->fmt_out.audio.i_bitspersample = bitspersample;
423 hr = IMFMediaType_GetUINT32(output_media_type, &MF_MT_AUDIO_NUM_CHANNELS, &channels);
424 if (SUCCEEDED(hr) && channels)
425 p_dec->fmt_out.audio.i_channels = channels;
428 hr = IMFMediaType_GetUINT32(output_media_type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &rate);
429 if (SUCCEEDED(hr) && rate)
430 p_dec->fmt_out.audio.i_rate = rate;
433 wf_tag_to_fourcc(subtype.Data1, &fourcc, NULL);
434 p_dec->fmt_out.i_codec = vlc_fourcc_GetCodecAudio(fourcc, p_dec->fmt_out.audio.i_bitspersample);
436 p_dec->fmt_out.audio.i_physical_channels = pi_channels_maps[p_dec->fmt_out.audio.i_channels];
437 p_dec->fmt_out.audio.i_original_channels = p_dec->fmt_out.audio.i_physical_channels;
440 *result = output_media_type;
445 msg_Err(p_dec, "Error in SetOutputType()");
446 if (output_media_type)
447 IMFMediaType_Release(output_media_type);
451 static int AllocateInputSample(decoder_t *p_dec, DWORD stream_id, IMFSample** result, DWORD size)
453 decoder_sys_t *p_sys = p_dec->p_sys;
454 MFHandle *mf = &p_sys->mf_handle;
459 IMFSample *input_sample = NULL;
461 MFT_INPUT_STREAM_INFO input_info;
462 hr = IMFTransform_GetInputStreamInfo(p_sys->mft, stream_id, &input_info);
466 hr = mf->fptr_MFCreateSample(&input_sample);
470 IMFMediaBuffer *input_media_buffer = NULL;
471 DWORD allocation_size = __MAX(input_info.cbSize, size);
472 hr = mf->fptr_MFCreateMemoryBuffer(allocation_size, &input_media_buffer);
476 hr = IMFSample_AddBuffer(input_sample, input_media_buffer);
477 IMFMediaBuffer_Release(input_media_buffer);
481 *result = input_sample;
486 msg_Err(p_dec, "Error in AllocateInputSample()");
488 IMFSample_Release(input_sample);
489 if (input_media_buffer)
490 IMFMediaBuffer_Release(input_media_buffer);
494 static int AllocateOutputSample(decoder_t *p_dec, DWORD stream_id, IMFSample **result)
496 decoder_sys_t *p_sys = p_dec->p_sys;
497 MFHandle *mf = &p_sys->mf_handle;
502 IMFSample *output_sample = NULL;
504 MFT_OUTPUT_STREAM_INFO output_info;
505 hr = IMFTransform_GetOutputStreamInfo(p_sys->mft, stream_id, &output_info);
509 if (output_info.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES | MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES))
511 /* The MFT will provide an allocated sample. */
515 DWORD expected_flags = 0;
516 if (p_dec->fmt_in.i_cat == VIDEO_ES)
517 expected_flags |= MFT_OUTPUT_STREAM_WHOLE_SAMPLES
518 | MFT_OUTPUT_STREAM_SINGLE_SAMPLE_PER_BUFFER
519 | MFT_OUTPUT_STREAM_FIXED_SAMPLE_SIZE;
520 if ((output_info.dwFlags & expected_flags) != expected_flags)
523 hr = mf->fptr_MFCreateSample(&output_sample);
527 IMFMediaBuffer *output_media_buffer = NULL;
528 DWORD allocation_size = output_info.cbSize;
529 DWORD alignment = output_info.cbAlignment;
531 hr = mf->fptr_MFCreateAlignedMemoryBuffer(allocation_size, alignment - 1, &output_media_buffer);
533 hr = mf->fptr_MFCreateMemoryBuffer(allocation_size, &output_media_buffer);
537 hr = IMFSample_AddBuffer(output_sample, output_media_buffer);
541 *result = output_sample;
546 msg_Err(p_dec, "Error in AllocateOutputSample()");
548 IMFSample_Release(output_sample);
552 static int ProcessInputStream(decoder_t *p_dec, DWORD stream_id, block_t *p_block)
554 decoder_sys_t *p_sys = p_dec->p_sys;
556 IMFSample *input_sample = NULL;
558 if (AllocateInputSample(p_dec, stream_id, &input_sample, p_block->i_buffer))
561 IMFMediaBuffer *input_media_buffer = NULL;
562 hr = IMFSample_GetBufferByIndex(input_sample, 0, &input_media_buffer);
567 hr = IMFMediaBuffer_Lock(input_media_buffer, &buffer_start, NULL, NULL);
571 memcpy(buffer_start, p_block->p_buffer, p_block->i_buffer);
573 if (p_dec->fmt_in.i_codec == VLC_CODEC_H264)
575 /* in-place NAL to annex B conversion. */
576 struct H264ConvertState convert_state = { 0, 0 };
577 convert_h264_to_annexb(buffer_start, p_block->i_buffer, p_sys->nal_size, &convert_state);
580 hr = IMFMediaBuffer_Unlock(input_media_buffer);
584 hr = IMFMediaBuffer_SetCurrentLength(input_media_buffer, p_block->i_buffer);
588 LONGLONG ts = p_block->i_pts;
589 if (!ts && p_block->i_dts)
592 /* Convert from microseconds to 100 nanoseconds unit. */
593 hr = IMFSample_SetSampleTime(input_sample, ts * 10);
597 hr = IMFTransform_ProcessInput(p_sys->mft, stream_id, input_sample, 0);
601 IMFMediaBuffer_Release(input_media_buffer);
602 IMFSample_Release(input_sample);
607 msg_Err(p_dec, "Error in ProcessInputStream()");
609 IMFSample_Release(input_sample);
613 /* Copy a packed buffer (no padding) to a picture_t */
614 static void CopyPackedBufferToPicture(picture_t *p_pic, const uint8_t *p_src)
616 for (int i = 0; i < p_pic->i_planes; ++i)
618 uint8_t *p_dst = p_pic->p[i].p_pixels;
620 if (p_pic->p[i].i_visible_pitch == p_pic->p[i].i_pitch)
622 /* Plane is packed, only one memcpy is needed. */
623 uint32_t plane_size = p_pic->p[i].i_pitch * p_pic->p[i].i_visible_lines;
624 memcpy(p_dst, p_src, plane_size);
629 for (int i_line = 0; i_line < p_pic->p[i].i_visible_lines; i_line++)
631 memcpy(p_dst, p_src, p_pic->p[i].i_visible_pitch);
632 p_src += p_pic->p[i].i_visible_pitch;
633 p_dst += p_pic->p[i].i_pitch;
638 static int ProcessOutputStream(decoder_t *p_dec, DWORD stream_id, void **result)
640 decoder_sys_t *p_sys = p_dec->p_sys;
642 picture_t *picture = NULL;
643 block_t *aout_buffer = NULL;
647 DWORD output_status = 0;
648 MFT_OUTPUT_DATA_BUFFER output_buffer = { stream_id, p_sys->output_sample, 0, NULL };
649 hr = IMFTransform_ProcessOutput(p_sys->mft, 0, 1, &output_buffer, &output_status);
650 if (output_buffer.pEvents)
651 IMFCollection_Release(output_buffer.pEvents);
652 /* Use the returned sample since it can be provided by the MFT. */
653 IMFSample *output_sample = output_buffer.pSample;
660 LONGLONG sample_time;
661 hr = IMFSample_GetSampleTime(output_sample, &sample_time);
664 /* Convert from 100 nanoseconds unit to microseconds. */
667 DWORD total_length = 0;
668 hr = IMFSample_GetTotalLength(output_sample, &total_length);
672 if (p_dec->fmt_in.i_cat == VIDEO_ES)
674 picture = decoder_NewPicture(p_dec);
678 UINT32 interlaced = false;
679 hr = IMFSample_GetUINT32(output_sample, &MFSampleExtension_Interlaced, &interlaced);
680 picture->b_progressive = !interlaced;
682 picture->date = sample_time;
686 if (p_dec->fmt_out.audio.i_bitspersample == 0 || p_dec->fmt_out.audio.i_channels == 0)
688 int samples = total_length / (p_dec->fmt_out.audio.i_bitspersample * p_dec->fmt_out.audio.i_channels / 8);
689 aout_buffer = decoder_NewAudioBuffer(p_dec, samples);
692 if (aout_buffer->i_buffer < total_length)
695 aout_buffer->i_pts = sample_time;
698 IMFMediaBuffer *output_media_buffer = NULL;
699 hr = IMFSample_GetBufferByIndex(output_sample, 0, &output_media_buffer);
702 hr = IMFMediaBuffer_Lock(output_media_buffer, &buffer_start, NULL, NULL);
706 if (p_dec->fmt_in.i_cat == VIDEO_ES)
707 CopyPackedBufferToPicture(picture, buffer_start);
709 memcpy(aout_buffer->p_buffer, buffer_start, total_length);
711 hr = IMFMediaBuffer_Unlock(output_media_buffer);
712 IMFSample_Release(output_media_buffer);
716 if (p_sys->output_sample)
718 /* Sample is not provided by the MFT: clear its content. */
719 hr = IMFMediaBuffer_SetCurrentLength(output_media_buffer, 0);
725 /* Sample is provided by the MFT: decrease refcount. */
726 IMFSample_Release(output_sample);
729 else if (hr == MF_E_TRANSFORM_STREAM_CHANGE || hr == MF_E_TRANSFORM_TYPE_NOT_SET)
731 if (p_sys->output_type)
732 IMFMediaType_Release(p_sys->output_type);
733 if (SetOutputType(p_dec, p_sys->output_stream_id, &p_sys->output_type))
736 /* Reallocate output sample. */
737 if (p_sys->output_sample)
738 IMFSample_Release(p_sys->output_sample);
739 p_sys->output_sample = NULL;
740 if (AllocateOutputSample(p_dec, 0, &p_sys->output_sample))
744 else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT)
748 else /* An error not listed above occurred */
750 msg_Err(p_dec, "Unexpected error in IMFTransform::ProcessOutput: %#lx",
755 if (p_dec->fmt_in.i_cat == VIDEO_ES)
758 *result = aout_buffer;
763 msg_Err(p_dec, "Error in ProcessOutputStream()");
765 picture_Release(picture);
767 block_Release(aout_buffer);
771 static void *DecodeSync(decoder_t *p_dec, block_t **pp_block)
773 decoder_sys_t *p_sys = p_dec->p_sys;
775 if (!pp_block || !*pp_block)
778 block_t *p_block = *pp_block;
779 if (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED))
781 block_Release(p_block);
785 /* Drain the output stream before sending the input packet. */
787 if (ProcessOutputStream(p_dec, p_sys->output_stream_id, &result))
792 if (ProcessInputStream(p_dec, p_sys->input_stream_id, p_block))
795 block_Release(p_block);
801 msg_Err(p_dec, "Error in DecodeSync()");
803 block_Release(p_block);
807 static HRESULT DequeueMediaEvent(decoder_t *p_dec)
809 decoder_sys_t *p_sys = p_dec->p_sys;
812 IMFMediaEvent *event = NULL;
813 hr = IMFMediaEventGenerator_GetEvent(p_sys->event_generator, MF_EVENT_FLAG_NO_WAIT, &event);
816 MediaEventType event_type;
817 hr = IMFMediaEvent_GetType(event, &event_type);
818 IMFMediaEvent_Release(event);
822 if (event_type == VLC_METransformNeedInput)
823 p_sys->pending_input_events += 1;
824 else if (event_type == VLC_METransformHaveOutput)
825 p_sys->pending_output_events += 1;
827 msg_Err(p_dec, "Unsupported asynchronous event.");
832 static void *DecodeAsync(decoder_t *p_dec, block_t **pp_block)
834 decoder_sys_t *p_sys = p_dec->p_sys;
837 if (!pp_block || !*pp_block)
840 block_t *p_block = *pp_block;
841 if (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED))
843 block_Release(p_block);
848 /* Dequeue all pending media events. */
849 while ((hr = DequeueMediaEvent(p_dec)) == S_OK)
851 if (hr != MF_E_NO_EVENTS_AVAILABLE && FAILED(hr))
854 /* Drain the output stream of the MFT before sending the input packet. */
855 if (p_sys->pending_output_events > 0)
857 p_sys->pending_output_events -= 1;
859 if (ProcessOutputStream(p_dec, p_sys->output_stream_id, &result))
864 /* Poll the MFT and return decoded frames until the input stream is ready. */
865 while (p_sys->pending_input_events == 0)
867 hr = DequeueMediaEvent(p_dec);
868 if (hr == MF_E_NO_EVENTS_AVAILABLE)
870 /* Sleep for 1 ms to avoid excessive polling. */
877 if (p_sys->pending_output_events > 0)
879 p_sys->pending_output_events -= 1;
881 if (ProcessOutputStream(p_dec, p_sys->output_stream_id, &result))
887 p_sys->pending_input_events -= 1;
888 if (ProcessInputStream(p_dec, p_sys->input_stream_id, p_block))
891 block_Release(p_block);
897 msg_Err(p_dec, "Error in DecodeAsync()");
898 block_Release(p_block);
902 static void DestroyMFT(decoder_t *p_dec);
904 static int InitializeMFT(decoder_t *p_dec)
906 decoder_sys_t *p_sys = p_dec->p_sys;
909 IMFAttributes *attributes = NULL;
910 hr = IMFTransform_GetAttributes(p_sys->mft, &attributes);
911 if (hr != E_NOTIMPL && FAILED(hr))
915 UINT32 is_async = false;
916 hr = IMFAttributes_GetUINT32(attributes, &MF_TRANSFORM_ASYNC, &is_async);
917 if (hr != MF_E_ATTRIBUTENOTFOUND && FAILED(hr))
919 p_sys->is_async = is_async;
922 hr = IMFAttributes_SetUINT32(attributes, &MF_TRANSFORM_ASYNC_UNLOCK, true);
925 hr = IMFTransform_QueryInterface(p_sys->mft, &IID_IMFMediaEventGenerator, (void**)&p_sys->event_generator);
931 DWORD input_streams_count;
932 DWORD output_streams_count;
933 hr = IMFTransform_GetStreamCount(p_sys->mft, &input_streams_count, &output_streams_count);
936 if (input_streams_count != 1 || output_streams_count != 1)
938 msg_Err(p_dec, "MFT decoder should have 1 input stream and 1 output stream.");
942 hr = IMFTransform_GetStreamIDs(p_sys->mft, 1, &p_sys->input_stream_id, 1, &p_sys->output_stream_id);
946 * This is not an error, it happens if:
947 * - there is a fixed number of streams.
949 * - streams are numbered consecutively from 0 to N-1.
951 p_sys->input_stream_id = 0;
952 p_sys->output_stream_id = 0;
957 if (SetInputType(p_dec, p_sys->input_stream_id, &p_sys->input_type))
960 if (SetOutputType(p_dec, p_sys->output_stream_id, &p_sys->output_type))
964 * The input type was not set by the previous call to
965 * SetInputType, try again after setting the output type.
967 if (!p_sys->input_type)
968 if (SetInputType(p_dec, p_sys->input_stream_id, &p_sys->input_type) || !p_sys->input_type)
971 /* This call can be a no-op for some MFT decoders, but it can potentially reduce starting time. */
972 hr = IMFTransform_ProcessMessage(p_sys->mft, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, (ULONG_PTR)0);
976 /* This event is required for asynchronous MFTs, optional otherwise. */
977 hr = IMFTransform_ProcessMessage(p_sys->mft, MFT_MESSAGE_NOTIFY_START_OF_STREAM, (ULONG_PTR)0);
981 if (p_dec->fmt_in.i_codec == VLC_CODEC_H264)
983 /* It's not an error if the following call fails. */
984 IMFAttributes_SetUINT32(attributes, &CODECAPI_AVLowLatencyMode, true);
986 if (p_dec->fmt_in.i_extra)
988 int buf_size = p_dec->fmt_in.i_extra + 20;
989 uint32_t size = p_dec->fmt_in.i_extra;
990 uint8_t *buf = malloc(buf_size);
991 if (((uint8_t*)p_dec->fmt_in.p_extra)[0] == 1)
993 convert_sps_pps(p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
995 &size, &p_sys->nal_size);
1003 msg_Err(p_dec, "Error in InitializeMFT()");
1005 return VLC_EGENERIC;
1008 static void DestroyMFT(decoder_t *p_dec)
1010 decoder_sys_t *p_sys = p_dec->p_sys;
1012 if (p_sys->event_generator)
1013 IMFMediaEventGenerator_Release(p_sys->event_generator);
1014 if (p_sys->input_type)
1015 IMFMediaType_Release(p_sys->input_type);
1016 if (p_sys->output_sample)
1018 IMFMediaBuffer *output_media_buffer = NULL;
1019 HRESULT hr = IMFSample_GetBufferByIndex(p_sys->output_sample, 0, &output_media_buffer);
1021 IMFSample_Release(output_media_buffer);
1022 IMFSample_Release(p_sys->output_sample);
1024 if (p_sys->output_type)
1025 IMFMediaType_Release(p_sys->output_type);
1027 IMFTransform_Release(p_sys->mft);
1029 p_sys->event_generator = NULL;
1030 p_sys->input_type = NULL;
1031 p_sys->output_sample = NULL;
1032 p_sys->output_type = NULL;
1036 static int FindMFT(decoder_t *p_dec)
1038 decoder_sys_t *p_sys = p_dec->p_sys;
1039 MFHandle *mf = &p_sys->mf_handle;
1042 /* Try to create a MFT using MFTEnumEx. */
1044 if (p_dec->fmt_in.i_cat == VIDEO_ES)
1046 category = MFT_CATEGORY_VIDEO_DECODER;
1047 p_sys->major_type = &MFMediaType_Video;
1048 p_sys->subtype = FormatToGUID(video_format_table, p_dec->fmt_in.i_codec);
1052 category = MFT_CATEGORY_AUDIO_DECODER;
1053 p_sys->major_type = &MFMediaType_Audio;
1054 p_sys->subtype = FormatToGUID(audio_format_table, p_dec->fmt_in.i_codec);
1056 if (!p_sys->subtype)
1057 return VLC_EGENERIC;
1059 UINT32 flags = MFT_ENUM_FLAG_SORTANDFILTER | MFT_ENUM_FLAG_LOCALMFT
1060 | MFT_ENUM_FLAG_SYNCMFT | MFT_ENUM_FLAG_ASYNCMFT
1061 | MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_TRANSCODE_ONLY;
1062 MFT_REGISTER_TYPE_INFO input_type = { *p_sys->major_type, *p_sys->subtype };
1063 IMFActivate **activate_objects = NULL;
1064 UINT32 activate_objects_count = 0;
1065 hr = mf->fptr_MFTEnumEx(category, flags, &input_type, NULL, &activate_objects, &activate_objects_count);
1067 return VLC_EGENERIC;
1069 msg_Dbg(p_dec, "Found %d available MFT module(s)", activate_objects_count);
1070 if (activate_objects_count == 0)
1071 return VLC_EGENERIC;
1073 for (UINT32 i = 0; i < activate_objects_count; ++i)
1075 hr = IMFActivate_ActivateObject(activate_objects[i], &IID_IMFTransform, (void**)&p_sys->mft);
1076 IMFActivate_Release(activate_objects[i]);
1080 if (InitializeMFT(p_dec) == VLC_SUCCESS)
1082 CoTaskMemFree(activate_objects);
1086 CoTaskMemFree(activate_objects);
1088 return VLC_EGENERIC;
1091 static int LoadMFTLibrary(MFHandle *mf)
1093 #if _WIN32_WINNT < 0x601
1094 mf->mfplat_dll = LoadLibrary(TEXT("mfplat.dll"));
1095 if (!mf->mfplat_dll)
1096 return VLC_EGENERIC;
1098 mf->fptr_MFTEnumEx = (void*)GetProcAddress(mf->mfplat_dll, "MFTEnumEx");
1099 mf->fptr_MFCreateSample = (void*)GetProcAddress(mf->mfplat_dll, "MFCreateSample");
1100 mf->fptr_MFCreateMemoryBuffer = (void*)GetProcAddress(mf->mfplat_dll, "MFCreateMemoryBuffer");
1101 mf->fptr_MFCreateAlignedMemoryBuffer = (void*)GetProcAddress(mf->mfplat_dll, "MFCreateAlignedMemoryBuffer");
1102 if (!mf->fptr_MFTEnumEx || !mf->fptr_MFCreateSample || !mf->fptr_MFCreateMemoryBuffer || !mf->fptr_MFCreateAlignedMemoryBuffer)
1103 return VLC_EGENERIC;
1105 mf->fptr_MFTEnumEx = &MFTEnumEx;
1106 mf->fptr_MFCreateSample = &MFCreateSample;
1107 mf->fptr_MFCreateMemoryBuffer = &MFCreateMemoryBuffer;
1108 mf->fptr_MFCreateAlignedMemoryBuffer = &MFCreateAlignedMemoryBuffer;
1114 int Open(vlc_object_t *p_this)
1116 decoder_t *p_dec = (decoder_t *)p_this;
1117 decoder_sys_t *p_sys;
1119 if (p_dec->fmt_in.i_cat != VIDEO_ES && p_dec->fmt_in.i_cat != AUDIO_ES)
1120 return VLC_EGENERIC;
1122 p_sys = p_dec->p_sys = calloc(1, sizeof(*p_sys));
1126 if( FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)) )
1127 vlc_assert_unreachable();
1129 if (LoadMFTLibrary(&p_sys->mf_handle))
1131 msg_Err(p_dec, "Failed to load MFT library.");
1137 msg_Err(p_dec, "Could not find suitable MFT decoder");
1141 /* Only one output sample is needed, we can allocate one and reuse it. */
1142 if (AllocateOutputSample(p_dec, 0, &p_sys->output_sample))
1145 if (p_sys->is_async)
1147 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))DecodeAsync;
1148 p_dec->pf_decode_audio = (block_t *(*)(decoder_t *, block_t **))DecodeAsync;
1152 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))DecodeSync;
1153 p_dec->pf_decode_audio = (block_t *(*)(decoder_t *, block_t **))DecodeSync;
1156 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
1157 p_dec->b_need_packetized = true;
1163 return VLC_EGENERIC;
1166 void Close(vlc_object_t *p_this)
1168 decoder_t *p_dec = (decoder_t *)p_this;
1169 decoder_sys_t *p_sys = p_dec->p_sys;
1170 MFHandle *mf = &p_sys->mf_handle;
1175 FreeLibrary(mf->mfplat_dll);