kernelbase: Reimplement LocaleNameToLCID() using the locale.nls data.
[wine.git] / dlls / winegstreamer / mfplat.c
blobed460144e8b55dce996a2502a2de07b16f8cc54b
1 /*
2 * Copyright 2019 Nikolay Sivov for CodeWeavers
3 * Copyright 2020 Zebediah Figura for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "gst_private.h"
22 #include "ks.h"
23 #include "ksmedia.h"
24 #include "wmcodecdsp.h"
25 #include "initguid.h"
26 #include "mfapi.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(mfplat);
32 struct video_processor
34 IMFTransform IMFTransform_iface;
35 LONG refcount;
36 IMFAttributes *attributes;
37 IMFAttributes *output_attributes;
40 static struct video_processor *impl_video_processor_from_IMFTransform(IMFTransform *iface)
42 return CONTAINING_RECORD(iface, struct video_processor, IMFTransform_iface);
45 static HRESULT WINAPI video_processor_QueryInterface(IMFTransform *iface, REFIID riid, void **obj)
47 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);
49 if (IsEqualIID(riid, &IID_IMFTransform) ||
50 IsEqualIID(riid, &IID_IUnknown))
52 *obj = iface;
53 IMFTransform_AddRef(iface);
54 return S_OK;
57 WARN("Unsupported %s.\n", debugstr_guid(riid));
58 *obj = NULL;
59 return E_NOINTERFACE;
62 static ULONG WINAPI video_processor_AddRef(IMFTransform *iface)
64 struct video_processor *transform = impl_video_processor_from_IMFTransform(iface);
65 ULONG refcount = InterlockedIncrement(&transform->refcount);
67 TRACE("%p, refcount %lu.\n", iface, refcount);
69 return refcount;
72 static ULONG WINAPI video_processor_Release(IMFTransform *iface)
74 struct video_processor *transform = impl_video_processor_from_IMFTransform(iface);
75 ULONG refcount = InterlockedDecrement(&transform->refcount);
77 TRACE("%p, refcount %lu.\n", iface, refcount);
79 if (!refcount)
81 if (transform->attributes)
82 IMFAttributes_Release(transform->attributes);
83 if (transform->output_attributes)
84 IMFAttributes_Release(transform->output_attributes);
85 free(transform);
88 return refcount;
91 static HRESULT WINAPI video_processor_GetStreamLimits(IMFTransform *iface, DWORD *input_minimum, DWORD *input_maximum,
92 DWORD *output_minimum, DWORD *output_maximum)
94 TRACE("%p, %p, %p, %p, %p.\n", iface, input_minimum, input_maximum, output_minimum, output_maximum);
96 *input_minimum = *input_maximum = *output_minimum = *output_maximum = 1;
98 return S_OK;
101 static HRESULT WINAPI video_processor_GetStreamCount(IMFTransform *iface, DWORD *inputs, DWORD *outputs)
103 TRACE("%p, %p, %p.\n", iface, inputs, outputs);
105 *inputs = *outputs = 1;
107 return S_OK;
110 static HRESULT WINAPI video_processor_GetStreamIDs(IMFTransform *iface, DWORD input_size, DWORD *inputs,
111 DWORD output_size, DWORD *outputs)
113 return E_NOTIMPL;
116 static HRESULT WINAPI video_processor_GetInputStreamInfo(IMFTransform *iface, DWORD id, MFT_INPUT_STREAM_INFO *info)
118 return E_NOTIMPL;
121 static HRESULT WINAPI video_processor_GetOutputStreamInfo(IMFTransform *iface, DWORD id, MFT_OUTPUT_STREAM_INFO *info)
123 return E_NOTIMPL;
126 static HRESULT WINAPI video_processor_GetAttributes(IMFTransform *iface, IMFAttributes **attributes)
128 struct video_processor *transform = impl_video_processor_from_IMFTransform(iface);
130 TRACE("%p, %p.\n", iface, attributes);
132 *attributes = transform->attributes;
133 IMFAttributes_AddRef(*attributes);
135 return S_OK;
138 static HRESULT WINAPI video_processor_GetInputStreamAttributes(IMFTransform *iface, DWORD id,
139 IMFAttributes **attributes)
141 return E_NOTIMPL;
144 static HRESULT WINAPI video_processor_GetOutputStreamAttributes(IMFTransform *iface, DWORD id,
145 IMFAttributes **attributes)
147 struct video_processor *transform = impl_video_processor_from_IMFTransform(iface);
149 TRACE("%p, %lu, %p.\n", iface, id, attributes);
151 *attributes = transform->output_attributes;
152 IMFAttributes_AddRef(*attributes);
154 return S_OK;
157 static HRESULT WINAPI video_processor_DeleteInputStream(IMFTransform *iface, DWORD id)
159 TRACE("%p, %lu.\n", iface, id);
161 return E_NOTIMPL;
164 static HRESULT WINAPI video_processor_AddInputStreams(IMFTransform *iface, DWORD streams, DWORD *ids)
166 TRACE("%p, %lu, %p.\n", iface, streams, ids);
168 return E_NOTIMPL;
171 static HRESULT WINAPI video_processor_GetInputAvailableType(IMFTransform *iface, DWORD id, DWORD index,
172 IMFMediaType **type)
174 FIXME("%p, %lu, %lu, %p.\n", iface, id, index, type);
176 return E_NOTIMPL;
179 static HRESULT WINAPI video_processor_GetOutputAvailableType(IMFTransform *iface, DWORD id, DWORD index,
180 IMFMediaType **type)
182 FIXME("%p, %lu, %lu, %p.\n", iface, id, index, type);
184 return E_NOTIMPL;
187 static HRESULT WINAPI video_processor_SetInputType(IMFTransform *iface, DWORD id, IMFMediaType *type, DWORD flags)
189 FIXME("%p, %lu, %p, %#lx.\n", iface, id, type, flags);
191 return E_NOTIMPL;
194 static HRESULT WINAPI video_processor_SetOutputType(IMFTransform *iface, DWORD id, IMFMediaType *type, DWORD flags)
196 FIXME("%p, %lu, %p, %#lx.\n", iface, id, type, flags);
198 return E_NOTIMPL;
201 static HRESULT WINAPI video_processor_GetInputCurrentType(IMFTransform *iface, DWORD id, IMFMediaType **type)
203 FIXME("%p, %lu, %p.\n", iface, id, type);
205 return E_NOTIMPL;
208 static HRESULT WINAPI video_processor_GetOutputCurrentType(IMFTransform *iface, DWORD id, IMFMediaType **type)
210 FIXME("%p, %lu, %p.\n", iface, id, type);
212 return E_NOTIMPL;
215 static HRESULT WINAPI video_processor_GetInputStatus(IMFTransform *iface, DWORD id, DWORD *flags)
217 FIXME("%p, %lu, %p.\n", iface, id, flags);
219 return E_NOTIMPL;
222 static HRESULT WINAPI video_processor_GetOutputStatus(IMFTransform *iface, DWORD *flags)
224 FIXME("%p, %p.\n", iface, flags);
226 return E_NOTIMPL;
229 static HRESULT WINAPI video_processor_SetOutputBounds(IMFTransform *iface, LONGLONG lower, LONGLONG upper)
231 FIXME("%p, %s, %s.\n", iface, wine_dbgstr_longlong(lower), wine_dbgstr_longlong(upper));
233 return E_NOTIMPL;
236 static HRESULT WINAPI video_processor_ProcessEvent(IMFTransform *iface, DWORD id, IMFMediaEvent *event)
238 TRACE("%p, %lu, %p.\n", iface, id, event);
240 return E_NOTIMPL;
243 static HRESULT WINAPI video_processor_ProcessMessage(IMFTransform *iface, MFT_MESSAGE_TYPE message, ULONG_PTR param)
245 FIXME("%p, %u, %#Ix.\n", iface, message, param);
247 return E_NOTIMPL;
250 static HRESULT WINAPI video_processor_ProcessInput(IMFTransform *iface, DWORD id, IMFSample *sample, DWORD flags)
252 FIXME("%p, %lu, %p, %#lx.\n", iface, id, sample, flags);
254 return E_NOTIMPL;
257 static HRESULT WINAPI video_processor_ProcessOutput(IMFTransform *iface, DWORD flags, DWORD count,
258 MFT_OUTPUT_DATA_BUFFER *samples, DWORD *status)
260 FIXME("%p, %#lx, %lu, %p, %p.\n", iface, flags, count, samples, status);
262 return E_NOTIMPL;
265 static const IMFTransformVtbl video_processor_vtbl =
267 video_processor_QueryInterface,
268 video_processor_AddRef,
269 video_processor_Release,
270 video_processor_GetStreamLimits,
271 video_processor_GetStreamCount,
272 video_processor_GetStreamIDs,
273 video_processor_GetInputStreamInfo,
274 video_processor_GetOutputStreamInfo,
275 video_processor_GetAttributes,
276 video_processor_GetInputStreamAttributes,
277 video_processor_GetOutputStreamAttributes,
278 video_processor_DeleteInputStream,
279 video_processor_AddInputStreams,
280 video_processor_GetInputAvailableType,
281 video_processor_GetOutputAvailableType,
282 video_processor_SetInputType,
283 video_processor_SetOutputType,
284 video_processor_GetInputCurrentType,
285 video_processor_GetOutputCurrentType,
286 video_processor_GetInputStatus,
287 video_processor_GetOutputStatus,
288 video_processor_SetOutputBounds,
289 video_processor_ProcessEvent,
290 video_processor_ProcessMessage,
291 video_processor_ProcessInput,
292 video_processor_ProcessOutput,
295 struct class_factory
297 IClassFactory IClassFactory_iface;
298 LONG refcount;
299 HRESULT (*create_instance)(REFIID riid, void **obj);
302 static struct class_factory *impl_from_IClassFactory(IClassFactory *iface)
304 return CONTAINING_RECORD(iface, struct class_factory, IClassFactory_iface);
307 static HRESULT WINAPI class_factory_QueryInterface(IClassFactory *iface, REFIID riid, void **obj)
309 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);
311 if (IsEqualGUID(riid, &IID_IClassFactory) ||
312 IsEqualGUID(riid, &IID_IUnknown))
314 *obj = iface;
315 IClassFactory_AddRef(iface);
316 return S_OK;
319 WARN("%s is not supported.\n", debugstr_guid(riid));
320 *obj = NULL;
321 return E_NOINTERFACE;
324 static ULONG WINAPI class_factory_AddRef(IClassFactory *iface)
326 struct class_factory *factory = impl_from_IClassFactory(iface);
327 return InterlockedIncrement(&factory->refcount);
330 static ULONG WINAPI class_factory_Release(IClassFactory *iface)
332 struct class_factory *factory = impl_from_IClassFactory(iface);
333 ULONG refcount = InterlockedDecrement(&factory->refcount);
335 if (!refcount)
336 free(factory);
338 return refcount;
341 static HRESULT WINAPI class_factory_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **obj)
343 struct class_factory *factory = impl_from_IClassFactory(iface);
345 TRACE("%p, %p, %s, %p.\n", iface, outer, debugstr_guid(riid), obj);
347 if (outer)
349 *obj = NULL;
350 return CLASS_E_NOAGGREGATION;
353 return factory->create_instance(riid, obj);
356 static HRESULT WINAPI class_factory_LockServer(IClassFactory *iface, BOOL dolock)
358 TRACE("%p, %d.\n", iface, dolock);
359 return S_OK;
362 static const IClassFactoryVtbl class_factory_vtbl =
364 class_factory_QueryInterface,
365 class_factory_AddRef,
366 class_factory_Release,
367 class_factory_CreateInstance,
368 class_factory_LockServer,
371 static HRESULT video_processor_create(REFIID riid, void **ret)
373 struct video_processor *object;
374 HRESULT hr;
376 if (!(object = calloc(1, sizeof(*object))))
377 return E_OUTOFMEMORY;
379 object->IMFTransform_iface.lpVtbl = &video_processor_vtbl;
380 object->refcount = 1;
382 if (FAILED(hr = MFCreateAttributes(&object->attributes, 0)))
383 goto failed;
385 if (FAILED(hr = MFCreateAttributes(&object->output_attributes, 0)))
386 goto failed;
388 *ret = &object->IMFTransform_iface;
389 return S_OK;
391 failed:
393 IMFTransform_Release(&object->IMFTransform_iface);
394 return hr;
397 static const GUID CLSID_GStreamerByteStreamHandler = {0x317df618, 0x5e5a, 0x468a, {0x9f, 0x15, 0xd8, 0x27, 0xa9, 0xa0, 0x81, 0x62}};
399 static const GUID CLSID_WINEAudioConverter = {0x6a170414,0xaad9,0x4693,{0xb8,0x06,0x3a,0x0c,0x47,0xc5,0x70,0xd6}};
401 static const struct class_object
403 const GUID *clsid;
404 HRESULT (*create_instance)(REFIID riid, void **obj);
406 class_objects[] =
408 { &CLSID_VideoProcessorMFT, &video_processor_create },
409 { &CLSID_GStreamerByteStreamHandler, &winegstreamer_stream_handler_create },
410 { &CLSID_WINEAudioConverter, &audio_converter_create },
411 { &CLSID_MSH264DecoderMFT, &h264_decoder_create },
414 HRESULT mfplat_get_class_object(REFCLSID rclsid, REFIID riid, void **obj)
416 struct class_factory *factory;
417 unsigned int i;
418 HRESULT hr;
420 for (i = 0; i < ARRAY_SIZE(class_objects); ++i)
422 if (IsEqualGUID(class_objects[i].clsid, rclsid))
424 if (!(factory = malloc(sizeof(*factory))))
425 return E_OUTOFMEMORY;
427 factory->IClassFactory_iface.lpVtbl = &class_factory_vtbl;
428 factory->refcount = 1;
429 factory->create_instance = class_objects[i].create_instance;
431 hr = IClassFactory_QueryInterface(&factory->IClassFactory_iface, riid, obj);
432 IClassFactory_Release(&factory->IClassFactory_iface);
433 return hr;
437 return CLASS_E_CLASSNOTAVAILABLE;
440 static WCHAR audio_converterW[] = L"Audio Converter";
441 static const GUID *const audio_converter_supported_types[] =
443 &MFAudioFormat_PCM,
444 &MFAudioFormat_Float,
447 static WCHAR wma_decoderW[] = L"WMAudio Decoder MFT";
448 static const GUID *const wma_decoder_input_types[] =
450 &MEDIASUBTYPE_MSAUDIO1,
451 &MFAudioFormat_WMAudioV8,
452 &MFAudioFormat_WMAudioV9,
453 &MFAudioFormat_WMAudio_Lossless,
455 static const GUID *const wma_decoder_output_types[] =
457 &MFAudioFormat_PCM,
458 &MFAudioFormat_Float,
461 static WCHAR h264_decoderW[] = L"Microsoft H264 Video Decoder MFT";
462 static const GUID *const h264_decoder_input_types[] =
464 &MFVideoFormat_H264,
465 &MFVideoFormat_H264_ES,
467 static const GUID *const h264_decoder_output_types[] =
469 &MFVideoFormat_NV12,
470 &MFVideoFormat_YV12,
471 &MFVideoFormat_IYUV,
472 &MFVideoFormat_I420,
473 &MFVideoFormat_YUY2,
476 static const struct mft
478 const GUID *clsid;
479 const GUID *category;
480 LPWSTR name;
481 const UINT32 flags;
482 const GUID *major_type;
483 const UINT32 input_types_count;
484 const GUID *const *input_types;
485 const UINT32 output_types_count;
486 const GUID *const *output_types;
488 mfts[] =
491 &CLSID_WINEAudioConverter,
492 &MFT_CATEGORY_AUDIO_EFFECT,
493 audio_converterW,
494 MFT_ENUM_FLAG_SYNCMFT,
495 &MFMediaType_Audio,
496 ARRAY_SIZE(audio_converter_supported_types),
497 audio_converter_supported_types,
498 ARRAY_SIZE(audio_converter_supported_types),
499 audio_converter_supported_types,
502 &CLSID_WMADecMediaObject,
503 &MFT_CATEGORY_AUDIO_DECODER,
504 wma_decoderW,
505 MFT_ENUM_FLAG_SYNCMFT,
506 &MFMediaType_Audio,
507 ARRAY_SIZE(wma_decoder_input_types),
508 wma_decoder_input_types,
509 ARRAY_SIZE(wma_decoder_output_types),
510 wma_decoder_output_types,
513 &CLSID_MSH264DecoderMFT,
514 &MFT_CATEGORY_VIDEO_DECODER,
515 h264_decoderW,
516 MFT_ENUM_FLAG_SYNCMFT,
517 &MFMediaType_Video,
518 ARRAY_SIZE(h264_decoder_input_types),
519 h264_decoder_input_types,
520 ARRAY_SIZE(h264_decoder_output_types),
521 h264_decoder_output_types,
525 HRESULT mfplat_DllRegisterServer(void)
527 unsigned int i, j;
528 HRESULT hr;
529 MFT_REGISTER_TYPE_INFO input_types[4], output_types[5];
531 for (i = 0; i < ARRAY_SIZE(mfts); i++)
533 const struct mft *cur = &mfts[i];
535 for (j = 0; j < cur->input_types_count; j++)
537 input_types[j].guidMajorType = *(cur->major_type);
538 input_types[j].guidSubtype = *(cur->input_types[j]);
540 for (j = 0; j < cur->output_types_count; j++)
542 output_types[j].guidMajorType = *(cur->major_type);
543 output_types[j].guidSubtype = *(cur->output_types[j]);
546 hr = MFTRegister(*(cur->clsid), *(cur->category), cur->name, cur->flags, cur->input_types_count,
547 input_types, cur->output_types_count, output_types, NULL);
549 if (FAILED(hr))
551 FIXME("Failed to register MFT, hr %#lx.\n", hr);
552 return hr;
555 return S_OK;
558 static const struct
560 const GUID *subtype;
561 enum wg_video_format format;
563 video_formats[] =
565 {&MFVideoFormat_ARGB32, WG_VIDEO_FORMAT_BGRA},
566 {&MFVideoFormat_RGB32, WG_VIDEO_FORMAT_BGRx},
567 {&MFVideoFormat_RGB24, WG_VIDEO_FORMAT_BGR},
568 {&MFVideoFormat_RGB555, WG_VIDEO_FORMAT_RGB15},
569 {&MFVideoFormat_RGB565, WG_VIDEO_FORMAT_RGB16},
570 {&MFVideoFormat_AYUV, WG_VIDEO_FORMAT_AYUV},
571 {&MFVideoFormat_I420, WG_VIDEO_FORMAT_I420},
572 {&MFVideoFormat_IYUV, WG_VIDEO_FORMAT_I420},
573 {&MFVideoFormat_NV12, WG_VIDEO_FORMAT_NV12},
574 {&MFVideoFormat_UYVY, WG_VIDEO_FORMAT_UYVY},
575 {&MFVideoFormat_YUY2, WG_VIDEO_FORMAT_YUY2},
576 {&MFVideoFormat_YV12, WG_VIDEO_FORMAT_YV12},
577 {&MFVideoFormat_YVYU, WG_VIDEO_FORMAT_YVYU},
580 static const struct
582 const GUID *subtype;
583 UINT32 depth;
584 enum wg_audio_format format;
586 audio_formats[] =
588 {&MFAudioFormat_PCM, 8, WG_AUDIO_FORMAT_U8},
589 {&MFAudioFormat_PCM, 16, WG_AUDIO_FORMAT_S16LE},
590 {&MFAudioFormat_PCM, 24, WG_AUDIO_FORMAT_S24LE},
591 {&MFAudioFormat_PCM, 32, WG_AUDIO_FORMAT_S32LE},
592 {&MFAudioFormat_Float, 32, WG_AUDIO_FORMAT_F32LE},
593 {&MFAudioFormat_Float, 64, WG_AUDIO_FORMAT_F64LE},
596 static inline UINT64 make_uint64(UINT32 high, UINT32 low)
598 return ((UINT64)high << 32) | low;
601 static IMFMediaType *mf_media_type_from_wg_format_audio(const struct wg_format *format)
603 IMFMediaType *type;
604 unsigned int i;
606 for (i = 0; i < ARRAY_SIZE(audio_formats); ++i)
608 if (format->u.audio.format == audio_formats[i].format)
610 if (FAILED(MFCreateMediaType(&type)))
611 return NULL;
613 IMFMediaType_SetGUID(type, &MF_MT_MAJOR_TYPE, &MFMediaType_Audio);
614 IMFMediaType_SetGUID(type, &MF_MT_SUBTYPE, audio_formats[i].subtype);
615 IMFMediaType_SetUINT32(type, &MF_MT_AUDIO_BITS_PER_SAMPLE, audio_formats[i].depth);
616 IMFMediaType_SetUINT32(type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, format->u.audio.rate);
617 IMFMediaType_SetUINT32(type, &MF_MT_AUDIO_NUM_CHANNELS, format->u.audio.channels);
618 IMFMediaType_SetUINT32(type, &MF_MT_AUDIO_CHANNEL_MASK, format->u.audio.channel_mask);
619 IMFMediaType_SetUINT32(type, &MF_MT_ALL_SAMPLES_INDEPENDENT, TRUE);
620 IMFMediaType_SetUINT32(type, &MF_MT_AUDIO_BLOCK_ALIGNMENT, format->u.audio.channels * audio_formats[i].depth / 8);
622 return type;
626 return NULL;
629 static IMFMediaType *mf_media_type_from_wg_format_video(const struct wg_format *format)
631 IMFMediaType *type;
632 unsigned int i;
634 for (i = 0; i < ARRAY_SIZE(video_formats); ++i)
636 if (format->u.video.format == video_formats[i].format)
638 if (FAILED(MFCreateMediaType(&type)))
639 return NULL;
641 IMFMediaType_SetGUID(type, &MF_MT_MAJOR_TYPE, &MFMediaType_Video);
642 IMFMediaType_SetGUID(type, &MF_MT_SUBTYPE, video_formats[i].subtype);
643 IMFMediaType_SetUINT64(type, &MF_MT_FRAME_SIZE,
644 make_uint64(format->u.video.width, format->u.video.height));
645 IMFMediaType_SetUINT64(type, &MF_MT_FRAME_RATE,
646 make_uint64(format->u.video.fps_n, format->u.video.fps_d));
647 IMFMediaType_SetUINT32(type, &MF_MT_COMPRESSED, FALSE);
648 IMFMediaType_SetUINT32(type, &MF_MT_ALL_SAMPLES_INDEPENDENT, TRUE);
649 IMFMediaType_SetUINT32(type, &MF_MT_VIDEO_ROTATION, MFVideoRotationFormat_0);
651 return type;
655 return NULL;
658 IMFMediaType *mf_media_type_from_wg_format(const struct wg_format *format)
660 switch (format->major_type)
662 case WG_MAJOR_TYPE_UNKNOWN:
663 return NULL;
665 case WG_MAJOR_TYPE_WMA:
666 FIXME("WMA format not implemented!\n");
667 return NULL;
669 case WG_MAJOR_TYPE_AUDIO:
670 return mf_media_type_from_wg_format_audio(format);
672 case WG_MAJOR_TYPE_VIDEO:
673 return mf_media_type_from_wg_format_video(format);
676 assert(0);
677 return NULL;
680 static void mf_media_type_to_wg_format_audio(IMFMediaType *type, const GUID *subtype, struct wg_format *format)
682 UINT32 rate, channels, channel_mask, depth;
683 unsigned int i;
685 if (FAILED(IMFMediaType_GetUINT32(type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &rate)))
687 FIXME("Sample rate is not set.\n");
688 return;
690 if (FAILED(IMFMediaType_GetUINT32(type, &MF_MT_AUDIO_NUM_CHANNELS, &channels)))
692 FIXME("Channel count is not set.\n");
693 return;
695 if (FAILED(IMFMediaType_GetUINT32(type, &MF_MT_AUDIO_BITS_PER_SAMPLE, &depth)))
697 FIXME("Depth is not set.\n");
698 return;
700 if (FAILED(IMFMediaType_GetUINT32(type, &MF_MT_AUDIO_CHANNEL_MASK, &channel_mask)))
702 if (channels == 1)
703 channel_mask = KSAUDIO_SPEAKER_MONO;
704 else if (channels == 2)
705 channel_mask = KSAUDIO_SPEAKER_STEREO;
706 else
708 FIXME("Channel mask is not set.\n");
709 return;
713 format->major_type = WG_MAJOR_TYPE_AUDIO;
714 format->u.audio.channels = channels;
715 format->u.audio.channel_mask = channel_mask;
716 format->u.audio.rate = rate;
718 for (i = 0; i < ARRAY_SIZE(audio_formats); ++i)
720 if (IsEqualGUID(subtype, audio_formats[i].subtype) && depth == audio_formats[i].depth)
722 format->u.audio.format = audio_formats[i].format;
723 return;
726 FIXME("Unrecognized audio subtype %s, depth %u.\n", debugstr_guid(subtype), depth);
729 static void mf_media_type_to_wg_format_video(IMFMediaType *type, const GUID *subtype, struct wg_format *format)
731 UINT64 frame_rate, frame_size;
732 unsigned int i;
734 if (FAILED(IMFMediaType_GetUINT64(type, &MF_MT_FRAME_SIZE, &frame_size)))
736 FIXME("Frame size is not set.\n");
737 return;
740 format->major_type = WG_MAJOR_TYPE_VIDEO;
741 format->u.video.width = (UINT32)(frame_size >> 32);
742 format->u.video.height = (UINT32)frame_size;
743 format->u.video.fps_n = 1;
744 format->u.video.fps_d = 1;
746 if (SUCCEEDED(IMFMediaType_GetUINT64(type, &MF_MT_FRAME_RATE, &frame_rate)) && (UINT32)frame_rate)
748 format->u.video.fps_n = (UINT32)(frame_rate >> 32);
749 format->u.video.fps_d = (UINT32)frame_rate;
752 for (i = 0; i < ARRAY_SIZE(video_formats); ++i)
754 if (IsEqualGUID(subtype, video_formats[i].subtype))
756 format->u.video.format = video_formats[i].format;
757 return;
760 FIXME("Unrecognized video subtype %s.\n", debugstr_guid(subtype));
763 static void mf_media_type_to_wg_format_wma(IMFMediaType *type, const GUID *subtype, struct wg_format *format)
765 UINT32 rate, depth, channels, block_align, bytes_per_second, codec_data_len;
766 BYTE codec_data[64];
767 UINT32 version;
769 if (FAILED(IMFMediaType_GetUINT32(type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &rate)))
771 FIXME("Sample rate is not set.\n");
772 return;
774 if (FAILED(IMFMediaType_GetUINT32(type, &MF_MT_AUDIO_NUM_CHANNELS, &channels)))
776 FIXME("Channel count is not set.\n");
777 return;
779 if (FAILED(IMFMediaType_GetUINT32(type, &MF_MT_AUDIO_BLOCK_ALIGNMENT, &block_align)))
781 FIXME("Block alignment is not set.\n");
782 return;
784 if (FAILED(IMFMediaType_GetUINT32(type, &MF_MT_AUDIO_BITS_PER_SAMPLE, &depth)))
786 FIXME("Depth is not set.\n");
787 return;
789 if (FAILED(IMFMediaType_GetBlob(type, &MF_MT_USER_DATA, codec_data, sizeof(codec_data), &codec_data_len)))
791 FIXME("Codec data is not set.\n");
792 return;
794 if (FAILED(IMFMediaType_GetUINT32(type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &bytes_per_second)))
796 FIXME("Bitrate is not set.\n");
797 bytes_per_second = 0;
800 if (IsEqualGUID(subtype, &MEDIASUBTYPE_MSAUDIO1))
801 version = 1;
802 else if (IsEqualGUID(subtype, &MFAudioFormat_WMAudioV8))
803 version = 2;
804 else if (IsEqualGUID(subtype, &MFAudioFormat_WMAudioV9))
805 version = 3;
806 else if (IsEqualGUID(subtype, &MFAudioFormat_WMAudio_Lossless))
807 version = 4;
808 else
810 assert(0);
811 return;
814 format->major_type = WG_MAJOR_TYPE_WMA;
815 format->u.wma.version = version;
816 format->u.wma.bitrate = bytes_per_second * 8;
817 format->u.wma.rate = rate;
818 format->u.wma.depth = depth;
819 format->u.wma.channels = channels;
820 format->u.wma.block_align = block_align;
821 format->u.wma.codec_data_len = codec_data_len;
822 memcpy(format->u.wma.codec_data, codec_data, codec_data_len);
825 void mf_media_type_to_wg_format(IMFMediaType *type, struct wg_format *format)
827 GUID major_type, subtype;
829 memset(format, 0, sizeof(*format));
831 if (FAILED(IMFMediaType_GetMajorType(type, &major_type)))
833 FIXME("Major type is not set.\n");
834 return;
836 if (FAILED(IMFMediaType_GetGUID(type, &MF_MT_SUBTYPE, &subtype)))
838 FIXME("Subtype is not set.\n");
839 return;
842 if (IsEqualGUID(&major_type, &MFMediaType_Audio))
844 if (IsEqualGUID(&subtype, &MEDIASUBTYPE_MSAUDIO1) ||
845 IsEqualGUID(&subtype, &MFAudioFormat_WMAudioV8) ||
846 IsEqualGUID(&subtype, &MFAudioFormat_WMAudioV9) ||
847 IsEqualGUID(&subtype, &MFAudioFormat_WMAudio_Lossless))
848 mf_media_type_to_wg_format_wma(type, &subtype, format);
849 else
850 mf_media_type_to_wg_format_audio(type, &subtype, format);
852 else if (IsEqualGUID(&major_type, &MFMediaType_Video))
853 mf_media_type_to_wg_format_video(type, &subtype, format);
854 else
855 FIXME("Unrecognized major type %s.\n", debugstr_guid(&major_type));
858 struct mf_sample
860 IMFSample *sample;
861 IMFMediaBuffer *media_buffer;
862 struct wg_sample wg_sample;
865 HRESULT mf_create_wg_sample(IMFSample *sample, struct wg_sample **out)
867 DWORD current_length, max_length;
868 struct mf_sample *mf_sample;
869 BYTE *buffer;
870 HRESULT hr;
872 if (!(mf_sample = calloc(1, sizeof(*mf_sample))))
873 return E_OUTOFMEMORY;
874 if (FAILED(hr = IMFSample_ConvertToContiguousBuffer(sample, &mf_sample->media_buffer)))
875 goto out;
876 if (FAILED(hr = IMFMediaBuffer_Lock(mf_sample->media_buffer, &buffer, &max_length, &current_length)))
877 goto out;
879 IMFSample_AddRef((mf_sample->sample = sample));
880 mf_sample->wg_sample.data = buffer;
881 mf_sample->wg_sample.size = current_length;
882 mf_sample->wg_sample.max_size = max_length;
884 TRACE("Created mf_sample %p for sample %p.\n", mf_sample, sample);
885 *out = &mf_sample->wg_sample;
886 return S_OK;
888 out:
889 if (mf_sample->media_buffer)
890 IMFMediaBuffer_Release(mf_sample->media_buffer);
891 free(mf_sample);
892 return hr;
895 void mf_destroy_wg_sample(struct wg_sample *wg_sample)
897 struct mf_sample *mf_sample = CONTAINING_RECORD(wg_sample, struct mf_sample, wg_sample);
899 IMFMediaBuffer_Unlock(mf_sample->media_buffer);
900 IMFMediaBuffer_SetCurrentLength(mf_sample->media_buffer, wg_sample->size);
901 IMFMediaBuffer_Release(mf_sample->media_buffer);
903 IMFSample_Release(mf_sample->sample);
904 free(mf_sample);