ea5a213bedd53d112190c5c605d8f12efbb95f29
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / directshow / dshowdecwrapper / gstdshowvideodec.cpp
1 /*
2  * GStreamer DirectShow codecs wrapper
3  * Copyright <2006, 2007, 2008, 2009, 2010> Fluendo <support@fluendo.com>
4  * Copyright <2006, 2007, 2008> Pioneers of the Inevitable <songbird@songbirdnest.com>
5  * Copyright <2007,2008> Sebastien Moutte <sebastien@moutte.net>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Alternatively, the contents of this file may be used under the
26  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
27  * which case the following provisions apply instead of the ones
28  * mentioned above:
29  *
30  * This library is free software; you can redistribute it and/or
31  * modify it under the terms of the GNU Library General Public
32  * License as published by the Free Software Foundation; either
33  * version 2 of the License, or (at your option) any later version.
34  *
35  * This library is distributed in the hope that it will be useful,
36  * but WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
38  * Library General Public License for more details.
39  *
40  * You should have received a copy of the GNU Library General Public
41  * License along with this library; if not, write to the
42  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
43  * Boston, MA 02110-1301, USA.
44  */
45
46 #ifdef HAVE_CONFIG_H
47 #include "config.h"
48 #endif
49
50 #include <dmoreg.h>
51 #include <wmcodecdsp.h>
52
53 #include "gstdshowvideodec.h"
54 #include <gst/video/video.h>
55
56 GST_DEBUG_CATEGORY_STATIC (dshowvideodec_debug);
57 #define GST_CAT_DEFAULT dshowvideodec_debug
58
59 #define gst_dshowvideodec_parent_class parent_class
60 G_DEFINE_TYPE(GstDshowVideoDec, gst_dshowvideodec, GST_TYPE_ELEMENT)
61
62 static void gst_dshowvideodec_finalize (GObject * object);
63 static GstStateChangeReturn gst_dshowvideodec_change_state
64     (GstElement * element, GstStateChange transition);
65
66 /* sink pad overwrites */
67 static gboolean gst_dshowvideodec_sink_setcaps (GstPad * pad, GstCaps * caps);
68 static gboolean gst_dshowvideodec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event);
69 static GstFlowReturn gst_dshowvideodec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer);
70
71 /* src pad overwrites */
72 static GstCaps *gst_dshowvideodec_src_getcaps (GstPad * pad);
73 static gboolean gst_dshowvideodec_src_setcaps (GstPad * pad, GstCaps * caps);
74
75 /* utils */
76 static gboolean gst_dshowvideodec_create_graph_and_filters (GstDshowVideoDec *
77     vdec);
78 static gboolean gst_dshowvideodec_destroy_graph_and_filters (GstDshowVideoDec *
79     vdec);
80 static gboolean gst_dshowvideodec_flush (GstDshowVideoDec * adec);
81 static gboolean gst_dshowvideodec_get_filter_output_format (GstDshowVideoDec *
82     vdec, const GUID subtype, VIDEOINFOHEADER ** format, guint * size);
83
84
85 #define GUID_MEDIATYPE_VIDEO    {0x73646976, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
86 #define GUID_MEDIASUBTYPE_WMVV1 {0x31564d57, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
87 #define GUID_MEDIASUBTYPE_WMVV2 {0x32564d57, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
88 #define GUID_MEDIASUBTYPE_WMVV3 {0x33564d57, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
89 #define GUID_MEDIASUBTYPE_WMVP  {0x50564d57, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
90 #define GUID_MEDIASUBTYPE_WMVA  {0x41564d57, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
91 #define GUID_MEDIASUBTYPE_WVC1  {0x31435657, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
92 #define GUID_MEDIASUBTYPE_CVID  {0x64697663, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
93 #define GUID_MEDIASUBTYPE_MP4S  {0x5334504d, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
94 #define GUID_MEDIASUBTYPE_MP42  {0x3234504d, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
95 #define GUID_MEDIASUBTYPE_MP43  {0x3334504d, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
96 #define GUID_MEDIASUBTYPE_M4S2  {0x3253344d, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
97 #define GUID_MEDIASUBTYPE_XVID  {0x44495658, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
98 #define GUID_MEDIASUBTYPE_DX50  {0x30355844, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
99 #define GUID_MEDIASUBTYPE_DIVX  {0x58564944, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
100 #define GUID_MEDIASUBTYPE_DIV3  {0x33564944, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
101
102 #define GUID_MEDIASUBTYPE_MPG4          {0x3447504d, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
103 #define GUID_MEDIASUBTYPE_MPEG1Payload  {0xe436eb81, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}}
104
105
106 /* output types */
107 #define GUID_MEDIASUBTYPE_YUY2    {0x32595559, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
108 #define GUID_MEDIASUBTYPE_YV12    {0x32315659, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }}
109 #define GUID_MEDIASUBTYPE_RGB32   {0xe436eb7e, 0x524f, 0x11ce, { 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70 }}
110 #define GUID_MEDIASUBTYPE_RGB565  {0xe436eb7b, 0x524f, 0x11ce, { 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70 }}
111
112 /* WMV always uses the WMV DMO */
113 static PreferredFilter preferred_wmv_filters[] = {
114   {&CLSID_CWMVDecMediaObject, &DMOCATEGORY_VIDEO_DECODER}, {0}
115 };
116
117 static const GUID CLSID_AVI_DECOMPRESSOR = 
118   {0xCF49D4E0, 0x1115, 0x11CE, 
119    {0xB0, 0x3A, 0x00, 0x20, 0xAF, 0x0B, 0xA7, 0x70}};
120 static PreferredFilter preferred_cinepack_filters[] = {
121   {&CLSID_AVI_DECOMPRESSOR}, {0}
122 };
123
124 /* Various MPEG-4 video variants */
125 // MPG4, mpg4, MP42, mp42
126 static PreferredFilter preferred_mpeg4_filters[] = {
127   {&CLSID_CMpeg4DecMediaObject, &DMOCATEGORY_VIDEO_DECODER}, {0}};
128 // MP4S, mp4s, M4S2, m4s2
129 static PreferredFilter preferred_mp4s_filters[] = {
130   {&CLSID_CMpeg4sDecMediaObject, &DMOCATEGORY_VIDEO_DECODER}, {0}};
131 // MP43, mp43
132 static PreferredFilter preferred_mp43_filters[] = {
133   {&CLSID_CMpeg43DecMediaObject, &DMOCATEGORY_VIDEO_DECODER}, {0}};
134
135 static const GUID CLSID_MPEG_VIDEO_DECODER = 
136   {0xFEB50740, 0x7BEF, 0x11CE, 
137    {0x9B, 0xD9, 0x00, 0x00, 0xE2, 0x02, 0x59, 0x9C}};
138 static PreferredFilter preferred_mpeg1_filters[] = {
139   {&CLSID_MPEG_VIDEO_DECODER}, {0}
140 };
141
142 /* video codecs array */
143 static const VideoCodecEntry video_dec_codecs[] = {
144   {"dshowvdec_wmv1", "Windows Media Video 7",
145    GST_MAKE_FOURCC ('W', 'M', 'V', '1'),
146    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_WMVV1,
147    "video/x-wmv, wmvversion = (int) 1",
148    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
149    GST_VIDEO_CAPS_MAKE("YUY2"),
150    preferred_wmv_filters},
151
152   {"dshowvdec_wmv2", "Windows Media Video 8",
153    GST_MAKE_FOURCC ('W', 'M', 'V', '2'),
154    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_WMVV2,
155    "video/x-wmv, wmvversion = (int) 2",
156    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
157    GST_VIDEO_CAPS_MAKE("YUY2"),
158    preferred_wmv_filters},
159
160   {"dshowvdec_wmv3", "Windows Media Video 9",
161    GST_MAKE_FOURCC ('W', 'M', 'V', '3'),
162    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_WMVV3,
163    "video/x-wmv, wmvversion = (int) 3, " "format = (string) WMV3",
164    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
165    GST_VIDEO_CAPS_MAKE("YUY2"),
166    preferred_wmv_filters},
167
168   {"dshowvdec_wmvp", "Windows Media Video 9 Image",
169    GST_MAKE_FOURCC ('W', 'M', 'V', 'P'),
170    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_WMVP,
171    "video/x-wmv, wmvversion = (int) 3, " "format = (string) { WMVP, MSS1 }",
172    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
173    GST_VIDEO_CAPS_MAKE("YUY2"),
174    preferred_wmv_filters},
175
176   {"dshowvdec_wmva", "Windows Media Video 9 Advanced",
177    GST_MAKE_FOURCC ('W', 'M', 'V', 'A'),
178    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_WMVA,
179    "video/x-wmv, wmvversion = (int) 3, " "format = (string) WMVA",
180    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
181    GST_VIDEO_CAPS_MAKE("YUY2"),
182    preferred_wmv_filters},
183
184    {"dshowvdec_wvc1", "Windows Media VC1 video",
185    GST_MAKE_FOURCC ('W', 'V', 'C', '1'),
186    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_WVC1,
187    "video/x-wmv, wmvversion = (int) 3, " "format = (string) WVC1",
188    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
189    GST_VIDEO_CAPS_MAKE("YUY2"),
190    preferred_wmv_filters},
191
192   {"dshowvdec_cinepak", "Cinepack",
193    0x64697663,
194    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_CVID,
195    "video/x-cinepak",
196    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_RGB32,
197    "video/x-raw, format=(string)RGB, bpp=(int)32, depth=(int)24, "
198        "endianness=(int)4321, red_mask=(int)65280, "
199        "green_mask=(int)16711680, blue_mask=(int)-16777216",
200    preferred_cinepack_filters},
201
202   {"dshowvdec_msmpeg41", "Microsoft ISO MPEG-4 version 1",
203    GST_MAKE_FOURCC ('M', 'P', '4', 'S'),
204    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_MP4S,
205    "video/x-msmpeg, msmpegversion=(int)41",
206    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
207    GST_VIDEO_CAPS_MAKE("YUY2"),
208    preferred_mp4s_filters},
209
210   {"dshowvdec_msmpeg42", "Microsoft ISO MPEG-4 version 2",
211    GST_MAKE_FOURCC ('M', 'P', '4', '2'),
212    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_MP42,
213    "video/x-msmpeg, msmpegversion=(int)42",
214    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
215    GST_VIDEO_CAPS_MAKE("YUY2"),
216    preferred_mpeg4_filters},
217
218   {"dshowvdec_msmpeg43", "Microsoft ISO MPEG-4 version 3",
219    GST_MAKE_FOURCC ('M', 'P', '4', '3'),
220    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_MP43,
221    "video/x-msmpeg, msmpegversion=(int)43",
222    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
223    GST_VIDEO_CAPS_MAKE("YUY2"),
224    preferred_mp43_filters},
225
226   {"dshowvdec_msmpeg4", "Microsoft ISO MPEG-4 version 1.1",
227    GST_MAKE_FOURCC ('M', '4', 'S', '2'),
228    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_M4S2,
229    "video/x-msmpeg, msmpegversion=(int)4",
230    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
231    GST_VIDEO_CAPS_MAKE("YUY2"),
232    preferred_mp4s_filters},
233
234   {"dshowvdec_mpeg1",
235    "MPEG-1 Video",
236    GST_MAKE_FOURCC ('M', 'P', 'E', 'G'),
237    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_MPEG1Payload,
238    "video/mpeg, mpegversion= (int) 1, "
239        "parsed= (boolean) true, " "systemstream= (boolean) false",
240    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
241    GST_VIDEO_CAPS_MAKE("YUY2"),
242    preferred_mpeg1_filters},
243    
244   {"dshowvdec_mpeg4", "MPEG-4 Video",
245    GST_MAKE_FOURCC ('M', 'P', 'G', '4'),
246    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_MPG4,
247    "video/mpeg, msmpegversion=(int)4",
248    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
249    GST_VIDEO_CAPS_MAKE("YUY2"),
250    preferred_mpeg4_filters},
251
252   /* The rest of these have no preferred filter; windows doesn't come
253    * with anything appropriate */
254   {"dshowvdec_xvid", "XVID Video",
255    GST_MAKE_FOURCC ('X', 'V', 'I', 'D'),
256    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_XVID,
257    "video/x-xvid",
258    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
259    GST_VIDEO_CAPS_MAKE("YUY2")},
260
261   {"dshowvdec_divx5", "DIVX 5.0 Video",
262    GST_MAKE_FOURCC ('D', 'X', '5', '0'),
263    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_DX50,
264    "video/x-divx, divxversion=(int)5",
265    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
266    GST_VIDEO_CAPS_MAKE("YUY2")},
267
268   {"dshowvdec_divx4", "DIVX 4.0 Video",
269    GST_MAKE_FOURCC ('D', 'I', 'V', 'X'),
270    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_DIVX,
271    "video/x-divx, divxversion=(int)4",
272    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
273    GST_VIDEO_CAPS_MAKE("YUY2")},
274
275   {"dshowvdec_divx3", "DIVX 3.0 Video",
276    GST_MAKE_FOURCC ('D', 'I', 'V', '3'),
277    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_MP43,
278    "video/x-divx, divxversion=(int)3",
279    GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
280    GST_VIDEO_CAPS_MAKE("YUY2")}
281 };
282
283 HRESULT VideoFakeSink::DoRenderSample(IMediaSample *pMediaSample)
284 {
285   gboolean in_seg = FALSE;
286   guint64 clip_start = 0, clip_stop = 0;
287   GstDshowVideoDecClass *klass =
288       (GstDshowVideoDecClass *) G_OBJECT_GET_CLASS (mDec);
289   GstBuffer *buf = NULL;
290   GstClockTime start, stop;
291   GstMapInfo map;
292
293   if(pMediaSample)
294   {
295     BYTE *pBuffer = NULL;
296     LONGLONG lStart = 0, lStop = 0;
297     long size = pMediaSample->GetActualDataLength();
298
299     pMediaSample->GetPointer(&pBuffer);
300     pMediaSample->GetTime(&lStart, &lStop);
301
302     start = lStart * 100;
303     stop = lStop * 100;
304     /* check if this buffer is in our current segment */
305     in_seg = gst_segment_clip (mDec->segment, GST_FORMAT_TIME,
306         start, stop, &clip_start, &clip_stop);
307
308     /* if the buffer is out of segment do not push it downstream */
309     if (!in_seg) {
310       GST_DEBUG_OBJECT (mDec,
311         "buffer is out of segment, start %" GST_TIME_FORMAT " stop %"
312         GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
313       goto done;
314     }
315
316     /* buffer is in our segment, allocate a new out buffer and clip its
317      * timestamps */
318     gst_buffer_pool_acquire_buffer(mDec->buffer_pool, &buf, NULL);
319     if (!buf) {
320       GST_WARNING_OBJECT (mDec,
321           "cannot allocate a new GstBuffer");
322       goto done;
323     }
324
325     /* set buffer properties */
326     GST_BUFFER_TIMESTAMP (buf) = clip_start;
327     GST_BUFFER_DURATION (buf) = clip_stop - clip_start;
328
329     gst_buffer_map(buf, &map, GST_MAP_WRITE);
330     if (strstr (klass->entry->srccaps, "rgb")) {
331       /* FOR RGB directshow decoder will return bottom-up BITMAP 
332        * There is probably a way to get top-bottom video frames from
333        * the decoder...
334        */
335       gint line = 0;
336       guint stride = mDec->width * 4;
337
338       for (; line < mDec->height; line++) {
339         memcpy (map.data + (line * stride),
340             pBuffer + (size - ((line + 1) * (stride))), stride);
341       }
342     } else {
343       memcpy (map.data, pBuffer, MIN ((unsigned int)size, map.size));
344     }
345     gst_buffer_unmap(buf, &map);
346
347     GST_LOG_OBJECT (mDec,
348         "push_buffer (size %d)=> pts %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
349         " duration %" GST_TIME_FORMAT, size,
350         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
351         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf)),
352         GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
353
354     /* push the buffer downstream */
355     mDec->last_ret = gst_pad_push (mDec->srcpad, buf);
356   }
357 done:
358
359   return S_OK;
360 }
361
362 HRESULT VideoFakeSink::CheckMediaType(const CMediaType *pmt)
363 {
364   if (pmt != NULL) {
365     if (*pmt == m_MediaType)
366       return S_OK;
367   }
368
369   return S_FALSE;
370 }
371
372 static void
373 gst_dshowvideodec_base_init (gpointer klass)
374 {
375   GstDshowVideoDecClass *videodec_class = (GstDshowVideoDecClass *) klass;
376   GstPadTemplate *src, *sink;
377   GstCaps *srccaps, *sinkcaps;
378   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
379   const VideoCodecEntry *tmp;
380   gpointer qdata;
381   gchar *longname, *description;
382
383   qdata = g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass), DSHOW_CODEC_QDATA);
384
385   /* element details */
386   tmp = videodec_class->entry = (VideoCodecEntry *) qdata;
387
388   longname = g_strdup_printf ("DirectShow %s Decoder Wrapper",
389       tmp->element_longname);
390   description = g_strdup_printf ("DirectShow %s Decoder Wrapper",
391       tmp->element_longname);
392
393   gst_element_class_set_metadata(element_class, longname, "Codec/Decoder/Video", description, 
394     "Sebastien Moutte <sebastien@moutte.net>");
395
396   g_free (longname);
397   g_free (description);
398
399   sinkcaps = gst_caps_from_string (tmp->sinkcaps);
400   gst_caps_set_simple (sinkcaps,
401       "width", GST_TYPE_INT_RANGE, 16, 4096,
402       "height", GST_TYPE_INT_RANGE, 16, 4096,
403       "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
404
405   srccaps = gst_caps_from_string (tmp->srccaps);
406
407   sink = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, sinkcaps);
408   src = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
409
410   gst_element_class_add_pad_template (element_class, src);
411   gst_element_class_add_pad_template (element_class, sink);
412
413   if (sinkcaps)
414     gst_caps_unref(sinkcaps);
415
416   if (srccaps)
417     gst_caps_unref(srccaps);
418 }
419
420 static void
421 gst_dshowvideodec_class_init (GstDshowVideoDecClass * klass)
422 {
423   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
424   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
425
426   gobject_class->finalize = gst_dshowvideodec_finalize;
427
428   gstelement_class->change_state =
429       GST_DEBUG_FUNCPTR (gst_dshowvideodec_change_state);
430
431   parent_class = (GstElementClass *) g_type_class_peek_parent (klass);
432 }
433
434 static void
435 gst_dshowvideodec_com_thread (GstDshowVideoDec * vdec)
436 {
437   HRESULT res;
438
439   g_mutex_lock (&vdec->com_init_lock);
440
441   /* Initialize COM with a MTA for this process. This thread will
442    * be the first one to enter the apartement and the last one to leave
443    * it, unitializing COM properly */
444
445   res = CoInitializeEx (0, COINIT_MULTITHREADED);
446   if (res == S_FALSE)
447     GST_WARNING_OBJECT (vdec, "COM has been already initialized in the same process");
448   else if (res == RPC_E_CHANGED_MODE)
449     GST_WARNING_OBJECT (vdec, "The concurrency model of COM has changed.");
450   else
451     GST_INFO_OBJECT (vdec, "COM initialized successfully");
452
453   vdec->comInitialized = TRUE;
454
455   /* Signal other threads waiting on this condition that COM was initialized */
456   g_cond_signal (&vdec->com_initialized);
457
458   g_mutex_unlock (&vdec->com_init_lock);
459
460   /* Wait until the uninitialize condition is met to leave the COM apartement */
461   g_mutex_lock (&vdec->com_deinit_lock);
462   g_cond_wait (&vdec->com_uninitialize, &vdec->com_deinit_lock);
463
464   CoUninitialize ();
465   GST_INFO_OBJECT (vdec, "COM uninitialized successfully");
466   vdec->comInitialized = FALSE;
467   g_cond_signal (&vdec->com_uninitialized);
468   g_mutex_unlock (&vdec->com_deinit_lock);
469 }
470
471 static void
472 gst_dshowvideodec_init (GstDshowVideoDec * vdec)
473 {
474   GstElementClass *element_class = GST_ELEMENT_GET_CLASS (vdec);
475
476   /* setup pads */
477   vdec->sinkpad =
478       gst_pad_new_from_template (gst_element_class_get_pad_template
479       (element_class, "sink"), "sink");
480
481   gst_pad_set_event_function (vdec->sinkpad, gst_dshowvideodec_sink_event);
482   gst_pad_set_chain_function (vdec->sinkpad, gst_dshowvideodec_chain);
483   gst_element_add_pad (GST_ELEMENT (vdec), vdec->sinkpad);
484
485   vdec->srcpad =
486       gst_pad_new_from_template (gst_element_class_get_pad_template
487       (element_class, "src"), "src");
488 /* needed to implement caps negotiation on our src pad */
489 /*  gst_pad_set_getcaps_function (vdec->srcpad, gst_dshowvideodec_src_getcaps);
490   gst_pad_set_setcaps_function (vdec->srcpad, gst_dshowvideodec_src_setcaps);*/
491   gst_element_add_pad (GST_ELEMENT (vdec), vdec->srcpad);
492
493   vdec->fakesrc = NULL;
494   vdec->fakesink = NULL;
495   vdec->decfilter = NULL;
496
497   vdec->last_ret = GST_FLOW_OK;
498
499   vdec->filtergraph = NULL;
500   vdec->mediafilter = NULL;
501   vdec->srccaps = NULL;
502   vdec->segment = gst_segment_new ();
503
504   vdec->setup = FALSE;
505   vdec->buffer_pool = NULL;
506
507   g_mutex_init (&vdec->com_init_lock);
508   g_mutex_init (&vdec->com_deinit_lock);
509   g_cond_init (&vdec->com_initialized);
510   g_cond_init (&vdec->com_uninitialize);
511   g_cond_init (&vdec->com_uninitialized);
512
513   g_mutex_lock (&vdec->com_init_lock);
514
515   /* create the COM initialization thread */
516   g_thread_new ("COM Init Thread", (GThreadFunc)gst_dshowvideodec_com_thread,
517     vdec);
518
519   /* wait until the COM thread signals that COM has been initialized */
520   g_cond_wait (&vdec->com_initialized, &vdec->com_init_lock);
521   g_mutex_unlock (&vdec->com_init_lock);
522 }
523
524 static void
525 gst_dshowvideodec_finalize (GObject * object)
526 {
527   GstDshowVideoDec *vdec = (GstDshowVideoDec *) (object);
528
529   if (vdec->segment) {
530     gst_segment_free (vdec->segment);
531     vdec->segment = NULL;
532   }
533
534   if(vdec->buffer_pool) {
535     gst_object_unref(vdec->buffer_pool);
536     vdec->buffer_pool = NULL;
537   }
538
539   /* signal the COM thread that it sould uninitialize COM */
540   if (vdec->comInitialized) {
541     g_mutex_lock (&vdec->com_deinit_lock);
542     g_cond_signal (&vdec->com_uninitialize);
543     g_cond_wait (&vdec->com_uninitialized, &vdec->com_deinit_lock);
544     g_mutex_unlock (&vdec->com_deinit_lock);
545   }
546
547   g_mutex_clear (&vdec->com_init_lock);
548   g_mutex_clear (&vdec->com_deinit_lock);
549   g_cond_clear (&vdec->com_initialized);
550   g_cond_clear (&vdec->com_uninitialize);
551   g_cond_clear (&vdec->com_uninitialized);
552
553   G_OBJECT_CLASS (parent_class)->finalize (object);
554 }
555
556 static GstStateChangeReturn
557 gst_dshowvideodec_change_state (GstElement * element, GstStateChange transition)
558 {
559   GstDshowVideoDec *vdec = (GstDshowVideoDec *) (element);
560
561   switch (transition) {
562     case GST_STATE_CHANGE_NULL_TO_READY:
563       if (!gst_dshowvideodec_create_graph_and_filters (vdec))
564         return GST_STATE_CHANGE_FAILURE;
565       break;
566     case GST_STATE_CHANGE_READY_TO_PAUSED:
567       break;
568     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
569       break;
570     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
571       break;
572     case GST_STATE_CHANGE_PAUSED_TO_READY:
573       break;
574     case GST_STATE_CHANGE_READY_TO_NULL:
575       if (!gst_dshowvideodec_destroy_graph_and_filters (vdec))
576         return GST_STATE_CHANGE_FAILURE;
577       break;
578     default:
579       break;
580   }
581
582   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
583 }
584
585 static gboolean
586 gst_dshowvideodec_sink_setcaps (GstPad * pad, GstCaps * caps)
587 {
588   gboolean ret = FALSE;
589   HRESULT hres;
590   GstStructure *s = gst_caps_get_structure (caps, 0);
591   GstDshowVideoDec *vdec = (GstDshowVideoDec *) gst_pad_get_parent (pad);
592   GstDshowVideoDecClass *klass =
593       (GstDshowVideoDecClass *) G_OBJECT_GET_CLASS (vdec);
594   GstBuffer *extradata = NULL;
595   gsize extra_size;
596   const GValue *v = NULL;
597   guint size = 0;
598   GstCaps *caps_out = NULL;
599   AM_MEDIA_TYPE output_mediatype, input_mediatype;
600   VIDEOINFOHEADER *input_vheader = NULL, *output_vheader = NULL;
601   IPinPtr output_pin;
602   IPinPtr input_pin;
603   IBaseFilter *srcfilter = NULL;
604   IBaseFilter *sinkfilter = NULL;
605   const GValue *fps, *par;
606   GstQuery *query = NULL;
607   GstBufferPool *pool = NULL;
608   GstStructure *pool_config = NULL;
609   guint pool_size, pool_min, pool_max;
610
611   /* read data */
612   if (!gst_structure_get_int (s, "width", &vdec->width) ||
613       !gst_structure_get_int (s, "height", &vdec->height)) {
614     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
615         ("error getting video width or height from caps"), (NULL));
616     goto end;
617   }
618   fps = gst_structure_get_value (s, "framerate");
619   if (fps) {
620     vdec->fps_n = gst_value_get_fraction_numerator (fps);
621     vdec->fps_d = gst_value_get_fraction_denominator (fps);
622   }
623   else {
624     /* Invent a sane default framerate; the timestamps matter
625      * more anyway. */
626     vdec->fps_n = 25;
627     vdec->fps_d = 1;
628   }
629
630   par = gst_structure_get_value (s, "pixel-aspect-ratio");
631   if (par) {
632     vdec->par_n = gst_value_get_fraction_numerator (par);
633     vdec->par_d = gst_value_get_fraction_denominator (par);
634   }
635   else {
636     vdec->par_n = vdec->par_d = 1;
637   }
638
639   if ((v = gst_structure_get_value (s, "codec_data"))) {
640     extradata = gst_value_get_buffer (v);
641     extra_size = gst_buffer_get_size(extradata);
642   }
643
644   /* define the input type format */
645   memset (&input_mediatype, 0, sizeof (AM_MEDIA_TYPE));
646   input_mediatype.majortype = klass->entry->input_majortype;
647   input_mediatype.subtype = klass->entry->input_subtype;
648   input_mediatype.bFixedSizeSamples = FALSE;
649   input_mediatype.bTemporalCompression = TRUE;
650
651   if (strstr (klass->entry->sinkcaps, "video/mpeg, mpegversion= (int) 1")) {
652     size =
653         sizeof (MPEG1VIDEOINFO) + (extradata ? extra_size - 1 : 0);
654     input_vheader = (VIDEOINFOHEADER *)g_malloc0 (size);
655
656     input_vheader->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
657     if (extradata) {
658       MPEG1VIDEOINFO *mpeg_info = (MPEG1VIDEOINFO *) input_vheader;
659
660       gst_buffer_extract(extradata, 0, mpeg_info->bSequenceHeader, extra_size);
661       mpeg_info->cbSequenceHeader = extra_size;
662     }
663     input_mediatype.formattype = FORMAT_MPEGVideo;
664   } else {
665     size =
666         sizeof (VIDEOINFOHEADER) + (extradata ? extra_size : 0);
667     input_vheader = (VIDEOINFOHEADER *)g_malloc0 (size);
668     input_vheader->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
669
670     if (extradata) {            /* Codec data is appended after our header */
671       gst_buffer_extract(extradata, 0,
672         ((guchar *) input_vheader) + sizeof (VIDEOINFOHEADER), extra_size);
673       input_vheader->bmiHeader.biSize += extra_size;
674     }
675     input_mediatype.formattype = FORMAT_VideoInfo;
676   }
677
678   input_vheader->rcSource.top = input_vheader->rcSource.left = 0;
679   input_vheader->rcSource.right = vdec->width;
680   input_vheader->rcSource.bottom = vdec->height;
681   input_vheader->rcTarget = input_vheader->rcSource;
682   input_vheader->bmiHeader.biWidth = vdec->width;
683   input_vheader->bmiHeader.biHeight = vdec->height;
684   input_vheader->bmiHeader.biPlanes = 1;
685   input_vheader->bmiHeader.biBitCount = 16;
686   input_vheader->bmiHeader.biCompression = klass->entry->format;
687   input_vheader->bmiHeader.biSizeImage =
688       (vdec->width * vdec->height) * (input_vheader->bmiHeader.biBitCount / 8);
689
690   input_mediatype.cbFormat = size;
691   input_mediatype.pbFormat = (BYTE *) input_vheader;
692   input_mediatype.lSampleSize = input_vheader->bmiHeader.biSizeImage;
693
694   vdec->fakesrc->GetOutputPin()->SetMediaType(&input_mediatype);
695
696   /* set the sample size for fakesrc filter to the output buffer size */
697   vdec->fakesrc->GetOutputPin()->SetSampleSize(input_mediatype.lSampleSize);
698
699   /* connect our fake src to decoder */
700   hres = vdec->fakesrc->QueryInterface(IID_IBaseFilter,
701       (void **) &srcfilter);
702   if (FAILED (hres)) {
703     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
704       ("Can't QT fakesrc to IBaseFilter: %x", hres), (NULL));
705     goto end;
706   }
707
708   output_pin = gst_dshow_get_pin_from_filter (srcfilter, PINDIR_OUTPUT);
709   if (!output_pin) {
710     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
711         ("Can't get output pin from our directshow fakesrc filter"), (NULL));
712     goto end;
713   }
714   input_pin = gst_dshow_get_pin_from_filter (vdec->decfilter, PINDIR_INPUT);
715   if (!input_pin) {
716     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
717         ("Can't get input pin from decoder filter"), (NULL));
718     goto end;
719   }
720
721   hres = vdec->filtergraph->ConnectDirect (output_pin, input_pin, NULL);
722   if (hres != S_OK) {
723     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
724         ("Can't connect fakesrc with decoder (error=%x)", hres), (NULL));
725     goto end;
726   }
727
728   /* get decoder output video format */
729   if (!gst_dshowvideodec_get_filter_output_format (vdec,
730           klass->entry->output_subtype, &output_vheader, &size)) {
731     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
732         ("Can't get decoder output video format"), (NULL));
733     goto end;
734   }
735
736   memset (&output_mediatype, 0, sizeof (AM_MEDIA_TYPE));
737   output_mediatype.majortype = klass->entry->output_majortype;
738   output_mediatype.subtype = klass->entry->output_subtype;
739   output_mediatype.bFixedSizeSamples = TRUE;
740   output_mediatype.bTemporalCompression = FALSE;
741   output_mediatype.lSampleSize = output_vheader->bmiHeader.biSizeImage;
742   output_mediatype.formattype = FORMAT_VideoInfo;
743   output_mediatype.cbFormat = size;
744   output_mediatype.pbFormat = (BYTE *) output_vheader;
745
746   vdec->fakesink->SetMediaType (&output_mediatype);
747
748   /* connect decoder to our fake sink */
749   output_pin = gst_dshow_get_pin_from_filter (vdec->decfilter, PINDIR_OUTPUT);
750   if (!output_pin) {
751     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
752         ("Can't get output pin from our decoder filter"), (NULL));
753     goto end;
754   }
755
756   hres = vdec->fakesink->QueryInterface(IID_IBaseFilter,
757       (void **) &sinkfilter);
758   if (FAILED (hres)) {
759     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
760       ("Can't QT fakesink to IBaseFilter: %x", hres), (NULL));
761     goto end;
762   }
763
764   input_pin = gst_dshow_get_pin_from_filter (sinkfilter, PINDIR_INPUT);
765   if (!input_pin) {
766     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
767         ("Can't get input pin from our directshow fakesink filter"), (NULL));
768     goto end;
769   }
770
771   hres = vdec->filtergraph->ConnectDirect(output_pin, input_pin,
772       &output_mediatype);
773   if (hres != S_OK) {
774     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
775         ("Can't connect decoder with fakesink (error=%x)", hres), (NULL));
776     goto end;
777   }
778
779   /* negotiate output */
780   caps_out = gst_caps_from_string (klass->entry->srccaps);
781   gst_caps_set_simple (caps_out,
782       "width", G_TYPE_INT, vdec->width,
783       "height", G_TYPE_INT, vdec->height, NULL);
784
785   if (vdec->fps_n && vdec->fps_d) {
786       gst_caps_set_simple (caps_out,
787           "framerate", GST_TYPE_FRACTION, vdec->fps_n, vdec->fps_d, NULL);
788   }
789
790   gst_caps_set_simple (caps_out, 
791       "pixel-aspect-ratio", GST_TYPE_FRACTION, vdec->par_n, vdec->par_d, NULL);
792
793   if (!gst_pad_set_caps (vdec->srcpad, caps_out)) {
794     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
795         ("Failed to negotiate output"), (NULL));
796     goto end;
797   }
798
799   /* request or create a buffer pool */
800   if (vdec->buffer_pool) {
801     gst_object_unref (vdec->buffer_pool);
802   }
803
804   query = gst_query_new_allocation(caps_out, TRUE);
805   gst_pad_peer_query(vdec->srcpad, query);
806
807   if (gst_query_get_n_allocation_pools (query) > 0) {
808     gst_query_parse_nth_allocation_pool (query, 0, &pool, &pool_size, &pool_min,
809       &pool_max);
810   }
811   else {
812     pool = NULL;
813     pool_size = output_mediatype.lSampleSize;
814     pool_min = 1;
815     pool_max = 0;
816   }
817
818   if (pool == NULL) {
819     pool = gst_video_buffer_pool_new ();
820   }
821
822   if (!pool) {
823     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
824         ("Could not create buffer bool"), (NULL));
825     goto end;
826   }
827
828   pool_config = gst_buffer_pool_get_config (pool);
829   gst_buffer_pool_config_set_params (pool_config, caps_out, pool_size,
830     pool_min, pool_max);
831   gst_buffer_pool_set_config (pool, pool_config);
832
833   if (!gst_buffer_pool_set_active (pool, TRUE)) {
834     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
835       ("Failed set buffer pool active"), (NULL));
836     goto end;
837   }
838
839   vdec->buffer_pool = pool;
840
841   hres = vdec->mediafilter->Run (-1);
842   if (hres != S_OK) {
843     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
844         ("Can't run the directshow graph (error=%d)", hres), (NULL));
845     goto end;
846   }
847
848   ret = TRUE;
849 end:
850   if (caps_out)
851     gst_caps_unref (caps_out);
852   gst_object_unref (vdec);
853   g_free (input_vheader);
854   if (srcfilter)
855     srcfilter->Release();
856   if (sinkfilter)
857     sinkfilter->Release();
858   if (query)
859     gst_query_unref(query);
860   return ret;
861 }
862
863 static gboolean
864 gst_dshowvideodec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
865 {
866   gboolean ret = TRUE;
867   GstDshowVideoDec *vdec = (GstDshowVideoDec *) gst_pad_get_parent (pad);
868
869   switch (GST_EVENT_TYPE (event)) {
870     case GST_EVENT_CAPS:
871       GstCaps *caps;
872       gst_event_parse_caps(event, &caps);
873       ret = gst_dshowvideodec_sink_setcaps(pad, caps);
874       break;
875
876     case GST_EVENT_FLUSH_STOP:
877       gst_dshowvideodec_flush (vdec);
878       ret = gst_pad_event_default (pad, parent, event);
879       break;
880     case GST_EVENT_SEGMENT:
881     {
882       const GstSegment *segment;
883
884       gst_event_parse_segment (event, &segment);
885
886       /* save the new segment in our local current segment */
887       gst_segment_copy_into(segment, vdec->segment);
888
889       GST_CAT_DEBUG_OBJECT (dshowvideodec_debug, vdec,
890           "new segment received => start=%" GST_TIME_FORMAT " stop=%"
891           GST_TIME_FORMAT, GST_TIME_ARGS (vdec->segment->start),
892           GST_TIME_ARGS (vdec->segment->stop));
893
894       ret = gst_pad_event_default (pad, parent, event);
895       break;
896     }
897     default:
898       ret = gst_pad_event_default (pad, parent, event);
899       break;
900   }
901
902   gst_object_unref (vdec);
903
904   return ret;
905 }
906
907 static GstFlowReturn
908 gst_dshowvideodec_chain (GstPad * pad, GstObject *parent, GstBuffer * buffer)
909 {
910   GstDshowVideoDec *vdec = (GstDshowVideoDec *) gst_pad_get_parent (pad);
911   bool discont = FALSE;
912   GstClockTime stop;
913   GstMapInfo map;
914
915   if (!vdec->setup) {
916     /* we are not setup */
917     GST_WARNING_OBJECT (vdec, "Decoder not set up, failing");
918     vdec->last_ret = GST_FLOW_FLUSHING;
919     goto beach;
920   }
921
922   if (vdec->last_ret != GST_FLOW_OK) {
923     GST_DEBUG_OBJECT (vdec, "last decoding iteration generated a fatal error "
924         "%s", gst_flow_get_name (vdec->last_ret));
925     goto beach;
926   }
927
928   /* check if duration is valid and use duration only when it's valid
929      /* because dshow is not decoding frames having stop smaller than start */
930   if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
931     stop = GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
932   } else {
933     stop = GST_BUFFER_TIMESTAMP (buffer);
934   }
935
936   GST_CAT_LOG_OBJECT (dshowvideodec_debug, vdec,
937       "chain (size %d)=> pts %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT,
938       gst_buffer_get_size (buffer), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
939       GST_TIME_ARGS (stop));
940
941   /* if the incoming buffer has discont flag set => flush decoder data */
942   if (buffer && GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
943     GST_CAT_DEBUG_OBJECT (dshowvideodec_debug, vdec,
944         "this buffer has a DISCONT flag (%" GST_TIME_FORMAT "), flushing",
945         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
946     gst_dshowvideodec_flush (vdec);
947     discont = TRUE;
948   }
949
950   gst_buffer_map(buffer, &map, GST_MAP_READ);
951   /* push the buffer to the directshow decoder */
952   vdec->fakesrc->GetOutputPin()->PushBuffer(
953       map.data, GST_BUFFER_TIMESTAMP (buffer), stop,
954       map.size, discont);
955   gst_buffer_unmap(buffer, &map);
956
957 beach:
958   gst_buffer_unref (buffer);
959   gst_object_unref (vdec);
960
961   return vdec->last_ret;
962 }
963
964 static GstCaps *
965 gst_dshowvideodec_src_getcaps (GstPad * pad)
966 {
967   GstDshowVideoDec *vdec = (GstDshowVideoDec *) gst_pad_get_parent (pad);
968   GstCaps *caps = NULL;
969
970   if (!vdec->srccaps)
971     vdec->srccaps = gst_caps_new_empty ();
972
973   if (vdec->decfilter) {
974     IPinPtr output_pin;
975     IEnumMediaTypesPtr enum_mediatypes;
976     HRESULT hres;
977     ULONG fetched;
978
979     output_pin = gst_dshow_get_pin_from_filter (vdec->decfilter, PINDIR_OUTPUT);
980     if (!output_pin) {
981       GST_ELEMENT_ERROR (vdec, STREAM, FAILED,
982           ("failed getting output pin from the decoder"), (NULL));
983       goto beach;
984     }
985
986     hres = output_pin->EnumMediaTypes (&enum_mediatypes);
987     if (hres == S_OK && enum_mediatypes) {
988       AM_MEDIA_TYPE *mediatype = NULL;
989
990       enum_mediatypes->Reset();
991       while (hres =
992           enum_mediatypes->Next(1, &mediatype, &fetched),
993           hres == S_OK) 
994       {
995         VIDEOINFOHEADER *video_info;
996         GstCaps *mediacaps = NULL;
997
998         /* RGB24 */
999         if (IsEqualGUID (mediatype->subtype, MEDIASUBTYPE_RGB24) &&
1000             IsEqualGUID (mediatype->formattype, FORMAT_VideoInfo))
1001         {
1002           video_info = (VIDEOINFOHEADER *) mediatype->pbFormat;
1003
1004           /* ffmpegcolorspace handles RGB24 in BIG_ENDIAN */
1005           mediacaps = gst_caps_new_simple ("video/x-raw-rgb",
1006               "bpp", G_TYPE_INT, 24,
1007               "depth", G_TYPE_INT, 24,
1008               "width", G_TYPE_INT, video_info->bmiHeader.biWidth,
1009               "height", G_TYPE_INT, video_info->bmiHeader.biHeight,
1010               "framerate", GST_TYPE_FRACTION,
1011               (int) (10000000 / video_info->AvgTimePerFrame), 1, "endianness",
1012               G_TYPE_INT, G_BIG_ENDIAN, "red_mask", G_TYPE_INT, 255,
1013               "green_mask", G_TYPE_INT, 65280, "blue_mask", G_TYPE_INT,
1014               16711680, NULL);
1015
1016           if (mediacaps) {
1017             vdec->mediatypes = g_list_append (vdec->mediatypes, mediatype);
1018             gst_caps_append (vdec->srccaps, mediacaps);
1019           } else {
1020             DeleteMediaType (mediatype);
1021           }
1022         } else {
1023           DeleteMediaType (mediatype);
1024         }
1025
1026       }
1027     }
1028   }
1029
1030   if (vdec->srccaps)
1031     caps = gst_caps_ref (vdec->srccaps);
1032
1033 beach:
1034   gst_object_unref (vdec);
1035
1036   return caps;
1037 }
1038
1039 static gboolean
1040 gst_dshowvideodec_src_setcaps (GstPad * pad, GstCaps * caps)
1041 {
1042   gboolean ret = FALSE;
1043
1044   return ret;
1045 }
1046
1047 static gboolean
1048 gst_dshowvideodec_flush (GstDshowVideoDec * vdec)
1049 {
1050   if (!vdec->fakesrc)
1051     return FALSE;
1052
1053   /* flush dshow decoder and reset timestamp */
1054   vdec->fakesrc->GetOutputPin()->Flush();
1055   vdec->last_ret = GST_FLOW_OK;
1056
1057   return TRUE;
1058 }
1059
1060 static gboolean
1061 gst_dshowvideodec_get_filter_output_format (GstDshowVideoDec * vdec,
1062     const GUID subtype, VIDEOINFOHEADER ** format, guint * size)
1063 {
1064   IPinPtr output_pin;
1065   IEnumMediaTypesPtr enum_mediatypes;
1066   HRESULT hres;
1067   ULONG fetched;
1068   BOOL ret = FALSE;
1069
1070   if (!vdec->decfilter)
1071     return FALSE;
1072
1073   output_pin = gst_dshow_get_pin_from_filter (vdec->decfilter, PINDIR_OUTPUT);
1074   if (!output_pin) {
1075     GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
1076         ("failed getting output pin from the decoder"), (NULL));
1077     return FALSE;
1078   }
1079
1080   hres = output_pin->EnumMediaTypes (&enum_mediatypes);
1081   if (hres == S_OK && enum_mediatypes) {
1082     AM_MEDIA_TYPE *mediatype = NULL;
1083
1084     enum_mediatypes->Reset();
1085     while (hres =
1086         enum_mediatypes->Next(1, &mediatype, &fetched),
1087         hres == S_OK) 
1088     {
1089       if (IsEqualGUID (mediatype->subtype, subtype) &&
1090           IsEqualGUID (mediatype->formattype, FORMAT_VideoInfo))
1091       {
1092         *size = mediatype->cbFormat;
1093         *format = (VIDEOINFOHEADER *)g_malloc0 (*size);
1094         memcpy (*format, mediatype->pbFormat, *size);
1095         ret = TRUE;
1096       }
1097       DeleteMediaType (mediatype);
1098       if (ret)
1099         break;
1100     }
1101   }
1102
1103   return ret;
1104 }
1105
1106 static gboolean
1107 gst_dshowvideodec_create_graph_and_filters (GstDshowVideoDec * vdec)
1108 {
1109   HRESULT hres = S_FALSE;
1110   GstDshowVideoDecClass *klass =
1111       (GstDshowVideoDecClass *) G_OBJECT_GET_CLASS (vdec);
1112   IBaseFilter *srcfilter = NULL;
1113   IBaseFilter *sinkfilter = NULL;
1114   gboolean ret = FALSE;
1115
1116   /* create the filter graph manager object */
1117   hres = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC,
1118       IID_IFilterGraph, (LPVOID *) & vdec->filtergraph);
1119   if (hres != S_OK || !vdec->filtergraph) {
1120     GST_ELEMENT_ERROR (vdec, STREAM, FAILED, ("Can't create an instance "
1121             "of the directshow graph manager (error=%d)", hres), (NULL));
1122     goto error;
1123   }
1124
1125   hres = vdec->filtergraph->QueryInterface(IID_IMediaFilter,
1126       (void **) &vdec->mediafilter);
1127   if (hres != S_OK || !vdec->mediafilter) {
1128     GST_ELEMENT_ERROR (vdec, STREAM, FAILED,
1129         ("Can't get IMediacontrol interface "
1130             "from the graph manager (error=%d)", hres), (NULL));
1131     goto error;
1132   }
1133
1134   /* create fake src filter */
1135   vdec->fakesrc = new FakeSrc();
1136   /* Created with a refcount of zero, so increment that */
1137   vdec->fakesrc->AddRef();
1138
1139   hres = vdec->fakesrc->QueryInterface(IID_IBaseFilter,
1140       (void **) &srcfilter);
1141   if (FAILED (hres)) {
1142     GST_WARNING_OBJECT (vdec, "Failed to QI fakesrc to IBaseFilter");
1143     goto error;
1144   }
1145
1146   /* search a decoder filter and create it */
1147   vdec->decfilter = gst_dshow_find_filter (
1148           klass->entry->input_majortype,
1149           klass->entry->input_subtype,
1150           klass->entry->output_majortype,
1151           klass->entry->output_subtype,
1152           klass->entry->preferred_filters);
1153   if (vdec->decfilter == NULL) {
1154     GST_ELEMENT_ERROR (vdec, STREAM, FAILED, ("Can't create an instance "
1155             "of the decoder filter"), (NULL));
1156     goto error;
1157   }
1158
1159   /* create fake sink filter */
1160   vdec->fakesink = new VideoFakeSink(vdec);
1161   /* Created with a refcount of zero, so increment that */
1162   vdec->fakesink->AddRef();
1163
1164   hres = vdec->fakesink->QueryInterface(IID_IBaseFilter,
1165       (void **) &sinkfilter);
1166   if (FAILED (hres)) {
1167     GST_WARNING_OBJECT (vdec, "Failed to QI fakesink to IBaseFilter");
1168     goto error;
1169   }
1170
1171   /* add filters to the graph */
1172   hres = vdec->filtergraph->AddFilter (srcfilter, L"src");
1173   if (hres != S_OK) {
1174     GST_ELEMENT_ERROR (vdec, STREAM, FAILED, ("Can't add fakesrc filter "
1175             "to the graph (error=%d)", hres), (NULL));
1176     goto error;
1177   }
1178
1179   hres = vdec->filtergraph->AddFilter(vdec->decfilter, L"decoder");
1180   if (hres != S_OK) {
1181     GST_ELEMENT_ERROR (vdec, STREAM, FAILED, ("Can't add decoder filter "
1182             "to the graph (error=%d)", hres), (NULL));
1183     goto error;
1184   }
1185
1186   hres = vdec->filtergraph->AddFilter(sinkfilter, L"sink");
1187   if (hres != S_OK) {
1188     GST_ELEMENT_ERROR (vdec, STREAM, FAILED, ("Can't add fakesink filter "
1189             "to the graph (error=%d)", hres), (NULL));
1190     goto error;
1191   }
1192
1193   vdec->setup = TRUE;
1194
1195   ret = TRUE;
1196
1197 done:
1198   if (srcfilter)
1199     srcfilter->Release();
1200   if (sinkfilter)
1201     sinkfilter->Release();
1202   return ret;
1203
1204 error:
1205   if (vdec->fakesrc) {
1206     vdec->fakesrc->Release();
1207     vdec->fakesrc = NULL;
1208   }
1209   if (vdec->decfilter) {
1210     vdec->decfilter->Release();
1211     vdec->decfilter = NULL;
1212   }
1213   if (vdec->fakesink) {
1214     vdec->fakesink->Release();
1215     vdec->fakesink = NULL;
1216   }
1217   if (vdec->mediafilter) {
1218     vdec->mediafilter->Release();
1219     vdec->mediafilter = NULL;
1220   }
1221   if (vdec->filtergraph) {
1222     vdec->filtergraph->Release();
1223     vdec->filtergraph = NULL;
1224   }
1225
1226   goto done;
1227 }
1228
1229 static gboolean
1230 gst_dshowvideodec_destroy_graph_and_filters (GstDshowVideoDec * vdec)
1231 {
1232   HRESULT hres;
1233
1234   if (vdec->mediafilter) {
1235     vdec->mediafilter->Stop();
1236   }
1237
1238   if (vdec->fakesrc) {
1239     if (vdec->filtergraph) {
1240       IBaseFilter *filter;
1241       hres = vdec->fakesrc->QueryInterface(IID_IBaseFilter,
1242           (void **) &filter);
1243       if (SUCCEEDED (hres)) {
1244         vdec->filtergraph->RemoveFilter(filter);
1245         filter->Release();
1246       }
1247     }
1248
1249     vdec->fakesrc->Release();
1250     vdec->fakesrc = NULL;
1251   }
1252   if (vdec->decfilter) {
1253     if (vdec->filtergraph)
1254       vdec->filtergraph->RemoveFilter(vdec->decfilter);
1255     vdec->decfilter->Release();
1256     vdec->decfilter = NULL;
1257   }
1258   if (vdec->fakesink) {
1259     if (vdec->filtergraph) {
1260       IBaseFilter *filter;
1261       hres = vdec->fakesink->QueryInterface(IID_IBaseFilter,
1262           (void **) &filter);
1263       if (SUCCEEDED (hres)) {
1264         vdec->filtergraph->RemoveFilter(filter);
1265         filter->Release();
1266       }
1267     }
1268
1269     vdec->fakesink->Release();
1270     vdec->fakesink = NULL;
1271   }
1272   if (vdec->mediafilter) {
1273     vdec->mediafilter->Release();
1274     vdec->mediafilter = NULL;
1275   }
1276   if (vdec->filtergraph) {
1277     vdec->filtergraph->Release();
1278     vdec->filtergraph = NULL;
1279   }
1280
1281   vdec->setup = FALSE;
1282
1283   return TRUE;
1284 }
1285
1286 gboolean
1287 dshow_vdec_register (GstPlugin * plugin)
1288 {
1289   GTypeInfo info = {
1290     sizeof (GstDshowVideoDecClass),
1291     (GBaseInitFunc) gst_dshowvideodec_base_init,
1292     NULL,
1293     (GClassInitFunc) gst_dshowvideodec_class_init,
1294     NULL,
1295     NULL,
1296     sizeof (GstDshowVideoDec),
1297     0,
1298     (GInstanceInitFunc) gst_dshowvideodec_init,
1299   };
1300   gint i;
1301   HRESULT hr;
1302
1303   GST_DEBUG_CATEGORY_INIT (dshowvideodec_debug, "dshowvideodec", 0,
1304       "Directshow filter video decoder");
1305
1306   hr = CoInitialize (0);
1307
1308   for (i = 0; i < sizeof (video_dec_codecs) / sizeof (VideoCodecEntry); i++) {
1309     GType type;
1310     IBaseFilterPtr filter;
1311     guint rank = GST_RANK_MARGINAL;
1312
1313     filter = gst_dshow_find_filter (
1314             video_dec_codecs[i].input_majortype,
1315             video_dec_codecs[i].input_subtype,
1316             video_dec_codecs[i].output_majortype,
1317             video_dec_codecs[i].output_subtype,
1318             video_dec_codecs[i].preferred_filters);
1319     if (filter != NULL) {
1320
1321       if (video_dec_codecs[i].format == GST_MAKE_FOURCC ('W', 'V', 'C', '1')) {
1322         /* FFMPEG WVC1 decoder sucks, get higher priority for ours */
1323         rank = GST_RANK_MARGINAL + 2;
1324       }
1325       GST_DEBUG ("Registering %s with rank %u", video_dec_codecs[i].element_name, rank);
1326
1327       type = g_type_register_static (GST_TYPE_ELEMENT,
1328           video_dec_codecs[i].element_name, &info, (GTypeFlags)0);
1329       g_type_set_qdata (type, DSHOW_CODEC_QDATA, (gpointer) (video_dec_codecs + i));
1330       if (!gst_element_register (plugin, video_dec_codecs[i].element_name, rank, type)) {
1331         return FALSE;
1332       }
1333       GST_DEBUG ("Registered %s", video_dec_codecs[i].element_name);
1334     } else {
1335       GST_DEBUG ("Element %s not registered "
1336         "(the format is not supported by the system)",
1337         video_dec_codecs[i].element_name);
1338     }
1339   }
1340
1341   if (SUCCEEDED(hr))
1342     CoUninitialize ();
1343
1344   return TRUE;
1345 }