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
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_codec.h>
44 #define _VIDEOINFOHEADER_
45 #include <vlc_codecs.h>
48 #include <mftransform.h>
50 #include <mfobjects.h>
52 static int Open(vlc_object_t *);
53 static void Close(vlc_object_t *);
56 set_description(N_("Media Foundation Transform decoder"))
58 set_capability("decoder", 1)
59 set_callbacks(Open, Close)
60 set_category(CAT_INPUT)
61 set_subcategory(SUBCAT_INPUT_VCODEC)
67 HRESULT (STDCALL *MFTEnumEx)(GUID guidCategory, UINT32 Flags,
68 const MFT_REGISTER_TYPE_INFO *pInputType,
69 const MFT_REGISTER_TYPE_INFO *pOutputType,
70 IMFActivate ***pppMFTActivate, UINT32 *pcMFTActivate);
71 HRESULT (STDCALL *MFCreateSample)(IMFSample **ppIMFSample);
72 HRESULT (STDCALL *MFCreateMemoryBuffer)(DWORD cbMaxLength, IMFMediaBuffer **ppBuffer);
73 HRESULT (STDCALL *MFCreateAlignedMemoryBuffer)(DWORD cbMaxLength, DWORD fAlignmentFlags, IMFMediaBuffer **ppBuffer);
82 const GUID* major_type;
85 /* For asynchronous MFT */
87 IMFMediaEventGenerator *event_generator;
88 int pending_input_events;
89 int pending_output_events;
92 DWORD input_stream_id;
93 IMFMediaType *input_type;
96 DWORD output_stream_id;
97 IMFSample *output_sample;
98 IMFMediaType *output_type;
104 static const int pi_channels_maps[9] =
108 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
109 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
110 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
111 | AOUT_CHAN_REARRIGHT,
112 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
113 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
114 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
115 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
116 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
117 | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
118 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
119 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
120 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
124 /* Possibly missing from mingw headers */
125 #ifndef MF_E_TRANSFORM_NEED_MORE_INPUT
126 # define MF_E_TRANSFORM_NEED_MORE_INPUT _HRESULT_TYPEDEF_(0xc00d6d72)
129 #ifndef MF_E_TRANSFORM_STREAM_CHANGE
130 # define MF_E_TRANSFORM_STREAM_CHANGE _HRESULT_TYPEDEF_(0xc00d6d61)
133 #ifndef MF_E_NO_EVENTS_AVAILABLE
134 # define MF_E_NO_EVENTS_AVAILABLE _HRESULT_TYPEDEF_(0xC00D3E80L)
137 #ifndef MF_EVENT_FLAG_NO_WAIT
138 # define MF_EVENT_FLAG_NO_WAIT 0x00000001
142 * The MFTransformXXX values might not be defined in mingw headers,
143 * thus we use our own enum with the VLC prefix.
147 VLC_METransformUnknown = 600,
148 VLC_METransformNeedInput,
149 VLC_METransformHaveOutput,
150 VLC_METransformDrainComplete,
151 VLC_METransformMarker,
161 * We need this table since the FOURCC used for GUID is not the same
162 * as the FOURCC used by VLC, for instance h264 vs H264.
164 static const pair_format_guid video_format_table[] =
166 { VLC_CODEC_H264, &MFVideoFormat_H264 },
167 { VLC_CODEC_MJPG, &MFVideoFormat_MJPG },
168 { VLC_CODEC_WMV1, &MFVideoFormat_WMV1 },
169 { VLC_CODEC_WMV2, &MFVideoFormat_WMV2 },
170 { VLC_CODEC_WMV3, &MFVideoFormat_WMV3 },
171 { VLC_CODEC_VC1, &MFVideoFormat_WVC1 },
175 DEFINE_GUID(MFAudioFormat_Dolby_AC3, 0xe06d802c, 0xdb46, 0x11cf, 0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea);
177 * We cannot use the FOURCC code for audio either since the
178 * WAVE_FORMAT value is used to create the GUID.
180 static const pair_format_guid audio_format_table[] =
182 { VLC_CODEC_MPGA, &MFAudioFormat_MPEG },
183 { VLC_CODEC_MP3, &MFAudioFormat_MP3 },
184 { VLC_CODEC_DTS, &MFAudioFormat_DTS },
185 { VLC_CODEC_MP4A, &MFAudioFormat_AAC },
186 { VLC_CODEC_WMA2, &MFAudioFormat_WMAudioV8 },
187 { VLC_CODEC_A52, &MFAudioFormat_Dolby_AC3 },
191 static const GUID *FormatToGUID(const pair_format_guid table[], vlc_fourcc_t fourcc)
193 for (int i = 0; table[i].fourcc; ++i)
194 if (table[i].fourcc == fourcc)
195 return table[i].guid;
201 * Low latency mode for Windows 8. Without this option, the H264
202 * decoder will fill *all* its internal buffers before returning a
203 * frame. Because of this behavior, the decoder might return no frame
204 * for more than 500 ms, making it unusable for playback.
206 DEFINE_GUID(CODECAPI_AVLowLatencyMode, 0x9c27891a, 0xed7a, 0x40e1, 0x88, 0xe8, 0xb2, 0x27, 0x27, 0xa0, 0x24, 0xee);
208 static int SetInputType(decoder_t *p_dec, DWORD stream_id, IMFMediaType **result)
210 decoder_sys_t *p_sys = p_dec->p_sys;
215 IMFMediaType *input_media_type = NULL;
217 /* Search a suitable input type for the MFT. */
218 int input_type_index = 0;
220 for (int i = 0; !found; ++i)
222 hr = IMFTransform_GetInputAvailableType(p_sys->mft, stream_id, i, &input_media_type);
223 if (hr == MF_E_NO_MORE_TYPES)
225 else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET)
227 /* The output type must be set before setting the input type for this MFT. */
234 hr = IMFMediaType_GetGUID(input_media_type, &MF_MT_SUBTYPE, &subtype);
238 if (IsEqualGUID(&subtype, p_sys->subtype))
242 input_type_index = i;
244 IMFMediaType_Release(input_media_type);
245 input_media_type = NULL;
250 hr = IMFTransform_GetInputAvailableType(p_sys->mft, stream_id, input_type_index, &input_media_type);
254 if (p_dec->fmt_in.i_cat == VIDEO_ES)
256 UINT64 width = p_dec->fmt_in.video.i_width;
257 UINT64 height = p_dec->fmt_in.video.i_height;
258 UINT64 frame_size = (width << 32) | height;
259 hr = IMFMediaType_SetUINT64(input_media_type, &MF_MT_FRAME_SIZE, frame_size);
265 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_ORIGINAL_WAVE_FORMAT_TAG, p_sys->subtype->Data1);
268 if (p_dec->fmt_in.audio.i_rate)
270 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, p_dec->fmt_in.audio.i_rate);
274 if (p_dec->fmt_in.audio.i_channels)
276 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_NUM_CHANNELS, p_dec->fmt_in.audio.i_channels);
280 if (p_dec->fmt_in.audio.i_bitspersample)
282 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_BITS_PER_SAMPLE, p_dec->fmt_in.audio.i_bitspersample);
286 if (p_dec->fmt_in.audio.i_blockalign)
288 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_BLOCK_ALIGNMENT, p_dec->fmt_in.audio.i_blockalign);
292 if (p_dec->fmt_in.i_bitrate)
294 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, p_dec->fmt_in.i_bitrate / 8);
300 if (p_dec->fmt_in.i_extra > 0)
302 UINT32 blob_size = 0;
303 hr = IMFMediaType_GetBlobSize(input_media_type, &MF_MT_USER_DATA, &blob_size);
305 * Do not overwrite existing user data in the input type, this
306 * can cause the MFT to reject the type.
308 if (hr == MF_E_ATTRIBUTENOTFOUND)
310 hr = IMFMediaType_SetBlob(input_media_type, &MF_MT_USER_DATA,
311 (const UINT8*)p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra);
317 hr = IMFTransform_SetInputType(p_sys->mft, stream_id, input_media_type, 0);
321 *result = input_media_type;
326 msg_Err(p_dec, "Error in SetInputType()");
327 if (input_media_type)
328 IMFMediaType_Release(input_media_type);
332 static int SetOutputType(decoder_t *p_dec, DWORD stream_id, IMFMediaType **result)
334 decoder_sys_t *p_sys = p_dec->p_sys;
339 IMFMediaType *output_media_type = NULL;
342 * Enumerate available output types. The list is ordered by
343 * preference thus we will use the first one unless YV12/I420 is
344 * available for video or float32 for audio.
346 int output_type_index = 0;
348 for (int i = 0; !found; ++i)
350 hr = IMFTransform_GetOutputAvailableType(p_sys->mft, stream_id, i, &output_media_type);
351 if (hr == MF_E_NO_MORE_TYPES)
353 else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET)
355 /* The input type must be set before setting the output type for this MFT. */
362 hr = IMFMediaType_GetGUID(output_media_type, &MF_MT_SUBTYPE, &subtype);
366 if (p_dec->fmt_in.i_cat == VIDEO_ES)
368 if (IsEqualGUID(&subtype, &MFVideoFormat_YV12) || IsEqualGUID(&subtype, &MFVideoFormat_I420))
373 UINT32 bits_per_sample;
374 hr = IMFMediaType_GetUINT32(output_media_type, &MF_MT_AUDIO_BITS_PER_SAMPLE, &bits_per_sample);
377 if (bits_per_sample == 32 && IsEqualGUID(&subtype, &MFAudioFormat_Float))
382 output_type_index = i;
384 IMFMediaType_Release(output_media_type);
385 output_media_type = NULL;
388 * It's not an error if we don't find the output type we were
389 * looking for, in this case we use the first available type which
390 * is the "preferred" output type for this MFT.
393 hr = IMFTransform_GetOutputAvailableType(p_sys->mft, stream_id, output_type_index, &output_media_type);
397 hr = IMFTransform_SetOutputType(p_sys->mft, stream_id, output_media_type, 0);
402 hr = IMFMediaType_GetGUID(output_media_type, &MF_MT_SUBTYPE, &subtype);
406 if (p_dec->fmt_in.i_cat == VIDEO_ES)
408 p_dec->fmt_out.video = p_dec->fmt_in.video;
409 p_dec->fmt_out.i_codec = vlc_fourcc_GetCodec(p_dec->fmt_in.i_cat, subtype.Data1);
413 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
415 UINT32 bitspersample = 0;
416 hr = IMFMediaType_GetUINT32(output_media_type, &MF_MT_AUDIO_BITS_PER_SAMPLE, &bitspersample);
417 if (SUCCEEDED(hr) && bitspersample)
418 p_dec->fmt_out.audio.i_bitspersample = bitspersample;
421 hr = IMFMediaType_GetUINT32(output_media_type, &MF_MT_AUDIO_NUM_CHANNELS, &channels);
422 if (SUCCEEDED(hr) && channels)
423 p_dec->fmt_out.audio.i_channels = channels;
426 hr = IMFMediaType_GetUINT32(output_media_type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &rate);
427 if (SUCCEEDED(hr) && rate)
428 p_dec->fmt_out.audio.i_rate = rate;
431 wf_tag_to_fourcc(subtype.Data1, &fourcc, NULL);
432 p_dec->fmt_out.i_codec = vlc_fourcc_GetCodecAudio(fourcc, p_dec->fmt_out.audio.i_bitspersample);
434 p_dec->fmt_out.audio.i_physical_channels = pi_channels_maps[p_dec->fmt_out.audio.i_channels];
435 p_dec->fmt_out.audio.i_original_channels = p_dec->fmt_out.audio.i_physical_channels;
438 *result = output_media_type;
443 msg_Err(p_dec, "Error in SetOutputType()");
444 if (output_media_type)
445 IMFMediaType_Release(output_media_type);
449 static int AllocateInputSample(decoder_t *p_dec, DWORD stream_id, IMFSample** result, DWORD size)
451 decoder_sys_t *p_sys = p_dec->p_sys;
452 MFHandle *mf = &p_sys->mf_handle;
457 IMFSample *input_sample = NULL;
459 MFT_INPUT_STREAM_INFO input_info;
460 hr = IMFTransform_GetInputStreamInfo(p_sys->mft, stream_id, &input_info);
464 hr = mf->MFCreateSample(&input_sample);
468 IMFMediaBuffer *input_media_buffer = NULL;
469 DWORD allocation_size = __MAX(input_info.cbSize, size);
470 hr = mf->MFCreateMemoryBuffer(allocation_size, &input_media_buffer);
474 hr = IMFSample_AddBuffer(input_sample, input_media_buffer);
475 IMFMediaBuffer_Release(input_media_buffer);
479 *result = input_sample;
484 msg_Err(p_dec, "Error in AllocateInputSample()");
486 IMFSample_Release(input_sample);
487 if (input_media_buffer)
488 IMFMediaBuffer_Release(input_media_buffer);
492 static int AllocateOutputSample(decoder_t *p_dec, DWORD stream_id, IMFSample **result)
494 decoder_sys_t *p_sys = p_dec->p_sys;
495 MFHandle *mf = &p_sys->mf_handle;
500 IMFSample *output_sample = NULL;
502 MFT_OUTPUT_STREAM_INFO output_info;
503 hr = IMFTransform_GetOutputStreamInfo(p_sys->mft, stream_id, &output_info);
507 if (output_info.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES | MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES))
509 /* The MFT will provide an allocated sample. */
513 DWORD expected_flags = 0;
514 if (p_dec->fmt_in.i_cat == VIDEO_ES)
515 expected_flags |= MFT_OUTPUT_STREAM_WHOLE_SAMPLES
516 | MFT_OUTPUT_STREAM_SINGLE_SAMPLE_PER_BUFFER
517 | MFT_OUTPUT_STREAM_FIXED_SAMPLE_SIZE;
518 if ((output_info.dwFlags & expected_flags) != expected_flags)
521 hr = mf->MFCreateSample(&output_sample);
525 IMFMediaBuffer *output_media_buffer = NULL;
526 DWORD allocation_size = output_info.cbSize;
527 DWORD alignment = output_info.cbAlignment;
529 hr = mf->MFCreateAlignedMemoryBuffer(allocation_size, alignment - 1, &output_media_buffer);
531 hr = mf->MFCreateMemoryBuffer(allocation_size, &output_media_buffer);
535 hr = IMFSample_AddBuffer(output_sample, output_media_buffer);
539 *result = output_sample;
544 msg_Err(p_dec, "Error in AllocateOutputSample()");
546 IMFSample_Release(output_sample);
550 static int ProcessInputStream(decoder_t *p_dec, DWORD stream_id, block_t *p_block)
552 decoder_sys_t *p_sys = p_dec->p_sys;
554 IMFSample *input_sample = NULL;
556 if (AllocateInputSample(p_dec, stream_id, &input_sample, p_block->i_buffer))
559 IMFMediaBuffer *input_media_buffer = NULL;
560 hr = IMFSample_GetBufferByIndex(input_sample, stream_id, &input_media_buffer);
565 hr = IMFMediaBuffer_Lock(input_media_buffer, &buffer_start, NULL, NULL);
569 memcpy(buffer_start, p_block->p_buffer, p_block->i_buffer);
571 if (p_dec->fmt_in.i_codec == VLC_CODEC_H264)
573 /* in-place NAL to annex B conversion. */
574 struct H264ConvertState convert_state = { 0, 0 };
575 convert_h264_to_annexb(buffer_start, p_block->i_buffer, p_sys->nal_size, &convert_state);
578 hr = IMFMediaBuffer_Unlock(input_media_buffer);
582 hr = IMFMediaBuffer_SetCurrentLength(input_media_buffer, p_block->i_buffer);
586 LONGLONG ts = p_block->i_pts;
587 if (!ts && p_block->i_dts)
590 /* Convert from microseconds to 100 nanoseconds unit. */
591 hr = IMFSample_SetSampleTime(input_sample, ts * 10);
595 hr = IMFTransform_ProcessInput(p_sys->mft, stream_id, input_sample, 0);
599 IMFMediaBuffer_Release(input_media_buffer);
600 IMFSample_Release(input_sample);
605 msg_Err(p_dec, "Error in ProcessInputStream()");
607 IMFSample_Release(input_sample);
611 /* Copy a packed buffer (no padding) to a picture_t */
612 static void CopyPackedBufferToPicture(picture_t *p_pic, const uint8_t *p_src)
614 for (int i = 0; i < p_pic->i_planes; ++i)
616 uint8_t *p_dst = p_pic->p[i].p_pixels;
618 if (p_pic->p[i].i_visible_pitch == p_pic->p[i].i_pitch)
620 /* Plane is packed, only one memcpy is needed. */
621 uint32_t plane_size = p_pic->p[i].i_pitch * p_pic->p[i].i_visible_lines;
622 memcpy(p_dst, p_src, plane_size);
627 for (int i_line = 0; i_line < p_pic->p[i].i_visible_lines; i_line++)
629 memcpy(p_dst, p_src, p_pic->p[i].i_visible_pitch);
630 p_src += p_pic->p[i].i_visible_pitch;
631 p_dst += p_pic->p[i].i_pitch;
636 static int ProcessOutputStream(decoder_t *p_dec, DWORD stream_id, void **result)
638 decoder_sys_t *p_sys = p_dec->p_sys;
640 picture_t *picture = NULL;
641 block_t *aout_buffer = NULL;
645 DWORD output_status = 0;
646 MFT_OUTPUT_DATA_BUFFER output_buffer = { stream_id, p_sys->output_sample, 0, NULL };
647 hr = IMFTransform_ProcessOutput(p_sys->mft, 0, 1, &output_buffer, &output_status);
648 if (output_buffer.pEvents)
649 IMFCollection_Release(output_buffer.pEvents);
650 /* Use the returned sample since it can be provided by the MFT. */
651 IMFSample *output_sample = output_buffer.pSample;
658 LONGLONG sample_time;
659 hr = IMFSample_GetSampleTime(output_sample, &sample_time);
662 /* Convert from 100 nanoseconds unit to microseconds. */
665 DWORD total_length = 0;
666 hr = IMFSample_GetTotalLength(output_sample, &total_length);
670 if (p_dec->fmt_in.i_cat == VIDEO_ES)
672 picture = decoder_NewPicture(p_dec);
676 UINT32 interlaced = false;
677 hr = IMFSample_GetUINT32(output_sample, &MFSampleExtension_Interlaced, &interlaced);
678 picture->b_progressive = !interlaced;
680 picture->date = sample_time;
684 if (p_dec->fmt_out.audio.i_bitspersample == 0 || p_dec->fmt_out.audio.i_channels == 0)
686 int samples = total_length / (p_dec->fmt_out.audio.i_bitspersample * p_dec->fmt_out.audio.i_channels / 8);
687 aout_buffer = decoder_NewAudioBuffer(p_dec, samples);
690 if (aout_buffer->i_buffer < total_length)
693 aout_buffer->i_pts = sample_time;
696 IMFMediaBuffer *output_media_buffer = NULL;
697 hr = IMFSample_GetBufferByIndex(output_sample, 0, &output_media_buffer);
700 hr = IMFMediaBuffer_Lock(output_media_buffer, &buffer_start, NULL, NULL);
704 if (p_dec->fmt_in.i_cat == VIDEO_ES)
705 CopyPackedBufferToPicture(picture, buffer_start);
707 memcpy(aout_buffer->p_buffer, buffer_start, total_length);
709 hr = IMFMediaBuffer_Unlock(output_media_buffer);
710 IMFSample_Release(output_media_buffer);
714 if (p_sys->output_sample)
716 /* Sample is not provided by the MFT: clear its content. */
717 hr = IMFMediaBuffer_SetCurrentLength(output_media_buffer, 0);
723 /* Sample is provided by the MFT: decrease refcount. */
724 IMFSample_Release(output_sample);
727 else if (hr == MF_E_TRANSFORM_STREAM_CHANGE || hr == MF_E_TRANSFORM_TYPE_NOT_SET)
729 if (p_sys->output_type)
730 IMFMediaType_Release(p_sys->output_type);
731 if (SetOutputType(p_dec, p_sys->output_stream_id, &p_sys->output_type))
734 /* Reallocate output sample. */
735 if (p_sys->output_sample)
736 IMFSample_Release(p_sys->output_sample);
737 p_sys->output_sample = NULL;
738 if (AllocateOutputSample(p_dec, 0, &p_sys->output_sample))
742 else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT)
746 else /* An error not listed above occurred */
748 msg_Err(p_dec, "Unexpected error in IMFTransform::ProcessOutput: %#x", hr);
752 if (p_dec->fmt_in.i_cat == VIDEO_ES)
755 *result = aout_buffer;
760 msg_Err(p_dec, "Error in ProcessOutputStream()");
762 picture_Release(picture);
764 block_Release(aout_buffer);
768 static void *DecodeSync(decoder_t *p_dec, block_t **pp_block)
770 decoder_sys_t *p_sys = p_dec->p_sys;
772 if (!pp_block || !*pp_block)
775 block_t *p_block = *pp_block;
776 if (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED))
778 block_Release(p_block);
782 /* Drain the output stream before sending the input packet. */
784 if (ProcessOutputStream(p_dec, p_sys->output_stream_id, &result))
789 if (ProcessInputStream(p_dec, p_sys->input_stream_id, p_block))
792 block_Release(p_block);
798 msg_Err(p_dec, "Error in DecodeSync()");
800 block_Release(p_block);
804 static HRESULT DequeueMediaEvent(decoder_t *p_dec)
806 decoder_sys_t *p_sys = p_dec->p_sys;
809 IMFMediaEvent *event = NULL;
810 hr = IMFMediaEventGenerator_GetEvent(p_sys->event_generator, MF_EVENT_FLAG_NO_WAIT, &event);
813 MediaEventType event_type;
814 hr = IMFMediaEvent_GetType(event, &event_type);
815 IMFMediaEvent_Release(event);
819 if (event_type == VLC_METransformNeedInput)
820 p_sys->pending_input_events += 1;
821 else if (event_type == VLC_METransformHaveOutput)
822 p_sys->pending_output_events += 1;
824 msg_Err(p_dec, "Unsupported asynchronous event.");
829 static void *DecodeAsync(decoder_t *p_dec, block_t **pp_block)
831 decoder_sys_t *p_sys = p_dec->p_sys;
834 if (!pp_block || !*pp_block)
837 block_t *p_block = *pp_block;
838 if (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED))
840 block_Release(p_block);
845 /* Dequeue all pending media events. */
846 while ((hr = DequeueMediaEvent(p_dec)) == S_OK)
848 if (hr != MF_E_NO_EVENTS_AVAILABLE && FAILED(hr))
851 /* Drain the output stream of the MFT before sending the input packet. */
852 if (p_sys->pending_output_events > 0)
854 p_sys->pending_output_events -= 1;
856 if (ProcessOutputStream(p_dec, p_sys->output_stream_id, &result))
861 /* Poll the MFT and return decoded frames until the input stream is ready. */
862 while (p_sys->pending_input_events == 0)
864 hr = DequeueMediaEvent(p_dec);
865 if (hr == MF_E_NO_EVENTS_AVAILABLE)
867 /* Sleep for 1 ms to avoid excessive polling. */
874 if (p_sys->pending_output_events > 0)
876 p_sys->pending_output_events -= 1;
878 if (ProcessOutputStream(p_dec, p_sys->output_stream_id, &result))
884 p_sys->pending_input_events -= 1;
885 if (ProcessInputStream(p_dec, p_sys->input_stream_id, p_block))
888 block_Release(p_block);
894 msg_Err(p_dec, "Error in DecodeAsync()");
895 block_Release(p_block);
899 static void DestroyMFT(decoder_t *p_dec);
901 static int InitializeMFT(decoder_t *p_dec)
903 decoder_sys_t *p_sys = p_dec->p_sys;
906 IMFAttributes *attributes = NULL;
907 hr = IMFTransform_GetAttributes(p_sys->mft, &attributes);
908 if (hr != E_NOTIMPL && FAILED(hr))
912 UINT32 is_async = false;
913 hr = IMFAttributes_GetUINT32(attributes, &MF_TRANSFORM_ASYNC, &is_async);
914 if (hr != MF_E_ATTRIBUTENOTFOUND && FAILED(hr))
916 p_sys->is_async = is_async;
919 hr = IMFAttributes_SetUINT32(attributes, &MF_TRANSFORM_ASYNC_UNLOCK, true);
922 hr = IMFTransform_QueryInterface(p_sys->mft, &IID_IMFMediaEventGenerator, (void**)&p_sys->event_generator);
928 DWORD input_streams_count;
929 DWORD output_streams_count;
930 hr = IMFTransform_GetStreamCount(p_sys->mft, &input_streams_count, &output_streams_count);
933 if (input_streams_count != 1 || output_streams_count != 1)
935 msg_Err(p_dec, "MFT decoder should have 1 input stream and 1 output stream.");
939 hr = IMFTransform_GetStreamIDs(p_sys->mft, 1, &p_sys->input_stream_id, 1, &p_sys->output_stream_id);
943 * This is not an error, it happens if:
944 * - there is a fixed number of streams.
946 * - streams are numbered consecutively from 0 to N-1.
948 p_sys->input_stream_id = 0;
949 p_sys->output_stream_id = 0;
954 if (SetInputType(p_dec, p_sys->input_stream_id, &p_sys->input_type))
957 if (SetOutputType(p_dec, p_sys->output_stream_id, &p_sys->output_type))
961 * The input type was not set by the previous call to
962 * SetInputType, try again after setting the output type.
964 if (!p_sys->input_type)
965 if (SetInputType(p_dec, p_sys->input_stream_id, &p_sys->input_type) || !p_sys->input_type)
968 /* This call can be a no-op for some MFT decoders, but it can potentially reduce starting time. */
969 hr = IMFTransform_ProcessMessage(p_sys->mft, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, (ULONG_PTR)0);
973 /* This event is required for asynchronous MFTs, optional otherwise. */
974 hr = IMFTransform_ProcessMessage(p_sys->mft, MFT_MESSAGE_NOTIFY_START_OF_STREAM, (ULONG_PTR)0);
978 if (p_dec->fmt_in.i_codec == VLC_CODEC_H264)
980 /* It's not an error if the following call fails. */
981 IMFAttributes_SetUINT32(attributes, &CODECAPI_AVLowLatencyMode, true);
983 if (p_dec->fmt_in.i_extra)
985 int buf_size = p_dec->fmt_in.i_extra + 20;
986 uint32_t size = p_dec->fmt_in.i_extra;
987 uint8_t *buf = malloc(buf_size);
988 if (((uint8_t*)p_dec->fmt_in.p_extra)[0] == 1)
990 convert_sps_pps(p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
992 &size, &p_sys->nal_size);
1000 msg_Err(p_dec, "Error in InitializeMFT()");
1002 return VLC_EGENERIC;
1005 static void DestroyMFT(decoder_t *p_dec)
1007 decoder_sys_t *p_sys = p_dec->p_sys;
1009 if (p_sys->event_generator)
1010 IMFMediaEventGenerator_Release(p_sys->event_generator);
1011 if (p_sys->input_type)
1012 IMFMediaType_Release(p_sys->input_type);
1013 if (p_sys->output_sample)
1015 IMFMediaBuffer *output_media_buffer = NULL;
1016 HRESULT hr = IMFSample_GetBufferByIndex(p_sys->output_sample, 0, &output_media_buffer);
1018 IMFSample_Release(output_media_buffer);
1019 IMFSample_Release(p_sys->output_sample);
1021 if (p_sys->output_type)
1022 IMFMediaType_Release(p_sys->output_type);
1024 IMFTransform_Release(p_sys->mft);
1026 p_sys->event_generator = NULL;
1027 p_sys->input_type = NULL;
1028 p_sys->output_sample = NULL;
1029 p_sys->output_type = NULL;
1033 static int FindMFT(decoder_t *p_dec)
1035 decoder_sys_t *p_sys = p_dec->p_sys;
1036 MFHandle *mf = &p_sys->mf_handle;
1039 /* Try to create a MFT using MFTEnumEx. */
1041 if (p_dec->fmt_in.i_cat == VIDEO_ES)
1043 category = MFT_CATEGORY_VIDEO_DECODER;
1044 p_sys->major_type = &MFMediaType_Video;
1045 p_sys->subtype = FormatToGUID(video_format_table, p_dec->fmt_in.i_codec);
1049 category = MFT_CATEGORY_AUDIO_DECODER;
1050 p_sys->major_type = &MFMediaType_Audio;
1051 p_sys->subtype = FormatToGUID(audio_format_table, p_dec->fmt_in.i_codec);
1053 if (!p_sys->subtype)
1054 return VLC_EGENERIC;
1056 UINT32 flags = MFT_ENUM_FLAG_SORTANDFILTER | MFT_ENUM_FLAG_LOCALMFT
1057 | MFT_ENUM_FLAG_SYNCMFT | MFT_ENUM_FLAG_ASYNCMFT
1058 | MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_TRANSCODE_ONLY;
1059 MFT_REGISTER_TYPE_INFO input_type = { *p_sys->major_type, *p_sys->subtype };
1060 IMFActivate **activate_objects = NULL;
1061 UINT32 activate_objects_count = 0;
1062 hr = mf->MFTEnumEx(category, flags, &input_type, NULL, &activate_objects, &activate_objects_count);
1064 return VLC_EGENERIC;
1066 msg_Dbg(p_dec, "Found %d available MFT module(s)", activate_objects_count);
1067 if (activate_objects_count == 0)
1068 return VLC_EGENERIC;
1070 for (UINT32 i = 0; i < activate_objects_count; ++i)
1072 hr = IMFActivate_ActivateObject(activate_objects[i], &IID_IMFTransform, (void**)&p_sys->mft);
1073 IMFActivate_Release(activate_objects[i]);
1077 if (InitializeMFT(p_dec) == VLC_SUCCESS)
1079 CoTaskMemFree(activate_objects);
1083 CoTaskMemFree(activate_objects);
1085 return VLC_EGENERIC;
1088 static int LoadMFTLibrary(MFHandle *mf)
1090 mf->mfplat_dll = LoadLibrary(TEXT("mfplat.dll"));
1091 if (!mf->mfplat_dll)
1092 return VLC_EGENERIC;
1094 mf->MFTEnumEx = (void*)GetProcAddress(mf->mfplat_dll, "MFTEnumEx");
1095 mf->MFCreateSample = (void*)GetProcAddress(mf->mfplat_dll, "MFCreateSample");
1096 mf->MFCreateMemoryBuffer = (void*)GetProcAddress(mf->mfplat_dll, "MFCreateMemoryBuffer");
1097 mf->MFCreateAlignedMemoryBuffer = (void*)GetProcAddress(mf->mfplat_dll, "MFCreateAlignedMemoryBuffer");
1098 if (!mf->MFTEnumEx || !mf->MFCreateSample || !mf->MFCreateMemoryBuffer || !mf->MFCreateAlignedMemoryBuffer)
1099 return VLC_EGENERIC;
1104 int Open(vlc_object_t *p_this)
1106 decoder_t *p_dec = (decoder_t *)p_this;
1107 decoder_sys_t *p_sys;
1109 if (p_dec->fmt_in.i_cat != VIDEO_ES && p_dec->fmt_in.i_cat != AUDIO_ES)
1110 return VLC_EGENERIC;
1112 p_sys = p_dec->p_sys = calloc(1, sizeof(*p_sys));
1116 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1118 if (LoadMFTLibrary(&p_sys->mf_handle))
1120 msg_Err(p_dec, "Failed to load MFT library.");
1126 msg_Err(p_dec, "Could not find suitable MFT decoder");
1130 /* Only one output sample is needed, we can allocate one and reuse it. */
1131 if (AllocateOutputSample(p_dec, 0, &p_sys->output_sample))
1134 if (p_sys->is_async)
1136 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))DecodeAsync;
1137 p_dec->pf_decode_audio = (block_t *(*)(decoder_t *, block_t **))DecodeAsync;
1141 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))DecodeSync;
1142 p_dec->pf_decode_audio = (block_t *(*)(decoder_t *, block_t **))DecodeSync;
1145 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
1146 p_dec->b_need_packetized = true;
1152 return VLC_EGENERIC;
1155 void Close(vlc_object_t *p_this)
1157 decoder_t *p_dec = (decoder_t *)p_this;
1158 decoder_sys_t *p_sys = p_dec->p_sys;
1159 MFHandle *mf = &p_sys->mf_handle;
1164 FreeLibrary(mf->mfplat_dll);