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>
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
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
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.
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.
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.
51 #include <wmcodecdsp.h>
53 #include "gstdshowvideodec.h"
54 #include <gst/video/video.h>
56 GST_DEBUG_CATEGORY_STATIC (dshowvideodec_debug);
57 #define GST_CAT_DEFAULT dshowvideodec_debug
59 #define gst_dshowvideodec_parent_class parent_class
60 G_DEFINE_TYPE(GstDshowVideoDec, gst_dshowvideodec, GST_TYPE_ELEMENT)
62 static void gst_dshowvideodec_finalize (GObject * object);
63 static GstStateChangeReturn gst_dshowvideodec_change_state
64 (GstElement * element, GstStateChange transition);
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);
71 /* src pad overwrites */
72 static GstCaps *gst_dshowvideodec_src_getcaps (GstPad * pad);
73 static gboolean gst_dshowvideodec_src_setcaps (GstPad * pad, GstCaps * caps);
76 static gboolean gst_dshowvideodec_create_graph_and_filters (GstDshowVideoDec *
78 static gboolean gst_dshowvideodec_destroy_graph_and_filters (GstDshowVideoDec *
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);
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 }}
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}}
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 }}
112 /* WMV always uses the WMV DMO */
113 static PreferredFilter preferred_wmv_filters[] = {
114 {&CLSID_CWMVDecMediaObject, &DMOCATEGORY_VIDEO_DECODER}, {0}
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}
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}};
132 static PreferredFilter preferred_mp43_filters[] = {
133 {&CLSID_CMpeg43DecMediaObject, &DMOCATEGORY_VIDEO_DECODER}, {0}};
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}
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},
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},
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},
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},
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},
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},
192 {"dshowvdec_cinepak", "Cinepack",
194 GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_CVID,
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},
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},
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},
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},
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},
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},
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},
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,
258 GUID_MEDIATYPE_VIDEO, GUID_MEDIASUBTYPE_YUY2,
259 GST_VIDEO_CAPS_MAKE("YUY2")},
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")},
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")},
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")}
283 HRESULT VideoFakeSink::DoRenderSample(IMediaSample *pMediaSample)
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;
295 BYTE *pBuffer = NULL;
296 LONGLONG lStart = 0, lStop = 0;
297 long size = pMediaSample->GetActualDataLength();
299 pMediaSample->GetPointer(&pBuffer);
300 pMediaSample->GetTime(&lStart, &lStop);
302 start = lStart * 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);
308 /* if the buffer is out of segment do not push it downstream */
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));
316 /* buffer is in our segment, allocate a new out buffer and clip its
318 gst_buffer_pool_acquire_buffer(mDec->buffer_pool, &buf, NULL);
320 GST_WARNING_OBJECT (mDec,
321 "cannot allocate a new GstBuffer");
325 /* set buffer properties */
326 GST_BUFFER_TIMESTAMP (buf) = clip_start;
327 GST_BUFFER_DURATION (buf) = clip_stop - clip_start;
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
336 guint stride = mDec->width * 4;
338 for (; line < mDec->height; line++) {
339 memcpy (map.data + (line * stride),
340 pBuffer + (size - ((line + 1) * (stride))), stride);
343 memcpy (map.data, pBuffer, MIN ((unsigned int)size, map.size));
345 gst_buffer_unmap(buf, &map);
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)));
354 /* push the buffer downstream */
355 mDec->last_ret = gst_pad_push (mDec->srcpad, buf);
362 HRESULT VideoFakeSink::CheckMediaType(const CMediaType *pmt)
365 if (*pmt == m_MediaType)
373 gst_dshowvideodec_base_init (gpointer klass)
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;
381 gchar *longname, *description;
383 qdata = g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass), DSHOW_CODEC_QDATA);
385 /* element details */
386 tmp = videodec_class->entry = (VideoCodecEntry *) qdata;
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);
393 gst_element_class_set_metadata(element_class, longname, "Codec/Decoder/Video", description,
394 "Sebastien Moutte <sebastien@moutte.net>");
397 g_free (description);
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);
405 srccaps = gst_caps_from_string (tmp->srccaps);
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);
410 gst_element_class_add_pad_template (element_class, src);
411 gst_element_class_add_pad_template (element_class, sink);
414 gst_caps_unref(sinkcaps);
417 gst_caps_unref(srccaps);
421 gst_dshowvideodec_class_init (GstDshowVideoDecClass * klass)
423 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
424 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
426 gobject_class->finalize = gst_dshowvideodec_finalize;
428 gstelement_class->change_state =
429 GST_DEBUG_FUNCPTR (gst_dshowvideodec_change_state);
431 parent_class = (GstElementClass *) g_type_class_peek_parent (klass);
435 gst_dshowvideodec_com_thread (GstDshowVideoDec * vdec)
439 g_mutex_lock (&vdec->com_init_lock);
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 */
445 res = CoInitializeEx (0, COINIT_MULTITHREADED);
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.");
451 GST_INFO_OBJECT (vdec, "COM initialized successfully");
453 vdec->comInitialized = TRUE;
455 /* Signal other threads waiting on this condition that COM was initialized */
456 g_cond_signal (&vdec->com_initialized);
458 g_mutex_unlock (&vdec->com_init_lock);
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);
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);
472 gst_dshowvideodec_init (GstDshowVideoDec * vdec)
474 GstElementClass *element_class = GST_ELEMENT_GET_CLASS (vdec);
478 gst_pad_new_from_template (gst_element_class_get_pad_template
479 (element_class, "sink"), "sink");
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);
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);
493 vdec->fakesrc = NULL;
494 vdec->fakesink = NULL;
495 vdec->decfilter = NULL;
497 vdec->last_ret = GST_FLOW_OK;
499 vdec->filtergraph = NULL;
500 vdec->mediafilter = NULL;
501 vdec->srccaps = NULL;
502 vdec->segment = gst_segment_new ();
505 vdec->buffer_pool = NULL;
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);
513 g_mutex_lock (&vdec->com_init_lock);
515 /* create the COM initialization thread */
516 g_thread_new ("COM Init Thread", (GThreadFunc)gst_dshowvideodec_com_thread,
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);
525 gst_dshowvideodec_finalize (GObject * object)
527 GstDshowVideoDec *vdec = (GstDshowVideoDec *) (object);
530 gst_segment_free (vdec->segment);
531 vdec->segment = NULL;
534 if(vdec->buffer_pool) {
535 gst_object_unref(vdec->buffer_pool);
536 vdec->buffer_pool = NULL;
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);
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);
553 G_OBJECT_CLASS (parent_class)->finalize (object);
556 static GstStateChangeReturn
557 gst_dshowvideodec_change_state (GstElement * element, GstStateChange transition)
559 GstDshowVideoDec *vdec = (GstDshowVideoDec *) (element);
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;
566 case GST_STATE_CHANGE_READY_TO_PAUSED:
568 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
570 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
572 case GST_STATE_CHANGE_PAUSED_TO_READY:
574 case GST_STATE_CHANGE_READY_TO_NULL:
575 if (!gst_dshowvideodec_destroy_graph_and_filters (vdec))
576 return GST_STATE_CHANGE_FAILURE;
582 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
586 gst_dshowvideodec_sink_setcaps (GstPad * pad, GstCaps * caps)
588 gboolean ret = FALSE;
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;
596 const GValue *v = NULL;
598 GstCaps *caps_out = NULL;
599 AM_MEDIA_TYPE output_mediatype, input_mediatype;
600 VIDEOINFOHEADER *input_vheader = NULL, *output_vheader = NULL;
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;
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));
618 fps = gst_structure_get_value (s, "framerate");
620 vdec->fps_n = gst_value_get_fraction_numerator (fps);
621 vdec->fps_d = gst_value_get_fraction_denominator (fps);
624 /* Invent a sane default framerate; the timestamps matter
630 par = gst_structure_get_value (s, "pixel-aspect-ratio");
632 vdec->par_n = gst_value_get_fraction_numerator (par);
633 vdec->par_d = gst_value_get_fraction_denominator (par);
636 vdec->par_n = vdec->par_d = 1;
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);
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;
651 if (strstr (klass->entry->sinkcaps, "video/mpeg, mpegversion= (int) 1")) {
653 sizeof (MPEG1VIDEOINFO) + (extradata ? extra_size - 1 : 0);
654 input_vheader = (VIDEOINFOHEADER *)g_malloc0 (size);
656 input_vheader->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
658 MPEG1VIDEOINFO *mpeg_info = (MPEG1VIDEOINFO *) input_vheader;
660 gst_buffer_extract(extradata, 0, mpeg_info->bSequenceHeader, extra_size);
661 mpeg_info->cbSequenceHeader = extra_size;
663 input_mediatype.formattype = FORMAT_MPEGVideo;
666 sizeof (VIDEOINFOHEADER) + (extradata ? extra_size : 0);
667 input_vheader = (VIDEOINFOHEADER *)g_malloc0 (size);
668 input_vheader->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
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;
675 input_mediatype.formattype = FORMAT_VideoInfo;
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);
690 input_mediatype.cbFormat = size;
691 input_mediatype.pbFormat = (BYTE *) input_vheader;
692 input_mediatype.lSampleSize = input_vheader->bmiHeader.biSizeImage;
694 vdec->fakesrc->GetOutputPin()->SetMediaType(&input_mediatype);
696 /* set the sample size for fakesrc filter to the output buffer size */
697 vdec->fakesrc->GetOutputPin()->SetSampleSize(input_mediatype.lSampleSize);
699 /* connect our fake src to decoder */
700 hres = vdec->fakesrc->QueryInterface(IID_IBaseFilter,
701 (void **) &srcfilter);
703 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
704 ("Can't QT fakesrc to IBaseFilter: %x", hres), (NULL));
708 output_pin = gst_dshow_get_pin_from_filter (srcfilter, PINDIR_OUTPUT);
710 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
711 ("Can't get output pin from our directshow fakesrc filter"), (NULL));
714 input_pin = gst_dshow_get_pin_from_filter (vdec->decfilter, PINDIR_INPUT);
716 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
717 ("Can't get input pin from decoder filter"), (NULL));
721 hres = vdec->filtergraph->ConnectDirect (output_pin, input_pin, NULL);
723 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
724 ("Can't connect fakesrc with decoder (error=%x)", hres), (NULL));
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));
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;
746 vdec->fakesink->SetMediaType (&output_mediatype);
748 /* connect decoder to our fake sink */
749 output_pin = gst_dshow_get_pin_from_filter (vdec->decfilter, PINDIR_OUTPUT);
751 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
752 ("Can't get output pin from our decoder filter"), (NULL));
756 hres = vdec->fakesink->QueryInterface(IID_IBaseFilter,
757 (void **) &sinkfilter);
759 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
760 ("Can't QT fakesink to IBaseFilter: %x", hres), (NULL));
764 input_pin = gst_dshow_get_pin_from_filter (sinkfilter, PINDIR_INPUT);
766 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
767 ("Can't get input pin from our directshow fakesink filter"), (NULL));
771 hres = vdec->filtergraph->ConnectDirect(output_pin, input_pin,
774 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
775 ("Can't connect decoder with fakesink (error=%x)", hres), (NULL));
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);
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);
790 gst_caps_set_simple (caps_out,
791 "pixel-aspect-ratio", GST_TYPE_FRACTION, vdec->par_n, vdec->par_d, NULL);
793 if (!gst_pad_set_caps (vdec->srcpad, caps_out)) {
794 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
795 ("Failed to negotiate output"), (NULL));
799 /* request or create a buffer pool */
800 if (vdec->buffer_pool) {
801 gst_object_unref (vdec->buffer_pool);
804 query = gst_query_new_allocation(caps_out, TRUE);
805 gst_pad_peer_query(vdec->srcpad, query);
807 if (gst_query_get_n_allocation_pools (query) > 0) {
808 gst_query_parse_nth_allocation_pool (query, 0, &pool, &pool_size, &pool_min,
813 pool_size = output_mediatype.lSampleSize;
819 pool = gst_video_buffer_pool_new ();
823 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
824 ("Could not create buffer bool"), (NULL));
828 pool_config = gst_buffer_pool_get_config (pool);
829 gst_buffer_pool_config_set_params (pool_config, caps_out, pool_size,
831 gst_buffer_pool_set_config (pool, pool_config);
833 if (!gst_buffer_pool_set_active (pool, TRUE)) {
834 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
835 ("Failed set buffer pool active"), (NULL));
839 vdec->buffer_pool = pool;
841 hres = vdec->mediafilter->Run (-1);
843 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
844 ("Can't run the directshow graph (error=%d)", hres), (NULL));
851 gst_caps_unref (caps_out);
852 gst_object_unref (vdec);
853 g_free (input_vheader);
855 srcfilter->Release();
857 sinkfilter->Release();
859 gst_query_unref(query);
864 gst_dshowvideodec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
867 GstDshowVideoDec *vdec = (GstDshowVideoDec *) gst_pad_get_parent (pad);
869 switch (GST_EVENT_TYPE (event)) {
872 gst_event_parse_caps(event, &caps);
873 ret = gst_dshowvideodec_sink_setcaps(pad, caps);
876 case GST_EVENT_FLUSH_STOP:
877 gst_dshowvideodec_flush (vdec);
878 ret = gst_pad_event_default (pad, parent, event);
880 case GST_EVENT_SEGMENT:
882 const GstSegment *segment;
884 gst_event_parse_segment (event, &segment);
886 /* save the new segment in our local current segment */
887 gst_segment_copy_into(segment, vdec->segment);
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));
894 ret = gst_pad_event_default (pad, parent, event);
898 ret = gst_pad_event_default (pad, parent, event);
902 gst_object_unref (vdec);
908 gst_dshowvideodec_chain (GstPad * pad, GstObject *parent, GstBuffer * buffer)
910 GstDshowVideoDec *vdec = (GstDshowVideoDec *) gst_pad_get_parent (pad);
911 bool discont = FALSE;
916 /* we are not setup */
917 GST_WARNING_OBJECT (vdec, "Decoder not set up, failing");
918 vdec->last_ret = GST_FLOW_FLUSHING;
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));
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);
933 stop = GST_BUFFER_TIMESTAMP (buffer);
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));
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);
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,
955 gst_buffer_unmap(buffer, &map);
958 gst_buffer_unref (buffer);
959 gst_object_unref (vdec);
961 return vdec->last_ret;
965 gst_dshowvideodec_src_getcaps (GstPad * pad)
967 GstDshowVideoDec *vdec = (GstDshowVideoDec *) gst_pad_get_parent (pad);
968 GstCaps *caps = NULL;
971 vdec->srccaps = gst_caps_new_empty ();
973 if (vdec->decfilter) {
975 IEnumMediaTypesPtr enum_mediatypes;
979 output_pin = gst_dshow_get_pin_from_filter (vdec->decfilter, PINDIR_OUTPUT);
981 GST_ELEMENT_ERROR (vdec, STREAM, FAILED,
982 ("failed getting output pin from the decoder"), (NULL));
986 hres = output_pin->EnumMediaTypes (&enum_mediatypes);
987 if (hres == S_OK && enum_mediatypes) {
988 AM_MEDIA_TYPE *mediatype = NULL;
990 enum_mediatypes->Reset();
992 enum_mediatypes->Next(1, &mediatype, &fetched),
995 VIDEOINFOHEADER *video_info;
996 GstCaps *mediacaps = NULL;
999 if (IsEqualGUID (mediatype->subtype, MEDIASUBTYPE_RGB24) &&
1000 IsEqualGUID (mediatype->formattype, FORMAT_VideoInfo))
1002 video_info = (VIDEOINFOHEADER *) mediatype->pbFormat;
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,
1017 vdec->mediatypes = g_list_append (vdec->mediatypes, mediatype);
1018 gst_caps_append (vdec->srccaps, mediacaps);
1020 DeleteMediaType (mediatype);
1023 DeleteMediaType (mediatype);
1031 caps = gst_caps_ref (vdec->srccaps);
1034 gst_object_unref (vdec);
1040 gst_dshowvideodec_src_setcaps (GstPad * pad, GstCaps * caps)
1042 gboolean ret = FALSE;
1048 gst_dshowvideodec_flush (GstDshowVideoDec * vdec)
1053 /* flush dshow decoder and reset timestamp */
1054 vdec->fakesrc->GetOutputPin()->Flush();
1055 vdec->last_ret = GST_FLOW_OK;
1061 gst_dshowvideodec_get_filter_output_format (GstDshowVideoDec * vdec,
1062 const GUID subtype, VIDEOINFOHEADER ** format, guint * size)
1065 IEnumMediaTypesPtr enum_mediatypes;
1070 if (!vdec->decfilter)
1073 output_pin = gst_dshow_get_pin_from_filter (vdec->decfilter, PINDIR_OUTPUT);
1075 GST_ELEMENT_ERROR (vdec, CORE, NEGOTIATION,
1076 ("failed getting output pin from the decoder"), (NULL));
1080 hres = output_pin->EnumMediaTypes (&enum_mediatypes);
1081 if (hres == S_OK && enum_mediatypes) {
1082 AM_MEDIA_TYPE *mediatype = NULL;
1084 enum_mediatypes->Reset();
1086 enum_mediatypes->Next(1, &mediatype, &fetched),
1089 if (IsEqualGUID (mediatype->subtype, subtype) &&
1090 IsEqualGUID (mediatype->formattype, FORMAT_VideoInfo))
1092 *size = mediatype->cbFormat;
1093 *format = (VIDEOINFOHEADER *)g_malloc0 (*size);
1094 memcpy (*format, mediatype->pbFormat, *size);
1097 DeleteMediaType (mediatype);
1107 gst_dshowvideodec_create_graph_and_filters (GstDshowVideoDec * vdec)
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;
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));
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));
1134 /* create fake src filter */
1135 vdec->fakesrc = new FakeSrc();
1136 /* Created with a refcount of zero, so increment that */
1137 vdec->fakesrc->AddRef();
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");
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));
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();
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");
1171 /* add filters to the graph */
1172 hres = vdec->filtergraph->AddFilter (srcfilter, L"src");
1174 GST_ELEMENT_ERROR (vdec, STREAM, FAILED, ("Can't add fakesrc filter "
1175 "to the graph (error=%d)", hres), (NULL));
1179 hres = vdec->filtergraph->AddFilter(vdec->decfilter, L"decoder");
1181 GST_ELEMENT_ERROR (vdec, STREAM, FAILED, ("Can't add decoder filter "
1182 "to the graph (error=%d)", hres), (NULL));
1186 hres = vdec->filtergraph->AddFilter(sinkfilter, L"sink");
1188 GST_ELEMENT_ERROR (vdec, STREAM, FAILED, ("Can't add fakesink filter "
1189 "to the graph (error=%d)", hres), (NULL));
1199 srcfilter->Release();
1201 sinkfilter->Release();
1205 if (vdec->fakesrc) {
1206 vdec->fakesrc->Release();
1207 vdec->fakesrc = NULL;
1209 if (vdec->decfilter) {
1210 vdec->decfilter->Release();
1211 vdec->decfilter = NULL;
1213 if (vdec->fakesink) {
1214 vdec->fakesink->Release();
1215 vdec->fakesink = NULL;
1217 if (vdec->mediafilter) {
1218 vdec->mediafilter->Release();
1219 vdec->mediafilter = NULL;
1221 if (vdec->filtergraph) {
1222 vdec->filtergraph->Release();
1223 vdec->filtergraph = NULL;
1230 gst_dshowvideodec_destroy_graph_and_filters (GstDshowVideoDec * vdec)
1234 if (vdec->mediafilter) {
1235 vdec->mediafilter->Stop();
1238 if (vdec->fakesrc) {
1239 if (vdec->filtergraph) {
1240 IBaseFilter *filter;
1241 hres = vdec->fakesrc->QueryInterface(IID_IBaseFilter,
1243 if (SUCCEEDED (hres)) {
1244 vdec->filtergraph->RemoveFilter(filter);
1249 vdec->fakesrc->Release();
1250 vdec->fakesrc = NULL;
1252 if (vdec->decfilter) {
1253 if (vdec->filtergraph)
1254 vdec->filtergraph->RemoveFilter(vdec->decfilter);
1255 vdec->decfilter->Release();
1256 vdec->decfilter = NULL;
1258 if (vdec->fakesink) {
1259 if (vdec->filtergraph) {
1260 IBaseFilter *filter;
1261 hres = vdec->fakesink->QueryInterface(IID_IBaseFilter,
1263 if (SUCCEEDED (hres)) {
1264 vdec->filtergraph->RemoveFilter(filter);
1269 vdec->fakesink->Release();
1270 vdec->fakesink = NULL;
1272 if (vdec->mediafilter) {
1273 vdec->mediafilter->Release();
1274 vdec->mediafilter = NULL;
1276 if (vdec->filtergraph) {
1277 vdec->filtergraph->Release();
1278 vdec->filtergraph = NULL;
1281 vdec->setup = FALSE;
1287 dshow_vdec_register (GstPlugin * plugin)
1290 sizeof (GstDshowVideoDecClass),
1291 (GBaseInitFunc) gst_dshowvideodec_base_init,
1293 (GClassInitFunc) gst_dshowvideodec_class_init,
1296 sizeof (GstDshowVideoDec),
1298 (GInstanceInitFunc) gst_dshowvideodec_init,
1303 GST_DEBUG_CATEGORY_INIT (dshowvideodec_debug, "dshowvideodec", 0,
1304 "Directshow filter video decoder");
1306 hr = CoInitialize (0);
1308 for (i = 0; i < sizeof (video_dec_codecs) / sizeof (VideoCodecEntry); i++) {
1310 IBaseFilterPtr filter;
1311 guint rank = GST_RANK_MARGINAL;
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) {
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;
1325 GST_DEBUG ("Registering %s with rank %u", video_dec_codecs[i].element_name, rank);
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)) {
1333 GST_DEBUG ("Registered %s", video_dec_codecs[i].element_name);
1335 GST_DEBUG ("Element %s not registered "
1336 "(the format is not supported by the system)",
1337 video_dec_codecs[i].element_name);