input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / codec / mft.c
blob09fd2c7b7ba42349b2c2898e88f2f3493a29da54
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 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #undef WINVER
28 #define WINVER 0x0601
30 /* Needed for many mingw macros. */
31 #define COBJMACROS
33 /* Avoid having GUIDs being defined as "extern". */
34 #define INITGUID
36 #ifndef STDCALL
37 # define STDCALL __stdcall
38 #endif
40 #include <winapifamily.h>
41 #undef WINAPI_FAMILY
42 #define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP
44 #include <assert.h>
46 #include <vlc_common.h>
47 #include <vlc_plugin.h>
48 #include <vlc_codec.h>
49 #include "../packetizer/h264_nal.h"
50 #define _VIDEOINFOHEADER_
51 #include <vlc_codecs.h>
53 #include <mfapi.h>
54 #include <mftransform.h>
55 #include <mferror.h>
56 #include <mfobjects.h>
58 static int Open(vlc_object_t *);
59 static void Close(vlc_object_t *);
61 vlc_module_begin()
62 set_description(N_("Media Foundation Transform decoder"))
63 add_shortcut("mft")
64 set_capability("video decoder", 1)
65 set_callbacks(Open, Close)
66 set_category(CAT_INPUT)
67 set_subcategory(SUBCAT_INPUT_VCODEC)
69 add_submodule()
70 add_shortcut("mft")
71 set_capability("audio decoder", 1)
72 set_callbacks(Open, Close)
73 vlc_module_end()
75 typedef struct
77 HINSTANCE mfplat_dll;
78 HRESULT (STDCALL *fptr_MFTEnumEx)(GUID guidCategory, UINT32 Flags,
79 const MFT_REGISTER_TYPE_INFO *pInputType,
80 const MFT_REGISTER_TYPE_INFO *pOutputType,
81 IMFActivate ***pppMFTActivate, UINT32 *pcMFTActivate);
82 HRESULT (STDCALL *fptr_MFCreateSample)(IMFSample **ppIMFSample);
83 HRESULT (STDCALL *fptr_MFCreateMemoryBuffer)(DWORD cbMaxLength, IMFMediaBuffer **ppBuffer);
84 HRESULT (STDCALL *fptr_MFCreateAlignedMemoryBuffer)(DWORD cbMaxLength, DWORD fAlignmentFlags, IMFMediaBuffer **ppBuffer);
85 } MFHandle;
87 typedef struct
89 MFHandle mf_handle;
91 IMFTransform *mft;
93 const GUID* major_type;
94 const GUID* subtype;
95 /* Container for a dynamically constructed subtype */
96 GUID custom_subtype;
98 /* For asynchronous MFT */
99 bool is_async;
100 IMFMediaEventGenerator *event_generator;
101 int pending_input_events;
102 int pending_output_events;
104 /* Input stream */
105 DWORD input_stream_id;
106 IMFMediaType *input_type;
108 /* Output stream */
109 DWORD output_stream_id;
110 IMFSample *output_sample;
111 IMFMediaType *output_type;
113 /* H264 only. */
114 uint8_t nal_length_size;
115 } decoder_sys_t;
117 static const int pi_channels_maps[9] =
120 AOUT_CHAN_CENTER,
121 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
122 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
123 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
124 | AOUT_CHAN_REARRIGHT,
125 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
126 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
127 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
128 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
129 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
130 | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
131 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
132 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
133 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
134 | AOUT_CHAN_LFE,
137 /* Possibly missing from mingw headers */
138 #ifndef MF_E_TRANSFORM_NEED_MORE_INPUT
139 # define MF_E_TRANSFORM_NEED_MORE_INPUT _HRESULT_TYPEDEF_(0xc00d6d72)
140 #endif
142 #ifndef MF_E_TRANSFORM_STREAM_CHANGE
143 # define MF_E_TRANSFORM_STREAM_CHANGE _HRESULT_TYPEDEF_(0xc00d6d61)
144 #endif
146 #ifndef MF_E_NO_EVENTS_AVAILABLE
147 # define MF_E_NO_EVENTS_AVAILABLE _HRESULT_TYPEDEF_(0xC00D3E80L)
148 #endif
150 #ifndef MF_EVENT_FLAG_NO_WAIT
151 # define MF_EVENT_FLAG_NO_WAIT 0x00000001
152 #endif
155 * The MFTransformXXX values might not be defined in mingw headers,
156 * thus we use our own enum with the VLC prefix.
158 enum
160 VLC_METransformUnknown = 600,
161 VLC_METransformNeedInput,
162 VLC_METransformHaveOutput,
163 VLC_METransformDrainComplete,
164 VLC_METransformMarker,
167 typedef struct
169 vlc_fourcc_t fourcc;
170 const GUID *guid;
171 } pair_format_guid;
174 * We need this table since the FOURCC used for GUID is not the same
175 * as the FOURCC used by VLC, for instance h264 vs H264.
177 static const pair_format_guid video_format_table[] =
179 { VLC_CODEC_H264, &MFVideoFormat_H264 },
180 { VLC_CODEC_MJPG, &MFVideoFormat_MJPG },
181 { VLC_CODEC_WMV1, &MFVideoFormat_WMV1 },
182 { VLC_CODEC_WMV2, &MFVideoFormat_WMV2 },
183 { VLC_CODEC_WMV3, &MFVideoFormat_WMV3 },
184 { VLC_CODEC_VC1, &MFVideoFormat_WVC1 },
185 { 0, NULL }
188 // 8-bit luminance only
189 DEFINE_MEDIATYPE_GUID (MFVideoFormat_L8, 50);
192 * Table to map MF Transform raw 3D3 output formats to native VLC FourCC
194 static const pair_format_guid d3d_format_table[] = {
195 { VLC_CODEC_RGB32, &MFVideoFormat_RGB32 },
196 { VLC_CODEC_RGB24, &MFVideoFormat_RGB24 },
197 { VLC_CODEC_RGBA, &MFVideoFormat_ARGB32 },
198 { VLC_CODEC_GREY, &MFVideoFormat_L8 },
199 { 0, NULL }
202 #if defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 4
203 DEFINE_GUID(MFAudioFormat_Dolby_AC3, 0xe06d802c, 0xdb46, 0x11cf, 0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea);
204 #endif
206 * We cannot use the FOURCC code for audio either since the
207 * WAVE_FORMAT value is used to create the GUID.
209 static const pair_format_guid audio_format_table[] =
211 { VLC_CODEC_MPGA, &MFAudioFormat_MPEG },
212 { VLC_CODEC_MP3, &MFAudioFormat_MP3 },
213 { VLC_CODEC_DTS, &MFAudioFormat_DTS },
214 { VLC_CODEC_MP4A, &MFAudioFormat_AAC },
215 { VLC_CODEC_WMA2, &MFAudioFormat_WMAudioV8 },
216 { VLC_CODEC_A52, &MFAudioFormat_Dolby_AC3 },
217 { 0, NULL }
220 static const GUID *FormatToGUID(const pair_format_guid table[], vlc_fourcc_t fourcc)
222 for (int i = 0; table[i].fourcc; ++i)
223 if (table[i].fourcc == fourcc)
224 return table[i].guid;
226 return NULL;
229 static vlc_fourcc_t GUIDToFormat(const pair_format_guid table[], const GUID* guid)
231 for (int i = 0; table[i].fourcc; ++i)
232 if (IsEqualGUID(table[i].guid, guid))
233 return table[i].fourcc;
235 return 0;
239 * Low latency mode for Windows 8. Without this option, the H264
240 * decoder will fill *all* its internal buffers before returning a
241 * frame. Because of this behavior, the decoder might return no frame
242 * for more than 500 ms, making it unusable for playback.
244 DEFINE_GUID(CODECAPI_AVLowLatencyMode, 0x9c27891a, 0xed7a, 0x40e1, 0x88, 0xe8, 0xb2, 0x27, 0x27, 0xa0, 0x24, 0xee);
246 static int SetInputType(decoder_t *p_dec, DWORD stream_id, IMFMediaType **result)
248 decoder_sys_t *p_sys = p_dec->p_sys;
249 HRESULT hr;
251 *result = NULL;
253 IMFMediaType *input_media_type = NULL;
255 /* Search a suitable input type for the MFT. */
256 int input_type_index = 0;
257 bool found = false;
258 for (int i = 0; !found; ++i)
260 hr = IMFTransform_GetInputAvailableType(p_sys->mft, stream_id, i, &input_media_type);
261 if (hr == MF_E_NO_MORE_TYPES)
262 break;
263 else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET)
265 /* The output type must be set before setting the input type for this MFT. */
266 return VLC_SUCCESS;
268 else if (FAILED(hr))
269 goto error;
271 GUID subtype;
272 hr = IMFMediaType_GetGUID(input_media_type, &MF_MT_SUBTYPE, &subtype);
273 if (FAILED(hr))
274 goto error;
276 if (IsEqualGUID(&subtype, p_sys->subtype))
277 found = true;
279 if (found)
280 input_type_index = i;
282 IMFMediaType_Release(input_media_type);
283 input_media_type = NULL;
285 if (!found)
286 goto error;
288 hr = IMFTransform_GetInputAvailableType(p_sys->mft, stream_id, input_type_index, &input_media_type);
289 if (FAILED(hr))
290 goto error;
292 if (p_dec->fmt_in.i_cat == VIDEO_ES)
294 UINT64 width = p_dec->fmt_in.video.i_width;
295 UINT64 height = p_dec->fmt_in.video.i_height;
296 UINT64 frame_size = (width << 32) | height;
297 hr = IMFMediaType_SetUINT64(input_media_type, &MF_MT_FRAME_SIZE, frame_size);
298 if (FAILED(hr))
299 goto error;
301 /* Some transforms like to know the frame rate and may reject the input type otherwise. */
302 UINT64 frame_ratio_num = p_dec->fmt_in.video.i_frame_rate;
303 UINT64 frame_ratio_dem = p_dec->fmt_in.video.i_frame_rate_base;
304 if(frame_ratio_num && frame_ratio_dem) {
305 UINT64 frame_rate = (frame_ratio_num << 32) | frame_ratio_dem;
306 hr = IMFMediaType_SetUINT64(input_media_type, &MF_MT_FRAME_RATE, frame_rate);
307 if(FAILED(hr))
308 goto error;
311 else
313 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_ORIGINAL_WAVE_FORMAT_TAG, p_sys->subtype->Data1);
314 if (FAILED(hr))
315 goto error;
316 if (p_dec->fmt_in.audio.i_rate)
318 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, p_dec->fmt_in.audio.i_rate);
319 if (FAILED(hr))
320 goto error;
322 if (p_dec->fmt_in.audio.i_channels)
324 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_NUM_CHANNELS, p_dec->fmt_in.audio.i_channels);
325 if (FAILED(hr))
326 goto error;
328 if (p_dec->fmt_in.audio.i_bitspersample)
330 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_BITS_PER_SAMPLE, p_dec->fmt_in.audio.i_bitspersample);
331 if (FAILED(hr))
332 goto error;
334 if (p_dec->fmt_in.audio.i_blockalign)
336 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_BLOCK_ALIGNMENT, p_dec->fmt_in.audio.i_blockalign);
337 if (FAILED(hr))
338 goto error;
340 if (p_dec->fmt_in.i_bitrate)
342 hr = IMFMediaType_SetUINT32(input_media_type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, p_dec->fmt_in.i_bitrate / 8);
343 if (FAILED(hr))
344 goto error;
348 if (p_dec->fmt_in.i_extra > 0)
350 UINT32 blob_size = 0;
351 hr = IMFMediaType_GetBlobSize(input_media_type, &MF_MT_USER_DATA, &blob_size);
353 * Do not overwrite existing user data in the input type, this
354 * can cause the MFT to reject the type.
356 if (hr == MF_E_ATTRIBUTENOTFOUND)
358 hr = IMFMediaType_SetBlob(input_media_type, &MF_MT_USER_DATA,
359 (const UINT8*)p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra);
360 if (FAILED(hr))
361 goto error;
365 hr = IMFTransform_SetInputType(p_sys->mft, stream_id, input_media_type, 0);
366 if (FAILED(hr))
367 goto error;
369 *result = input_media_type;
371 return VLC_SUCCESS;
373 error:
374 msg_Err(p_dec, "Error in SetInputType()");
375 if (input_media_type)
376 IMFMediaType_Release(input_media_type);
377 return VLC_EGENERIC;
380 static int SetOutputType(decoder_t *p_dec, DWORD stream_id, IMFMediaType **result)
382 decoder_sys_t *p_sys = p_dec->p_sys;
383 HRESULT hr;
385 *result = NULL;
387 IMFMediaType *output_media_type = NULL;
390 * Enumerate available output types. The list is ordered by
391 * preference thus we will use the first one unless YV12/I420 is
392 * available for video or float32 for audio.
394 int output_type_index = -1;
395 bool found = false;
396 for (int i = 0; !found; ++i)
398 hr = IMFTransform_GetOutputAvailableType(p_sys->mft, stream_id, i, &output_media_type);
399 if (hr == MF_E_NO_MORE_TYPES)
400 break;
401 else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET)
403 /* The input type must be set before setting the output type for this MFT. */
404 return VLC_SUCCESS;
406 else if (FAILED(hr))
407 goto error;
409 GUID subtype;
410 hr = IMFMediaType_GetGUID(output_media_type, &MF_MT_SUBTYPE, &subtype);
411 if (FAILED(hr))
412 goto error;
414 if (p_dec->fmt_in.i_cat == VIDEO_ES)
416 if (IsEqualGUID(&subtype, &MFVideoFormat_YV12) || IsEqualGUID(&subtype, &MFVideoFormat_I420))
417 found = true;
418 /* Transform might offer output in a D3DFMT propietary FCC. If we can
419 * use it, fall back to it in case we do not find YV12 or I420 */
420 else if(output_type_index < 0 && GUIDToFormat(d3d_format_table, &subtype) > 0)
421 output_type_index = i;
423 else
425 UINT32 bits_per_sample;
426 hr = IMFMediaType_GetUINT32(output_media_type, &MF_MT_AUDIO_BITS_PER_SAMPLE, &bits_per_sample);
427 if (FAILED(hr))
428 continue;
429 if (bits_per_sample == 32 && IsEqualGUID(&subtype, &MFAudioFormat_Float))
430 found = true;
433 if (found)
434 output_type_index = i;
436 IMFMediaType_Release(output_media_type);
437 output_media_type = NULL;
440 * It's not an error if we don't find the output type we were
441 * looking for, in this case we use the first available type.
443 if(output_type_index < 0)
444 /* No output format found we prefer, just pick the first one preferred
445 * by the MFT */
446 output_type_index = 0;
448 hr = IMFTransform_GetOutputAvailableType(p_sys->mft, stream_id, output_type_index, &output_media_type);
449 if (FAILED(hr))
450 goto error;
452 hr = IMFTransform_SetOutputType(p_sys->mft, stream_id, output_media_type, 0);
453 if (FAILED(hr))
454 goto error;
456 GUID subtype;
457 hr = IMFMediaType_GetGUID(output_media_type, &MF_MT_SUBTYPE, &subtype);
458 if (FAILED(hr))
459 goto error;
461 if (p_dec->fmt_in.i_cat == VIDEO_ES)
463 video_format_Copy( &p_dec->fmt_out.video, &p_dec->fmt_in.video );
465 /* Transform might offer output in a D3DFMT propietary FCC */
466 vlc_fourcc_t fcc = GUIDToFormat(d3d_format_table, &subtype);
467 if(fcc) {
468 /* D3D formats are upside down */
469 p_dec->fmt_out.video.orientation = ORIENT_BOTTOM_LEFT;
470 } else {
471 fcc = vlc_fourcc_GetCodec(p_dec->fmt_in.i_cat, subtype.Data1);
474 p_dec->fmt_out.i_codec = fcc;
476 else
478 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
480 UINT32 bitspersample = 0;
481 hr = IMFMediaType_GetUINT32(output_media_type, &MF_MT_AUDIO_BITS_PER_SAMPLE, &bitspersample);
482 if (SUCCEEDED(hr) && bitspersample)
483 p_dec->fmt_out.audio.i_bitspersample = bitspersample;
485 UINT32 channels = 0;
486 hr = IMFMediaType_GetUINT32(output_media_type, &MF_MT_AUDIO_NUM_CHANNELS, &channels);
487 if (SUCCEEDED(hr) && channels)
488 p_dec->fmt_out.audio.i_channels = channels;
490 UINT32 rate = 0;
491 hr = IMFMediaType_GetUINT32(output_media_type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &rate);
492 if (SUCCEEDED(hr) && rate)
493 p_dec->fmt_out.audio.i_rate = rate;
495 vlc_fourcc_t fourcc;
496 wf_tag_to_fourcc(subtype.Data1, &fourcc, NULL);
497 p_dec->fmt_out.i_codec = vlc_fourcc_GetCodecAudio(fourcc, p_dec->fmt_out.audio.i_bitspersample);
499 p_dec->fmt_out.audio.i_physical_channels = pi_channels_maps[p_dec->fmt_out.audio.i_channels];
502 *result = output_media_type;
504 return VLC_SUCCESS;
506 error:
507 msg_Err(p_dec, "Error in SetOutputType()");
508 if (output_media_type)
509 IMFMediaType_Release(output_media_type);
510 return VLC_EGENERIC;
513 static int AllocateInputSample(decoder_t *p_dec, DWORD stream_id, IMFSample** result, DWORD size)
515 decoder_sys_t *p_sys = p_dec->p_sys;
516 MFHandle *mf = &p_sys->mf_handle;
517 HRESULT hr;
519 *result = NULL;
521 IMFSample *input_sample = NULL;
523 MFT_INPUT_STREAM_INFO input_info;
524 hr = IMFTransform_GetInputStreamInfo(p_sys->mft, stream_id, &input_info);
525 if (FAILED(hr))
526 goto error;
528 hr = mf->fptr_MFCreateSample(&input_sample);
529 if (FAILED(hr))
530 goto error;
532 IMFMediaBuffer *input_media_buffer = NULL;
533 DWORD allocation_size = __MAX(input_info.cbSize, size);
534 hr = mf->fptr_MFCreateMemoryBuffer(allocation_size, &input_media_buffer);
535 if (FAILED(hr))
536 goto error;
538 hr = IMFSample_AddBuffer(input_sample, input_media_buffer);
539 IMFMediaBuffer_Release(input_media_buffer);
540 if (FAILED(hr))
541 goto error;
543 *result = input_sample;
545 return VLC_SUCCESS;
547 error:
548 msg_Err(p_dec, "Error in AllocateInputSample()");
549 if (input_sample)
550 IMFSample_Release(input_sample);
551 if (input_media_buffer)
552 IMFMediaBuffer_Release(input_media_buffer);
553 return VLC_EGENERIC;
556 static int AllocateOutputSample(decoder_t *p_dec, DWORD stream_id, IMFSample **result)
558 decoder_sys_t *p_sys = p_dec->p_sys;
559 MFHandle *mf = &p_sys->mf_handle;
560 HRESULT hr;
562 *result = NULL;
564 IMFSample *output_sample = NULL;
566 MFT_OUTPUT_STREAM_INFO output_info;
567 hr = IMFTransform_GetOutputStreamInfo(p_sys->mft, stream_id, &output_info);
568 if (FAILED(hr))
569 goto error;
571 if (output_info.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES | MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES))
573 /* The MFT will provide an allocated sample. */
574 return VLC_SUCCESS;
577 DWORD expected_flags = 0;
578 if (p_dec->fmt_in.i_cat == VIDEO_ES)
579 expected_flags |= MFT_OUTPUT_STREAM_WHOLE_SAMPLES
580 | MFT_OUTPUT_STREAM_SINGLE_SAMPLE_PER_BUFFER
581 | MFT_OUTPUT_STREAM_FIXED_SAMPLE_SIZE;
582 if ((output_info.dwFlags & expected_flags) != expected_flags)
583 goto error;
585 hr = mf->fptr_MFCreateSample(&output_sample);
586 if (FAILED(hr))
587 goto error;
589 IMFMediaBuffer *output_media_buffer = NULL;
590 DWORD allocation_size = output_info.cbSize;
591 DWORD alignment = output_info.cbAlignment;
592 if (alignment > 0)
593 hr = mf->fptr_MFCreateAlignedMemoryBuffer(allocation_size, alignment - 1, &output_media_buffer);
594 else
595 hr = mf->fptr_MFCreateMemoryBuffer(allocation_size, &output_media_buffer);
596 if (FAILED(hr))
597 goto error;
599 hr = IMFSample_AddBuffer(output_sample, output_media_buffer);
600 if (FAILED(hr))
601 goto error;
603 *result = output_sample;
605 return VLC_SUCCESS;
607 error:
608 msg_Err(p_dec, "Error in AllocateOutputSample()");
609 if (output_sample)
610 IMFSample_Release(output_sample);
611 return VLC_EGENERIC;
614 static int ProcessInputStream(decoder_t *p_dec, DWORD stream_id, block_t *p_block)
616 decoder_sys_t *p_sys = p_dec->p_sys;
617 HRESULT hr;
618 IMFSample *input_sample = NULL;
620 if (AllocateInputSample(p_dec, stream_id, &input_sample, p_block->i_buffer))
621 goto error;
623 IMFMediaBuffer *input_media_buffer = NULL;
624 hr = IMFSample_GetBufferByIndex(input_sample, 0, &input_media_buffer);
625 if (FAILED(hr))
626 goto error;
628 BYTE *buffer_start;
629 hr = IMFMediaBuffer_Lock(input_media_buffer, &buffer_start, NULL, NULL);
630 if (FAILED(hr))
631 goto error;
633 memcpy(buffer_start, p_block->p_buffer, p_block->i_buffer);
635 if (p_dec->fmt_in.i_codec == VLC_CODEC_H264)
637 /* in-place NAL to annex B conversion. */
638 h264_AVC_to_AnnexB(buffer_start, p_block->i_buffer, p_sys->nal_length_size);
641 hr = IMFMediaBuffer_Unlock(input_media_buffer);
642 if (FAILED(hr))
643 goto error;
645 hr = IMFMediaBuffer_SetCurrentLength(input_media_buffer, p_block->i_buffer);
646 if (FAILED(hr))
647 goto error;
649 LONGLONG ts = p_block->i_pts;
650 if (!ts && p_block->i_dts)
651 ts = p_block->i_dts;
653 /* Convert from microseconds to 100 nanoseconds unit. */
654 hr = IMFSample_SetSampleTime(input_sample, ts * 10);
655 if (FAILED(hr))
656 goto error;
658 hr = IMFTransform_ProcessInput(p_sys->mft, stream_id, input_sample, 0);
659 if (FAILED(hr))
660 goto error;
662 IMFMediaBuffer_Release(input_media_buffer);
663 IMFSample_Release(input_sample);
665 return VLC_SUCCESS;
667 error:
668 msg_Err(p_dec, "Error in ProcessInputStream()");
669 if (input_sample)
670 IMFSample_Release(input_sample);
671 return VLC_EGENERIC;
674 /* Copy a packed buffer (no padding) to a picture_t */
675 static void CopyPackedBufferToPicture(picture_t *p_pic, const uint8_t *p_src)
677 for (int i = 0; i < p_pic->i_planes; ++i)
679 uint8_t *p_dst = p_pic->p[i].p_pixels;
681 if (p_pic->p[i].i_visible_pitch == p_pic->p[i].i_pitch)
683 /* Plane is packed, only one memcpy is needed. */
684 uint32_t plane_size = p_pic->p[i].i_pitch * p_pic->p[i].i_visible_lines;
685 memcpy(p_dst, p_src, plane_size);
686 p_src += plane_size;
687 continue;
690 for (int i_line = 0; i_line < p_pic->p[i].i_visible_lines; i_line++)
692 memcpy(p_dst, p_src, p_pic->p[i].i_visible_pitch);
693 p_src += p_pic->p[i].i_visible_pitch;
694 p_dst += p_pic->p[i].i_pitch;
699 static int ProcessOutputStream(decoder_t *p_dec, DWORD stream_id)
701 decoder_sys_t *p_sys = p_dec->p_sys;
702 HRESULT hr;
703 picture_t *picture = NULL;
704 block_t *aout_buffer = NULL;
706 DWORD output_status = 0;
707 MFT_OUTPUT_DATA_BUFFER output_buffer = { stream_id, p_sys->output_sample, 0, NULL };
708 hr = IMFTransform_ProcessOutput(p_sys->mft, 0, 1, &output_buffer, &output_status);
709 if (output_buffer.pEvents)
710 IMFCollection_Release(output_buffer.pEvents);
711 /* Use the returned sample since it can be provided by the MFT. */
712 IMFSample *output_sample = output_buffer.pSample;
714 if (hr == S_OK)
716 if (!output_sample)
717 return VLC_SUCCESS;
719 LONGLONG sample_time;
720 hr = IMFSample_GetSampleTime(output_sample, &sample_time);
721 if (FAILED(hr))
722 goto error;
723 /* Convert from 100 nanoseconds unit to microseconds. */
724 sample_time /= 10;
726 DWORD total_length = 0;
727 hr = IMFSample_GetTotalLength(output_sample, &total_length);
728 if (FAILED(hr))
729 goto error;
731 if (p_dec->fmt_in.i_cat == VIDEO_ES)
733 if (decoder_UpdateVideoFormat(p_dec))
734 return VLC_SUCCESS;
735 picture = decoder_NewPicture(p_dec);
736 if (!picture)
737 return VLC_SUCCESS;
739 UINT32 interlaced = false;
740 hr = IMFSample_GetUINT32(output_sample, &MFSampleExtension_Interlaced, &interlaced);
741 picture->b_progressive = !interlaced;
743 picture->date = sample_time;
745 else
747 if (decoder_UpdateAudioFormat(p_dec))
748 goto error;
749 if (p_dec->fmt_out.audio.i_bitspersample == 0 || p_dec->fmt_out.audio.i_channels == 0)
750 goto error;
751 int samples = total_length / (p_dec->fmt_out.audio.i_bitspersample * p_dec->fmt_out.audio.i_channels / 8);
752 aout_buffer = decoder_NewAudioBuffer(p_dec, samples);
753 if (!aout_buffer)
754 return VLC_SUCCESS;
755 if (aout_buffer->i_buffer < total_length)
756 goto error;
758 aout_buffer->i_pts = sample_time;
761 IMFMediaBuffer *output_media_buffer = NULL;
762 hr = IMFSample_GetBufferByIndex(output_sample, 0, &output_media_buffer);
764 BYTE *buffer_start;
765 hr = IMFMediaBuffer_Lock(output_media_buffer, &buffer_start, NULL, NULL);
766 if (FAILED(hr))
767 goto error;
769 if (p_dec->fmt_in.i_cat == VIDEO_ES)
770 CopyPackedBufferToPicture(picture, buffer_start);
771 else
772 memcpy(aout_buffer->p_buffer, buffer_start, total_length);
774 hr = IMFMediaBuffer_Unlock(output_media_buffer);
775 IMFSample_Release(output_media_buffer);
776 if (FAILED(hr))
777 goto error;
779 if (p_sys->output_sample)
781 /* Sample is not provided by the MFT: clear its content. */
782 hr = IMFMediaBuffer_SetCurrentLength(output_media_buffer, 0);
783 if (FAILED(hr))
784 goto error;
786 else
788 /* Sample is provided by the MFT: decrease refcount. */
789 IMFSample_Release(output_sample);
792 else if (hr == MF_E_TRANSFORM_STREAM_CHANGE || hr == MF_E_TRANSFORM_TYPE_NOT_SET)
794 if (p_sys->output_type)
795 IMFMediaType_Release(p_sys->output_type);
796 if (SetOutputType(p_dec, p_sys->output_stream_id, &p_sys->output_type))
797 goto error;
799 /* Reallocate output sample. */
800 if (p_sys->output_sample)
801 IMFSample_Release(p_sys->output_sample);
802 p_sys->output_sample = NULL;
803 if (AllocateOutputSample(p_dec, 0, &p_sys->output_sample))
804 goto error;
805 return VLC_SUCCESS;
807 else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT)
809 return VLC_SUCCESS;
811 else /* An error not listed above occurred */
813 msg_Err(p_dec, "Unexpected error in IMFTransform::ProcessOutput: %#lx",
814 hr);
815 goto error;
818 if (p_dec->fmt_in.i_cat == VIDEO_ES)
819 decoder_QueueVideo(p_dec, picture);
820 else
821 decoder_QueueAudio(p_dec, aout_buffer);
823 return VLC_SUCCESS;
825 error:
826 msg_Err(p_dec, "Error in ProcessOutputStream()");
827 if (picture)
828 picture_Release(picture);
829 if (aout_buffer)
830 block_Release(aout_buffer);
831 return VLC_EGENERIC;
834 static int DecodeSync(decoder_t *p_dec, block_t *p_block)
836 decoder_sys_t *p_sys = p_dec->p_sys;
838 if (!p_block) /* No Drain */
839 return VLCDEC_SUCCESS;
841 if (p_block->i_flags & (BLOCK_FLAG_CORRUPTED))
843 block_Release(p_block);
844 return VLCDEC_SUCCESS;
847 /* Drain the output stream before sending the input packet. */
848 if (ProcessOutputStream(p_dec, p_sys->output_stream_id))
849 goto error;
850 if (ProcessInputStream(p_dec, p_sys->input_stream_id, p_block))
851 goto error;
852 block_Release(p_block);
854 return VLCDEC_SUCCESS;
856 error:
857 msg_Err(p_dec, "Error in DecodeSync()");
858 block_Release(p_block);
859 return VLCDEC_SUCCESS;
862 static HRESULT DequeueMediaEvent(decoder_t *p_dec)
864 decoder_sys_t *p_sys = p_dec->p_sys;
865 HRESULT hr;
867 IMFMediaEvent *event = NULL;
868 hr = IMFMediaEventGenerator_GetEvent(p_sys->event_generator, MF_EVENT_FLAG_NO_WAIT, &event);
869 if (FAILED(hr))
870 return hr;
871 MediaEventType event_type;
872 hr = IMFMediaEvent_GetType(event, &event_type);
873 IMFMediaEvent_Release(event);
874 if (FAILED(hr))
875 return hr;
877 if (event_type == VLC_METransformNeedInput)
878 p_sys->pending_input_events += 1;
879 else if (event_type == VLC_METransformHaveOutput)
880 p_sys->pending_output_events += 1;
881 else
882 msg_Err(p_dec, "Unsupported asynchronous event.");
884 return S_OK;
887 static int DecodeAsync(decoder_t *p_dec, block_t *p_block)
889 decoder_sys_t *p_sys = p_dec->p_sys;
890 HRESULT hr;
892 if (!p_block) /* No Drain */
893 return VLCDEC_SUCCESS;
895 if (p_block->i_flags & (BLOCK_FLAG_CORRUPTED))
897 block_Release(p_block);
898 return VLCDEC_SUCCESS;
901 /* Dequeue all pending media events. */
902 while ((hr = DequeueMediaEvent(p_dec)) == S_OK)
903 continue;
904 if (hr != MF_E_NO_EVENTS_AVAILABLE && FAILED(hr))
905 goto error;
907 /* Drain the output stream of the MFT before sending the input packet. */
908 if (p_sys->pending_output_events > 0)
910 p_sys->pending_output_events -= 1;
911 if (ProcessOutputStream(p_dec, p_sys->output_stream_id))
912 goto error;
915 /* Poll the MFT and return decoded frames until the input stream is ready. */
916 while (p_sys->pending_input_events == 0)
918 hr = DequeueMediaEvent(p_dec);
919 if (hr == MF_E_NO_EVENTS_AVAILABLE)
921 /* Sleep for 1 ms to avoid excessive polling. */
922 Sleep(1);
923 continue;
925 if (FAILED(hr))
926 goto error;
928 if (p_sys->pending_output_events > 0)
930 p_sys->pending_output_events -= 1;
931 if (ProcessOutputStream(p_dec, p_sys->output_stream_id))
932 goto error;
933 break;
937 p_sys->pending_input_events -= 1;
938 if (ProcessInputStream(p_dec, p_sys->input_stream_id, p_block))
939 goto error;
941 block_Release(p_block);
943 return VLCDEC_SUCCESS;
945 error:
946 msg_Err(p_dec, "Error in DecodeAsync()");
947 block_Release(p_block);
948 return VLCDEC_SUCCESS;
951 static void DestroyMFT(decoder_t *p_dec);
953 static int InitializeMFT(decoder_t *p_dec)
955 decoder_sys_t *p_sys = p_dec->p_sys;
956 HRESULT hr;
958 IMFAttributes *attributes = NULL;
959 hr = IMFTransform_GetAttributes(p_sys->mft, &attributes);
960 if (hr != E_NOTIMPL && FAILED(hr))
961 goto error;
962 if (SUCCEEDED(hr))
964 UINT32 is_async = false;
965 hr = IMFAttributes_GetUINT32(attributes, &MF_TRANSFORM_ASYNC, &is_async);
966 if (hr != MF_E_ATTRIBUTENOTFOUND && FAILED(hr))
967 goto error;
968 p_sys->is_async = is_async;
969 if (p_sys->is_async)
971 hr = IMFAttributes_SetUINT32(attributes, &MF_TRANSFORM_ASYNC_UNLOCK, true);
972 if (FAILED(hr))
973 goto error;
974 hr = IMFTransform_QueryInterface(p_sys->mft, &IID_IMFMediaEventGenerator, (void**)&p_sys->event_generator);
975 if (FAILED(hr))
976 goto error;
980 DWORD input_streams_count;
981 DWORD output_streams_count;
982 hr = IMFTransform_GetStreamCount(p_sys->mft, &input_streams_count, &output_streams_count);
983 if (FAILED(hr))
984 goto error;
985 if (input_streams_count != 1 || output_streams_count != 1)
987 msg_Err(p_dec, "MFT decoder should have 1 input stream and 1 output stream.");
988 goto error;
991 hr = IMFTransform_GetStreamIDs(p_sys->mft, 1, &p_sys->input_stream_id, 1, &p_sys->output_stream_id);
992 if (hr == E_NOTIMPL)
995 * This is not an error, it happens if:
996 * - there is a fixed number of streams.
997 * AND
998 * - streams are numbered consecutively from 0 to N-1.
1000 p_sys->input_stream_id = 0;
1001 p_sys->output_stream_id = 0;
1003 else if (FAILED(hr))
1004 goto error;
1006 if (SetInputType(p_dec, p_sys->input_stream_id, &p_sys->input_type))
1007 goto error;
1009 if (SetOutputType(p_dec, p_sys->output_stream_id, &p_sys->output_type))
1010 goto error;
1013 * The input type was not set by the previous call to
1014 * SetInputType, try again after setting the output type.
1016 if (!p_sys->input_type)
1017 if (SetInputType(p_dec, p_sys->input_stream_id, &p_sys->input_type) || !p_sys->input_type)
1018 goto error;
1020 /* This call can be a no-op for some MFT decoders, but it can potentially reduce starting time. */
1021 hr = IMFTransform_ProcessMessage(p_sys->mft, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, (ULONG_PTR)0);
1022 if (FAILED(hr))
1023 goto error;
1025 /* This event is required for asynchronous MFTs, optional otherwise. */
1026 hr = IMFTransform_ProcessMessage(p_sys->mft, MFT_MESSAGE_NOTIFY_START_OF_STREAM, (ULONG_PTR)0);
1027 if (FAILED(hr))
1028 goto error;
1030 if (p_dec->fmt_in.i_codec == VLC_CODEC_H264)
1032 /* It's not an error if the following call fails. */
1033 IMFAttributes_SetUINT32(attributes, &CODECAPI_AVLowLatencyMode, true);
1035 if (p_dec->fmt_in.i_extra)
1037 if (h264_isavcC((uint8_t*)p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra))
1039 size_t i_buf;
1040 uint8_t *buf = h264_avcC_to_AnnexB_NAL(p_dec->fmt_in.p_extra,
1041 p_dec->fmt_in.i_extra,
1042 &i_buf, &p_sys->nal_length_size);
1043 if(buf)
1045 free(p_dec->fmt_in.p_extra);
1046 p_dec->fmt_in.p_extra = buf;
1047 p_dec->fmt_in.i_extra = i_buf;
1052 return VLC_SUCCESS;
1054 error:
1055 msg_Err(p_dec, "Error in InitializeMFT()");
1056 DestroyMFT(p_dec);
1057 return VLC_EGENERIC;
1060 static void DestroyMFT(decoder_t *p_dec)
1062 decoder_sys_t *p_sys = p_dec->p_sys;
1064 if (p_sys->event_generator)
1065 IMFMediaEventGenerator_Release(p_sys->event_generator);
1066 if (p_sys->input_type)
1067 IMFMediaType_Release(p_sys->input_type);
1068 if (p_sys->output_sample)
1070 IMFMediaBuffer *output_media_buffer = NULL;
1071 HRESULT hr = IMFSample_GetBufferByIndex(p_sys->output_sample, 0, &output_media_buffer);
1072 if (SUCCEEDED(hr))
1073 IMFSample_Release(output_media_buffer);
1074 IMFSample_Release(p_sys->output_sample);
1076 if (p_sys->output_type)
1077 IMFMediaType_Release(p_sys->output_type);
1078 if (p_sys->mft)
1079 IMFTransform_Release(p_sys->mft);
1081 p_sys->event_generator = NULL;
1082 p_sys->input_type = NULL;
1083 p_sys->output_sample = NULL;
1084 p_sys->output_type = NULL;
1085 p_sys->mft = NULL;
1088 static int FindMFT(decoder_t *p_dec)
1090 decoder_sys_t *p_sys = p_dec->p_sys;
1091 MFHandle *mf = &p_sys->mf_handle;
1092 HRESULT hr;
1094 /* Try to create a MFT using MFTEnumEx. */
1095 GUID category;
1096 if (p_dec->fmt_in.i_cat == VIDEO_ES)
1098 category = MFT_CATEGORY_VIDEO_DECODER;
1099 p_sys->major_type = &MFMediaType_Video;
1100 p_sys->subtype = FormatToGUID(video_format_table, p_dec->fmt_in.i_codec);
1101 if(!p_sys->subtype) {
1102 /* Codec is not well known. Construct a MF transform subtype from the fourcc */
1103 p_sys->custom_subtype = MFVideoFormat_Base;
1104 p_sys->custom_subtype.Data1 = p_dec->fmt_in.i_codec;
1105 p_sys->subtype = &p_sys->custom_subtype;
1108 else
1110 category = MFT_CATEGORY_AUDIO_DECODER;
1111 p_sys->major_type = &MFMediaType_Audio;
1112 p_sys->subtype = FormatToGUID(audio_format_table, p_dec->fmt_in.i_codec);
1114 if (!p_sys->subtype)
1115 return VLC_EGENERIC;
1117 UINT32 flags = MFT_ENUM_FLAG_SORTANDFILTER | MFT_ENUM_FLAG_LOCALMFT
1118 | MFT_ENUM_FLAG_SYNCMFT | MFT_ENUM_FLAG_ASYNCMFT
1119 | MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_TRANSCODE_ONLY;
1120 MFT_REGISTER_TYPE_INFO input_type = { *p_sys->major_type, *p_sys->subtype };
1121 IMFActivate **activate_objects = NULL;
1122 UINT32 activate_objects_count = 0;
1123 hr = mf->fptr_MFTEnumEx(category, flags, &input_type, NULL, &activate_objects, &activate_objects_count);
1124 if (FAILED(hr))
1125 return VLC_EGENERIC;
1127 msg_Dbg(p_dec, "Found %d available MFT module(s)", activate_objects_count);
1128 if (activate_objects_count == 0)
1129 return VLC_EGENERIC;
1131 for (UINT32 i = 0; i < activate_objects_count; ++i)
1133 hr = IMFActivate_ActivateObject(activate_objects[i], &IID_IMFTransform, (void**)&p_sys->mft);
1134 IMFActivate_Release(activate_objects[i]);
1135 if (FAILED(hr))
1136 continue;
1138 if (InitializeMFT(p_dec) == VLC_SUCCESS)
1140 CoTaskMemFree(activate_objects);
1141 return VLC_SUCCESS;
1144 CoTaskMemFree(activate_objects);
1146 return VLC_EGENERIC;
1149 static int LoadMFTLibrary(MFHandle *mf)
1151 #if _WIN32_WINNT < _WIN32_WINNT_WIN7 || VLC_WINSTORE_APP || __MINGW64_VERSION_MAJOR < 6
1152 mf->mfplat_dll = LoadLibrary(TEXT("mfplat.dll"));
1153 if (!mf->mfplat_dll)
1154 return VLC_EGENERIC;
1156 mf->fptr_MFTEnumEx = (void*)GetProcAddress(mf->mfplat_dll, "MFTEnumEx");
1157 mf->fptr_MFCreateSample = (void*)GetProcAddress(mf->mfplat_dll, "MFCreateSample");
1158 mf->fptr_MFCreateMemoryBuffer = (void*)GetProcAddress(mf->mfplat_dll, "MFCreateMemoryBuffer");
1159 mf->fptr_MFCreateAlignedMemoryBuffer = (void*)GetProcAddress(mf->mfplat_dll, "MFCreateAlignedMemoryBuffer");
1160 if (!mf->fptr_MFTEnumEx || !mf->fptr_MFCreateSample || !mf->fptr_MFCreateMemoryBuffer || !mf->fptr_MFCreateAlignedMemoryBuffer)
1161 return VLC_EGENERIC;
1162 #else
1163 mf->fptr_MFTEnumEx = &MFTEnumEx;
1164 mf->fptr_MFCreateSample = &MFCreateSample;
1165 mf->fptr_MFCreateMemoryBuffer = &MFCreateMemoryBuffer;
1166 mf->fptr_MFCreateAlignedMemoryBuffer = &MFCreateAlignedMemoryBuffer;
1167 #endif
1169 return VLC_SUCCESS;
1172 static int Open(vlc_object_t *p_this)
1174 decoder_t *p_dec = (decoder_t *)p_this;
1175 decoder_sys_t *p_sys;
1177 p_sys = p_dec->p_sys = calloc(1, sizeof(*p_sys));
1178 if (!p_sys)
1179 return VLC_ENOMEM;
1181 if( FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)) )
1183 free(p_sys);
1184 return VLC_EGENERIC;
1187 if (LoadMFTLibrary(&p_sys->mf_handle))
1189 msg_Err(p_dec, "Failed to load MFT library.");
1190 goto error;
1193 if (FindMFT(p_dec))
1195 msg_Err(p_dec, "Could not find suitable MFT decoder");
1196 goto error;
1199 /* Only one output sample is needed, we can allocate one and reuse it. */
1200 if (AllocateOutputSample(p_dec, 0, &p_sys->output_sample))
1201 goto error;
1203 p_dec->pf_decode = p_sys->is_async ? DecodeAsync : DecodeSync;
1205 return VLC_SUCCESS;
1207 error:
1208 Close(p_this);
1209 return VLC_EGENERIC;
1212 static void Close(vlc_object_t *p_this)
1214 decoder_t *p_dec = (decoder_t *)p_this;
1215 decoder_sys_t *p_sys = p_dec->p_sys;
1216 MFHandle *mf = &p_sys->mf_handle;
1218 DestroyMFT(p_dec);
1220 if (mf->mfplat_dll)
1221 FreeLibrary(mf->mfplat_dll);
1223 free(p_sys);
1225 CoUninitialize();