winks: improve framerate fraction conversions
[platform/upstream/gstreamer.git] / sys / winks / ksvideohelpers.c
1 /*
2  * Copyright (C) 2007 Haakon Sporsheim <hakon.sporsheim@tandberg.com>
3  *               2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
4  *               2009 Knut Inge Hvidsten <knut.inge.hvidsten@tandberg.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "ksvideohelpers.h"
23
24 #include <math.h>
25 #include <uuids.h>
26 #include "kshelpers.h"
27
28 GST_DEBUG_CATEGORY_EXTERN (gst_ks_debug);
29 #define GST_CAT_DEFAULT gst_ks_debug
30
31 static const GUID MEDIASUBTYPE_FOURCC =
32     { 0x0 /* FourCC here */ , 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xAA, 0x00,
33     0x38, 0x9B, 0x71}
34 };
35
36 extern const GUID MEDIASUBTYPE_I420 =
37     { 0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B,
38     0x71}
39 };
40
41 typedef struct _KsVideoDeviceEntry KsVideoDeviceEntry;
42
43 struct _KsVideoDeviceEntry
44 {
45   KsDeviceEntry *device;
46   gint priority;
47 };
48
49 static void
50 ks_video_device_entry_decide_priority (KsVideoDeviceEntry * videodevice)
51 {
52   HANDLE filter_handle;
53
54   videodevice->priority = 0;
55
56   filter_handle = CreateFile (videodevice->device->path,
57       GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
58       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
59   if (ks_is_valid_handle (filter_handle)) {
60     GUID *propsets = NULL;
61     gulong propsets_len;
62
63     if (ks_object_get_supported_property_sets (filter_handle, &propsets,
64             &propsets_len)) {
65       gulong i;
66
67       for (i = 0; i < propsets_len; i++) {
68         if (memcmp (&propsets[i], &PROPSETID_VIDCAP_CAMERACONTROL,
69                 sizeof (GUID)) == 0) {
70           videodevice->priority++;
71           break;
72         }
73       }
74
75       g_free (propsets);
76     }
77   }
78
79   CloseHandle (filter_handle);
80 }
81
82 static gint
83 ks_video_device_entry_compare (gconstpointer a, gconstpointer b)
84 {
85   const KsVideoDeviceEntry *videodevice_a = a;
86   const KsVideoDeviceEntry *videodevice_b = b;
87
88   if (videodevice_a->priority > videodevice_b->priority)
89     return -1;
90   else if (videodevice_a->priority == videodevice_b->priority)
91     return 0;
92   else
93     return 1;
94 }
95
96 GList *
97 ks_video_device_list_sort_cameras_first (GList * devices)
98 {
99   GList *videodevices = NULL, *walk;
100   guint i;
101
102   for (walk = devices; walk != NULL; walk = walk->next) {
103     KsDeviceEntry *device = walk->data;
104     KsVideoDeviceEntry *videodevice;
105
106     videodevice = g_new (KsVideoDeviceEntry, 1);
107     videodevice->device = device;
108     ks_video_device_entry_decide_priority (videodevice);
109
110     videodevices = g_list_append (videodevices, videodevice);
111   }
112
113   videodevices = g_list_sort (videodevices, ks_video_device_entry_compare);
114
115   g_list_free (devices);
116   devices = NULL;
117
118   for (walk = videodevices, i = 0; walk != NULL; walk = walk->next, i++) {
119     KsVideoDeviceEntry *videodevice = walk->data;
120
121     videodevice->device->index = i;
122     devices = g_list_append (devices, videodevice->device);
123
124     g_free (videodevice);
125   }
126
127   g_list_free (videodevices);
128
129   return devices;
130 }
131
132 static GstStructure *
133 ks_video_format_to_structure (GUID subtype_guid, GUID format_guid)
134 {
135   GstStructure *structure = NULL;
136
137   if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_MJPG) || IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_TVMJ) ||     /* FIXME: NOT tested */
138       IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_WAKE) ||        /* FIXME: NOT tested */
139       IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_CFCC) ||        /* FIXME: NOT tested */
140       IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_IJPG)) {        /* FIXME: NOT tested */
141     structure = gst_structure_new ("image/jpeg", NULL);
142   } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB555) ||       /* FIXME: NOT tested */
143       IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB565) ||      /* FIXME: NOT tested */
144       IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB24) || IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB32) ||   /* FIXME: NOT tested */
145       IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_ARGB1555) ||    /* FIXME: NOT tested */
146       IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_ARGB32) ||      /* FIXME: NOT tested */
147       IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_ARGB4444)) {    /* FIXME: NOT tested */
148     guint depth = 0, bpp = 0;
149     gint endianness = 0;
150     guint32 r_mask = 0, b_mask = 0, g_mask = 0;
151
152     if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB555)) {
153       bpp = 16;
154       depth = 15;
155       endianness = G_BIG_ENDIAN;
156       r_mask = 0x7c00;
157       g_mask = 0x03e0;
158       b_mask = 0x001f;
159     } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB565)) {
160       bpp = depth = 16;
161       endianness = G_BIG_ENDIAN;
162       r_mask = 0xf800;
163       g_mask = 0x07e0;
164       b_mask = 0x001f;
165     } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB24)) {
166       bpp = depth = 24;
167       endianness = G_BIG_ENDIAN;
168       r_mask = 0x0000ff;
169       g_mask = 0x00ff00;
170       b_mask = 0xff0000;
171     } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB32)) {
172       bpp = 32;
173       depth = 24;
174       endianness = G_BIG_ENDIAN;
175       r_mask = 0x000000ff;
176       g_mask = 0x0000ff00;
177       b_mask = 0x00ff0000;
178       /* FIXME: check
179        *r_mask = 0xff000000;
180        *g_mask = 0x00ff0000;
181        *b_mask = 0x0000ff00;
182        */
183     } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_ARGB1555)) {
184       bpp = 16;
185       depth = 15;
186       endianness = G_BIG_ENDIAN;
187       r_mask = 0x7c00;
188       g_mask = 0x03e0;
189       b_mask = 0x001f;
190     } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_ARGB32)) {
191       bpp = depth = 32;
192       endianness = G_BIG_ENDIAN;
193       r_mask = 0x000000ff;
194       g_mask = 0x0000ff00;
195       b_mask = 0x00ff0000;
196       /* FIXME: check
197        *r_mask = 0xff000000;
198        *g_mask = 0x00ff0000;
199        *b_mask = 0x0000ff00;
200        */
201     } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_ARGB4444)) {
202       bpp = 16;
203       depth = 12;
204       endianness = G_BIG_ENDIAN;
205       r_mask = 0x0f00;
206       g_mask = 0x00f0;
207       b_mask = 0x000f;
208       //r_mask = 0x000f;
209       //g_mask = 0x00f0;
210       //b_mask = 0x0f00;
211     } else {
212       g_assert_not_reached ();
213     }
214
215     structure = gst_structure_new ("video/x-raw-rgb",
216         "bpp", G_TYPE_INT, bpp,
217         "depth", G_TYPE_INT, depth,
218         "red_mask", G_TYPE_INT, r_mask,
219         "green_mask", G_TYPE_INT, g_mask,
220         "blue_mask", G_TYPE_INT, b_mask,
221         "endianness", G_TYPE_INT, endianness, NULL);
222   } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_dvsd)) {
223     if (IsEqualGUID (&format_guid, &FORMAT_DvInfo)) {
224       structure = gst_structure_new ("video/x-dv",
225           "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
226     } else if (IsEqualGUID (&format_guid, &FORMAT_VideoInfo)) {
227       structure = gst_structure_new ("video/x-dv",
228           "systemstream", G_TYPE_BOOLEAN, FALSE,
229           "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('d', 'v', 's', 'd'),
230           NULL);
231     }
232   } else if (memcmp (&subtype_guid.Data2, &MEDIASUBTYPE_FOURCC.Data2,
233           sizeof (subtype_guid) - sizeof (subtype_guid.Data1)) == 0) {
234     guint8 *p = (guint8 *) & subtype_guid.Data1;
235
236     structure = gst_structure_new ("video/x-raw-yuv",
237         "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC (p[0], p[1], p[2], p[3]),
238         NULL);
239   }
240
241   if (!structure) {
242     GST_DEBUG ("Unknown DirectShow Video GUID %08x-%04x-%04x-%04x-%08x%04x",
243         subtype_guid.Data1, subtype_guid.Data2, subtype_guid.Data3,
244         *(WORD *) subtype_guid.Data4, *(DWORD *) & subtype_guid.Data4[2],
245         *(WORD *) & subtype_guid.Data4[6]);
246   }
247
248   return structure;
249 }
250
251 static void
252 guess_aspect (gint width, gint height, gint * par_width, gint * par_height)
253 {
254   /*
255    * As we dont have access to the actual pixel aspect, we will try to do a
256    * best-effort guess. The guess is based on most sensors being either 4/3
257    * or 16/9, and most pixel aspects being close to 1/1.
258    */
259   if ((width == 768) && (height == 448)) {      /* special case for w448p */
260     *par_width = 28;
261     *par_height = 27;
262   } else {
263     if (((float) width / (float) height) < 1.2778) {
264       *par_width = 12;
265       *par_height = 11;
266     } else {
267       *par_width = 1;
268       *par_height = 1;
269     }
270   }
271 }
272
273 /* NOTE: would probably be better to use a continued fractions approach here */
274 static void
275 compress_fraction (gint64 in_num, gint64 in_den, gint64 * out_num,
276     gint64 * out_den)
277 {
278   gdouble on, od, orig;
279   guint denominators[] = { 1, 2, 3, 5, 7 }, i;
280   const gdouble max_loss = 0.1;
281
282   on = in_num;
283   od = in_den;
284   orig = on / od;
285
286   for (i = 0; i < G_N_ELEMENTS (denominators); i++) {
287     gint64 cur_n, cur_d;
288     gdouble cur, loss;
289
290     cur_n = floor ((on / (od / (gdouble) denominators[i])) + 0.5);
291     cur_d = denominators[i];
292     cur = (gdouble) cur_n / (gdouble) cur_d;
293     loss = fabs (cur - orig);
294
295     if (loss <= max_loss) {
296       *out_num = cur_n;
297       *out_den = cur_d;
298
299       return;
300     }
301   }
302
303   *out_num = in_num;
304   *out_den = in_den;
305 }
306
307 static gboolean
308 ks_video_append_video_stream_cfg_fields (GstStructure * structure,
309     const KS_VIDEO_STREAM_CONFIG_CAPS * vscc)
310 {
311   GValue val = { 0, };
312   gint64 max_n, max_d;
313
314   g_return_val_if_fail (structure, FALSE);
315   g_return_val_if_fail (vscc, FALSE);
316
317   /* width */
318   if (vscc->MinOutputSize.cx == vscc->MaxOutputSize.cx) {
319     gst_structure_set (structure,
320         "width", G_TYPE_INT, vscc->MaxOutputSize.cx, NULL);
321   } else {
322     gst_structure_set (structure,
323         "width", GST_TYPE_INT_RANGE,
324         vscc->MinOutputSize.cx, vscc->MaxOutputSize.cx, NULL);
325   }
326
327   /* height */
328   if (vscc->MinOutputSize.cy == vscc->MaxOutputSize.cy) {
329     gst_structure_set (structure,
330         "height", G_TYPE_INT, vscc->MaxOutputSize.cy, NULL);
331   } else {
332     gst_structure_set (structure,
333         "height", GST_TYPE_INT_RANGE,
334         vscc->MinOutputSize.cy, vscc->MaxOutputSize.cy, NULL);
335   }
336
337   /* framerate */
338   compress_fraction (NANOSECONDS, vscc->MaxFrameInterval, &max_n, &max_d);
339
340   if (vscc->MinFrameInterval == vscc->MaxFrameInterval) {
341     g_value_init (&val, GST_TYPE_FRACTION);
342     gst_value_set_fraction (&val, max_n, max_d);
343   } else {
344     gint64 min_n, min_d;
345
346     compress_fraction (NANOSECONDS, vscc->MinFrameInterval, &min_n, &min_d);
347
348     g_value_init (&val, GST_TYPE_FRACTION_RANGE);
349     gst_value_set_fraction_range_full (&val, max_n, max_d, min_n, min_d);
350   }
351
352   gst_structure_set_value (structure, "framerate", &val);
353   g_value_unset (&val);
354
355   {
356     gint par_width, par_height;
357
358     guess_aspect (vscc->MaxOutputSize.cx, vscc->MaxOutputSize.cy,
359         &par_width, &par_height);
360
361     gst_structure_set (structure,
362         "pixel-aspect-ratio", GST_TYPE_FRACTION, par_width, par_height, NULL);
363   }
364
365   return TRUE;
366 }
367
368 KsVideoMediaType *
369 ks_video_media_type_dup (KsVideoMediaType * media_type)
370 {
371   KsVideoMediaType *result = g_new (KsVideoMediaType, 1);
372
373   memcpy (result, media_type, sizeof (KsVideoMediaType));
374
375   result->range = g_malloc (media_type->range->FormatSize);
376   memcpy ((gpointer) result->range, media_type->range,
377       media_type->range->FormatSize);
378
379   result->format = g_malloc (media_type->format_size);
380   memcpy (result->format, media_type->format, media_type->format_size);
381
382   result->translated_caps = gst_caps_ref (media_type->translated_caps);
383
384   return result;
385 }
386
387 void
388 ks_video_media_type_free (KsVideoMediaType * media_type)
389 {
390   if (media_type == NULL)
391     return;
392
393   g_free ((gpointer) media_type->range);
394
395   g_free (media_type->format);
396
397   if (media_type->translated_caps != NULL)
398     gst_caps_unref (media_type->translated_caps);
399
400   g_free (media_type);
401 }
402
403 static GList *
404 ks_video_media_type_list_remove_duplicates (GList * media_types)
405 {
406   GList *master, *duplicates;
407
408   do {
409     GList *entry;
410
411     master = duplicates = NULL;
412
413     /* Find the first set of duplicates and their master */
414     for (entry = media_types; entry != NULL && duplicates == NULL;
415         entry = entry->next) {
416       KsVideoMediaType *mt = entry->data;
417       GList *other_entry;
418
419       for (other_entry = media_types; other_entry != NULL;
420           other_entry = other_entry->next) {
421         KsVideoMediaType *other_mt = other_entry->data;
422
423         if (other_mt == mt)
424           continue;
425
426         if (gst_caps_is_equal (mt->translated_caps, other_mt->translated_caps))
427           duplicates = g_list_prepend (duplicates, other_mt);
428       }
429
430       if (duplicates != NULL)
431         master = entry;
432     }
433
434     if (duplicates != NULL) {
435       KsVideoMediaType *selected_mt = master->data;
436
437       /*
438        * Pick a FORMAT_VideoInfo2 if present, if not we just stay with the
439        * first entry
440        */
441       for (entry = duplicates; entry != NULL; entry = entry->next) {
442         KsVideoMediaType *mt = entry->data;
443
444         if (IsEqualGUID (&mt->range->Specifier, &FORMAT_VideoInfo2)) {
445           ks_video_media_type_free (selected_mt);
446           selected_mt = mt;
447         } else {
448           ks_video_media_type_free (mt);
449         }
450
451         /* Remove the dupe from the main list */
452         media_types = g_list_remove (media_types, mt);
453       }
454
455       /* Update master node with the selected MediaType */
456       master->data = selected_mt;
457
458       g_list_free (duplicates);
459     }
460   }
461   while (master != NULL);
462
463   return media_types;
464 }
465
466 GList *
467 ks_video_probe_filter_for_caps (HANDLE filter_handle)
468 {
469   GList *ret = NULL;
470   gulong pin_count;
471   guint pin_id;
472
473   if (!ks_filter_get_pin_property (filter_handle, 0, KSPROPSETID_Pin,
474           KSPROPERTY_PIN_CTYPES, &pin_count, sizeof (pin_count), NULL))
475     goto beach;
476
477   GST_DEBUG ("pin_count = %d", pin_count);
478
479   for (pin_id = 0; pin_id < pin_count; pin_id++) {
480     KSPIN_COMMUNICATION pin_comm;
481     KSPIN_DATAFLOW pin_flow;
482     GUID pin_cat;
483
484     if (!ks_filter_get_pin_property (filter_handle, pin_id, KSPROPSETID_Pin,
485             KSPROPERTY_PIN_COMMUNICATION, &pin_comm, sizeof (pin_comm), NULL))
486       continue;
487
488     if (!ks_filter_get_pin_property (filter_handle, pin_id, KSPROPSETID_Pin,
489             KSPROPERTY_PIN_DATAFLOW, &pin_flow, sizeof (pin_flow), NULL))
490       continue;
491
492     if (!ks_filter_get_pin_property (filter_handle, pin_id, KSPROPSETID_Pin,
493             KSPROPERTY_PIN_CATEGORY, &pin_cat, sizeof (pin_cat), NULL))
494       continue;
495
496     GST_DEBUG ("pin[%u]: pin_comm=%d, pin_flow=%d", pin_id, pin_comm, pin_flow);
497
498     if (pin_flow == KSPIN_DATAFLOW_OUT &&
499         memcmp (&pin_cat, &PINNAME_CAPTURE, sizeof (GUID)) == 0) {
500       KSMULTIPLE_ITEM *items;
501
502       if (ks_filter_get_pin_property_multi (filter_handle, pin_id,
503               KSPROPSETID_Pin, KSPROPERTY_PIN_DATARANGES, &items, NULL)) {
504         KSDATARANGE *range = (KSDATARANGE *) (items + 1);
505         guint i;
506
507         for (i = 0; i < items->Count; i++) {
508           if (IsEqualGUID (&range->MajorFormat, &KSDATAFORMAT_TYPE_VIDEO)) {
509             KsVideoMediaType *entry;
510             gpointer src_vscc, src_format;
511             GstStructure *media_structure;
512
513             entry = g_new0 (KsVideoMediaType, 1);
514             entry->pin_id = pin_id;
515
516             entry->range = g_malloc (range->FormatSize);
517             memcpy ((gpointer) entry->range, range, range->FormatSize);
518
519             if (IsEqualGUID (&range->Specifier, &FORMAT_VideoInfo)) {
520               KS_DATARANGE_VIDEO *vr = (KS_DATARANGE_VIDEO *) entry->range;
521
522               src_vscc = &vr->ConfigCaps;
523               src_format = &vr->VideoInfoHeader;
524
525               entry->format_size = sizeof (vr->VideoInfoHeader);
526               entry->sample_size = vr->VideoInfoHeader.bmiHeader.biSizeImage;
527             } else if (IsEqualGUID (&range->Specifier, &FORMAT_VideoInfo2)) {
528               KS_DATARANGE_VIDEO2 *vr = (KS_DATARANGE_VIDEO2 *) entry->range;
529
530               src_vscc = &vr->ConfigCaps;
531               src_format = &vr->VideoInfoHeader;
532
533               entry->format_size = sizeof (vr->VideoInfoHeader);
534               entry->sample_size = vr->VideoInfoHeader.bmiHeader.biSizeImage;
535             } else if (IsEqualGUID (&range->Specifier, &FORMAT_MPEGVideo)) {
536               /* Untested and probably wrong... */
537               KS_DATARANGE_MPEG1_VIDEO *vr =
538                   (KS_DATARANGE_MPEG1_VIDEO *) entry->range;
539
540               src_vscc = &vr->ConfigCaps;
541               src_format = &vr->VideoInfoHeader;
542
543               entry->format_size = sizeof (vr->VideoInfoHeader);
544               entry->sample_size =
545                   vr->VideoInfoHeader.hdr.bmiHeader.biSizeImage;
546             } else if (IsEqualGUID (&range->Specifier, &FORMAT_MPEG2Video)) {
547               /* Untested and probably wrong... */
548               KS_DATARANGE_MPEG2_VIDEO *vr =
549                   (KS_DATARANGE_MPEG2_VIDEO *) entry->range;
550
551               src_vscc = &vr->ConfigCaps;
552               src_format = &vr->VideoInfoHeader;
553
554               entry->format_size = sizeof (vr->VideoInfoHeader);
555               entry->sample_size =
556                   vr->VideoInfoHeader.hdr.bmiHeader.biSizeImage;
557             } else {
558               gchar *guid_str;
559
560               guid_str = ks_guid_to_string (&range->Specifier);
561               GST_DEBUG ("pin[%u]: ignoring unknown specifier GUID %s",
562                   pin_id, guid_str);
563               g_free (guid_str);
564
565               ks_video_media_type_free (entry);
566               entry = NULL;
567             }
568
569             if (entry != NULL) {
570               g_assert (entry->sample_size != 0);
571
572               memcpy ((gpointer) & entry->vscc, src_vscc, sizeof (entry->vscc));
573
574               entry->format = g_malloc (entry->format_size);
575               memcpy (entry->format, src_format, entry->format_size);
576
577               media_structure =
578                   ks_video_format_to_structure (range->SubFormat,
579                   range->MajorFormat);
580
581               if (media_structure == NULL) {
582                 g_warning ("ks_video_format_to_structure returned NULL");
583                 ks_video_media_type_free (entry);
584                 entry = NULL;
585               } else if (ks_video_append_video_stream_cfg_fields
586                   (media_structure, &entry->vscc)) {
587                 entry->translated_caps = gst_caps_new_empty ();
588                 gst_caps_append_structure (entry->translated_caps,
589                     media_structure);
590               } else {
591                 gst_structure_free (media_structure);
592                 ks_video_media_type_free (entry);
593                 entry = NULL;
594               }
595
596               if (entry != NULL)
597                 ret = g_list_prepend (ret, entry);
598             }
599           }
600
601           /* REVISIT: Each KSDATARANGE should start on a 64-bit boundary */
602           range = (KSDATARANGE *) (((guchar *) range) + range->FormatSize);
603         }
604
605         g_free (items);
606       }
607     }
608   }
609
610   if (ret != NULL) {
611     ret = g_list_reverse (ret);
612     ret = ks_video_media_type_list_remove_duplicates (ret);
613   }
614
615 beach:
616   return ret;
617 }
618
619 KSPIN_CONNECT *
620 ks_video_create_pin_conn_from_media_type (KsVideoMediaType * media_type)
621 {
622   KSPIN_CONNECT *conn = NULL;
623   KSDATAFORMAT *format = NULL;
624   guint8 *vih;
625
626   conn = g_malloc0 (sizeof (KSPIN_CONNECT) + sizeof (KSDATAFORMAT) +
627       media_type->format_size);
628
629   conn->Interface.Set = KSINTERFACESETID_Standard;
630   conn->Interface.Id = KSINTERFACE_STANDARD_STREAMING;
631   conn->Interface.Flags = 0;
632
633   conn->Medium.Set = KSMEDIUMSETID_Standard;
634   conn->Medium.Id = KSMEDIUM_TYPE_ANYINSTANCE;
635   conn->Medium.Flags = 0;
636
637   conn->PinId = media_type->pin_id;
638   conn->PinToHandle = NULL;
639   conn->Priority.PriorityClass = KSPRIORITY_NORMAL;
640   conn->Priority.PrioritySubClass = 1;
641
642   format = (KSDATAFORMAT *) (conn + 1);
643   memcpy (format, media_type->range, sizeof (KSDATAFORMAT));
644   format->FormatSize = sizeof (KSDATAFORMAT) + media_type->format_size;
645
646   vih = (guint8 *) (format + 1);
647   memcpy (vih, media_type->format, media_type->format_size);
648
649   return conn;
650 }
651
652 gboolean
653 ks_video_fixate_media_type (const KSDATARANGE * range,
654     guint8 * format, gint width, gint height, gint fps_n, gint fps_d)
655 {
656   KS_DATARANGE_VIDEO *vr;
657   KS_VIDEOINFOHEADER *vih;
658   KS_BITMAPINFOHEADER *bih;
659   DWORD dwRate;
660
661   g_return_val_if_fail (format != NULL, FALSE);
662
663   if (IsEqualGUID (&range->Specifier, &FORMAT_VideoInfo)) {
664     bih = &((KS_VIDEOINFOHEADER *) format)->bmiHeader;
665   } else if (IsEqualGUID (&range->Specifier, &FORMAT_VideoInfo2)) {
666     bih = &((KS_VIDEOINFOHEADER2 *) format)->bmiHeader;
667   } else if (IsEqualGUID (&range->Specifier, &FORMAT_MPEGVideo)) {
668     bih = &((KS_MPEG1VIDEOINFO *) format)->hdr.bmiHeader;
669   } else if (IsEqualGUID (&range->Specifier, &FORMAT_MPEG2Video)) {
670     bih = &((KS_MPEGVIDEOINFO2 *) format)->hdr.bmiHeader;
671   } else {
672     return FALSE;
673   }
674
675   /* These formats' structures share the most basic stuff */
676   vr = (KS_DATARANGE_VIDEO *) range;
677   vih = (KS_VIDEOINFOHEADER *) format;
678
679   /* FIXME: Need to figure out how to properly handle ranges */
680   if (bih->biWidth != width || bih->biHeight != height)
681     return FALSE;
682
683   /* Framerate, clamped because of fraction conversion rounding errors */
684   vih->AvgTimePerFrame =
685       gst_util_uint64_scale_int_round (NANOSECONDS, fps_d, fps_n);
686   vih->AvgTimePerFrame =
687       MAX (vih->AvgTimePerFrame, vr->ConfigCaps.MinFrameInterval);
688   vih->AvgTimePerFrame =
689       MIN (vih->AvgTimePerFrame, vr->ConfigCaps.MaxFrameInterval);
690
691   /* Bitrate, clamped for the same reason as framerate */
692   dwRate = (width * height * fps_n) / fps_d;
693   vih->dwBitRate = dwRate * bih->biBitCount;
694   vih->dwBitRate = MAX (vih->dwBitRate, vr->ConfigCaps.MinBitsPerSecond);
695   vih->dwBitRate = MIN (vih->dwBitRate, vr->ConfigCaps.MaxBitsPerSecond);
696
697   return TRUE;
698 }
699
700 static GstStructure *
701 ks_video_append_var_video_fields (GstStructure * structure)
702 {
703   if (structure) {
704     gst_structure_set (structure,
705         "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
706         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
707         "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
708   }
709
710   return structure;
711 }
712
713 GstCaps *
714 ks_video_get_all_caps (void)
715 {
716   static GstCaps *caps = NULL;
717
718   if (caps == NULL) {
719     GstStructure *structure;
720     caps = gst_caps_new_empty ();
721
722     /* from Windows SDK 6.0 uuids.h */
723     /* RGB formats */
724     structure =
725         ks_video_append_var_video_fields (ks_video_format_to_structure
726         (MEDIASUBTYPE_RGB555, FORMAT_VideoInfo));
727     gst_caps_append_structure (caps, structure);
728
729     structure =
730         ks_video_append_var_video_fields (ks_video_format_to_structure
731         (MEDIASUBTYPE_RGB565, FORMAT_VideoInfo));
732     gst_caps_append_structure (caps, structure);
733
734     structure =
735         ks_video_append_var_video_fields (ks_video_format_to_structure
736         (MEDIASUBTYPE_RGB24, FORMAT_VideoInfo));
737     gst_caps_append_structure (caps, structure);
738
739     structure =
740         ks_video_append_var_video_fields (ks_video_format_to_structure
741         (MEDIASUBTYPE_RGB32, FORMAT_VideoInfo));
742     gst_caps_append_structure (caps, structure);
743
744     /* YUV formats */
745     structure =
746         ks_video_append_var_video_fields (gst_structure_new ("video/x-raw-yuv",
747             NULL));
748     gst_caps_append_structure (caps, structure);
749
750     /* Other formats */
751     structure =
752         ks_video_append_var_video_fields (ks_video_format_to_structure
753         (MEDIASUBTYPE_MJPG, FORMAT_VideoInfo));
754     gst_caps_append_structure (caps, structure);
755
756     structure =
757         ks_video_append_var_video_fields (ks_video_format_to_structure
758         (MEDIASUBTYPE_dvsd, FORMAT_VideoInfo));
759     gst_caps_append_structure (caps, structure);
760
761     structure =                 /* no variable video fields (width, height, framerate) */
762         ks_video_format_to_structure (MEDIASUBTYPE_dvsd, FORMAT_DvInfo);
763     gst_caps_append_structure (caps, structure);
764   }
765
766   return caps;
767 }