winegstreamer: Do not allow timestamp + late to go negative in QOS messages.
[wine] / dlls / winegstreamer / gsttffilter.c
1 /*
2  * GStreamer wrapper filter
3  *
4  * Copyright 2010 Maarten Lankhorst for CodeWeavers
5  * Copyright 2010 Aric Stewart for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  */
22
23 #include "config.h"
24
25 #include <gst/app/gstappsink.h>
26 #include <gst/app/gstappsrc.h>
27 #include <gst/app/gstappbuffer.h>
28
29 #include "gst_private.h"
30 #include "gst_guids.h"
31
32 #include "uuids.h"
33 #include "mmreg.h"
34 #include "windef.h"
35 #include "winbase.h"
36 #include "dshow.h"
37 #include "strmif.h"
38 #include "vfwmsgs.h"
39 #include "dvdmedia.h"
40 #include "ks.h"
41 #include "ksmedia.h"
42 #include "msacm.h"
43
44 #include <assert.h>
45
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
48
49 #include "initguid.h"
50 DEFINE_GUID(WMMEDIASUBTYPE_MP3, 0x00000055, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
51
52 WINE_DEFAULT_DEBUG_CHANNEL(gstreamer);
53
54 struct typeinfo {
55     GstCaps *caps;
56     const char *type;
57 };
58
59 static const IBaseFilterVtbl GSTTf_Vtbl;
60
61 static gboolean match_element(GstPluginFeature *feature, gpointer gdata) {
62     struct typeinfo *data = (struct typeinfo*)gdata;
63     GstElementFactory *factory;
64     const GList *list;
65
66     if (!GST_IS_ELEMENT_FACTORY(feature))
67         return FALSE;
68     factory = GST_ELEMENT_FACTORY(feature);
69     if (!strstr(gst_element_factory_get_klass(factory), data->type))
70         return FALSE;
71     for (list = gst_element_factory_get_static_pad_templates(factory); list; list = list->next) {
72         GstStaticPadTemplate *pad = (GstStaticPadTemplate*)list->data;
73         GstCaps *caps;
74         gboolean ret;
75         if (pad->direction != GST_PAD_SINK)
76             continue;
77         caps = gst_static_caps_get(&pad->static_caps);
78         ret = gst_caps_is_always_compatible(caps, data->caps);
79         gst_caps_unref(caps);
80         if (ret)
81             return TRUE;
82     }
83     return FALSE;
84 }
85
86 static const char *Gstreamer_FindMatch(const char *strcaps)
87 {
88     struct typeinfo data;
89     GList *list, *copy;
90     guint bestrank = 0;
91     GstElementFactory *bestfactory = NULL;
92     GstCaps *caps = gst_caps_from_string(strcaps);
93
94     data.caps = caps;
95     data.type = "Decoder";
96     copy = gst_default_registry_feature_filter(match_element, 0, &data);
97     for (list = copy; list; list = list->next) {
98         GstElementFactory *factory = (GstElementFactory*)list->data;
99         guint rank;
100         rank = gst_plugin_feature_get_rank(GST_PLUGIN_FEATURE(factory));
101         if (rank > bestrank || !bestrank) {
102             bestrank = rank;
103             bestfactory = factory;
104         }
105     }
106     gst_caps_unref(caps);
107     g_list_free(copy);
108
109     if (!bestfactory) {
110         FIXME("Could not find plugin for %s\n", strcaps);
111         return NULL;
112     }
113     return gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(bestfactory));
114 }
115
116 typedef struct GstTfImpl {
117     TransformFilter tf;
118     IUnknown *seekthru_unk;
119     const char *gstreamer_name;
120     GstElement *filter;
121     GstPad *my_src, *my_sink, *their_src, *their_sink;
122     LONG cbBuffer;
123 } GstTfImpl;
124
125 static HRESULT WINAPI Gstreamer_transform_ProcessBegin(TransformFilter *iface) {
126     GstTfImpl *This = (GstTfImpl*)iface;
127     int ret;
128
129     ret = gst_element_set_state(This->filter, GST_STATE_PLAYING);
130     TRACE("Returned: %i\n", ret);
131     return S_OK;
132 }
133
134 static HRESULT WINAPI Gstreamer_transform_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
135 {
136     GstTfImpl *This = (GstTfImpl*)tf;
137     ALLOCATOR_PROPERTIES actual;
138
139     if (!ppropInputRequest->cbAlign)
140         ppropInputRequest->cbAlign = 1;
141
142     ppropInputRequest->cbBuffer = This->cbBuffer;
143
144     if (ppropInputRequest->cBuffers < 2)
145         ppropInputRequest->cBuffers = 2;
146
147     return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
148 }
149
150 static void release_sample(void *data) {
151     TRACE("Releasing %p\n", data);
152     IMediaSample_Release((IMediaSample *)data);
153 }
154
155 static GstFlowReturn got_data(GstPad *pad, GstBuffer *buf) {
156     GstTfImpl *This = gst_pad_get_element_private(pad);
157     IMediaSample *sample = GST_APP_BUFFER(buf)->priv;
158     REFERENCE_TIME tStart, tStop;
159     HRESULT hr;
160
161     if (GST_BUFFER_TIMESTAMP_IS_VALID(buf) &&
162         GST_BUFFER_DURATION_IS_VALID(buf)) {
163         tStart = buf->timestamp / 100;
164         tStop = tStart + buf->duration / 100;
165         IMediaSample_SetTime(sample, &tStart, &tStop);
166     }
167     else
168         IMediaSample_SetTime(sample, NULL, NULL);
169     if (GST_BUFFER_OFFSET_IS_VALID(buf) &&
170         GST_BUFFER_OFFSET_END_IS_VALID(buf)) {
171         tStart = buf->offset / 100;
172         tStop = buf->offset_end / 100;
173         IMediaSample_SetMediaTime(sample, &tStart, &tStop);
174     }
175     else
176         IMediaSample_SetMediaTime(sample, NULL, NULL);
177
178     IMediaSample_SetDiscontinuity(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DISCONT));
179     IMediaSample_SetPreroll(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_PREROLL));
180     IMediaSample_SetSyncPoint(sample, !GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DELTA_UNIT));
181
182     hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], sample);
183     gst_buffer_unref(buf);
184     if (FAILED(hr))
185         return GST_FLOW_WRONG_STATE;
186     if (hr != S_OK)
187         return GST_FLOW_RESEND;
188     return GST_FLOW_OK;
189 }
190
191 static GstFlowReturn request_buffer(GstPad *pad, guint64 ofs, guint size, GstCaps *caps, GstBuffer **buf) {
192     GstTfImpl *This = gst_pad_get_element_private(pad);
193     IMediaSample *sample;
194     BYTE *ptr;
195     HRESULT hr;
196     TRACE("Requesting buffer\n");
197
198     hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &sample, NULL, NULL, 0);
199     if (FAILED(hr)) {
200         ERR("Could not get output buffer: %08x\n", hr);
201         return GST_FLOW_WRONG_STATE;
202     }
203     IMediaSample_SetActualDataLength(sample, size);
204     IMediaSample_GetPointer(sample, &ptr);
205     *buf = gst_app_buffer_new(ptr, size, release_sample, sample);
206
207     if (!*buf) {
208         IMediaSample_Release(sample);
209         ERR("Out of memory\n");
210         return GST_FLOW_ERROR;
211     }
212     if (!caps)
213         caps = gst_pad_get_caps_reffed(This->my_sink);
214     gst_buffer_set_caps(*buf, caps);
215     return GST_FLOW_OK;
216 }
217
218 static HRESULT WINAPI Gstreamer_transform_ProcessData(TransformFilter *iface, IMediaSample *sample) {
219     GstTfImpl *This = (GstTfImpl*)iface;
220     REFERENCE_TIME tStart, tStop;
221     BYTE *data;
222     GstBuffer *buf;
223     HRESULT hr;
224     int ret;
225     TRACE("Reading %p\n", sample);
226
227     EnterCriticalSection(&This->tf.filter.csFilter);
228     IMediaSample_GetPointer(sample, &data);
229     buf = gst_app_buffer_new(data, IMediaSample_GetActualDataLength(sample), release_sample, sample);
230     if (!buf) {
231         LeaveCriticalSection(&This->tf.filter.csFilter);
232         return S_OK;
233     }
234     gst_buffer_set_caps(buf, gst_pad_get_caps_reffed(This->my_src));
235     IMediaSample_AddRef(sample);
236     buf->duration = buf->timestamp = -1;
237     hr = IMediaSample_GetTime(sample, &tStart, &tStop);
238     if (SUCCEEDED(hr)) {
239         buf->timestamp = tStart * 100;
240         if (hr == S_OK)
241             buf->duration = (tStop - tStart)*100;
242     }
243     if (IMediaSample_GetMediaTime(sample, &tStart, &tStop) == S_OK) {
244         buf->offset = tStart * 100;
245         buf->offset_end = tStop * 100;
246     }
247     if (IMediaSample_IsDiscontinuity(sample) == S_OK)
248         GST_BUFFER_FLAG_SET(buf, GST_BUFFER_FLAG_DISCONT);
249     if (IMediaSample_IsPreroll(sample) == S_OK)
250         GST_BUFFER_FLAG_SET(buf, GST_BUFFER_FLAG_PREROLL);
251     if (IMediaSample_IsSyncPoint(sample) != S_OK)
252         GST_BUFFER_FLAG_SET(buf, GST_BUFFER_FLAG_DELTA_UNIT);
253     LeaveCriticalSection(&This->tf.filter.csFilter);
254     ret = gst_pad_push(This->my_src, buf);
255     if (ret)
256         WARN("Sending returned: %i\n", ret);
257     if (ret == GST_FLOW_ERROR)
258         return E_FAIL;
259     if (ret == GST_FLOW_WRONG_STATE)
260         return VFW_E_WRONG_STATE;
261     if (ret == GST_FLOW_RESEND)
262         return S_FALSE;
263     return S_OK;
264 }
265
266 static HRESULT WINAPI Gstreamer_transform_ProcessEnd(TransformFilter *iface) {
267     GstTfImpl *This = (GstTfImpl*)iface;
268     int ret;
269
270     LeaveCriticalSection(&This->tf.filter.csFilter);
271     ret = gst_element_set_state(This->filter, GST_STATE_READY);
272     EnterCriticalSection(&This->tf.filter.csFilter);
273     TRACE("Returned: %i\n", ret);
274     return S_OK;
275 }
276
277 static void Gstreamer_transform_pad_added(GstElement *filter, GstPad *pad, GstTfImpl *This)
278 {
279     int ret;
280     if (!GST_PAD_IS_SRC(pad))
281         return;
282
283     ret = gst_pad_link(pad, This->my_sink);
284     if (ret < 0)
285         WARN("Failed to link with %i\n", ret);
286     This->their_src = pad;
287
288     gst_pad_set_active(pad, TRUE);
289     gst_pad_set_active(This->my_sink, TRUE);
290 }
291
292 static HRESULT Gstreamer_transform_ConnectInput(GstTfImpl *This, const AM_MEDIA_TYPE *amt, GstCaps *capsin, GstCaps *capsout) {
293     GstIterator *it;
294     int done = 0, found = 0, ret;
295
296     This->filter = gst_element_factory_make(This->gstreamer_name, NULL);
297     if (!This->filter) {
298         FIXME("Could not make %s filter\n", This->gstreamer_name);
299         return E_FAIL;
300     }
301     This->my_src = gst_pad_new(NULL, GST_PAD_SRC);
302     gst_pad_set_element_private (This->my_src, This);
303
304     This->my_sink = gst_pad_new(NULL, GST_PAD_SINK);
305     gst_pad_set_chain_function(This->my_sink, got_data);
306     gst_pad_set_bufferalloc_function(This->my_sink, request_buffer);
307     gst_pad_set_element_private (This->my_sink, This);
308
309     ret = gst_pad_set_caps(This->my_src, capsin);
310     if (ret < 0) {
311         WARN("Failed to set caps on own source with %i\n", ret);
312         return E_FAIL;
313     }
314
315     ret = gst_pad_set_caps(This->my_sink, capsout);
316     if (ret < 0) {
317         WARN("Failed to set caps on own sink with %i\n", ret);
318         return E_FAIL;
319     }
320
321     it = gst_element_iterate_sink_pads(This->filter);
322     while (!done) {
323         gpointer item;
324
325         switch (gst_iterator_next(it, &item)) {
326         case GST_ITERATOR_RESYNC:
327             gst_iterator_resync (it);
328             break;
329         case GST_ITERATOR_OK:
330             This->their_sink = item;
331         case GST_ITERATOR_ERROR:
332         case GST_ITERATOR_DONE:
333             done = 1;
334             break;
335         }
336     }
337     gst_iterator_free(it);
338     if (!This->their_sink) {
339         ERR("Could not find sink on filter %s\n", This->gstreamer_name);
340         return E_FAIL;
341     }
342
343     it = gst_element_iterate_src_pads(This->filter);
344     gst_iterator_resync(it);
345     done = 0;
346     while (!done) {
347         gpointer item;
348
349         switch (gst_iterator_next(it, &item)) {
350         case GST_ITERATOR_RESYNC:
351             gst_iterator_resync (it);
352             break;
353         case GST_ITERATOR_OK:
354             This->their_src = item;
355         case GST_ITERATOR_ERROR:
356         case GST_ITERATOR_DONE:
357             done = 1;
358             break;
359         }
360     }
361     gst_iterator_free(it);
362     found = !!This->their_src;
363     if (!found)
364         g_signal_connect(This->filter, "pad-added", G_CALLBACK(Gstreamer_transform_pad_added), This);
365     ret = gst_pad_link(This->my_src, This->their_sink);
366     if (ret < 0) {
367         WARN("Failed to link with %i\n", ret);
368         return E_FAIL;
369     }
370
371     if (found)
372         Gstreamer_transform_pad_added(This->filter, This->their_src, This);
373
374     if (!gst_pad_is_linked(This->my_sink))
375         return E_FAIL;
376
377     TRACE("Connected\n");
378     return S_OK;
379 }
380
381 static HRESULT WINAPI Gstreamer_transform_Cleanup(TransformFilter *tf, PIN_DIRECTION dir) {
382     GstTfImpl *This = (GstTfImpl*)tf;
383
384     if (dir == PINDIR_INPUT)
385     {
386         if (This->filter) {
387             gst_element_set_state(This->filter, GST_STATE_NULL);
388             gst_object_unref(This->filter);
389         }
390         This->filter = NULL;
391         if (This->my_src) {
392             gst_pad_unlink(This->my_src, This->their_sink);
393             gst_object_unref(This->my_src);
394         }
395         if (This->my_sink) {
396             gst_pad_unlink(This->their_src, This->my_sink);
397             gst_object_unref(This->my_sink);
398         }
399         This->my_sink = This->my_src = This->their_sink = This->their_src = NULL;
400         FIXME("%p stub\n", This);
401     }
402     return S_OK;
403 }
404
405 static HRESULT WINAPI Gstreamer_transform_EndOfStream(TransformFilter *iface) {
406     GstTfImpl *This = (GstTfImpl*)iface;
407     TRACE("%p\n", This);
408
409     gst_pad_push_event(This->my_src, gst_event_new_eos());
410     return S_OK;
411 }
412
413 static HRESULT WINAPI Gstreamer_transform_BeginFlush(TransformFilter *iface) {
414     GstTfImpl *This = (GstTfImpl*)iface;
415     TRACE("%p\n", This);
416
417     gst_pad_push_event(This->my_src, gst_event_new_flush_start());
418     return S_OK;
419 }
420
421 static HRESULT WINAPI Gstreamer_transform_EndFlush(TransformFilter *iface) {
422     GstTfImpl *This = (GstTfImpl*)iface;
423     TRACE("%p\n", This);
424
425     gst_pad_push_event(This->my_src, gst_event_new_flush_stop());
426     return S_OK;
427 }
428
429 static HRESULT WINAPI Gstreamer_transform_NewSegment(TransformFilter *iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate) {
430     GstTfImpl *This = (GstTfImpl*)iface;
431     TRACE("%p\n", This);
432
433     gst_pad_push_event(This->my_src, gst_event_new_new_segment_full(1,
434                        1.0, dRate, GST_FORMAT_TIME, 0, tStop <= tStart ? -1 : tStop * 100, tStart*100));
435     return S_OK;
436 }
437
438 static HRESULT WINAPI Gstreamer_transform_QOS(TransformFilter *iface, IBaseFilter *sender, Quality qm) {
439     GstTfImpl *This = (GstTfImpl*)iface;
440     REFERENCE_TIME late = qm.Late;
441     if (qm.Late < 0 && -qm.Late > qm.TimeStamp)
442         late = -qm.TimeStamp;
443     gst_pad_push_event(This->my_sink, gst_event_new_qos(1000. / qm.Proportion, late * 100, qm.TimeStamp * 100));
444     return QualityControlImpl_Notify((IQualityControl*)&iface->qcimpl, sender, qm);
445 }
446
447 static HRESULT Gstreamer_transform_create(IUnknown *punkout, const CLSID *clsid, const char *name, const TransformFilterFuncTable *vtbl, void **obj)
448 {
449     GstTfImpl *This;
450
451     if (FAILED(TransformFilter_Construct(&GSTTf_Vtbl, sizeof(GstTfImpl), clsid, vtbl, (IBaseFilter**)&This)))
452         return E_OUTOFMEMORY;
453     else
454     {
455         ISeekingPassThru *passthru;
456         CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
457         IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
458         ISeekingPassThru_Init(passthru, FALSE, (IPin*)This->tf.ppPins[0]);
459         ISeekingPassThru_Release(passthru);
460     }
461
462     This->gstreamer_name = name;
463     *obj = This;
464
465     return S_OK;
466 }
467
468 static HRESULT WINAPI Gstreamer_Mp3_QueryConnect(TransformFilter *iface, const AM_MEDIA_TYPE *amt) {
469     GstTfImpl *This = (GstTfImpl*)iface;
470     TRACE("%p %p\n", This, amt);
471     dump_AM_MEDIA_TYPE(amt);
472
473     if ( (!IsEqualGUID(&amt->majortype, &MEDIATYPE_Audio) &&
474           !IsEqualGUID(&amt->majortype, &MEDIATYPE_Stream)) ||
475          (!IsEqualGUID(&amt->subtype, &MEDIASUBTYPE_MPEG1AudioPayload) &&
476           !IsEqualGUID(&amt->subtype, &WMMEDIASUBTYPE_MP3))
477         || !IsEqualGUID(&amt->formattype, &FORMAT_WaveFormatEx))
478         return S_FALSE;
479
480     return S_OK;
481 }
482
483 static HRESULT WINAPI Gstreamer_Mp3_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE *amt) {
484     GstTfImpl *This = (GstTfImpl*)tf;
485     GstCaps *capsin, *capsout;
486     AM_MEDIA_TYPE *outpmt = &This->tf.pmt;
487     WAVEFORMATEX *wfx, *wfxin;
488     HRESULT hr;
489     int layer;
490
491     if (dir != PINDIR_INPUT)
492         return S_OK;
493
494     if (Gstreamer_Mp3_QueryConnect(&This->tf, amt) == S_FALSE || !amt->pbFormat)
495         return VFW_E_TYPE_NOT_ACCEPTED;
496
497     wfxin = (WAVEFORMATEX*)amt->pbFormat;
498     switch (wfxin->wFormatTag) {
499         case WAVE_FORMAT_MPEGLAYER3:
500             layer = 3;
501             break;
502         case WAVE_FORMAT_MPEG: {
503             MPEG1WAVEFORMAT *mpgformat = (MPEG1WAVEFORMAT*)wfxin;
504             layer = mpgformat->fwHeadLayer;
505             break;
506         }
507         default:
508             FIXME("Unhandled tag %x\n", wfxin->wFormatTag);
509             return E_FAIL;
510     }
511
512     FreeMediaType(outpmt);
513     CopyMediaType(outpmt, amt);
514
515     outpmt->subtype = MEDIASUBTYPE_PCM;
516     outpmt->formattype = FORMAT_WaveFormatEx;
517     outpmt->cbFormat = sizeof(*wfx);
518     CoTaskMemFree(outpmt->pbFormat);
519     wfx = CoTaskMemAlloc(outpmt->cbFormat);
520     outpmt->pbFormat = (BYTE*)wfx;
521     wfx->wFormatTag = WAVE_FORMAT_PCM;
522     wfx->wBitsPerSample = 16;
523     wfx->nSamplesPerSec = wfxin->nSamplesPerSec;
524     wfx->nChannels = wfxin->nChannels;
525     wfx->nBlockAlign = wfx->wBitsPerSample * wfx->nChannels / 8;
526     wfx->cbSize = 0;
527     wfx->nAvgBytesPerSec = wfx->nSamplesPerSec * wfx->nBlockAlign;
528
529     capsin = gst_caps_new_simple("audio/mpeg",
530                                  "mpegversion", G_TYPE_INT, 1,
531                                  "layer", G_TYPE_INT, layer,
532                                  "rate", G_TYPE_INT, wfx->nSamplesPerSec,
533                                  "channels", G_TYPE_INT, wfx->nChannels,
534                                  NULL);
535     capsout = gst_caps_new_simple("audio/x-raw-int",
536                                   "endianness", G_TYPE_INT, 1234,
537                                   "signed", G_TYPE_BOOLEAN, 1,
538                                   "width", G_TYPE_INT, 16,
539                                   "depth", G_TYPE_INT, 16,
540                                   "rate", G_TYPE_INT, wfx->nSamplesPerSec,
541                                   "channels", G_TYPE_INT, wfx->nChannels,
542                                    NULL);
543
544     hr = Gstreamer_transform_ConnectInput(This, amt, capsin, capsout);
545     gst_caps_unref(capsin);
546     gst_caps_unref(capsout);
547
548     This->cbBuffer = wfx->nAvgBytesPerSec / 4;
549
550     return hr;
551 }
552
553 static HRESULT WINAPI Gstreamer_Mp3_ConnectInput(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
554 {
555     return S_OK;
556 }
557
558 static const TransformFilterFuncTable Gstreamer_Mp3_vtbl = {
559     Gstreamer_transform_DecideBufferSize,
560     Gstreamer_transform_ProcessBegin,
561     Gstreamer_transform_ProcessData,
562     Gstreamer_transform_ProcessEnd,
563     Gstreamer_Mp3_QueryConnect,
564     Gstreamer_Mp3_SetMediaType,
565     Gstreamer_Mp3_ConnectInput,
566     Gstreamer_transform_Cleanup,
567     Gstreamer_transform_EndOfStream,
568     Gstreamer_transform_BeginFlush,
569     Gstreamer_transform_EndFlush,
570     Gstreamer_transform_NewSegment,
571     Gstreamer_transform_QOS
572 };
573
574 IUnknown * CALLBACK Gstreamer_Mp3_create(IUnknown *punkout, HRESULT *phr)
575 {
576     const char *plugin;
577     IUnknown *obj = NULL;
578     if (!Gstreamer_init())
579     {
580         *phr = E_FAIL;
581         return NULL;
582     }
583     plugin = Gstreamer_FindMatch("audio/mpeg, mpegversion=(int) 1");
584     if (!plugin)
585     {
586         *phr = E_FAIL;
587         return NULL;
588     }
589     *phr = Gstreamer_transform_create(punkout, &CLSID_Gstreamer_Mp3, plugin, &Gstreamer_Mp3_vtbl, (LPVOID*)&obj);
590     return obj;
591 }
592
593 static HRESULT WINAPI Gstreamer_YUV_QueryConnect(TransformFilter *iface, const AM_MEDIA_TYPE *amt) {
594     GstTfImpl *This = (GstTfImpl*)iface;
595     TRACE("%p %p\n", This, amt);
596     dump_AM_MEDIA_TYPE(amt);
597
598     if (!IsEqualGUID(&amt->majortype, &MEDIATYPE_Video) ||
599         (!IsEqualGUID(&amt->formattype, &FORMAT_VideoInfo) &&
600          !IsEqualGUID(&amt->formattype, &FORMAT_VideoInfo2)))
601         return S_FALSE;
602     if (memcmp(&amt->subtype.Data2, &MEDIATYPE_Video.Data2, sizeof(GUID) - sizeof(amt->subtype.Data1)))
603         return S_FALSE;
604     switch (amt->subtype.Data1) {
605         case mmioFOURCC('I','4','2','0'):
606         case mmioFOURCC('Y','V','1','2'):
607         case mmioFOURCC('N','V','1','2'):
608         case mmioFOURCC('N','V','2','1'):
609         case mmioFOURCC('Y','U','Y','2'):
610         case mmioFOURCC('Y','V','Y','U'):
611             return S_OK;
612         default:
613             WARN("Unhandled fourcc %s\n", debugstr_an((char*)&amt->subtype.Data1, 4));
614             return S_FALSE;
615     }
616 }
617
618 static HRESULT WINAPI Gstreamer_YUV_ConnectInput(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
619 {
620     return S_OK;
621 }
622
623 static HRESULT WINAPI Gstreamer_YUV_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir,  const AM_MEDIA_TYPE *amt) {
624     GstTfImpl *This = (GstTfImpl*)tf;
625     GstCaps *capsin, *capsout;
626     AM_MEDIA_TYPE *outpmt = &This->tf.pmt;
627     HRESULT hr;
628     int avgtime;
629     DWORD width, height;
630
631     if (dir != PINDIR_INPUT)
632         return S_OK;
633
634     if (Gstreamer_YUV_QueryConnect(&This->tf, amt) == S_FALSE || !amt->pbFormat)
635         return E_FAIL;
636
637     FreeMediaType(outpmt);
638     CopyMediaType(outpmt, amt);
639
640     if (IsEqualGUID(&amt->formattype, &FORMAT_VideoInfo)) {
641         VIDEOINFOHEADER *vih = (VIDEOINFOHEADER*)outpmt->pbFormat;
642         avgtime = vih->AvgTimePerFrame;
643         width = vih->bmiHeader.biWidth;
644         height = vih->bmiHeader.biHeight;
645         if ((LONG)vih->bmiHeader.biHeight > 0)
646             vih->bmiHeader.biHeight = -vih->bmiHeader.biHeight;
647         vih->bmiHeader.biBitCount = 24;
648         vih->bmiHeader.biCompression = BI_RGB;
649     } else {
650         VIDEOINFOHEADER2 *vih = (VIDEOINFOHEADER2*)outpmt->pbFormat;
651         avgtime = vih->AvgTimePerFrame;
652         width = vih->bmiHeader.biWidth;
653         height = vih->bmiHeader.biHeight;
654         if ((LONG)vih->bmiHeader.biHeight > 0)
655             vih->bmiHeader.biHeight = -vih->bmiHeader.biHeight;
656         vih->bmiHeader.biBitCount = 24;
657         vih->bmiHeader.biCompression = BI_RGB;
658     }
659     if (!avgtime)
660         avgtime = 10000000 / 30;
661
662     outpmt->subtype = MEDIASUBTYPE_RGB24;
663
664     capsin = gst_caps_new_simple("video/x-raw-yuv",
665                                  "format", GST_TYPE_FOURCC, amt->subtype.Data1,
666                                  "width", G_TYPE_INT, width,
667                                  "height", G_TYPE_INT, height,
668                                  "framerate", GST_TYPE_FRACTION, 10000000, avgtime,
669                                  NULL);
670     capsout = gst_caps_new_simple("video/x-raw-rgb",
671                                   "endianness", G_TYPE_INT, 4321,
672                                   "width", G_TYPE_INT, width,
673                                   "height", G_TYPE_INT, height,
674                                   "framerate", GST_TYPE_FRACTION, 10000000, avgtime,
675                                   "bpp", G_TYPE_INT, 24,
676                                   "depth", G_TYPE_INT, 24,
677                                   "red_mask", G_TYPE_INT, 0xff,
678                                   "green_mask", G_TYPE_INT, 0xff00,
679                                   "blue_mask", G_TYPE_INT, 0xff0000,
680                                    NULL);
681
682     hr = Gstreamer_transform_ConnectInput(This, amt, capsin, capsout);
683     gst_caps_unref(capsin);
684     gst_caps_unref(capsout);
685
686     This->cbBuffer = width * height * 4;
687     return hr;
688 }
689
690 static const TransformFilterFuncTable Gstreamer_YUV_vtbl = {
691     Gstreamer_transform_DecideBufferSize,
692     Gstreamer_transform_ProcessBegin,
693     Gstreamer_transform_ProcessData,
694     Gstreamer_transform_ProcessEnd,
695     Gstreamer_YUV_QueryConnect,
696     Gstreamer_YUV_SetMediaType,
697     Gstreamer_YUV_ConnectInput,
698     Gstreamer_transform_Cleanup,
699     Gstreamer_transform_EndOfStream,
700     Gstreamer_transform_BeginFlush,
701     Gstreamer_transform_EndFlush,
702     Gstreamer_transform_NewSegment,
703     Gstreamer_transform_QOS
704 };
705
706 IUnknown * CALLBACK Gstreamer_YUV_create(IUnknown *punkout, HRESULT *phr)
707 {
708     IUnknown *obj = NULL;
709     if (!Gstreamer_init())
710     {
711         *phr = E_FAIL;
712         return NULL;
713     }
714     *phr = Gstreamer_transform_create(punkout, &CLSID_Gstreamer_YUV, "ffmpegcolorspace", &Gstreamer_YUV_vtbl, (LPVOID*)&obj);
715     return obj;
716 }
717
718 static HRESULT WINAPI Gstreamer_AudioConvert_QueryConnect(TransformFilter *iface, const AM_MEDIA_TYPE *amt) {
719     GstTfImpl *This = (GstTfImpl*)iface;
720     TRACE("%p %p\n", This, amt);
721     dump_AM_MEDIA_TYPE(amt);
722
723     if (!IsEqualGUID(&amt->majortype, &MEDIATYPE_Audio) ||
724         !IsEqualGUID(&amt->subtype, &MEDIASUBTYPE_PCM) ||
725         !IsEqualGUID(&amt->formattype, &FORMAT_WaveFormatEx))
726         return S_FALSE;
727     return S_OK;
728 }
729
730 static HRESULT WINAPI Gstreamer_AudioConvert_ConnectInput(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
731 {
732     return S_OK;
733 }
734
735 static HRESULT WINAPI Gstreamer_AudioConvert_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE *amt) {
736     GstTfImpl *This = (GstTfImpl*)tf;
737     GstCaps *capsin, *capsout;
738     AM_MEDIA_TYPE *outpmt = &This->tf.pmt;
739     WAVEFORMATEX *inwfe;
740     WAVEFORMATEX *outwfe;
741     WAVEFORMATEXTENSIBLE *outwfx;
742     HRESULT hr;
743     int inisfloat = 0, indepth;
744
745     if (dir != PINDIR_INPUT)
746         return S_OK;
747
748     if (Gstreamer_AudioConvert_QueryConnect(&This->tf, amt) == S_FALSE || !amt->pbFormat)
749         return E_FAIL;
750
751     FreeMediaType(outpmt);
752     *outpmt = *amt;
753     outpmt->pUnk = NULL;
754     outpmt->cbFormat = sizeof(WAVEFORMATEXTENSIBLE);
755     outpmt->pbFormat = CoTaskMemAlloc(outpmt->cbFormat);
756
757     inwfe = (WAVEFORMATEX*)amt->pbFormat;
758     indepth = inwfe->wBitsPerSample;
759     if (inwfe->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
760         WAVEFORMATEXTENSIBLE *inwfx = (WAVEFORMATEXTENSIBLE*)inwfe;
761         inisfloat = IsEqualGUID(&inwfx->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT);
762         if (inwfx->Samples.wValidBitsPerSample)
763             indepth = inwfx->Samples.wValidBitsPerSample;
764     }
765
766     capsin = gst_caps_new_simple(inisfloat ? "audio/x-raw-float" : "audio/x-raw-int",
767                                  "endianness", G_TYPE_INT, 1234,
768                                  "width", G_TYPE_INT, inwfe->wBitsPerSample,
769                                  "depth", G_TYPE_INT, indepth,
770                                  "channels", G_TYPE_INT, inwfe->nChannels,
771                                  "rate", G_TYPE_INT, inwfe->nSamplesPerSec,
772                                   NULL);
773
774     outwfe = (WAVEFORMATEX*)outpmt->pbFormat;
775     outwfx = (WAVEFORMATEXTENSIBLE*)outwfe;
776     outwfe->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
777     outwfe->nChannels = 2;
778     outwfe->nSamplesPerSec = inwfe->nSamplesPerSec;
779     outwfe->wBitsPerSample = 16;
780     outwfe->nBlockAlign = outwfe->nChannels * outwfe->wBitsPerSample / 8;
781     outwfe->nAvgBytesPerSec = outwfe->nBlockAlign * outwfe->nSamplesPerSec;
782     outwfe->cbSize = sizeof(*outwfx) - sizeof(*outwfe);
783     outwfx->Samples.wValidBitsPerSample = outwfe->wBitsPerSample;
784     outwfx->dwChannelMask = SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT;
785     outwfx->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
786
787     capsout = gst_caps_new_simple("audio/x-raw-int",
788                                   "endianness", G_TYPE_INT, 1234,
789                                   "width", G_TYPE_INT, outwfe->wBitsPerSample,
790                                   "depth", G_TYPE_INT, outwfx->Samples.wValidBitsPerSample,
791                                   "channels", G_TYPE_INT, outwfe->nChannels,
792                                   "rate", G_TYPE_INT, outwfe->nSamplesPerSec,
793                                    NULL);
794
795     hr = Gstreamer_transform_ConnectInput(This, amt, capsin, capsout);
796     FIXME("%08x\n", hr);
797     gst_caps_unref(capsin);
798     gst_caps_unref(capsout);
799
800     This->cbBuffer = inwfe->nAvgBytesPerSec;
801     return hr;
802 }
803
804 static const TransformFilterFuncTable Gstreamer_AudioConvert_vtbl = {
805     Gstreamer_transform_DecideBufferSize,
806     Gstreamer_transform_ProcessBegin,
807     Gstreamer_transform_ProcessData,
808     Gstreamer_transform_ProcessEnd,
809     Gstreamer_AudioConvert_QueryConnect,
810     Gstreamer_AudioConvert_SetMediaType,
811     Gstreamer_AudioConvert_ConnectInput,
812     Gstreamer_transform_Cleanup,
813     Gstreamer_transform_EndOfStream,
814     Gstreamer_transform_BeginFlush,
815     Gstreamer_transform_EndFlush,
816     Gstreamer_transform_NewSegment,
817     Gstreamer_transform_QOS
818 };
819
820 IUnknown * CALLBACK Gstreamer_AudioConvert_create(IUnknown *punkout, HRESULT *phr)
821 {
822     IUnknown *obj = NULL;
823     if (!Gstreamer_init())
824     {
825         *phr = E_FAIL;
826         return NULL;
827     }
828     *phr = Gstreamer_transform_create(punkout, &CLSID_Gstreamer_AudioConvert, "audioconvert", &Gstreamer_AudioConvert_vtbl, (LPVOID*)&obj);
829     return obj;
830 }
831
832 HRESULT WINAPI GSTTf_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
833 {
834     HRESULT hr;
835     GstTfImpl *This = (GstTfImpl*)iface;
836     TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
837
838     if (IsEqualIID(riid, &IID_IMediaSeeking))
839         return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
840
841     hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
842
843     return hr;
844 }
845
846 static const IBaseFilterVtbl GSTTf_Vtbl =
847 {
848     GSTTf_QueryInterface,
849     BaseFilterImpl_AddRef,
850     TransformFilterImpl_Release,
851     BaseFilterImpl_GetClassID,
852     TransformFilterImpl_Stop,
853     TransformFilterImpl_Pause,
854     TransformFilterImpl_Run,
855     BaseFilterImpl_GetState,
856     BaseFilterImpl_SetSyncSource,
857     BaseFilterImpl_GetSyncSource,
858     BaseFilterImpl_EnumPins,
859     TransformFilterImpl_FindPin,
860     BaseFilterImpl_QueryFilterInfo,
861     BaseFilterImpl_JoinFilterGraph,
862     BaseFilterImpl_QueryVendorInfo
863 };