dbghelp: Remove DMT_ entries for .DBG and .PDB files.
[wine.git] / dlls / winegstreamer / quartz_parser.c
blob0670fbd5181e3dccbea381ea8cbd39ec6a7b14bf
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 static unsigned int wg_format_get_max_size_video_raw(enum wg_video_format format, unsigned int width, unsigned int height)
285 switch (format)
287 case WG_VIDEO_FORMAT_BGRA:
288 case WG_VIDEO_FORMAT_BGRx:
289 case WG_VIDEO_FORMAT_AYUV:
290 return width * height * 4;
292 case WG_VIDEO_FORMAT_BGR:
293 return ALIGN(width * 3, 4) * height;
295 case WG_VIDEO_FORMAT_RGB15:
296 case WG_VIDEO_FORMAT_RGB16:
297 case WG_VIDEO_FORMAT_UYVY:
298 case WG_VIDEO_FORMAT_YUY2:
299 case WG_VIDEO_FORMAT_YVYU:
300 return ALIGN(width * 2, 4) * height;
302 case WG_VIDEO_FORMAT_I420:
303 case WG_VIDEO_FORMAT_YV12:
304 return ALIGN(width, 4) * ALIGN(height, 2) /* Y plane */
305 + 2 * ALIGN((width + 1) / 2, 4) * ((height + 1) / 2); /* U and V planes */
307 case WG_VIDEO_FORMAT_NV12:
308 return ALIGN(width, 4) * ALIGN(height, 2) /* Y plane */
309 + ALIGN(width, 4) * ((height + 1) / 2); /* U/V plane */
311 case WG_VIDEO_FORMAT_UNKNOWN:
312 FIXME("Cannot guess maximum sample size for unknown video format.\n");
313 return 0;
316 assert(0);
317 return 0;
320 unsigned int wg_format_get_max_size(const struct wg_format *format)
322 switch (format->major_type)
324 case WG_MAJOR_TYPE_VIDEO:
325 return wg_format_get_max_size_video_raw(format->u.video.format,
326 format->u.video.width, abs(format->u.video.height));
328 case WG_MAJOR_TYPE_VIDEO_CINEPAK:
329 /* Both ffmpeg's encoder and a Cinepak file seen in the wild report
330 * 24 bpp. ffmpeg sets biSizeImage as below; others may be smaller,
331 * but as long as every sample fits into our allocator, we're fine. */
332 return format->u.video_cinepak.width * format->u.video_cinepak.height * 3;
334 case WG_MAJOR_TYPE_VIDEO_MPEG1:
335 /* Estimated max size of a compressed video frame.
336 * There's no way to no way to know the real upper bound,
337 * so let's just use the decompressed size and hope it works. */
338 return wg_format_get_max_size_video_raw(WG_VIDEO_FORMAT_YV12,
339 format->u.video_mpeg1.width, format->u.video_mpeg1.height);
341 case WG_MAJOR_TYPE_AUDIO:
343 unsigned int rate = format->u.audio.rate, channels = format->u.audio.channels;
345 /* Actually we don't know how large of a sample GStreamer will give
346 * us. Hopefully 1 second is enough... */
348 switch (format->u.audio.format)
350 case WG_AUDIO_FORMAT_U8:
351 return rate * channels;
353 case WG_AUDIO_FORMAT_S16LE:
354 return rate * channels * 2;
356 case WG_AUDIO_FORMAT_S24LE:
357 return rate * channels * 3;
359 case WG_AUDIO_FORMAT_S32LE:
360 case WG_AUDIO_FORMAT_F32LE:
361 return rate * channels * 4;
363 case WG_AUDIO_FORMAT_F64LE:
364 return rate * channels * 8;
366 case WG_AUDIO_FORMAT_UNKNOWN:
367 FIXME("Cannot guess maximum sample size for unknown audio format.\n");
368 return 0;
370 break;
373 case WG_MAJOR_TYPE_AUDIO_MPEG1:
374 switch (format->u.audio_mpeg1.layer)
376 case 1:
377 return 56000;
379 case 2:
380 return 48000;
382 case 3:
383 return 40000;
385 break;
387 case WG_MAJOR_TYPE_AUDIO_MPEG4:
388 case WG_MAJOR_TYPE_AUDIO_WMA:
389 case WG_MAJOR_TYPE_VIDEO_H264:
390 case WG_MAJOR_TYPE_VIDEO_WMV:
391 case WG_MAJOR_TYPE_VIDEO_INDEO:
392 FIXME("Format %u not implemented!\n", format->major_type);
393 return 0;
395 case WG_MAJOR_TYPE_UNKNOWN:
396 FIXME("Cannot guess maximum sample size for unknown format.\n");
397 return 0;
400 assert(0);
401 return 0;
404 static const GUID *wg_video_format_get_mediasubtype(enum wg_video_format format)
406 switch (format)
408 case WG_VIDEO_FORMAT_UNKNOWN: return &GUID_NULL;
409 case WG_VIDEO_FORMAT_BGRA: return &MEDIASUBTYPE_ARGB32;
410 case WG_VIDEO_FORMAT_BGRx: return &MEDIASUBTYPE_RGB32;
411 case WG_VIDEO_FORMAT_BGR: return &MEDIASUBTYPE_RGB24;
412 case WG_VIDEO_FORMAT_RGB15: return &MEDIASUBTYPE_RGB555;
413 case WG_VIDEO_FORMAT_RGB16: return &MEDIASUBTYPE_RGB565;
414 case WG_VIDEO_FORMAT_AYUV: return &MEDIASUBTYPE_AYUV;
415 case WG_VIDEO_FORMAT_I420: return &MEDIASUBTYPE_I420;
416 case WG_VIDEO_FORMAT_NV12: return &MEDIASUBTYPE_NV12;
417 case WG_VIDEO_FORMAT_UYVY: return &MEDIASUBTYPE_UYVY;
418 case WG_VIDEO_FORMAT_YUY2: return &MEDIASUBTYPE_YUY2;
419 case WG_VIDEO_FORMAT_YV12: return &MEDIASUBTYPE_YV12;
420 case WG_VIDEO_FORMAT_YVYU: return &MEDIASUBTYPE_YVYU;
423 assert(0);
424 return NULL;
427 static DWORD wg_video_format_get_compression(enum wg_video_format format)
429 switch (format)
431 case WG_VIDEO_FORMAT_UNKNOWN: return 0;
432 case WG_VIDEO_FORMAT_BGRA: return BI_RGB;
433 case WG_VIDEO_FORMAT_BGRx: return BI_RGB;
434 case WG_VIDEO_FORMAT_BGR: return BI_RGB;
435 case WG_VIDEO_FORMAT_RGB15: return BI_RGB;
436 case WG_VIDEO_FORMAT_RGB16: return BI_BITFIELDS;
437 case WG_VIDEO_FORMAT_AYUV: return mmioFOURCC('A','Y','U','V');
438 case WG_VIDEO_FORMAT_I420: return mmioFOURCC('I','4','2','0');
439 case WG_VIDEO_FORMAT_NV12: return mmioFOURCC('N','V','1','2');
440 case WG_VIDEO_FORMAT_UYVY: return mmioFOURCC('U','Y','V','Y');
441 case WG_VIDEO_FORMAT_YUY2: return mmioFOURCC('Y','U','Y','2');
442 case WG_VIDEO_FORMAT_YV12: return mmioFOURCC('Y','V','1','2');
443 case WG_VIDEO_FORMAT_YVYU: return mmioFOURCC('Y','V','Y','U');
446 assert(0);
447 return 0;
450 static WORD wg_video_format_get_depth(enum wg_video_format format)
452 switch (format)
454 case WG_VIDEO_FORMAT_UNKNOWN: return 0;
455 case WG_VIDEO_FORMAT_BGRA: return 32;
456 case WG_VIDEO_FORMAT_BGRx: return 32;
457 case WG_VIDEO_FORMAT_BGR: return 24;
458 case WG_VIDEO_FORMAT_RGB15: return 16;
459 case WG_VIDEO_FORMAT_RGB16: return 16;
460 case WG_VIDEO_FORMAT_AYUV: return 32;
461 case WG_VIDEO_FORMAT_I420: return 12;
462 case WG_VIDEO_FORMAT_NV12: return 12;
463 case WG_VIDEO_FORMAT_UYVY: return 16;
464 case WG_VIDEO_FORMAT_YUY2: return 16;
465 case WG_VIDEO_FORMAT_YV12: return 12;
466 case WG_VIDEO_FORMAT_YVYU: return 16;
469 assert(0);
470 return 0;
473 static bool amt_from_wg_format_video(AM_MEDIA_TYPE *mt, const struct wg_format *format, bool wm)
475 VIDEOINFO *video_format;
476 uint32_t frame_time;
478 if (format->u.video.format == WG_VIDEO_FORMAT_UNKNOWN)
479 return false;
481 if (!(video_format = CoTaskMemAlloc(sizeof(*video_format))))
482 return false;
484 mt->majortype = MEDIATYPE_Video;
485 mt->subtype = *wg_video_format_get_mediasubtype(format->u.video.format);
486 if (wm)
487 mt->bFixedSizeSamples = TRUE;
488 else
489 mt->bTemporalCompression = TRUE;
490 mt->lSampleSize = 1;
491 mt->formattype = FORMAT_VideoInfo;
492 mt->cbFormat = sizeof(VIDEOINFOHEADER);
493 mt->pbFormat = (BYTE *)video_format;
495 memset(video_format, 0, sizeof(*video_format));
497 if (wm)
499 SetRect(&video_format->rcSource, 0, 0, format->u.video.width, abs(format->u.video.height));
500 video_format->rcTarget = video_format->rcSource;
502 if ((frame_time = MulDiv(10000000, format->u.video.fps_d, format->u.video.fps_n)) != -1)
503 video_format->AvgTimePerFrame = frame_time;
504 video_format->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
505 video_format->bmiHeader.biWidth = format->u.video.width;
506 video_format->bmiHeader.biHeight = format->u.video.height;
507 if (wg_video_format_is_rgb(format->u.video.format))
508 video_format->bmiHeader.biHeight = -format->u.video.height;
509 video_format->bmiHeader.biPlanes = 1;
510 video_format->bmiHeader.biBitCount = wg_video_format_get_depth(format->u.video.format);
511 video_format->bmiHeader.biCompression = wg_video_format_get_compression(format->u.video.format);
512 video_format->bmiHeader.biSizeImage = wg_format_get_max_size(format);
514 if (format->u.video.format == WG_VIDEO_FORMAT_RGB16)
516 mt->cbFormat = offsetof(VIDEOINFO, dwBitMasks[3]);
517 video_format->dwBitMasks[iRED] = 0xf800;
518 video_format->dwBitMasks[iGREEN] = 0x07e0;
519 video_format->dwBitMasks[iBLUE] = 0x001f;
522 return true;
525 static bool amt_from_wg_format_video_cinepak(AM_MEDIA_TYPE *mt, const struct wg_format *format)
527 VIDEOINFO *video_format;
528 uint32_t frame_time;
530 if (!(video_format = CoTaskMemAlloc(sizeof(*video_format))))
531 return false;
533 mt->majortype = MEDIATYPE_Video;
534 mt->subtype = MEDIASUBTYPE_CVID;
535 mt->bTemporalCompression = TRUE;
536 mt->lSampleSize = 1;
537 mt->formattype = FORMAT_VideoInfo;
538 mt->cbFormat = sizeof(VIDEOINFOHEADER);
539 mt->pbFormat = (BYTE *)video_format;
541 memset(video_format, 0, sizeof(*video_format));
542 if ((frame_time = MulDiv(10000000, format->u.video_cinepak.fps_d, format->u.video_cinepak.fps_n)) != -1)
543 video_format->AvgTimePerFrame = frame_time;
544 video_format->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
545 video_format->bmiHeader.biWidth = format->u.video_cinepak.width;
546 video_format->bmiHeader.biHeight = format->u.video_cinepak.height;
547 video_format->bmiHeader.biPlanes = 1;
548 video_format->bmiHeader.biBitCount = 24;
549 video_format->bmiHeader.biCompression = mt->subtype.Data1;
550 video_format->bmiHeader.biSizeImage = wg_format_get_max_size(format);
552 return true;
555 static bool amt_from_wg_format_video_wmv(AM_MEDIA_TYPE *mt, const struct wg_format *format)
557 VIDEOINFO *video_format;
558 uint32_t frame_time;
559 const GUID *subtype;
561 if (!(video_format = CoTaskMemAlloc(sizeof(*video_format))))
562 return false;
564 switch (format->u.video_wmv.format)
566 case WG_WMV_VIDEO_FORMAT_WMV1:
567 subtype = &MEDIASUBTYPE_WMV1;
568 break;
569 case WG_WMV_VIDEO_FORMAT_WMV2:
570 subtype = &MEDIASUBTYPE_WMV2;
571 break;
572 case WG_WMV_VIDEO_FORMAT_WMV3:
573 subtype = &MEDIASUBTYPE_WMV3;
574 break;
575 case WG_WMV_VIDEO_FORMAT_WMVA:
576 subtype = &MEDIASUBTYPE_WMVA;
577 break;
578 case WG_WMV_VIDEO_FORMAT_WVC1:
579 subtype = &MEDIASUBTYPE_WVC1;
580 break;
581 default:
582 WARN("Invalid WMV format %u.\n", format->u.video_wmv.format);
583 return false;
586 mt->majortype = MEDIATYPE_Video;
587 mt->subtype = *subtype;
588 mt->bFixedSizeSamples = FALSE;
589 mt->bTemporalCompression = TRUE;
590 mt->lSampleSize = 0;
591 mt->formattype = FORMAT_VideoInfo;
592 mt->cbFormat = sizeof(VIDEOINFOHEADER);
593 mt->pbFormat = (BYTE *)video_format;
595 memset(video_format, 0, sizeof(*video_format));
596 SetRect(&video_format->rcSource, 0, 0, format->u.video_wmv.width, format->u.video_wmv.height);
597 video_format->rcTarget = video_format->rcSource;
598 if ((frame_time = MulDiv(10000000, format->u.video_wmv.fps_d, format->u.video_wmv.fps_n)) != -1)
599 video_format->AvgTimePerFrame = frame_time;
600 video_format->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
601 video_format->bmiHeader.biWidth = format->u.video_wmv.width;
602 video_format->bmiHeader.biHeight = format->u.video_wmv.height;
603 video_format->bmiHeader.biPlanes = 1;
604 video_format->bmiHeader.biCompression = mt->subtype.Data1;
606 return true;
609 static bool amt_from_wg_format_video_mpeg1(AM_MEDIA_TYPE *mt, const struct wg_format *format)
611 MPEG1VIDEOINFO *video_format;
612 uint32_t frame_time;
614 if (!(video_format = CoTaskMemAlloc(sizeof(*video_format))))
615 return false;
617 mt->majortype = MEDIATYPE_Video;
618 mt->subtype = MEDIASUBTYPE_MPEG1Payload;
619 mt->bTemporalCompression = TRUE;
620 mt->lSampleSize = 1;
621 mt->formattype = FORMAT_MPEGVideo;
622 mt->cbFormat = sizeof(MPEG1VIDEOINFO);
623 mt->pbFormat = (BYTE *)video_format;
625 memset(video_format, 0, sizeof(*video_format));
626 if ((frame_time = MulDiv(10000000, format->u.video_mpeg1.fps_d, format->u.video_mpeg1.fps_n)) != -1)
627 video_format->hdr.AvgTimePerFrame = frame_time;
628 video_format->hdr.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
629 video_format->hdr.bmiHeader.biWidth = format->u.video_mpeg1.width;
630 video_format->hdr.bmiHeader.biHeight = format->u.video_mpeg1.height;
631 video_format->hdr.bmiHeader.biPlanes = 1;
632 video_format->hdr.bmiHeader.biBitCount = 12;
633 video_format->hdr.bmiHeader.biCompression = mt->subtype.Data1;
634 video_format->hdr.bmiHeader.biSizeImage = wg_format_get_max_size(format);
636 return true;
639 bool amt_from_wg_format(AM_MEDIA_TYPE *mt, const struct wg_format *format, bool wm)
641 memset(mt, 0, sizeof(*mt));
643 switch (format->major_type)
645 case WG_MAJOR_TYPE_AUDIO_MPEG4:
646 case WG_MAJOR_TYPE_AUDIO_WMA:
647 case WG_MAJOR_TYPE_VIDEO_H264:
648 case WG_MAJOR_TYPE_VIDEO_INDEO:
649 FIXME("Format %u not implemented!\n", format->major_type);
650 /* fallthrough */
651 case WG_MAJOR_TYPE_UNKNOWN:
652 return false;
654 case WG_MAJOR_TYPE_AUDIO:
655 return amt_from_wg_format_audio(mt, format);
657 case WG_MAJOR_TYPE_AUDIO_MPEG1:
658 return amt_from_wg_format_audio_mpeg1(mt, format);
660 case WG_MAJOR_TYPE_VIDEO:
661 return amt_from_wg_format_video(mt, format, wm);
663 case WG_MAJOR_TYPE_VIDEO_CINEPAK:
664 return amt_from_wg_format_video_cinepak(mt, format);
666 case WG_MAJOR_TYPE_VIDEO_WMV:
667 return amt_from_wg_format_video_wmv(mt, format);
669 case WG_MAJOR_TYPE_VIDEO_MPEG1:
670 return amt_from_wg_format_video_mpeg1(mt, format);
673 assert(0);
674 return false;
677 static bool amt_to_wg_format_audio(const AM_MEDIA_TYPE *mt, struct wg_format *format)
679 static const struct
681 const GUID *subtype;
682 WORD depth;
683 enum wg_audio_format format;
685 format_map[] =
687 {&MEDIASUBTYPE_PCM, 8, WG_AUDIO_FORMAT_U8},
688 {&MEDIASUBTYPE_PCM, 16, WG_AUDIO_FORMAT_S16LE},
689 {&MEDIASUBTYPE_PCM, 24, WG_AUDIO_FORMAT_S24LE},
690 {&MEDIASUBTYPE_PCM, 32, WG_AUDIO_FORMAT_S32LE},
691 {&MEDIASUBTYPE_IEEE_FLOAT, 32, WG_AUDIO_FORMAT_F32LE},
692 {&MEDIASUBTYPE_IEEE_FLOAT, 64, WG_AUDIO_FORMAT_F64LE},
695 const WAVEFORMATEX *audio_format = (const WAVEFORMATEX *)mt->pbFormat;
696 unsigned int i;
698 if (!IsEqualGUID(&mt->formattype, &FORMAT_WaveFormatEx))
700 FIXME("Unknown format type %s.\n", debugstr_guid(&mt->formattype));
701 return false;
703 if (mt->cbFormat < sizeof(WAVEFORMATEX) || !mt->pbFormat)
705 ERR("Unexpected format size %lu.\n", mt->cbFormat);
706 return false;
709 format->major_type = WG_MAJOR_TYPE_AUDIO;
710 format->u.audio.channels = audio_format->nChannels;
711 format->u.audio.rate = audio_format->nSamplesPerSec;
713 if (audio_format->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
715 const WAVEFORMATEXTENSIBLE *ext_format = (const WAVEFORMATEXTENSIBLE *)mt->pbFormat;
717 format->u.audio.channel_mask = ext_format->dwChannelMask;
719 else
721 if (audio_format->nChannels == 1)
722 format->u.audio.channel_mask = KSAUDIO_SPEAKER_MONO;
723 else if (audio_format->nChannels == 2)
724 format->u.audio.channel_mask = KSAUDIO_SPEAKER_STEREO;
725 else
727 ERR("Unexpected channel count %u.\n", audio_format->nChannels);
728 return false;
732 for (i = 0; i < ARRAY_SIZE(format_map); ++i)
734 if (IsEqualGUID(&mt->subtype, format_map[i].subtype)
735 && audio_format->wBitsPerSample == format_map[i].depth)
737 format->u.audio.format = format_map[i].format;
738 return true;
742 FIXME("Unknown subtype %s, depth %u.\n", debugstr_guid(&mt->subtype), audio_format->wBitsPerSample);
743 return false;
746 static bool amt_to_wg_format_audio_mpeg1(const AM_MEDIA_TYPE *mt, struct wg_format *format)
748 const MPEG1WAVEFORMAT *audio_format = (const MPEG1WAVEFORMAT *)mt->pbFormat;
750 if (!IsEqualGUID(&mt->formattype, &FORMAT_WaveFormatEx))
752 FIXME("Unknown format type %s.\n", debugstr_guid(&mt->formattype));
753 return false;
755 if (mt->cbFormat < sizeof(*audio_format) || !mt->pbFormat)
757 ERR("Unexpected format size %lu.\n", mt->cbFormat);
758 return false;
761 format->major_type = WG_MAJOR_TYPE_AUDIO_MPEG1;
762 format->u.audio_mpeg1.channels = audio_format->wfx.nChannels;
763 format->u.audio_mpeg1.rate = audio_format->wfx.nSamplesPerSec;
764 format->u.audio_mpeg1.layer = audio_format->fwHeadLayer;
765 return true;
768 static bool amt_to_wg_format_audio_mpeg1_layer3(const AM_MEDIA_TYPE *mt, struct wg_format *format)
770 const MPEGLAYER3WAVEFORMAT *audio_format = (const MPEGLAYER3WAVEFORMAT *)mt->pbFormat;
772 if (!IsEqualGUID(&mt->formattype, &FORMAT_WaveFormatEx))
774 FIXME("Unknown format type %s.\n", debugstr_guid(&mt->formattype));
775 return false;
777 if (mt->cbFormat < sizeof(*audio_format) || !mt->pbFormat)
779 ERR("Unexpected format size %lu.\n", mt->cbFormat);
780 return false;
783 format->major_type = WG_MAJOR_TYPE_AUDIO_MPEG1;
784 format->u.audio_mpeg1.channels = audio_format->wfx.nChannels;
785 format->u.audio_mpeg1.rate = audio_format->wfx.nSamplesPerSec;
786 format->u.audio_mpeg1.layer = 3;
787 return true;
790 static bool amt_to_wg_format_video(const AM_MEDIA_TYPE *mt, struct wg_format *format)
792 static const struct
794 const GUID *subtype;
795 enum wg_video_format format;
797 format_map[] =
799 {&MEDIASUBTYPE_ARGB32, WG_VIDEO_FORMAT_BGRA},
800 {&MEDIASUBTYPE_RGB32, WG_VIDEO_FORMAT_BGRx},
801 {&MEDIASUBTYPE_RGB24, WG_VIDEO_FORMAT_BGR},
802 {&MEDIASUBTYPE_RGB555, WG_VIDEO_FORMAT_RGB15},
803 {&MEDIASUBTYPE_RGB565, WG_VIDEO_FORMAT_RGB16},
804 {&MEDIASUBTYPE_AYUV, WG_VIDEO_FORMAT_AYUV},
805 {&MEDIASUBTYPE_I420, WG_VIDEO_FORMAT_I420},
806 {&MEDIASUBTYPE_NV12, WG_VIDEO_FORMAT_NV12},
807 {&MEDIASUBTYPE_UYVY, WG_VIDEO_FORMAT_UYVY},
808 {&MEDIASUBTYPE_YUY2, WG_VIDEO_FORMAT_YUY2},
809 {&MEDIASUBTYPE_YV12, WG_VIDEO_FORMAT_YV12},
810 {&MEDIASUBTYPE_YVYU, WG_VIDEO_FORMAT_YVYU},
813 const VIDEOINFOHEADER *video_format = (const VIDEOINFOHEADER *)mt->pbFormat;
814 unsigned int i;
816 if (!IsEqualGUID(&mt->formattype, &FORMAT_VideoInfo))
818 FIXME("Unknown format type %s.\n", debugstr_guid(&mt->formattype));
819 return false;
821 if (mt->cbFormat < sizeof(VIDEOINFOHEADER) || !mt->pbFormat)
823 ERR("Unexpected format size %lu.\n", mt->cbFormat);
824 return false;
827 format->major_type = WG_MAJOR_TYPE_VIDEO;
828 format->u.video.width = video_format->bmiHeader.biWidth;
829 format->u.video.height = video_format->bmiHeader.biHeight;
830 format->u.video.fps_n = 10000000;
831 format->u.video.fps_d = video_format->AvgTimePerFrame;
833 for (i = 0; i < ARRAY_SIZE(format_map); ++i)
835 if (IsEqualGUID(&mt->subtype, format_map[i].subtype))
837 format->u.video.format = format_map[i].format;
838 if (wg_video_format_is_rgb(format->u.video.format))
839 format->u.video.height = -format->u.video.height;
840 return true;
844 FIXME("Unknown subtype %s.\n", debugstr_guid(&mt->subtype));
845 return false;
848 static bool amt_to_wg_format_video_wmv(const AM_MEDIA_TYPE *mt, struct wg_format *format)
850 const VIDEOINFOHEADER *video_format = (const VIDEOINFOHEADER *)mt->pbFormat;
852 if (!IsEqualGUID(&mt->formattype, &FORMAT_VideoInfo))
854 FIXME("Unknown format type %s.\n", debugstr_guid(&mt->formattype));
855 return false;
857 if (mt->cbFormat < sizeof(VIDEOINFOHEADER) || !mt->pbFormat)
859 ERR("Unexpected format size %lu.\n", mt->cbFormat);
860 return false;
863 format->major_type = WG_MAJOR_TYPE_VIDEO_WMV;
864 format->u.video_wmv.width = video_format->bmiHeader.biWidth;
865 format->u.video_wmv.height = video_format->bmiHeader.biHeight;
866 format->u.video_wmv.fps_n = 10000000;
867 format->u.video_wmv.fps_d = video_format->AvgTimePerFrame;
869 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV1))
870 format->u.video_wmv.format = WG_WMV_VIDEO_FORMAT_WMV1;
871 else if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV2))
872 format->u.video_wmv.format = WG_WMV_VIDEO_FORMAT_WMV2;
873 else if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV3))
874 format->u.video_wmv.format = WG_WMV_VIDEO_FORMAT_WMV3;
875 else if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMVA))
876 format->u.video_wmv.format = WG_WMV_VIDEO_FORMAT_WMVA;
877 else if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WVC1))
878 format->u.video_wmv.format = WG_WMV_VIDEO_FORMAT_WVC1;
879 else
880 format->u.video_wmv.format = WG_WMV_VIDEO_FORMAT_UNKNOWN;
882 return true;
885 static bool amt_to_wg_format_video_mpeg1(const AM_MEDIA_TYPE *mt, struct wg_format *format)
887 const MPEG1VIDEOINFO *video_format = (const MPEG1VIDEOINFO *)mt->pbFormat;
889 if (!IsEqualGUID(&mt->formattype, &FORMAT_MPEGVideo))
891 FIXME("Unknown format type %s.\n", debugstr_guid(&mt->formattype));
892 return false;
894 if (mt->cbFormat < sizeof(VIDEOINFOHEADER) || !mt->pbFormat)
896 ERR("Unexpected format size %lu.\n", mt->cbFormat);
897 return false;
900 format->major_type = WG_MAJOR_TYPE_VIDEO_MPEG1;
901 format->u.video_mpeg1.width = video_format->hdr.bmiHeader.biWidth;
902 format->u.video_mpeg1.height = video_format->hdr.bmiHeader.biHeight;
903 format->u.video_mpeg1.fps_n = 10000000;
904 format->u.video_mpeg1.fps_d = video_format->hdr.AvgTimePerFrame;
906 return true;
909 bool amt_to_wg_format(const AM_MEDIA_TYPE *mt, struct wg_format *format)
911 memset(format, 0, sizeof(*format));
913 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Video))
915 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV1)
916 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV2)
917 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMVA)
918 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMVP)
919 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WVP2)
920 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV_Unknown)
921 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WVC1)
922 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WMV3)
923 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_VC1S))
924 return amt_to_wg_format_video_wmv(mt, format);
925 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1Payload))
926 return amt_to_wg_format_video_mpeg1(mt, format);
927 return amt_to_wg_format_video(mt, format);
929 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Audio))
931 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1AudioPayload))
932 return amt_to_wg_format_audio_mpeg1(mt, format);
933 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MP3))
934 return amt_to_wg_format_audio_mpeg1_layer3(mt, format);
935 return amt_to_wg_format_audio(mt, format);
938 FIXME("Unknown major type %s.\n", debugstr_guid(&mt->majortype));
939 return false;
943 * scale_uint64() is based on gst_util_scale_int() from GStreamer, which is
944 * covered by the following license:
946 * GStreamer
947 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
948 * 2000 Wim Taymans <wtay@chello.be>
949 * 2002 Thomas Vander Stichele <thomas@apestaart.org>
950 * 2004 Wim Taymans <wim@fluendo.com>
951 * 2015 Jan Schmidt <jan@centricular.com>
953 * gstutils.c: Utility functions
955 * This library is free software; you can redistribute it and/or
956 * modify it under the terms of the GNU Library General Public
957 * License as published by the Free Software Foundation; either
958 * version 2 of the License, or (at your option) any later version.
960 * This library is distributed in the hope that it will be useful,
961 * but WITHOUT ANY WARRANTY; without even the implied warranty of
962 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
963 * Library General Public License for more details.
965 * You should have received a copy of the GNU Library General Public
966 * License along with this library; if not, write to the
967 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
968 * Boston, MA 02110-1301, USA.
970 static uint64_t scale_uint64(uint64_t value, uint32_t numerator, uint32_t denominator)
972 ULARGE_INTEGER i, high, low;
974 if (!value)
975 return 0;
977 i.QuadPart = value;
978 low.QuadPart = (ULONGLONG)i.u.LowPart * numerator;
979 high.QuadPart = (ULONGLONG)i.u.HighPart * numerator + low.u.HighPart;
980 low.u.HighPart = 0;
982 if (high.u.HighPart >= denominator)
983 return ULLONG_MAX;
985 low.QuadPart += (high.QuadPart % denominator) << 32;
986 return ((high.QuadPart / denominator) << 32) + (low.QuadPart / denominator);
989 /* Fill and send a single IMediaSample. */
990 static HRESULT send_sample(struct parser_source *pin, IMediaSample *sample,
991 const struct wg_parser_buffer *buffer, uint32_t offset, uint32_t size, DWORD bytes_per_second)
993 HRESULT hr;
994 BYTE *ptr = NULL;
996 TRACE("offset %u, size %u, sample size %lu.\n", offset, size, IMediaSample_GetSize(sample));
998 hr = IMediaSample_SetActualDataLength(sample, size);
999 if(FAILED(hr)){
1000 ERR("Failed to set sample size, hr %#lx.\n", hr);
1001 return hr;
1004 IMediaSample_GetPointer(sample, &ptr);
1006 if (!wg_parser_stream_copy_buffer(pin->wg_stream, ptr, offset, size))
1008 /* The GStreamer pin has been flushed. */
1009 return S_OK;
1012 if (buffer->has_pts)
1014 REFERENCE_TIME start_pts = buffer->pts;
1016 if (offset)
1017 start_pts += scale_uint64(offset, 10000000, bytes_per_second);
1018 start_pts -= pin->seek.llCurrent;
1019 start_pts *= pin->seek.dRate;
1021 if (buffer->has_duration)
1023 REFERENCE_TIME end_pts = buffer->pts + buffer->duration;
1025 if (offset + size < buffer->size)
1026 end_pts = buffer->pts + scale_uint64(offset + size, 10000000, bytes_per_second);
1027 end_pts -= pin->seek.llCurrent;
1028 end_pts *= pin->seek.dRate;
1030 IMediaSample_SetTime(sample, &start_pts, &end_pts);
1031 IMediaSample_SetMediaTime(sample, &start_pts, &end_pts);
1033 else
1035 IMediaSample_SetTime(sample, &start_pts, NULL);
1036 IMediaSample_SetMediaTime(sample, NULL, NULL);
1039 else
1041 IMediaSample_SetTime(sample, NULL, NULL);
1042 IMediaSample_SetMediaTime(sample, NULL, NULL);
1045 IMediaSample_SetDiscontinuity(sample, !offset && buffer->discontinuity);
1046 IMediaSample_SetPreroll(sample, buffer->preroll);
1047 IMediaSample_SetSyncPoint(sample, !buffer->delta);
1049 if (!pin->pin.pin.peer)
1050 return VFW_E_NOT_CONNECTED;
1052 hr = IMemInputPin_Receive(pin->pin.pMemInputPin, sample);
1053 TRACE("Receive() returned hr %#lx.\n", hr);
1054 return hr;
1057 /* Send a single GStreamer buffer (splitting it into multiple IMediaSamples if
1058 * necessary). */
1059 static void send_buffer(struct parser_source *pin, const struct wg_parser_buffer *buffer)
1061 HRESULT hr;
1062 IMediaSample *sample;
1064 if (pin->need_segment)
1066 if (FAILED(hr = IPin_NewSegment(pin->pin.pin.peer,
1067 pin->seek.llCurrent, pin->seek.llStop, pin->seek.dRate)))
1068 WARN("Failed to deliver new segment, hr %#lx.\n", hr);
1069 pin->need_segment = false;
1072 if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_WaveFormatEx)
1073 && (IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_PCM)
1074 || IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_IEEE_FLOAT)))
1076 WAVEFORMATEX *format = (WAVEFORMATEX *)pin->pin.pin.mt.pbFormat;
1077 uint32_t offset = 0;
1079 while (offset < buffer->size)
1081 uint32_t advance;
1083 if (FAILED(hr = IMemAllocator_GetBuffer(pin->pin.pAllocator, &sample, NULL, NULL, 0)))
1085 ERR("Failed to get a sample, hr %#lx.\n", hr);
1086 break;
1089 advance = min(IMediaSample_GetSize(sample), buffer->size - offset);
1091 hr = send_sample(pin, sample, buffer, offset, advance, format->nAvgBytesPerSec);
1093 IMediaSample_Release(sample);
1095 if (FAILED(hr))
1096 break;
1098 offset += advance;
1101 else
1103 if (FAILED(hr = IMemAllocator_GetBuffer(pin->pin.pAllocator, &sample, NULL, NULL, 0)))
1105 ERR("Failed to get a sample, hr %#lx.\n", hr);
1107 else
1109 hr = send_sample(pin, sample, buffer, 0, buffer->size, 0);
1111 IMediaSample_Release(sample);
1115 wg_parser_stream_release_buffer(pin->wg_stream);
1118 static DWORD CALLBACK stream_thread(void *arg)
1120 struct parser_source *pin = arg;
1121 struct parser *filter = impl_from_strmbase_filter(pin->pin.pin.filter);
1123 TRACE("Starting streaming thread for pin %p.\n", pin);
1125 for (;;)
1127 struct wg_parser_buffer buffer;
1129 EnterCriticalSection(&filter->streaming_cs);
1131 while (filter->flushing)
1132 SleepConditionVariableCS(&filter->flushing_cv, &filter->streaming_cs, INFINITE);
1134 if (!filter->streaming)
1136 LeaveCriticalSection(&filter->streaming_cs);
1137 break;
1140 LeaveCriticalSection(&filter->streaming_cs);
1142 EnterCriticalSection(&pin->flushing_cs);
1144 if (pin->eos)
1146 SleepConditionVariableCS(&pin->eos_cv, &pin->flushing_cs, INFINITE);
1147 LeaveCriticalSection(&pin->flushing_cs);
1148 continue;
1151 if (wg_parser_stream_get_buffer(filter->wg_parser, pin->wg_stream, &buffer))
1153 send_buffer(pin, &buffer);
1155 else
1157 TRACE("Got EOS.\n");
1158 IPin_EndOfStream(pin->pin.pin.peer);
1159 pin->eos = true;
1162 LeaveCriticalSection(&pin->flushing_cs);
1165 TRACE("Streaming stopped; exiting.\n");
1166 return 0;
1169 static DWORD CALLBACK read_thread(void *arg)
1171 struct parser *filter = arg;
1172 LONGLONG file_size, unused;
1173 size_t buffer_size = 4096;
1174 void *data = NULL;
1176 if (!(data = malloc(buffer_size)))
1177 return 0;
1179 IAsyncReader_Length(filter->reader, &file_size, &unused);
1181 TRACE("Starting read thread for filter %p.\n", filter);
1183 while (filter->sink_connected)
1185 uint64_t offset;
1186 uint32_t size;
1187 HRESULT hr;
1189 if (!wg_parser_get_next_read_offset(filter->wg_parser, &offset, &size))
1190 continue;
1192 if (offset >= file_size)
1193 size = 0;
1194 else if (offset + size >= file_size)
1195 size = file_size - offset;
1197 if (!array_reserve(&data, &buffer_size, size, 1))
1199 free(data);
1200 return 0;
1203 hr = IAsyncReader_SyncRead(filter->reader, offset, size, data);
1204 if (FAILED(hr))
1205 ERR("Failed to read %u bytes at offset %I64u, hr %#lx.\n", size, offset, hr);
1207 wg_parser_push_data(filter->wg_parser, SUCCEEDED(hr) ? data : NULL, size);
1210 free(data);
1211 TRACE("Streaming stopped; exiting.\n");
1212 return 0;
1215 static inline struct parser_source *impl_from_IMediaSeeking(IMediaSeeking *iface)
1217 return CONTAINING_RECORD(iface, struct parser_source, seek.IMediaSeeking_iface);
1220 static struct strmbase_pin *parser_get_pin(struct strmbase_filter *base, unsigned int index)
1222 struct parser *filter = impl_from_strmbase_filter(base);
1224 if (filter->enum_sink_first)
1226 if (!index)
1227 return &filter->sink.pin;
1228 else if (index <= filter->source_count)
1229 return &filter->sources[index - 1]->pin.pin;
1231 else
1233 if (index < filter->source_count)
1234 return &filter->sources[index]->pin.pin;
1235 else if (index == filter->source_count)
1236 return &filter->sink.pin;
1238 return NULL;
1241 static void parser_destroy(struct strmbase_filter *iface)
1243 struct parser *filter = impl_from_strmbase_filter(iface);
1244 HRESULT hr;
1246 /* Don't need to clean up output pins, disconnecting input pin will do that */
1247 if (filter->sink.pin.peer)
1249 hr = IPin_Disconnect(filter->sink.pin.peer);
1250 assert(hr == S_OK);
1251 hr = IPin_Disconnect(&filter->sink.pin.IPin_iface);
1252 assert(hr == S_OK);
1255 if (filter->reader)
1256 IAsyncReader_Release(filter->reader);
1257 filter->reader = NULL;
1259 wg_parser_destroy(filter->wg_parser);
1261 filter->streaming_cs.DebugInfo->Spare[0] = 0;
1262 DeleteCriticalSection(&filter->streaming_cs);
1264 strmbase_sink_cleanup(&filter->sink);
1265 strmbase_filter_cleanup(&filter->filter);
1266 free(filter);
1269 static HRESULT parser_init_stream(struct strmbase_filter *iface)
1271 struct parser *filter = impl_from_strmbase_filter(iface);
1272 DWORD stop_flags = AM_SEEKING_NoPositioning;
1273 const SourceSeeking *seeking;
1274 unsigned int i;
1276 if (!filter->sink_connected)
1277 return S_OK;
1279 filter->streaming = true;
1281 for (i = 0; i < filter->source_count; ++i)
1283 struct parser_source *source = filter->sources[i];
1284 struct wg_format format;
1285 bool ret;
1287 if (source->pin.pin.peer)
1289 ret = amt_to_wg_format(&source->pin.pin.mt, &format);
1290 assert(ret);
1291 wg_parser_stream_enable(source->wg_stream, &format);
1293 else
1295 wg_parser_stream_disable(source->wg_stream);
1299 /* DirectShow retains the old seek positions, but resets to them every time
1300 * it transitions from stopped -> paused. */
1302 seeking = &filter->sources[0]->seek;
1303 if (seeking->llStop)
1304 stop_flags = AM_SEEKING_AbsolutePositioning;
1305 wg_parser_stream_seek(filter->sources[0]->wg_stream, seeking->dRate,
1306 seeking->llCurrent, seeking->llStop, AM_SEEKING_AbsolutePositioning, stop_flags);
1308 for (i = 0; i < filter->source_count; ++i)
1310 struct parser_source *pin = filter->sources[i];
1311 HRESULT hr;
1313 if (!pin->pin.pin.peer)
1314 continue;
1316 if (FAILED(hr = IMemAllocator_Commit(pin->pin.pAllocator)))
1317 ERR("Failed to commit allocator, hr %#lx.\n", hr);
1319 pin->need_segment = true;
1320 pin->eos = false;
1322 pin->thread = CreateThread(NULL, 0, stream_thread, pin, 0, NULL);
1325 return S_OK;
1328 static HRESULT parser_cleanup_stream(struct strmbase_filter *iface)
1330 struct parser *filter = impl_from_strmbase_filter(iface);
1331 unsigned int i;
1333 if (!filter->sink_connected)
1334 return S_OK;
1336 EnterCriticalSection(&filter->streaming_cs);
1337 filter->streaming = false;
1338 LeaveCriticalSection(&filter->streaming_cs);
1340 for (i = 0; i < filter->source_count; ++i)
1342 struct parser_source *pin = filter->sources[i];
1344 if (!pin->pin.pin.peer)
1345 continue;
1347 IMemAllocator_Decommit(pin->pin.pAllocator);
1349 WakeConditionVariable(&pin->eos_cv);
1350 WaitForSingleObject(pin->thread, INFINITE);
1351 CloseHandle(pin->thread);
1352 pin->thread = NULL;
1355 return S_OK;
1358 static const struct strmbase_filter_ops filter_ops =
1360 .filter_get_pin = parser_get_pin,
1361 .filter_destroy = parser_destroy,
1362 .filter_init_stream = parser_init_stream,
1363 .filter_cleanup_stream = parser_cleanup_stream,
1366 static inline struct parser *impl_from_strmbase_sink(struct strmbase_sink *iface)
1368 return CONTAINING_RECORD(iface, struct parser, sink);
1371 static HRESULT sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
1373 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
1374 return S_OK;
1375 return S_FALSE;
1378 static HRESULT parser_sink_connect(struct strmbase_sink *iface, IPin *peer, const AM_MEDIA_TYPE *pmt)
1380 struct parser *filter = impl_from_strmbase_sink(iface);
1381 LONGLONG file_size, unused;
1382 HRESULT hr = S_OK;
1383 unsigned int i;
1385 filter->reader = NULL;
1386 if (FAILED(hr = IPin_QueryInterface(peer, &IID_IAsyncReader, (void **)&filter->reader)))
1387 return hr;
1389 IAsyncReader_Length(filter->reader, &file_size, &unused);
1391 filter->sink_connected = true;
1392 filter->read_thread = CreateThread(NULL, 0, read_thread, filter, 0, NULL);
1394 if (FAILED(hr = wg_parser_connect(filter->wg_parser, file_size)))
1395 goto err;
1397 if (!filter->init_gst(filter))
1399 hr = E_FAIL;
1400 goto err;
1403 for (i = 0; i < filter->source_count; ++i)
1405 struct parser_source *pin = filter->sources[i];
1407 pin->seek.llDuration = pin->seek.llStop = wg_parser_stream_get_duration(pin->wg_stream);
1408 pin->seek.llCurrent = 0;
1411 return S_OK;
1412 err:
1413 GST_RemoveOutputPins(filter);
1414 IAsyncReader_Release(filter->reader);
1415 filter->reader = NULL;
1416 return hr;
1419 static void parser_sink_disconnect(struct strmbase_sink *iface)
1421 struct parser *filter = impl_from_strmbase_sink(iface);
1423 GST_RemoveOutputPins(filter);
1425 IAsyncReader_Release(filter->reader);
1426 filter->reader = NULL;
1429 static const struct strmbase_sink_ops sink_ops =
1431 .base.pin_query_accept = sink_query_accept,
1432 .sink_connect = parser_sink_connect,
1433 .sink_disconnect = parser_sink_disconnect,
1436 static BOOL decodebin_parser_filter_init_gst(struct parser *filter)
1438 wg_parser_t parser = filter->wg_parser;
1439 unsigned int i, stream_count;
1440 WCHAR source_name[20];
1442 stream_count = wg_parser_get_stream_count(parser);
1443 for (i = 0; i < stream_count; ++i)
1445 swprintf(source_name, ARRAY_SIZE(source_name), L"Stream %02u", i);
1446 if (!create_pin(filter, wg_parser_get_stream(parser, i), source_name))
1447 return FALSE;
1450 return TRUE;
1453 static HRESULT decodebin_parser_source_query_accept(struct parser_source *pin, const AM_MEDIA_TYPE *mt)
1455 struct wg_format format;
1457 /* At least make sure we can convert it to wg_format. */
1458 return amt_to_wg_format(mt, &format) ? S_OK : S_FALSE;
1461 static HRESULT decodebin_parser_source_get_media_type(struct parser_source *pin,
1462 unsigned int index, AM_MEDIA_TYPE *mt)
1464 struct wg_format format;
1466 static const enum wg_video_format video_formats[] =
1468 /* Try to prefer YUV formats over RGB ones. Most decoders output in the
1469 * YUV color space, and it's generally much less expensive for
1470 * videoconvert to do YUV -> YUV transformations. */
1471 WG_VIDEO_FORMAT_AYUV,
1472 WG_VIDEO_FORMAT_I420,
1473 WG_VIDEO_FORMAT_YV12,
1474 WG_VIDEO_FORMAT_YUY2,
1475 WG_VIDEO_FORMAT_UYVY,
1476 WG_VIDEO_FORMAT_YVYU,
1477 WG_VIDEO_FORMAT_NV12,
1478 WG_VIDEO_FORMAT_BGRA,
1479 WG_VIDEO_FORMAT_BGRx,
1480 WG_VIDEO_FORMAT_BGR,
1481 WG_VIDEO_FORMAT_RGB16,
1482 WG_VIDEO_FORMAT_RGB15,
1485 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
1487 memset(mt, 0, sizeof(AM_MEDIA_TYPE));
1489 if (amt_from_wg_format(mt, &format, false))
1491 if (!index--)
1492 return S_OK;
1493 FreeMediaType(mt);
1496 if (format.major_type == WG_MAJOR_TYPE_VIDEO && index < ARRAY_SIZE(video_formats))
1498 format.u.video.format = video_formats[index];
1499 /* Downstream filters probably expect RGB video to be bottom-up. */
1500 if (format.u.video.height > 0 && wg_video_format_is_rgb(video_formats[index]))
1501 format.u.video.height = -format.u.video.height;
1502 if (!amt_from_wg_format(mt, &format, false))
1503 return E_OUTOFMEMORY;
1504 return S_OK;
1506 else if (format.major_type == WG_MAJOR_TYPE_AUDIO && !index)
1508 format.u.audio.format = WG_AUDIO_FORMAT_S16LE;
1509 if (!amt_from_wg_format(mt, &format, false))
1510 return E_OUTOFMEMORY;
1511 return S_OK;
1514 return VFW_S_NO_MORE_ITEMS;
1517 static HRESULT parser_create(enum wg_parser_type type, BOOL output_compressed, struct parser **parser)
1519 struct parser *object;
1521 if (!(object = calloc(1, sizeof(*object))))
1522 return E_OUTOFMEMORY;
1524 if (!(object->wg_parser = wg_parser_create(type, output_compressed)))
1526 free(object);
1527 return E_OUTOFMEMORY;
1530 InitializeCriticalSection(&object->streaming_cs);
1531 object->streaming_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": parser.streaming_cs");
1533 InitializeConditionVariable(&object->flushing_cv);
1535 *parser = object;
1536 return S_OK;
1539 HRESULT decodebin_parser_create(IUnknown *outer, IUnknown **out)
1541 struct parser *object;
1542 HRESULT hr;
1544 if (FAILED(hr = parser_create(WG_PARSER_DECODEBIN, FALSE, &object)))
1545 return hr;
1547 strmbase_filter_init(&object->filter, outer, &CLSID_decodebin_parser, &filter_ops);
1548 strmbase_sink_init(&object->sink, &object->filter, L"input pin", &sink_ops, NULL);
1550 object->init_gst = decodebin_parser_filter_init_gst;
1551 object->source_query_accept = decodebin_parser_source_query_accept;
1552 object->source_get_media_type = decodebin_parser_source_get_media_type;
1554 TRACE("Created GStreamer demuxer %p.\n", object);
1555 *out = &object->filter.IUnknown_inner;
1556 return S_OK;
1559 static struct parser *impl_from_IAMStreamSelect(IAMStreamSelect *iface)
1561 return CONTAINING_RECORD(iface, struct parser, IAMStreamSelect_iface);
1564 static HRESULT WINAPI stream_select_QueryInterface(IAMStreamSelect *iface, REFIID iid, void **out)
1566 struct parser *filter = impl_from_IAMStreamSelect(iface);
1567 return IUnknown_QueryInterface(filter->filter.outer_unk, iid, out);
1570 static ULONG WINAPI stream_select_AddRef(IAMStreamSelect *iface)
1572 struct parser *filter = impl_from_IAMStreamSelect(iface);
1573 return IUnknown_AddRef(filter->filter.outer_unk);
1576 static ULONG WINAPI stream_select_Release(IAMStreamSelect *iface)
1578 struct parser *filter = impl_from_IAMStreamSelect(iface);
1579 return IUnknown_Release(filter->filter.outer_unk);
1582 static HRESULT WINAPI stream_select_Count(IAMStreamSelect *iface, DWORD *count)
1584 struct parser *filter = impl_from_IAMStreamSelect(iface);
1585 TRACE("filter %p, count %p\n", filter, count);
1586 EnterCriticalSection(&filter->filter.filter_cs);
1587 if (filter->sink.pin.peer)
1588 *count = wg_parser_get_stream_count(filter->wg_parser);
1589 else
1590 *count = 0;
1591 LeaveCriticalSection(&filter->filter.filter_cs);
1592 return S_OK;
1595 static HRESULT WINAPI stream_select_Info(IAMStreamSelect *iface, LONG index,
1596 AM_MEDIA_TYPE **mt, DWORD *flags, LCID *lcid, DWORD *group, WCHAR **name,
1597 IUnknown **object, IUnknown **unknown)
1599 struct parser *filter = impl_from_IAMStreamSelect(iface);
1600 HRESULT hr = S_OK;
1602 FIXME("filter %p, index %ld, mt %p, flags %p, lcid %p, group %p, name %p, object %p, unknown %p, semi-stub!\n",
1603 filter, index, mt, flags, lcid, group, name, object, unknown);
1604 EnterCriticalSection(&filter->filter.filter_cs);
1606 if (!filter->sink.pin.peer)
1608 LeaveCriticalSection(&filter->filter.filter_cs);
1609 return VFW_E_NOT_CONNECTED;
1611 if (index < 0 || index >= filter->source_count)
1613 LeaveCriticalSection(&filter->filter.filter_cs);
1614 return S_FALSE;
1617 if (mt)
1618 *mt = CreateMediaType(&filter->sources[index]->pin.pin.mt);
1619 if (flags) /* todo */
1620 *flags = 0;
1621 if (lcid) /* todo */
1622 *lcid = 0;
1623 if (group) /* todo */
1624 *group = 0;
1625 if (name) /* todo */
1626 *name = NULL;
1627 if (object) /* todo */
1628 *object = NULL;
1629 if (unknown)
1630 *unknown = NULL;
1632 LeaveCriticalSection(&filter->filter.filter_cs);
1633 return hr;
1636 static HRESULT WINAPI stream_select_Enable(IAMStreamSelect *iface, LONG index, DWORD flags)
1638 FIXME("iface %p, index %ld, flags %#lx, stub!\n", iface, index, flags);
1639 return E_NOTIMPL;
1642 static const IAMStreamSelectVtbl stream_select_vtbl =
1644 stream_select_QueryInterface,
1645 stream_select_AddRef,
1646 stream_select_Release,
1647 stream_select_Count,
1648 stream_select_Info,
1649 stream_select_Enable,
1652 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface)
1654 struct parser_source *This = impl_from_IMediaSeeking(iface);
1655 TRACE("(%p)\n", This);
1656 return S_OK;
1659 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface)
1661 struct parser_source *This = impl_from_IMediaSeeking(iface);
1662 TRACE("(%p)\n", This);
1663 return S_OK;
1666 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface)
1668 struct parser_source *pin = impl_from_IMediaSeeking(iface);
1670 wg_parser_stream_seek(pin->wg_stream, pin->seek.dRate, 0, 0,
1671 AM_SEEKING_NoPositioning, AM_SEEKING_NoPositioning);
1672 return S_OK;
1675 static HRESULT WINAPI GST_Seeking_QueryInterface(IMediaSeeking *iface, REFIID riid, void **ppv)
1677 struct parser_source *This = impl_from_IMediaSeeking(iface);
1678 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
1681 static ULONG WINAPI GST_Seeking_AddRef(IMediaSeeking *iface)
1683 struct parser_source *This = impl_from_IMediaSeeking(iface);
1684 return IPin_AddRef(&This->pin.pin.IPin_iface);
1687 static ULONG WINAPI GST_Seeking_Release(IMediaSeeking *iface)
1689 struct parser_source *This = impl_from_IMediaSeeking(iface);
1690 return IPin_Release(&This->pin.pin.IPin_iface);
1693 static HRESULT WINAPI GST_Seeking_SetPositions(IMediaSeeking *iface,
1694 LONGLONG *current, DWORD current_flags, LONGLONG *stop, DWORD stop_flags)
1696 struct parser_source *pin = impl_from_IMediaSeeking(iface);
1697 struct parser *filter = impl_from_strmbase_filter(pin->pin.pin.filter);
1698 int i;
1700 TRACE("pin %p, current %s, current_flags %#lx, stop %s, stop_flags %#lx.\n",
1701 pin, current ? debugstr_time(*current) : "<null>", current_flags,
1702 stop ? debugstr_time(*stop) : "<null>", stop_flags);
1704 if (pin->pin.pin.filter->state == State_Stopped)
1706 SourceSeekingImpl_SetPositions(iface, current, current_flags, stop, stop_flags);
1707 return S_OK;
1710 if (!(current_flags & AM_SEEKING_NoFlush))
1712 for (i = 0; i < filter->source_count; ++i)
1714 if (filter->sources[i]->pin.pin.peer)
1715 IPin_BeginFlush(filter->sources[i]->pin.pin.peer);
1718 if (filter->reader)
1719 IAsyncReader_BeginFlush(filter->reader);
1722 /* Signal the streaming threads to "pause". */
1723 EnterCriticalSection(&filter->streaming_cs);
1724 filter->flushing = true;
1725 LeaveCriticalSection(&filter->streaming_cs);
1727 /* Acquire the flushing locks, to make sure the streaming threads really
1728 * are paused. This ensures the seek is serialized between flushes. */
1729 for (i = 0; i < filter->source_count; ++i)
1731 struct parser_source *flush_pin = filter->sources[i];
1733 if (flush_pin->pin.pin.peer)
1734 EnterCriticalSection(&flush_pin->flushing_cs);
1737 SourceSeekingImpl_SetPositions(iface, current, current_flags, stop, stop_flags);
1739 wg_parser_stream_seek(pin->wg_stream, pin->seek.dRate,
1740 pin->seek.llCurrent, pin->seek.llStop, current_flags, stop_flags);
1742 if (!(current_flags & AM_SEEKING_NoFlush))
1744 for (i = 0; i < filter->source_count; ++i)
1746 struct parser_source *flush_pin = filter->sources[i];
1748 if (flush_pin->pin.pin.peer)
1749 IPin_EndFlush(flush_pin->pin.pin.peer);
1752 if (filter->reader)
1753 IAsyncReader_EndFlush(filter->reader);
1756 /* Release the flushing locks. */
1757 for (i = filter->source_count - 1; i >= 0; --i)
1759 struct parser_source *flush_pin = filter->sources[i];
1761 flush_pin->need_segment = true;
1762 flush_pin->eos = false;
1764 if (flush_pin->pin.pin.peer)
1766 LeaveCriticalSection(&flush_pin->flushing_cs);
1767 WakeConditionVariable(&flush_pin->eos_cv);
1771 /* Signal the streaming threads to resume. */
1772 EnterCriticalSection(&filter->streaming_cs);
1773 filter->flushing = false;
1774 LeaveCriticalSection(&filter->streaming_cs);
1775 WakeConditionVariable(&filter->flushing_cv);
1777 return S_OK;
1780 static const IMediaSeekingVtbl GST_Seeking_Vtbl =
1782 GST_Seeking_QueryInterface,
1783 GST_Seeking_AddRef,
1784 GST_Seeking_Release,
1785 SourceSeekingImpl_GetCapabilities,
1786 SourceSeekingImpl_CheckCapabilities,
1787 SourceSeekingImpl_IsFormatSupported,
1788 SourceSeekingImpl_QueryPreferredFormat,
1789 SourceSeekingImpl_GetTimeFormat,
1790 SourceSeekingImpl_IsUsingTimeFormat,
1791 SourceSeekingImpl_SetTimeFormat,
1792 SourceSeekingImpl_GetDuration,
1793 SourceSeekingImpl_GetStopPosition,
1794 SourceSeekingImpl_GetCurrentPosition,
1795 SourceSeekingImpl_ConvertTimeFormat,
1796 GST_Seeking_SetPositions,
1797 SourceSeekingImpl_GetPositions,
1798 SourceSeekingImpl_GetAvailable,
1799 SourceSeekingImpl_SetRate,
1800 SourceSeekingImpl_GetRate,
1801 SourceSeekingImpl_GetPreroll
1804 static inline struct parser_source *impl_from_IQualityControl( IQualityControl *iface )
1806 return CONTAINING_RECORD(iface, struct parser_source, IQualityControl_iface);
1809 static HRESULT WINAPI GST_QualityControl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
1811 struct parser_source *pin = impl_from_IQualityControl(iface);
1812 return IPin_QueryInterface(&pin->pin.pin.IPin_iface, riid, ppv);
1815 static ULONG WINAPI GST_QualityControl_AddRef(IQualityControl *iface)
1817 struct parser_source *pin = impl_from_IQualityControl(iface);
1818 return IPin_AddRef(&pin->pin.pin.IPin_iface);
1821 static ULONG WINAPI GST_QualityControl_Release(IQualityControl *iface)
1823 struct parser_source *pin = impl_from_IQualityControl(iface);
1824 return IPin_Release(&pin->pin.pin.IPin_iface);
1827 static HRESULT WINAPI GST_QualityControl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality q)
1829 struct parser_source *pin = impl_from_IQualityControl(iface);
1830 uint64_t timestamp;
1831 int64_t diff;
1833 TRACE("pin %p, sender %p, type %s, proportion %ld, late %s, timestamp %s.\n",
1834 pin, sender, q.Type == Famine ? "Famine" : "Flood", q.Proportion,
1835 debugstr_time(q.Late), debugstr_time(q.TimeStamp));
1837 /* DirectShow filters sometimes pass negative timestamps (Audiosurf uses the
1838 * current time instead of the time of the last buffer). GstClockTime is
1839 * unsigned, so clamp it to 0. */
1840 timestamp = max(q.TimeStamp, 0);
1842 /* The documentation specifies that timestamp + diff must be nonnegative. */
1843 diff = q.Late;
1844 if (diff < 0 && timestamp < (uint64_t)-diff)
1845 diff = -timestamp;
1847 /* DirectShow "Proportion" describes what percentage of buffers the upstream
1848 * filter should keep (i.e. dropping the rest). If frames are late, the
1849 * proportion will be less than 1. For example, a proportion of 500 means
1850 * that the element should drop half of its frames, essentially because
1851 * frames are taking twice as long as they should to arrive.
1853 * GStreamer "proportion" is the inverse of this; it describes how much
1854 * faster the upstream element should produce frames. I.e. if frames are
1855 * taking twice as long as they should to arrive, we want the frames to be
1856 * decoded twice as fast, and so we pass 2.0 to GStreamer. */
1858 if (!q.Proportion)
1860 WARN("Ignoring quality message with zero proportion.\n");
1861 return S_OK;
1864 /* GST_QOS_TYPE_OVERFLOW is also used for buffers that arrive on time, but
1865 * DirectShow filters might use Famine, so check that there actually is an
1866 * underrun. */
1867 wg_parser_stream_notify_qos(pin->wg_stream, q.Type == Famine && q.Proportion < 1000,
1868 1000.0 / q.Proportion, diff, timestamp);
1870 return S_OK;
1873 static HRESULT WINAPI GST_QualityControl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
1875 struct parser_source *pin = impl_from_IQualityControl(iface);
1876 TRACE("(%p)->(%p)\n", pin, pin);
1877 /* Do nothing */
1878 return S_OK;
1881 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl = {
1882 GST_QualityControl_QueryInterface,
1883 GST_QualityControl_AddRef,
1884 GST_QualityControl_Release,
1885 GST_QualityControl_Notify,
1886 GST_QualityControl_SetSink
1889 static inline struct parser_source *impl_source_from_IPin(IPin *iface)
1891 return CONTAINING_RECORD(iface, struct parser_source, pin.pin.IPin_iface);
1894 static HRESULT source_query_interface(struct strmbase_pin *iface, REFIID iid, void **out)
1896 struct parser_source *pin = impl_source_from_IPin(&iface->IPin_iface);
1898 if (IsEqualGUID(iid, &IID_IMediaSeeking))
1899 *out = &pin->seek.IMediaSeeking_iface;
1900 else if (IsEqualGUID(iid, &IID_IQualityControl))
1901 *out = &pin->IQualityControl_iface;
1902 else
1903 return E_NOINTERFACE;
1905 IUnknown_AddRef((IUnknown *)*out);
1906 return S_OK;
1909 static HRESULT source_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
1911 struct parser_source *pin = impl_source_from_IPin(&iface->IPin_iface);
1912 struct parser *filter = impl_from_strmbase_filter(iface->filter);
1913 return filter->source_query_accept(pin, mt);
1916 static HRESULT source_get_media_type(struct strmbase_pin *iface, unsigned int index, AM_MEDIA_TYPE *mt)
1918 struct parser_source *pin = impl_source_from_IPin(&iface->IPin_iface);
1919 struct parser *filter = impl_from_strmbase_filter(iface->filter);
1920 return filter->source_get_media_type(pin, index, mt);
1923 static HRESULT WINAPI GSTOutPin_DecideBufferSize(struct strmbase_source *iface,
1924 IMemAllocator *allocator, ALLOCATOR_PROPERTIES *props)
1926 struct parser_source *pin = impl_source_from_IPin(&iface->pin.IPin_iface);
1927 unsigned int buffer_count = 1;
1928 unsigned int buffer_size = 16384;
1929 ALLOCATOR_PROPERTIES ret_props;
1931 if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_VideoInfo))
1933 VIDEOINFOHEADER *format = (VIDEOINFOHEADER *)pin->pin.pin.mt.pbFormat;
1934 buffer_size = format->bmiHeader.biSizeImage;
1936 else if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_MPEGVideo))
1938 MPEG1VIDEOINFO *format = (MPEG1VIDEOINFO *)pin->pin.pin.mt.pbFormat;
1939 buffer_size = format->hdr.bmiHeader.biSizeImage;
1940 buffer_count = 8;
1942 else if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_WaveFormatEx)
1943 && (IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_PCM)
1944 || IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_IEEE_FLOAT)))
1946 WAVEFORMATEX *format = (WAVEFORMATEX *)pin->pin.pin.mt.pbFormat;
1947 buffer_size = format->nAvgBytesPerSec;
1949 else if (IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_MPEG1AudioPayload)
1950 || IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_MP3))
1952 /* mpg123audiodec requires at least 3 buffers as it will keep
1953 * references to the last 2 samples. */
1954 buffer_count = 3;
1957 /* We do need to drop any buffers that might have been sent with the old
1958 * caps, but this will be handled in parser_init_stream(). */
1960 props->cBuffers = max(props->cBuffers, buffer_count);
1961 props->cbBuffer = max(props->cbBuffer, buffer_size);
1962 props->cbAlign = max(props->cbAlign, 1);
1963 return IMemAllocator_SetProperties(allocator, props, &ret_props);
1966 static void free_source_pin(struct parser_source *pin)
1968 if (pin->pin.pin.peer)
1970 if (SUCCEEDED(IMemAllocator_Decommit(pin->pin.pAllocator)))
1971 IPin_Disconnect(pin->pin.pin.peer);
1972 IPin_Disconnect(&pin->pin.pin.IPin_iface);
1975 pin->flushing_cs.DebugInfo->Spare[0] = 0;
1976 DeleteCriticalSection(&pin->flushing_cs);
1978 strmbase_seeking_cleanup(&pin->seek);
1979 strmbase_source_cleanup(&pin->pin);
1980 free(pin);
1983 static const struct strmbase_source_ops source_ops =
1985 .base.pin_query_interface = source_query_interface,
1986 .base.pin_query_accept = source_query_accept,
1987 .base.pin_get_media_type = source_get_media_type,
1988 .pfnAttemptConnection = BaseOutputPinImpl_AttemptConnection,
1989 .pfnDecideAllocator = BaseOutputPinImpl_DecideAllocator,
1990 .pfnDecideBufferSize = GSTOutPin_DecideBufferSize,
1993 static struct parser_source *create_pin(struct parser *filter,
1994 wg_parser_stream_t stream, const WCHAR *name)
1996 struct parser_source *pin, **new_array;
1998 if (!(new_array = realloc(filter->sources, (filter->source_count + 1) * sizeof(*filter->sources))))
1999 return NULL;
2000 filter->sources = new_array;
2002 if (!(pin = calloc(1, sizeof(*pin))))
2003 return NULL;
2005 pin->wg_stream = stream;
2006 strmbase_source_init(&pin->pin, &filter->filter, name, &source_ops);
2007 pin->IQualityControl_iface.lpVtbl = &GSTOutPin_QualityControl_Vtbl;
2008 strmbase_seeking_init(&pin->seek, &GST_Seeking_Vtbl, GST_ChangeStop,
2009 GST_ChangeCurrent, GST_ChangeRate);
2010 BaseFilterImpl_IncrementPinVersion(&filter->filter);
2012 InitializeCriticalSection(&pin->flushing_cs);
2013 pin->flushing_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": pin.flushing_cs");
2014 InitializeConditionVariable(&pin->eos_cv);
2016 filter->sources[filter->source_count++] = pin;
2017 return pin;
2020 static HRESULT GST_RemoveOutputPins(struct parser *This)
2022 unsigned int i;
2024 TRACE("(%p)\n", This);
2026 if (!This->sink_connected)
2027 return S_OK;
2029 for (i = 0; i < This->source_count; ++i)
2031 if (This->sources[i])
2032 free_source_pin(This->sources[i]);
2035 wg_parser_disconnect(This->wg_parser);
2037 /* read_thread() needs to stay alive to service any read requests GStreamer
2038 * sends, so we can only shut it down after GStreamer stops. */
2039 This->sink_connected = false;
2040 WaitForSingleObject(This->read_thread, INFINITE);
2041 CloseHandle(This->read_thread);
2043 This->source_count = 0;
2044 free(This->sources);
2045 This->sources = NULL;
2047 BaseFilterImpl_IncrementPinVersion(&This->filter);
2048 return S_OK;
2051 static BOOL compare_media_types(const AM_MEDIA_TYPE *a, const AM_MEDIA_TYPE *b)
2053 return IsEqualGUID(&a->majortype, &b->majortype)
2054 && IsEqualGUID(&a->subtype, &b->subtype)
2055 && IsEqualGUID(&a->formattype, &b->formattype)
2056 && a->cbFormat == b->cbFormat
2057 && !memcmp(a->pbFormat, b->pbFormat, a->cbFormat);
2060 static HRESULT wave_parser_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2062 if (!IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
2063 return S_FALSE;
2064 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WAVE))
2065 return S_OK;
2066 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_AU) || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_AIFF))
2067 FIXME("AU and AIFF files are not yet supported.\n");
2068 return S_FALSE;
2071 static const struct strmbase_sink_ops wave_parser_sink_ops =
2073 .base.pin_query_accept = wave_parser_sink_query_accept,
2074 .sink_connect = parser_sink_connect,
2075 .sink_disconnect = parser_sink_disconnect,
2078 static BOOL wave_parser_filter_init_gst(struct parser *filter)
2080 if (!create_pin(filter, wg_parser_get_stream(filter->wg_parser, 0), L"output"))
2081 return FALSE;
2083 return TRUE;
2086 static HRESULT wave_parser_source_query_accept(struct parser_source *pin, const AM_MEDIA_TYPE *mt)
2088 struct wg_format format;
2089 AM_MEDIA_TYPE pad_mt;
2090 HRESULT hr;
2092 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
2093 if (!amt_from_wg_format(&pad_mt, &format, false))
2094 return E_OUTOFMEMORY;
2095 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2096 FreeMediaType(&pad_mt);
2097 return hr;
2100 static HRESULT wave_parser_source_get_media_type(struct parser_source *pin,
2101 unsigned int index, AM_MEDIA_TYPE *mt)
2103 struct wg_format format;
2105 if (index > 0)
2106 return VFW_S_NO_MORE_ITEMS;
2107 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
2108 if (!amt_from_wg_format(mt, &format, false))
2109 return E_OUTOFMEMORY;
2110 return S_OK;
2113 HRESULT wave_parser_create(IUnknown *outer, IUnknown **out)
2115 struct parser *object;
2116 HRESULT hr;
2118 if (FAILED(hr = parser_create(WG_PARSER_WAVPARSE, FALSE, &object)))
2119 return hr;
2121 strmbase_filter_init(&object->filter, outer, &CLSID_WAVEParser, &filter_ops);
2122 strmbase_sink_init(&object->sink, &object->filter, L"input pin", &wave_parser_sink_ops, NULL);
2123 object->init_gst = wave_parser_filter_init_gst;
2124 object->source_query_accept = wave_parser_source_query_accept;
2125 object->source_get_media_type = wave_parser_source_get_media_type;
2127 TRACE("Created WAVE parser %p.\n", object);
2128 *out = &object->filter.IUnknown_inner;
2129 return S_OK;
2132 static HRESULT avi_splitter_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2134 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream)
2135 && IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_Avi))
2136 return S_OK;
2137 return S_FALSE;
2140 static const struct strmbase_sink_ops avi_splitter_sink_ops =
2142 .base.pin_query_accept = avi_splitter_sink_query_accept,
2143 .sink_connect = parser_sink_connect,
2144 .sink_disconnect = parser_sink_disconnect,
2147 static BOOL avi_splitter_filter_init_gst(struct parser *filter)
2149 wg_parser_t parser = filter->wg_parser;
2150 uint32_t i, stream_count;
2151 WCHAR source_name[20];
2153 stream_count = wg_parser_get_stream_count(parser);
2154 for (i = 0; i < stream_count; ++i)
2156 swprintf(source_name, ARRAY_SIZE(source_name), L"Stream %02u", i);
2157 if (!create_pin(filter, wg_parser_get_stream(parser, i), source_name))
2158 return FALSE;
2161 return TRUE;
2164 static HRESULT avi_splitter_source_query_accept(struct parser_source *pin, const AM_MEDIA_TYPE *mt)
2166 struct wg_format format;
2167 AM_MEDIA_TYPE pad_mt;
2168 HRESULT hr;
2170 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
2171 if (!amt_from_wg_format(&pad_mt, &format, false))
2172 return E_OUTOFMEMORY;
2173 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2174 FreeMediaType(&pad_mt);
2175 return hr;
2178 static HRESULT avi_splitter_source_get_media_type(struct parser_source *pin,
2179 unsigned int index, AM_MEDIA_TYPE *mt)
2181 struct wg_format format;
2183 if (index > 0)
2184 return VFW_S_NO_MORE_ITEMS;
2185 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
2186 if (!amt_from_wg_format(mt, &format, false))
2187 return E_OUTOFMEMORY;
2188 return S_OK;
2191 HRESULT avi_splitter_create(IUnknown *outer, IUnknown **out)
2193 struct parser *object;
2194 HRESULT hr;
2196 if (FAILED(hr = parser_create(WG_PARSER_AVIDEMUX, FALSE, &object)))
2197 return hr;
2199 strmbase_filter_init(&object->filter, outer, &CLSID_AviSplitter, &filter_ops);
2200 strmbase_sink_init(&object->sink, &object->filter, L"input pin", &avi_splitter_sink_ops, NULL);
2201 object->init_gst = avi_splitter_filter_init_gst;
2202 object->source_query_accept = avi_splitter_source_query_accept;
2203 object->source_get_media_type = avi_splitter_source_get_media_type;
2205 TRACE("Created AVI splitter %p.\n", object);
2206 *out = &object->filter.IUnknown_inner;
2207 return S_OK;
2210 static HRESULT mpeg_splitter_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2212 if (!IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
2213 return S_FALSE;
2214 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1Audio)
2215 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1System))
2216 return S_OK;
2217 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1Video)
2218 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
2219 FIXME("Unsupported subtype %s.\n", wine_dbgstr_guid(&mt->subtype));
2220 return S_FALSE;
2223 static const struct strmbase_sink_ops mpeg_splitter_sink_ops =
2225 .base.pin_query_accept = mpeg_splitter_sink_query_accept,
2226 .sink_connect = parser_sink_connect,
2227 .sink_disconnect = parser_sink_disconnect,
2230 static BOOL mpeg_splitter_filter_init_gst(struct parser *filter)
2232 wg_parser_t parser = filter->wg_parser;
2233 unsigned int i, stream_count;
2234 wg_parser_stream_t stream;
2235 struct wg_format fmt;
2237 stream_count = wg_parser_get_stream_count(parser);
2238 for (i = 0; i < stream_count; ++i)
2240 stream = wg_parser_get_stream(parser, i);
2241 wg_parser_stream_get_preferred_format(stream, &fmt);
2242 if (fmt.major_type == WG_MAJOR_TYPE_VIDEO_MPEG1)
2244 if (!create_pin(filter, wg_parser_get_stream(parser, i), L"Video"))
2245 return FALSE;
2247 else if (fmt.major_type == WG_MAJOR_TYPE_AUDIO_MPEG1)
2249 if (!create_pin(filter, wg_parser_get_stream(parser, i), L"Audio"))
2250 return FALSE;
2252 else FIXME("unexpected format %u\n", fmt.major_type);
2255 return TRUE;
2258 static HRESULT mpeg_splitter_source_query_accept(struct parser_source *pin, const AM_MEDIA_TYPE *mt)
2260 struct wg_format format;
2261 AM_MEDIA_TYPE pad_mt;
2262 HRESULT hr;
2264 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
2265 if (!amt_from_wg_format(&pad_mt, &format, false))
2266 return E_OUTOFMEMORY;
2267 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2268 FreeMediaType(&pad_mt);
2269 return hr;
2272 static HRESULT mpeg_splitter_source_get_media_type(struct parser_source *pin,
2273 unsigned int index, AM_MEDIA_TYPE *mt)
2275 struct wg_format format;
2277 if (index > 0)
2278 return VFW_S_NO_MORE_ITEMS;
2279 wg_parser_stream_get_preferred_format(pin->wg_stream, &format);
2280 if (!amt_from_wg_format(mt, &format, false))
2281 return E_OUTOFMEMORY;
2282 return S_OK;
2285 static HRESULT mpeg_splitter_query_interface(struct strmbase_filter *iface, REFIID iid, void **out)
2287 struct parser *filter = impl_from_strmbase_filter(iface);
2289 if (IsEqualGUID(iid, &IID_IAMStreamSelect))
2291 *out = &filter->IAMStreamSelect_iface;
2292 IUnknown_AddRef((IUnknown *)*out);
2293 return S_OK;
2296 return E_NOINTERFACE;
2299 static const struct strmbase_filter_ops mpeg_splitter_ops =
2301 .filter_query_interface = mpeg_splitter_query_interface,
2302 .filter_get_pin = parser_get_pin,
2303 .filter_destroy = parser_destroy,
2304 .filter_init_stream = parser_init_stream,
2305 .filter_cleanup_stream = parser_cleanup_stream,
2308 HRESULT mpeg_splitter_create(IUnknown *outer, IUnknown **out)
2310 struct parser *object;
2311 HRESULT hr;
2313 if (FAILED(hr = parser_create(WG_PARSER_DECODEBIN, TRUE, &object)))
2314 return hr;
2316 strmbase_filter_init(&object->filter, outer, &CLSID_MPEG1Splitter, &mpeg_splitter_ops);
2317 strmbase_sink_init(&object->sink, &object->filter, L"Input", &mpeg_splitter_sink_ops, NULL);
2318 object->IAMStreamSelect_iface.lpVtbl = &stream_select_vtbl;
2320 object->init_gst = mpeg_splitter_filter_init_gst;
2321 object->source_query_accept = mpeg_splitter_source_query_accept;
2322 object->source_get_media_type = mpeg_splitter_source_get_media_type;
2323 object->enum_sink_first = TRUE;
2325 TRACE("Created MPEG-1 splitter %p.\n", object);
2326 *out = &object->filter.IUnknown_inner;
2327 return S_OK;