cmd: DIR command outputs free space for the path.
[wine.git] / dlls / winegstreamer / quartz_parser.c
blobf9d8d79b259b37a9bc12dc727c238400bc9a5a80
1 /*
2 * DirectShow parser filters
4 * Copyright 2010 Maarten Lankhorst for CodeWeavers
5 * Copyright 2010 Aric Stewart for CodeWeavers
6 * Copyright 2019-2020 Zebediah Figura
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library 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 GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "gst_private.h"
24 #include "gst_guids.h"
26 #include "amvideo.h"
28 #include <limits.h>
29 #include "dvdmedia.h"
30 #include "mmreg.h"
31 #include "ks.h"
32 #include "wmcodecdsp.h"
33 #include "initguid.h"
34 #include "ksmedia.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
38 static const GUID MEDIASUBTYPE_CVID = {mmioFOURCC('c','v','i','d'), 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
39 static const GUID MEDIASUBTYPE_VC1S = {mmioFOURCC('V','C','1','S'), 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
40 static const GUID MEDIASUBTYPE_MP3 = {WAVE_FORMAT_MPEGLAYER3, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
41 static const GUID MEDIASUBTYPE_WMV_Unknown = {0x7ce12ca9, 0xbfbf, 0x43d9, {0x9d, 0x00, 0x82, 0xb8, 0xed, 0x54, 0x31, 0x6b}};
43 struct parser
45 struct strmbase_filter filter;
46 IAMStreamSelect IAMStreamSelect_iface;
48 struct strmbase_sink sink;
49 IAsyncReader *reader;
51 struct parser_source **sources;
52 unsigned int source_count;
53 BOOL enum_sink_first;
55 wg_parser_t wg_parser;
57 /* This protects the "streaming" and "flushing" fields, accessed by both
58 * the application and streaming threads.
59 * We cannot use the filter lock for this, since that is held while waiting
60 * for the streaming thread, and hence the streaming thread cannot take the
61 * filter lock.
62 * This lock must not be acquired before acquiring the filter lock or
63 * flushing_cs. */
64 CRITICAL_SECTION streaming_cs;
65 CONDITION_VARIABLE flushing_cv;
67 /* FIXME: It would be nice to avoid duplicating these with strmbase.
68 * However, synchronization is tricky; we need access to be protected by a
69 * separate lock. */
70 bool streaming, sink_connected;
72 bool flushing;
74 HANDLE read_thread;
76 BOOL (*init_gst)(struct parser *filter);
77 HRESULT (*source_query_accept)(struct parser_source *pin, const AM_MEDIA_TYPE *mt);
78 HRESULT (*source_get_media_type)(struct parser_source *pin, unsigned int index, AM_MEDIA_TYPE *mt);
81 struct parser_source
83 struct strmbase_source pin;
84 IQualityControl IQualityControl_iface;
86 wg_parser_stream_t wg_stream;
88 SourceSeeking seek;
90 CRITICAL_SECTION flushing_cs;
91 CONDITION_VARIABLE eos_cv;
92 HANDLE thread;
94 /* This variable is read and written by both the streaming thread and
95 * application threads. However, it is only written by the application
96 * thread when the streaming thread is not running, or when it is blocked
97 * by flushing_cs. */
98 bool need_segment;
100 bool eos;
103 static inline struct parser *impl_from_strmbase_filter(struct strmbase_filter *iface)
105 return CONTAINING_RECORD(iface, struct parser, filter);
108 static const IMediaSeekingVtbl GST_Seeking_Vtbl;
109 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl;
111 static struct parser_source *create_pin(struct parser *filter,
112 wg_parser_stream_t stream, const WCHAR *name);
113 static HRESULT GST_RemoveOutputPins(struct parser *This);
114 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface);
115 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface);
116 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface);
118 static bool wg_audio_format_is_float(enum wg_audio_format format)
120 switch (format)
122 case WG_AUDIO_FORMAT_UNKNOWN: return false;
123 case WG_AUDIO_FORMAT_U8: return false;
124 case WG_AUDIO_FORMAT_S16LE: return false;
125 case WG_AUDIO_FORMAT_S24LE: return false;
126 case WG_AUDIO_FORMAT_S32LE: return false;
127 case WG_AUDIO_FORMAT_F32LE: return true;
128 case WG_AUDIO_FORMAT_F64LE: return true;
131 assert(0);
132 return false;
135 static WORD wg_audio_format_get_depth(enum wg_audio_format format)
137 switch (format)
139 case WG_AUDIO_FORMAT_UNKNOWN: return 0;
140 case WG_AUDIO_FORMAT_U8: return 8;
141 case WG_AUDIO_FORMAT_S16LE: return 16;
142 case WG_AUDIO_FORMAT_S24LE: return 24;
143 case WG_AUDIO_FORMAT_S32LE: return 32;
144 case WG_AUDIO_FORMAT_F32LE: return 32;
145 case WG_AUDIO_FORMAT_F64LE: return 64;
148 assert(0);
149 return 0;
152 static bool amt_from_wg_format_audio(AM_MEDIA_TYPE *mt, const struct wg_format *format)
154 mt->majortype = MEDIATYPE_Audio;
155 mt->formattype = FORMAT_WaveFormatEx;
157 switch (format->u.audio.format)
159 case WG_AUDIO_FORMAT_UNKNOWN:
160 return false;
162 case WG_AUDIO_FORMAT_U8:
163 case WG_AUDIO_FORMAT_S16LE:
164 case WG_AUDIO_FORMAT_S24LE:
165 case WG_AUDIO_FORMAT_S32LE:
166 case WG_AUDIO_FORMAT_F32LE:
167 case WG_AUDIO_FORMAT_F64LE:
169 bool is_float = wg_audio_format_is_float(format->u.audio.format);
170 WORD depth = wg_audio_format_get_depth(format->u.audio.format);
172 if (is_float || format->u.audio.channels > 2)
174 WAVEFORMATEXTENSIBLE *wave_format;
176 if (!(wave_format = CoTaskMemAlloc(sizeof(*wave_format))))
177 return false;
178 memset(wave_format, 0, sizeof(*wave_format));
180 mt->subtype = is_float ? MEDIASUBTYPE_IEEE_FLOAT : MEDIASUBTYPE_PCM;
181 mt->bFixedSizeSamples = TRUE;
182 mt->pbFormat = (BYTE *)wave_format;
183 mt->cbFormat = sizeof(*wave_format);
184 wave_format->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
185 wave_format->Format.nChannels = format->u.audio.channels;
186 wave_format->Format.nSamplesPerSec = format->u.audio.rate;
187 wave_format->Format.nAvgBytesPerSec = format->u.audio.rate * format->u.audio.channels * depth / 8;
188 wave_format->Format.nBlockAlign = format->u.audio.channels * depth / 8;
189 wave_format->Format.wBitsPerSample = depth;
190 wave_format->Format.cbSize = sizeof(*wave_format) - sizeof(WAVEFORMATEX);
191 wave_format->Samples.wValidBitsPerSample = depth;
192 wave_format->dwChannelMask = format->u.audio.channel_mask;
193 wave_format->SubFormat = is_float ? KSDATAFORMAT_SUBTYPE_IEEE_FLOAT : KSDATAFORMAT_SUBTYPE_PCM;
194 mt->lSampleSize = wave_format->Format.nBlockAlign;
196 else
198 WAVEFORMATEX *wave_format;
200 if (!(wave_format = CoTaskMemAlloc(sizeof(*wave_format))))
201 return false;
202 memset(wave_format, 0, sizeof(*wave_format));
204 mt->subtype = MEDIASUBTYPE_PCM;
205 mt->bFixedSizeSamples = TRUE;
206 mt->pbFormat = (BYTE *)wave_format;
207 mt->cbFormat = sizeof(*wave_format);
208 wave_format->wFormatTag = WAVE_FORMAT_PCM;
209 wave_format->nChannels = format->u.audio.channels;
210 wave_format->nSamplesPerSec = format->u.audio.rate;
211 wave_format->nAvgBytesPerSec = format->u.audio.rate * format->u.audio.channels * depth / 8;
212 wave_format->nBlockAlign = format->u.audio.channels * depth / 8;
213 wave_format->wBitsPerSample = depth;
214 wave_format->cbSize = 0;
215 mt->lSampleSize = wave_format->nBlockAlign;
217 return true;
221 assert(0);
222 return false;
225 static bool amt_from_wg_format_audio_mpeg1(AM_MEDIA_TYPE *mt, const struct wg_format *format)
227 mt->majortype = MEDIATYPE_Audio;
228 mt->formattype = FORMAT_WaveFormatEx;
230 switch (format->u.audio_mpeg1.layer)
232 case 1:
233 case 2:
235 MPEG1WAVEFORMAT *wave_format;
237 if (!(wave_format = CoTaskMemAlloc(sizeof(*wave_format))))
238 return false;
239 memset(wave_format, 0, sizeof(*wave_format));
241 mt->subtype = MEDIASUBTYPE_MPEG1AudioPayload;
242 mt->cbFormat = sizeof(*wave_format);
243 mt->pbFormat = (BYTE *)wave_format;
244 wave_format->wfx.wFormatTag = WAVE_FORMAT_MPEG;
245 wave_format->wfx.nChannels = format->u.audio_mpeg1.channels;
246 wave_format->wfx.nSamplesPerSec = format->u.audio_mpeg1.rate;
247 wave_format->wfx.cbSize = sizeof(*wave_format) - sizeof(WAVEFORMATEX);
248 wave_format->fwHeadLayer = format->u.audio_mpeg1.layer;
249 return true;
252 case 3:
254 MPEGLAYER3WAVEFORMAT *wave_format;
256 if (!(wave_format = CoTaskMemAlloc(sizeof(*wave_format))))
257 return false;
258 memset(wave_format, 0, sizeof(*wave_format));
260 mt->subtype = MEDIASUBTYPE_MP3;
261 mt->cbFormat = sizeof(*wave_format);
262 mt->pbFormat = (BYTE *)wave_format;
263 wave_format->wfx.wFormatTag = WAVE_FORMAT_MPEGLAYER3;
264 wave_format->wfx.nChannels = format->u.audio_mpeg1.channels;
265 wave_format->wfx.nSamplesPerSec = format->u.audio_mpeg1.rate;
266 wave_format->wfx.cbSize = sizeof(*wave_format) - sizeof(WAVEFORMATEX);
267 /* FIXME: We can't get most of the MPEG data from the caps. We may have
268 * to manually parse the header. */
269 wave_format->wID = MPEGLAYER3_ID_MPEG;
270 wave_format->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON;
271 wave_format->nFramesPerBlock = 1;
272 wave_format->nCodecDelay = 1393;
273 return true;
277 assert(0);
278 return false;
281 #define ALIGN(n, alignment) (((n) + (alignment) - 1) & ~((alignment) - 1))
283 unsigned int wg_format_get_max_size(const struct wg_format *format)
285 switch (format->major_type)
287 case WG_MAJOR_TYPE_VIDEO:
289 unsigned int width = format->u.video.width, height = abs(format->u.video.height);
291 switch (format->u.video.format)
293 case WG_VIDEO_FORMAT_BGRA:
294 case WG_VIDEO_FORMAT_BGRx:
295 case WG_VIDEO_FORMAT_AYUV:
296 return width * height * 4;
298 case WG_VIDEO_FORMAT_BGR:
299 return ALIGN(width * 3, 4) * height;
301 case WG_VIDEO_FORMAT_RGB15:
302 case WG_VIDEO_FORMAT_RGB16:
303 case WG_VIDEO_FORMAT_UYVY:
304 case WG_VIDEO_FORMAT_YUY2:
305 case WG_VIDEO_FORMAT_YVYU:
306 return ALIGN(width * 2, 4) * height;
308 case WG_VIDEO_FORMAT_I420:
309 case WG_VIDEO_FORMAT_YV12:
310 return ALIGN(width, 4) * ALIGN(height, 2) /* Y plane */
311 + 2 * ALIGN((width + 1) / 2, 4) * ((height + 1) / 2); /* U and V planes */
313 case WG_VIDEO_FORMAT_NV12:
314 return ALIGN(width, 4) * ALIGN(height, 2) /* Y plane */
315 + ALIGN(width, 4) * ((height + 1) / 2); /* U/V plane */
317 case WG_VIDEO_FORMAT_UNKNOWN:
318 FIXME("Cannot guess maximum sample size for unknown video format.\n");
319 return 0;
321 break;
324 case WG_MAJOR_TYPE_VIDEO_CINEPAK:
325 /* Both ffmpeg's encoder and a Cinepak file seen in the wild report
326 * 24 bpp. ffmpeg sets biSizeImage as below; others may be smaller,
327 * but as long as every sample fits into our allocator, we're fine. */
328 return format->u.video_cinepak.width * format->u.video_cinepak.height * 3;
330 case WG_MAJOR_TYPE_AUDIO:
332 unsigned int rate = format->u.audio.rate, channels = format->u.audio.channels;
334 /* Actually we don't know how large of a sample GStreamer will give
335 * us. Hopefully 1 second is enough... */
337 switch (format->u.audio.format)
339 case WG_AUDIO_FORMAT_U8:
340 return rate * channels;
342 case WG_AUDIO_FORMAT_S16LE:
343 return rate * channels * 2;
345 case WG_AUDIO_FORMAT_S24LE:
346 return rate * channels * 3;
348 case WG_AUDIO_FORMAT_S32LE:
349 case WG_AUDIO_FORMAT_F32LE:
350 return rate * channels * 4;
352 case WG_AUDIO_FORMAT_F64LE:
353 return rate * channels * 8;
355 case WG_AUDIO_FORMAT_UNKNOWN:
356 FIXME("Cannot guess maximum sample size for unknown audio format.\n");
357 return 0;
359 break;
362 case WG_MAJOR_TYPE_AUDIO_MPEG1:
363 switch (format->u.audio_mpeg1.layer)
365 case 1:
366 return 56000;
368 case 2:
369 return 48000;
371 case 3:
372 return 40000;
374 break;
376 case WG_MAJOR_TYPE_AUDIO_MPEG4:
377 case WG_MAJOR_TYPE_AUDIO_WMA:
378 case WG_MAJOR_TYPE_VIDEO_H264:
379 case WG_MAJOR_TYPE_VIDEO_WMV:
380 case WG_MAJOR_TYPE_VIDEO_INDEO:
381 FIXME("Format %u not implemented!\n", format->major_type);
382 return 0;
384 case WG_MAJOR_TYPE_UNKNOWN:
385 FIXME("Cannot guess maximum sample size for unknown format.\n");
386 return 0;
389 assert(0);
390 return 0;
393 static const GUID *wg_video_format_get_mediasubtype(enum wg_video_format format)
395 switch (format)
397 case WG_VIDEO_FORMAT_UNKNOWN: return &GUID_NULL;
398 case WG_VIDEO_FORMAT_BGRA: return &MEDIASUBTYPE_ARGB32;
399 case WG_VIDEO_FORMAT_BGRx: return &MEDIASUBTYPE_RGB32;
400 case WG_VIDEO_FORMAT_BGR: return &MEDIASUBTYPE_RGB24;
401 case WG_VIDEO_FORMAT_RGB15: return &MEDIASUBTYPE_RGB555;
402 case WG_VIDEO_FORMAT_RGB16: return &MEDIASUBTYPE_RGB565;
403 case WG_VIDEO_FORMAT_AYUV: return &MEDIASUBTYPE_AYUV;
404 case WG_VIDEO_FORMAT_I420: return &MEDIASUBTYPE_I420;
405 case WG_VIDEO_FORMAT_NV12: return &MEDIASUBTYPE_NV12;
406 case WG_VIDEO_FORMAT_UYVY: return &MEDIASUBTYPE_UYVY;
407 case WG_VIDEO_FORMAT_YUY2: return &MEDIASUBTYPE_YUY2;
408 case WG_VIDEO_FORMAT_YV12: return &MEDIASUBTYPE_YV12;
409 case WG_VIDEO_FORMAT_YVYU: return &MEDIASUBTYPE_YVYU;
412 assert(0);
413 return NULL;
416 static DWORD wg_video_format_get_compression(enum wg_video_format format)
418 switch (format)
420 case WG_VIDEO_FORMAT_UNKNOWN: return 0;
421 case WG_VIDEO_FORMAT_BGRA: return BI_RGB;
422 case WG_VIDEO_FORMAT_BGRx: return BI_RGB;
423 case WG_VIDEO_FORMAT_BGR: return BI_RGB;
424 case WG_VIDEO_FORMAT_RGB15: return BI_RGB;
425 case WG_VIDEO_FORMAT_RGB16: return BI_BITFIELDS;
426 case WG_VIDEO_FORMAT_AYUV: return mmioFOURCC('A','Y','U','V');
427 case WG_VIDEO_FORMAT_I420: return mmioFOURCC('I','4','2','0');
428 case WG_VIDEO_FORMAT_NV12: return mmioFOURCC('N','V','1','2');
429 case WG_VIDEO_FORMAT_UYVY: return mmioFOURCC('U','Y','V','Y');
430 case WG_VIDEO_FORMAT_YUY2: return mmioFOURCC('Y','U','Y','2');
431 case WG_VIDEO_FORMAT_YV12: return mmioFOURCC('Y','V','1','2');
432 case WG_VIDEO_FORMAT_YVYU: return mmioFOURCC('Y','V','Y','U');
435 assert(0);
436 return 0;
439 static WORD wg_video_format_get_depth(enum wg_video_format format)
441 switch (format)
443 case WG_VIDEO_FORMAT_UNKNOWN: return 0;
444 case WG_VIDEO_FORMAT_BGRA: return 32;
445 case WG_VIDEO_FORMAT_BGRx: return 32;
446 case WG_VIDEO_FORMAT_BGR: return 24;
447 case WG_VIDEO_FORMAT_RGB15: return 16;
448 case WG_VIDEO_FORMAT_RGB16: return 16;
449 case WG_VIDEO_FORMAT_AYUV: return 32;
450 case WG_VIDEO_FORMAT_I420: return 12;
451 case WG_VIDEO_FORMAT_NV12: return 12;
452 case WG_VIDEO_FORMAT_UYVY: return 16;
453 case WG_VIDEO_FORMAT_YUY2: return 16;
454 case WG_VIDEO_FORMAT_YV12: return 12;
455 case WG_VIDEO_FORMAT_YVYU: return 16;
458 assert(0);
459 return 0;
462 static bool amt_from_wg_format_video(AM_MEDIA_TYPE *mt, const struct wg_format *format, bool wm)
464 VIDEOINFO *video_format;
465 uint32_t frame_time;
467 if (format->u.video.format == WG_VIDEO_FORMAT_UNKNOWN)
468 return false;
470 if (!(video_format = CoTaskMemAlloc(sizeof(*video_format))))
471 return false;
473 mt->majortype = MEDIATYPE_Video;
474 mt->subtype = *wg_video_format_get_mediasubtype(format->u.video.format);
475 if (wm)
476 mt->bFixedSizeSamples = TRUE;
477 else
478 mt->bTemporalCompression = TRUE;
479 mt->lSampleSize = 1;
480 mt->formattype = FORMAT_VideoInfo;
481 mt->cbFormat = sizeof(VIDEOINFOHEADER);
482 mt->pbFormat = (BYTE *)video_format;
484 memset(video_format, 0, sizeof(*video_format));
486 if (wm)
488 SetRect(&video_format->rcSource, 0, 0, format->u.video.width, abs(format->u.video.height));
489 video_format->rcTarget = video_format->rcSource;
491 if ((frame_time = MulDiv(10000000, format->u.video.fps_d, format->u.video.fps_n)) != -1)
492 video_format->AvgTimePerFrame = frame_time;
493 video_format->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
494 video_format->bmiHeader.biWidth = format->u.video.width;
495 video_format->bmiHeader.biHeight = format->u.video.height;
496 if (wg_video_format_is_rgb(format->u.video.format))
497 video_format->bmiHeader.biHeight = -format->u.video.height;
498 video_format->bmiHeader.biPlanes = 1;
499 video_format->bmiHeader.biBitCount = wg_video_format_get_depth(format->u.video.format);
500 video_format->bmiHeader.biCompression = wg_video_format_get_compression(format->u.video.format);
501 video_format->bmiHeader.biSizeImage = wg_format_get_max_size(format);
503 if (format->u.video.format == WG_VIDEO_FORMAT_RGB16)
505 mt->cbFormat = offsetof(VIDEOINFO, dwBitMasks[3]);
506 video_format->dwBitMasks[iRED] = 0xf800;
507 video_format->dwBitMasks[iGREEN] = 0x07e0;
508 video_format->dwBitMasks[iBLUE] = 0x001f;
511 return true;
514 static bool amt_from_wg_format_video_cinepak(AM_MEDIA_TYPE *mt, const struct wg_format *format)
516 VIDEOINFO *video_format;
517 uint32_t frame_time;
519 if (!(video_format = CoTaskMemAlloc(sizeof(*video_format))))
520 return false;
522 mt->majortype = MEDIATYPE_Video;
523 mt->subtype = MEDIASUBTYPE_CVID;
524 mt->bTemporalCompression = TRUE;
525 mt->lSampleSize = 1;
526 mt->formattype = FORMAT_VideoInfo;
527 mt->cbFormat = sizeof(VIDEOINFOHEADER);
528 mt->pbFormat = (BYTE *)video_format;
530 memset(video_format, 0, sizeof(*video_format));
531 if ((frame_time = MulDiv(10000000, format->u.video_cinepak.fps_d, format->u.video_cinepak.fps_n)) != -1)
532 video_format->AvgTimePerFrame = frame_time;
533 video_format->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
534 video_format->bmiHeader.biWidth = format->u.video_cinepak.width;
535 video_format->bmiHeader.biHeight = format->u.video_cinepak.height;
536 video_format->bmiHeader.biPlanes = 1;
537 video_format->bmiHeader.biBitCount = 24;
538 video_format->bmiHeader.biCompression = mt->subtype.Data1;
539 video_format->bmiHeader.biSizeImage = wg_format_get_max_size(format);
541 return true;
544 static bool amt_from_wg_format_video_wmv(AM_MEDIA_TYPE *mt, const struct wg_format *format)
546 VIDEOINFO *video_format;
547 uint32_t frame_time;
548 const GUID *subtype;
550 if (!(video_format = CoTaskMemAlloc(sizeof(*video_format))))
551 return false;
553 switch (format->u.video_wmv.format)
555 case WG_WMV_VIDEO_FORMAT_WMV1:
556 subtype = &MEDIASUBTYPE_WMV1;
557 break;
558 case WG_WMV_VIDEO_FORMAT_WMV2:
559 subtype = &MEDIASUBTYPE_WMV2;
560 break;
561 case WG_WMV_VIDEO_FORMAT_WMV3:
562 subtype = &MEDIASUBTYPE_WMV3;
563 break;
564 case WG_WMV_VIDEO_FORMAT_WMVA:
565 subtype = &MEDIASUBTYPE_WMVA;
566 break;
567 case WG_WMV_VIDEO_FORMAT_WVC1:
568 subtype = &MEDIASUBTYPE_WVC1;
569 break;
570 default:
571 WARN("Invalid WMV format %u.\n", format->u.video_wmv.format);
572 return false;
575 mt->majortype = MEDIATYPE_Video;
576 mt->subtype = *subtype;
577 mt->bFixedSizeSamples = FALSE;
578 mt->bTemporalCompression = TRUE;
579 mt->lSampleSize = 0;
580 mt->formattype = FORMAT_VideoInfo;
581 mt->cbFormat = sizeof(VIDEOINFOHEADER);
582 mt->pbFormat = (BYTE *)video_format;
584 memset(video_format, 0, sizeof(*video_format));
585 SetRect(&video_format->rcSource, 0, 0, format->u.video_wmv.width, format->u.video_wmv.height);
586 video_format->rcTarget = video_format->rcSource;
587 if ((frame_time = MulDiv(10000000, format->u.video_wmv.fps_d, format->u.video_wmv.fps_n)) != -1)
588 video_format->AvgTimePerFrame = frame_time;
589 video_format->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
590 video_format->bmiHeader.biWidth = format->u.video_wmv.width;
591 video_format->bmiHeader.biHeight = format->u.video_wmv.height;
592 video_format->bmiHeader.biPlanes = 1;
593 video_format->bmiHeader.biCompression = mt->subtype.Data1;
595 return true;
598 bool amt_from_wg_format(AM_MEDIA_TYPE *mt, const struct wg_format *format, bool wm)
600 memset(mt, 0, sizeof(*mt));
602 switch (format->major_type)
604 case WG_MAJOR_TYPE_AUDIO_MPEG4:
605 case WG_MAJOR_TYPE_AUDIO_WMA:
606 case WG_MAJOR_TYPE_VIDEO_H264:
607 case WG_MAJOR_TYPE_VIDEO_INDEO:
608 FIXME("Format %u not implemented!\n", format->major_type);
609 /* fallthrough */
610 case WG_MAJOR_TYPE_UNKNOWN:
611 return false;
613 case WG_MAJOR_TYPE_AUDIO:
614 return amt_from_wg_format_audio(mt, format);
616 case WG_MAJOR_TYPE_AUDIO_MPEG1:
617 return amt_from_wg_format_audio_mpeg1(mt, format);
619 case WG_MAJOR_TYPE_VIDEO:
620 return amt_from_wg_format_video(mt, format, wm);
622 case WG_MAJOR_TYPE_VIDEO_CINEPAK:
623 return amt_from_wg_format_video_cinepak(mt, format);
625 case WG_MAJOR_TYPE_VIDEO_WMV:
626 return amt_from_wg_format_video_wmv(mt, format);
629 assert(0);
630 return false;
633 static bool amt_to_wg_format_audio(const AM_MEDIA_TYPE *mt, struct wg_format *format)
635 static const struct
637 const GUID *subtype;
638 WORD depth;
639 enum wg_audio_format format;
641 format_map[] =
643 {&MEDIASUBTYPE_PCM, 8, WG_AUDIO_FORMAT_U8},
644 {&MEDIASUBTYPE_PCM, 16, WG_AUDIO_FORMAT_S16LE},
645 {&MEDIASUBTYPE_PCM, 24, WG_AUDIO_FORMAT_S24LE},
646 {&MEDIASUBTYPE_PCM, 32, WG_AUDIO_FORMAT_S32LE},
647 {&MEDIASUBTYPE_IEEE_FLOAT, 32, WG_AUDIO_FORMAT_F32LE},
648 {&MEDIASUBTYPE_IEEE_FLOAT, 64, WG_AUDIO_FORMAT_F64LE},
651 const WAVEFORMATEX *audio_format = (const WAVEFORMATEX *)mt->pbFormat;
652 unsigned int i;
654 if (!IsEqualGUID(&mt->formattype, &FORMAT_WaveFormatEx))
656 FIXME("Unknown format type %s.\n", debugstr_guid(&mt->formattype));
657 return false;
659 if (mt->cbFormat < sizeof(WAVEFORMATEX) || !mt->pbFormat)
661 ERR("Unexpected format size %lu.\n", mt->cbFormat);
662 return false;
665 format->major_type = WG_MAJOR_TYPE_AUDIO;
666 format->u.audio.channels = audio_format->nChannels;
667 format->u.audio.rate = audio_format->nSamplesPerSec;
669 if (audio_format->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
671 const WAVEFORMATEXTENSIBLE *ext_format = (const WAVEFORMATEXTENSIBLE *)mt->pbFormat;
673 format->u.audio.channel_mask = ext_format->dwChannelMask;
675 else
677 if (audio_format->nChannels == 1)
678 format->u.audio.channel_mask = KSAUDIO_SPEAKER_MONO;
679 else if (audio_format->nChannels == 2)
680 format->u.audio.channel_mask = KSAUDIO_SPEAKER_STEREO;
681 else
683 ERR("Unexpected channel count %u.\n", audio_format->nChannels);
684 return false;
688 for (i = 0; i < ARRAY_SIZE(format_map); ++i)
690 if (IsEqualGUID(&mt->subtype, format_map[i].subtype)
691 && audio_format->wBitsPerSample == format_map[i].depth)
693 format->u.audio.format = format_map[i].format;
694 return true;
698 FIXME("Unknown subtype %s, depth %u.\n", debugstr_guid(&mt->subtype), audio_format->wBitsPerSample);
699 return false;
702 static bool amt_to_wg_format_audio_mpeg1(const AM_MEDIA_TYPE *mt, struct wg_format *format)
704 const MPEG1WAVEFORMAT *audio_format = (const MPEG1WAVEFORMAT *)mt->pbFormat;
706 if (!IsEqualGUID(&mt->formattype, &FORMAT_WaveFormatEx))
708 FIXME("Unknown format type %s.\n", debugstr_guid(&mt->formattype));
709 return false;
711 if (mt->cbFormat < sizeof(*audio_format) || !mt->pbFormat)
713 ERR("Unexpected format size %lu.\n", mt->cbFormat);
714 return false;
717 format->major_type = WG_MAJOR_TYPE_AUDIO_MPEG1;
718 format->u.audio_mpeg1.channels = audio_format->wfx.nChannels;
719 format->u.audio_mpeg1.rate = audio_format->wfx.nSamplesPerSec;
720 format->u.audio_mpeg1.layer = audio_format->fwHeadLayer;
721 return true;
724 static bool amt_to_wg_format_audio_mpeg1_layer3(const AM_MEDIA_TYPE *mt, struct wg_format *format)
726 const MPEGLAYER3WAVEFORMAT *audio_format = (const MPEGLAYER3WAVEFORMAT *)mt->pbFormat;
728 if (!IsEqualGUID(&mt->formattype, &FORMAT_WaveFormatEx))
730 FIXME("Unknown format type %s.\n", debugstr_guid(&mt->formattype));
731 return false;
733 if (mt->cbFormat < sizeof(*audio_format) || !mt->pbFormat)
735 ERR("Unexpected format size %lu.\n", mt->cbFormat);
736 return false;
739 format->major_type = WG_MAJOR_TYPE_AUDIO_MPEG1;
740 format->u.audio_mpeg1.channels = audio_format->wfx.nChannels;
741 format->u.audio_mpeg1.rate = audio_format->wfx.nSamplesPerSec;
742 format->u.audio_mpeg1.layer = 3;
743 return true;
746 static bool amt_to_wg_format_video(const AM_MEDIA_TYPE *mt, struct wg_format *format)
748 static const struct
750 const GUID *subtype;
751 enum wg_video_format format;
753 format_map[] =
755 {&MEDIASUBTYPE_ARGB32, WG_VIDEO_FORMAT_BGRA},
756 {&MEDIASUBTYPE_RGB32, WG_VIDEO_FORMAT_BGRx},
757 {&MEDIASUBTYPE_RGB24, WG_VIDEO_FORMAT_BGR},
758 {&MEDIASUBTYPE_RGB555, WG_VIDEO_FORMAT_RGB15},
759 {&MEDIASUBTYPE_RGB565, WG_VIDEO_FORMAT_RGB16},
760 {&MEDIASUBTYPE_AYUV, WG_VIDEO_FORMAT_AYUV},
761 {&MEDIASUBTYPE_I420, WG_VIDEO_FORMAT_I420},
762 {&MEDIASUBTYPE_NV12, WG_VIDEO_FORMAT_NV12},
763 {&MEDIASUBTYPE_UYVY, WG_VIDEO_FORMAT_UYVY},
764 {&MEDIASUBTYPE_YUY2, WG_VIDEO_FORMAT_YUY2},
765 {&MEDIASUBTYPE_YV12, WG_VIDEO_FORMAT_YV12},
766 {&MEDIASUBTYPE_YVYU, WG_VIDEO_FORMAT_YVYU},
769 const VIDEOINFOHEADER *video_format = (const VIDEOINFOHEADER *)mt->pbFormat;
770 unsigned int i;
772 if (!IsEqualGUID(&mt->formattype, &FORMAT_VideoInfo))
774 FIXME("Unknown format type %s.\n", debugstr_guid(&mt->formattype));
775 return false;
777 if (mt->cbFormat < sizeof(VIDEOINFOHEADER) || !mt->pbFormat)
779 ERR("Unexpected format size %lu.\n", mt->cbFormat);
780 return false;
783 format->major_type = WG_MAJOR_TYPE_VIDEO;
784 format->u.video.width = video_format->bmiHeader.biWidth;
785 format->u.video.height = video_format->bmiHeader.biHeight;
786 format->u.video.fps_n = 10000000;
787 format->u.video.fps_d = video_format->AvgTimePerFrame;
789 for (i = 0; i < ARRAY_SIZE(format_map); ++i)
791 if (IsEqualGUID(&mt->subtype, format_map[i].subtype))
793 format->u.video.format = format_map[i].format;
794 if (wg_video_format_is_rgb(format->u.video.format))
795 format->u.video.height = -format->u.video.height;
796 return true;
800 FIXME("Unknown subtype %s.\n", debugstr_guid(&mt->subtype));
801 return false;
804 static bool amt_to_wg_format_video_wmv(const AM_MEDIA_TYPE *mt, struct wg_format *format)
806 const VIDEOINFOHEADER *video_format = (const VIDEOINFOHEADER *)mt->pbFormat;
808 if (!IsEqualGUID(&mt->formattype, &FORMAT_VideoInfo))
810 FIXME("Unknown format type %s.\n", debugstr_guid(&mt->formattype));
811 return false;
813 if (mt->cbFormat < sizeof(VIDEOINFOHEADER) || !mt->pbFormat)
815 ERR("Unexpected format size %lu.\n", mt->cbFormat);
816 return false;
819 format->major_type = WG_MAJOR_TYPE_VIDEO_WMV;
820 format->u.video_wmv.width = video_format->bmiHeader.biWidth;
821 format->u.video_wmv.height = video_format->bmiHeader.biHeight;
822 format->u.video_wmv.fps_n = 10000000;
823 format->u.video_wmv.fps_d = video_format->AvgTimePerFrame;
825 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV1))
826 format->u.video_wmv.format = WG_WMV_VIDEO_FORMAT_WMV1;
827 else if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV2))
828 format->u.video_wmv.format = WG_WMV_VIDEO_FORMAT_WMV2;
829 else if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV3))
830 format->u.video_wmv.format = WG_WMV_VIDEO_FORMAT_WMV3;
831 else if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMVA))
832 format->u.video_wmv.format = WG_WMV_VIDEO_FORMAT_WMVA;
833 else if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WVC1))
834 format->u.video_wmv.format = WG_WMV_VIDEO_FORMAT_WVC1;
835 else
836 format->u.video_wmv.format = WG_WMV_VIDEO_FORMAT_UNKNOWN;
838 return true;
841 bool amt_to_wg_format(const AM_MEDIA_TYPE *mt, struct wg_format *format)
843 memset(format, 0, sizeof(*format));
845 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Video))
847 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV1)
848 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV2)
849 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMVA)
850 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMVP)
851 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WVP2)
852 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV_Unknown)
853 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WVC1)
854 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV3)
855 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_VC1S))
856 return amt_to_wg_format_video_wmv(mt, format);
857 return amt_to_wg_format_video(mt, format);
859 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Audio))
861 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1AudioPayload))
862 return amt_to_wg_format_audio_mpeg1(mt, format);
863 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MP3))
864 return amt_to_wg_format_audio_mpeg1_layer3(mt, format);
865 return amt_to_wg_format_audio(mt, format);
868 FIXME("Unknown major type %s.\n", debugstr_guid(&mt->majortype));
869 return false;
873 * scale_uint64() is based on gst_util_scale_int() from GStreamer, which is
874 * covered by the following license:
876 * GStreamer
877 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
878 * 2000 Wim Taymans <wtay@chello.be>
879 * 2002 Thomas Vander Stichele <thomas@apestaart.org>
880 * 2004 Wim Taymans <wim@fluendo.com>
881 * 2015 Jan Schmidt <jan@centricular.com>
883 * gstutils.c: Utility functions
885 * This library is free software; you can redistribute it and/or
886 * modify it under the terms of the GNU Library General Public
887 * License as published by the Free Software Foundation; either
888 * version 2 of the License, or (at your option) any later version.
890 * This library is distributed in the hope that it will be useful,
891 * but WITHOUT ANY WARRANTY; without even the implied warranty of
892 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
893 * Library General Public License for more details.
895 * You should have received a copy of the GNU Library General Public
896 * License along with this library; if not, write to the
897 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
898 * Boston, MA 02110-1301, USA.
900 static uint64_t scale_uint64(uint64_t value, uint32_t numerator, uint32_t denominator)
902 ULARGE_INTEGER i, high, low;
904 if (!value)
905 return 0;
907 i.QuadPart = value;
908 low.QuadPart = (ULONGLONG)i.u.LowPart * numerator;
909 high.QuadPart = (ULONGLONG)i.u.HighPart * numerator + low.u.HighPart;
910 low.u.HighPart = 0;
912 if (high.u.HighPart >= denominator)
913 return ULLONG_MAX;
915 low.QuadPart += (high.QuadPart % denominator) << 32;
916 return ((high.QuadPart / denominator) << 32) + (low.QuadPart / denominator);
919 /* Fill and send a single IMediaSample. */
920 static HRESULT send_sample(struct parser_source *pin, IMediaSample *sample,
921 const struct wg_parser_buffer *buffer, uint32_t offset, uint32_t size, DWORD bytes_per_second)
923 HRESULT hr;
924 BYTE *ptr = NULL;
926 TRACE("offset %u, size %u, sample size %lu.\n", offset, size, IMediaSample_GetSize(sample));
928 hr = IMediaSample_SetActualDataLength(sample, size);
929 if(FAILED(hr)){
930 ERR("Failed to set sample size, hr %#lx.\n", hr);
931 return hr;
934 IMediaSample_GetPointer(sample, &ptr);
936 if (!wg_parser_stream_copy_buffer(pin->wg_stream, ptr, offset, size))
938 /* The GStreamer pin has been flushed. */
939 return S_OK;
942 if (buffer->has_pts)
944 REFERENCE_TIME start_pts = buffer->pts;
946 if (offset)
947 start_pts += scale_uint64(offset, 10000000, bytes_per_second);
948 start_pts -= pin->seek.llCurrent;
949 start_pts *= pin->seek.dRate;
951 if (buffer->has_duration)
953 REFERENCE_TIME end_pts = buffer->pts + buffer->duration;
955 if (offset + size < buffer->size)
956 end_pts = buffer->pts + scale_uint64(offset + size, 10000000, bytes_per_second);
957 end_pts -= pin->seek.llCurrent;
958 end_pts *= pin->seek.dRate;
960 IMediaSample_SetTime(sample, &start_pts, &end_pts);
961 IMediaSample_SetMediaTime(sample, &start_pts, &end_pts);
963 else
965 IMediaSample_SetTime(sample, &start_pts, NULL);
966 IMediaSample_SetMediaTime(sample, NULL, NULL);
969 else
971 IMediaSample_SetTime(sample, NULL, NULL);
972 IMediaSample_SetMediaTime(sample, NULL, NULL);
975 IMediaSample_SetDiscontinuity(sample, !offset && buffer->discontinuity);
976 IMediaSample_SetPreroll(sample, buffer->preroll);
977 IMediaSample_SetSyncPoint(sample, !buffer->delta);
979 if (!pin->pin.pin.peer)
980 return VFW_E_NOT_CONNECTED;
982 hr = IMemInputPin_Receive(pin->pin.pMemInputPin, sample);
983 TRACE("Receive() returned hr %#lx.\n", hr);
984 return hr;
987 /* Send a single GStreamer buffer (splitting it into multiple IMediaSamples if
988 * necessary). */
989 static void send_buffer(struct parser_source *pin, const struct wg_parser_buffer *buffer)
991 HRESULT hr;
992 IMediaSample *sample;
994 if (pin->need_segment)
996 if (FAILED(hr = IPin_NewSegment(pin->pin.pin.peer,
997 pin->seek.llCurrent, pin->seek.llStop, pin->seek.dRate)))
998 WARN("Failed to deliver new segment, hr %#lx.\n", hr);
999 pin->need_segment = false;
1002 if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_WaveFormatEx)
1003 && (IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_PCM)
1004 || IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_IEEE_FLOAT)))
1006 WAVEFORMATEX *format = (WAVEFORMATEX *)pin->pin.pin.mt.pbFormat;
1007 uint32_t offset = 0;
1009 while (offset < buffer->size)
1011 uint32_t advance;
1013 if (FAILED(hr = IMemAllocator_GetBuffer(pin->pin.pAllocator, &sample, NULL, NULL, 0)))
1015 ERR("Failed to get a sample, hr %#lx.\n", hr);
1016 break;
1019 advance = min(IMediaSample_GetSize(sample), buffer->size - offset);
1021 hr = send_sample(pin, sample, buffer, offset, advance, format->nAvgBytesPerSec);
1023 IMediaSample_Release(sample);
1025 if (FAILED(hr))
1026 break;
1028 offset += advance;
1031 else
1033 if (FAILED(hr = IMemAllocator_GetBuffer(pin->pin.pAllocator, &sample, NULL, NULL, 0)))
1035 ERR("Failed to get a sample, hr %#lx.\n", hr);
1037 else
1039 hr = send_sample(pin, sample, buffer, 0, buffer->size, 0);
1041 IMediaSample_Release(sample);
1045 wg_parser_stream_release_buffer(pin->wg_stream);
1048 static DWORD CALLBACK stream_thread(void *arg)
1050 struct parser_source *pin = arg;
1051 struct parser *filter = impl_from_strmbase_filter(pin->pin.pin.filter);
1053 TRACE("Starting streaming thread for pin %p.\n", pin);
1055 for (;;)
1057 struct wg_parser_buffer buffer;
1059 EnterCriticalSection(&filter->streaming_cs);
1061 while (filter->flushing)
1062 SleepConditionVariableCS(&filter->flushing_cv, &filter->streaming_cs, INFINITE);
1064 if (!filter->streaming)
1066 LeaveCriticalSection(&filter->streaming_cs);
1067 break;
1070 LeaveCriticalSection(&filter->streaming_cs);
1072 EnterCriticalSection(&pin->flushing_cs);
1074 if (pin->eos)
1076 SleepConditionVariableCS(&pin->eos_cv, &pin->flushing_cs, INFINITE);
1077 LeaveCriticalSection(&pin->flushing_cs);
1078 continue;
1081 if (wg_parser_stream_get_buffer(filter->wg_parser, pin->wg_stream, &buffer))
1083 send_buffer(pin, &buffer);
1085 else
1087 TRACE("Got EOS.\n");
1088 IPin_EndOfStream(pin->pin.pin.peer);
1089 pin->eos = true;
1092 LeaveCriticalSection(&pin->flushing_cs);
1095 TRACE("Streaming stopped; exiting.\n");
1096 return 0;
1099 static DWORD CALLBACK read_thread(void *arg)
1101 struct parser *filter = arg;
1102 LONGLONG file_size, unused;
1103 size_t buffer_size = 4096;
1104 void *data = NULL;
1106 if (!(data = malloc(buffer_size)))
1107 return 0;
1109 IAsyncReader_Length(filter->reader, &file_size, &unused);
1111 TRACE("Starting read thread for filter %p.\n", filter);
1113 while (filter->sink_connected)
1115 uint64_t offset;
1116 uint32_t size;
1117 HRESULT hr;
1119 if (!wg_parser_get_next_read_offset(filter->wg_parser, &offset, &size))
1120 continue;
1122 if (offset >= file_size)
1123 size = 0;
1124 else if (offset + size >= file_size)
1125 size = file_size - offset;
1127 if (!array_reserve(&data, &buffer_size, size, 1))
1129 free(data);
1130 return 0;
1133 hr = IAsyncReader_SyncRead(filter->reader, offset, size, data);
1134 if (FAILED(hr))
1135 ERR("Failed to read %u bytes at offset %I64u, hr %#lx.\n", size, offset, hr);
1137 wg_parser_push_data(filter->wg_parser, SUCCEEDED(hr) ? data : NULL, size);
1140 free(data);
1141 TRACE("Streaming stopped; exiting.\n");
1142 return 0;
1145 static inline struct parser_source *impl_from_IMediaSeeking(IMediaSeeking *iface)
1147 return CONTAINING_RECORD(iface, struct parser_source, seek.IMediaSeeking_iface);
1150 static struct strmbase_pin *parser_get_pin(struct strmbase_filter *base, unsigned int index)
1152 struct parser *filter = impl_from_strmbase_filter(base);
1154 if (filter->enum_sink_first)
1156 if (!index)
1157 return &filter->sink.pin;
1158 else if (index <= filter->source_count)
1159 return &filter->sources[index - 1]->pin.pin;
1161 else
1163 if (index < filter->source_count)
1164 return &filter->sources[index]->pin.pin;
1165 else if (index == filter->source_count)
1166 return &filter->sink.pin;
1168 return NULL;
1171 static void parser_destroy(struct strmbase_filter *iface)
1173 struct parser *filter = impl_from_strmbase_filter(iface);
1174 HRESULT hr;
1176 /* Don't need to clean up output pins, disconnecting input pin will do that */
1177 if (filter->sink.pin.peer)
1179 hr = IPin_Disconnect(filter->sink.pin.peer);
1180 assert(hr == S_OK);
1181 hr = IPin_Disconnect(&filter->sink.pin.IPin_iface);
1182 assert(hr == S_OK);
1185 if (filter->reader)
1186 IAsyncReader_Release(filter->reader);
1187 filter->reader = NULL;
1189 wg_parser_destroy(filter->wg_parser);
1191 filter->streaming_cs.DebugInfo->Spare[0] = 0;
1192 DeleteCriticalSection(&filter->streaming_cs);
1194 strmbase_sink_cleanup(&filter->sink);
1195 strmbase_filter_cleanup(&filter->filter);
1196 free(filter);
1199 static HRESULT parser_init_stream(struct strmbase_filter *iface)
1201 struct parser *filter = impl_from_strmbase_filter(iface);
1202 DWORD stop_flags = AM_SEEKING_NoPositioning;
1203 const SourceSeeking *seeking;
1204 unsigned int i;
1206 if (!filter->sink_connected)
1207 return S_OK;
1209 filter->streaming = true;
1211 for (i = 0; i < filter->source_count; ++i)
1213 struct parser_source *source = filter->sources[i];
1214 struct wg_format format;
1215 bool ret;
1217 if (source->pin.pin.peer)
1219 ret = amt_to_wg_format(&source->pin.pin.mt, &format);
1220 assert(ret);
1221 wg_parser_stream_enable(source->wg_stream, &format);
1223 else
1225 wg_parser_stream_disable(source->wg_stream);
1229 /* DirectShow retains the old seek positions, but resets to them every time
1230 * it transitions from stopped -> paused. */
1232 seeking = &filter->sources[0]->seek;
1233 if (seeking->llStop)
1234 stop_flags = AM_SEEKING_AbsolutePositioning;
1235 wg_parser_stream_seek(filter->sources[0]->wg_stream, seeking->dRate,
1236 seeking->llCurrent, seeking->llStop, AM_SEEKING_AbsolutePositioning, stop_flags);
1238 for (i = 0; i < filter->source_count; ++i)
1240 struct parser_source *pin = filter->sources[i];
1241 HRESULT hr;
1243 if (!pin->pin.pin.peer)
1244 continue;
1246 if (FAILED(hr = IMemAllocator_Commit(pin->pin.pAllocator)))
1247 ERR("Failed to commit allocator, hr %#lx.\n", hr);
1249 pin->need_segment = true;
1250 pin->eos = false;
1252 pin->thread = CreateThread(NULL, 0, stream_thread, pin, 0, NULL);
1255 return S_OK;
1258 static HRESULT parser_cleanup_stream(struct strmbase_filter *iface)
1260 struct parser *filter = impl_from_strmbase_filter(iface);
1261 unsigned int i;
1263 if (!filter->sink_connected)
1264 return S_OK;
1266 EnterCriticalSection(&filter->streaming_cs);
1267 filter->streaming = false;
1268 LeaveCriticalSection(&filter->streaming_cs);
1270 for (i = 0; i < filter->source_count; ++i)
1272 struct parser_source *pin = filter->sources[i];
1274 if (!pin->pin.pin.peer)
1275 continue;
1277 IMemAllocator_Decommit(pin->pin.pAllocator);
1279 WakeConditionVariable(&pin->eos_cv);
1280 WaitForSingleObject(pin->thread, INFINITE);
1281 CloseHandle(pin->thread);
1282 pin->thread = NULL;
1285 return S_OK;
1288 static const struct strmbase_filter_ops filter_ops =
1290 .filter_get_pin = parser_get_pin,
1291 .filter_destroy = parser_destroy,
1292 .filter_init_stream = parser_init_stream,
1293 .filter_cleanup_stream = parser_cleanup_stream,
1296 static inline struct parser *impl_from_strmbase_sink(struct strmbase_sink *iface)
1298 return CONTAINING_RECORD(iface, struct parser, sink);
1301 static HRESULT sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
1303 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
1304 return S_OK;
1305 return S_FALSE;
1308 static HRESULT parser_sink_connect(struct strmbase_sink *iface, IPin *peer, const AM_MEDIA_TYPE *pmt)
1310 struct parser *filter = impl_from_strmbase_sink(iface);
1311 LONGLONG file_size, unused;
1312 HRESULT hr = S_OK;
1313 unsigned int i;
1315 filter->reader = NULL;
1316 if (FAILED(hr = IPin_QueryInterface(peer, &IID_IAsyncReader, (void **)&filter->reader)))
1317 return hr;
1319 IAsyncReader_Length(filter->reader, &file_size, &unused);
1321 filter->sink_connected = true;
1322 filter->read_thread = CreateThread(NULL, 0, read_thread, filter, 0, NULL);
1324 if (FAILED(hr = wg_parser_connect(filter->wg_parser, file_size)))
1325 goto err;
1327 if (!filter->init_gst(filter))
1329 hr = E_FAIL;
1330 goto err;
1333 for (i = 0; i < filter->source_count; ++i)
1335 struct parser_source *pin = filter->sources[i];
1337 pin->seek.llDuration = pin->seek.llStop = wg_parser_stream_get_duration(pin->wg_stream);
1338 pin->seek.llCurrent = 0;
1341 return S_OK;
1342 err:
1343 GST_RemoveOutputPins(filter);
1344 IAsyncReader_Release(filter->reader);
1345 filter->reader = NULL;
1346 return hr;
1349 static void parser_sink_disconnect(struct strmbase_sink *iface)
1351 struct parser *filter = impl_from_strmbase_sink(iface);
1353 GST_RemoveOutputPins(filter);
1355 IAsyncReader_Release(filter->reader);
1356 filter->reader = NULL;
1359 static const struct strmbase_sink_ops sink_ops =
1361 .base.pin_query_accept = sink_query_accept,
1362 .sink_connect = parser_sink_connect,
1363 .sink_disconnect = parser_sink_disconnect,
1366 static BOOL decodebin_parser_filter_init_gst(struct parser *filter)
1368 wg_parser_t parser = filter->wg_parser;
1369 unsigned int i, stream_count;
1370 WCHAR source_name[20];
1372 stream_count = wg_parser_get_stream_count(parser);
1373 for (i = 0; i < stream_count; ++i)
1375 swprintf(source_name, ARRAY_SIZE(source_name), L"Stream %02u", i);
1376 if (!create_pin(filter, wg_parser_get_stream(parser, i), source_name))
1377 return FALSE;
1380 return TRUE;
1383 static HRESULT decodebin_parser_source_query_accept(struct parser_source *pin, const AM_MEDIA_TYPE *mt)
1385 struct wg_format format;
1387 /* At least make sure we can convert it to wg_format. */
1388 return amt_to_wg_format(mt, &format) ? S_OK : S_FALSE;
1391 static HRESULT decodebin_parser_source_get_media_type(struct parser_source *pin,
1392 unsigned int index, AM_MEDIA_TYPE *mt)
1394 struct wg_format format;
1396 static const enum wg_video_format video_formats[] =
1398 /* Try to prefer YUV formats over RGB ones. Most decoders output in the
1399 * YUV color space, and it's generally much less expensive for
1400 * videoconvert to do YUV -> YUV transformations. */
1401 WG_VIDEO_FORMAT_AYUV,
1402 WG_VIDEO_FORMAT_I420,
1403 WG_VIDEO_FORMAT_YV12,
1404 WG_VIDEO_FORMAT_YUY2,
1405 WG_VIDEO_FORMAT_UYVY,
1406 WG_VIDEO_FORMAT_YVYU,
1407 WG_VIDEO_FORMAT_NV12,
1408 WG_VIDEO_FORMAT_BGRA,
1409 WG_VIDEO_FORMAT_BGRx,
1410 WG_VIDEO_FORMAT_BGR,
1411 WG_VIDEO_FORMAT_RGB16,
1412 WG_VIDEO_FORMAT_RGB15,
1415 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
1417 memset(mt, 0, sizeof(AM_MEDIA_TYPE));
1419 if (amt_from_wg_format(mt, &format, false))
1421 if (!index--)
1422 return S_OK;
1423 FreeMediaType(mt);
1426 if (format.major_type == WG_MAJOR_TYPE_VIDEO && index < ARRAY_SIZE(video_formats))
1428 format.u.video.format = video_formats[index];
1429 /* Downstream filters probably expect RGB video to be bottom-up. */
1430 if (format.u.video.height > 0 && wg_video_format_is_rgb(video_formats[index]))
1431 format.u.video.height = -format.u.video.height;
1432 if (!amt_from_wg_format(mt, &format, false))
1433 return E_OUTOFMEMORY;
1434 return S_OK;
1436 else if (format.major_type == WG_MAJOR_TYPE_AUDIO && !index)
1438 format.u.audio.format = WG_AUDIO_FORMAT_S16LE;
1439 if (!amt_from_wg_format(mt, &format, false))
1440 return E_OUTOFMEMORY;
1441 return S_OK;
1444 return VFW_S_NO_MORE_ITEMS;
1447 static HRESULT parser_create(enum wg_parser_type type, struct parser **parser)
1449 struct parser *object;
1451 if (!(object = calloc(1, sizeof(*object))))
1452 return E_OUTOFMEMORY;
1454 if (!(object->wg_parser = wg_parser_create(type)))
1456 free(object);
1457 return E_OUTOFMEMORY;
1460 InitializeCriticalSection(&object->streaming_cs);
1461 object->streaming_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": parser.streaming_cs");
1463 InitializeConditionVariable(&object->flushing_cv);
1465 *parser = object;
1466 return S_OK;
1469 HRESULT decodebin_parser_create(IUnknown *outer, IUnknown **out)
1471 struct parser *object;
1472 HRESULT hr;
1474 if (FAILED(hr = parser_create(WG_PARSER_DECODEBIN, &object)))
1475 return hr;
1477 strmbase_filter_init(&object->filter, outer, &CLSID_decodebin_parser, &filter_ops);
1478 strmbase_sink_init(&object->sink, &object->filter, L"input pin", &sink_ops, NULL);
1480 object->init_gst = decodebin_parser_filter_init_gst;
1481 object->source_query_accept = decodebin_parser_source_query_accept;
1482 object->source_get_media_type = decodebin_parser_source_get_media_type;
1484 TRACE("Created GStreamer demuxer %p.\n", object);
1485 *out = &object->filter.IUnknown_inner;
1486 return S_OK;
1489 static struct parser *impl_from_IAMStreamSelect(IAMStreamSelect *iface)
1491 return CONTAINING_RECORD(iface, struct parser, IAMStreamSelect_iface);
1494 static HRESULT WINAPI stream_select_QueryInterface(IAMStreamSelect *iface, REFIID iid, void **out)
1496 struct parser *filter = impl_from_IAMStreamSelect(iface);
1497 return IUnknown_QueryInterface(filter->filter.outer_unk, iid, out);
1500 static ULONG WINAPI stream_select_AddRef(IAMStreamSelect *iface)
1502 struct parser *filter = impl_from_IAMStreamSelect(iface);
1503 return IUnknown_AddRef(filter->filter.outer_unk);
1506 static ULONG WINAPI stream_select_Release(IAMStreamSelect *iface)
1508 struct parser *filter = impl_from_IAMStreamSelect(iface);
1509 return IUnknown_Release(filter->filter.outer_unk);
1512 static HRESULT WINAPI stream_select_Count(IAMStreamSelect *iface, DWORD *count)
1514 FIXME("iface %p, count %p, stub!\n", iface, count);
1515 return E_NOTIMPL;
1518 static HRESULT WINAPI stream_select_Info(IAMStreamSelect *iface, LONG index,
1519 AM_MEDIA_TYPE **mt, DWORD *flags, LCID *lcid, DWORD *group, WCHAR **name,
1520 IUnknown **object, IUnknown **unknown)
1522 FIXME("iface %p, index %ld, mt %p, flags %p, lcid %p, group %p, name %p, object %p, unknown %p, stub!\n",
1523 iface, index, mt, flags, lcid, group, name, object, unknown);
1524 return E_NOTIMPL;
1527 static HRESULT WINAPI stream_select_Enable(IAMStreamSelect *iface, LONG index, DWORD flags)
1529 FIXME("iface %p, index %ld, flags %#lx, stub!\n", iface, index, flags);
1530 return E_NOTIMPL;
1533 static const IAMStreamSelectVtbl stream_select_vtbl =
1535 stream_select_QueryInterface,
1536 stream_select_AddRef,
1537 stream_select_Release,
1538 stream_select_Count,
1539 stream_select_Info,
1540 stream_select_Enable,
1543 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface)
1545 struct parser_source *This = impl_from_IMediaSeeking(iface);
1546 TRACE("(%p)\n", This);
1547 return S_OK;
1550 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface)
1552 struct parser_source *This = impl_from_IMediaSeeking(iface);
1553 TRACE("(%p)\n", This);
1554 return S_OK;
1557 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface)
1559 struct parser_source *pin = impl_from_IMediaSeeking(iface);
1561 wg_parser_stream_seek(pin->wg_stream, pin->seek.dRate, 0, 0,
1562 AM_SEEKING_NoPositioning, AM_SEEKING_NoPositioning);
1563 return S_OK;
1566 static HRESULT WINAPI GST_Seeking_QueryInterface(IMediaSeeking *iface, REFIID riid, void **ppv)
1568 struct parser_source *This = impl_from_IMediaSeeking(iface);
1569 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
1572 static ULONG WINAPI GST_Seeking_AddRef(IMediaSeeking *iface)
1574 struct parser_source *This = impl_from_IMediaSeeking(iface);
1575 return IPin_AddRef(&This->pin.pin.IPin_iface);
1578 static ULONG WINAPI GST_Seeking_Release(IMediaSeeking *iface)
1580 struct parser_source *This = impl_from_IMediaSeeking(iface);
1581 return IPin_Release(&This->pin.pin.IPin_iface);
1584 static HRESULT WINAPI GST_Seeking_SetPositions(IMediaSeeking *iface,
1585 LONGLONG *current, DWORD current_flags, LONGLONG *stop, DWORD stop_flags)
1587 struct parser_source *pin = impl_from_IMediaSeeking(iface);
1588 struct parser *filter = impl_from_strmbase_filter(pin->pin.pin.filter);
1589 int i;
1591 TRACE("pin %p, current %s, current_flags %#lx, stop %s, stop_flags %#lx.\n",
1592 pin, current ? debugstr_time(*current) : "<null>", current_flags,
1593 stop ? debugstr_time(*stop) : "<null>", stop_flags);
1595 if (pin->pin.pin.filter->state == State_Stopped)
1597 SourceSeekingImpl_SetPositions(iface, current, current_flags, stop, stop_flags);
1598 return S_OK;
1601 if (!(current_flags & AM_SEEKING_NoFlush))
1603 for (i = 0; i < filter->source_count; ++i)
1605 if (filter->sources[i]->pin.pin.peer)
1606 IPin_BeginFlush(filter->sources[i]->pin.pin.peer);
1609 if (filter->reader)
1610 IAsyncReader_BeginFlush(filter->reader);
1613 /* Signal the streaming threads to "pause". */
1614 EnterCriticalSection(&filter->streaming_cs);
1615 filter->flushing = true;
1616 LeaveCriticalSection(&filter->streaming_cs);
1618 /* Acquire the flushing locks, to make sure the streaming threads really
1619 * are paused. This ensures the seek is serialized between flushes. */
1620 for (i = 0; i < filter->source_count; ++i)
1622 struct parser_source *flush_pin = filter->sources[i];
1624 if (flush_pin->pin.pin.peer)
1625 EnterCriticalSection(&flush_pin->flushing_cs);
1628 SourceSeekingImpl_SetPositions(iface, current, current_flags, stop, stop_flags);
1630 wg_parser_stream_seek(pin->wg_stream, pin->seek.dRate,
1631 pin->seek.llCurrent, pin->seek.llStop, current_flags, stop_flags);
1633 if (!(current_flags & AM_SEEKING_NoFlush))
1635 for (i = 0; i < filter->source_count; ++i)
1637 struct parser_source *flush_pin = filter->sources[i];
1639 if (flush_pin->pin.pin.peer)
1640 IPin_EndFlush(flush_pin->pin.pin.peer);
1643 if (filter->reader)
1644 IAsyncReader_EndFlush(filter->reader);
1647 /* Release the flushing locks. */
1648 for (i = filter->source_count - 1; i >= 0; --i)
1650 struct parser_source *flush_pin = filter->sources[i];
1652 flush_pin->need_segment = true;
1653 flush_pin->eos = false;
1655 if (flush_pin->pin.pin.peer)
1657 LeaveCriticalSection(&flush_pin->flushing_cs);
1658 WakeConditionVariable(&flush_pin->eos_cv);
1662 /* Signal the streaming threads to resume. */
1663 EnterCriticalSection(&filter->streaming_cs);
1664 filter->flushing = false;
1665 LeaveCriticalSection(&filter->streaming_cs);
1666 WakeConditionVariable(&filter->flushing_cv);
1668 return S_OK;
1671 static const IMediaSeekingVtbl GST_Seeking_Vtbl =
1673 GST_Seeking_QueryInterface,
1674 GST_Seeking_AddRef,
1675 GST_Seeking_Release,
1676 SourceSeekingImpl_GetCapabilities,
1677 SourceSeekingImpl_CheckCapabilities,
1678 SourceSeekingImpl_IsFormatSupported,
1679 SourceSeekingImpl_QueryPreferredFormat,
1680 SourceSeekingImpl_GetTimeFormat,
1681 SourceSeekingImpl_IsUsingTimeFormat,
1682 SourceSeekingImpl_SetTimeFormat,
1683 SourceSeekingImpl_GetDuration,
1684 SourceSeekingImpl_GetStopPosition,
1685 SourceSeekingImpl_GetCurrentPosition,
1686 SourceSeekingImpl_ConvertTimeFormat,
1687 GST_Seeking_SetPositions,
1688 SourceSeekingImpl_GetPositions,
1689 SourceSeekingImpl_GetAvailable,
1690 SourceSeekingImpl_SetRate,
1691 SourceSeekingImpl_GetRate,
1692 SourceSeekingImpl_GetPreroll
1695 static inline struct parser_source *impl_from_IQualityControl( IQualityControl *iface )
1697 return CONTAINING_RECORD(iface, struct parser_source, IQualityControl_iface);
1700 static HRESULT WINAPI GST_QualityControl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
1702 struct parser_source *pin = impl_from_IQualityControl(iface);
1703 return IPin_QueryInterface(&pin->pin.pin.IPin_iface, riid, ppv);
1706 static ULONG WINAPI GST_QualityControl_AddRef(IQualityControl *iface)
1708 struct parser_source *pin = impl_from_IQualityControl(iface);
1709 return IPin_AddRef(&pin->pin.pin.IPin_iface);
1712 static ULONG WINAPI GST_QualityControl_Release(IQualityControl *iface)
1714 struct parser_source *pin = impl_from_IQualityControl(iface);
1715 return IPin_Release(&pin->pin.pin.IPin_iface);
1718 static HRESULT WINAPI GST_QualityControl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality q)
1720 struct parser_source *pin = impl_from_IQualityControl(iface);
1721 uint64_t timestamp;
1722 int64_t diff;
1724 TRACE("pin %p, sender %p, type %s, proportion %ld, late %s, timestamp %s.\n",
1725 pin, sender, q.Type == Famine ? "Famine" : "Flood", q.Proportion,
1726 debugstr_time(q.Late), debugstr_time(q.TimeStamp));
1728 /* DirectShow filters sometimes pass negative timestamps (Audiosurf uses the
1729 * current time instead of the time of the last buffer). GstClockTime is
1730 * unsigned, so clamp it to 0. */
1731 timestamp = max(q.TimeStamp, 0);
1733 /* The documentation specifies that timestamp + diff must be nonnegative. */
1734 diff = q.Late;
1735 if (diff < 0 && timestamp < (uint64_t)-diff)
1736 diff = -timestamp;
1738 /* DirectShow "Proportion" describes what percentage of buffers the upstream
1739 * filter should keep (i.e. dropping the rest). If frames are late, the
1740 * proportion will be less than 1. For example, a proportion of 500 means
1741 * that the element should drop half of its frames, essentially because
1742 * frames are taking twice as long as they should to arrive.
1744 * GStreamer "proportion" is the inverse of this; it describes how much
1745 * faster the upstream element should produce frames. I.e. if frames are
1746 * taking twice as long as they should to arrive, we want the frames to be
1747 * decoded twice as fast, and so we pass 2.0 to GStreamer. */
1749 if (!q.Proportion)
1751 WARN("Ignoring quality message with zero proportion.\n");
1752 return S_OK;
1755 /* GST_QOS_TYPE_OVERFLOW is also used for buffers that arrive on time, but
1756 * DirectShow filters might use Famine, so check that there actually is an
1757 * underrun. */
1758 wg_parser_stream_notify_qos(pin->wg_stream, q.Type == Famine && q.Proportion < 1000,
1759 1000.0 / q.Proportion, diff, timestamp);
1761 return S_OK;
1764 static HRESULT WINAPI GST_QualityControl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
1766 struct parser_source *pin = impl_from_IQualityControl(iface);
1767 TRACE("(%p)->(%p)\n", pin, pin);
1768 /* Do nothing */
1769 return S_OK;
1772 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl = {
1773 GST_QualityControl_QueryInterface,
1774 GST_QualityControl_AddRef,
1775 GST_QualityControl_Release,
1776 GST_QualityControl_Notify,
1777 GST_QualityControl_SetSink
1780 static inline struct parser_source *impl_source_from_IPin(IPin *iface)
1782 return CONTAINING_RECORD(iface, struct parser_source, pin.pin.IPin_iface);
1785 static HRESULT source_query_interface(struct strmbase_pin *iface, REFIID iid, void **out)
1787 struct parser_source *pin = impl_source_from_IPin(&iface->IPin_iface);
1789 if (IsEqualGUID(iid, &IID_IMediaSeeking))
1790 *out = &pin->seek.IMediaSeeking_iface;
1791 else if (IsEqualGUID(iid, &IID_IQualityControl))
1792 *out = &pin->IQualityControl_iface;
1793 else
1794 return E_NOINTERFACE;
1796 IUnknown_AddRef((IUnknown *)*out);
1797 return S_OK;
1800 static HRESULT source_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
1802 struct parser_source *pin = impl_source_from_IPin(&iface->IPin_iface);
1803 struct parser *filter = impl_from_strmbase_filter(iface->filter);
1804 return filter->source_query_accept(pin, mt);
1807 static HRESULT source_get_media_type(struct strmbase_pin *iface, unsigned int index, AM_MEDIA_TYPE *mt)
1809 struct parser_source *pin = impl_source_from_IPin(&iface->IPin_iface);
1810 struct parser *filter = impl_from_strmbase_filter(iface->filter);
1811 return filter->source_get_media_type(pin, index, mt);
1814 static HRESULT WINAPI GSTOutPin_DecideBufferSize(struct strmbase_source *iface,
1815 IMemAllocator *allocator, ALLOCATOR_PROPERTIES *props)
1817 struct parser_source *pin = impl_source_from_IPin(&iface->pin.IPin_iface);
1818 unsigned int buffer_count = 1;
1819 unsigned int buffer_size = 16384;
1820 ALLOCATOR_PROPERTIES ret_props;
1822 if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_VideoInfo))
1824 VIDEOINFOHEADER *format = (VIDEOINFOHEADER *)pin->pin.pin.mt.pbFormat;
1825 buffer_size = format->bmiHeader.biSizeImage;
1827 else if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_WaveFormatEx)
1828 && (IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_PCM)
1829 || IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_IEEE_FLOAT)))
1831 WAVEFORMATEX *format = (WAVEFORMATEX *)pin->pin.pin.mt.pbFormat;
1832 buffer_size = format->nAvgBytesPerSec;
1834 else if (IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_MPEG1AudioPayload)
1835 || IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_MP3))
1837 /* mpg123audiodec requires at least 3 buffers as it will keep
1838 * references to the last 2 samples. */
1839 buffer_count = 3;
1842 /* We do need to drop any buffers that might have been sent with the old
1843 * caps, but this will be handled in parser_init_stream(). */
1845 props->cBuffers = max(props->cBuffers, buffer_count);
1846 props->cbBuffer = max(props->cbBuffer, buffer_size);
1847 props->cbAlign = max(props->cbAlign, 1);
1848 return IMemAllocator_SetProperties(allocator, props, &ret_props);
1851 static void free_source_pin(struct parser_source *pin)
1853 if (pin->pin.pin.peer)
1855 if (SUCCEEDED(IMemAllocator_Decommit(pin->pin.pAllocator)))
1856 IPin_Disconnect(pin->pin.pin.peer);
1857 IPin_Disconnect(&pin->pin.pin.IPin_iface);
1860 pin->flushing_cs.DebugInfo->Spare[0] = 0;
1861 DeleteCriticalSection(&pin->flushing_cs);
1863 strmbase_seeking_cleanup(&pin->seek);
1864 strmbase_source_cleanup(&pin->pin);
1865 free(pin);
1868 static const struct strmbase_source_ops source_ops =
1870 .base.pin_query_interface = source_query_interface,
1871 .base.pin_query_accept = source_query_accept,
1872 .base.pin_get_media_type = source_get_media_type,
1873 .pfnAttemptConnection = BaseOutputPinImpl_AttemptConnection,
1874 .pfnDecideAllocator = BaseOutputPinImpl_DecideAllocator,
1875 .pfnDecideBufferSize = GSTOutPin_DecideBufferSize,
1878 static struct parser_source *create_pin(struct parser *filter,
1879 wg_parser_stream_t stream, const WCHAR *name)
1881 struct parser_source *pin, **new_array;
1883 if (!(new_array = realloc(filter->sources, (filter->source_count + 1) * sizeof(*filter->sources))))
1884 return NULL;
1885 filter->sources = new_array;
1887 if (!(pin = calloc(1, sizeof(*pin))))
1888 return NULL;
1890 pin->wg_stream = stream;
1891 strmbase_source_init(&pin->pin, &filter->filter, name, &source_ops);
1892 pin->IQualityControl_iface.lpVtbl = &GSTOutPin_QualityControl_Vtbl;
1893 strmbase_seeking_init(&pin->seek, &GST_Seeking_Vtbl, GST_ChangeStop,
1894 GST_ChangeCurrent, GST_ChangeRate);
1895 BaseFilterImpl_IncrementPinVersion(&filter->filter);
1897 InitializeCriticalSection(&pin->flushing_cs);
1898 pin->flushing_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": pin.flushing_cs");
1899 InitializeConditionVariable(&pin->eos_cv);
1901 filter->sources[filter->source_count++] = pin;
1902 return pin;
1905 static HRESULT GST_RemoveOutputPins(struct parser *This)
1907 unsigned int i;
1909 TRACE("(%p)\n", This);
1911 if (!This->sink_connected)
1912 return S_OK;
1914 for (i = 0; i < This->source_count; ++i)
1916 if (This->sources[i])
1917 free_source_pin(This->sources[i]);
1920 wg_parser_disconnect(This->wg_parser);
1922 /* read_thread() needs to stay alive to service any read requests GStreamer
1923 * sends, so we can only shut it down after GStreamer stops. */
1924 This->sink_connected = false;
1925 WaitForSingleObject(This->read_thread, INFINITE);
1926 CloseHandle(This->read_thread);
1928 This->source_count = 0;
1929 free(This->sources);
1930 This->sources = NULL;
1932 BaseFilterImpl_IncrementPinVersion(&This->filter);
1933 return S_OK;
1936 static BOOL compare_media_types(const AM_MEDIA_TYPE *a, const AM_MEDIA_TYPE *b)
1938 return IsEqualGUID(&a->majortype, &b->majortype)
1939 && IsEqualGUID(&a->subtype, &b->subtype)
1940 && IsEqualGUID(&a->formattype, &b->formattype)
1941 && a->cbFormat == b->cbFormat
1942 && !memcmp(a->pbFormat, b->pbFormat, a->cbFormat);
1945 static HRESULT wave_parser_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
1947 if (!IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
1948 return S_FALSE;
1949 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WAVE))
1950 return S_OK;
1951 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_AU) || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_AIFF))
1952 FIXME("AU and AIFF files are not yet supported.\n");
1953 return S_FALSE;
1956 static const struct strmbase_sink_ops wave_parser_sink_ops =
1958 .base.pin_query_accept = wave_parser_sink_query_accept,
1959 .sink_connect = parser_sink_connect,
1960 .sink_disconnect = parser_sink_disconnect,
1963 static BOOL wave_parser_filter_init_gst(struct parser *filter)
1965 if (!create_pin(filter, wg_parser_get_stream(filter->wg_parser, 0), L"output"))
1966 return FALSE;
1968 return TRUE;
1971 static HRESULT wave_parser_source_query_accept(struct parser_source *pin, const AM_MEDIA_TYPE *mt)
1973 struct wg_format format;
1974 AM_MEDIA_TYPE pad_mt;
1975 HRESULT hr;
1977 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
1978 if (!amt_from_wg_format(&pad_mt, &format, false))
1979 return E_OUTOFMEMORY;
1980 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
1981 FreeMediaType(&pad_mt);
1982 return hr;
1985 static HRESULT wave_parser_source_get_media_type(struct parser_source *pin,
1986 unsigned int index, AM_MEDIA_TYPE *mt)
1988 struct wg_format format;
1990 if (index > 0)
1991 return VFW_S_NO_MORE_ITEMS;
1992 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
1993 if (!amt_from_wg_format(mt, &format, false))
1994 return E_OUTOFMEMORY;
1995 return S_OK;
1998 HRESULT wave_parser_create(IUnknown *outer, IUnknown **out)
2000 struct parser *object;
2001 HRESULT hr;
2003 if (FAILED(hr = parser_create(WG_PARSER_WAVPARSE, &object)))
2004 return hr;
2006 strmbase_filter_init(&object->filter, outer, &CLSID_WAVEParser, &filter_ops);
2007 strmbase_sink_init(&object->sink, &object->filter, L"input pin", &wave_parser_sink_ops, NULL);
2008 object->init_gst = wave_parser_filter_init_gst;
2009 object->source_query_accept = wave_parser_source_query_accept;
2010 object->source_get_media_type = wave_parser_source_get_media_type;
2012 TRACE("Created WAVE parser %p.\n", object);
2013 *out = &object->filter.IUnknown_inner;
2014 return S_OK;
2017 static HRESULT avi_splitter_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2019 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream)
2020 && IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_Avi))
2021 return S_OK;
2022 return S_FALSE;
2025 static const struct strmbase_sink_ops avi_splitter_sink_ops =
2027 .base.pin_query_accept = avi_splitter_sink_query_accept,
2028 .sink_connect = parser_sink_connect,
2029 .sink_disconnect = parser_sink_disconnect,
2032 static BOOL avi_splitter_filter_init_gst(struct parser *filter)
2034 wg_parser_t parser = filter->wg_parser;
2035 uint32_t i, stream_count;
2036 WCHAR source_name[20];
2038 stream_count = wg_parser_get_stream_count(parser);
2039 for (i = 0; i < stream_count; ++i)
2041 swprintf(source_name, ARRAY_SIZE(source_name), L"Stream %02u", i);
2042 if (!create_pin(filter, wg_parser_get_stream(parser, i), source_name))
2043 return FALSE;
2046 return TRUE;
2049 static HRESULT avi_splitter_source_query_accept(struct parser_source *pin, const AM_MEDIA_TYPE *mt)
2051 struct wg_format format;
2052 AM_MEDIA_TYPE pad_mt;
2053 HRESULT hr;
2055 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
2056 if (!amt_from_wg_format(&pad_mt, &format, false))
2057 return E_OUTOFMEMORY;
2058 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2059 FreeMediaType(&pad_mt);
2060 return hr;
2063 static HRESULT avi_splitter_source_get_media_type(struct parser_source *pin,
2064 unsigned int index, AM_MEDIA_TYPE *mt)
2066 struct wg_format format;
2068 if (index > 0)
2069 return VFW_S_NO_MORE_ITEMS;
2070 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
2071 if (!amt_from_wg_format(mt, &format, false))
2072 return E_OUTOFMEMORY;
2073 return S_OK;
2076 HRESULT avi_splitter_create(IUnknown *outer, IUnknown **out)
2078 struct parser *object;
2079 HRESULT hr;
2081 if (FAILED(hr = parser_create(WG_PARSER_AVIDEMUX, &object)))
2082 return hr;
2084 strmbase_filter_init(&object->filter, outer, &CLSID_AviSplitter, &filter_ops);
2085 strmbase_sink_init(&object->sink, &object->filter, L"input pin", &avi_splitter_sink_ops, NULL);
2086 object->init_gst = avi_splitter_filter_init_gst;
2087 object->source_query_accept = avi_splitter_source_query_accept;
2088 object->source_get_media_type = avi_splitter_source_get_media_type;
2090 TRACE("Created AVI splitter %p.\n", object);
2091 *out = &object->filter.IUnknown_inner;
2092 return S_OK;
2095 static HRESULT mpeg_splitter_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2097 if (!IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
2098 return S_FALSE;
2099 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1Audio))
2100 return S_OK;
2101 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1Video)
2102 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1System)
2103 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
2104 FIXME("Unsupported subtype %s.\n", wine_dbgstr_guid(&mt->subtype));
2105 return S_FALSE;
2108 static const struct strmbase_sink_ops mpeg_splitter_sink_ops =
2110 .base.pin_query_accept = mpeg_splitter_sink_query_accept,
2111 .sink_connect = parser_sink_connect,
2112 .sink_disconnect = parser_sink_disconnect,
2115 static BOOL mpeg_splitter_filter_init_gst(struct parser *filter)
2117 if (!create_pin(filter, wg_parser_get_stream(filter->wg_parser, 0), L"Audio"))
2118 return FALSE;
2120 return TRUE;
2123 static HRESULT mpeg_splitter_source_query_accept(struct parser_source *pin, const AM_MEDIA_TYPE *mt)
2125 struct wg_format format;
2126 AM_MEDIA_TYPE pad_mt;
2127 HRESULT hr;
2129 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
2130 if (!amt_from_wg_format(&pad_mt, &format, false))
2131 return E_OUTOFMEMORY;
2132 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2133 FreeMediaType(&pad_mt);
2134 return hr;
2137 static HRESULT mpeg_splitter_source_get_media_type(struct parser_source *pin,
2138 unsigned int index, AM_MEDIA_TYPE *mt)
2140 struct wg_format format;
2142 if (index > 0)
2143 return VFW_S_NO_MORE_ITEMS;
2144 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
2145 if (!amt_from_wg_format(mt, &format, false))
2146 return E_OUTOFMEMORY;
2147 return S_OK;
2150 static HRESULT mpeg_splitter_query_interface(struct strmbase_filter *iface, REFIID iid, void **out)
2152 struct parser *filter = impl_from_strmbase_filter(iface);
2154 if (IsEqualGUID(iid, &IID_IAMStreamSelect))
2156 *out = &filter->IAMStreamSelect_iface;
2157 IUnknown_AddRef((IUnknown *)*out);
2158 return S_OK;
2161 return E_NOINTERFACE;
2164 static const struct strmbase_filter_ops mpeg_splitter_ops =
2166 .filter_query_interface = mpeg_splitter_query_interface,
2167 .filter_get_pin = parser_get_pin,
2168 .filter_destroy = parser_destroy,
2169 .filter_init_stream = parser_init_stream,
2170 .filter_cleanup_stream = parser_cleanup_stream,
2173 HRESULT mpeg_splitter_create(IUnknown *outer, IUnknown **out)
2175 struct parser *object;
2176 HRESULT hr;
2178 if (FAILED(hr = parser_create(WG_PARSER_MPEGAUDIOPARSE, &object)))
2179 return hr;
2181 strmbase_filter_init(&object->filter, outer, &CLSID_MPEG1Splitter, &mpeg_splitter_ops);
2182 strmbase_sink_init(&object->sink, &object->filter, L"Input", &mpeg_splitter_sink_ops, NULL);
2183 object->IAMStreamSelect_iface.lpVtbl = &stream_select_vtbl;
2185 object->init_gst = mpeg_splitter_filter_init_gst;
2186 object->source_query_accept = mpeg_splitter_source_query_accept;
2187 object->source_get_media_type = mpeg_splitter_source_get_media_type;
2188 object->enum_sink_first = TRUE;
2190 TRACE("Created MPEG-1 splitter %p.\n", object);
2191 *out = &object->filter.IUnknown_inner;
2192 return S_OK;